Weird reults in training AlexNet

I’m trying to train a dataset with AlexNet model. The task is multiclass classification (15 classes). I am wondering why I am getting very low accuracy.
I tried different learning rate but has not been improved.

Here is the snippet for the training.

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=1e-3, momentum=0.9)  

def train_valid_model():
    
    num_epochs=5
                          
    for epoch in range(num_epochs):  
        print('Epoch {}/{}'.format(epoch, num_epochs - 1))
               
        for phase in ['train', 'valid', 'test']:
            if phase == 'train':
                
                model.train()  
            else:
                model.eval()   
        
            train_loss = 0.0
            total_train = 0
            correct_train = 0

            for t_image, target, image_path in dataLoaders[phase]:
                                
                t_image = t_image.to(device)
                target = target.to(device)

                optimizer.zero_grad()
                
                with torch.set_grad_enabled(phase == 'train'):
                    outputs = model(t_image) 
                    outputs = F.softmax(outputs, dim=1)
                     
                    loss = criterion(outputs,target)         
                    if phase == 'train':
                        loss.backward() 
                        optimizer.step()                           
            
                _, predicted = torch.max(outputs.data, 1)
                train_loss += loss.item()* t_image.size(0)
                correct_train += (predicted == target).sum().item()
                
            epoch_loss = train_loss / len(dataLoaders[phase].dataset)
            epoch_acc = 100 * correct_train / len(dataLoaders[phase].dataset) 
                                             
            print('{} Loss: {:.4f} {} Acc: {:.4f}'.format(phase, epoch_loss, phase, epoch_acc))
Epoch 0/4
train Loss: 2.7026 train Acc: 17.2509
valid Loss: 2.6936 valid Acc: 28.7632
test Loss: 2.6936 test Acc: 28.7632

Epoch 1/4
train Loss: 2.6425 train Acc: 17.8019
valid Loss: 2.6357 valid Acc: 28.7632
test Loss: 2.6355 test Acc: 28.7632

what is your input? Images?

and could you please write the clean code,could you tell me what you are trying to do

Input is Echocardiography images (png file). size of images is 227*227

Basically it’s classification task. Dataset have 15 classes, and each image can have one class.

Try normalising the inputs

transform_train = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225]),
])

@sai_tharun I did it, but didn’t help. It gets worse, zero accuracy for validation.

Do I need to get outputs = F.softmax(outputs, dim=1) the place I am doing or should be after last layer in the model? am I calculating accuracy correctly?

if you are using crossentropy loss,no need to do the softmax function for the output,it will be done by crossentropy loss itself.
try removing the softmax function and train the model