@@ -45,3 +45,34 @@ def run_mediapipe_solution(solution, inp_size):
45
45
run_tflite_model ("face_detection_short_range" , (128 , 128 ))
46
46
47
47
run_mediapipe_solution (mp .solutions .selfie_segmentation .SelfieSegmentation (model_selection = 0 ), (256 , 256 ))
48
+
49
+ # Save TensorFlow model as TFLite
50
+ def save_tflite_model (model , inp , name ):
51
+ func = model .__call__ .get_concrete_function ()
52
+ converter = tf .lite .TFLiteConverter .from_concrete_functions ([func ], model )
53
+ tflite_model = converter .convert ()
54
+
55
+ with open (f'{ name } .tflite' , 'wb' ) as f :
56
+ f .write (tflite_model )
57
+
58
+ out = model (inp )
59
+
60
+ np .save (f'{ name } _inp.npy' , inp .transpose (0 , 3 , 1 , 2 ))
61
+ np .save (f'{ name } _out_PartitionedCall:0.npy' , np .array (out ).transpose (0 , 3 , 1 , 2 ))
62
+
63
+
64
+ class ReplicateByPack (tf .Module ):
65
+ @tf .function (input_signature = [tf .TensorSpec (shape = [1 , 3 , 3 , 1 ], dtype = tf .float32 )])
66
+ def __call__ (self , x ):
67
+ pack_1 = tf .stack ([x , x ], axis = 3 )
68
+ reshape_1 = tf .reshape (pack_1 , [1 , 3 , 6 , 1 ])
69
+ pack_2 = tf .stack ([reshape_1 , reshape_1 ], axis = 2 )
70
+ reshape_2 = tf .reshape (pack_2 , [1 , 6 , 6 , 1 ])
71
+ scaled = tf .image .resize (reshape_2 , size = (3 , 3 ), method = tf .image .ResizeMethod .NEAREST_NEIGHBOR )
72
+ return scaled + x
73
+
74
+ model = ReplicateByPack ()
75
+ inp = np .random .standard_normal ((1 , 3 , 3 , 1 )).astype (np .float32 )
76
+
77
+ save_tflite_model (model , inp , 'replicate_by_pack' )
78
+
0 commit comments