Exemplo n.º 1
0
def preload():
    ckpt_path = 'ckpt-alexnet'
    x_holder = tf.placeholder(tf.float32, [None, 150, 150, 3])
    keep_prob = tf.placeholder(tf.float32)
    pred, prob = alexnet.classifier(x_holder, keep_prob)
    sess = tf.Session()
    saver = tf.train.Saver()
    saver.restore(sess, os.path.join(ckpt_path, 'alexnet-cnn.ckpt'))
    return keep_prob, prob, sess, x_holder
def classify(
        image,
        top_k,
        k_patches,
        ckpt_path,
        imagenet_path):
    """	Procedure to classify the image given through the command line

        Args:
            image:	path to the image to classify
            top_k: 	integer representing the number of predictions with highest probability
                    to retrieve
            k_patches:	number of crops taken from an image and to input to the model
            ckpt_path:	path to model's tensorflow checkpoint
            imagenet_path:	path to ILSRVC12 ImageNet folder containing train images,
                        validation images, annotations and metadata file

    """
    wnids, words = tu.load_imagenet_meta(os.path.join(imagenet_path, 'data/meta.mat'))

    # taking a few crops from an image
    image_patches = tu.read_k_patches(image, k_patches)

    x = tf.placeholder(tf.float32, [None, 224, 224, 3])

    _, pred = alexnet.classifier(x, dropout=1.0)

    # calculate the average precision through the crops
    avg_prediction = tf.div(tf.reduce_sum(pred, 0), k_patches)

    # retrieve top 5 scores
    scores, indexes = tf.nn.top_k(avg_prediction, k=top_k)

    saver = tf.train.Saver()

    with tf.Session(config=tf.ConfigProto()) as sess:
        saver.restore(sess, os.path.join(ckpt_path, 'alexnet-cnn.ckpt'))

        s, i = sess.run([scores, indexes], feed_dict={x: image_patches})
        s, i = np.squeeze(s), np.squeeze(i)

        print('AlexNet saw:')
        for idx in range(top_k):
            print ('{} - score: {}'.format(words[i[idx]], s[idx]))
Exemplo n.º 3
0
def test(top_k, k_patches, display_step, imagenet_path, ckpt_path):
    """
	Procedure to evaluate top-1 and top-k accuracy (and error-rate) on the 
	ILSVRC2012 validation (test) set.

	Args:
		top_k: 	integer representing the number of predictions with highest probability
				to retrieve
		k_patches:	number of crops taken from an image and to input to the model
		display_step: number representing how often printing the current testing accuracy
		imagenet_path:	path to ILSRVC12 ImageNet folder containing train images, 
						validation images, annotations and metadata file 
		ckpt_path:	path to model's tensorflow checkpoint
	"""

    test_images = sorted(
        os.listdir(os.path.join(imagenet_path, 'ILSVRC2012_img_val')))
    test_labels = tu.read_test_labels(
        os.path.join(imagenet_path,
                     'data/ILSVRC2012_validation_ground_truth.txt'))

    test_examples = len(test_images)

    x = tf.placeholder(tf.float32, [None, 224, 224, 3])
    y = tf.placeholder(tf.float32, [None, 1000])

    _, pred = alexnet.classifier(x, 1.0)

    # calculate the average precision of the crops of the image
    avg_prediction = tf.div(tf.reduce_sum(pred, 0), k_patches)

    # accuracy
    top1_correct = tf.equal(tf.argmax(avg_prediction, 0), tf.argmax(y, 1))
    top1_accuracy = tf.reduce_mean(tf.cast(top1_correct, tf.float32))

    topk_correct = tf.nn.in_top_k(tf.stack([avg_prediction]),
                                  tf.argmax(y, 1),
                                  k=top_k)
    topk_accuracy = tf.reduce_mean(tf.cast(topk_correct, tf.float32))

    saver = tf.train.Saver()

    with tf.Session(config=tf.ConfigProto()) as sess:
        saver.restore(sess, os.path.join(ckpt_path, 'alexnet-cnn.ckpt'))

        total_top1_accuracy = 0.
        total_topk_accuracy = 0.

        for i in range(test_examples):
            # taking a few patches from an image
            image_patches = tu.read_k_patches(
                os.path.join(imagenet_path, 'ILSVRC2012_img_val',
                             test_images[i]), k_patches)
            label = test_labels[i]

            top1_a, topk_a = sess.run([top1_accuracy, topk_accuracy],
                                      feed_dict={
                                          x: image_patches,
                                          y: [label]
                                      })
            total_top1_accuracy += top1_a
            total_topk_accuracy += topk_a

            if i % display_step == 0:
                print(
                    'Examples done: {:5d}/{} ---- Top-1: {:.4f} -- Top-{}: {:.4f}'
                    .format(i + 1, test_examples,
                            total_top1_accuracy / (i + 1), top_k,
                            total_topk_accuracy / (i + 1)))

        print('---- Final accuracy ----')
        print('Top-1: {:.4f} -- Top-{}: {:.4f}'.format(
            total_top1_accuracy / test_examples, top_k,
            total_topk_accuracy / test_examples))
        print('Top-1 error rate: {:.4f} -- Top-{} error rate: {:.4f}'.format(
            1 - (total_top1_accuracy / test_examples), top_k,
            1 - (total_topk_accuracy / test_examples)))
