ValueError:无法将字符串转换为float:'nonPdr' [英] ValueError: could not convert string to float: 'nonPdr'

查看:124
本文介绍了ValueError:无法将字符串转换为float:'nonPdr'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到此错误:ValueError:当我运行此代码时,无法将字符串转换为float:'nonPdr':

I'm facing this error: ValueError: could not convert string to float: 'nonPdr', when I run this code:

model = Sequential()
model.add(Conv2D(input_shape=(605,700,3), filters=64, kernel_size=(3,3), padding="valid",activation="tanh"))
model.add(Flatten())
model.add(Dense(32, activation='tanh', input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])

data, labels = ReadImages(TRAIN_DIR)

# Train the model, iterating on the data in batches of 32 samples
model.fit(np.array(data), np.array(labels), epochs=10, batch_size=32)

详细信息:"nonPdr"是我的2个img类之一

Detail: 'nonPdr' is one of my 2 img classes

更新我的readImg方法

UPDATE My readImg method

def ReadImages(Path):
    ImageList = list()
    LabelList = list()
    ImageCV = list()

    # Get all subdirectories
    FolderList = os.listdir(Path)

    # Loop over each directory
    for File in FolderList:
        if(os.path.isdir(os.path.join(Path, File))):
            for Image in os.listdir(os.path.join(Path, File)):
                # Add the image path to the list
                ImageList.append(os.path.join(Path, File) + os.path.sep + Image)
                # Convert the path into a file
                ImageCV.append(cv2.imread(os.path.join(Path, File) + os.path.sep + Image))    
                # Add a label for each image and remove the file extension
                LabelList.append(os.path.splitext(File)[0])
        else:
            ImageList.append(os.path.join(Path, File))

            ImageCV.append(cv2.imread(os.path.join(Path, File) + os.path.sep + Image))    
            # Add a label for each image and remove the file extension
            LabelList.append(os.path.splitext(File)[0])

    return ImageCV, LabelList

推荐答案

在ReadImages函数中,您正在创建字符串列表:

In your ReadImages function, you are making list of strings:

LabelList.append(os.path.splitext(File)[0])

稍后,当您使用ReadImages函数时,您尝试将此字符串列表转换为numpy数组.在这里:

And later when you use ReadImages function, you are trying to convert this list of strings into numpy array. Here:

data, labels = ReadImages(TRAIN_DIR)
model.fit(np.array(data), np.array(labels), epochs=10, batch_size=32)

可能的解决方案可能是将您的班级名称分配给数字:

Possible solution might be assigning your class names to numbers:

classes = ["nonPdr", "another_class"]
LabelList.append(classes.index[os.path.splitext(File)[0]])

当您的类为"nonPdr"时,0将附加到LabelList,如果您的类是"other_class",则将附加1.

0 will be appended to LabelList when your class is "nonPdr", and 1 will be append if your class is "other_class".

这篇关于ValueError:无法将字符串转换为float:'nonPdr'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