In my code i pass in a tensor.byte() to my model. It says:
RuntimeError: expected scalar type Byte but found Float
Here is my code: (without all the imports) skipping it for cleanness
device = ‘cuda’ if torch.cuda.is_available() else ‘cpu’
print(‘Using {} device’.format(device))
def tensorToImage(tensor,name):
cv2.imwrite(name, tensor.detach().numpy())
def imageToTensor(filename):
img = Image.open(filename)
return torch.from_numpy(numpy.array(img))
class net(nn.Module):
def init(self):
super(net, self).init()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
nn.Linear(128128, 150150), # take 128 by 128 image
nn.ReLU(),
nn.Linear(150150, 150150),
nn.ReLU(),
nn.Linear(150150, 128128),
)
def forward(self, x):
#x = self.flatten(x)
logits = self.linear_relu_stack(x)
return logits
model = net().to(device)
model.train()
x = torch.ones(28*28)
#tensorToImage(model(x),“pic.png”)
#torch.save(model.state_dict(), “cog”)
Initialize BCELoss function
criterion = nn.BCELoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
for epoch in range(2): # loop over the dataset multiple times
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
#ERROR HERE ERROR HERE ERROR HERE ERROR HERE ERROR HERE ERROR HERE ERROR HERE ERROR HERE
outputs = model(inp.byte()) #error on this line
#ERROR HERE ERROR HERE ERROR HERE ERROR HERE ERROR HERE
#ERROR HERE ERROR HERE ERROR HERE ERROR HERE ERROR HERE
loss = criterion(outputs, expectedOut)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss))
running_loss = 0.0
print(‘Finished Training’)
torch.save(model.state_dict(), “model”)
###########################################
im trying to train a model to get images from folder 2 and turn them into images in folder 3.
full error:
RuntimeError Traceback (most recent call last)
in ()
68
69 # forward + backward + optimize
—> 70 outputs = model(inp.byte())
71 loss = criterion(outputs, expectedOut)
72 loss.backward()
6 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
1845 if has_torch_function_variadic(input, weight):
1846 return handle_torch_function(linear, (input, weight), input, weight, bias=bias)
→ 1847 return torch._C._nn.linear(input, weight, bias)
1848
1849
RuntimeError: expected scalar type Byte but found Float