[英]PyTorch | getting "RuntimeError: Found dtype Long but expected Float"
I'm trying to train a CNN on a custom dataset.我正在尝试在自定义数据集上训练 CNN。 Code:
代码:
Dataset.py
class MyDataset(Dataset):
def __init__(self, csv_file, root_dir):
self.annotations = pd.read_csv(csv_file)
self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.annotations)
def __getitem__(self, index):
img_path = os.path.join(self.root_dir, self.annotations.iloc[index, 0])
y_label = torch.tensor(int(self.annotations.iloc[index, 1]))
img = cv2.imread(img_path)
# resize
res = cv2.resize(img, dsize=(50, 50), interpolation=cv2.INTER_CUBIC)
# convert image to tensor
res = torch.from_numpy(res)
return (res, y_label)
Model.py
class ConvNet(torch.nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
f2 = 4
self.layer2 = nn.Sequential(
nn.Conv2d(50, f2, kernel_size=5, padding=2),
nn.ReLU(),
nn.BatchNorm2d(f2),
nn.MaxPool2d(kernel_size=2, stride=2))
self.fc1 = nn.Linear(100, 200)
self.fc2 = nn.Linear(200, 20)
self.fc3 = nn.Linear(20, 1)
def forward(self, x):
x = self.layer2(x.float())
x = x.reshape(x.size(0), -1)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
return x
and here is my training code:这是我的培训代码:
dataset = MyDataset(
csv_file='dataset.csv',
root_dir='tmp')
train_set, test_set = torch.utils.data.random_split(dataset, lengths=[500, 70])
train_loader = DataLoader(dataset=train_set, batch_size=16, shuffle=True)
test_loader = DataLoader(dataset=test_set, batch_size=16, shuffle=True)
model = ConvNet()
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
for epoch in range(20):
losses = []
for batch_idx, (data, targets) in enumerate(train_loader):
data = data.to(device=device)
targets = targets.to(device=device)
# forward
scores = model(data)
loss = criterion(scores, targets)
losses.append(loss.item())
# backward
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('Cost: {0} = {1}'.format(epoch, sum(losses)/len(losses)))
But I get RuntimeError: Found dtype Long but expected Float
.但我得到
RuntimeError: Found dtype Long but expected Float
。 This probably comes from the fact that I do x = self.layer2(x.float())
to avoid overflow.这可能来自我执行
x = self.layer2(x.float())
以避免溢出的事实。
I would like to know how to fix that error.我想知道如何解决该错误。 It's difficult to pin-point where the exact problem comes from.
很难确定问题的确切来源。
How can I solve this?我该如何解决这个问题?
The problem might be caused by data tensor.该问题可能是由数据张量引起的。 When data loader calls images via getitem () method, image is read with opencv and transformed to tensor.
当数据加载器通过 getitem() 方法调用图像时,使用opencv读取图像并转换为张量。 I think at that point, type of your data tensor is long but it should be float.
我认为在这一点上,你的数据张量的类型很长,但它应该是浮动的。 If you cast your numpy array -named res - to float it should work fine.
如果您将 numpy 数组 - 命名为res - 转换为浮动,它应该可以正常工作。 You can see my solution below.
你可以在下面看到我的解决方案。
res = cv2.resize(img, dsize=(50, 50), interpolation=cv2.INTER_CUBIC)
res = res.astype(np.float32) # you should add this line
res = torch.from_numpy(res)
res = res.permute(2, 0, 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.