Ejemplo n.º 1
0
def initialize_model(path_classes, path_anchors, path_model, input_shape,
                     freeze_body, path_weights, td_len, mode, spp, loss_percs,
                     **kwargs):

    class_names = ktrain.get_classes(path_classes)
    num_classes = len(class_names)
    anchors = ktrain.get_anchors(path_anchors)

    # Create model
    title = 'Create Keras model'
    print('{} {} {}'.format('=' * print_indnt, title,
                            '=' * (print_line - 2 - len(title) - print_indnt)))
    model = create_model(input_shape,
                         anchors,
                         num_classes,
                         freeze_body=freeze_body,
                         weights_path=path_weights,
                         td_len=td_len,
                         mode=mode,
                         spp=spp,
                         loss_percs=loss_percs)

    # Store model architecture
    model_architecture = model.to_json()
    with open(path_model + 'architecture.json', 'w') as f:
        json.dump(model_architecture, f)
    print("Model architecture stored as json")
    print('=' * print_line)

    # Training callbacks
    callbacks = {
        'logging':
        TensorBoard(log_dir=path_model),
        'checkpoint':
        ModelCheckpoint(
            path_model + 'weights/' +
            'ep{epoch:03d}-loss{loss:.5f}-val_loss{val_loss:.5f}.h5',
            monitor='val_loss',
            save_weights_only=True,
            save_best_only=True,
            period=1),
        'reduce_lr_1':
        ReduceLROnPlateau(monitor='loss',
                          min_delta=0.5,
                          factor=0.1,
                          patience=4,
                          verbose=1),
        'reduce_lr_2':
        ReduceLROnPlateau(monitor='val_loss',
                          min_delta=0,
                          factor=0.1,
                          patience=4,
                          verbose=1),
        'early_stopping':
        EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1)
    }

    log('MODEL CREATED')

    return model, callbacks, anchors, num_classes
def get_excel_resume_full(model_folder, train_params, train_loss, val_loss, 
						  eval_stats_train, eval_stats_val, train_diff, best_weights):
	
	if 'egocentric_results' in train_params['path_weights']:
		path_weights = '/'.join(train_params['path_weights'].split('/')[4:6])
	else:
		path_weights = train_params['path_weights'] 
	
	tiny_version = len(ktrain.get_anchors(train_params['path_anchors'])) == 6
	if tiny_version: model = 'tiny'
	elif train_params.get('spp', False): model = 'spp'
	else: model = 'yolo'
	
	mode = '{} | {} | {}'.format(
			'bw' if best_weights else 's2',
			model,
			train_params['mode'] if train_params.get('mode', None) is not None else '-')
		
	result = '{model_folder}\t{version}\t{input_shape}\t{annotations}\t{anchors}\t{pretraining}\t{frozen_training:d}\t{mode}\t{training_time}'.format(
				model_folder = '/'.join(model_folder.split('/')[-2:]), 
				version = train_params.get('version', ''),
				input_shape = 'multi_scale' if train_params.get('multi_scale', False) else train_params['input_shape'],
				annotations = train_params['path_annotations'],
				anchors = train_params['path_anchors'],
				pretraining = path_weights,
				frozen_training = train_params['freeze_body'],
				mode = mode,
				training_time = train_diff
			)
	
	result += '\t{train_loss}\t{val_loss}'.format(
				train_loss=train_loss, val_loss=val_loss).replace('.', ',')
	
	if eval_stats_train is not None:
		result += '\t{mAPtrain:.5f}\t{mAP50train:.5f}\t{R100train:.5f}'.format(
					mAPtrain = eval_stats_train['total'][0]*100 if 'total' in eval_stats_train else 0,
					mAP50train = eval_stats_train['total'][1]*100 if 'total' in eval_stats_train else 0,
					R100train = eval_stats_train['total'][7]*100 if 'total' in eval_stats_train else 0,
				).replace('.', ',')
	else:
		result += '\t'*3
		
	result += '\t{mAPval:.5f}\t{mAP50val:.5f}\t{mAP75val:.5f}\t{R100val:.5f}'.format(
				mAPval = eval_stats_val['total'][0]*100 if 'total' in eval_stats_val else 0,
				mAP50val = eval_stats_val['total'][1]*100 if 'total' in eval_stats_val else 0,
				mAP75val = eval_stats_val['total'][2]*100 if 'total' in eval_stats_val else 0,
				R100val = eval_stats_val['total'][7]*100 if 'total' in eval_stats_val else 0,
			).replace('.', ',')

	result += '\t{mAPS}\t{mAPM}\t{mAPL}'.format(
				mAPS=eval_stats_val['total'][3]*100 if 'total' in eval_stats_val else 0, 
				mAPM=eval_stats_val['total'][4]*100 if 'total' in eval_stats_val else 0, 
				mAPL=eval_stats_val['total'][5]*100 if 'total' in eval_stats_val else 0, 
			).replace('.', ',')
	
	return result
Ejemplo n.º 3
0
    #input_shape = (img_size,img_size)
    #h, w = input_shape
    #y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \
    #	num_anchors//3, num_classes+5)) for l in range(3)]

    model = r_yolo_body(image_input_td, num_anchors // 3, num_classes, td_len,
                        'lstm')
    model.summary()

    # %%

    num_classes = 7
    path_anchors = 'base_models/yolo_anchors.txt'
    path_weights = 'base_models/yolo.h5'
    anchors = ktrain.get_anchors(path_anchors)
    img_size = 320
    model = None

    td_len = 3
    model = create_model((img_size, img_size),
                         anchors,
                         num_classes,
                         load_pretrained=True,
                         freeze_body=2,
                         weights_path=path_weights,
                         td_len=5,
                         mode='bilstm')

    # %%
