预测测试图像时出现错误 - 无法重塑大小数组

问题描述 投票:0回答:2

我正在尝试使用 TensorFlow 和 Keras 在 Python 中进行图像识别,并且我已经关注了下面的博客。 https://stackabuse.com/image-recognition-in-python-with-tensorflow-and-keras/

我需要找到每一层的输出,同时我也试图预测图像。

请参阅下面我的代码。我无法预测和想象我所提供的 非常感谢您为解决此问题提供的任何帮助和意见。

    import numpy
    from keras import models
    from keras.models import Sequential
    from keras.layers import Dense, Dropout, Flatten, BatchNormalization, Activation
    from keras.layers.convolutional import Conv2D, MaxPooling2D
    from keras.constraints import maxnorm
    from keras.utils import np_utils
    from matplotlib import pyplot


    # Create dictionary of target classes
    label_dict = {
     0: 'T-shirt/top',
     1: 'Trouser',
     2: 'Pullover',
     3: 'Dress',
     4: 'Coat',
     5: 'Sandal',
     6: 'Shirt',
     7: 'Sneaker',
     8: 'Bag',
     9: 'Ankle boot',
    }

    # example of loading the mnist dataset
    from keras.datasets import fashion_mnist
    from matplotlib import pyplot
    # load dataset
    #trainX, trainy), (testX, testy) = fashion_mnist.load_data()
    (X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()
    # summarize loaded dataset
    print('Train: X=%s, y=%s' % (X_train.shape, y_train.shape))
    print('Test: X=%s, y=%s' % (X_test.shape, y_test.shape))
    # plot first few images
    for i in range(9):
        # define subplot
        pyplot.subplot(330 + 1 + i)
        # plot raw pixel data
        pyplot.imshow(X_train[i], cmap=pyplot.get_cmap('gray'))
        # show the figure
    
    pyplot.show()


    # normalize the inputs from 0-255 to between 0 and 1 by dividing by 255
        
    X_train = X_train.astype('float32')
    X_test = X_test.astype('float32')
    X_train = X_train / 255.0
    X_test = X_test / 255.0


    # one hot encode outputs
    y_train = np_utils.to_categorical(y_train)
    y_test = np_utils.to_categorical(y_test)
    class_num =10

    numpy.max(X_train[0])

    numpy.min(X_train[0])

    X_train[0][500:]
    
    y_train[:500][0]
    
    y_train[:500][0]

    X_train.shape, X_test.shape
    
    y_train.shape, y_test.shape
    
    # Reshape training and testing image
    X_train = X_train.reshape(-1, 28, 28, 1)
    X_test = X_test.reshape(-1,28,28,1)
    
    model = Sequential()
    
    model.add(Conv2D(32, (3, 3), input_shape=X_train.shape[1:], padding='same'))
    model.add(Activation('relu'))
    
    model.add(Conv2D(32, (3, 3), input_shape=(3, 32, 32), activation='relu', padding='same'))
    
    model.add(Dropout(0.2))
    
    model.add(BatchNormalization())
    
    model.add(Conv2D(64, (3, 3), padding='same', name='test1'))
    model.add(Activation('relu'))
    
    
    
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())
    
    
    
    model.add(Conv2D(64, (3, 3), padding='same', name='test2'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())
    model.add(Conv2D(128, (3, 3), padding='same', name='test3'))
    model.add(Activation('relu'))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())
    
    
    model.add(Flatten())
    model.add(Dropout(0.2))
    
    model.add(Dense(256, kernel_constraint=maxnorm(3)))
    model.add(Activation('relu'))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())
    model.add(Dense(128, kernel_constraint=maxnorm(3)))
    model.add(Activation('relu'))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())
    
    
    
    model.add(Dense(class_num))
    model.add(Activation('softmax'))
    
    
    
    epochs = 2
    optimizer = 'adam'
    
    model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])


    print(model.summary())


    model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=epochs, batch_size=64)


    # Model evaluation
    scores = model.evaluate(X_test, y_test, verbose=0)
    print("Accuracy: %.2f%%" % (scores[1]*100))


# This I am predicting from test images
    image_index = 430
    pyplot.imshow(X_test[image_index].reshape(28, 28,1),cmap='Greys')
    pred = model.predict(X_test[image_index].reshape(-1, 28, 28, 1))
    print(pred.argmax())

我正在尝试使用下图预测模型

尝试使用上述训练模型来预测该图像

我写了下面的代码

from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator

    img_path = 'data/img.jpg'
    img = image.load_img(img_path, target_size=(28, 28))
    img_tensor = image.img_to_array(img)
    img_tensor = numpy.expand_dims(img_tensor, axis=0)
    img_tensor /= 255.
    pyplot.imshow(img_tensor[0])
    pyplot.show()
    print(img_tensor.shape)


    pred = model.predict(img_tensor.reshape(-1, 28, 28, 28))
    print(pred.argmax())

使用上述 model.predict,我得到以下错误

    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-125-965f3e01bc73> in <module>
    ----> 1 pred = model.predict(img_tensor.reshape(-1, 28, 28, 28))
          2 print(pred.argmax())

    **ValueError: cannot reshape array of size 2352 into shape (28,28,28)**

我已经尝试了所有选项,但没有得到正确的结果。 任何人都可以帮助我得到正确的结果吗?

python machine-learning image-processing deep-learning tf.keras
2个回答
0
投票

您的输入大小为 (28,28,3),但您将其转换为 (28,28,28),这是错误的。尝试:

pred = model.predict(img_tensor.reshape(-1, 28, 28, 3))

0
投票

文件“c:\Xampp\htdocs\python\multi-pose&face-detection esting6.py”,第 100 行,位于 特征 = features.reshape((1, features.shape[0], features.shape[1], 1)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ ValueError:无法将大小为 8 的数组重塑为形状 (1,1,4,1)

© www.soinside.com 2019 - 2024. All rights reserved.