Esempio n. 1
0
def fine_tune_grid_exp(height_mode,
                       layers_to_keep_num,
                       learning_rate,
                       decay_step,
                       decay_rate,
                       epochs,
                       model_name):
    # set gpu
    os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
    os.environ['CUDA_VISIBLE_DEVICES'] = flags.GPU
    # environment settings
    np.random.seed(flags.random_seed)
    tf.set_random_seed(flags.random_seed)

    # get weight
    tf.reset_default_graph()
    if height_mode == 'subtract':
        kernel = sis_utils.get_unet_first_layer_weight(flags.pre_trained_model, 1)
    elif height_mode == 'all':
        kernel = sis_utils.get_unet_first_layer_weight(flags.pre_trained_model, 2)
    else:
        kernel = sis_utils.get_unet_first_layer_weight(flags.pre_trained_model, 3)
    tf.reset_default_graph()

    # data prepare step
    Data = rsrClassData(flags.rsr_data_dir)
    (collect_files_train, meta_train) = Data.getCollectionByName(flags.train_data_dir)
    pe_train = patch_extractor.PatchExtractorUrbanMapper(flags.rsr_data_dir,
                                                               collect_files_train, patch_size=flags.input_size,
                                                               tile_dim=meta_train['dim_image'][:2],
                                                               appendix=flags.train_patch_appendix)
    train_data_dir = pe_train.extract(flags.patch_dir)
    (collect_files_valid, meta_valid) = Data.getCollectionByName(flags.valid_data_dir)
    pe_valid = patch_extractor.PatchExtractorUrbanMapper(flags.rsr_data_dir,
                                                               collect_files_valid, patch_size=flags.input_size,
                                                               tile_dim=meta_train['dim_image'][:2],
                                                               appendix=flags.valid_patch_appendix)
    valid_data_dir = pe_valid.extract(flags.patch_dir)

    # image reader
    coord = tf.train.Coordinator()

    # load reader
    reader_train = image_reader.ImageLabelReaderHeight(train_data_dir, flags.input_size, coord,
                                                       city_list=flags.city_name, tile_list=flags.train_tile_names,
                                                       ds_name='urban_mapper', data_aug=flags.data_aug,
                                                       height_mode=flags.height_mode)
    reader_valid = image_reader.ImageLabelReaderHeight(valid_data_dir, flags.input_size, coord,
                                                       city_list=flags.city_name, tile_list=flags.valid_tile_names,
                                                       ds_name='urban_mapper', data_aug=flags.data_aug,
                                                       height_mode=flags.height_mode)
    reader_train_iter = reader_train.image_height_label_iterator(flags.batch_size)
    reader_valid_iter = reader_valid.image_height_label_iterator(flags.batch_size)

    # define place holder
    if height_mode == 'all':
        X = tf.placeholder(tf.float32, shape=[None, flags.input_size[0], flags.input_size[1], 5], name='X')
    elif height_mode == 'subtract':
        X = tf.placeholder(tf.float32, shape=[None, flags.input_size[0], flags.input_size[1], 4], name='X')
    else:
        X = tf.placeholder(tf.float32, shape=[None, flags.input_size[0], flags.input_size[1], 6], name='X')
    y = tf.placeholder(tf.int32, shape=[None, flags.input_size[0], flags.input_size[1], 1], name='y')
    mode = tf.placeholder(tf.bool, name='mode')

    # initialize model
    model = unet.UnetModel_Height_Appendix({'X':X, 'Y':y}, trainable=mode, model_name=model_name, input_size=flags.input_size)
    model.create_graph('X', flags.num_classes)
    model.load_weights(flags.pre_trained_model, layers_to_keep_num, kernel)
    model.make_loss('Y')
    model.make_learning_rate(learning_rate,
                             tf.cast(flags.n_train/flags.batch_size * decay_step, tf.int32), decay_rate)
    model.make_update_ops('X', 'Y')
    model.make_optimizer(model.learning_rate)
    # set ckdir
    model.make_ckdir(flags.ckdir)
    # make summary
    model.make_summary()
    # set up graph and initialize
    config = tf.ConfigProto()

    # run training
    start_time = time.time()
    with tf.Session(config=config) as sess:
        init = tf.global_variables_initializer()
        sess.run(init)

        saver = tf.train.Saver(var_list=tf.global_variables(), max_to_keep=1)

        if os.path.exists(model.ckdir) and tf.train.get_checkpoint_state(model.ckdir):
            latest_check_point = tf.train.latest_checkpoint(model.ckdir)
            saver.restore(sess, latest_check_point)
            print('Model Loaded {}'.format(model.ckdir))

        threads = tf.train.start_queue_runners(coord=coord, sess=sess)
        try:
            train_summary_writer = tf.summary.FileWriter(model.ckdir, sess.graph)
            model.train('X', 'Y', epochs, flags.n_train, flags.batch_size, sess, train_summary_writer,
                        train_iterator=reader_train_iter, valid_iterator=reader_valid_iter, image_summary=sis_utils.image_summary)
        finally:
            coord.request_stop()
            coord.join(threads)
            saver.save(sess, '{}/model.ckpt'.format(model.ckdir), global_step=model.global_step)

    duration = time.time() - start_time
    print('duration {:.2f} hours'.format(duration/60/60))
