Example #1
0
def view_encoded_patches(img_ids,
                         files,
                         patchDir,
                         model_name,
                         img_dir,
                         cust_name=''):
    if model_name == 'unet':
        input_size = 572
    else:
        input_size = 321
    fig = plt.figure(figsize=(11, 2))
    grid = Grid(fig,
                rect=111,
                nrows_ncols=(1, 6),
                axes_pad=0.1,
                label_mode='L')
    for plt_cnt, iid in enumerate(img_ids):
        img = np.zeros((input_size, input_size, 3), dtype=np.uint8)
        for cnt in range(3):
            img[:, :, cnt] = imageio.imread(
                os.path.join(patchDir,
                             files[iid][:-1] + '_RGB{}.jpg'.format(cnt)))
        grid[plt_cnt].imshow(crop_center(img, 224, 224))
        grid[plt_cnt].set_axis_off()
    plt.tight_layout()
    if not os.path.exists(os.path.join(img_dir, cust_name)):
        os.makedirs(os.path.join(img_dir, cust_name))
    plt.savefig(
        os.path.join(
            img_dir, cust_name,
            '{}_{}.png'.format(model_name,
                               '_'.join([str(a) for a in img_ids]))))
    plt.show()
Example #2
0
def make_res50_features(model_name, task_dir, GPU=0, force_run=False):
    tf.reset_default_graph()
    feature_file_name = os.path.join(task_dir, 'res50_atlanta_{}.csv'.format(model_name))
    patch_file_name = os.path.join(task_dir, 'res50_atlanta_{}.txt'.format(model_name))

    if model_name == 'deeplab':
        input_size = (321, 321)
        overlap = 0
    else:
        input_size = (572, 572)
        overlap = 184
    blCol = uab_collectionFunctions.uabCollection('atlanta')
    img_mean = blCol.getChannelMeans([0, 1, 2])
    extrObj = uab_DataHandlerFunctions.uabPatchExtr([0, 1, 2, 3],
                                                    cSize=input_size,
                                                    numPixOverlap=overlap,
                                                    extSave=['jpg', 'jpg', 'jpg', 'png'],
                                                    isTrain=True,
                                                    gtInd=3,
                                                    pad=overlap // 2)
    patchDir = extrObj.run(blCol)

    if not os.path.exists(feature_file_name) or not os.path.exists(patch_file_name) or force_run:
        os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
        os.environ['CUDA_VISIBLE_DEVICES'] = '{}'.format(GPU)
        import keras

        input_size_fit = (224, 224)

        file_name = os.path.join(patchDir, 'fileList.txt')
        with open(file_name, 'r') as f:
            files = f.readlines()

        res50 = keras.applications.resnet50.ResNet50(include_top=True, weights='imagenet')
        fc2048 = keras.models.Model(inputs=res50.input, outputs=res50.get_layer('flatten_1').output)
        with open(feature_file_name, 'w+') as f:
            with open(patch_file_name, 'w+') as f2:
                for file_line in tqdm(files):
                    patch_name = file_line.split('.')[0][:-5]
                    img = np.zeros((input_size[0], input_size[1], 3), dtype=np.uint8)
                    for cnt, file in enumerate(file_line.strip().split(' ')[:3]):
                        img[:, :, cnt] = imageio.imread(os.path.join(patchDir, file)) - img_mean[cnt]

                    img = np.expand_dims(crop_center(img, input_size_fit[0], input_size_fit[1]), axis=0)

                    fc1000 = fc2048.predict(img).reshape((-1,)).tolist()
                    writer = csv.writer(f, lineterminator='\n')
                    writer.writerow(['{}'.format(x) for x in fc1000])
                    f2.write('{}\n'.format(patch_name))

    return feature_file_name, patch_file_name, input_size[0], patchDir
Example #3
0
    input_size_fit = (224, 224)
    img_dir, task_dir = sis_utils.get_task_img_folder()
    feature_file_name = os.path.join(task_dir, 'ucmerced_res50_inria.csv')
    patch_file_name = os.path.join(task_dir, 'ucmerced_res50_inria.txt')
    res50 = keras.applications.resnet50.ResNet50(include_top=True,
                                                 weights='imagenet')
    fc2048 = keras.models.Model(inputs=res50.input,
                                outputs=res50.get_layer('flatten_1').output)
    if not os.path.exists(feature_file_name) and not os.path.exists(
            os.path.join(patch_file_name)):
        with open(feature_file_name, 'w+') as f, open(patch_file_name,
                                                      'w+') as f2:
            for file_line in tqdm(file_list):
                img = imageio.imread(file_line)
                img = np.expand_dims(crop_center(img, input_size_fit[0],
                                                 input_size_fit[1]),
                                     axis=0)

                fc1000 = fc2048.predict(img).reshape((-1, )).tolist()
                writer = csv.writer(f, lineterminator='\n')
                writer.writerow(['{}'.format(x) for x in fc1000])
                f2.write('{}\n'.format(file_line.split('/')[-1]))

    feature = pd.read_csv(feature_file_name, sep=',', header=None).values
    with open(patch_file_name, 'r') as f:
        patch_names = f.readlines()

    perplex = 40
    file_name = os.path.join(task_dir, 'land_inria_p{}.npy'.format(perplex))
    feature_encode = run_tsne(feature,
                              file_name,