Skip to content

Commit

Permalink
Fixed odd size tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjanag committed Feb 1, 2024
1 parent ec1fd57 commit 1cd9f1a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
18 changes: 11 additions & 7 deletions src/cleanvision/issue_managers/image_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,20 @@ def get_scores(
q3 + iqr_factor * size_iqr,
)
mid_threshold = (min_threshold + max_threshold) / 2
threshold_distance = (max_threshold - min_threshold) / 2
threshold_gap = max_threshold - min_threshold
distance = np.absolute(size - mid_threshold)

if threshold_distance > 0:
norm_dist = (distance * self.threshold) / threshold_distance
score_values = 1 - np.clip(norm_dist, 0, 1)
if threshold_gap > 0:
norm_value = threshold_gap
self.threshold = 0.5
elif threshold_gap == 0:
norm_value = mid_threshold
self.threshold = 1.0
else:
norm_value = np.min(distance) / 0.5
norm_dist = distance / norm_value
score_values = 1 - np.clip(norm_dist, 0, 1)
raise ValueError("threshold_gap should be non negative")

norm_dist = distance / norm_value
score_values = 1 - np.clip(norm_dist, 0, 1)

scores = pd.DataFrame(index=raw_scores.index)
scores[get_score_colname(issue_type)] = score_values
Expand Down
27 changes: 15 additions & 12 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def test_odd_size_too_large_image(generate_local_dataset_once):
sqrt(width * height)*threshold(default 10),is_odd_size_issue is set to True. In this example, the median area is sqrt(300x300) so 300.
An image with 3001 x 3001 has an value of 3001 so its more than 10x smaller and thus should be flagged.
"""
arr = np.random.randint(low=0, high=256, size=(3001, 3001, 3), dtype=np.uint8)
arr = np.random.randint(low=0, high=256, size=(400, 400, 3), dtype=np.uint8)
img = Image.fromarray(arr, mode="RGB")
img.save(Path(generate_local_dataset_once / "class_0" / "larger.png"))

Expand All @@ -244,7 +244,7 @@ def test_odd_size_too_small_image(generate_local_dataset_once):
arr = np.random.randint(
low=0,
high=256,
size=(29, 29, 3),
size=(200, 200, 3),
dtype=np.uint8, # 30 x 30 pixel image should be detected
)
img = Image.fromarray(arr, mode="RGB")
Expand All @@ -265,23 +265,26 @@ def test_custom_threshold_for_odd_size(generate_local_dataset_once):
With default threshold the small image would be flagged (See test_filepath_dataset_size_to_small). However,
with a custom threshold of 11 instead of 10, the imaage is within the allowed range and should not be flagged.
"""
arr = np.random.randint(
low=0,
high=256,
size=(29, 29, 3),
dtype=np.uint8, # 29 x 29 pixel image should not be detected with threshold 11
)
img = Image.fromarray(arr, mode="RGB")
img.save(Path(generate_local_dataset_once / "class_0" / "smaller.png"))
for i in range(5):
arr = np.random.randint(
low=0,
high=256,
size=(100 * (i + 1), 100 * (i + 1), 3),
dtype=np.uint8,
)
img = Image.fromarray(arr, mode="RGB")
img.save(Path(generate_local_dataset_once / "class_0" / f"odd_{i}.png"))

files = os.listdir(generate_local_dataset_once / "class_0")
filepaths = [
os.path.join(generate_local_dataset_once / "class_0", f) for f in files
]
imagelab = Imagelab(filepaths=filepaths)
imagelab.find_issues({"odd_size": {"threshold": 11.0}})
imagelab.find_issues(
{"odd_size": {"threshold": 0.5}}
) # for this case default threshold is 1.0
assert len(imagelab.issues.columns) == 2 # Only size
assert len(imagelab.issues[imagelab.issues["is_odd_size_issue"]]) == 0
assert len(imagelab.issues[imagelab.issues["is_odd_size_issue"]]) == 2


def test_list_default_issue_types():
Expand Down

0 comments on commit 1cd9f1a

Please sign in to comment.