@ -42,7 +42,8 @@ def run_simple(dataset: tf.data.Dataset,
|
|||||||
channels: int = 1,
|
channels: int = 1,
|
||||||
zsize: int = 32,
|
zsize: int = 32,
|
||||||
batch_size: int = 128,
|
batch_size: int = 128,
|
||||||
verbose: bool = True) -> None:
|
verbose: bool = False,
|
||||||
|
debug: bool = False) -> None:
|
||||||
"""
|
"""
|
||||||
Runs the trained auto-encoder for given data set.
|
Runs the trained auto-encoder for given data set.
|
||||||
|
|
||||||
@ -55,7 +56,8 @@ def run_simple(dataset: tf.data.Dataset,
|
|||||||
channels: number of channels in input image (default: 1)
|
channels: number of channels in input image (default: 1)
|
||||||
zsize: size of the intermediary z (default: 32)
|
zsize: size of the intermediary z (default: 32)
|
||||||
batch_size: size of each batch (default: 128)
|
batch_size: size of each batch (default: 128)
|
||||||
verbose: if True prints train progress info to console (default: True)
|
verbose: if True training progress is printed to console (default: False)
|
||||||
|
debug: if True summaries are collected (default: False)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# checkpointed tensors and variables
|
# checkpointed tensors and variables
|
||||||
@ -77,6 +79,7 @@ def run_simple(dataset: tf.data.Dataset,
|
|||||||
outputs = _run_one_epoch_simple(dataset,
|
outputs = _run_one_epoch_simple(dataset,
|
||||||
batch_size=batch_size,
|
batch_size=batch_size,
|
||||||
global_step=global_step,
|
global_step=global_step,
|
||||||
|
debug=debug,
|
||||||
**checkpointables)
|
**checkpointables)
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
@ -88,6 +91,7 @@ def run_simple(dataset: tf.data.Dataset,
|
|||||||
|
|
||||||
def _run_one_epoch_simple(dataset: tf.data.Dataset,
|
def _run_one_epoch_simple(dataset: tf.data.Dataset,
|
||||||
batch_size: int,
|
batch_size: int,
|
||||||
|
debug: bool,
|
||||||
encoder: model.Encoder,
|
encoder: model.Encoder,
|
||||||
decoder: model.Decoder,
|
decoder: model.Decoder,
|
||||||
global_step: tf.Variable) -> Dict[str, float]:
|
global_step: tf.Variable) -> Dict[str, float]:
|
||||||
@ -103,7 +107,7 @@ def _run_one_epoch_simple(dataset: tf.data.Dataset,
|
|||||||
global_step=global_step)
|
global_step=global_step)
|
||||||
enc_dec_loss_avg(reconstruction_loss)
|
enc_dec_loss_avg(reconstruction_loss)
|
||||||
|
|
||||||
if int(global_step % train.LOG_FREQUENCY) == 0:
|
if int(global_step % train.LOG_FREQUENCY) == 0 and debug:
|
||||||
comparison = K.concatenate([x[:int(batch_size / 2)], x_decoded[:int(batch_size / 2),
|
comparison = K.concatenate([x[:int(batch_size / 2)], x_decoded[:int(batch_size / 2),
|
||||||
z[:int(batch_size / 2)]]], axis=0)
|
z[:int(batch_size / 2)]]], axis=0)
|
||||||
grid = util.prepare_image(comparison.cpu(), nrow=int(batch_size / 2))
|
grid = util.prepare_image(comparison.cpu(), nrow=int(batch_size / 2))
|
||||||
@ -126,7 +130,8 @@ def _run_one_epoch_simple(dataset: tf.data.Dataset,
|
|||||||
|
|
||||||
def _run_enc_dec_step_simple(encoder: model.Encoder, decoder: model.Decoder,
|
def _run_enc_dec_step_simple(encoder: model.Encoder, decoder: model.Decoder,
|
||||||
inputs: tf.Tensor,
|
inputs: tf.Tensor,
|
||||||
global_step: tf.Variable) -> Tuple[tf.Tensor, tf.Tensor, tf.Tensor]:
|
global_step: tf.Variable,
|
||||||
|
debug: bool) -> Tuple[tf.Tensor, tf.Tensor, tf.Tensor]:
|
||||||
"""
|
"""
|
||||||
Runs the encoder and decoder jointly for one step (one batch).
|
Runs the encoder and decoder jointly for one step (one batch).
|
||||||
|
|
||||||
@ -135,6 +140,7 @@ def _run_enc_dec_step_simple(encoder: model.Encoder, decoder: model.Decoder,
|
|||||||
decoder: instance of decoder model
|
decoder: instance of decoder model
|
||||||
inputs: inputs from data set
|
inputs: inputs from data set
|
||||||
global_step: the global step variable
|
global_step: the global step variable
|
||||||
|
debug: if True summaries are collected
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
tuple of reconstruction loss, reconstructed input, latent space value
|
tuple of reconstruction loss, reconstructed input, latent space value
|
||||||
@ -144,7 +150,7 @@ def _run_enc_dec_step_simple(encoder: model.Encoder, decoder: model.Decoder,
|
|||||||
|
|
||||||
reconstruction_loss = tf.losses.log_loss(inputs, x_decoded)
|
reconstruction_loss = tf.losses.log_loss(inputs, x_decoded)
|
||||||
|
|
||||||
if int(global_step % train.LOG_FREQUENCY) == 0:
|
if int(global_step % train.LOG_FREQUENCY) == 0 and debug:
|
||||||
summary_ops_v2.scalar(name='reconstruction_loss', tensor=reconstruction_loss,
|
summary_ops_v2.scalar(name='reconstruction_loss', tensor=reconstruction_loss,
|
||||||
step=global_step)
|
step=global_step)
|
||||||
|
|
||||||
|
|||||||
@ -52,7 +52,8 @@ def train_simple(dataset: tf.data.Dataset,
|
|||||||
lr: float = 0.002,
|
lr: float = 0.002,
|
||||||
train_epoch: int = 80,
|
train_epoch: int = 80,
|
||||||
batch_size: int = 128,
|
batch_size: int = 128,
|
||||||
verbose: bool = True) -> None:
|
verbose: bool = False,
|
||||||
|
debug: bool = False) -> None:
|
||||||
"""
|
"""
|
||||||
Trains auto-encoder for given data set.
|
Trains auto-encoder for given data set.
|
||||||
|
|
||||||
@ -72,7 +73,8 @@ def train_simple(dataset: tf.data.Dataset,
|
|||||||
lr: initial learning rate (default: 0.002)
|
lr: initial learning rate (default: 0.002)
|
||||||
train_epoch: number of epochs to train (default: 80)
|
train_epoch: number of epochs to train (default: 80)
|
||||||
batch_size: size of each batch (default: 128)
|
batch_size: size of each batch (default: 128)
|
||||||
verbose: if True prints train progress info to console (default: True)
|
verbose: if True training progress is printed to console (default: False)
|
||||||
|
debug: if True summaries are collected (default: False)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# checkpointed tensors and variables
|
# checkpointed tensors and variables
|
||||||
@ -115,6 +117,7 @@ def train_simple(dataset: tf.data.Dataset,
|
|||||||
_epoch = epoch + previous_epochs
|
_epoch = epoch + previous_epochs
|
||||||
outputs = _train_one_epoch_simple(_epoch, dataset,
|
outputs = _train_one_epoch_simple(_epoch, dataset,
|
||||||
verbose=verbose,
|
verbose=verbose,
|
||||||
|
debug=debug,
|
||||||
batch_size=batch_size,
|
batch_size=batch_size,
|
||||||
**checkpointables)
|
**checkpointables)
|
||||||
|
|
||||||
@ -138,6 +141,7 @@ def train_simple(dataset: tf.data.Dataset,
|
|||||||
def _train_one_epoch_simple(epoch: int,
|
def _train_one_epoch_simple(epoch: int,
|
||||||
dataset: tf.data.Dataset,
|
dataset: tf.data.Dataset,
|
||||||
verbose: bool,
|
verbose: bool,
|
||||||
|
debug: bool,
|
||||||
batch_size: int,
|
batch_size: int,
|
||||||
learning_rate_var: tf.Variable,
|
learning_rate_var: tf.Variable,
|
||||||
decoder: model.Decoder,
|
decoder: model.Decoder,
|
||||||
@ -166,10 +170,11 @@ def _train_one_epoch_simple(epoch: int,
|
|||||||
optimizer=enc_dec_optimizer,
|
optimizer=enc_dec_optimizer,
|
||||||
inputs=x,
|
inputs=x,
|
||||||
global_step_enc_dec=global_step_enc_dec,
|
global_step_enc_dec=global_step_enc_dec,
|
||||||
global_step=global_step)
|
global_step=global_step,
|
||||||
|
debug=debug)
|
||||||
enc_dec_loss_avg(reconstruction_loss)
|
enc_dec_loss_avg(reconstruction_loss)
|
||||||
|
|
||||||
if int(global_step % LOG_FREQUENCY) == 0 and verbose:
|
if int(global_step % LOG_FREQUENCY) == 0 and debug:
|
||||||
comparison = K.concatenate([x[:int(batch_size / 2)], x_decoded[:int(batch_size / 2)],
|
comparison = K.concatenate([x[:int(batch_size / 2)], x_decoded[:int(batch_size / 2)],
|
||||||
z[:int(batch_size/2)]], axis=0)
|
z[:int(batch_size/2)]], axis=0)
|
||||||
grid = util.prepare_image(comparison.cpu(), nrow=int(batch_size/2))
|
grid = util.prepare_image(comparison.cpu(), nrow=int(batch_size/2))
|
||||||
@ -194,17 +199,22 @@ def _train_enc_dec_step_simple(encoder: model.Encoder, decoder: model.Decoder,
|
|||||||
optimizer: tf.train.Optimizer,
|
optimizer: tf.train.Optimizer,
|
||||||
inputs: tf.Tensor,
|
inputs: tf.Tensor,
|
||||||
global_step: tf.Variable,
|
global_step: tf.Variable,
|
||||||
global_step_enc_dec: tf.Variable) -> Tuple[tf.Tensor, tf.Tensor, tf.Tensor]:
|
global_step_enc_dec: tf.Variable,
|
||||||
|
debug: bool) -> Tuple[tf.Tensor, tf.Tensor, tf.Tensor]:
|
||||||
"""
|
"""
|
||||||
Trains the encoder and decoder jointly for one step (one batch).
|
Trains the encoder and decoder jointly for one step (one batch).
|
||||||
|
|
||||||
:param encoder: instance of encoder model
|
Args:
|
||||||
:param decoder: instance of decoder model
|
encoder: instance of encoder model
|
||||||
:param optimizer: instance of chosen optimizer
|
decoder: instance of decoder model
|
||||||
:param inputs: inputs from data set
|
optimizer: instance of chosen optimizer
|
||||||
:param global_step: the global step variable
|
inputs: inputs from data set
|
||||||
:param global_step_enc_dec: global step variable for enc_dec
|
global_step: the global step variable
|
||||||
:return: tuple of reconstruction loss, reconstructed input, z value
|
global_step_enc_dec: global step variable for enc_dec
|
||||||
|
debug: if True summaries are collected
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
tuple of reconstruction loss, reconstructed input, z value
|
||||||
"""
|
"""
|
||||||
with tf.GradientTape() as tape:
|
with tf.GradientTape() as tape:
|
||||||
z = encoder(inputs)
|
z = encoder(inputs)
|
||||||
@ -214,7 +224,7 @@ def _train_enc_dec_step_simple(encoder: model.Encoder, decoder: model.Decoder,
|
|||||||
|
|
||||||
enc_dec_grads = tape.gradient(reconstruction_loss,
|
enc_dec_grads = tape.gradient(reconstruction_loss,
|
||||||
encoder.trainable_variables + decoder.trainable_variables)
|
encoder.trainable_variables + decoder.trainable_variables)
|
||||||
if int(global_step % LOG_FREQUENCY) == 0:
|
if int(global_step % LOG_FREQUENCY) == 0 and debug:
|
||||||
summary_ops_v2.scalar(name='reconstruction_loss', tensor=reconstruction_loss,
|
summary_ops_v2.scalar(name='reconstruction_loss', tensor=reconstruction_loss,
|
||||||
step=global_step)
|
step=global_step)
|
||||||
for grad, variable in zip(enc_dec_grads, encoder.trainable_variables + decoder.trainable_variables):
|
for grad, variable in zip(enc_dec_grads, encoder.trainable_variables + decoder.trainable_variables):
|
||||||
|
|||||||
@ -32,6 +32,7 @@ def main() -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument("--verbose", default=False, action="store_true", help="provide to get extra output")
|
parser.add_argument("--verbose", default=False, action="store_true", help="provide to get extra output")
|
||||||
|
parser.add_argument("--debug", default=False, action="store_true", help="provide to collect tensorboard summaries")
|
||||||
parser.add_argument('--version', action='version', version='2martens Masterthesis 0.1.0')
|
parser.add_argument('--version', action='version', version='2martens Masterthesis 0.1.0')
|
||||||
sub_parsers = parser.add_subparsers(dest="action")
|
sub_parsers = parser.add_subparsers(dest="action")
|
||||||
sub_parsers.required = True
|
sub_parsers.required = True
|
||||||
@ -128,7 +129,7 @@ def _use(args: argparse.Namespace) -> None:
|
|||||||
f"{args.summary_path}/use/category-{category}/{args.iteration}"
|
f"{args.summary_path}/use/category-{category}/{args.iteration}"
|
||||||
)
|
)
|
||||||
with use_summary_writer.as_default():
|
with use_summary_writer.as_default():
|
||||||
run.run_simple(coco_data, iteration=args.iteration_trained,
|
run.run_simple(coco_data, iteration=args.iteration_trained, debug=args.debug,
|
||||||
weights_prefix=f"{args.weights_path}/category-{category_trained}",
|
weights_prefix=f"{args.weights_path}/category-{category_trained}",
|
||||||
zsize=64, verbose=args.verbose, channels=3, batch_size=batch_size)
|
zsize=64, verbose=args.verbose, channels=3, batch_size=batch_size)
|
||||||
|
|
||||||
@ -151,7 +152,7 @@ def _auto_encoder_train(args: argparse.Namespace) -> None:
|
|||||||
with train_summary_writer.as_default():
|
with train_summary_writer.as_default():
|
||||||
train.train_simple(coco_data, iteration=args.iteration,
|
train.train_simple(coco_data, iteration=args.iteration,
|
||||||
weights_prefix=f"{args.weights_path}/category-{category}",
|
weights_prefix=f"{args.weights_path}/category-{category}",
|
||||||
zsize=64, lr=0.0001, verbose=args.verbose,
|
zsize=64, lr=0.0001, verbose=args.verbose, debug=args.debug,
|
||||||
channels=3, train_epoch=args.num_epochs, batch_size=batch_size)
|
channels=3, train_epoch=args.num_epochs, batch_size=batch_size)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user