Esempio n. 2
0
def test_authentic_unet_height_fmap(rsr_data_dir,
                                    test_data_dir,
                                    input_size,
                                    model_name,
                                    num_classes,
                                    ckdir,
                                    city,
                                    batch_size,
                                    ds_name='inria',
                                    GPU='0',
                                    random_seed=1234,
                                    height_mode='subtract'):
    import re
    import scipy.misc
    import tensorflow as tf
    from network import unet
    from dataReader import image_reader
    from rsrClassData import rsrClassData

    def name_has_city(city_list, name):
        for city in city_list:
            if city in name:
                return True
        return False

    city = city.split(',')

    # set gpu
    os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
    os.environ['CUDA_VISIBLE_DEVICES'] = GPU
    tf.reset_default_graph()
    # environment settings
    np.random.seed(random_seed)
    tf.set_random_seed(random_seed)

    # data prepare step
    Data = rsrClassData(rsr_data_dir)
    (collect_files_test, meta_test) = Data.getCollectionByName(test_data_dir)

    # image reader
    coord = tf.train.Coordinator()

    # define place holder
    if height_mode == 'all':
        X = tf.placeholder(tf.float32,
                           shape=[None, input_size[0], input_size[1], 5],
                           name='X')
    elif height_mode == 'subtract':
        X = tf.placeholder(tf.float32,
                           shape=[None, input_size[0], input_size[1], 7],
                           name='X')
    else:
        X = tf.placeholder(tf.float32,
                           shape=[None, input_size[0], input_size[1], 6],
                           name='X')
    y = tf.placeholder(tf.int32,
                       shape=[None, input_size[0], input_size[1], 1],
                       name='y')
    mode = tf.placeholder(tf.bool, name='mode')

    # initialize model
    #model = unet.UnetModel_Origin({'X': X, 'Y': y}, trainable=mode, model_name=model_name, input_size=input_size)
    model = unet.UnetModel_Height_Appendix({
        'X': X,
        'Y': y
    },
                                           trainable=mode,
                                           model_name=model_name,
                                           input_size=input_size)
    model.create_graph('X', num_classes)
    model.make_update_ops('X', 'Y')
    # set ckdir
    model.make_ckdir(ckdir)
    # set up graph and initialize
    config = tf.ConfigProto()

    result_dict = {}

    # run training
    with tf.Session(config=config) as sess:
        init = tf.global_variables_initializer()
        sess.run(init)

        saver = tf.train.Saver(var_list=tf.global_variables(), max_to_keep=1)

        if os.path.exists(model.ckdir) and tf.train.get_checkpoint_state(
                model.ckdir):
            latest_check_point = tf.train.latest_checkpoint(model.ckdir)
            saver.restore(sess, latest_check_point)
            print('loaded model from {}'.format(latest_check_point))

        threads = tf.train.start_queue_runners(coord=coord, sess=sess)
        for (image_name, dsm_name, dtm_name, f_name,
             label_name) in collect_files_test:
            #if city in image_name:
            if name_has_city(city, image_name):
                if ds_name == 'inria':
                    city_name = re.findall('[a-z\-]*(?=[0-9]+\.)',
                                           image_name)[0]
                    tile_id = re.findall('[0-9]+(?=\.tif)', image_name)[0]
                else:
                    city_name = os.path.basename(image_name)[:3]
                    tile_id = os.path.basename(image_name)[9:12]

                iter_file_list = [
                    os.path.join(rsr_data_dir, image_name),
                    os.path.join(rsr_data_dir, dsm_name),
                    os.path.join(rsr_data_dir, dtm_name),
                    os.path.join(rsr_data_dir, f_name)
                ]

                # load reader
                iterator_test = image_reader.image_height_fmap_label_iterator(
                    iter_file_list,
                    batch_size=batch_size,
                    tile_dim=meta_test['dim_image'][:2],
                    patch_size=input_size,
                    overlap=184,
                    padding=92,
                    height_mode=height_mode)
                # run
                result = model.test('X', sess, iterator_test)
                pred_label_img = get_output_label(
                    result, (meta_test['dim_image'][0] + 184,
                             meta_test['dim_image'][1] + 184),
                    input_size,
                    meta_test['colormap'],
                    overlap=184,
                    output_image_dim=meta_test['dim_image'],
                    output_patch_size=(input_size[0] - 184,
                                       input_size[1] - 184))
                #pred_label_img[np.where(pred_label_img==2)] = 1
                # evaluate
                truth_label_img = scipy.misc.imread(
                    os.path.join(rsr_data_dir, label_name))
                #truth_label_img[np.where(truth_label_img == 2)] = 1
                #truth_label_img[np.where(truth_label_img == 1)] = 255
                iou = iou_metric(truth_label_img, pred_label_img)
                result_dict['{}{}'.format(city_name, tile_id)] = iou
            coord.request_stop()
            coord.join(threads)
    return result_dict
