imutils.resize не получит правильную форму: ожидалось, что conv2d_1_input будет иметь форму (64, 64, 3), но получил массив с формой (91, 64, 3) - PullRequest
0 голосов
/ 29 мая 2020

В моем файле модели я уже установил для параметра inputhape значение (64,64,3) width = 64, height = 64 и 3 канала, но когда я выполнил файл, я получил сообщение об ошибке, которое ожидалось, что conv2d_1_input будет иметь форму (64, 64, 3), но получил массив с формой (91, 64, 3), как показано ниже. Что означает (91, 64, 3) и почему я получил эту ошибку? Как я могу это исправить? Пожалуйста, помогите

ошибка, которую я получил

ValueError: Error when checking input: expected conv2d_1_input to have shape (64, 64, 3) but got array with shape (91, 64, 3)

l enet .py

from keras.models import Sequential
from keras.layers.convolutional import MaxPooling2D, Conv2D
from keras.layers.core import Activation, Dense, Flatten
from keras import backend as K


class LeNet:
    @staticmethod
    def build(width, height, depth, classes):
        #initialize the model
        model = Sequential()
        inputShpage = (height, width, depth)

        # if we are using "channel first" update the input shape
        if K.image_data_format() == "channels_first":
            inputShpage = (depth, height, width)

        # first set of CONV => RELU => MaxPooling
        model.add(Conv2D(32, (3, 3), padding="same", input_shape=inputShpage))
        model.add(Activation("relu"))
        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

        # second set of CONV => RELU => MaxPooling
        model.add(Conv2D(32, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

        # FC => RELU layer
        model.add(Flatten())
        model.add(Dense(500))
        model.add(Activation("relu"))

        # softmax classifier for probability
        model.add(Dense(classes))
        model.add(Activation("softmax"))

        # return the constructed network architecture
        return model

model.py

from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from keras.preprocessing.image import img_to_array
from keras.utils import np_utils
from lenet import LeNet
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import imutils
import cv2
import os

# construct the argument parse and parse the argument
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True, help="path to the dataset")
ap.add_argument("-m", "--model", required=True, help="path to the output model or saved model")
args = vars(ap.parse_args())

# initialize the list of data and label
data = []
labels = []

for imagePath in sorted(list(paths.list_images(args["dataset"]))):
    # load the image, preprocessing it and store it in the data list
    image = cv2.imread(imagePath)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = imutils.resize(image, width=64)
    image = img_to_array(image)
    data.append(image)

    # extract the class label from the image path and update the labels list
    label = imagePath.split(os.path.sep)[-3]
    label = "Hong" if label == "hong" else "Not Hong"
    labels.append(label)

    # scale the raw pixel intensities to the range [0,1]
    data = np.array(data, dtype="float") / 255.0
    label = np.array(labels)

    # convert labels from integers to vectors
    le = LabelEncoder().fit(labels)
    labels = np_utils.to_categorical(le.transform(labels), 2)

    # account for skew in the labeled data
    classTotals = labels.sum(axis=0)
    classWeight = classTotals.max() / classTotals

    # split the data into training_set and test_set
    (trainX, testX, trainY, testY) = train_test_split(data, labels, 
                                                    test_size=0.33, 
                                                    random_state=42)

    # initialize the model
    print("[ WORKING ON MODEL ] compiling model.....")
    model = LeNet.build(width=64, height=64, depth=3, classes=2)
    model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])

    # train the network
    print("[ WORKING ON NETWORK ] traning the network.....")
    H = model.fit(trainX, trainY, validation_data=(testX, testY),
                class_weight=classWeight, batch_size=32, epochs=25, verbose=1)

    # evaluate the network
    print("[ WORKING ON NETWORK ] evaluating the network.....")
    predictions = model.predict(testX, batch_size=32)
    print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1),
                                target_names=le.classes_))

    # saving the model
    print("[ WORKING ON NETWORK ] serializing network.....")
    model.save(args["model"])

1 Ответ

2 голосов
/ 29 мая 2020

imutils.resize() сохраняет соотношение сторон изображения, поэтому вы можете указать только либо ширину, или высоту, но не оба сразу. Поскольку вам может потребоваться изменить соотношение сторон, чтобы оно соответствовало вашей нейронной сети, я предлагаю вам использовать cv2.resize().

image = cv2.resize(image, (64, 64))

вместо этого:

image = imutils.resize(image, width=64)  
...