forked from ankane/torch.rb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgan.rb
152 lines (120 loc) · 4 KB
/
gan.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# ported from PyTorch-GAN
# https://github.com/eriklindernoren/PyTorch-GAN/blob/master/implementations/gan/gan.py
# see LICENSE-gan-examples.txt
# paper: https://arxiv.org/abs/1406.2661
require "torch"
require "torchvision"
Dir.mkdir("images") unless Dir.exist?("images")
img_shape = [1, 28, 28]
cuda = Torch::CUDA::available?
class Generator < Torch::NN::Module
def initialize(img_shape)
super()
@img_shape = img_shape
block = lambda do |in_feat, out_feat, normalize=true|
layers = [Torch::NN::Linear.new(in_feat, out_feat)]
if normalize
layers << Torch::NN::BatchNorm1d.new(out_feat, eps: 0.8)
end
layers << Torch::NN::LeakyReLU.new(negative_slope: 0.2, inplace: true)
layers
end
@model = Torch::NN::Sequential.new(
*block.call(100, 128, false),
*block.call(128, 256),
*block.call(256, 512),
*block.call(512, 1024),
Torch::NN::Linear.new(1024, img_shape.inject(:*)),
Torch::NN::Tanh.new
)
end
def forward(z)
img = @model.call(z)
img = img.view(img.size(0), *@img_shape)
img
end
end
class Discriminator < Torch::NN::Module
def initialize(img_shape)
super()
@model = Torch::NN::Sequential.new(
Torch::NN::Linear.new(img_shape.inject(:*), 512),
Torch::NN::LeakyReLU.new(negative_slope: 0.2, inplace: true),
Torch::NN::Linear.new(512, 256),
Torch::NN::LeakyReLU.new(negative_slope: 0.2, inplace: true),
Torch::NN::Linear.new(256, 1),
Torch::NN::Sigmoid.new,
)
end
def forward(img)
img_flat = img.view(img.size(0), -1)
validity = @model.call(img_flat)
validity
end
end
# Loss function
adversarial_loss = Torch::NN::BCELoss.new
# Initialize generator and discriminator
generator = Generator.new(img_shape)
discriminator = Discriminator.new(img_shape)
if cuda
generator.cuda
discriminator.cuda
adversarial_loss.cuda
end
# Configure data loader
dataloader = Torch::Utils::Data::DataLoader.new(
TorchVision::Datasets::MNIST.new(
"./data",
train: true,
download: true,
transform: TorchVision::Transforms::Compose.new(
[TorchVision::Transforms::Resize.new(28), TorchVision::Transforms::ToTensor.new, TorchVision::Transforms::Normalize.new([0.5], [0.5])]
)
),
batch_size: 64,
shuffle: true
)
# Optimizers
optimizer_g = Torch::Optim::Adam.new(generator.parameters, lr: 0.0002, betas: [0.5, 0.999])
optimizer_d = Torch::Optim::Adam.new(discriminator.parameters, lr: 0.0002, betas: [0.5, 0.999])
Tensor = cuda ? Torch::CUDA::FloatTensor : Torch::FloatTensor
# ----------
# Training
# ----------
200.times do |epoch|
dataloader.each_with_index do |(imgs, _), i|
# Adversarial ground truths
valid = Tensor.new(imgs.size(0), 1).fill!(1.0)
fake = Tensor.new(imgs.size(0), 1).fill!(0.0)
# Configure input
real_imgs = imgs.type(Tensor)
# -----------------
# Train Generator
# -----------------
optimizer_g.zero_grad
# Sample noise as generator input
z = Tensor.new(imgs.shape[0], 100).normal!(0, 1)
# Generate a batch of images
gen_imgs = generator.call(z)
# Loss measures generator's ability to fool the discriminator
g_loss = adversarial_loss.call(discriminator.call(gen_imgs), valid)
g_loss.backward
optimizer_g.step
# ---------------------
# Train Discriminator
# ---------------------
optimizer_d.zero_grad
# Measure discriminator's ability to classify real from generated samples
real_loss = adversarial_loss.call(discriminator.call(real_imgs), valid)
fake_loss = adversarial_loss.call(discriminator.call(gen_imgs.detach), fake)
d_loss = (real_loss + fake_loss) / 2
d_loss.backward
optimizer_d.step
puts "[Epoch %d/%d] [Batch %d/%d] [D loss: %f] [G loss: %f]" % [epoch, 200, i, dataloader.size, d_loss.item, g_loss.item]
batches_done = epoch * dataloader.size + i
if batches_done % 25 == 0
TorchVision::Utils.save_image(gen_imgs.data[0...25], "images/%d.png" % batches_done, nrow: 5, normalize: true)
end
end
end