train_mnist_siamese.py 2.61 KB
Newer Older
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Wed 11 May 2016 09:39:36 CEST 


"""
Simple script that trains MNIST with LENET using Tensor flow

Usage:
11 12
  train_mnist_siamese.py [--batch-size=<arg> --iterations=<arg> --validation-interval=<arg> --use-gpu]
  train_mnist_siamese.py -h | --help
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
13 14 15 16 17 18 19 20 21 22 23
Options:
  -h --help     Show this screen.
  --batch-size=<arg>  [default: 1]
  --iterations=<arg>  [default: 30000]
  --validation-interval=<arg>  [default: 100]
"""

from docopt import docopt
import tensorflow as tf
from .. import util
SEED = 10
24
from bob.learn.tensorflow.data import MemoryPairDataShuffler, TextDataShuffler
25 26 27
from bob.learn.tensorflow.network import Lenet
from bob.learn.tensorflow.trainers import SiameseTrainer
from bob.learn.tensorflow.loss import ContrastiveLoss
28
import bob.db.mobio
29
import numpy
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
30 31 32 33 34 35 36 37

def main():
    args = docopt(__doc__, version='Mnist training with TensorFlow')

    BATCH_SIZE = int(args['--batch-size'])
    ITERATIONS = int(args['--iterations'])
    VALIDATION_TEST = int(args['--validation-interval'])
    USE_GPU = args['--use-gpu']
38
    perc_train = 0.9
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
39

40
    # Loading data
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
41
    data, labels = util.load_mnist(data_dir="./src/bob.db.mnist/bob/db/mnist/")
42
    data = numpy.reshape(data, (data.shape[0], 28, 28, 1))
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    data_shuffler = MemoryPairDataShuffler(data, labels,
                                           input_shape=[28, 28, 1],
                                           train_batch_size=BATCH_SIZE,
                                           validation_batch_size=BATCH_SIZE*1000
                                           )

    #db = bob.db.mobio.Database()
    #objects = db.objects(protocol="male")

    #labels = [o.client_id for o in objects]
    #file_names = [o.make_path(
    #    directory="/remote/lustre/2/temp/tpereira/FACEREC_EXPERIMENTS/mobio_male/lda/preprocessed",
    #    extension=".hdf5")
    #              for o in objects]
    #data_shuffler = TextDataShuffler(file_names, labels,
    #                                 input_shape=[80, 64, 1],
    #                                 train_batch_size=BATCH_SIZE,
    #                                 validation_batch_size=BATCH_SIZE*100)
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
61 62


63
    # Preparing the architecture
64
    lenet = Lenet(default_feature_layer="fc2")
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
65

66
    loss = ContrastiveLoss()
67 68 69
    trainer = SiameseTrainer(architecture=lenet,
                             loss=loss,
                             iterations=ITERATIONS,
70
                             base_lr=0.0001,
71 72
                             save_intermediate=False,
                             snapshot=VALIDATION_TEST)
73
    trainer.train(data_shuffler)
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
74 75