Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.18 KB | None | 0 0
  1. # Imports here
  2. #%matplotlib inline
  3. #%config InlineBackend.figure_format = 'retina'
  4.  
  5. import matplotlib.pyplot as plt
  6. import numpy as np
  7. import time
  8. import workspace_utils
  9.  
  10. import torch
  11. from torch import nn
  12. from torch import optim
  13. import torch.nn.functional as F
  14. from torch.autograd import Variable
  15. from torchvision import datasets, transforms, models
  16. from workspace_utils import active_session
  17.  
  18. # Give user the option to choose data directory, whether to use the GPU or not, and the model architecture
  19. parser = argparse.ArgumentParser(description='Train a model to recognize flowers')
  20. parser.add_argument('--data_directory', type=str, help='Directory containing data')
  21. parser.add_argument('--save_directory', type=str, help='Directory where the checkpoints will be saved')
  22. parser.add_argument('--gpu', type=bool, default=False, help='Wherther to use GPU for training or not')
  23. parser.add_argument('--arch', type=str, default='VGG', help='Choose the architecture')
  24. parser.add_argument('--lr', type=float, default=0.0002, help='Choose the learining rate for the model')
  25. parser.add_argument('--hidden_unit', type=int, default=500, help='Choose the number of hiddent units in the model')
  26. parser.add_argument('--epochs', type=float, default=3, help='Choose the training epochs for the model')
  27.  
  28. # Store user options in the "args" variable
  29. args = parser.parse_args()
  30.  
  31. data_dir = 'flowers'
  32. train_dir = data_dir + '/train'
  33. valid_dir = data_dir + '/valid'
  34. test_dir = data_dir + '/test'
  35.  
  36. # TODO: Define your transforms for the training, validation, and testing sets
  37. transforms_training = transforms.Compose([transforms.RandomRotation(30),
  38. transforms.RandomResizedCrop(224),
  39. transforms.RandomHorizontalFlip(),
  40. transforms.ToTensor(),
  41. transforms.Normalize([0.485, 0.456, 0.406],
  42. [0.229, 0.224, 0.225])])
  43.  
  44. transforms_validation = transforms.Compose([transforms.Resize(256),
  45. transforms.CenterCrop(224),
  46. transforms.ToTensor(),
  47. transforms.Normalize([0.485, 0.456, 0.406],
  48. [0.229, 0.224, 0.225])])
  49.  
  50. transforms_testing = transforms.Compose([transforms.Resize(256),
  51. transforms.CenterCrop(224),
  52. transforms.ToTensor(),
  53. transforms.Normalize([0.485, 0.456, 0.406],
  54. [0.229, 0.224, 0.225])])
  55.  
  56. # TODO: Load the datasets with ImageFolder
  57. train_data = datasets.ImageFolder(train_dir, transform=transforms_training)
  58. validate_data = datasets.ImageFolder(valid_dir, transform=transforms_validation)
  59. test_data = datasets.ImageFolder(test_dir, transform=transforms_testing)
  60.  
  61. # TODO: Using the image datasets and the trainforms, define the dataloaders
  62. trainloader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True)
  63. validloader = torch.utils.data.DataLoader(validate_data, batch_size=32)
  64. testloader = torch.utils.data.DataLoader(test_data, batch_size=32)
  65.  
  66. # A dictionary mapping the integer encoded categories to the actual names of the flowers
  67. import json
  68.  
  69. with open('cat_to_name.json', 'r') as f:
  70. cat_to_name = json.load(f)
  71.  
  72. # TODO: Build the model use the chosen architecture
  73. model = models.args.arch(pretrained=True)
  74.  
  75. # Freeze parameters so we don't backprop through them
  76. for param in model.parameters():
  77. param.requires_grad = False
  78.  
  79. # Build classifier
  80. from collections import OrderedDict
  81. classifier = nn.Sequential(OrderedDict([
  82. ('fc1', nn.Linear(25088, 2000)),
  83. ('relu', nn.ReLU()),
  84. ('dropout', nn.Dropout(p=0.3)),
  85. ('fc2', nn.Linear(2000, args.hidden_unit)),
  86. ('relu', nn.ReLU()),
  87. ('dropout', nn.Dropout(p=0.3)),
  88. ('fc3', nn.Linear(args.hidden_unit, 102)),
  89. ('output', nn.LogSoftmax(dim=1))
  90. ]))
  91.  
  92. model.classifier = classifier
  93.  
  94. # Set negative log loss as the criteria
  95. criterion = nn.NLLLoss()
  96. # Only train the classifier parameters, feature parameters are frozen
  97. optimizer = optim.Adam(model.classifier.parameters(), lr=args.lr) # Need to give the option to change learn rate
  98.  
  99. # Train the network
  100.  
  101. epochs = args.epochs
  102. steps = 0
  103. running_loss = 0
  104. print_every = 40
  105.  
  106. device = torch.device('cuda:0' if args.gpu else 'cpu')
  107.  
  108. model.to(device)
  109.  
  110. with active_session():
  111. for e in range(epochs):
  112. # Model in training mode, dropout is on
  113. model.train()
  114. for inputs, labels in iter(trainloader):
  115. steps += 1
  116.  
  117. # Wrap images and labels in Variables so we can calculate gradients
  118. inputs = Variable(inputs)
  119. targets = Variable(labels)
  120.  
  121. # Move input and label tensors to the GPU
  122. inputs, labels = inputs.to(device), labels.to(device)
  123.  
  124. optimizer.zero_grad()
  125.  
  126. output = model.forward(inputs)
  127. loss = criterion(output, labels)
  128. loss.backward()
  129. optimizer.step()
  130.  
  131. running_loss += loss.data[0]
  132.  
  133. if steps % print_every == 0:
  134. # Model in inference mode, dropout is off
  135. model.eval()
  136.  
  137. accuracy = 0
  138. test_loss = 0
  139. for ii, (inputs, labels) in enumerate(validloader):
  140.  
  141. # Set volatile to True so we don't save the history
  142. inputs = Variable(inputs, volatile=True)
  143. labels = Variable(labels, volatile=True)
  144.  
  145. inputs, labels = inputs.to(device), labels.to(device)
  146.  
  147. output = model.forward(inputs)
  148. test_loss += criterion(output, labels).data[0]
  149.  
  150. ## Calculating the accuracy
  151. # Model's output is log-softmax, take exponential to get the probabilities
  152. ps = torch.exp(output).data
  153. # Class with highest probability is our predicted class, compare with true label
  154. equality = (labels.data == ps.max(1)[1])
  155. # Accuracy is number of correct predictions divided by all predictions, just take the mean
  156. accuracy += equality.type_as(torch.FloatTensor()).mean()
  157.  
  158. print("Epoch: {}/{}.. ".format(e+1, epochs),
  159. "Training Loss: {:.3f}.. ".format(running_loss/print_every),
  160. "Test Loss: {:.3f}.. ".format(test_loss/len(testloader)),
  161. "Test Accuracy: {:.3f}".format(accuracy/len(testloader)))
  162.  
  163. running_loss = 0
  164.  
  165. # Make sure dropout is on for training
  166. model.train()
  167.  
  168. # TODO: Do validation on the test set
  169.  
  170. model.eval()
  171.  
  172. correct = 0
  173. total = 0
  174. with torch.no_grad():
  175. for data in testloader:
  176. images, labels = data
  177. images, labels = images.to(device), labels.to(device)
  178. outputs = model(images)
  179. _, predicted = torch.max(outputs.data, 1)
  180. total += labels.size(0)
  181. correct += (predicted == labels).sum().item()
  182.  
  183. print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))
  184.  
  185. # TODO: Save the checkpoint
  186. model.class_to_idx = train_data.class_to_idx
  187.  
  188. checkpoint = {'model':model,
  189. 'input': 25088,
  190. 'output':102,
  191. 'epochs':3,
  192. 'learning_rate':0.0002,
  193. 'dropout':0.3,
  194. 'batch_size':32,
  195. 'classifier':classifier,
  196. 'state_dict':model.state_dict(),
  197. 'optimizer':optimizer.state_dict(),
  198. 'class_to_idx': model.class_to_idx}
  199. torch.save(checkpoint, args.save_directory)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement