I’m trying to train a Pneumonia classifier using Resnet34. While training the model, the loss is increasing and accuracy is decreasing drastically (both in training and validation sets). What might be the potential reason behind this?
def train(model, dataloaders, loss, optimizer, epochs=5):

  train = dataloaders['train']

  valid = dataloaders['valid']

  device = 'cuda' if torch.cuda.is_available() else 'cpu'

  metric = Accuracy().to(device)

  for epoch in tqdm(range(epochs), desc="EPOCHS : "):

    model.train()

    cst = 0

    for x, y in tqdm(train, leave=True, desc="Trainning : "):

      optimizer.zero_grad()

      x = x.to(device)

      y = y.to(device)

      preds = model(x).to(device)

      acc = metric(preds.argmax(dim=1), y)

      cost = loss(preds, y)

      cst += cost.item()

      cost.backward()

      optimizer.step()

    acc = metric.compute()

    cst /= len(train)

    print(f'Train loss : {cst} \t Train acc : {acc}')

    model.eval()

    cst = 0

    for x, y in tqdm(valid, leave=True, desc="Validation : "):

      x = x.to(device)

      y = y.to(device)

      preds = model(x).to(device)

      acc = metric(preds.argmax(dim=1), y)

      cost = loss(preds, y)

      cst += cost.item()

    acc = metric.compute()

    cst /= len(valid)

    print(f'Valid loss : {cst} \t Valid acc : {acc}')

  return model

model = models.resnet34(pretrained=True)

for param in model.parameters():

  param.requires_grad = False

model.fc = nn.Sequential(

    nn.Dropout(p=.7),

    nn.Linear(in_features=model.fc.in_features, out_features=2),

    nn.LogSoftmax(dim=1)

)

model = model.to(device)

LR = 3e-3

WD = 1e-4

loss = nn.NLLLoss()

optimizer = optim.Adam(model.parameters(), lr=LR, weight_decay=WD)

md = train(model, dataloaders, loss, optimizer, epochs=5)


Screenshot (1)1920×1080 115 KB