Weird reults in training AlexNet

Neda: <p>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.<br>
I tried different learning rate but has not been improved.</p>
<p>Here is the snippet for the training.</p>
<pre><code class="lang-auto">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))
</code></pre>
<pre><code class="lang-auto">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
</code></pre>

sai_tharun: <p>what is your input?  Images?</p>
<p>and could you please write the clean code,could you tell me what you are trying to do</p>

Neda: <p>Input is Echocardiography images (png file). size of images is 227*227</p>
<p>Basically it’s classification task. Dataset have 15 classes, and each image can have one class.</p>

sai_tharun: <p>Try normalising the inputs</p>
<pre><code class="lang-auto">transform_train = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225]),
])
</code></pre>

Neda: <p><a class="mention" href="/u/sai_tharun">@sai_tharun</a> I did it, but didn’t help. It gets worse, zero accuracy for validation.</p>
<p>Do I need to get <code>outputs = F.softmax(outputs, dim=1)</code> the place I am doing or should be after last layer in the model? am I calculating accuracy correctly?</p>

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