Ejemplo n.º 1
0
def main(flags):
    # make network
    # 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')
    model = uabMakeNetwork_FRRN.FRRN({
        'X': X,
        'Y': y
    },
                                     trainable=mode,
                                     model_name=flags.model_name,
                                     input_size=flags.input_size,
                                     batch_size=flags.batch_size,
                                     learn_rate=flags.learning_rate,
                                     decay_step=flags.decay_step,
                                     decay_rate=flags.decay_rate,
                                     epochs=flags.epochs,
                                     start_filter_num=flags.sfn)
    model.create_graph('X', class_num=flags.num_classes)

    # create collection
    # the original file is in /ei-edl01/data/uab_datasets/inria
    blCol = uab_collectionFunctions.uabCollection('inria')
    opDetObj = bPreproc.uabOperTileDivide(
        255)  # inria GT has value 0 and 255, we map it back to 0 and 1
    # [3] is the channel id of GT
    rescObj = uabPreprocClasses.uabPreprocMultChanOp([], 'GT_Divide.tif',
                                                     'Map GT to (0, 1)', [3],
                                                     opDetObj)
    rescObj.run(blCol)
    img_mean = blCol.getChannelMeans([0, 1, 2])  # get mean of rgb info

    # extract patches
    extrObj = uab_DataHandlerFunctions.uabPatchExtrRand(
        [0, 1, 2, 4],  # extract all 4 channels
        cSize=flags.input_size,  # patch size as 572*572
        numPerTile=121,  # overlap as 92
        extSave=['jpg', 'jpg', 'jpg',
                 'png'],  # save rgb files as jpg and gt as png
        isTrain=True,
        gtInd=3,
        pad=model.get_overlap(),
        name='Rand{}'.format(flags.run_id))  # pad around the tiles
    patchDir = extrObj.run(blCol)

    # make data reader
    # use uabCrossValMaker to get fileLists for training and validation
    idx, file_list = uabCrossValMaker.uabUtilGetFolds(patchDir, 'fileList.txt',
                                                      'force_tile')
    # use first 5 tiles for validation
    file_list_train = uabCrossValMaker.make_file_list_by_key(
        idx, file_list, [i for i in range(6, 37)])
    file_list_valid = uabCrossValMaker.make_file_list_by_key(
        idx, file_list, [i for i in range(0, 6)])

    with tf.name_scope('image_loader'):
        # GT has no mean to subtract, append a 0 for block mean
        dataReader_train = uabDataReader.ImageLabelReader(
            [3], [0, 1, 2],
            patchDir,
            file_list_train,
            flags.input_size,
            flags.tile_size,
            flags.batch_size,
            dataAug='flip,rotate',
            block_mean=np.append([0], img_mean))
        # no augmentation needed for validation
        dataReader_valid = uabDataReader.ImageLabelReader([3], [0, 1, 2],
                                                          patchDir,
                                                          file_list_valid,
                                                          flags.input_size,
                                                          flags.tile_size,
                                                          flags.batch_size,
                                                          dataAug=' ',
                                                          block_mean=np.append(
                                                              [0], img_mean))

    # train
    start_time = time.time()

    model.train_config('X',
                       'Y',
                       flags.n_train,
                       flags.n_valid,
                       flags.input_size,
                       uabRepoPaths.modelPath,
                       loss_type='xent')
    model.run(
        train_reader=dataReader_train,
        valid_reader=dataReader_valid,
        pretrained_model_dir=
        None,  # train from scratch, no need to load pre-trained model
        isTrain=True,
        img_mean=img_mean,
        verb_step=100,  # print a message every 100 step(sample)
        save_epoch=5,  # save the model every 5 epochs
        gpu=GPU,
        tile_size=flags.tile_size,
        patch_size=flags.input_size)

    duration = time.time() - start_time
    print('duration {:.2f} hours'.format(duration / 60 / 60))
Ejemplo n.º 2
0
# make network
# define place holder
X = tf.placeholder(tf.float32,
                   shape=[None, chip_size[0], chip_size[1], 3],
                   name='X')
y = tf.placeholder(tf.int32,
                   shape=[None, chip_size[0], chip_size[1], 1],
                   name='y')
mode = tf.placeholder(tf.bool, name='mode')
model = uabMakeNetwork_FRRN.FRRN({
    'X': X,
    'Y': y
},
                                 trainable=mode,
                                 model_name=model_name,
                                 input_size=chip_size,
                                 batch_size=batch_size,
                                 learn_rate=learn_rate,
                                 decay_step=decay_step,
                                 decay_rate=decay_rate,
                                 epochs=epochs,
                                 start_filter_num=start_filter_num)
model.create_graph('X', class_num=2)

# prepare data
img_mean = np.array([109.54834218, 114.86824715, 102.69644417])
patchDir_name = 'chipExtrReg_cSz{}x{}_pad{}'.format(chip_size[0], chip_size[1],
                                                    model.get_overlap())
patchDir = os.path.join(uabRepoPaths.resPath, 'PatchExtr', 'inria',
                        patchDir_name)
Ejemplo n.º 3
0
        tile_name = file_name_truth.split('_')[0]
        print('Evaluating {} ...'.format(tile_name))

        # make the model
        # define place holder
        X = tf.placeholder(tf.float32,
                           shape=[None, input_size[0], input_size[1], 3],
                           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')
        model = uabMakeNetwork_FRRN.FRRN({
            'X': X,
            'Y': y
        },
                                         trainable=mode,
                                         input_size=input_size,
                                         batch_size=5,
                                         start_filter_num=32)
        # create graph
        model.create_graph('X', class_num=2)

        # prepare the reader
        reader = uabDataReader.ImageLabelReader(gtInds=[0],
                                                dataInds=[0],
                                                nChannels=3,
                                                parentDir=parent_dir,
                                                chipFiles=[file_name],
                                                chip_size=input_size,
                                                tile_size=tile_size,
                                                batchSize=batch_size,
Ejemplo n.º 4
0
        idx_truth,
        file_list_truth, [i for i in range(0, 6)],
        filter_list=[
            'bellingham', 'bloomington', 'sfo', 'tyrol-e', 'innsbruck'
        ])
    img_mean = blCol.getChannelMeans([0, 1, 2])

    # make the model
    # define place holder
    X = tf.placeholder(tf.float32,
                       shape=[None, input_size[0], input_size[1], 3],
                       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')
    model = uabMakeNetwork_FRRN.FRRN({
        'X': X,
        'Y': y
    },
                                     trainable=mode,
                                     input_size=input_size,
                                     batch_size=5)
    # create graph
    model.create_graph('X', class_num=2)

    # evaluate on tiles
    model.evaluate(file_list_valid, file_list_valid_truth, parent_dir,
                   parent_dir_truth, input_size, tile_size, batch_size,
                   img_mean, model_dir, gpu)