Passing in byte, but crys about not being a byte? WHAT!

Trevor_Blythe: <p>In my code i pass in a tensor.byte() to my model.  It says:<br>
RuntimeError: expected scalar type Byte but found Float</p>
<p>Here is my code:  (without all the imports) skipping it for cleanness</p>
<p>device = ‘cuda’ if torch.cuda.is_available() else ‘cpu’<br>
print(‘Using {} device’.format(device))</p>
<p>def tensorToImage(tensor,name):<br>
cv2.imwrite(name, tensor.detach().numpy())</p>
<p>def imageToTensor(filename):<br>
img = Image.open(filename)<br>
return torch.from_numpy(numpy.array(img))</p>
<p>class net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(net, self).<strong>init</strong>()<br>
self.flatten = nn.Flatten()<br>
self.linear_relu_stack = nn.Sequential(<br>
nn.Linear(128<em>128, 150</em>150), # take 128 by 128 image<br>
nn.ReLU(),<br>
nn.Linear(150<em>150, 150</em>150),<br>
nn.ReLU(),<br>
nn.Linear(150<em>150, 128</em>128),<br>
)</p>
<pre><code>def forward(self, x):
    #x = self.flatten(x)
    logits = self.linear_relu_stack(x)
    return logits
</code></pre>
<p>model = net().to(device)</p>
<p>model.train()</p>
<p>x = torch.ones(28*28)<br>
<span class="hashtag">#tensorToImage</span>(model(x),“pic.png”)</p>
<p><span class="hashtag">#torch</span>.save(model.state_dict(), “cog”)</p>
<h1>
<a name="initialize-bceloss-function-1" class="anchor" href="#initialize-bceloss-function-1"></a>Initialize BCELoss function</h1>
<p>criterion = nn.BCELoss()<br>
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)</p>
<p>for epoch in range(2):  # loop over the dataset multiple times</p>
<pre><code>running_loss = 0.0
for i in range(50):
    # get the inputs; data is a list of [inputs, labels]
    inp = torch.flatten(imageToTensor("2/" + str(i) + ".png"))
    expectedOut = torch.flatten(imageToTensor("3/" + str(i )+ ".png"))
    # zero the parameter gradients
    optimizer.zero_grad()

    # forward + backward + optimize
</code></pre>
<p><span class="hashtag">#ERROR</span> HERE ERROR HERE ERROR HERE ERROR HERE ERROR HERE ERROR HERE ERROR HERE ERROR HERE<br>
outputs = model(inp.byte()) <span class="hashtag">#error</span> on this line<br>
<span class="hashtag">#ERROR</span> HERE ERROR HERE ERROR HERE ERROR HERE ERROR HERE<br>
<span class="hashtag">#ERROR</span> HERE ERROR HERE ERROR HERE ERROR HERE ERROR HERE<br>
loss = criterion(outputs, expectedOut)<br>
loss.backward()<br>
optimizer.step()</p>
<pre><code>    # print statistics
    running_loss += loss.item()

    print('[%d, %5d] loss: %.3f' %
              (epoch + 1, i + 1, running_loss))
    running_loss = 0.0
</code></pre>
<p>print(‘Finished Training’)</p>
<p>torch.save(model.state_dict(), “model”)<br>
###########################################</p>
<p>im trying to train a model to get images from folder 2 and turn them into images in folder 3.</p>
<h2>
<a name="full-error-2" class="anchor" href="#full-error-2"></a>full error:</h2>
<p>RuntimeError                              Traceback (most recent call last)</p>
<p> in ()<br>
68<br>
69         # forward + backward + optimize<br>
—&gt; 70         outputs = model(inp.byte())<br>
71         loss = criterion(outputs, expectedOut)<br>
72         loss.backward()</p>
<p>6 frames</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)<br>
1845     if has_torch_function_variadic(input, weight):<br>
1846         return handle_torch_function(linear, (input, weight), input, weight, bias=bias)<br>
 → 1847     return torch._C._nn.linear(input, weight, bias)<br>
1848<br>
1849</p>
<p>RuntimeError: expected scalar type Byte but found Float</p>

ptrblck: <p>The error points to the wrong <code>dtype</code> of the model parameters, so you would have to call <code>byte()</code> on the model, too. However, that’s not possible since modules only accept floating point types.</p>

Trevor_Blythe: <p>i fixed it by removing the .byte() at the end??? Explain google? is it c optimised tho?</p>