forked from tucan9389/tf2-mobile-2d-single-pose-estimation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_tflite.py
More file actions
62 lines (46 loc) · 2.29 KB
/
evaluate_tflite.py
File metadata and controls
62 lines (46 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Copyright 2019 Doyoung Gwak (tucan.dev@gmail.com)
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ======================
#-*- coding: utf-8 -*-
import numpy as np
import tensorflow as tf
tf.random.set_seed(3)
# tflite_model_path = "/Volumes/tucan-SSD/ml-project/experime
# nt001/ai_challenger/06022331_mv2_hourglass_basic/tflite/mv2_cpm-249000.tflite"
# input_index = 0
# output_index = 3
class TFLiteModel:
def __init__(self, tflite_model_path, input_index=0, output_index=-1):
self.tflite_model_path = tflite_model_path
self.input_index = input_index
self.output_index = output_index
# Load the TFLite model and allocate tensors.
self.interpreter = tf.lite.Interpreter(model_path=tflite_model_path)
self.interpreter.allocate_tensors()
# Get input and output tensors.
self.input_details = self.interpreter.get_input_details()
self.output_details = self.interpreter.get_output_details()
# Test the model on random input data.
self.input_shape = self.input_details[input_index]['shape']
self.output_shape = self.output_details[output_index]['shape']
print("model loaded")
print(self.interpreter.get_input_details())
print("output_details:", self.output_details[output_index]['shape'])
def inference(self, input_data):
# print("input_shape == input_data.shape:", self.input_shape == input_data.shape)
self.interpreter.set_tensor(self.input_details[self.input_index]['index'], input_data)
self.interpreter.invoke()
# The function `get_tensor()` returns a copy of the tensor data.
# Use `tensor()` in order to get a pointer to the tensor.
output_data = self.interpreter.get_tensor(self.output_details[self.output_index]['index'])
return output_data