From 35e1fc16e6d3380649e0853ea9edaeb45de7824a Mon Sep 17 00:00:00 2001 From: ldy <498435408@qq.com> Date: Mon, 30 Oct 2017 21:17:39 +0800 Subject: [PATCH] first upload --- .gitignore | 104 +++++++++++++++++++++ checkpoint/.gitignore | 2 + data/.gitignore | 2 + extract_feature.py | 178 ++++++++++++++++++++++++++++++++++++ network.py | 126 +++++++++++++++++++++++++ samples/.gitignore | 2 + train_marta_gan.py | 208 ++++++++++++++++++++++++++++++++++++++++++ train_svm.py | 40 ++++++++ utils.py | 53 +++++++++++ 9 files changed, 715 insertions(+) create mode 100644 .gitignore create mode 100644 checkpoint/.gitignore create mode 100644 data/.gitignore create mode 100644 extract_feature.py create mode 100644 network.py create mode 100644 samples/.gitignore create mode 100755 train_marta_gan.py create mode 100644 train_svm.py create mode 100644 utils.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af2f537 --- /dev/null +++ b/.gitignore @@ -0,0 +1,104 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +.static_storage/ +.media/ +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ diff --git a/checkpoint/.gitignore b/checkpoint/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/checkpoint/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/data/.gitignore b/data/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/data/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/extract_feature.py b/extract_feature.py new file mode 100644 index 0000000..3893ad4 --- /dev/null +++ b/extract_feature.py @@ -0,0 +1,178 @@ +import os +import sys +import scipy.misc +import pprint +import numpy as np +import time +import tensorflow as tf +import tensorlayer as tl +from glob import glob +from random import shuffle +from tensorlayer.layers import * +from utils import * +from network import * + +pp = pprint.PrettyPrinter() + +""" +TensorLayer implementation of DCGAN to generate face image. + +Usage : see README.md +""" + +flags = tf.app.flags +flags.DEFINE_integer("epoch", 100, "Epoch to train [25]") +flags.DEFINE_float("learning_rate", 0.0002, "Learning rate of for adam [0.0002]") +flags.DEFINE_float("beta1", 0.5, "Momentum term of adam [0.5]") +flags.DEFINE_integer("train_size", np.inf, "The size of train images [np.inf]") +flags.DEFINE_integer("batch_size", 1, "The number of batch images [64]") +flags.DEFINE_integer("image_size", 256, "The size of image to use (will be center cropped) [108]") +flags.DEFINE_integer("output_size", 256, "The size of the output images to produce [64]") +flags.DEFINE_integer("sample_size", 64, "The number of sample images [64]") +flags.DEFINE_integer("c_dim", 3, "Dimension of image color. [3]") +flags.DEFINE_integer("sample_step", 500, "The interval of generating sample. [500]") +flags.DEFINE_integer("save_step", 50, "The interval of saveing checkpoints. [500]") +flags.DEFINE_string("dataset", "uc_train_256_data", "The name of dataset [celebA, mnist, lsun]") +flags.DEFINE_string("checkpoint_dir", "checkpoint", "Directory name to save the checkpoints [checkpoint]") +flags.DEFINE_string("sample_dir", "samples", "Directory name to save the image samples [samples]") +flags.DEFINE_string("feature_dir", "features", "Directory name to save features") +flags.DEFINE_boolean("is_train", False, "True for training, False for testing [False]") +flags.DEFINE_boolean("is_crop", False, "True for training, False for testing [False]") +flags.DEFINE_boolean("visualize", False, "True for visualizing, False for nothing [False]") +FLAGS = flags.FLAGS + + + +def main(_): + pp.pprint(flags.FLAGS.__flags) + + if not os.path.exists(FLAGS.checkpoint_dir): + os.makedirs(FLAGS.checkpoint_dir) + if not os.path.exists(FLAGS.sample_dir): + os.makedirs(FLAGS.sample_dir) + + + # with tf.device("/gpu:0"): # <-- if you have a GPU machine + real_images = tf.placeholder(tf.float32, [FLAGS.batch_size, FLAGS.output_size, FLAGS.output_size, FLAGS.c_dim], name='real_images') + + # z --> generator for training + + + net_d, d_logits, features = discriminator_simplified_api(real_images, is_train=FLAGS.is_train, reuse=False) + + + sess=tf.Session() + tl.ops.set_gpu_fraction(sess=sess, gpu_fraction=0.88) + sess.run(tf.initialize_all_variables()) + + # load checkpoints + print("[*] Loading checkpoints...") + model_dir = "%s_%s_%s" % (FLAGS.dataset, 64, FLAGS.output_size) + save_dir = os.path.join(FLAGS.checkpoint_dir, model_dir) + #print save_dir + # load the latest checkpoints + nums = [75] + for num in nums: + net_g_name = os.path.join(save_dir, '%dnet_g.npz'%num) + net_d_name = os.path.join(save_dir, '%dnet_d.npz'%num) + + print net_g_name, net_d_name + + if not (os.path.exists(net_g_name) and os.path.exists(net_d_name)): + print("[!] Loading checkpoints failed!") + else: + net_d_loaded_params = tl.files.load_npz(name=net_d_name) + tl.files.assign_params(sess, net_d_loaded_params, net_d) + print("[*] Loading checkpoints SUCCESS!") + + NUM_STYLE_LABELS = 21 + style_label_file = './style_names.txt' + style_labels = list(np.loadtxt(style_label_file, str, delimiter='\n')) + if NUM_STYLE_LABELS > 0: + style_labels = style_labels[:NUM_STYLE_LABELS] + + + if not os.path.exists(FLAGS.feature_dir): + os.makedirs(FLAGS.feature_dir) + + print 'extract traning feature' + + data_files = glob(os.path.join("./data", 'uc_train_256_feat', "*.jpg")) + shuffle(data_files) + + + batch_idxs = min(len(data_files), FLAGS.train_size) // FLAGS.batch_size + + lens = batch_idxs*FLAGS.batch_size + + + y = np.zeros(lens, dtype=np.uint8) + for i in xrange(lens): + for j in xrange(len(style_labels)): + if style_labels[j] in data_files[i]: + y[i] = j + break + + feats = np.zeros((lens, 14336)) + + for idx in xrange(batch_idxs): + batch_files = data_files[idx*FLAGS.batch_size:(idx+1)*FLAGS.batch_size] + # get real images + batch = [get_image(batch_file, FLAGS.image_size, is_crop=FLAGS.is_crop, resize_w=FLAGS.output_size, is_grayscale = 0) for batch_file in batch_files] + batch_images = np.array(batch).astype(np.float32) + # update sample files based on shuffled data + #img, errG = sess.run([net_g2.outputs, g_loss], feed_dict={z : sample_seed}) + feat = sess.run(features, feed_dict={real_images: batch_images}) + + #print feat.shape + + begin = FLAGS.batch_size*idx + end = FLAGS.batch_size + begin + feats[begin:end, ...] = feat + + np.save('features/features%d_train.npy'%num, feats) + np.save('features/label%d_train.npy'%num, y) + + print 'extract testing feature' + data_files = glob(os.path.join("./data", 'uc_test_256', "*.jpg")) + shuffle(data_files) + #data_files = data_files[0:5000] + + + batch_idxs = min(len(data_files), FLAGS.train_size) // FLAGS.batch_size + + lens = batch_idxs*FLAGS.batch_size + + + y = np.zeros(lens, dtype=np.uint8) + for i in xrange(lens): + for j in xrange(len(style_labels)): + if style_labels[j] in data_files[i]: + y[i] = j + break + + feats = np.zeros((lens, 14336)) + + + for idx in xrange(batch_idxs): + batch_files = data_files[idx*FLAGS.batch_size:(idx+1)*FLAGS.batch_size] + + batch = [get_image(batch_file, FLAGS.image_size, is_crop=FLAGS.is_crop, resize_w=FLAGS.output_size, is_grayscale = 0) for batch_file in batch_files] + batch_images = np.array(batch).astype(np.float32) + # update sample files based on shuffled data + #img, errG = sess.run([net_g2.outputs, g_loss], feed_dict={z : sample_seed}) + feat = sess.run(features, feed_dict={real_images: batch_images}) + + begin = FLAGS.batch_size*idx + end = FLAGS.batch_size + begin + feats[begin:end, ...] = feat + + #print idx + + np.save('features/features%d_test.npy'%num, feats) + np.save('features/label%d_test.npy'%num, y) + + + +if __name__ == '__main__': + tf.app.run() diff --git a/network.py b/network.py new file mode 100644 index 0000000..d0f1719 --- /dev/null +++ b/network.py @@ -0,0 +1,126 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- +""" +Created on Mon Oct 30 20:52:00 2017 + +@author: ldy +""" + + +import numpy as np + +import tensorflow as tf +import tensorlayer as tl +from tensorlayer.layers import * + +from utils import * + + + +def generator_simplified_api(inputs, is_train=True, reuse=False): + image_size = 256 + k = 5 + # 128, 64, 32, 16 + s2, s4, s8, s16, s32, s64 = int(image_size/2), int(image_size/4), int(image_size/8), int(image_size/16), int(image_size/32), int(image_size/64) + + batch_size = 64 + gf_dim = 16 # Dimension of gen filters in first conv layer. [64] + + w_init = tf.random_normal_initializer(stddev=0.02) + gamma_init = tf.random_normal_initializer(1., 0.02) + + with tf.variable_scope("generator", reuse=reuse): + tl.layers.set_name_reuse(reuse) + + net_in = InputLayer(inputs, name='g/in') + net_h0 = DenseLayer(net_in, n_units=gf_dim*32*s64*s64, W_init=w_init, + act = tf.identity, name='g/h0/lin') + net_h0 = ReshapeLayer(net_h0, shape=[-1, s64, s64, gf_dim*32], name='g/h0/reshape') + net_h0 = BatchNormLayer(net_h0, act=tf.nn.relu, is_train=is_train, + gamma_init=gamma_init, name='g/h0/batch_norm') + + net_h1 = DeConv2d(net_h0, gf_dim*16, (k, k), out_size=(s32, s32), strides=(2, 2), + padding='SAME', batch_size=batch_size, act=None, W_init=w_init, name='g/h1/decon2d') + net_h1 = BatchNormLayer(net_h1, act=tf.nn.relu, is_train=is_train, + gamma_init=gamma_init, name='g/h1/batch_norm') + + net_h2 = DeConv2d(net_h1, gf_dim*8, (k, k), out_size=(s16, s16), strides=(2, 2), + padding='SAME', batch_size=batch_size, act=None, W_init=w_init, name='g/h2/decon2d') + net_h2 = BatchNormLayer(net_h2, act=tf.nn.relu, is_train=is_train, + gamma_init=gamma_init, name='g/h2/batch_norm') + + net_h3 = DeConv2d(net_h2, gf_dim*4, (k, k), out_size=(s8, s8), strides=(2, 2), + padding='SAME', batch_size=batch_size, act=None, W_init=w_init, name='g/h3/decon2d') + net_h3 = BatchNormLayer(net_h3, act=tf.nn.relu, is_train=is_train, + gamma_init=gamma_init, name='g/h3/batch_norm') + + net_h4 = DeConv2d(net_h3, gf_dim*2, (k, k), out_size=(s4, s4), strides=(2, 2), + padding='SAME', batch_size=batch_size, act=None, W_init=w_init, name='g/h4/decon2d') + net_h4 = BatchNormLayer(net_h4, act=tf.nn.relu, is_train=is_train, + gamma_init=gamma_init, name='g/h4/batch_norm') + + net_h5 = DeConv2d(net_h4, gf_dim*1, (k, k), out_size=(s2, s2), strides=(2, 2), + padding='SAME', batch_size=batch_size, act=None, W_init=w_init, name='g/h5/decon2d') + net_h5 = BatchNormLayer(net_h5, act=tf.nn.relu, is_train=is_train, + gamma_init=gamma_init, name='g/h5/batch_norm') + + net_h6 = DeConv2d(net_h5, 3, (k, k), out_size=(image_size, image_size), strides=(2, 2), + padding='SAME', batch_size=batch_size, act=None, W_init=w_init, name='g/h6/decon2d') + logits = net_h6.outputs + net_h6.outputs = tf.nn.tanh(net_h6.outputs) + return net_h6, logits + +def sigmoid(x): + return 1 / (1 + np.exp(-x)) + +def discriminator_simplified_api(inputs, is_train=True, reuse=False): + k = 5 + df_dim = 16 # Dimension of discrim filters in first conv layer. [64] + w_init = tf.random_normal_initializer(stddev=0.02) + gamma_init = tf.random_normal_initializer(1., 0.02) + + with tf.variable_scope("discriminator", reuse=reuse): + tl.layers.set_name_reuse(reuse) + + net_in = InputLayer(inputs, name='d/in') + net_h0 = Conv2d(net_in, df_dim, (k, k), (2, 2), act=lambda x: tl.act.lrelu(x, 0.2), + padding='SAME', W_init=w_init, name='d/h0/conv2d') + + net_h1 = Conv2d(net_h0, df_dim*2, (k, k), (2, 2), act=None, + padding='SAME', W_init=w_init, name='d/h1/conv2d') + net_h1 = BatchNormLayer(net_h1, act=lambda x: tl.act.lrelu(x, 0.2), + is_train=is_train, gamma_init=gamma_init, name='d/h1/batch_norm') + + net_h2 = Conv2d(net_h1, df_dim*4, (k, k), (2, 2), act=None, + padding='SAME', W_init=w_init, name='d/h2/conv2d') + net_h2 = BatchNormLayer(net_h2, act=lambda x: tl.act.lrelu(x, 0.2), + is_train=is_train, gamma_init=gamma_init, name='d/h2/batch_norm') + + net_h3 = Conv2d(net_h2, df_dim*8, (k, k), (2, 2), act=None, + padding='SAME', W_init=w_init, name='d/h3/conv2d') + net_h3 = BatchNormLayer(net_h3, act=lambda x: tl.act.lrelu(x, 0.2), + is_train=is_train, gamma_init=gamma_init, name='d/h3/batch_norm') + + global_max1 = MaxPool2d(net_h3, filter_size=(4, 4), strides=None, padding='SAME', name='maxpool1') + global_max1 = FlattenLayer(global_max1, name='d/h3/flatten') + + net_h4 = Conv2d(net_h3, df_dim*16, (k, k), (2, 2), act=None, + padding='SAME', W_init=w_init, name='d/h4/conv2d') + net_h4 = BatchNormLayer(net_h4, act=lambda x: tl.act.lrelu(x, 0.2), + is_train=is_train, gamma_init=gamma_init, name='d/h4/batch_norm') + + global_max2 = MaxPool2d(net_h4, filter_size=(2, 2), strides=None, padding='SAME', name='maxpool2') + global_max2 = FlattenLayer(global_max2, name='d/h4/flatten') + + net_h5 = Conv2d(net_h4, df_dim*32, (k, k), (2, 2), act=None, + padding='SAME', W_init=w_init, name='d/h5/conv2d') + net_h5 = BatchNormLayer(net_h5, act=lambda x: tl.act.lrelu(x, 0.2), + is_train=is_train, gamma_init=gamma_init, name='d/h5/batch_norm') + global_max3 = FlattenLayer(net_h5, name='d/h5/flatten') + + feature = ConcatLayer(layer = [global_max1, global_max2, global_max3], name ='d/concat_layer1') + net_h6 = DenseLayer(feature, n_units=1, act=tf.identity, + W_init = w_init, name='d/h6/lin_sigmoid') + logits = net_h6.outputs + net_h6.outputs = tf.nn.sigmoid(net_h6.outputs) + return net_h6, logits, feature.outputs \ No newline at end of file diff --git a/samples/.gitignore b/samples/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/samples/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/train_marta_gan.py b/train_marta_gan.py new file mode 100755 index 0000000..79f6426 --- /dev/null +++ b/train_marta_gan.py @@ -0,0 +1,208 @@ +import os +import sys +import scipy.misc +import pprint +import numpy as np +import time +import tensorflow as tf +import tensorlayer as tl +from tensorlayer.layers import * +from glob import glob +from random import shuffle +from utils import * +from network import * +pp = pprint.PrettyPrinter() + +""" +TensorLayer implementation of DCGAN to generate face image. + +Usage : see README.md +""" + +flags = tf.app.flags +flags.DEFINE_integer("epoch", 100, "Epoch to train [25]") +flags.DEFINE_float("learning_rate", 0.0002, "Learning rate of for adam [0.0002]") +flags.DEFINE_float("beta1", 0.5, "Momentum term of adam [0.5]") +flags.DEFINE_integer("train_size", np.inf, "The size of train images [np.inf]") +flags.DEFINE_integer("batch_size", 64, "The number of batch images [64]") +flags.DEFINE_integer("image_size", 256, "The size of image to use (will be center cropped) [108]") +flags.DEFINE_integer("output_size", 256, "The size of the output images to produce [64]") +flags.DEFINE_integer("sample_size", 64, "The number of sample images [64]") +flags.DEFINE_integer("c_dim", 3, "Dimension of image color. [3]") +flags.DEFINE_integer("sample_step", 500, "The interval of generating sample. [500]") +flags.DEFINE_integer("save_step", 50, "The interval of saveing checkpoints. [500]") +flags.DEFINE_string("dataset", "uc_train_256_data", "The name of dataset [celebA, mnist, lsun]") +flags.DEFINE_string("checkpoint_dir", "checkpoint", "Directory name to save the checkpoints [checkpoint]") +flags.DEFINE_string("sample_dir", "samples", "Directory name to save the image samples [samples]") +flags.DEFINE_boolean("is_train", True, "True for training, False for testing [False]") +flags.DEFINE_boolean("is_crop", False, "True for training, False for testing [False]") +flags.DEFINE_boolean("visualize", False, "True for visualizing, False for nothing [False]") +FLAGS = flags.FLAGS + + + + + + +def main(_): + pp.pprint(flags.FLAGS.__flags) + + if not os.path.exists(FLAGS.checkpoint_dir): + os.makedirs(FLAGS.checkpoint_dir) + if not os.path.exists(FLAGS.sample_dir): + os.makedirs(FLAGS.sample_dir) + + z_dim = 100 + + # with tf.device("/gpu:0"): # <-- if you have a GPU machine + z = tf.placeholder(tf.float32, [FLAGS.batch_size, z_dim], name='z_noise') + real_images = tf.placeholder(tf.float32, [FLAGS.batch_size, FLAGS.output_size, FLAGS.output_size, FLAGS.c_dim], name='real_images') + + # z --> generator for training + net_g, g_logits = generator_simplified_api(z, is_train=True, reuse=False) + # generated fake images --> discriminator + net_d, d_logits, feature_fake = discriminator_simplified_api(net_g.outputs, is_train=True, reuse=False) + # real images --> discriminator + net_d2, d2_logits, feature_real = discriminator_simplified_api(real_images, is_train=True, reuse=True) + # sample_z --> generator for evaluation, set is_train to False + # so that BatchNormLayer behave differently + net_g2, g2_logits = generator_simplified_api(z, is_train=False, reuse=True) + + # + net_d3, d3_logits, _ = discriminator_simplified_api(real_images, is_train=False, reuse=True) + + # cost for updating discriminator and generator + # discriminator: real images are labelled as 1 + d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d2_logits, labels=tf.ones_like(d2_logits))) # real == 1 + # discriminator: images from generator (fake) are labelled as 0 + d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits, labels=tf.zeros_like(d_logits))) # fake == 0 + d_loss = d_loss_real + d_loss_fake + # generator: try to make the the fake images look real (1) + g_loss1 = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits, labels=tf.ones_like(d_logits))) + g_loss2 = tf.reduce_mean(tf.nn.l2_loss(feature_real-feature_fake))/(FLAGS.image_size*FLAGS.image_size) + g_loss = g_loss1+g_loss2 + #g_loss = tf.reduce_mean(tf.abs(feature_real-feature_fake)) + # trainable parameters for updating discriminator and generator + g_vars = net_g.all_params # only updates the generator + d_vars = net_d.all_params # only updates the discriminator + + net_g.print_params(False) + print("---------------") + net_d.print_params(False) + + # optimizers for updating discriminator and generator + d_optim = tf.train.AdamOptimizer(FLAGS.learning_rate, beta1=FLAGS.beta1) \ + .minimize(d_loss, var_list=d_vars) + g_optim = tf.train.AdamOptimizer(FLAGS.learning_rate, beta1=FLAGS.beta1) \ + .minimize(g_loss, var_list=g_vars) + + sess=tf.Session() + tl.ops.set_gpu_fraction(sess=sess, gpu_fraction=0.88) + sess.run(tf.initialize_all_variables()) + + # load checkpoints + print("[*] Loading checkpoints...") + model_dir = "%s_%s_%s" % (FLAGS.dataset, 64, FLAGS.output_size) + save_dir = os.path.join(FLAGS.checkpoint_dir, model_dir) + # load the latest checkpoints + #for num in xrange(70, 71): + net_g_name = os.path.join(save_dir, 'net_g.npz') + net_d_name = os.path.join(save_dir, 'net_d.npz') + + print net_g_name, net_d_name + + if not (os.path.exists(net_g_name) and os.path.exists(net_d_name)): + print("[!] Loading checkpoints failed!") + else: + net_g_loaded_params = tl.files.load_npz(name=net_g_name) + net_d_loaded_params = tl.files.load_npz(name=net_d_name) + tl.files.assign_params(sess, net_g_loaded_params, net_g) + tl.files.assign_params(sess, net_d_loaded_params, net_d) + print("[*] Loading checkpoints SUCCESS!") + + + # TODO: use minbatch to shuffle and iterate + data_files = glob(os.path.join("./data", FLAGS.dataset, "*.jpg")) + + + # TODO: shuffle sample_files each epoch + sample_seed = np.random.uniform(low=-1, high=1, size=(FLAGS.batch_size, z_dim)).astype(np.float32) + if FLAGS.is_train: + + iter_counter = 0 + for epoch in range(FLAGS.epoch): + #shuffle data + shuffle(data_files) + print("[*]Dataset shuffled!") + + # update sample files based on shuffled data + sample_files = data_files[0:FLAGS.batch_size] + sample = [get_image(sample_file, FLAGS.image_size, is_crop=FLAGS.is_crop, resize_w=FLAGS.output_size, is_grayscale = 0) for sample_file in sample_files] + sample_images = np.array(sample).astype(np.float32) + print sample_images.shape + print("[*]Sample images updated!") + + # load image data + batch_idxs = min(len(data_files), FLAGS.train_size) // FLAGS.batch_size + + for idx in xrange(batch_idxs): + batch_files = data_files[idx*FLAGS.batch_size:(idx+1)*FLAGS.batch_size] + # get real images + batch = [get_image(batch_file, FLAGS.image_size, is_crop=FLAGS.is_crop, resize_w=FLAGS.output_size, is_grayscale = 0) for batch_file in batch_files] + batch_images = np.array(batch).astype(np.float32) + batch_z = np.random.uniform(low=-1, high=1, size=(FLAGS.batch_size, z_dim)).astype(np.float32) + start_time = time.time() + # updates the discriminator + errD, _ = sess.run([d_loss, d_optim], feed_dict={z: batch_z, real_images: batch_images }) + # updates the generator, run generator twice to make sure that d_loss does not go to zero (difference from paper) + for _ in range(2): + errG, _ = sess.run([g_loss, g_optim], feed_dict={z: batch_z, real_images: batch_images}) + print("Epoch: [%2d/%2d] [%4d/%4d] time: %4.4f, d_loss: %.8f, g_loss: %.8f" \ + % (epoch, FLAGS.epoch, idx, batch_idxs, + time.time() - start_time, errD, errG)) + sys.stdout.flush() + + iter_counter += 1 + if np.mod(epoch, 1) == 0: + # generate and visualize generated images + #img, errD, errG = sess.run([net_g2.outputs, d_loss, g_loss], feed_dict={z : sample_seed, real_images: sample_images}) + img, errG = sess.run([net_g2.outputs, g_loss], feed_dict={z : sample_seed, real_images: sample_images}) + D, D_, errD = sess.run([net_d3.all_layers, net_d3.outputs, d_loss_real], feed_dict={real_images: sample_images}) + + ''' + img255 = (np.array(img) + 1) / 2 * 255 + tl.visualize.images2d(images=img255, second=0, saveable=True, + name='./{}/train_{:02d}_{:04d}'.format(FLAGS.sample_dir, epoch, idx), dtype=None, fig_idx=2838) + ''' + save_images(img, [8, 8], + './{}/train_{:02d}.png'.format(FLAGS.sample_dir, epoch)) + print("[Sample] d_loss: %.8f, g_loss: %.8f" % (errD, errG)) +# for i in range(len(D)): +# print D[i].shape + print D[-1], D_, sigmoid(D[-1]), sigmoid(D[-1])==D_ + sys.stdout.flush() + + if np.mod(epoch, 5) == 0: + print epoch + # save current network parameters + print("[*] Saving checkpoints...") + model_dir = "%s_%s_%s" % (FLAGS.dataset, FLAGS.batch_size, FLAGS.output_size) + save_dir = os.path.join(FLAGS.checkpoint_dir, model_dir) + if not os.path.exists(save_dir): + os.makedirs(save_dir) + # the latest version location + net_g_name = os.path.join(save_dir, str(epoch)+'net_g.npz') + net_d_name = os.path.join(save_dir, str(epoch)+'net_d.npz') + # this version is for future re-check and visualization analysis +# net_g_iter_name = os.path.join(save_dir, 'net_g_%d.npz' % iter_counter) +# net_d_iter_name = os.path.join(save_dir, 'net_d_%d.npz' % iter_counter) + tl.files.save_npz(net_g.all_params, name=net_g_name, sess=sess) + tl.files.save_npz(net_d.all_params, name=net_d_name, sess=sess) +# tl.files.save_npz(net_g.all_params, name=net_g_iter_name, sess=sess) +# tl.files.save_npz(net_d.all_params, name=net_d_iter_name, sess=sess) + print("[*] Saving checkpoints SUCCESS!") + + + +if __name__ == '__main__': + tf.app.run() diff --git a/train_svm.py b/train_svm.py new file mode 100644 index 0000000..d8a85a8 --- /dev/null +++ b/train_svm.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +""" +Created on Mon May 16 20:41:24 2016 + +@author: ldy +""" + + + +from time import time +from sklearn.metrics import accuracy_score +from sklearn.cross_validation import train_test_split +from sklearn.externals import joblib + +from sklearn import svm +# +import numpy as np +acc = [] +nums = [55, 60, 65, 70, 75, 80, 85] +for num in nums: + X_train=np.load('feat_match/features%d_train.npy'%num) + y_train=np.load('feat_match/label%d_train.npy'%num) + X_test=np.load('feat_match/features%d_test.npy'%num) + y_test=np.load('feat_match/label%d_test.npy'%num) + + print("Fitting the classifier to the training set") + t0 = time() + C = 1000.0 # SVM regularization parameter + clf = svm.SVC(kernel='linear', C=C).fit(X_train, y_train) + print("done in %0.3fs" % (time() - t0)) + + print("Predicting...") + t0 = time() + y_pred = clf.predict(X_test) + + print "Accuracy: %.3f" %(accuracy_score(y_test, y_pred)) + acc.append(accuracy_score(y_test, y_pred)) +print acc + + diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..6d417fe --- /dev/null +++ b/utils.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- +""" +Created on Mon Oct 30 20:47:21 2017 + +@author: ldy +""" +import scipy.misc + +import numpy as np + +def center_crop(x, crop_h, crop_w=None, resize_w=64): + if crop_w is None: + crop_w = crop_h + h, w = x.shape[:2] + j = int(round((h - crop_h)/2.)) + i = int(round((w - crop_w)/2.)) + return scipy.misc.imresize(x[j:j+crop_h, i:i+crop_w], + [resize_w, resize_w]) + +def merge(images, size): + h, w = images.shape[1], images.shape[2] + img = np.zeros((h * size[0], w * size[1], 3)) + for idx, image in enumerate(images): + i = idx % size[1] + j = idx // size[1] + img[j*h:j*h+h, i*w:i*w+w, :] = image + return img + +def transform(image, npx=64, is_crop=True, resize_w=64): + if is_crop: + cropped_image = center_crop(image, npx, resize_w=resize_w) + else: + cropped_image = image + return np.array(cropped_image)/127.5 - 1. + +def inverse_transform(images): + return (images+1.)/2. + +def imread(path, is_grayscale = False): + if (is_grayscale): + return scipy.misc.imread(path, flatten = True).astype(np.float) + else: + return scipy.misc.imread(path).astype(np.float) + +def imsave(images, size, path): + return scipy.misc.imsave(path, merge(images, size)) + +def get_image(image_path, image_size, is_crop=True, resize_w=64, is_grayscale = False): + return transform(imread(image_path, is_grayscale), image_size, is_crop, resize_w) + +def save_images(images, size, image_path): + return imsave(inverse_transform(images), size, image_path) \ No newline at end of file