def main(flags):
    # set gpu
    os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
    os.environ['CUDA_VISIBLE_DEVICES'] = flags.GPU
    # environment settings
    np.random.seed(flags.random_seed)
    tf.set_random_seed(flags.random_seed)

    # data prepare step
    Data = rsrClassData(flags.rsr_data_dir)
    (collect_files_train, meta_train) = Data.getCollectionByName(flags.train_data_dir)
    pe_train = patch_extractor.PatchExtractorInria(flags.rsr_data_dir,
                                                   collect_files_train, patch_size=flags.input_size,
                                                   tile_dim=meta_train['dim_image'][:2],
                                                   appendix=flags.train_patch_appendix,
                                                   overlap=184)
    train_data_dir = pe_train.extract(flags.patch_dir, pad=184)
    (collect_files_valid, meta_valid) = Data.getCollectionByName(flags.valid_data_dir)
    pe_valid = patch_extractor.PatchExtractorInria(flags.rsr_data_dir,
                                                   collect_files_valid, patch_size=flags.input_size,
                                                   tile_dim=meta_valid['dim_image'][:2],
                                                   appendix=flags.valid_patch_appendix,
                                                   overlap=184)
    valid_data_dir = pe_valid.extract(flags.patch_dir, pad=184)

    # image reader
    coord = tf.train.Coordinator()

    # load reader
    with tf.name_scope('image_loader'):
        reader_train = image_reader.ImageLabelReader(train_data_dir, flags.input_size, coord,
                                                     city_list=flags.city_name, tile_list=flags.train_tile_names,
                                                     data_aug=flags.data_aug)
        reader_valid = image_reader.ImageLabelReader(valid_data_dir, flags.input_size, coord,
                                                     city_list=flags.city_name, tile_list=flags.valid_tile_names,
                                                     data_aug=flags.data_aug)
        X_batch_op, y_batch_op = reader_train.dequeue(flags.batch_size)
        X_batch_op_valid, y_batch_op_valid = reader_valid.dequeue(flags.batch_size)
    reader_train_op = [X_batch_op, y_batch_op]
    reader_valid_op = [X_batch_op_valid, y_batch_op_valid]

    # define place holder
    X = tf.placeholder(tf.float32, shape=[None, flags.input_size[0], flags.input_size[1], 3], name='X')
    y = tf.placeholder(tf.int32, shape=[None, flags.input_size[0], flags.input_size[1], 1], name='y')
    mode = tf.placeholder(tf.bool, name='mode')

    # initialize model
    flags.model_name = '{}_EP-{}_DS-{}_LR-{}'.format(flags.model_name, flags.epochs, flags.decay_step, flags.learning_rate)
    model = unet.UnetModel_Height_Appendix({'X':X, 'Y':y}, trainable=mode, model_name=flags.model_name, input_size=flags.input_size)
    model.create_graph('X', flags.num_classes, start_filter_num=40)
    model.make_loss('Y')
    model.make_learning_rate(flags.learning_rate,
                             tf.cast(flags.n_train/flags.batch_size * flags.decay_step, tf.int32), flags.decay_rate)
    model.make_update_ops('X', 'Y')
    model.make_optimizer(model.learning_rate)
    # set ckdir
    model.make_ckdir(flags.ckdir)
    # make summary
    model.make_summary()
    # set up graph and initialize
    config = tf.ConfigProto()

    # run training
    start_time = time.time()
    with tf.Session(config=config) as sess:
        init = tf.global_variables_initializer()
        sess.run(init)

        saver = tf.train.Saver(var_list=tf.global_variables(), max_to_keep=1)

        if os.path.exists(model.ckdir) and tf.train.get_checkpoint_state(model.ckdir):
            latest_check_point = tf.train.latest_checkpoint(model.ckdir)
            saver.restore(sess, latest_check_point)

        threads = tf.train.start_queue_runners(coord=coord, sess=sess)
        try:
            train_summary_writer = tf.summary.FileWriter(model.ckdir, sess.graph)

            model.train('X', 'Y', flags.epochs, flags.n_train, flags.batch_size, sess, train_summary_writer,
                        train_reader=reader_train_op, valid_reader=reader_valid_op, image_summary=utils.image_summary)
        finally:
            coord.request_stop()
            coord.join(threads)
            saver.save(sess, '{}/model.ckpt'.format(model.ckdir), global_step=model.global_step)

    duration = time.time() - start_time
    print('duration {:.2f} hours'.format(duration/60/60))
