def test_model(model,optimizer,trial): since = time.time() model.eval() # Iterate over data. for index, (inputs, labels) in enumerate(dataloaders_test): inputs = inputs.to(device) labels = labels.to(device) inputs = inputs.float() labels = labels.float() # zero the parameter gradients optimizer.zero_grad() # forward # track history if only in train with torch.set_grad_enabled(False): outputs = model(inputs) # backward + optimize only if in training phase if index == 0: outputs_test = outputs labels_test = labels else: outputs_test = torch.cat((outputs_test, outputs), 0) labels_test = torch.cat((labels_test, labels), 0) cmap, emap = cemap_cal(outputs_test.to(torch.device("cpu")).numpy(), labels_test.to(torch.device("cpu")).numpy()) print('Test:') print(cmap,emap) p, r, f = prf_cal(outputs_test.to(torch.device("cpu")).numpy(), labels_test.to(torch.device("cpu")).numpy(), 3) time_elapsed = time.time() - since print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60)) scipy.io.savemat('./results/resnet101_model1fc_results_'+str(trial)+'imgSize'+str(img_size)+'.mat', mdict={'cmap': cmap, 'emap': emap, 'p': p,'r': r, 'f': f, 'scores': outputs_test.to(torch.device("cpu")).numpy()})
def train_model(model, criterion, optimizer, scheduler, num_epochs=25): since = time.time() best_model_wts = copy.deepcopy(model.state_dict()) best_emap = 0 for epoch in range(num_epochs): scheduler.step() print('Epoch {}/{}'.format(epoch, num_epochs - 1)) print('-' * 10) for param_group in optimizer.param_groups: cur_lr = param_group['lr'] print('Current learning rate: ' + '%.5f' % cur_lr) if cur_lr < 0.00001: break # Each epoch has a training and validation phase for phase in ['train']: if phase == 'train': model.train() # Set model to training mode else: model.eval() # Set model to evaluate mode running_loss = 0.0 running_corrects = 0 # Iterate over data. for index, (inputs, labels) in enumerate(dataloaders[phase]): inputs = inputs.to(device) labels = labels.to(device) # mixed up inputs = inputs.float() labels = labels.float() labels_tr = labels.clone() labels_tr[labels == 0] = 1 labels_tr[labels == -1] = 0 #if epoch % 2 == 0: # inputs,labels_tr = mixup_data3(inputs,labels_tr,use_cuda=True) # zero the parameter gradients optimizer.zero_grad() # forward # track history if only in train with torch.set_grad_enabled(phase == 'train'): outputs = model(inputs) loss = criterion(outputs, labels_tr) # loss = newHinge_rank_loss(outputs,labels) # backward + optimize only if in training phase if phase == 'train': loss.backward() optimizer.step() if phase == 'val': if index == 0: outputs_val = outputs labels_val = labels else: outputs_val = torch.cat((outputs_val, outputs), 0) labels_val = torch.cat((labels_val, labels), 0) running_loss += loss.item() * inputs.size(0) if phase == 'train': epoch_loss = running_loss / dataset_sizes['trainval'] else: epoch_loss = running_loss / dataset_sizes['val'] if phase == 'val': cmap, emap = cemap_cal( outputs_val.to(torch.device("cpu")).numpy(), labels_val.to(torch.device("cpu")).numpy()) p, r, f = prf_cal( outputs_val.to(torch.device("cpu")).numpy(), labels_val.to(torch.device("cpu")).numpy(), 3) val_loss = epoch_loss print('{} Loss: {:.4f}'.format(phase, epoch_loss)) # deep copy the model if phase == 'val' and cmap > best_emap: best_emap = cmap print(cmap, emap) print(p, r, f) test_model(model, optimizer, trial) #best_model_wts = copy.deepcopy(model.state_dict()) #torch.save(model_ft.state_dict(),'./resnet101_model1_trial0.pt') print() time_elapsed = time.time() - since print('Training complete in {:.0f}m {:.0f}s'.format( time_elapsed // 60, time_elapsed % 60)) # load best model weights model.load_state_dict(best_model_wts) return model