def migrate_model_2(new_model):
    old_model = model.build_encoder_decoder()
    old_model = model.build_refinement(old_model)
    old_model.load_weights("models/final.42-0.0398.hdf5")
    old = [2,4,7,9,12,14,16,19,21,26,28,30]
    new = [1,2,4,5,7,8,9,11,12,24,25,26]
    print(old_model.summary())
    old_layers = [l for l in old_model.layers]
    new_layers = [l for l in new_model.layers]

    for i in range(12):
        index_vgg = old[i]
        index_model = new[i]
        old_layer = old_layers[index_vgg]
        new_layer = new_layers[index_model]
        new_layer.set_weights(old_layer.get_weights())

    for i in range(28, 74):
        # print(i)
        old_layer = old_layers[i+4]
        new_layer = new_layers[i]
        new_layer.set_weights(old_layer.get_weights())
    del old_model
示例#2
0
    if bg_h > h:
        y = np.random.randint(0, bg_h - h)
    bg = np.array(bg[y:y + h, x:x + w], np.float32)
    alpha = np.zeros((h, w, 1), np.float32)
    alpha[:, :, 0] = a / 255.
    im = alpha * fg + (1 - alpha) * bg
    im = im.astype(np.uint8)
    return im, bg


if __name__ == '__main__':
    img_rows, img_cols = 320, 320
    channel = 4

    pretrained_path = 'models/final.42-0.0398.hdf5'
    encoder_decoder = build_encoder_decoder()
    final = build_refinement(encoder_decoder)
    final.load_weights(pretrained_path)
    print(final.summary())

    out_test_path = 'data/merged_test/'
    test_images = [f for f in os.listdir(out_test_path) if
                   os.path.isfile(os.path.join(out_test_path, f)) and f.endswith('.png')]
    samples = random.sample(test_images, 10)

    bg_test = 'data/bg_test/'
    test_bgs = [f for f in os.listdir(bg_test) if
                os.path.isfile(os.path.join(bg_test, f)) and f.endswith('.png')]
    sample_bgs = random.sample(test_bgs, 10)

    total_loss = 0.0
示例#3
0
    class MyCbk(keras.callbacks.Callback):
        def __init__(self, model):
            keras.callbacks.Callback.__init__(self)
            self.model_to_save = model

        def on_epoch_end(self, epoch, logs=None):
            fmt = checkpoint_models_path + 'model.%02d-%.4f.hdf5'
            self.model_to_save.save(fmt % (epoch, logs['val_loss']))


    # Load our model, added support for Multi-GPUs
    num_gpu = len(get_available_gpus())
    if num_gpu >= 2:
        with tf.device("/cpu:0"):
            model = build_encoder_decoder()
            if pretrained_path is not None:
                model.load_weights(pretrained_path)

        new_model = multi_gpu_model(model, gpus=num_gpu)
        # rewrite the callback: saving through the original model and not the multi-gpu model.
        model_checkpoint = MyCbk(model)
    else:
        new_model = build_encoder_decoder()
        if pretrained_path is not None:
            new_model.load_weights(pretrained_path)

    sgd = keras.optimizers.SGD(lr=1e-5, decay=1e-6, momentum=0.9, nesterov=True)
    new_model.compile(optimizer=sgd, loss=cross_entropy)

    print(new_model.summary())
示例#4
0
import random

import cv2 as cv
import keras.backend as K
import numpy as np

from config import num_classes
from data_generator import random_choice, safe_crop, to_bgr
from model import build_encoder_decoder

if __name__ == '__main__':
    img_rows, img_cols = 320, 320
    channel = 3

    model_weights_path = 'models/model.54-2.2507.hdf5'
    model = build_encoder_decoder()
    model.load_weights(model_weights_path)

    print(model.summary())

    class_weights = np.load('median_class_weights.npy')

    test_images_folder = 'data/instance-level_human_parsing/Testing/Images'
    id_file = 'data/instance-level_human_parsing/Testing/test_id.txt'
    with open(id_file, 'r') as f:
        names = f.read().splitlines()

    samples = random.sample(names, 10)

    for i in range(len(samples)):
        image_name = samples[i]
