In [1]:
#@title Check GPU
#@markdown Run this to connect to a Colab Instance, and see what GPU Google gave you.

gpu = !nvidia-smi --query-gpu=gpu_name --format=csv
print(gpu[1])
print("The Tesla T4 and P100 are fast and support hardware encoding. The K80 and P4 are slower.")
print("Sometimes resetting the instance in the 'runtime' tab will give you a different GPU.")


Tesla P100-PCIE-16GB
The Tesla T4 and P100 are fast and support hardware encoding. The K80 and P4 are slower.
Sometimes resetting the instance in the 'runtime' tab will give you a different GPU.

In [2]:
from google.colab import drive
drive.mount('/content/drive')


Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly

Enter your authorization code:
··········
Mounted at /content/drive

In [3]:
!git clone https://github.com/junyanz/interactive-deep-colorization ideepcolor


Cloning into 'ideepcolor'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 335 (delta 3), reused 8 (delta 2), pack-reused 326
Receiving objects: 100% (335/335), 12.33 MiB | 6.20 MiB/s, done.
Resolving deltas: 100% (182/182), done.

In [4]:
%cd ideepcolor


/content/ideepcolor

In [5]:
!bash ./models/fetch_models.sh


--2020-03-19 19:14:40--  http://colorization.eecs.berkeley.edu/siggraph/models/model.caffemodel
Resolving colorization.eecs.berkeley.edu (colorization.eecs.berkeley.edu)... 128.32.189.73
Connecting to colorization.eecs.berkeley.edu (colorization.eecs.berkeley.edu)|128.32.189.73|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 194117235 (185M)
Saving to: ‘./models/reference_model/model.caffemodel’

./models/reference_ 100%[===================>] 185.12M  21.5MB/s    in 9.6s    

2020-03-19 19:14:50 (19.3 MB/s) - ‘./models/reference_model/model.caffemodel’ saved [194117235/194117235]

--2020-03-19 19:14:51--  http://colorization.eecs.berkeley.edu/siggraph/models/global_model.caffemodel
Resolving colorization.eecs.berkeley.edu (colorization.eecs.berkeley.edu)... 128.32.189.73
Connecting to colorization.eecs.berkeley.edu (colorization.eecs.berkeley.edu)|128.32.189.73|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 140028664 (134M)
Saving to: ‘./models/global_model/global_model.caffemodel’

./models/global_mod 100%[===================>] 133.54M  17.9MB/s    in 7.9s    

2020-03-19 19:14:59 (16.9 MB/s) - ‘./models/global_model/global_model.caffemodel’ saved [140028664/140028664]

--2020-03-19 19:14:59--  http://colorization.eecs.berkeley.edu/siggraph/models/dummy.caffemodel
Resolving colorization.eecs.berkeley.edu (colorization.eecs.berkeley.edu)... 128.32.189.73
Connecting to colorization.eecs.berkeley.edu (colorization.eecs.berkeley.edu)|128.32.189.73|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 583
Saving to: ‘./models/global_model/dummy.caffemodel’

./models/global_mod 100%[===================>]     583  --.-KB/s    in 0s      

2020-03-19 19:14:59 (90.2 MB/s) - ‘./models/global_model/dummy.caffemodel’ saved [583/583]

--2020-03-19 19:14:59--  http://colorization.eecs.berkeley.edu/siggraph/models/pytorch.pth
Resolving colorization.eecs.berkeley.edu (colorization.eecs.berkeley.edu)... 128.32.189.73
Connecting to colorization.eecs.berkeley.edu (colorization.eecs.berkeley.edu)|128.32.189.73|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 136785828 (130M)
Saving to: ‘./models/pytorch/pytorch_trained.pth’

./models/pytorch/py 100%[===================>] 130.45M  21.1MB/s    in 7.1s    

2020-03-19 19:15:06 (18.3 MB/s) - ‘./models/pytorch/pytorch_trained.pth’ saved [136785828/136785828]

--2020-03-19 19:15:06--  http://colorization.eecs.berkeley.edu/siggraph/models/caffemodel.pth
Resolving colorization.eecs.berkeley.edu (colorization.eecs.berkeley.edu)... 128.32.189.73
Connecting to colorization.eecs.berkeley.edu (colorization.eecs.berkeley.edu)|128.32.189.73|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 136787426 (130M)
Saving to: ‘./models/pytorch/caffemodel.pth’

./models/pytorch/ca 100%[===================>] 130.45M  21.1MB/s    in 7.1s    

