I coded DeepLabV3+ model(segmentation_model_pytorch reference) but got error such as title:
Traceback (most recent call last):
File "train.py", line 135, in <module>
prd = model(img)
File "/home/***/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/home/***/DeeplabV3_plus/DLV3P.py", line 212, in forward
x = self.encoder(x)
File "/home/***/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/home/***/DeeplabV3_plus/DLV3P.py", line 82, in forward
x = self.layer1(x)
File "/home/***/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/home/***/.local/lib/python3.8/site-packages/torch/nn/modules/container.py", line 217, in forward
input = module(input)
File "/home/***/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/home/***/DeeplabV3_plus/DLV3P.py", line 33, in forward
x = self.conv1(x)
File "/home/***/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/home/***/.local/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 463, in forward
return self._conv_forward(input, self.weight, self.bias)
File "/home/***/.local/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 459, in _conv_forward
return F.conv2d(input, weight, bias, self.stride,
RuntimeError: Given groups=1, weight of size [64, 64, 1, 1], expected input[64, 32, 63, 63] to have 64 channels, but got 32 channels instead
I fixed with x.permute when N,C,H,W was getting wrong position but it still shows error like it.
My model code is below:
import torch
from torch import nn
import numpy as np
class BottleNeck(nn.Module):
def __init__(self, in_channels, out_channels, stride=1, dilation=1, down=True):
super(BottleNeck, self).__init__()
self.down = down
self.dilation = dilation
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(num_features=out_channels, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, padding=self.dilation, dilation=self.dilation, bias=False)
self.bn2 = nn.BatchNorm2d(num_features=out_channels, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
self.conv3 = nn.Conv2d(out_channels, out_channels*4, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(num_features=out_channels*4, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
self.relu = nn.ReLU(inplace=True)
self.downsample = nn.Sequential(
nn.Conv2d(in_channels, out_channels*4, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(num_features=out_channels*4)
)
self.stride = stride
def forward(self, x):
"""residual = x
out = self.relu(self.bn1(self.conv1(x)))
out = self.relu(self.bn2(self.conv2(out)))
out = self.bn3(self.conv3(out))
if self.down:
residual = self.downsample(residual)
out += residual
out = self.relu(out)"""
x = self.conv1(x)
x = self.bn1(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.conv3(x)
x = self.bn3(x)
x = self.relu(x)
if self.down:
x = self.downsample(x)
return x
class ResNetEncoder(nn.Module):
def __init__(self):
super(ResNetEncoder, self).__init__()
self.block = BottleNeck
self.in_channels = 64
self.conv1 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=7, stride=2, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(num_features=64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2)
self.layer1 = nn.Sequential(
self.block(64, 64),
self.block(256, 64, down=False),
self.block(256, 64, down=False)
)
self.layer2 = nn.Sequential(
self.block(256, 128, stride=2),
self.block(512, 128, down=False),
self.block(512, 128, down=False),
self.block(512, 128, down=False)
)
self.layer3 = nn.Sequential()
for i in range(23):
if i == 0:
self.layer3.add_module('block{}'.format(i), self.block(512, 256, stride=2))
else:
self.layer3.add_module('block{}'.format(i), self.block(1024, 256, down=False))
self.layer4 = nn.Sequential(
self.block(1024, 512, stride=1, dilation=2),
self.block(2048, 512, down=False, dilation=2),
self.block(2048, 512, down=False, dilation=2)
)
def forward(self, x):
#x = x.permute(1,0,2,3)
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = x.permute(1,0,2,3)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
return x
class ASPPSeparableConv(nn.Module):
def __init__(self, in_channels, out_channels, dilation):
super(ASPPSeparableConv, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=dilation, dilation=dilation, groups=in_channels, bias=False),
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, bias=False)
)
self.bn = nn.BatchNorm2d(num_features=out_channels)
self.relu = nn.ReLU()
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.relu(x)
return x
class ASPPPooling(nn.Module):
def __init__(self, in_channels, out_channels):
super(ASPPPooling, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, bias=False)
self.bn = nn.BatchNorm2d(num_features=out_channels)
self.relu = nn.ReLU()
self.pool = nn.AdaptiveAvgPool2d(output_size=1)
def forward(self, x):
x = self.pool(x)
x = self.conv(x)
x = self.bn(x)
x = self.relu(x)
return x
class UpSamplingBiLinear(nn.Module):
def __init__(self, scale_factor):
super(UpSamplingBiLinear, self).__init__()
self.scale_factor = scale_factor
def forward(self, x):
size = x.shape[-2:]
x = nn.functional.interpolate(x, size=(size[0]*self.scale_factor, size[1]*self.scale_factor), mode='bilinear', align_corners=True)
return x
class ASPP(nn.Module):
def __init__(self):
super(ASPP, self).__init__()
in_channels = 2048
out_channels = 256
self.convs = nn.ModuleList([
nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(num_features=out_channels),
nn.ReLU()
),
ASPPSeparableConv(in_channels, out_channels, dilation=12),
ASPPSeparableConv(in_channels, out_channels, dilation=24),
ASPPSeparableConv(in_channels, out_channels, dilation=36),
ASPPPooling(in_channels, out_channels)
])
self.project = nn.Sequential(
nn.BatchNorm2d(num_features=out_channels, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True),
nn.ReLU(),
nn.Dropout(p=0.5, inplace=False)
)
self.SeparableConv2d = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1, groups = out_channels, bias=False),
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, bias=False),
)
self.bn = nn.BatchNorm2d(num_features=out_channels, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
self.relu = nn.ReLU()
def forward(self, x):
x = torch.cat([conv(x) for conv in self.convs])
x = self.SeparableConv2d(x)
x = self.bn(x)
x = self.relu(x)
x = self.project(x)
return x
class DeepLabV3PlusDecoder(nn.Module):
def __init__(self, num_classes):
super(DeepLabV3PlusDecoder, self).__init__()
self.aspp = ASPP()
self.up = UpSamplingBiLinear(scale_factor=4)
self.block1 = nn.Sequential(
nn.Conv2d(in_channels=256, out_channels=48, kernel_size=1, bias=False),
nn.BatchNorm2d(num_features=48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True),
nn.ReLU()
)
self.block2 = nn.Sequential(
nn.Conv2d(in_channels=304, out_channels=304, kernel_size=3, stride=1, padding=1, groups = 304, bias=False),
nn.Conv2d(in_channels=304, out_channels=256, kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(num_features=256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True),
nn.ReLU()
)
self.segmentation_head = nn.Sequential(
nn.Conv2d(in_channels=256, out_channels=num_classes, kernel_size=1, stride=1),
nn.UpsamplingBilinear2d(scale_factor=4),
nn.Sigmoid()
)
self.SeparableConv2d = nn.Sequential(
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1, groups = 256, bias=False),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=1, stride=1, bias=False),
)
self.bn = nn.BatchNorm2d(num_features=256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
self.relu = nn.ReLU()
def forward(self, x):
x = self.aspp(x)
x = self.up(x)
low_level_feature = self.block1(x)
x = torch.cat((x, low_level_feature), dim=1)
x = self.block2(x)
x = self.SeparableConv2d(x)
x = self.bn(x)
x = self.relu(x)
x = self.segmentation_head(x)
return x
class DeepLabV3Plus(nn.Module):
def __init__(self, num_classes):
super(DeepLabV3Plus, self).__init__()
self.encoder = ResNetEncoder()
self.decoder = DeepLabV3PlusDecoder(num_classes=num_classes)
def forward(self, x):
x = x.permute(1, 0, 2, 3)
x = self.encoder(x)
x = self.decoder(x)
return x
Is there my fault? Or, is my data inputted wrong?