Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3657127
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2022-04-29 14:36:03

import torch.nn as nn

import torch

import cv2

import os

# 路径改为反斜杠,在linuxwindows中都可使用

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")

阅读(536) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~