README_en.md 4.5 KB
Newer Older
1 2 3
# gfm_resnet34_matting

|Module Name|gfm_resnet34_matting|
jm_12138's avatar
jm_12138 已提交
4
| :--- | :---: |
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|Category|Image Matting|
|Network|gfm_resnet34|
|Dataset|AM-2k|
|Support Fine-tuning|No|
|Module Size|562MB|
|Data Indicators|SAD10.89|
|Latest update date|2021-12-03|


## I. Basic Information

- ### Application Effect Display

  - Sample results:
    <p align="center">
jm_12138's avatar
jm_12138 已提交
20 21
    <img src="https://user-images.githubusercontent.com/35907364/145993777-9b69a85d-d31c-4743-8620-82b2a56ca1e7.jpg" width = "480" height = "350" hspace='10'/>
    <img src="https://user-images.githubusercontent.com/35907364/145993809-b0fb4bae-2c64-4868-99fc-500f19343442.png" width = "480" height = "350" hspace='10'/>
22 23 24 25 26 27 28
    </p>

- ### Module Introduction

  - Mating is the technique of extracting foreground from an image by calculating its color and transparency. It is widely used in the film industry to replace background, image composition, and visual effects. Each pixel in the image will have a value that represents its foreground transparency, called Alpha. The set of all Alpha values in an image is called Alpha Matte. The part of the image covered by the mask can be extracted to complete foreground separation.


jm_12138's avatar
jm_12138 已提交
29

30
  - For more information, please refer to: [gfm_resnet34_matting](https://github.com/JizhiziLi/GFM)
jm_12138's avatar
jm_12138 已提交
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

## II. Installation

- ### 1、Environmental Dependence

    - paddlepaddle >= 2.2.0

    - paddlehub >= 2.1.0

    - paddleseg >= 2.3.0


- ### 2、Installation

    - ```shell
      $ hub install gfm_resnet34_matting
      ```
jm_12138's avatar
jm_12138 已提交
49

50 51 52
    - In case of any problems during installation, please refer to:[Windows_Quickstart](../../../../docs/docs_en/get_start/windows_quickstart.md)
    | [Linux_Quickstart](../../../../docs/docs_en/get_start/linux_quickstart.md) | [Mac_Quickstart](../../../../docs/docs_en/get_start/mac_quickstart.md)  

jm_12138's avatar
jm_12138 已提交
53

54 55 56 57 58 59 60
## III. Module API Prediction

- ### 1、Command line Prediction

  - ```shell
    $ hub run gfm_resnet34_matting --input_path "/PATH/TO/IMAGE"
    ```
jm_12138's avatar
jm_12138 已提交
61

62 63 64 65 66 67 68 69 70 71
  - If you want to call the Hub module through the command line, please refer to: [PaddleHub Command Line Instruction](../../../../docs/docs_en/tutorial/cmd_usage.rst)


- ### 2、Prediction Code Example

    - ```python
      import paddlehub as hub
      import cv2

      model = hub.Module(name="gfm_resnet34_matting")
jm_12138's avatar
jm_12138 已提交
72
      result = model.predict([cv2.imread("/PATH/TO/IMAGE")])
73 74 75 76 77 78
      print(result)

      ```
- ### 3、API

    - ```python
jm_12138's avatar
jm_12138 已提交
79 80 81
        def predict(self,
                    image_list,
                    visualization,
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
                    save_path):
      ```

        - Prediction API for matting.

        - **Parameter**

            - image_list (list(str | numpy.ndarray)): Image path or image data, ndarray.shape is in the format \[H, W, C\],BGR.
            - visualization (bool): Whether to save the recognition results as picture files, default is False.
            - save_path (str): Save path of images, "modnet_mobilenetv2_matting_output" by default.

        - **Return**

            - result (list(numpy.ndarray)):The list of model results.

jm_12138's avatar
jm_12138 已提交
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
## IV. Server Deployment

- PaddleHub Serving can deploy an online service of matting.

- ### Step 1: Start PaddleHub Serving

  - Run the startup command:

    - ```shell
      $ hub serving start -m gfm_resnet34_matting
      ```

    - The servitization API is now deployed and the default port number is 8866.

    - **NOTE:**  If GPU is used for prediction, set CUDA_VISIBLE_DEVICES environment variable before the service, otherwise it need not be set.

- ### Step 2: Send a predictive request

  - With a configured server, use the following lines of code to send the prediction request and obtain the result


    ```python
    import requests
    import json
    import cv2
    import base64
    import time
    import numpy as np

    def cv2_to_base64(image):
        data = cv2.imencode('.jpg', image)[1]
        return base64.b64encode(data.tostring()).decode('utf8')


    def base64_to_cv2(b64str):
        data = base64.b64decode(b64str.encode('utf8'))
        data = np.fromstring(data, np.uint8)
        data = cv2.imdecode(data, cv2.IMREAD_COLOR)
        return data

    data = {'images':[cv2_to_base64(cv2.imread("/PATH/TO/IMAGE"))]}

    headers = {"Content-type": "application/json"}
    url = "http://127.0.0.1:8866/predict/gfm_resnet34_matting"
    r = requests.post(url=url, headers=headers, data=json.dumps(data))

    for image in r.json()["results"]['data']:
        data = base64_to_cv2(image)
        image_path =str(time.time()) + ".png"
        cv2.imwrite(image_path, data)
      ```

## V. Release Note

- 1.0.0

H
haoyuying 已提交
154
  First release