#NEWLINE# def create_dataset( ) : #NEWLINE# #INDENT# def save_to_records( save_path , images , labels ) : #NEWLINE# #INDENT# writer = tf . python_io . TFRecordWriter ( save_path ) #NEWLINE# for i in xrange ( images . shape [ 0 ] ) : #NEWLINE# #INDENT# image_raw = images [ i ] . tostring ( ) #NEWLINE# example = tf . train . Example ( features = tf . train . Features ( feature = { #NEWLINE# #INDENT# 'height' : _int64_feature ( 32 ) , #NEWLINE# 'width' : _int64_feature ( 32 ) , #NEWLINE# 'depth' : _int64_feature ( 3 ) , #NEWLINE# 'label' : _int64_feature ( int ( labels [ i ] ) ) , #NEWLINE# 'image_raw' : _bytes_feature ( image_raw ) , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# writer . write ( example . SerializeToString ( ) ) #NEWLINE# #UNINDENT# #UNINDENT# data_root = '/home/bgshi/research/datasets/cifar-10-batches-py' #NEWLINE# train_images = np . zeros ( ( 50000 , 3072 ) , dtype = np . float32 ) #NEWLINE# trian_labels = np . zeros ( ( 50000 , ) , dtype = np . int32 ) #NEWLINE# for i in xrange ( 5 ) : #NEWLINE# #INDENT# data_batch = joblib . load ( os . path . join ( data_root , ( 'data_batch_%d' % ( i + 1 ) ) ) ) #NEWLINE# train_images [ ( 10000 * i ) : ( 10000 * ( i + 1 ) ) ] = data_batch [ 'data' ] . astype ( np . float32 ) #NEWLINE# trian_labels [ ( 10000 * i ) : ( 10000 * ( i + 1 ) ) ] = np . asarray ( data_batch [ 'labels' ] , dtype = np . int32 ) #NEWLINE# #UNINDENT# train_images = np . reshape ( train_images , [ 50000 , 3 , 32 , 32 ] ) #NEWLINE# train_images = np . transpose ( train_images , axes = [ 0 , 2 , 3 , 1 ] ) #NEWLINE# images_mean = np . mean ( train_images , axis = 0 ) #NEWLINE# data_std = np . std ( train_images ) #NEWLINE# train_images -= images_mean #NEWLINE# train_images /= data_std #NEWLINE# train_set = { #NEWLINE# #INDENT# 'images' : train_images , #NEWLINE# 'labels' : trian_labels , #NEWLINE# #UNINDENT# } #NEWLINE# save_to_records ( '../data/cifar10/train_simple_norm.tf' , train_images , trian_labels ) #NEWLINE# data_batch = joblib . load ( os . path . join ( data_root , 'test_batch' ) ) #NEWLINE# test_images = data_batch [ 'data' ] . astype ( np . float32 ) #NEWLINE# test_images = np . reshape ( test_images , [ 10000 , 3 , 32 , 32 ] ) #NEWLINE# test_images = np . transpose ( test_images , axes = [ 0 , 2 , 3 , 1 ] ) #NEWLINE# test_images -= images_mean #NEWLINE# test_images /= data_std #NEWLINE# test_labels = np . asarray ( data_batch [ 'labels' ] , dtype = np . int32 ) #NEWLINE# test_set = { #NEWLINE# #INDENT# 'images' : test_images , #NEWLINE# 'labels' : test_labels , #NEWLINE# #UNINDENT# } #NEWLINE# save_to_records ( '../data/cifar10/test_simple_norm.tf' , test_images , test_labels )