Not sure how to get output of model?

Bastulli: <aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="16" height="16">
      <a href="https://github.com/bastulli/AutoCarJetsonNano" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars1.githubusercontent.com/u/18600510?s=400&amp;amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/bastulli/AutoCarJetsonNano" target="_blank" rel="nofollow noopener">bastulli/AutoCarJetsonNano</a></h3>

<p>PyTorch Python Neural Network Autonomous 1/10 Car for Nvidia Jetson Nano - bastulli/AutoCarJetsonNano</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>I’m trying to control the steering of a car by getting an output between -1 ,1.  Currently, I trained my network with this model.</p>
<pre><code class="lang-auto">import torch.nn as nn
import torch.nn.functional as F

# define the CNN architecture
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # convolutional layer (sees 32x32x3 image tensor)
        self.conv1 = nn.Conv2d(3, 16, 3, padding=1)
        # convolutional layer (sees 16x16x16 tensor)
        self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
        self.pool = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(32 * 4 * 4, 256)
        self.fc2 = nn.Linear(256, 1)
        self.dropout = nn.Dropout(0.25)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        # flatten image input
        x = x.view(-1, 32 * 4 * 4)
        x = self.dropout(x)
        x = F.relu(self.fc1(x))
        x = self.dropout(x)
        x = self.fc2(x)
        return x

# create a complete CNN
model = Net()
print(model)

# move tensors to GPU if CUDA is available
if train_on_gpu:
    model.cuda()
</code></pre>
<p><a href="https://github.com/bastulli/AutoCarJetsonNano/blob/master/train/autocar.ipynb" rel="nofollow noopener">Also you can see the jupyter notebook here</a>  The problem is i’m not sure what to use to get the output in real time such as np.argmax, np.max, ect…</p>
<pre><code class="lang-auto">    def autopilot(self):
        
        img = self.preprocess(self.cam.value)
        count = self.cam.count
        
        if count!= self.temp:
            print('RUN!')
            self.model.eval()
            with torch.no_grad():
                output = self.model(img)
            _, angle_tensor = torch.max(output,1)
            self.angle_out = angle_tensor.cpu().data.numpy()
            #self.angle_out = np.argmax(output.cpu().data.numpy())#angle[0].cpu().numpy()
            self.temp = count
            print(self.angle_out)
            
        else:
            pass
</code></pre>
<p>I’m new to pytorch and i’m stuck trying to get the correct output like I did in jupyter notebook</p>

JuanFMontesinos: <p>In teory you are not doing anything wrong.<br>
To get outputs you just have to do the same than for training but adding under the decoration wtih torch.no_grad():</p>
<p>is it not what you tried?<br>
COuld you provide few more details about what does it fail?</p>

Bastulli: <p>The issue that I’m having is that my output is a array that is 195 in length for each image I pass through my model.  I just want a single numpy output between -1,1.  I think I set up my output function incorrectly.  Here is a project very similar to what i’m trying to do <a href="https://github.com/autorope/donkeycar/blob/dev/donkeycar/parts/keras.py" rel="nofollow noopener">https://github.com/autorope/donkeycar/blob/dev/donkeycar/parts/keras.py</a></p>

JuanFMontesinos: <p>But as far as I can see you have a fc layer which goes from 256 to 1 feature. I find it’s not possible you to get 195 elements on a tensor. If you does not modify the network it should be a tensor of size batch x 1 or similar to that.<br>
Could you print output shape? Or, are you running batch size 195 or getting 195 items in the batch dimension when you apply that x = x.view(-1, 32 * 4 * 4)?</p>

Bastulli: <p>Here is my output<br>
<img src="https://discuss.pytorch.org/uploads/default/original/2X/0/0119094e615488b42c1019859cc40dd9186ab16d.png" alt="Screenshot%20from%202019-05-02%2016-37-07" data-base62-sha1="9I7i5dlA83kFdk8M2wanN7dlPn" width="672" height="144"> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/1/1fd68f53b55a03f0d1d941bd6fa2e5c29443eaac.png" data-download-href="https://discuss.pytorch.org/uploads/default/1fd68f53b55a03f0d1d941bd6fa2e5c29443eaac" title="Screenshot%20from%202019-05-02%2016-37-25"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/1/1fd68f53b55a03f0d1d941bd6fa2e5c29443eaac_2_690x271.png" alt="Screenshot%20from%202019-05-02%2016-37-25" data-base62-sha1="4xEuCT3UBqLuFD8wQg7jk6uKgpK" width="690" height="271" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/1/1fd68f53b55a03f0d1d941bd6fa2e5c29443eaac_2_690x271.png, https://discuss.pytorch.org/uploads/default/original/2X/1/1fd68f53b55a03f0d1d941bd6fa2e5c29443eaac.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/1/1fd68f53b55a03f0d1d941bd6fa2e5c29443eaac.png 2x" data-dominant-color="ECEBEC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot%20from%202019-05-02%2016-37-25</span><span class="informations">799×314 34.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

JuanFMontesinos: <p>The problem is that your output before applying reshaping is Batchx32x56x56<br>
Why do you reshape that to -1,512?<br>
It makes no sense and that’s why it does not work.</p>
<p>You are squeezing basically maping around 100k elements into a 196*512 matrix</p>

Bastulli: <p>Thanks, I never changed the flatten size after modifying a the network.  I forgot my input is 244x244 &gt; maxpool  &gt; 122x122 &gt; maxpool  &gt; 56x56.  Then multiplied by out_channel size of 32</p>