signature:
def save_model ( self , network = None , model_name = 'model' , ** kwargs ) :
    """Save model architecture and parameters into database , timestamp will be added automatically .
 Parameters
 network : TensorLayer layer
 TensorLayer layer instance .
 model _ name : str
 The name / key of model .
 kwargs : other events
 Other events , such as name , accuracy , loss , step number and etc ( optinal ) .
 Examples
 Save model architecture and parameters into database .
 > > > db . save _ model ( net , accuracy = 0.8 , loss = 2.3 , name = ' second _ model ' )
 Load one model with parameters from database ( run this in other script )
 > > > net = db . find _ top _ model ( sess = sess , accuracy = 0.8 , loss = 2.3)
 Find and load the latest model .
 > > > net = db . find _ top _ model ( sess = sess , sort = [ ( " time " , pymongo . DESCENDING ) ] )
 > > > net = db . find _ top _ model ( sess = sess , sort = [ ( " time " , - 1 ) ] )
 Find and load the oldest model .
 > > > net = db . find _ top _ model ( sess = sess , sort = [ ( " time " , pymongo . ASCENDING ) ] )
 > > > net = db . find _ top _ model ( sess = sess , sort = [ ( " time " , 1 ) ] )
 Get model information
 > > > net . _ accuracy
 . . . 0.8
 Returns
 boolean : True for success , False for fail ."""

implementation:
kwargs . update ( { 'model_name' : model_name } )
self . _fill_project_info ( kwargs )
# put project _ name into kwargs
params = network . get_all_params ( )
s = time . time ( )
kwargs . update ( { 'architecture' : network . all_graphs , 'time' : datetime . utcnow ( ) } )
try :
    params_id = self . model_fs . put ( self . _serialization ( params ) )
    kwargs . update ( { 'params_id' : params_id , 'time' : datetime . utcnow ( ) } )
    self . db . Model . insert_one ( kwargs )
    print ( "[Database] Save model: SUCCESS, took: {}s" . format ( round ( time . time ( ) - s , 2 ) ) )
    return True
except Exception as e :
    exc_type , exc_obj , exc_tb = sys . exc_info ( )
    fname = os . path . split ( exc_tb . tb_frame . f_code . co_filename ) [ 1 ]
    logging . info ( "{}  {}  {}  {}  {}" . format ( exc_type , exc_obj , fname , exc_tb . tb_lineno , e ) )
    print ( "[Database] Save model: FAIL" )
    return False


signature:
def find_top_model ( self , sess , sort = None , model_name = 'model' , ** kwargs ) :
    """Finds and returns a model architecture and its parameters from the database which matches the requirement .
 Parameters
 sess : Session
 TensorFlow session .
 sort : List of tuple
 PyMongo sort comment , search " PyMongo find one sorting " and ` collection level operations < http : / / api . mongodb . com / python / current / api / pymongo / collection . html > ` _ _ for more details .
 model _ name : str or None
 The name / key of model .
 kwargs : other events
 Other events , such as name , accuracy , loss , step number and etc ( optinal ) .
 Examples
 - see ` ` save _ model ` ` .
 Returns
 network : TensorLayer layer
 Note that , the returned network contains all information of the document ( record ) , e . g . if you saved accuracy in the document , you can get the accuracy by using ` ` net . _ accuracy ` ` ."""

implementation:
# print ( kwargs ) # { }
kwargs . update ( { 'model_name' : model_name } )
self . _fill_project_info ( kwargs )
s = time . time ( )
d = self . db . Model . find_one ( filter = kwargs , sort = sort )
_temp_file_name = '_find_one_model_ztemp_file'
if d is not None :
    params_id = d [ 'params_id' ]
    graphs = d [ 'architecture' ]
    _datetime = d [ 'time' ]
    exists_or_mkdir ( _temp_file_name , False )
    with open ( os . path . join ( _temp_file_name , 'graph.pkl' ) , 'wb' ) as file :
        pickle . dump ( graphs , file , protocol = pickle . HIGHEST_PROTOCOL )
else :
    print ( "[Database] FAIL! Cannot find model: {}" . format ( kwargs ) )
    return False
try :
    params = self . _deserialization ( self . model_fs . get ( params_id ) . read ( ) )
    np . savez ( os . path . join ( _temp_file_name , 'params.npz' ) , params = params )
    network = load_graph_and_params ( name = _temp_file_name , sess = sess )
    del_folder ( _temp_file_name )
    pc = self . db . Model . find ( kwargs )
    print ( "[Database] Find one model SUCCESS. kwargs:{} sort:{} save time:{} took: {}s" . format ( kwargs , sort , _datetime , round ( time . time ( ) - s , 2 ) ) )
    # put all informations of model into the TL layer
    for key in d :
        network . __dict__ . update ( { "_%s" % key : d [ key ] } )
    # check whether more parameters match the requirement
    params_id_list = pc . distinct ( 'params_id' )
    n_params = len ( params_id_list )
    if n_params != 1 :
        print ( "     Note that there are {} models match the kwargs" . format ( n_params ) )
    return network
except Exception as e :
    exc_type , exc_obj , exc_tb = sys . exc_info ( )
    fname = os . path . split ( exc_tb . tb_frame . f_code . co_filename ) [ 1 ]
    logging . info ( "{}  {}  {}  {}  {}" . format ( exc_type , exc_obj , fname , exc_tb . tb_lineno , e ) )
    return False


signature:
def delete_model ( self , ** kwargs ) :
    """Delete model .
 Parameters
 kwargs : logging information
 Find items to delete , leave it empty to delete all log ."""

implementation:
self . _fill_project_info ( kwargs )
self . db . Model . delete_many ( kwargs )
logging . info ( "[Database] Delete Model SUCCESS" )


signature:
def save_dataset ( self , dataset = None , dataset_name = None , ** kwargs ) :
    """Saves one dataset into database , timestamp will be added automatically .
 Parameters
 dataset : any type
 The dataset you want to store .
 dataset _ name : str
 The name of dataset .
 kwargs : other events
 Other events , such as description , author and etc ( optinal ) .
 Examples
 Save dataset
 > > > db . save _ dataset ( [ X _ train , y _ train , X _ test , y _ test ] , ' mnist ' , description = ' this is a tutorial ' )
 Get dataset
 > > > dataset = db . find _ top _ dataset ( ' mnist ' )
 Returns
 boolean : Return True if save success , otherwise , return False ."""

implementation:
self . _fill_project_info ( kwargs )
if dataset_name is None :
    raise Exception ( "dataset_name is None, please give a dataset name" )
kwargs . update ( { 'dataset_name' : dataset_name } )
s = time . time ( )
try :
    dataset_id = self . dataset_fs . put ( self . _serialization ( dataset ) )
    kwargs . update ( { 'dataset_id' : dataset_id , 'time' : datetime . utcnow ( ) } )
    self . db . Dataset . insert_one ( kwargs )
    # print ( " [ Database ] Save params : { } SUCCESS , took : { } s " . format ( file _ name , round ( time . time ( ) - s , 2 ) ) )
    print ( "[Database] Save dataset: SUCCESS, took: {}s" . format ( round ( time . time ( ) - s , 2 ) ) )
    return True
except Exception as e :
    exc_type , exc_obj , exc_tb = sys . exc_info ( )
    fname = os . path . split ( exc_tb . tb_frame . f_code . co_filename ) [ 1 ]
    logging . info ( "{}  {}  {}  {}  {}" . format ( exc_type , exc_obj , fname , exc_tb . tb_lineno , e ) )
    print ( "[Database] Save dataset: FAIL" )
    return False


signature:
def find_top_dataset ( self , dataset_name = None , sort = None , ** kwargs ) :
    """Finds and returns a dataset from the database which matches the requirement .
 Parameters
 dataset _ name : str
 The name of dataset .
 sort : List of tuple
 PyMongo sort comment , search " PyMongo find one sorting " and ` collection level operations < http : / / api . mongodb . com / python / current / api / pymongo / collection . html > ` _ _ for more details .
 kwargs : other events
 Other events , such as description , author and etc ( optinal ) .
 Examples
 Save dataset
 > > > db . save _ dataset ( [ X _ train , y _ train , X _ test , y _ test ] , ' mnist ' , description = ' this is a tutorial ' )
 Get dataset
 > > > dataset = db . find _ top _ dataset ( ' mnist ' )
 > > > datasets = db . find _ datasets ( ' mnist ' )
 Returns
 dataset : the dataset or False
 Return False if nothing found ."""

implementation:
self . _fill_project_info ( kwargs )
if dataset_name is None :
    raise Exception ( "dataset_name is None, please give a dataset name" )
kwargs . update ( { 'dataset_name' : dataset_name } )
s = time . time ( )
d = self . db . Dataset . find_one ( filter = kwargs , sort = sort )
if d is not None :
    dataset_id = d [ 'dataset_id' ]
else :
    print ( "[Database] FAIL! Cannot find dataset: {}" . format ( kwargs ) )
    return False
try :
    dataset = self . _deserialization ( self . dataset_fs . get ( dataset_id ) . read ( ) )
    pc = self . db . Dataset . find ( kwargs )
    print ( "[Database] Find one dataset SUCCESS, {} took: {}s" . format ( kwargs , round ( time . time ( ) - s , 2 ) ) )
    # check whether more datasets match the requirement
    dataset_id_list = pc . distinct ( 'dataset_id' )
    n_dataset = len ( dataset_id_list )
    if n_dataset != 1 :
        print ( "     Note that there are {} datasets match the requirement" . format ( n_dataset ) )
    return dataset
except Exception as e :
    exc_type , exc_obj , exc_tb = sys . exc_info ( )
    fname = os . path . split ( exc_tb . tb_frame . f_code . co_filename ) [ 1 ]
    logging . info ( "{}  {}  {}  {}  {}" . format ( exc_type , exc_obj , fname , exc_tb . tb_lineno , e ) )
    return False


signature:
def find_datasets ( self , dataset_name = None , ** kwargs ) :
    """Finds and returns all datasets from the database which matches the requirement .
 In some case , the data in a dataset can be stored separately for better management .
 Parameters
 dataset _ name : str
 The name / key of dataset .
 kwargs : other events
 Other events , such as description , author and etc ( optional ) .
 Returns
 params : the parameters , return False if nothing found ."""

implementation:
self . _fill_project_info ( kwargs )
if dataset_name is None :
    raise Exception ( "dataset_name is None, please give a dataset name" )
kwargs . update ( { 'dataset_name' : dataset_name } )
s = time . time ( )
pc = self . db . Dataset . find ( kwargs )
if pc is not None :
    dataset_id_list = pc . distinct ( 'dataset_id' )
    dataset_list = [ ]
    for dataset_id in dataset_id_list : # you may have multiple Buckets files
        tmp = self . dataset_fs . get ( dataset_id ) . read ( )
        dataset_list . append ( self . _deserialization ( tmp ) )
else :
    print ( "[Database] FAIL! Cannot find any dataset: {}" . format ( kwargs ) )
    return False
print ( "[Database] Find {} datasets SUCCESS, took: {}s" . format ( len ( dataset_list ) , round ( time . time ( ) - s , 2 ) ) )
return dataset_list


signature:
def delete_datasets ( self , ** kwargs ) :
    """Delete datasets .
 Parameters
 kwargs : logging information
 Find items to delete , leave it empty to delete all log ."""

implementation:
self . _fill_project_info ( kwargs )
self . db . Dataset . delete_many ( kwargs )
logging . info ( "[Database] Delete Dataset SUCCESS" )


signature:
def save_training_log ( self , ** kwargs ) :
    """Saves the training log , timestamp will be added automatically .
 Parameters
 kwargs : logging information
 Events , such as accuracy , loss , step number and etc .
 Examples
 > > > db . save _ training _ log ( accuracy = 0.33 , loss = 0.98)"""

implementation:
self . _fill_project_info ( kwargs )
kwargs . update ( { 'time' : datetime . utcnow ( ) } )
_result = self . db . TrainLog . insert_one ( kwargs )
_log = self . _print_dict ( kwargs )
logging . info ( "[Database] train log: " + _log )


signature:
def save_validation_log ( self , ** kwargs ) :
    """Saves the validation log , timestamp will be added automatically .
 Parameters
 kwargs : logging information
 Events , such as accuracy , loss , step number and etc .
 Examples
 > > > db . save _ validation _ log ( accuracy = 0.33 , loss = 0.98)"""

implementation:
self . _fill_project_info ( kwargs )
kwargs . update ( { 'time' : datetime . utcnow ( ) } )
_result = self . db . ValidLog . insert_one ( kwargs )
_log = self . _print_dict ( kwargs )
logging . info ( "[Database] valid log: " + _log )


signature:
def delete_training_log ( self , ** kwargs ) :
    """Deletes training log .
 Parameters
 kwargs : logging information
 Find items to delete , leave it empty to delete all log .
 Examples
 Save training log
 > > > db . save _ training _ log ( accuracy = 0.33)
 > > > db . save _ training _ log ( accuracy = 0.44)
 Delete logs that match the requirement
 > > > db . delete _ training _ log ( accuracy = 0.33)
 Delete all logs
 > > > db . delete _ training _ log ( )"""

implementation:
self . _fill_project_info ( kwargs )
self . db . TrainLog . delete_many ( kwargs )
logging . info ( "[Database] Delete TrainLog SUCCESS" )


