训练模型时出现错误,提示“无法将字符串转换为浮点数”

时间:2019-04-14 09:35:26

标签: python deep-learning

我正在尝试使用保存在某些目录中的图像来训练模型。我正在训练CNN,它将对三种类型的皮肤癌进行分类。我为三个类别制作了三个文件夹,并按类别调整了大小并还原了这些文件夹上的图像。然后我从这些文件夹中读取图像并尝试训练CNN。

我的代码是

pathname = os.path.dirname(sys.argv[0])

train_melanoma_dir=r'{}\Melanoma_resize'.format(str(pathname))
train_sebborhic_dir= r'{}\Sebborhic_resize'.format(str(pathname))
train_nevus_dir=r'{}\Nevus_resize'.format(str(pathname))

train_melanoma=[r'C:\Users\renukasi\Documents\College_Research_Work\Skin_Cancer_Classification\Melanoma_resize\{}'.format(i) for i in os.listdir(train_melanoma_dir)]
train_sebborhic=[r'C:\Users\renukasi\Documents\College_Research_Work\Skin_Cancer_Classification\Sebborhic_resize\{}'.format(i) for i in os.listdir(train_sebborhic_dir)]
train_nevus=[r'C:\Users\renukasi\Documents\College_Research_Work\Skin_Cancer_Classification\Nevus_resize\{}'.format(i) for i in os.listdir(train_nevus_dir)]

train_melanoma_label =[]
train_sebborhic_label = []
train_nevus_label =[]
for i in range(0,len(train_melanoma)):
    train_melanoma_label.append(0)

for i in range(0,len(train_sebborhic)):
    train_sebborhic_label.append(1)

for i in range(0,len(train_nevus)):
    train_nevus_label.append(2)

train_images = []
train_images = train_melanoma+train_sebborhic+train_nevus
train_label=train_melanoma_label+train_sebborhic_label+train_nevus_label
X=[]    # list of shuffled train images
Y=[]    # list of shuffled_images labels
index_of_images=list(range(len(train_images)))
shuffle(index_of_images)
for i in index_of_images:
    X.append(train_images[i])
    Y.append(train_label[i])

final_train_images=X    
final_train_images=np.array(final_train_images)
Y=np.array(Y)

# Split the data into train and test set
from sklearn.model_selection import train_test_split

X_train,X_val,Y_train,Y_val=train_test_split(final_train_images,Y ,test_size=0.20)
batch_size=32
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255)          
val_datagen = ImageDataGenerator(rescale=1./255) # We do not augment 
validation data.We only perform rescale 

train_generator=train_datagen.flow(X_train,Y_train,batch_size=batch_size)
val_generator = val_datagen.flow(X_val,Y_val,batch_size=batch_size)

运行此代码时,出现以下错误-

Traceback (most recent call last):
File    "C:\Users\renukasi\Documents\College_Research_Work\\myModelwithvggstructure.py", line 100, in <module>
train_generator=train_datagen.flow(X_train,Y_train,batch_size=batch_size)
 File "C:\Program Files\Python36\lib\site-packages\keras_preprocessing\image\image_data_generator.py", line 430, in flow
subset=subset
 File "C:\Program Files\Python36\lib\site- 
 packages\keras_preprocessing\image\numpy_array_iterator.py", line 112, in __init__
self.x = np.asarray(x, dtype=self.dtype)
 File "C:\Program Files\Python36\lib\site-packages\numpy\core\numeric.py", line 538, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: 
'C:\\Users\\renukasi\\Documents\\College_Research_Work\\ Skin_Cancer_Classification\\Melanoma_resize\\Image329.jpg'
>>>  

有人可以指出错误的原因吗?

0 个答案:

没有答案