<jupyter_start><jupyter_text>** Artistic Colorizer ** ◢ DeOldify - Colorize your own photos!This notebook is a combination of 2 colab notebooks from [jantic/DeOldify](https://github.com/jantic/DeOldify) and 1 colab notebook from [Dakini/AnimeColorDeOldify](https://github.com/Dakini/AnimeColorDeOldify) and tries to combine everything related to images in one notebook. My fork can be found in [styler00dollar/Colab-DeOldify](https://github.com/styler00dollar/Colab-DeOldify).Special features:- Switch between 2 different DeOldify and 4 different AnimeColorDeOldify models.- Local file upload support. (The visualization might need to be improved)- I compressed code as much as possible. Less cells mean less clicks.**Original DeOldify credits:**Special thanks to:<NAME> and <NAME> for pioneering the DeOldify image colab notebook.  <NAME> for doing things, breaking stuff & having an opinion on everything.** IMPORTANT **In the "Runtime" menu for the notebook window, select "Change runtime type." Ensure that the following are selected:* Runtime Type = Python 3* Hardware Accelerator = GPU Warning: Restart runtime if you want to switch between anime and stock deoldify.  Using original DeOldify<jupyter_code>#@title ```Warning: ONLY run this if you want to force session reload. Do "restart runtime" instead.```
#import os
#os._exit(00)
#@title ◢ Git clone and install DeOldify
%cd /content/
#!git clone https://github.com/jantic/DeOldify.git DeOldify 
!git clone https://github.com/styler00dollar/Colab-DeOldify DeOldify
%cd DeOldify
#NOTE:  This must be the first call in order to work properly!
from deoldify import device
from deoldify.device_id import DeviceId
#choices:  CPU, GPU0...GPU7
device.set(device=DeviceId.GPU0)

import torch

if not torch.cuda.is_available():
    print('GPU not available.')
!pip install -r colab_requirements.txt
import fastai
from deoldify.visualize import *
!mkdir 'models'

!wget https://deepai-opensource-codebases-models.s3-us-west-2.amazonaws.com/deoldify/ColorizeArtistic_gen.pth -O ./models/ColorizeArtistic_gen.pth
!wget https://www.dropbox.com/s/usf7uifrctqw9rl/ColorizeStable_gen.pth?dl=0 -O ./models/ColorizeStable_gen.pth
!wget https://media.githubusercontent.com/media/jantic/DeOldify/master/resource_images/watermark.png -O ./resource_images/watermark.png

colorizer = get_image_colorizer(artistic=True)<jupyter_output><empty_output><jupyter_text>◢ Colorize<jupyter_code># You can switch between stable and artistic. The default is False. Set the value to True or False and run this cell.
%cd /content/DeOldify
colorizer = get_image_colorizer(artistic=True)
%cd /content/DeOldify
source_url = '' #@param {type:"string"}
render_factor = 35  #@param {type: "slider", min: 7, max: 40}
watermarked = False #@param {type:"boolean"}

if source_url is not None and source_url !='':
    image_path = colorizer.plot_transformed_image_from_url(url=source_url, render_factor=render_factor, compare=True, watermarked=watermarked)
    show_image_in_notebook(image_path)
else:
    print('Provide an image url and try again.')
#@title ## See how well render_factor values perform on the image here (Currently only URL support)
for i in range(10,40,2):
    colorizer.plot_transformed_image('test_images/image.png', render_factor=i, display_render_factor=True, figsize=(8,8))
#@title Upload file (Original file can be downloaded from ```/content/DeOldify/result_images```)
from google.colab import files
import matplotlib.pyplot as plt
import torchvision
import cv2
%cd /content/
!mkdir /content/files/
%cd /content/files/
uploaded_names = files.upload().keys()

if(len(uploaded_names)==0): raise Exception("You need to upload at least one image.")
filepath = "/content/files/"+list(uploaded_names)[0]

final_image = colorizer.get_transformed_image(filepath, render_factor=35)
final_image.save("/content/DeOldify/result_images/image.png")

%cd /content/DeOldify/result_images
final_image = cv2.imread("image.png")
final_image = cv2.cvtColor(final_image, cv2.COLOR_BGR2RGB)

plt.axis('off')
plt.figure(figsize=(20,20))
plt.imshow(final_image)
plt.show()

%cd /content/
!sudo rm -rf /content/files/<jupyter_output><empty_output><jupyter_text>AnimeColorDeOldifyThe original repo and colab can be found [here](https://github.com/Dakini/AnimeColorDeOldify). Special thanks to Dakini and his awesome AnimeColorDeOldify repo.<jupyter_code>#@title ```Warning: ONLY run this if you want to force session reload. Do "restart runtime" instead.```
import os
os._exit(00)
#@title Git clone and install AnimeColorDeOldify
%cd /content/
import torch
if not torch.cuda.is_available():
    print('GPU not available.')
!git clone https://github.com/Dakini/AnimeColorDeOldify.git AnimeColorDeOldify 
%cd  AnimeColorDeOldify
!pip install -r colab_requirements.txt
import fastai
from deoldify.visualize import *
torch.backends.cudnn.benchmark = True
!mkdir 'models'
from deoldify.visualize import *
!pip install wget
import wget
#@title Choose one of 4 models

%cd /content/AnimeColorDeOldify

anime_model = 'Sketch' #@param ["Image", "Grayscale", "Sketch", "Sketch2Gray"]

%cd /content/AnimeColorDeOldify/models
if anime_model == 'Image':
  # image
  #!wget https://www.dropbox.com/s/0m1rwdk7je1r39j/MQT9QHUfXxKpQ9b2CPda89htDBzFtmHD9r2YZsHp.pth?dl=1 -O ./models/ColorizeArtistic_gen.pth
  wget.download('https://www.dropbox.com/s/0m1rwdk7je1r39j/MQT9QHUfXxKpQ9b2CPda89htDBzFtmHD9r2YZsHp.pth?dl=1')
  os.rename(r'/content/AnimeColorDeOldify/models/MQT9QHUfXxKpQ9b2CPda89htDBzFtmHD9r2YZsHp.pth',r'/content/AnimeColorDeOldify/models/ColorizeArtistic_gen.pth')
if anime_model == 'Grayscale':
  #grayscale
  #!wget https://www.dropbox.com/s/ojrwwsre1gt6sfx/UDLVQJgsv5RwrTkVWAhS9LrdGDFzdbuN2SFxCFsZ.pth?dl=1 -O ./models/ColorizeArtistic_gen.pth
  wget.download('https://www.dropbox.com/s/ojrwwsre1gt6sfx/UDLVQJgsv5RwrTkVWAhS9LrdGDFzdbuN2SFxCFsZ.pth?dl=1')
  os.rename(r'/content/AnimeColorDeOldify/models/UDLVQJgsv5RwrTkVWAhS9LrdGDFzdbuN2SFxCFsZ.pth',r'/content/AnimeColorDeOldify/models/ColorizeArtistic_gen.pth')
if anime_model == 'Sketch':
  # sketch
  #!wget https://www.dropbox.com/s/lykykhvpy9byb7u/JG5yF2bRBdpEJweytyvBSz3Qu6jcg8cfZ5kcFYGY.pth?dl=1 -O ./models/ColorizeArtistic_gen.pth
  wget.download('https://www.dropbox.com/s/lykykhvpy9byb7u/JG5yF2bRBdpEJweytyvBSz3Qu6jcg8cfZ5kcFYGY.pth?dl=1')
  os.rename(r'/content/AnimeColorDeOldify/models/JG5yF2bRBdpEJweytyvBSz3Qu6jcg8cfZ5kcFYGY.pth',r'/content/AnimeColorDeOldify/models/ColorizeArtistic_gen.pth')
if anime_model == 'Sketch2Gray':
  # sketch2gray
  #!wget https://www.dropbox.com/s/6me8m9e7nfmlid6/tDEFrpvevtu6WGRKf2uV5cFtsFAEhuA5kmN7FpgZ.pth?dl=1 -O ./models/ColorizeArtistic_gen.pth
  wget.download('https://www.dropbox.com/s/6me8m9e7nfmlid6/tDEFrpvevtu6WGRKf2uV5cFtsFAEhuA5kmN7FpgZ.pth?dl=1')
  os.rename(r'/content/AnimeColorDeOldify/models/tDEFrpvevtu6WGRKf2uV5cFtsFAEhuA5kmN7FpgZ.pth',r'/content/AnimeColorDeOldify/models/ColorizeArtistic_gen.pth')
%cd /content/AnimeColorDeOldify/
stats = ([0.7137, 0.6628, 0.6519],[0.2970, 0.3017, 0.2979])
colorizer = get_image_colorizer(artistic=True,stats=stats)
source_url = '' #@param {type:"string"}
render_factor = 12 #@param {type:"slider", min:7, max:45, step:1}
watermarked = False #@param {type:"boolean"}

if source_url is not None and source_url !='':
    image_path = colorizer.plot_transformed_image_from_url(url=source_url, render_factor=render_factor, compare=True, post_process=False, watermarked=watermarked)
    show_image_in_notebook(image_path)
else:
    print('Provide an image url and try again.')
#@title ## See how well render_factor values perform on the image here (Currently only URL support)
for i in range(10,40,2):
    colorizer.plot_transformed_image('test_images/image.png', render_factor=i, display_render_factor=True, figsize=(8,8))
#@title Upload file (Original file can be downloaded from ```/content/AnimeColorDeOldify/result_images```)
from google.colab import files
import matplotlib.pyplot as plt
import torchvision
import cv2
%cd /content/
!mkdir /content/files/
%cd /content/files/
uploaded_names = files.upload().keys()

if(len(uploaded_names)==0): raise Exception("You need to upload at least one image.")
filepath = "/content/files/"+list(uploaded_names)[0]

final_image = colorizer.get_transformed_image(filepath, render_factor=35)
final_image.save("/content/AnimeColorDeOldify/result_images/image.png")

%cd /content/AnimeColorDeOldify/result_images
final_image = cv2.imread("image.png")
final_image = cv2.cvtColor(final_image, cv2.COLOR_BGR2RGB)

plt.axis('off')
plt.figure(figsize=(20,20))
plt.imshow(final_image)
plt.show()

%cd /content/
!sudo rm -rf /content/files/<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11693408749855708, 'fraction_numerical': 0.02412559159644465, 'mean_word_length': 6.292929292929293, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 18, 'lorem ipsum': 0, 'www.': 9, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '439822', 'license': 'MIT', 'n_tokens_mistral': 3333, 'n_tokens_neox': 3224, 'n_words': 656, 'path': 'Colab-DeOldify-(Original+Anime).ipynb', 'repo_name': 'styler00dollar/Colab-DeOldify', 'size': '8684'}