繁体   English   中英

Tensorflow / Keras ValueError:层“模型”的输入 0 与层不兼容:预期形状=(无,224,224,3),发现形状=(32,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.

相关问题 ValueError: Input 0 is incompatible with layer similar_model: expected shape=(None, 224, 224, 3), found shape=(None, None, 224, 224, 3) ValueError: Input 0 is in compatible with layer vggface_resnet50: expected shape=(None, 224, 224, 3), found shape=(None, 1, 224, 224, 3) ValueError: 图层“model_1”的输入 0 与图层不兼容:预期形状=(None, 224, 224, 3),发现形状=(None, 290, 290, 3) ValueError:层“model_10”的输入 0 与层不兼容:预期形状 =(None, 244, 244, 3),找到的形状 =(None, 224, 224, 3) 我收到错误,例如“图层“model_5”的输入 1 与图层不兼容:预期形状 =(无,224、224、3),找到形状 =(无,5) ValueError:层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3 [None, 224, 224, 1] 输入 0 与层 functional_3 不兼容:预期 shape=(None, 224, 224, 3),发现 shape=(None, 240, 240, 3) ValueError:层 sequential_16 的输入 0 与层不兼容:预期 ndim=5,发现 ndim=4。 收到的完整形状:[无,224、224、3] ValueError:层 sequential_3 的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 已收到完整形状:(无、224、256) ValueError:layersequence_4 的输入 0 与 layer 不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(32、224、3)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM