我正在运行一个CNN,用于检查图像,但不进行分类。实际上,输出层是一个密集层,具有1d标签中的图像大小作为参数。
如下代码所示,我使用的是model.fit_generator()而不是model.fit,在开始训练模型时会出现以下错误:
“ ValueError:检查目标时出错:预期density_14具有2 尺寸,但数组的形状为(144,11,1756)”
模型摘要如下:
conv2d_45(Conv2D)(无,53,1754,8)80
activation_57(激活)(无,53,1754,8)0
max_pooling2d_43(MaxPooling(None,17,584,8)0
conv2d_46(Conv2D)(无,15、582、16)1168
activation_58(激活)(无,15、582、16)0
max_pooling2d_44(MaxPooling(None,5,194,16)0
conv2d_47(Conv2D)(无,3,192,32)4640
activation_59(激活)(无,3、192、32)0
max_pooling2d_45(MaxPooling(None,1,64,32)0
activation_60(激活)(无,1、64、32)0
flatten_15(Flatten)(无,2048)0
总参数:39,584,372 可训练的参数:39,584,372 不可训练的参数:0
有什么建议吗? 预先感谢!
代码:
def generator(data_arr, batch_size = 12):
num = len(data_arr)
# Loop forever so the generator never terminates
while True:
#shuffle(data_arr)
i = 0
for offset in range(0, num, batch_size):
batch_samples = data_arr[offset:offset+batch_size]
samples = []
labels = []
for i in range(0, offset+batch_size, 1):
for batch_sample in batch_samples:
samples.append(data_arr[i][0])
labels.append(data_arr[i][1])
X_ = np.array(samples)
Y_ = np.array(labels)
i+= batch_size
X_ = X_[:, :, :, newaxis]
yield (X_, Y_)
# compile and train the model using the generator function
train_generator = generator(training_data, batch_size = 12)
validation_generator = generator(val_data, batch_size = 12)
run_opts = tf.RunOptions(report_tensor_allocations_upon_oom = True)
model = Sequential()
model.add(Conv2D(8, (3, 3), input_shape = (55,1756,1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (3, 3)))
model.add(Conv2D(16, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (3, 3)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (3, 3)))
model.add(Activation('softmax'))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature
vectors
model.add(Dense(19316))
model.compile(loss = 'sparse_categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'],
options = run_opts)
model.summary()
batch_size = 12
nb_epoch = 6
model.fit_generator(train_generator,
steps_per_epoch = len(training_data) ,
epochs = nb_epoch,
validation_data = validation_generator,
validation_steps = len(val_data))