Python如何实现sg2im文字转图像
小编给大家分享一下Python如何实现sg2im文字转图像,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
1.从github上下载源码
!git clone https://github.com/google/sg2im.git
Cloning into 'sg2im'...
remote: Enumerating objects: 85, done.[K
remote: Total 85 (delta 0), reused 0 (delta 0), pack-reused 85[K
Unpacking objects: 100% (85/85), done.
! cp -r sg2im/sg2im sg2im/scripts/
!pip install -r sg2im/requirements.txt
Collecting cloudpickle==0.5.3
Downloading https://files.pythonhosted.org/packages/e7/bf/60ae7ec1e8c6742d2abbb6819c39a48ee796793bcdb7e1d5e41a3e379ddd/cloudpickle-0.5.3-py2.py3-none-any.whl
Requirement already satisfied: cycler==0.10.0 in /usr/local/lib/python3.6/dist-packages (from -r sg2im/requirements.txt (line 2)) (0.10.0)
Collecting Cython==0.28.3
[?25l Downloading https://files.pythonhosted.org/packages/6f/79/d8e2cd00bea8156a995fb284ce7b6677c49eccd2d318f73e201a9ce560dc/Cython-0.28.3-cp36-cp36m-manylinux1_x86_64.whl (3.4MB)
[K |████████████████████████████████| 3.4MB 8.6MB/s
[?25hCollecting dask==0.17.5
[?25l Downloading https://files.pythonhosted.org/packages/91/1a/71be14f468f8f3f94e708afd5662cf75a0ca33a78924ca9f129a9c45c66b/dask-0.17.5-py3-none-any.whl (598kB)
[K |████████████████████████████████| 604kB 30.6MB/s
[?25hCollecting decorator==4.3.0
Downloading https://files.pythonhosted.org/packages/bc/bb/a24838832ba35baf52f32ab1a49b906b5f82fb7c76b2f6a7e35e140bac30/decorator-4.3.0-py2.py3-none-any.whl
Collecting h6py==2.8.0
[?25l Downloading https://files.pythonhosted.org/packages/8e/cb/726134109e7bd71d98d1fcc717ffe051767aac42ede0e7326fd1787e5d64/h6py-2.8.0-cp36-cp36m-manylinux1_x86_64.whl (2.8MB)
[K |████████████████████████████████| 2.8MB 57.5MB/s
[?25hCollecting imageio==2.3.0
[?25l Downloading https://files.pythonhosted.org/packages/a7/1d/33c8686072148b3b0fcc12a2e0857dd8316b8ae20a0fa66c8d6a6d01c05c/imageio-2.3.0-py2.py3-none-any.whl (3.3MB)
[K |████████████████████████████████| 3.3MB 59.0MB/s
[?25hCollecting kiwisolver==1.0.1
[?25l Downloading https://files.pythonhosted.org/packages/69/a7/88719d132b18300b4369fbffa741841cfd36d1e637e1990f27929945b538/kiwisolver-1.0.1-cp36-cp36m-manylinux1_x86_64.whl (949kB)
[K |████████████████████████████████| 952kB 56.0MB/s
[?25hCollecting matplotlib==2.2.2
[?25l Downloading https://files.pythonhosted.org/packages/49/b8/89dbd27f2fb171ce753bb56220d4d4f6dbc5fe32b95d8edc4415782ef07f/matplotlib-2.2.2-cp36-cp36m-manylinux1_x86_64.whl (12.6MB)
[K |████████████████████████████████| 12.6MB 238kB/s
[?25hCollecting networkx==2.1
[?25l Downloading https://files.pythonhosted.org/packages/11/42/f951cc6838a4dff6ce57211c4d7f8444809ccbe2134179950301e5c4c83c/networkx-2.1.zip (1.6MB)
[K |████████████████████████████████| 1.6MB 49.4MB/s
[?25hCollecting numpy==1.14.4
[?25l Downloading https://files.pythonhosted.org/packages/4b/3d/9c0a34ad8544abef864714840fb8954d630b04433f00881bc8fde7b2ab27/numpy-1.14.4-cp36-cp36m-manylinux1_x86_64.whl (12.2MB)
[K |████████████████████████████████| 12.2MB 149kB/s
[?25hCollecting Pillow==5.1.0
[?25l Downloading https://files.pythonhosted.org/packages/5f/4b/8b54ab9d37b93998c81b364557dff9f61972c0f650efa0ceaf470b392740/Pillow-5.1.0-cp36-cp36m-manylinux1_x86_64.whl (2.0MB)
[K |████████████████████████████████| 2.0MB 53.7MB/s
[?25hCollecting pyparsing==2.2.0
[?25l Downloading https://files.pythonhosted.org/packages/6a/8a/718fd7d3458f9fab8e67186b00abdd345b639976bc7fb3ae722e1b026a50/pyparsing-2.2.0-py2.py3-none-any.whl (56kB)
[K |████████████████████████████████| 61kB 9.3MB/s
[?25hCollecting python-dateutil==2.7.3
[?25l Downloading https://files.pythonhosted.org/packages/cf/f5/af2b09c957ace60dcfac112b669c45c8c97e32f94aa8b56da4c6d1682825/python_dateutil-2.7.3-py2.py3-none-any.whl (211kB)
[K |████████████████████████████████| 215kB 49.8MB/s
[?25hCollecting pytz==2018.4
[?25l Downloading https://files.pythonhosted.org/packages/dc/83/15f7833b70d3e067ca91467ca245bae0f6fe56ddc7451aa0dc5606b120f2/pytz-2018.4-py2.py3-none-any.whl (510kB)
[K |████████████████████████████████| 512kB 56.7MB/s
[?25hCollecting PyWavelets==0.5.2
[?25l Downloading https://files.pythonhosted.org/packages/32/c0/3646053c0ce297686da524bc968bff6017151a9089d16c33afe7d330a48b/PyWavelets-0.5.2-cp36-cp36m-manylinux1_x86_64.whl (5.7MB)
[K |████████████████████████████████| 5.7MB 29.6MB/s
[?25hCollecting scikit-image==0.14.0
[?25l Downloading https://files.pythonhosted.org/packages/34/79/cefff573a53ca3fb4c390739d19541b95f371e24d2990aed4cd8837971f0/scikit_image-0.14.0-cp36-cp36m-manylinux1_x86_64.whl (25.3MB)
[K |████████████████████████████████| 25.3MB 115kB/s
[?25hCollecting scipy==1.1.0
[?25l Downloading https://files.pythonhosted.org/packages/a8/0b/f163da98d3a01b3e0ef1cab8dd2123c34aee2bafbb1c5bffa354cc8a1730/scipy-1.1.0-cp36-cp36m-manylinux1_x86_64.whl (31.2MB)
[K |████████████████████████████████| 31.2MB 97kB/s
[?25hCollecting six==1.11.0
Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting toolz==0.9.0
[?25l Downloading https://files.pythonhosted.org/packages/14/d0/a73c15bbeda3d2e7b381a36afb0d9cd770a9f4adc5d1532691013ba881db/toolz-0.9.0.tar.gz (45kB)
[K |████████████████████████████████| 51kB 8.4MB/s
[?25hCollecting torch==0.4.0
[?25l Downloading https://files.pythonhosted.org/packages/69/43/380514bd9663f1bf708abeb359b8b48d3fabb1c8e95bb3427a980a064c57/torch-0.4.0-cp36-cp36m-manylinux1_x86_64.whl (484.0MB)
[K |████████████████████████████████| 484.0MB 33kB/s
[?25hCollecting torchvision==0.2.1
[?25l Downloading https://files.pythonhosted.org/packages/ca/0d/f00b2885711e08bd71242ebe7b96561e6f6d01fdb4b9dcf4d37e2e13c5e1/torchvision-0.2.1-py2.py3-none-any.whl (54kB)
[K |████████████████████████████████| 61kB 9.8MB/s
[?25hRequirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from kiwisolver==1.0.1->-r sg2im/requirements.txt (line 8)) (47.1.1)
Building wheels for collected packages: networkx, toolz
Building wheel for networkx (setup.py) ... [?25l[?25hdone
Created wheel for networkx: filename=networkx-2.1-py2.py3-none-any.whl size=1447765 sha256=4e89cc8350ab7270295c4e879190531eee2b1205e4a7b0c073ed8fe950717a25
Stored in directory: /root/.cache/pip/wheels/44/c0/34/6f98693a554301bdb405f8d65d95bbcd3e50180cbfdd98a94e
Building wheel for toolz (setup.py) ... [?25l[?25hdone
Created wheel for toolz: filename=toolz-0.9.0-cp36-none-any.whl size=53240 sha256=eb0e9434019a90c774ffcbfb077542b8688b43df4895b0c5c57204702dadc064
Stored in directory: /root/.cache/pip/wheels/f4/0c/f6/ce6b2d1aa459ee97cc3c0f82236302bd62d89c86c700219463
Successfully built networkx toolz
[31mERROR: xarray 0.15.1 has requirement numpy>=1.15, but you'll have numpy 1.14.4 which is incompatible.[0m
[31mERROR: umap-learn 0.4.3 has requirement numpy>=1.17, but you'll have numpy 1.14.4 which is incompatible.[0m
[31mERROR: umap-learn 0.4.3 has requirement scipy>=1.3.1, but you'll have scipy 1.1.0 which is incompatible.[0m
[31mERROR: tifffile 2020.5.30 has requirement numpy>=1.15.1, but you'll have numpy 1.14.4 which is incompatible.[0m
[31mERROR: tensorflow 2.2.0 has requirement h6py<2.11.0,>=2.10.0, but you'll have h6py 2.8.0 which is incompatible.[0m
[31mERROR: tensorflow 2.2.0 has requirement numpy<2.0,>=1.16.0, but you'll have numpy 1.14.4 which is incompatible.[0m
[31mERROR: tensorflow 2.2.0 has requirement scipy==1.4.1; python_version >= "3", but you'll have scipy 1.1.0 which is incompatible.[0m
[31mERROR: tensorflow 2.2.0 has requirement six>=1.12.0, but you'll have six 1.11.0 which is incompatible.[0m
[31mERROR: tensorflow-probability 0.10.0 has requirement cloudpickle>=1.2.2, but you'll have cloudpickle 0.5.3 which is incompatible.[0m
[31mERROR: tensorflow-hub 0.8.0 has requirement six>=1.12.0, but you'll have six 1.11.0 which is incompatible.[0m
[31mERROR: spacy 2.2.4 has requirement numpy>=1.15.0, but you'll have numpy 1.14.4 which is incompatible.[0m
[31mERROR: plotnine 0.6.0 has requirement matplotlib>=3.1.1, but you'll have matplotlib 2.2.2 which is incompatible.[0m
[31mERROR: plotnine 0.6.0 has requirement numpy>=1.16.0, but you'll have numpy 1.14.4 which is incompatible.[0m
[31mERROR: plotnine 0.6.0 has requirement scipy>=1.2.0, but you'll have scipy 1.1.0 which is incompatible.[0m
[31mERROR: numba 0.48.0 has requirement numpy>=1.15, but you'll have numpy 1.14.4 which is incompatible.[0m
[31mERROR: mizani 0.6.0 has requirement matplotlib>=3.1.1, but you'll have matplotlib 2.2.2 which is incompatible.[0m
[31mERROR: imgaug 0.2.9 has requirement numpy>=1.15.0, but you'll have numpy 1.14.4 which is incompatible.[0m
[31mERROR: gym 0.17.2 has requirement cloudpickle<1.4.0,>=1.2.0, but you'll have cloudpickle 0.5.3 which is incompatible.[0m
[31mERROR: google-colab 1.0.0 has requirement six~=1.12.0, but you'll have six 1.11.0 which is incompatible.[0m
[31mERROR: featuretools 0.4.1 has requirement dask>=0.19.4, but you'll have dask 0.17.5 which is incompatible.[0m
[31mERROR: fbprophet 0.6 has requirement python-dateutil>=2.8.0, but you'll have python-dateutil 2.7.3 which is incompatible.[0m
[31mERROR: fastai 1.0.61 has requirement numpy>=1.15, but you'll have numpy 1.14.4 which is incompatible.[0m
[31mERROR: fastai 1.0.61 has requirement torch>=1.0.0, but you'll have torch 0.4.0 which is incompatible.[0m
[31mERROR: distributed 1.25.3 has requirement dask>=0.18.0, but you'll have dask 0.17.5 which is incompatible.[0m
[31mERROR: datascience 0.10.6 has requirement folium==0.2.1, but you'll have folium 0.8.3 which is incompatible.[0m
[31mERROR: cvxpy 1.0.31 has requirement numpy>=1.15, but you'll have numpy 1.14.4 which is incompatible.[0m
[31mERROR: blis 0.4.1 has requirement numpy>=1.15.0, but you'll have numpy 1.14.4 which is incompatible.[0m
[31mERROR: astropy 4.0.1.post1 has requirement numpy>=1.16, but you'll have numpy 1.14.4 which is incompatible.[0m
[31mERROR: albumentations 0.1.12 has requirement imgaug<0.2.7,>=0.2.5, but you'll have imgaug 0.2.9 which is incompatible.[0m
Installing collected packages: cloudpickle, Cython, dask, decorator, six, numpy, h6py, Pillow, imageio, kiwisolver, python-dateutil, pytz, pyparsing, matplotlib, networkx, PyWavelets, scipy, scikit-image, toolz, torch, torchvision
Found existing installation: cloudpickle 1.3.0
Uninstalling cloudpickle-1.3.0:
Successfully uninstalled cloudpickle-1.3.0
Found existing installation: Cython 0.29.19
Uninstalling Cython-0.29.19:
Successfully uninstalled Cython-0.29.19
Found existing installation: dask 2.12.0
Uninstalling dask-2.12.0:
Successfully uninstalled dask-2.12.0
Found existing installation: decorator 4.4.2
Uninstalling decorator-4.4.2:
Successfully uninstalled decorator-4.4.2
Found existing installation: six 1.12.0
Uninstalling six-1.12.0:
Successfully uninstalled six-1.12.0
Found existing installation: numpy 1.18.4
Uninstalling numpy-1.18.4:
Successfully uninstalled numpy-1.18.4
Found existing installation: h6py 2.10.0
Uninstalling h6py-2.10.0:
Successfully uninstalled h6py-2.10.0
Found existing installation: Pillow 7.0.0
Uninstalling Pillow-7.0.0:
Successfully uninstalled Pillow-7.0.0
Found existing installation: imageio 2.4.1
Uninstalling imageio-2.4.1:
Successfully uninstalled imageio-2.4.1
Found existing installation: kiwisolver 1.2.0
Uninstalling kiwisolver-1.2.0:
Successfully uninstalled kiwisolver-1.2.0
Found existing installation: python-dateutil 2.8.1
Uninstalling python-dateutil-2.8.1:
Successfully uninstalled python-dateutil-2.8.1
Found existing installation: pytz 2018.9
Uninstalling pytz-2018.9:
Successfully uninstalled pytz-2018.9
Found existing installation: pyparsing 2.4.7
Uninstalling pyparsing-2.4.7:
Successfully uninstalled pyparsing-2.4.7
Found existing installation: matplotlib 3.2.1
Uninstalling matplotlib-3.2.1:
Successfully uninstalled matplotlib-3.2.1
Found existing installation: networkx 2.4
Uninstalling networkx-2.4:
Successfully uninstalled networkx-2.4
Found existing installation: PyWavelets 1.1.1
Uninstalling PyWavelets-1.1.1:
Successfully uninstalled PyWavelets-1.1.1
Found existing installation: scipy 1.4.1
Uninstalling scipy-1.4.1:
Successfully uninstalled scipy-1.4.1
Found existing installation: scikit-image 0.16.2
Uninstalling scikit-image-0.16.2:
Successfully uninstalled scikit-image-0.16.2
Found existing installation: toolz 0.10.0
Uninstalling toolz-0.10.0:
Successfully uninstalled toolz-0.10.0
Found existing installation: torch 1.5.0+cu101
Uninstalling torch-1.5.0+cu101:
Successfully uninstalled torch-1.5.0+cu101
Found existing installation: torchvision 0.6.0+cu101
Uninstalling torchvision-0.6.0+cu101:
Successfully uninstalled torchvision-0.6.0+cu101
Successfully installed Cython-0.28.3 Pillow-5.1.0 PyWavelets-0.5.2 cloudpickle-0.5.3 dask-0.17.5 decorator-4.3.0 h6py-2.8.0 imageio-2.3.0 kiwisolver-1.0.1 matplotlib-2.2.2 networkx-2.1 numpy-1.14.4 pyparsing-2.2.0 python-dateutil-2.7.3 pytz-2018.4 scikit-image-0.14.0 scipy-1.1.0 six-1.11.0 toolz-0.9.0 torch-0.4.0 torchvision-0.2.1
!bash sg2im/scripts/download_models.sh
--2020-06-05 08:11:22-- https://storage.googleapis.com/sg2im-data/small/coco64.pt
Resolving storage.googleapis.com (storage.googleapis.com)... 173.194.79.128, 2a00:1450:4013:c05::80
Connecting to storage.googleapis.com (storage.googleapis.com)|173.194.79.128|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 119806264 (114M) [application/octet-stream]
Saving to: 'sg2im-models/coco64.pt'sg2im-models/coco64 100%[===================>] 114.26M 38.5MB/s in 3.0s
2020-06-05 08:11:25 (38.5 MB/s) - 'sg2im-models/coco64.pt' saved [119806264/119806264]
--2020-06-05 08:11:25-- https://storage.googleapis.com/sg2im-data/small/vg64.pt
Resolving storage.googleapis.com (storage.googleapis.com)... 108.177.119.128, 2a00:1450:4013:c00::80
Connecting to storage.googleapis.com (storage.googleapis.com)|108.177.119.128|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 119873465 (114M) [application/octet-stream]
Saving to: 'sg2im-models/vg64.pt'sg2im-models/vg64.p 100%[===================>] 114.32M 44.0MB/s in 2.6s
2020-06-05 08:11:29 (44.0 MB/s) - 'sg2im-models/vg64.pt' saved [119873465/119873465]
--2020-06-05 08:11:29-- https://storage.googleapis.com/sg2im-data/small/vg128.pt
Resolving storage.googleapis.com (storage.googleapis.com)... 74.125.128.128, 2a00:1450:4013:c02::80
Connecting to storage.googleapis.com (storage.googleapis.com)|74.125.128.128|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 129319241 (123M) [application/octet-stream]
Saving to: 'sg2im-models/vg128.pt'sg2im-models/vg128. 100%[===================>] 123.33M 54.2MB/s in 2.3s
2020-06-05 08:11:32 (54.2 MB/s) - 'sg2im-models/vg128.pt' saved [129319241/129319241]
2.训练与结果展示
!python3 sg2im/scripts/run_model.py --checkpoint sg2im-models/vg128.pt --scene_graphs sg2im/scene_graphs/figure_6_sheep.json --output_dir outputs
import matplotlib.pyplot as pltimport cv2%matplotlib inlineimg0 = cv2.imread("outputs/img000000.png")img1 = cv2.imread("outputs/img000001.png")img2 = cv2.imread("outputs/img000002.png")img3 = cv2.imread("outputs/img000003.png")img4 = cv2.imread("outputs/img000004.png")img5 = cv2.imread("outputs/img000005.png")img6 = cv2.imread("outputs/img000006.png")plt.figure()plt.subplot(3,3,1)plt.imshow(img0)plt.subplot(3,3,2)plt.imshow(img1)plt.subplot(3,3,3)plt.imshow(img2)plt.subplot(3,3,4)plt.imshow(img3)plt.subplot(3,3,5)plt.imshow(img4)plt.subplot(3,3,6)plt.imshow(img5)plt.subplot(3,3,7)plt.imshow(img6)
!python3 sg2im/scripts/run_model.py --checkpoint sg2im-models/vg128.pt --scene_graphs sg2im/scene_graphs/figure_6_street.json --output_dir outputs
import matplotlib.pyplot as pltimport cv2%matplotlib inlineimg0 = cv2.imread("outputs/img000000.png")img1 = cv2.imread("outputs/img000001.png")img2 = cv2.imread("outputs/img000002.png")img3 = cv2.imread("outputs/img000003.png")img4 = cv2.imread("outputs/img000004.png")img5 = cv2.imread("outputs/img000005.png")img6 = cv2.imread("outputs/img000006.png")plt.figure()plt.subplot(3,3,1)plt.imshow(img0)plt.subplot(3,3,2)plt.imshow(img1)plt.subplot(3,3,3)plt.imshow(img2)plt.subplot(3,3,4)plt.imshow(img3)plt.subplot(3,3,5)plt.imshow(img4)plt.subplot(3,3,6)plt.imshow(img5)plt.subplot(3,3,7)plt.imshow(img6)
!python3 sg2im/scripts/run_model.py --checkpoint sg2im-models/vg128.pt --scene_graphs sg2im/scene_graphs/figure_5_vg.json --output_dir outputs
import matplotlib.pyplot as pltimport cv2%matplotlib inlineimg0 = cv2.imread("outputs/img000000.png")img1 = cv2.imread("outputs/img000001.png")img2 = cv2.imread("outputs/img000002.png")img3 = cv2.imread("outputs/img000003.png")img4 = cv2.imread("outputs/img000004.png")img5 = cv2.imread("outputs/img000005.png")img6 = cv2.imread("outputs/img000006.png")img7 = cv2.imread("outputs/img000007.png")plt.figure()plt.subplot(3,3,1)plt.imshow(img0)plt.subplot(3,3,2)plt.imshow(img1)plt.subplot(3,3,3)plt.imshow(img2)plt.subplot(3,3,4)plt.imshow(img3)plt.subplot(3,3,5)plt.imshow(img4)plt.subplot(3,3,6)plt.imshow(img5)plt.subplot(3,3,7)plt.imshow(img6)plt.subplot(3,3,8)plt.imshow(img7)
看完了这篇文章,相信你对"Python如何实现sg2im文字转图像"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!