Ошибка в моей модели keras cnn относительно формы ввода
Мой код работает хорошо до определения модели cnn. Но как только я пытаюсь начать тренировку, я получаю эту ошибку: ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (0, 1)
Как я могу это исправить?
набор данных, который я использую - это набор данных GTSRB со всей аннотацией.
Вот мой код
import numpy as np
from skimage import io, color, exposure, transform
from sklearn.cross_validation import train_test_split
import os
import glob
import h5py
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, model_from_json
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.callbacks import LearningRateScheduler, ModelCheckpoint
from keras import backend as K
K.set_image_data_format('channels_first')
from matplotlib import pyplot as plt
NUM_CLASSES = 43
IMG_SIZE = 48
def preprocess_img(img):
# Histogram normalization in y
hsv = color.rgb2hsv(img)
hsv[:,:,2] = exposure.equalize_hist(hsv[:,:,2])
img = color.hsv2rgb(hsv)
# central scrop
min_side = min(img.shape[:-1])
centre = img.shape[0]//2, img.shape[1]//2
img = img[centre[0]-min_side//2:centre[0]+min_side//2,
centre[1]-min_side//2:centre[1]+min_side//2,
:]
# rescale to standard size
img = transform.resize(img, (IMG_SIZE, IMG_SIZE))
# roll color axis to axis 0
img = np.rollaxis(img,-1)
return img
def get_class(img_path):
return int(img_path.split('/')[-2])
try:
with h5py.File('X.h5') as hf:
X, Y = hf['imgs'][:], hf['labels'][:]
print("Loaded images from X.h5")
except (IOError,OSError, KeyError):
print("Error in reading X.h5. Processing all images...")
root_dir = '/GTSRB/Final_Training/Images/'
imgs = []
labels = []
all_img_paths = glob.glob(os.path.join(root_dir, '*/*.ppm'))
np.random.shuffle(all_img_paths)
for img_path in all_img_paths:
try:
img = preprocess_img(io.imread(img_path))
label = get_class(img_path)
imgs.append(img)
labels.append(label)
if len(imgs)%1000 == 0: print("Processed {}/{}".format(len(imgs), len(all_img_paths)))
except (IOError, OSError):
print('missed', img_path)
pass
X = np.array(imgs, dtype='float32')
Y = np.eye(NUM_CLASSES, dtype='uint8')[labels]
with h5py.File('X.h5','w') as hf:
hf.create_dataset('imgs', data=X)
hf.create_dataset('labels', data=Y)
def cnn_model():
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
input_shape=(3, IMG_SIZE, IMG_SIZE),
activation='relu'))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3), padding='same',
activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(128, (3, 3), padding='same',
activation='relu'))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(NUM_CLASSES, activation='softmax'))
return model
model = cnn_model()
# let's train the model using SGD + momentum (how original).
lr = 0.01
sgd = SGD(lr=lr, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
def lr_schedule(epoch):
return lr*(0.1**int(epoch/10))
batch_size = 32
nb_epoch = 30
model.fit(X, Y,
batch_size=batch_size,
epochs=nb_epoch,
validation_split=0.2,
shuffle=True,
callbacks=[LearningRateScheduler(lr_schedule),
ModelCheckpoint('model.h5',save_best_only=True)]
)
Вот полный трекбек:
C:\Users\pandey\Anaconda3\lib\site-packages\sklearn\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
"This module will be removed in 0.20.", DeprecationWarning)
C:\Users\pandey\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
Using TensorFlow backend.
Loaded images from X.h5
Traceback (most recent call last):
File "<ipython-input-1-ffb0cf89d761>", line 134, in <module>
ModelCheckpoint('model.h5',save_best_only=True)]
File "C:\Users\pandey\Anaconda3\lib\site-packages\keras\models.py", line 963, in fit
validation_steps=validation_steps)
File "C:\Users\pandey\Anaconda3\lib\site-packages\keras\engine\training.py", line 1630, in fit
batch_size=batch_size)
File "C:\Users\pandey\Anaconda3\lib\site-packages\keras\engine\training.py", line 1476, in _standardize_user_data
exception_prefix='input')
File "C:\Users\pandey\Anaconda3\lib\site-packages\keras\engine\training.py", line 113, in _standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (0, 1)