Python 如何从TensorFlow中tf.WholeFileReader.read()返回的文件名中提取类标签?

Python 如何从TensorFlow中tf.WholeFileReader.read()返回的文件名中提取类标签?,python,tensorflow,Python,Tensorflow,我在子目录中组织了一组图像,它们对应于图像的类标签。例如 images/1/0000001.jpg,images/1/0000002.jpg。。。对于第1类图像 images/2/0123456.jpg,images/2/0123457.jpg。。。对于第2类图像 现在,我想知道如何在使用tf.WholeFileReader()时获得整数类标签,该函数具有read方法,该方法将文件名作为张量。在图的外部,我可以简单地执行int('images/2/0123457.jpg'.split('/'

我在子目录中组织了一组图像,它们对应于图像的类标签。例如

  • images/1/0000001.jpg,images/1/0000002.jpg。。。对于第1类图像
  • images/2/0123456.jpg,images/2/0123457.jpg。。。对于第2类图像
现在,我想知道如何在使用
tf.WholeFileReader()
时获得整数类标签,该函数具有
read
方法,该方法将文件名作为张量。在图的外部,我可以简单地执行
int('images/2/0123457.jpg'.split('/')[1])
来获取整数标签,但是如何在图的内部执行,以便使用标签进行模型训练?下面是一个简单的例子,我基本上是在寻找
class_label=…#的解决方案在下面的示例中,从文件\u name
获取类标签:

将tensorflow导入为tf
g=tf.Graph()
使用g.as_default():
filename\u queue=tf.train.string\u input\u producer(
tf.train.match_filename_once('images/*/*.jpg'))
image\u reader=tf.WholeFileReader()
文件名,image\u raw=image\u reader.read(文件名\u队列)
file\u name=tf.identity(file\u name,name='file\u name')
image=tf.image.decode\u jpeg(image\u raw,name='image')
类别标签=…#从文件名获取类标签
将tf.Session(graph=g)作为sess:
sess.run(tf.local\u variables\u initializer())
coord=tf.train.Coordinator()
线程=tf.train.start\u queue\u runner(coord=coord)
image\u tensor=sess.run('image:0')
打印('Image shape:',Image_tensor.shape)
file\u name=sess.run('file\u name:0')
打印('文件名:',文件名)
协调请求停止()
坐标连接(线程)

刚刚用
tf.split\u string
tf.string\u to\u number
找到了我问题的解决方案:

class_label = tf.string_split([file_name], '/').values[1]
class_label = tf.string_to_number(class_label, tf.int32)

刚刚用
tf.split\u string
tf.string\u to\u number
找到了我问题的解决方案:

class_label = tf.string_split([file_name], '/').values[1]
class_label = tf.string_to_number(class_label, tf.int32)