Example #1
0
def main_FCNN():

    # Step 01: Get Input Resources and Model Configuration
    parser = app_argparse()
    args = parser.parse_args()
    print(args)

    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

    use_gpu = args.use_gpu
    use_pretrain = args.use_pretrain

    epochs = args.epochs
    batch_size = args.batch_size
    tile_size = args.tile_size
    learning_rate = args.learning_rate
    weight_decay = args.weight_decay

    # Step 02: load the pretrained model
    device = utils.device(use_gpu=use_gpu)
    # init model structure
    model = FCNN()
    # model = utils.load_weights_from_disk(model)
    if use_pretrain:
        model = utils.load_entire_model(model, WEIGHTS_FILE_PATH, use_gpu)
        print("use pretrained model!")

    train_loader = dataset.training_loader(
        image_path=INPUT_IMAGE_PATH,
        label_path=LABEL_IMAGE_PATH,
        batch_size=batch_size,
        tile_size=tile_size,
        shuffle=True  # use shuffle
    )  # turn the shuffle

    model, stats = train(
        model=model,
        train_loader=train_loader,
        device=device,
        epochs=epochs,
        batch_size=batch_size,
        tile_size=tile_size,
        learning_rate=learning_rate,
        weight_decay=weight_decay,
    )

    # comment the following section to compare the results with 4 workers and pin_memory in dataloader.
    # Step 03: save the model
    # model_path = utils.save_weights_to_disk(model)
    model_path = utils.save_entire_model(model, WEIGHTS_FILE_PATH)

    # save the loss figure and data
    stats.save_loss_plot(LOSS_PLOT_PATH)
Example #2
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)
Example #3
0
def dev_model(args):  # modified from __main__ in train.py
    # Get the arguments from GUI

    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

    use_gpu = args.use_gpu
    use_pretrain = args.use_pretrain

    epochs = args.epochs
    batch_size = args.batch_size
    tile_size = (args.tile_size_height, args.tile_size_width)
    learning_rate = args.learning_rate
    weight_decay = args.weight_decay

    device = utils.device(use_gpu=use_gpu)
    # init model structure
    model = FCNN()
    # model = utils.load_weights_from_disk(model)
    if use_pretrain:
        model = utils.load_entire_model(model, WEIGHTS_FILE_PATH, use_gpu)
        print("use pretrained model!")

    train_loader = dataset.training_loader(
        image_path=INPUT_IMAGE_PATH,
        label_path=LABEL_IMAGE_PATH,
        batch_size=batch_size,
        tile_size=tile_size,
        shuffle=True,  # use shuffle
    )  # turn the shuffle

    model, stats = train(
        model=model,
        train_loader=train_loader,
        device=device,
        epochs=epochs,
        batch_size=batch_size,
        tile_size=tile_size,
        learning_rate=learning_rate,
        weight_decay=weight_decay,
    )
    # model_path = utils.save_weights_to_disk(model)
    model_path = utils.save_entire_model(model, WEIGHTS_FILE_PATH)

    # save the loss figure and data
    stats.save_loss_plot(LOSS_PLOT_PATH)

    print("[>>>] Passed!")
Example #4
0
def main_UNet_via_Folder():

    # Step 01: Get Input Resources and Model Configuration
    parser = app_argparse()
    args = parser.parse_args()
    # INPUT_IMAGE_PATH = args.input_RGB
    # LABEL_IMAGE_PATH = args.input_GT
    INPUT_IMAGE_PATH = "data/Aerial/RGBRandom"
    LABEL_IMAGE_PATH = "data/Aerial/GTRandom"
    # WEIGHTS_FILE_PATH = args.output_model_path
    WEIGHTS_FILE_PATH = "weights/Adam.UNet.weights.II.pt"
    # LOSS_PLOT_PATH = args.output_loss_plot
    OUTPUT_IMAGE_PATH = args.output_images

    print(args)

    use_gpu = args.use_gpu
    use_pretrain = True

    # epochs = args.epochs

    epochs = 10

    batch_size = args.batch_size
    tile_size = args.tile_size
    learning_rate = args.learning_rate
    weight_decay = args.weight_decay

    # Step 02: load the pretrained model
    device = utils.device(use_gpu=use_gpu)
    # init model structure
    model = UNet()
    # model = utils.load_weights_from_disk(model)
    if use_pretrain and Path(WEIGHTS_FILE_PATH).is_file():

        model = utils.load_entire_model(model, WEIGHTS_FILE_PATH, use_gpu)
        print("use pretrained model!")
    else:
        print("build new model")

    train_loader = dataset.create_image_loader(
        image_path=INPUT_IMAGE_PATH,
        label_path=LABEL_IMAGE_PATH,
        batch_size=batch_size,
        shuffle=True  # use shuffle
    )  # turn the shuffle

    model, stats = train(
        model=model,
        train_loader=train_loader,
        device=device,
        epochs=epochs,
        batch_size=batch_size,
        tile_size=tile_size,
        learning_rate=learning_rate,
        weight_decay=weight_decay,
    )

    # comment the following section to compare the results with 4 workers and pin_memory in dataloader.
    # Step 03: save the model
    # model_path = utils.save_weights_to_disk(model)
    model_path = utils.save_entire_model(model, WEIGHTS_FILE_PATH)

    # save the loss figure and data
    stats.save_loss_plot(OUTPUT_IMAGE_PATH)
Example #5
0
    i = 0
    for x, y in loader:
        # print(x.shape, y.shape)
        images_RGB[i, :, :, :] = x
        images_GT[i, :, :, :] = y
        i = i + 1

    # # create grid of images
    img_grid_RGB = torchvision.utils.make_grid(images_RGB, 4)
    writer.add_image("aerial_images_samples_RGB", img_grid_RGB, 1)

    img_grid_GT = torchvision.utils.make_grid(images_GT, 4)
    writer.add_image("aerial_images_samples_GT", img_grid_GT, 1)

    # # show images
    # matplotlib_imshow(img_grid_RGB, one_channel=True)
    # matplotlib_imshow(img_grid_GT, one_channel=True)

    # 3. Inspect the model using TensorBoard
    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)

    writer.add_graph(model, x)
    writer.close()

## Action: visualization
# tensorboard --logdir=runs
Example #6
0
import torch
import torchvision
import utils
import dataset
from model import FCNN
from utils import ClassLabel
from torchsummary import summary

dummy_input = torch.randn(10, 3, 250, 250)

model = FCNN()
# model = utils.load_weights_from_disk(model)
model = utils.load_entire_model(model)
# call model.eval() to set dropout and batch normalization layers to evaluation mode before running inference.
model.eval()

print(summary(model, (3, 250, 250)))
# Providing input and output names sets the display names for values
# within the model's graph. Setting these does not change the semantics
# of the graph; it is only for readability.
#
# The inputs to the network consist of the flat list of inputs (i.e.
# the values you would pass to the forward() method) followed by the
# flat list of parameters. You can partially specify names, i.e. provide
# a list here shorter than the number of inputs to the model, and we will
# only set that subset of names, starting from the beginning.

# input_names = ["actual_input_1"]
# output_names = ["output1"]

# torch.onnx.export(model, dummy_input, "model.onnx", verbose=True,
Example #7
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