Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Resnet support added #246

Merged
merged 9 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions fastembed/image/onnx_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
"hf": "Qdrant/clip-ViT-B-32-vision",
},
"model_file": "model.onnx",
},
{
"model": "AndrewOgn/resnet_onnx",
"dim": 2048,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just as a reminder: we might want to inspect other resnet models to have lower dimensionality

"description": "ResNet-50 from `Deep Residual Learning for Image Recognition <https://arxiv.org/abs/1512.03385>`__.",
"size_in_GB": 0.1,
"sources": {
"hf": "AndrewOgn/resnet_onnx",
},
"model_file": "model.onnx",
}
]

Expand Down
5 changes: 4 additions & 1 deletion fastembed/image/onnx_image_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ def load_onnx_model(
)
self.processor = load_preprocessor(model_dir=model_dir)

def _build_onnx_input(self, encoded: np.ndarray) -> Dict[str, np.ndarray]:
return {node.name: encoded for node in self.model.get_inputs()}

def onnx_embed(self, images: List[PathInput]) -> np.ndarray:
with contextlib.ExitStack():
image_files = [Image.open(image) for image in images]
encoded = self.processor(image_files)
onnx_input = {"pixel_values": encoded}
onnx_input = self._build_onnx_input(encoded)
onnx_input = self._preprocess_onnx_input(onnx_input)

model_output = self.model.run(None, onnx_input)
Expand Down
5 changes: 5 additions & 0 deletions fastembed/image/transform/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def __init__(self, scale: float = 1 / 255):
def __call__(self, images: List[np.ndarray]) -> List[np.ndarray]:
return [rescale(image, scale=self.scale) for image in images]

class PILtoNDarray(Transform):
def __call__(self, images: List[np.ndarray]) -> List[np.ndarray]:
return [np.asarray(image).swapaxes(2, 1) for image in images]

class Compose:
def __init__(self, transforms: List[Transform]):
Expand Down Expand Up @@ -96,6 +99,8 @@ def from_config(cls, config: Dict[str, Any]) -> "Compose":
else:
raise ValueError(f"Invalid crop size: {crop_size}")
transforms.append(CenterCrop(size=crop_size))
if config.get("PIL_to_ndarray", False):
transforms.append(PILtoNDarray())
if config.get("do_rescale", True):
rescale_factor = config.get("rescale_factor", 1 / 255)
transforms.append(Rescale(scale=rescale_factor))
Expand Down
Loading