当keras调用float32的dtype的numpy.asarray时,如何解决“ float()参数必须是字符串或数字,而不是'PngImageFile”错误

时间:2019-06-02 16:28:25

标签: tf.keras

我正在通过使用kerastensorflow作为后端的二进制分类问题构建多层感知器来学习神经元网络。

这里是图像数据的source

我关注了thisthis

从发现的那些问题来看,我认为该错误与corrupted图片有关,但是我通过验证图片尝试了那些链接内的建议,该图片对我来说没有问题,但错误仍然存​​在。 / p>

stacktrace显示当keras尝试将图像数据转换为数据类型为numpy的{​​{1}}数组时发生了错误,因此我尝试将图像转换为{{ 1}}自己排列数组,然后像float32那样进行转换,但numpy却没有numpy.asarray(image)在做。

假设所有numpy.asarray(image, dtype='float32')就位。

所以要准备数据的代码

keras

用于构建和训练模型的代码

import

预期结果:无错误

实际结果:

image_data_path = '../data/breast_histopathology'

image_width = 50
image_height = 50

train_size_as_percentage = 0.8
validate_size_percentage_of_train_data = 0.1

data_extract_path = image_data_path + '_prep'

train_data_path = data_extract_path + '/training'
test_data_path = data_extract_path + '/testing'
validation_data_path = data_extract_path + '/validation'

if os.path.isdir(data_extract_path):
    shutil.rmtree(data_extract_path)

os.makedirs(train_data_path)
os.makedirs(train_data_path + '/0')
os.makedirs(train_data_path + '/1')
os.makedirs(test_data_path)
os.makedirs(test_data_path + '/0')
os.makedirs(test_data_path + '/1')
os.makedirs(validation_data_path)
os.makedirs(validation_data_path + '/0')
os.makedirs(validation_data_path + '/1')

image_paths = [image_path for image_path in glob.glob(image_data_path + '/**/*', recursive=True)]

random.seed(128)
random.shuffle(image_paths)

training_size = int(len(image_paths) * train_size_as_percentage)
training_image_paths = image_paths[:training_size]
testing_image_paths = image_paths[training_size:]

validation_size = int(len(training_image_paths) * validate_size_percentage_of_train_data)
validation_image_paths = training_image_paths[:validation_size]
training_image_paths = training_image_paths[validation_size:]

datasets = [
    (train_data_path, training_image_paths),
    (test_data_path, testing_image_paths),
    (validation_data_path, validation_image_paths)
]

for data_path, image_paths in datasets:
    for image_path in image_paths:
        filename = image_path.split(os.path.sep)[-1]
        # filename would be, 10253_idx5_x1001_y1001_class0.png,
        # the character before . and word after class are the
        # labeling for the image
        class_label = filename[-5:-4]

        copy_destination = '{}/{}/{}'.format(data_path, class_label, filename)

        if os.path.isfile(image_path):            
            try:
                image = PIL.Image.open(image_path)
                image.verify()

#                 print('=============')
#                 print(filename)
#                 print(image_path)
#                 print(image)
#                 print(image.size)
#                 print(image.format)
#                 print(image.mode)
#                 print(image.verify())
#                 print(numpy.asarray(image, dtype='float32'))
#                 print('XXXXXXXXXXXXX')

                width, height = image.size

                if width == height == image_width and image.format == 'PNG':
                    shutil.copy2(image_path, copy_destination)
            except Exception as e:
                print(str(e))
                pass

0 个答案:

没有答案