Advertisement
lamiastella

loocv fine-tuning

Nov 21st, 2018
599
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from __future__ import print_function, division
  2.  
  3. import torch
  4. from torch.autograd import Variable
  5. from sklearn.metrics import accuracy_score
  6. from sklearn.metrics import confusion_matrix
  7. import torch
  8. import torch.nn as nn
  9. import torch.optim as optim
  10. from torch.optim import lr_scheduler
  11. import numpy as np
  12. import torchvision
  13. from torchvision import datasets, models, transforms
  14. import matplotlib.pyplot as plt
  15. import time
  16. import os
  17. import copy
  18.  
  19.  
  20.  
  21. import torch.utils.data as data_utils
  22. from torch.utils import data
  23.  
  24.  
  25. data_transforms = {
  26.     'train': transforms.Compose([
  27.         transforms.RandomResizedCrop(224),
  28.         transforms.RandomHorizontalFlip(),
  29.         transforms.RandomRotation(20),
  30.         transforms.ToTensor(),
  31.         transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
  32.     ])
  33. }
  34.  
  35.  
  36. data_dir = "test_images"
  37.  
  38. device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  39.  
  40.  
  41.  
  42. def train_model(model, criterion, optimizer, scheduler, train_input, train_label, num_epochs=25):
  43.     since = time.time()
  44.  
  45.  
  46.     for epoch in range(num_epochs):
  47.         print('Epoch {}/{}'.format(epoch, num_epochs - 1))
  48.         print('-' * 10)
  49.  
  50.  
  51.         scheduler.step()
  52.         model.train()  # Set model to training mode
  53.  
  54.         running_loss = 0.0
  55.         running_corrects = 0
  56.  
  57.         # Iterate over data.
  58.         train_input = train_input.to(device)
  59.         train_label = train_label.to(device)
  60.  
  61.         # zero the parameter gradients
  62.         optimizer.zero_grad()
  63.  
  64.         # forward
  65.         # track history if only in train
  66.         with torch.set_grad_enabled(True):
  67.             output = model(train_input)
  68.             _, pred = torch.max(output, 1)
  69.             loss = criterion(output, train_label)
  70.  
  71.             # backward + optimize only if in training phase
  72.            
  73.             loss.backward()
  74.             optimizer.step()
  75.  
  76.         # statistics
  77.         running_loss += loss.item() * train_input.size(0)
  78.         running_corrects += torch.sum(pred == train_label.data)
  79.  
  80.         epoch_loss = running_loss / dataset_size['train']
  81.         epoch_acc = running_corrects.double() / dataset_size['train']
  82.  
  83.         print('train Loss: {:.4f} Acc: {:.4f}'.format(epoch_loss, epoch_acc))
  84.  
  85.         print()
  86.  
  87.     time_elapsed = time.time() - since
  88.     print('Training complete in {:.0f}m {:.0f}s'.format(
  89.         time_elapsed // 60, time_elapsed % 60))
  90.  
  91.     return model
  92.  
  93.  
  94.  
  95. ######################################################################
  96. # Finetuning the convnet
  97. # ----------------------
  98. #
  99. # Load a pretrained model and reset final fully connected layer.
  100. #
  101.  
  102. model_ft = models.resnet50(pretrained=True)
  103.  
  104. num_ftrs = model_ft.fc.in_features
  105. model_ft.fc = nn.Linear(num_ftrs, 2)
  106.  
  107. model_ft = model_ft.to(device)
  108.  
  109. criterion = nn.CrossEntropyLoss()
  110.  
  111. # Observe that all parameters are being optimized
  112. optimizer_ft = optim.SGD(model_ft.parameters(), lr=0.001, momentum=0.9)
  113.  
  114. # Decay LR by a factor of 0.1 every 7 epochs
  115. exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)
  116.  
  117.  
  118.  
  119. #model_ft = model_ft.cuda()
  120. nb_samples = 10
  121. nb_classes = 2
  122.  
  123.  
  124.  
  125. image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),
  126.                                           data_transforms[x])
  127.                   for x in ['train']}
  128.  
  129. dataset_size = {x: len(image_datasets[x]) for x in ['train']}
  130. class_names = image_datasets['train'].classes
  131.  
  132. # LOOCV
  133. loocv_preds = []
  134. loocv_targets = []
  135. for idx in range(nb_samples):
  136.  
  137.     print('Using sample {} as test data'.format(idx))
  138.  
  139.     # Get all indices and remove test sample
  140.     train_indices = list(range(len(image_datasets['train'])))
  141.     del train_indices[idx]
  142.  
  143.     # Create new sampler
  144.     sampler = data.SubsetRandomSampler(train_indices)
  145.  
  146.     dataloader = data.DataLoader(
  147.         image_datasets['train'],
  148.         num_workers=2,
  149.         batch_size=1,
  150.         sampler=sampler
  151.     )
  152.  
  153.     # Train model
  154.     for batch_idx, (sample, target) in enumerate(dataloader):
  155.         print('Batch {}'.format(batch_idx))
  156.         model_ft = train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler, sample, target, num_epochs=2) # do I add this line here?
  157.  
  158.     # Test on LOO sample
  159.     model_ft.eval()
  160.     test_data, test_target = image_datasets['train'][idx]
  161.     test_data = test_data.cuda()
  162.     test_target = torch.tensor(test_target)
  163.     test_target = test_target.cuda()
  164.     test_data.unsqueeze_(0)
  165.     test_target.unsqueeze_(0)
  166.     print(test_data.shape)
  167.     output = model_ft(test_data)
  168.     pred = torch.argmax(output, 1)
  169.     loocv_preds.append(pred)
  170.     loocv_targets.append(test_target.item())
  171.  
  172.  
  173. print("loocv preds: ", loocv_preds)
  174. print("loocv targets: ", loocv_targets)
  175. print(accuracy_score(loocv_targets, loocv_preds))
  176. print(confusion_matrix(loocv_targets, loocv_preds))
Advertisement
RAW Paste Data Copied
Advertisement