How to run on Google Colab Jupyter Notbook?

Author: rraallvvCreated Feb 26, 2018Updated Sep 8, 2021

This is how I've tried to run the demo on a Jupyter Notebook on Google Colab

Download the models

# Download the file we just uploaded.
#
# Replace the assignment below with your file ID
# to download a different file.
#
# A file ID looks like: 1uBtlaggVyWshwcyP6kEI-y_W3P8D26sz
file_id = '1ENgQm9TgabE1R99zhNf5q6meBvX6WFuq'

import io
import sys
from googleapiclient.http import MediaIoBaseDownload
from googleapiclient.discovery import build
drive_service = build('drive', 'v3')

filename = 'data.zip'

request = drive_service.files().get_media(fileId=file_id)
downloaded = io.FileIO(filename, 'wb')
downloader = MediaIoBaseDownload(downloaded, request)
done = False
while done is False:
  status, done = downloader.next_chunk()
  sys.stdout.write("\r{0:.0f}%".format(status.progress() * 100))
  sys.stdout.flush()

print('\rDownloaded file: {}'.format(filename))

Install dependencies

# Anaconda
! wget https://repo.continuum.io/archive/Anaconda2-5.1.0-Linux-x86_64.sh
! bash Anaconda2-5.1.0-Linux-x86_64.sh -b -p $HOME/anaconda

# PyTorch
! PATH=$HOME/anaconda/bin:$PATH && conda install pytorch=0.3.0 torchvision cuda90 -y -c pytorch
  
# Image magic
! apt-get install -y axel imagemagick

# Cupy
! PATH=$HOME/anaconda/bin:$PATH && pip install scikit-umfpack
! PATH=$HOME/anaconda/bin:$PATH && pip install -U setuptools
! PATH=$HOME/anaconda/bin:$PATH && pip install pynvrtc

! apt -y install libcusparse8.0 libnvrtc8.0 libnvtoolsext1
! ln -snf /usr/lib/x86_64-linux-gnu/libnvrtc-builtins.so.8.0 /usr/lib/x86_64-linux-gnu/libnvrtc-builtins.so
! PATH=$HOME/anaconda/bin:$PATH && pip install 'cupy-cuda90==4.0.0b4' 'chainer==4.0.0b4'

Copy the repository and the models

! git clone https://github.com/NVIDIA/FastPhotoStyle
! unzip data.zip -d FastPhotoStyle
! mkdir -p FastPhotoStyle/images && rm -fr FastPhotoStyle/images/* && mkdir -p FastPhotoStyle/results && rm -fr FastPhotoStyle/results/*
! axel -n 1 http://freebigpictures.com/wp-content/uploads/shady-forest.jpg --output=FastPhotoStyle/images/content1.png  
! axel -n 1 https://vignette.wikia.nocookie.net/strangerthings8338/images/e/e0/Wiki-background.jpeg/revision/latest?cb=20170522192233 --output=FastPhotoStyle/images/style1.png
! cd FastPhotoStyle/images && convert -resize 25% content1.png content1.png && convert -resize 50% style1.png style1.png

Run demo

! cd FastPhotoStyle && python demo.py

Output

Traceback (most recent call last):
  File "demo.py", line 8, in <module>
    import process_stylization
  File "/content/FastPhotoStyle/process_stylization.py", line 17, in <module>
    from smooth_filter import smooth_filter
  File "/content/FastPhotoStyle/smooth_filter.py", line 326, in <module>
    from cupy.cuda import function
ImportError: No module named cupy.cuda

I'm not sure what dependencies are missing or whether Cuda is already installed in the VM.