Skip to content

Commit

Permalink
0.17 patches (#325)
Browse files Browse the repository at this point in the history
* fixes #323 Default indexer distance is now cosine in Sim Model.

Calling create_index method now defaults to cosine distance.

Additionally, auto distance defaults to cosine if no distance is passed to compile.

* fixes #322 remove all calls to tf.convert_to_tensor in SimModel.

* Update gitignore to exclude models and datasets from the example notebooks.

* Update multi-modal notebook to remove the call to compile.

* Patch bump
  • Loading branch information
owenvallis authored Apr 7, 2023
1 parent b06cd40 commit 6b67e0a
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 134 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ release.sh
benchmark/supervised/datasets/
benchmark/supervised/models/
datasets/
multi_modal_datasets/
*.h5

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
249 changes: 137 additions & 112 deletions examples/multimodal_example.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tensorflow_similarity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__version__ = "0.17.0"
__version__ = "0.17.1"


from . import algebra # noqa
Expand Down
30 changes: 9 additions & 21 deletions tensorflow_similarity/models/similarity_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,17 @@ def compile(
"""
# Fetching the distance used from the first loss if auto
if distance == "auto":
if isinstance(loss, list):
metric_loss = loss[0]
if loss is None:
distance = distance_canonicalizer("cosine")
else:
metric_loss = loss
metric_loss = loss[0] if isinstance(loss, list) else loss

try:
distance = metric_loss.distance
except AttributeError:
msg = "distance='auto' only works if the first loss is a " "metric loss"
try:
distance = metric_loss.distance
except AttributeError:
msg = "distance='auto' only works if the first loss is a " "metric loss"
raise ValueError(msg)

raise ValueError(msg)
print(f"Distance metric automatically set to {distance} use the " "distance arg to override.")
else:
distance = distance_canonicalizer(distance)
Expand Down Expand Up @@ -265,7 +265,7 @@ def _index(self, index: Indexer) -> None:

def create_index(
self,
distance: Distance | str = "auto",
distance: Distance | str = "cosine",
search: Search | str = "nmslib",
kv_store: Store | str = "memory",
evaluator: Evaluator | str = "memory",
Expand Down Expand Up @@ -362,8 +362,6 @@ def index(
if verbose:
print(f"[Indexing {tf.shape(x)[0]} points]")
print("|-Computing embeddings")
with tf.device("/cpu:0"):
x = tf.convert_to_tensor(np.array(x))

predictions = self.predict(x)

Expand Down Expand Up @@ -430,8 +428,6 @@ def lookup(self, x: Tensor, k: int = 5, verbose: int = 1) -> list[list[Lookup]]:
list of list of k nearest neighboors:
list[list[Lookup]]
"""
with tf.device("/cpu:0"):
x = tf.convert_to_tensor(np.array(x))
predictions = self.predict(x)

return self._index.batch_lookup(predictions=predictions, k=k, verbose=verbose)
Expand Down Expand Up @@ -516,8 +512,6 @@ def calibrate(
thresholds_targets = {}

# predict
with tf.device("/cpu:0"):
x = tf.convert_to_tensor(np.array(x))
predictions = self.predict(x)

# calibrate
Expand Down Expand Up @@ -582,8 +576,6 @@ def match(
raise ValueError("Uncalibrated model: run model.calibration()")

# get predictions
with tf.device("/cpu:0"):
x = tf.convert_to_tensor(np.array(x))
predictions = self.predict(x)

# matching
Expand Down Expand Up @@ -634,8 +626,6 @@ def evaluate_retrieval(
# get embeddings
if verbose:
print("|-Computing embeddings")
with tf.device("/cpu:0"):
x = tf.convert_to_tensor(np.array(x))
predictions = self.predict(x)

if verbose:
Expand Down Expand Up @@ -713,8 +703,6 @@ def evaluate_classification(
# get embeddings
if verbose:
print("|-Computing embeddings")
with tf.device("/cpu:0"):
x = tf.convert_to_tensor(np.array(x))
predictions = self.predict(x)

results: defaultdict[str, dict[str, str | np.ndarray]] = defaultdict(dict)
Expand Down

0 comments on commit 6b67e0a

Please sign in to comment.