-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
79 lines (68 loc) · 2.11 KB
/
api.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
from flask import Flask
from flask import render_template
from flask import request
import os
import torch
from torchvision import datasets,transforms
from PIL import Image
app=Flask(__name__)
UPLOAD_FOLDER="/home/abhinav/kaggle/intel_image_classification/static/media/"
model_path='/home/abhinav/kaggle/intel_image_classification/models/resnet50.pth'
device=torch.device("cuda:0" if torch.cuda.is_available() else 'cpu')
image_transforms = {
'train': transforms.Compose([
transforms.RandomResizedCrop(size=256, scale=(0.8, 1.0)),
transforms.RandomRotation(degrees=15),
transforms.CenterCrop(size=224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
]),
'valid': transforms.Compose([
transforms.Resize(size=256),
transforms.CenterCrop(size=224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
])
}
def predict(image,model):
image=Image.open(image)
image_tensor = image_transforms['valid'](image).float()
image_tensor = image_tensor.unsqueeze_(0)
input = torch.autograd.Variable(image_tensor)
input = input.to(device)
output = model_ft(input)
index = output.data.cpu().numpy().argmax()
return index
@app.route("/", methods=["GET","POST"])
def upload_predict():
if request.method == "POST":
image_file=request.files["image"]
if image_file:
image_location=os.path.join(
UPLOAD_FOLDER,
image_file.filename
)
image_file.save(image_location)
pred=predict(image_location,model_ft)
print(pred)
if pred==0:
pred="buildings"
elif pred==1:
pred="forest"
elif pred==2:
pred="glacier"
elif pred==3:
pred="mountain"
elif pred==4:
pred="sea"
elif pred==5:
pred="street"
return render_template("index.html",prediction=pred,image_loc=image_file.filename)
return render_template("index.html",prediction=0, image_loc=None)
if __name__ == '__main__':
model_ft=torch.load(model_path)
model_ft.to(device)
model_ft.eval()
app.run(port=12000, debug=True)