I am getting error: “RuntimeError: Failed to run torchinfo. See above stack traces for…
- User
I am getting error: “RuntimeError: Failed to run torchinfo. See above stack traces for more details. Executed layers up to: [Conv2d: 1, ReLU: 1, MaxPool2d: 1, Conv2d: 1, ReLU: 1, Conv2d: 1, ReLU: 1, Conv2d: 1, ReLU: 1, Conv2d: 1, ReLU: 1, Conv2d: 1, ReLU: 1, Flatten: 1]” when running: “summary(model, input_size=(32, 3, 128, 128), device=device)” Here is my model: “model = nn.Sequential( nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(32, 64, 3, 1, 1), nn.ReLU(), nn.Conv2d(64, 128, 3, 1, 1), nn.ReLU(), nn.Conv2d(128, 256, 3, 1, 1), nn.ReLU(), nn.Conv2d(256, 512, 3, 1, 1), nn.ReLU(), nn.Conv2d(512, 1024, 3, 1, 1), nn.ReLU(), nn.Flatten(), nn.Linear(1024, 8) )”
- Assistant
It seems like there's an issue with the dimensions when you are passing the input through the linear layer of your model. First, let's verify whether the fully connected layer has an appropriate input size. We can do this by printing the size of the output after the convolution and pooling layers:
import torch import torch.nn as nn class Model(nn.Module): def __init__(self): super(Model, self).__init__() self.conv_layers = nn.Sequential( nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(32, 64, 3, 1, 1), nn.ReLU(), nn.Conv2d(64, 128, 3, 1, 1), nn.ReLU(), nn.Conv2d(128, 256, 3, 1, 1), nn.ReLU(), nn.Conv2d(256, 512, 3, 1, 1), nn.ReLU(), nn.Conv2d(512, 1024, 3, 1, 1), nn.ReLU(), ) self.fc_layers = nn.Sequential( nn.Flatten(), nn.Linear(1024, 8) ) def forward(self, x): x = self.conv_layers(x) print("Output size after conv layers:", x.size()) x = self.fc_layers(x) return x device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = Model().to(device) x = torch.randn(32, 3, 128, 128).to(device) output = model(x) - User
I got error: "3\result\code\kayla_tools.py:254, in Model.forward(self, x) 253 def forward(self, x): --> 254 x = self.conv_layers(x) 255 print("Output size after conv layers:", x.size()) 256 x = self.fc_layers(x)
File c:\Programs\mambaforge\envs\416a3\lib\site-packages\torch\nn\modules\module.py:1614, in Module.getattr(self, name) ... 1613 return modules[name] -> 1614 raise AttributeError("'{}' object has no attribute '{}'".format( 1615 type(self).name, name))
AttributeError: 'Model' object has no attribute 'conv_layers'"
- Assistant
I apologize for the confusion. It seems like the error is caused by the way the model is defined. Let's try a different approach to define the model using the
__init__andforwardmethods:import torch import torch.nn as nn class Model(nn.Module): def __init__(self): super(Model, self).__init__() self.conv_layers = nn.Sequential( nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(32, 64, 3, 1, 1), nn.ReLU(), nn.Conv2d(64, 128, 3, 1, 1), nn.ReLU(), nn.Conv2d(128, 256, 3, 1, 1), nn.ReLU(), nn.Conv2d(256, 512, 3, 1, 1), nn.ReLU(), nn.Conv2d(512, 1024, 3, 1, 1), nn.ReLU(), ) self.fc_layers = nn.Sequential( nn.Flatten(), nn.Linear(4 * 4 * 1024, 8) ) def forward(self, x): x = self.conv_layers(x) print("Output size after conv layers:", x.size()) x = self.fc_layers(x) return x device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = Model().to(device) x = torch.randn(32, 3, 128, 128).to(device) output = model(x) - User
I got Output size after conv layers: torch.Size([32, 1024, 64, 64]) and solved my issue, what should be the last model: "class Model(nn.Module): def init(self): super(Model, self).init() self.conv_layers = nn.Sequential( nn.Conv2d( in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1 ), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(32, 64, 3, 1, 1), nn.ReLU(), nn.Conv2d(64, 128, 3, 1, 1), nn.ReLU(), nn.Conv2d(128, 256, 3, 1, 1), nn.ReLU(), nn.Conv2d(256, 512, 3, 1, 1), nn.ReLU(), nn.Conv2d(512, 1024, 3, 1, 1), nn.ReLU(), ) self.fc_layers = nn.Sequential(nn.Flatten(), nn.Linear(1024 * 64 * 64, 8))
def forward(self, x): x = self.conv_layers(x) x = self.fc_layers(x) return x" - Assistant
Yes, based on the output size you got, you should update the Linear layer input size to 1024 * 64 * 64. Here's the final model definition:
class Model(nn.Module): def __init__(self): super(Model, self).__init__() self.conv_layers = nn.Sequential( nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(32, 64, 3, 1, 1), nn.ReLU(), nn.Conv2d(64, 128, 3, 1, 1), nn.ReLU(), nn.Conv2d(128, 256, 3, 1, 1), nn.ReLU(), nn.Conv2d(256, 512, 3, 1, 1), nn.ReLU(), nn.Conv2d(512, 1024, 3, 1, 1), nn.ReLU(), ) self.fc_layers = nn.Sequential(nn.Flatten(), nn.Linear(1024 * 64 * 64, 8)) def forward(self, x): x = self.conv_layers(x) x = self.fc_layers(x) return xNow you can use this model for your tasks. Keep in mind that the model has a large number of parameters due to the large input size of the Linear layer, which may require more memory and longer training times. If you face any issues related to memory or training time, consider adding more pooling layers or using a different architecture to reduce the size of the feature maps before the fully connected layer.