Esempio n. 4
0
def test_and_save(flags, model_name, save_dir):
    # set gpu
    os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
    os.environ['CUDA_VISIBLE_DEVICES'] = flags.GPU
    # environment settings
    np.random.seed(flags.random_seed)
    tf.set_random_seed(flags.random_seed)

    # data prepare step
    Data = rsrClassData(flags.rsr_data_dir)
    (collect_files_test, meta_test) = Data.getCollectionByName(flags.test_data_dir)

    # image reader
    coord = tf.train.Coordinator()

    # define place holder
    X = tf.placeholder(tf.float32, shape=[None, flags.input_size[0], flags.input_size[1], 3], name='X')
    y = tf.placeholder(tf.int32, shape=[None, flags.input_size[0], flags.input_size[1], 1], name='y')
    mode = tf.placeholder(tf.bool, name='mode')

    # initialize model
    if 'appendix' in model_name:
        model = unet.UnetModel_Height_Appendix({'X':X, 'Y':y}, trainable=mode, model_name=model_name, input_size=flags.input_size)
    elif 'Res' in model_name:
        model = unet.ResUnetModel_Crop({'X': X, 'Y': y}, trainable=mode, model_name=model_name,
                                      input_size=flags.input_size)
    else:
        model = unet.UnetModel_Origin({'X':X, 'Y':y}, trainable=mode, model_name=model_name, input_size=flags.input_size)
    if 'large' in model_name:
        model.create_graph('X', flags.num_classes, start_filter_num=40)
    else:
        model.create_graph('X', flags.num_classes)
    model.make_update_ops('X', 'Y')
    # set ckdir
    model.make_ckdir(flags.ckdir)
    # set up graph and initialize
    config = tf.ConfigProto()

    # make fold if not exists
    save_path = os.path.join(save_dir, 'temp_save', model_name)
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    else:
        return save_path

    # run training
    start_time = time.time()
    with tf.Session(config=config) as sess:
        init = tf.global_variables_initializer()
        sess.run(init)

        saver = tf.train.Saver(var_list=tf.global_variables(), max_to_keep=1)

        if os.path.exists(model.ckdir) and tf.train.get_checkpoint_state(model.ckdir):
            latest_check_point = tf.train.latest_checkpoint(model.ckdir)
            saver.restore(sess, latest_check_point)
            print('loaded {}'.format(latest_check_point))

        threads = tf.train.start_queue_runners(coord=coord, sess=sess)
        try:
            for (image_name, label_name) in collect_files_test:
                c_names = flags.city_name.split(',')
                for c_name in c_names:
                    if c_name in image_name:
                        city_name = re.findall('[a-z\-]*(?=[0-9]+\.)', image_name)[0]
                        tile_id = re.findall('[0-9]+(?=\.tif)', image_name)[0]

                        print('Scoring {}_{} using {}...'.format(city_name, tile_id, model_name))

                        # load reader
                        iterator_test = image_reader.image_label_iterator(
                            os.path.join(flags.rsr_data_dir, image_name),
                            batch_size=flags.batch_size,
                            tile_dim=meta_test['dim_image'][:2],
                            patch_size=flags.input_size,
                            overlap=184, padding=92,
                            image_mean=IMG_MEAN)
                        # run
                        result = model.test('X', sess, iterator_test, soft_pred=True)

                        pred_label_img = sis_utils.get_output_label(result,
                                                                    (meta_test['dim_image'][0]+184, meta_test['dim_image'][1]+184),
                                                                    flags.input_size,
                                                                    meta_test['colormap'], overlap=184,
                                                                    output_image_dim=meta_test['dim_image'],
                                                                    output_patch_size=(flags.input_size[0]-184, flags.input_size[1]-184),
                                                                    make_map=False, soft_pred=True)
                        file_name = os.path.join(save_path, '{}_{}.npy'.format(city_name, tile_id))
                        np.save(file_name, pred_label_img)
        finally:
            coord.request_stop()
            coord.join(threads)

    duration = time.time() - start_time
    print('duration {:.2f} minutes'.format(duration/60))
    return save_path
