層次3 DCGAN動漫影像生成

專注的阿熊發表於2022-04-29

import torch.nn as nn

import torch

import cv2

import os

# 路徑改為反斜槓,在 linux windows 中都可使用

pkl_dir = "D:/DCGAN/pkl/25g.pkl"

# 在同目錄下建立一個 bitmap_epoch10 資料夾

test_dir = "bitmaps_epoch10/"

if not os.path.exists(test_dir): os.makedirs(test_dir)

device = torch.device("cuda:0" if (torch.cuda.is_available()) else "cpu")

class Generator(nn.Module):

     def __init__(self):

         super(Generator, self).__init__()

         self.deconv1 =外匯跟單gendan5.com nn.Sequential(

             nn.ConvTranspose2d(  # stride(input_w-1)+k-2*Padding

                 in_channels=100,

                 out_channels=64 * 8,

                 kernel_size=4,

                 stride=1,

                 padding=0,

                 bias=False,

             ),

             nn.BatchNorm2d(64 * 8),

             nn.ReLU(inplace=True),

         )  # 14

         self.deconv2 = nn.Sequential(

             nn.ConvTranspose2d(  # stride(input_w-1)+k-2*Padding

                 in_channels=64 * 8,

                 out_channels=64 * 4,

                 kernel_size=4,

                 stride=2,

                 padding=1,

                 bias=False,

             ),

             nn.BatchNorm2d(64 * 4),

             nn.ReLU(inplace=True),

         )  # 24

         self.deconv3 = nn.Sequential(

             nn.ConvTranspose2d(  # stride(input_w-1)+k-2*Padding

                 in_channels=64 * 4,

                 out_channels=64 * 2,

                 kernel_size=4,

                 stride=2,

                 padding=1,

                 bias=False,

             ),

             nn.BatchNorm2d(64 * 2),

             nn.ReLU(inplace=True),

         )  # 48

         self.deconv4 = nn.Sequential(

             nn.ConvTranspose2d(  # stride(input_w-1)+k-2*Padding

                 in_channels=64 * 2,

                 out_channels=64 * 1,

                 kernel_size=4,

                 stride=2,

                 padding=1,

                 bias=False,

             ),

             nn.BatchNorm2d(64),

             nn.ReLU(inplace=True),

         )

         self.deconv5 = nn.Sequential(

             nn.ConvTranspose2d(64, 3, 5, 3, 1, bias=False),

             nn.Tanh(),

         )

     def forward(self, x):

         x = self.deconv1(x)

         x = self.deconv2(x)

         x = self.deconv3(x)

         x = self.deconv4(x)

         x = self.deconv5(x)

         return x

g = torch.load(pkl_dir)

imgs = g(torch.randn(100, 100, 1, 1).to(device))

for i in range(len(imgs)):

     img = imgs[i].permute(1, 2, 0).cpu().detach().numpy() * 255

     cv2.imwrite(test_dir + str(i) + ".jpg", img, )

print(" test done")


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2889417/,如需轉載,請註明出處,否則將追究法律責任。

相關文章