【问题标题】:PyTorch | getting "RuntimeError: Found dtype Long but expected Float"火炬 |得到“RuntimeError: Found dtype Long but expected Float”
【发布时间】:2021-04-09 17:28:15
【问题描述】:

我正在尝试在自定义数据集上训练 CNN。代码:

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

这是我的训练代码:

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)))

但我得到RuntimeError: Found dtype Long but expected Float。这可能是因为我使用x = self.layer2(x.float()) 来避免溢出。

我想知道如何解决这个错误。很难确定问题出在哪里。

我该如何解决这个问题?

【问题讨论】:

    标签: python deep-learning pytorch tensor


    【解决方案1】:

    问题可能是由 data 张量引起的。当数据加载器通过 getitem() 方法调用图像时,图像被 opencv 读取并转换为张量。我认为在这一点上,你的 data 张量类型很长,但它应该是浮动的。如果您将 numpy 数组 -named res- 转换为浮动,它应该可以正常工作。您可以在下面看到我的解决方案。

    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)
    

    【讨论】:

    • 我已经更改了我的代码,上面写着RuntimeError: Given groups=1, weight of size [3, 1, 5, 5], expected input[16, 50, 50, 3] to have 1 channels, but got 50 channels instead now...
    • 我不明白为什么预期的输入应该有 1 个通道。但是输入张量的格式是错误的。输入具有格式(批量大小 x 高度 x 宽度 x 通道数)。格式应该是(批量大小、通道数、高度、宽度)。我编辑了代码块并添加了最后一行以更改输入格式。
    猜你喜欢
    • 2021-11-06
    • 2021-03-19
    • 2021-07-31
    • 2021-05-10
    • 2020-02-01
    • 2019-12-13
    • 2020-10-24
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多