Ejemplo n.º 4
0
def model_comparision(models,
                      train,
                      path_results,
                      dataset_name,
                      iou=0.5,
                      plot_loss=True,
                      table=['data', 'arch', 'prtr', 'inp', 'map50', 'loss']):
    mAPs, losses, train_params = {}, {}, {}
    if train:
        score, num_annotation_file = MIN_SCORE, 0
    else:
        score, num_annotation_file = 0, 1

    for model_num, label in models.items():
        if model_num < 0:
            mAPs[model_num] = 0
            losses[model_num] = 0
        else:
            # model, class_names, videos, occurrences, resume, eval_stats, train_params, loss
            _, _, _, _, _, eval_stats_t, _, loss_t = main(path_results,
                                                          dataset_name,
                                                          model_num,
                                                          score,
                                                          iou,
                                                          num_annotation_file,
                                                          plot=False,
                                                          full=True,
                                                          best_weights=True)

            _, _, _, _, _, eval_stats_f, tp, loss_f = main(path_results,
                                                           dataset_name,
                                                           model_num,
                                                           score,
                                                           iou,
                                                           num_annotation_file,
                                                           plot=False,
                                                           full=True,
                                                           best_weights=False)

            train_params[model_num] = tp

            if eval_stats_t['total'][1] >= eval_stats_f['total'][1]:
                print('Model {} con best_weights: {:.2f}'.format(
                    model_num, eval_stats_t['total'][1]))
                mAPs[model_num] = eval_stats_t['total'][1]
                losses[model_num] = loss_t[1]
            else:
                print('Model {} con stage_2: {:.2f}'.format(
                    model_num, eval_stats_f['total'][1]))
                mAPs[model_num] = eval_stats_f['total'][1]
                losses[model_num] = loss_f[1]

    if not table:
        fig, ax1 = plt.subplots()
        ax1.bar(list(models.values()),
                list(mAPs.values()),
                alpha=0.6,
                color='b')

        if plot_loss:
            ax1.set_ylabel('mAP', color='b')
            ax1.tick_params(axis='y', labelcolor='b')
            ax2 = ax1.twinx()
            ax2.plot(list(models.values()), list(losses.values()), color='g')
            ax2.set_ylabel('loss', color='g')
            ax2.tick_params(axis='y', labelcolor='g')
        else:
            ax1.set_ylabel('mAP')

        fig.tight_layout()

        return fig

    else:
        ['arch', 'inp', 'map50', 'loss']
        table_data = []
        if 'num' in table: table_data.append('model\_num')
        if 'data' in table: table_data.append('dataset')
        if 'arch' in table: table_data.append('architecture')
        if 'prtr' in table: table_data.append('pretraining')
        if 'inp' in table: table_data.append('input size')
        if 'loss' in table: table_data.append('Loss')
        if 'map50' in table: table_data.append('mAP\\textsubscript{50}')
        table_data = ' & '.join(table_data) + ' \\\\ \hline\n'

        for model_num, label in models.items():
            if model_num < 0:
                row = ['{} {}'.format(model_num, label)
                       ] + ['--'] * (len(table) - 1)
            else:
                row = []
                if 'num' in table: row.append(str(model_num))
                if 'data' in table:
                    if 'v2' in train_params[model_num]['version']:
                        row.append('v2')
                    elif 'v3' in train_params[model_num]['version']:
                        row.append('v3')
                    else:
                        row.append('raw')
                if 'arch' in table:
                    tiny_version = len(
                        ktrain.get_anchors(
                            train_params[model_num]['path_anchors'])) == 6
                    if tiny_version: row.append('tiny')
                    elif train_params[model_num].get('spp', False):
                        row.append('spp')
                    else:
                        row.append('base')
                if 'prtr' in table:
                    if 'darknet' in train_params[model_num]['path_weights']:
                        row.append('backbone')
                    elif train_params[model_num]['path_weights'] == '':
                        row.append('--')
                    elif 'model_0' in train_params[model_num]['path_weights']:
                        row.apend('kitchen 17')
                    elif 'model_1' in train_params[model_num]['path_weights']:
                        row.apend('kitchen 18')
                    else:
                        row.append('coco')
                if 'inp' in table:                    row.append(str(train_params[model_num]['input_shape'][0])) \
    if not train_params[model_num].get('multi_scale', False) else row.append('multi-scale')
                if 'loss' in table:
                    row.append('{:.2f}'.format(losses[model_num]))
                if 'map50' in table:
                    row.append('{:.3f}'.format(mAPs[model_num] * 100))

            table_data += ' & '.join(row) + ' \\\\ \hline\n'

#		table_data = '\\begin{table}[!htb]\n\centering\n' + \
#						'\\begin{tabular}{|' + '{}'.format('l|'*len(table)) + '}\n' + \
#						'\hline\n' + table_data + \
#						'\end{tabular}\n\caption[Caption for LOF]{}\n\label{tab:}\n\end{table}'
        return table_data