예제 #1
0
def main_UNet_II():
   # TODO: Get through CLI arg
    # Step 01: Get Input Resources and Model Configuration
    parser = app_argparse()
    args = parser.parse_args()
    # print(args)

    use_gpu = args.use_gpu
    # tile_size = args.tile_size
    tile_size = (200, 200)

    INPUT_IMAGE_PATH = args.input_RGB
    LABEL_IMAGE_PATH = args.input_GT
    # WEIGHTS_FILE_PATH = args.output_model_path
    WEIGHTS_FILE_PATH = "weights/Adam.UNet.weights.II.pt"
    OUTPUT_IMAGE_PATH = args.output_images

    # Step 02: Get Input Resources and Model Configuration
    device = utils.device(use_gpu=use_gpu)
    model = UNet()
    # model = utils.load_weights_from_disk(model)
    model = utils.load_entire_model(model, WEIGHTS_FILE_PATH, use_gpu)
    print("use pretrained model!")
    # print(model)
    # summary(model, (3, tile_size[0], tile_size[1]))

    # this is issue !!!
    loader = dataset.full_image_loader(
        INPUT_IMAGE_PATH, LABEL_IMAGE_PATH, tile_size=tile_size)

    prediction = predict(model, loader, device=device,
                         class_label=ClassLabel.house)

    # Step 03: save the output
    input_image = utils.input_image(INPUT_IMAGE_PATH)
    pred_image, mask_image = utils.overlay_class_prediction(
        input_image, prediction)

    pred_image_path = OUTPUT_IMAGE_PATH + "/prediction.png"
    pred_image.save(pred_image_path)

    pred_mask_path = OUTPUT_IMAGE_PATH + "/mask.png"
    mask_image.save(pred_mask_path)

    print("(i)    Prediction and Mask image saved at {}".format(pred_image_path))
    print("(ii)   Mask image saved at {}".format(pred_mask_path))

    # Step 04: Check the metrics

    img_gt = np.array(Image.open(LABEL_IMAGE_PATH), dtype=np.int32)
    img_mask = np.array(Image.open(pred_mask_path), dtype=np.int32)

    metricComputation(img_gt, img_mask)
예제 #2
0
def tile_dataset(tile_size,
                 tile_stride_ratio=1.0,
                 img_path=None,
                 augumentation=False):
    # TODO: Perform data augmentation in this

    if img_path is None:
        img_ls = glob.glob(os.path.join("images", "img", "*.jpg"))
    else:
        img_ls = img_path
    mask_folder = os.path.join("images", "mask")
    x_tiles, y_tiles = None, None
    for i in img_ls:
        img_pth = i
        mask_pth = glob.glob(
            os.path.join(
                mask_folder,
                os.path.basename(i)[:os.path.basename(i).index(".")] +
                "*.jpg"))[0]
        x_image = utils.input_image(img_pth).convert('RGB')
        y_image = utils.label_image(mask_pth).convert('1')
        x_tile, y_tile = img_tile_generator(
            tile_size, x_image, y_image, tile_stride_ratio=tile_stride_ratio)
        if x_tiles is None and y_tiles is None:
            x_tiles, y_tiles = x_tile, y_tile
        else:
            x_tiles = np.vstack((x_tiles, x_tile))
            y_tiles = np.vstack((y_tiles, y_tile))

        if augumentation:
            x_augs, y_augs = rough.augmented_image(img_pth, mask_pth)
            for x_aug, y_aug in zip(x_augs, y_augs):
                x_tile_aug, y_tile_aug = img_tile_generator(
                    tile_size,
                    x_aug.convert('RGB'),
                    y_aug.convert('1'),
                    tile_stride_ratio=tile_stride_ratio)
                x_tiles = np.vstack((x_tiles, x_tile_aug))
                y_tiles = np.vstack((y_tiles, y_tile_aug))

    # Clip tiles accumulators to the actual number of tiles
    # Since some tiles might have been discarded, n <= tile_count
    x_tiles = torch.from_numpy(x_tiles)
    y_tiles = torch.from_numpy(y_tiles)

    x_tiles = x_tiles.to(dtype=utils.x_dtype())
    y_tiles = y_tiles.to(dtype=utils.y_dtype())

    dataset = torch.utils.data.TensorDataset(x_tiles, y_tiles)

    return dataset
