Beispiel #1
0
def train(network_specs, training_params, image_path, save_path, ckpt_path):

    print('creating datapipe...')
    # create images DataPipeline
    datapipe = DataPipeline(image_path=image_path,
                            training_params=training_params)

    print('creating network model...')
    # create model VAE
    model = UNet(network_specs=network_specs,
                 datapipe=datapipe,
                 training_params=training_params,
                 mode='evaluating')

    # train the model
    # save_config is flexible
    print('''
=============
 HERE WE GO
=============
''')
    images, labels, preds = model.evaluate(ckpt_path=ckpt_path)

    for i in range(labels.shape[0]):
        # plt.subplots(figsize=[16,12])
        for j in range(3):
            plt.subplot(2, 6, 2 * j + 1)
            plt.imshow(labels[i, :, :, j])
            plt.subplot(2, 6, 2 * j + 2)
            plt.imshow(preds[i, :, :, j])

        plt.subplot(2, 6, 7)
        plt.imshow(np.squeeze(images[i]))
        plt.show()
        _f2s(result[m])
        for m in ['mean_iou', 'mean_AP', 'mean_AR', 'f1_score']
    ]
    score_table.append(f"|{'|'.join(average)}|")

    report = OUTPUT_FORMAT.format(date=d,
                                  runtime=runtime,
                                  config_table='\n'.join(config_table),
                                  score_table='\n'.join(score_table))

    CONFIG.EVAL_DIR.mkdir(parents=True, exist_ok=True)
    with open(CONFIG.EVAL_DIR / f'report_{d}.md', 'w') as f:
        f.write(report)


def _f2s(v, ndigits=4):
    return f'{round(v, ndigits)}'


if __name__ == '__main__':
    data_gen_val = DataGenerator('val')
    ds_val = data_gen_val.get_one_shot_iterator()

    model = UNet(input_shape=(data_gen_val.H, data_gen_val.W, 3),
                 class_num=CONFIG.CLASS_NUM)

    result = model.evaluate(ds=ds_val,
                            steps=data_gen_val.data_length //
                            CONFIG.BATCH_SIZE)
    report(result=result)