ValueError:检查输入时出错:预期 conv2d_25_input 的形状为 (144, 256, 3) 但得到的数组的形状为 (256, 144, 3)

数据挖掘 喀拉斯 张量流 美国有线电视新闻网 谷歌 合作实验室
2022-02-22 06:11:56

我已经构建了一个 CNN,其图像大小为 720(高度)和 1280(宽度)。我尝试将图像重新缩放为 144x256。但是,我收到此错误:

ValueError: Error when checking input: expected conv2d_25_input to have shape (144, 256, 3) but got array with shape (256, 144, 3)

我做了显而易见的事情并交换了高度和宽度,但错误仍然存​​在:

ValueError: Error when checking input: expected conv2d_33_input to have shape (256, 144, 3) but got array with shape (144, 256, 3)

错误是相同的,但相反。这是我的代码,在 google colab 上运行。我已将来自 github 的数据嵌入到项目中,因此运行整个程序应该没有问题。有人可以解释错误的来源吗?谢谢!

# Get data
! git clone https://github.com/finn-williams/cachedetector.git

# Import dependancies
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import cv2
import os
import random
%tensorflow_version 1.x
import tensorflow
#from keras.models import Sequential
from keras.layers import Conv2D,Dense,Flatten,Dropout,MaxPooling2D
#from keras.preprocessing.image import ImageDataGenerator
#from keras.callbacks import ReduceLROnPlateau
#from keras import *
from keras.preprocessing.image import ImageDataGenerator
#from keras.preprocessing import image
from keras.callbacks import ModelCheckpoint
from keras import *

# Get data paths
total_images_train_cache = os.listdir('/content/cachedetector/Data/train/Cache')
total_images_train_no_cache = os.listdir('/content/cachedetector/Data/train/NoCache')

# Image processing with generators
image_train_transformer = ImageDataGenerator(rescale = 1./255,
                                   rotation_range = 5,
                                   shear_range = 0.2,
                                   width_shift_range = 0.1,  # Shifts width of each image up to second parameter
                                   height_shift_range = 0.1, # Shifts width of each image up to second parameter
                                   zoom_range = 0.2,  # Zooms into image
                                   brightness_range = [0.2, 1.0], # Changes brightness between parameters
                                   channel_shift_range = 70 # Changes brightness
                                   )

image_test_transformer = ImageDataGenerator(rescale = 1./255)

# Hyperparameters
image_height = 144
image_width = 256
batch_size = 10 # High batch = poor generalization
no_of_epochs  = 200

# Reshape Images
training_set = image_train_transformer.flow_from_directory('/content/cachedetector/Data/train',
                                                 target_size = (image_width, image_height),
                                                 batch_size = batch_size,
                                                 class_mode = 'binary')

test_set = image_test_transformer.flow_from_directory('/content/cachedetector/Data/test',
                                            target_size = (image_width, image_height),
                                            batch_size = batch_size,
                                            class_mode = 'binary')

val_set = image_test_transformer.flow_from_directory('/content/cachedetector/Data/val',
                                            target_size = (image_width, image_height),
                                            batch_size = 1,
                                            shuffle = False,
                                            class_mode = 'binary')

# Build model
model = Sequential([Conv2D(32, (3, 3),input_shape = (image_height, image_width, 3), activation = 'relu'),
                    # Keep filters low at beginning/iamges close to output
                    Conv2D(32,(3,3),activation='relu'),
                    MaxPooling2D(pool_size=(2,2)),
                    Dropout(0.2),
                    Conv2D(64,(3,3),activation='relu'),
                    # Ramp up filters in hidden layer
                    Conv2D(64,(3,3),activation='relu'),
                    MaxPooling2D(pool_size=(2,2)),
                    # Max pooling to reduce dimenstions caused by filters
                    Dropout(0.2),
                    Conv2D(128,(3,3),activation='relu'),
                    Conv2D(128,(3,3),activation='relu'),
                    MaxPooling2D(pool_size=(2,2)),
                    Dropout(0.2),
                    Conv2D(256,(3,3),activation='relu'),
                    Conv2D(256,(3,3),activation='relu'),
                    MaxPooling2D(pool_size=(2,2)),
                    Flatten(),
                    Dense(units=256,activation='relu'),
                    Dropout(0.2),
                    Dense(units=1,activation='sigmoid')])

# Model settings
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Train
history = model.fit_generator(training_set,
                    steps_per_epoch=1319//batch_size, # When to conduct new epoch (calculated using ceil(num_samples / batch_size) )
                    epochs=no_of_epochs,  
                    validation_data=test_set,
                    validation_steps=145//batch_size,
                    callbacks=callbacks_list
                   )
1个回答

显然,输入图像的形状之间存在这种不一致:

target_size = (image_width, image_height)

和模型的输入形状:

input_shape = (image_height, image_width, 3)

这两个应该是一样的。因此,为了省力并做出最小的更改,只需将模型的输入形状固定如下:

input_shape = (image_width, image_height, 3)