Skip to content

Instantly share code, notes, and snippets.



Last active Jun 11, 2017
What would you like to do?
# "Colorizing B/W Movies with Neural Nets",
# Network/Code Created by Ryan Dahl, hacked by to work with movies
# 1. Download TensorFlow model from:
# 2. Use FFMPEG or such to extract frames from video.
# 3. Make sure your images are 224x224 pixels dimension. You can use imagemagicks "mogrify", here some useful commands:
# mogrify -resize 224x224 *.jpg
# mogrify -gravity center -background black -extent 224x224 *.jpg
# mogrify -colorspace sRGB -type TrueColor *.jpg
# 4. Create a directory "kidframe" next to this python file, put your extracted video frames inside.
# 5. Make sure to have a directory called "out" next to it. Inside "out" a second directory analogue to first ("kidframe")-
# 6. Run: python
# 7. Grab your rendered frames at "out/kidframe/xxx0001.jpg".
# 8. Recombine frames with FFMPEG, e.g:
# cat *.jpg | ffmpeg -f image2pipe -r 25 -vcodec mjpeg -i - -vcodec libx264 out.mp4
import tensorflow as tf
import skimage.transform
from import imsave, imread
import os
from os import listdir, path
from os.path import isfile, join
def get_directory(folder):
foundfile = []
for path, subdirs, files in os.walk(folder):
for name in files:
found = os.path.join(path, name)
if name.endswith('.jpg'):
return foundfile
def load_image(path):
img = imread(path)
# crop image from center
short_edge = min(img.shape[:2])
yy = int((img.shape[0] - short_edge) / 2)
xx = int((img.shape[1] - short_edge) / 2)
crop_img = img[yy : yy + short_edge, xx : xx + short_edge]
# resize to 224, 224
img = skimage.transform.resize(crop_img, (224, 224))
# desaturate image
return (img[:,:,0] + img[:,:,1] + img[:,:,2]) / 3.0
with open("colorize.tfmodel", mode='rb') as f:
fileContent =
graph_def = tf.GraphDef()
grayscale = tf.placeholder("float", [1, 224, 224, 1])
tf.import_graph_def(graph_def, input_map={ "grayscale": grayscale }, name='')
images = get_directory("kidframes")
for image in images:
print image
shark_gray = load_image(image).reshape(1, 224, 224, 1)
with tf.Session() as sess:
inferred_rgb = sess.graph.get_tensor_by_name("inferred_rgb:0")
inferred_batch =, feed_dict={ grayscale: shark_gray })
filename = "out/"+image
imsave(filename, inferred_batch[0])
print "saved " + filename

This comment has been minimized.

Copy link

@sinanisler sinanisler commented Sep 17, 2016

thanks man 👍


This comment has been minimized.

Copy link

@aidiss aidiss commented Mar 17, 2017

Where can i find inferred_rgb:0 tensor?
KeyError: "The name 'inferred_rgb:0' refers to a Tensor which does not exist. The operation, 'inferred_rgb', does not exist in the graph.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment