Skip to content

Commit 311b4dd

Browse files
shiyu22Chiiizzzy
authored andcommitted
Merge pull request #15 from Chiiizzzy/image_copy
Update image copy bootcamp
2 parents c9e665c + 6da8849 commit 311b4dd

File tree

6 files changed

+36
-20
lines changed

6 files changed

+36
-20
lines changed

codelabs/14-image-deduplication/index.md renamed to codelabs/14-image-copy-detection/index.md

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,19 @@ A generic embedding model turns images into dense vectors; an encoder-based embe
3131
To accomplish this, these encoder models shouldn't be trained on traditional image recognition/localization datasets such as CIFAR or ImageNet; instead, a siamese network trained with contrastive or triplet loss must be used. Among all these encoder-based embedding models, `resnet` is a widely applied one. In this tutorial, we take `resnet50` as an example to show Towhee's capability of comparing similar images in a few lines of code with image-processing operators and pre-built embedding models:
3232

3333
```python
34-
from towhee import dc
35-
36-
dc_1 = dc['path_1', 'path_2']([['Lenna.png', 'Lenna.png'], ['Lenna.png', 'logo.png']])\
37-
.image_decode['path_1', 'img_1']()\
38-
.image_decode['path_2', 'img_2']()\
39-
.image_embedding.timm['img_1', 'emb_1'](model_name='resnet50')\
40-
.image_embedding.timm['img_2', 'emb_2'](model_name='resnet50')
41-
42-
dc_1.show()
34+
from towhee.dc2 import pipe, ops
35+
from towhee.datacollection import DataCollection
36+
37+
emb_pipe = (
38+
pipe.input('target', 'path')
39+
.flat_map('path', 'path', lambda x: x)
40+
.map('target', 'target', ops.image_decode.cv2('rgb'))
41+
.map('path', 'image', ops.image_decode.cv2('rgb'))
42+
.map('target', 'target_emb', ops.image_embedding.timm(model_name='resnet50'))
43+
.map('image', 'emb', ops.image_embedding.timm(model_name='resnet50'))
44+
)
45+
46+
emb_res = emb_pipe.output('target', 'image', 'path', 'emb', 'target_emb')('lena.png', ['lena.png', 'towhee_logo.png'])
4347
```
4448
![](./pic/emb.png)
4549

@@ -61,10 +65,15 @@ Towhee also support runnig a self-defined function as operator with `runas_op`,
6165

6266
```python
6367
import numpy as np
68+
6469
thresh = 0.01
65-
dc_1.runas_op[('emb_1', 'emb_2'), 'is_sim'](lambda x, y: np.linalg.norm(x - y) < thresh)\
66-
.select['is_sim']()\
67-
.show()
70+
detect_res = (
71+
emb_pipe.map(('emb', 'target_emb'), 'is_similar', lambda x, y: np.linalg.norm(x - y) < thresh)
72+
.output('target', 'image', 'is_similar')
73+
)
74+
75+
res = detect_res('lena.png', ['lena.png', 'towhee_logo.png'])
76+
DataCollection(res).show()
6877
```
6978
![](./pic/res.png)
7079

@@ -77,17 +86,24 @@ duration: 1
7786
Putting it all together, we can check if two images are duplicate with the following code snippet:
7887

7988
```python
80-
from towhee import dc
8189
import numpy as np
90+
from towhee.dc2 import pipe, ops
91+
from towhee.datacollection import DataCollection
8292

8393
thresh = 0.01
84-
res = dc['path_1', 'path_2']([['path/to/image/1', 'path/to/image/2']])\
85-
.image_decode['path_1', 'img_1']()\
86-
.image_decode['path_2', 'img_2']()\
87-
.image_embedding.timm['img_1', 'emb_1'](model_name='resnet50')\
88-
.image_embedding.timm['img_2', 'emb_2'](model_name='resnet50')\
89-
.runas_op[('emb_1', 'emb_2'), 'is_sim'](lambda x, y: np.linalg.norm(x - y) < thresh)\
90-
.select['is_sim']()
94+
p = (
95+
pipe.input('target', 'path')
96+
.flat_map('path', 'path', lambda x: x)
97+
.map('target', 'target', ops.image_decode.cv2('rgb'))
98+
.map('path', 'image', ops.image_decode.cv2('rgb'))
99+
.map('target', 'target_emb', ops.image_embedding.timm(model_name='resnet50'))
100+
.map('image', 'emb', ops.image_embedding.timm(model_name='resnet50'))
101+
.map(('emb', 'target_emb'), 'is_similar', lambda x, y: np.linalg.norm(x - y) < thresh)
102+
.output('target', 'image', 'is_similar')
103+
)
104+
105+
res = p('lena.png', ['lena.png', 'towhee_logo.png'])
106+
DataCollection(res).show()
91107
```
92108

93109
And that's it! Have fun and happy embedding :)
398 KB
Loading
325 KB
Loading
-443 KB
Binary file not shown.
-371 KB
Binary file not shown.

0 commit comments

Comments
 (0)