Ejemplo n.º 1
0
def predict_single_sample():
    mnist = input_data.read_data_sets('data/mnist-data', one_hot=False)
    sess = tf.InteractiveSession()

    # setup siamese network
    siamese = inference.siamese(tf.estimator.ModeKeys.PREDICT)
    saver = tf.train.Saver()
    print('Restore parameters from model {}'.format(model_snapshot_path))
    saver.restore(sess, save_path=model_snapshot_path)
    test_images = mnist.test.images
    test_labels = mnist.test.labels
    print('test_images:', test_images.shape, test_images.dtype)
    print('test_labels:', test_labels.shape, test_labels.dtype)
    gallery_image = []
    gallery_label = []
    for i in range(100):
        if len(gallery_image) != 10:
            if test_labels[i] not in gallery_label:
                gallery_label.append(test_labels[i])
                gallery_image.append(test_images[i])
        else:
            break
    # print(type(gallery_image[1]))
    # gallery_image = np.array(gallery_image)
    # print(i, len(gallery_label), type(gallery_label[0]))
    # print(gallery_label)
    # print(type(gallery_image[1]))
    # print(gallery_image.shape)
    # visualize.show_data(gallery_image.reshape([-1, 28, 28]), 'gallery_image')
    # plt.show()

    tm = test_images[121]
    distance = siamese.distance.eval({
        siamese.x1:
        inference.format_single_sample(tm),
        siamese.x2:
        gallery_image
    })
    idn = siamese.single_sample_identity.eval({
        siamese.x1:
        inference.format_single_sample(tm),
        siamese.x2:
        gallery_image
    })
    print(idn, type(idn))
    print('predict label:', gallery_label[idn])
    print('true label:', test_labels[121])
    print('distance:', distance)
    print(tm.reshape([28, 28])[14, :])
    plt.imshow((tm.reshape([28, 28]) * 255).astype('uint8'), cmap='gray')
    plt.show()
Ejemplo n.º 2
0
def validate_accuracy():
    mnist = input_data.read_data_sets('data/mnist-data', one_hot=False)
    sess = tf.InteractiveSession()

    # setup siamese network
    siamese = inference.siamese(tf.estimator.ModeKeys.EVAL)
    saver = tf.train.Saver()
    print('Restore parameters from model {}'.format(model_snapshot_path))
    saver.restore(sess, save_path=model_snapshot_path)
    test_images = mnist.test.images
    test_labels = mnist.test.labels
    print('test_images:', test_images.shape, test_images.dtype)
    print('test_labels:', test_labels.shape, test_labels.dtype)
    gallery_image = []
    gallery_label = []
    for i in range(100):
        if len(gallery_image) != 10:
            if test_labels[i] not in gallery_label:
                gallery_label.append(test_labels[i])
                gallery_image.append(test_images[i])
        else:
            break

    correct_count = 0
    for i in range(100, len(test_images)):
        tm = test_images[i]
        idn = siamese.single_sample_identity.eval({
            siamese.x1:
            inference.format_single_sample(tm),
            siamese.x2:
            gallery_image
        })
        if gallery_label[idn] == test_labels[i]:
            correct_count += 1
        if (i + 1) % 1000 == 0:
            print('Test {:d} images'.format(i + 1))
    accuracy = correct_count / (len(test_images) - 100)
    print('accuracy:', accuracy)
Ejemplo n.º 3
0
def train_siamese():
    # prepare data and tf.session
    global_step = tf.Variable(0, name='global_step', trainable=False)
    lr = tf.train.piecewise_constant(global_step, boundaries, learning_rates)
    siamese = inference.siamese(tf.estimator.ModeKeys.TRAIN)
    train_step = tf.train.GradientDescentOptimizer(lr).minimize(
        siamese.loss, global_step=global_step)
    saver = tf.train.Saver()

    mnist = input_data.read_data_sets('data/mnist-data', one_hot=False)
    test_images = mnist.test.images
    test_labels = mnist.test.labels
    gallery_image = []
    gallery_label = []
    for i in range(100):
        if len(gallery_image) != 10:
            if test_labels[i] not in gallery_label:
                gallery_label.append(test_labels[i])
                gallery_image.append(test_images[i])
        else:
            break

    sess = tf.InteractiveSession()
    tf.global_variables_initializer().run()

    batch_x1, batch_y1 = mnist.train.next_batch(128)
    batch_x2, batch_y2 = mnist.train.next_batch(128)
    batch_y = (batch_y1 == batch_y2).astype('float')
    initial_loss = sess.run(siamese.loss,
                            feed_dict={
                                siamese.x1: batch_x1,
                                siamese.x2: batch_x2,
                                siamese.y_: batch_y
                            })
    print('The initial loss:', initial_loss)
    print('Global step:', sess.run(global_step))
    print('Initial learning rate:', sess.run(lr))

    print('Start train...')
    for step in range(80000):
        iterations = step + 1
        batch_x1, batch_y1 = mnist.train.next_batch(128)
        batch_x2, batch_y2 = mnist.train.next_batch(128)
        batch_y = (batch_y1 == batch_y2).astype('float')

        _, loss_v, gs_v, lr_v = sess.run(
            [train_step, siamese.loss, global_step, lr],
            feed_dict={
                siamese.x1: batch_x1,
                siamese.x2: batch_x2,
                siamese.y_: batch_y
            })

        if np.isnan(loss_v):
            print('Model diverged with loss = NaN')
            quit()
        if iterations % 100 == 0:
            # print('step %d: loss %.3f' % (iterations, loss_v))
            print(
                'Global step: {:d}, iterations: {:d}, learning rate: {:.5f}, loss: {:.4f}'
                .format(gs_v, iterations, lr_v, loss_v))

        if iterations % 1000 == 0:
            saver.save(sess=sess,
                       save_path=model_save_path,
                       global_step=iterations)

            print('Start test...')
            correct_count = 0
            for i in range(100, len(test_images)):
                tm = test_images[i]
                idn = siamese.single_sample_identity.eval({
                    siamese.x1:
                    inference.format_single_sample(tm),
                    siamese.x2:
                    gallery_image
                })
                if gallery_label[idn] == test_labels[i]:
                    correct_count += 1
            accuracy = correct_count / (len(test_images) - 100)
            print('Test accuracy: {:.4f}'.format(accuracy))