-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmoto_clf.py
64 lines (58 loc) · 1.8 KB
/
moto_clf.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
#Pytorch
import torch
import torchvision
from torchvision import transforms
#ML net
from ml_models.net import Net
#image Managment
from PIL import Image
import requests
from io import BytesIO
def load_model():
#pretrained model path
PATH = 'ml_models/model.pth'
# Initializate model
model = Net(64)
#Call pretrained model from path
model.load_state_dict(torch.load(PATH))
model.eval()
return model
def transform_image(path):
'''
Make specific tranformations to an image
in order to pass through the net.
'''
#Trasnformations
preprocess = transforms.Compose ([
transforms.Resize(64),
transforms.CenterCrop(64),
transforms.ToTensor(),
transforms.Normalize(
mean = [0.485, 0.456, 0.406],
std = [0.229, 0.224, 0.225]
)])
#get response from url
response = requests.get(path)
#Open image & convert to rgb
img = Image.open(BytesIO(response.content)).convert('RGB')
#Pass through preprocess
img_t = preprocess(img)
#Flat tensor
processed_img = torch.unsqueeze(img_t, 0)
return processed_img
def predict(img, model):
#Stablishing all Moto cathegories
CATHEGORIES = ['off_road',
'naked',
'scooter',
'scrambler',
'cafe_racer',
'cruiser',
'sport',
'touring',
'bike',
'quad']
#Call prediciton from model
outputs = model(img)
_, predicted = torch.max(outputs, 1)
return CATHEGORIES[predicted]