Windows: how to install U^2-Net HTTP server


U^2-Net HTTP is an HTTP service wrapper of the model: U^2-Net: Going Deeper with Nested U-Structure for Salient Object Detection (Qin et al, Pattern Recognition 2020)

Requirements:
Python >=3.6
pytorch (How to install pytorch on Windows)

Step 1:

git clone https://github.com/cyrildiagne/u2net-http.git

Result:

Administrator@REL0CC1I7V27L7B MINGW64 /d/Python
$ git clone https://github.com/cyrildiagne/u2net-http.git
Cloning into 'u2net-http'...
remote: Enumerating objects: 23, done.
remote: Counting objects: 100% (23/23), done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 23 (delta 6), reused 13 (delta 0), pack-reused 0
Unpacking objects: 100% (23/23), done.

Step 2:

cd u2net-http
git clone https://github.com/NathanUA/U-2-Net.git

Result:

Administrator@REL0CC1I7V27L7B MINGW64 /d/Python/u2net-http (master)
$ git clone https://github.com/NathanUA/U-2-Net.git
Cloning into 'U-2-Net'...
remote: Enumerating objects: 247, done.
remote: Total 247 (delta 0), reused 0 (delta 0), pack-reused 247
Receiving objects: 100% (247/247), 8.26 MiB | 1.81 MiB/s, done.
Resolving deltas: 100% (101/101), done.

Step 4:
Download u2netp.pth (4.7 MB) to ./U-2-Net/saved_models/u2netp/

Step 5:
Edit file u2net.py

Line 22:

# from model import U2NETP # small version u2net 4.7 MB

to

from model import U2NETP # small version u2net 4.7 MB

Line 24:

model_dir = './U-2-Net/saved_models/u2net/u2net.pth'

to:

model_dir = './U-2-Net/saved_models/u2netp/u2netp.pth'

Line 27:

net = U2NET(3, 1)

to:

net = U2NETP(3, 1)

Step 6:

pip install Flask==1.1.1
pip install flask-cors==3.0.8
pip install gunicorn==19.9.0
pip install numpy==1.15.2
pip install scikit-image==0.14.0
pip install Pillow==6.2.2
pip install scipy
pip install matplotlib
D:\Python\u2net-http>pip install scipy
Collecting scipy
  Downloading scipy-1.5.2-cp37-cp37m-win_amd64.whl (31.2 MB)
     |████████████████████████████████| 31.2 MB 121 kB/s
Requirement already satisfied: numpy>=1.14.5 in c:\users\administrator\appdata\l
ocal\programs\python\python37\lib\site-packages (from scipy) (1.15.2)
Installing collected packages: scipy
Successfully installed scipy-1.5.2

D:\Python\u2net-http>pip install matplotlib
Collecting matplotlib
  Downloading matplotlib-3.3.1-cp37-cp37m-win_amd64.whl (8.5 MB)
     |████████████████████████████████| 8.5 MB 3.3 MB/s
Requirement already satisfied: numpy>=1.15 in c:\users\administrator\appdata\loc
al\programs\python\python37\lib\site-packages (from matplotlib) (1.15.2)
Collecting cycler>=0.10
  Downloading cycler-0.10.0-py2.py3-none-any.whl (6.5 kB)
Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3
  Downloading pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)
     |████████████████████████████████| 67 kB 622 kB/s
Collecting kiwisolver>=1.0.1
  Downloading kiwisolver-1.2.0-cp37-none-win_amd64.whl (57 kB)
     |████████████████████████████████| 57 kB 295 kB/s
Collecting python-dateutil>=2.1
  Downloading python_dateutil-2.8.1-py2.py3-none-any.whl (227 kB)
     |████████████████████████████████| 227 kB 6.4 MB/s
Collecting certifi>=2020.06.20
  Downloading certifi-2020.6.20-py2.py3-none-any.whl (156 kB)
     |████████████████████████████████| 156 kB 3.2 MB/s
Requirement already satisfied: pillow>=6.2.0 in c:\users\administrator\appdata\l
ocal\programs\python\python37\lib\site-packages (from matplotlib) (6.2.2)
Requirement already satisfied: six in c:\users\administrator\appdata\local\progr
ams\python\python37\lib\site-packages (from cycler>=0.10->matplotlib) (1.15.0)
Installing collected packages: cycler, pyparsing, kiwisolver, python-dateutil, c
ertifi, matplotlib
Successfully installed certifi-2020.6.20 cycler-0.10.0 kiwisolver-1.2.0 matplotl
ib-3.3.1 pyparsing-2.4.7 python-dateutil-2.8.1

Step 7: for non CUDA device

Edit file u2net.py

Change lines:

net.load_state_dict(torch.load(model_dir))
if torch.cuda.is_available():
    net.cuda()

to:

if torch.cuda.is_available():
    net.load_state_dict(torch.load(model_dir))
    net.cuda()
else:
    net.load_state_dict(torch.load(model_dir,map_location='cpu'))

Step 8: Run server python main.py

D:\Python\u2net-http>python main.py
Loading U-2-Net...
 * Serving Flask app "main" (lazy loading)
 * Environment: development
 * Debug mode: on
INFO:werkzeug: * Restarting with stat
Loading U-2-Net...
WARNING:werkzeug: * Debugger is active!
INFO:werkzeug: * Debugger PIN: 254-112-049
INFO:werkzeug: * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)

Example:

Administrator@REL0CC1I7V27L7B MINGW64 ~/Desktop
$ curl -F "data=@picture.jpg" http://localhost:8080 -o result.png
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  152k  100 15566  100  137k   3488  31452  0:00:04  0:00:04 --:--:-- 31452

Leave a Reply