[英]ValueError: Input 0 is incompatible with layer similarity_model: expected shape=(None, 224, 224, 3), found shape=(None, None, 224, 224, 3)
[英]Tensorflow / Keras ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(32, 224, 3)
我检查了所有其他类似的错误,但没有人工作。 我正在从 keras 中的 re.net50 model 进行迁移学习。这就是我创建 model 的方式:
inputs = keras.Input(shape=input_shape, dtype=tf.float32)
augmentation_layer = Sequential([
layers.RandomFlip(**data_aug_layer["random_flip"]),
layers.RandomRotation(**data_aug_layer["random_rotation"]),
layers.RandomZoom(**data_aug_layer["random_zoom"]),
])
x = augmentation_layer(inputs)
x = preprocess_input(x)
scale_layer = layers.Rescaling(scale=1./255)
x = scale_layer(x)
base_model=ResNet50(
include_top=False,
weights='imagenet',
pooling='avg',
input_shape=input_shape
)
x = base_model(x, training=False)
x = layers.Dropout(dropout_rate)(x)
outputs=layers.Dense(classes, activation='softmax')(x)
model = Model(inputs, outputs)
训练结束后,我保存权重并加载它们,然后再次进行图像预处理:
def norma(arr):
normalization_layer = layers.Rescaling(1./255)
return normalization_layer(arr)
ims=keras.utils.load_img(test_files[0], target_size=(224, 224))
im_arr=keras.utils.img_to_array(ims)
im_arr_preproc=tf.keras.applications.resnet.preprocess_input(im_arr)
im_arr_scaled = norma(im_arr_preproc)
WEIGHTS="/home/app/src/experiments/exp_007/model.01-5.2777.h5"
wg_model = resnet_50.create_model(weights = WEIGHTS)
wg_model.predict(im_arr_scaled)
预测总是失败,并显示“ValueError:层“model_2”的输入 0 与层不兼容:预期形状 =(无,224,224,3),找到形状 =(32,224,3)”
但是我正在检查图像的每一步的形状和大小,并且从不转向 (32, 224, 3)。 不知道错误可能在哪里,任何想法将不胜感激。
这是错误 output:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In [61], line 1
----> 1 cnn_model.predict(im_arr_scaled)
File ~/.local/lib/python3.8/site-packages/keras/utils/traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
File ~/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py:1147, in func_graph_from_py_func.<locals>.autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/home/app/.local/lib/python3.8/site-packages/keras/engine/training.py", line 1801, in predict_function *
return step_function(self, iterator)
File "/home/app/.local/lib/python3.8/site-packages/keras/engine/training.py", line 1790, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
...
File "/home/app/.local/lib/python3.8/site-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "model_2" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(32, 224, 3)
您可能缺少批次维度。 尝试:
wg_model.predict(im_arr_scaled[None, ...])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.