model.fit()和\u目录()中的流\u中出现错误

ovfsdjhp  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(271)

我正在做一个面部识别项目。train文件夹包含2个子文件夹,每个子文件夹包含100个图像。测试文件夹也是如此。

IMAGE_SIZE = [224, 224]

train_path = "C:/Users/hp/Desktop/Capstone/Train/"
test_path = "C:/Users/hp/Desktop/Capstone/Test/"

# tell the model what cost and optimization method to use

model.compile(
  loss='categorical_crossentropy',
  optimizer='sgd',
  metrics=['accuracy']
)

train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
                                   rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory(train_path,
                                                 target_size = (224, 224),
                                                 batch_size = 32,
                                                 class_mode = 'categorical')

test_set = test_datagen.flow_from_directory(test_path,
                                            target_size = (224, 224),
                                            batch_size = 32,
                                            class_mode = 'categorical')

print(training_set.__len__())

r=model.fit(training_set,
            validation_data=test_set,
            epochs=10,
            steps_per_epoch=5,
            validation_steps=32,
            verbose=2)

但是在进行training_set.len()时,它会输出7。另外,我在模型中得到一个错误。fit()。我找不到解决这个错误的办法。代码的上半部分被剪切。这里的主要问题是model.fit()中的错误。

Found 200 images belonging to 2 classes.
Found 40 images belonging to 2 classes.
7
Traceback (most recent call last):
  File "C:/Users/hp/PycharmProjects/Capstone/main.py", line 68, in <module>
    r=model.fit(training_set,
  File "C:\Users\hp\PycharmProjects\Capstone\venv\lib\site-packages\keras\engine\training.py", line 1108, in fit
    data_handler = data_adapter.get_data_handler(
  File "C:\Users\hp\PycharmProjects\Capstone\venv\lib\site-packages\keras\engine\data_adapter.py", line 1348, in get_data_handler
    return DataHandler(*args,**kwargs)
  File "C:\Users\hp\PycharmProjects\Capstone\venv\lib\site-packages\keras\engine\data_adapter.py", line 1138, in __init__
    self._adapter = adapter_cls(
  File "C:\Users\hp\PycharmProjects\Capstone\venv\lib\site-packages\keras_preprocessing\image\iterator.py", line 65, in __getitem__
    return self._get_batches_of_transformed_samples(index_array)
  File "C:\Users\hp\PycharmProjects\Capstone\venv\lib\site-packages\keras_preprocessing\image\iterator.py", line 231, in _get_batches_of_transformed_samples
    x = img_to_array(img, data_format=self.data_format)
  File "C:\Users\hp\PycharmProjects\Capstone\venv\lib\site-packages\keras_preprocessing\image\utils.py", line 309, in img_to_array
    x = np.asarray(img, dtype=dtype)
  File "C:\Users\hp\PycharmProjects\Capstone\venv\lib\site-packages\numpy\core\_asarray.py", line 83, in asarray
    return array(a, dtype, copy=False, order=order)
TypeError: __array__() takes 1 positional argument but 2 were given
kknvjkwl

kknvjkwl1#

这是新版本的 PIL 因此,降低它的级别应该能起到作用。

pip install pillow==8.2.0

相关问题