Convolution neural network

Julian_Costa: <p>My code is,</p>
<p>import os<br>
import cv2<br>
import numpy as np<br>
from tqdm import tqdm<br>
import matplotlib.pyplot as plt<br>
import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
import torch.optim as optim</p>
<p>REBUILD_DATA = False</p>
<p>class Mea():</p>
<pre><code>IMG_SIZE = 50
Mea = "T"
LABELS={ Mea:(range(0,360))}
training_data=[]
Meacount = 0
</code></pre>
<p>def make_training_data(self):<br>
for label in self.LABELS:<br>
print(label)<br>
for f in tqdm(os.listdir(label)):<br>
path= os.path.join(label,f)<br>
img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)<br>
self.training_data.append([np.array(img), np.eye(360)[self.LABELS[label]]])</p>
<pre><code>            np.random.shuffle(self.training_data) 
            np.save("training_data.npy", self.training_data)
</code></pre>
<p>if REBUILD_DATA:<br>
MEA=Mea()<br>
MEA.make_training_data()</p>
<p>training_data = np.load(“training_data.npy”,allow_pickle=True)</p>
<p>class Net(nn.Module):</p>
<p>def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(1,32,5)<br>
self.conv2 = nn.Conv2d(32,64,5)<br>
self.conv3 = nn.Conv2d(64,128,5)</p>
<pre><code> x= torch.randn(50,50).view(-1,1,50,50)
 self._to_linear = None
 self.convs(x)
         
 self.fc1 = nn.Linear(self._to_linear,512)
 self.fc2 = nn.Linear(512,2)
</code></pre>
<p>def convs(self, x):</p>
<pre><code>    x=F.max_pool2d(F.relu(self.conv1(x)),(2,2))
    x=F.max_pool2d(F.relu(self.conv2(x)),(2,2))
    x=F.max_pool2d(F.relu(self.conv3(x)),(2,2))

    print(x[0].shape)

    if self._to_linear is None:
      self._to_linear = x[0].shape[0]*x[0].shape[1]*x[0].shape[2]

    return x
</code></pre>
<p>def forward(self, x):<br>
x= self.convs(x)<br>
x= x.view(-1, self._to_linear)<br>
x= F.relu(self.fc1(x))<br>
x= self.fc2(x)</p>
<pre><code>     return F.softmax(x, dim=1)
</code></pre>
<p>net = Net()<br>
<span class="hashtag">#print</span>(net)</p>
<p>optimizer = optim.Adam(net.parameters(), lr=0.001)<br>
loss_function = nn.MSELoss()</p>
<p>x= torch.Tensor([i[0] for i in training_data]).view(-1,50,50)<br>
x= x/255.0<br>
y= torch.Tensor([i[1] for i in training_data])</p>
<p>VAL_PCT=0.1<br>
val_size=int(len(x)*VAL_PCT)<br>
<span class="hashtag">#print</span>(val_size)</p>
<p>train_x = x[:-val_size]<br>
train_y = y[:-val_size]</p>
<p>test_x = x[-val_size:]<br>
test_y = y[-val_size:]</p>
<p>print(len(train_x), len(test_x))</p>
<p>BATCH_SIZE = 100<br>
EPOCHS = 1</p>
<p>for epoch in range(EPOCHS):<br>
for i in tqdm(range(0, len(train_x), BATCH_SIZE)):<br>
print(f"{i}:{i+BATCH_SIZE}")<br>
batch_x = train_x[i:i+BATCH_SIZE].view(-1, 1, 50, 50)<br>
batch_y = train_y[i:i+BATCH_SIZE]</p>
<pre><code>    net.zero_grad()

    outputs = net(batch_x)
    loss = loss_function(outputs, batch_y)
    loss.backward()
    optimizer.step()   

print(f"Epoch: {epoch}. Loss: {loss}")        
</code></pre>
<h4></h4>
<p>im getting this error,<br>
RuntimeError: The size of tensor a (2) must match the size of tensor b (360) at non-singleton dimension 2</p>

dejanbatanjac: <p>Hi, <a href="https://discuss.pytorch.org/u/Julian_Costa">Julian_Costa</a>,</p>
<p>Please go ahead and set your code inside ``` block, so it looks nice.</p>

albanD: <p>Hi,</p>
<p>Could you give the full stack trace that comes with the error? That will tell you which line is causing the issue. You can add printing at this line to check the size of the Tensor and investigate why they are not what you expect.</p>

Julian_Costa: <p>Warning (from warnings module):<br>
File “C:\Users\Asus\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\loss.py”, line 431<br>
return F.mse_loss(input, target, reduction=self.reduction)<br>
UserWarning: Using a target size (torch.Size([100, 360, 360])) that is different to the input size (torch.Size([100, 2])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.</p>
<p>0%|          | 0/4 [00:00&lt;?, ?it/s]<br>
Traceback (most recent call last):<br>
File “C:/Users/Asus/Desktop/Neural Networks/Pytorch.py”, line 115, in <br>
loss = loss_function(outputs, batch_y)<br>
File “C:\Users\Asus\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\module.py”, line 547, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “C:\Users\Asus\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\loss.py”, line 431, in forward<br>
return F.mse_loss(input, target, reduction=self.reduction)<br>
File “C:\Users\Asus\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\functional.py”, line 2189, in mse_loss<br>
expanded_input, expanded_target = torch.broadcast_tensors(input, target)<br>
File “C:\Users\Asus\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\functional.py”, line 53, in broadcast_tensors<br>
return torch._C._VariableFunctions.broadcast_tensors(tensors)<br>
RuntimeError: The size of tensor a (2) must match the size of tensor b (360) at non-singleton dimension 2</p>
<blockquote>
<blockquote>
<blockquote></blockquote>
</blockquote>
</blockquote>

albanD: <p>Hi, have you seen this warning in the stack trace:<br>
Warning (from warnings module):<br>
File “C:\Users\Asus\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\loss.py”, line 431<br>
return F.mse_loss(input, target, reduction=self.reduction)<br>
UserWarning: Using a target size (torch.Size([100, 360, 360])) that is different to the input size (torch.Size([100, 2])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.</p>
<p>So I think the problem is that what you give to your MSE loss are two tensors of different sizes, so it will give unexpected results.</p>