lamiastella

LOOCV Transfer Learning IndexError: list assignment index

Nov 13th, 2018
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.84 KB | None | 0 0
  1. #model_ft = model_ft.cuda()
  2. nb_samples = 931
  3. nb_classes = 9
  4.  
  5.  
  6. from __future__ import print_function, division
  7.  
  8. import torch
  9. import torch.nn as nn
  10. import torch.optim as optim
  11. from torch.optim import lr_scheduler
  12. import numpy as np
  13. import torchvision
  14. from torchvision import datasets, models, transforms
  15. import matplotlib.pyplot as plt
  16. import time
  17. import os
  18. import copy
  19.  
  20. import torch.utils.data as data_utils
  21. from torch.utils import data
  22.  
  23.  
  24.  
  25.  
  26.  
  27. data_transforms = {
  28.     'train': transforms.Compose([
  29.         transforms.RandomResizedCrop(224),
  30.         transforms.RandomHorizontalFlip(),
  31.         transforms.RandomRotation(20),
  32.         transforms.ToTensor(),
  33.         transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
  34.     ]),
  35.  
  36.         'test': transforms.Compose([
  37.         transforms.Resize(256),
  38.         transforms.CenterCrop(224),
  39.         transforms.ToTensor(),
  40.         transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
  41.     ]),
  42. }
  43.  
  44. val_loader = data.DataLoader(
  45.         image_datasets['test'],
  46.         num_workers=2,
  47.         batch_size=1
  48.     )
  49. val_loader = iter(val_loader)
  50.  
  51. data_dir = "images"
  52. image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),
  53.                                           data_transforms[x])
  54.                   for x in ['train', 'test']}
  55.  
  56. dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'test']}
  57. print(dataset_sizes)
  58. class_names = image_datasets['train'].classes
  59.  
  60. device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  61. # LOOCV
  62. loocv_preds = []
  63. loocv_targets = []
  64. for idx in range(nb_samples):
  65.    
  66.     print('Using sample {} as test data'.format(idx))
  67.    
  68.     # Get all indices and remove test sample
  69.     train_indices = list(range(len(image_datasets)))
  70.     del train_indices[idx]
  71.    
  72.     # Create new sampler
  73.     sampler = data.SubsetRandomSampler(train_indices)
  74.  
  75.     dataloader = data.DataLoader(
  76.         image_datasets['train'],
  77.         num_workers=2,
  78.         batch_size=1,
  79.         sampler=sampler
  80.     )
  81.    
  82.     # Train model
  83.     for batch_idx, (samples, target) in enumerate(dataloader):
  84.         print('Batch {}'.format(batch_idx))
  85.         model_ft = train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler, num_epochs=25) # do I add this line here?
  86.                
  87.     # Test on LOO sample
  88.     model_ft.eval()
  89. #    test_data, test_target = image_datasets['train'][idx]
  90.     test_data, test_target = val_loader.next()
  91.     test_data = test_data.cuda()
  92.     test_target = test_target.cuda()
  93.     #test_data.unsqueeze_(1)
  94.     #test_target.unsqueeze_(0)
  95.  
  96.     output = model_ft(test_data)
  97.     pred = torch.argmax(output, 1)
  98.     loocv_preds.append(pred)
  99.     loocv_targets.append(test_target.item())
  100.  
  101.  
  102.  
  103. -------------------------------------------------------
  104. -------------------------------------------------------
  105. {'train': 791, 'test': 140}
  106. Using sample 0 as test data
  107. Batch 0
  108. Epoch 0/24
  109. ----------
  110. train Loss: 2.0170 Acc: 0.3009
  111. test Loss: 2.2370 Acc: 0.3571
  112.  
  113. Epoch 1/24
  114. ----------
  115. train Loss: 2.0436 Acc: 0.3236
  116. test Loss: 1.8071 Acc: 0.4071
  117.  
  118. Epoch 2/24
  119. ----------
  120. train Loss: 1.9560 Acc: 0.3300
  121. test Loss: 1.9911 Acc: 0.3714
  122.  
  123. Epoch 3/24
  124. ----------
  125. train Loss: 1.9052 Acc: 0.3413
  126. test Loss: 2.4123 Acc: 0.3286
  127.  
  128. Epoch 4/24
  129. ----------
  130. train Loss: 1.8917 Acc: 0.3692
  131. test Loss: 2.8163 Acc: 0.3929
  132.  
  133. Epoch 5/24
  134. ----------
  135. train Loss: 1.8890 Acc: 0.3515
  136. test Loss: 2.3379 Acc: 0.3429
  137.  
  138. Epoch 6/24
  139. ----------
  140. train Loss: 1.9090 Acc: 0.3388
  141. test Loss: 2.3404 Acc: 0.3929
  142.  
  143. Epoch 7/24
  144. ----------
  145. train Loss: 1.6069 Acc: 0.4475
  146. test Loss: 2.0714 Acc: 0.4143
  147.  
  148. Epoch 8/24
  149. ----------
  150. train Loss: 1.4701 Acc: 0.5019
  151. test Loss: 2.1390 Acc: 0.4071
  152.  
  153. Epoch 9/24
  154. ----------
  155. train Loss: 1.5322 Acc: 0.4867
  156. test Loss: 2.0545 Acc: 0.4286
  157.  
  158. Epoch 10/24
  159. ----------
  160. train Loss: 1.4743 Acc: 0.5082
  161. test Loss: 2.0200 Acc: 0.4143
  162.  
  163. Epoch 11/24
  164. ----------
  165. train Loss: 1.4498 Acc: 0.5006
  166. test Loss: 2.0408 Acc: 0.4071
  167.  
  168. Epoch 12/24
  169. ----------
  170. train Loss: 1.4314 Acc: 0.5095
  171. test Loss: 2.0807 Acc: 0.4214
  172.  
  173. Epoch 13/24
  174. ----------
  175. train Loss: 1.4097 Acc: 0.5133
  176. test Loss: 2.1612 Acc: 0.4214
  177.  
  178. Epoch 14/24
  179. ----------
  180. train Loss: 1.4116 Acc: 0.5234
  181. test Loss: 2.0989 Acc: 0.4000
  182.  
  183. Epoch 15/24
  184. ----------
  185. train Loss: 1.3736 Acc: 0.5234
  186. test Loss: 2.1113 Acc: 0.4214
  187.  
  188. Epoch 16/24
  189. ----------
  190. train Loss: 1.3562 Acc: 0.5310
  191. test Loss: 2.1046 Acc: 0.4143
  192.  
  193. Epoch 17/24
  194. ----------
  195. train Loss: 1.3730 Acc: 0.5297
  196. test Loss: 2.1644 Acc: 0.4214
  197.  
  198. Epoch 18/24
  199. ----------
  200. train Loss: 1.3351 Acc: 0.5398
  201. test Loss: 2.1070 Acc: 0.4357
  202.  
  203. Epoch 19/24
  204. ----------
  205. train Loss: 1.3228 Acc: 0.5449
  206. test Loss: 2.1646 Acc: 0.4214
  207.  
  208. Epoch 20/24
  209. ----------
  210. train Loss: 1.3837 Acc: 0.5272
  211. test Loss: 2.1686 Acc: 0.4214
  212.  
  213. Epoch 21/24
  214. ----------
  215. train Loss: 1.3377 Acc: 0.5424
  216. test Loss: 2.1626 Acc: 0.4143
  217.  
  218. Epoch 22/24
  219. ----------
  220. train Loss: 1.3879 Acc: 0.5158
  221. test Loss: 2.1593 Acc: 0.4286
  222.  
  223. Epoch 23/24
  224. ----------
  225. train Loss: 1.3329 Acc: 0.5563
  226. test Loss: 2.2069 Acc: 0.4357
  227.  
  228. Epoch 24/24
  229. ----------
  230. train Loss: 1.3181 Acc: 0.5613
  231. test Loss: 2.1064 Acc: 0.4143
  232.  
  233. Training complete in 5m 5s
  234. Using sample 1 as test data
  235. Batch 0
  236. Epoch 0/24
  237. ----------
  238. train Loss: 1.3487 Acc: 0.5310
  239. test Loss: 2.1172 Acc: 0.4286
  240.  
  241. Epoch 1/24
  242. ----------
  243. train Loss: 1.3218 Acc: 0.5474
  244. test Loss: 2.0875 Acc: 0.4214
  245.  
  246. Epoch 2/24
  247. ----------
  248. train Loss: 1.3528 Acc: 0.5436
  249. test Loss: 2.2111 Acc: 0.4286
  250.  
  251. Epoch 3/24
  252. ----------
  253. train Loss: 1.3242 Acc: 0.5499
  254. test Loss: 2.1504 Acc: 0.4286
  255.  
  256. Epoch 4/24
  257. ----------
  258. train Loss: 1.3318 Acc: 0.5499
  259. test Loss: 2.1848 Acc: 0.4286
  260.  
  261. Epoch 5/24
  262. ----------
  263. train Loss: 1.3252 Acc: 0.5449
  264. test Loss: 2.1637 Acc: 0.4357
  265.  
  266. Epoch 6/24
  267. ----------
  268. train Loss: 1.3135 Acc: 0.5424
  269. test Loss: 2.0848 Acc: 0.4357
  270.  
  271. Epoch 7/24
  272. ----------
  273. train Loss: 1.3492 Acc: 0.5373
  274. test Loss: 2.1031 Acc: 0.4071
  275.  
  276. Epoch 8/24
  277. ----------
  278. train Loss: 1.2792 Acc: 0.5575
  279. test Loss: 2.1071 Acc: 0.4286
  280.  
  281. Epoch 9/24
  282. ----------
  283. train Loss: 1.3436 Acc: 0.5386
  284. test Loss: 2.1486 Acc: 0.4500
  285.  
  286. Epoch 10/24
  287. ----------
  288. train Loss: 1.3596 Acc: 0.5373
  289. test Loss: 2.1443 Acc: 0.4214
  290.  
  291. Epoch 11/24
  292. ----------
  293. train Loss: 1.3750 Acc: 0.5499
  294. test Loss: 2.1618 Acc: 0.4214
  295.  
  296. Epoch 12/24
  297. ----------
  298. train Loss: 1.3241 Acc: 0.5424
  299. test Loss: 2.1119 Acc: 0.4214
  300.  
  301. Epoch 13/24
  302. ----------
  303. train Loss: 1.3706 Acc: 0.5335
  304. test Loss: 2.0601 Acc: 0.4143
  305.  
  306. Epoch 14/24
  307. ----------
  308. train Loss: 1.3505 Acc: 0.5525
  309. test Loss: 2.0726 Acc: 0.4214
  310.  
  311. Epoch 15/24
  312. ----------
  313. train Loss: 1.3460 Acc: 0.5550
  314. test Loss: 2.0473 Acc: 0.4429
  315.  
  316. Epoch 16/24
  317. ----------
  318. train Loss: 1.3337 Acc: 0.5563
  319. test Loss: 2.0629 Acc: 0.4143
  320.  
  321. Epoch 17/24
  322. ----------
  323. train Loss: 1.3146 Acc: 0.5601
  324. test Loss: 2.1087 Acc: 0.4214
  325.  
  326. Epoch 18/24
  327. ----------
  328. train Loss: 1.3320 Acc: 0.5512
  329. test Loss: 2.1290 Acc: 0.4286
  330.  
  331. Epoch 19/24
  332. ----------
  333. train Loss: 1.3393 Acc: 0.5601
  334. test Loss: 2.0548 Acc: 0.4143
  335.  
  336. Epoch 20/24
  337. ----------
  338. train Loss: 1.3232 Acc: 0.5474
  339. test Loss: 2.0624 Acc: 0.4429
  340.  
  341. Epoch 21/24
  342. ----------
  343. train Loss: 1.3537 Acc: 0.5386
  344. test Loss: 2.0688 Acc: 0.4286
  345.  
  346. Epoch 22/24
  347. ----------
  348. train Loss: 1.2933 Acc: 0.5664
  349. test Loss: 2.1751 Acc: 0.4143
  350.  
  351. Epoch 23/24
  352. ----------
  353. train Loss: 1.3747 Acc: 0.5424
  354. test Loss: 2.1009 Acc: 0.4143
  355.  
  356. Epoch 24/24
  357. ----------
  358. train Loss: 1.3455 Acc: 0.5183
  359. test Loss: 2.1395 Acc: 0.4143
  360.  
  361. Training complete in 5m 10s
  362. Using sample 2 as test data
  363.  
  364. ---------------------------------------------------------------------------
  365. IndexError                                Traceback (most recent call last)
  366. <ipython-input-10-15f05fc7d5ca> in <module>()
  367.      68     # Get all indices and remove test sample
  368.      69     train_indices = list(range(len(image_datasets)))
  369. ---> 70     del train_indices[idx]
  370.      71
  371.      72     # Create new sampler
  372.  
  373. IndexError: list assignment index out of range
Add Comment
Please, Sign In to add comment