Skip to content

Commit

Permalink
Merge pull request Vladkryvoruchko#74 from Vladkryvoruchko/feat/tests
Browse files Browse the repository at this point in the history
Feat/tests
  • Loading branch information
Vladkryvoruchko authored Sep 24, 2019
2 parents 33c6fbd + 0e66b9c commit 436d740
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 32 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
make test:
pytest -vv -s --disable-warnings --tb short -n 4 tests/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ List of arguments:




61 changes: 32 additions & 29 deletions pspnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,33 +213,9 @@ def __init__(self, nb_classes, weights, input_shape):
input_shape=input_shape, weights=weights)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--model', type=str, default='pspnet101_voc2012',
help='Model/Weights to use',
choices=['pspnet50_ade20k',
'pspnet101_cityscapes',
'pspnet101_voc2012'])
parser.add_argument('-w', '--weights', type=str, default=None)
parser.add_argument('-i', '--input_path', type=str, default='example_images/ade20k.jpg',
help='Path the input image')
parser.add_argument('-g', '--glob_path', type=str, default=None,
help='Glob path for multiple images')
parser.add_argument('-o', '--output_path', type=str, default='example_results/ade20k.jpg',
help='Path to output')
parser.add_argument('--id', default="0")
parser.add_argument('--input_size', type=int, default=500)
parser.add_argument('-s', '--sliding', action='store_true',
help="Whether the network should be slided over the original image for prediction.")
parser.add_argument('-f', '--flip', action='store_true', default=True,
help="Whether the network should predict on both image and flipped image.")
parser.add_argument('-ms', '--multi_scale', action='store_true',
help="Whether the network should predict on multiple scales.")

args = parser.parse_args()

def main(args):
# Handle input and output args
images = glob(args.glob_path) if args.glob_path else [args.input_path,]
images = glob(args.glob_path) if args.glob_path else [args.input_path, ]
if args.glob_path:
fn, ext = splitext(args.output_path)
if ext:
Expand Down Expand Up @@ -278,17 +254,16 @@ def __init__(self, nb_classes, weights, input_shape):
EVALUATION_SCALES = [0.5, 0.75, 1.0, 1.25, 1.5, 1.75] # must be all floats! Taken from original paper

for i, img_path in enumerate(images):
print("Processing image {} / {}".format(i+1,len(images)))
print("Processing image {} / {}".format(i + 1, len(images)))
img = imread(img_path, pilmode='RGB')

#probs = pspnet.predict(img, args.flip)
probs = pspnet.predict_multi_scale(img, args.flip, args.sliding, EVALUATION_SCALES)

cm = np.argmax(probs, axis=2)
pm = np.max(probs, axis=2)

colored_class_image = utils.color_class_image(cm, args.model)
alpha_blended = 0.5 * colored_class_image * 255 + 0.5 * img
alpha_blended = 0.5 * colored_class_image + 0.5 * img

if args.glob_path:
input_filename, ext = splitext(basename(img_path))
Expand All @@ -300,3 +275,31 @@ def __init__(self, nb_classes, weights, input_shape):
misc.imsave(filename + "_seg" + ext, colored_class_image)
misc.imsave(filename + "_probs" + ext, pm)
misc.imsave(filename + "_seg_blended" + ext, alpha_blended)

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--model', type=str, default='pspnet101_voc2012',
help='Model/Weights to use',
choices=['pspnet50_ade20k',
'pspnet101_cityscapes',
'pspnet101_voc2012'])
parser.add_argument('-w', '--weights', type=str, default=None)
parser.add_argument('-i', '--input_path', type=str, default='example_images/ade20k.jpg',
help='Path the input image')
parser.add_argument('-g', '--glob_path', type=str, default=None,
help='Glob path for multiple images')
parser.add_argument('-o', '--output_path', type=str, default='example_results/ade20k.jpg',
help='Path to output')
parser.add_argument('--id', default="0")
parser.add_argument('--input_size', type=int, default=500)
parser.add_argument('-s', '--sliding', action='store_true',
help="Whether the network should be slided over the original image for prediction.")
parser.add_argument('-f', '--flip', action='store_true', default=True,
help="Whether the network should predict on both image and flipped image.")
parser.add_argument('-ms', '--multi_scale', action='store_true',
help="Whether the network should predict on multiple scales.")

