Python tensorflow预训练模型的图像读取与预处理
我在Tensorflow方面没有太多经验。我试图使用一个预训练的ResNet152模型来获得最后一层的激活作为输出。我用于输入的图像存储在我的硬盘上。因此,我需要加载图像,对其进行预处理,然后从预训练模型中获得输出。我找到了使用图像URL的例子,但当我尝试使用图像路径时,我无法让它工作。这是我到目前为止得到的(目前只有一张图片): 我在Jupyter笔记本上做这件事。当我执行代码时,我没有收到错误消息,它一直在运行,直到我重新启动内核Python tensorflow预训练模型的图像读取与预处理,python,tensorflow,pre-trained-model,Python,Tensorflow,Pre Trained Model,我在Tensorflow方面没有太多经验。我试图使用一个预训练的ResNet152模型来获得最后一层的激活作为输出。我用于输入的图像存储在我的硬盘上。因此,我需要加载图像,对其进行预处理,然后从预训练模型中获得输出。我找到了使用图像URL的例子,但当我尝试使用图像路径时,我无法让它工作。这是我到目前为止得到的(目前只有一张图片): 我在Jupyter笔记本上做这件事。当我执行代码时,我没有收到错误消息,它一直在运行,直到我重新启动内核 知道我做错了什么吗?如何处理多个图像?我通过将tf.Whol
知道我做错了什么吗?如何处理多个图像?我通过将
tf.WholeFileReader()
替换为tf.read\u file()
找到了解决方案:
with tf.Graph().as_default():
filename_queue = tf.train.string_input_producer(['./testimg/A_008.jpg'])
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
image = tf.image.decode_jpeg(value, channels=3)
preprocessing = preprocessing_factory.get_preprocessing('resnet_v2_152', is_training=False)
processed_image = preprocessing(image, 299,299)
processed_images = tf.expand_dims(processed_image, 0)
with slim.arg_scope(resnet_v2.resnet_arg_scope()):
logits, end_points = resnet_v2.resnet_v2_152(processed_images, is_training=False)
checkpoints_dir='./models/resnet_v2_152'
init_fn = slim.assign_from_checkpoint_fn(
os.path.join(checkpoints_dir, 'resnet_v2_152.ckpt'),
slim.get_variables_to_restore())
with tf.Session() as sess:
init_fn(sess)
np_image, fv = sess.run([image, logits])
graph = tf.Graph()
with graph.as_default():
image_path = image = tf.placeholder(tf.string)
image = tf.image.decode_jpeg(tf.read_file(image_path), channels=3)
preprocessing = preprocessing_factory.get_preprocessing('resnet_v2_152', is_training=False)
processed_image = preprocessing(image, image_size, image_size)
processed_images = tf.expand_dims(processed_image, 0)
with slim.arg_scope(resnet_v2.resnet_arg_scope()):
logits, end_points = resnet_v2.resnet_v2_152(processed_images, is_training=False)
checkpoints_dir='./models/resnet_v2_152'
init_fn = slim.assign_from_checkpoint_fn(
os.path.join(checkpoints_dir, 'resnet_v2_152.ckpt'),
slim.get_variables_to_restore())
images = ['./testimg/A_008.jpg', './testimg/logo.jpg']
with tf.Session(graph=graph) as sess:
init_fn(sess)
for img in images:
fv = sess.run(logits, feed_dict={image_path: img})
print(fv)