signature:
def delete_validation_log ( self , ** kwargs ) :
    """Deletes validation log .
 Parameters
 kwargs : logging information
 Find items to delete , leave it empty to delete all log .
 Examples
 - see ` ` save _ training _ log ` ` ."""

implementation:
self . _fill_project_info ( kwargs )
self . db . ValidLog . delete_many ( kwargs )
logging . info ( "[Database] Delete ValidLog SUCCESS" )


signature:
def create_task ( self , task_name = None , script = None , hyper_parameters = None , saved_result_keys = None , ** kwargs ) :
    """Uploads a task to the database , timestamp will be added automatically .
 Parameters
 task _ name : str
 The task name .
 script : str
 File name of the python script .
 hyper _ parameters : dictionary
 The hyper parameters pass into the script .
 saved _ result _ keys : list of str
 The keys of the task results to keep in the database when the task finishes .
 kwargs : other parameters
 Users customized parameters such as description , version number .
 Examples
 Uploads a task
 > > > db . create _ task ( task _ name = ' mnist ' , script = ' example / tutorial _ mnist _ simple . py ' , description = ' simple tutorial ' )
 Finds and runs the latest task
 > > > db . run _ top _ task ( sess = sess , sort = [ ( " time " , pymongo . DESCENDING ) ] )
 > > > db . run _ top _ task ( sess = sess , sort = [ ( " time " , - 1 ) ] )
 Finds and runs the oldest task
 > > > db . run _ top _ task ( sess = sess , sort = [ ( " time " , pymongo . ASCENDING ) ] )
 > > > db . run _ top _ task ( sess = sess , sort = [ ( " time " , 1 ) ] )"""

implementation:
if not isinstance ( task_name , str ) : # is None :
    raise Exception ( "task_name should be string" )
if not isinstance ( script , str ) : # is None :
    raise Exception ( "script should be string" )
if hyper_parameters is None :
    hyper_parameters = { }
if saved_result_keys is None :
    saved_result_keys = [ ]
self . _fill_project_info ( kwargs )
kwargs . update ( { 'time' : datetime . utcnow ( ) } )
kwargs . update ( { 'hyper_parameters' : hyper_parameters } )
kwargs . update ( { 'saved_result_keys' : saved_result_keys } )
_script = open ( script , 'rb' ) . read ( )
kwargs . update ( { 'status' : 'pending' , 'script' : _script , 'result' : { } } )
self . db . Task . insert_one ( kwargs )
logging . info ( "[Database] Saved Task - task_name: {} script: {}" . format ( task_name , script ) )


signature:
def run_top_task ( self , task_name = None , sort = None , ** kwargs ) :
    """Finds and runs a pending task that in the first of the sorting list .
 Parameters
 task _ name : str
 The task name .
 sort : List of tuple
 PyMongo sort comment , search " PyMongo find one sorting " and ` collection level operations < http : / / api . mongodb . com / python / current / api / pymongo / collection . html > ` _ _ for more details .
 kwargs : other parameters
 Users customized parameters such as description , version number .
 Examples
 Monitors the database and pull tasks to run
 > > > while True :
 > > > print ( " waiting task from distributor " )
 > > > db . run _ top _ task ( task _ name = ' mnist ' , sort = [ ( " time " , - 1 ) ] )
 > > > time . sleep ( 1)
 Returns
 boolean : True for success , False for fail ."""

implementation:
if not isinstance ( task_name , str ) : # is None :
    raise Exception ( "task_name should be string" )
self . _fill_project_info ( kwargs )
kwargs . update ( { 'status' : 'pending' } )
# find task and set status to running
task = self . db . Task . find_one_and_update ( kwargs , { '$set' : { 'status' : 'running' } } , sort = sort )
try : # get task info e . g . hyper parameters , python script
    if task is None :
        logging . info ( "[Database] Find Task FAIL: key: {} sort: {}" . format ( task_name , sort ) )
        return False
    else :
        logging . info ( "[Database] Find Task SUCCESS: key: {} sort: {}" . format ( task_name , sort ) )
    _datetime = task [ 'time' ]
    _script = task [ 'script' ]
    _id = task [ '_id' ]
    _hyper_parameters = task [ 'hyper_parameters' ]
    _saved_result_keys = task [ 'saved_result_keys' ]
    logging . info ( "  hyper parameters:" )
    for key in _hyper_parameters :
        globals ( ) [ key ] = _hyper_parameters [ key ]
        logging . info ( "    {}: {}" . format ( key , _hyper_parameters [ key ] ) )
    # run task
    s = time . time ( )
    logging . info ( "[Database] Start Task: key: {} sort: {} push time: {}" . format ( task_name , sort , _datetime ) )
    _script = _script . decode ( 'utf-8' )
    with tf . Graph ( ) . as_default ( ) : # as graph : # clear all TF graphs
        exec ( _script , globals ( ) )
    # set status to finished
    _ = self . db . Task . find_one_and_update ( { '_id' : _id } , { '$set' : { 'status' : 'finished' } } )
    # return results
    __result = { }
    for _key in _saved_result_keys :
        logging . info ( "  result: {}={} {}" . format ( _key , globals ( ) [ _key ] , type ( globals ( ) [ _key ] ) ) )
        __result . update ( { "%s" % _key : globals ( ) [ _key ] } )
    _ = self . db . Task . find_one_and_update ( { '_id' : _id } , { '$set' : { 'result' : __result } } , return_document = pymongo . ReturnDocument . AFTER )
    logging . info ( "[Database] Finished Task: task_name - {} sort: {} push time: {} took: {}s" . format ( task_name , sort , _datetime , time . time ( ) - s ) )
    return True
except Exception as e :
    exc_type , exc_obj , exc_tb = sys . exc_info ( )
    fname = os . path . split ( exc_tb . tb_frame . f_code . co_filename ) [ 1 ]
    logging . info ( "{}  {}  {}  {}  {}" . format ( exc_type , exc_obj , fname , exc_tb . tb_lineno , e ) )
    logging . info ( "[Database] Fail to run task" )
    # if fail , set status back to pending
    _ = self . db . Task . find_one_and_update ( { '_id' : _id } , { '$set' : { 'status' : 'pending' } } )
    return False


signature:
def delete_tasks ( self , ** kwargs ) :
    """Delete tasks .
 Parameters
 kwargs : logging information
 Find items to delete , leave it empty to delete all log .
 Examples
 > > > db . delete _ tasks ( )"""

implementation:
self . _fill_project_info ( kwargs )
self . db . Task . delete_many ( kwargs )
logging . info ( "[Database] Delete Task SUCCESS" )


signature:
def check_unfinished_task ( self , task_name = None , ** kwargs ) :
    """Finds and runs a pending task .
 Parameters
 task _ name : str
 The task name .
 kwargs : other parameters
 Users customized parameters such as description , version number .
 Examples
 Wait until all tasks finish in user ' s local console
 > > > while not db . check _ unfinished _ task ( ) :
 > > > time . sleep ( 1)
 > > > print ( " all tasks finished " )
 > > > sess = tf . InteractiveSession ( )
 > > > net = db . find _ top _ model ( sess = sess , sort = [ ( " test _ accuracy " , - 1 ) ] )
 > > > print ( " the best accuracy { } is from model { } " . format ( net . _ test _ accuracy , net . _ name ) )
 Returns
 boolean : True for success , False for fail ."""

implementation:
if not isinstance ( task_name , str ) : # is None :
    raise Exception ( "task_name should be string" )
self . _fill_project_info ( kwargs )
kwargs . update ( { '$or' : [ { 'status' : 'pending' } , { 'status' : 'running' } ] } )
# # # find task
# task = self . db . Task . find _ one ( kwargs )
task = self . db . Task . find ( kwargs )
task_id_list = task . distinct ( '_id' )
n_task = len ( task_id_list )
if n_task == 0 :
    logging . info ( "[Database] No unfinished task - task_name: {}" . format ( task_name ) )
    return False
else :
    logging . info ( "[Database] Find {} unfinished task - task_name: {}" . format ( n_task , task_name ) )
    return True


signature:
def augment_with_ngrams ( unigrams , unigram_vocab_size , n_buckets , n = 2 ) :
    """Augment unigram features with hashed n - gram features ."""

implementation:
def get_ngrams ( n ) :
    return list ( zip ( * [ unigrams [ i : ] for i in range ( n ) ] ) )
def hash_ngram ( ngram ) :
    bytes_ = array . array ( 'L' , ngram ) . tobytes ( )
    hash_ = int ( hashlib . sha256 ( bytes_ ) . hexdigest ( ) , 16 )
    return unigram_vocab_size + hash_ % n_buckets
return unigrams + [ hash_ngram ( ngram ) for i in range ( 2 , n + 1 ) for ngram in get_ngrams ( i ) ]


signature:
def load_and_preprocess_imdb_data ( n_gram = None ) :
    """Load IMDb data and augment with hashed n - gram features ."""

implementation:
X_train , y_train , X_test , y_test = tl . files . load_imdb_dataset ( nb_words = VOCAB_SIZE )
if n_gram is not None :
    X_train = np . array ( [ augment_with_ngrams ( x , VOCAB_SIZE , N_BUCKETS , n = n_gram ) for x in X_train ] )
    X_test = np . array ( [ augment_with_ngrams ( x , VOCAB_SIZE , N_BUCKETS , n = n_gram ) for x in X_test ] )
return X_train , y_train , X_test , y_test


signature:
def read_image ( image , path = '' ) :
    """Read one image .
 Parameters
 image : str
 The image file name .
 path : str
 The image folder path .
 Returns
 numpy . array
 The image ."""

implementation: return imageio . imread ( os . path . join ( path , image ) )


signature:
def read_images ( img_list , path = '' , n_threads = 10 , printable = True ) :
    """Returns all images in list by given path and name of each image file .
 Parameters
 img _ list : list of str
 The image file names .
 path : str
 The image folder path .
 n _ threads : int
 The number of threads to read image .
 printable : boolean
 Whether to print information when reading images .
 Returns
 list of numpy . array
 The images ."""

implementation:
imgs = [ ]
for idx in range ( 0 , len ( img_list ) , n_threads ) :
    b_imgs_list = img_list [ idx : idx + n_threads ]
    b_imgs = tl . prepro . threading_data ( b_imgs_list , fn = read_image , path = path )
    # tl . logging . info ( b _ imgs . shape )
    imgs . extend ( b_imgs )
    if printable :
        tl . logging . info ( 'read %d from %s' % ( len ( imgs ) , path ) )
return imgs


signature:
def save_image ( image , image_path = '_temp.png' ) :
    """Save a image .
 Parameters
 image : numpy array
 [ w , h , c ]
 image _ path : str
 path"""

implementation:
try : # RGB
    imageio . imwrite ( image_path , image )
except Exception : # Greyscale
    imageio . imwrite ( image_path , image [ : , : , 0 ] )


signature:
def save_images ( images , size , image_path = '_temp.png' ) :
    """Save multiple images into one single image .
 Parameters
 images : numpy array
 ( batch , w , h , c )
 size : list of 2 ints
 row and column number .
 number of images should be equal or less than size [ 0 ] * size [ 1]
 image _ path : str
 save path
 Examples
 > > > import numpy as np
 > > > import tensorlayer as tl
 > > > images = np . random . rand ( 64 , 100 , 100 , 3)
 > > > tl . visualize . save _ images ( images , [ 8 , 8 ] , ' temp . png ' )"""

implementation:
if len ( images . shape ) == 3 : # Greyscale [ batch , h , w ] - - > [ batch , h , w , 1]
    images = images [ : , : , : , np . newaxis ]
def merge ( images , size ) :
    h , w = images . shape [ 1 ] , images . shape [ 2 ]
    img = np . zeros ( ( h * size [ 0 ] , w * size [ 1 ] , 3 ) , dtype = images . dtype )
    for idx , image in enumerate ( images ) :
        i = idx % size [ 1 ]
        j = idx // size [ 1 ]
        img [ j * h : j * h + h , i * w : i * w + w , : ] = image
    return img
def imsave ( images , size , path ) :
    if np . max ( images ) <= 1 and ( - 1 <= np . min ( images ) < 0 ) :
        images = ( ( images + 1 ) * 127.5 ) . astype ( np . uint8 )
    elif np . max ( images ) <= 1 and np . min ( images ) >= 0 :
        images = ( images * 255 ) . astype ( np . uint8 )
    return imageio . imwrite ( path , merge ( images , size ) )
if len ( images ) > size [ 0 ] * size [ 1 ] :
    raise AssertionError ( "number of images should be equal or less than size[0] * size[1] {}" . format ( len ( images ) ) )
return imsave ( images , size , image_path )


