Skip to content

Commit ecdf3df

Browse files
authored
Merge pull request #80 from tue-robotics/feat/test
Added test for image_recognition_tensorflow
2 parents 1d72ee0 + 8960525 commit ecdf3df

File tree

1,987 files changed

+93
-46
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,987 files changed

+93
-46
lines changed

.travis.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ matrix:
77
- name: "Kinetic"
88
env: ROS_DISTRO=kinetic
99

10-
- name: "Melodic"
11-
env: ROS_DISTRO=melodic
12-
1310
install:
1411
- git clone https://github.com/ros-industrial/industrial_ci.git .ci_config
1512

image_recognition_tensorflow/CMakeLists.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ catkin_python_setup()
77

88
catkin_package()
99

10-
# Test catkin lint
11-
find_program(CATKIN_LINT catkin_lint REQUIRED)
12-
execute_process(COMMAND "${CATKIN_LINT}" "-q" "-W2" "${CMAKE_SOURCE_DIR}" RESULT_VARIABLE lint_result)
13-
if(NOT ${lint_result} EQUAL 0)
14-
message(FATAL_ERROR "catkin_lint failed")
10+
11+
if (CATKIN_ENABLE_TESTING)
12+
# Test catkin lint
13+
find_program(CATKIN_LINT catkin_lint REQUIRED)
14+
execute_process(COMMAND "${CATKIN_LINT}" "-q" "-W2" "${CMAKE_SOURCE_DIR}" RESULT_VARIABLE lint_result)
15+
if(NOT ${lint_result} EQUAL 0)
16+
message(FATAL_ERROR "catkin_lint failed")
17+
endif()
18+
19+
catkin_add_nosetests(test)
1520
endif()
1621

1722
install(PROGRAMS
@@ -20,4 +25,4 @@ install(PROGRAMS
2025
scripts/object_recognition_node
2126
scripts/retrain
2227
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
23-
)
28+
)

