train_mnist.py 1.42 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 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/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:
  train_mnist.py [--batch-size=<arg> --iterations=<arg> --validation-interval=<arg> --use-gpu]
  train_mnist.py -h | --help
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
from ..DataShuffler import *
25 26 27
from bob.learn.tensorflow.network import Lenet
from bob.learn.tensorflow.trainers import Trainer
import numpy
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
28 29 30 31 32 33 34 35 36 37 38 39

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']
    perc_train = 0.9

    # Loading data
    data, labels = util.load_mnist(data_dir="./src/bob.db.mnist/bob/db/mnist/")
40
    data = numpy.reshape(data, (data.shape[0], 28, 28, 1))
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
41 42
    data_shuffler = DataShuffler(data, labels)

43 44
    # Preparing the architecture
    lenet = Lenet()
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
45

46
    loss = tf.nn.sparse_softmax_cross_entropy_with_logits
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
47
    trainer = Trainer(architecture=lenet, loss=loss, iterations=ITERATIONS)
48
    trainer.train(data_shuffler)
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
49 50