gpt4 book ai didi

python - TensorFlow使用dataset代替函数feed_dict

转载 作者:行者123 更新时间:2023-12-01 08:54:49 26 4
gpt4 key购买 nike

当我学习tensorflow项目时,发现一行代码:

cls_prob, box_pred = sess.run([output_cls_prob, output_box_pred], feed_dict={input_img: blob})

但是,这行代码花了很多时间。 (使用CPU需要15秒...┭┮﹏┭┮)

通过查阅资料,我发现使用“dataset”函数可以解决这个花了很多时间的问题,我应该如何使用它?

“ Blob ”的来源:

img = cv2.imread('./imgs/001.jpg')
img_scale = float(600) / min(img_data.shape[0], img_data.shape[1])
if np.round(img_scale * max(img_data.shape[0], img_data.shape[1])) > 1200:
img_scale = float(1200) / max(img_data.shape[0], img_data.shape[1])
img_data = cv2.resize(img_data, None, None, fx=img_scale, fy=img_scale, interpolation=cv2.INTER_LINEAR)
img_orig = img_data.astype(np.float32, copy=True)
blob = np.zeros((1, img_data.shape[0], img_data.shape[1], 3),dtype=np.float32)
blob[0, 0:img_data.shape[0], 0:img_data.shape[1], :] = img_orig

“output_cls_prob”&“output_box_pred”&“input_img”的来源:

# Actually,read PB model...
input_img = sess.graph.get_tensor_by_name('Placeholder:0')
output_cls_prob = sess.graph.get_tensor_by_name('Reshape_2:0')
output_box_pred = sess.graph.get_tensor_by_name('rpn_bbox_pred/Reshape_1:0')

参数类型:

blob:type 'numpy.ndarray'

output_cls_prob:class 'tensorflow.python.framework.ops.Tensor'

output_box_pred:class 'tensorflow.python.framework.ops.Tensor'

input_img:class 'tensorflow.python.framework.ops.Tensor'

最佳答案

tf.data 是 tensorflow 输入管道的推荐 API。这是关于 tensorflow.org 的教程。对于您的示例,部分 "Decoding image data and resizing it"可能是最有用的。例如,您可以执行以下操作:

# Reads an image from a file, decodes it into a dense tensor, and resizes it
# to a fixed shape.
def _parse_function(filename):
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_jpeg(image_string)
image_resized = tf.image.resize_images(image_decoded, [new_width, new_height])
image_resized = tf.expand_dims(image_resized, 0) # Adds size 1 dimension
return image_resized

# A vector of filenames.
filenames = tf.constant(["./imgs/001.jpg", ...])

dataset = tf.data.Dataset.from_tensor_slices(filenames)
dataset = dataset.map(_parse_function)

不要让 input_img 成为占位符,而是更改:

input_img = tf.placeholder(tf.float32)
output_class_prob, output_class_pred = (... use input_img ...)

至:

iterator = dataset.make_one_shot_iterator()
input_img = iterator.get_next()
output_class_prob, output_class_pred = (... use input_img ...)

关于python - TensorFlow使用dataset代替函数feed_dict,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52848973/

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