-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
93 lines (76 loc) · 3.05 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import numpy as np
import csv
import torch
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from torch.utils.data.sampler import SubsetRandomSampler
# Load the datasets
# We randomly sample some images from the dataset, because ImageNet itself is too large.
def dataloader(train_size, test_size, data_dir, batch_size, num_workers, total_num=50000):
# Setup the transformation
train_transforms = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
test_transforms = transforms.Compose([
transforms.Resize(size=(224, 224)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
index = np.arange(total_num)
np.random.shuffle(index)
train_index = index[:train_size]
test_index = index[train_size: (train_size + test_size)]
train_dataset = torchvision.datasets.ImageFolder(root=data_dir, transform=train_transforms)
test_dataset = torchvision.datasets.ImageFolder(root=data_dir, transform=test_transforms)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, sampler=SubsetRandomSampler(train_index), num_workers=num_workers, pin_memory=True, shuffle=False)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, sampler=SubsetRandomSampler(test_index), num_workers=num_workers, pin_memory=True, shuffle=False)
return train_loader, test_loader
# Test the model on clean dataset
def test(model, dataloader):
model.eval()
correct, total = 0, 0
for (images, labels) in dataloader:
images = images.cuda()
labels = labels.cuda()
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += 1
if(predicted[0] == labels):
correct += 1
print(correct, total, correct/total)
return correct / total
# Test the model on clean dataset
def test_place(model, dataloader, classes):
model.eval()
correct, total = 0, 0
for (images, labels) in dataloader:
label = int( classes[ labels.item() ] )
images = images.cuda()
#labels = labels.cuda()
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += 1
if(predicted[0].data.cpu().numpy() == labels):
correct += 1
print(correct, total, correct/total)
return correct / total
# Load the log and generate the training line
def log_generation(log_dir):
# Load the statistics in the log
epochs, train_rate, test_rate = [], [], []
with open(log_dir, 'r') as f:
reader = csv.reader(f)
flag = 0
for i in reader:
if flag == 0:
flag += 1
continue
else:
epochs.append(int(i[0]))
train_rate.append(float(i[1]))
test_rate.append(float(i[2]))