Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #model_ft = model_ft.cuda()
- nb_samples = 931
- nb_classes = 9
- from __future__ import print_function, division
- import torch
- import torch.nn as nn
- import torch.optim as optim
- from torch.optim import lr_scheduler
- import numpy as np
- import torchvision
- from torchvision import datasets, models, transforms
- import matplotlib.pyplot as plt
- import time
- import os
- import copy
- import torch.utils.data as data_utils
- from torch.utils import data
- data_transforms = {
- 'train': transforms.Compose([
- transforms.RandomResizedCrop(224),
- transforms.RandomHorizontalFlip(),
- transforms.RandomRotation(20),
- transforms.ToTensor(),
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
- ]),
- 'test': transforms.Compose([
- transforms.Resize(256),
- transforms.CenterCrop(224),
- transforms.ToTensor(),
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
- ]),
- }
- val_loader = data.DataLoader(
- image_datasets['test'],
- num_workers=2,
- batch_size=1
- )
- val_loader = iter(val_loader)
- data_dir = "images"
- image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),
- data_transforms[x])
- for x in ['train', 'test']}
- dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'test']}
- print(dataset_sizes)
- class_names = image_datasets['train'].classes
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
- # LOOCV
- loocv_preds = []
- loocv_targets = []
- for idx in range(nb_samples):
- print('Using sample {} as test data'.format(idx))
- # Get all indices and remove test sample
- train_indices = list(range(len(image_datasets)))
- del train_indices[idx]
- # Create new sampler
- sampler = data.SubsetRandomSampler(train_indices)
- dataloader = data.DataLoader(
- image_datasets['train'],
- num_workers=2,
- batch_size=1,
- sampler=sampler
- )
- # Train model
- for batch_idx, (samples, target) in enumerate(dataloader):
- print('Batch {}'.format(batch_idx))
- model_ft = train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler, num_epochs=25) # do I add this line here?
- # Test on LOO sample
- model_ft.eval()
- # test_data, test_target = image_datasets['train'][idx]
- test_data, test_target = val_loader.next()
- test_data = test_data.cuda()
- test_target = test_target.cuda()
- #test_data.unsqueeze_(1)
- #test_target.unsqueeze_(0)
- output = model_ft(test_data)
- pred = torch.argmax(output, 1)
- loocv_preds.append(pred)
- loocv_targets.append(test_target.item())
- -------------------------------------------------------
- -------------------------------------------------------
- {'train': 791, 'test': 140}
- Using sample 0 as test data
- Batch 0
- Epoch 0/24
- ----------
- train Loss: 2.0170 Acc: 0.3009
- test Loss: 2.2370 Acc: 0.3571
- Epoch 1/24
- ----------
- train Loss: 2.0436 Acc: 0.3236
- test Loss: 1.8071 Acc: 0.4071
- Epoch 2/24
- ----------
- train Loss: 1.9560 Acc: 0.3300
- test Loss: 1.9911 Acc: 0.3714
- Epoch 3/24
- ----------
- train Loss: 1.9052 Acc: 0.3413
- test Loss: 2.4123 Acc: 0.3286
- Epoch 4/24
- ----------
- train Loss: 1.8917 Acc: 0.3692
- test Loss: 2.8163 Acc: 0.3929
- Epoch 5/24
- ----------
- train Loss: 1.8890 Acc: 0.3515
- test Loss: 2.3379 Acc: 0.3429
- Epoch 6/24
- ----------
- train Loss: 1.9090 Acc: 0.3388
- test Loss: 2.3404 Acc: 0.3929
- Epoch 7/24
- ----------
- train Loss: 1.6069 Acc: 0.4475
- test Loss: 2.0714 Acc: 0.4143
- Epoch 8/24
- ----------
- train Loss: 1.4701 Acc: 0.5019
- test Loss: 2.1390 Acc: 0.4071
- Epoch 9/24
- ----------
- train Loss: 1.5322 Acc: 0.4867
- test Loss: 2.0545 Acc: 0.4286
- Epoch 10/24
- ----------
- train Loss: 1.4743 Acc: 0.5082
- test Loss: 2.0200 Acc: 0.4143
- Epoch 11/24
- ----------
- train Loss: 1.4498 Acc: 0.5006
- test Loss: 2.0408 Acc: 0.4071
- Epoch 12/24
- ----------
- train Loss: 1.4314 Acc: 0.5095
- test Loss: 2.0807 Acc: 0.4214
- Epoch 13/24
- ----------
- train Loss: 1.4097 Acc: 0.5133
- test Loss: 2.1612 Acc: 0.4214
- Epoch 14/24
- ----------
- train Loss: 1.4116 Acc: 0.5234
- test Loss: 2.0989 Acc: 0.4000
- Epoch 15/24
- ----------
- train Loss: 1.3736 Acc: 0.5234
- test Loss: 2.1113 Acc: 0.4214
- Epoch 16/24
- ----------
- train Loss: 1.3562 Acc: 0.5310
- test Loss: 2.1046 Acc: 0.4143
- Epoch 17/24
- ----------
- train Loss: 1.3730 Acc: 0.5297
- test Loss: 2.1644 Acc: 0.4214
- Epoch 18/24
- ----------
- train Loss: 1.3351 Acc: 0.5398
- test Loss: 2.1070 Acc: 0.4357
- Epoch 19/24
- ----------
- train Loss: 1.3228 Acc: 0.5449
- test Loss: 2.1646 Acc: 0.4214
- Epoch 20/24
- ----------
- train Loss: 1.3837 Acc: 0.5272
- test Loss: 2.1686 Acc: 0.4214
- Epoch 21/24
- ----------
- train Loss: 1.3377 Acc: 0.5424
- test Loss: 2.1626 Acc: 0.4143
- Epoch 22/24
- ----------
- train Loss: 1.3879 Acc: 0.5158
- test Loss: 2.1593 Acc: 0.4286
- Epoch 23/24
- ----------
- train Loss: 1.3329 Acc: 0.5563
- test Loss: 2.2069 Acc: 0.4357
- Epoch 24/24
- ----------
- train Loss: 1.3181 Acc: 0.5613
- test Loss: 2.1064 Acc: 0.4143
- Training complete in 5m 5s
- Using sample 1 as test data
- Batch 0
- Epoch 0/24
- ----------
- train Loss: 1.3487 Acc: 0.5310
- test Loss: 2.1172 Acc: 0.4286
- Epoch 1/24
- ----------
- train Loss: 1.3218 Acc: 0.5474
- test Loss: 2.0875 Acc: 0.4214
- Epoch 2/24
- ----------
- train Loss: 1.3528 Acc: 0.5436
- test Loss: 2.2111 Acc: 0.4286
- Epoch 3/24
- ----------
- train Loss: 1.3242 Acc: 0.5499
- test Loss: 2.1504 Acc: 0.4286
- Epoch 4/24
- ----------
- train Loss: 1.3318 Acc: 0.5499
- test Loss: 2.1848 Acc: 0.4286
- Epoch 5/24
- ----------
- train Loss: 1.3252 Acc: 0.5449
- test Loss: 2.1637 Acc: 0.4357
- Epoch 6/24
- ----------
- train Loss: 1.3135 Acc: 0.5424
- test Loss: 2.0848 Acc: 0.4357
- Epoch 7/24
- ----------
- train Loss: 1.3492 Acc: 0.5373
- test Loss: 2.1031 Acc: 0.4071
- Epoch 8/24
- ----------
- train Loss: 1.2792 Acc: 0.5575
- test Loss: 2.1071 Acc: 0.4286
- Epoch 9/24
- ----------
- train Loss: 1.3436 Acc: 0.5386
- test Loss: 2.1486 Acc: 0.4500
- Epoch 10/24
- ----------
- train Loss: 1.3596 Acc: 0.5373
- test Loss: 2.1443 Acc: 0.4214
- Epoch 11/24
- ----------
- train Loss: 1.3750 Acc: 0.5499
- test Loss: 2.1618 Acc: 0.4214
- Epoch 12/24
- ----------
- train Loss: 1.3241 Acc: 0.5424
- test Loss: 2.1119 Acc: 0.4214
- Epoch 13/24
- ----------
- train Loss: 1.3706 Acc: 0.5335
- test Loss: 2.0601 Acc: 0.4143
- Epoch 14/24
- ----------
- train Loss: 1.3505 Acc: 0.5525
- test Loss: 2.0726 Acc: 0.4214
- Epoch 15/24
- ----------
- train Loss: 1.3460 Acc: 0.5550
- test Loss: 2.0473 Acc: 0.4429
- Epoch 16/24
- ----------
- train Loss: 1.3337 Acc: 0.5563
- test Loss: 2.0629 Acc: 0.4143
- Epoch 17/24
- ----------
- train Loss: 1.3146 Acc: 0.5601
- test Loss: 2.1087 Acc: 0.4214
- Epoch 18/24
- ----------
- train Loss: 1.3320 Acc: 0.5512
- test Loss: 2.1290 Acc: 0.4286
- Epoch 19/24
- ----------
- train Loss: 1.3393 Acc: 0.5601
- test Loss: 2.0548 Acc: 0.4143
- Epoch 20/24
- ----------
- train Loss: 1.3232 Acc: 0.5474
- test Loss: 2.0624 Acc: 0.4429
- Epoch 21/24
- ----------
- train Loss: 1.3537 Acc: 0.5386
- test Loss: 2.0688 Acc: 0.4286
- Epoch 22/24
- ----------
- train Loss: 1.2933 Acc: 0.5664
- test Loss: 2.1751 Acc: 0.4143
- Epoch 23/24
- ----------
- train Loss: 1.3747 Acc: 0.5424
- test Loss: 2.1009 Acc: 0.4143
- Epoch 24/24
- ----------
- train Loss: 1.3455 Acc: 0.5183
- test Loss: 2.1395 Acc: 0.4143
- Training complete in 5m 10s
- Using sample 2 as test data
- ---------------------------------------------------------------------------
- IndexError Traceback (most recent call last)
- <ipython-input-10-15f05fc7d5ca> in <module>()
- 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
- IndexError: list assignment index out of range
Add Comment
Please, Sign In to add comment