を得た私は、私はそのがあるためであると考え、次のエラーとValueError:形状を持つことが期待convolution2d_input_8(なし、3、150、150)が、形状を持つ配列(32、150、150、3)
ValueError: Error when checking model input: expected convolution2d_input_8 to have shape (None, 3, 150, 150) but got array with shape (32, 150, 150, 3)
を得ましたのdim_orderingの。テンソルフローバックエンドを使用しています。 しかし、私はそれを削除した場合、私は別のエラーが出る:
ValueError: Negative dimension size caused by subtracting 3 from 1 for 'Conv2D_17' (op: 'Conv2D') with input shapes: [?,1,74,16], [3,3,16,32].
import keras
import numpy as np
np.random.seed(123) # reproducibility
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
# expected image size
img_width, img_height = 150, 150
# folder containing the images on which
# the network will train. The train folder
# has two sub folders, dogs and cats.
train_data_dir = 'C:/Users/Ishan Sohony/Downloads/Compressed/train'
# folder containing the validation samples
# folder structure is same as the training folder
validation_data_dir = 'C:/Users/Ishan Sohony/Downloads/Compressed/test1'
# how many images to be considered for training
train_samples = 2000
# how many images to be used for validation
validation_samples = 800
# how many runs will the network make
# over the training set before starting on
# validation
epoch = 50
# this is the augmentation configuration we will use for training
# we are generating a lot of transformed images so that the model
# can handle variety in the real world scenario
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1./255)
# this section is actually taking images from the folder
# and passing on to the ImageGenerator which then
# creates a lot of transformed versions
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=32,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=32,
class_mode='binary')
# ** Model Begins **
model = Sequential()
model.add(Convolution2D(32, 3, 3, input_shape=(3, img_width, img_height),dim_ordering='th'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),dim_ordering='th'))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),dim_ordering='th'))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),dim_ordering='th'))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
# ** Model Ends **
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
# this is where the actual processing happens
# it will take some time to run this step.
model.fit_generator(
train_generator,
samples_per_epoch=train_samples,
nb_epoch=epoch,
validation_data=validation_generator,
nb_val_samples=validation_samples)
model.save_weights('trial.h5')