Resnet 50 on fashionmnist not getting trained

pabowi4384: <p>i am trying to train resnet50 from scratch for 100 epochs. using sgd(lr=0.1,weightdecay=0.0001,momentum=0.9) as optimizer and scheduler to decay lr by factor of 10 after every 30 epochs. batch size is 128.</p>
<p>import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
import torchvision<br>
from torchvision import transforms as transforms<br>
import torch.utils.data</p>
<p>transform = transforms.Compose([transforms.Resize(224), transforms.ToTensor()])</p>
<p>train_fmnist_dataset = torchvision.datasets.FashionMNIST(root=‘./’, train=True, transform=transform,download=True)<br>
test_fmnist_dataset = torchvision.datasets.FashionMNIST(root=‘./’, train=False, transform=transform,download=True)</p>
<p>train_data_loader = torch.utils.data.DataLoader(train_fmnist_dataset, batch_size=128, shuffle=True,num_workers=2,pin_memory=True)<br>
test_data_loader = torch.utils.data.DataLoader(test_fmnist_dataset, batch_size=128, shuffle=False,num_workers=2,pin_memory=True)</p>
<p>from tqdm import tqdm</p>
<p>def train_epoch(model, criterion, optimizer, scheduler, train_loader, epoch, device):<br>
loss_train = 0.<br>
acc_train = 0.<br>
ntrain = len(train_loader.dataset)</p>
<p>with tqdm(train_loader, unit=‘batches’) as pbar:<br>
for i, (images, labels) in enumerate(pbar):<br>
pbar.set_description(f"Epoch {epoch}")<br>
images = images.to(device)<br>
labels = labels.to(device).to(torch.long).view(-1)</p>
<pre><code>  # train and back-propagate
  optimizer.zero_grad()
  output = model(images)
  loss = criterion(output, labels)
  loss.backward()
  optimizer.step()
  scheduler.step()

  # train accuracy
  with torch.no_grad():
    probs = F.softmax(output, dim=1).cpu()
    preds = torch.max(probs, dim=1)[1]
    acc_train += (preds == labels.cpu()).sum()
    loss_train += loss.item()

  if i &lt; len(train_loader) - 1:
    denom = images.shape[0] * (i+1)
  else:
    denom = ntrain

  pbar.set_postfix(train_loss=loss_train/denom, train_acc=acc_train/denom)
</code></pre>
<p>def validate(model, criterion, val_loader, device):<br>
acc_val = 0.<br>
loss_val = 0.<br>
nval = len(val_loader.dataset)<br>
with torch.no_grad():<br>
for val_images, val_labels in val_loader:<br>
val_images = val_images.to(device)<br>
val_labels = val_labels.to(device).to(torch.long).view(-1)</p>
<pre><code>  val_output = model(val_images)
  val_loss = criterion(val_output, val_labels)
  loss_val += val_loss.item()

  probs = F.softmax(val_output, dim=1).cpu()
  preds = torch.max(probs, dim=1)[1]
  acc_val += (preds == val_labels.cpu()).sum()
