1、出现caused by subtracting 2 from 1 for 'block1_pool/MaxPool
File "D:\tool\learning_tools\python3.5\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 627, in call_cpp_shape_fn
require_shape_fn)
File "D:\tool\learning_tools\python3.5\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 691, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Negative dimension size caused by subtracting 2 from 1 for 'block1_pool/MaxPool' (op: 'MaxPool') with input shapes: [?,1,100,64].
错误的原因Keras是Theano或Tensorflow库的包装器在使用调用api出现的一些问题
1、~/.keras/keras.json设置image_dim_ordering 2、image_dim_ordering在模型中指定如下:model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="th")) 3、在程序中直接添加 from keras import backend as K K.set_image_dim_ordering('th')
2、model.compile参数设置
# 二分类 #model.compile(loss='binary_crossentropy', # optimizer='rmsprop', # metrics=['accuracy']) # 多分类 model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) # 优化器rmsprop:除学习率可调整外,建议保持优化器的其他默认参数不变 # 优化器的设置 # initiate RMSprop optimizer opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) model.compile(loss='binary_crossentropy', optimizer=optimizers.SGD(lr=1e-4, momentum=0.9), metrics=['accuracy'])
3、图片预处理
imageDataGenerator:
用以生成一个batch的图像数据,支持实时数据提升。训练时该函数会无限生成数据,直到达到规定的epoch次数为止。
flow_from_directory(directory):
以文件夹路径为参数,生成经过数据提升/归一化后的数据,在一个无限循环中无限产生batch数据,直接产生数据和标签。
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
'''
rescale: rescaling factor. Defaults to None.
If None or 0, no rescaling is applied,
otherwise we multiply the data by the value provided
(after applying all other transformations).
shear_range: Float. Shear Intensity
(Shear angle in counter-clockwise direction in degrees)
zoom_range: Float or [lower, upper]. Range for random zoom.
If a float, `[lower, upper] = [1-zoom_range, 1+zoom_range]`
horizontal_flip: Boolean. Randomly flip inputs horizontally.
'''
flow_from_directory的用法介绍
https://blog.csdn.net/mieleizhi0522/article/details/82191331
4、模型的fit的介绍
model.fit_generator(train_generator,
steps_per_epoch=1000,
epochs=2,
validation_data=validation_generator,
validation_steps=200)
model.fit(X_train,Y_train,epochs = 20,batch_size = 128)