libfidl.py
def retrain_model(props):
    """
        This function is using the Imprinting technique to retrain the model by only changing the last layer.
        All classes will be abandoned while training multiple users
    """
    MODEL_PATH = props['classification']['default_path']

    click.echo('Parsing data for retraining...')
    train_set = {}
    test_set = {}
    for user in props['user'].keys():
        image_dir = props['user'][user]['images']
        images = [f for f in os.listdir(image_dir)
                if os.path.isfile(os.path.join(image_dir, f))]
        if images:
            # allocate the number of images for training an validation
            net_pictures =len(images)
            click.echo(click.style('We found {} pictures for {}'.format(net_pictures,user),fg='green'))
            while True: 
                k = int(click.prompt('How many pictures do you want for validating the training?'))
                if k > 0.25*net_pictures: 
                    click.echo(click.style('At most 25% ({} pictures) of the training data can be used for testing the model!'.format(int(0.25*net_pictures)), fg='yellow'))
                elif k <2:
                    click.echo(click.style('At least 3 pictues must be used for testing the model!', fg='yellow'))
                else:
                    break
            
            test_set[user] = images[:k]
            assert test_set, 'No images to test [{}]'.format(user)
            train_set[user] = images[k:]
            assert train_set, 'No images to train [{}]'.format(user)
        
    #get shape of model to retrain
    tmp = BasicEngine(MODEL_PATH)
    input_tensor = tmp.get_input_tensor_shape()
    shape = (input_tensor[2], input_tensor[1])

    #rezising pictures and creating new labels map
    train_input = []
    labels_map = {}
    for user_id, (user, image_list) in enumerate(train_set.items()):
        ret = []
        for filename in image_list:
            with Image.open(os.path.join(props['user'][user]['images'],filename)) as img:
                img = img.convert('RGB')
                img = img.resize(shape, Image.NEAREST)
                ret.append(np.asarray(img).flatten())
        train_input.append(np.array(ret))
        labels_map[user_id] = user
    #Train model
    click.echo('Start training')
    engine = ImprintingEngine(MODEL_PATH, keep_classes=False)
    engine.train_all(train_input)
    click.echo(click.style('Training finished!', fg='green'))
        
    #gethering old model files
    old_model = props['classification']['path']
    old_labels = props['classification']['labels']
    #saving new model
    props['classification']['path'] = './Models/model{}.tflite'.format(''.join(['_' + u for u in labels_map.values()]))
    engine.save_model(props['classification']['path'])
    #saving labels
    props['classification']['labels'] = props['classification']['path'].replace('classification','labels').replace('tflite','json')
    with open(props['classification']['labels'] , 'w') as f:
        json.dump(labels_map, f, indent=4)
    #Evaluating how well the retrained model performed
    click.echo('Start evaluation')
    engine = ClassificationEngine(props['classification']['path'])
    top_k = 5
    correct = [0] * top_k
    wrong = [0] * top_k
    for user, image_list in test_set.items():
        for img_name in image_list:
            img = Image.open(os.path.join(props['user'][user]['images'],img_name))
            candidates = engine.classify_with_image(img, threshold=0.1, top_k=top_k)
            recognized = False
            for i in range(top_k):
                if i < len(candidates) and  user == labels_map[candidates[i][0]]:
                    recognized = True
                if recognized:
                    correct[i] = correct[i] + 1
                else:
                    wrong[i] = wrong[i] + 1
        click.echo('Evaluation Results:')
        for i in range(top_k):
            click.echo('Top {} : {:.0%}'.format(i+1, correct[i] / (correct[i] + wrong[i])))
        #  TODO  highlight with colors how well it perforemed

    if  not old_model == props['classification']['path'] and not old_labels == props['classification']['labels'] and (os.path.exists(old_labels) or os.path.exists(old_model)):
        if not click.confirm('Do you want to keep old models?'):
            os.remove(old_model)
            os.remove(old_labels)
            click.echo(click.style('Old models removed.', fg='green'))
    #saving properties
    save_properties(props)

        This function is using the Imprinting technique to retrain the model by only changing the last layer.
        All classes will be abandoned while training multiple users
    
