Example #1
0
    def predict(self, options):

        print('-' * 30)
        print('Loading and preprocessing test data...')
        print('-' * 30)
        create_test_data(options)

        imgs_test = load_test_data(options)
        imgs_test = imgs_test.astype('float32')

        imgs_test /= 255.  # scale masks to [0, 1]

        print('-' * 30)
        print('Loading saved weights...')
        print('-' * 30)

        model = self.get_unet()
        weight_dir = options.inputdir + '/weights'
        if not os.path.exists(weight_dir):
            os.mkdir(weight_dir)
        model.load_weights(os.path.join(weight_dir, project_name + '.h5'))

        print('-' * 30)
        print('Predicting masks on test data...')
        print('-' * 30)

        imgs_mask_test = model.predict(imgs_test, batch_size=1, verbose=1)

        npy_mask_dir = 'test_mask_npy'
        if not os.path.exists(npy_mask_dir):
            os.mkdir(npy_mask_dir)

        np.save(os.path.join(options.outputdir, project_name + '_mask.npy'),
                imgs_mask_test)

        print('-' * 30)
        print('Saving predicted masks to files...')
        print('-' * 30)

        imgs_mask_test = preprocess_squeeze(imgs_mask_test)
        # imgs_mask_test /= 1.7
        #imgs_mask_test = np.around(imgs_mask_test, decimals=0)
        #info = np.iinfo(np.uint16) # Get the information of the incoming image type
        #imgs_mask_test = imgs_mask_test.astype(np.uint16)
        #imgs_mask_test=imgs_mask_test* info.max # convert back to original class/labels
        imgs_mask_test = (imgs_mask_test * 255.).astype(np.uint8)
        count_visualize = 1
        count_processed = 0
        pred_dir = 'preds'
        if not os.path.exists(pred_dir):
            os.mkdir(pred_dir)
        pred_dir = os.path.join(options.outputdir, project_name)
        if not os.path.exists(pred_dir):
            os.mkdir(pred_dir)
        for x in range(0, imgs_mask_test.shape[0]):
            for y in range(0, imgs_mask_test.shape[1]):
                if (count_visualize > 1) and (count_visualize < 16):
                    save_img = imgs_mask_test[x][y].astype(np.uint16)
                    imsave(
                        os.path.join(pred_dir,
                                     'pred_' + str(count_processed) + '.png'),
                        save_img)
                    count_processed += 1

                count_visualize += 1
                if count_visualize == 17:
                    count_visualize = 1
                if (count_processed % 100) == 0:
                    print('Done: {0}/{1} test images'.format(
                        count_processed, imgs_mask_test.shape[0] * 14))

        print('-' * 30)
        print('Prediction finished')
        print('-' * 30)
def predict():

    print('-' * 30)
    print('Loading and preprocessing test data...')
    print('-' * 30)

    imgs_test = load_test_data()
    imgs_test = imgs_test.astype('float32')

    imgs_test /= 255.  # scale masks to [0, 1]

    print('-' * 30)
    print('Loading saved weights...')
    print('-' * 30)

    model = get_unet()
    weight_dir = 'weights'
    if not os.path.exists(weight_dir):
        os.mkdir(weight_dir)
    model.load_weights(os.path.join(weight_dir, project_name + '.h5'))

    print('-' * 30)
    print('Predicting masks on test data...')
    print('-' * 30)

    imgs_mask_test = model.predict(imgs_test, batch_size=1, verbose=1)

    npy_mask_dir = 'test_mask_npy'
    if not os.path.exists(npy_mask_dir):
        os.mkdir(npy_mask_dir)

    np.save(os.path.join(npy_mask_dir, project_name + '_mask.npy'),
            imgs_mask_test)

    print('-' * 30)
    print('Saving predicted masks to files...')
    print('-' * 30)

    imgs_mask_test = preprocess_squeeze(imgs_mask_test)
    # imgs_mask_test /= 1.7
    imgs_mask_test = np.around(imgs_mask_test, decimals=0)
    imgs_mask_test = (imgs_mask_test * 255.).astype(np.uint8)
    count_visualize = 1
    count_processed = 0
    pred_dir = 'preds'
    if not os.path.exists(pred_dir):
        os.mkdir(pred_dir)
    pred_dir = os.path.join('preds/', project_name)
    if not os.path.exists(pred_dir):
        os.mkdir(pred_dir)
    for x in range(0, imgs_mask_test.shape[0]):
        for y in range(0, imgs_mask_test.shape[1]):
            if (count_visualize > 1) and (count_visualize < 16):
                imsave(
                    os.path.join(
                        pred_dir,
                        'pred_' + str(f"{count_processed:03}") + '.png'),
                    imgs_mask_test[x][y])
                count_processed += 1

            count_visualize += 1
            if count_visualize == 17:
                count_visualize = 1
            if (count_processed % 100) == 0:
                print('Done: {0}/{1} test images'.format(
                    count_processed, imgs_mask_test.shape[0] * 14))

    print('-' * 30)
    print('Prediction finished')
    print('-' * 30)
Example #3
0
    def predict(self, options):
        print('-' * 30)
        print('Loading and preprocessing test data...')
        print('-' * 30)
        create_test_data(options)
        imgs_test = load_test_data(options)
        imgs_test = imgs_test.astype('float32')
        print('-' * 30)
        print('Test npy size:' + str(imgs_test.shape))
        print('-' * 30)

        imgs_test /= 255.  # scale masks to [0, 1]

        print('-' * 30)
        print('Loading saved weights...')
        print('-' * 30)

        model = self.get_unet()
        if not os.path.exists(options.inputdir + '/weights'):
            print('Weight not found.')
            return

        weight_dir = os.listdir(options.inputdir + '/weights')

        total = 0
        for weight in weight_dir:
            if not weight.endswith('.h5'):
                print('Invalid file: ' + weight)
                print('-' * 30)
                continue
            pred_name = weight.split('.')[0]
            total += 1
            print(weight)
            model.load_weights(
                os.path.join(options.inputdir + '/weights', weight))

            print('Predicting masks using:', weight)
            print('-' * 30)

            imgs_mask_test = model.predict(imgs_test, batch_size=1, verbose=1)

            if not os.path.exists(options.outputdir):
                os.mkdir(options.outputdir)

            np.save(os.path.join(options.outputdir, pred_name + '_mask.npy'),
                    imgs_mask_test)

            print('Saving predicted masks to files...')
            print('-' * 30)

            imgs_mask_test = preprocess_squeeze(imgs_mask_test)
            imgs_mask_test = (imgs_mask_test * 255.).astype(np.uint8)
            count_visualize = 1
            count_processed = 0
            pred_dir = os.path.join(options.outputdir, pred_name)
            if not os.path.exists(pred_dir):
                os.mkdir(pred_dir)
            for x in range(0, imgs_mask_test.shape[0]):
                for y in range(0, imgs_mask_test.shape[1]):
                    if (count_visualize > 1) and (count_visualize < 16):
                        save_img = imgs_mask_test[x][y].astype(np.uint16)
                        imsave(
                            os.path.join(
                                pred_dir,
                                'pred_' + str(count_processed) + '.png'),
                            save_img)
                        count_processed += 1

                    count_visualize += 1
                    if count_visualize == 17:
                        count_visualize = 1
                    if (count_processed % 100) == 0:
                        print('Done: {0}/{1} test images'.format(
                            count_processed, imgs_mask_test.shape[0] * 14))
            print('-' * 30)
            print(pred_name, ' Done.')
            print('-' * 30)
        print('Prediction done: using ' + str(total) + ' models.')