hey ! i got error in my code
here is the link(https://jovian.ml/chamschamanthi36/mini-pro-error/v/1&cellId=9)
i used lenet 5 architecture
hey ! i got error in my code
here is the link(https://jovian.ml/chamschamanthi36/mini-pro-error/v/1&cellId=9)
i used lenet 5 architecture
Similar! Nedd help plz! What I try to implement is writing a Basic block based on this paper and github.
“https://arxiv.org/pdf/2006.11538.pdf”
“https://github.com/iduta/pyconv/blob/master/models/pyconvresnet.py”
Error window:
File “D:/Github_code/Wujie/main_fer.py”, line 333, in
train(epoch)
File “D:/Github_code/Wujie/main_fer.py”, line 203, in train
outputs = net(inputs)
File “C:\Users\zhy34\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 722, in _call_impl
result = self.forward(*input, **kwargs)
File “D:\Github_code\Wujie\models\pyconvresnet.py”, line 314, in forward
x = self.layer2(x)
File “C:\Users\zhy34\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 722, in _call_impl
result = self.forward(*input, **kwargs)
File “C:\Users\zhy34\anaconda3\lib\site-packages\torch\nn\modules\container.py”, line 117, in forward
input = module(input)
File “C:\Users\zhy34\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 722, in _call_impl
result = self.forward(*input, **kwargs)
File “D:\Github_code\Wujie\models\pyconvresnet.py”, line 226, in forward
out = F.relu(self.bn1(self.conv1(x)))
File “C:\Users\zhy34\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 722, in _call_impl
result = self.forward(*input, **kwargs)
File “D:\Github_code\Wujie\models\pyconvresnet.py”, line 114, in forward
return torch.cat((self.conv2_1(x), self.conv2_2(x), self.conv2_3(x)), dim=1)
File “C:\Users\zhy34\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 722, in _call_impl
result = self.forward(*input, **kwargs)
File “C:\Users\zhy34\anaconda3\lib\site-packages\torch\nn\modules\conv.py”, line 419, in forward
return self._conv_forward(input, self.weight)
File “C:\Users\zhy34\anaconda3\lib\site-packages\torch\nn\modules\conv.py”, line 415, in _conv_forward
return F.conv2d(input, weight, self.bias, self.stride,
RuntimeError: Given groups=1, weight of size [32, 128, 3, 3], expected input[128, 64, 11, 11] to have 128 channels, but got 64 channels instead
Process finished with exit code 1
Here is part of my code.
class PyConvBasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None, norm_layer=None, pyconv_groups=1, pyconv_kernels=1):
super(PyConvBasicBlock, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
# Both self.conv1 and self.downsample layers downsample the input when stride != 1
self.conv1 = get_pyconv(planes, planes, pyconv_kernels=pyconv_kernels, stride=stride,
pyconv_groups=pyconv_groups)
self.bn1 = norm_layer(planes)
#self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = norm_layer(planes)
self.shortcut = nn.Sequential()
if stride != 1 or inplanes != self.expansion*planes:
self.shortcut = nn.Sequential(
nn.Conv2d(inplanes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(self.expansion*planes)
)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out += self.shortcut(x)
out = F.relu(out)
return out
class PyConvResNet(nn.Module):
def __init__(self, block, layers, num_classes=7, zero_init_residual=False, norm_layer=None, dropout_prob0=0.5):
super(PyConvResNet, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self.inplanes = 64
#was
#self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = norm_layer(64)
self.relu = nn.ReLU(inplace=True)
self.layer1 = self._make_layer(block, 64, layers[0], stride=2, norm_layer=norm_layer,
pyconv_kernels=[3, 5, 7, 9], pyconv_groups=[1, 4, 8, 16])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2, norm_layer=norm_layer,
pyconv_kernels=[3, 5, 7], pyconv_groups=[1, 4, 8])
self.layer3 = self._make_layer(block, 256, layers[2], stride=2, norm_layer=norm_layer,
pyconv_kernels=[3, 5], pyconv_groups=[1, 4])
self.layer4 = self._make_layer(block, 512, layers[3], stride=2, norm_layer=norm_layer,
pyconv_kernels=[3], pyconv_groups=[1])
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
if dropout_prob0 > 0.0:
self.dp = nn.Dropout(dropout_prob0, inplace=True)
print("Using Dropout with the prob to set to 0 of: ", dropout_prob0)
else:
self.dp = None
self.fc = nn.Linear(512 * block.expansion, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
# Zero-initialize the last BN in each residual branch,
# so that the residual branch starts with zeros, and each residual block behaves like an identity.
# This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
if zero_init_residual:
for m in self.modules():
if isinstance(m, PyConvBasicBlock) or isinstance(m, PyConvBottleneck):
nn.init.constant_(m.bn3.weight, 0)
def _make_layer(self, block, planes, blocks, stride=1, norm_layer=None, pyconv_kernels=[3], pyconv_groups=[1]):
if norm_layer is None:
norm_layer = nn.BatchNorm2d
downsample = None
if stride != 1 and self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.MaxPool2d(kernel_size=3, stride=stride, padding=1),
conv1x1(self.inplanes, planes * block.expansion),
norm_layer(planes * block.expansion),
)
elif self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
conv1x1(self.inplanes, planes * block.expansion),
norm_layer(planes * block.expansion),
)
elif stride != 1:
downsample = nn.MaxPool2d(kernel_size=3, stride=stride, padding=1)
layers = []
layers.append(block(self.inplanes, planes, stride=stride, downsample=downsample, norm_layer=norm_layer,
pyconv_kernels=pyconv_kernels, pyconv_groups=pyconv_groups))
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes, norm_layer=norm_layer,
pyconv_kernels=pyconv_kernels, pyconv_groups=pyconv_groups))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
if self.dp is not None:
x = self.dp(x)
x = self.fc(x)
return x
Any help will be appreciated!!!
The first layer in your model expects an input with a single input channel, while you are passing image tensors with 3 channels.
You could either use in_channels=3
in the first conv layer or reduce the number of channels in the input image to 1.
The error is raised in self.layer2
so you could add print statements in the forward
method of this module and check the shapes of the input as well as the intermediate activations.
I guess self.conv1
might raise this error, as it’s not using inplanes
as the number of input channels but planes
. However, since the code isn’t executable I cannot debug so this is just a guess.
PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.
when i changed in_channels=3, i got this error RuntimeError: Given groups=1, weight of size [6, 3, 5, 5], expected input[100, 1, 32, 32] to have 3 channels, but got 1 channels instead
here is the error
This could mean that your dataset contains grayscale (single channel) as well as RGB images.
If that’s the case you could transform all images e.g. via transforms.Grayscale
and make sure they all have the same number of channels.
Hi, I’m getting a very similar error but I don’t know what’s wrong with it.
My error:
Given groups=1, weight of size [64, 3, 3, 3], expected input[15, 1, 224, 224] to have 3 channels, but got 1 channels instead
class NucleusDataset(Dataset):
def __init__(self, split): #split = train or test super().__init__() self.root = '/content/drive/My Drive/Nucleus dataset/' self.allfolderlist = listdir(self.root) print("There are "+str(len(self.allfolderlist))+" data totally") self.folderlist = [] self.split = split if self.split == 'train': print("get dataset: train") for n in range(int(len(self.allfolderlist)/2)): self.folderlist.append(self.allfolderlist[n]) print("There are "+str(len(self.folderlist))+" data in training dataset") elif self.split == 'test': print("get dataset: test") for n in range(int(len(self.allfolderlist)/2),int(len(self.allfolderlist))): self.folderlist.append(self.allfolderlist[n]) print("There are "+str(len(self.folderlist))+" data in testing dataset") else: print("split parameter is not setted") def __len__(self): return len(self.folderlist) def __getitem__(self, index): foldername = self.folderlist[index] filename = foldername.split(".")[0]+".png" img = cv2.imread(self.root+foldername+"/images/"+filename) img = cv2.resize(img, (224, 224), interpolation=cv2.INTER_LINEAR) img_np = np.array(img, dtype=np.float32) flat_img_np = np.empty(shape=(1,224,224)) for x in range(224): for y in range(224): flat_img_np[0][x][y] = (img_np[x][y][0]+img_np[x][y][1]+img_np[x][y][2])/765 sum = 0 for x in range(224): for y in range(224): sum+=flat_img_np[0][x][y] flat_img_np = flat_img_np*0.5/sum*224*224 outputpath = self.root+foldername+"/masks/" isfirst = True for objectpic in listdir(outputpath): obimg = cv2.imread(outputpath+objectpic) obimg = cv2.resize(obimg, (224, 224), interpolation=cv2.INTER_LINEAR) if isfirst: obimg_np = np.array(obimg, dtype=np.float32) isfirst = False else: obimg_np += np.array(obimg, dtype=np.float32) obimg_np/2 flat_obimg_np = np.empty(shape=(1,224,224)) for x in range(224): for y in range(224): if obimg_np[x][y][0]==255: flat_obimg_np[0][x][y]=1 else: flat_obimg_np[0][x][y]=0 return flat_img_np, flat_obimg_np
Train:
def train(model, dataloader, optimizer, loss_fn, epochs,filename):
model.cuda() model.train(True) # Set trainind mode = true f = open("/content/drive/My Drive/Nucleus dataset/limf"+filename+".txt", 'a') # for epoch in range(epochs): print('-' * 10) print('Epoch {}/{}'.format(epoch+1, epochs)) step = 0 eloss =0 eiou = 0 emae = 0 ef = 0 # for x, y in dataloader: x = x.float() x.requires_grad = True x = x.cuda() y = y.float() y = y.cuda() # step += 1 optimizer.zero_grad() # output = model(x) # output = output.cuda() loss = loss_fn(output, y) # iou = IOU(output, y) mae = MAE(output, y) fmeasure = Fmeasure(output, y) y = y.cpu().detach().numpy() output = output.cpu().detach().numpy() output = 1*(output[:,:,:,:]>0.5) ys = "" os = "" for n in range(224): for m in range(224): ys+=str(int(y[0][0][n][m])) os+=str(int(output[0][0][n][m])) print(ys+" "+os,file=f) ys = "" os = "" print("----------",file=f) eloss+=loss eiou+=iou emae+=mae ef+=fmeasure loss.backward() # optimizer.step() # print('Current step: {} Loss: {} IOU: {} MAE: {} F: {}'.format(step, loss, iou, mae, fmeasure),file=f) eloss/=step eiou/=step emae/=step ef/=step print('-----Epoch {} finish Loss: {} IOU: {} MAE: {} F: {}'.format(epoch, eloss, eiou, emae, ef),file=f) print('-----Epoch {} finish Loss: {} IOU: {} MAE: {} F: {}'.format(epoch, eloss, eiou, emae, ef)) f.close()
def IOU(prediction, groundtruth, bs=15):
prediction = prediction.cpu().detach().numpy() groundtruth = groundtruth.cpu().detach().numpy() prediction = 1*(prediction[:,:,:,:]>0.5) intersection = 0 union = 0 for l in range(bs): for n in range(224): for m in range(224): if prediction[l][0][n][m]==1 and groundtruth[l][0][n][m]==1: intersection+=1 if prediction[l][0][n][m]==1 or groundtruth[l][0][n][m]==1: union+=1 iou_score = intersection / union # 交集除以聯集 return iou_score
def MAE(prediction, groundtruth, bs=15):
prediction = prediction.cpu().detach().numpy() groundtruth = groundtruth.cpu().detach().numpy() prediction = 1*(prediction[:,:,:,:]>0.5) error = 0 for l in range(bs): for x in range(224): for y in range(224): if prediction[l][0][x][y]!=groundtruth[l][0][x][y]: error+=1 return error/224/224/bs
def Fmeasure(prediction, groundtruth, bs=15, b=1):
prediction = prediction.cpu().detach().numpy() groundtruth = groundtruth.cpu().detach().numpy() prediction = 1*(prediction[:,:,:,:]>0.5) TP = 0 FP = 0 FN = 0 for l in range(bs): for x in range(224): for y in range(224): if prediction[l][0][x][y]==1 and groundtruth[l][0][x][y]==1: TP+=1 if prediction[l][0][x][y]==1 and groundtruth[l][0][x][y]==0: FP+=1 if prediction[l][0][x][y]==0 and groundtruth[l][0][x][y]==1: FN+=1 if (TP+FP)==0: precision=0 else: precision = TP/(TP+FP) if (TP+FN)==0: recall=0 else: recall = TP/(TP+FN) if precision+recall==0: return 0 return ((1+b*b)*precision*recall)/(b*b*(precision+recall))
Model
import torch
import torch.nn as nn
import torch.nn.functional as F
from collections import OrderedDict
class SegNet(nn.Module):
def __init__(self,input_nbr,label_nbr): super(SegNet, self).__init__() batchNorm_momentum = 0.1 self.conv11 = nn.Conv2d(input_nbr, 64, kernel_size=3, padding=1) self.bn11 = nn.BatchNorm2d(64, momentum= batchNorm_momentum) self.conv12 = nn.Conv2d(64, 64, kernel_size=3, padding=1) self.bn12 = nn.BatchNorm2d(64, momentum= batchNorm_momentum) self.conv21 = nn.Conv2d(64, 128, kernel_size=3, padding=1) self.bn21 = nn.BatchNorm2d(128, momentum= batchNorm_momentum) self.conv22 = nn.Conv2d(128, 128, kernel_size=3, padding=1) self.bn22 = nn.BatchNorm2d(128, momentum= batchNorm_momentum) self.conv31 = nn.Conv2d(128, 256, kernel_size=3, padding=1) self.bn31 = nn.BatchNorm2d(256, momentum= batchNorm_momentum) self.conv32 = nn.Conv2d(256, 256, kernel_size=3, padding=1) self.bn32 = nn.BatchNorm2d(256, momentum= batchNorm_momentum) self.conv33 = nn.Conv2d(256, 256, kernel_size=3, padding=1) self.bn33 = nn.BatchNorm2d(256, momentum= batchNorm_momentum) self.conv41 = nn.Conv2d(256, 512, kernel_size=3, padding=1) self.bn41 = nn.BatchNorm2d(512, momentum= batchNorm_momentum) self.conv42 = nn.Conv2d(512, 512, kernel_size=3, padding=1) self.bn42 = nn.BatchNorm2d(512, momentum= batchNorm_momentum) self.conv43 = nn.Conv2d(512, 512, kernel_size=3, padding=1) self.bn43 = nn.BatchNorm2d(512, momentum= batchNorm_momentum) self.conv51 = nn.Conv2d(512, 512, kernel_size=3, padding=1) self.bn51 = nn.BatchNorm2d(512, momentum= batchNorm_momentum) self.conv52 = nn.Conv2d(512, 512, kernel_size=3, padding=1) self.bn52 = nn.BatchNorm2d(512, momentum= batchNorm_momentum) self.conv53 = nn.Conv2d(512, 512, kernel_size=3, padding=1) self.bn53 = nn.BatchNorm2d(512, momentum= batchNorm_momentum) self.conv53d = nn.Conv2d(512, 512, kernel_size=3, padding=1) self.bn53d = nn.BatchNorm2d(512, momentum= batchNorm_momentum) self.conv52d = nn.Conv2d(512, 512, kernel_size=3, padding=1) self.bn52d = nn.BatchNorm2d(512, momentum= batchNorm_momentum) self.conv51d = nn.Conv2d(512, 512, kernel_size=3, padding=1) self.bn51d = nn.BatchNorm2d(512, momentum= batchNorm_momentum) self.conv43d = nn.Conv2d(512, 512, kernel_size=3, padding=1) self.bn43d = nn.BatchNorm2d(512, momentum= batchNorm_momentum) self.conv42d = nn.Conv2d(512, 512, kernel_size=3, padding=1) self.bn42d = nn.BatchNorm2d(512, momentum= batchNorm_momentum) self.conv41d = nn.Conv2d(512, 256, kernel_size=3, padding=1) self.bn41d = nn.BatchNorm2d(256, momentum= batchNorm_momentum) self.conv33d = nn.Conv2d(256, 256, kernel_size=3, padding=1) self.bn33d = nn.BatchNorm2d(256, momentum= batchNorm_momentum) self.conv32d = nn.Conv2d(256, 256, kernel_size=3, padding=1) self.bn32d = nn.BatchNorm2d(256, momentum= batchNorm_momentum) self.conv31d = nn.Conv2d(256, 128, kernel_size=3, padding=1) self.bn31d = nn.BatchNorm2d(128, momentum= batchNorm_momentum) self.conv22d = nn.Conv2d(128, 128, kernel_size=3, padding=1) self.bn22d = nn.BatchNorm2d(128, momentum= batchNorm_momentum) self.conv21d = nn.Conv2d(128, 64, kernel_size=3, padding=1) self.bn21d = nn.BatchNorm2d(64, momentum= batchNorm_momentum) self.conv12d = nn.Conv2d(64, 64, kernel_size=3, padding=1) self.bn12d = nn.BatchNorm2d(64, momentum= batchNorm_momentum) self.conv11d = nn.Conv2d(64, label_nbr, kernel_size=3, padding=1) def forward(self, x): # Stage 1 x11 = F.relu(self.bn11(self.conv11(x))) x12 = F.relu(self.bn12(self.conv12(x11))) x1p, id1 = F.max_pool2d(x12,kernel_size=2, stride=2,return_indices=True) # Stage 2 x21 = F.relu(self.bn21(self.conv21(x1p))) x22 = F.relu(self.bn22(self.conv22(x21))) x2p, id2 = F.max_pool2d(x22,kernel_size=2, stride=2,return_indices=True) # Stage 3 x31 = F.relu(self.bn31(self.conv31(x2p))) x32 = F.relu(self.bn32(self.conv32(x31))) x33 = F.relu(self.bn33(self.conv33(x32))) x3p, id3 = F.max_pool2d(x33,kernel_size=2, stride=2,return_indices=True) # Stage 4 x41 = F.relu(self.bn41(self.conv41(x3p))) x42 = F.relu(self.bn42(self.conv42(x41))) x43 = F.relu(self.bn43(self.conv43(x42))) x4p, id4 = F.max_pool2d(x43,kernel_size=2, stride=2,return_indices=True) # Stage 5 x51 = F.relu(self.bn51(self.conv51(x4p))) x52 = F.relu(self.bn52(self.conv52(x51))) x53 = F.relu(self.bn53(self.conv53(x52))) x5p, id5 = F.max_pool2d(x53,kernel_size=2, stride=2,return_indices=True) # Stage 5d x5d = F.max_unpool2d(x5p, id5, kernel_size=2, stride=2) x53d = F.relu(self.bn53d(self.conv53d(x5d))) x52d = F.relu(self.bn52d(self.conv52d(x53d))) x51d = F.relu(self.bn51d(self.conv51d(x52d))) # Stage 4d x4d = F.max_unpool2d(x51d, id4, kernel_size=2, stride=2) x43d = F.relu(self.bn43d(self.conv43d(x4d))) x42d = F.relu(self.bn42d(self.conv42d(x43d))) x41d = F.relu(self.bn41d(self.conv41d(x42d))) # Stage 3d x3d = F.max_unpool2d(x41d, id3, kernel_size=2, stride=2) x33d = F.relu(self.bn33d(self.conv33d(x3d))) x32d = F.relu(self.bn32d(self.conv32d(x33d))) x31d = F.relu(self.bn31d(self.conv31d(x32d))) # Stage 2d x2d = F.max_unpool2d(x31d, id2, kernel_size=2, stride=2) x22d = F.relu(self.bn22d(self.conv22d(x2d))) x21d = F.relu(self.bn21d(self.conv21d(x22d))) # Stage 1d x1d = F.max_unpool2d(x21d, id1, kernel_size=2, stride=2) x12d = F.relu(self.bn12d(self.conv12d(x1d))) x11d = self.conv11d(x12d) return x11d def load_from_segnet(self, model_path): s_dict = self.state_dict()# create a copy of the state dict th = torch.load(model_path).state_dict() # load the weigths # for name in th: # s_dict[corresp_name[name]] = th[name] self.load_state_dict(th)
Main
if name == ‘main’:
#see whether gpu can be used #device = torch.device('cuda' if torch.cuda.is_available() else 'CPU') #print("device: "+str(device)) #load dataset train_dataset = NucleusDataset("train") bs=15 #batch size train_loader = DataLoader(train_dataset, batch_size=bs, num_workers=1, drop_last=True, shuffle=False) model = SegNet(1,1) train(model, train_loader, torch.optim.Adam(model.parameters(), lr=0.01), nn.BCELoss(), 10, "_bce_e10")
Based on the provided code snippet you are creating a single channel input image:
flat_img_np = np.empty(shape=(1,224,224))
and copy the data to it, while the model expects an input containing 3 channels.
You could thus either make sure the input images have 3 channels or you could change the in_channels
of the first conv layer to 1
.
can anyone please help me in increasing the hidden layers in this code. I am a novel with very low knowledge. I tried it my self but got this error " RuntimeError: Given groups=1, weight of size [8, 3, 3, 3], expected input[5, 8, 384, 384] to have 3 channels, but got 8 channels instead" . kindly
help me it would be a great favour pleaseee.
`import math
import torch
import torch.nn as nn
import torch.nn.functional as F
class ConvBlock(nn.Module):
def init(self, in_planes, out_planes, stride=1):
super(ConvBlock, self).init()
self.conv = nn.Conv2d(in_planes, out_planes, 3, stride=stride, padding=1, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
self.act = nn.PReLU(out_planes)
def forward(self, input):
output = self.act(self.bn(self.conv(input)))
return output
class DilatedParallelConvBlockD2(nn.Module):
def init(self, in_planes, out_planes):
super(DilatedParallelConvBlockD2, self).init()
self.conv0 = nn.Conv2d(in_planes, out_planes, 1, stride=1, padding=0, dilation=1, groups=1, bias=False)
self.conv1 = nn.Conv2d(out_planes, out_planes, 3, stride=1, padding=1, dilation=1, groups=out_planes, bias=False)
self.conv2 = nn.Conv2d(out_planes, out_planes, 3, stride=1, padding=2, dilation=2, groups=out_planes, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
def forward(self, input):
output = self.conv0(input)
d1 = self.conv1(output)
d2 = self.conv2(output)
output = d1 + d2
output = self.bn(output)
return output
class DilatedParallelConvBlock(nn.Module):
def init(self, in_planes, out_planes, stride=1):
super(DilatedParallelConvBlock, self).init()
assert out_planes % 4 == 0
inter_planes = out_planes // 4
self.conv1x1_down = nn.Conv2d(in_planes, inter_planes, 1, padding=0, groups=1, bias=False)
self.conv1 = nn.Conv2d(inter_planes, inter_planes, 3, stride=stride, padding=1, dilation=1, groups=inter_planes, bias=False)
self.conv2 = nn.Conv2d(inter_planes, inter_planes, 3, stride=stride, padding=2, dilation=2, groups=inter_planes, bias=False)
self.conv3 = nn.Conv2d(inter_planes, inter_planes, 3, stride=stride, padding=4, dilation=4, groups=inter_planes, bias=False)
self.conv4 = nn.Conv2d(inter_planes, inter_planes, 3, stride=stride, padding=8, dilation=8, groups=inter_planes, bias=False)
self.pool = nn.AvgPool2d(3, stride=stride, padding=1)
self.conv1x1_fuse = nn.Conv2d(out_planes, out_planes, 1, padding=0, groups=4, bias=False)
self.attention = nn.Conv2d(out_planes, 4, 1, padding=0, groups=4, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
self.act = nn.PReLU(out_planes)
def forward(self, input):
output = self.conv1x1_down(input)
d1 = self.conv1(output)
d2 = self.conv2(output)
d3 = self.conv3(output)
d4 = self.conv4(output)
p = self.pool(output)
d1 = d1 + p
d2 = d1 + d2
d3 = d2 + d3
d4 = d3 + d4
att = torch.sigmoid(self.attention(torch.cat([d1, d2, d3, d4], 1)))
d1 = d1 + d1 * att[:, 0].unsqueeze(1)
d2 = d2 + d2 * att[:, 1].unsqueeze(1)
d3 = d3 + d3 * att[:, 2].unsqueeze(1)
d4 = d4 + d4 * att[:, 3].unsqueeze(1)
output = self.conv1x1_fuse(torch.cat([d1, d2, d3, d4], 1))
output = self.act(self.bn(output))
return output
class DownsamplerBlock(nn.Module):
def init(self, in_planes, out_planes, stride=2):
super(DownsamplerBlock, self).init()
self.conv0 = nn.Conv2d(in_planes, out_planes, 1, stride=1, padding=0, groups=1, bias=False)
self.conv1 = nn.Conv2d(out_planes, out_planes, 5, stride=stride, padding=2, groups=out_planes, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
self.act = nn.PReLU(out_planes)
def forward(self, input):
output = self.conv1(self.conv0(input))
output = self.act(self.bn(output))
return output
def split(x):
c = int(x.size()[1])
c1 = round(c // 2)
x1 = x[:, :c1, :, :].contiguous()
x2 = x[:, c1:, :, :].contiguous()
return x1, x2
class MiniSeg(nn.Module):
def init(self, classes=2, P1=2, P2=3, P3=8, P4=6, aux=True):
super(MiniSeg, self).init()
self.D1 = int(P1/2)
self.D2 = int(P2/2)
self.D3 = int(P3/2)
self.D4 = int(P4/2)
self.aux = aux
self.long1 = DownsamplerBlock(3, 8, stride=2)
self.down1 = ConvBlock(3, 8, stride=2)
self.level1 = nn.ModuleList()
self.level1_long = nn.ModuleList()
for i in range(0, P1):
self.level1.append(ConvBlock(8, 8))
for i in range(0, self.D1):
self.level1_long.append(DownsamplerBlock(8, 8, stride=1))
self.cat1 = nn.Sequential(
nn.Conv2d(16, 16, 1, stride=1, padding=0, groups=1, bias=False),
nn.BatchNorm2d(16))
self.long2 = DownsamplerBlock(8, 24, stride=2)
self.down2 = DilatedParallelConvBlock(8, 24, stride=2)
self.level2 = nn.ModuleList()
self.level2_long = nn.ModuleList()
for i in range(0, P2):
self.level2.append(DilatedParallelConvBlock(24, 24))
for i in range(0, self.D2):
self.level2_long.append(DownsamplerBlock(24, 24, stride=1))
self.cat2 = nn.Sequential(
nn.Conv2d(48, 48, 1, stride=1, padding=0, groups=1, bias=False),
nn.BatchNorm2d(48))
self.long3 = DownsamplerBlock(24, 32, stride=2)
self.down3 = DilatedParallelConvBlock(24, 32, stride=2)
self.level3 = nn.ModuleList()
self.level3_long = nn.ModuleList()
for i in range(0, P3):
self.level3.append(DilatedParallelConvBlock(32, 32))
for i in range(0, self.D3):
self.level3_long.append(DownsamplerBlock(32, 32, stride=1))
self.cat3 = nn.Sequential(
nn.Conv2d(64, 64, 1, stride=1, padding=0, groups=1, bias=False),
nn.BatchNorm2d(64))
self.long4 = DownsamplerBlock(32, 64, stride=2)
self.down4 = DilatedParallelConvBlock(32, 64, stride=2)
self.level4 = nn.ModuleList()
self.level4_long = nn.ModuleList()
for i in range(0, P4):
self.level4.append(DilatedParallelConvBlock(64, 64))
for i in range(0, self.D4):
self.level4_long.append(DownsamplerBlock(64, 64, stride=1))
self.up4_conv4 = nn.Conv2d(64, 64, 1, stride=1, padding=0)
self.up4_bn4 = nn.BatchNorm2d(64)
self.up4_act = nn.PReLU(64)
self.up3_conv4 = DilatedParallelConvBlockD2(64, 32)
self.up3_conv3 = nn.Conv2d(32, 32, 1, stride=1, padding=0)
self.up3_bn3 = nn.BatchNorm2d(32)
self.up3_act = nn.PReLU(32)
self.up2_conv3 = DilatedParallelConvBlockD2(32, 24)
self.up2_conv2 = nn.Conv2d(24, 24, 1, stride=1, padding=0)
self.up2_bn2 = nn.BatchNorm2d(24)
self.up2_act = nn.PReLU(24)
self.up1_conv2 = DilatedParallelConvBlockD2(24, 8)
self.up1_conv1 = nn.Conv2d(8, 8, 1, stride=1, padding=0)
self.up1_bn1 = nn.BatchNorm2d(8)
self.up1_act = nn.PReLU(8)
if self.aux:
self.pred4 = nn.Sequential(nn.Dropout2d(0.01, False), nn.Conv2d(64, classes, 1, stride=1, padding=0))
self.pred3 = nn.Sequential(nn.Dropout2d(0.01, False), nn.Conv2d(32, classes, 1, stride=1, padding=0))
self.pred2 = nn.Sequential(nn.Dropout2d(0.01, False), nn.Conv2d(24, classes, 1, stride=1, padding=0))
self.pred1 = nn.Sequential(nn.Dropout2d(0.01, False), nn.Conv2d(8, classes, 1, stride=1, padding=0))
def forward(self, input):
long1 = self.long1(input)
output1 = self.down1(input)
output1_add = output1 + long1
for i, layer in enumerate(self.level1):
if i < self.D1:
output1 = layer(output1_add) + output1
long1 = self.level1_long[i](output1_add) + long1
output1_add = output1 + long1
else:
output1 = layer(output1_add) + output1
output1_add = output1 + long1
output1_cat = self.cat1(torch.cat([long1, output1], 1))
output1_l, output1_r = split(output1_cat)
long2 = self.long2(output1_l + long1)
output2 = self.down2(output1_r + output1)
output2_add = output2 + long2
for i, layer in enumerate(self.level2):
if i < self.D2:
output2 = layer(output2_add) + output2
long2 = self.level2_long[i](output2_add) + long2
output2_add = output2 + long2
else:
output2 = layer(output2_add) + output2
output2_add = output2 + long2
output2_cat = self.cat2(torch.cat([long2, output2], 1))
output2_l, output2_r = split(output2_cat)
long3 = self.long3(output2_l + long2)
output3 = self.down3(output2_r + output2)
output3_add = output3 + long3
for i, layer in enumerate(self.level3):
if i < self.D3:
output3 = layer(output3_add) + output3
long3 = self.level3_long[i](output3_add) + long3
output3_add = output3 + long3
else:
output3 = layer(output3_add) + output3
output3_add = output3 + long3
output3_cat = self.cat3(torch.cat([long3, output3], 1))
output3_l, output3_r = split(output3_cat)
long4 = self.long4(output3_l + long3)
output4 = self.down4(output3_r + output3)
output4_add = output4 + long4
for i, layer in enumerate(self.level4):
if i < self.D4:
output4 = layer(output4_add) + output4
long4 = self.level4_long[i](output4_add) + long4
output4_add = output4 + long4
else:
output4 = layer(output4_add) + output4
output4_add = output4 + long4
up4_conv4 = self.up4_bn4(self.up4_conv4(output4))
up4 = self.up4_act(up4_conv4)
up4 = F.interpolate(up4, output3.size()[2:], mode='bilinear', align_corners=False)
up3_conv4 = self.up3_conv4(up4)
up3_conv3 = self.up3_bn3(self.up3_conv3(output3))
up3 = self.up3_act(up3_conv4 + up3_conv3)
up3 = F.interpolate(up3, output2.size()[2:], mode='bilinear', align_corners=False)
up2_conv3 = self.up2_conv3(up3)
up2_conv2 = self.up2_bn2(self.up2_conv2(output2))
up2 = self.up2_act(up2_conv3 + up2_conv2)
up2 = F.interpolate(up2, output1.size()[2:], mode='bilinear', align_corners=False)
up1_conv2 = self.up1_conv2(up2)
up1_conv1 = self.up1_bn1(self.up1_conv1(output1))
up1 = self.up1_act(up1_conv2 + up1_conv1)
if self.aux:
pred4 = F.interpolate(self.pred4(up4), input.size()[2:], mode='bilinear', align_corners=False)
pred3 = F.interpolate(self.pred3(up3), input.size()[2:], mode='bilinear', align_corners=False)
pred2 = F.interpolate(self.pred2(up2), input.size()[2:], mode='bilinear', align_corners=False)
pred1 = F.interpolate(self.pred1(up1), input.size()[2:], mode='bilinear', align_corners=False)
if self.aux:
return (pred1, pred2, pred3, pred4, )
else:
return (pred1, )`
can you please help me with my code … I want to increase the layers but getting this error. don’t know how to do increase layers without getting error “RuntimeError: Given groups=1, weight of size [8, 3, 3, 3], expected input[5, 8, 384, 384] to have 3 channels, but got 8 channels instead”
Your posted code is not really readable at this moment so please format it by wrapping code snippets into three backticks ```.
The error points to a wrong number of input channels. The conv layer seems to expect an input containing 3 channels, while your input has 8 channels.
This is the working code sir. I want to increase it’s layer in order to improve accuracy. but when I do so by adding more layers into this code I got error . i have been trying this for many days, but all in vain. then I came across this post . would you please help me with this how can I add layers without getting error.
import torch
import torch.nn as nn
import torch.nn.functional as F
class ConvBlock(nn.Module):
def __init__(self, in_planes, out_planes, stride=1):
super(ConvBlock, self).__init__()
self.conv = nn.Conv2d(in_planes, out_planes, 3, stride=stride, padding=1, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
self.act = nn.PReLU(out_planes)
def forward(self, input):
output = self.act(self.bn(self.conv(input)))
return output
class DilatedParallelConvBlockD2(nn.Module):
def __init__(self, in_planes, out_planes):
super(DilatedParallelConvBlockD2, self).__init__()
self.conv0 = nn.Conv2d(in_planes, out_planes, 1, stride=1, padding=0, dilation=1, groups=1, bias=False)
self.conv1 = nn.Conv2d(out_planes, out_planes, 3, stride=1, padding=1, dilation=1, groups=out_planes, bias=False)
self.conv2 = nn.Conv2d(out_planes, out_planes, 3, stride=1, padding=2, dilation=2, groups=out_planes, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
def forward(self, input):
output = self.conv0(input)
d1 = self.conv1(output)
d2 = self.conv2(output)
output = d1 + d2
output = self.bn(output)
return output
class DilatedParallelConvBlock(nn.Module):
def __init__(self, in_planes, out_planes, stride=1):
super(DilatedParallelConvBlock, self).__init__()
assert out_planes % 4 == 0
inter_planes = out_planes // 4
self.conv1x1_down = nn.Conv2d(in_planes, inter_planes, 1, padding=0, groups=1, bias=False)
self.conv1 = nn.Conv2d(inter_planes, inter_planes, 3, stride=stride, padding=1, dilation=1, groups=inter_planes, bias=False)
self.conv2 = nn.Conv2d(inter_planes, inter_planes, 3, stride=stride, padding=2, dilation=2, groups=inter_planes, bias=False)
self.conv3 = nn.Conv2d(inter_planes, inter_planes, 3, stride=stride, padding=4, dilation=4, groups=inter_planes, bias=False)
self.conv4 = nn.Conv2d(inter_planes, inter_planes, 3, stride=stride, padding=8, dilation=8, groups=inter_planes, bias=False)
self.pool = nn.AvgPool2d(3, stride=stride, padding=1)
self.conv1x1_fuse = nn.Conv2d(out_planes, out_planes, 1, padding=0, groups=4, bias=False)
self.attention = nn.Conv2d(out_planes, 4, 1, padding=0, groups=4, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
self.act = nn.PReLU(out_planes)
def forward(self, input):
output = self.conv1x1_down(input)
d1 = self.conv1(output)
d2 = self.conv2(output)
d3 = self.conv3(output)
d4 = self.conv4(output)
p = self.pool(output)
d1 = d1 + p
d2 = d1 + d2
d3 = d2 + d3
d4 = d3 + d4
att = torch.sigmoid(self.attention(torch.cat([d1, d2, d3, d4], 1)))
d1 = d1 + d1 * att[:, 0].unsqueeze(1)
d2 = d2 + d2 * att[:, 1].unsqueeze(1)
d3 = d3 + d3 * att[:, 2].unsqueeze(1)
d4 = d4 + d4 * att[:, 3].unsqueeze(1)
output = self.conv1x1_fuse(torch.cat([d1, d2, d3, d4], 1))
output = self.act(self.bn(output))
return output
class DownsamplerBlock(nn.Module):
def __init__(self, in_planes, out_planes, stride=2):
super(DownsamplerBlock, self).__init__()
self.conv0 = nn.Conv2d(in_planes, out_planes, 1, stride=1, padding=0, groups=1, bias=False)
self.conv1 = nn.Conv2d(out_planes, out_planes, 5, stride=stride, padding=2, groups=out_planes, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
self.act = nn.PReLU(out_planes)
def forward(self, input):
output = self.conv1(self.conv0(input))
output = self.act(self.bn(output))
return output
def split(x):
c = int(x.size()[1])
c1 = round(c // 2)
x1 = x[:, :c1, :, :].contiguous()
x2 = x[:, c1:, :, :].contiguous()
return x1, x2
class MiniSeg(nn.Module):
def __init__(self, classes=2, P1=2, P2=3, P3=8, P4=6, aux=True):
super(MiniSeg, self).__init__()
self.D1 = int(P1/2)
self.D2 = int(P2/2)
self.D3 = int(P3/2)
self.D4 = int(P4/2)
self.aux = aux
self.long1 = DownsamplerBlock(3, 8, stride=2)
self.down1 = ConvBlock(3, 8, stride=2)
self.level1 = nn.ModuleList()
self.level1_long = nn.ModuleList()
for i in range(0, P1):
self.level1.append(ConvBlock(8, 8))
for i in range(0, self.D1):
self.level1_long.append(DownsamplerBlock(8, 8, stride=1))
self.cat1 = nn.Sequential(
nn.Conv2d(16, 16, 1, stride=1, padding=0, groups=1, bias=False),
nn.BatchNorm2d(16))
self.long2 = DownsamplerBlock(8, 24, stride=2)
self.down2 = DilatedParallelConvBlock(8, 24, stride=2)
self.level2 = nn.ModuleList()
self.level2_long = nn.ModuleList()
for i in range(0, P2):
self.level2.append(DilatedParallelConvBlock(24, 24))
for i in range(0, self.D2):
self.level2_long.append(DownsamplerBlock(24, 24, stride=1))
self.cat2 = nn.Sequential(
nn.Conv2d(48, 48, 1, stride=1, padding=0, groups=1, bias=False),
nn.BatchNorm2d(48))
self.long3 = DownsamplerBlock(24, 32, stride=2)
self.down3 = DilatedParallelConvBlock(24, 32, stride=2)
self.level3 = nn.ModuleList()
self.level3_long = nn.ModuleList()
for i in range(0, P3):
self.level3.append(DilatedParallelConvBlock(32, 32))
for i in range(0, self.D3):
self.level3_long.append(DownsamplerBlock(32, 32, stride=1))
self.cat3 = nn.Sequential(
nn.Conv2d(64, 64, 1, stride=1, padding=0, groups=1, bias=False),
nn.BatchNorm2d(64))
self.long4 = DownsamplerBlock(32, 64, stride=2)
self.down4 = DilatedParallelConvBlock(32, 64, stride=2)
self.level4 = nn.ModuleList()
self.level4_long = nn.ModuleList()
for i in range(0, P4):
self.level4.append(DilatedParallelConvBlock(64, 64))
for i in range(0, self.D4):
self.level4_long.append(DownsamplerBlock(64, 64, stride=1))
self.up4_conv4 = nn.Conv2d(64, 64, 1, stride=1, padding=0)
self.up4_bn4 = nn.BatchNorm2d(64)
self.up4_act = nn.PReLU(64)
self.up3_conv4 = DilatedParallelConvBlockD2(64, 32)
self.up3_conv3 = nn.Conv2d(32, 32, 1, stride=1, padding=0)
self.up3_bn3 = nn.BatchNorm2d(32)
self.up3_act = nn.PReLU(32)
self.up2_conv3 = DilatedParallelConvBlockD2(32, 24)
self.up2_conv2 = nn.Conv2d(24, 24, 1, stride=1, padding=0)
self.up2_bn2 = nn.BatchNorm2d(24)
self.up2_act = nn.PReLU(24)
self.up1_conv2 = DilatedParallelConvBlockD2(24, 8)
self.up1_conv1 = nn.Conv2d(8, 8, 1, stride=1, padding=0)
self.up1_bn1 = nn.BatchNorm2d(8)
self.up1_act = nn.PReLU(8)
if self.aux:
self.pred4 = nn.Sequential(nn.Dropout2d(0.01, False), nn.Conv2d(64, classes, 1, stride=1, padding=0))
self.pred3 = nn.Sequential(nn.Dropout2d(0.01, False), nn.Conv2d(32, classes, 1, stride=1, padding=0))
self.pred2 = nn.Sequential(nn.Dropout2d(0.01, False), nn.Conv2d(24, classes, 1, stride=1, padding=0))
self.pred1 = nn.Sequential(nn.Dropout2d(0.01, False), nn.Conv2d(8, classes, 1, stride=1, padding=0))
def forward(self, input):
long1 = self.long1(input)
output1 = self.down1(input)
output1_add = output1 + long1
for i, layer in enumerate(self.level1):
if i < self.D1:
output1 = layer(output1_add) + output1
long1 = self.level1_long[i](output1_add) + long1
output1_add = output1 + long1
else:
output1 = layer(output1_add) + output1
output1_add = output1 + long1
output1_cat = self.cat1(torch.cat([long1, output1], 1))
output1_l, output1_r = split(output1_cat)
long2 = self.long2(output1_l + long1)
output2 = self.down2(output1_r + output1)
output2_add = output2 + long2
for i, layer in enumerate(self.level2):
if i < self.D2:
output2 = layer(output2_add) + output2
long2 = self.level2_long[i](output2_add) + long2
output2_add = output2 + long2
else:
output2 = layer(output2_add) + output2
output2_add = output2 + long2
output2_cat = self.cat2(torch.cat([long2, output2], 1))
output2_l, output2_r = split(output2_cat)
long3 = self.long3(output2_l + long2)
output3 = self.down3(output2_r + output2)
output3_add = output3 + long3
for i, layer in enumerate(self.level3):
if i < self.D3:
output3 = layer(output3_add) + output3
long3 = self.level3_long[i](output3_add) + long3
output3_add = output3 + long3
else:
output3 = layer(output3_add) + output3
output3_add = output3 + long3
output3_cat = self.cat3(torch.cat([long3, output3], 1))
output3_l, output3_r = split(output3_cat)
long4 = self.long4(output3_l + long3)
output4 = self.down4(output3_r + output3)
output4_add = output4 + long4
for i, layer in enumerate(self.level4):
if i < self.D4:
output4 = layer(output4_add) + output4
long4 = self.level4_long[i](output4_add) + long4
output4_add = output4 + long4
else:
output4 = layer(output4_add) + output4
output4_add = output4 + long4
up4_conv4 = self.up4_bn4(self.up4_conv4(output4))
up4 = self.up4_act(up4_conv4)
up4 = F.interpolate(up4, output3.size()[2:], mode='bilinear', align_corners=False)
up3_conv4 = self.up3_conv4(up4)
up3_conv3 = self.up3_bn3(self.up3_conv3(output3))
up3 = self.up3_act(up3_conv4 + up3_conv3)
up3 = F.interpolate(up3, output2.size()[2:], mode='bilinear', align_corners=False)
up2_conv3 = self.up2_conv3(up3)
up2_conv2 = self.up2_bn2(self.up2_conv2(output2))
up2 = self.up2_act(up2_conv3 + up2_conv2)
up2 = F.interpolate(up2, output1.size()[2:], mode='bilinear', align_corners=False)
up1_conv2 = self.up1_conv2(up2)
up1_conv1 = self.up1_bn1(self.up1_conv1(output1))
up1 = self.up1_act(up1_conv2 + up1_conv1)
if self.aux:
pred4 = F.interpolate(self.pred4(up4), input.size()[2:], mode='bilinear', align_corners=False)
pred3 = F.interpolate(self.pred3(up3), input.size()[2:], mode='bilinear', align_corners=False)
pred2 = F.interpolate(self.pred2(up2), input.size()[2:], mode='bilinear', align_corners=False)
pred1 = F.interpolate(self.pred1(up1), input.size()[2:], mode='bilinear', align_corners=False)
if self.aux:
return (pred1, pred2, pred3, pred4, )
else:
return (pred1, )
Thanks for the code. It’s working fine for me using:
model = MiniSeg()
x = torch.randn(1, 3, 224, 224)
out = model(x)
for o in out:
print(o.shape)
> torch.Size([1, 2, 224, 224])
torch.Size([1, 2, 224, 224])
torch.Size([1, 2, 224, 224])
torch.Size([1, 2, 224, 224])
and I don’t get the previously mentioned error.
yes it is working properly… my problem is that when I increase the layers in the code I got the above error. I am doing it wrong and couldn’t be able to find any help. would you please help me in increasing the layers ? how to alter the above code for adding more layers. I’ll be very very grateful if you help me.
Sure, I could take a look at your implementation and see where you are stuck.
Generally, you would be able to add layers to each module as long as their setup fits the overall workflow (i.e. in particular the input and output shapes of the new layers should match the model architecture). So feel free to post a code snippet containing the newly added layers, which create the issues.
Continuing the discussion from RuntimeError: Given groups=1, weight[64, 3, 3, 3], so expected input[16, 64, 256, 256] to have 3 channels, but got 64 channels instead:
I only increased this layer "the layer which have +++++++ Infront of it "
my accuracy is 90 I want to increase it to 97 … i googled it and read that if we increase the layers , the model accuracy will increase as a result …but when I tried to add layer only one at first, I got error.
def __init__(self, in_planes, out_planes, stride=1):
super(ConvBlock, self).__init__()
self.conv1 = nn.Conv2d(in_planes, out_planes, 3, stride=stride, padding=1, bias=False) ++++++++++++
self.conv2 = nn.Conv2d(in_planes, out_planes, 3, stride=stride, padding=1, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
self.act = nn.PReLU(out_planes)
def forward(self, input):
output = self.conv1(input)
output = self.conv2(output)
output = self.act(self.bn(output))
return output
See this answer in the current thread.
Why am I getting this error?
Given groups=1, weight of size [16, 1, 3, 3], expected input[64, 3, 32, 32] to have 1 channels, but got 3 channels instead.
Code:
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.init_size = opt.img_size // 4
self.l1 = nn.Sequential(nn.Linear(opt.latent_dim, 128 * self.init_size ** 2))
self.conv_blocks = nn.Sequential(
nn.BatchNorm2d(128),
nn.Upsample(scale_factor=2),
nn.Conv2d(128, 128, 3, stride=1, padding=1),
nn.BatchNorm2d(128, 0.8),
nn.LeakyReLU(0.2, inplace=True),
nn.Upsample(scale_factor=2),
nn.Conv2d(128, 64, 3, stride=1, padding=1),
nn.BatchNorm2d(64, 0.8),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64, opt.channels, 3, stride=1, padding=1),
nn.Tanh(),
)
def forward(self, z):
out = self.l1(z)
out = out.view(out.shape[0], 128, self.init_size, self.init_size)
img = self.conv_blocks(out)
return img
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
def discriminator_block(in_filters, out_filters, bn=True):
block = [nn.Conv2d(in_filters, out_filters, 3, 2, 1), nn.LeakyReLU(0.2, inplace=True), nn.Dropout2d(0.25)]
if bn:
block.append(nn.BatchNorm2d(out_filters, 0.8))
return block
self.model = nn.Sequential(
*discriminator_block(opt.channels, 16, bn=False),
*discriminator_block(16, 32),
*discriminator_block(32, 64),
*discriminator_block(64, 128),
)
# The height and width of downsampled image
ds_size = opt.img_size // 2 ** 4
print(ds_size)
self.adv_layer = nn.Sequential(nn.Linear(128 * ds_size ** 2, 1), nn.Sigmoid())
def forward(self, img):
out = self.model(img)
out = out.view(out.shape[0], -1)
validity = self.adv_layer(out)
#print(validity)
return validity
for epoch in range(opt.n_epochs):
for i, (imgs, _) in enumerate(dataloader):
# Adversarial ground truths
valid = Variable(Tensor(imgs.shape[0], 1).fill_(1.0), requires_grad=False)
fake = Variable(Tensor(imgs.shape[0], 1).fill_(0.0), requires_grad=False)
# Configure input
real_imgs = Variable(imgs.type(Tensor))
# -----------------
# Train Generator
# -----------------
optimizer_G.zero_grad()
# Sample noise as generator input
z = Variable(Tensor(np.random.normal(0, 1, (imgs.shape[0], opt.latent_dim))))
# Generate a batch of images
gen_imgs = generator(z)
# Loss measures generator's ability to fool the discriminator
g_loss = adversarial_loss(discriminator(gen_imgs), valid)
g_loss.backward()
optimizer_G.step()
# ---------------------
# Train Discriminator
# ---------------------
optimizer_D.zero_grad()
# Measure discriminator's ability to classify real from generated samples
real_loss = adversarial_loss(discriminator(real_imgs), valid)
fake_loss = adversarial_loss(discriminator(gen_imgs.detach()), fake)
d_loss = (real_loss + fake_loss) / 2
d_loss.backward()
optimizer_D.step()
print(
"[Epoch %d/%d] [Batch %d/%d] [D loss: %f] [G loss: %f]"
% (epoch, opt.n_epochs, i, len(dataloader), d_loss.item(), g_loss.item())
)
batches_done = epoch * len(dataloader) + i
if batches_done % opt.sample_interval == 0:
save_image(gen_imgs.data[:25], "images/%d.png" % batches_done, nrow=5, normalize=True)
A conv layer is created with in_channels=1
while its input is using 3 channels.
I can’t directly see in your code where this conv layer is defined, so you could print the model and check where this error might occur and either fix the conv layer setup or the input shape.