-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
119 lines (82 loc) · 3.09 KB
/
predict.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
import os
from dataset import KenyanFood13Dataset
from augmentation import (
image_resize,
image_preprocess,
common_transforms,
data_aug,
extra_data_aug
)
from settings import cfg
import torch
from torch.utils.data import DataLoader
import torch.nn.functional as F
import pandas as pd
def prediction(model, batch_input):
model.to(cfg.device)
model.eval()
data = batch_input.to(cfg.device)
bs, ncrops, c, h, w = data.size()
output = model(data.view(-1, c, h, w))
output = output.view(bs, ncrops, -1).mean(1) # <----- max?
prob = F.softmax(output, dim=1)
pred_prob = prob.data.max(dim=1)[0]
pred_index = prob.data.max(dim=1)[1]
return pred_index.cpu().numpy(), pred_prob.cpu().numpy()
def average_multiple_predictions(models, inputs, targets):
pred_prob = prob.data.max(dim=1)[0]
pred_index = prob.data.max(dim=1)[1]
return pred_index.cpu().numpy(), pred_prob.cpu().numpy()
def get_results(model):
train_dataset = KenyanFood13Dataset(common_transforms(cfg.mean, cfg.std))
test_dataset = KenyanFood13Dataset(transform=common_transforms(cfg.mean, cfg.std), train=False)
test_loader = DataLoader(
test_dataset,
batch_size=cfg.batch_size,
shuffle=False,
num_workers=cfg.num_workers)
predictions = []
for batch in test_loader:
idx, prob = prediction(model, batch_input=batch)
for target in idx:
predictions.append(train_dataset.index_to_class(target))
classes = pd.DataFrame(predictions, columns=['class'])
result = test_dataset.label_df.join(classes)
return result
def combine_and_get_results(models):
train_dataset = KenyanFood13Dataset(common_transforms(cfg.mean, cfg.std))
test_dataset = KenyanFood13Testset(transform=common_transforms(cfg.mean, cfg.std))
test_loader = DataLoader(test_dataset,
batch_size=cfg.batch_size,
shuffle=False,
num_workers=cfg.num_workers)
model_1, model_2, model_3, model_4, model_5 = models
model_1.to(cfg.device)
model_1.eval()
model_2.to(cfg.device)
model_2.eval()
model_3.to(cfg.device)
model_3.eval()
model_4.to(cfg.device)
model_4.eval()
model_5.to(cfg.device)
model_5.eval()
predictions = []
for data in test_loader:
data = data.to(cfg.device)
output_m1 = model_1(data)
output_m2 = model_2(data)
output_m3 = model_3(data)
output_m4 = model_4(data)
output_m5 = model_5(data)
output = torch.sum(output_m1, output_m2, output_m3, output_m4, output_m5) / 5.
outputs = F.softmax(output, 1)
pred_prob = outputs.data.max(dim=1)[0]
pred_index = outputs.data.max(dim=1)[1]
pred_index.cpu().numpy()
pred_prob.cpu().numpy()
for target in pred_index:
predictions.append(train_dataset.index_to_class(target))
classes = pd.DataFrame(predictions, columns=['class'])
result = test_dataset.label_df.join(classes)
return result