def display_examples(n_images): """ Display an example of MNIST & CelebA :param n_images: number of images to display """ show_n_images = n_images get_ipython().magic('matplotlib inline') mnist_images = helper.get_batch(glob(os.path.join(DATA_DIR, 'mnist/*.jpg'))[:show_n_images], 28, 28, 'L') plt.imshow(helper.images_square_grid(mnist_images, 'L'), cmap='gray') plt.show() mnist_images = helper.get_batch(glob(os.path.join(DATA_DIR, 'img_align_celeba/*.jpg'))[:show_n_images], 28, 28, 'RGB') plt.imshow(helper.images_square_grid(mnist_images, 'RGB')) plt.show()
def train(self, vectorized_songs): for iter in tqdm(range(self.num_training_iterations)): x_batch, y_batch = helper.get_batch(vectorized_songs, self.seq_length, self.batch_size) loss = self.train_step(x_batch, y_batch) self.history.append(loss.numpy().mean()) if iter % 100 == 0: self.model.save_weights(self.checkpoint_prefix) self.model.save_weights(self.checkpoint_prefix)
def load_image(): data_dir = './data' # helper.download_extract(data_dir) show_n_images = 10000 image_size = 56 plt.figure(figsize=(10, 10)) images = helper.get_batch( glob(os.path.join(data_dir, 'img_align_celeba/*.jpg'))[:show_n_images], image_size, image_size, 'RGB') return images
def _run_train(sess, model, x_train, y_train, x_eval, y_eval, train_config): for epoch in range(train_config.EPOCHS): # train for batch_x, batch_y in helper.get_batch(x_train, y_train, step=train_config.BATCH_SIZE): model.train(sess, batch_x, batch_y, dropout_keep_prob=train_config.DROPOUT_KEEP_PROB) # eval loss = model.evaluate_loss(sess, x_eval, y_eval, dropout_keep_prob=1.0) _print_eval_results(loss, epoch)
def _run_train_with_summary(sess, model, x_train, y_train, x_eval, y_eval, train_config): saver = tf.train.Saver() helper.mkdir(train_config.SUMMARY_DIR) summary_writer = tf.summary.FileWriter(logdir=train_config.SUMMARY_DIR) summary_writer.add_graph(sess.graph) for epoch in range(train_config.EPOCHS): # train for batch_x, batch_y in helper.get_batch(x_train, y_train, step=train_config.BATCH_SIZE): summary, _ = model.train( sess, batch_x, batch_y, dropout_keep_prob=train_config.DROPOUT_KEEP_PROB) summary_writer.add_summary(summary) # eval summary, loss = model.evaluate_loss(sess, batch_x, batch_y, dropout_keep_prob=1.0) summary_writer.add_summary(summary) _print_eval_results(loss, epoch)
def run(): train_map, dev_map, test_map = get_datamap(composers=composers, split=True) train_size = get_size(train_map) dev_size = get_size(dev_map) print(train_size, dev_size) # Create Train and Dev generators train_gen = randomized_generator(train_map, batch_size=batch_size, timesteps=timesteps, n_classes=n_classes) dev_gen = randomized_generator(dev_map, batch_size=batch_size, timesteps=timesteps, n_classes=n_classes) train_iterations = int(train_size/(10*batch_size)) dev_iterations = int(dev_size/(10*batch_size)) print(train_iterations, dev_iterations) # Train Model composer_clf = Composer_Classifier(n_classes=n_classes) composer_clf.create_model() composer_clf.train(train_generator=train_gen, dev_generator=dev_gen, steps_per_epoch=train_iterations, validation_steps=dev_iterations, epochs=n_epochs) # Test x_test, y_test = get_batch(test_map, batch_size=test_batch_size, timesteps=100) composer_clf.test(x_test, y_test)
# ### MNIST # [MNIST](http://yann.lecun.com/exdb/mnist/) 是一个手写数字的图像数据集。你可以更改 `show_n_images` 探索此数据集。 # In[90]: show_n_images = 25 """ DON'T MODIFY ANYTHING IN THIS CELL """ get_ipython().magic(u'matplotlib inline') import os from glob import glob from matplotlib import pyplot mnist_images = helper.get_batch(glob(os.path.join(data_dir, 'mnist/*.jpg'))[:show_n_images], 28, 28, 'L') pyplot.imshow(helper.images_square_grid(mnist_images, 'L'), cmap='gray') # ### CelebA # [CelebFaces Attributes Dataset (CelebA)](http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html) 是一个包含 20 多万张名人图片及相关图片说明的数据集。你将用此数据集生成人脸,不会用不到相关说明。你可以更改 `show_n_images` 探索此数据集。 # In[91]: show_n_images = 25 """ DON'T MODIFY ANYTHING IN THIS CELL """ mnist_images = helper.get_batch(glob(os.path.join(data_dir, 'img_align_celeba/*.jpg'))[:show_n_images], 28, 28, 'RGB') pyplot.imshow(helper.images_square_grid(mnist_images, 'RGB'))
import helper import os from glob import glob import numpy as np from matplotlib import pyplot from distutils.version import LooseVersion import warnings import tensorflow as tf import problem_unittests as tests show_n_images = 40 celeb_images = helper.get_batch( glob(os.path.join('img_align_celeba/*.jpg'))[:show_n_images], 50, 50, 'RGB') pyplot.imshow(helper.images_square_grid(celeb_images, 'RGB')) # pyplot.show() # Check TensorFlow Version assert LooseVersion(tf.__version__) >= LooseVersion( '1.0' ), 'Please use TensorFlow version 1.0 or newer. You are using {}'.format( tf.__version__) print('TensorFlow Version: {}'.format(tf.__version__)) if not tf.test.gpu_device_name(): warnings.warn( 'No GPU found. Please use a GPU to train your neural network.') else: print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
# In[47]: def output_fig(images_array, file_name): plt.figure(figsize=(6, 6), dpi=100) plt.imshow(helper.images_square_grid(images_array)) plt.axis("off") plt.savefig(file_name+'.png', bbox_inches='tight', pad_inches=0) # In[50]: for i in range(500): width = height = 64 generated_images = helper.get_batch(glob('fake_images/*.png')[0+(i*9):9+(i*9)], 64, 64, 'RGB') output_fig(generated_images, file_name="./fake_images_9x9/img%03d.png"%i) del generated_images # In[48]: output_fig(generated_images) # In[ ]: "./fake_images_9x9/img%03d.png"%
train_loss_hist = [] val_loss_hist = [] lr_hist = [] with tf.device("/gpu:0"): with tf.Session() as sess: train_writer = tf.summary.FileWriter( '../../outputs/summary/train_2' + cost_type, sess.graph) val_writer = tf.summary.FileWriter('../../outputs/summary/test_2' + cost_type) sess.run(tf.global_variables_initializer()) init_fn(sess) if data_bias == "balancing": iterations = iterations_number for step in range(iterations): batch = hp.get_batch(cluster_mini_batches, mini_size, clusters) images, labels = hp.load_images_labels2(batch, train, path) sess.run( optimiser, { md.input: images, md.target: labels, md.kp: kp, md.lr: learning_rate }) if step % print_step == 0: train_loss, train_summary = sess.run( [md.loss, merged], { md.input: images, md.target: labels, md.kp: 1.0
dacr_sum = np.zeros(2) dacf_sum = np.zeros(2) gac_sum = np.zeros(2) for epch in range(maxepch): print('Iteration No.', epch) for bcnt in range(0, dir.shape[0], batch_size): #random.shuffle(dir) n_critic = 1 for ct in range(n_critic): # Label smoothing #label_r = ones + (np.random.sample([batch_size,1]) * 0.01) #label_f = zeros + (np.random.sample([batch_size, 1]) * 0.01) select = np.random.randint(0, dir.shape[0], batch_size) real_image = (hp.get_batch(dir[select], img_size, img_size, 'RGB') - 127.5) / 127.5 nd_noise = np.random.normal(0, 1, (batch_size, noise_size)) # nd_noise = np.random.uniform(-1, 1, (batch_size, noise_size)) fake_img = G.predict(nd_noise) dacr_sum += D.train_on_batch(real_image, mones) dacf_sum += D.train_on_batch(fake_img, ones) # Clip discriminator weights for l in D.layers: weights = l.get_weights() weights = [np.clip(w, -0.05, 0.05) for w in weights] l.set_weights(weights) # Train generator nd_noise = np.random.normal(0,1,(batch_size, noise_size)) # nd_noise = np.random.uniform(-1, 1, (batch_size, noise_size))
image = Image.open(image_path) return np.array(image.convert(mode)) def get_batch(image_files, width, height, mode): data_batch = np.array( [get_image(sample_file, width, height, mode) for sample_file in image_files]).astype(np.float32) # Make sure the images are in 4 dimensions if len(data_batch.shape) < 4: data_batch = data_batch.reshape(data_batch.shape + (1,)) return data_batch show_n_images = 25 mnist_images = helper.get_batch(glob(os.path.join(data_resized_dir, '*.jpg'))[:show_n_images], 64, 64, 'RGB') plt.imshow(helper.images_square_grid(mnist_images, 'RGB')) # Taken from Udacity face generator project from distutils.version import LooseVersion import warnings # Check TensorFlow Version assert LooseVersion(tf.__version__) >= LooseVersion('1.0'), 'Please use TensorFlow version 1.0 or newer. You are using {}'.format(tf.__version__) print('TensorFlow Version: {}'.format(tf.__version__))
# -*- coding: utf-8 -*- """ Created on Thu Nov 7 22:39:07 2019 @author: Keke """ import os import helper import matplotlib.pyplot as plt from glob import glob data_dir = '../../cs-ioc5008-hw2/pytorch_GAN_zoo/output_datasets' file_dir = '../../cs-ioc5008-hw2/images/' def output_fig(images_array, file_name="./results"): plt.figure(figsize=(6, 6), dpi=100) plt.imshow(helper.images_square_grid(images_array)) plt.axis("off") plt.savefig(file_name + '.png', bbox_inches='tight', pad_inches=0) for i in range(5): generated_images = helper.get_batch( glob(os.path.join(data_dir, '*.png'))[i * 9:(i + 1) * 9], 32, 32, 'RGB') print(generated_images.shape) output_fig(generated_images, file_name=file_dir + "{}_image".format(str.zfill(str(i), 3)))
tests.test_discriminator(discriminator, tf) tests.test_generator(generator, tf) tests.test_model_loss(model_loss) tests.test_model_opt(model_opt, tf) # 학습 Parameters show_n_images = 25 batch_size = 64 z_dim = 100 learning_rate = 0.00025 beta1 = 0.45 epochs = 100 # 시간/장비의 제약으로 인해 11번째 epoch 까지만 진행 # celeba.zip 데이터셋 다운로드 helper.download_extract('celeba', './celeba') mnist_images = helper.get_batch(glob(os.path.join('./celeba', 'img_align_celeba/*.jpg'))[:show_n_images], 28, 28, 'RGB') pyplot.imshow(helper.images_square_grid(mnist_images, 'RGB')) # 샘플 데이터 1개 보여주기 # 다운로드 받은 celeba 이미지들을 학습시킬 수 있는 데이터셋으로 변환 celeba_dataset = helper.Dataset('celeba', glob(os.path.join('./celeba', 'img_align_celeba/*.jpg'))) # GPU 사용 여부 확인 if not tf.test.gpu_device_name(): warnings.warn('GPU가 없습니다. - GPU 사용 권장') else: print('사용중인 GPU: {}'.format(tf.test.gpu_device_name())) # 실제 학습시키고 output을 생성하는 부분 (시간이 오래 걸립니다) with tf.Graph().as_default(): train(epochs, batch_size, z_dim, learning_rate, beta1, celeba_dataset.get_batches, celeba_dataset.shape, celeba_dataset.image_mode)
def show_images(ids): show_n_images = 16 celeb_images = helper.get_batch(ids[:show_n_images], 28, 28, 'RGB') plt.imshow(helper.images_square_grid(celeb_images, 'RGB'))
""" import helper helper.download_extract('mnist', data_dir) helper.download_extract('celeba', data_dir) show_n_images = 25 """ DON'T MODIFY ANYTHING IN THIS CELL """ import os from glob import glob from matplotlib import pyplot mnist_images = helper.get_batch(glob(os.path.join(data_dir, 'mnist/*.jpg'))[:show_n_images], 28, 28, 'L') pyplot.imshow(helper.images_square_grid(mnist_images, 'L'), cmap='gray') import problem_unittests as tests """ DON'T MODIFY ANYTHING IN THIS CELL """ from distutils.version import LooseVersion import warnings import tensorflow as tf # Check TensorFlow Version assert LooseVersion(tf.__version__) >= LooseVersion( '1.0'), 'Please use TensorFlow version 1.0 or newer. You are using {}'.format(tf.__version__) print('TensorFlow Version: {}'.format(tf.__version__))
# ## 探索数据(Explore the Data) # ### MNIST # [MNIST](http://yann.lecun.com/exdb/mnist/) 是一个手写数字的图像数据集。你可以更改 `show_n_images` 探索此数据集。 # In[2]: show_n_images = 25 """ DON'T MODIFY ANYTHING IN THIS CELL """ get_ipython().run_line_magic('matplotlib', 'inline') import os from glob import glob from matplotlib import pyplot mnist_images = helper.get_batch( glob(os.path.join(data_dir, 'mnist/*.jpg'))[:show_n_images], 28, 28, 'L') pyplot.imshow(helper.images_square_grid(mnist_images, 'L'), cmap='gray') # ### CelebA # [CelebFaces Attributes Dataset (CelebA)](http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html) 是一个包含 20 多万张名人图片及相关图片说明的数据集。你将用此数据集生成人脸,不会用不到相关说明。你可以更改 `show_n_images` 探索此数据集。 # In[3]: show_n_images = 25 """ DON'T MODIFY ANYTHING IN THIS CELL """ mnist_images = helper.get_batch( glob(os.path.join(data_dir, 'img_align_celeba/*.jpg'))[:show_n_images], 28, 28, 'RGB') pyplot.imshow(helper.images_square_grid(mnist_images, 'RGB'))
# In[1]: show_n_images = 25 import helper import numpy as np """ DON'T MODIFY ANYTHING IN THIS CELL """ get_ipython().run_line_magic('matplotlib', 'inline') import os from glob import glob from matplotlib import pyplot mnist_images = helper.get_batch( glob(os.path.join('F:/uav+ai/data_for_neural_network/', 'mnist/*.jpg'))[:show_n_images], 28, 28, 'L') print(np.shape(mnist_images)) pyplot.imshow(helper.images_square_grid(mnist_images, 'L'), cmap='gray') # ### CelebA # [CelebFaces Attributes Dataset (CelebA)](http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html) 是一个包含 20 多万张名人图片及相关图片说明的数据集。你将用此数据集生成人脸,不会用不到相关说明。你可以更改 `show_n_images` 探索此数据集。 # In[2]: show_n_images = 25 """ DON'T MODIFY ANYTHING IN THIS CELL """ mnist_images = helper.get_batch( glob(