gpt4 book ai didi

python - 有了 tensorflow ,我就有了图像信息的字典。如何训练这些图像中的特征?

转载 作者:行者123 更新时间:2023-11-30 09:50:46 25 4
gpt4 key购买 nike

我正在使用每个文件夹名称作为标签从目录中读取图像。图像列表中的每个元素都包含一个如下所示的字典:

{'image': array([[[152, 117,  89],
[150, 115, 87],
...etc]], dtype=uint8),
'label': 'dog',
'path': 'images/dog/20311lpr.jpg'}

我想训练 tensorflow 以了解每个标签中所有图像之间的相似性,以便稍后我可以使用另一个脚本根据此数据检查图像,并告诉我新图像应该具有什么标签。

接下来我应该学习/阅读什么?

我有很多这样的图像:

$ find images -type f
images/cat/images.jpeg
images/cat/images-1.jpeg
images/cat/Unknown.jpeg
...etc
images/dog/Common-dog-behaviors-explained.jpg
images/dog/hungry-spaniel-eats-from-bowlimage.gif
images/dog/image02.PNG
images/dog/20311lpr.jpg
...etc

到目前为止,我的脚本如下所示:

#!/usr/bin/env python

import argparse, os
from pprint import pprint
import tensorflow as tf

os.environ['TF_CPP_MIN_LOG_LEVEL']='2' # Don't show warnings

parser = argparse.ArgumentParser(description="Options")
parser.add_argument('imagedir', default='./images', type=str)
args = parser.parse_args()

def main():
images_w_labels = get_all_images(args.imagedir)
pprint(images_w_labels[-1]) # Print the last one

def _process_image(filename):
if filename.lower().endswith('png'):
imgdata = tf.image.decode_png(tf.read_file(filename))
elif filename.lower().endswith('gif'):
imgdata = tf.image.decode_gif(tf.read_file(filename))
else:
imgdata = tf.image.decode_jpeg(tf.read_file(filename))
sess = tf.Session()
imgdata = sess.run(imgdata)
return imgdata

def get_all_images(imagedir):
images = []
for root, subdir, files in os.walk(imagedir):
if len(files) == 0: continue
for file in files:
images.append({
'path': os.path.join(root,file),
'label': os.path.split(root)[-1],
'image': _process_image(os.path.join(root,file))
})
return images

if __name__ == '__main__':
main()

到目前为止,该脚本的输出如下:

$ ./build_image.data.py images
{'image': array([[[152, 117, 89],
[150, 115, 87],
[149, 114, 86],
...,
[143, 104, 61],
[143, 104, 61],
[144, 105, 62]],

[[151, 117, 89],
[150, 116, 88],
[149, 115, 87],
...,
[142, 103, 60],
[142, 103, 60],
[143, 104, 61]],

[[149, 118, 89],
[148, 117, 88],
[147, 116, 87],
...,
[142, 103, 62],
[142, 103, 62],
[142, 103, 62]],

...,
[[163, 136, 106],
[164, 137, 107],
[164, 137, 108],
...,
[ 9, 13, 12],
[ 10, 14, 13],
[ 10, 14, 13]],

[[162, 137, 106],
[162, 137, 106],
[162, 137, 107],
...,
[ 12, 14, 13],
[ 13, 15, 14],
[ 13, 15, 14]],

[[161, 136, 105],
[161, 136, 105],
[162, 137, 107],
...,
[ 13, 15, 14],
[ 13, 15, 14],
[ 14, 16, 15]]], dtype=uint8),
'label': 'dog',
'path': 'images/dog/20311lpr.jpg'}

最佳答案

您可以使用 feed_dict 将 numpy 数组提供给 tensorflow 占位符,该 feed_dict 是 session.runtensor.eval 的参数。不过,要做到这一点,您需要一个模型。

关于python - 有了 tensorflow ,我就有了图像信息的字典。如何训练这些图像中的特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45528809/

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