keras 无效参数错误:日志和标签必须是可广播的:对数大小=[10,10]标签大小=[100,10]

mcvgt66p  于 2022-12-04  发布在  其他
关注(0)|答案(3)|浏览(132)

我在网上看了一个教程,他们用一个狗和猫的数据集创建了一个CNN模型。我在学习教程的时候使用了kaggle的Animals-10。当我拟合模型时,我得到了InvalidArgumentError: logits and labels must be broadcastable: logits_size=[10,10] labels_size=[100,10] [[{{node loss_17/dense_38_loss/softmax_cross_entropy_with_logits}}]]错误。我在谷歌上搜索了一下,但是我什么也想不出来,而且所有的解决方案似乎都是针对具体问题的。我已经在这个问题上纠结了一段时间了,想不出来。谢谢你的帮助。

代码

# Preprocessing for training data
butterfly_path = './animals/butterfly/'
cat_path = './animals/cat/'
chicken_path = './animals/chicken/'
cow_path = './animals/cow/'
dog_path = './animals/dog/'
elephant_path = './animals/elephant/'
horse_path = './animals/horse/'
sheep_path = './animals/sheep/'
spider_path = './animals/spider/'
squirrel_path = './animals/squirrel/'
butterfly_files = [f for f in listdir(butterfly_path) if isfile(join(butterfly_path, f))]
cat_files = [f for f in listdir(cat_path) if isfile(join(cat_path, f))]
chicken_files = [f for f in listdir(chicken_path) if isfile(join(chicken_path, f))]
cow_files = [f for f in listdir(cow_path) if isfile(join(cow_path, f))]
dog_files = [f for f in listdir(dog_path) if isfile(join(dog_path, f))]
elephant_files = [f for f in listdir(elephant_path) if isfile(join(elephant_path, f))]
horse_files = [f for f in listdir(horse_path) if isfile(join(horse_path, f))]
sheep_files = [f for f in listdir(sheep_path) if isfile(join(sheep_path, f))]
spider_files = [f for f in listdir(spider_path) if isfile(join(spider_path, f))]
squirrel_files = [f for f in listdir(squirrel_path) if isfile(join(squirrel_path, f))]
X_train = []
y_train = []

# Convert images to arrays and append labels to list
for file in butterfly_files:
    img_path = butterfly_path + file
    y_train.append(0) # butterfly
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in cat_files:
    img_path = cat_path + file
    y_train.append(1) # cat
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in chicken_files:
    img_path = chicken_path + file
    y_train.append(2) # chicken
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in cow_files:
    img_path = cow_path + file
    y_train.append(3) # cow
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in dog_files:
    img_path = dog_path + file
    y_train.append(4) # dog
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in elephant_files:
    img_path = elephant_path + file
    y_train.append(5) # elephant
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in horse_files:
    img_path = horse_path + file
    y_train.append(6) # horse
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in sheep_files:
    img_path = sheep_path + file
    y_train.append(7) # sheep
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in spider_files:
    img_path = spider_path + file
    y_train.append(8)  # spider
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in squirrel_files:
    img_path = squirrel_path + file
    y_train.append(9) # squirrel
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)

X_train = np.array(X_train)
y_train = np.array(y_train)

new_X_train, new_y_train = sklearn.utils.shuffle(X_train, y_train, random_state = 0)
# Observe data shapes
print("Train Images: ", X_train.shape, "\nTrain Labels: ", y_train.shape)

输出

Train Images:  (26179, 100, 100) 
Train Labels:  (26179,)

更多代码

# Flatten data
new_X_train = new_X_train.reshape(-1, 100, 100, 1)

new_X_train.shape

new_X_train[0]

输出

(26179, 100, 100, 1)

array([[[0.11311903],
        [0.11212739],
        [0.10983502],
        ...,
        [0.05591958],
        [0.05303901],
        [0.05191926]],

       [[0.11488738],
        [0.11407327],
        [0.11111339],
        ...,
        [0.05361823],
        [0.05217732],
        [0.05204283]],

       [[0.11666758],
        [0.11481607],
        [0.11202889],
        ...,
        [0.05181058],
        [0.05311151],
        [0.05275289]],

       ...,

       [[0.26910149],
        [0.26756103],
        [0.26437813],
        ...,
        [0.54128431],
        [0.52813318],
        [0.52014463]],

       [[0.26764131],
        [0.2660793 ],
        [0.26377079],
        ...,
        [0.53660792],
        [0.52183369],
        [0.51606187]],

       [[0.26523861],
        [0.26538986],
        [0.26358519],
        ...,
        [0.53761132],
        [0.51546228],
        [0.50781162]]])

开始创建模型的代码

# Convert labels to categorical
new_y_train = keras.utils.to_categorical(new_y_train, 10)

# Create the model
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(64, (3, 3), input_shape = new_X_train.shape[1:], activation = 'relu'))
model.add(keras.layers.MaxPooling2D(pool_size = (2, 2)))

model.add(keras.layers.Conv2D(64, (3, 3), activation = 'relu'))
model.add(keras.layers.MaxPooling2D(pool_size = (2, 2)))

model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(units = 64, activation = 'relu'))

model.add(keras.layers.Dense(units = 10, activation = 'softmax'))

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

# Fit model
model.fit(x = new_X_train, y = new_y_train, batch_size = 10, epochs = 1)
lymnna71

lymnna711#

您的代码正在运行:

import tensorflow as tf
from tensorflow import keras
new_X_train = tf.random.uniform((26179, 100, 100, 1))
new_y_train = tf.random.uniform((26179,))
# Convert labels to categorical
new_y_train = keras.utils.to_categorical(new_y_train, 10)

# Create the model
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(64, (3, 3), input_shape = new_X_train.shape[1:], activation = 'relu'))
model.add(keras.layers.MaxPooling2D(pool_size = (2, 2)))

model.add(keras.layers.Conv2D(64, (3, 3), activation = 'relu'))
model.add(keras.layers.MaxPooling2D(pool_size = (2, 2)))

model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(units = 64, activation = 'relu'))

model.add(keras.layers.Dense(units = 10, activation = 'softmax'))

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

# Fit model
model.fit(x = new_X_train, y = new_y_train, batch_size = 10, epochs = 1)
a2mppw5e

a2mppw5e2#

我想我的问题是这段代码:

# Convert labels to categorical
new_y_train = keras.utils.to_categorical(new_y_train, 10)

由于某种原因,我得到了一个10x10矩阵的每一项,但现在它似乎工作,只是给我一个10项数组

yuvru6vn

yuvru6vn3#

我有一个类似的问题,第一维的logits是不同的一个标签。
我试着用这个代码:https://keras.io/examples/vision/oxford_pets_image_segmentation/
但有非正方形的图像。什么解决了我的问题,调整图像的平方比(我仍然不知道为什么logits成为不同的大小相对于标签与矩形图像...)

相关问题