REST API - Tensorflow/server.py
def batch_train():
    '''
    Take an input batch of images and train the classifier
    '''
    result = {'success':False}
    #Fetch the file
    if flask.request.method == "POST":
        """
	Validate the zip file which we will receive. If the available memory is 
        more than request file size + DNN model, then train the model. 
	"""
        avl_mem = (psutil.virtual_memory().free)*1024
        
        cls_mem = cls.get_model_memory_usage()
        content_length = flask.request.content_length
        logging.info('Mem: {}'.format(avl_mem - cls_mem -content_length))
        if (avl_mem - cls_mem -content_length) < 0.05*(avl_mem):
            result = {'success':False, 'Error': 'large file'}
            return flask.jsonify(result)
            
        
        if flask.request.files.get("zip"):
            zip_read = flask.request.files["zip"]
            logging.info('Zip read Sucess')
            # if the file uploaded is a zip, open the file and save create a numpy array
            zip_ref = zipfile.ZipFile(zip_read, 'r')
            # before extracting create a temp dir where we will save those files
            try:
                os.stat('tmp')
            except:
                os.mkdir('tmp')
            
            zip_ref.extractall(os.path.join(os.getcwd(), 'tmp'))
            zip_ref.close()
            new_train = []
            new_label = []
            
            # Now open the tmp dir and one by one extract the images 
            for root, dirs, files in os.walk(os.path.join(os.getcwd(), 'tmp')):
                # we will get the labels from the file name 
                for file in files:
                    file_name = os.path.splitext(file)[0]
                    _label = file_name.split('_')[1]
                    # read the image and convert it into numpy array
                    image = Image.open(os.path.join(root,file))
                    image = np.reshape(image,(IMG_ROWS,IMG_COLS,1))
                    
                    # append the labels and image array to the list
                    new_train.append(image)
                    new_label.append(int(_label))
                    
            # One hot encoding
            enc = OneHotEncoder()
            new_labels = enc.fit_transform(np.reshape(new_label, (-1, 1))).toarray()
            # Now send this to the training
            logging.info(np.shape(new_train))        
            cls.batch_train_online(np.array(new_train), new_labels)
            logging.info('Batch Training is done.')
            result = {'success':True}

    return flask.jsonify(result)

    Take an input batch of images and train the classifier
    
Take an input batch of images and train the classifier
Take an input batch of images and train the classifier
def batch_train():
    result = {'success':False}
    if flask.request.method == "POST":
        avl_mem = (psutil.virtual_memory().free)*1024
        cls_mem = cls.get_model_memory_usage()
        content_length = flask.request.content_length
        logging.info('Mem: {}'.format(avl_mem - cls_mem -content_length))
        if (avl_mem - cls_mem -content_length) < 0.05*(avl_mem):
            result = {'success':False, 'Error': 'large file'}
            return flask.jsonify(result)
        if flask.request.files.get("zip"):
            zip_read = flask.request.files["zip"]
            logging.info('Zip read Sucess')
            zip_ref = zipfile.ZipFile(zip_read, 'r')
            try:
                os.stat('tmp')
            except:
                os.mkdir('tmp')
            zip_ref.extractall(os.path.join(os.getcwd(), 'tmp'))
            zip_ref.close()
            new_train = []
            new_label = []
            for root, dirs, files in os.walk(os.path.join(os.getcwd(), 'tmp')):
                for file in files:
                    file_name = os.path.splitext(file)[0]
                    _label = file_name.split('_')[1]
                    image = Image.open(os.path.join(root,file))
                    image = np.reshape(image,(IMG_ROWS,IMG_COLS,1))
                    new_train.append(image)
                    new_label.append(int(_label))
            enc = OneHotEncoder()
            new_labels = enc.fit_transform(np.reshape(new_label, (-1, 1))).toarray()
            logging.info(np.shape(new_train))        
            cls.batch_train_online(np.array(new_train), new_labels)
            logging.info('Batch Training is done.')
            result = {'success':True}
    return flask.jsonify(result)
def batch_train ( ) : result = { 'success' : False } if flask . request . method == "POST" : """
	Validate the zip file which we will receive. If the available memory is 
        more than request file size + DNN model, then train the model. 
	""" avl_mem = ( psutil . virtual_memory ( ) . free ) * 1024 cls_mem = cls . get_model_memory_usage ( ) content_length = flask . request . content_length logging . info ( 'Mem: {}' . format ( avl_mem - cls_mem - content_length ) ) if ( avl_mem - cls_mem - content_length ) < 0.05 * ( avl_mem ) : result = { 'success' : False , 'Error' : 'large file' } return flask . jsonify ( result ) if flask . request . files . get ( "zip" ) : zip_read = flask . request . files [ "zip" ] logging . info ( 'Zip read Sucess' ) zip_ref = zipfile . ZipFile ( zip_read , 'r' ) try : os . stat ( 'tmp' ) except : os . mkdir ( 'tmp' ) zip_ref . extractall ( os . path . join ( os . getcwd ( ) , 'tmp' ) ) zip_ref . close ( ) new_train = [ ] new_label = [ ] for root , dirs , files in os . walk ( os . path . join ( os . getcwd ( ) , 'tmp' ) ) : for file in files : file_name = os . path . splitext ( file ) [ 0 ] _label = file_name . split ( '_' ) [ 1 ] image = Image . open ( os . path . join ( root , file ) ) image = np . reshape ( image , ( IMG_ROWS , IMG_COLS , 1 ) ) new_train . append ( image ) new_label . append ( int ( _label ) ) enc = OneHotEncoder ( ) new_labels = enc . fit_transform ( np . reshape ( new_label , ( - 1 , 1 ) ) ) . toarray ( ) logging . info ( np . shape ( new_train ) ) cls . batch_train_online ( np . array ( new_train ) , new_labels ) logging . info ( 'Batch Training is done.' ) result = { 'success' : True } return flask . jsonify ( result )
Take an input batch of images and train the classifier
Take an input batch of images and train the classifier
'''
    Take an input batch of images and train the classifier
    ''' #Fetch the file """
	Validate the zip file which we will receive. If the available memory is 
        more than request file size + DNN model, then train the model. 
	""" # if the file uploaded is a zip, open the file and save create a numpy array # before extracting create a temp dir where we will save those files # Now open the tmp dir and one by one extract the images  # we will get the labels from the file name  # read the image and convert it into numpy array # append the labels and image array to the list # One hot encoding # Now send this to the training