</code></pre>
<p>return loss_val/nval, acc_val/nval</p>
<p>def train(model, criterion, optimizer, scheduler, train_loader, device):<br>
for epoch in range(100):<br>
train_epoch(model, criterion, optimizer, scheduler, train_loader, epoch, device)</p>
<p>from torchvision.models import resnet50</p>
<p>m1=resnet50()<br>
m1.conv1=nn.Conv2d(1,64,kernel_size=7,stride=2,padding=3,bias=False)<br>
for module in m1.modules():<br>
if isinstance(module, nn.BatchNorm2d):<br>
module.momentum=0.9<br>
m1.fc=nn.Linear(in_features=2048,out_features=10)<br>
m1.layer2[0].conv1.stride,m1.layer2[0].conv2.stride=2,1<br>
m1.layer3[0].conv1.stride,m1.layer3[0].conv2.stride=2,1<br>
m1.layer4[0].conv1.stride,m1.layer4[0].conv2.stride=2,1</p>
<p>import torch.optim as optim<br>
from torch.optim import lr_scheduler<br>
criterion = nn.CrossEntropyLoss(reduction=‘sum’)<br>
optimizer = optim.SGD(params=m1.parameters(), lr=0.1,momentum=0.9,weight_decay=0.0001)<br>
scheduler=lr_scheduler.StepLR(optimizer,step_size=30,gamma=0.1)<br>
device = ‘cuda’ if torch.cuda.is_available() else ‘cpu’<br>
m1=m1.to(device)<br>
criterion=criterion.to(device)</p>
<p>m1.train()<br>
train(m1, criterion, optimizer, scheduler, train_data_loader, device)<br>
torch.save(m1.state_dict(), “./m1”)<br>
m1.eval()<br>
e,a1=validate(m1,criterion,test_data_loader,device)<br>
print(a1)</p>
<p>Epoch 0: 100%|██████████| 469/469 [04:13&lt;00:00,  1.85batches/s, train_acc=tensor(0.1012), train_loss=31.4]<br>
Epoch 1: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1010), train_loss=2.44]<br>
Epoch 2: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1015), train_loss=2.45]<br>
Epoch 3: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1015), train_loss=2.47]<br>
Epoch 4: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1019), train_loss=2.46]<br>
Epoch 5: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1015), train_loss=2.46]<br>
Epoch 6: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1012), train_loss=2.49]<br>
Epoch 7: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1014), train_loss=2.48]<br>
Epoch 8: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1018), train_loss=2.45]<br>
Epoch 9: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1017), train_loss=2.44]<br>
Epoch 10: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1014), train_loss=2.48]<br>
Epoch 11: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1014), train_loss=2.45]<br>
Epoch 12: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1015), train_loss=2.47]<br>
Epoch 13: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1015), train_loss=2.46]<br>
Epoch 14: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1016), train_loss=2.44]<br>
Epoch 15: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1013), train_loss=2.44]<br>
Epoch 16: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1013), train_loss=2.44]<br>
Epoch 17: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1014), train_loss=2.45]<br>
Epoch 18: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1017), train_loss=2.44]<br>
Epoch 19: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1016), train_loss=2.45]<br>
Epoch 20: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1018), train_loss=2.44]<br>
Epoch 21: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1017), train_loss=2.45]<br>
Epoch 22: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1014), train_loss=2.45]<br>
Epoch 23: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1010), train_loss=2.45]<br>
Epoch 24: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1016), train_loss=2.44]<br>
Epoch 25: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1017), train_loss=2.44]<br>
Epoch 26: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1014), train_loss=2.45]<br>
Epoch 27: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1013), train_loss=2.46]<br>
Epoch 28: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1015), train_loss=2.46]<br>
Epoch 29: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1015), train_loss=2.43]<br>
Epoch 30: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1016), train_loss=2.45]<br>
Epoch 31: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1016), train_loss=2.44]<br>
Epoch 32: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1015), train_loss=2.45]<br>
Epoch 33: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1012), train_loss=2.46]<br>
Epoch 34: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1012), train_loss=2.44]<br>
Epoch 35: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1017), train_loss=2.45]<br>
Epoch 36: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1016), train_loss=2.48]<br>
Epoch 37: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1013), train_loss=2.45]<br>
Epoch 38: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1016), train_loss=2.45]<br>
Epoch 39: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1017), train_loss=2.45]<br>
Epoch 40: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1015), train_loss=2.47]<br>
Epoch 41: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1017), train_loss=2.45]<br>
Epoch 42: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1017), train_loss=2.46]<br>
Epoch 43: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1015), train_loss=2.45]<br>
Epoch 44: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1018), train_loss=2.44]<br>
Epoch 45: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1018), train_loss=2.46]<br>
Epoch 46: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1017), train_loss=2.46]<br>
Epoch 47: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1013), train_loss=2.45]<br>
Epoch 48: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1018), train_loss=2.46]<br>
Epoch 49: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1016), train_loss=2.45]<br>
Epoch 50: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1012), train_loss=2.44]<br>
Epoch 51: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1014), train_loss=2.45]<br>
Epoch 52: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1014), train_loss=2.42]<br>
Epoch 53: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1018), train_loss=2.47]<br>
Epoch 54: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1015), train_loss=2.45]<br>
Epoch 55: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1014), train_loss=2.45]<br>
Epoch 56: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1015), train_loss=2.47]<br>
Epoch 57: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1016), train_loss=2.47]<br>
Epoch 58: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1016), train_loss=2.43]<br>
Epoch 59: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1018), train_loss=2.44]<br>
Epoch 60: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1015), train_loss=2.45]<br>
Epoch 61: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1015), train_loss=2.46]<br>
Epoch 62: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1014), train_loss=2.44]<br>
Epoch 63: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1015), train_loss=2.44]<br>
Epoch 64: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1014), train_loss=2.44]<br>
Epoch 65: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1012), train_loss=2.46]<br>
Epoch 66: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1014), train_loss=2.43]<br>
Epoch 67: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1014), train_loss=2.46]<br>
Epoch 68: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1018), train_loss=2.45]<br>
Epoch 69: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1012), train_loss=2.45]<br>
Epoch 70: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1011), train_loss=2.45]<br>
Epoch 71: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1015), train_loss=2.47]<br>
Epoch 72: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1014), train_loss=2.45]<br>
Epoch 73: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1013), train_loss=2.44]<br>
Epoch 74: 100%|██████████| 469/469 [04:07&lt;00:00,  1.90batches/s, train_acc=tensor(0.1013), train_loss=2.45]<br>
Epoch 75: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1016), train_loss=2.45]<br>
Epoch 76: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1015), train_loss=2.44]<br>
Epoch 77: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1011), train_loss=2.46]<br>
Epoch 78: 100%|██████████| 469/469 [04:07&lt;00:00,  1.89batches/s, train_acc=tensor(0.1011), train_loss=2.44]<br>
Epoch 79:  31%|███       | 145/469 [01:17&lt;02:52,  1.87batches/s, train_acc=tensor(0.1029), train_loss=2.41]</p>
<p>but model not getting trained even after 79 epochs.<br>
i am running this on kaggle’s p100 gpu.</p>