# dependency: pip install pydot & brew install graphviz
from model import build_encoder_decoder
from tensorflow.keras.utils import plot_model

if __name__ == '__main__':
    img_rows, img_cols = 320, 320
    channel = 3
    model = build_encoder_decoder(img_rows, img_cols, channel)
    plot_model(model,
               to_file='model.svg',
               show_layer_names=True,
               show_shapes=True)
示例#6
0
import cv2 as cv
import keras.backend as K
import numpy as np

from data_generator import generate_trimap, random_choice, get_alpha_test
from model import build_encoder_decoder, build_refinement
from utils import get_final_output, safe_crop, draw_str
from utils import compute_mse_loss, compute_sad_loss

if __name__ == '__main__':
    img_rows, img_cols = 320, 320
    channel = 4

    pretrained_path = 'models/model.05-0.1460.hdf5'
    encoder_decoder = build_encoder_decoder()
    final = build_refinement(encoder_decoder)
    final.load_weights(pretrained_path)
    print(final.summary())

    out_test_path = 'merged_test/'
    test_images = [f for f in os.listdir(out_test_path) if
                   os.path.isfile(os.path.join(out_test_path, f)) and f.endswith('.png')]
    samples = random.sample(test_images, 10)

    total_loss = 0.0
    for i in range(len(samples)):
        filename = samples[i]
        image_name = filename.split('.')[0]
        print('Start processing image: {}'.format(filename))
        x_test = np.empty((1, img_rows, img_cols, 4), dtype=np.float32)
