Files
DeOldify/ImageColorizerColab.ipynb
T

8.4 KiB

Open In Colab

DeOldify on Colab

This notebook allows you to colorize your own images using Google Colab!

Special thanks to the that made this possible!

Original Author: Matt Robinson, matthew67robinson@gmail.com

Additional Contributions: Maria Benavente

In [ ]:
!git clone -b FastAIv1 --single-branch https://github.com/jantic/DeOldify.git DeOldify
In [ ]:
from os import path
import torch
print(torch.__version__)
print(torch.cuda.is_available())
In [ ]:
cd DeOldify
In [ ]:
!pip install PyDrive
In [ ]:
import os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
from google.colab import drive
from IPython.display import Image
import fastai
from fastai import *
from fasterai.visualize import *
from pathlib import Path
from itertools import repeat
from google.colab import drive
torch.backends.cudnn.benchmark=True
In [ ]:
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

Note that the above requires a verification step. It isn't too bad.

With access to your Google Drive, the "deOldifyImages" directory will be created. Drop there your personal images, and after the full execution of the notebook find the results at its subdirectory "results"

In [ ]:
results_dir=Path('/content/drive/My Drive/deOldifyImages/results')

#Adjust this if image doesn't look quite right (max 64 on 11GB GPU).  The default here works for most photos.  
#It literally just is a number multiplied by 16 to get the square render resolution.  
#Note that this doesn't affect the resolution of the final output- the output is the same resolution as the input.
#Example:  render_factor=21 => color is rendered at 16x21 = 336x336 px.  
render_factor=36

#◢ Artistic vs Stable Model

In [ ]:
artistic = False #@param {type:"boolean"}
In [ ]:
!mkdir 'models'
if artistic:
    !wget https://www.dropbox.com/s/9ne9su2mc5t0m38/ColorizeImagesArtistic_gen.pth?dl=0 -O ./models/ColorizeImagesArtistic_gen.pth
else:
    !wget https://www.dropbox.com/s/ztgygpaz1z3jkjg/ColorizeImagesStable_gen.pth?dl=0 -O ./models/ColorizeImagesStable_gen.pth
In [ ]:
from google.colab import drive
drive.mount('/content/drive')
In [ ]:
!mkdir "/content/drive/My Drive/deOldifyImages"
!mkdir "/content/drive/My Drive/deOldifyImages/results"
In [ ]:
vis = get_image_colorizer(render_factor=render_factor, artistic=artistic)

Here's an example of colorizing an image downloaded from the internet:

In [ ]:
!wget "https://media.githubusercontent.com/media/jantic/DeOldify/master/test_images/TV1930s.jpg" -O "family_TV.jpg"
In [ ]:
vis.plot_transformed_image('family_TV.jpg')

If you want to colorize pictures from your Google drive, drop them in a directory named deOldifyImages (in the root of your drive) and the next cell will colorize all of them and save the resulting images in deOldifyImages/results.

In [ ]:
for img in os.listdir("/content/drive/My Drive/deOldifyImages/"):
  img_path = str("/content/drive/My Drive/deOldifyImages/") + img
  if os.path.isfile(img_path):
    vis.plot_transformed_image(img_path)