我正在使用KITTI数据集构建道路分割模型。当我尝试训练模型时,出现以下错误
Error when checking target: expected activation_26 to have 2 dimensions,
but got array with shape (289, 160, 576, 2)
我的x具有(289、160、576、3)和y(289、160、576、2)的形状。我的模型看起来像这样
我的模特。
model = Sequential()
model.add(Conv2D(96, (5, 5), padding="same",input_shape=(160, 576, 3)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2), dim_ordering="tf"))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2), dim_ordering="tf"))
model.add(ZeroPadding2D((1,1)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2) , dim_ordering="tf"))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
# softmax classifier
model.add(Dense(2))
model.add(Activation("softmax"))
#Here is my data augmentation
aug = ImageDataGenerator(rotation_range=40, width_shift_range=0.2,
height_shift_range=0.2, shear_range=0.1, zoom_range=0.2,
fill_mode="nearest")
adam = Adam(lr=1e-5)
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
H = model.fit_generator(aug.flow(x, y, batch_size=BATCH_SIZE),
validation_data=(x, y), steps_per_epoch=len(x) // BATCH_SIZE,
epochs=EPOCHS, verbose=1)
我想这是最后一层的问题。我的y是多维的,但我的最后一层是2维的。当我将图层更改为model.add(Dense((289, 160, 576, 2)))
时,出现此错误TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
答案 0 :(得分:0)
尝试执行以下操作:
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, ZeroPadding2D, Convolution2D
from keras.models import Sequential
model = Sequential()
model.add(Conv2D(96, (5, 5), padding="same", input_shape=(160, 576, 3)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2), dim_ordering="tf"))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2), dim_ordering="tf"))
model.add(ZeroPadding2D((1, 1)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2), dim_ordering="tf"))
# at this point the representation is (20, 72, 512)
# we need to evolve this to become (160, 576, 2)
#### This part I would leave out:
# model.add(Flatten())
# model.add(Dense(4096, activation='relu'))
# model.add(Dropout(0.5))
# model.add(Dense(4096, activation='relu'))
# model.add(Dropout(0.5))
# # softmax classifier
# model.add(Dense(2))
# model.add(Activation("softmax"))
#### ... and replace it with:
model.add(Conv2D(128, (2, 2), padding="same")) # (20, 72, 128)
model.add(UpSampling2D((2,2))) # (40, 144, 128)
model.add(Conv2D(64, (2, 2), padding="same")) # (40, 144, 64)
model.add(UpSampling2D((2,2))) # (80, 288, 64)
model.add(Conv2D(16, (2, 2), padding="same")) # (80, 288, 16)
model.add(UpSampling2D((2,2))) # # (160, 576, 16)
model.add(Conv2D(2, (2, 2), activation="softmax", padding="same")) # (160, 576, 2)
print(model.summary())