728x90
반응형
from __future__ import absolute_import, division, print_function, unicode_literals
In [306]:
import tensorflow as tf
tf.__version__
Out[306]:
In [0]:
import glob
import imageio
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
from PIL import Image
from tensorflow.keras.layers import *
import time
import cv2
import tqdm
from IPython import display
In [5]:
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
In [9]:
!mkdir -p drive
!google-drive-ocamlfuse drive
!ls drive/"Colab Notebooks"
In [0]:
path = './drive/Colab Notebooks/DCGAN/pokemon'
In [0]:
filenames = np.array(glob.glob('./drive/Colab Notebooks/DCGAN/pokemon/*.jpg'))
In [424]:
pokemon = list()
for i in tqdm.tqdm_notebook(range(len(filenames))):
img = cv2.imread(filenames[i],cv2.IMREAD_COLOR)
img = cv2.resize(img,(64,64))
pokemon.append(img)
In [425]:
plt.imshow(pokemon[2])
print(pokemon[2].shape)
In [0]:
def preprocess(x):
return (x/255)*2-1
def deprocess(x):
return np.uint8((x+1)/2*255)
In [0]:
from tensorflow.keras.models import *
from tensorflow.keras.layers import *
In [0]:
def get_generator():
gen_model = Sequential()
gen_model.add(Dense(input_dim=100, units=2048))
gen_model.add(LeakyReLU(alpha=0.2))
gen_model.add(Dense(256 * 8 * 8))
gen_model.add(BatchNormalization())
gen_model.add(LeakyReLU(alpha=0.2))
gen_model.add(Reshape((8, 8, 256), input_shape=(256 * 8 * 8,)))
gen_model.add(UpSampling2D(size=(2, 2)))
gen_model.add(Conv2D(128, (5, 5), padding='same'))
gen_model.add(LeakyReLU(alpha=0.2))
gen_model.add(UpSampling2D(size=(2, 2)))
gen_model.add(Conv2D(64, (5, 5), padding='same'))
gen_model.add(LeakyReLU(alpha=0.2))
gen_model.add(UpSampling2D(size=(2, 2)))
gen_model.add(Conv2D(3, (5, 5), padding='same'))
gen_model.add(Activation('tanh'))
return gen_model
In [0]:
def get_discriminator():
dis_model = Sequential()
dis_model.add(Conv2D(128, (5, 5), padding='same', input_shape=(64, 64, 3)))
dis_model.add(LeakyReLU(alpha=0.2))
dis_model.add(MaxPooling2D(pool_size=(2, 2)))
dis_model.add(Conv2D(256, (3, 3)))
dis_model.add(LeakyReLU(alpha=0.2))
dis_model.add(MaxPooling2D(pool_size=(2, 2)))
dis_model.add(Conv2D(512, (3, 3)))
dis_model.add(LeakyReLU(alpha=0.2))
dis_model.add(MaxPooling2D(pool_size=(2, 2)))
dis_model.add(Flatten())
dis_model.add(Dense(1024))
dis_model.add(LeakyReLU(alpha=0.2))
dis_model.add(Dense(1))
dis_model.add(Activation('sigmoid'))
return dis_model
In [0]:
batch_size = 128
z_shape = 100
epochs = 1000
dis_learning_rate = 0.0002
gen_learning_rate = 0.0002
dis_momentum = 0.5
gen_momentum = 0.5
dis_nesterov = True
gen_nesterov = True
In [0]:
X = np.array(pokemon)
X = (X-127.5) / 127.5
In [0]:
# Define optimizers
from tensorflow.keras.optimizers import *
dis_optimizer = Adam(lr=dis_learning_rate, beta_1=dis_momentum)
gen_optimizer = Adam(lr=gen_learning_rate, beta_1=gen_momentum)
# dis_optimizer = SGD(lr=dis_learning_rate, momentum=dis_momentum, nesterov=dis_nesterov)
# gen_optimizer = SGD(lr=gen_learning_rate, momentum=gen_momentum, nesterov=gen_nesterov)
In [0]:
gen_model = get_generator()
gen_model.compile(loss='binary_crossentropy', optimizer=gen_optimizer)
In [0]:
dis_model = get_discriminator()
dis_model.compile(loss='binary_crossentropy', optimizer=dis_optimizer)
In [0]:
adversarial_model = Sequential()
adversarial_model.add(gen_model)
dis_model.trainable = False
adversarial_model.add(dis_model)
In [0]:
adversarial_model.compile(loss='binary_crossentropy', optimizer=gen_optimizer)
In [0]:
from tensorflow.keras.callbacks import TensorBoard
tensorboard = TensorBoard(log_dir="logs/{}".format(time.time()), write_images=True, write_grads=True, write_graph=True)
tensorboard.set_model(gen_model)
tensorboard.set_model(dis_model)
In [0]:
def save_rgb_img(img,path):
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.imshow(img)
ax.axis('off')
ax.set_title("Image")
plt.savefig(path)
plt.close()
In [439]:
device_name = tf.test.gpu_device_name()
device_name
with tf.device('/GPU:0'):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# log_device_placement을 True로 설정하여 세션을 만듭니다.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# op를 실행합니다.
print(sess.run(c))
In [0]:
for epoch in range(epochs):
print("Epoch is", epoch)
number_of_batches = int(X.shape[0] / batch_size)
print("Number of batches", number_of_batches)
for index in range(number_of_batches):
z_noise = np.random.normal(0, 1, size=(batch_size, z_shape))
image_batch = X[index * batch_size:(index + 1) * batch_size]
generated_images = gen_model.predict_on_batch(z_noise)
y_real = np.ones(batch_size) - np.random.random_sample(batch_size) * 0.2
y_fake = np.random.random_sample(batch_size) * 0.2
dis_loss_real = dis_model.train_on_batch(image_batch, y_real)
dis_loss_fake = dis_model.train_on_batch(generated_images, y_fake)
d_loss = (dis_loss_real+dis_loss_fake)/2
print("d_loss:", d_loss)
z_noise = np.random.normal(0, 1, size=(batch_size, z_shape))
g_loss = adversarial_model.train_on_batch(z_noise, [1] * batch_size)
print("g_loss:", g_loss)
if epoch % 10 == 0:
z_noise = np.random.normal(0, 1, size=(batch_size, z_shape))
gen_images1 = gen_model.predict_on_batch(z_noise)
for img in gen_images1[:2]: # [:2]
save_rgb_img(img, "./drive/Colab Notebooks/DCGAN/results/one_{}.png".format(epoch))
gen_model.save("./drive/Colab Notebooks/DCGAN/results/gen_model.h5")
dis_model.save("./drive/Colab Notebooks/DCGAN/results/dis_model.h5")
In [0]:
path2 = './drive/Colab Notebooks/DCGAN/results'
In [0]:
img_test = cv2.imread(os.path.join(path2,'one_400.png'))
In [16]:
plt.imshow(img_test)
Out[16]:
In [22]:
glob.glob(path2+'/'+'one_*.png')
Out[22]:
In [0]:
# Create GIF
anim_file = 'pokemon_dcgan.gif'
with imageio.get_writer(anim_file,mode='I') as writer:
filenames = glob.glob(path2+'/'+'one_*.png')
filenames = sorted(filenames)
last = -1
for i,filename in enumerate(filenames):
frame = 2*(i**0.5)
if round(frame) > round(last):
last = frame
else:
continue
image = imageio.imread(filename)
writer.append_data(image)
image = imageio.imread(filename)
writer.append_data(image)
import IPython
if IPython.version_info > (6,2,0,''):
displya.Image(filename=anim_file)
In [0]:
try:
from google.colab import files
except ImportError:
pass
else:
files.download(anim_file)
Epoch 650이후 부터 흰색 이미지를 내뱉어서 모델 내부를 다시 수정해야할 것 같다.
중간 중간 결과물들을 보면 언뜻 포켓몬 처럼 생긴 캐릭터가 보이기도 한다.
728x90
반응형