diff --git a/examples/mnist/finetune_mnist.py b/examples/mnist/finetune_mnist.py index d2ef8d8..fe9c018 100755 --- a/examples/mnist/finetune_mnist.py +++ b/examples/mnist/finetune_mnist.py @@ -37,13 +37,13 @@ def gen_data_batch(source): ip2 = net.layers['ip2'] pred = tf.nn.softmax(ip2) -loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(ip2, labels), 0) +loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=ip2,labels= labels), 0) opt = tf.train.RMSPropOptimizer(0.001) train_op = opt.minimize(loss) with tf.Session() as sess: # Load the data - sess.run(tf.initialize_all_variables()) + sess.run(tf.global_variables_initializer()) net.load('mynet.npy', sess) data_gen = gen_data_batch(mnist.train) diff --git a/finetune_vgg.py b/finetune_vgg.py new file mode 100755 index 0000000..12a8614 --- /dev/null +++ b/finetune_vgg.py @@ -0,0 +1,89 @@ +# Import the converted model's class +import numpy as np +import random +import tensorflow as tf +from tensorflow.examples.tutorials.mnist import input_data + +from vggnet import VGG_FACE_16_layers as MyNet + +# load image and labels +X = np.load("../../outfile_x.npy") +y = np.load("../../outfile_y.npy").astype(int) +n_class= len(np.unique(y)) +print "It's a {}-class classification problem".format(str(n_class)) +print "====The shape of data : X : {} , y : {}====".format(str(X.shape),str(y.shape)) + + +arr = np.arange(len(X)) +np.random.shuffle(arr) +X=X[arr] +y=y[arr] +data={ + 'X_train': X[:int(len(X)*0.8)], + 'y_train': y[:int(len(X)*0.8)], + 'X_val': X[int(len(X)*0.8):], + 'y_val': y[int(len(X)*0.8):], +} +print "there are "+ str(data['X_train'].shape[0]) + " images in training set" +print "there are "+ str(data['X_val'].shape[0]) + " images in testing set" + + + +def weight_variable(shape): + initial = tf.truncated_normal(shape, stddev=0.0001) + return tf.Variable(initial) + +def bias_variable(shape): + initial = tf.constant(0.01, shape=shape) + return tf.Variable(initial) + +images_raw = tf.placeholder(tf.float32, shape=[None, 32,32,3]) +images = tf.image.resize_images(images_raw,(224,224)) +labels_raw = tf.placeholder(tf.int32, shape=[None,],name='truth_y') +labels = tf.one_hot(labels_raw,n_class) + +# concate network +net = MyNet({'data': images}) + +pool5 = net.layers['pool5'] +with tf.name_scope('fintune_whimh'): + with tf.name_scope('fc6'): + pool5_flat_size = int(np.prod(pool5.shape[1:])) + pool5_flat = tf.reshape(pool5, [-1, pool5_flat_size]) + W_fc6 = weight_variable([pool5_flat_size, 1024]) + b_fc6 = bias_variable([1024]) + H_fc6=tf.nn.relu(tf.matmul(pool5_flat, W_fc6) + b_fc6) + with tf.name_scope('cross_entropy'): + #calculate_entropy + W_fc7 = weight_variable([1024, n_class]) + b_fc7 = bias_variable([n_class]) + fc7 = tf.matmul(H_fc6, W_fc7) + b_fc7 + predctions = tf.argmax(fc7,1, name='predictions') + ground_truth = tf.argmax(labels,1) + +correct_prediction = tf.equal(predctions, ground_truth) +accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) +loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=fc7 ,labels= labels), 0) +train_op = tf.train.AdamOptimizer(1e-3).minimize(loss) + +with tf.Session() as sess: + # Load the data + sess.run(tf.global_variables_initializer()) + net.load('vggnet.npy', sess) + + for i in range(1000): + arr = np.arange(len(data['X_train'])) + np.random.shuffle(arr) + np_images, np_labels = (data['X_train'][arr[:30]]\ + ,data['y_train'][arr[:30]]) + + feed = {images_raw: np_images, labels_raw: np_labels} + np_loss,acc, _ = sess.run([loss,accuracy, train_op], feed_dict=feed) + if i % 30 == 0: + arr = np.arange(len(data['X_val'])) + np.random.shuffle(arr) + np_images, np_labels = (data['X_val'][arr[:30]]\ + ,data['y_val'][arr[:30]]) + feed = {images_raw: np_images, labels_raw: np_labels} + np_loss,acc = sess.run([loss,accuracy], feed_dict=feed) + print('Iteration: ', i, np_loss,acc) diff --git a/forward->pool5.ipynb b/forward->pool5.ipynb new file mode 100644 index 0000000..1dc1bd3 --- /dev/null +++ b/forward->pool5.ipynb @@ -0,0 +1,818 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "It's a 5-class classification problem\n", + "====The shape of data : X : (7799, 32, 32, 3) , y : (7799,)====\n" + ] + } + ], + "source": [ + "# Import the converted model's class\n", + "import numpy as np\n", + "import random\n", + "import tensorflow as tf\n", + "from tensorflow.examples.tutorials.mnist import input_data\n", + "\n", + "from vggnet import VGG_FACE_16_layers as MyNet\n", + "\n", + "# load image and labels\n", + "X = np.load(\"../../outfile_x.npy\")\n", + "y = np.load(\"../../outfile_y.npy\").astype(int)\n", + "n_class= len(np.unique(y))\n", + "print \"It's a {}-class classification problem\".format(str(n_class))\n", + "print \"====The shape of data : X : {} , y : {}====\".format(str(X.shape),str(y.shape))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Counter({0: 275, 1: 6568, 2: 326, 3: 365, 4: 265})" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from collections import Counter\n", + "Counter(y)\n", + "# since 1 is to many sample 1000 out" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "label1=np.where(y==1)\n", + "np.random.shuffle(label1)\n", + "others=np.where(y!=1)\n", + "label_list=list(label1[0][:4000])+list(others[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(5231, 32, 32, 3)\n", + "(5231,)\n" + ] + } + ], + "source": [ + "# limit number of label1\n", + "X=X[label_list]\n", + "y=y[label_list]\n", + "print X.shape\n", + "print y.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def weight_variable(shape):\n", + " initial = tf.truncated_normal(shape, stddev=0.0001)\n", + " return tf.Variable(initial)\n", + "\n", + "def bias_variable(shape):\n", + " initial = tf.constant(0.01, shape=shape)\n", + " return tf.Variable(initial)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# [Sec1] fintune from freeze pool5" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "images_raw = tf.placeholder(tf.float32, shape=[None, 32,32,3])\n", + "images = tf.image.resize_images(images_raw,(224,224))\n", + "\n", + "# concate network\n", + "net = MyNet({'data': images})\n", + "pool5 = net.layers['pool5']\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "with tf.Session() as sess:\n", + " # Load the data\n", + " sess.run(tf.global_variables_initializer())\n", + " net.load('vggnet.npy', sess)\n", + " batch_size=30\n", + " idx=0\n", + " result_list=[]\n", + " while idx*batch_size