I am trying to implement alexnet from scratch in fastai on imagewoof dataset
but when I fit the model I get this flatten error, I have seen some stackoverflow but I don’t how to implement that in fastai
My code for the model is
class AlexNet(Module):
def __init__(self):
self.features = nn.Sequential(
nn.Conv2d(in_channels=3,out_channels=64,kernel_size=11,stride=4,padding=2),
nn.ReLU(),
nn.LocalResponseNorm(size=5,alpha=0.0001,beta=0.75,k=2),
nn.MaxPool2d(kernel_size=3,stride=2),
nn.Conv2d(in_channels=64,out_channels=192,kernel_size=5,padding=2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=3,stride=2),
nn.Conv2d(in_channels=192,out_channels=384,kernel_size=3,padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=384,out_channels=256,kernel_size=3,padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=256,out_channels=256,kernel_size=3,padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=3,stride=2)
)
self.avgpool = nn.AdaptiveAvgPool2d((6,6))
self.classsifer = nn.Sequential(
nn.Dropout(p=0.5,inplace=True),
nn.Linear((256*6*6),4096),
nn.ReLU(),
nn.Dropout(p=0.5,inplace=True),
nn.Linear(4096,4096),
nn.ReLU(),
nn.Linear(4096,10) #10 out features since 10 output classes here
)
def forward(self,x):
x = self.features(x),
x = self.avgpool(x),
x = torch.flatten(x,1),
x = self.classifier(x)
return x
model = AlexNet()
and the code for loading the dataset into datablocks is
dblock = DataBlock(blocks=(ImageBlock,CategoryBlock),
get_items=get_image_files,
get_y = label_func,
item_tfms=[ToTensor(),Resize(227)],
splitter = GrandparentSplitter(),
batch_tfms = [FlipItem(), RandomResizedCrop(128, min_scale=0.35),
IntToFloatTensor(), Normalize.from_stats(*imagenet_stats)])
The error stack
|epoch|train_loss|valid_loss|accuracy|time|
| --- | --- | --- | --- | --- |
|0|0.000000|00:01|
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
[<ipython-input-15-835ae40d7789>](https://localhost:8080/#) in <module>() 1 #original learning rate and optimizer was not used here, only the architecture was reused 2 learn = Learner(dls,model,loss_func=F.cross_entropy,metrics=accuracy) ----> 3 learn.fit_one_cycle(5)
15 frames
[<ipython-input-14-9bbb9730ce57>](https://localhost:8080/#) in forward(self, x) 31 x = self.features(x), 32 #x = self.avgpool(x), ---> 33 x = torch.flatten(x,1), 34 x = self.classifier(x) 35 return x
TypeError: flatten(): argument 'input' (position 1) must be Tensor, not tuple