bloos: <p>there are a lot of reasons why a neural network might not converge. The most common ones in my experience are</p>
<ul>
<li>bad input normalization and</li>
<li>exploding/vanishing gradients</li>
</ul>
<p>I don’t know the torchvision.dataset.FashionMNIST dataset. Is it normalized by default? If not, you should change it. In the worst case it is between 0 and 256, what would be a bad input for resnet.</p>
<p>For the exploding/vanishing gradient part: You start with a learning rate of 0.1 which is quite high. It is possible that you catapult your weights into saturation from where they are not able to recover. There are several things you can try to fix this. The simplest one is starting with a learning rate of 1e-4 or even 1e-5. Gradient clipping can also be worth a try</p>

pabowi4384: <p>i used transforms.totensor(). I read that it normalizes input to be in range [0,1] though i did not check formally</p>

pabowi4384: <p>Actually i am trying to implement “delving into the estimation shift on batch normalization in a network” paper. In that the configuration is given for sgd and scheduler. But the authors used imagenet. I wanted to first try on fashion mnist and cifar 10</p>

bloos: <p>you seem to be right. ToTensor normalizes the tensors to be in [0,1]. Nonetheless, resnet was trained on rescaled data that had a mean 0 and standard deviation of 1. You should calculate the statistics of your dataset and correct for these terms.</p>
<p>Did your paper write something about gradient clipping? Sadly, machine learning papers are often very vague when it comes to the important details.<br>
Your loss jumps to 31.4 in the first epoch. This is a huge indication for me that something is exploding in your network.<br>
This is still a guess. If you want to be sure you could also log the L2 norm of your gradients</p>

ptrblck: <p>It seems you are changing the <code>momentum</code> in all batchnorm layers from the default <code>0.1</code> to <code>0.9</code>. It doesn’t explain the training failure but are you sure you are not using another definition of the <code>momentum</code> term?<br>
Besides that you could try to overfit a tiny subset, e.g. just 10 samples, first to make sure your model is able to do so.</p>