Unequal dimensions when converting from tensors to numpy

username: adomakor412
cooked: <p>Why is my dimension unequal?<br>
Full script here:</p>
<p><a href="https://github.com/adomakor412/NERTO_CIMSS_GOES-R/blob/ca2d75bce4a279a8781c0fc1f3567bdac1856fce/AI_model-BW-NUMPY.ipynb" rel="noopener nofollow ugc">https://github.com/adomakor412/NERTO_CIMSS_GOES-R/blob/ca2d75bce4a279a8781c0fc1f3567bdac1856fce/AI_model-BW-NUMPY.ipynb</a></p>
<pre><code class="lang-auto">device = torch.device("cuda" if torch.cuda.is_available() 
                                  else "cpu")
# model = models.resnet50(pretrained=True)
PATH = 'MODELS/model_epoch_60_May-10-21:1605_1620677105.pth'
model = torch.load(PATH)
#torch.save(model, 'ResnetPretrained.pth');
</code></pre>
<pre><code class="lang-auto">myTestData = []
myTrainData = []
myValData = []

def load_split_train_test(traindir, testdir, valdir):
    train_transforms = transforms.Compose([transforms.Resize(32),
                                       transforms.ToTensor(),
                                       ])
    test_transforms = transforms.Compose([transforms.Resize(32),
                                      transforms.ToTensor(),
                                      ])
    val_transforms = transforms.Compose([transforms.Resize(32),
                                      transforms.ToTensor(),
                                      ])

    train_data = datasets.ImageFolder(traindir,       
                    transform=train_transforms)
    test_data = datasets.ImageFolder(testdir,
                    transform=test_transforms)
    val_data = datasets.ImageFolder(valdir,
                    transform=test_transforms)
    
    
#     train_idx = list(range(len(traindir)))
#     nr.shuffle(np.array(train_idx))
    
#     test_idx = list(range(len(testdir)))
#     nr.shuffle(np.array(test_idx))
    
#     val_idx = list(range(len(valdir)))
#     nr.shuffle(np.array(val_idx))

    train_idx = list(range(len(traindir)))
    nr.shuffle(train_idx)
    
    test_idx = list(range(len(testdir)))
    nr.shuffle(test_idx)
    
    val_idx = list(range(len(valdir)))
    nr.shuffle(val_idx)
    
    train_sampler = SubsetRandomSampler(train_idx)
    test_sampler = SubsetRandomSampler(test_idx)
    val_sampler = SubsetRandomSampler(val_idx)

#     trainloader = torch.utils.data.DataLoader(train_data, sampler = train_sampler, batch_size = batch_size)
#     testloader = torch.utils.data.DataLoader(test_data, sampler = test_sampler, batch_size = batch_size)
#     valloader = torch.utils.data.DataLoader(val_data, sampler = val_sampler, batch_size = batch_size)
    
    trainloader = torch.utils.data.DataLoader(train_data, batch_size = batch_size)
    testloader = torch.utils.data.DataLoader(test_data, batch_size = batch_size)
    valloader = torch.utils.data.DataLoader(val_data, batch_size = batch_size)
    
    myTestData.append(test_data)
    myTrainData.append(train_data)
    myValData.append(val_data)
    
    return trainloader, testloader, valloader
</code></pre>
<pre><code class="lang-auto">trainloader, testloader, valloader = load_split_train_test(data_train_dir,
                                                           data_test_dir, 
                                                           data_val_dir)

print(valloader.dataset.classes)
print(testloader.dataset.classes)
print(trainloader.dataset.classes)
</code></pre>
<p>[‘fillin’, ‘sharkfin’]<br>
[‘fillin’, ‘sharkfin’]<br>
[‘fillin’, ‘sharkfin’]</p>
<pre><code class="lang-auto">train_X = next(iter(trainloader.dataset))[0].numpy()
test_X = trainloader.dataset.targets
train_y = next(iter(testloader.dataset))[0].numpy()
test_y = trainloader.dataset.targets

train_dataset = TensorDataset(torch.Tensor(train_X), torch.Tensor(train_y))
train_loader = DataLoader(train_dataset, batch_size=16, shuffle=True)

net = model

criterion = nn.CrossEntropyLoss()# cross entropy loss

optimizer = torch.optim.SGD(net.parameters(), lr=0.01)