示例#7
0
def video_test(videopath, models_path, videomask_path, is_rotate=False):
    cam = cv.VideoCapture(videopath)
    width = int(cam.get(cv.CAP_PROP_FRAME_WIDTH))
    height = int(cam.get(cv.CAP_PROP_FRAME_HEIGHT))

    cam_mask = cv.VideoCapture(videomask_path)
    width_mask = int(cam_mask.get(cv.CAP_PROP_FRAME_WIDTH))
    height_mask = int(cam_mask.get(cv.CAP_PROP_FRAME_HEIGHT))

    pretrained_path = models_path  #'models/final.87-0.0372.hdf5'         #'models/final.42-0.0398.hdf5'
    encoder_decoder = build_encoder_decoder()
    final = build_refinement(encoder_decoder)
    final.load_weights(pretrained_path)
    print(final.summary())

    tri_videopath = videopath[:-4] + '_tri.mp4'
    tri_video = video_save(tri_videopath, w=width_mask, h=height_mask)

    matting_videopath = videopath[:-4] + '_out.mp4'
    matting_video = video_save(matting_videopath, w=width_mask, h=height_mask)

    comp_videopath = videopath[:-4] + '_comp.mp4'
    comp_video = video_save(comp_videopath, w=width_mask, h=height_mask)

    while (cam.isOpened() and cam_mask.isOpened()):
        start_time = time.time()
        ret, frame = cam.read()
        ret, frame_mask = cam_mask.read()

        if is_rotate:
            frame = imutils.rotate_bound(frame, 90)
            frame_mask = imutils.rotate_bound(frame_mask, 90)
        #             print(frame.shape)
        if frame is None:
            print('Error image!')
            break
        if frame_mask is None:
            print('Error mask image!')
            break

        bg_h, bg_w = height, width
        print('bg_h, bg_w: ' + str((bg_h, bg_w)))

        #         a = get_alpha_test(image_name)
        a = cv.cvtColor(frame_mask, cv.COLOR_BGR2GRAY)
        _, a = cv.threshold(a, 240, 255, cv.THRESH_BINARY)

        a_h, a_w = height_mask, width_mask
        print('a_h, a_w: ' + str((a_h, a_w)))

        alpha = np.zeros((bg_h, bg_w), np.float32)

        alpha[0:a_h, 0:a_w] = a
        trimap = generate_trimap_withmask(alpha)
        #         fg = np.array(np.greater_equal(a, 255).astype(np.float32))
        #         cv.imshow('test_show',fg)
        different_sizes = [(320, 320), (320, 320), (320, 320), (480, 480),
                           (640, 640)]
        crop_size = random.choice(different_sizes)

        bgr_img = frame
        alpha = alpha
        trimap = trimap
        #         cv.imwrite('images/{}_image.png'.format(i), np.array(bgr_img).astype(np.uint8))
        #         cv.imwrite('images/{}_trimap.png'.format(i), np.array(trimap).astype(np.uint8))
        #         cv.imwrite('images/{}_alpha.png'.format(i), np.array(alpha).astype(np.uint8))

        #         x_test = np.empty((1, img_rows, img_cols, 4), dtype=np.float32)
        #         x_test[0, :, :, 0:3] = bgr_img / 255.
        #         x_test[0, :, :, 3] = trimap / 255.

        x_test = np.empty((1, 320, 320, 4), dtype=np.float32)
        bgr_img1 = cv.resize(bgr_img, (320, 320))
        trimap1 = cv.resize(trimap, (320, 320))
        x_test[0, :, :, 0:3] = bgr_img1 / 255.
        x_test[0, :, :, 3] = trimap1 / 255.

        y_true = np.empty((1, img_rows, img_cols, 2), dtype=np.float32)
        y_true[0, :, :, 0] = alpha / 255.
        y_true[0, :, :, 1] = trimap / 255.

        y_pred = final.predict(x_test)
        # print('y_pred.shape: ' + str(y_pred.shape))

        #         y_pred = np.reshape(y_pred, (img_rows, img_cols))
        y_pred = np.reshape(y_pred, (320, 320))
        print(y_pred.shape)
        y_pred = cv.resize(y_pred, (width, height))
        y_pred = y_pred * 255.0
        cv.imshow('pred', y_pred)
        y_pred = get_final_output(y_pred, trimap)

        y_pred = y_pred.astype(np.uint8)

        sad_loss = compute_sad_loss(y_pred, alpha, trimap)
        mse_loss = compute_mse_loss(y_pred, alpha, trimap)
        str_msg = 'sad_loss: %.4f, mse_loss: %.4f, crop_size: %s' % (
            sad_loss, mse_loss, str(crop_size))
        print(str_msg)

        out = y_pred.copy()
        comp = composite_alpha(frame, out)
        draw_str(out, (10, 20), str_msg)

        trimap_show = np.stack((trimap, trimap, trimap), -1)
        out_show = cv.merge((out, out, out))
        #         print(trimap_show.shape,out_show.shape,comp.shape)
        tri_video.write(trimap_show)
        matting_video.write(out_show)
        comp_video.write(comp)
        #         cv.imwrite('images/{}_out.png'.format(filename[6:]), out)

        #### composite background
        #             sample_bg = sample_bgs[i]
        #             bg = cv.imread(os.path.join(bg_test, sample_bg))
        #             bh, bw = bg.shape[:2]
        #             wratio = img_cols / bw
        #             hratio = img_rows / bh
        #             ratio = wratio if wratio > hratio else hratio
        #             if ratio > 1:
        #                 bg = cv.resize(src=bg, dsize=(math.ceil(bw * ratio), math.ceil(bh * ratio)), interpolation=cv.INTER_CUBIC)
        #     #         im, bg = composite4(bgr_img, bg, y_pred, img_cols, img_rows)
        #             im, bg = composite4(bgr_img, bg, y_pred, img_cols, img_rows)
        #     #         cv.imwrite('images/{}_compose.png'.format(filename[6:]), im)
        #     #         cv.imwrite('images/{}_new_bg.png'.format(i), bg)

        print("Time: {:.2f} s / img".format(time.time() - start_time))

        cv.imshow('out', out)
        cv.imshow('frame', frame)
        cv.imshow('comp', comp)
        cv.imshow('trimap', trimap)

        if cv.waitKey(1) & 0xFF == ord('q'):
            break
    cam.release()
    cam_mask.release()
    tri_video.release()
    matting_video.release()
    comp_video.release()

    cv.destroyAllWindows()