有没有办法将MobileNet的输入维度从(224,224,3)修改为(150,150,3)
有没有办法修改MobileNet的输入维度。每当我将其更改为所需的输入 (150,150,3) 时,它都会引发错误。
import tensorflow_hub as hub
from tensorflow.keras import Sequential
# from tensorflow.keras import Activations
classifier_url
="https://hub.tensorflow.google.cn/google/tf2-
preview/mobilenet_v2/feature_vector/4"
baseModel = hub.KerasLayer(classifier_url,
input_shape=(150,150,3), output_shape=[1280],
name="Mobilenet")
baseModel.trainable = False # freeze mobilenet
weights
myModel =
Sequential(name="Mobilenet_tranferLearning")
myModel.add(baseModel)
myModel.add(Dropout(0.5))
myModel.add(tf.keras.layers.Activation("relu"))
myModel.add(Dense(102))
myModel.add(tf.keras.layers.Activation("softmax"))
myModel.summary()
ValueError:调用层“Mobilenet”(类型 KerasLayer)时遇到异常。
在用户代码中:
File "/usr/local/lib/python3.7/dist-packages/tensorflow_hub/keras_layer.py", line 237, in call *
result = smart_cond.smart_cond(training,
ValueError: Could not find matching concrete function to call loaded from the SavedModel. Got:
Positional arguments (4 total):
* Tensor("inputs:0", shape=(None, 150, 150, 3), dtype=float32)
* False
* False
* 0.99
Keyword arguments: {}
Expected these arguments to match one of the following 4 option(s):
Option 1:
Positional arguments (4 total):
* TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
* True
* False
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Option 2:
Positional arguments (4 total):
* TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
* True
* True
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Option 3:
Positional arguments (4 total):
* TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
* False
* True
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Option 4:
Positional arguments (4 total):
* TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
* False
* False
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
收到的调用参数: 输入= tf.Tensor(形状=(无,150,150,3),dtype = float32) • 训练=False
Is there a way to modify the input dimension of MobileNet. Whenever I change it to my desired input of (150,150,3) it throws an error.
import tensorflow_hub as hub
from tensorflow.keras import Sequential
# from tensorflow.keras import Activations
classifier_url
="https://hub.tensorflow.google.cn/google/tf2-
preview/mobilenet_v2/feature_vector/4"
baseModel = hub.KerasLayer(classifier_url,
input_shape=(150,150,3), output_shape=[1280],
name="Mobilenet")
baseModel.trainable = False # freeze mobilenet
weights
myModel =
Sequential(name="Mobilenet_tranferLearning")
myModel.add(baseModel)
myModel.add(Dropout(0.5))
myModel.add(tf.keras.layers.Activation("relu"))
myModel.add(Dense(102))
myModel.add(tf.keras.layers.Activation("softmax"))
myModel.summary()
ValueError: Exception encountered when calling layer "Mobilenet" (type KerasLayer).
in user code:
File "/usr/local/lib/python3.7/dist-packages/tensorflow_hub/keras_layer.py", line 237, in call *
result = smart_cond.smart_cond(training,
ValueError: Could not find matching concrete function to call loaded from the SavedModel. Got:
Positional arguments (4 total):
* Tensor("inputs:0", shape=(None, 150, 150, 3), dtype=float32)
* False
* False
* 0.99
Keyword arguments: {}
Expected these arguments to match one of the following 4 option(s):
Option 1:
Positional arguments (4 total):
* TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
* True
* False
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Option 2:
Positional arguments (4 total):
* TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
* True
* True
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Option 3:
Positional arguments (4 total):
* TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
* False
* True
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Option 4:
Positional arguments (4 total):
* TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
* False
* False
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Call arguments received:
• inputs=tf.Tensor(shape=(None, 150, 150, 3), dtype=float32)
• training=False
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请查看此处,了解有关使用 MobilenetV2 的信息。
如果设置 include top=False,则不限于 224 X 224。另外,我建议您设置参数池 - 'Max',这样 MobileNet 会输出一个向量,您可以直接将其输入到密集层中。下面是我用于数百个分类任务的代码。通常我使用 EfficientNetB3,但它也适用于 MobileNet。
注意 MobileNet 要求像素在 -1 到 +1 范围内缩放,因此请使用下面的函数来缩放输入图像。另外,我假设您的标签是一种热编码的标签,因此我将损失指定为“categorical_crossentropy”。如果这不是一个热门使用稀疏_分类_交叉熵。
Take a look here for info on using MobilenetV2.
You are not restricted to 224 X 224 if you set include top=False. Also I advise you set the parameter pooling-'Max' That way MobileNet outputs a vector that you can directly feed into a dense layer. Below is the code I have used for 100's of classification tasks. Normally I use EfficientNetB3 but it works well for MobileNet as well.
Note MobileNet requires pixels be scaled within the range -1 to +1 so use the function below to scale your input images. Also I assume your labels are one hot encoded so I specified the loss as 'categorical_crossentropy'. If the are not one hot use sparse_categorical_crossentropy.