Example #1
0
def run_training():
    # you need to change this to your data directory
    train_dir = "D://Documents//PySpace//dataset//cats_vs_dogs//train//"

    logs_train_dir = ".//logs//train"
    logs_val_dir = ".//logs//val"

    tra_images, tra_labels, val_images, val_images = input.get_files(
        train_dir, RATIO)
Example #2
0
def run_training():
    
    # you need to change the directories to yours.
    train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/'
    logs_train_dir = '/home/kevin/tensorflow/cats_vs_dogs/logs/train/'
    logs_val_dir = '/home/kevin/tensorflow/cats_vs_dogs/logs/val/'
    
    train, train_label, val, val_label = input_train_val_split.get_files(train_dir, RATIO)
    train_batch, train_label_batch = input_train_val_split.get_batch(train,
                                                  train_label,
                                                  IMG_W,
                                                  IMG_H,
                                                  BATCH_SIZE, 
                                                  CAPACITY)
    val_batch, val_label_batch = input_train_val_split.get_batch(val,
                                                  val_label,
                                                  IMG_W,
                                                  IMG_H,
                                                  BATCH_SIZE, 
                                                  CAPACITY)
    

    
    x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3])
    y_ = tf.placeholder(tf.int16, shape=[BATCH_SIZE])
    
    logits = model.inference(x, BATCH_SIZE, N_CLASSES)
    loss = model.losses(logits, y_)  
    acc = model.evaluation(logits, y_)
    train_op = model.trainning(loss, learning_rate)
    
    
             
    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess= sess, coord=coord)
        
        summary_op = tf.summary.merge_all()        
        train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
        val_writer = tf.summary.FileWriter(logs_val_dir, sess.graph)
    
        try:
            for step in np.arange(MAX_STEP):
                if coord.should_stop():
                        break
                
                tra_images,tra_labels = sess.run([train_batch, train_label_batch])
                _, tra_loss, tra_acc = sess.run([train_op, loss, acc],
                                                feed_dict={x:tra_images, y_:tra_labels})
                if step % 50 == 0:
                    print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.0))
                    summary_str = sess.run(summary_op)
                    train_writer.add_summary(summary_str, step)
                    
                if step % 200 == 0 or (step + 1) == MAX_STEP:
                    val_images, val_labels = sess.run([val_batch, val_label_batch])
                    val_loss, val_acc = sess.run([loss, acc], 
                                                 feed_dict={x:val_images, y_:val_labels})
                    print('**  Step %d, val loss = %.2f, val accuracy = %.2f%%  **' %(step, val_loss, val_acc*100.0))
                    summary_str = sess.run(summary_op)
                    val_writer.add_summary(summary_str, step)  
                                    
                if step % 2000 == 0 or (step + 1) == MAX_STEP:
                    checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                    saver.save(sess, checkpoint_path, global_step=step)
                    
        except tf.errors.OutOfRangeError:
            print('Done training -- epoch limit reached')
        finally:
            coord.request_stop()           
        coord.join(threads)
