价值错误:gan中的火炬目标大小和火炬输入大小不匹配

发布于 2025-02-11 20:50:02 字数 5973 浏览 2 评论 0原文

嗨,我正在使用自定义图像的gan工作。我收到以下错误,这对我没有:

valueError:使用目标大小(Torch.Size([64,1]))与输入大小不同(Torch.Size([[47)) ,1]))被弃用。请确保它们的尺寸相同。

我看不到这些尺寸中的任何一个。有人可以帮我吗?该错误将在训练过程中的Loss_disrimenator上找到(用箭头标记)。我正在使用VS代码窗口。

epoch 0有效,然后出现问题是否正常?

[末端 - 时期0损耗区分和生成器的sceenshot] [1]

import torch
from glob import glob
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from skimage import io
import matplotlib.pyplot as plt


path = 'Punks'
image_paths = glob(path + '/*.png')

img_size = 28
batch_size = 32

transform = transforms.Compose(
    [
        transforms.ToPILImage(),
        transforms.Resize(img_size),
        transforms.CenterCrop(img_size),
        transforms.ToTensor(),
        transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
    ]
)


class ImageDataset(Dataset):
    def __init__(self, paths, transform):
        self.paths = paths
        self.transform = transform

    def __len__(self):
        return len(self.paths)

    def __getitem__(self, index):
        image_path = self.paths[index]
        image = io.imread(image_path)

        if self.transform:
            image_tensor = self.transform(image)

        return image_tensor


if __name__ == '__main__':

    dataset = ImageDataset(image_paths, transform)

    train_loader = DataLoader(
        dataset, batch_size=batch_size, num_workers=1, shuffle=True)

    # PLOTTING SAMPLES

    real_samples = next(iter(train_loader))
    for i in range(9):
        ax = plt.subplot(3, 3, 3 + 1)
        plt.imshow(real_samples[i].reshape(28, 28, 3))
        plt.xticks([])
        plt.yticks([])
        plt.show()

    device = 'cuda' if torch.cuda.is_available() else 'cpu'

    class Discriminator(nn.Module):
        def __init__(self):
            super().__init__()
            self.model = nn.Sequential(
                nn.Linear(784*3, 2048),
                nn.ReLU(),
                nn.Dropout(0.3),
                nn.Linear(2048, 1024),
                nn.ReLU(),
                nn.Dropout(0.3),
                nn.Linear(1024, 512),
                nn.ReLU(),
                nn.Dropout(0.3),
                nn.Linear(512, 256),
                nn.ReLU(),
                nn.Dropout(0.3),
                nn.Linear(256, 1),
                nn.Sigmoid(),
            )

        def forward(self, x):
            x = x.view(x.size(0), 784*3)  # change required for 3 channel image
            output = self.model(x)
            return output

    discriminator = Discriminator().to(device=device)

    class Generator(nn.Module):
        def __init__(self):
            super().__init__()
            self.model = nn.Sequential(
                nn.Linear(100, 256),
                nn.ReLU(),
                nn.Linear(256, 512),
                nn.ReLU(),
                nn.Linear(512, 1024),
                nn.ReLU(),
                nn.Linear(1024, 2048),
                nn.ReLU(),
                nn.Linear(2048, 784*3),
                nn.Tanh(),
            )

        def forward(self, x):
            output = self.model(x)
            output = output.view(x.size(0), 3, 28, 28)
            return output

    generator = Generator().to(device=device)

    # TRAINING PARAMS

    lr = 0.0001
    num_epochs = 10
    loss_function = nn.BCELoss()

    optimizer_discriminator = torch.optim.Adam(discriminator.parameters(), lr=lr)
    optimizer_generator = torch.optim.Adam(generator.parameters(), lr=lr)
    for epoch in range(num_epochs):
        for n, real_samples in enumerate(train_loader):
            # Data for training the discriminator
            real_samples = real_samples.to(device=device)
            real_samples_labels = torch.ones((batch_size, 1)).to(
                device=device
            )
            latent_space_samples = torch.randn((batch_size, 100)).to(
                device=device
            )
            print(f'Latent space samples : {latent_space_samples.shape}')
            generated_samples = generator(latent_space_samples)
            generated_samples_labels = torch.zeros((batch_size, 1)).to(
                device=device
            )
            all_samples = torch.cat((real_samples, generated_samples))
            print(f'Real samples : {real_samples.shape}, generated samples : {generated_samples.shape}')
            all_samples_labels = torch.cat(
                (real_samples_labels, generated_samples_labels)
            )

            # Training the discriminator
            discriminator.zero_grad()
            output_discriminator = discriminator(all_samples)
            loss_discriminator = loss_function(
                output_discriminator, all_samples_labels
            )
 ------->    loss_discriminator.backward()
            optimizer_discriminator.step()

            # Data for training the generator
            latent_space_samples = torch.randn((batch_size, 100)).to(
                device=device
            )

            # Training the generator
            generator.zero_grad()
            generated_samples = generator(latent_space_samples)
            output_discriminator_generated = discriminator(generated_samples)
            loss_generator = loss_function(
                output_discriminator_generated, real_samples_labels
            )
            loss_generator.backward()
            optimizer_generator.step()

            # Show loss
            if n == batch_size - 1:
                print(f"Epoch: {epoch} Loss D.: {loss_discriminator}")
                print(f"Epoch: {epoch} Loss G.: {loss_generator}")

    latent_space_samples = torch.randn(batch_size, 100).to(device=device)
    generated_samples = generator(latent_space_samples)

    generated_samples = generated_samples.cpu().detach()
    for i in range(9):
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(generated_samples[i].reshape(28, 28, 3))
        plt.xticks([])
        plt.yticks([])
        plt.show()´´´

