Spaces:
Configuration error
Configuration error
File size: 1,276 Bytes
9f26583 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import os
from typing import Optional
import torch
import torch.nn.functional as F
from PIL import Image
from torchvision import transforms
from app.models.resnet34_siamese import SiameseResNet34
from app.settings import IMAGE_SIZE, EMBED_DIM, DEVICE, MODEL_PATH
_transform = transforms.Compose([
transforms.Resize((IMAGE_SIZE, IMAGE_SIZE)),
transforms.ToTensor(),
])
def load_model(model_path: Optional[str] = None) -> SiameseResNet34:
model_path = model_path or MODEL_PATH
model = SiameseResNet34(
embed_dim=EMBED_DIM,
pretrained=False,
freeze_backbone=False,
).to(DEVICE)
state = torch.load(model_path, map_location=DEVICE)
model.load_state_dict(state)
model.eval()
return model
def image_to_tensor(image_path: str) -> torch.Tensor:
if not os.path.isfile(image_path):
raise FileNotFoundError(f"Image not found: {image_path}")
img = Image.open(image_path).convert("L")
tensor = _transform(img).unsqueeze(0) # [1,1,H,W]
return tensor
@torch.no_grad()
def make_embedding(image_path: str, model: SiameseResNet34) -> torch.Tensor:
x = image_to_tensor(image_path).to(DEVICE)
emb = model.forward_once(x)
emb = F.normalize(emb, p=2, dim=1)
return emb.squeeze(0).cpu() |