‘DataParallel’ object has no attribute ‘conv1’

I am trying to visualize cnn network features map for conv1 layer based on the code and architecture below. It’s working properly without DataParallel, but when I am activating model = nn.DataParallel(model) it raised with error: ‘DataParallel’ object has no attribute ‘conv1’. Any suggestion appreciated.

class Model(nn.Module):
    def __init__(self, kernel, num_filters, res = ResidualBlock):
        super(Model, self).__init__()
        
        self.conv0 = nn.Sequential(
            nn.Conv2d(4, num_filters, kernel_size = kernel*3, 
                       padding = 4),
            nn.BatchNorm2d(num_filters),
            nn.ReLU(inplace=True))
        
        self.conv1 = nn.Sequential(
            nn.Conv2d(num_filters, num_filters*2, kernel_size = kernel, 
                      stride=2, padding = 1),
            nn.BatchNorm2d(num_filters*2),
            nn.ReLU(inplace=True))
        
        self.conv2 = nn.Sequential(
            nn.Conv2d(num_filters*2, num_filters*4, kernel_size = kernel, stride=2, padding = 1),
            nn.BatchNorm2d(num_filters*4),
            nn.ReLU(inplace=True))
               
        self.tsconv0 = nn.Sequential(
            nn.ConvTranspose2d(num_filters*4, num_filters*2, kernel_size = kernel, padding = 1),
            nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True),
            nn.ReLU(inplace=True),
            nn.BatchNorm2d(num_filters*2))

        self.tsconv1 = nn.Sequential(
            nn.ConvTranspose2d(num_filters*2, num_filters, kernel_size = kernel, padding = 1),
            nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True),
            nn.ReLU(inplace=True),
            nn.BatchNorm2d(num_filters))
        
        self.tsconv2 = nn.Sequential(
            nn.Conv2d(num_filters, 1, kernel_size = kernel*3, padding = 4, bias=False),
            nn.ReLU(inplace=True))

model = Model(kernel, num_filters)
model = nn.DataParallel(model)

The code for feature map visualization:

def get_activation(name):
    def hook(model, x_train_batch, y_train_pred):
        activation[name] = y_train_pred.detach()
    return hook

model.conv3.register_forward_hook(get_activation('conv3'))
x_train_batch[0,0,:,:]
y_train_pred = model(x_train_batch)

act = activation['conv3'].squeeze()
act1 = act.cpu().detach().numpy()
act=act[0,:,:,:] 
    
fig, axarr = plt.subplots(6,16) 
k = 0
for idx in range(act.size(0)//16):
    for idy in range(act.size(0)//6):
        axarr[idx, idy].imshow(act[k])
        k += 1

1 answer

  • answered 2020-10-29 17:27 Rika

    When you use DataParallel, add an extra module there. instead of doing model.conv3. simply write model.module.conv3.