1. 程式人生 > 其它 >《動手學深度學習Pytorch版》之AlexNet預測結果展示

《動手學深度學習Pytorch版》之AlexNet預測結果展示

技術標籤:Pytorch深度學習python深度學習

《動手學深度學習Pytorch版》https://github.com/ShusenTang/Dive-into-DL-PyTorch
為了檢視預測結果,在原Demo上加了預測顯示程式碼。
注意

  • 影象尺寸由原先的 28 × 28 28\times28 28×28變為 224 × 224 224\times224 224×224,因此顯示函式需要做變動。
  • 由於使用了GPU,所以預測資料也要做相應的轉換。

預測程式如下:

# d2l.show_fashion_mnist??
import matplotlib.
pyplot as plt def show_fashion_mnist(images, labels): d2l.use_svg_display() # 這裡的_表示我們忽略(不使用)的變數 _, figs = plt.subplots(1, len(images), figsize=(12, 12)) for f, img, lbl in zip(figs, images, labels): f.imshow(img.view((224, 224)).numpy()) #之前案例影象的尺寸為28*28,此處需要修改 f.set_title(
lbl) f.axes.get_xaxis().set_visible(False) f.axes.get_yaxis().set_visible(False) X, y = iter(test_iter).next() #此處的X,y在cpu上 true_labels = d2l.get_fashion_mnist_labels(y.numpy()) pred_labels = d2l.get_fashion_mnist_labels(net(X.cuda()).argmax(dim=1).cpu().numpy()) #將測試集資料移到GPU上,用於計算,將計算結果移到cpu上
titles = [true + '\n' + pred for true, pred in zip(true_labels, pred_labels)] show_fashion_mnist(X[0:10], titles[0:10])

結果

training on  cuda
epoch 1, loss 0.2106, train acc 0.922, test acc 0.915, time 21.6 sec
epoch 2, loss 0.2100, train acc 0.922, test acc 0.915, time 21.8 sec
epoch 3, loss 0.2109, train acc 0.923, test acc 0.915, time 21.9 sec
epoch 4, loss 0.2092, train acc 0.922, test acc 0.915, time 21.7 sec
epoch 5, loss 0.2087, train acc 0.922, test acc 0.915, time 21.8 sec

看起來,預測結果高了不少
在這裡插入圖片描述