This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[WIP] kwargs flow through for Image Similarity -> Nearest Neighbors #3233
Open
nishantnath
wants to merge
6
commits into
apple:main
Choose a base branch
from
nishantnath:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c36a277
kwargs flow through for image_similarity.create()
nishantnath ee6751f
Merge pull request #1 from nishantnath/img_sim_kwargs_patch
nishantnath 0ec2937
adding link to nearest neighbors doc
nishantnath 713f7b0
Merge pull request #2 from nishantnath/nishantnath-patch-2
nishantnath 625d104
patches for tests
nishantnath 304e82e
Merge pull request #3 from nishantnath/nishantnath-patch-3
nishantnath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -251,21 +251,25 @@ def get_psnr(x, y): | |
) | ||
|
||
# Get model distances for comparison | ||
img = data[0:1][self.feature][0] | ||
img_fixed = tc.image_analysis.resize(img, *reversed(self.input_image_shape)) | ||
tc_ret = self.model.query(img_fixed, k=data.num_rows()) | ||
|
||
if _mac_ver() >= (10, 13): | ||
from PIL import Image as _PIL_Image | ||
|
||
pil_img = _PIL_Image.fromarray(img_fixed.pixel_data) | ||
coreml_ret = coreml_model.predict({"awesome_image": pil_img}) | ||
|
||
# Compare distances | ||
coreml_distances = np.array(coreml_ret["distance"]) | ||
tc_distances = tc_ret.sort("reference_label")["distance"].to_numpy() | ||
psnr_value = get_psnr(coreml_distances, tc_distances) | ||
self.assertTrue(psnr_value > 50) | ||
if self.feature == "awesome_image": | ||
img = data[0:1][self.feature][0] | ||
img_fixed = tc.image_analysis.resize(img, *reversed(self.input_image_shape)) | ||
tc_ret = self.model.query(img_fixed, k=data.num_rows()) | ||
|
||
if _mac_ver() >= (10, 13): | ||
from PIL import Image as _PIL_Image | ||
|
||
pil_img = _PIL_Image.fromarray(img_fixed.pixel_data) | ||
coreml_ret = coreml_model.predict({"awesome_image": pil_img}) | ||
|
||
# Compare distances | ||
coreml_distances = np.array(coreml_ret["distance"]) | ||
tc_distances = tc_ret.sort("reference_label")["distance"].to_numpy() | ||
psnr_value = get_psnr(coreml_distances, tc_distances) | ||
self.assertTrue(psnr_value > 50) | ||
else: | ||
# Broad else clause to ignore features not supported in coreml | ||
pass | ||
|
||
def test_save_and_load(self): | ||
with test_util.TempDirectory() as filename: | ||
|
@@ -287,6 +291,60 @@ def test_save_and_load(self): | |
print("Export coreml passed") | ||
|
||
|
||
class ImageSimilarityTestWithKwargs(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(self, input_image_shape=(3, 224, 224), model="resnet-50"): | ||
""" | ||
The setup class method for the basic test case with all default values. | ||
""" | ||
self.feature = "awesome_image" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need the majority of the code in this |
||
self.label = None | ||
self.input_image_shape = input_image_shape | ||
self.pre_trained_model = model | ||
|
||
# Create the model | ||
self.def_opts = { | ||
"model": "resnet-50", | ||
"verbose": True, | ||
} | ||
|
||
# Model | ||
self.model = tc.image_similarity.create( | ||
data, feature=self.feature, label=None, model=self.pre_trained_model, | ||
method='lsh', distance='squared_euclidean' | ||
) | ||
self.nn_model = self.model.feature_extractor | ||
self.lm_model = self.model.similarity_model | ||
self.opts = self.def_opts.copy() | ||
|
||
# Answers | ||
self.get_ans = { | ||
"similarity_model": lambda x: type(x) | ||
== tc.nearest_neighbors.NearestNeighborsModel, | ||
"feature": lambda x: x == self.feature, | ||
"training_time": lambda x: x > 0, | ||
"input_image_shape": lambda x: x == self.input_image_shape, | ||
"label": lambda x: x == self.label, | ||
"feature_extractor": lambda x: callable(x.extract_features), | ||
"num_features": lambda x: x == self.lm_model.num_features, | ||
"num_examples": lambda x: x == self.lm_model.num_examples, | ||
"model": lambda x: ( | ||
x == self.pre_trained_model | ||
or ( | ||
self.pre_trained_model == "VisionFeaturePrint_Screen" | ||
and x == "VisionFeaturePrint_Scene" | ||
) | ||
), | ||
} | ||
self.fields_ans = self.get_ans.keys() | ||
|
||
def assertModelWorks(self): | ||
self.assertEqual(self.model.similarity_model.distance[0][1], | ||
'squared_euclidean' | ||
) | ||
|
||
|
||
|
||
class ImageSimilaritySqueezeNetTest(ImageSimilarityTest): | ||
@classmethod | ||
def setUpClass(self): | ||
|
@@ -306,7 +364,7 @@ def setUpClass(self): | |
) | ||
|
||
|
||
# A test to gaurantee that old code using the incorrect name still works. | ||
# A test to guarantee that old code using the incorrect name still works. | ||
@unittest.skipIf( | ||
_mac_ver() < (10, 14), "VisionFeaturePrint_Scene only supported on macOS 10.14+" | ||
) | ||
|
@@ -316,3 +374,12 @@ def setUpClass(self): | |
super(ImageSimilarityVisionFeaturePrintSceneTest_bad_name, self).setUpClass( | ||
model="VisionFeaturePrint_Screen", input_image_shape=(3, 299, 299) | ||
) | ||
|
||
|
||
# A test to ensure kwargs are still accepted in create() | ||
class ImageSimilarityCreateKwargsTest(ImageSimilarityTest): | ||
@classmethod | ||
def setUpClass(self): | ||
super(ImageSimilarityCreateKwargsTest, self).setUpClass( | ||
model="resnet-50", input_image_shape=(3, 300, 300) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are you trying to accomplish by this change?