my issue:
1- view not has an attribute of tuple
2- When solving the above issue, i got another error of
RuntimeError: size mismatch, m1: [8 x 3], m2: [3000 x 8]
class MyEnsemble(nn.Module):
def init(self, model_1, model_2, model_3 , nb_classes=8):
super(MyEnsemble, self).init()
self.model_1 = model_1
self.model_2 = model_2
self.model_3 = model_3
# Remove last linear layer
self.model_1.classifier = nn.Identity()
self.model_2.classifier = nn.Identity()
self.model_3.classifier = nn.Identity()
self.classifier = nn.Linear(1000+1000+1000, 8)
def forward(self, x):
x1 = self.model_1(x.clone()) # clone to make sure x is not changed by inplace
x1 = [[item] for t in x1 for item in t]
x1= torch.tensor(x1)
x1 = x1.view(x1.size(0), -1)
x2 = self.model_2(x)
x2 = [[item] for t in x2 for item in t]
x2= torch.tensor(x2)
x2 = x2.view(x2.size(0), -1)
x3 = self.model_3(x)
x3 = [[item] for t in x3 for item in t]
x3= torch.tensor(x3)
x3 = x3.view(x3.size(0), -1)
x = torch.cat((x1, x2,x3), dim=1)
print(x.shape)
x=F.relu(x)
print(x.shape)
x = self.classifier(x)
return x
That’s Your code after modifying it.
3- Here is myensemble model summary
[Classifier(
(backbone): VGG(
(features): Sequential(
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace)
(3): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(4): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(5): ReLU(inplace)
(6): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(7): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(8): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(9): ReLU(inplace)
(10): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(11): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(12): ReLU(inplace)
(13): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(14): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(15): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(16): ReLU(inplace)
(17): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(18): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(19): ReLU(inplace)
(20): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(21): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(22): ReLU(inplace)
(23): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(24): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(25): ReLU(inplace)
(26): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(27): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(28): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(29): ReLU(inplace)
(30): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(31): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(32): ReLU(inplace)
(33): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(34): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(35): ReLU(inplace)
(36): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(37): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(38): ReLU(inplace)
(39): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(40): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(41): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(42): ReLU(inplace)
(43): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(44): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(45): ReLU(inplace)
(46): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(47): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(48): ReLU(inplace)
(49): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(50): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(51): ReLU(inplace)
(52): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(avgpool): AdaptiveAvgPool2d(output_size=(7, 7))
(classifier): Sequential(
(0): Linear(in_features=25088, out_features=4096, bias=True)
(1): ReLU(inplace)
(2): Dropout(p=0.5)
(3): Linear(in_features=4096, out_features=4096, bias=True)
(4): ReLU(inplace)
(5): Dropout(p=0.5)
(6): Linear(in_features=4096, out_features=1000, bias=True)
)
)
(global_pool): GlobalPool(
(avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
(maxpool): AdaptiveMaxPool2d(output_size=(1, 1))
(exp_pool): ExpPool()
(linear_pool): LinearPool()
(lse_pool): LogSumExpPool()
)
(fc_0): Conv2d(1024, 1, kernel_size=(1, 1), stride=(1, 1))
(fc_1): Conv2d(1024, 1, kernel_size=(1, 1), stride=(1, 1))
(fc_2): Conv2d(1024, 1, kernel_size=(1, 1), stride=(1, 1))
(fc_3): Conv2d(1024, 1, kernel_size=(1, 1), stride=(1, 1))
(fc_4): Conv2d(1024, 1, kernel_size=(1, 1), stride=(1, 1))
(fc_5): Conv2d(1024, 1, kernel_size=(1, 1), stride=(1, 1))
(fc_6): Conv2d(1024, 1, kernel_size=(1, 1), stride=(1, 1))
(fc_7): Conv2d(1024, 1, kernel_size=(1, 1), stride=(1, 1))
(bn_0): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(bn_1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(bn_2): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(bn_3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(bn_4): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(bn_5): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(bn_6): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(bn_7): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(attention_map): AttentionMap(
(channel_attention): CAModule(
(fc1): Linear(in_features=512, out_features=256, bias=True)
(fc2): Linear(in_features=256, out_features=512, bias=True)
(relu): ReLU()
(sigmoid): Sigmoid()
)
(spatial_attention): SAModule(
(conv1): Conv2d(512, 64, kernel_size=(1, 1), stride=(1, 1))
(conv2): Conv2d(512, 64, kernel_size=(1, 1), stride=(1, 1))
(conv3): Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1))
)
(pyramid_attention): FPAModule(
(gap_branch): Sequential(
(0): AdaptiveAvgPool2d(output_size=1)
(1): Conv2dNormRelu(
(conv): Sequential(
(0): Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1))
(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace)
)
)
)
(mid_branch): Conv2dNormRelu(
(conv): Sequential(
(0): Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1))
(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace)
)
)
(downsample1): Conv2dNormRelu(
(conv): Sequential(
(0): Conv2d(512, 1, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3))
(1): BatchNorm2d(1, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace)
)
)
(downsample2): Conv2dNormRelu(
(conv): Sequential(
(0): Conv2d(1, 1, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2))
(1): BatchNorm2d(1, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace)
)
)
(scale1): Conv2dNormRelu(
(conv): Sequential(
(0): Conv2d(1, 1, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))
(1): BatchNorm2d(1, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace)
)
)
(scale2): Conv2dNormRelu(
(conv): Sequential(
(0): Conv2d(1, 1, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(1): BatchNorm2d(1, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace)
)
)
(scale3): Conv2dNormRelu(
(conv): Sequential(
(0): Conv2d(1, 1, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): BatchNorm2d(1, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace)
)
)
)
)
(classifier): Identity()
), Linear(in_features=3000, out_features=8, bias=True)]