我在tf.keras框架中编写了一个简单的CNN模型,希望将其用于可变的输入大小。
根据this“文档”,我可以通过设置input_shape=(None, None, n_channels)
来使用可变的输入大小,并且在密集层之前使用了GlobalMaxPooling2D
层来标准化密集层的输入
但是,当我使用一种尺寸的图像训练模型并尝试预测另一种尺寸时,会出现错误:
File "multilabel_384.py", line 180, in main
probabilities = model.predict(test_data)
File "/usr/local/miniconda3/envs/deepchem/lib/python3.5/site-packages/tensorflow/python/keras/engine/training.py", line 1471, in predict
x, check_steps=True, steps_name='steps', steps=steps)
File "/usr/local/miniconda3/envs/deepchem/lib/python3.5/site-packages/tensorflow/python/keras/engine/training.py", line 868, in _standardize_user_data
exception_prefix='input')
File "/usr/local/miniconda3/envs/deepchem/lib/python3.5/site-packages/tensorflow/python/keras/engine/training_utils.py", line 191, in standardize_input_data
' but got array with shape ' + str(data_shape))
ValueError: Error when checking input: expected sequential_input to have shape (16, 24, 1) but got array with shape (32, 48, 1)
这是用于定义我的模型的代码:
from tensorflow.keras import layers
import tensorflow as tf
def make_model(num_classes=8):
# type (int) -> tf.keras.model
"""implementation of SimpleNet in keras"""
model = tf.keras.Sequential()
# conv layers
model.add(layers.ZeroPadding2D(2))
model.add(layers.Conv2D(input_shape=(None, None, 1),
filters=32, kernel_size=5, activation="relu"))
model.add(layers.BatchNormalization())
model.add(layers.ZeroPadding2D(2))
model.add(layers.Conv2D(filters=64, kernel_size=5, activation="relu"))
model.add(layers.Conv2D(filters=128, kernel_size=3, activation="relu"))
model.add(layers.Conv2D(filters=256, kernel_size=3, activation="relu"))
model.add(layers.Conv2D(filters=128, kernel_size=3, activation="relu"))
model.add(layers.GlobalMaxPooling2D())
# dense layers
model.add(layers.Flatten())
model.add(layers.Dense(128, activation="relu"))
model.add(layers.Dropout(0.25))
model.add(layers.Dense(256, activation="relu"))
model.add(layers.Dropout(0.25))
# use sigmoid for multiclass problems
model.add(layers.Dense(num_classes, activation="sigmoid"))
return model
所以从本质上讲,我的问题是,为什么keras still 定义了预期的输入形状,并且有什么方法可以禁用正在发生的这种隐式standardize_input_data
?
答案 0 :(得分:1)
输入形状应该在模型的第一层,但要放在第二层。因此Keras从您的训练数据中得出了一个形状。