Example #1
0
def prepare_fine_tune(model, is_finetune, old_weight_path):
    if is_finetune:
        file_path, epoch_number = os.path.splitext(old_weight_path)
        epoch_number.replace('.', '')
        epoch_number = int(epoch_number)

        # loading model weights
        pre_trained_model = keras_segmentation.predict.model_from_specific_checkpoint_path(
            file_path, epoch_number)
        transfer_weights(pre_trained_model, model)

    return model
Example #2
0
    def predict_folder(self):
        self.model = vgg_unet(n_classes=256,
                              input_height=1024,
                              input_width=1024)
        old_model = load_model(self.model_file)
        transfer_weights(old_model, self.model)
        self.model.predict_multiple(inp_dir=self.dir_images, out_dir="outputs")

        self.mask = 'outputs/' + os.path.basename(os.path.normpath(self.image))
        im = PIL.Image.open(self.mask)
        im.thumbnail((600, 400), PIL.Image.ANTIALIAS)
        ph = PIL.ImageTk.PhotoImage(im)
        self.canvas = Canvas(self.window, width=ph.width(), height=ph.height())
        self.canvas.pack()
        self.canvas.create_image(0, 0, anchor=W, image=ph)
        self.canvas.image = ph
parser.add_argument("command", type=str)
# parser.add_argument("--checkpoints_path", type = str  )
# parser.add_argument("--input_path", type = str , default = "")
# parser.add_argument("--output_path", type = str , default = "")
args = parser.parse_args()
command = sys.argv[1]

# Download this pretrained model (PSPNET-50 trained on ADE-20K dataset) and place it in .keras/datasets/
# https://www.dropbox.com/s/0uxn14y26jcui4v/pspnet50_ade20k.h5?dl=1:

#Load this model and transfer its weights to new model
pre_trained_psp_model = keras_segmentation.pretrained.pspnet_50_ADE_20K()
new_model = keras_segmentation.models.pspnet.pspnet_50(n_classes=3)
new_model.summary()
transfer_weights(
    pre_trained_psp_model,
    new_model)  # transfer weights from pre-trained model to your model

input_path = "/mnt/disk3/rohit2/bhomik_work/flixstock/shm_data/input/"
trimap_path = "/mnt/disk3/rohit2/bhomik_work/flixstock/shm_data/trimap_labels/15/"
checkpoints_path = "/mnt/disk3/rohit2/bhomik_work/flixstock/shm_data/pspnet_training_3_checkpoint/"
output_path = "/mnt/disk3/rohit2/bhomik_work/flixstock/shm_data/predictions/"

if command == "train_tnet":

    # For pretraining on a given dataset, provide

    # train images,
    # annotations path (images with labels 0 (background) ,1 (uncertain) ,2 (foreground) )
    # checkpoints path - where model is saved after each epoch
Example #4
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 #5
0
            show_shapes=True,
            show_layer_names=True,
            rankdir="TB",
            expand_nested=False,
            dpi=96,
        )
        tf.keras.utils.plot_model(
            pretrained_model,
            to_file=init + "pspnet_50_ade_20k.png",
            show_shapes=True,
            show_layer_names=True,
            rankdir="TB",
            expand_nested=False,
            dpi=96,
        )
        transfer_weights(pretrained_model, model, trainable_source=True)

        opt = optimizers.Adam(learning_rate=0.00001)
    else:
        checkpoints_path = init + "pspnet_50_slim_checkpoint_50/"
        full_config_path = checkpoints_path + "_config.json"
        assert (os.path.isfile(full_config_path)
                #assert (os.path.isfile("./"+checkpoints_path+"_config.json")
                ), "Checkpoint not found."
        model_config = json.loads(open(full_config_path, "r").read())
        latest_weights = find_latest_checkpoint(checkpoints_path)
        assert (latest_weights is not None), "Weights not found."
        assert (os.path.isfile(latest_weights)), "Weights not found."
        #model = model_from_name[model_config['model_class']](

        model = model_from_name[model_config['model_class']](
Example #6
0
import os
#os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
#os.environ["CUDA_VISIBLE_DEVICES"] = ""

from keras_segmentation.models.model_utils import transfer_weights
from keras_segmentation.pretrained import pspnet_50_ADE_20K
from keras_segmentation.models.pspnet import pspnet_50

pretrained_model = pspnet_50_ADE_20K()

keji_model1 = pspnet_50(n_classes=150)

transfer_weights(
    pretrained_model,
    keji_model1)  # transfer weights from pre-trained model to your model

keji_model1.train(train_images="../VGdata/images_prepped_train_png/",
                  train_annotations="../VGdata/annotations_prepped_train_png/",
                  checkpoints_path="./keji1check",
                  epochs=5)
Example #7
0
import cv2
import os
import matplotlib.pyplot as plt

from keras.models import load_model
from keras_segmentation.models.unet import vgg_unet
from IPython.display import Image
from keras_segmentation.metrics import get_iou
from keras_segmentation.models.model_utils import transfer_weights
from IPython.display import Image
from keras_segmentation.predict import predict_multiple

model = vgg_unet(n_classes=6, input_height=320, input_width=640)
m = load_model(os.path.dirname(os.getcwd()) + "/datasets/op/vgg_unet")

transfer_weights(m, model, verbose=True)
'''
# TESTING THE MODEL ON TEST IMAGES (#1)
out = model.predict_segmentation(
    inp=os.path.dirname(os.getcwd()) + "/datasets/data/images_prepped_test/b1-09517_Clipped.jpg",
    out_fname=os.path.dirname(os.getcwd()) + "/datasets/op/outout1.png",
    checkpoints_path=os.path.dirname(os.getcwd()) + "/datasets/op/vgg_unet_1")
print('Done')

# TESTING THE MODEL ON TEST IMAGES (#2)
o = model.predict_segmentation(
    inp=os.path.dirname(os.getcwd()) + "/datasets/data/images_prepped_test/b1-09517_Clipped.jpg",
    out_fname=os.path.dirname(os.getcwd()) + "/datasets/op/outout2.png", overlay_img=False, show_legends=True,
    class_names=["Sky", "Forest Floor", "Vegetation", "Grass", "Obstacle", "Tree"],
    checkpoints_path=os.path.dirname(os.getcwd()) + "/datasets/op/vgg_unet_1")