This function is using the Imprinting technique to retrain the model by only changing the last layer.
All classes will be abandoned while training multiple users
This function is using the Imprinting technique to retrain the model by only changing the last layer . All classes will be abandoned while training multiple users
def retrain_model(props):
    MODEL_PATH = props['classification']['default_path']
    click.echo('Parsing data for retraining...')
    train_set = {}
    test_set = {}
    for user in props['user'].keys():
        image_dir = props['user'][user]['images']
        images = [f for f in os.listdir(image_dir)
                if os.path.isfile(os.path.join(image_dir, f))]
        if images:
            net_pictures =len(images)
            click.echo(click.style('We found {} pictures for {}'.format(net_pictures,user),fg='green'))
            while True: 
                k = int(click.prompt('How many pictures do you want for validating the training?'))
                if k > 0.25*net_pictures: 
                    click.echo(click.style('At most 25% ({} pictures) of the training data can be used for testing the model!'.format(int(0.25*net_pictures)), fg='yellow'))
                elif k <2:
                    click.echo(click.style('At least 3 pictues must be used for testing the model!', fg='yellow'))
                else:
                    break
            test_set[user] = images[:k]
            assert test_set, 'No images to test [{}]'.format(user)
            train_set[user] = images[k:]
            assert train_set, 'No images to train [{}]'.format(user)
    tmp = BasicEngine(MODEL_PATH)
    input_tensor = tmp.get_input_tensor_shape()
    shape = (input_tensor[2], input_tensor[1])
    train_input = []
    labels_map = {}
    for user_id, (user, image_list) in enumerate(train_set.items()):
        ret = []
        for filename in image_list:
            with Image.open(os.path.join(props['user'][user]['images'],filename)) as img:
                img = img.convert('RGB')
                img = img.resize(shape, Image.NEAREST)
                ret.append(np.asarray(img).flatten())
        train_input.append(np.array(ret))
        labels_map[user_id] = user
    click.echo('Start training')
    engine = ImprintingEngine(MODEL_PATH, keep_classes=False)
    engine.train_all(train_input)
    click.echo(click.style('Training finished!', fg='green'))
    old_model = props['classification']['path']
    old_labels = props['classification']['labels']
    props['classification']['path'] = './Models/model{}.tflite'.format(''.join(['_' + u for u in labels_map.values()]))
    engine.save_model(props['classification']['path'])
    props['classification']['labels'] = props['classification']['path'].replace('classification','labels').replace('tflite','json')
    with open(props['classification']['labels'] , 'w') as f:
        json.dump(labels_map, f, indent=4)
    click.echo('Start evaluation')
    engine = ClassificationEngine(props['classification']['path'])
    top_k = 5
    correct = [0] * top_k
    wrong = [0] * top_k
    for user, image_list in test_set.items():
        for img_name in image_list:
            img = Image.open(os.path.join(props['user'][user]['images'],img_name))
            candidates = engine.classify_with_image(img, threshold=0.1, top_k=top_k)
            recognized = False
            for i in range(top_k):
                if i < len(candidates) and  user == labels_map[candidates[i][0]]:
                    recognized = True
                if recognized:
                    correct[i] = correct[i] + 1
                else:
                    wrong[i] = wrong[i] + 1
        click.echo('Evaluation Results:')
        for i in range(top_k):
            click.echo('Top {} : {:.0%}'.format(i+1, correct[i] / (correct[i] + wrong[i])))
    if  not old_model == props['classification']['path'] and not old_labels == props['classification']['labels'] and (os.path.exists(old_labels) or os.path.exists(old_model)):
        if not click.confirm('Do you want to keep old models?'):
            os.remove(old_model)
            os.remove(old_labels)
            click.echo(click.style('Old models removed.', fg='green'))
    save_properties(props)
