-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommons.py
More file actions
75 lines (61 loc) · 2.73 KB
/
Copy pathcommons.py
File metadata and controls
75 lines (61 loc) · 2.73 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
import io
from PIL import Image
from torchvision import models
import torch.nn as nn
import torchvision.transforms as transforms
import torch
import numpy as np
import torch.nn.functional as F
import cv2
path = 'model.ckpt'
class SimpleCNN(nn.Module):
def __init__(self):
# ancestor constructor call
super(SimpleCNN, self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, padding=2)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=2)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=2)
self.conv4 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=2)
self.conv5 = nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=2)
self.bn1 = nn.BatchNorm2d(32)
self.bn2 = nn.BatchNorm2d(64)
self.bn3 = nn.BatchNorm2d(128)
self.bn4 = nn.BatchNorm2d(256)
self.bn5 = nn.BatchNorm2d(512)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.avg = nn.AvgPool2d(8)
self.fc = nn.Linear(512 * 1 * 1, 2) # !!!
def forward(self, x):
x = self.pool(F.leaky_relu(self.bn1(self.conv1(x)))) # first convolutional layer then batchnorm, then activation then pooling layer.
x = self.pool(F.leaky_relu(self.bn2(self.conv2(x))))
x = self.pool(F.leaky_relu(self.bn3(self.conv3(x))))
x = self.pool(F.leaky_relu(self.bn4(self.conv4(x))))
x = self.pool(F.leaky_relu(self.bn5(self.conv5(x))))
x = self.avg(x)
#print(x.shape) # lifehack to find out the correct dimension for the Linear Layer
x = x.view(-1, 512 * 1 * 1) # !!!
x = self.fc(x)
return x
checkpoint = torch.load(path,map_location='cpu')
def get_model():
model = SimpleCNN()
model.load_state_dict(checkpoint)
model.eval()
return model
def transform_image(image_bytes):
my_transforms = transforms.Compose([transforms.ToPILImage(),
transforms.Pad(64, padding_mode='reflect'),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5],std=[0.5, 0.5, 0.5])])
nparr = np.fromstring(image_bytes, np.uint8)
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
#image = Image.open(io.BytesIO(image_bytes))
#image = np.array(image)
return my_transforms(image).unsqueeze_(0).float()
# ImageNet classes are often of the form `can_opener` or `Egyptian_cat`
# will use this method to properly format it so that we get
# `Can Opener` or `Egyptian Cat`
def format_class_name(class_name):
class_name = class_name.replace('_', ' ')
class_name = class_name.title()
return class_name