Exemplo n.º 4
0
def train(epochs, batch_size, learning_rate, dropout, momentum, lmbda, resume,
          imagenet_path, display_step, test_step, ckpt_path, summary_path):
    """	Procedure to train the model on ImageNet ILSVRC 2012 training set
		Args:
			resume:	boolean variable, true if want to resume the training, false to train from scratch
			imagenet_path:	path to ILSRVC12 ImageNet folder containing train images, 
						validation images, annotations and metadata file
			display_step: number representing how often printing the current training accuracy
			test_step: number representing how often make a test and print the validation accuracy
			ckpt_path: path where to save model's tensorflow checkpoint (or from where resume)
			summary_path: path where to save logs for TensorBoard
	"""
    train_img_path = os.path.join(imagenet_path, 'ILSVRC2012_img_train')
    ts_size = tu.imagenet_size(train_img_path)
    num_batches = int(float(ts_size) / batch_size)

    wnid_labels, _ = tu.load_imagenet_meta(
        os.path.join(imagenet_path, 'data/meta.mat'))

    x = tf.placeholder(tf.float32, [None, 224, 224, 3])
    y = tf.placeholder(tf.float32, [None, 1000])

    lr = tf.placeholder(tf.float32)
    keep_prob = tf.placeholder(tf.float32)

    # queue of examples being filled on the cpu
    with tf.device('/cpu:0'):
        q = tf.FIFOQueue(batch_size * 3, [tf.float32, tf.float32],
                         shapes=[[224, 224, 3], [1000]])
        enqueue_op = q.enqueue_many([x, y])

        x_b, y_b = q.dequeue_many(batch_size)

    pred, _ = alexnet.classifier(x_b, keep_prob)

    # cross-entropy and weight decay
    with tf.name_scope('cross_entropy'):
        cross_entropy = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=pred,
                                                    labels=y_b,
                                                    name='cross-entropy'))

    with tf.name_scope('l2_loss'):
        l2_loss = tf.reduce_sum(
            lmbda *
            tf.stack([tf.nn.l2_loss(v) for v in tf.get_collection('weights')]))
        tf.summary.scalar('l2_loss', l2_loss)

    with tf.name_scope('loss'):
        loss = cross_entropy + l2_loss
        tf.summary.scalar('loss', loss)

    # accuracy
    with tf.name_scope('accuracy'):
        correct = tf.equal(tf.argmax(pred, 1), tf.argmax(y_b, 1))
        accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
        tf.summary.scalar('accuracy', accuracy)

    global_step = tf.Variable(0, trainable=False)
    epoch = tf.div(global_step, num_batches)

    # momentum optimizer
    with tf.name_scope('optimizer'):
        optimizer = tf.train.MomentumOptimizer(learning_rate=lr,
                                               momentum=momentum).minimize(
                                                   loss,
                                                   global_step=global_step)

    # merge summaries to write them to file
    merged = tf.summary.merge_all()

    # checkpoint saver
    saver = tf.train.Saver()

    coord = tf.train.Coordinator()

    #init = tf.initialize_all_variables()
    init = tf.global_variables_initializer()

    with tf.Session(config=tf.ConfigProto()) as sess:
        if resume:
            saver.restore(sess, os.path.join(ckpt_path, 'alexnet-cnn.ckpt'))
        else:
            sess.run(init)

        # enqueuing batches procedure
        def enqueue_batches():
            while not coord.should_stop():
                im, l = tu.read_batch(batch_size, train_img_path, wnid_labels)
                sess.run(enqueue_op, feed_dict={x: im, y: l})

        # creating and starting parallel threads to fill the queue
        num_threads = 3
        for i in range(num_threads):
            t = threading.Thread(target=enqueue_batches)
            t.setDaemon(True)
            t.start()

        # operation to write logs for tensorboard visualization
        train_writer = tf.summary.FileWriter(
            os.path.join(summary_path, 'train'), sess.graph)

        start_time = time.time()
        for e in range(sess.run(epoch), epochs):
            for i in range(num_batches):

                summary_str, _, step = sess.run(
                    [merged, optimizer, global_step],
                    feed_dict={
                        lr: learning_rate,
                        keep_prob: dropout
                    })
                train_writer.add_summary(summary_str, step)

                # decaying learning rate
                if step == 170000 or step == 350000:
                    learning_rate /= 10

                # display current training informations
                if step % display_step == 0:
                    c, a = sess.run([loss, accuracy],
                                    feed_dict={
                                        lr: learning_rate,
                                        keep_prob: 1.0
                                    })
                    print(
                        'Epoch: {:03d} Step/Batch: {:09d} --- Loss: {:.7f} Training accuracy: {:.4f}'
                        .format(e, step, c, a))

                # make test and evaluate validation accuracy
                if step % test_step == 0:
                    val_im, val_cls = tu.read_validation_batch(
                        batch_size,
                        os.path.join(imagenet_path, 'ILSVRC2012_img_val'),
                        os.path.join(
                            imagenet_path,
                            'data/ILSVRC2012_validation_ground_truth.txt'))
                    v_a = sess.run(accuracy,
                                   feed_dict={
                                       x_b: val_im,
                                       y_b: val_cls,
                                       lr: learning_rate,
                                       keep_prob: 1.0
                                   })
                    # intermediate time
                    int_time = time.time()
                    print('Elapsed time: {}'.format(
                        tu.format_time(int_time - start_time)))
                    print('Validation accuracy: {:.04f}'.format(v_a))
                    # save weights to file
                    save_path = saver.save(
                        sess, os.path.join(ckpt_path, 'alexnet-cnn.ckpt'))
                    print('Variables saved in file: %s' % save_path)

        end_time = time.time()
        print('Elapsed time: {}'.format(tu.format_time(end_time - start_time)))
        save_path = saver.save(sess, os.path.join(ckpt_path,
                                                  'alexnet-cnn.ckpt'))
        print('Variables saved in file: %s' % save_path)

        coord.request_stop()
