简体   繁体   English

CNN - 检测手写笑脸:ValueError:无法将输入数组从形状 (26,26,3) 广播到形状 (26)

[英]CNN - Detecting Handwritten Smilies: ValueError: could not broadcast input array from shape (26,26,3) into shape (26)

In my root folder images I have three folders called 0 , 1 , 2 .在我的根文件夹images我有三个名为012文件夹。 In folder 0 there are no smilies .在文件夹0no smilies In folder 1 there are happy handwritten smilies and in folder 2 there are sad handwritten smilies .文件夹1happy handwritten smilies ,文件夹2sad handwritten smilies The images are jpg color images with the dimension 26x26 .图像是尺寸为26x26 jpg彩色图像。

This is my code这是我的代码

from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten
import numpy as np
import os
import cv2
from sklearn.model_selection import train_test_split

def getImages(path, classes):
    folder = os.listdir(path)
    classes_counter = 0
    images = []
    images_classes = []

    for x in range (0,len(folder)):
        myPicList = os.listdir(path+"/"+ str(classes[classes_counter]))
        for pic in myPicList:
            img_path = path+"/" + str(classes[classes_counter]) + "/" + pic
            img = cv2.imread(img_path)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            images.append(img)
            images_classes.append(classes_counter)
        classes_counter +=1

    images = np.array(images, dtype="float") / 255
    return images, images_classes

def createModel(classes, images_dimension):
    classes_amount = len(np.unique(classes))
    model = Sequential()
    model.add(Conv2D(32, kernel_size=(3, 3), padding='same', activation='relu', input_shape=images_dimension))
    model.add(Conv2D(32, kernel_size=(3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))
    model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))
    model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(classes_amount, activation='softmax'))

    return model


labels = [0,1,2]
images, images_classes = getImages('training-images', labels)
images_dimension=(26,26,3)

X_train, X_test, Y_train, Y_test = train_test_split(images, images_classes, test_size=0.2)  # if 1000 images split will 200 for testing
X_train, X_validation, Y_train, Y_validation = train_test_split(X_train, Y_train, test_size=0.2) # if 1000 images 20% of remaining 800 will be 160 for validation

model = createModel(labels, images_dimension)
batch_size = 20
epochs = 100
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(X_validation, Y_validation))
model.evaluate(X_test,Y_test,verbose=0)

In the Line images = np.array(images, dtype="float") / 255 I get the error: images = np.array(images, dtype="float") / 255我得到错误:

Traceback (most recent call last):
  File "train-nn.py", line 54, in <module>
    images, images_classes = getImages('training-images', labels)
  File "train-nn.py", line 24, in getImages
    images = np.array(images, dtype="float") / 255
ValueError: could not broadcast input array from shape (26,26) into shape (26)

I think something is wrong with the datastructure or with the array structure.我认为数据结构或数组结构有问题。 I have no clue what I did wrong.我不知道我做错了什么。 Maybe someone known this problem and can give me a hint!也许有人知道这个问题,可以给我一个提示!

Here you can download the whole project as zip file.在这里,您可以将整个项目下载为 zip 文件。 http://fileshare.mynotiz.de/cnn-handwritten-smilies.zip http://fileshare.mynotiz.de/cnn-handwritten-smilies.zip

I found the problem.我发现了问题。 One of my test-data images was not in the format of 26x26 it was 26x23.我的一张测试数据图像不是 26x26 的格式,而是 26x23。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:检查输入时出错:预期density_26_input具有形状(45781),但数组形状为(2,) - ValueError: Error when checking input: expected dense_26_input to have shape (45781,) but got array with shape (2,) ValueError:无法将输入数组从形状(22500,3)广播到形状(1) - ValueError: could not broadcast input array from shape (22500,3) into shape (1) ValueError:无法从形状(相同形状)广播输入数组 - ValueError: could not broadcast input array from shape (same shape) ValueError:无法将输入数组从形状(3)广播到形状(2) - ValueError: could not broadcast input array from shape (3) into shape (2) ValueError:无法将输入数组从形状(83)广播到形状(84) - ValueError: could not broadcast input array from shape (83) into shape (84) Numpy:ValueError:无法将输入数组从形状(4,1)广播到形状(4) - Numpy: ValueError: could not broadcast input array from shape (4,1) into shape (4) ValueError:无法将输入数组从形状(1,3)广播到形状(3,1) - ValueError: could not broadcast input array from shape (1,3) into shape (3,1) ValueError:无法将输入数组从形状(0,2)广播到形状(2,) - ValueError: could not broadcast input array from shape (0,2) into shape (2,) python - ValueError:无法将形状 (2,2) 中的输入数组广播到形状 (3,) - python - ValueError: could not broadcast input array from shape (2,2) into shape (3,) ValueError:无法将输入数组从形状(6)广播到形状(1) - ValueError: could not broadcast input array from shape (6) into shape (1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM