I’m trying to create my own custom model by using resnet50 as the body and defining my own head
class stdConv(nn.Module):
def __init__(self, nin, nout, stride, drop=0.1):
super().__init__()
self.conv1 = nn.Conv2d(nin, nout, kernel_size = 3, stride = stride, padding = 1)
self.bn = nn.BatchNorm2d(nout)
self.drop = nn.Dropout(drop)
def forward(self, x):
x = F.relu(self.conv1(x))
x = self.bn(x)
x = self.drop(x)
return x
flattening method
def flatten_conv(x, k):
bs, nf, gx, gy = x.size()
x = x.permute(0, 2, 3, 1).contiguous()
return x.view(bs, -1, nf//k)
class outConv(nn.Module):
def __init__(self, k, nin, bias):
super().__init__()
self.oconv1 = nn.Conv2d(nin, (len(cat)+1)*k, 3, padding = 1)
self.oconv2 = nn.Conv2d(nin, 4*k, 3, padding = 1)
self.oconv1.bias.data.zero_().add(bias)
def forward(self, x):
return [flatten_conv(self.oconv1(x), self.k),
flatten_conv(self.oconv2(x), self.k)])
a class that defines the architecture head
class myHead(nn.Module):
def __init__(self, bias, k):
super().__init__()
self.drop = nn.Dropout(0.25)
self.sconv1 = stdConv(nin = 2048, nout = 1024, stride = 2)
self.sconv2 = stdConv(nin = 1024, nout = 512, stride = 2)
self.sconv3 = stdConv(nin = 512, nout = 256, stride = 2)
self.sconv4 = stdConv(nin = 256, nout = 256, stride = 2)
self.oconv = outConv(k, 256, bias)
def forward(self, x):
x = self.drop(F.relu(x))
print(x)
x = self.sconv1(x)
x = self.sconv2(x)
x = self.sconv3(x)
x = self.sconv4(x)
x = self.oconv(x)
return x
class myModel(Module):
def __init__(self, encoder, head):
self.encoder, self.head = encoder, head
def forward(self, x):
return self.head(self.encoder)
create the head of the model
myhead = myHead(3, 1)
create the body of the model
encoder = create_body(resnet50, n_in=3, pretrained=True, cut=-2)
merge the body and the head together to create the model
modely = myModel(encoder, myhead)
learn = Learner(dls = dl, model = modely)
return one batch from the training set
d, i, z = dl.one_batch()