-
Notifications
You must be signed in to change notification settings - Fork 0
/
09_predict_fastai.py
executable file
·63 lines (47 loc) · 1.62 KB
/
09_predict_fastai.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
#!/usr/bin/env python3
from torch.autograd import Variable
from models import resnext_50
from torch import nn
from other.layers import AdaptiveConcatPool2d, Flatten
import torch
import torchvision.datasets as dsets
from other.utils import load_model
import torchvision.transforms as transforms
from PIL import Image
def layer0():
return [AdaptiveConcatPool2d(), Flatten()]
def layer1():
model = [nn.BatchNorm1d(num_features=4096)]
model.append(nn.Dropout(p=0.25))
model.append(nn.Linear(in_features=4096, out_features=512))
model.append(nn.ReLU())
return model
def layer2():
model = [nn.BatchNorm1d(num_features=512)]
model.append(nn.Dropout(p=0.5))
model.append(nn.Linear(in_features=512, out_features=12))
model.append(nn.LogSoftmax())
return model
def cut_model(m, cut):
return list(m.children())[:cut] if cut else [m]
def build_model():
model = resnext_50.resnext_50_32x4d()
model = cut_model(model, 8)
model = model + layer0() + layer1() + layer2()
model = nn.Sequential(*model)
return model
imsize = 250
preprocess = transforms.Compose([transforms.Resize(imsize), transforms.ToTensor()])
def image_loader(image_name):
model.eval()
image = Image.open(image_name)
image = preprocess(image).unsqueeze(0)
image = Variable(image)
val = model(image)
predicted = torch.max(val, 1)
return predicted[1].data.numpy()
if __name__ == '__main__':
model = build_model()
model = load_model(model, '../data/models/resnext_50_all_data.h5')
res = image_loader('/Users/krishnakalyan3/Educational/Plant/data/test/0ad9e7dfb.png')
print(res)