-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusable_model.py
198 lines (171 loc) · 5.41 KB
/
usable_model.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#By Samay Gandhi
import sys
import numpy as np
from PIL import Image
import torch
import torch.nn as nn
import torchvision.transforms.functional as TF
img = Image.open(sys.argv[1])
img = TF.Normalize(
TF.to_tensor(
TF.resize(
img, size=(256,256))
),
mean=(0.485, 0.456, 0.406),std=(0.229, 0.224, 0.225)
)
#Define the CNN block now
#Defined as per the U-net Structure
#Made some modifications too to the original structure
class DoubleCNNBlock(nn.Module):
def __init__(self,in_channels,out_channels):
super().__init__()
self.conv1 = nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=3,
padding=1,
stride=1,
bias=False
)
self.bn1 = nn.BatchNorm2d(
out_channels
)
self.act1 = nn.ReLU()
self.conv2 = nn.Conv2d(
in_channels=out_channels,
out_channels=out_channels,
kernel_size=3,
padding=1,
stride=1,
bias=False
)
self.bn2 = nn.BatchNorm2d(
out_channels
)
self.act2 = nn.ReLU()
def forward(self,x):
out = self.act1(self.bn1(self.conv1(x)))
out = self.act2(self.bn2(self.conv2(out)))
return out
class UpConv(nn.Module):
def __init__(self,in_channels,out_channels):
super().__init__()
self.tconv = nn.ConvTranspose2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=2,
stride=2
)
def forward(self,x,skip_connection):
out = self.tconv(x)
if out.shape != skip_connection.shape:
out = TF.resize(out ,size=skip_connection.shape[2:])
out = torch.cat([skip_connection,out],axis = 1)
return out
class Bottom(nn.Module):
def __init__(self,channel=[128,256]):
super().__init__()
self.channel=channel
self.conv1 = nn.Conv2d(
in_channels=self.channel[0],
out_channels=self.channel[1],
kernel_size=3,
padding=1,
stride=1,
bias=False
)
self.bn1 = nn.BatchNorm2d(
self.channel[1]
)
self.act1 = nn.ReLU()
self.conv2 = nn.Conv2d(
in_channels=self.channel[1],
out_channels=self.channel[1],
kernel_size=3,
padding=1,
stride=1,
bias=False
)
self.bn2 = nn.BatchNorm2d(
self.channel[1]
)
self.act2 = nn.ReLU()
self.bottom = nn.Sequential(
self.conv1,
self.bn1,
self.act1,
self.conv2,
self.bn2,
self.act2
)
def forward(self,x):
# out = self.act1(self.bn1(self.conv1(x)))
# print("1:{}".format(out.shape))
# out = self.act2(self.bn2(self.conv2(out)))
# print("2:{}".format(out.shape))
return self.bottom(x)
class Unet(nn.Module):
def __init__(self,num_classes,filters=[16,32,64,128],input_channels=3):
super().__init__()
self.contract = nn.ModuleList()
self.expand = nn.ModuleList() #64 - #128 - #256 - #512 - #1024 -#512
self.filters = filters
self.input_channels = input_channels
self.num_classes = num_classes
self.pool = nn.MaxPool2d(
kernel_size=2,
stride=2
)
for filters in self.filters:
self.contract.append(
DoubleCNNBlock(
in_channels=input_channels,
out_channels=filters
)
)
input_channels = filters
for filters in reversed(self.filters):
self.expand.append(
UpConv(
in_channels=filters*2,
out_channels=filters
)
)
self.expand.append(
DoubleCNNBlock(
in_channels=filters*2,
out_channels=filters
)
)
self.final = nn.Conv2d(
in_channels=self.filters[0],
out_channels=num_classes,
kernel_size=3,
padding=1,
stride=1
)
def forward(self,x):
skip_connections = []
for downs in self.contract:
out = downs(x)
skip_connections.append(out)
out = self.pool(out)
x = out
bottom = Bottom()
bottom.to(DEVICE)
y = bottom(x)
for idx in range(0,len(self.expand),2):
skip_connection = skip_connections[len(skip_connections)-idx//2-1]
y = self.expand[idx](y,skip_connection)
y = self.expand[idx+1](y)
return self.final(y)
model = Unet(num_classes=8)
model = torch.load('SegmentationModel')
#model.to(DEVICE)
preds = model(img)
final_image = torch.argmax(F.softmax(preds,dim=1),axis=1)
from skimage.color import label2rgb
rgb_image = label2rgb(img.view(256,256).detach().cpu().numpy())
import subprocess
p = subprocess.Popen(["display", rgb_image])
p.kill()