def retrain_model ( props ) : MODEL_PATH = props [ 'classification' ] [ 'default_path' ] click . echo ( 'Parsing data for retraining...' ) train_set = { } test_set = { } for user in props [ 'user' ] . keys ( ) : image_dir = props [ 'user' ] [ user ] [ 'images' ] images = [ f for f in os . listdir ( image_dir ) if os . path . isfile ( os . path . join ( image_dir , f ) ) ] if images : net_pictures = len ( images ) click . echo ( click . style ( 'We found {} pictures for {}' . format ( net_pictures , user ) , fg = 'green' ) ) while True : k = int ( click . prompt ( 'How many pictures do you want for validating the training?' ) ) if k > 0.25 * net_pictures : click . echo ( click . style ( 'At most 25% ({} pictures) of the training data can be used for testing the model!' . format ( int ( 0.25 * net_pictures ) ) , fg = 'yellow' ) ) elif k < 2 : click . echo ( click . style ( 'At least 3 pictues must be used for testing the model!' , fg = 'yellow' ) ) else : break test_set [ user ] = images [ : k ] assert test_set , 'No images to test [{}]' . format ( user ) train_set [ user ] = images [ k : ] assert train_set , 'No images to train [{}]' . format ( user ) tmp = BasicEngine ( MODEL_PATH ) input_tensor = tmp . get_input_tensor_shape ( ) shape = ( input_tensor [ 2 ] , input_tensor [ 1 ] ) train_input = [ ] labels_map = { } for user_id , ( user , image_list ) in enumerate ( train_set . items ( ) ) : ret = [ ] for filename in image_list : with Image . open ( os . path . join ( props [ 'user' ] [ user ] [ 'images' ] , filename ) ) as img : img = img . convert ( 'RGB' ) img = img . resize ( shape , Image . NEAREST ) ret . append ( np . asarray ( img ) . flatten ( ) ) train_input . append ( np . array ( ret ) ) labels_map [ user_id ] = user click . echo ( 'Start training' ) engine = ImprintingEngine ( MODEL_PATH , keep_classes = False ) engine . train_all ( train_input ) click . echo ( click . style ( 'Training finished!' , fg = 'green' ) ) old_model = props [ 'classification' ] [ 'path' ] old_labels = props [ 'classification' ] [ 'labels' ] props [ 'classification' ] [ 'path' ] = './Models/model{}.tflite' . format ( '' . join ( [ '_' + u for u in labels_map . values ( ) ] ) ) engine . save_model ( props [ 'classification' ] [ 'path' ] ) props [ 'classification' ] [ 'labels' ] = props [ 'classification' ] [ 'path' ] . replace ( 'classification' , 'labels' ) . replace ( 'tflite' , 'json' ) with open ( props [ 'classification' ] [ 'labels' ] , 'w' ) as f : json . dump ( labels_map , f , indent = 4 ) click . echo ( 'Start evaluation' ) engine = ClassificationEngine ( props [ 'classification' ] [ 'path' ] ) top_k = 5 correct = [ 0 ] * top_k wrong = [ 0 ] * top_k for user , image_list in test_set . items ( ) : for img_name in image_list : img = Image . open ( os . path . join ( props [ 'user' ] [ user ] [ 'images' ] , img_name ) ) candidates = engine . classify_with_image ( img , threshold = 0.1 , top_k = top_k ) recognized = False for i in range ( top_k ) : if i < len ( candidates ) and user == labels_map [ candidates [ i ] [ 0 ] ] : recognized = True if recognized : correct [ i ] = correct [ i ] + 1 else : wrong [ i ] = wrong [ i ] + 1 click . echo ( 'Evaluation Results:' ) for i in range ( top_k ) : click . echo ( 'Top {} : {:.0%}' . format ( i + 1 , correct [ i ] / ( correct [ i ] + wrong [ i ] ) ) ) if not old_model == props [ 'classification' ] [ 'path' ] and not old_labels == props [ 'classification' ] [ 'labels' ] and ( os . path . exists ( old_labels ) or os . path . exists ( old_model ) ) : if not click . confirm ( 'Do you want to keep old models?' ) : os . remove ( old_model ) os . remove ( old_labels ) click . echo ( click . style ( 'Old models removed.' , fg = 'green' ) ) save_properties ( props )
This function is using the Imprinting technique to retrain the model by only changing the last layer.
This function is using the Imprinting technique to retrain the model by only changing the last layer .
"""
        This function is using the Imprinting technique to retrain the model by only changing the last layer.
        All classes will be abandoned while training multiple users
    """ # allocate the number of images for training an validation #get shape of model to retrain #rezising pictures and creating new labels map #Train model #gethering old model files #saving new model #saving labels #Evaluating how well the retrained model performed #  TODO  highlight with colors how well it perforemed #saving properties
props