tf.reshape和tf.Tensor.get_shape()

tf.reshape(tensor, shape, name=None) 
  • 第1个参数tensor为被调整维度的张量。
  • 第2个参数shape为要调整为的形状。
  • 返回一个shape形状的新tensor

注意shape里最多有一个维度的值可以填写为-1,表示自动计算此维度。

Reshapes a tensor.
Given tensor, this operation returns a tensor that has the same values as tensor with shape shape.
If shape is the special value {[-1], then tensor is flattened and the operation outputs a 1-D tensor with all elements of tensor.
If shape is 1-D or higher, then the operation returns a tensor with shape shape filled with the values of tensor. In this case, the number of elements implied by shape must be the same as the number of elements in tensor.

1 # tensor ‘t’ is [1, 2, 3, 4, 5, 6, 7, 8, 9]
2 # tensor ‘t’ has shape [9]
3 reshape(t, [3, 3]) ==> [[1, 2, 3]
4 [4, 5, 6]
5 [7, 8, 9]]
6
7 # tensor ‘t’ is [[[1, 1], [2, 2]]
8 # [[3, 3], [4, 4]]]
9 # tensor ‘t’ has shape [2, 2]
10 reshape(t, [2, 4]) ==> [[1, 1, 2, 2]
11 [3, 3, 4, 4]]
12
13 # tensor ‘t’ is [[[1, 1, 1],
14 # [2, 2, 2]],
15 # [[3, 3, 3],
16 # [4, 4, 4]],
17 # [[5, 5, 5],
18 # [6, 6, 6]]]
19 # tensor ‘t’ has shape [3, 2, 3]
20 # pass ‘[¡1]’ to flatten ‘t’
21 reshape(t, [¡1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6,
6, 6]

tf.Tensor.get_shape()
这里写图片描述

将.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。如果您的模型包含其他操作,则需要对代码进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值