-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
189 lines (148 loc) · 6.02 KB
/
test.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'''
Dataloader first; training and testing
define network; frezze layers
define GPU;
define optimizer, loss;
training loss, testing loss;
training accuracy, testing accuracy
'''
import os
import torch
import numpy as np
from torch import nn, optim
import torch.nn as nn
from torchvision import datasets, transforms
import torchvision.models as models
cuda = torch.cuda.is_available()
device = torch.device("cuda" if cuda else "cpu")
kwargs = {'num_workers': 1, 'pin_memory': True} if cuda else {}
img_dim = 256
n_classes = 917 #change this to the number of different cattle
iters = 500
log_interval = 1
# change data path
train_root = "/media/HDD1/Cattle/extreme_clean/train/"
vali_root = "/media/HDD1/Cattle/extreme_clean/valid/"
test_root = "/media/HDD1/Cattle/extreme_clean/test/"
checkpoint_root = "/media/HDD1/Cattle/extreme_clean/checkpoints/"
data_transforms = transforms.Compose([
transforms.Resize((img_dim,img_dim)),
transforms.ToTensor(),
])
# dictionary = datasets.ImageFolder(train_root, transform=data_transforms)
# print(dictionary.class_to_idx)
train_loader = torch.utils.data.DataLoader(
datasets.ImageFolder(train_root, transform=data_transforms),
batch_size = 65*3, shuffle=True, **kwargs)
vali_loader = torch.utils.data.DataLoader(
datasets.ImageFolder(vali_root, transform=data_transforms),
batch_size = 65*3, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
datasets.ImageFolder(test_root, transform=data_transforms),
batch_size = 65*3, shuffle=True, **kwargs)
model = models.resnet34(pretrained=True)
for param in model.parameters():
param.requires_grad = True
# resnet config
# model.fc = nn.Sequential(
# nn.Linear(512, 256),
# nn.ReLU(),
# nn.Dropout(0.4),
# nn.Linear(256, n_classes),
# nn.LogSoftmax(dim=1))
# model.classifier[6] = nn.Linear(4096,n_classes)
# model.AuxLogits.fc = nn.Linear(768, n_classes)
# model.fc = nn.Linear(1024, n_classes)
model.fc = nn.Linear(512, n_classes)
model = model.to(device)
# criterion = nn.NLLLoss() # resnet
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters())
all_train_losses = list()
all_test_losses = list()
min_loss = float("inf")
epoch = 0
# load last checkpoint
last_saved = checkpoint_root + "classifier_0.0000.tar"
if last_saved.endswith(".tar"):
checkpoint = torch.load(last_saved, map_location='cpu')
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
all_train_losses = checkpoint['train_losses']
all_test_losses = checkpoint['test_losses']
min_loss = checkpoint['min_loss']
epoch = checkpoint['epoch']
# while epoch < iters:
# model.train()
# epoch_train_loss = list()
# train_correct = 0
# train_total = 0
# for batch_idx, (img, label) in enumerate(train_loader):
# img = img.to(device)
# label = label.to(device)
# out = model(img)
# loss = criterion(out, label)
# epoch_train_loss.append(loss.item())
# _, predicted = torch.max(out.data, 1)
# train_total += label.size()[0]
# train_correct += (predicted == label).sum().item()
# optimizer.zero_grad()
# loss.backward()
# optimizer.step()
# if batch_idx % log_interval == 0:
# print('Train Epoch: {} [{}/{} ({:.0f}%)]\tloss: {:.6f}'.format(
# epoch, batch_idx * len(img), len(train_loader.dataset),
# 100. * batch_idx / len(train_loader),
# loss.item()))
# mean_train_loss = np.mean(np.array(epoch_train_loss))
# all_train_losses.append(mean_train_loss)
# print('====> Epoch: {} Average training_loss: {:.4f}; Train accuracy: {:.4%}'.format(epoch, mean_train_loss, train_correct / train_total))
# model.eval()
# epoch_test_loss = list()
# test_correct = 0
# test_total = 0
# with torch.no_grad():
# for batch_idx, (img, label) in enumerate(vali_loader):
# img = img.to(device)
# label = label.to(device)
# out = model(img)
# loss = criterion(out, label)
# epoch_test_loss.append(loss.item())
# _, predicted = torch.max(out.data, 1)
# test_total += label.size()[0]
# test_correct += (predicted == label).sum().item()
# mean_test_loss = np.mean(np.array(epoch_test_loss))
# all_test_losses.append(mean_test_loss)
# print('====> Epoch: {} Average valid_loss: {:.4f}; Validation accuracy: {:.4%}'.format(epoch, mean_test_loss, test_correct / test_total))
# # save the better model
# if mean_train_loss < min_loss:
# min_loss = mean_train_loss
# # for file in os.listdir(checkpoint_root):
# # if file.startswith("classifier") and file.endswith(".tar"):
# # os.remove(checkpoint_root + file)
# torch.save({
# 'epoch': epoch,
# 'state_dict': model.state_dict(),
# 'optimizer': optimizer.state_dict(),
# 'train_losses': all_train_losses,
# 'test_losses': all_test_losses,
# 'min_loss': min_loss
# }, checkpoint_root + "classifier_" + '{:.4f}'.format(min_loss) + ".tar")
# epoch += 1
model.eval()
epoch_test_loss = list()
test_correct = 0
test_total = 0
with torch.no_grad():
for batch_idx, (img, label) in enumerate(test_loader):
img = img.to(device)
label = label.to(device)
out = model(img)
loss = criterion(out, label)
epoch_test_loss.append(loss.item())
_, predicted = torch.max(out.data, 1)
test_total += label.size()[0]
test_correct += (predicted == label).sum().item()
mean_test_loss = np.mean(np.array(epoch_test_loss))
all_test_losses.append(mean_test_loss)
print('====> Epoch: {} Average testing_loss: {:.4f}; testing accuracy: {:.4%}'.format(epoch, mean_test_loss, test_correct / test_total))