#adding the extra layers
class added_layers(nn.Module):
def __init__(self):
super(added_layers,self).__init__()
self.add = nn.Sequential(
nn.AdaptiveMaxPool2d((1, 1)),
nn.Linear(2048,256),nn.ReLU(),
nn.Linear(256,3),nn.Softmax(dim=1)
)
def forward(self,x):
add = self.add(x)
return add
added_layer = added_layers().to('cuda')
model = nn.Sequential(model,added_layer)
model.to('cuda')
and
#removing the fully connected layer
class nochange(nn.Module):
def __init__(self):
super(nochange, self).__init__()
def forward(self, x):
return x
model.fc = nochange()
#freeze the trained layers
for parameters in model.parameters():
parameters.requires_grad = False
these are my code for combining pretrained model exception with additional layers. I am getting the following error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-13-3b76556c6596> in <module>
1 #model training for num_ epoch
----> 2 training_loss,val_loss = training(model,optimizer,train_loader,val_loader,test_loader,num_epoch,device = 'cuda',save_dir = '/content/drive/MyDrive/Applied_Artificial_intelligence/saved_model')
3 plt.plot(training_loss)
12 frames
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/utils.py in _list_with_default(out_size, defaults)
34 return out_size
35 if len(defaults) <= len(out_size):
---> 36 raise ValueError(
37 "Input dimension should be at least {}".format(len(out_size) + 1)
38 )
ValueError: Input dimension should be at least 3
what would be the issue here,
(conv2): DwsConvBlock(
(activ): ReLU()
(conv): DwsConv(
(dw_conv): Conv2d(1536, 1536, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=1536, bias=False)
(pw_conv): Conv2d(1536, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
)
(bn): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(activ): ReLU(inplace=True)
(pool): AvgPool2d(kernel_size=10, stride=1, padding=0)
)
)
(output): Linear(in_features=2048, out_features=1000, bias=True)
(fc): nochange()
)
(1): added_layers(
(add): Sequential(
(0): AdaptiveMaxPool2d(output_size=(1, 1))
(1): Linear(in_features=2048, out_features=256, bias=True)
(2): ReLU()
(3): Linear(in_features=256, out_features=3, bias=True)
(4): Softmax(dim=1)
)
)
)
my last few layers are looking like this