signature:
def draw_boxes_and_labels_to_image ( image , classes , coords , scores , classes_list , is_center = True , is_rescale = True , save_name = None ) :
    """Draw bboxes and class labels on image . Return or save the image with bboxes , example in the docs of ` ` tl . prepro ` ` .
 Parameters
 image : numpy . array
 The RGB image [ height , width , channel ] .
 classes : list of int
 A list of class ID ( int ) .
 coords : list of int
 A list of list for coordinates .
 - Should be [ x , y , x2 , y2 ] ( up - left and botton - right format )
 - If [ x _ center , y _ center , w , h ] ( set is _ center to True ) .
 scores : list of float
 A list of score ( float ) . ( Optional )
 classes _ list : list of str
 for converting ID to string on image .
 is _ center : boolean
 Whether the coordinates is [ x _ center , y _ center , w , h ]
 - If coordinates are [ x _ center , y _ center , w , h ] , set it to True for converting it to [ x , y , x2 , y2 ] ( up - left and botton - right ) internally .
 - If coordinates are [ x1 , x2 , y1 , y2 ] , set it to False .
 is _ rescale : boolean
 Whether to rescale the coordinates from pixel - unit format to ratio format .
 - If True , the input coordinates are the portion of width and high , this API will scale the coordinates to pixel unit internally .
 - If False , feed the coordinates with pixel unit format .
 save _ name : None or str
 The name of image file ( i . e . image . png ) , if None , not to save image .
 Returns
 numpy . array
 The saved image .
 References
 - OpenCV rectangle and putText .
 - ` scikit - image < http : / / scikit - image . org / docs / dev / api / skimage . draw . html # skimage . draw . rectangle > ` _ _ ."""

implementation:
if len ( coords ) != len ( classes ) :
    raise AssertionError ( "number of coordinates and classes are equal" )
if len ( scores ) > 0 and len ( scores ) != len ( classes ) :
    raise AssertionError ( "number of scores and classes are equal" )
