Can anyone help me with this error?
I want to see the summary of my deeplearning architecture features which is SegNet that using PyTorch. I try the code that've been made by trypag from github (https://github.com/trypag/pytorch-unet-segnet). I use summary() from torchinfo made by TylerYep (https://github.com/TylerYep/torchinfo). It was fine with other architecture like vgg16, resnet50, even with the samples code in the repository. But when I try to use it with codes from trypag I get:
RuntimeError: Failed to run torchinfo. See above stack traces for more details. Executed layers up to:
[Encoder: 2-1, Sequential: 3-1, Conv2d: 4-1, BatchNorm2d: 4-2, ReLU: 4-3, Conv2d: 4-4, BatchNorm2d: 4-5,
ReLU: 4-6, Encoder: 2-2, Sequential: 3-2, Conv2d: 4-7, BatchNorm2d: 4-8, ReLU: 4-9, Conv2d: 4-10, BatchNorm2d: 4-11,
ReLU: 4-12, Encoder: 2-3, Sequential: 3-3, Conv2d: 4-13, BatchNorm2d: 4-14, ReLU: 4-15, Conv2d: 4-16,
BatchNorm2d: 4-17, ReLU: 4-18, Dropout: 4-19, Encoder: 2-4, Sequential: 3-4, Conv2d: 4-20, BatchNorm2d: 4-21,
ReLU: 4-22, Conv2d: 4-23, BatchNorm2d: 4-24, ReLU: 4-25,` Dropout: 4-26, Encoder: 2-5, Sequential: 3-5, Conv2d: 4-27,
BatchNorm2d: 4-28, ReLU: 4-29, Conv2d: 4-30, BatchNorm2d: 4-31, ReLU: 4-32, Dropout: 4-33, Decoder: 2-6,
Sequential: 3-6, Conv2d: 4-34, BatchNorm2d: 4-35, ReLU: 4-36, Conv2d: 4-37, BatchNorm2d: 4-38, ReLU: 4-39,
Dropout: 4-40, Decoder: 2-7, Sequential: 3-7, Conv2d: 4-41, BatchNorm2d: 4-42, ReLU: 4-43, Conv2d: 4-44,
BatchNorm2d: 4-45, ReLU: 4-46, Dropout: 4-47, Decoder: 2-8, Sequential: 3-8, Conv2d: 4-48, BatchNorm2d: 4-49,
ReLU: 4-50, Conv2d: 4-51, BatchNorm2d: 4-52, ReLU: 4-53, Dropout: 4-54, Decoder: 2-9, Sequential: 3-9, Conv2d: 4-55,
BatchNorm2d: 4-56, ReLU: 4-57, Conv2d: 4-58, BatchNorm2d: 4-59, ReLU: 4-60, Conv2d: 4-61]
This is the code that I use:
from torch import nn
import torch.nn.functional as F
import torch
class SegNet(nn.Module):
def __init__(self, num_classes, in_features = 1, drop_rate = 0.5,
filter_config=(64, 128, 256, 512, 512)):
super(SegNet, self).__init__()
self.encoders = nn.ModuleList()
self.decoders = nn.ModuleList()
encoder_n_layers = (2, 2, 3, 3, 3)
encoder_filter_config = (in_features,) + filter_config
decoder_n_layers = (3, 3, 3, 2, 1)
decoder_filter_config = filter_config[::-1] + (filter_config[0],)
for i in range(0, 5):
self.encoders.append(Encoder(encoder_filter_config[i],
encoder_filter_config[i + 1],
encoder_n_layers[i], drop_rate))
self.decoders.append(Decoder(decoder_filter_config[i],
decoder_filter_config[i + 1],
decoder_n_layers[i], drop_rate))
self.classifier = nn.Conv2d(filter_config[0], num_classes, kernel_size = 3,
stride = 1, padding = 1)
def forward(self, x):
indices = []
unpool_sizes = []
feat = x
for i in range(0, 5):
(feat, ind), size = self.encoders[i](feat)
indices.append(ind)
unpool_sizes.append(size)
for i in range(0, 5):
feat = self.decoders[i](feat, indices[4 - i], unpool_sizes[4 - i])
return self.classifier(feat)
class Encoder(nn.Module):
"""
A helper Module that performs ConvoBlock (Convolutional, BN, Activation).
Maxpooling follows each ConvoBlock.
"""
def __init__(self,
in_features: int,
out_features: int,
n_blocks: int = 2,
drop_rate = 0.5):
super(Encoder, self).__init__()
layers = [nn.Conv2d(in_features, out_features, kernel_size = 3,
stride = 1, padding = 1, bias = True),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace = True)]
if n_blocks > 1:
layers += [nn.Conv2d(out_features, out_features, kernel_size = 3,
stride = 1, padding = 1, bias = True),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace = True)]
if n_blocks == 3:
layers += [nn.Dropout(drop_rate)]
self.features = nn.Sequential(*layers)
def forward(self, x):
output = self.features(x)
return F.max_pool2d(output, kernel_size = 2, stride = 2, return_indices =
True), output.size()
class Decoder(nn.Module):
def __init__(self,
in_features: int,
out_features: int,
n_blocks: int = 2,
drop_rate = 0.5):
super(Decoder, self).__init__()
layers = [nn.Conv2d(in_features, out_features, kernel_size = 3,
stride = 1, padding = 1, bias = True),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace = True)]
if n_blocks > 1:
layers += [nn.Conv2d(out_features, out_features, kernel_size = 3,
stride = 1, padding = 1, bias = True),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace = True)]
if n_blocks == 3:
layers += [nn.Dropout(drop_rate)]
self.features = nn.Sequential(*layers)
def forward(self, x, indices, size):
unpooled = F.max_unpool2d(x, indices = indices, kernel_size = 2, stride = 2,
padding = 0, output_size = size)
return self.features(unpooled)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = SegNet(num_classes = 2, in_features = 3).to(device)
x = torch.randn(size = (1, 3, 360, 480),
dtype=torch.float32).to(device)
with torch.no_grad():
out = model(x)
from torchinfo import summary
summary = summary(model, (32, 3, 360, 480))
This part of code is only for testing to display the summary:
model = SegNet(num_classes = 2, in_features = 3).to(device)
x = torch.randn(size = (1, 3, 360, 480),
dtype=torch.float32).to(device)
with torch.no_grad():
out = model(x)
from torchinfo import summary
summary = summary(model, (32, 3, 360, 480))
Should I trace the error by looking the code behind torchinfo summary() then revised my code? Or any other advice for me?
Any advice or solution will help me alot. Thank You !