<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import datumio . datagen as dtd <EOL> from keras . datasets import cifar10 <EOL> from keras . models import Sequential <EOL> from keras . layers . core import Dense , Dropout , Activation , Flatten <EOL> from keras . layers . convolutional import Convolution2D , MaxPooling2D <EOL> from keras . optimizers import SGD , Adadelta , Adagrad <EOL> from keras . utils import np_utils , generic_utils <EOL> from six . moves import range <EOL> import numpy as np <EOL> import os <EOL> from PIL import Image <EOL> import shutil <EOL> batch_size = <NUM_LIT:32> <EOL> nb_classes = <NUM_LIT:10> <EOL> nb_epoch = <NUM_LIT:20> <EOL> data_augmentation = True <EOL> img_rows , img_cols = <NUM_LIT:32> , <NUM_LIT:32> <EOL> img_channels = <NUM_LIT:3> <EOL> ( X_train , y_train ) , ( X_test , y_test ) = cifar10 . load_data ( ) <EOL> print ( '<STR_LIT>' , X_train . shape ) <EOL> print ( X_train . shape [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> print ( X_test . shape [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> if not os . path . exists ( '<STR_LIT>' ) : os . mkdir ( '<STR_LIT>' ) <EOL> X_train_paths = [ ] <EOL> for i , x in enumerate ( X_train ) : <EOL> path = os . path . abspath ( os . path . join ( '<STR_LIT>' , '<STR_LIT>' % ( i + <NUM_LIT:1> ) ) ) <EOL> X_train_paths . append ( path ) <EOL> Image . fromarray ( x . transpose ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) ) . save ( path ) <EOL> if not os . path . exists ( '<STR_LIT>' ) : os . mkdir ( '<STR_LIT>' ) <EOL> X_test_paths = [ ] <EOL> for i , x in enumerate ( X_test ) : <EOL> path = os . path . abspath ( os . path . join ( '<STR_LIT>' , '<STR_LIT>' % ( i + <NUM_LIT:1> ) ) ) <EOL> X_test_paths . append ( path ) <EOL> Image . fromarray ( x . transpose ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) ) . save ( path ) <EOL> Y_train = np_utils . to_categorical ( y_train , nb_classes ) <EOL> Y_test = np_utils . to_categorical ( y_test , nb_classes ) <EOL> model = Sequential ( ) <EOL> model . add ( Convolution2D ( <NUM_LIT:32> , <NUM_LIT:3> , <NUM_LIT:3> , border_mode = '<STR_LIT>' , <EOL> input_shape = ( img_channels , img_rows , img_cols ) ) ) <EOL> model . add ( Activation ( '<STR_LIT:relu>' ) ) <EOL> model . add ( Convolution2D ( <NUM_LIT:32> , <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> model . add ( Activation ( '<STR_LIT:relu>' ) ) <EOL> model . add ( MaxPooling2D ( pool_size = ( <NUM_LIT:2> , <NUM_LIT:2> ) ) ) <EOL> model . add ( Dropout ( <NUM_LIT> ) ) <EOL> model . add ( Convolution2D ( <NUM_LIT:64> , <NUM_LIT:3> , <NUM_LIT:3> , border_mode = '<STR_LIT>' ) ) <EOL> model . add ( Activation ( '<STR_LIT:relu>' ) ) <EOL> model . add ( Convolution2D ( <NUM_LIT:64> , <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> model . add ( Activation ( '<STR_LIT:relu>' ) ) <EOL> model . add ( MaxPooling2D ( pool_size = ( <NUM_LIT:2> , <NUM_LIT:2> ) ) ) <EOL> model . add ( Dropout ( <NUM_LIT> ) ) <EOL> model . add ( Flatten ( ) ) <EOL> model . add ( Dense ( <NUM_LIT> ) ) <EOL> model . add ( Activation ( '<STR_LIT:relu>' ) ) <EOL> model . add ( Dropout ( <NUM_LIT:0.5> ) ) <EOL> model . add ( Dense ( nb_classes ) ) <EOL> model . add ( Activation ( '<STR_LIT>' ) ) <EOL> sgd = SGD ( lr = <NUM_LIT> , decay = <NUM_LIT> , momentum = <NUM_LIT> , nesterov = True ) <EOL> model . compile ( loss = '<STR_LIT>' , optimizer = sgd ) <EOL> X_train = X_train . astype ( '<STR_LIT>' ) <EOL> X_test = X_test . astype ( '<STR_LIT>' ) <EOL> X_train /= <NUM_LIT:255> <EOL> X_test /= <NUM_LIT:255> <EOL> if not data_augmentation : <EOL> print ( '<STR_LIT>' ) <EOL> model . fit ( X_train , Y_train , batch_size = batch_size , nb_epoch = nb_epoch ) <EOL> score = model . evaluate ( X_test , Y_test , batch_size = batch_size ) <EOL> print ( '<STR_LIT>' , score ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> rng_aug_params = { '<STR_LIT>' : ( - <NUM_LIT:20> , <NUM_LIT:20> ) , <EOL> '<STR_LIT>' : ( - <NUM_LIT:4> , <NUM_LIT:4> ) , <EOL> '<STR_LIT>' : True } <EOL> X_train = np . array ( X_train_paths ) <EOL> X_test = np . array ( X_test_paths ) <EOL> errors = [ ] <EOL> for e in range ( nb_epoch ) : <EOL> datagen = dtd . DataGenerator ( X_train , y = Y_train , <EOL> rng_aug_params = rng_aug_params , <EOL> dataset_zmuv = True , dataset_axis = <NUM_LIT:0> ) <EOL> datagen . resample_dataset ( Y_train , '<STR_LIT>' ) <EOL> testgen = dtd . DataGenerator ( X_test , y = Y_test , <EOL> dataset_zmuv = True , dataset_axis = <NUM_LIT:0> ) <EOL> testgen . mean = datagen . mean <EOL> testgen . std = datagen . std <EOL> print ( '<STR_LIT:->' * <NUM_LIT> ) <EOL> print ( '<STR_LIT>' , e ) <EOL> print ( '<STR_LIT:->' * <NUM_LIT> ) <EOL> print ( '<STR_LIT>' ) <EOL> progbar = generic_utils . Progbar ( X_train . shape [ <NUM_LIT:0> ] ) <EOL> for X_batch , Y_batch in datagen . get_batch ( <EOL> batch_size = batch_size , shuffle = True , <EOL> chw_order = True ) : <EOL> loss = model . train_on_batch ( X_batch , Y_batch ) <EOL> progbar . add ( X_batch . shape [ <NUM_LIT:0> ] , values = [ ( '<STR_LIT>' , loss [ <NUM_LIT:0> ] ) ] ) <EOL> print ( '<STR_LIT>' ) <EOL> progbar = generic_utils . Progbar ( X_test . shape [ <NUM_LIT:0> ] ) <EOL> scores = [ ] <EOL> for X_batch , Y_batch in testgen . get_batch ( <EOL> batch_size = batch_size , chw_order = True ) : <EOL> score = model . test_on_batch ( X_batch , Y_batch ) <EOL> progbar . add ( X_batch . shape [ <NUM_LIT:0> ] , values = [ ( '<STR_LIT>' , score [ <NUM_LIT:0> ] ) ] ) <EOL> scores . append ( score ) <EOL> errors . append ( np . mean ( scores ) ) <EOL> print ( errors ) <EOL> shutil . rmtree ( '<STR_LIT>' ) <EOL> shutil . rmtree ( '<STR_LIT>' ) </s>
