Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.71 KB | None | 0 0
  1. !pip3 install 'torch==1.3.1'
  2. !pip3 install 'torchvision==0.4.2'
  3. !pip3 install 'Pillow-SIMD'
  4. !pip3 install 'tqdm'
  5.  
  6.  
  7.  
  8. import os
  9. import logging
  10.  
  11. import torch
  12. import torch.nn as nn
  13. import torch.optim as optim
  14. from torch.utils.data import Subset, DataLoader, Dataset
  15. from torch.backends import cudnn
  16.  
  17. import math
  18.  
  19. import torchvision
  20. from torchvision import transforms
  21. from torchvision.models import alexnet
  22.  
  23. from PIL import Image
  24. from tqdm import tqdm
  25. from torchvision.datasets import VisionDataset
  26.  
  27. import warnings
  28. warnings.filterwarnings("ignore")
  29.  
  30. import matplotlib.pyplot as plt
  31.  
  32.  
  33. DEVICE = 'cuda' # 'cuda' or 'cpu'
  34.  
  35. NUM_CLASSES = 101 # 101 + 1: There is am extra Background class that should be removed
  36.  
  37. BATCH_SIZE = 256     # Higher batch sizes allows for larger learning rates. An empirical heuristic suggests that, when changing
  38.                      # the batch size, learning rate should change by the same factor to have comparable results
  39.  
  40. LR = 0.01 # [0.01,0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.09,0.1] # The initial Learning Rate
  41.  
  42. from random import seed
  43. from random import random
  44.  
  45.  
  46. seed(42) # because the reply of the foundamental question is 42
  47.  
  48.  
  49.  
  50. list_accuracy_param = []
  51.  
  52. for i in range(0,1):
  53.  
  54.   MOMENTUM = 0.9       # Hyperparameter for SGD, keep this at 0.9 when using SGD
  55.   WEIGHT_DECAY = 5e-5  # Regularization, you can keep this at the default
  56.  
  57.   NUM_EPOCHS_O = 15    # Total number of training epochs (iterations over dataset)
  58.   STEP_SIZE_O = 12      # How many epochs before decreasing learning rate (if using a step-down policy)
  59.   GAMMA = 0.1          # Multiplicative factor for learning rate step-down
  60.  
  61.   LOG_FREQUENCY = 10
  62.  
  63.   STEP_SIZE = STEP_SIZE_O
  64.   NUM_EPOCHS = NUM_EPOCHS_O
  65.  
  66.   print(("running with step_size: %f, and num epochs: %d") % (STEP_SIZE,NUM_EPOCHS))
  67.  
  68.   # Define transforms for training phase
  69.   train_transform = transforms.Compose([transforms.Resize(256),      # Resizes short size of the PIL image to 256
  70.                                         transforms.CenterCrop(224),  # Crops a central square patch of the image
  71.                                                                     # 224 because torchvision's AlexNet needs a 224x224 input!
  72.                                                                     # Remember this when applying different transformations, otherwise you get an error
  73.                                         transforms.ToTensor(), # Turn PIL Image to torch.Tensor
  74.                                         transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) # Normalizes tensor with mean and standard deviation
  75.   ])
  76.   # Define transforms for the evaluation phase
  77.   eval_transform = transforms.Compose([transforms.Resize(256),
  78.                                         transforms.CenterCrop(224),
  79.                                         transforms.ToTensor(),
  80.                                         transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))                                    
  81.   ])
  82.  
  83.   !rm -rf 'Homework2-Caltech101'
  84.  
  85.   # Clone github repository with data
  86.   if not os.path.isdir('./Homework2-Caltech101'):
  87.     !git clone https://github.com/eliax1996/Homework2-Caltech101
  88.  
  89.   !mv 'Homework2-Caltech101' 'Homework2_Caltech101'
  90.  
  91.   from Homework2_Caltech101 import caltech_dataset as cal_ds
  92.  
  93.   DATA_DIR = 'Homework2-Caltech101/101_ObjectCategories'
  94.  
  95.   # Prepare Pytorch train/test Datasets
  96.   train_dataset = cal_ds.Caltech(DATA_DIR, split = 'train', transform=train_transform)
  97.   test_dataset = cal_ds.Caltech(DATA_DIR, split = 'test', transform=eval_transform)
  98.  
  99.  
  100.   train_indexes_ = [idx for idx in range(len(train_dataset)) if idx % 5]
  101.   test_indexes = [idx for idx in range(len(test_dataset)) if not idx % 5]
  102.  
  103.  
  104.   j = 0
  105.   val_indexes = []
  106.   train_indexes = []
  107.   for ind in train_indexes_:
  108.     if j % 2: val_indexes.append(ind)
  109.     else: train_indexes.append(ind)
  110.     j = j+1
  111.  
  112.  
  113.   train_dataset = Subset(train_dataset, train_indexes)
  114.   test_dataset = Subset(test_dataset, test_indexes)
  115.   val_dataset = Subset(train_dataset, val_indexes)
  116.  
  117.   # Check dataset sizes
  118.   #print('Train Dataset: {}'.format(len(train_dataset)))
  119.   #print('Validation Dataset: {}'.format(len(val_dataset)))
  120.   #print('Test Dataset: {}'.format(len(test_dataset)))
  121.  
  122.  
  123.   # Dataloaders iterate over pytorch datasets and transparently provide useful functions (e.g. parallelization and shuffling)
  124.   train_dataloader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4, drop_last=True)
  125.   val_dataloader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4)
  126.   test_dataloader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4)
  127.  
  128.  
  129.   net = alexnet(pretrained=True) # Loading AlexNet model
  130.   net.classifier[6] = nn.Linear(4096, NUM_CLASSES)
  131.  
  132.   #sys.exit(0)
  133.  
  134.  
  135.   criterion = nn.CrossEntropyLoss()
  136.   parameters_to_optimize = net.parameters()
  137.  
  138.   print(parameters_to_optimize)
  139.  
  140.   i = 0
  141.   new_p = []
  142.   for param in parameters_to_optimize:
  143.     if i in (0,3,6,8,10): #(1,4,6)
  144.       param.requires_grad = False
  145.     new_p.append(param)
  146.     i = i+1
  147.  
  148.   # fully connected
  149.   #net.classifier[1].requires_grad = False
  150.   #net.classifier[4].requires_grad = False
  151.   #net.classifier[6].requires_grad = False
  152.  
  153.   # convolutional
  154.   #net.features[0].requires_grad = False
  155.   #net.features[3].requires_grad = False
  156.   #net.features[6].requires_grad = False
  157.   #net.features[8].requires_grad = False
  158.   #net.features[10].requires_grad = False
  159.  
  160.  
  161.   optimizer = optim.SGD(new_p, lr=LR, momentum=MOMENTUM, weight_decay=WEIGHT_DECAY)
  162.   scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=STEP_SIZE, gamma=GAMMA)
  163.  
  164.  
  165.  
  166.   !rm -rf "weights_only.pth"
  167.  
  168.   # By default, everything is loaded to cpu
  169.   net = net.to(DEVICE) # this will bring the network to GPU if DEVICE is cuda
  170.  
  171.   cudnn.benchmark # Calling this optimizes runtime
  172.  
  173.   current_step = 0
  174.  
  175.   best_acc = 0
  176.   loss_list = []
  177.   acc_list = []
  178.   # Start iterating over the epochs
  179.   for epoch in range(NUM_EPOCHS):
  180.     print('Starting epoch {}/{}, LR = {}'.format(epoch+1, NUM_EPOCHS, scheduler.get_lr()))
  181.  
  182.     # Iterate over the dataset
  183.     for images, labels in train_dataloader:
  184.       # Bring data over the device of choice
  185.       images = images.to(DEVICE)
  186.       labels = labels.to(DEVICE)
  187.  
  188.       net.train() # Sets module in training mode
  189.  
  190.       # PyTorch, by default, accumulates gradients after each backward pass
  191.       # We need to manually set the gradients to zero before starting a new iteration
  192.       optimizer.zero_grad() # Zero-ing the gradients
  193.      
  194.       # Forward pass to the network
  195.       outputs = net(images)
  196.  
  197.       # Compute loss based on output and ground truth
  198.       loss = criterion(outputs, labels)
  199.  
  200.       # Log loss
  201.       if current_step % LOG_FREQUENCY == 0:
  202.         print('Step {}, Loss {}'.format(current_step, loss.item()))
  203.         loss_list.append(loss)
  204.  
  205.       if math.isnan(loss):
  206.         break
  207.  
  208.       # Compute gradients for each layer and update weights
  209.       loss.backward()  # backward pass: computes gradients
  210.       optimizer.step() # update weights based on accumulated gradients
  211.  
  212.       current_step += 1
  213.    
  214.     # Step the scheduler
  215.     scheduler.step()
  216.    
  217.     running_corrects=0
  218.     for img,lab in val_dataloader:
  219.       lab = lab.to(DEVICE)
  220.       img = img.to(DEVICE)
  221.       outputs = net(img)
  222.       _, preds = torch.max(outputs.data, 1)
  223.       running_corrects += torch.sum(preds == lab.data).data.item()
  224.       accuracy = running_corrects / float(len(val_dataset))
  225.     acc_list.append(accuracy)
  226.  
  227.     if accuracy > best_acc:
  228.       print("best accuracy on Validation now is: %f", accuracy)
  229.       best_acc = accuracy
  230.       torch.save(net.state_dict(), 'weights_only.pth')
  231.  
  232.    
  233.  
  234.   plt.plot(loss_list)
  235.   plt.title('loss')
  236.   plt.grid()
  237.   plt.show()
  238.   plt.plot(acc_list)
  239.   plt.title('accuracy')
  240.   plt.grid()
  241.   plt.show()
  242.   print()
  243.   print("the best accuracy on validation set is: %f" % best_acc)
  244.   print()
  245.   print()
  246.  
  247.  
  248.   list_accuracy_param.append([[NUM_EPOCHS,STEP_SIZE],best_acc])
  249.  
  250.  
  251.  
  252.   net.load_state_dict(torch.load('weights_only.pth'))
  253.  
  254.   net.to(DEVICE) # this will bring the network to GPU if DEVICE is cuda
  255.   net.train(False) # Set Network to evaluation mode
  256.  
  257.   running_corrects = 0
  258.   for images, labels in tqdm(test_dataloader):
  259.     images = images.to(DEVICE)
  260.     labels = labels.to(DEVICE)
  261.  
  262.     # Forward Pass
  263.     outputs = net(images)
  264.  
  265.     # Get predictions
  266.     _, preds = torch.max(outputs.data, 1)
  267.  
  268.     # Update Corrects
  269.     running_corrects += torch.sum(preds == labels.data).data.item()
  270.  
  271.   # Calculate Accuracy
  272.   accuracy = running_corrects / float(len(test_dataset))
  273.  
  274.   print('Test Accuracy: {}'.format(accuracy))
  275.  
  276. print(list_accuracy_param)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement