[tensorflow]如何通过子类化模型类(tf.keras.Model)来包装代码序列



我有代码序列如下。在那里,我试图通过使用tensorflow模型类子类化来包装代码。。然而,我得到以下错误。希望能提供任何帮助来解决这些错误。提前谢谢你

编码序列

input_tensor = Input(shape=(720, 540, 2))
base_model = ResNet50V2(input_tensor=input_tensor, include_top=False, weights=None, classes=4)
x = base_model.output
x = GlobalAveragePooling2D()(x)
predictions = Dense(4, activation= 'sigmoid')(x)
model = Model(inputs = base_model.input, outputs = predictions)

尝试的模型类

class StreoModel(tf.keras.Model):
def __init__(self):
super(StreoModel, self).__init__()
self.dense1 = Dense(4, activation='sigmoid')
def call(self, inputs):
input_tensor = Input(shape=(720, 540, 2))
x = ResNet50V2(input_tensor=input_tensor, include_top=False, weights=None, classes=4)
x= x.output
x = GlobalAveragePooling2D()(x)
predictions = self.dense1(x)
return predictions

错误日志:

TypeError: Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.

我认为问题在于您将数据传递给ResNet50V2的方式。试着像这样定义一个简单的子类化模型:

class StreoModel(tf.keras.Model):
def __init__(self):
super(StreoModel, self).__init__()
self.resnet_v2 = tf.keras.applications.resnet_v2.ResNet50V2(include_top=False, weights=None, classes=4, input_shape=(720, 540, 2))
self.resnet_v2.trainable = True 
x= self.resnet_v2.output
x = tf.keras.layers.GlobalAveragePooling2D()(x)
output = tf.keras.layers.Dense(4, activation='softmax')(x)
self.model = tf.keras.Model(self.resnet_v2.input, output)

注意,我删除了你的输入层,并添加了一个输入形状到ResNet50V2。根据文档,如果include_top=False,您应该指定input_shape。我还将您的输出激活函数更改为softmax,因为您正在处理4个类。

然后使用它:

sm = StreoModel()
sm.model(np.random.random((1, 720, 540, 2)))
# <tf.Tensor: shape=(1, 4), dtype=float32, numpy=array([[0.25427648, 0.25267935, 0.23970276, 0.2533414 ]], dtype=float32)>

如果你想用call方法定义你的模型,那么你可以这样做:

class StreoModel(tf.keras.Model):
def __init__(self):
super(StreoModel, self).__init__()
self.dense = tf.keras.layers.Dense(4, activation='softmax')
self.resnet = tf.keras.applications.resnet_v2.ResNet50V2(include_top=False, weights=None, classes=4, input_shape=(720, 540, 2))
self.pooling = tf.keras.layers.GlobalAveragePooling2D()
def call(self, inputs):
x = self.resnet(inputs)
x = self.pooling(x)
predictions = self.dense(x)
return predictions

并像这样使用:

sm = StreoModel()
sm(np.random.random((1, 720, 540, 2)))
# <tf.Tensor: shape=(1, 4), dtype=float32, numpy=array([[0.25062975, 0.2428435 , 0.25178066, 0.25474608]], dtype=float32)>

最新更新