1 Star 0 Fork 0

闲人/dl_class

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
DCGAN.py 2.73 KB
一键复制 编辑 原始数据 按行查看 历史
闲人 提交于 2021-05-26 17:27 . update
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)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lhcse/dl_class.git
git@gitee.com:lhcse/dl_class.git
lhcse
dl_class
dl_class
master

搜索帮助