-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
188 lines (159 loc) · 6.54 KB
/
train.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import argparse
import glob
import logging
import pylab as plt
import tensorflow as tf
import tqdm
import docclean
logger = logging.getLogger(__name__)
AUTOTUNE = tf.data.experimental.AUTOTUNE
if __name__ == "__main__":
parser = argparse.ArgumentParser(
"Training Script for DocClean",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-t",
"--type",
help="Which model to train",
choices=["cycle_gan", "autoencoder"],
required=True,
type=str,
)
parser.add_argument(
"-k", "--kaggle_data_dir", help="Kaggle Data Directory", required=True
)
parser.add_argument(
"-c",
"--clean_books_dir",
help="Directory containing clean images",
required=False,
default=None,
)
parser.add_argument(
"-d",
"--dirty_books_dir",
help="Directory containing dirty images",
required=False,
default=None,
)
parser.add_argument(
"-e",
"--epochs",
help="Number of epochs to train for",
type=int,
default=100,
required=False,
)
parser.add_argument(
"-b", "--batch_size", help="Batch size", type=int, default=16, required=False
)
parser.add_argument("-v", "--verbose", help="Be verbose", action="store_true")
args = parser.parse_args()
logging_format = (
"%(asctime)s - %(funcName)s -%(name)s - %(levelname)s - %(message)s"
)
if args.verbose:
logging.basicConfig(level=logging.DEBUG, format=logging_format)
else:
logging.basicConfig(level=logging.INFO, format=logging_format)
# Set up kaggle data ingestion
kaggle_img_list = glob.glob(f"{args.kaggle_data_dir}/*png")
train_imgs_list = []
logging.info("Reading Kaggle Data.")
for img in tqdm.tqdm(kaggle_img_list):
im_frame = plt.imread(img)
if im_frame.shape[0] != 258:
train_imgs_list.append(img)
clean_imgs_list = [x.replace("train", "train_cleaned") for x in train_imgs_list]
if args.type == "autoencoder":
list_ds = tf.data.Dataset.from_tensor_slices(train_imgs_list)
list_ds = list_ds.shuffle(buffer_size=96, seed=1993)
train_dataset = list_ds.take(80)
validate_dataset = list_ds.skip(80)
list_ds = train_dataset.repeat(15)
validate_dataset = validate_dataset.repeat(10)
labeled_ds = list_ds.map(
docclean.utils.get_kaggle_paired_data, num_parallel_calls=AUTOTUNE
).cache()
val_ds = validate_dataset.map(
docclean.utils.get_kaggle_paired_data, num_parallel_calls=AUTOTUNE
).cache()
labeled_ds = labeled_ds.map(
docclean.utils.kaggle_paired_augment, num_parallel_calls=AUTOTUNE
)
val_ds = val_ds.map(
docclean.utils.kaggle_paired_augment, num_parallel_calls=AUTOTUNE
)
labeled_ds = labeled_ds.batch(args.batch_size)
labeled_ds = labeled_ds.prefetch(AUTOTUNE)
val_ds = val_ds.batch(args.batch_size)
val_ds = val_ds.prefetch(AUTOTUNE)
logging.info("Creating Autoencoder Model")
autoencoder = docclean.autoencoder.Autoencoder()
logging.info(f"Training Autoencoder Model for {args.epochs} epochs.")
autoencoder.train_model(labeled_ds, validation_data=val_ds, epochs=args.epochs)
logging.info(f"Saving the model under the name Docclean_autoencoder.")
autoencoder.autoencoder_model.save_weights("weights/ae")
else:
kaggle_dirty_images = tf.data.Dataset.from_tensor_slices(train_imgs_list)
kaggle_clean_images = tf.data.Dataset.from_tensor_slices(clean_imgs_list)
kaggle_dirty_images = kaggle_dirty_images.shuffle(buffer_size=96, seed=1993)
kaggle_clean_images = kaggle_clean_images.shuffle(buffer_size=96, seed=1993)
kaggle_dirty_images = kaggle_dirty_images.take(80)
kaggle_clean_images = kaggle_clean_images.take(80)
kaggle_dirty_images = kaggle_dirty_images.repeat(10)
kaggle_clean_images = kaggle_clean_images.repeat(10)
kaggle_dirty_images = kaggle_dirty_images.map(
docclean.utils.get_kaggle_data, num_parallel_calls=AUTOTUNE
).cache()
kaggle_clean_images = kaggle_clean_images.map(
docclean.utils.get_kaggle_data, num_parallel_calls=AUTOTUNE
).cache()
kaggle_dirty_images = kaggle_dirty_images.map(
docclean.utils.kaggle_crop_and_augment, num_parallel_calls=AUTOTUNE
)
kaggle_clean_images = kaggle_clean_images.map(
docclean.utils.kaggle_crop_and_augment, num_parallel_calls=AUTOTUNE
)
if args.dirty_books_dir is not None:
logging.info("Reading Dirty/Clean books data.")
books_dirty_images = tf.data.Dataset.from_tensor_slices(
glob.glob(f"{args.dirty_books_dir}/*png")
).shuffle(buffer_size=40960)
books_clean_images = tf.data.Dataset.from_tensor_slices(
glob.glob(f"{args.clean_books_dir}/*png")
).shuffle(buffer_size=40960)
books_dirty_images = books_dirty_images.map(
docclean.utils.get_png_data
).cache()
books_clean_images = books_clean_images.map(
docclean.utils.get_png_data
).cache()
books_dirty_images = books_dirty_images.map(
docclean.utils.books_crop_and_augment, num_parallel_calls=AUTOTUNE
)
books_clean_images = books_clean_images.map(
docclean.utils.books_crop_and_augment, num_parallel_calls=AUTOTUNE
)
dirty_images = tf.data.Dataset.concatenate(
kaggle_dirty_images, books_dirty_images
)
clean_images = tf.data.Dataset.concatenate(
kaggle_clean_images, books_clean_images
)
else:
dirty_images = kaggle_dirty_images
clean_images = kaggle_clean_images
dirty_images = (
dirty_images.shuffle(4096).batch(args.batch_size).prefetch(AUTOTUNE)
)
clean_images = (
clean_images.shuffle(4096).batch(args.batch_size).prefetch(AUTOTUNE)
)
logging.info("Creating the Cycle GAN model.")
cycle_gan = docclean.cycle_gan.CycleGan()
logging.info("Training the Cycle GAN model.")
cycle_gan.train(dirty_images, clean_images, epochs=args.epochs)
logging.info("Saving the model under Docclean_cyclegan")
cycle_gan.generator_g.save_weights("weights/cg")