提问人:NickTheGreek 提问时间:3/30/2023 更新时间:3/30/2023 访问量:11
更改 CLI 包装器以进行tflite_transfer_converter的问题
Issue with altering the CLI wrapper for tflite_transfer_converter
问:
我正在尝试更改我的模型的训练和推理签名函数,该模型将导出为 .tflite 模型,以便在智能手机上用于设备上的训练。就像现在的模型一样,它的主体是 MobileNetV2,而头部是设备上可训练的部分,只是一个与 softMax 完全连接的层。我正在尝试更改训练和推理功能,以便在我激活全连接层的 softMax 之前,mobileNetV2 的最后 2 层作为 2 个隐藏层再次在头部使用。
这是我的初始化,我试图保存倒数第二层和最后一层:
def __init__(self, learning_rate=0.001):
"""Initializes a transfer learning model instance.
Args:
learning_rate: A learning rate for the optimzer.
"""
self.num_features = NUM_FEATURES
self.num_classes = NUM_CLASSES
# trainable weights and bias for softmax
self.ws = tf.Variable(
tf.zeros((self.num_features, self.num_classes)),
name='ws',
trainable=True)
self.bs = tf.Variable(
tf.zeros((1, self.num_classes)), name='bs', trainable=True)
# base model
self.base = tf.keras.applications.MobileNetV2(
input_shape=(IMG_SIZE, IMG_SIZE, 3),
alpha=1.0,
include_top=False,
weights='imagenet')
# loss function and optimizer
self.loss_fn = tf.keras.losses.CategoricalCrossentropy()
self.optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
# save the last layer of the base model
self.pre_last_layer = self.base.layers[-2].output
self.last_layer = self.base.layers[-1].output
这是我更改的训练函数,我添加了这 2 层。
@tf.function(input_signature=[
tf.TensorSpec([None, NUM_FEATURES], tf.float32),
tf.TensorSpec([None, NUM_CLASSES], tf.float32),
])
def train(self, bottleneck, label):
"""Runs one training step with the given bottleneck features and labels.
Args:
bottleneck: A tensor of bottleneck features generated from the base model.
label: A tensor of class labels for the given batch.
Returns:
Map of the training loss.
"""
with tf.GradientTape() as tape:
x = tf.keras.layers.Reshape((7, 7, 1280))(bottleneck)
x = self.pre_last_layer(x, training = True)
x = self.last_layer(x, training=True)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
logits = tf.matmul(x, self.ws) + self.bs
prediction = tf.nn.softmax(logits)
loss = self.loss_fn(prediction, label)
gradients = tape.gradient(loss, [self.ws, self.bs])
self.optimizer.apply_gradients(zip(gradients, [self.ws, self.bs]))
result = {'loss': loss}
for grad in gradients:
result[grad.name] = grad
return result
就像现在一样,我一直收到 TypeError:'KerasTensor' 对象在我使用的地方不可调用,我找不到修复它的方法。x = self.pre_last_layer(x, training= True)
任何帮助都是值得赞赏的。请注意,我是 tensorflow 和 tensorflow-lite 的新手。干杯!
在网上搜索 TypeError,我发现这是由于您尝试将输入传递给输入张量而引起的,至少据我所知,我没有发现这里发生的情况。
答: 暂无答案
评论