-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathaugmentation.py
executable file
·74 lines (54 loc) · 2.58 KB
/
augmentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Paper: "Fast and Accurate Image Super Resolution by Deep CNN with Skip Connection and Network in Network"
Author: Jin Yamanaka
Github: https://github.com/jiny2001/dcscn-image-super-resolution
Create Augmented training images
Put your images under data/[your dataset name]/ and specify [your dataset name] for --dataset.
--augment_level 2-8: will generate flipped / rotated images
"""
import os
import numpy as np
import tensorflow.compat.v1 as tf
from helper import args, utilty as util
args.flags.DEFINE_integer("augment_level", 4, "Augmentation level. 4:+LR/UD/LR-UD flipped, 7:+rotated")
FLAGS = args.get()
def main(not_parsed_args):
if len(not_parsed_args) > 1:
print("Unknown args:%s" % not_parsed_args)
exit()
print("Building x%d augmented data." % FLAGS.augment_level)
training_filenames = util.get_files_in_directory(FLAGS.data_dir + "/" + FLAGS.dataset + "/")
target_dir = FLAGS.data_dir + "/" + FLAGS.dataset + ("_%d/" % FLAGS.augment_level)
util.make_dir(target_dir)
for file_path in training_filenames:
org_image = util.load_image(file_path)
filename = os.path.basename(file_path)
filename, extension = os.path.splitext(filename)
new_filename = target_dir + filename
util.save_image(new_filename + extension, org_image)
if FLAGS.augment_level >= 2:
ud_image = np.flipud(org_image)
util.save_image(new_filename + "_v" + extension, ud_image)
if FLAGS.augment_level >= 3:
lr_image = np.fliplr(org_image)
util.save_image(new_filename + "_h" + extension, lr_image)
if FLAGS.augment_level >= 4:
lr_image = np.fliplr(org_image)
lrud_image = np.flipud(lr_image)
util.save_image(new_filename + "_hv" + extension, lrud_image)
if FLAGS.augment_level >= 5:
rotated_image1 = np.rot90(org_image)
util.save_image(new_filename + "_r1" + extension, rotated_image1)
if FLAGS.augment_level >= 6:
rotated_image2 = np.rot90(org_image, -1)
util.save_image(new_filename + "_r2" + extension, rotated_image2)
if FLAGS.augment_level >= 7:
rotated_image1 = np.rot90(org_image)
ud_image = np.flipud(rotated_image1)
util.save_image(new_filename + "_r1_v" + extension, ud_image)
if FLAGS.augment_level >= 8:
rotated_image2 = np.rot90(org_image, -1)
ud_image = np.flipud(rotated_image2)
util.save_image(new_filename + "_r2_v" + extension, ud_image)
if __name__ == '__main__':
tf.app.run()