Skip to content

Commit ecd3039

Browse files
committed
Refactor: remove all periods in python comment
1 parent e91dd8a commit ecd3039

File tree

7 files changed

+58
-58
lines changed

7 files changed

+58
-58
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This is a Tensorflow implementation of the Residual Encoder Network based on [Au
1515
* `read_input.py`: all functions related to input
1616
* `residual_encoder.py`: the residual encoder model
1717
* `common.py`: the common part for training and testing, which is mainly the workflow for this model
18-
* `train.py`: train the residual encoder model using Tensorflow built-in GradientDescentOptimizer
18+
* `train.py`: train the residual encoder model using Tensorflow built-in AdamOptimizer
1919
* `test.py`: test your own images and save the output images
2020

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

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

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

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

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

5959
## References
6060

common.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -35,64 +35,64 @@ def init_model(train=True):
3535
:param train: indicate if current is in training
3636
:return: all stuffs that need for this model
3737
"""
38-
# Create training summary folder if not exist.
38+
# Create training summary folder if not exist
3939
create_folder("summary/train/images")
4040

41-
# Create testing summary folder if not exist.
41+
# Create testing summary folder if not exist
4242
create_folder("summary/test/images")
4343

44-
# Use gpu if exist.
44+
# Use gpu if exist
4545
with tf.device('/device:GPU:0'):
46-
# Init image data file path.
46+
# Init image data file path
4747
print("⏳ Init input file path...")
4848
if train:
4949
file_paths = init_file_path(training_dir)
5050
else:
5151
file_paths = init_file_path(testing_dir)
5252

53-
# Init training flag and global step.
53+
# Init training flag and global step
5454
print("⏳ Init placeholder and variables...")
5555
is_training = tf.placeholder(tf.bool, name="is_training")
5656
global_step = tf.train.get_or_create_global_step()
5757

58-
# Load vgg16 model.
58+
# Load vgg16 model
5959
print("🤖 Load vgg16 model...")
6060
vgg = vgg16.Vgg16()
6161

62-
# Build residual encoder model.
62+
# Build residual encoder model
6363
print("🤖 Build residual encoder model...")
6464
residual_encoder = ResidualEncoder()
6565

66-
# Get dataset iterator.
66+
# Get dataset iterator
6767
iterator = get_dataset_iterator(file_paths, batch_size, shuffle=True)
6868

69-
# Get color image.
69+
# Get color image
7070
color_image_rgb = iterator.get_next(name="color_image_rgb")
7171
color_image_yuv = rgb_to_yuv(color_image_rgb, "color_image_yuv")
7272

73-
# Get gray image.
73+
# Get gray image
7474
gray_image_one_channel = tf.image.rgb_to_grayscale(color_image_rgb, name="gray_image_one_channel")
7575
gray_image_three_channels = tf.image.grayscale_to_rgb(gray_image_one_channel, name="gray_image_three_channels")
7676
gray_image_yuv = rgb_to_yuv(gray_image_three_channels, "gray_image_yuv")
7777

78-
# Build vgg model.
78+
# Build vgg model
7979
with tf.name_scope("vgg16"):
8080
vgg.build(gray_image_three_channels)
8181

82-
# Predict model.
82+
# Predict model
8383
predict = residual_encoder.build(input_data=gray_image_three_channels, vgg=vgg, is_training=is_training)
8484
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")
8585
predict_rgb = yuv_to_rgb(predict_yuv, "predict_rgb")
8686

87-
# Get loss.
87+
# Get loss
8888
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"))
8989

90-
# Prepare optimizer.
90+
# Prepare optimizer
9191
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
9292
with tf.control_dependencies(update_ops):
9393
optimizer = tf.train.AdamOptimizer().minimize(loss, global_step=global_step, name='optimizer')
9494

95-
# Init tensorflow summaries.
95+
# Init tensorflow summaries
9696
print("⏳ Init tensorflow summaries...")
9797
tf.summary.histogram("loss", loss)
9898
tf.summary.image("gray_image", gray_image_three_channels, max_outputs=1)

config.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,37 @@
1212
import tensorflow as tf
1313

1414

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

18-
# Image size for training.
18+
# Image size for training
1919
image_size = 224
2020

21-
# Image resize method.
21+
# Image resize method
2222
image_resize_method = tf.image.ResizeMethod.BILINEAR
2323

24-
# Parameters for neural network.
25-
training_iters = 3000000 # The training iterations number.
26-
batch_size = 6 # Batch size for training data.
27-
display_step = 50 # Step interval for displaying loss and saving summary during training phase.
28-
testing_step = 1000 # Step interval for testing and saving image during training phase.
29-
saving_step = 10000 # Step interval for saving model during training phase.
24+
# Parameters for neural network
25+
training_iters = 3000000 # The training iterations number
26+
batch_size = 6 # Batch size for training data
27+
display_step = 50 # Step interval for displaying loss and saving summary during training phase
28+
testing_step = 1000 # Step interval for testing and saving image during training phase
29+
saving_step = 10000 # Step interval for saving model during training phase
3030
shuffle_buffer_size = 2000
3131

3232
# UV channel normalization parameters
3333
u_norm_para = 0.435912
3434
v_norm_para = 0.614777
3535

36-
# Directory for training and testing dataset.
36+
# Directory for training and testing dataset
3737
training_dir = "train2014"
3838
testing_dir = "test2014"
3939

40-
# Model, result and generated images stored path.
40+
# Model, result and generated images stored path
4141
summary_path = "summary"
4242
training_summary = summary_path + "/train"
4343
testing_summary = summary_path + "/test"
4444

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

55-
# Gaussian blur kernel (not trainable).
55+
# Gaussian blur kernel (not trainable)
5656
gaussin_blur_3x3 = np.divide([
5757
[1., 2., 1.],
5858
[2., 4., 2.],

image_helper.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ def rgb_to_yuv(rgb_image, scope):
2121
:return: an image with YUV color space
2222
"""
2323
with tf.name_scope(scope):
24-
# Get r, g, b channel.
24+
# Get r, g, b channel
2525
_r = tf.slice(rgb_image, [0, 0, 0, 0], [-1, -1, -1, 1])
2626
_g = tf.slice(rgb_image, [0, 0, 0, 1], [-1, -1, -1, 1])
2727
_b = tf.slice(rgb_image, [0, 0, 0, 2], [-1, -1, -1, 1])
2828

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

35-
# Normalize u, v channel.
35+
# Normalize u, v channel
3636
_u = _u / (u_norm_para * 2) + 0.5
3737
_v = _v / (v_norm_para * 2) + 0.5
3838

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

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

55-
# Denormalize u, v channel.
55+
# Denormalize u, v channel
5656
_u = (_u - 0.5) * u_norm_para * 2
5757
_v = (_v - 0.5) * v_norm_para * 2
5858

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

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

6868
def concat_images(img_a, img_b):

read_input.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ def init_file_path(directory):
2828
print("Throwing all gray space images now... (this will take a long time if the training dataset is huge)")
2929

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

7575

residual_encoder.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ def build(self, input_data, vgg, is_training):
9494
if debug:
9595
assert input_data.get_shape().as_list()[1:] == [224, 224, 3]
9696

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

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

105-
# Backward upscale layer 4 and add convolutional layer 3.
105+
# Backward upscale layer 4 and add convolutional layer 3
106106
b_conv4_upscale = tf.image.resize_images(b_conv4, [56, 56], method=image_resize_method)
107107
bn_3 = self.batch_normal(vgg.conv3_3, "bn_3", is_training)
108108
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):
114114
assert b_conv3_input.get_shape().as_list()[1:] == [56, 56, 256]
115115
assert b_conv3.get_shape().as_list()[1:] == [56, 56, 128]
116116

