- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
目前,我尝试使用 Tensorflow 的新 Estimator API 在自定义图像数据集上训练自动编码器。
到目前为止一切正常。我唯一的问题是当模型处于评估模式时将输入和输出图像保存为摘要。我在训练模式下创建的所有图像摘要都会正确存储并显示在 Tensorboard 中。
这是我的代码:
def model_fn_autoencoder(features, labels, mode, params):
is_training = mode == ModeKeys.TRAIN
# Define model's architecture
logits = architecture_autoencoder(features, is_training=is_training)
# Loss, training and eval operations are not needed during inference.
loss = None
train_op = None
#eval_metric_ops = {}
if mode != ModeKeys.INFER:
loss = tf.reduce_mean(tf.square(logits - features))
train_op = get_train_op_fn(loss, params)
#eval_metric_ops = get_eval_metric_ops(labels, predictions)
if mode == ModeKeys.TRAIN:
for i in range(10):
tf.summary.image("Input/Train/" + str(i), tf.reshape(features[i],[1, 150, 150, 3]))
tf.summary.image("Output/Train/" + str(i), tf.reshape(logits[i],[1, 150, 150, 3]))
if mode == ModeKeys.EVAL:
for i in range(10):
tf.summary.image("Input/Eval/" + str(i), tf.reshape(features[i], [1, 150, 150, 3]))
tf.summary.image("Output/Eval/" + str(i), tf.reshape(logits[i], [1, 150, 150, 3]))
return tf.estimator.EstimatorSpec(
mode=mode,
predictions=logits,
loss=loss,
train_op=train_op,
#eval_metric_ops=eval_metric_ops
也许有人可以告诉我我做错了什么?
更新以下是估算器和实验创建的函数:
估算器:
def get_estimator(run_config, params):
return tf.estimator.Estimator(
model_fn=model_fn_autoencoder, # First-class function
params=params, # HParams
config=run_config # RunConfig
)
实验:
def experiment_fn(run_config, params):
run_config = run_config.replace(save_checkpoints_steps=params.min_eval_frequency)
estimator = get_estimator(run_config, params)
tf_path = 'path/to/tfrecord'
train_file = 'Crops-Faces-Negtives-150-150.tfrecord'
val_file = 'Crops-Faces-Negtives-150-150-TEST.tfrecord'
tfrecords_train = [os.path.join(tf_path, train_file)]
tfrecords_test = [os.path.join(tf_path, val_file)]
# Setup data loaders
train_input_fn = get_train_inputs(batch_size=128, tfrecord_files=tfrecords_train)
eval_input_fn = get_train_inputs(batch_size=128, tfrecord_files=tfrecords_test)
# Define the experiment
experiment = tf.contrib.learn.Experiment(
estimator=estimator, # Estimator
train_input_fn=train_input_fn, # First-class function
eval_input_fn=eval_input_fn, # First-class function
train_steps=params.train_steps, # Minibatch steps
min_eval_frequency=params.min_eval_frequency, # Eval frequency
eval_steps=10 # Number of eval batches
)
return experiment
最佳答案
使用 TF1.4,您可以通过 tf.estimator.EstimatorSpec evaluation_hooks。evaluation_hooks 是一个 Hook 列表,您必须向其中添加以下 Hook :
# Create a SummarySaverHook
eval_summary_hook = tf.train.SummarySaverHook(
save_steps=1,
output_dir= self.job_dir + "/eval_core",
summary_op=tf.summary.merge_all())
# Add it to the evaluation_hook list
evaluation_hooks.append(eval_summary_hook)
#Now, return the estimator:
return tf.estimator.EstimatorSpec(
mode=mode,
predictions=predictions,
loss=loss,
train_op=train_op,
training_hooks=training_hooks,
eval_metric_ops=eval_metric_ops,
evaluation_hooks=evaluation_hooks)
现在您可以简单地添加 tf.summary.image 并将其放入 Tensorboard 中。使用您在 eval_summary Hook 中使用的指定输出目录的父目录上打开 Tensrobaord。在我的示例中,它被称为“eval_core”,因此我在其父目录中打开了 Tensorboard,如下图所示,它很好地显示在一个蓝色框中。
关于python - Tensorflow Estimator API 在评估模式下保存图像摘要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46852018/
几个月前,我使用了tf.contrib.learn.DNNRegressor来自 TensorFlow 的 API,我发现它使用起来非常方便。最近几个月我没有跟上TensorFlow的发展。现在我有一
我们正在尝试将旧的训练代码转换为更符合 tf.estimator.Estimator 的代码。在初始代码中,我们针对目标数据集微调原始模型。在使用 variables_to_restore 和 ini
我目前运行的是 TensorFlow 1.9.0。我的自定义估算器是使用 tf.estimator.Estimator 创建的,并且运行时没有出现任何故障。但是,我在 model_dir 下没有找到任
我刚刚用 tensorflow 训练了一个 CNN 来识别太阳黑子。我的模型与 this 几乎相同.问题是我无法在任何地方找到关于如何使用训练阶段生成的检查点进行预测的明确解释。 尝试使用标准恢复方法
我正在尝试使用我自己的数据集和类对在 imagenet 上预训练的 Inception-resnet v2 模型进行迁移学习。我的原始代码库是对 tf.slim 的修改我再也找不到的示例,现在我正在尝
在 train(...) 完成后,如何从 tf.estimator.Estimator 获取最后一个 global_step ?例如,典型的基于估算器的训练例程可能如下设置: n_epochs = 1
一年多来我一直在使用自己的 Estimator/Experiment 之类的代码,但我最终想加入 Dataset+Estimator 的行列。 我想做如下的事情: for _ in range(N):
我正在考虑将我的代码库移动到 tf.estimator.Estimator ,但我找不到如何将它与张量板摘要结合使用的示例。 MWE: import numpy as np import tensor
我的印象是在 tf.estimator.Estimator 实例上调用 evaluate() 不会在多个 GPU 上运行模型,即使分配策略是 MirroredStrategy,配置为至少使用 2 个
我遇到了一些小问题,但我不知道如何处理。 当我使用 tf.estimator.Estimator 时,它会在每个步骤中记录两行,例如: INFO:tensorflow:global_step/sec:
在此tutorial ,他们通过为 tf.nn.softmax 节点命名成功地记录了 softmax 函数。 tf.nn.softmax(logits, name="softmax_tensor")
我发现 tensorflow train_and_evaluate 的工作方式与传统的 tf.estimator train 和 evaluate 相比有点不同。train_and_evaluate
我正在使用 tensorflow 版本 2.0.0-beta1。打电话时 tf.estimator.inputs.pandas_input_fn 它给了我一个错误。 module 'tensorflo
有没有办法在另一个模型 B 中使用经过 tf.estimator 训练的模型 A? 这是情况,假设我有一个训练有素的“模型 A”和 model_a_fn()。“模型 A”获取图像作为输入,并输出一些类
我正在尝试在本地运行对象检测 API。 我相信我已经按照 TensorFlow Object Detection API 中的描述设置了所有内容。但是,当我尝试运行 model_main.py 时,会
请原谅我的编码经验。我正在尝试使用 GridSearch 进行一系列回归。我正在尝试循环整个过程以使过程更快,但我的代码不够好并且不介意提高效率。这是我的代码: classifiers=[Lasso(
我在将纯 Keras 模型转换为不平衡数据集上的 TensorFlow Estimator API 时遇到了一些麻烦。 使用纯 Keras API 时,class_weight 参数在 model.f
当发生上述错误时,我经常使用有关估计器的tensorflow官方教程,而它在google.colab中正常运行。 我使用的环境是win10-64bit&tensorflow-gpu==1.12.0&p
Closed. This question is opinion-based。它当前不接受答案。 想要改善这个问题吗?更新问题,以便editing this post用事实和引用来回答。 已关闭6年。
Closed. This question is opinion-based。它当前不接受答案。 想要改善这个问题吗?更新问题,以便editing this post用事实和引用来回答。 1年前关闭。
我是一名优秀的程序员,十分优秀!