attributeError:'张量'对象没有属性' numpy'通过我的数据集映射功能

发布于 2025-01-27 22:51:04 字数 539 浏览 2 评论 0原文

我正在尝试将函数process_image映射到数据集。此功能调用另一个函数,get_label。在get_label中,我正在尝试从图像中检索标签的名称。

文件路径就是这样:c:\\ users \\ sis \\ desktop \\ test \\ 0002_c1s1_000451_03.jpg。该标签为编号0002

“在此处输入图像说明”

def get_lab(file_path):
    parts = tf.strings.split(file_path, os.path.sep)
    part=parts[-1].numpy().decode().split('_')[0]
    label=tf.strings.to_number(part)
    return label

I'm trying to map a function process_image to the dataset. This function calls another function, get_label. In get_label, I'm trying to retrieve the label's name from images.

The file path is like this: C:\\Users\\sis\\Desktop\\test\\0002_c1s1_000451_03.jpg. The label is number 0002.

enter image description here

def get_lab(file_path):
    parts = tf.strings.split(file_path, os.path.sep)
    part=parts[-1].numpy().decode().split('_')[0]
    label=tf.strings.to_number(part)
    return label

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘寄 2025-02-03 22:51:04

我解决了!我不明白错误的位置,我认为上一个代码混合的急切模式和图形模式,因此我更改了get_label函数的代码,并且可以使用!

def get_lab(file_path):

    parts = tf.strings.split(file_path, os.path.sep)[-1]
    part=tf.strings.split(parts, sep='_')[0]
    print(part)
    label=tf.strings.to_number(part)
    return label 

I solved it! I didn't understand exactly where the error was, I think the previous code mixed eager mode and graph mode, so I changed the code of get_label function and it worked!

def get_lab(file_path):

    parts = tf.strings.split(file_path, os.path.sep)[-1]
    part=tf.strings.split(parts, sep='_')[0]
    print(part)
    label=tf.strings.to_number(part)
    return label 
我不会写诗 2025-02-03 22:51:04

您可以应用于创建用于外部程序的足够的文件夹名称。

[示例]:

import os
import tensorflow as tf

def get_lab(file_path):
    parts = tf.strings.split(file_path, os.path.sep)
    part=parts[-2].numpy().decode().split('.')[0]
    label=tf.strings.to_number(part)
    return label.numpy()

directory = "F:\\datasets\\downloads\\Actors\\train\\Candidt Kibt\\01.tif\\"
print( 'label as number: ' + str(get_lab( directory )) )

directory = "F:\\datasets\\downloads\\Actors\\train\\"
print( 'classname: ' + str(tf.io.gfile.listdir(
    directory
))
)

[输出]:

label as number: 1.0
classname: ['Candidt Kibt', 'Pikaploy']

F:\temp\Python>

You may applied to folder name that create sufficeints used of external programs.

[ Sample ]:

import os
import tensorflow as tf

def get_lab(file_path):
    parts = tf.strings.split(file_path, os.path.sep)
    part=parts[-2].numpy().decode().split('.')[0]
    label=tf.strings.to_number(part)
    return label.numpy()

directory = "F:\\datasets\\downloads\\Actors\\train\\Candidt Kibt\\01.tif\\"
print( 'label as number: ' + str(get_lab( directory )) )

directory = "F:\\datasets\\downloads\\Actors\\train\\"
print( 'classname: ' + str(tf.io.gfile.listdir(
    directory
))
)

[ Output ]:

label as number: 1.0
classname: ['Candidt Kibt', 'Pikaploy']

F:\temp\Python>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文