train_mnist_siamese.py 1.73 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 25 26 27
from bob.learn.tensorflow.data import PairDataShuffler
from bob.learn.tensorflow.network import Lenet
from bob.learn.tensorflow.trainers import SiameseTrainer
from bob.learn.tensorflow.loss import ContrastiveLoss
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
28

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))
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
43

44
    data_shuffler = PairDataShuffler(data, labels)
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
45

46
    # Preparing the architecture
47
    lenet = Lenet(feature_layer="fc2")
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
48

49
    loss = ContrastiveLoss()
50 51 52 53 54 55
    trainer = SiameseTrainer(architecture=lenet,
                             loss=loss,
                             iterations=ITERATIONS,
                             base_lr=0.00001,
                             save_intermediate=False,
                             snapshot=VALIDATION_TEST)
56
    trainer.train(data_shuffler)
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
57 58