-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcover_test_model.py
146 lines (107 loc) · 3.86 KB
/
cover_test_model.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
from testmodel import *
"""
Order predictions from most probable to least probable
"""
def getPredInOrder(preds):
res = []
cpupred = preds.cpu()
for i in range(len(preds)):
_ , best = torch.kthvalue(cpupred, len(cpupred) - i, 0)
res.append(int(best))
return res
"""
Get the top-k accuracy
"""
def getKsAccs(preds, labels, topK):
labels = labels.cpu()
labels = np.asarray(labels)
labels = labels.tolist()
predInOrder = []
for i in range(len(preds)):
tmp = getPredInOrder(preds[i])
predInOrder.append(tmp)
accs= []
for i in range(30):
accs.append(0)
for i in range(len(preds)):
for j in range(len(preds[i])):
if(predInOrder[i][j] == labels[i]):
accs[j] += 1
cumsum = 0
for i in range(len(accs)):
acc = accs[i]
accs[i] += cumsum
cumsum += acc
return accs[topK - 1]
"""
Print the top-k accuracy
"""
def print_acc(model, dataloader, dataset_size, topK, batch_size, device):
criterion = nn.CrossEntropyLoss()
running_loss = 0.0
running_corrects = 0
# Iterate over data.
progress = 0
lastPrint = 0
start = time.time()
myAcc= 0
for inputs, labels in dataloader:
progress += batch_size / dataset_size * 100
if(progress > 10 + lastPrint) or lastPrint == 0:
lastPrint = progress
print('Progress {:.2f}% time : {:.2f}'.format(progress, time.time() - start))
if type(inputs) is list or type(inputs) is tuple:
for i, input in enumerate(inputs):
inputs[i] = input.to(device)
else:
inputs = inputs.to(device)
labels = labels.to(device)
# forward
# track history if only in train
with torch.set_grad_enabled(False):
outputs = model(inputs)
getPredInOrder(outputs[0])
_, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)
myAcc += getKsAccs(outputs, labels, topK)
# statistics
if type(inputs) is list or type(inputs) is tuple:
running_loss += loss.item() * sum([input.size(0) for input in inputs])
else:
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
epoch_loss = running_loss / dataset_size
epoch_acc = running_corrects.double() / dataset_size
epoch_acc2 = float(myAcc) / dataset_size
print("MyAcc ", epoch_acc2)
end = time.time()
print('Loss: {:.4f} Acc: {:.4f}'.format(
epoch_loss, epoch_acc))
if __name__ == "__main__":
batch_size = 64
n_workers = 2
min_lr = 1e-4
max_lr = 6e-3
modelPath = "cover_final/64 w relu/model64"
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print("Loading model...")
model = load_resnet(18)
model = change_model(model, 10, 30)
model = model.to(device)
model.load_state_dict(torch.load(modelPath))
model.eval()
print("Loaded !")
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
cover_path = "dataset/covers"
csv_path = "dataset/book30-listing-test.csv"
image_dataset = BookDataset(csv_path, cover_path, transform=transform)
dataloader = torch.utils.data.DataLoader(image_dataset, batch_size=batch_size,
shuffle=True, num_workers=n_workers, pin_memory=False)
dataset_size = len(image_dataset)
class_names = image_dataset.classes
print_acc(model, dataloader, dataset_size, 0, batch_size, device)