# 创建一个全卷积网络实例net
net = nn.Sequential(*list(pretrained_net.children())[:-2])# 这一行的作用就是将之前训练好的模型拿过来,把最后两层去掉
X = torch.rand(size=(1,3,320,480))
net(X).shape
/Users/tiger/opt/anaconda3/envs/d2l-zh/lib/python3.8/site-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at ../c10/core/TensorImpl.h:1156.)
return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
torch.Size([1, 512, 10, 15])
# 预测defpredict(img):
X = test_iter.dataset.normalize_image(img).unsqueeze(0)
pred = net(X.to(devices[0])).argmax(dim=1)return pred.reshape(pred.shape[1], pred.shape[2])
# 可视化预测的类别deflabel2image(pred):
colormap = torch.tensor(d2l.VOC_COLORMAP, device=devices[0])
X = pred.long()return colormap[X,:]
voc_dir = d2l.download_extract('voc2012','VOCdevkit/VOC2012')
test_images, test_labels = d2l.read_voc_images(voc_dir,False)
n, imgs =4,[]for i inrange(n):
crop_rect =(0,0,320,480)
X = torchvision.transforms.functional.crop(test_images[i],*crop_rect)
pred = label2image(predict(X))
imgs +=[
X.permute(1,2,0),
pred.cpu(),
torchvision.transforms.functional.crop(test_labels[i],*crop_rect).permute(1,2,0)]
d2l.show_images(imgs[::3]+ imgs[1::3]+ imgs[2::3],3, n, scale=2);