net.train()

# for epoch in range(1000):
    
#     for inputs, targets in train_loader:
#         optimizer.zero_grad()
#         out = net(inputs)
#         loss = criterion(out, targets.long())
#         loss.backward()
#         optimizer.step()

#     if epoch % 100 == 0:
#         print('number of epoch', epoch, 'loss', loss.item())

predict_out = net(torch.Tensor(test_X))
_, predict_y = torch.max(predict_out, 1)

print('prediction accuracy', accuracy_score(test_y.data, predict_y.data))

print('macro precision', precision_score(test_y.data, predict_y.data, average='macro'))
print('micro precision', precision_score(test_y.data, predict_y.data, average='micro'))
print('macro recall', recall_score(test_y.data, predict_y.data, average='macro'))
print('micro recall', recall_score(test_y.data, predict_y.data, average='micro'))
</code></pre>

username: adomakor412
cooked: <hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
27 #         print(‘number of epoch’, epoch, ‘loss’, loss.item())<br>
28<br>
—&gt; 29 predict_out = net(torch.Tensor(test_X))<br>
30 _, predict_y = torch.max(predict_out, 1)<br>
31</p>
<p>/scratch/adomakor412/conda/envs/MyEnv/lib/python3.6/site-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
530             result = self._slow_forward(*input, **kwargs)<br>
531         else:<br>
 → 532             result = self.forward(*input, **kwargs)<br>
533         for hook in self._forward_hooks.values():<br>
534             hook_result = hook(self, input, result)</p>
<p>/scratch/adomakor412/conda/envs/MyEnv/lib/python3.6/site-packages/torchvision/models/resnet.py in forward(self, x)<br>
214<br>
215     def forward(self, x):<br>
 → 216         return self._forward_impl(x)<br>
217<br>
218</p>
<p>/scratch/adomakor412/conda/envs/MyEnv/lib/python3.6/site-packages/torchvision/models/resnet.py in _forward_impl(self, x)<br>
197     def _forward_impl(self, x):<br>
198         # See note [TorchScript super()]<br>
 → 199         x = self.conv1(x)<br>
200         x = self.bn1(x)<br>
201         x = self.relu(x)</p>
<p>/scratch/adomakor412/conda/envs/MyEnv/lib/python3.6/site-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
530             result = self._slow_forward(*input, **kwargs)<br>
531         else:<br>
 → 532             result = self.forward(*input, **kwargs)<br>
533         for hook in self._forward_hooks.values():<br>
534             hook_result = hook(self, input, result)</p>
<p>/scratch/adomakor412/conda/envs/MyEnv/lib/python3.6/site-packages/torch/nn/modules/conv.py in forward(self, input)<br>
343<br>
344     def forward(self, input):<br>
 → 345         return self.conv2d_forward(input, self.weight)<br>
346<br>
347 class Conv3d(_ConvNd):</p>
<p>/scratch/adomakor412/conda/envs/MyEnv/lib/python3.6/site-packages/torch/nn/modules/conv.py in conv2d_forward(self, input, weight)<br>
340                             _pair(0), self.dilation, self.groups)<br>
341         return F.conv2d(input, weight, self.bias, self.stride,<br>
 → 342                         self.padding, self.dilation, self.groups)<br>
343<br>
344     def forward(self, input):</p>
<p>RuntimeError: Expected 4-dimensional input for 4-dimensional weight 64 3 7 7, but got 1-dimensional input of size [1455] instead</p>

username: adomakor412
cooked: <p>Well, even though I had the right <code>dim</code> I need to pick the last value of nn.Linear(…, last) to match the length of my class</p>
<pre><code class="lang-auto">for param in model.parameters():
    param.requires_grad = False
    
model.fc = nn.Sequential(nn.Linear(2048, 512),
                                 nn.ReLU(),
                                 nn.Dropout(0.2),
                                 nn.Linear(512, 2),
                                 nn.LogSoftmax(dim=1))
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.fc.parameters(), lr=0.003)
model.to(device);
</code></pre>
<p>model.fc = nn.Sequential(nn.Linear(x, y),<br>
nn.ReLU(),<br>
nn.Dropout(0.2),<br>
nn.Linear(y, len(class)),<br>
nn.LogSoftmax(dim=1))</p>