args = parser.parse_args()

main(args)

6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
numpy
scipy==1.3
tensorflow==1.0.0
#tensorflow-gpu
tensorflow-gpu
keras==2.1.1
imageio==2.5.0
opencv-python=4.1.1
opencv-python=4.1.1
pytest==5.1.3
pytest-xdist==1.29.0
File renamed without changes
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import argparse

import pytest


@pytest.fixture
def cli_args_ade():
args = argparse.Namespace(flip=False, glob_path=None, id='0', input_path='tests/ade20k_test.jpg', input_size=500, model='pspnet50_ade20k', output_path='tests/ade20k_test.jpg', sliding=False, weights=None, multi_scale=False)
return args

@pytest.fixture
def cli_args_cityscapes():
args = argparse.Namespace(flip=False, glob_path=None, id='0', input_path='tests/cityscapes_test.jpg', input_size=500, model='pspnet101_cityscapes', output_path='tests/cityscapes_test.jpg', sliding=False, weights=None, multi_scale=False)
return args

@pytest.fixture
def cli_args_voc():
args = argparse.Namespace(flip=False, glob_path=None, id='0', input_path='tests/pascal_voc_test.jpg', input_size=500, model='pspnet101_voc2012', output_path='tests/pascal_voc_test.jpg', sliding=False, weights=None, multi_scale=False)
return args
60 changes: 60 additions & 0 deletions tests/test_smoke.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os
import pytest

import numpy as np
from imageio import imread


def compare_2_images(validator_path, output_path):
val_abs_path = os.path.join(os.path.dirname(__file__), validator_path)
out_abs_path = os.path.join(os.path.dirname(__file__), output_path)
val_img = imread(val_abs_path, pilmode='RGB')
out_img = imread(out_abs_path, pilmode='RGB')
assert np.all(np.equal(val_img, out_img))

def clean_test_results(output_file_no_ext):
os.remove("tests/" + output_file_no_ext + "_probs.jpg")
os.remove("tests/" + output_file_no_ext + "_seg.jpg")
os.remove("tests/" + output_file_no_ext + "_seg_blended.jpg")
os.remove("tests/" + output_file_no_ext + "_seg_read.jpg")

def test_main_flip_ade20k(cli_args_ade):
from pspnet import main
main(cli_args_ade)
compare_2_images("ade20k_test_probs.jpg", "validators/ade20k_test_probs.jpg")
compare_2_images("ade20k_test_seg.jpg", "validators/ade20k_test_seg.jpg")
compare_2_images("ade20k_test_seg_read.jpg", "validators/ade20k_test_seg_read.jpg")
clean_test_results("ade20k_test")


@pytest.mark.skip
def test_main_flip_cityscapes(cli_args_cityscapes):
"""
TODO: Add images
:param cli_args_cityscapes:
:return:
"""
from pspnet import main
main(cli_args_cityscapes)

compare_2_images("cityscapes_test_probs.jpg", "validators/cityscapes_test_probs.jpg")
compare_2_images("cityscapes_test_seg.jpg", "validators/cityscapes_test_seg.jpg")
compare_2_images("cityscapes_test_seg_read.jpg", "validators/cityscapes_test_seg_read.jpg")
clean_test_results("cityscapes_test")



@pytest.mark.skip
def test_main_flip_voc(cli_args_voc):
"""
TODO: Add images
:param cli_args_voc:
:return:
"""
from pspnet import main
main(cli_args_voc)

compare_2_images("pascal_voc_test_probs.jpg", "validators/pascal_voc_test_probs.jpg")
compare_2_images("pascal_voc_test_seg.jpg", "validators/pascal_voc_test_seg.jpg")
compare_2_images("pascal_voc_test_seg_read.jpg", "validators/pascal_voc_test_seg_read.jpg")
clean_test_results("pascal_voc_test")
Binary file added tests/validators/ade20k_test_probs.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/validators/ade20k_test_seg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/validators/ade20k_test_seg_blended.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/validators/ade20k_test_seg_read.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 436d740

Please sign in to comment.