gpt4 book ai didi

python - ValueError : could not broadcast input array from shape (300, 300,3) 变成形状 (300,300)

转载 作者:太空宇宙 更新时间:2023-11-04 01:46:02 24 4
gpt4 key购买 nike

我有一个简单的 Python 代码(用于训练的 Keras 教程)。我试图删除 img = img.convert('L') 以在加载图像时保持颜色(我所有的图像都是 RGB 彩色的,所以数据不是问题),但我遇到了这个错误:

training_images = np.array([i[0] for i in training_data]).reshape(-1, IMAGE_SIZE, IMAGE_SIZE, 3)
ValueError: could not broadcast input array from shape (300,300,3) into shape (300,300)

怎么了?如何解决?

from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from PIL import Image
from random import shuffle, choice
import numpy as np
import os

IMAGE_SIZE = 300
IMAGE_DIRECTORY = './data/test_set'

def label_img(name):
if name == 'cats': return np.array([1, 0])
elif name == 'notcats' : return np.array([0, 1])

def load_data():
train_data = []
directories = next(os.walk(IMAGE_DIRECTORY))[1]

for dirname in directories:
file_names = next(os.walk(os.path.join(IMAGE_DIRECTORY, dirname)))[2]

for i in range(200):
image_name = choice(file_names)
image_path = os.path.join(IMAGE_DIRECTORY, dirname, image_name)
label = label_img(dirname)
img = Image.open(image_path)
#img = img.convert('L')
img = img.resize((IMAGE_SIZE, IMAGE_SIZE), Image.ANTIALIAS)
train_data.append([np.array(img), label])

return train_data

def create_model():
model = Sequential()
model.add(Conv2D(32, kernel_size = (3, 3), activation='relu',
input_shape=(IMAGE_SIZE, IMAGE_SIZE, 1)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(BatchNormalization())
model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(BatchNormalization())
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(64, activation='relu'))
model.add(Dense(2, activation = 'softmax'))

return model

training_data = load_data()
training_images = np.array([i[0] for i in training_data]).reshape(-1, IMAGE_SIZE, IMAGE_SIZE, 1)
training_labels = np.array([i[1] for i in training_data])

model = create_model()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(training_images, training_labels, batch_size=50, epochs=10, verbose=1)

最佳答案

因为我能够在一些 discussion in the comments 之后识别问题,我会将其发布为答案。

在线

training_images = np.array([i[0] for i in training_data]).reshape(-1, IMAGE_SIZE, IMAGE_SIZE, 1)

你正试图将 3 channel RGB 图像 reshape 为单 channel (灰度)图像,这是不可能的(而且你也不想做,因为你想保留颜色),因此 ValueError 。这部分仅在您删除 img = img.convert('L') 之前是必需的,以便为训练数据提供模型的正确形状,该模型的输入形状为 ( IMAGE_SIZE,IMAGE_SIZE,1)

现在您正在处理 RGB 图像,可以删除 reshape,因为图像已经具有返回的正确形状 (IMAGE_SIZE, IMAGE_SIZE, 3)通过 load_data()。但是,如 nneonneo's answer 中所述,您的模型将需要修改才能处理新的输入形状。

关于python - ValueError : could not broadcast input array from shape (300, 300,3) 变成形状 (300,300),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59077156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com