117-
# Backward upscale layer 3 and add convolutional layer 2.
117+
# Backward upscale layer 3 and add convolutional layer 2
118118
b_conv3_upscale = tf.image.resize_images(b_conv3, [112, 112], method=image_resize_method)
119119
bn_2 = self.batch_normal(vgg.conv2_2, "bn_2", is_training)
120120
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):
126126
assert b_conv2_input.get_shape().as_list()[1:] == [112, 112, 128]
127127
assert b_conv2.get_shape().as_list()[1:] == [112, 112, 64]
128128

129-
# Backward upscale layer 2 and add convolutional layer 1.
129+
# Backward upscale layer 2 and add convolutional layer 1
130130
b_conv2_upscale = tf.image.resize_images(b_conv2, [224, 224], method=image_resize_method)
131131
bn_1 = self.batch_normal(vgg.conv1_2, "bn_1", is_training)
132132
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):
138138
assert b_conv1_input.get_shape().as_list()[1:] == [224, 224, 64]
139139
assert b_conv1.get_shape().as_list()[1:] == [224, 224, 3]
140140

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

151-
# Output layer.
151+
# Output layer
152152
output_layer = self.conv_layer(b_conv0, "output_conv", is_training, relu=False)
153153

154154
if debug:

train.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,32 @@
1818

1919

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

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

31-
# Create a session for running operations in the Graph.
31+
# Create a session for running operations in the Graph
3232
with tf.train.MonitoredTrainingSession(checkpoint_dir=summary_path,
3333
hooks=[summary_hook, checkpoint_hook, num_step_hook],
3434
scaffold=scaffold,
3535
config=config) as sess:
3636
print("🤖 Start training...")
3737

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

4242
if step % display_step == 0:
43-
# Print batch loss.
43+
# Print batch loss
4444
print("📖 Iter %d, Minibatch Loss = %f" % (step, l))
4545

46-
# Save testing image.
46+
# Save testing image
4747
if step % testing_step == 0:
4848
summary_image = concat_images(gray[0], pred[0])
4949
summary_image = concat_images(summary_image, color[0])

0 commit comments

Comments
 (0)