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

Add remove endpoint #37

Merged
merged 6 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions cbir/api/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ async def index_image(request: Request, image: UploadFile = File()) -> None:
)


@router.delete("/images/remove")
def remove_image(request: Request, filename: str) -> None:
"""Remove an indexed image."""

database = request.app.state.database

if not database.contains(filename):
raise HTTPException(status_code=404, detail=f"{filename} not found")

database.remove(filename)


@router.post("/images/retrieve")
async def retrieve_image(
request: Request,
Expand Down
17 changes: 11 additions & 6 deletions cbir/retrieval/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def __init__(
self.resources = faiss.StandardGpuResources()
self.index = faiss.index_cpu_to_gpu(self.resources, 0, self.index)

def contains(self, name: str) -> bool:
"""Check if a filename is in the index database."""

return self.redis.get(name) is not None

def save(self) -> None:
"""Save the index to the file."""

Expand All @@ -77,22 +82,22 @@ def add(self, images: torch.Tensor, names: List[str]) -> None:

def remove(self, name: str) -> None:
"""Remove an image from the index database."""

key = self.redis.get(name).decode("utf-8")
label = int(key)

id_selector = faiss.IDSelectorRange(label, label + 1)

self.index = faiss.index_gpu_to_cpu(self.index) if self.gpu else self.index
self.index.remove_ids(id_selector)

if self.gpu:
self.index = faiss.index_gpu_to_cpu(self.index)
self.resources = faiss.StandardGpuResources()
self.index = faiss.index_cpu_to_gpu(self.resources, 0, self.index)

self.index.remove_ids(id_selector)
self.save()
self.redis.delete(key)
self.redis.delete(name)

if self.gpu:
self.index = faiss.index_cpu_to_gpu(self.resources, 0, self.index)

def index_image(self, model: Model, image: torch.Tensor, filename: str) -> None:
"""Index an image."""

Expand Down
39 changes: 30 additions & 9 deletions tests/test_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,48 @@ def test_index_image() -> None:

database_settings = DatabaseSetting.get_settings()

with open("tests/data/image.png", "rb") as image:
files = {"image": image.read()}
with TestClient(app) as client, open("tests/data/image.png", "rb") as image:
response = client.post(
"/api/images/index",
files={"image": image},
)

assert response.status_code == 200
assert os.path.isfile(database_settings.filename) is True


def test_remove_image_not_found() -> None:
"""Test remove an image that do not exist in the database."""

with TestClient(app) as client:
response = client.post("/api/images/index", files=files)
response = client.delete(
"/api/images/remove",
params={"filename": "notfound.png"},
)

assert response.status_code == 404


def test_remove_image() -> None:
"""Test remove an image from the database."""

with TestClient(app) as client:
response = client.delete(
"/api/images/remove",
params={"filename": "image.png"},
)

assert response.status_code == 200
assert os.path.isfile(database_settings.filename) is True


def test_retrieve_image() -> None:
"""Test image retrieval."""

with open("tests/data/image.png", "rb") as image:
files = {"image": image.read()}

with TestClient(app) as client:
with TestClient(app) as client, open("tests/data/image.png", "rb") as image:
response = client.post(
"/api/images/retrieve",
data={"nrt_neigh": "10"},
files=files,
files={"image": image},
)

data = response.json()
Expand Down