# don ' t change the original image , and avoid error https : / / stackoverflow . com / questions / 30249053 / python - opencv - drawing - errors - after - manipulating - array - with - numpy
image = image . copy ( )
imh , imw = image . shape [ 0 : 2 ]
thick = int ( ( imh + imw ) // 430 )
for i , _v in enumerate ( coords ) :
    if is_center :
        x , y , x2 , y2 = tl . prepro . obj_box_coord_centroid_to_upleft_butright ( coords [ i ] )
    else :
        x , y , x2 , y2 = coords [ i ]
    if is_rescale : # scale back to pixel unit if the coords are the portion of width and high
        x , y , x2 , y2 = tl . prepro . obj_box_coord_scale_to_pixelunit ( [ x , y , x2 , y2 ] , ( imh , imw ) )
    cv2 . rectangle ( image , ( int ( x ) , int ( y ) ) , ( int ( x2 ) , int ( y2 ) ) , # up - left and botton - right
    [ 0 , 255 , 0 ] , thick )
    cv2 . putText ( image , classes_list [ classes [ i ] ] + ( ( " %.2f" % ( scores [ i ] ) ) if ( len ( scores ) != 0 ) else " " ) , ( int ( x ) , int ( y ) ) , # button left
    0 , 1.5e-3 * imh , # bigger = larger font
    [ 0 , 0 , 256 ] , # self . meta [ ' colors ' ] [ max _ indx ] ,
    int ( thick / 2 ) + 1 )
# bold
if save_name is not None : # cv2 . imwrite ( ' _ my . png ' , image )
    save_image ( image , save_name )
# if len ( coords ) = = 0:
# tl . logging . info ( " draw _ boxes _ and _ labels _ to _ image : no bboxes exist , cannot draw ! " )
return image


signature:
def draw_mpii_pose_to_image ( image , poses , save_name = 'image.png' ) :
    """Draw people ( s ) into image using MPII dataset format as input , return or save the result image .
 This is an experimental API , can be changed in the future .
 Parameters
 image : numpy . array
 The RGB image [ height , width , channel ] .
 poses : list of dict
 The people ( s ) annotation in MPII format , see ` ` tl . files . load _ mpii _ pose _ dataset ` ` .
 save _ name : None or str
 The name of image file ( i . e . image . png ) , if None , not to save image .
 Returns
 numpy . array
 The saved image .
 Examples
 > > > import pprint
 > > > import tensorlayer as tl
 > > > img _ train _ list , ann _ train _ list , img _ test _ list , ann _ test _ list = tl . files . load _ mpii _ pose _ dataset ( )
 > > > image = tl . vis . read _ image ( img _ train _ list [ 0 ] )
 > > > tl . vis . draw _ mpii _ pose _ to _ image ( image , ann _ train _ list [ 0 ] , ' image . png ' )
 > > > pprint . pprint ( ann _ train _ list [ 0 ] )
 References
 - ` MPII Keyponts and ID < http : / / human - pose . mpi - inf . mpg . de / # download > ` _ _"""

implementation:
# import skimage
# don ' t change the original image , and avoid error https : / / stackoverflow . com / questions / 30249053 / python - opencv - drawing - errors - after - manipulating - array - with - numpy
image = image . copy ( )
imh , imw = image . shape [ 0 : 2 ]
thick = int ( ( imh + imw ) // 430 )
# radius = int ( image . shape [ 1 ] / 500 ) + 1
radius = int ( thick * 1.5 )
if image . max ( ) < 1 :
    image = image * 255
for people in poses : # Pose Keyponts
    joint_pos = people [ 'joint_pos' ]
    # draw sketch
    # joint id ( 0 - r ankle , 1 - r knee , 2 - r hip , 3 - l hip , 4 - l knee ,
    # 5 - l ankle , 6 - pelvis , 7 - thorax , 8 - upper neck ,
    # 9 - head top , 10 - r wrist , 11 - r elbow , 12 - r shoulder ,
    # 13 - l shoulder , 14 - l elbow , 15 - l wrist )
    # 12 * * 7 * * 13
    # 11 * 14
    # 10 2 * 6 * 3 15
    # 1 4
    # 0 5
    lines = [ [ ( 0 , 1 ) , [ 100 , 255 , 100 ] ] , [ ( 1 , 2 ) , [ 50 , 255 , 50 ] ] , [ ( 2 , 6 ) , [ 0 , 255 , 0 ] ] , # right leg
    [ ( 3 , 4 ) , [ 100 , 100 , 255 ] ] , [ ( 4 , 5 ) , [ 50 , 50 , 255 ] ] , [ ( 6 , 3 ) , [ 0 , 0 , 255 ] ] , # left leg
    [ ( 6 , 7 ) , [ 255 , 255 , 100 ] ] , [ ( 7 , 8 ) , [ 255 , 150 , 50 ] ] , # body
    [ ( 8 , 9 ) , [ 255 , 200 , 100 ] ] , # head
    [ ( 10 , 11 ) , [ 255 , 100 , 255 ] ] , [ ( 11 , 12 ) , [ 255 , 50 , 255 ] ] , [ ( 12 , 8 ) , [ 255 , 0 , 255 ] ] , # right hand
    [ ( 8 , 13 ) , [ 0 , 255 , 255 ] ] , [ ( 13 , 14 ) , [ 100 , 255 , 255 ] ] , [ ( 14 , 15 ) , [ 200 , 255 , 255 ] ] # left hand
    ]
    for line in lines :
        start , end = line [ 0 ]
        if ( start in joint_pos ) and ( end in joint_pos ) :
            cv2 . line ( image , ( int ( joint_pos [ start ] [ 0 ] ) , int ( joint_pos [ start ] [ 1 ] ) ) , ( int ( joint_pos [ end ] [ 0 ] ) , int ( joint_pos [ end ] [ 1 ] ) ) , # up - left and botton - right
            line [ 1 ] , thick )
            # rr , cc , val = skimage . draw . line _ aa ( int ( joint _ pos [ start ] [ 1 ] ) , int ( joint _ pos [ start ] [ 0 ] ) , int ( joint _ pos [ end ] [ 1 ] ) , int ( joint _ pos [ end ] [ 0 ] ) )
            # image [ rr , cc ] = line [ 1]
    # draw circles
    for pos in joint_pos . items ( ) :
        _ , pos_loc = pos
        # pos _ id , pos _ loc
        pos_loc = ( int ( pos_loc [ 0 ] ) , int ( pos_loc [ 1 ] ) )
        cv2 . circle ( image , center = pos_loc , radius = radius , color = ( 200 , 200 , 200 ) , thickness = - 1 )
        # rr , cc = skimage . draw . circle ( int ( pos _ loc [ 1 ] ) , int ( pos _ loc [ 0 ] ) , radius )
        # image [ rr , cc ] = [ 0 , 255 , 0]
    # Head
    head_rect = people [ 'head_rect' ]
    if head_rect : # if head exists
        cv2 . rectangle ( image , ( int ( head_rect [ 0 ] ) , int ( head_rect [ 1 ] ) ) , ( int ( head_rect [ 2 ] ) , int ( head_rect [ 3 ] ) ) , # up - left and botton - right
        [ 0 , 180 , 0 ] , thick )
if save_name is not None : # cv2 . imwrite ( save _ name , image )
    save_image ( image , save_name )
return image


signature:
def frame ( I = None , second = 5 , saveable = True , name = 'frame' , cmap = None , fig_idx = 12836 ) :
    """Display a frame . Make sure OpenAI Gym render ( ) is disable before using it .
 Parameters
 I : numpy . array
 The image .
 second : int
 The display second ( s ) for the image ( s ) , if saveable is False .
 saveable : boolean
 Save or plot the figure .
 name : str
 A name to save the image , if saveable is True .
 cmap : None or str
 ' gray ' for greyscale , None for default , etc .
 fig _ idx : int
 matplotlib figure index .
 Examples
 > > > env = gym . make ( " Pong - v0 " )
 > > > observation = env . reset ( )
 > > > tl . visualize . frame ( observation )"""

implementation:
import matplotlib . pyplot as plt
if saveable is False :
    plt . ion ( )
plt . figure ( fig_idx )
# show all feature images
if len ( I . shape ) and I . shape [ - 1 ] == 1 : # (10,10,1 ) - - > ( 10,10)
    I = I [ : , : , 0 ]
plt . imshow ( I , cmap )
plt . title ( name )
# plt . gca ( ) . xaxis . set _ major _ locator ( plt . NullLocator ( ) ) # distable tick
# plt . gca ( ) . yaxis . set _ major _ locator ( plt . NullLocator ( ) )
if saveable :
    plt . savefig ( name + '.pdf' , format = 'pdf' )
else :
    plt . draw ( )
    plt . pause ( second )


signature:
def CNN2d ( CNN = None , second = 10 , saveable = True , name = 'cnn' , fig_idx = 3119362 ) :
    """Display a group of RGB or Greyscale CNN masks .
 Parameters
 CNN : numpy . array
 The image . e . g : 64 5x5 RGB images can be ( 5 , 5 , 3 , 64 ) .
 second : int
 The display second ( s ) for the image ( s ) , if saveable is False .
 saveable : boolean
 Save or plot the figure .
 name : str
 A name to save the image , if saveable is True .
 fig _ idx : int
 The matplotlib figure index .
 Examples
 > > > tl . visualize . CNN2d ( network . all _ params [ 0 ] . eval ( ) , second = 10 , saveable = True , name = ' cnn1 _ mnist ' , fig _ idx = 2012)"""

implementation:
import matplotlib . pyplot as plt
# tl . logging . info ( CNN . shape ) # ( 5 , 5 , 3 , 64)
# exit ( )
n_mask = CNN . shape [ 3 ]
n_row = CNN . shape [ 0 ]
n_col = CNN . shape [ 1 ]
n_color = CNN . shape [ 2 ]
row = int ( np . sqrt ( n_mask ) )
col = int ( np . ceil ( n_mask / row ) )
plt . ion ( )
# active mode
fig = plt . figure ( fig_idx )
count = 1
for _ir in range ( 1 , row + 1 ) :
    for _ic in range ( 1 , col + 1 ) :
        if count > n_mask :
            break
        fig . add_subplot ( col , row , count )
        # tl . logging . info ( CNN [ : , : , : , count - 1 ] . shape , n _ row , n _ col ) # ( 5 , 1 , 32 ) 5 5
        # exit ( )
        # plt . imshow (
        # np . reshape ( CNN [ count - 1 , : , : , : ] , ( n _ row , n _ col ) ) ,
        # cmap = ' gray ' , interpolation = " nearest " ) # theano
        if n_color == 1 :
            plt . imshow ( np . reshape ( CNN [ : , : , : , count - 1 ] , ( n_row , n_col ) ) , cmap = 'gray' , interpolation = "nearest" )
        elif n_color == 3 :
            plt . imshow ( np . reshape ( CNN [ : , : , : , count - 1 ] , ( n_row , n_col , n_color ) ) , cmap = 'gray' , interpolation = "nearest" )
        else :
            raise Exception ( "Unknown n_color" )
        plt . gca ( ) . xaxis . set_major_locator ( plt . NullLocator ( ) )
        # distable tick
        plt . gca ( ) . yaxis . set_major_locator ( plt . NullLocator ( ) )
        count = count + 1
if saveable :
    plt . savefig ( name + '.pdf' , format = 'pdf' )
else :
    plt . draw ( )
    plt . pause ( second )


signature:
def tsne_embedding ( embeddings , reverse_dictionary , plot_only = 500 , second = 5 , saveable = False , name = 'tsne' , fig_idx = 9862 ) :
    """Visualize the embeddings by using t - SNE .
 Parameters
 embeddings : numpy . array
 The embedding matrix .
 reverse _ dictionary : dictionary
 id _ to _ word , mapping id to unique word .
 plot _ only : int
 The number of examples to plot , choice the most common words .
 second : int
 The display second ( s ) for the image ( s ) , if saveable is False .
 saveable : boolean
 Save or plot the figure .
 name : str
 A name to save the image , if saveable is True .
 fig _ idx : int
 matplotlib figure index .
 Examples
 > > > see ' tutorial _ word2vec _ basic . py '
 > > > final _ embeddings = normalized _ embeddings . eval ( )
 > > > tl . visualize . tsne _ embedding ( final _ embeddings , labels , reverse _ dictionary ,
 . . . plot _ only = 500 , second = 5 , saveable = False , name = ' tsne ' )"""

implementation:
import matplotlib . pyplot as plt
def plot_with_labels ( low_dim_embs , labels , figsize = ( 18 , 18 ) , second = 5 , saveable = True , name = 'tsne' , fig_idx = 9862 ) :
    if low_dim_embs . shape [ 0 ] < len ( labels ) :
        raise AssertionError ( "More labels than embeddings" )
    if saveable is False :
        plt . ion ( )
        plt . figure ( fig_idx )
    plt . figure ( figsize = figsize )
    # in inches
    for i , label in enumerate ( labels ) :
        x , y = low_dim_embs [ i , : ]
        plt . scatter ( x , y )
        plt . annotate ( label , xy = ( x , y ) , xytext = ( 5 , 2 ) , textcoords = 'offset points' , ha = 'right' , va = 'bottom' )
    if saveable :
        plt . savefig ( name + '.pdf' , format = 'pdf' )
    else :
        plt . draw ( )
        plt . pause ( second )
try :
    from sklearn . manifold import TSNE
    from six . moves import xrange
    tsne = TSNE ( perplexity = 30 , n_components = 2 , init = 'pca' , n_iter = 5000 )
    # plot _ only = 500
    low_dim_embs = tsne . fit_transform ( embeddings [ : plot_only , : ] )
    labels = [ reverse_dictionary [ i ] for i in xrange ( plot_only ) ]
    plot_with_labels ( low_dim_embs , labels , second = second , saveable = saveable , name = name , fig_idx = fig_idx )
except ImportError :
    _err = "Please install sklearn and matplotlib to visualize embeddings."
    tl . logging . error ( _err )
    raise ImportError ( _err )


signature:
def draw_weights ( W = None , second = 10 , saveable = True , shape = None , name = 'mnist' , fig_idx = 2396512 ) :
    """Visualize every columns of the weight matrix to a group of Greyscale img .
 Parameters
 W : numpy . array
 The weight matrix
 second : int
 The display second ( s ) for the image ( s ) , if saveable is False .
 saveable : boolean
 Save or plot the figure .
 shape : a list with 2 int or None
 The shape of feature image , MNIST is [ 28 , 80 ] .
 name : a string
 A name to save the image , if saveable is True .
 fig _ idx : int
 matplotlib figure index .
 Examples
 > > > tl . visualize . draw _ weights ( network . all _ params [ 0 ] . eval ( ) , second = 10 , saveable = True , name = ' weight _ of _ 1st _ layer ' , fig _ idx = 2012)"""

implementation:
if shape is None :
    shape = [ 28 , 28 ]
import matplotlib . pyplot as plt
if saveable is False :
    plt . ion ( )
fig = plt . figure ( fig_idx )
# show all feature images
n_units = W . shape [ 1 ]
num_r = int ( np . sqrt ( n_units ) )
# 每行显示的个数 若25个hidden unit - > 每行显示5个
num_c = int ( np . ceil ( n_units / num_r ) )
count = int ( 1 )
for _row in range ( 1 , num_r + 1 ) :
    for _col in range ( 1 , num_c + 1 ) :
        if count > n_units :
            break
        fig . add_subplot ( num_r , num_c , count )
        # plt . imshow ( np . reshape ( W [ : , count - 1 ] , ( 28,28 ) ) , cmap = ' gray ' )
        feature = W [ : , count - 1 ] / np . sqrt ( ( W [ : , count - 1 ] ** 2 ) . sum ( ) )
        # feature [ feature < 0.0001 ] = 0 # value threshold
        # if count = = 1 or count = = 2:
        # print ( np . mean ( feature ) )
        # if np . std ( feature ) < 0.03 : # condition threshold
        # feature = np . zeros _ like ( feature )
        # if np . mean ( feature ) < - 0.015 : # condition threshold
        # feature = np . zeros _ like ( feature )
        plt . imshow ( np . reshape ( feature , ( shape [ 0 ] , shape [ 1 ] ) ) , cmap = 'gray' , interpolation = "nearest" )
        # , vmin = np . min ( feature ) , vmax = np . max ( feature ) )
        # plt . title ( name )
        # plt . imshow ( np . reshape ( W [ : , count - 1 ] , ( np . sqrt ( size ) , np . sqrt ( size ) ) ) , cmap = ' gray ' , interpolation = " nearest " )
        plt . gca ( ) . xaxis . set_major_locator ( plt . NullLocator ( ) )
        # distable tick
        plt . gca ( ) . yaxis . set_major_locator ( plt . NullLocator ( ) )
        count = count + 1
if saveable :
    plt . savefig ( name + '.pdf' , format = 'pdf' )
else :
    plt . draw ( )
    plt . pause ( second )


signature:
def data_to_tfrecord ( images , labels , filename ) :
    """Save data into TFRecord ."""

implementation:
if os . path . isfile ( filename ) :
    print ( "%s exists" % filename )
    return
print ( "Converting data into %s ..." % filename )
# cwd = os . getcwd ( )
writer = tf . python_io . TFRecordWriter ( filename )
for index , img in enumerate ( images ) :
    img_raw = img . tobytes ( )
    # Visualize a image
    # tl . visualize . frame ( np . asarray ( img , dtype = np . uint8 ) , second = 1 , saveable = False , name = ' frame ' , fig _ idx = 1236)
    label = int ( labels [ index ] )
    # print ( label )
    # Convert the bytes back to image as follow :
    # image = Image . frombytes ( ' RGB ' , ( 32 , 32 ) , img _ raw )
    # image = np . fromstring ( img _ raw , np . float32)
    # image = image . reshape ( [ 32 , 32 , 3 ] )
    # tl . visualize . frame ( np . asarray ( image , dtype = np . uint8 ) , second = 1 , saveable = False , name = ' frame ' , fig _ idx = 1236)
    example = tf . train . Example ( features = tf . train . Features ( feature = { "label" : tf . train . Feature ( int64_list = tf . train . Int64List ( value = [ label ] ) ) , 'img_raw' : tf . train . Feature ( bytes_list = tf . train . BytesList ( value = [ img_raw ] ) ) , } ) )
    writer . write ( example . SerializeToString ( ) )
    # Serialize To String
writer . close ( )


signature:
def read_and_decode ( filename , is_train = None ) :
    """Return tensor to read from TFRecord ."""

implementation:
filename_queue = tf . train . string_input_producer ( [ filename ] )
reader = tf . TFRecordReader ( )
_ , serialized_example = reader . read ( filename_queue )
features = tf . parse_single_example ( serialized_example , features = { 'label' : tf . FixedLenFeature ( [ ] , tf . int64 ) , 'img_raw' : tf . FixedLenFeature ( [ ] , tf . string ) , } )
# You can do more image distortion here for training data
img = tf . decode_raw ( features [ 'img_raw' ] , tf . float32 )
img = tf . reshape ( img , [ 32 , 32 , 3 ] )
# img = tf . cast ( img , tf . float32 ) # * ( 1 . / 255 ) - 0.5
if is_train == True : # 1 . Randomly crop a [ height , width ] section of the image .
    img = tf . random_crop ( img , [ 24 , 24 , 3 ] )
    # 2 . Randomly flip the image horizontally .
    img = tf . image . random_flip_left_right ( img )
    # 3 . Randomly change brightness .
    img = tf . image . random_brightness ( img , max_delta = 63 )
    # 4 . Randomly change contrast .
    img = tf . image . random_contrast ( img , lower = 0.2 , upper = 1.8 )
    # 5 . Subtract off the mean and divide by the variance of the pixels .
    img = tf . image . per_image_standardization ( img )
elif is_train == False : # 1 . Crop the central [ height , width ] of the image .
    img = tf . image . resize_image_with_crop_or_pad ( img , 24 , 24 )
    # 2 . Subtract off the mean and divide by the variance of the pixels .
    img = tf . image . per_image_standardization ( img )
elif is_train == None :
    img = img
label = tf . cast ( features [ 'label' ] , tf . int32 )
return img , label


signature:
def print_params ( self , details = True , session = None ) :
    """Print all info of parameters in the network"""

implementation:
for i , p in enumerate ( self . all_params ) :
    if details :
        try :
            val = p . eval ( session = session )
            logging . info ( "  param {:3}: {:20} {:15}    {} (mean: {:<18}, median: {:<18}, std: {:<18})   " . format ( i , p . name , str ( val . shape ) , p . dtype . name , val . mean ( ) , np . median ( val ) , val . std ( ) ) )
        except Exception as e :
            logging . info ( str ( e ) )
            raise Exception ( "Hint: print params details after tl.layers.initialize_global_variables(sess) " "or use network.print_params(False)." )
    else :
        logging . info ( "  param {:3}: {:20} {:15}    {}" . format ( i , p . name , str ( p . get_shape ( ) ) , p . dtype . name ) )
logging . info ( "  num of params: %d" % self . count_params ( ) )


signature:
def print_layers ( self ) :
    """Print all info of layers in the network ."""

implementation:
for i , layer in enumerate ( self . all_layers ) : # logging . info ( " layer % d : % s " % ( i , str ( layer ) ) )
    logging . info ( "  layer {:3}: {:20} {:15}    {}" . format ( i , layer . name , str ( layer . get_shape ( ) ) , layer . dtype . name ) )


signature:
def count_params ( self ) :
    """Returns the number of parameters in the network ."""

implementation:
n_params = 0
for _i , p in enumerate ( self . all_params ) :
    n = 1
    # for s in p . eval ( ) . shape :
    for s in p . get_shape ( ) :
        try :
            s = int ( s )
        except Exception :
            s = 1
        if s :
            n = n * s
    n_params = n_params + n
return n_params


signature:
def get_all_params ( self , session = None ) :
    """Return the parameters in a list of array ."""

implementation:
_params = [ ]
for p in self . all_params :
    if session is None :
        _params . append ( p . eval ( ) )
    else :
        _params . append ( session . run ( p ) )
return _params


signature:
def _get_init_args ( self , skip = 4 ) :
    """Get all arguments of current layer for saving the graph ."""

implementation:
stack = inspect . stack ( )
if len ( stack ) < skip + 1 :
    raise ValueError ( "The length of the inspection stack is shorter than the requested start position." )
args , _ , _ , values = inspect . getargvalues ( stack [ skip ] [ 0 ] )
params = { }
for arg in args : # some args dont need to be saved into the graph . e . g . the input placeholder
    if values [ arg ] is not None and arg not in [ 'self' , 'prev_layer' , 'inputs' ] :
        val = values [ arg ]
        # change function ( e . g . act ) into dictionary of module path and function name
        if inspect . isfunction ( val ) :
            params [ arg ] = { "module_path" : val . __module__ , "func_name" : val . __name__ }
        # ignore more args e . g . TF class
        elif arg . endswith ( 'init' ) :
            continue
        # for other data type , save them directly
        else :
            params [ arg ] = val
return params


signature:
def roi_pooling ( input , rois , pool_height , pool_width ) :
    """returns a tensorflow operation for computing the Region of Interest Pooling
 @ arg input : feature maps on which to perform the pooling operation
 @ arg rois : list of regions of interest in the format ( feature map index , upper left , bottom right )
 @ arg pool _ width : size of the pooling sections"""

implementation:
# TODO ( maciek ) : ops scope
out = roi_pooling_module . roi_pooling ( input , rois , pool_height = pool_height , pool_width = pool_width )
output , argmax_output = out [ 0 ] , out [ 1 ]
return output


signature:
def _int64_feature ( value ) :
    """Wrapper for inserting an int64 Feature into a SequenceExample proto ,
 e . g , An integer label ."""

implementation: return tf . train . Feature ( int64_list = tf . train . Int64List ( value = [ value ] ) )


signature:
def _bytes_feature ( value ) :
    """Wrapper for inserting a bytes Feature into a SequenceExample proto ,
 e . g , an image in byte"""

implementation:
# return tf . train . Feature ( bytes _ list = tf . train . BytesList ( value = [ str ( value ) ] ) )
return tf . train . Feature ( bytes_list = tf . train . BytesList ( value = [ value ] ) )


signature:
def _int64_feature_list ( values ) :
    """Wrapper for inserting an int64 FeatureList into a SequenceExample proto ,
 e . g , sentence in list of ints"""

implementation: return tf . train . FeatureList ( feature = [ _int64_feature ( v ) for v in values ] )


signature:
def _bytes_feature_list ( values ) :
    """Wrapper for inserting a bytes FeatureList into a SequenceExample proto ,
 e . g , sentence in list of bytes"""

implementation: return tf . train . FeatureList ( feature = [ _bytes_feature ( v ) for v in values ] )


signature:
def distort_image ( image , thread_id ) :
    """Perform random distortions on an image .
 Args :
 image : A float32 Tensor of shape [ height , width , 3 ] with values in [ 0 , 1 ) .
 thread _ id : Preprocessing thread id used to select the ordering of color
 distortions . There should be a multiple of 2 preprocessing threads .
 Returns : ` ` ` `
 distorted _ image : A float32 Tensor of shape [ height , width , 3 ] with values in
 [0 , 1 ] ."""

implementation:
# Randomly flip horizontally .
with tf . name_scope ( "flip_horizontal" ) : # , values = [ image ] ) : # DH MOdify
# with tf . name _ scope ( " flip _ horizontal " , values = [ image ] ) :
    image = tf . image . random_flip_left_right ( image )
# Randomly distort the colors based on thread id .
color_ordering = thread_id % 2
with tf . name_scope ( "distort_color" ) : # , values = [ image ] ) : # DH MOdify
# with tf . name _ scope ( " distort _ color " , values = [ image ] ) : # DH MOdify
    if color_ordering == 0 :
        image = tf . image . random_brightness ( image , max_delta = 32. / 255. )
        image = tf . image . random_saturation ( image , lower = 0.5 , upper = 1.5 )
        image = tf . image . random_hue ( image , max_delta = 0.032 )
        image = tf . image . random_contrast ( image , lower = 0.5 , upper = 1.5 )
    elif color_ordering == 1 :
        image = tf . image . random_brightness ( image , max_delta = 32. / 255. )
        image = tf . image . random_contrast ( image , lower = 0.5 , upper = 1.5 )
        image = tf . image . random_saturation ( image , lower = 0.5 , upper = 1.5 )
        image = tf . image . random_hue ( image , max_delta = 0.032 )
    # The random _ * ops do not necessarily clamp .
    image = tf . clip_by_value ( image , 0.0 , 1.0 )
return image


signature:
def prefetch_input_data ( reader , file_pattern , is_training , batch_size , values_per_shard , input_queue_capacity_factor = 16 , num_reader_threads = 1 , shard_queue_name = "filename_queue" , value_queue_name = "input_queue" ) :
    """Prefetches string values from disk into an input queue .
 In training the capacity of the queue is important because a larger queue
 means better mixing of training examples between shards . The minimum number of
 values kept in the queue is values _ per _ shard * input _ queue _ capacity _ factor ,
 where input _ queue _ memory factor should be chosen to trade - off better mixing
 with memory usage .
 Args :
 reader : Instance of tf . ReaderBase .
 file _ pattern : Comma - separated list of file patterns ( e . g .
 / tmp / train _ data - ? ? ? ? ? - of - 00100 ) .
 is _ training : Boolean ; whether prefetching for training or eval .
 batch _ size : Model batch size used to determine queue capacity .
 values _ per _ shard : Approximate number of values per shard .
 input _ queue _ capacity _ factor : Minimum number of values to keep in the queue
 in multiples of values _ per _ shard . See comments above .
 num _ reader _ threads : Number of reader threads to fill the queue .
 shard _ queue _ name : Name for the shards filename queue .
 value _ queue _ name : Name for the values input queue .
 Returns :
 A Queue containing prefetched string values ."""

implementation:
data_files = [ ]
for pattern in file_pattern . split ( "," ) :
    data_files . extend ( tf . gfile . Glob ( pattern ) )
if not data_files :
    tl . logging . fatal ( "Found no input files matching %s" , file_pattern )
else :
    tl . logging . info ( "Prefetching values from %d files matching %s" , len ( data_files ) , file_pattern )
if is_training :
    print ( "   is_training == True : RandomShuffleQueue" )
    filename_queue = tf . train . string_input_producer ( data_files , shuffle = True , capacity = 16 , name = shard_queue_name )
    min_queue_examples = values_per_shard * input_queue_capacity_factor
    capacity = min_queue_examples + 100 * batch_size
    values_queue = tf . RandomShuffleQueue ( capacity = capacity , min_after_dequeue = min_queue_examples , dtypes = [ tf . string ] , name = "random_" + value_queue_name )
else :
    print ( "   is_training == False : FIFOQueue" )
    filename_queue = tf . train . string_input_producer ( data_files , shuffle = False , capacity = 1 , name = shard_queue_name )
    capacity = values_per_shard + 3 * batch_size
    values_queue = tf . FIFOQueue ( capacity = capacity , dtypes = [ tf . string ] , name = "fifo_" + value_queue_name )
enqueue_ops = [ ]
for _ in range ( num_reader_threads ) :
    _ , value = reader . read ( filename_queue )
    enqueue_ops . append ( values_queue . enqueue ( [ value ] ) )
tf . train . queue_runner . add_queue_runner ( tf . train . queue_runner . QueueRunner ( values_queue , enqueue_ops ) )
tf . summary . scalar ( "queue/%s/fraction_of_%d_full" % ( values_queue . name , capacity ) , tf . cast ( values_queue . size ( ) , tf . float32 ) * ( 1. / capacity ) )
return values_queue


signature:
def batch_with_dynamic_pad ( images_and_captions , batch_size , queue_capacity , add_summaries = True ) :
    """Batches input images and captions .
 This function splits the caption into an input sequence and a target sequence ,
 where the target sequence is the input sequence right - shifted by 1 . Input and
 target sequences are batched and padded up to the maximum length of sequences
 in the batch . A mask is created to distinguish real words from padding words .
 Example :
 Actual captions in the batch ( ' - ' denotes padded character ) :
 [ 1 2 5 4 5 ] ,
 [ 1 2 3 4 - ] ,
 [ 1 2 3 - - ] ,
 input _ seqs :
 [ 1 2 3 4 ] ,
 [ 1 2 3 - ] ,
 [ 1 2 - - ] ,
 target _ seqs :
 [ 2 3 4 5 ] ,
 [ 2 3 4 - ] ,
 [ 2 3 - - ] ,
 mask :
 [ 1 1 1 1 ] ,
 [ 1 1 1 0 ] ,
 [ 1 1 0 0 ] ,
 Args :
 images _ and _ captions : A list of pairs [ image , caption ] , where image is a
 Tensor of shape [ height , width , channels ] and caption is a 1 - D Tensor of
 any length . Each pair will be processed and added to the queue in a
 separate thread .
 batch _ size : Batch size .
 queue _ capacity : Queue capacity .
 add _ summaries : If true , add caption length summaries .
 Returns :
 images : A Tensor of shape [ batch _ size , height , width , channels ] .
 input _ seqs : An int32 Tensor of shape [ batch _ size , padded _ length ] .
 target _ seqs : An int32 Tensor of shape [ batch _ size , padded _ length ] .
 mask : An int32 0/1 Tensor of shape [ batch _ size , padded _ length ] ."""

implementation:
enqueue_list = [ ]
for image , caption in images_and_captions :
    caption_length = tf . shape ( caption ) [ 0 ]
    input_length = tf . expand_dims ( tf . subtract ( caption_length , 1 ) , 0 )
    input_seq = tf . slice ( caption , [ 0 ] , input_length )
    target_seq = tf . slice ( caption , [ 1 ] , input_length )
    indicator = tf . ones ( input_length , dtype = tf . int32 )
    enqueue_list . append ( [ image , input_seq , target_seq , indicator ] )
images , input_seqs , target_seqs , mask = tf . train . batch_join ( enqueue_list , batch_size = batch_size , capacity = queue_capacity , dynamic_pad = True , name = "batch_and_pad" )
if add_summaries :
    lengths = tf . add ( tf . reduce_sum ( mask , 1 ) , 1 )
    tf . summary . scalar ( "caption_length/batch_min" , tf . reduce_min ( lengths ) )
    tf . summary . scalar ( "caption_length/batch_max" , tf . reduce_max ( lengths ) )
    tf . summary . scalar ( "caption_length/batch_mean" , tf . reduce_mean ( lengths ) )
return images , input_seqs , target_seqs , mask


signature:
def _to_channel_first_bias ( b ) :
    """Reshape [ c ] to [ c , 1 , 1 ] ."""

implementation:
channel_size = int ( b . shape [ 0 ] )
new_shape = ( channel_size , 1 , 1 )
# new _ shape = [ - 1 , 1 , 1 ] # doesn ' t work with tensorRT
return tf . reshape ( b , new_shape )


signature:
def _bias_scale ( x , b , data_format ) :
    """The multiplication counter part of tf . nn . bias _ add ."""

implementation:
if data_format == 'NHWC' :
    return x * b
elif data_format == 'NCHW' :
    return x * _to_channel_first_bias ( b )
else :
    raise ValueError ( 'invalid data_format: %s' % data_format )


signature:
def _bias_add ( x , b , data_format ) :
    """Alternative implementation of tf . nn . bias _ add which is compatiable with tensorRT ."""

implementation:
if data_format == 'NHWC' :
    return tf . add ( x , b )
elif data_format == 'NCHW' :
    return tf . add ( x , _to_channel_first_bias ( b ) )
else :
    raise ValueError ( 'invalid data_format: %s' % data_format )


signature:
def batch_normalization ( x , mean , variance , offset , scale , variance_epsilon , data_format , name = None ) :
    """Data Format aware version of tf . nn . batch _ normalization ."""

implementation:
with ops . name_scope ( name , 'batchnorm' , [ x , mean , variance , scale , offset ] ) :
    inv = math_ops . rsqrt ( variance + variance_epsilon )
    if scale is not None :
        inv *= scale
    a = math_ops . cast ( inv , x . dtype )
    b = math_ops . cast ( offset - mean * inv if offset is not None else - mean * inv , x . dtype )
    # Return a * x + b with customized data _ format .
    # Currently TF doesn ' t have bias _ scale , and tensorRT has bug in converting tf . nn . bias _ add
    # So we reimplemted them to allow make the model work with tensorRT .
    # See https : / / github . com / tensorlayer / openpose - plus / issues / 75 for more details .
    df = { 'channels_first' : 'NCHW' , 'channels_last' : 'NHWC' }
    return _bias_add ( _bias_scale ( x , a , df [ data_format ] ) , b , df [ data_format ] )


signature:
def compute_alpha ( x ) :
    """Computing the scale parameter ."""

implementation:
threshold = _compute_threshold ( x )
alpha1_temp1 = tf . where ( tf . greater ( x , threshold ) , x , tf . zeros_like ( x , tf . float32 ) )
alpha1_temp2 = tf . where ( tf . less ( x , - threshold ) , x , tf . zeros_like ( x , tf . float32 ) )
alpha_array = tf . add ( alpha1_temp1 , alpha1_temp2 , name = None )
alpha_array_abs = tf . abs ( alpha_array )
alpha_array_abs1 = tf . where ( tf . greater ( alpha_array_abs , 0 ) , tf . ones_like ( alpha_array_abs , tf . float32 ) , tf . zeros_like ( alpha_array_abs , tf . float32 ) )
alpha_sum = tf . reduce_sum ( alpha_array_abs )
n = tf . reduce_sum ( alpha_array_abs1 )
alpha = tf . div ( alpha_sum , n )
return alpha


signature:
def flatten_reshape ( variable , name = 'flatten' ) :
    """Reshapes a high - dimension vector input .
 [ batch _ size , mask _ row , mask _ col , n _ mask ] - - - > [ batch _ size , mask _ row x mask _ col x n _ mask ]
 Parameters
 variable : TensorFlow variable or tensor
 The variable or tensor to be flatten .
 name : str
 A unique layer name .
 Returns
 Tensor
 Flatten Tensor
 Examples
 > > > import tensorflow as tf
 > > > import tensorlayer as tl
 > > > x = tf . placeholder ( tf . float32 , [ None , 128 , 128 , 3 ] )
 > > > # Convolution Layer with 32 filters and a kernel size of 5
 > > > network = tf . layers . conv2d ( x , 32 , 5 , activation = tf . nn . relu )
 > > > # Max Pooling ( down - sampling ) with strides of 2 and kernel size of 2
 > > > network = tf . layers . max _ pooling2d ( network , 2 , 2)
 > > > print ( network . get _ shape ( ) [ : ] . as _ list ( ) )
 > > > [ None , 62 , 62 , 32]
 > > > network = tl . layers . flatten _ reshape ( network )
 > > > print ( network . get _ shape ( ) [ : ] . as _ list ( ) [ 1 : ] )
 > > > [ None , 123008]"""

implementation:
dim = 1
for d in variable . get_shape ( ) [ 1 : ] . as_list ( ) :
    dim *= d
return tf . reshape ( variable , shape = [ - 1 , dim ] , name = name )


signature:
def get_layers_with_name ( net , name = "" , verbose = False ) :
    """Get a list of layers ' output in a network by a given name scope .
 Parameters
 net : : class : ` Layer `
 The last layer of the network .
 name : str
 Get the layers ' output that contain this name .
 verbose : boolean
 If True , print information of all the layers ' output
 Returns
 list of Tensor
 A list of layers ' output ( TensorFlow tensor )
 Examples
 > > > import tensorlayer as tl
 > > > layers = tl . layers . get _ layers _ with _ name ( net , " CNN " , True )"""

implementation:
logging . info ( "  [*] geting layers with %s" % name )
layers = [ ]
i = 0
for layer in net . all_layers : # logging . info ( type ( layer . name ) )
    if name in layer . name :
        layers . append ( layer )
        if verbose :
            logging . info ( "  got {:3}: {:15}   {}" . format ( i , layer . name , str ( layer . get_shape ( ) ) ) )
            i = i + 1
return layers


signature:
def get_variables_with_name ( name = None , train_only = True , verbose = False ) :
    """Get a list of TensorFlow variables by a given name scope .
 Parameters
 name : str
 Get the variables that contain this name .
 train _ only : boolean
 If Ture , only get the trainable variables .
 verbose : boolean
 If True , print the information of all variables .
 Returns
 list of Tensor
 A list of TensorFlow variables
 Examples
 > > > import tensorlayer as tl
 > > > dense _ vars = tl . layers . get _ variables _ with _ name ( ' dense ' , True , True )"""

implementation:
if name is None :
    raise Exception ( "please input a name" )
logging . info ( "  [*] geting variables with %s" % name )
# tvar = tf . trainable _ variables ( ) if train _ only else tf . all _ variables ( )
if train_only :
    t_vars = tf . trainable_variables ( )
else :
    t_vars = tf . global_variables ( )
d_vars = [ var for var in t_vars if name in var . name ]
if verbose :
    for idx , v in enumerate ( d_vars ) :
        logging . info ( "  got {:3}: {:15}   {}" . format ( idx , v . name , str ( v . get_shape ( ) ) ) )
return d_vars


signature:
def initialize_rnn_state ( state , feed_dict = None ) :
    """Returns the initialized RNN state .
 The inputs are ` LSTMStateTuple ` or ` State ` of ` RNNCells ` , and an optional ` feed _ dict ` .
 Parameters
 state : RNN state .
 The TensorFlow ' s RNN state .
 feed _ dict : dictionary
 Initial RNN state ; if None , returns zero state .
 Returns
 RNN state
 The TensorFlow ' s RNN state ."""

implementation:
if isinstance ( state , LSTMStateTuple ) :
    c = state . c . eval ( feed_dict = feed_dict )
    h = state . h . eval ( feed_dict = feed_dict )
    return c , h
else :
    new_state = state . eval ( feed_dict = feed_dict )
    return new_state


signature:
def list_remove_repeat ( x ) :
    """Remove the repeated items in a list , and return the processed list .
 You may need it to create merged layer like Concat , Elementwise and etc .
 Parameters
 x : list
 Input
 Returns
 list
 A list that after removing it ' s repeated items
 Examples
 > > > l = [ 2 , 3 , 4 , 2 , 3]
 > > > l = list _ remove _ repeat ( l )
 [2 , 3 , 4]"""

implementation:
y = [ ]
for i in x :
    if i not in y :
        y . append ( i )
return y


signature:
def merge_networks ( layers = None ) :
    """Merge all parameters , layers and dropout probabilities to a : class : ` Layer ` .
 The output of return network is the first network in the list .
 Parameters
 layers : list of : class : ` Layer `
 Merge all parameters , layers and dropout probabilities to the first layer in the list .
 Returns
 : class : ` Layer `
 The network after merging all parameters , layers and dropout probabilities to the first network in the list .
 Examples
 > > > import tensorlayer as tl
 > > > n1 = . . .
 > > > n2 = . . .
 > > > n1 = tl . layers . merge _ networks ( [ n1 , n2 ] )"""

implementation:
if layers is None :
    raise Exception ( "layers should be a list of TensorLayer's Layers." )
layer = layers [ 0 ]
all_params = [ ]
all_layers = [ ]
all_drop = { }
for l in layers :
    all_params . extend ( l . all_params )
    all_layers . extend ( l . all_layers )
    all_drop . update ( l . all_drop )
layer . all_params = list ( all_params )
layer . all_layers = list ( all_layers )
layer . all_drop = dict ( all_drop )
layer . all_layers = list_remove_repeat ( layer . all_layers )
layer . all_params = list_remove_repeat ( layer . all_params )
return layer


signature:
def print_all_variables ( train_only = False ) :
    """Print information of trainable or all variables ,
 without ` ` tl . layers . initialize _ global _ variables ( sess ) ` ` .
 Parameters
 train _ only : boolean
 Whether print trainable variables only .
 - If True , print the trainable variables .
 - If False , print all variables ."""

implementation:
# tvar = tf . trainable _ variables ( ) if train _ only else tf . all _ variables ( )
if train_only :
    t_vars = tf . trainable_variables ( )
    logging . info ( "  [*] printing trainable variables" )
else :
    t_vars = tf . global_variables ( )
    logging . info ( "  [*] printing global variables" )
for idx , v in enumerate ( t_vars ) :
    logging . info ( "  var {:3}: {:15}   {}" . format ( idx , str ( v . get_shape ( ) ) , v . name ) )


signature:
def ternary_operation ( x ) :
    """Ternary operation use threshold computed with weights ."""

implementation:
g = tf . get_default_graph ( )
with g . gradient_override_map ( { "Sign" : "Identity" } ) :
    threshold = _compute_threshold ( x )
    x = tf . sign ( tf . add ( tf . sign ( tf . add ( x , threshold ) ) , tf . sign ( tf . add ( x , - threshold ) ) ) )
    return x


signature:
def _compute_threshold ( x ) :
    """ref : https : / / github . com / XJTUWYD / TWN
 Computing the threshold ."""

implementation:
x_sum = tf . reduce_sum ( tf . abs ( x ) , reduction_indices = None , keepdims = False , name = None )
threshold = tf . div ( x_sum , tf . cast ( tf . size ( x ) , tf . float32 ) , name = None )
threshold = tf . multiply ( 0.7 , threshold , name = None )
return threshold


signature:
def freeze_graph ( graph_path , checkpoint_path , output_path , end_node_names , is_binary_graph ) :
    """Reimplementation of the TensorFlow official freeze _ graph function to freeze the graph and checkpoint together :
 Parameters
 graph _ path : string
 the path where your graph file save .
 checkpoint _ output _ path : string
 the path where your checkpoint save .
 output _ path : string
 the path where you want to save the output proto buff
 end _ node _ names : string
 the name of the end node in your graph you want to get in your proto buff
 is _ binary _ graph : boolean
 declare your file whether is a binary graph
 References
 - ` onnx - tf exporting tutorial < https : / / github . com / onnx / tutorials / blob / master / tutorials / OnnxTensorflowExport . ipynb > ` _ _
 - ` tensorflow freeze _ graph < https : / / github . com / tensorflow / tensorflow / blob / master / tensorflow / python / tools / freeze _ graph . py > `"""

implementation: _freeze_graph ( input_graph = graph_path , input_saver = '' , input_binary = is_binary_graph , input_checkpoint = checkpoint_path , output_graph = output_path , output_node_names = end_node_names , restore_op_name = 'save/restore_all' , filename_tensor_name = 'save/Const:0' , clear_devices = True , initializer_nodes = None )


signature:
def convert_model_to_onnx ( frozen_graph_path , end_node_names , onnx_output_path ) :
    """Reimplementation of the TensorFlow - onnx official tutorial convert the proto buff to onnx file :
 Parameters
 frozen _ graph _ path : string
 the path where your frozen graph file save .
 end _ node _ names : string
 the name of the end node in your graph you want to get in your proto buff
 onnx _ output _ path : string
 the path where you want to save the onnx file .
 References
 - ` onnx - tf exporting tutorial < https : / / github . com / onnx / tutorials / blob / master / tutorials / OnnxTensorflowExport . ipynb > `"""

implementation:
with tf . gfile . GFile ( frozen_graph_path , "rb" ) as f :
    graph_def = tf . GraphDef ( )
    graph_def . ParseFromString ( f . read ( ) )
    onnx_model = tensorflow_graph_to_onnx_model ( graph_def , end_node_names , opset = 6 )
    file = open ( onnx_output_path , "wb" )
    file . write ( onnx_model . SerializeToString ( ) )
    file . close ( )


signature:
def convert_onnx_to_model ( onnx_input_path ) :
    """Reimplementation of the TensorFlow - onnx official tutorial convert the onnx file to specific : model
 Parameters
 onnx _ input _ path : string
 the path where you save the onnx file .
 References
 - ` onnx - tf exporting tutorial < https : / / github . com / onnx / tutorials / blob / master / tutorials / OnnxTensorflowExport . ipynb > ` _ _"""

implementation:
model = onnx . load ( onnx_input_path )
tf_rep = prepare ( model )
# Image Path
img = np . load ( "./assets/image.npz" )
output = tf_rep . run ( img . reshape ( [ 1 , 784 ] ) )
print ( "The digit is classified as " , np . argmax ( output ) )


signature:
def _add_deprecated_function_notice_to_docstring ( doc , date , instructions ) :
    """Adds a deprecation notice to a docstring for deprecated functions ."""

implementation:
if instructions :
    deprecation_message = """
            .. warning::
                **THIS FUNCTION IS DEPRECATED:** It will be removed after %s.
                *Instructions for updating:* %s.
        """ % ( ( 'in a future version' if date is None else ( 'after %s' % date ) ) , instructions )
else :
    deprecation_message = """
            .. warning::
                **THIS FUNCTION IS DEPRECATED:** It will be removed after %s.
        """ % ( ( 'in a future version' if date is None else ( 'after %s' % date ) ) )
main_text = [ deprecation_message ]
return _add_notice_to_docstring ( doc , 'DEPRECATED FUNCTION' , main_text )


signature:
def _add_notice_to_docstring ( doc , no_doc_str , notice ) :
    """Adds a deprecation notice to a docstring ."""

implementation:
if not doc :
    lines = [ no_doc_str ]
else :
    lines = _normalize_docstring ( doc ) . splitlines ( )
notice = [ '' ] + notice
if len ( lines ) > 1 : # Make sure that we keep our distance from the main body
    if lines [ 1 ] . strip ( ) :
        notice . append ( '' )
    lines [ 1 : 1 ] = notice
else :
    lines += notice
return '\n' . join ( lines )


signature:
def alphas ( shape , alpha_value , name = None ) :
    """Creates a tensor with all elements set to ` alpha _ value ` .
 This operation returns a tensor of type ` dtype ` with shape ` shape ` and all
 elements set to alpha .
 Parameters
 shape : A list of integers , a tuple of integers , or a 1 - D ` Tensor ` of type ` int32 ` .
 The shape of the desired tensor
 alpha _ value : ` float32 ` , ` float64 ` , ` int8 ` , ` uint8 ` , ` int16 ` , ` uint16 ` , int32 ` , ` int64 `
 The value used to fill the resulting ` Tensor ` .
 name : str
 A name for the operation ( optional ) .
 Returns
 A ` Tensor ` with all elements set to alpha .
 Examples
 > > > tl . alphas ( [ 2 , 3 ] , tf . int32 ) # [ [ alpha , alpha , alpha ] , [ alpha , alpha , alpha ] ]"""

implementation:
with ops . name_scope ( name , "alphas" , [ shape ] ) as name :
    alpha_tensor = convert_to_tensor ( alpha_value )
    alpha_dtype = dtypes . as_dtype ( alpha_tensor . dtype ) . base_dtype
    if not isinstance ( shape , ops . Tensor ) :
        try :
            shape = constant_op . _tensor_shape_tensor_conversion_function ( tensor_shape . TensorShape ( shape ) )
        except ( TypeError , ValueError ) :
            shape = ops . convert_to_tensor ( shape , dtype = dtypes . int32 )
    if not shape . _shape_tuple ( ) :
        shape = reshape ( shape , [ - 1 ] )
    # Ensure it ' s a vector
    try :
        output = constant ( alpha_value , shape = shape , dtype = alpha_dtype , name = name )
    except ( TypeError , ValueError ) :
        output = fill ( shape , constant ( alpha_value , dtype = alpha_dtype ) , name = name )
    if output . dtype . base_dtype != alpha_dtype :
        raise AssertionError ( "Dtypes do not corresponds: %s and %s" % ( output . dtype . base_dtype , alpha_dtype ) )
    return output


signature:
def alphas_like ( tensor , alpha_value , name = None , optimize = True ) :
    """Creates a tensor with all elements set to ` alpha _ value ` .
 Given a single tensor ( ` tensor ` ) , this operation returns a tensor of the same
 type and shape as ` tensor ` with all elements set to ` alpha _ value ` .
 Parameters
 tensor : tf . Tensor
 The Tensorflow Tensor that will be used as a template .
 alpha _ value : ` float32 ` , ` float64 ` , ` int8 ` , ` uint8 ` , ` int16 ` , ` uint16 ` , int32 ` , ` int64 `
 The value used to fill the resulting ` Tensor ` .
 name : str
 A name for the operation ( optional ) .
 optimize : bool
 if true , attempt to statically determine the shape of ' tensor ' and encode it as a constant .
 Returns
 A ` Tensor ` with all elements set to ` alpha _ value ` .
 Examples
 > > > tensor = tf . constant ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
 > > > tl . alphas _ like ( tensor , 0.5 ) # [ [ 0.5 , 0.5 , 0.5 ] , [ 0.5 , 0.5 , 0.5 ] ]"""

implementation:
with ops . name_scope ( name , "alphas_like" , [ tensor ] ) as name :
    tensor = ops . convert_to_tensor ( tensor , name = "tensor" )
    if context . in_eager_mode ( ) : # and dtype is not None and dtype ! = tensor . dtype :
        ret = alphas ( shape_internal ( tensor , optimize = optimize ) , alpha_value = alpha_value , name = name )
    else : # if context . in _ graph _ mode ( ) :
    # For now , variant types must be created via zeros _ like ; as we need to
    # pass the input variant object to the proper zeros callback .
        if ( optimize and tensor . shape . is_fully_defined ( ) ) : # We can produce a zeros tensor independent of the value of ' tensor ' ,
        # since the shape is known statically .
            ret = alphas ( tensor . shape , alpha_value = alpha_value , name = name )
        # elif dtype is not None and dtype ! = tensor . dtype and dtype ! = dtypes . variant :
        else :
            ret = alphas ( shape_internal ( tensor , optimize = optimize ) , alpha_value = alpha_value , name = name )
        ret . set_shape ( tensor . get_shape ( ) )
    return ret


signature:
def example1 ( ) :
    """Example 1 : Applying transformation one - by - one is very SLOW !"""

implementation:
st = time . time ( )
for _ in range ( 100 ) : # Try 100 times and compute the averaged speed
    xx = tl . prepro . rotation ( image , rg = - 20 , is_random = False )
    xx = tl . prepro . flip_axis ( xx , axis = 1 , is_random = False )
    xx = tl . prepro . shear2 ( xx , shear = ( 0. , - 0.2 ) , is_random = False )
    xx = tl . prepro . zoom ( xx , zoom_range = 1 / 0.8 )
    xx = tl . prepro . shift ( xx , wrg = - 0.1 , hrg = 0 , is_random = False )
print ( "apply transforms one-by-one took %fs for each image" % ( ( time . time ( ) - st ) / 100 ) )
tl . vis . save_image ( xx , '_result_slow.png' )


signature:
def example2 ( ) :
    """Example 2 : Applying all transforms in one is very FAST !"""

implementation:
st = time . time ( )
for _ in range ( 100 ) : # Repeat 100 times and compute the averaged speed
    transform_matrix = create_transformation_matrix ( )
    result = tl . prepro . affine_transform_cv2 ( image , transform_matrix )
    # Transform the image using a single operation
print ( "apply all transforms once took %fs for each image" % ( ( time . time ( ) - st ) / 100 ) )
# usually 50x faster
tl . vis . save_image ( result , '_result_fast.png' )


signature:
def example3 ( ) :
    """Example 3 : Using TF dataset API to load and process image for training"""

implementation:
n_data = 100
imgs_file_list = [ 'tiger.jpeg' ] * n_data
train_targets = [ np . ones ( 1 ) ] * n_data
def generator ( ) :
    if len ( imgs_file_list ) != len ( train_targets ) :
        raise RuntimeError ( 'len(imgs_file_list) != len(train_targets)' )
    for _input , _target in zip ( imgs_file_list , train_targets ) :
        yield _input , _target
def _data_aug_fn ( image ) :
    transform_matrix = create_transformation_matrix ( )
    result = tl . prepro . affine_transform_cv2 ( image , transform_matrix )
    # Transform the image using a single operation
    return result
def _map_fn ( image_path , target ) :
    image = tf . read_file ( image_path )
    image = tf . image . decode_jpeg ( image , channels = 3 )
    # Get RGB with 0 ~ 1
    image = tf . image . convert_image_dtype ( image , dtype = tf . float32 )
    image = tf . py_func ( _data_aug_fn , [ image ] , [ tf . float32 ] )
    # image = tf . reshape ( image , ( h , w , 3 ) )
    target = tf . reshape ( target , ( ) )
    return image , target
n_epoch = 10
batch_size = 5
dataset = tf . data . Dataset ( ) . from_generator ( generator , output_types = ( tf . string , tf . int64 ) )
dataset = dataset . shuffle ( buffer_size = 4096 )
# shuffle before loading images
dataset = dataset . repeat ( n_epoch )
dataset = dataset . map ( _map_fn , num_parallel_calls = multiprocessing . cpu_count ( ) )
dataset = dataset . batch ( batch_size )
# TODO : consider using tf . contrib . map _ and _ batch
dataset = dataset . prefetch ( 1 )
# prefetch 1 batch
iterator = dataset . make_one_shot_iterator ( )
one_element = iterator . get_next ( )
sess = tf . Session ( )
# feed ` one _ element ` into a network , for demo , we simply get the data as follows
n_step = round ( n_epoch * n_data / batch_size )
st = time . time ( )
for _ in range ( n_step ) :
    _images , _targets = sess . run ( one_element )
print ( "dataset APIs took %fs for each image" % ( ( time . time ( ) - st ) / batch_size / n_step ) )


signature:
def example4 ( ) :
    """Example 4 : Transforming coordinates using affine matrix ."""

implementation:
transform_matrix = create_transformation_matrix ( )
result = tl . prepro . affine_transform_cv2 ( image , transform_matrix )
# 76 times faster
# Transform keypoint coordinates
coords = [ [ ( 50 , 100 ) , ( 100 , 100 ) , ( 100 , 50 ) , ( 200 , 200 ) ] , [ ( 250 , 50 ) , ( 200 , 50 ) , ( 200 , 100 ) ] ]
coords_result = tl . prepro . affine_transform_keypoints ( coords , transform_matrix )
def imwrite ( image , coords_list , name ) :
    coords_list_ = [ ]
    for coords in coords_list :
        coords = np . array ( coords , np . int32 )
        coords = coords . reshape ( ( - 1 , 1 , 2 ) )
        coords_list_ . append ( coords )
    image = cv2 . polylines ( image , coords_list_ , True , ( 0 , 255 , 255 ) , 3 )
    cv2 . imwrite ( name , image [ ... , : : - 1 ] )
imwrite ( image , coords , '_with_keypoints_origin.png' )
imwrite ( result , coords_result , '_with_keypoints_result.png' )


signature:
def distort_fn ( x , is_train = False ) :
    """The images are processed as follows :
 . . They are cropped to 24 x 24 pixels , centrally for evaluation or randomly for training .
 . . They are approximately whitened to make the model insensitive to dynamic range .
 For training , we additionally apply a series of random distortions to
 artificially increase the data set size :
 . . Randomly flip the image from left to right .
 . . Randomly distort the image brightness ."""

implementation:
# print ( ' begin ' , x . shape , np . min ( x ) , np . max ( x ) )
x = tl . prepro . crop ( x , 24 , 24 , is_random = is_train )
# print ( ' after crop ' , x . shape , np . min ( x ) , np . max ( x ) )
if is_train : # x = tl . prepro . zoom ( x , zoom _ range = ( 0.9 , 1.0 ) , is _ random = True )
# print ( ' after zoom ' , x . shape , np . min ( x ) , np . max ( x ) )
    x = tl . prepro . flip_axis ( x , axis = 1 , is_random = True )
    # print ( ' after flip ' , x . shape , np . min ( x ) , np . max ( x ) )
    x = tl . prepro . brightness ( x , gamma = 0.1 , gain = 1 , is_random = True )
    # print ( ' after brightness ' , x . shape , np . min ( x ) , np . max ( x ) )
    # tmp = np . max ( x )
    # x + = np . random . uniform ( - 20 , 20)
    # x / = tmp
# normalize the image
x = ( x - np . mean ( x ) ) / max ( np . std ( x ) , 1e-5 )
# avoid values divided by 0
# print ( ' after norm ' , x . shape , np . min ( x ) , np . max ( x ) , np . mean ( x ) )
return x


signature:
def fit ( sess , network , train_op , cost , X_train , y_train , x , y_ , acc = None , batch_size = 100 , n_epoch = 100 , print_freq = 5 , X_val = None , y_val = None , eval_train = True , tensorboard_dir = None , tensorboard_epoch_freq = 5 , tensorboard_weight_histograms = True , tensorboard_graph_vis = True ) :
    """Training a given non time - series network by the given cost function , training data , batch _ size , n _ epoch etc .
 - MNIST example click ` here < https : / / github . com / tensorlayer / tensorlayer / blob / master / example / tutorial _ mnist _ simple . py > ` _ .
 - In order to control the training details , the authors HIGHLY recommend ` ` tl . iterate ` ` see two MNIST examples ` 1 < https : / / github . com / tensorlayer / tensorlayer / blob / master / example / tutorial _ mlp _ dropout1 . py > ` _ , ` 2 < https : / / github . com / tensorlayer / tensorlayer / blob / master / example / tutorial _ mlp _ dropout1 . py > ` _ .
 Parameters
 sess : Session
 TensorFlow Session .
 network : TensorLayer layer
 the network to be trained .
 train _ op : TensorFlow optimizer
 The optimizer for training e . g . tf . train . AdamOptimizer .
 X _ train : numpy . array
 The input of training data
 y _ train : numpy . array
 The target of training data
 x : placeholder
 For inputs .
 y _ : placeholder
 For targets .
 acc : TensorFlow expression or None
 Metric for accuracy or others . If None , would not print the information .
 batch _ size : int
 The batch size for training and evaluating .
 n _ epoch : int
 The number of training epochs .
 print _ freq : int
 Print the training information every ` ` print _ freq ` ` epochs .
 X _ val : numpy . array or None
 The input of validation data . If None , would not perform validation .
 y _ val : numpy . array or None
 The target of validation data . If None , would not perform validation .
 eval _ train : boolean
 Whether to evaluate the model during training .
 If X _ val and y _ val are not None , it reflects whether to evaluate the model on training data .
 tensorboard _ dir : string
 path to log dir , if set , summary data will be stored to the tensorboard _ dir / directory for visualization with tensorboard . ( default None )
 Also runs ` tl . layers . initialize _ global _ variables ( sess ) ` internally in fit ( ) to setup the summary nodes .
 tensorboard _ epoch _ freq : int
 How many epochs between storing tensorboard checkpoint for visualization to log / directory ( default 5 ) .
 tensorboard _ weight _ histograms : boolean
 If True updates tensorboard data in the logs / directory for visualization
 of the weight histograms every tensorboard _ epoch _ freq epoch ( default True ) .
 tensorboard _ graph _ vis : boolean
 If True stores the graph in the tensorboard summaries saved to log / ( default True ) .
 Examples
 See ` tutorial _ mnist _ simple . py < https : / / github . com / tensorlayer / tensorlayer / blob / master / example / tutorial _ mnist _ simple . py > ` _
 > > > tl . utils . fit ( sess , network , train _ op , cost , X _ train , y _ train , x , y _ ,
 . . . acc = acc , batch _ size = 500 , n _ epoch = 200 , print _ freq = 5,
 . . . X _ val = X _ val , y _ val = y _ val , eval _ train = False )
 > > > tl . utils . fit ( sess , network , train _ op , cost , X _ train , y _ train , x , y _ ,
 . . . acc = acc , batch _ size = 500 , n _ epoch = 200 , print _ freq = 5,
 . . . X _ val = X _ val , y _ val = y _ val , eval _ train = False ,
 . . . tensorboard = True , tensorboard _ weight _ histograms = True , tensorboard _ graph _ vis = True )
 Notes
 If tensorboard _ dir not None , the ` global _ variables _ initializer ` will be run inside the fit function
 in order to initialize the automatically generated summary nodes used for tensorboard visualization ,
 thus ` tf . global _ variables _ initializer ( ) . run ( ) ` before the ` fit ( ) ` call will be undefined ."""

implementation:
if X_train . shape [ 0 ] < batch_size :
    raise AssertionError ( "Number of training examples should be bigger than the batch size" )
if tensorboard_dir is not None :
    tl . logging . info ( "Setting up tensorboard ..." )
    # Set up tensorboard summaries and saver
    tl . files . exists_or_mkdir ( tensorboard_dir )
    # Only write summaries for more recent TensorFlow versions
    if hasattr ( tf , 'summary' ) and hasattr ( tf . summary , 'FileWriter' ) :
        if tensorboard_graph_vis :
            train_writer = tf . summary . FileWriter ( tensorboard_dir + '/train' , sess . graph )
            val_writer = tf . summary . FileWriter ( tensorboard_dir + '/validation' , sess . graph )
        else :
            train_writer = tf . summary . FileWriter ( tensorboard_dir + '/train' )
            val_writer = tf . summary . FileWriter ( tensorboard_dir + '/validation' )
    # Set up summary nodes
    if ( tensorboard_weight_histograms ) :
        for param in network . all_params :
            if hasattr ( tf , 'summary' ) and hasattr ( tf . summary , 'histogram' ) :
                tl . logging . info ( 'Param name %s' % param . name )
                tf . summary . histogram ( param . name , param )
    if hasattr ( tf , 'summary' ) and hasattr ( tf . summary , 'histogram' ) :
        tf . summary . scalar ( 'cost' , cost )
    merged = tf . summary . merge_all ( )
    # Initalize all variables and summaries
    tl . layers . initialize_global_variables ( sess )
    tl . logging . info ( "Finished! use `tensorboard --logdir=%s/` to start tensorboard" % tensorboard_dir )
tl . logging . info ( "Start training the network ..." )
start_time_begin = time . time ( )
tensorboard_train_index , tensorboard_val_index = 0 , 0
for epoch in range ( n_epoch ) :
    start_time = time . time ( )
    loss_ep = 0
    n_step = 0
    for X_train_a , y_train_a in tl . iterate . minibatches ( X_train , y_train , batch_size , shuffle = True ) :
        feed_dict = { x : X_train_a , y_ : y_train_a }
        feed_dict . update ( network . all_drop )
        # enable noise layers
        loss , _ = sess . run ( [ cost , train_op ] , feed_dict = feed_dict )
        loss_ep += loss
        n_step += 1
    loss_ep = loss_ep / n_step
    if tensorboard_dir is not None and hasattr ( tf , 'summary' ) :
        if epoch + 1 == 1 or ( epoch + 1 ) % tensorboard_epoch_freq == 0 :
            for X_train_a , y_train_a in tl . iterate . minibatches ( X_train , y_train , batch_size , shuffle = True ) :
                dp_dict = dict_to_one ( network . all_drop )
                # disable noise layers
                feed_dict = { x : X_train_a , y_ : y_train_a }
                feed_dict . update ( dp_dict )
                result = sess . run ( merged , feed_dict = feed_dict )
                train_writer . add_summary ( result , tensorboard_train_index )
                tensorboard_train_index += 1
            if ( X_val is not None ) and ( y_val is not None ) :
                for X_val_a , y_val_a in tl . iterate . minibatches ( X_val , y_val , batch_size , shuffle = True ) :
                    dp_dict = dict_to_one ( network . all_drop )
                    # disable noise layers
                    feed_dict = { x : X_val_a , y_ : y_val_a }
                    feed_dict . update ( dp_dict )
                    result = sess . run ( merged , feed_dict = feed_dict )
                    val_writer . add_summary ( result , tensorboard_val_index )
                    tensorboard_val_index += 1
    if epoch + 1 == 1 or ( epoch + 1 ) % print_freq == 0 :
        if ( X_val is not None ) and ( y_val is not None ) :
            tl . logging . info ( "Epoch %d of %d took %fs" % ( epoch + 1 , n_epoch , time . time ( ) - start_time ) )
            if eval_train is True :
                train_loss , train_acc , n_batch = 0 , 0 , 0
                for X_train_a , y_train_a in tl . iterate . minibatches ( X_train , y_train , batch_size , shuffle = True ) :
                    dp_dict = dict_to_one ( network . all_drop )
                    # disable noise layers
                    feed_dict = { x : X_train_a , y_ : y_train_a }
                    feed_dict . update ( dp_dict )
                    if acc is not None :
                        err , ac = sess . run ( [ cost , acc ] , feed_dict = feed_dict )
                        train_acc += ac
                    else :
                        err = sess . run ( cost , feed_dict = feed_dict )
                    train_loss += err
                    n_batch += 1
                tl . logging . info ( "   train loss: %f" % ( train_loss / n_batch ) )
                if acc is not None :
                    tl . logging . info ( "   train acc: %f" % ( train_acc / n_batch ) )
            val_loss , val_acc , n_batch = 0 , 0 , 0
            for X_val_a , y_val_a in tl . iterate . minibatches ( X_val , y_val , batch_size , shuffle = True ) :
                dp_dict = dict_to_one ( network . all_drop )
                # disable noise layers
                feed_dict = { x : X_val_a , y_ : y_val_a }
                feed_dict . update ( dp_dict )
                if acc is not None :
                    err , ac = sess . run ( [ cost , acc ] , feed_dict = feed_dict )
                    val_acc += ac
                else :
                    err = sess . run ( cost , feed_dict = feed_dict )
                val_loss += err
                n_batch += 1
            tl . logging . info ( "   val loss: %f" % ( val_loss / n_batch ) )
            if acc is not None :
                tl . logging . info ( "   val acc: %f" % ( val_acc / n_batch ) )
        else :
            tl . logging . info ( "Epoch %d of %d took %fs, loss %f" % ( epoch + 1 , n_epoch , time . time ( ) - start_time , loss_ep ) )
tl . logging . info ( "Total training time: %fs" % ( time . time ( ) - start_time_begin ) )


signature:
def predict ( sess , network , X , x , y_op , batch_size = None ) :
    """Return the predict results of given non time - series network .
 Parameters
 sess : Session
 TensorFlow Session .
 network : TensorLayer layer
 The network .
 X : numpy . array
 The inputs .
 x : placeholder
 For inputs .
 y _ op : placeholder
 The argmax expression of softmax outputs .
 batch _ size : int or None
 The batch size for prediction , when dataset is large , we should use minibatche for prediction ;
 if dataset is small , we can set it to None .
 Examples
 See ` tutorial _ mnist _ simple . py < https : / / github . com / tensorlayer / tensorlayer / blob / master / example / tutorial _ mnist _ simple . py > ` _
 > > > y = network . outputs
 > > > y _ op = tf . argmax ( tf . nn . softmax ( y ) , 1)
 > > > print ( tl . utils . predict ( sess , network , X _ test , x , y _ op ) )"""

implementation:
if batch_size is None :
    dp_dict = dict_to_one ( network . all_drop )
    # disable noise layers
    feed_dict = { x : X , }
    feed_dict . update ( dp_dict )
    return sess . run ( y_op , feed_dict = feed_dict )
else :
    result = None
    for X_a , _ in tl . iterate . minibatches ( X , X , batch_size , shuffle = False ) :
        dp_dict = dict_to_one ( network . all_drop )
        feed_dict = { x : X_a , }
        feed_dict . update ( dp_dict )
        result_a = sess . run ( y_op , feed_dict = feed_dict )
        if result is None :
            result = result_a
        else :
            result = np . concatenate ( ( result , result_a ) )
    if result is None :
        if len ( X ) % batch_size != 0 :
            dp_dict = dict_to_one ( network . all_drop )
            feed_dict = { x : X [ - ( len ( X ) % batch_size ) : , : ] , }
            feed_dict . update ( dp_dict )
            result_a = sess . run ( y_op , feed_dict = feed_dict )
            result = result_a
    else :
        if len ( X ) != len ( result ) and len ( X ) % batch_size != 0 :
            dp_dict = dict_to_one ( network . all_drop )
            feed_dict = { x : X [ - ( len ( X ) % batch_size ) : , : ] , }
            feed_dict . update ( dp_dict )
            result_a = sess . run ( y_op , feed_dict = feed_dict )
            result = np . concatenate ( ( result , result_a ) )
    return result


signature:
def evaluation ( y_test = None , y_predict = None , n_classes = None ) :
    """Input the predicted results , targets results and
 the number of class , return the confusion matrix , F1 - score of each class ,
 accuracy and macro F1 - score .
 Parameters
 y _ test : list
 The target results
 y _ predict : list
 The predicted results
 n _ classes : int
 The number of classes
 Examples
 > > > c _ mat , f1 , acc , f1 _ macro = tl . utils . evaluation ( y _ test , y _ predict , n _ classes )"""

implementation:
c_mat = confusion_matrix ( y_test , y_predict , labels = [ x for x in range ( n_classes ) ] )
f1 = f1_score ( y_test , y_predict , average = None , labels = [ x for x in range ( n_classes ) ] )
f1_macro = f1_score ( y_test , y_predict , average = 'macro' )
acc = accuracy_score ( y_test , y_predict )
tl . logging . info ( 'confusion matrix: \n%s' % c_mat )
tl . logging . info ( 'f1-score        : %s' % f1 )
tl . logging . info ( 'f1-score(macro) : %f' % f1_macro )
# same output with > f1 _ score ( y _ true , y _ pred , average = ' macro ' )
tl . logging . info ( 'accuracy-score  : %f' % acc )
return c_mat , f1 , acc , f1_macro


signature:
def class_balancing_oversample ( X_train = None , y_train = None , printable = True ) :
    """Input the features and labels , return the features and labels after oversampling .
 Parameters
 X _ train : numpy . array
 The inputs .
 y _ train : numpy . array
 The targets .
 Examples
 One X
 > > > X _ train , y _ train = class _ balancing _ oversample ( X _ train , y _ train , printable = True )
 Two X
 > > > X , y = tl . utils . class _ balancing _ oversample ( X _ train = np . hstack ( ( X1 , X2 ) ) , y _ train = y , printable = False )
 > > > X1 = X [ : , 0:5]
 > > > X2 = X [ : , 5 : ]"""

implementation:
# = = = = = Classes balancing
if printable :
    tl . logging . info ( "Classes balancing for training examples..." )
c = Counter ( y_train )
if printable :
    tl . logging . info ( 'the occurrence number of each stage: %s' % c . most_common ( ) )
    tl . logging . info ( 'the least stage is Label %s have %s instances' % c . most_common ( ) [ - 1 ] )
    tl . logging . info ( 'the most stage is  Label %s have %s instances' % c . most_common ( 1 ) [ 0 ] )
most_num = c . most_common ( 1 ) [ 0 ] [ 1 ]
if printable :
    tl . logging . info ( 'most num is %d, all classes tend to be this num' % most_num )
locations = { }
number = { }
for lab , num in c . most_common ( ) : # find the index from y _ train
    number [ lab ] = num
    locations [ lab ] = np . where ( np . array ( y_train ) == lab ) [ 0 ]
if printable :
    tl . logging . info ( 'convert list(np.array) to dict format' )
X = { }
# convert list to dict
for lab , num in number . items ( ) :
    X [ lab ] = X_train [ locations [ lab ] ]
# oversampling
if printable :
    tl . logging . info ( 'start oversampling' )
for key in X :
    temp = X [ key ]
    while True :
        if len ( X [ key ] ) >= most_num :
            break
        X [ key ] = np . vstack ( ( X [ key ] , temp ) )
if printable :
    tl . logging . info ( 'first features of label 0 > %d' % len ( X [ 0 ] [ 0 ] ) )
    tl . logging . info ( 'the occurrence num of each stage after oversampling' )
for key in X :
    tl . logging . info ( "%s %d" % ( key , len ( X [ key ] ) ) )
if printable :
    tl . logging . info ( 'make each stage have same num of instances' )
for key in X :
    X [ key ] = X [ key ] [ 0 : most_num , : ]
    tl . logging . info ( "%s %d" % ( key , len ( X [ key ] ) ) )
# convert dict to list
if printable :
    tl . logging . info ( 'convert from dict to list format' )
y_train = [ ]
X_train = np . empty ( shape = ( 0 , len ( X [ 0 ] [ 0 ] ) ) )
for key in X :
    X_train = np . vstack ( ( X_train , X [ key ] ) )
    y_train . extend ( [ key for i in range ( len ( X [ key ] ) ) ] )
# tl . logging . info ( len ( X _ train ) , len ( y _ train ) )
c = Counter ( y_train )
if printable :
    tl . logging . info ( 'the occurrence number of each stage after oversampling: %s' % c . most_common ( ) )
# = = = = = End of Classes balancing
return X_train , y_train


signature:
def get_random_int ( min_v = 0 , max_v = 10 , number = 5 , seed = None ) :
    """Return a list of random integer by the given range and quantity .
 Parameters
 min _ v : number
 The minimum value .
 max _ v : number
 The maximum value .
 number : int
 Number of value .
 seed : int or None
 The seed for random .
 Examples
 > > > r = get _ random _ int ( min _ v = 0 , max _ v = 10 , number = 5)
 [10 , 2 , 3 , 3 , 7]"""

implementation:
rnd = random . Random ( )
if seed :
    rnd = random . Random ( seed )
# return [ random . randint ( min , max ) for p in range ( 0 , number ) ]
return [ rnd . randint ( min_v , max_v ) for p in range ( 0 , number ) ]


implementation:
dictionary = { }
for idx , c in enumerate ( string ) :
    dictionary . update ( { c : idx } )
return dictionary


signature:
def exit_tensorflow ( sess = None , port = 6006 ) :
    """Close TensorFlow session , TensorBoard and Nvidia - process if available .
 Parameters
 sess : Session
 TensorFlow Session .
 tb _ port : int
 TensorBoard port you want to close , ` 6006 ` as default ."""

implementation:
text = "[TL] Close tensorboard and nvidia-process if available"
text2 = "[TL] Close tensorboard and nvidia-process not yet supported by this function (tl.ops.exit_tf) on "
if sess is not None :
    sess . close ( )
if _platform == "linux" or _platform == "linux2" :
    tl . logging . info ( 'linux: %s' % text )
    os . system ( 'nvidia-smi' )
    os . system ( 'fuser ' + port + '/tcp -k' )
    # kill tensorboard 6006
    os . system ( "nvidia-smi | grep python |awk '{print $3}'|xargs kill" )
    # kill all nvidia - smi python process
    _exit ( )
elif _platform == "darwin" :
    tl . logging . info ( 'OS X: %s' % text )
    subprocess . Popen ( "lsof -i tcp:" + str ( port ) + "  | grep -v PID | awk '{print $2}' | xargs kill" , shell = True )
    # kill tensorboard
elif _platform == "win32" :
    raise NotImplementedError ( "this function is not supported on the Windows platform" )
else :
    tl . logging . info ( text2 + _platform )


signature:
def open_tensorboard ( log_dir = '/tmp/tensorflow' , port = 6006 ) :
    """Open Tensorboard .
 Parameters
 log _ dir : str
 Directory where your tensorboard logs are saved
 port : int
 TensorBoard port you want to open , 6006 is tensorboard default"""

implementation:
text = "[TL] Open tensorboard, go to localhost:" + str ( port ) + " to access"
text2 = " not yet supported by this function (tl.ops.open_tb)"
if not tl . files . exists_or_mkdir ( log_dir , verbose = False ) :
    tl . logging . info ( "[TL] Log reportory was created at %s" % log_dir )
if _platform == "linux" or _platform == "linux2" :
    raise NotImplementedError ( )
elif _platform == "darwin" :
    tl . logging . info ( 'OS X: %s' % text )
    subprocess . Popen ( sys . prefix + " | python -m tensorflow.tensorboard --logdir=" + log_dir + " --port=" + str ( port ) , shell = True )
    # open tensorboard in localhost : 6006 / or whatever port you chose
elif _platform == "win32" :
    raise NotImplementedError ( "this function is not supported on the Windows platform" )
else :
    tl . logging . info ( _platform + text2 )