U ^ 2-Net HTTP es un contenedor de servicio HTTP del modelo: U ^ 2-Net: Profundizando con estructura en U anidada para la detección de objetos salientes (Qin et al, Pattern Recognition 2020)
Requisitos:
Python> = 3.6 ( CentOS 7: instalar python36 y python36-pip )
Paso 1:
1 2 | cd ~/photoshop |
Resultado:
1 2 3 4 5 6 7 | [root@s126368 photoshop]# 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. |
Paso 2:
1 2 | cd u2net-http git clone https://github.com/NathanUA/U-2-Net.git |
1 2 3 4 5 6 | [root@s126368 u2net-http]# 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 | 8.98 MiB/s, done. Resolving deltas: 100% (101/101), done. |
Paso 3:
Paso 3a:: Caso 1: use u2net.pth , descargue este archivo en / home / tutorialspots / Downloads /
1 2 3 | mkdir U-2-Net/saved_models mkdir U-2-Net/saved_models/u2net cp /home/tutorialspots/Downloads/u2net.pth U-2-Net/saved_models/u2net |
Paso 3b: Caso 2: use u2netp.pth , descargue este archivo en / home / tutorialspots / Downloads /
1 2 3 | mkdir U-2-Net/saved_models mkdir U-2-Net/saved_models/u2netp cp /home/tutorialspots/Downloads/u2netp.pth U-2-Net/saved_models/u2netp |
Paso 4 (opcional): Solución para dispositivos que no son CUDA:
Error que obtuvimos cuando se ejecutó python3 main.py
:
1 | RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location='cpu' to map your storages to the CPU. |
Editar archivo u2net.py
Cambiar líneas:
1 2 3 | net.load_state_dict(torch.load(model_dir)) if torch.cuda.is_available(): net.cuda() |
a:
1 2 3 4 5 | 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' )) |
Paso 5 (opcional): solo para el caso 2 (paso 3b)
Editar archivo u2net.py
Línea 22:
1 | # from model import U2NETP # small version u2net 4.7 MB |
a
1 | from model import U2NETP # small version u2net 4.7 MB |
Línea 24:
1 | model_dir = './U-2-Net/saved_models/u2net/u2net.pth' |
a:
1 | model_dir = './U-2-Net/saved_models/u2netp/u2netp.pth' |
Línea 27:
1 | net = U2NET( 3 , 1 ) |
a:
1 | net = U2NETP( 3 , 1 ) |
Paso 6:
1 | pip3 install torch==0.4.1 |
Resultado:
1 2 3 4 5 6 | [root@s126368 u2net-http]# pip3 install torch==0.4.1 WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead. Collecting torch==0.4.1 Installing collected packages: torch Successfully installed torch-0.4.1 |
Paso 7:
1 | pip3 install -r requirements.txt |
Resultado:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 dieciséis 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 sesenta y cinco 66 67 68 69 70 | [root@s126368 u2net-http]# pip3 install -r requirements.txt WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead. Collecting Flask==1.1.1 (from -r requirements.txt (line 1)) Collecting flask-cors==3.0.8 (from -r requirements.txt (line 2)) Collecting gunicorn==19.9.0 (from -r requirements.txt (line 3)) Collecting numpy==1.15.2 (from -r requirements.txt (line 4)) Collecting scikit-image==0.14.0 (from -r requirements.txt (line 5)) Collecting Pillow==6.2.2 (from -r requirements.txt (line 6)) Collecting torchvision==0.2.1 (from -r requirements.txt (line 7)) Collecting itsdangerous>=0.24 (from Flask==1.1.1->-r requirements.txt (line 1)) Requirement already satisfied: Jinja2>=2.10.1 in /usr/local/lib64/python3.6/site-packages (from Flask==1.1.1->-r requirements.txt (line 1)) Collecting Werkzeug>=0.15 (from Flask==1.1.1->-r requirements.txt (line 1)) Collecting click>=5.1 (from Flask==1.1.1->-r requirements.txt (line 1)) Requirement already satisfied: Six in /usr/local/lib/python3.6/site-packages (from flask-cors==3.0.8->-r requirements.txt (line 2)) Collecting scipy>=0.17.0 (from scikit-image==0.14.0->-r requirements.txt (line 5)) Collecting PyWavelets>=0.4.0 (from scikit-image==0.14.0->-r requirements.txt (line 5)) Collecting matplotlib>=2.0.0 (from scikit-image==0.14.0->-r requirements.txt (line 5)) Collecting cloudpickle>=0.2.1 (from scikit-image==0.14.0->-r requirements.txt (line 5)) Collecting dask[array]>=0.9.0 (from scikit-image==0.14.0->-r requirements.txt (line 5)) Collecting networkx>=1.8 (from scikit-image==0.14.0->-r requirements.txt (line 5)) Requirement already satisfied: torch in /usr/local/lib64/python3.6/site-packages (from torchvision==0.2.1->-r requirements.txt (line 7)) Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib64/python3.6/site-packages (from Jinja2>=2.10.1->Flask==1.1.1->-r requirements.txt (line 1)) Collecting cycler>=0.10 (from matplotlib>=2.0.0->scikit-image==0.14.0->-r requirements.txt (line 5)) Collecting kiwisolver>=1.0.1 (from matplotlib>=2.0.0->scikit-image==0.14.0->-r requirements.txt (line 5)) Downloading https://files.pythonhosted.org/packages/ae/23/147de658aabbf968324551ea22c0c13a00284c4ef49a77002e91f79657b7/kiwisolver-1.2.0-cp36-cp36m-manylinux1_x86_64.whl (88kB) 100% |████████████████████████████████| 92kB 6.5MB/s Collecting certifi>=2020.06.20 (from matplotlib>=2.0.0->scikit-image==0.14.0->-r requirements.txt (line 5)) Downloading https://files.pythonhosted.org/packages/5e/c4/6c4fe722df5343c33226f0b4e0bb042e4dc13483228b4718baf286f86d87/certifi-2020.6.20-py2.py3-none-any.whl (156kB) 100% |████████████████████████████████| 163kB 1.7MB/s Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 (from matplotlib>=2.0.0->scikit-image==0.14.0->-r requirements.txt (line 5)) Downloading https://files.pythonhosted.org/packages/8a/bb/488841f56197b13700afd5658fc279a2025a39e22449b7cf29864669b15d/pyparsing-2.4.7-py2.py3-none-any.whl (67kB) 100% |████████████████████████████████| 71kB 6.7MB/s Collecting python-dateutil>=2.1 (from matplotlib>=2.0.0->scikit-image==0.14.0->-r requirements.txt (line 5)) Downloading https://files.pythonhosted.org/packages/d4/70/d60450c3dd48ef87586924207ae8907090de0b306af2bce5d134d78615cb/python_dateutil-2.8.1-py2.py3-none-any.whl (227kB) 100% |████████████████████████████████| 235kB 4.1MB/s Collecting pyyaml (from dask[array]>=0.9.0->scikit-image==0.14.0->-r requirements.txt (line 5)) Downloading https://files.pythonhosted.org/packages/64/c2/b80047c7ac2478f9501676c988a5411ed5572f35d1beff9cae07d321512c/PyYAML-5.3.1.tar.gz (269kB) 100% |████████████████████████████████| 276kB 3.6MB/s Collecting toolz>=0.8.2; extra == "array" (from dask[array]>=0.9.0->scikit-image==0.14.0->-r requirements.txt (line 5)) Downloading https://files.pythonhosted.org/packages/22/8e/037b9ba5c6a5739ef0dcde60578c64d49f45f64c5e5e886531bfbc39157f/toolz-0.10.0.tar.gz (49kB) 100% |████████████████████████████████| 51kB 6.7MB/s Collecting decorator>=4.3.0 (from networkx>=1.8->scikit-image==0.14.0->-r requirements.txt (line 5)) Installing collected packages: itsdangerous, Werkzeug, click, Flask, flask-cors, gunicorn, numpy, Pillow, scipy, PyWavelets, cycler, kiwisolver, certifi, pyparsing, python-dateutil, matplotlib, cloudpickle, pyyaml, toolz, dask, decorator, networkx, scikit-image, torchvision Found existing installation: numpy 1.19.1 Uninstalling numpy-1.19.1: Successfully uninstalled numpy-1.19.1 Found existing installation: Pillow 7.2.0 Uninstalling Pillow-7.2.0: Successfully uninstalled Pillow-7.2.0 Running setup.py install for pyyaml ... done Running setup.py install for toolz ... done Successfully installed Flask-1.1.1 Pillow-6.2.2 PyWavelets-1.1.1 Werkzeug-1.0.1 certifi-2020.6.20 click-7.1.2 cloudpickle-1.5.0 cycler-0.10.0 dask-2.24.0 decorator-4.4.2 flask-cors-3.0.8 gunicorn-19.9.0 itsdangerous-1.1.0 kiwisolver-1.2.0 matplotlib-3.3.1 networkx-2.5 numpy-1.15.2 pyparsing-2.4.7 python-dateutil-2.8.1 pyyaml-5.3.1 scikit-image-0.14.0 scipy-1.5.2 toolz-0.10.0 torchvision-0.2.1 |
Paso 8: ejecutar el servidor
1 2 3 4 5 6 7 8 9 10 | [root@s126368 u2net-http]# python3 main.py Loading U-2-Net... * Serving Flask app "main" (lazy loading) * Environment: development * Debug mode: on INFO:werkzeug: * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit) INFO:werkzeug: * Restarting with stat Loading U-2-Net... WARNING:werkzeug: * Debugger is active! INFO:werkzeug: * Debugger PIN: 286-791-475 |
Para la producción, podemos usar un servicio:
Crear archivo /etc/systemd/system/my-python-daemon-3.service
1 | vi /etc/systemd/system/my-python-daemon-3.service |
Contenido:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [Unit] Description=My Python App 3 [Service] Type=simple Environment="PORT=8080" WorkingDirectory=/root/photoshop/u2net-http ExecStart=/usr/bin/python3 main.py Restart=always # Restart service after 10 seconds if python service crashes RestartSec=10 [Install] WantedBy=multi-user.target |
Comienza el servicio:
1 2 | systemctl enable my-python-daemon-3 systemctl start my-python-daemon-3 |
Términos de búsqueda recientes:
- instalación de u2net
0 Comentarios
Dejanos tu comentario para seguir mejorando!