Exemplo n.º 5
0
def train(
        epochs,
        batch_size,
        learning_rate,
        dropout,
        momentum,
        lmbda,
        resume,
        display_step,
        test_step,
        ckpt_path,
        summary_path
):

    train_img_path = '/var/data/bias_data/image/train'
    evaluate_path = '/var/data/bias_data/image/train'
    num_whole_images = 60000
    num_batches = int(float(num_whole_images) / batch_size)
    wnid_labels = ['cheer_out', 'fearful_out', 'happy_out', 'joy_out', 'rage_out', 'sorrow_out']

    x = tf.placeholder(tf.float32, [None, 150, 150, 3])
    y = tf.placeholder(tf.float32, [None, 6])

    lr = tf.placeholder(tf.float32)
    keep_prob = tf.placeholder(tf.float32)

    # queue of examples being filled on the cpu
    with tf.device('/cpu:0'):
        q = tf.FIFOQueue(batch_size * 3, [tf.float32, tf.float32], shapes=[[150, 150, 3], [6]])
        enqueue_op = q.enqueue_many([x, y])
        x_b, y_b = q.dequeue_many(batch_size)

    pred, prob = alexnet.classifier(x_b, keep_prob)

    # cross-entropy and weight decay
    with tf.name_scope('cross_entropy'):
        cross_entropy = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y_b, name='cross-entropy'))

    with tf.name_scope('l2_loss'):
        l2_loss = tf.reduce_sum(lmbda * tf.stack([tf.nn.l2_loss(v) for v in tf.get_collection('weights')]))
        tf.summary.scalar('l2_loss', l2_loss)

    with tf.name_scope('loss'):
        loss = cross_entropy + l2_loss
        tf.summary.scalar('loss', loss)

    # accuracy
    with tf.name_scope('accuracy'):
        correct = tf.equal(tf.argmax(pred, 1), tf.argmax(y_b, 1))
        accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
        tf.summary.scalar('accuracy', accuracy)

    global_step = tf.Variable(0, trainable=False)
    epoch = tf.div(global_step, num_batches)

    # momentum optimizer
    with tf.name_scope('optimizer'):
        optimizer = tf.train.MomentumOptimizer(learning_rate=lr, momentum=momentum).minimize(loss,
                                                                                             global_step=global_step)
    merged = tf.summary.merge_all()
    saver = tf.train.Saver()

    coord = tf.train.Coordinator()
    init = tf.global_variables_initializer()

    with tf.Session(config=tf.ConfigProto()) as sess:
        if resume:
            saver.restore(sess, os.path.join(ckpt_path, 'alexnet-cnn.ckpt'))
        else:
            sess.run(init)

        # enqueuing batches procedure
        def enqueue_batches():
            while not coord.should_stop():
                im, l = tu.read_batch(batch_size, train_img_path, wnid_labels)
                sess.run(enqueue_op, feed_dict={x: im, y: l})

        # creating and starting parallel threads to fill the queue
        num_threads = 3
        threads = []
        for i in range(num_threads):
            t = threading.Thread(target=enqueue_batches)
            t.setDaemon(True)
            t.start()
            threads.append(t)

        # operation to write logs for tensorboard visualization
        train_writer = tf.summary.FileWriter(os.path.join(summary_path, 'train'), sess.graph)

        valid_batch_size = 126
        val_im, val_cls = tu.read_batch(valid_batch_size, evaluate_path, wnid_labels)

        start_time = time.time()
        for e in range(sess.run(epoch), epochs):
            for i in range(num_batches):

                _, step = sess.run([optimizer, global_step], feed_dict={lr: learning_rate, keep_prob: dropout})
                # train_writer.add_summary(summary, step)

                # decaying learning rate
                if step == 170000 or step == 350000:
                    learning_rate /= 10

                # display current training informations
                if step % display_step == 0:
                    c, a = sess.run([loss, accuracy], feed_dict={lr: learning_rate, keep_prob: 1.0})
                    print (
                        'Epoch: {:03d} Step/Batch: {:09d} --- Loss: {:.7f} Training accuracy: {:.4f}'.format(e, step, c,
                                                                                                             a))

                # make test and evaluate validation accuracy
                if step % test_step == 0:
                    v_a = sess.run(accuracy, feed_dict={x_b: val_im, y_b: val_cls, lr: learning_rate, keep_prob: 1.0})
                    # intermediate time
                    int_time = time.time()
                    print ('Elapsed time: {}'.format(tu.format_time(int_time - start_time)))
                    print ('Validation accuracy: {:.04f}'.format(v_a))
                    # save weights to file
                    save_path = saver.save(sess, os.path.join(ckpt_path, 'alexnet-cnn.ckpt'))
                    print('Variables saved in file: %s' % save_path)

        end_time = time.time()
        print ('Elapsed time: {}'.format(tu.format_time(end_time - start_time)))
        save_path = saver.save(sess, os.path.join(ckpt_path, 'alexnet-cnn.ckpt'))
        print('Variables saved in file: %s' % save_path)

        coord.request_stop()
        coord.join(threads)