예제 #3
0
def tile_dataset(image_path, label_path, tile_size, tile_stride_ratio=1.0):

    # TODO: Perform data augmentation in this

    x_image = utils.input_image(image_path).convert("RGB")
    y_image = utils.label_image(label_path).convert("1")

    # assert x_image.size == y_image.size

    tile_stride = (
        int(tile_size[0] * tile_stride_ratio),
        int(tile_size[1] * tile_stride_ratio),
    )

    tile_count, extended_size = utils.tiled_image_size(x_image.size, tile_size,
                                                       tile_stride_ratio)

    x_extended = utils.extend_image(x_image, extended_size, color=0)
    y_extended = utils.extend_image(y_image, extended_size, color=255)

    x_tiles = np.zeros((tile_count, 3, tile_size[0], tile_size[1]))
    y_tiles = np.zeros((tile_count, tile_size[0], tile_size[1]))

    def tile_generator():
        for x in range(0, extended_size[0], tile_stride[0]):
            for y in range(0, extended_size[1], tile_stride[1]):
                yield (x, y, tile_size[0], tile_size[1])

    for n, (x, y, w, h) in enumerate(tile_generator()):

        box = (x, y, x + w, y + h)

        x_tile = np.array(x_extended.crop(box))
        y_tile = np.array(y_extended.crop(box))

        x_tiles[n, :, :, :] = np.moveaxis(x_tile, -1, 0)
        y_tiles[n, :, :] = y_tile

    # Clip tiles accumulators to the actual number of tiles
    # Since some tiles might have been discarded, n <= tile_count
    x_tiles = torch.from_numpy(x_tiles[0:n + 1, :, :, :])
    y_tiles = torch.from_numpy(y_tiles[0:n + 1, :, :])
    # x_tiles = torch.from_numpy(x_tiles)
    # y_tiles = torch.from_numpy(y_tiles)
    x_tiles = x_tiles.to(dtype=utils.x_dtype())
    y_tiles = y_tiles.to(dtype=utils.y_dtype())

    dataset = torch.utils.data.TensorDataset(x_tiles, y_tiles)

    return dataset
예제 #4
0
        return torch.max(y_full, dim=1)[1]

    if class_label == ClassLabel.house:
        return torch.max(-y_full, dim=1)[1]

    #TODO: Subclass error
    raise ValueError('Unknown class label: {}'.format(class_label))


if __name__ == '__main__':

    #TODO: Get through CLI arg
    use_gpu = False
    tile_size = (256, 256)

    device = utils.device(use_gpu=use_gpu)

    model = UNet()
    model = utils.load_weights_from_disk(model)
    input_image = utils.input_image()
    loader = dataset.full_image_loader(tile_size=tile_size, img_path="images/img/train1.jpg")

    prediction = predict(model, loader, device=device,
                         class_label=ClassLabel.house)

    pred_image = utils.overlay_class_prediction(input_image, prediction)

    pred_image_path = './images/output/prediction.png'
    pred_image.save(pred_image_path)
    print('(i) Prediction image saved at {}'.format(pred_image_path))
예제 #5
0
def dev_predit(args):
    use_gpu = args.use_gpu
    tile_size = tile_size = (args.tile_size_height, args.tile_size_width)
    INPUT_IMAGE_PATH = args.input_RGB
    LABEL_IMAGE_PATH = args.input_GT
    WEIGHTS_FILE_PATH = args.output_model_path
    LOSS_PLOT_PATH = args.output_loss_plot
    OUTPUT_IMAGE_PATH = args.output_images

    # Step 02: Get Input Resources and Model Configuration
    device = utils.device(use_gpu=use_gpu)
    model = FCNN()
    # model = utils.load_weights_from_disk(model)
    model = utils.load_entire_model(model, WEIGHTS_FILE_PATH, use_gpu)
    # print(model)
    # summary(model, (3, tile_size[0], tile_size[1]))
    # this is issue !!!
    loader = dataset.full_image_loader(INPUT_IMAGE_PATH,
                                       LABEL_IMAGE_PATH,
                                       tile_size=tile_size)

    prediction = predict(model,
                         loader,
                         device=device,
                         class_label=utils.ClassLabel.house)

    # Step 03: save the output
    input_image = utils.input_image(INPUT_IMAGE_PATH)
    pred_image, mask_image = utils.overlay_class_prediction(
        input_image, prediction)

    pred_image_path = OUTPUT_IMAGE_PATH + "/prediction.png"
    pred_image.save(pred_image_path)

    pred_mask_path = OUTPUT_IMAGE_PATH + "/mask.png"
    mask_image.save(pred_mask_path)

    print("(i) Prediction and Mask image saved at {}".format(pred_image_path))
    print("(ii) Prediction and Mask image saved at {}".format(pred_mask_path))

    # Show Metrics Computation
    img_gt = np.array(Image.open(LABEL_IMAGE_PATH), dtype=np.int32)
    img_mask = np.array(Image.open(pred_mask_path), dtype=np.int32)

    metricComputation(img_gt, img_mask)

    # show images
    img_rgb = cv.imread(INPUT_IMAGE_PATH)
    img_gt = cv.imread(LABEL_IMAGE_PATH)
    img_pred = cv.imread(pred_mask_path)  # pred_image_path
    img_lost = cv.imread(LOSS_PLOT_PATH)

    images = [img_rgb, img_gt, img_pred, img_lost]
    titles = ["RGB", "GT", "Prediction", "Training Loss"]
    plt.figure(num=None, figsize=(7, 7), dpi=80, facecolor="w", edgecolor="k")
    for i in range(4):
        plt.subplot(2, 2, i + 1), plt.imshow(images[i],
                                             "gray",
                                             vmin=0,
                                             vmax=255)
        plt.title(titles[i])
        plt.xticks([]), plt.yticks([])

    plt.show()

    return pred_image_path, pred_mask_path