-
Notifications
You must be signed in to change notification settings - Fork 34
/
model.py
128 lines (109 loc) · 5.15 KB
/
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
from __future__ import print_function
from __future__ import division
import torch
import torch.nn as nn
import numpy as np
from torchvision import models, transforms
import time
import resnet_seg_ae as resnet
# Initialize and Reshape the Encoders
def initialize_encoder(model_name, num_classes, use_pretrained=True):
# Initialize these variables which will be set in this if statement. Each of these
# variables is model specific.
model_ft = None
if model_name == "resnet18":
""" Resnet18
"""
#model_ft = models.resnet18(pretrained=use_pretrained)
model_ft = resnet.resnet18(pretrained=use_pretrained, num_classes=1000)
#set_parameter_requires_grad(model_ft)
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, num_classes)
elif model_name == "resnet34":
""" Resnet34
"""
#model_ft = models.resnet34(pretrained=use_pretrained)
model_ft = resnet.resnet34(pretrained=use_pretrained, num_classes=1000)
#set_parameter_requires_grad(model_ft)
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, num_classes)
elif model_name == "resnet50":
""" Resnet50
"""
#model_ft = models.resnet50(pretrained=use_pretrained)
model_ft = resnet.resnet50(pretrained=use_pretrained, num_classes=1000)
#set_parameter_requires_grad(model_ft)
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, num_classes)
elif model_name == "resnet101":
""" Resnet101
"""
#model_ft = models.resnet101(pretrained=use_pretrained)
model_ft = resnet.resnet101(pretrained=use_pretrained, num_classes=1000)
#set_parameter_requires_grad(model_ft)
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, num_classes)
else:
print("Invalid model name, exiting...")
exit()
return model_ft
# full model
class SegNet(nn.Module):
def __init__(self, encoder, num_classes):
super(SegNet, self).__init__()
self.resnet = encoder
self.upsample = nn.Upsample(scale_factor=2, mode='nearest')
# resnet50
# self.conv1 = nn.Conv2d(2048, 1024, kernel_size=3, stride=1, padding=1)
# self.conv2 = nn.Conv2d(2048, 512, kernel_size=3, stride=1, padding=1)
# self.conv3 = nn.Conv2d(1024, 256, kernel_size=3, stride=1, padding=1)
# self.conv4 = nn.Conv2d(512, 64, kernel_size=3, stride=1, padding=1)
# self.conv5 = nn.Conv2d(128, 3, kernel_size=3, stride=1, padding=1)
# #self.conv5 = nn.Conv2d(128, 1, kernel_size=3, stride=1, padding=1)
# self.conv11 = nn.Conv2d(2048, 1024, kernel_size=3, stride=1, padding=1)
# self.conv22 = nn.Conv2d(2048+1024, 512, kernel_size=3, stride=1, padding=1)
# self.conv33 = nn.Conv2d(1024+512, 256, kernel_size=3, stride=1, padding=1)
# self.conv44 = nn.Conv2d(512+256, 64, kernel_size=3, stride=1, padding=1)
# self.conv55 = nn.Conv2d(128+64, 1, kernel_size=3, stride=1, padding=1)
# resnet18/34
self.conv1 = nn.Conv2d(512, 256, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv2d(512, 128, kernel_size=3, stride=1, padding=1)
self.conv3 = nn.Conv2d(256, 64, kernel_size=3, stride=1, padding=1)
self.conv4 = nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1)
#self.conv5 = nn.Conv2d(128, 1, kernel_size=3, stride=1, padding=1)
self.conv5 = nn.Conv2d(128, 3, kernel_size=3, stride=1, padding=1)
self.conv11 = nn.Conv2d(512, 256, kernel_size=3, stride=1, padding=1)
self.conv22 = nn.Conv2d(512+256, 128, kernel_size=3, stride=1, padding=1)
self.conv33 = nn.Conv2d(256+128, 64, kernel_size=3, stride=1, padding=1)
self.conv44 = nn.Conv2d(128+64, 64, kernel_size=3, stride=1, padding=1)
self.conv55 = nn.Conv2d(128+64, 1, kernel_size=3, stride=1, padding=1)
self.relu = nn.ReLU(inplace=True)
self.sigmoid = nn.Sigmoid()
def forward(self, images):
x4, x3, x2, x1, x0 = self.resnet(images)
# decoder
# lay
x3_ = self.relu(self.conv1(self.upsample(x4)))
x3_c = torch.cat((x3_, x3),1)
x2_ = self.relu(self.conv2(self.upsample(x3_c)))
x2_c = torch.cat((x2_, x2),1)
x1_ = self.relu(self.conv3(self.upsample(x2_c)))
x1_c = torch.cat((x1_, x1),1)
x0_ = self.relu(self.conv4(self.upsample(x1_c)))
x0_c = torch.cat((x0_, x0),1)
x0_cs = self.sigmoid(self.conv5(self.upsample(x0_c)))
# cor
x = self.relu(self.conv11(self.upsample(x4)))
x = torch.cat((x, x3_c),1)
x = self.relu(self.conv22(self.upsample(x)))
x = torch.cat((x, x2_c),1)
x = self.relu(self.conv33(self.upsample(x)))
x = torch.cat((x, x1_c),1)
x = self.relu(self.conv44(self.upsample(x)))
x = torch.cat((x, x0_c),1)
x = self.sigmoid(self.conv55(self.upsample(x)))
return x0_cs, x
# Set Model Parameters, requires_grad attribute
def set_parameter_requires_grad(model):
for param in model.parameters():
param.requires_grad = True