Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
101 lines (81 loc) · 2.38 KB
/
Copy pathapp.py
File metadata and controls
101 lines (81 loc) · 2.38 KB
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
import io
import logging
import albumentations as A
import jsonpickle
import numpy as np
import torch
from albumentations.pytorch.transforms import ToTensorV2 as ToTensor
from flask import Flask, request
from PIL import Image
from torch import nn
from train import RoomEfficientNet
from utils import crop_center_square
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
app = Flask(__name__)
image_size = 300
batch_size = 64
num_classes = 7
efficientnet = "efficientnet-b3"
device = "cpu"
if torch.cuda.is_available():
device = "cuda"
net = RoomEfficientNet.load_from_checkpoint(
checkpoint_path="model.ckpt",
num_classes=num_classes,
efficientnet=efficientnet,
weights_path="efficientnet-b3-5fb5a3c3.pth",
)
net.to(device)
net.eval()
net.freeze()
app.logger.debug("model loaded!")
transform = A.Compose(
[
A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
ToTensor(),
]
)
label2idx = {
"interior": 0,
"bathroom": 1,
"bedroom": 2,
"exterior": 3,
"living_room": 4,
"kitchen": 5,
"dining_room": 6,
}
softmax = nn.Softmax(dim=1)
@app.route("/", methods=["POST"])
def classify_room():
"""Receive everything in json!!!"""
app.logger.debug(f"Receiving data ...")
data = request.json
data = jsonpickle.decode(data)
app.logger.debug(f"decompressing image ...")
image = data["image"]
image = io.BytesIO(image)
app.logger.debug(f"Reading a PIL image ...")
image = Image.open(image)
if image.mode != "RGB":
image = image.convert("RGB")
app.logger.debug(f"cropping and resizing image ...")
image = crop_center_square(image)
image = image.resize(size=(image_size, image_size))
image = transform(image=np.array(image))["image"]
image = image.to(device)
app.logger.debug("Running the room-classifier ...")
pred = net(image.unsqueeze(0))
pred = softmax(pred).detach().cpu().numpy().squeeze()
pred = pred.tolist()
assert len(pred) == len(label2idx)
results = {label: pred[idx] for label, idx in label2idx.items()}
app.logger.info(f"results: {results}")
response = jsonpickle.encode(results)
app.logger.info("json-pickle is done.")
return response
if __name__ == "__main__":
app.run(host="0.0.0.0", port=10005)