Hi I am working on a GAN with custom images. I got the following error, which doesn't add up for me:

ValueError: Using a target size (torch.Size([64, 1])) that is different to the input size (torch.Size([47, 1])) is deprecated. Please ensure they have the same size.

I do not see where either of these sizes come from. Could someone please help me out? The error is to be found at the loss_disrimenator in the course of the training (marked with an arrow) after epoch 0. Below you find the related code. I am using vs code windows.

Also is it normal that epoch 0 works and then the problem appears?

[Sceenshot of Terminal- Epoch 0 Loss Discriminatorand Generator][1]

import torch
from glob import glob
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from skimage import io
import matplotlib.pyplot as plt


path = 'Punks'
image_paths = glob(path + '/*.png')

img_size = 28
batch_size = 32

transform = transforms.Compose(
    [
        transforms.ToPILImage(),
        transforms.Resize(img_size),
        transforms.CenterCrop(img_size),
        transforms.ToTensor(),
        transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
    ]
)


class ImageDataset(Dataset):
    def __init__(self, paths, transform):
        self.paths = paths
        self.transform = transform

    def __len__(self):
        return len(self.paths)

    def __getitem__(self, index):
        image_path = self.paths[index]
        image = io.imread(image_path)

        if self.transform:
            image_tensor = self.transform(image)

        return image_tensor


