logo
Browse Source

Update

Signed-off-by: shiyu22 <shiyu.chen@zilliz.com>
main
shiyu22 3 years ago
parent
commit
d65fe65616
  1. 81
      README.md
  2. 0
      readme_src/operator.png
  3. BIN
      test_data/test.jpg
  4. 28
      test_transform_image.py

81
README.md

@ -10,94 +10,41 @@ In computer vision (CV) directions, image transformations are usually an indispe
## Interface ## Interface
`Class TransformImage(Operator)` [\[source\]](./transform_image.py)
`__init__(self, size: int)` `__init__(self, size: int)`
**params:**
**Args:**
​ size(`int`):
- size(int): the size of the output image.
​ The size of the output image.
`__call__(self, img_tensor: Union[np.ndarray, Image.Image, torch.Tensor, str])` `__call__(self, img_tensor: Union[np.ndarray, Image.Image, torch.Tensor, str])`
**params:**
**Args:**
​ img_tensor(`Union[np.ndarray, Image.Image, torch.Tensor, str]`):
​ Original image data, the type can be np.ndarry, PIL.image, or str path of the image.
Img_tensor(np.ndarray/Image.Image/torch.Tensor, str): original image data, the type can be np.ndarry, PIL.image, or str path of the image.
**Returns:**
**return:**
​ (`Tuple[('img_transformed', torch.Tensor)]`)
img_transformed(torch.Tensor): the tensor of the transformed image.
​ The tensor of the transformed image.
## How to use ## How to use
### Requirements ### Requirements
You can get the required python package by [requirements.txt](./requirements.txt). In fact, Towhee will automatically install these packages when you first load the Operator Repo, so you don't need to install them manually, here is just a list.
You can get the required python package by [requirements.txt](./requirements.txt).
- towhee
- torch
- torchvision
- numpy
- pillow - pillow
### How it works ### How it works
The `towhee/transform-image` Operator is used for image transformation and is an important part of data preprocessing. It can be added to the pipeline and is usually used as the first custom operator of the pipeline. For example, it's the first Operator named processing within [image_embedding_resnet50](https://hub.towhee.io/towhee/image-embedding-resnet50) pipeline, and it is the red box in the picture below. The `towhee/transform-image` Operator is used for image transformation and is an important part of data preprocessing. It can be added to the pipeline and is usually used as the first custom operator of the pipeline. For example, it's the first Operator named processing within [image_embedding_resnet50](https://hub.towhee.io/towhee/image-embedding-resnet50) pipeline, and it is the red box in the picture below.
![img](./pic/operator.png)
When using this Operator to write Pipeline's Yaml file, you need to declare the following content according to the interface of TransformImage class:
```yaml
operators:
-
name: 'preprocessing'
function: 'towhee/transform-image'
tag: 'main'
init_args:
size: 256
inputs:
-
df: 'image'
name: 'img_tensor'
col: 0
outputs:
-
df: 'image_preproc'
iter_info:
type: map
dataframes:
-
name: 'image'
columns:
-
name: 'img_tensor'
vtype: 'PIL.Image'
-
name: 'image_preproc'
columns:
-
name: 'img_transformed'
vtype: 'torch.Tensor'
```
> In the Interface section, we said that the input of the Operator can be np.ndarry, PIL.image, or str path of the image, but here we only use PIL.Image as an example, which is also used in [image_embedding_resnet50](https://hub.towhee.io/towhee/image-embedding-resnet50) pipeline, of course you can also change to the dataframe you want.
### File Structure
Here is the main file structure of the `transform-image` Operator. If you want to learn more about the source code or modify it yourself, you can learn from it.
```bash
├── .gitattributes
├── .gitignore
├── README.md
├── __init__.py
├── requirements.txt #General python dependency package
├── transform_image.py #The python file for Towhee, it defines the interface of the system.
├── transform_image.yaml #The YAML file contains Operator information, such as frame, input, and output.
├── test_data/ #The directory of test data, including test.jpg
└── test_transform_image.py #The unittest file of this Operator.
```
![img](./readme_src/operator.png)
## Reference ## Reference

0
pic/operator.png → readme_src/operator.png

Before

Width:  |  Height:  |  Size: 262 KiB

After

Width:  |  Height:  |  Size: 262 KiB

BIN
test_data/test.jpg

Binary file not shown.

Before

Width:  |  Height:  |  Size: 178 KiB

28
test_transform_image.py

@ -1,28 +0,0 @@
import unittest
from PIL import Image
from torchvision import transforms
from transform_image import TransformImage
class TestTransformImage(unittest.TestCase):
def test_transform_image(self):
img_src = './test_data/test.jpg'
test_img = Image.open(img_src)
tfms = transforms.Compose(
[
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
]
)
img1 = tfms(test_img).unsqueeze(0)
op = TransformImage(256)
outputs = op(test_img)
print("The output tyep of operator:", type(outputs.img_transformed))
c = (img1.numpy() == outputs.img_transformed.numpy())
self.assertTrue(c.all())
if __name__ == '__main__':
unittest.main()
Loading…
Cancel
Save