import tensorflow as tf kmodel = tf.keras.models.load_model('model.h5') kmodel.save('model.kmodle')出现WARNING:absl:Found untraced functions such as _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op while saving (showing 5 of 7). These functions will not be directly callable after loading. INFO:tensorflow:Assets written to: model.kmodle\assets INFO:tensorflow:Assets written to: model.kmodle\assets的警告怎么修改解决
时间: 2023-06-13 12:06:03 浏览: 238
这个警告是因为在保存 Keras 模型时,某些自定义的 Op 操作没有被跟踪到。这将导致在加载模型时,这些 Op 操作将不可直接调用。
你可以尝试使用 `tf.keras.experimental.export_saved_model` 函数来保存模型,例如:
```
import tensorflow as tf
kmodel = tf.keras.models.load_model('model.h5')
tf.keras.experimental.export_saved_model(kmodel, 'model_kmodle')
```
这将使用 TensorFlow SavedModel 格式保存模型,可以避免这个警告。
相关问题
.tflite转k210的.kmodel模型代码
将.tflite模型转换为.kmodel模型的代码如下:
```python
import tensorflow as tf
import numpy as np
from tensorflow.keras import Model
from tensorflow.keras.layers import Input
# 加载tflite模型
interpreter = tf.lite.Interpreter(model_path="model.tflite")
interpreter.allocate_tensors()
# 获取输入输出张量的索引
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# 创建Keras模型
inputs = Input(shape=input_details[0]['shape'][1:])
x = tf.keras.layers.Lambda(lambda x: x / 255.)(inputs) # 对输入进行归一化
for i, layer in enumerate(interpreter.get_tensor_details()):
if i < 2:
continue
op = layer['op']
name = layer['name']
shape = layer['shape']
weights = interpreter.get_tensor(layer['index'])
if op == 'CONV_2D':
x = tf.keras.layers.Conv2D(
filters=shape[-1],
kernel_size=shape[1:3],
strides=layer['stride'][1:3],
padding='same',
use_bias=layer['quantization_parameters']['quantized_dimension'] == 3,
kernel_initializer=tf.keras.initializers.Constant(weights[0]),
bias_initializer=tf.keras.initializers.Constant(weights[1])
)(x)
elif op == 'DEPTHWISE_CONV_2D':
x = tf.keras.layers.DepthwiseConv2D(
kernel_size=shape[1:3],
strides=layer['stride'][1:3],
padding='same',
use_bias=layer['quantization_parameters']['quantized_dimension'] == 3,
depthwise_initializer=tf.keras.initializers.Constant(weights[0]),
bias_initializer=tf.keras.initializers.Constant(weights[1])
)(x)
elif op == 'AVERAGE_POOL_2D':
x = tf.keras.layers.AveragePooling2D(
pool_size=shape[1:3],
strides=layer['stride'][1:3],
padding='same'
)(x)
elif op == 'MAX_POOL_2D':
x = tf.keras.layers.MaxPooling2D(
pool_size=shape[1:3],
strides=layer['stride'][1:3],
padding='same'
)(x)
elif op == 'ADD':
x = tf.keras.layers.Add()([x, tf.keras.layers.Lambda(lambda y: y[..., 0])(x)])
elif op == 'RELU':
x = tf.keras.layers.ReLU()(x)
elif op == 'RESHAPE':
x = tf.keras.layers.Reshape(target_shape=shape[1:])(x)
elif op == 'FULLY_CONNECTED':
x = tf.keras.layers.Dense(
units=shape[-1],
use_bias=True,
kernel_initializer=tf.keras.initializers.Constant(weights[0]),
bias_initializer=tf.keras.initializers.Constant(weights[1])
)(x)
elif op == 'SOFTMAX':
x = tf.keras.layers.Softmax()(x)
outputs = x
keras_model = Model(inputs, outputs)
# 将Keras模型转换为K210的.kmodel模型
import tensorflow.keras.backend as K
from tensorflow.keras.models import model_from_json
# 保存Keras模型的权重
keras_model.save_weights('weights.h5')
# 保存Keras模型的结构
keras_model_json = keras_model.to_json()
with open('model.json', 'w') as f:
f.write(keras_model_json)
# 读取Keras模型的结构
with open('model.json', 'r') as f:
keras_model_json = f.read()
# 将Keras模型的结构转换为K210的.kmodel模型
k210_model = model_from_json(keras_model_json)
k210_model.load_weights('weights.h5')
# 保存K210的.kmodel模型
k210_model.save('model.kmodel')
```
注意:此代码只适用于具有以下操作的.tflite模型:CONV_2D,DEPTHWISE_CONV_2D,AVERAGE_POOL_2D,MAX_POOL_2D,ADD,RELU,RESHAPE,FULLY_CONNECTED和SOFTMAX。如果您的模型包含其他操作,则需要对代码进行修改。
阅读全文