if __name__ == '__main__':

    dataset = ImageDataset(image_paths, transform)

    train_loader = DataLoader(
        dataset, batch_size=batch_size, num_workers=1, shuffle=True)

    # PLOTTING SAMPLES

    real_samples = next(iter(train_loader))
    for i in range(9):
        ax = plt.subplot(3, 3, 3 + 1)
        plt.imshow(real_samples[i].reshape(28, 28, 3))
        plt.xticks([])
        plt.yticks([])
        plt.show()

    device = 'cuda' if torch.cuda.is_available() else 'cpu'

    class Discriminator(nn.Module):
        def __init__(self):
            super().__init__()
            self.model = nn.Sequential(
                nn.Linear(784*3, 2048),
                nn.ReLU(),
                nn.Dropout(0.3),
                nn.Linear(2048, 1024),
                nn.ReLU(),
                nn.Dropout(0.3),
                nn.Linear(1024, 512),
                nn.ReLU(),
                nn.Dropout(0.3),
                nn.Linear(512, 256),
                nn.ReLU(),
                nn.Dropout(0.3),
                nn.Linear(256, 1),
                nn.Sigmoid(),
            )

        def forward(self, x):
            x = x.view(x.size(0), 784*3)  # change required for 3 channel image
            output = self.model(x)
            return output

    discriminator = Discriminator().to(device=device)

    class Generator(nn.Module):
        def __init__(self):
            super().__init__()
            self.model = nn.Sequential(
                nn.Linear(100, 256),
                nn.ReLU(),
                nn.Linear(256, 512),
                nn.ReLU(),
                nn.Linear(512, 1024),
                nn.ReLU(),
                nn.Linear(1024, 2048),
                nn.ReLU(),
                nn.Linear(2048, 784*3),
                nn.Tanh(),
            )

        def forward(self, x):
            output = self.model(x)
            output = output.view(x.size(0), 3, 28, 28)
            return output

    generator = Generator().to(device=device)

    # TRAINING PARAMS

    lr = 0.0001
    num_epochs = 10
    loss_function = nn.BCELoss()

    optimizer_discriminator = torch.optim.Adam(discriminator.parameters(), lr=lr)
    optimizer_generator = torch.optim.Adam(generator.parameters(), lr=lr)
    for epoch in range(num_epochs):
        for n, real_samples in enumerate(train_loader):
            # Data for training the discriminator
            real_samples = real_samples.to(device=device)
            real_samples_labels = torch.ones((batch_size, 1)).to(
                device=device
            )
            latent_space_samples = torch.randn((batch_size, 100)).to(
                device=device
            )
            print(f'Latent space samples : {latent_space_samples.shape}')
            generated_samples = generator(latent_space_samples)
            generated_samples_labels = torch.zeros((batch_size, 1)).to(
                device=device
            )
            all_samples = torch.cat((real_samples, generated_samples))
            print(f'Real samples : {real_samples.shape}, generated samples : {generated_samples.shape}')
            all_samples_labels = torch.cat(
                (real_samples_labels, generated_samples_labels)
            )

            # Training the discriminator
            discriminator.zero_grad()
            output_discriminator = discriminator(all_samples)
            loss_discriminator = loss_function(
                output_discriminator, all_samples_labels
            )
 ------->    loss_discriminator.backward()
            optimizer_discriminator.step()

            # Data for training the generator
            latent_space_samples = torch.randn((batch_size, 100)).to(
                device=device
            )

            # Training the generator
            generator.zero_grad()
            generated_samples = generator(latent_space_samples)
            output_discriminator_generated = discriminator(generated_samples)
            loss_generator = loss_function(
                output_discriminator_generated, real_samples_labels
            )
            loss_generator.backward()
            optimizer_generator.step()

            # Show loss
            if n == batch_size - 1:
                print(f"Epoch: {epoch} Loss D.: {loss_discriminator}")
                print(f"Epoch: {epoch} Loss G.: {loss_generator}")

    latent_space_samples = torch.randn(batch_size, 100).to(device=device)
    generated_samples = generator(latent_space_samples)

    generated_samples = generated_samples.cpu().detach()
    for i in range(9):
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(generated_samples[i].reshape(28, 28, 3))
        plt.xticks([])
        plt.yticks([])
        plt.show()´´´

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗心人 2025-02-18 20:50:02

由于这是在第一个时代结束时发生的,因此本质上发生的事情是您指定了64的批次大小,但是数据集中的图像数为some_integer_number * 64 + 47 < /代码>。这是因为当您分批阅读数据时,读取等于batch_size的样本数量。但是,当您到达时代的末尾时,可能要少于batch_size示例要加载。

在您的代码中,0th Epoch的最后一步中生成的图像数为47,而生成的假映像的数量为64,因为您使用batch_size来示例batch_size 假图像的数量。

一个简单的解决方案是在所有地方都使用len(real_samples)代替batch_size。您可以通过第一次设置batch_size = len(real_samples)作为 for 循环的第一行。

for epoch in range(num_epochs):
   for n, real_samples in enumerate(train_loader):
      # Data for training the discriminator
      batch_size = len(real_samples)
      real_samples = real_samples.to(device=device)
      real_samples_labels = torch.ones((batch_size, 1)).to(device=device)

      # rest of the code continues

我希望这能解决您的问题。

Since this is happening at the end of the first epoch, what's essentially happening is that you have specified a batch size of 64 but the number of images in your dataset is some_integer_number * 64 + 47. This is because when you read the data in batches, the number of samples equal to your batch_size is read. However, when you reach the end of the epoch, there is a possibility that fewer than batch_size examples are left to load.

In your code, the number of generated images in the last step of the 0th epoch is 47 whereas the number of fake images that you are generating is 64 since you use batch_size to sample batch_size number of fake images.

A simple solution would be to use len(real_samples) in place of batch_size at all the places. You can do this by first setting batch_size=len(real_samples) as the first line in the for loop.

for epoch in range(num_epochs):
   for n, real_samples in enumerate(train_loader):
      # Data for training the discriminator
      batch_size = len(real_samples)
      real_samples = real_samples.to(device=device)
      real_samples_labels = torch.ones((batch_size, 1)).to(device=device)

      # rest of the code continues

I hope this solves your issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文