2020-03-19 19:15:14 (18.3 MB/s) - ‘./models/pytorch/caffemodel.pth’ saved [136787426/136787426]


In [6]:
# !apt install -y caffe-cpu
!apt install -y caffe-cuda


Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  caffe-tools-cuda cython3 fonts-lyx javascript-common libblosc1
  libcaffe-cuda1 libcublas9.1 libcudart9.1 libcurand9.1 libgflags2.2
  libgoogle-glog0v5 libjs-jquery libjs-jquery-ui libleveldb1v5 liblmdb0
  python-matplotlib-data python-tables-data python3-bs4 python3-caffe-cuda
  python3-chardet python3-cycler python3-dateutil python3-decorator
  python3-gflags python3-h5py python3-html5lib python3-ipython
  python3-ipython-genutils python3-leveldb python3-lxml python3-matplotlib
  python3-networkx python3-nose python3-numexpr python3-olefile python3-pandas
  python3-pandas-lib python3-pexpect python3-pickleshare python3-pil
  python3-pkg-resources python3-prompt-toolkit python3-protobuf
  python3-ptyprocess python3-pygments python3-pyparsing python3-pywt
  python3-scipy python3-simplegeneric python3-six python3-skimage
  python3-skimage-lib python3-tables python3-tables-lib python3-traitlets
  python3-tz python3-wcwidth python3-webencodings python3-yaml
  ttf-bitstream-vera
Suggested packages:
  libcaffe-cuda-dev caffe-doc cython-doc apache2 | lighttpd | httpd
  libjs-jquery-ui-docs python-cycler-doc python-h5py-doc python3-genshi
  python3-lxml-dbg python-lxml-doc dvipng gir1.2-gtk-3.0 ghostscript inkscape
  ipython3 python-matplotlib-doc python3-cairocffi python3-gi-cairo
  python3-gobject python3-pyqt4 python3-sip python3-tornado
  texlive-extra-utils texlive-latex-extra ttf-staypuft python3-pydotplus
  python-nose-doc python-pandas-doc python-pexpect-doc python-pil-doc
  python3-pil-dbg python3-setuptools python-pyparsing-doc python-scipy-doc
  python-skimage-doc python-tables-doc python3-netcdf4 vitables
The following NEW packages will be installed:
  caffe-cuda caffe-tools-cuda cython3 fonts-lyx javascript-common libblosc1
  libcaffe-cuda1 libcublas9.1 libcudart9.1 libcurand9.1 libgflags2.2
  libgoogle-glog0v5 libjs-jquery libjs-jquery-ui libleveldb1v5 liblmdb0
  python-matplotlib-data python-tables-data python3-bs4 python3-caffe-cuda
  python3-chardet python3-cycler python3-dateutil python3-decorator
  python3-gflags python3-h5py python3-html5lib python3-ipython
  python3-ipython-genutils python3-leveldb python3-lxml python3-matplotlib
  python3-networkx python3-nose python3-numexpr python3-olefile python3-pandas
  python3-pandas-lib python3-pexpect python3-pickleshare python3-pil
  python3-pkg-resources python3-prompt-toolkit python3-protobuf
  python3-ptyprocess python3-pygments python3-pyparsing python3-pywt
  python3-scipy python3-simplegeneric python3-six python3-skimage
  python3-skimage-lib python3-tables python3-tables-lib python3-traitlets
  python3-tz python3-wcwidth python3-webencodings python3-yaml
  ttf-bitstream-vera
