代码拉取完成,页面将自动刷新
import torch
import torch.nn as nn
from utils import Swish
class Generator(torch.nn.Module):
def __init__(self, channels):
super().__init__()
# Filters [1024, 512, 256]
# Input_dim = 100
# Output_dim = C (number of channels)
self.main_module = nn.Sequential(
# Z latent vector 100
nn.ConvTranspose2d(in_channels=100, out_channels=1024, kernel_size=2, stride=1, padding=0),
nn.BatchNorm2d(num_features=1024),
Swish(),
# State (1024x2x2)
nn.ConvTranspose2d(in_channels=1024, out_channels=512, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(num_features=512),
Swish(),
# State (1024x4x4)
nn.ConvTranspose2d(in_channels=512, out_channels=256, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(num_features=256),
nn.ReLU(True),
# State (512x8x8)
nn.ConvTranspose2d(in_channels=256, out_channels=64, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(num_features=64),
nn.ReLU(True),
# State (256x16x16)
nn.ConvTranspose2d(in_channels=64, out_channels=channels, kernel_size=4, stride=2, padding=1))
# output of main module --> Image (Cx32x32)
self.output = nn.Tanh()
def forward(self, x):
x = self.main_module(x)
return self.output(x)
class Discriminator(torch.nn.Module):
def __init__(self, channels):
super().__init__()
# Filters [256, 512, 1024]
# Input_dim = channels (Cx64x64)
# Output_dim = 1
self.main_module = nn.Sequential(
# Image (Cx32x32)
nn.Conv2d(in_channels=channels, out_channels=256, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(0.2, inplace=True),
# State (256x16x16)
nn.Conv2d(in_channels=256, out_channels=512, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2, inplace=True),
# State (512x8x8)
nn.Conv2d(in_channels=512, out_channels=1024, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(1024),
nn.LeakyReLU(0.2, inplace=True))
# outptut of main module --> State (1024x4x4)
self.output = nn.Sequential(
nn.Conv2d(in_channels=1024, out_channels=1, kernel_size=4, stride=1, padding=0),
# Output 1
nn.Sigmoid())
def forward(self, x):
x = self.main_module(x)
return self.output(x)
def feature_extraction(self, x):
# Use discriminator for feature extraction then flatten to vector of 16384 features
x = self.main_module(x)
return x.view(-1, 1024*4*4)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。