Example #1
0
def segmentation(model, input_path, output_path):

    if model == "scene_parsing":
        model = pretrained.pspnet_50_ADE_20K()

    elif model == "cityscapes":
        model = pretrained.pspnet_101_cityscapes()
    else:
        model == pretrained.pspnet_101_voc12()

    model.predict_segmentation(inp=input_path, out_fname=output_path)
def get_segmentation_model(model_type, freeze=True):
    x = None
    segmentation_model = None
    if model_type == "three_class_trained":
        segmentation_model = model_from_checkpoint_path(
            three_class_checkpoints_path)

    elif model_type == "seven_class_trained":
        segmentation_model = model_from_checkpoint_path(
            seven_class_checkpoints_path)

        x = segmentation_model.get_layer("conv2d_6").output

    elif model_type == "pretrained":
        segmentation_model = pspnet_101_cityscapes()
        x = segmentation_model.get_layer("activation_107").output

    elif model_type == "seven_class_vanilla_psp":
        segmentation_model = model_from_checkpoint_path(
            seven_class_vanilla_psp_path)
        x = segmentation_model.get_layer("activation_10").output

    elif model_type == "seven_class_vanilla_psp_depth":
        from utils.pspnet import model_from_checkpoint_path as custom_model_from_checkpoint_path
        segmentation_model = custom_model_from_checkpoint_path(
            seven_class_vanilla_psp_depth_path)
        x = segmentation_model.get_layer("activation_10").output

    elif model_type == "seven_class_mobile":
        segmentation_model = model_from_checkpoint_path(
            seven_class_mobile_checkpoints_path)
        output_layer = get_layer_with_name(segmentation_model,
                                           "conv_dw_6_relu")

        x = output_layer.output

    elif model_type == "resnet50_pspnet_8_classes":
        segmentation_model = model_from_checkpoint_path(
            resnet50_pspnet_8_classes)
        output_layer = segmentation_model.get_layer(name="conv2d_6")
        x = output_layer.output

    plot_model(segmentation_model, "segmentation_model.png", show_shapes=True)
    # Explicitly define new model input and output by slicing out old model layers
    model_new = Model(inputs=segmentation_model.layers[0].input, outputs=x)

    if freeze:
        for layer in model_new.layers:
            layer.trainable = False

    return model_new
Example #3
0
def main(opt):

    model = pspnet_101_cityscapes()

    files = sorted(os.listdir(opt.src_rgb))
    print(f"Total: {len(files)}")

    if 500 * (opt.part + 1) < len(files):
        partial = files[500 * opt.part:500 * (opt.part + 1)]
        print(f"Processing from {500*opt.part} to {500*(opt.part+1)} ...")
    else:
        partial = files[500 * opt.part:]
        print(f"Processing from {500*opt.part} to {len(files)} ...")

    for f in tqdm(partial):
        out = model.predict_segmentation(inp=os.path.join(opt.src_rgb, f),
                                         out_fname=os.path.join(
                                             opt.out_dir, f))
Example #4
0
def predict(images, predictions, pretrained, entire_image, checkpoint_dir):

    if pretrained:
        model = pspnet_101_cityscapes(
        )  # load the pretrained model trained on Cityscapes dataset
    else:
        model = model_from_checkpoint_path(normpath(checkpoint_dir))

    files = [f for f in listdir(images) if isfile(join(images, f))]

    for file in files:
        name, ext = splitext(file)
        if entire_image:
            predict_standard(model,
                             inp=f'{images}{file}',
                             out_fname=f'{predictions}{name}.png')
        else:
            predict_split(model,
                          inp=f'{images}{file}',
                          out_fname=f'{predictions}{name}.png')
Example #5
0
# -*- coding: utf-8 -*-
import os
################ CPU only ##############
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
########################################
from keras_segmentation.models.model_utils import transfer_weights
from keras_segmentation.pretrained import resnet_pspnet_VOC12_v0_1, pspnet_101_cityscapes
from keras_segmentation.models.pspnet import resnet50_pspnet, pspnet_101

n_classes = 2
#model = vgg_unet(n_classes=n_classes ,  input_height=512, input_width=512)

pred_dir = '/home/GDDC-CV1/Desktop/data_1024/pred_x_presentation/'
out_dir = '/home/GDDC-CV1/Desktop/pred_out_pred-function'
checkpoints_path = '/home/GDDC-CV1/Desktop/CV-Semantic-Segmentation/model_transfer_learning/checkpoint/'

pretrained_model = pspnet_101_cityscapes()
new_model = pspnet_101(n_classes=n_classes)
transfer_weights(pretrained_model, new_model)
model = pspnet_101(n_classes=n_classes)

out = model.predict_multiple(inp_dir=pred_dir,
                             out_dir=out_dir,
                             checkpoints_path=checkpoints_path)
'''
model=None, inps=None, inp_dir=None, out_dir=None,
                     checkpoints_path=None, overlay_img=False,
                     class_names=None, show_legends=False, colors=class_colors,
                     prediction_width=None, prediction_height=None)
'''
Example #6
0
from keras_segmentation.pretrained import pspnet_101_cityscapes

model = pspnet_101_cityscapes() # load the pretrained model trained on Cityscapes dataset


out = model.predict_segmentation(
    inp="cam6_eletric_0005.jpg",
    out_fname="out.png"
)