Example #3
0
def train():
    with tf.name_scope('input'):
        train, train_label, val, val_label = input_train_val_split.get_files(
            train_dir, RATIO)
        tra_image_batch, tra_label_batch = input_train_val_split.get_batch(
            train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
        val_image_batch, val_label_batch = input_train_val_split.get_batch(
            val, val_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)

    x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3])
    y_ = tf.placeholder(tf.int16, shape=[BATCH_SIZE, N_CLASSES])

    logits = VGG.VGG16N(x, N_CLASSES, IS_PRETRAIN)
    loss = tools.loss(logits, y_)
    accuracy = tools.accuracy(logits, y_)

    my_global_step = tf.Variable(0, name='global_step', trainable=False)
    train_op = tools.optimize(loss, learning_rate, my_global_step)

    saver = tf.train.Saver(tf.global_variables())
    summary_op = tf.summary.merge_all()

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        tools.load_with_skip(pre_trained_weights, sess, ['fc8'])
        print("load weights done")

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        tra_summary_writer = tf.summary.FileWriter(train_log_dir, sess.graph)
        val_summary_writer = tf.summary.FileWriter(val_log_dir, sess.graph)

        try:
            for step in np.arange(MAX_STEP):
                if coord.should_stop():
                    break
                tra_images, tra_labels = sess.run(
                    [tra_image_batch, tra_label_batch])
                _, tra_loss, tra_acc = sess.run([train_op, loss, accuracy],
                                                feed_dict={
                                                    x: tra_images,
                                                    y_: tra_labels
                                                })
                if step % 2 == 0 or (step + 1) == MAX_STEP:

                    print('Step: %d, loss: %.4f, accuracy: %.4f%%' %
                          (step, tra_loss, tra_acc))
                    _, summary_str = sess.run([train_op, summary_op],
                                              feed_dict={
                                                  x: tra_images,
                                                  y_: tra_labels
                                              })
                    tra_summary_writer.add_summary(summary_str, step)

                if step % 4 == 0 or (step + 1) == MAX_STEP:
                    val_images, val_labels = sess.run(
                        [val_image_batch, val_label_batch])
                    val_loss, val_acc = sess.run([loss, accuracy],
                                                 feed_dict={
                                                     x: val_images,
                                                     y_: val_labels
                                                 })

                    print(
                        '**  Step %d, val loss = %.2f, val accuracy = %.2f%%  **'
                        % (step, val_loss, val_acc))
                    _, summary_str = sess.run([train_op, summary_op],
                                              feed_dict={
                                                  x: val_images,
                                                  y_: val_labels
                                              })
                    val_summary_writer.add_summary(summary_str, step)

                if step % 8 == 0 or (step + 1) == MAX_STEP:
                    checkpoint_path = os.path.join(train_log_dir, 'model.ckpt')
                    saver.save(sess, checkpoint_path, global_step=step)

        except tf.errors.OutOfRangeError:
            print('Done training -- epoch limit reached')

        finally:
            coord.request_stop()

        coord.join(threads)