image_recognition_tensorflow/scripts/evaluate_classifier

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ The script will output a .csv that can be evaluated. """
55

66
import argparse
77

8-
from image_recognition_tensorflow.object_recognizer import ObjectRecognizer
9-
from image_recognition_util.image_reader import read_annotated
10-
from image_recognition_util.classification_score_matrix import ClassificationScoreMatrix
8+
from image_recognition_tensorflow.evaluate_classifier import evaluate_classifier
119

1210
# Assign description to the help doc
1311
parser = argparse.ArgumentParser(description='Evaluate the classification performance of a TensorFlow network')
@@ -18,33 +16,10 @@ parser.add_argument('labels_path', type=str, help='Path to a file with the outpu
1816
parser.add_argument('annotated_dir', type=str, help='Use a the given directory (which must have subdirs for each class)'
1917
' and use the annotations to get some metrics about the classifier')
2018
parser.add_argument('-n', '--top_n', type=int, help='Top N results to display', default=1)
21-
parser.add_argument('--input-tensor', type=str, help='Tensor name of input image', default="Cast:0")
19+
parser.add_argument('--input-tensor', type=str, help='Tensor name of input image', default="Cast:0")
2220
parser.add_argument('--output-tensor', type=str, help='Tensor name of classifications label', default="final_result:0")
2321
parser.add_argument('-o', '--output', type=str, help='Classification result output csv. First column is ground truth '
2422
'label, other columns are the class scores', default="result.csv")
25-
2623
args = parser.parse_args()
27-
28-
object_recognizer = ObjectRecognizer(args.graph_path, args.labels_path, args.input_tensor, args.output_tensor)
29-
30-
classification_score_matrix = ClassificationScoreMatrix(object_recognizer.labels)
31-
32-
print ">> Evaluating classified with labels: {}".format(object_recognizer.labels)
33-
34-
matches = []
35-
for label, image, filename in read_annotated(args.annotated_dir):
36-
if label not in object_recognizer.labels:
37-
print ">> Skipping image with label '{}', label not present in classifier".format(label)
38-
continue
39-
40-
scores = object_recognizer.classify(image)
41-
match, best_label, best_score = classification_score_matrix.add_classification(label, scores)
42-
43-
matches.append(match)
44-
print "{}({}/{})\t{}\t{} classified as {} ({:.3f})\033[0m".format("\033[92m" if match else "\033[91m",
45-
matches.count(True), len(matches),
46-
filename, label, best_label, best_score)
47-
48-
classification_score_matrix.write_to_file(args.output)
49-
50-
print ">> Final accuracy: {}".format(float(matches.count(True)) / len(matches))
24+
evaluate_classifier(args.graph_path, args.labels_path, args.annotated_dir, args.output, args.input_tensor,
25+
args.output_tensor)

image_recognition_tensorflow/scripts/retrain

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ if __name__ == '__main__':
2424
parser.add_argument('--flip_left_right', type=bool, default=False,
2525
help="""Whether to randomly flip half of the training images horizontally.""")
2626
parser.add_argument('--random_crop', type=int, default=0,
27-
help="""A percentage determining how much of a margin to randomly crop off the training images.""")
27+
help="""A percentage determining how much of a margin to randomly crop off the training
28+
images.""")
2829
parser.add_argument('--random_scale', type=int, default=0,
2930
help="""A percentage determining how much to randomly scale up the size of the training images by.""")
3031
parser.add_argument('--random_brightness', type=int, default=0,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
3+
from __future__ import print_function
4+
5+
from image_recognition_tensorflow.object_recognizer import ObjectRecognizer
6+
from image_recognition_util.classification_score_matrix import ClassificationScoreMatrix
7+
from image_recognition_util.image_reader import read_annotated
8+
9+
10+
def evaluate_classifier(graph_path, labels_path, annotated_dir, output,
11+
input_tensor="Cast:0", output_tensor="final_result:0"):
12+
"""
13+
Evaluate a given classifier against a directory of annotated images.
14+
The script will output a .csv that can be evaluated.
15+
:param graph_path: Path to graph.pb
16+
:param labels_path: Path to labels.txt
17+
:param annotated_dir: Directory path where the annotated images are stored, to be verified
18+
:param output: Output file
19+
:param input_tensor: Input tensor name
20+
:param output_tensor: Output tensor name
21+
:return: Final accuracy
22+
"""
23+
object_recognizer = ObjectRecognizer(graph_path, labels_path, input_tensor, output_tensor)
24+
25+
classification_score_matrix = ClassificationScoreMatrix(object_recognizer.labels)
26+
27+
print("Evaluating classified with labels: {}".format(object_recognizer.labels))
28+
29+
matches = []
30+
for label, image, filename in read_annotated(annotated_dir):
31+
if label not in object_recognizer.labels:
32+
print("Skipping image with label '{}', label not present in classifier".format(label))
33+
continue
34+
35+
scores = object_recognizer.classify(image)
36+
match, best_label, best_score = classification_score_matrix.add_classification(label, scores)
37+
38+
matches.append(match)
39+
print("{}({}/{})\t{}\t{} classified as {} ({:.3f})\033[0m".format("\033[92m" if match else "\033[91m",
40+
matches.count(True), len(matches),
41+
filename, label, best_label, best_score))
42+
43+
classification_score_matrix.write_to_file(output)
44+
45+
accuracy = float(matches.count(True)) / len(matches)
46+
print("Final accuracy: {}".format(accuracy))
47+
return accuracy

image_recognition_tensorflow/src/image_recognition_tensorflow/retrain.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
from __future__ import division
55
from __future__ import print_function
66

7+
import errno
78
import os
89
import sys
910
from argparse import Namespace
1011

11-
import errno
1212
import tensorflow as tf
1313
from image_recognition_tensorflow.tf_retrain import main as tf_main
1414

@@ -25,7 +25,8 @@ def mkdir_p(path):
2525
raise
2626

2727

28-
def main(image_dir, model_dir, output_dir, steps, batch, architecture="inception_v3", flip_left_right=False, random_crop=0, random_scale=0, random_brightness=0):
28+
def main(image_dir, model_dir, output_dir, steps, batch, architecture="inception_v3", flip_left_right=False,
29+
random_crop=0, random_scale=0, random_brightness=0):
2930
tf.app.flags.FLAGS.image_dir = image_dir
3031
tf.app.flags.FLAGS.model_dir = model_dir
3132

@@ -46,8 +47,7 @@ def main(image_dir, model_dir, output_dir, steps, batch, architecture="inception
4647
tf.app.flags.FLAGS.random_scale = random_scale
4748
tf.app.flags.FLAGS.random_brightness = random_brightness
4849

49-
# Do not pass on the '-s' argument of rqt
50-
if "rqt" in sys.argv[0]:
51-
sys.argv.pop(1)
50+
# Do not pass additional flags to tf (from rqt and nose)
51+
sys.argv = [arg for arg in sys.argv if not arg.startswith("-")]
5252

5353
tf_main('')

image_recognition_tensorflow/src/image_recognition_tensorflow/tf_retrain.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,7 @@ def _progress(count, block_size, total_size):
438438
filepath, _ = urllib.request.urlretrieve(data_url, filepath, _progress)
439439
print()
440440
statinfo = os.stat(filepath)
441-
tf.logging.info('Successfully downloaded', filename, statinfo.st_size,
442-
'bytes.')
441+
#tf.logging.info('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
443442
print('Extracting file from ', filepath)
444443
tarfile.open(filepath, 'r:gz').extractall(dest_directory)
445444
else:
6.24 KB
4.4 KB
4.88 KB

0 commit comments

Comments
 (0)