Skip to content

Commit b979846

Browse files
committed
test: 重构测试用例并添加新功能
- 删除了 test_clip_base.py 和 test_clip_feature.py 两个测试文件 - 新增 test_clip.py 综合测试文件,包含以下功能: - Clip 类初始化和设备选择的单元测试 - 图像和文本嵌入功能的单元测试 - 图像相似度比较功能的单元测试 - 多线程 executor 的单元测试 - 优化了测试用例结构,提高了测试覆盖率 - 添加了更多错误处理和边界条件的测试
1 parent bfd07e7 commit b979846

8 files changed

Lines changed: 497 additions & 424 deletions

File tree

README.md

Lines changed: 30 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ Similarity between images: 87.5%
192192
全局参数(适用于所有子命令):
193193
- `-d DB_NAME`:指定数据库名称(默认:`default`
194194
- `-B BIND`:服务绑定地址(默认:`~/.isearch/isearch.sock`;TCP 格式:`host:port`
195-
- `-v, --version`:显示版本(当前:0.1.1
195+
- `-v, --version`:显示版本(当前:0.2.0
196196

197197
子命令特定参数见上文。
198198

@@ -246,19 +246,19 @@ print(f'清空成功: {cleared}')
246246

247247
ImgSearch 支持多种 TinyCLIP 模型变体,平衡速度、准确率和资源。默认 `ViT-45LY` 适用于大多数场景。
248248

249-
| 模型键 | ImageNet-1K Acc@1 (%) | MACs (G) | Throughput (pairs/s) | 推荐场景 |
250-
|----------|-----------------------|----------|----------------------|-----------------------------|
251-
| ViT-8Y | 41.1 | 2.0 | 4,150 | 移动/低资源,快速但基本准确 |
252-
| RN-19L | 56.4 | 4.4 | 3,024 | 平衡,CPU 友好 |
253-
| ViT-22L | 53.7 | 1.9 | 5,504 | 高速搜索 |
254-
| RN-30L | 59.1 | 6.9 | 1,811 | 中等准确,GPU 加速 |
255-
| ViT-39Y | 63.5 | 9.5 | 1,469 | 高准确,大型数据集 |
256-
| ViT-40L | 59.8 | 3.5 | 4,641 | 通用桌面 |
257-
| ViT-45L | 61.4 | 3.7 | 3,682 | 平衡(默认备选) |
258-
| ViT-45LY | 62.7 | 1.9 | 3,685 | **默认:最佳平衡** |
259-
| ViT-61L | 62.4 | 5.3 | 3,191 | 高准确,中等速度 |
260-
| ViT-63L | 63.9 | 5.6 | 2,905 | 生产环境,高召回 |
261-
| ViT-63LY | 64.5 | 5.6 | 2,909 | 最高准确,资源密集 |
249+
| 模型键 | ImageNet-1K Acc@1 (%) | MACs (G) | Throughput (pairs/s) | 推荐场景 |
250+
|----------|-----------------------|----------|----------------------|------------------------------------|
251+
| ViT-8Y | 41.1 | 2.0 | 4,150 | 资源消耗最低,速度快,准确度略低 |
252+
| RN-19L | 56.4 | 4.4 | 3,024 | |
253+
| ViT-22L | 53.7 | 1.9 | 5,504 | 速度最快,适合对速度要求高的场景 |
254+
| RN-30L | 59.1 | 6.9 | 1,811 | |
255+
| ViT-39Y | 63.5 | 9.5 | 1,469 | 准确度高,资源消耗中等,但速度较慢 |
256+
| ViT-40L | 59.8 | 3.5 | 4,641 | |
257+
| ViT-45L | 61.4 | 3.7 | 3,682 | |
258+
| ViT-45LY | 62.7 | 1.9 | 3,685 | **默认:最佳均衡**,速度与精度兼备 |
259+
| ViT-61L | 62.4 | 5.3 | 3,191 | |
260+
| ViT-63L | 63.9 | 5.6 | 2,905 | |
261+
| ViT-63LY | 64.5 | 5.6 | 2,909 | 准确度最高 |
262262

263263
数据来源:TinyCLIP 模型库。选择小模型(如 ViT-8Y)用于低端设备;大模型(如 ViT-63LY)用于高精度需求。切换模型需重启服务:`isearch service start -m MODEL_KEY`
264264

@@ -278,41 +278,6 @@ src/imgsearch/
278278

279279
测试目录:`src/test/`(包含单元测试,使用 pytest)。
280280

281-
## 性能和故障排除
282-
283-
- **内存使用**:空数据库 ~50MB;每 1000 张图片 ~20-50MB(取决于模型)。服务启动后预加载模型,初始 ~200-500MB。
284-
- **搜索速度**~1-5ms/查询(取决于 k 和 ef 参数,CPU 上)。
285-
- **常见问题**
286-
- 服务未运行:`Failed to connect to service` - 运行 `isearch service start`
287-
- 队列满:高负载时搜索返回 None - 等待或增加并发(修改 server.py)。
288-
- 模型加载慢:首次启动预加载,GPU 加速显著。
289-
- 数据库损坏:删除 `~/.isearch/数据库名/` 重建。
290-
291-
监控:使用 `isearch service status` 查看 PID 和内存。
292-
293-
## 贡献和测试
294-
295-
欢迎贡献!运行测试:
296-
297-
```shell
298-
pytest src/test/
299-
```
300-
301-
确保代码风格一致,使用现代 Python(>=3.11)。详见 [贡献指南](CONTRIBUTING.md)(若存在)。
302-
303-
## 依赖
304-
305-
- Python >=3.11
306-
- torch >=2.2.2 (CPU/GPU)
307-
- open-clip-torch (TinyCLIP)
308-
- hnswlib >=0.8.0
309-
- pyro5 >=5.15
310-
- Pillow >=11.3.0
311-
- psutil >=7.0.0
312-
- bidict >=0.23.1
313-
- msgpack >=1.1.1
314-
- numpy (平台特定)
315-
316281
## 许可证
317282

318283
MIT License
@@ -513,7 +478,7 @@ Similarity between images: 87.5%
513478
Global args (all subcommands):
514479
- `-d DB_NAME`: Database name (default: `default`)
515480
- `-B BIND`: Service bind (default: `~/.isearch/isearch.sock`; TCP: `host:port`)
516-
- `-v, --version`: Show version (current: 0.1.1)
481+
- `-v, --version`: Show version
517482

518483
Subcommand-specific: See above.
519484

@@ -567,21 +532,21 @@ print(f'Cleared: {cleared}')
567532

568533
ImgSearch supports various TinyCLIP model variants, balancing speed, accuracy, resources. Default `ViT-45LY` for most cases.
569534

570-
| Model Key | ImageNet-1K Acc@1 (%) | MACs (G) | Throughput (pairs/s) | Recommended For |
571-
|-----------|-----------------------|----------|----------------------|-------------------------------------|
572-
| ViT-8Y | 41.1 | 2.0 | 4,150 | Mobile/low-resource, fast but basic |
573-
| RN-19L | 56.4 | 4.4 | 3,024 | Balanced, CPU-friendly |
574-
| ViT-22L | 53.7 | 1.9 | 5,504 | High-speed search |
575-
| RN-30L | 59.1 | 6.9 | 1,811 | Medium accuracy, GPU |
576-
| ViT-39Y | 63.5 | 9.5 | 1,469 | High accuracy, large data |
577-
| ViT-40L | 59.8 | 3.5 | 4,641 | General desktop |
578-
| ViT-45L | 61.4 | 3.7 | 3,682 | Balanced (default alt) |
579-
| ViT-45LY | 62.7 | 1.9 | 3,685 | **Default: Best balance** |
580-
| ViT-61L | 62.4 | 5.3 | 3,191 | High accuracy, medium speed |
581-
| ViT-63L | 63.9 | 5.6 | 2,905 | Production, high recall |
582-
| ViT-63LY | 64.5 | 5.6 | 2,909 | Highest accuracy, intensive |
583-
584-
Data from TinyCLIP model zoo. Use small models (ViT-8Y) for low-end; large (ViT-63LY) for precision. Switch: `isearch service start -m MODEL_KEY`.
535+
| Model Key | ImageNet-1K Acc@1 (%) | MACs (G) | Throughput (pairs/s) | Recommended Scenarios |
536+
|-----------|-----------------------|----------|----------------------|-----------------------------------------------------------------------|
537+
| ViT-8Y | 41.1 | 2.0 | 4,150 | Lowest resource usage, fast performance, with slightly lower accuracy |
538+
| RN-19L | 56.4 | 4.4 | 3,024 | |
539+
| ViT-22L | 53.7 | 1.9 | 5,504 | Fastest speed, ideal for scenarios with high speed requirements |
540+
| RN-30L | 59.1 | 6.9 | 1,811 | |
541+
| ViT-39Y | 63.5 | 9.5 | 1,469 | High accuracy, moderate resource consumption, but slower speed |
542+
| ViT-40L | 59.8 | 3.5 | 4,641 | |
543+
| ViT-45L | 61.4 | 3.7 | 3,682 | |
544+
| ViT-45LY | 62.7 | 1.9 | 3,685 | **Default: Best balance** of speed and accuracy |
545+
| ViT-61L | 62.4 | 5.3 | 3,191 | |
546+
| ViT-63L | 63.9 | 5.6 | 2,905 | |
547+
| ViT-63LY | 64.5 | 5.6 | 2,909 | Highest accuracy |
548+
549+
Data source: TinyCLIP model library. Choose small models (e.g., ViT-8Y) for low-end devices; large models (e.g., ViT-63LY) for high-precision needs. To switch models, restart the service: `isearch service start -m MODEL_KEY`.
585550

586551
## Directory Structure
587552

@@ -599,41 +564,6 @@ src/imgsearch/
599564

600565
Tests: `src/test/` (unit tests with pytest).
601566

602-
## Performance and Troubleshooting
603-
604-
- **Memory**: Empty DB ~50MB; per 1000 images ~20-50MB (model-dependent). Service preload ~200-500MB initial.
605-
- **Search Speed**: ~1-5ms/query (depends on k, ef; CPU).
606-
- **Common Issues**:
607-
- Service not running: `Failed to connect` - Run `isearch service start`.
608-
- Queue full: Search returns None (high load) - Wait or increase concurrency (edit server.py).
609-
- Slow model load: First start preloads; GPU speeds up.
610-
- Corrupt DB: Delete `~/.isearch/db_name/` and rebuild.
611-
612-
Monitor: `isearch service status` for PID/memory.
613-
614-
## Contributions and Testing
615-
616-
Contributions welcome! Run tests:
617-
618-
```shell
619-
pytest src/test/
620-
```
621-
622-
Follow style, use modern Python (>=3.11). See [CONTRIBUTING.md] if available.
623-
624-
## Dependencies
625-
626-
- Python >=3.11
627-
- torch >=2.2.2 (CPU/GPU)
628-
- open-clip-torch (TinyCLIP)
629-
- hnswlib >=0.8.0
630-
- pyro5 >=5.15
631-
- Pillow >=11.3.0
632-
- psutil >=7.0.0
633-
- bidict >=0.23.1
634-
- msgpack >=1.1.1
635-
- numpy (platform-specific)
636-
637567
## License
638568

639569
MIT License

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "imgsearch"
3-
version = "0.1.1"
3+
version = "0.2.0"
44
description = "ImgSearch is a lightweight image search engine that supports searching images by image."
55
readme = "README.md"
66
authors = [
@@ -14,8 +14,8 @@ classifiers = [
1414
"Programming Language :: Python :: 3.11",
1515
"Programming Language :: Python :: 3.12",
1616
"Programming Language :: Python :: 3.13",
17-
"Topic :: SCIENCE :: Artificial Intelligence :: Image Recognition",
1817
"Topic :: Scientific/Engineering :: Artificial Intelligence",
18+
"Topic :: Scientific/Engineering :: Image Recognition",
1919
"Topic :: Software Development :: Libraries :: Python Modules",
2020
"Topic :: Utilities",
2121
]

src/imgsearch/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.1.1'
1+
__version__ = '0.2.0'

src/imgsearch/clip.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
from concurrent.futures import ThreadPoolExecutor
10+
from functools import cached_property
1011

1112
import numpy as np
1213
import torch
@@ -60,14 +61,12 @@ def __init__(self, model_key: str = DEFAULT_MODEL_KEY, device: str | None = None
6061
if self.device.type == 'mps':
6162
self.model = self.model.float()
6263

63-
# Lazy thread pool for image preprocessing (I/O bound)
64-
self._executor: ThreadPoolExecutor | None = None
65-
6664
def __del__(self):
67-
if self._executor is not None:
68-
self._executor.shutdown(wait=False)
65+
if 'executor' in self.__dict__:
66+
self.executor.shutdown(wait=False)
67+
del self.executor
6968

70-
@property
69+
@cached_property
7170
def executor(self) -> ThreadPoolExecutor:
7271
"""Get thread pool executor for concurrent preprocessing (lazy init).
7372
@@ -77,9 +76,7 @@ def executor(self) -> ThreadPoolExecutor:
7776
Returns:
7877
ThreadPoolExecutor: Configured executor instance.
7978
"""
80-
if self._executor is None:
81-
self._executor = ThreadPoolExecutor(max_workers=max(cpu_count(), 2))
82-
return self._executor
79+
return ThreadPoolExecutor(max_workers=max(cpu_count(), 2))
8380

8481
@staticmethod
8582
def get_device(name: str | None = None) -> torch.device:

0 commit comments

Comments
 (0)