当前位置: 代码迷 >> 综合 >> The “freeze_support()“ line can be omitted if the program is not going to be frozen to produ
  详细解决方案

The “freeze_support()“ line can be omitted if the program is not going to be frozen to produ

热度:33   发布时间:2024-02-04 12:45:04.0

这是在pytorch官网60分钟学习时遇到的一个问题,训练图像分类器中,一开始要下载训练集和测试集,其中下载代码如下

trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)                                    

在训练的时候download都设置为True,因为我们要下载,但是在下载完之后要打开图片,就需要改为False,并且,还应该把训练图像的代码放入下方中,这样才可以正常运行

if __name__ == '__main__':

完整代码如下:

# Training an image classifier训练图像分类器
# 1. Loading and normalizing CIFAR10加载并标准化CIFAR10
import torch
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np# If running on Windows and you get a BrokenPipeError, try setting the num_worker of torch.utils.data.DataLoader() to 0.
# 如果在Windows上运行时遇到BrokenPipeError,请尝试设置torch.utils.data.DataLoader()为0。# 下载训练集和测试集
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=False, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')if __name__ == '__main__':# functions to show an imagedef imshow(img):img = img / 2 + 0.5     # unnormalizenpimg = img.numpy()plt.imshow(np.transpose(npimg, (1, 2, 0)))plt.show()# get some random training imagesdataiter = iter(trainloader)images, labels = dataiter.next()# show imagesimshow(torchvision.utils.make_grid(images))# print labelsprint(' '.join('%5s' % classes[labels[j]] for j in range(4)))
  相关解决方案