-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_cifar.py
121 lines (103 loc) · 4.33 KB
/
main_cifar.py
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
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 26 10:24:13 2018
@author: tgill
"""
import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import mnist, cifar10
from keras.optimizers import Adam
from keras.utils.np_utils import to_categorical
import cv2
from tqdm import tqdm
from networks import get_models, adversarial, bin_accuracy
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
X_train = X_train / 255
X_test = X_test / 255
names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
y_train = to_categorical(y_train, num_classes=10)
y_test = to_categorical(y_test, num_classes=10)
#X_train = np.expand_dims(X_train, axis=-1)
#X_test = np.expand_dims(X_test, axis=-1)
n_epochs = 100
epoch_size = 500
batch_size = 128
target_size = (64, 64)
x_m = []
for i in range(len(X_train)):
x = cv2.resize(X_train[i], target_size)
x = x.reshape(target_size[0], target_size[1], 3)
x_m.append(x)
X_train = np.asarray(x_m)
noise_dim=100
gen, disc = get_models(64, 64, 256, 512, noise_dim, 10, n_channels=3)
opt = Adam(0.0002, 0.5)
disc.compile(loss=['binary_crossentropy', 'categorical_crossentropy'], optimizer=opt, metrics=['accuracy'])
adv = adversarial(gen, disc, opt)
for i in range(n_epochs):
print("Epoch ", i)
loss_disc=[]
loss_gen=[]
for j in tqdm(range(epoch_size)):
#print("Iteration ", j)
idxs_batch = np.random.randint(0, len(X_train), batch_size)
X_true = X_train[idxs_batch]
label_true = y_train[idxs_batch]
noise = np.random.uniform(-1.0, 1.0, size=[batch_size, noise_dim])
#noise = np.random.randn(batch_size, noise_dim)
sampled_labels = np.random.randint(0, 10, (batch_size, 1))
sampled_labels = to_categorical(sampled_labels, num_classes=10)
X_fake = gen.predict([noise, sampled_labels], batch_size=batch_size)
# label_fake = np.zeros_like(label_true)
# label_fake[:,10]=1
X_batch = np.concatenate([X_true, X_fake])
y_batch = np.ones([2*batch_size, 1])
y_batch[batch_size:] = 0
labels_batch = np.concatenate([label_true, sampled_labels])
y_true = np.ones([batch_size, 1])
y_fake = np.zeros([batch_size, 1])+0.1
# shuffle = np.random.permutation(2*batch_size)
# X_batch = X_batch[shuffle]
# y_batch = y_batch[shuffle]
# disc.trainable = True
# disc.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
disc_loss1 = disc.train_on_batch(X_true, [y_true, label_true])
disc_loss2 = disc.train_on_batch(X_fake, [y_fake, sampled_labels])
loss_disc.append(np.add(disc_loss1,disc_loss2)/2)
# disc_loss = disc.train_on_batch(X_batch, [y_batch, labels_batch])
# loss_disc.append(disc_loss)
#print(disc_loss)
y_noise = np.ones([batch_size, 1])
X_noise = np.random.uniform(-1.0, 1.0, size=[batch_size, 100])
#X_noise = np.random.randn(batch_size, noise_dim)
# label_fake = np.zeros_like(label_true)
# label_fake[:,:10]=sampled_labels
gen_loss = adv.train_on_batch([X_noise, sampled_labels], [y_noise, sampled_labels])
loss_gen.append(gen_loss)
#print(gen_loss)
print("Disc", np.mean(loss_disc, axis=0))
print("Gen", np.mean(loss_gen, axis=0))
#plt.imshow(X_fake[1][:,:,0])
#plt.title(sampled_labels[1])
if i%10==0:
examples=9
noise = np.random.uniform(-1.0, 1.0, size=[examples, noise_dim])
labels = np.arange(9)
# l = labels.reshape(-1, 1)
# l = np.tile(l, 10)
# l = l.reshape(100)
# labels = np.tile(labels, 10)
labels = to_categorical(labels, num_classes=10)
# l = to_categorical(l)
# labels = labels
images = gen.predict([noise, labels], batch_size=batch_size)
images = images.reshape(-1, target_size[0],target_size[1], 3)
plt.figure(figsize=(10, 10))
for i in range(images.shape[0]):
ax=plt.subplot(3, 3, i+1)
ax.set_title(names[i], fontsize=10)
plt.imshow(images[i], interpolation='nearest', cmap='gray_r')
plt.axis('off')
plt.tight_layout()
plt.show()
plt.pause(0.05)