Exemplo n.º 6
0
            os.makedirs(layer_spec_dir)
            print('Create dir: %s' % layer_spec_dir)
    """ get imagenet images """
    test_images = sorted(
        os.listdir(os.path.join(imagenet_path, 'ILSVRC2012_img_val')))
    test_labels = tu.read_test_labels(
        os.path.join(imagenet_path,
                     'data/ILSVRC2012_validation_ground_truth.txt'))
    n_imgs = len(test_images)
    print(n_imgs)
    """ build graph """
    # set up the computational graph
    x = tf.placeholder(tf.float32, [None, 224, 224, 3])
    y = tf.placeholder(tf.float32, [None, 1000])

    _, pred = alexnet.classifier(x, 1.0)

    # load the model back
    saver = tf.train.Saver()
    """"""
    sess = tf.InteractiveSession()
    saver.restore(sess, os.path.join(ckpt_path, 'alexnet-cnn.ckpt'))

    # get layer name
    conv1 = tf.get_default_graph().get_tensor_by_name(
        'alexnet_cnn/alexnet_cnn_conv1/Relu:0')
    conv2 = tf.get_default_graph().get_tensor_by_name(
        'alexnet_cnn/alexnet_cnn_conv2/Relu:0')
    conv3 = tf.get_default_graph().get_tensor_by_name(
        'alexnet_cnn/alexnet_cnn_conv3/Relu:0')
    conv4 = tf.get_default_graph().get_tensor_by_name(