Skip to content
Permalink
Browse files

Refactor: remove all periods in python comment

  • Loading branch information...
Armour committed Jul 3, 2018
1 parent e91dd8a commit ecd3039c6021ad260c7145a516de4e34f64a0c9b
Showing with 58 additions and 58 deletions.
  1. +4 βˆ’4 README.md
  2. +15 βˆ’15 common.py
  3. +13 βˆ’13 config.py
  4. +8 βˆ’8 image_helper.py
  5. +6 βˆ’6 read_input.py
  6. +6 βˆ’6 residual_encoder.py
  7. +6 βˆ’6 train.py
@@ -15,7 +15,7 @@ This is a Tensorflow implementation of the Residual Encoder Network based on [Au
* `read_input.py`: all functions related to input
* `residual_encoder.py`: the residual encoder model
* `common.py`: the common part for training and testing, which is mainly the workflow for this model
* `train.py`: train the residual encoder model using Tensorflow built-in GradientDescentOptimizer
* `train.py`: train the residual encoder model using Tensorflow built-in AdamOptimizer
* `test.py`: test your own images and save the output images

## Tensorflow graph
@@ -26,11 +26,11 @@ This is a Tensorflow implementation of the Residual Encoder Network based on [Au

* First please download pre-trained VGG16 model [vgg16.npy](https://mega.nz/#!YU1FWJrA!O1ywiCS2IiOlUCtCpI6HTJOMrneN-Qdv3ywQP5poecM) to vgg folder

* Use pre-trained residual encoder model
* Option 1: Use pre-trained residual encoder model
* Model can be downloaded [here](https://github.com/Armour/Automatic-Image-Colorization/releases/tag/2.0)
* Unzip all files to `summary_path` (you can change this path in `config.py`)

* Train your own model
* Option 2: Train your own model!
1. Change the `batch_size` and `training_iters` if you want.
2. Change `training_dir` to your directory that contains all your training jpg images
3. Run `python train.py`
@@ -54,7 +54,7 @@ This is a Tensorflow implementation of the Residual Encoder Network based on [Au
* ![11](images/11.png)
* ![12](images/12.png)

More examples can be found at [sample_output_images](https://github.com/Armour/Automatic-Image-Colorization/blob/master/sample_output_images) folder.
* More example output images can be found in [sample_output_images](https://github.com/Armour/Automatic-Image-Colorization/blob/master/sample_output_images) folder.

## References

@@ -35,64 +35,64 @@ def init_model(train=True):
:param train: indicate if current is in training
:return: all stuffs that need for this model
"""
# Create training summary folder if not exist.
# Create training summary folder if not exist
create_folder("summary/train/images")

# Create testing summary folder if not exist.
# Create testing summary folder if not exist
create_folder("summary/test/images")

# Use gpu if exist.
# Use gpu if exist
with tf.device('/device:GPU:0'):
# Init image data file path.
# Init image data file path
print("⏳ Init input file path...")
if train:
file_paths = init_file_path(training_dir)
else:
file_paths = init_file_path(testing_dir)

# Init training flag and global step.
# Init training flag and global step
print("⏳ Init placeholder and variables...")
is_training = tf.placeholder(tf.bool, name="is_training")
global_step = tf.train.get_or_create_global_step()

# Load vgg16 model.
# Load vgg16 model
print("πŸ€– Load vgg16 model...")
vgg = vgg16.Vgg16()

# Build residual encoder model.
# Build residual encoder model
print("πŸ€– Build residual encoder model...")
residual_encoder = ResidualEncoder()

# Get dataset iterator.
# Get dataset iterator
iterator = get_dataset_iterator(file_paths, batch_size, shuffle=True)

# Get color image.
# Get color image
color_image_rgb = iterator.get_next(name="color_image_rgb")
color_image_yuv = rgb_to_yuv(color_image_rgb, "color_image_yuv")

# Get gray image.
# Get gray image
gray_image_one_channel = tf.image.rgb_to_grayscale(color_image_rgb, name="gray_image_one_channel")
gray_image_three_channels = tf.image.grayscale_to_rgb(gray_image_one_channel, name="gray_image_three_channels")
gray_image_yuv = rgb_to_yuv(gray_image_three_channels, "gray_image_yuv")

# Build vgg model.
# Build vgg model
with tf.name_scope("vgg16"):
vgg.build(gray_image_three_channels)

# Predict model.
# Predict model
predict = residual_encoder.build(input_data=gray_image_three_channels, vgg=vgg, is_training=is_training)
predict_yuv = tf.concat(axis=3, values=[tf.slice(gray_image_yuv, [0, 0, 0, 0], [-1, -1, -1, 1], name="gray_image_y"), predict], name="predict_yuv")
predict_rgb = yuv_to_rgb(predict_yuv, "predict_rgb")

# Get loss.
# Get loss
loss = residual_encoder.get_loss(predict_val=predict, real_val=tf.slice(color_image_yuv, [0, 0, 0, 1], [-1, -1, -1, 2], name="color_image_uv"))

# Prepare optimizer.
# Prepare optimizer
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
optimizer = tf.train.AdamOptimizer().minimize(loss, global_step=global_step, name='optimizer')

# Init tensorflow summaries.
# Init tensorflow summaries
print("⏳ Init tensorflow summaries...")
tf.summary.histogram("loss", loss)
tf.summary.image("gray_image", gray_image_three_channels, max_outputs=1)
@@ -12,37 +12,37 @@
import tensorflow as tf


# Debug flag, if true, will check model shape using assert in each step and skip gray image check part (to save time).
# Debug flag, if true, will check model shape using assert in each step and skip gray image check part (to save time)
debug = False

# Image size for training.
# Image size for training
image_size = 224

# Image resize method.
# Image resize method
image_resize_method = tf.image.ResizeMethod.BILINEAR

# Parameters for neural network.
training_iters = 3000000 # The training iterations number.
batch_size = 6 # Batch size for training data.
display_step = 50 # Step interval for displaying loss and saving summary during training phase.
testing_step = 1000 # Step interval for testing and saving image during training phase.
saving_step = 10000 # Step interval for saving model during training phase.
# Parameters for neural network
training_iters = 3000000 # The training iterations number
batch_size = 6 # Batch size for training data
display_step = 50 # Step interval for displaying loss and saving summary during training phase
testing_step = 1000 # Step interval for testing and saving image during training phase
saving_step = 10000 # Step interval for saving model during training phase
shuffle_buffer_size = 2000

# UV channel normalization parameters
u_norm_para = 0.435912
v_norm_para = 0.614777

# Directory for training and testing dataset.
# Directory for training and testing dataset
training_dir = "train2014"
testing_dir = "test2014"

# Model, result and generated images stored path.
# Model, result and generated images stored path
summary_path = "summary"
training_summary = summary_path + "/train"
testing_summary = summary_path + "/test"

# Weights for each layer (trainable).
# Weights for each layer (trainable)
weights = {
'b_conv4': tf.Variable(tf.truncated_normal([1, 1, 512, 256], stddev=0.01), trainable=True),
'b_conv3': tf.Variable(tf.truncated_normal([3, 3, 256, 128], stddev=0.01), trainable=True),
@@ -52,7 +52,7 @@
'output_conv': tf.Variable(tf.truncated_normal([3, 3, 3, 2], stddev=0.01), trainable=True),
}

# Gaussian blur kernel (not trainable).
# Gaussian blur kernel (not trainable)
gaussin_blur_3x3 = np.divide([
[1., 2., 1.],
[2., 4., 2.],
@@ -21,22 +21,22 @@ def rgb_to_yuv(rgb_image, scope):
:return: an image with YUV color space
"""
with tf.name_scope(scope):
# Get r, g, b channel.
# Get r, g, b channel
_r = tf.slice(rgb_image, [0, 0, 0, 0], [-1, -1, -1, 1])
_g = tf.slice(rgb_image, [0, 0, 0, 1], [-1, -1, -1, 1])
_b = tf.slice(rgb_image, [0, 0, 0, 2], [-1, -1, -1, 1])

# Calculate y, u, v channel.
# Calculate y, u, v channel
# https://www.pcmag.com/encyclopedia/term/55166/yuv-rgb-conversion-formulas
_y = 0.299 * _r + 0.587 * _g + 0.114 * _b
_u = 0.492 * (_b - _y)
_v = 0.877 * (_r - _y)

# Normalize u, v channel.
# Normalize u, v channel
_u = _u / (u_norm_para * 2) + 0.5
_v = _v / (v_norm_para * 2) + 0.5

# Get image with YUV color space.
# Get image with YUV color space
return tf.clip_by_value(tf.concat(axis=3, values=[_y, _u, _v]), 0.0, 1.0)

def yuv_to_rgb(yuv_image, scope):
@@ -47,22 +47,22 @@ def yuv_to_rgb(yuv_image, scope):
:return: an image with RGB color space
"""
with tf.name_scope(scope):
# Get y, u, v channel.
# Get y, u, v channel
_y = tf.slice(yuv_image, [0, 0, 0, 0], [-1, -1, -1, 1])
_u = tf.slice(yuv_image, [0, 0, 0, 1], [-1, -1, -1, 1])
_v = tf.slice(yuv_image, [0, 0, 0, 2], [-1, -1, -1, 1])

# Denormalize u, v channel.
# Denormalize u, v channel
_u = (_u - 0.5) * u_norm_para * 2
_v = (_v - 0.5) * v_norm_para * 2

# Calculate r, g, b channel.
# Calculate r, g, b channel
# https://www.pcmag.com/encyclopedia/term/55166/yuv-rgb-conversion-formulas
_r = _y + 1.14 * _v
_g = _y - 0.395 * _u - 0.581 * _v
_b = _y + 2.033 * _u

# Get image with RGB color space.
# Get image with RGB color space
return tf.clip_by_value(tf.concat(axis=3, values=[_r, _g, _b]), 0.0, 1.0)

def concat_images(img_a, img_b):
@@ -28,12 +28,12 @@ def init_file_path(directory):
print("Throwing all gray space images now... (this will take a long time if the training dataset is huge)")

for file_name in os.listdir(directory):
# Skip files that is not jpg.
# Skip files that is not jpg
file_path = '%s/%s' % (directory, file_name)
if imghdr.what(file_path) is not 'jpeg':
continue
if not debug:
# Delete all gray space images.
# Delete all gray space images
is_gray_space = True
img = cv2.imread(file_path, cv2.IMREAD_UNCHANGED)
if len(img.shape) == 3 and img.shape[2] == 3:
@@ -63,13 +63,13 @@ def read_image(filename):
:param filename_queue: the filename queue for image files
:return: image with RGB color space
"""
# Read image file.
# Read image file
content = tf.read_file(filename)
# Decode the image with RGB color space.
# Decode the image with RGB color space
rgb_image = tf.image.decode_jpeg(content, channels=3, name="color_image_original")
# Resize image to the right image_size.
# Resize image to the right image_size
rgb_image = tf.image.resize_images(rgb_image, [image_size, image_size], method=image_resize_method)
# Map all pixel element value into [0, 1].
# Map all pixel element value into [0, 1]
return tf.clip_by_value(tf.div(tf.cast(rgb_image, tf.float32), 255), 0.0, 1.0, name="color_image_in_0_1")


@@ -94,15 +94,15 @@ def build(self, input_data, vgg, is_training):
if debug:
assert input_data.get_shape().as_list()[1:] == [224, 224, 3]

# Batch norm and 1x1 convolutional layer 4.
# Batch norm and 1x1 convolutional layer 4
bn_4 = self.batch_normal(vgg.conv4_3, "bn_4", is_training)
b_conv4 = self.conv_layer(bn_4, "b_conv4", is_training, bn=False)

if debug:
assert bn_4.get_shape().as_list()[1:] == [28, 28, 512]
assert b_conv4.get_shape().as_list()[1:] == [28, 28, 256]

# Backward upscale layer 4 and add convolutional layer 3.
# Backward upscale layer 4 and add convolutional layer 3
b_conv4_upscale = tf.image.resize_images(b_conv4, [56, 56], method=image_resize_method)
bn_3 = self.batch_normal(vgg.conv3_3, "bn_3", is_training)
b_conv3_input = tf.add(bn_3, b_conv4_upscale, name="b_conv3_input")
@@ -114,7 +114,7 @@ def build(self, input_data, vgg, is_training):
assert b_conv3_input.get_shape().as_list()[1:] == [56, 56, 256]
assert b_conv3.get_shape().as_list()[1:] == [56, 56, 128]

# Backward upscale layer 3 and add convolutional layer 2.
# Backward upscale layer 3 and add convolutional layer 2
b_conv3_upscale = tf.image.resize_images(b_conv3, [112, 112], method=image_resize_method)
bn_2 = self.batch_normal(vgg.conv2_2, "bn_2", is_training)
b_conv2_input = tf.add(bn_2, b_conv3_upscale, name="b_conv2_input")
@@ -126,7 +126,7 @@ def build(self, input_data, vgg, is_training):
assert b_conv2_input.get_shape().as_list()[1:] == [112, 112, 128]
assert b_conv2.get_shape().as_list()[1:] == [112, 112, 64]

# Backward upscale layer 2 and add convolutional layer 1.
# Backward upscale layer 2 and add convolutional layer 1
b_conv2_upscale = tf.image.resize_images(b_conv2, [224, 224], method=image_resize_method)
bn_1 = self.batch_normal(vgg.conv1_2, "bn_1", is_training)
b_conv1_input = tf.add(bn_1, b_conv2_upscale, name="b_conv1_input")
@@ -138,7 +138,7 @@ def build(self, input_data, vgg, is_training):
assert b_conv1_input.get_shape().as_list()[1:] == [224, 224, 64]
assert b_conv1.get_shape().as_list()[1:] == [224, 224, 3]

# Backward upscale layer 1 and add input layer.
# Backward upscale layer 1 and add input layer
bn_0 = self.batch_normal(input_data, "bn_0", is_training)
b_conv0_input = tf.add(bn_0, b_conv1, name="b_conv0_input")
b_conv0 = self.conv_layer(b_conv0_input, "b_conv0", is_training)
@@ -148,7 +148,7 @@ def build(self, input_data, vgg, is_training):
assert b_conv0_input.get_shape().as_list()[1:] == [224, 224, 3]
assert b_conv0.get_shape().as_list()[1:] == [224, 224, 3]

# Output layer.
# Output layer
output_layer = self.conv_layer(b_conv0, "output_conv", is_training, relu=False)

if debug:
@@ -18,32 +18,32 @@


if __name__ == '__main__':
# Init model.
# Init model
is_training, global_step, optimizer, loss, predict_rgb, color_image_rgb, gray_image, _ = init_model(train=True)

# Init scaffold, hooks and config.
# Init scaffold, hooks and config
scaffold = tf.train.Scaffold()
summary_hook = tf.train.SummarySaverHook(output_dir=training_summary, save_steps=display_step, scaffold=scaffold)
checkpoint_hook = tf.train.CheckpointSaverHook(checkpoint_dir=summary_path, save_steps=saving_step, scaffold=scaffold)
num_step_hook = tf.train.StopAtStepHook(num_steps=training_iters)
config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True, gpu_options=(tf.GPUOptions(allow_growth=True)))

# Create a session for running operations in the Graph.
# Create a session for running operations in the Graph
with tf.train.MonitoredTrainingSession(checkpoint_dir=summary_path,
hooks=[summary_hook, checkpoint_hook, num_step_hook],
scaffold=scaffold,
config=config) as sess:
print("πŸ€– Start training...")

while not sess.should_stop():
# Run optimizer.
# Run optimizer
_, step, l, pred, color, gray = sess.run([optimizer, global_step, loss, predict_rgb, color_image_rgb, gray_image] , feed_dict={is_training: True})

if step % display_step == 0:
# Print batch loss.
# Print batch loss
print("πŸ“– Iter %d, Minibatch Loss = %f" % (step, l))

# Save testing image.
# Save testing image
if step % testing_step == 0:
summary_image = concat_images(gray[0], pred[0])
summary_image = concat_images(summary_image, color[0])

0 comments on commit ecd3039

Please sign in to comment.
You can’t perform that action at this time.