Example #4
0
def train():
    
    train_dir = './data'
    logs_train_dir = './logs/train/'
    logs_val_dir = './logs/val/'
    
    train, train_label, val, val_label = input_train_val_split.get_files(train_dir, RATIO)
	
    
    train_batch, train_label_batch = input_train_val_split.get_batch_OneHot(train,
                                                  train_label,
                                                  IMG_W,
                                                  IMG_H,
                                                  BATCH_SIZE, 
                                                  CAPACITY,
                                                  CHANNEL)
            
    val_batch, val_label_batch = input_train_val_split.get_batch_OneHot(val,
                                                  val_label,
                                                  IMG_W,
                                                  IMG_H,
                                                  BATCH_SIZE, 
                                                  CAPACITY,
                                                  CHANNEL)    
    
    with tf.name_scope('input'):
        x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, CHANNEL], name='x-input')
        y_ = tf.placeholder(tf.float32, shape=[BATCH_SIZE, N_CLASSES], name='y-input')
        keep_prob = tf.placeholder(tf.float32)
        
    # To run nicely in jupyter notebook
    #sess = tf.InteractiveSession()
    hidden5 = model.inference(
            images = x, 
            IMG_W = IMG_W, 
            IMG_H = IMG_H, 
            keep_prob = keep_prob, 
            BATCH_SIZE = BATCH_SIZE, 
            N_CLASSES = N_CLASSES, 
            CHANNEL = CHANNEL)
    
    # Probabilities - output from model (not the same as logits)
    y = tf.nn.softmax(hidden5)#?
    
    # Loss and optimizer
    with tf.name_scope('cross_entropy'):
        # The raw formulation of cross-entropy,
        diff = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=hidden5)
        with tf.name_scope('total'):
            cross_entropy = tf.reduce_mean(diff)
    tf.summary.scalar('cross_entropy', cross_entropy)
   
    with tf.name_scope('train'):
        train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)
 
    # Setup to test accuracy of model
    with tf.name_scope('accuracy'):
        with tf.name_scope('correct_prediction'):
            #tf.equal: Returns the truth value of (x == y) element-wise.
            #print('tf.argmax(y, 1) = {}'.format(tf.argmax(y, 1)))
            #print('tf.argmax(y_, 1) = {}'.format(tf.argmax(y_, 1)))
            correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        with tf.name_scope('accuracy'):
            #tf.cast: Casts a tensor to a new type.
            #tf.reduce_mean: Computes the mean of elements across dimensions of a tensor.
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar('accuracy', accuracy)
    
    saver = tf.train.Saver()
    
    with tf.Session() as sess:
        
        # Merge all the summaries and write them out to /tmp/tensorflow/mnist/logs/mnist_with_summaries (by default)
        merged = tf.summary.merge_all()
        train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
        test_writer = tf.summary.FileWriter(logs_val_dir)
        
        #tf.global_variables_initializer().run()#?
        ## Initilize all global variables
        sess.run(tf.global_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess = sess, coord = coord)
        
        #bTrain=True: data is batch of train data
        #bTrain=False: data is all of test data
        def feed_dict(bTrain):
            """Make a TensorFlow feed_dict: maps data onto Tensor placeholders."""
            if bTrain:
                xs, ys = tra_images, tra_labels#mnist.train.next_batch(100, fake_data=fake_data)
                k = dropout
            else:
                xs, ys = val_images, val_labels
                k = 1.0
            #print('len(xs) = {}'.format(len(xs)))
            #print('len(ys) = {}'.format(len(ys)))
            #print('ys[0] = {}'.format(ys[0]))
            #print('k = {}'.format(k))
            return {x: xs, y_: ys, keep_prob: k}
        
        # Train model
        # Run once to get the model to a good confidence level
        for i in range(max_steps):
            if i % 100 == 0:
                val_images, val_labels = sess.run([val_batch, val_label_batch])
                #print('len(val_labels) = {}'.format(len(val_labels)))
                #print('len(val_labels[0]) = {}'.format(len(val_labels[0])))
                #print('val_labels[0] = {}'.format(val_labels[0]))
                summary, acc = sess.run([merged, accuracy], feed_dict=feed_dict(False))
                test_writer.add_summary(summary, i)
                print('Accuracy at step %s: %s' % (i, acc))
                
            if i % 2000 == 0 or (i + 1) == max_steps:
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step = i)
                
            else:  # Record train set summaries, and train	            
                tra_images, tra_labels = sess.run([train_batch, train_label_batch])
        		
                if i % 200 == 199:
                    run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
                    run_metadata = tf.RunMetadata()
                    summary, _ = sess.run([merged, train_step],
        							feed_dict=feed_dict(True),
        							options=run_options,
        							run_metadata=run_metadata)
                    train_writer.add_run_metadata(run_metadata, 'step%03d' % i)
                    train_writer.add_summary(summary, i)
                    #print('Adding run metadata for', i)
                else:  # Record a summary
                    summary, _ = sess.run([merged, train_step], feed_dict=feed_dict(True))
                    train_writer.add_summary(summary, i)
        train_writer.close()
        test_writer.close()			
        
        coord.request_stop()
        coord.join(threads)		  
Example #5
0
def run_training():
    
    # you need to change the directories to yours.
    train_dir = 'C:/MIGUEL/ML/dogs and cats/train/'
    logs_train_dir = 'C:/MIGUEL/ML/dogs and cats/logs/train/'
    logs_val_dir = 'C:/MIGUEL/ML/dogs and cats/logs/val/'
    
    train, train_label, val, val_label = input_train_val_split.get_files(train_dir, RATIO)
    train_batch, train_label_batch = input_train_val_split.get_batch(train,
                                                  train_label,
                                                  IMG_W,
                                                  IMG_H,
                                                  BATCH_SIZE, 
                                                  CAPACITY)
    val_batch, val_label_batch = input_train_val_split.get_batch(val,
                                                  val_label,
                                                  IMG_W,
                                                  IMG_H,
                                                  BATCH_SIZE, 
                                                  CAPACITY)
    

    
    x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3],name='x')
    y_ = tf.placeholder(tf.int32, shape=[BATCH_SIZE],name='y')
    training = tf.placeholder_with_default(False, shape=(), name='training')
    

    logits = model.inference(x, BATCH_SIZE, N_CLASSES,training)
    loss = model.losses(logits, y_)  
    acc = model.evaluation(logits, y_)
    train_op = model.trainning(loss, learning_rate)
    

             
    with tf.Session() as sess:

        sess.run(tf.global_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess= sess, coord=coord)
        saver = tf.train.Saver()

        
        summary_op = tf.summary.merge_all()        
        train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
        val_writer = tf.summary.FileWriter(logs_val_dir, sess.graph)
        bn_update_ops=tf.get_collection(tf.GraphKeys.UPDATE_OPS)
       
        for op in (x,y_,training,train_op, loss, acc,summary_op,logits):#,bn_update_ops
            tf.add_to_collection("retrain_ops",op)
    
        try:
            for step in np.arange(MAX_STEP):
                if coord.should_stop():
                        break
                tra_images,tra_labels = sess.run([train_batch, train_label_batch])


                if step % 50 != 0:
                    _, lossValue, accValue,bnUpdateOps = sess.run([train_op, loss, acc,bn_update_ops]
                                                                  ,feed_dict={x:tra_images, y_:tra_labels,training:True})


               
                else : # means step % 50 == 0 
                    _, lossValue, accValue,bnUpdateOps,summary_str = sess.run([train_op, loss, acc,bn_update_ops,summary_op]
                                                                              ,feed_dict={x:tra_images, y_:tra_labels,training:True})
                    train_writer.add_summary(summary_str, step)
                    print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, lossValue, accValue*100.0))

                if step % 200 == 0 or (step) == MAX_STEP:
                    val_images, val_labels = sess.run([val_batch, val_label_batch])
                    val_loss, val_acc ,summary_str= sess.run([loss, acc,summary_op], 
                                                 feed_dict={x:val_images, y_:val_labels})
                    
                    val_writer.add_summary(summary_str, step)  
                    print('**  Step %d, val loss = %.2f, val accuracy = %.2f%%  **' %(step, val_loss, val_acc*100.0))

                if step % 20 == 0 or (step ) == MAX_STEP:
                    checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                    saver.save(sess, checkpoint_path, global_step=step)
                
                    
        except tf.errors.OutOfRangeError:
            print('Done training -- epoch limit reached')
        finally:
            coord.request_stop()           
        coord.join(threads)
Example #6
0
IMG_W = 208  # resize the image, if the input image is too large, training will be very slow.
IMG_H = 208
BATCH_SIZE = 16
CAPACITY = 2000
RATIO = 0.2
MAX_STEP = 1000  # with current parameters, it is suggested to use MAX_STEP>10k
learning_rate = 0.0001  # with current parameters, it is suggested to use learning rate<0.0001

# In[4]:

# you need to change the directories to yours.
train_dir = 'C:/MIGUEL/ML/dogs and cats/train/'
logs_train_dir = 'C:/MIGUEL/ML/dogs and cats/logs/train/retrain/saveretrain'
logs_val_dir = 'C:/MIGUEL/ML/dogs and cats/logs/val/'

train, train_label, val, val_label = input_train_val_split.get_files(
    train_dir, RATIO)
train_batch, train_label_batch = input_train_val_split.get_batch(
    train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
val_batch, val_label_batch = input_train_val_split.get_batch(
    val, val_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)

# In[5]:

train_writer = tf.summary.FileWriter(logs_train_dir, tf.get_default_graph())
val_writer = tf.summary.FileWriter(logs_val_dir, tf.get_default_graph())

# In[6]:

with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)