Removed some tests
This commit is contained in:
parent
72319ecbf5
commit
8ad5918229
|
@ -92,36 +92,6 @@ class ImageClassifierTest(parameterized.TestCase):
|
||||||
test_util.get_test_data_path(_IMAGE_FILE))
|
test_util.get_test_data_path(_IMAGE_FILE))
|
||||||
self.model_path = test_util.get_test_data_path(_MODEL_FILE)
|
self.model_path = test_util.get_test_data_path(_MODEL_FILE)
|
||||||
|
|
||||||
def test_create_from_file_succeeds_with_valid_model_path(self):
|
|
||||||
# Creates with default option and valid model file successfully.
|
|
||||||
with _ImageClassifier.create_from_model_path(self.model_path) as classifier:
|
|
||||||
self.assertIsInstance(classifier, _ImageClassifier)
|
|
||||||
|
|
||||||
def test_create_from_options_succeeds_with_valid_model_path(self):
|
|
||||||
# Creates with options containing model file successfully.
|
|
||||||
base_options = _BaseOptions(file_name=self.model_path)
|
|
||||||
options = _ImageClassifierOptions(base_options=base_options)
|
|
||||||
with _ImageClassifier.create_from_options(options) as classifier:
|
|
||||||
self.assertIsInstance(classifier, _ImageClassifier)
|
|
||||||
|
|
||||||
def test_create_from_options_fails_with_invalid_model_path(self):
|
|
||||||
# Invalid empty model path.
|
|
||||||
with self.assertRaisesRegex(
|
|
||||||
ValueError,
|
|
||||||
r"ExternalFile must specify at least one of 'file_content', "
|
|
||||||
r"'file_name' or 'file_descriptor_meta'."):
|
|
||||||
base_options = _BaseOptions(file_name='')
|
|
||||||
options = _ImageClassifierOptions(base_options=base_options)
|
|
||||||
_ImageClassifier.create_from_options(options)
|
|
||||||
|
|
||||||
def test_create_from_options_succeeds_with_valid_model_content(self):
|
|
||||||
# Creates with options containing model content successfully.
|
|
||||||
with open(self.model_path, 'rb') as f:
|
|
||||||
base_options = _BaseOptions(file_content=f.read())
|
|
||||||
options = _ImageClassifierOptions(base_options=base_options)
|
|
||||||
classifier = _ImageClassifier.create_from_options(options)
|
|
||||||
self.assertIsInstance(classifier, _ImageClassifier)
|
|
||||||
|
|
||||||
@parameterized.parameters(
|
@parameterized.parameters(
|
||||||
(ModelFileType.FILE_NAME, 4, _EXPECTED_CLASSIFICATION_RESULT),
|
(ModelFileType.FILE_NAME, 4, _EXPECTED_CLASSIFICATION_RESULT),
|
||||||
(ModelFileType.FILE_CONTENT, 4, _EXPECTED_CLASSIFICATION_RESULT))
|
(ModelFileType.FILE_CONTENT, 4, _EXPECTED_CLASSIFICATION_RESULT))
|
||||||
|
@ -151,141 +121,6 @@ class ImageClassifierTest(parameterized.TestCase):
|
||||||
# a context.
|
# a context.
|
||||||
classifier.close()
|
classifier.close()
|
||||||
|
|
||||||
@parameterized.parameters(
|
|
||||||
(ModelFileType.FILE_NAME, 4, _EXPECTED_CLASSIFICATION_RESULT),
|
|
||||||
(ModelFileType.FILE_CONTENT, 4, _EXPECTED_CLASSIFICATION_RESULT))
|
|
||||||
def test_classify_in_context(self, model_file_type, max_results,
|
|
||||||
expected_classification_result):
|
|
||||||
if model_file_type is ModelFileType.FILE_NAME:
|
|
||||||
base_options = _BaseOptions(file_name=self.model_path)
|
|
||||||
elif model_file_type is ModelFileType.FILE_CONTENT:
|
|
||||||
with open(self.model_path, 'rb') as f:
|
|
||||||
model_content = f.read()
|
|
||||||
base_options = _BaseOptions(file_content=model_content)
|
|
||||||
else:
|
|
||||||
# Should never happen
|
|
||||||
raise ValueError('model_file_type is invalid.')
|
|
||||||
|
|
||||||
classifier_options = _ClassifierOptions(max_results=max_results)
|
|
||||||
options = _ImageClassifierOptions(
|
|
||||||
base_options=base_options, classifier_options=classifier_options)
|
|
||||||
with _ImageClassifier.create_from_options(options) as classifier:
|
|
||||||
# Performs object detection on the input.
|
|
||||||
image_result = classifier.classify(self.test_image)
|
|
||||||
# Comparing results.
|
|
||||||
self.assertEqual(image_result, expected_classification_result)
|
|
||||||
|
|
||||||
def test_score_threshold_option(self):
|
|
||||||
classifier_options = _ClassifierOptions(score_threshold=_SCORE_THRESHOLD)
|
|
||||||
options = _ImageClassifierOptions(
|
|
||||||
base_options=_BaseOptions(file_name=self.model_path),
|
|
||||||
classifier_options=classifier_options)
|
|
||||||
with _ImageClassifier.create_from_options(options) as classifier:
|
|
||||||
# Performs image classification on the input.
|
|
||||||
image_result = classifier.classify(self.test_image)
|
|
||||||
classifications = image_result.classifications
|
|
||||||
|
|
||||||
for classification in classifications:
|
|
||||||
for entry in classification.entries:
|
|
||||||
score = entry.categories[0].score
|
|
||||||
self.assertGreaterEqual(
|
|
||||||
score, _SCORE_THRESHOLD,
|
|
||||||
f'Classification with score lower than threshold found. '
|
|
||||||
f'{classification}')
|
|
||||||
|
|
||||||
def test_max_results_option(self):
|
|
||||||
classifier_options = _ClassifierOptions(score_threshold=_SCORE_THRESHOLD)
|
|
||||||
options = _ImageClassifierOptions(
|
|
||||||
base_options=_BaseOptions(file_name=self.model_path),
|
|
||||||
classifier_options=classifier_options)
|
|
||||||
with _ImageClassifier.create_from_options(options) as classifier:
|
|
||||||
# Performs image classification on the input.
|
|
||||||
image_result = classifier.classify(self.test_image)
|
|
||||||
categories = image_result.classifications[0].entries[0].categories
|
|
||||||
|
|
||||||
self.assertLessEqual(
|
|
||||||
len(categories), _MAX_RESULTS, 'Too many results returned.')
|
|
||||||
|
|
||||||
def test_allow_list_option(self):
|
|
||||||
classifier_options = _ClassifierOptions(category_allowlist=_ALLOW_LIST)
|
|
||||||
options = _ImageClassifierOptions(
|
|
||||||
base_options=_BaseOptions(file_name=self.model_path),
|
|
||||||
classifier_options=classifier_options)
|
|
||||||
with _ImageClassifier.create_from_options(options) as classifier:
|
|
||||||
# Performs image classification on the input.
|
|
||||||
image_result = classifier.classify(self.test_image)
|
|
||||||
classifications = image_result.classifications
|
|
||||||
|
|
||||||
for classification in classifications:
|
|
||||||
for entry in classification.entries:
|
|
||||||
label = entry.categories[0].category_name
|
|
||||||
self.assertIn(label, _ALLOW_LIST,
|
|
||||||
f'Label {label} found but not in label allow list')
|
|
||||||
|
|
||||||
def test_deny_list_option(self):
|
|
||||||
classifier_options = _ClassifierOptions(category_denylist=_DENY_LIST)
|
|
||||||
options = _ImageClassifierOptions(
|
|
||||||
base_options=_BaseOptions(file_name=self.model_path),
|
|
||||||
classifier_options=classifier_options)
|
|
||||||
with _ImageClassifier.create_from_options(options) as classifier:
|
|
||||||
# Performs image classification on the input.
|
|
||||||
image_result = classifier.classify(self.test_image)
|
|
||||||
classifications = image_result.classifications
|
|
||||||
|
|
||||||
for classification in classifications:
|
|
||||||
for entry in classification.entries:
|
|
||||||
label = entry.categories[0].category_name
|
|
||||||
self.assertNotIn(label, _DENY_LIST,
|
|
||||||
f'Label {label} found but in deny list.')
|
|
||||||
|
|
||||||
def test_combined_allowlist_and_denylist(self):
|
|
||||||
# Fails with combined allowlist and denylist
|
|
||||||
with self.assertRaisesRegex(
|
|
||||||
ValueError,
|
|
||||||
r'`category_allowlist` and `category_denylist` are mutually '
|
|
||||||
r'exclusive options.'):
|
|
||||||
classifier_options = _ClassifierOptions(category_allowlist=['foo'],
|
|
||||||
category_denylist=['bar'])
|
|
||||||
options = _ImageClassifierOptions(
|
|
||||||
base_options=_BaseOptions(file_name=self.model_path),
|
|
||||||
classifier_options=classifier_options)
|
|
||||||
with _ImageClassifier.create_from_options(options) as unused_classifier:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def test_empty_classification_outputs(self):
|
|
||||||
classifier_options = _ClassifierOptions(score_threshold=1)
|
|
||||||
options = _ImageClassifierOptions(
|
|
||||||
base_options=_BaseOptions(file_name=self.model_path),
|
|
||||||
classifier_options=classifier_options)
|
|
||||||
with _ImageClassifier.create_from_options(options) as classifier:
|
|
||||||
# Performs image classification on the input.
|
|
||||||
image_result = classifier.classify(self.test_image)
|
|
||||||
self.assertEmpty(image_result.classifications[0].entries[0].categories)
|
|
||||||
|
|
||||||
def test_missing_result_callback(self):
|
|
||||||
options = _ImageClassifierOptions(
|
|
||||||
base_options=_BaseOptions(file_name=self.model_path),
|
|
||||||
running_mode=_RUNNING_MODE.LIVE_STREAM)
|
|
||||||
with self.assertRaisesRegex(ValueError,
|
|
||||||
r'result callback must be provided'):
|
|
||||||
with _ImageClassifier.create_from_options(options) as unused_classifier:
|
|
||||||
pass
|
|
||||||
|
|
||||||
@parameterized.parameters((_RUNNING_MODE.IMAGE), (_RUNNING_MODE.VIDEO))
|
|
||||||
def test_illegal_result_callback(self, running_mode):
|
|
||||||
|
|
||||||
def pass_through(unused_result: _ClassificationResult):
|
|
||||||
pass
|
|
||||||
|
|
||||||
options = _ImageClassifierOptions(
|
|
||||||
base_options=_BaseOptions(file_name=self.model_path),
|
|
||||||
running_mode=running_mode,
|
|
||||||
result_callback=pass_through)
|
|
||||||
with self.assertRaisesRegex(ValueError,
|
|
||||||
r'result callback should not be provided'):
|
|
||||||
with _ImageClassifier.create_from_options(options) as unused_classifier:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
absltest.main()
|
absltest.main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user