分类: Python/Ruby
2022-04-29 14:36:03
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")