def main():
    with CustomObjectScope(custom_objects()):
        model1 = load_model(SAVED_MODEL1)
        model2 = load_model(SAVED_MODEL2)

    images = np.load('data/images-224.npy')
    masks = np.load('data/masks-224.npy')
    # only hair
    masks = masks[:, :, :, 0].reshape(-1, size, size)

    _, images, _, masks = train_test_split(images,
                                           masks,
                                           test_size=0.2,
                                           random_state=seed)

    for img, mask in zip(images, masks):
        batched1 = img.reshape(1, size, size, 3).astype(float)
        batched2 = img.reshape(1, size, size, 3).astype(float)

        t1 = time.time()
        pred1 = model1.predict(standardize(batched1)).reshape(size, size)
        elapsed = time.time() - t1
        print('elapsed1: ', elapsed)

        t1 = time.time()
        pred2 = model2.predict(standardize(batched2)).reshape(size, size)
        elapsed = time.time() - t1
        print('elapsed2: ', elapsed)

        dice = np_dice_coef(mask.astype(float) / 255, pred1)
        print('dice1: ', dice)

        dice = np_dice_coef(mask.astype(float) / 255, pred2)
        print('dice2: ', dice)

        if True:
            plt.subplot(2, 2, 1)
            plt.imshow(img)
            plt.subplot(2, 2, 2)
            plt.imshow(mask)
            plt.subplot(2, 2, 3)
            plt.imshow(pred1)
            plt.subplot(2, 2, 4)
            plt.imshow(pred2)
            plt.show()
def main(img_dir, only_weight, img_size):
    if only_weight != 0:
        with CustomObjectScope(custom_objects()):
            model = load_model(SAVED_MODEL)
            print("loading model file: ", SAVED_MODEL, img_size)

    else:
        weight_file = SAVED_WEIGHT
        model = MobileUNet(input_shape=(img_size, img_size, 3),
                           alpha=1,
                           alpha_up=0.25)

        model.load_weights(weight_file, by_name=True)
        print("loading weight file: ", weight_file, img_size)

    model.summary()

    img_files = glob(img_dir + '/*.jpg')
    img_files = sorted(img_files, key=os.path.getmtime)

    for img_file in reversed(img_files):
        img = imread(img_file)
        img = imresize(img, (img_size, img_size))

        mask_file = re.sub('img2.jpg$', 'msk1.png', img_file)

        mask = imread(mask_file)

        mask = imresize(mask, (img_size, img_size), interp='nearest')
        mask1 = mask[:, :, 0]

        batched1 = img.reshape(1, img_size, img_size, 3).astype(float)
        pred1 = model.predict(standardize(batched1)).reshape(
            img_size, img_size)

        mask1 = mask1.astype(float) / 255

        dice = np_dice_coef(mask1, pred1)

        print('dice1: ', dice)
        print('sum ', np.sum(mask1))
        print('shap ', mask1.shape)

        pred1 = beautify(pred1)

        if True:
            plt.subplot(2, 2, 1)
            plt.imshow(img)
            #plt.subplot(2, 2, 2)
            #plt.imshow(mask)
            plt.subplot(2, 2, 3)
            plt.imshow(pred1)
            plt.subplot(2, 2, 4)
            plt.imshow(mask1)
            plt.show()
示例#3
0
def main(weight_file):
    model = MobileUNet(input_shape=(128, 128, 3), alpha=1, alpha_up=0.25)

    model.summary()

    model.load_weights(weight_file, by_name=True)

    images = np.load('data/id_pack/images-128.npy')
    masks = np.load('data/id_pack/masks-128.npy')
    # only hair
    masks = masks[:, :, :, 0].reshape(-1, size, size)

    _, images, _, masks = train_test_split(images,
                                           masks,
                                           test_size=0.2,
                                           random_state=seed)

    for img, mask in zip(images, masks):
        batched1 = img.reshape(1, size, size, 3).astype(float)
        batched2 = img.reshape(1, size, size, 3).astype(float)

        t1 = time.time()
        pred1 = model.predict(standardize(batched1)).reshape(size, size)
        elapsed = time.time() - t1
        print('elapsed1: ', elapsed)

        dice = np_dice_coef(mask.astype(float) / 255, pred1)
        print('dice1: ', dice)

        if True:
            plt.subplot(2, 2, 1)
            plt.imshow(img)
            plt.subplot(2, 2, 2)
            plt.imshow(mask)
            plt.subplot(2, 2, 3)
            plt.imshow(pred1)
            plt.show()