開始使用免費開始

產生影像

現在你已經設計並訓練好 GAN,是時候評估它能產生的影像品質了。首先,先以目視檢查,看看產生的結果是否看起來像寶可夢。為此,你會建立作為產生器輸入的隨機雜訊,將它傳入模型,並繪出輸出結果。

已替你提供帶有訓練權重的深度卷積式產生器 gentorch 以及 matplotlib.pyplot(別名 plt)也都已匯入完成。

本練習屬於課程

使用 PyTorch 進行影像深度學習

檢視課程

練習說明

  • 建立形狀為 num_images_to_generate 乘以 16(你用來訓練產生器的輸入雜訊大小)的隨機雜訊張量,指派給 noise
  • 將雜訊傳入產生器以產生影像,並指派給 fake
  • 在 for 迴圈中,對 fake 進行切片,擷取第 i 張影像,指派給 image_tensor
  • image_tensor 的維度由(color, height, width)置換為(height, width, color),並將輸出指派給 image_tensor_permuted

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

num_images_to_generate = 5
# Create random noise tensor
noise = ____

# Generate images
with torch.no_grad():
    fake = ____
print(f"Generated tensor shape: {fake.shape}")
    
for i in range(num_images_to_generate):
    # Slice fake to select i-th image
    image_tensor = ____
    # Permute the image dimensions
    image_tensor_permuted = ____
    plt.imshow(image_tensor_permuted)
    plt.show()
編輯並執行程式碼