我正在尝试使用以下构造的模型训练图像数据。我总共有16353张图片,形状是224x224x1。我正在尝试使此模型起作用。但是面临输入或尺寸方面的问题。
我尝试将形状更改为[None,1],但此方法不再起作用
#dense and softmax done
def add_output_layer(layer):
from keras.layers.core import Dense, Activation
from keras.layers.wrappers import TimeDistributed
layer = TimeDistributed(Dense(2))(layer) # 2 categories
return Activation('softmax')(layer)
#batch and relu
def _bn_relu(layer):
from keras.layers import BatchNormalization
from keras.layers import Activation
layer = BatchNormalization()(layer)
layer = Activation('relu')(layer)
# if dropout > 0:
from keras.layers import Dropout
layer = Dropout(0.2)(layer)
return layer
def add_conv_weight(
layer, subsample_length=1):
from keras.layers import Conv1D
layer = Conv2D(
filters=32,
kernel_size=(16),
strides=subsample_length,
padding='same',
kernel_initializer='he_normal',
input_shape = (224,224,1))(layer)
return layer
def resnet_block(
layer,
subsample_length,
block_index):
from keras.layers import Add
from keras.layers import MaxPooling1D
from keras.layers.core import Lambda
shortcut = MaxPooling2D(pool_size=subsample_length)(layer)
for i in range(2):
if not (block_index == 0 and i == 0):
layer = _bn_relu(
layer)
layer = add_conv_weight(
layer,
subsample_length if i == 0 else 1)
layer = Add()([shortcut, layer])
return layer
#start here
input_img = keras.Input(shape = (224,224,1))
#First block
layer = add_conv_weight(input_img)
layer = _bn_relu(layer)
#Second block #Third block
for index, subsample_length in enumerate([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]):
layer = resnet_block(
layer,
subsample_length,
index)
layer = _bn_relu(layer)
#Forth block
from keras.layers import Add
layer = _bn_relu(layer)
#layer = Add()(Flatten())
out = add_output_layer(layer)
model = keras.Model(inputs=[input_img], outputs=[out])
from keras.optimizers import Adam
optimizer = Adam(
lr=0.001,
clipnorm=1)
model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
预期activation_920具有4个维度,但数组的形状为(16353,2)。之前我有conv1D和maxpool1D。更改为2D