gpt4 book ai didi

tensorflow - 无法在 tensorflow 官方 resnet 模型中加载图像以进行评估

转载 作者:行者123 更新时间:2023-12-03 09:50:31 24 4
gpt4 key购买 nike

我正在尝试在我自己的图像和标签上训练 Tensorflow 官方 resnet 模型 ( link )。
我创建了 imagenet_main.py 的副本( my_data_main.py ) 在那里我像这样更改了与数据集相关的硬编码值(我现在只是想让它处理很少的图像):

"""Runs a ResNet model on the ImageNet dataset."""                             

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import sys

import tensorflow as tf # pylint: disable=g-bad-import-order

from official.resnet import imagenet_preprocessing
from official.resnet import resnet_model
from official.resnet import resnet_run_loop

_DEFAULT_IMAGE_SIZE = 299
_NUM_CHANNELS = 3
_NUM_CLASSES = 9

# TODO: generate dynamically:
_NUM_IMAGES = {
'train': 484,
'validation': 121,
}

_NUM_TRAIN_FILES = 2
_NUM_VAL_FILES = 2
_SHUFFLE_BUFFER = 200

###############################################################################
# Data processing
###############################################################################
def get_filenames(is_training, data_dir):
"""Return filenames for dataset."""
if is_training:
return [
os.path.join(data_dir, 'my_data_train_%05d-of-%05d.tfrecord' % (i, _NUM_TRAIN_FILES))
for i in range(_NUM_TRAIN_FILES)]
else:
return [
os.path.join(data_dir, 'my_data_validation_%05d-of-%05d.tfrecord' % (i, _NUM_VAL_FILES))
for i in range(_NUM_VAL_FILES)]

# rest of program unchanged

为了加载我的数据,我为 train 和 eval 创建了 TFRecords,并将其添加到 data_dir 目录 ~/Projects/my_data/data/images/ .
然后我启动程序:
python3 my_data_main.py \
--data_dir ~/Projects/my_data/data/images/ \
--model_dir /tmp/tests \
--export_dir /tmp/exports \
--train_epochs 10 \
--max_train_steps 200 \
--epochs_between_evals 1 \
--batch_size 256 \
--multi_gpu \
--hooks LoggingTensorHook \
--num_parallel_calls 12 \
--inter_op_parallelism_threads 0 \
--intra_op_parallelism_threads 0 \
--dtype fp32 \
--export_dir /tmp/resnet \
--version 1 \
--resnet_size 18

问题 :图像已正确加载用于训练,但未正确加载用于评估。 resnet_run_loop.py 中的以下行开头 def resnet_model_fn在 Tensorboard 中加载图像:
  # Generate a summary node for the images                                      
tf.summary.image('images', features, max_outputs=6)

我可以看到我的火车运行图像
enter image description here
但不适用于 Tensorboard 中的 eval
enter image description here

我查了什么 :
我检查了我的 TFRecords 是否已成功读取。
我查看了 estimator.py并从 _get_features_and_labels_from_input_fn 打印张量形状(在 _evaluate_model 中调用)。我找不到任何错误。

我还没有做什么 :
我目前正在下载完整的 imagenet 数据,试图找出他们准备数据的方式的不同之处。

在写这篇文章之前,我尽力在网上找到答案。珍惜每个人的时间。

最佳答案

这是@robieta on the github thread 的回答.在此处复制答案以供将来引用:

I will admit this one threw me. It looks like this is an active feature request in TensorFlow. (https://github.com/tensorflow/tensorflow/issues/15332) It is possible to hack it in using hooks. (Tensorflow Estimator API save image summary in eval mode) Just note martinwicke's warnings about what a summary is in eval. Here is a simple example:


import tensorflow as tf


def input_fn(*args, **kwargs):
batch_size = 20
images = tf.zeros((batch_size, 10), tf.float32)
labels = tf.zeros((batch_size, 1), tf.int32)
return tf.data.Dataset.from_tensors((images, labels)).repeat(10)

def model_fn(features, labels, mode, params):
pred = tf.layers.dense(inputs=features, units=1)

if mode == tf.estimator.ModeKeys.TRAIN:
loss = tf.losses.absolute_difference(labels, pred)
tf.summary.scalar(name="train_scalar", tensor=1)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
return tf.estimator.EstimatorSpec(
mode=tf.estimator.ModeKeys.TRAIN,
loss=loss,
train_op=optimizer.minimize(loss, tf.train.get_or_create_global_step()))
if mode == tf.estimator.ModeKeys.EVAL:
loss = tf.losses.absolute_difference(labels, pred)
tf.summary.scalar(name="eval_scalar", tensor=2)

# Use a hook to force evaluation. If commented out the scalar will not be saved.
eval_summary_hook = tf.train.SummarySaverHook(
save_steps=1,
output_dir= "/tmp/summary_test/eval_core",
summary_op=tf.summary.merge_all())

return tf.estimator.EstimatorSpec(
mode=tf.estimator.ModeKeys.EVAL,
loss=loss,
evaluation_hooks=[eval_summary_hook]
)


def main(_):
model_dir = "/tmp/summary_test"
if tf.gfile.Exists(model_dir):
tf.gfile.DeleteRecursively(model_dir)

run_config = tf.estimator.RunConfig(save_summary_steps=1)

estimator = tf.estimator.Estimator(
model_fn=model_fn,
model_dir=model_dir,
params={},
config=run_config
)

for _ in range(10):
estimator.train(input_fn=input_fn)
estimator.evaluate(input_fn=input_fn)

if __name__ == "__main__":
main([])

Hope this helps, and once evaluate has better support for summaries I will definitely look to save more evaluation info by default.

关于tensorflow - 无法在 tensorflow 官方 resnet 模型中加载图像以进行评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50143417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com