0 upgraded, 61 newly installed, 0 to remove and 25 not upgraded.
Need to get 121 MB of archives.
After this operation, 316 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-yaml amd64 3.12-1build2 [109 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 libcublas9.1 amd64 9.1.85-3ubuntu1 [25.0 MB]
Get:3 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 libcudart9.1 amd64 9.1.85-3ubuntu1 [121 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 libcurand9.1 amd64 9.1.85-3ubuntu1 [38.9 MB]
Get:5 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libgflags2.2 amd64 2.2.1-1 [72.4 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libgoogle-glog0v5 amd64 0.3.5-1 [50.5 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic/main amd64 libleveldb1v5 amd64 1.20-2 [136 kB]
Get:8 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 liblmdb0 amd64 0.9.21-1ubuntu0.1 [44.6 kB]
Get:9 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 libcaffe-cuda1 amd64 1.0.0-6build1 [1,600 kB]
Get:10 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 caffe-tools-cuda amd64 1.0.0-6build1 [105 kB]
Get:11 http://archive.ubuntu.com/ubuntu bionic/universe amd64 cython3 amd64 0.26.1-0.4 [1,925 kB]
Get:12 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-six all 1.11.0-2 [11.4 kB]
Get:13 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-dateutil all 2.6.1-1 [52.3 kB]
Get:14 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-gflags all 1.5.1-5 [35.6 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-h5py amd64 2.7.1-2 [631 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-decorator all 4.1.2-1 [9,364 B]
Get:17 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-ptyprocess all 0.5.2-1 [12.7 kB]
Get:18 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-pexpect all 4.2.1-1 [42.4 kB]
Get:19 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-pickleshare all 0.7.4-2 [6,904 B]
Get:20 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-pkg-resources all 39.0.1-2 [98.8 kB]
Get:21 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-wcwidth all 0.1.7+dfsg1-1 [14.7 kB]
Get:22 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-prompt-toolkit all 1.0.15-1 [163 kB]
Get:23 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-pygments all 2.2.0+dfsg-1 [574 kB]
Get:24 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-simplegeneric all 0.8.1-1 [11.5 kB]
Get:25 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-ipython-genutils all 0.2.0-1 [20.9 kB]
Get:26 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-traitlets all 4.3.2-1 [59.1 kB]
Get:27 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-ipython all 5.5.0-1 [381 kB]
Get:28 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-leveldb amd64 0~svn68-3build3 [18.3 kB]
Get:29 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 fonts-lyx all 2.2.4-0ubuntu0.18.04.1 [155 kB]
Get:30 http://archive.ubuntu.com/ubuntu bionic/universe amd64 ttf-bitstream-vera all 1.10-8 [352 kB]
Get:31 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python-matplotlib-data all 2.1.1-2ubuntu3 [3,774 kB]
Get:32 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-pyparsing all 2.2.0+dfsg1-2 [52.2 kB]
Get:33 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-tz all 2018.3-2 [25.1 kB]
Get:34 http://archive.ubuntu.com/ubuntu bionic/main amd64 libjs-jquery all 3.2.1-1 [152 kB]
Get:35 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libjs-jquery-ui all 1.12.1+dfsg-5 [232 kB]
Get:36 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-cycler all 0.10.0-1 [7,622 B]
Get:37 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-matplotlib amd64 2.1.1-2ubuntu3 [3,907 kB]
Get:38 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 python3-networkx all 1.11-1ubuntu3 [606 kB]
Get:39 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-nose all 1.3.7-3 [115 kB]
Get:40 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 python3-pandas-lib amd64 0.22.0-4ubuntu1 [3,041 kB]
Get:41 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 python3-pandas all 0.22.0-4ubuntu1 [2,765 kB]
Get:42 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-pil amd64 5.1.0-1ubuntu0.2 [329 kB]
Get:43 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-protobuf amd64 3.0.0-9.1ubuntu1 [262 kB]
Get:44 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-scipy amd64 0.19.1-2ubuntu1 [9,619 kB]
Get:45 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-skimage-lib amd64 0.13.1-2 [1,504 kB]
Get:46 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-pywt amd64 0.5.1-1.1ubuntu4 [932 kB]
Get:47 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-skimage all 0.13.1-2 [19.6 MB]
Get:48 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 python3-caffe-cuda amd64 1.0.0-6build1 [689 kB]
Get:49 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 caffe-cuda amd64 1.0.0-6build1 [4,564 B]
Get:50 http://archive.ubuntu.com/ubuntu bionic/main amd64 javascript-common all 11 [6,066 B]
Get:51 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libblosc1 amd64 1.14.2+ds1-1 [31.4 kB]
Get:52 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python-tables-data all 3.4.2-4 [46.1 kB]
Get:53 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-bs4 all 4.6.0-1 [67.8 kB]
Get:54 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-chardet all 3.0.4-1 [80.3 kB]
Get:55 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-webencodings all 0.5-2 [10.4 kB]
Get:56 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-html5lib all 0.999999999-1 [81.9 kB]
Get:57 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-lxml amd64 4.2.1-1ubuntu0.1 [1,091 kB]
Get:58 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-numexpr amd64 2.6.4-1 [119 kB]
Get:59 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-olefile all 0.45.1-1 [33.3 kB]
Get:60 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-tables-lib amd64 3.4.2-4 [413 kB]
Get:61 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-tables all 3.4.2-4 [331 kB]
Fetched 121 MB in 24s (4,996 kB/s)
Extracting templates from packages: 100%
Selecting previously unselected package python3-yaml.
(Reading database ... 144542 files and directories currently installed.)
Preparing to unpack .../00-python3-yaml_3.12-1build2_amd64.deb ...
Unpacking python3-yaml (3.12-1build2) ...
Selecting previously unselected package libcublas9.1:amd64.
Preparing to unpack .../01-libcublas9.1_9.1.85-3ubuntu1_amd64.deb ...
Unpacking libcublas9.1:amd64 (9.1.85-3ubuntu1) ...
Selecting previously unselected package libcudart9.1:amd64.
Preparing to unpack .../02-libcudart9.1_9.1.85-3ubuntu1_amd64.deb ...
Unpacking libcudart9.1:amd64 (9.1.85-3ubuntu1) ...
Selecting previously unselected package libcurand9.1:amd64.
Preparing to unpack .../03-libcurand9.1_9.1.85-3ubuntu1_amd64.deb ...
Unpacking libcurand9.1:amd64 (9.1.85-3ubuntu1) ...
Selecting previously unselected package libgflags2.2.
Preparing to unpack .../04-libgflags2.2_2.2.1-1_amd64.deb ...
Unpacking libgflags2.2 (2.2.1-1) ...
Selecting previously unselected package libgoogle-glog0v5.
Preparing to unpack .../05-libgoogle-glog0v5_0.3.5-1_amd64.deb ...
Unpacking libgoogle-glog0v5 (0.3.5-1) ...
Selecting previously unselected package libleveldb1v5:amd64.
Preparing to unpack .../06-libleveldb1v5_1.20-2_amd64.deb ...
Unpacking libleveldb1v5:amd64 (1.20-2) ...
Selecting previously unselected package liblmdb0:amd64.
Preparing to unpack .../07-liblmdb0_0.9.21-1ubuntu0.1_amd64.deb ...
Unpacking liblmdb0:amd64 (0.9.21-1ubuntu0.1) ...
Selecting previously unselected package libcaffe-cuda1:amd64.
Preparing to unpack .../08-libcaffe-cuda1_1.0.0-6build1_amd64.deb ...
Unpacking libcaffe-cuda1:amd64 (1.0.0-6build1) ...
Selecting previously unselected package caffe-tools-cuda.
Preparing to unpack .../09-caffe-tools-cuda_1.0.0-6build1_amd64.deb ...
Unpacking caffe-tools-cuda (1.0.0-6build1) ...
Selecting previously unselected package cython3.
Preparing to unpack .../10-cython3_0.26.1-0.4_amd64.deb ...
Unpacking cython3 (0.26.1-0.4) ...
Selecting previously unselected package python3-six.
Preparing to unpack .../11-python3-six_1.11.0-2_all.deb ...
Unpacking python3-six (1.11.0-2) ...
Selecting previously unselected package python3-dateutil.
Preparing to unpack .../12-python3-dateutil_2.6.1-1_all.deb ...
Unpacking python3-dateutil (2.6.1-1) ...
Selecting previously unselected package python3-gflags.
Preparing to unpack .../13-python3-gflags_1.5.1-5_all.deb ...
Unpacking python3-gflags (1.5.1-5) ...
Selecting previously unselected package python3-h5py.
Preparing to unpack .../14-python3-h5py_2.7.1-2_amd64.deb ...
Unpacking python3-h5py (2.7.1-2) ...
Selecting previously unselected package python3-decorator.
Preparing to unpack .../15-python3-decorator_4.1.2-1_all.deb ...
Unpacking python3-decorator (4.1.2-1) ...
Selecting previously unselected package python3-ptyprocess.
Preparing to unpack .../16-python3-ptyprocess_0.5.2-1_all.deb ...
Unpacking python3-ptyprocess (0.5.2-1) ...
Selecting previously unselected package python3-pexpect.
Preparing to unpack .../17-python3-pexpect_4.2.1-1_all.deb ...
Unpacking python3-pexpect (4.2.1-1) ...
Selecting previously unselected package python3-pickleshare.
Preparing to unpack .../18-python3-pickleshare_0.7.4-2_all.deb ...
Unpacking python3-pickleshare (0.7.4-2) ...
Selecting previously unselected package python3-pkg-resources.
Preparing to unpack .../19-python3-pkg-resources_39.0.1-2_all.deb ...
Unpacking python3-pkg-resources (39.0.1-2) ...
Selecting previously unselected package python3-wcwidth.
Preparing to unpack .../20-python3-wcwidth_0.1.7+dfsg1-1_all.deb ...
Unpacking python3-wcwidth (0.1.7+dfsg1-1) ...
Selecting previously unselected package python3-prompt-toolkit.
Preparing to unpack .../21-python3-prompt-toolkit_1.0.15-1_all.deb ...
Unpacking python3-prompt-toolkit (1.0.15-1) ...
Selecting previously unselected package python3-pygments.
Preparing to unpack .../22-python3-pygments_2.2.0+dfsg-1_all.deb ...
Unpacking python3-pygments (2.2.0+dfsg-1) ...
Selecting previously unselected package python3-simplegeneric.
Preparing to unpack .../23-python3-simplegeneric_0.8.1-1_all.deb ...
Unpacking python3-simplegeneric (0.8.1-1) ...
Selecting previously unselected package python3-ipython-genutils.
Preparing to unpack .../24-python3-ipython-genutils_0.2.0-1_all.deb ...
Unpacking python3-ipython-genutils (0.2.0-1) ...
Selecting previously unselected package python3-traitlets.
Preparing to unpack .../25-python3-traitlets_4.3.2-1_all.deb ...
Unpacking python3-traitlets (4.3.2-1) ...
Selecting previously unselected package python3-ipython.
Preparing to unpack .../26-python3-ipython_5.5.0-1_all.deb ...
Unpacking python3-ipython (5.5.0-1) ...
Selecting previously unselected package python3-leveldb.
Preparing to unpack .../27-python3-leveldb_0~svn68-3build3_amd64.deb ...
Unpacking python3-leveldb (0~svn68-3build3) ...
Selecting previously unselected package fonts-lyx.
Preparing to unpack .../28-fonts-lyx_2.2.4-0ubuntu0.18.04.1_all.deb ...
Unpacking fonts-lyx (2.2.4-0ubuntu0.18.04.1) ...
Selecting previously unselected package ttf-bitstream-vera.
Preparing to unpack .../29-ttf-bitstream-vera_1.10-8_all.deb ...
Unpacking ttf-bitstream-vera (1.10-8) ...
Selecting previously unselected package python-matplotlib-data.
Preparing to unpack .../30-python-matplotlib-data_2.1.1-2ubuntu3_all.deb ...
Unpacking python-matplotlib-data (2.1.1-2ubuntu3) ...
Selecting previously unselected package python3-pyparsing.
Preparing to unpack .../31-python3-pyparsing_2.2.0+dfsg1-2_all.deb ...
Unpacking python3-pyparsing (2.2.0+dfsg1-2) ...
Selecting previously unselected package python3-tz.
Preparing to unpack .../32-python3-tz_2018.3-2_all.deb ...
Unpacking python3-tz (2018.3-2) ...
Selecting previously unselected package libjs-jquery.
Preparing to unpack .../33-libjs-jquery_3.2.1-1_all.deb ...
Unpacking libjs-jquery (3.2.1-1) ...
Selecting previously unselected package libjs-jquery-ui.
Preparing to unpack .../34-libjs-jquery-ui_1.12.1+dfsg-5_all.deb ...
Unpacking libjs-jquery-ui (1.12.1+dfsg-5) ...
Selecting previously unselected package python3-cycler.
Preparing to unpack .../35-python3-cycler_0.10.0-1_all.deb ...
Unpacking python3-cycler (0.10.0-1) ...
Selecting previously unselected package python3-matplotlib.
Preparing to unpack .../36-python3-matplotlib_2.1.1-2ubuntu3_amd64.deb ...
Unpacking python3-matplotlib (2.1.1-2ubuntu3) ...
Selecting previously unselected package python3-networkx.
Preparing to unpack .../37-python3-networkx_1.11-1ubuntu3_all.deb ...
Unpacking python3-networkx (1.11-1ubuntu3) ...
Selecting previously unselected package python3-nose.
Preparing to unpack .../38-python3-nose_1.3.7-3_all.deb ...
Unpacking python3-nose (1.3.7-3) ...
Selecting previously unselected package python3-pandas-lib.
Preparing to unpack .../39-python3-pandas-lib_0.22.0-4ubuntu1_amd64.deb ...
Unpacking python3-pandas-lib (0.22.0-4ubuntu1) ...
Selecting previously unselected package python3-pandas.
Preparing to unpack .../40-python3-pandas_0.22.0-4ubuntu1_all.deb ...
Unpacking python3-pandas (0.22.0-4ubuntu1) ...
Selecting previously unselected package python3-pil:amd64.
Preparing to unpack .../41-python3-pil_5.1.0-1ubuntu0.2_amd64.deb ...
Unpacking python3-pil:amd64 (5.1.0-1ubuntu0.2) ...
Selecting previously unselected package python3-protobuf.
Preparing to unpack .../42-python3-protobuf_3.0.0-9.1ubuntu1_amd64.deb ...
Unpacking python3-protobuf (3.0.0-9.1ubuntu1) ...
Selecting previously unselected package python3-scipy.
Preparing to unpack .../43-python3-scipy_0.19.1-2ubuntu1_amd64.deb ...
Unpacking python3-scipy (0.19.1-2ubuntu1) ...
Selecting previously unselected package python3-skimage-lib:amd64.
Preparing to unpack .../44-python3-skimage-lib_0.13.1-2_amd64.deb ...
Unpacking python3-skimage-lib:amd64 (0.13.1-2) ...
Selecting previously unselected package python3-pywt.
Preparing to unpack .../45-python3-pywt_0.5.1-1.1ubuntu4_amd64.deb ...
Unpacking python3-pywt (0.5.1-1.1ubuntu4) ...
Selecting previously unselected package python3-skimage.
Preparing to unpack .../46-python3-skimage_0.13.1-2_all.deb ...
Unpacking python3-skimage (0.13.1-2) ...
Selecting previously unselected package python3-caffe-cuda.
Preparing to unpack .../47-python3-caffe-cuda_1.0.0-6build1_amd64.deb ...
Unpacking python3-caffe-cuda (1.0.0-6build1) ...
Selecting previously unselected package caffe-cuda.
Preparing to unpack .../48-caffe-cuda_1.0.0-6build1_amd64.deb ...
Unpacking caffe-cuda (1.0.0-6build1) ...
Selecting previously unselected package javascript-common.
Preparing to unpack .../49-javascript-common_11_all.deb ...
Unpacking javascript-common (11) ...
Selecting previously unselected package libblosc1.
Preparing to unpack .../50-libblosc1_1.14.2+ds1-1_amd64.deb ...
Unpacking libblosc1 (1.14.2+ds1-1) ...
Selecting previously unselected package python-tables-data.
Preparing to unpack .../51-python-tables-data_3.4.2-4_all.deb ...
Unpacking python-tables-data (3.4.2-4) ...
Selecting previously unselected package python3-bs4.
Preparing to unpack .../52-python3-bs4_4.6.0-1_all.deb ...
Unpacking python3-bs4 (4.6.0-1) ...
Selecting previously unselected package python3-chardet.
Preparing to unpack .../53-python3-chardet_3.0.4-1_all.deb ...
Unpacking python3-chardet (3.0.4-1) ...
Selecting previously unselected package python3-webencodings.
Preparing to unpack .../54-python3-webencodings_0.5-2_all.deb ...
Unpacking python3-webencodings (0.5-2) ...
Selecting previously unselected package python3-html5lib.
Preparing to unpack .../55-python3-html5lib_0.999999999-1_all.deb ...
Unpacking python3-html5lib (0.999999999-1) ...
Selecting previously unselected package python3-lxml:amd64.
Preparing to unpack .../56-python3-lxml_4.2.1-1ubuntu0.1_amd64.deb ...
Unpacking python3-lxml:amd64 (4.2.1-1ubuntu0.1) ...
Selecting previously unselected package python3-numexpr.
Preparing to unpack .../57-python3-numexpr_2.6.4-1_amd64.deb ...
Unpacking python3-numexpr (2.6.4-1) ...
Selecting previously unselected package python3-olefile.
Preparing to unpack .../58-python3-olefile_0.45.1-1_all.deb ...
Unpacking python3-olefile (0.45.1-1) ...
Selecting previously unselected package python3-tables-lib.
Preparing to unpack .../59-python3-tables-lib_3.4.2-4_amd64.deb ...
Unpacking python3-tables-lib (3.4.2-4) ...
Selecting previously unselected package python3-tables.
Preparing to unpack .../60-python3-tables_3.4.2-4_all.deb ...
Unpacking python3-tables (3.4.2-4) ...
Setting up libjs-jquery (3.2.1-1) ...
Setting up python3-yaml (3.12-1build2) ...
Setting up libblosc1 (1.14.2+ds1-1) ...
Setting up python3-pickleshare (0.7.4-2) ...
Setting up libgflags2.2 (2.2.1-1) ...
Setting up python3-pil:amd64 (5.1.0-1ubuntu0.2) ...
Setting up python3-simplegeneric (0.8.1-1) ...
Setting up python3-webencodings (0.5-2) ...
Setting up python3-tables-lib (3.4.2-4) ...
Setting up liblmdb0:amd64 (0.9.21-1ubuntu0.1) ...
Setting up python3-lxml:amd64 (4.2.1-1ubuntu0.1) ...
Setting up python3-olefile (0.45.1-1) ...
Setting up python3-six (1.11.0-2) ...
Setting up python3-pyparsing (2.2.0+dfsg1-2) ...
Setting up python3-cycler (0.10.0-1) ...
Setting up python-tables-data (3.4.2-4) ...
Setting up python3-pkg-resources (39.0.1-2) ...
Setting up python3-bs4 (4.6.0-1) ...
Setting up python3-gflags (1.5.1-5) ...
update-alternatives: using /usr/bin/python3-gflags2man to provide /usr/bin/gflags2man (gflags2man) in auto mode
Setting up libleveldb1v5:amd64 (1.20-2) ...
Setting up python3-skimage-lib:amd64 (0.13.1-2) ...
Setting up python3-pandas-lib (0.22.0-4ubuntu1) ...
Setting up python3-wcwidth (0.1.7+dfsg1-1) ...
Setting up libcurand9.1:amd64 (9.1.85-3ubuntu1) ...
Setting up python3-protobuf (3.0.0-9.1ubuntu1) ...
Setting up python3-ipython-genutils (0.2.0-1) ...
Setting up python3-nose (1.3.7-3) ...
Setting up libgoogle-glog0v5 (0.3.5-1) ...
Setting up python3-chardet (3.0.4-1) ...
Setting up python3-html5lib (0.999999999-1) ...
Setting up libjs-jquery-ui (1.12.1+dfsg-5) ...
Setting up libcublas9.1:amd64 (9.1.85-3ubuntu1) ...
Setting up ttf-bitstream-vera (1.10-8) ...
Setting up cython3 (0.26.1-0.4) ...
Setting up python3-pywt (0.5.1-1.1ubuntu4) ...
Setting up libcudart9.1:amd64 (9.1.85-3ubuntu1) ...
Setting up javascript-common (11) ...
Setting up python3-decorator (4.1.2-1) ...
Setting up python3-traitlets (4.3.2-1) ...
Setting up python3-ptyprocess (0.5.2-1) ...
Setting up python3-tz (2018.3-2) ...
Setting up python3-leveldb (0~svn68-3build3) ...
Setting up python3-dateutil (2.6.1-1) ...
Setting up python3-h5py (2.7.1-2) ...
Setting up fonts-lyx (2.2.4-0ubuntu0.18.04.1) ...
Setting up python3-pygments (2.2.0+dfsg-1) ...
Setting up libcaffe-cuda1:amd64 (1.0.0-6build1) ...
Setting up python3-scipy (0.19.1-2ubuntu1) ...
Setting up python3-prompt-toolkit (1.0.15-1) ...
Setting up python-matplotlib-data (2.1.1-2ubuntu3) ...
Setting up python3-numexpr (2.6.4-1) ...
Setting up python3-tables (3.4.2-4) ...
Setting up python3-pexpect (4.2.1-1) ...
Setting up python3-networkx (1.11-1ubuntu3) ...
Setting up python3-pandas (0.22.0-4ubuntu1) ...
Setting up caffe-tools-cuda (1.0.0-6build1) ...
Setting up python3-matplotlib (2.1.1-2ubuntu3) ...
Setting up python3-ipython (5.5.0-1) ...
Setting up python3-skimage (0.13.1-2) ...
Setting up python3-caffe-cuda (1.0.0-6build1) ...
Setting up caffe-cuda (1.0.0-6build1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for fontconfig (2.12.6-0ubuntu2) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
/sbin/ldconfig.real: /usr/local/lib/python3.6/dist-packages/ideep4py/lib/libmkldnn.so.0 is not a symbolic link


In [7]:
! echo $PYTHONPATH

import os
os.environ['PYTHONPATH'] += ":/content/ideepcolor/caffe_files"

! echo $PYTHONPATH


/tensorflow-1.15.0/python3.6:/env/python
/tensorflow-1.15.0/python3.6:/env/python:/content/ideepcolor/caffe_files

In [0]:
#sometimes this is not enough and running will fail
#in that case just move everything from `caffe_files` to parent

In [0]:
from os import listdir
from os.path import isfile, join
mypath='caffe_files'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for f in onlyfiles:
  !cp {'./caffe_files/'+f} {'./'+f}

In [0]:
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from data import colorize_image as CI
from skimage import color
from data import lab_gamut as lab

%matplotlib inline

In [0]:
base_path='//content/drive/My Drive/kontext/deold'

In [12]:
!mkdir {'"'+base_path+'/4"'}
!mkdir {'"'+base_path+'/4/colorized_auto"'}
!mkdir {'"'+base_path+'/4/colorized_ref"'}


mkdir: cannot create directory ‘//content/drive/My Drive/kontext/deold/4’: File exists
mkdir: cannot create directory ‘//content/drive/My Drive/kontext/deold/4/colorized_auto’: File exists
mkdir: cannot create directory ‘//content/drive/My Drive/kontext/deold/4/colorized_ref’: File exists

In [0]:
import caffe
import skimage

In [0]:
def load_image(filename, color=True):
    """
    Load an image converting from grayscale or alpha as needed.
    Parameters
    ----------
    filename : string
    color : boolean
        flag for color format. True (default) loads as RGB while False
        loads as intensity (if image is already grayscale).
    Returns
    -------
    image : an image with type np.float32 in range [0, 1]
        of size (H x W x 3) in RGB or
        of size (H x W x 1) in grayscale.
    """
    img = skimage.img_as_float(skimage.io.imread(filename)).astype(np.float32)
    if img.ndim == 2:
        img = img[:, :, np.newaxis]
        if color:
            img = np.tile(img, (1, 1, 3))
    elif img.shape[2] == 4:
        img = img[:, :, :3]
    return img

In [0]:
# Colorization with reference global histogram
def get_global_histogram(ref_path):
    ref_img_fullres = load_image(ref_path)
    img_glob_dist = (255*caffe.io.resize_image(ref_img_fullres,(Xd,Xd))).astype('uint8') # load image
    gt_glob_net.blobs['img_bgr'].data[...] = img_glob_dist[:,:,::-1].transpose((2,0,1)) # put into 
    gt_glob_net.forward();
    glob_dist_in = gt_glob_net.blobs['gt_glob_ab_313_drop'].data[0,:-1,0,0].copy()
    return (glob_dist_in,ref_img_fullres)

In [0]:
from os import listdir
from os.path import isfile, join
mypath=base_path+'/2/goodscenes'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

In [17]:
for ref in ['szf2']:
  # color histogram to use
  ref_path = ref+'.png'
  for img_path in onlyfiles:
    if ref in img_path: 
      print(img_path)   
      gpu_id = 0 # gpu to use
      Xd = 256

      # Colorization network
      cid = CI.ColorizeImageCaffeGlobDist(Xd)
      cid.prep_net(gpu_id,prototxt_path='./models/global_model/deploy_nodist.prototxt',\
                  caffemodel_path='./models/global_model/global_model.caffemodel')

      # Global distribution network - extracts global color statistics from an image
      gt_glob_net = caffe.Net('./models/global_model/global_stats.prototxt',\
                            './models/global_model/dummy.caffemodel', caffe.TEST)

      # Load image
      cid.load_image(base_path+'/2/goodscenes/'+img_path)

      # Dummy variables
      input_ab = np.zeros((2,Xd,Xd))
      input_mask = np.zeros((1,Xd,Xd))

      # Colorization without global histogram
      img_pred = cid.net_forward(input_ab,input_mask);
      img_pred_auto_fullres = cid.get_img_fullres()

      # Gray image
      img_gray_fullres = cid.get_img_gray_fullres()

      # Plot result
      plt.figure(figsize=(10,4))
      plt.imshow(np.concatenate((img_gray_fullres,img_pred_auto_fullres),axis=1));
      plt.imsave(base_path+'/4/'+'colorized_auto/'+img_path, img_pred_auto_fullres)
      plt.title('Grayscale image / Automatic colorization (no reference histogram)')
      plt.axis('off');

      (glob_dist_ref,ref_img_fullres) = get_global_histogram(base_path+'/2/goodscenes/'+ref_path)
      img_pred = cid.net_forward(input_ab,input_mask,glob_dist_ref);
      img_pred_withref_fullres = cid.get_img_fullres()

      plt.figure(figsize=(10,4))
      plt.imshow(img_gray_fullres);
      plt.title('Grayscale image')
      plt.axis('off');
      plt.figure(figsize=(10,4))
      plt.imshow(img_pred_withref_fullres);
      plt.imsave(base_path+'/4/'+'colorized_ref/'+img_path, img_pred_withref_fullres)
      plt.title('Resulting colorization with reference histogram')
      plt.axis('off');

      plt.figure(figsize=(4,4))
      plt.imshow(ref_img_fullres);
      plt.title('Reference image');
      plt.axis('off');


Output hidden; open in https://colab.research.google.com to view.

In [0]: