-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathGAN_Compare
92 lines (81 loc) · 2.68 KB
/
GAN_Compare
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
# GAN
begin <- Sys.time()
#setwd("~/Desktop")
library(keras)
# MNIST data
mnist <- dataset_mnist()
c(c(trainx, trainy), c(testx, testy)) %<-% mnist
trainx <- trainx[trainy==5,,]
trainx <- array_reshape(trainx, c(nrow(trainx), 28, 28, 1))
trainx <- trainx / 255
# Generator network
h <- 28; w <- 28; c <- 1; l <- 28
gi <- layer_input(shape = l)
go <- gi %>% layer_dense(units = 32 * 14 * 14) %>%
layer_activation_leaky_relu() %>%
layer_reshape(target_shape = c(14, 14, 32)) %>%
layer_conv_2d(filters = 32,
kernel_size = 5,
padding = "same") %>%
layer_activation_leaky_relu() %>%
layer_conv_2d_transpose(filters = 32,
kernel_size = 4,
strides = 2,
padding = "same") %>%
layer_activation_leaky_relu() %>%
layer_conv_2d(filters = 1,
kernel_size = 5,
activation = "tanh",
padding = "same")
g <- keras_model(gi, go)
# Discriminator
di <- layer_input(shape = c(h, w, c))
do <- di %>%
layer_conv_2d(filters = 64, kernel_size = 4) %>%
layer_activation_leaky_relu() %>%
layer_flatten() %>%
layer_dropout(rate = 0.3) %>%
layer_dense(units = 1, activation = "sigmoid")
d <- keras_model(di, do)
d %>% compile(optimizer = 'rmsprop',
loss = "binary_crossentropy")
# Adversarial network
freeze_weights(d)
gani <- layer_input(shape = l)
gano <- gani %>% g %>% d
gan <- keras_model(gani, gano)
gan %>% compile(optimizer = 'rmsprop',
loss = "binary_crossentropy")
# Train
b <- 50
dir <- "images"
dir.create(dir)
# Start
start <- 1; dloss <- NULL; gloss <- NULL
for (i in 1:500) {noise <- matrix(rnorm(b*l), nrow = b, ncol= l)
fake <- g %>% predict(noise)
stop <- start + b - 1
real <- trainx[start:stop,,,]
real <- array_reshape(real, c(nrow(real), 28, 28, 1))
rows <- nrow(real)
both <- array(0, dim = c(rows * 2, dim(real)[-1]))
both[1:rows,,,] <- fake
both[(rows+1):(rows*2),,,] <- real
labels <- rbind(matrix(runif(b, 0.9,1), nrow = b, ncol = 1),
matrix(runif(b, 0, 0.1), nrow = b, ncol = 1))
dloss[i] <- d %>% train_on_batch(both, labels)
fakeAsReal <- array(runif(b, 0, 0.1), dim = c(b, 1))
gloss[i] <- gan %>% train_on_batch(noise, fakeAsReal)
start <- start + b
if (start > (nrow(trainx) - b)) start <- 1
cat("Discriminator Loss:", dloss[i], "\n")
cat("Gan Loss:", gloss[i], "\n")
# Saves
f <- fake[1,,,]
dim(f) <- c(28,28,1)
image_array_save(f, path = file.path(dir, paste0("f", i+100, ".png")))}
#Time
end <- Sys.time()
begin
end
end - begin