train.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import copy
  2. import time
  3. import torch
  4. import torch.optim as optim
  5. from torch import nn
  6. # TODO: get these properly
  7. device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  8. feature_extract = True
  9. model_ft = None # TODO
  10. dataloaders_dict = None # TODO
  11. model_name = None # TODO
  12. def train_model(model, dataloaders, criterion, optimizer, num_epochs=25, is_inception=False):
  13. since = time.time()
  14. val_acc_history = []
  15. best_model_wts = copy.deepcopy(model.state_dict())
  16. best_acc = 0.0
  17. # Detect if we have a GPU available
  18. device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  19. for epoch in range(num_epochs):
  20. print('Epoch {}/{}'.format(epoch, num_epochs - 1))
  21. print('-' * 10)
  22. # Each epoch has a training and validation phase
  23. for phase in ['train', 'val']:
  24. if phase == 'train':
  25. model.train() # Set model to training mode
  26. else:
  27. model.eval() # Set model to evaluate mode
  28. running_loss = 0.0
  29. running_corrects = 0
  30. # Iterate over data.
  31. for inputs, labels in dataloaders[phase]:
  32. inputs = inputs.to(device)
  33. labels = labels.to(device)
  34. # zero the parameter gradients
  35. optimizer.zero_grad()
  36. # forward
  37. # track history if only in train
  38. with torch.set_grad_enabled(phase == 'train'):
  39. # Get model outputs and calculate loss
  40. # Special case for inception because in training it has an auxiliary output. In train
  41. # mode we calculate the loss by summing the final output and the auxiliary output
  42. # but in testing we only consider the final output.
  43. if is_inception and phase == 'train':
  44. # https://discuss.pytorch.org/t/how-to-optimize-inception-model-with-auxiliary-classifiers/7958
  45. outputs, aux_outputs = model(inputs)
  46. loss1 = criterion(outputs, labels)
  47. loss2 = criterion(aux_outputs, labels)
  48. loss = loss1 + 0.4 * loss2
  49. else:
  50. outputs = model(inputs)
  51. loss = criterion(outputs, labels)
  52. _, preds = torch.max(outputs, 1)
  53. # backward + optimize only if in training phase
  54. if phase == 'train':
  55. loss.backward()
  56. optimizer.step()
  57. # statistics
  58. running_loss += loss.item() * inputs.size(0)
  59. running_corrects += torch.sum(preds == labels.data)
  60. epoch_loss = running_loss / len(dataloaders[phase].dataset)
  61. epoch_acc = running_corrects.double() / len(dataloaders[phase].dataset)
  62. print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc))
  63. # deep copy the model
  64. if phase == 'val' and epoch_acc > best_acc:
  65. best_acc = epoch_acc
  66. best_model_wts = copy.deepcopy(model.state_dict())
  67. if phase == 'val':
  68. val_acc_history.append(epoch_acc)
  69. print()
  70. time_elapsed = time.time() - since
  71. print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
  72. print('Best val Acc: {:4f}'.format(best_acc))
  73. # load best model weights
  74. model.load_state_dict(best_model_wts)
  75. return model, val_acc_history
  76. # Send the model to GPU
  77. model_ft = model_ft.to(device)
  78. # Gather the parameters to be optimized/updated in this run. If we are
  79. # finetuning we will be updating all parameters. However, if we are
  80. # doing feature extract method, we will only update the parameters
  81. # that we have just initialized, i.e. the parameters with requires_grad
  82. # is True.
  83. params_to_update = model_ft.parameters()
  84. print("Params to learn:")
  85. if feature_extract:
  86. params_to_update = []
  87. for name, param in model_ft.named_parameters():
  88. if param.requires_grad is True:
  89. params_to_update.append(param)
  90. print("\t", name)
  91. else:
  92. for name, param in model_ft.named_parameters():
  93. if param.requires_grad is True:
  94. print("\t", name)
  95. # Observe that all parameters are being optimized
  96. optimizer_ft = optim.SGD(params_to_update, lr=0.001, momentum=0.9)
  97. # Setup the loss fxn
  98. criterion = nn.CrossEntropyLoss()
  99. # Train and evaluate
  100. model_ft, hist = train_model(model_ft, dataloaders_dict, criterion, optimizer_ft, num_epochs=num_epochs,
  101. is_inception=(model_name == "inception"))