Esempio n. 5
0
def test(flags, model_name, save_dir):
    # set gpu
    os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
    os.environ['CUDA_VISIBLE_DEVICES'] = flags.GPU
    # environment settings
    np.random.seed(flags.random_seed)
    tf.set_random_seed(flags.random_seed)

    # data prepare step
    Data = rsrClassData(flags.rsr_data_dir)
    (collect_files_test, meta_test) = Data.getCollectionByName(flags.test_data_dir)

    # image reader
    coord = tf.train.Coordinator()

    # define place holder
    X = tf.placeholder(tf.float32, shape=[None, flags.input_size[0], flags.input_size[1], 3], name='X')
    y = tf.placeholder(tf.int32, shape=[None, flags.input_size[0], flags.input_size[1], 1], name='y')
    mode = tf.placeholder(tf.bool, name='mode')

    # initialize model
    if 'appendix' in model_name:
        model = unet.UnetModel_Height_Appendix({'X':X, 'Y':y}, trainable=mode, model_name=model_name, input_size=flags.input_size)
    else:
        model = unet.UnetModel_Origin({'X':X, 'Y':y}, trainable=mode, model_name=model_name, input_size=flags.input_size)
    if 'large' in model_name:
        model.create_graph('X', flags.num_classes, start_filter_num=40)
    else:
        model.create_graph('X', flags.num_classes)
    model.make_update_ops('X', 'Y')
    # set ckdir
    model.make_ckdir(flags.ckdir)
    # set up graph and initialize
    config = tf.ConfigProto()

    # run training
    start_time = time.time()
    with tf.Session(config=config) as sess:
        init = tf.global_variables_initializer()
        sess.run(init)

        saver = tf.train.Saver(var_list=tf.global_variables(), max_to_keep=1)

        if os.path.exists(model.ckdir) and tf.train.get_checkpoint_state(model.ckdir):
            latest_check_point = tf.train.latest_checkpoint(model.ckdir)
            saver.restore(sess, latest_check_point)
            print('loaded {}'.format(latest_check_point))

        threads = tf.train.start_queue_runners(coord=coord, sess=sess)
        try:
            iou_record = {}
            for (image_name, label_name) in collect_files_test:
                c_names = flags.city_name.split(',')
                for c_name in c_names:
                    if c_name in image_name:
                        city_name = re.findall('[a-z\-]*(?=[0-9]+\.)', image_name)[0]
                        tile_id = re.findall('[0-9]+(?=\.tif)', image_name)[0]

                        # load reader
                        iterator_test = image_reader.image_label_iterator(
                            os.path.join(flags.rsr_data_dir, image_name),
                            batch_size=flags.batch_size,
                            tile_dim=meta_test['dim_image'][:2],
                            patch_size=flags.input_size,
                            overlap=184, padding=92,
                            image_mean=IMG_MEAN)
                        # run
                        result = model.test('X', sess, iterator_test)

                        pred_label_img = sis_utils.get_output_label(result,
                                                                    (meta_test['dim_image'][0]+184, meta_test['dim_image'][1]+184),
                                                                    flags.input_size,
                                                                    meta_test['colormap'], overlap=184,
                                                                    output_image_dim=meta_test['dim_image'],
                                                                    output_patch_size=(flags.input_size[0]-184, flags.input_size[1]-184),
                                                                    make_map=False)
                        # evaluate
                        truth_label_img = scipy.misc.imread(os.path.join(flags.rsr_data_dir, label_name))
                        iou = sis_utils.iou_metric(truth_label_img, pred_label_img * 255)

                        '''ax1 = plt.subplot(121)
                        ax1.imshow(truth_label_img)
                        ax2 = plt.subplot(122, sharex=ax1, sharey=ax1)
                        ax2.imshow(pred_label_img)
                        plt.show()'''

                        iou_record[image_name] = iou
                        print('{}_{}: iou={:.2f}'.format(city_name, tile_id, iou*100))
        finally:
            coord.request_stop()
            coord.join(threads)

    duration = time.time() - start_time
    print('duration {:.2f} minutes'.format(duration/60))
    np.save(os.path.join(save_dir, '{}.npy').format(model.model_name), iou_record)

    iou_mean = []
    for _, val in iou_record.items():
        iou_mean.append(val)
    print(np.mean(iou_mean))
    return np.mean(iou_mean)