From b165ab4633326ce876952b1da07ed3f18feba5e1 Mon Sep 17 00:00:00 2001 From: "michael.divia" <michael.divia@etu.hesge.ch> Date: Wed, 9 Apr 2025 17:58:58 +0200 Subject: [PATCH] Added Dynamic size --- python/convert_onnx.py | 8 ++++++-- python/pokedex_rpi.py | 4 +++- python/pokedex_test.py | 4 +++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/python/convert_onnx.py b/python/convert_onnx.py index 5cc6061..846a0b8 100644 --- a/python/convert_onnx.py +++ b/python/convert_onnx.py @@ -11,15 +11,19 @@ args = parser.parse_args() if args.model == "1": h5_path = "../models/ResNet50/pokedex_ResNet50.h5" onnx_path = "../models/ResNet50/pokedex_ResNet50.onnx" + size=(224, 224, 3) + size_2=(1, 224, 224, 3) elif args.model == "2": h5_path = "../models/Xception/pokedex_Xception.h5" onnx_path = "../models/ResNet50/pokedex_Xception.onnx" + size=(256, 256, 3) + size_2=(1, 256, 256, 3) # --- Load Sequential model --- seq_model = tf.keras.models.load_model(h5_path, compile=False) # --- Create input layer with same shape --- -inputs = tf.keras.Input(shape=(224, 224, 3), name="input") +inputs = tf.keras.Input(shape=size, name="input") # --- Call the Sequential model as a function --- outputs = seq_model(inputs) @@ -28,7 +32,7 @@ outputs = seq_model(inputs) model = tf.keras.Model(inputs=inputs, outputs=outputs) # --- Convert to ONNX --- -spec = (tf.TensorSpec((1, 224, 224, 3), tf.float32, name="input"),) +spec = (tf.TensorSpec(size_2, tf.float32, name="input"),) onnx_model, _ = tf2onnx.convert.from_keras( model, input_signature=spec, diff --git a/python/pokedex_rpi.py b/python/pokedex_rpi.py index 3f197ca..258de2e 100644 --- a/python/pokedex_rpi.py +++ b/python/pokedex_rpi.py @@ -13,9 +13,11 @@ args = parser.parse_args() if args.model == "1": hef_path = "../models/ResNet50/pokedex_ResNet50.hef" json_path = "../models/ResNet50/class_names.json" + size=(224,224) elif args.model == "2": hef_path = "../models/Xception/pokedex_Xception.hef" json_path = "../models/Xception/class_names.json" + size=(256,256) # Load class names with open(json_path, "r") as f: @@ -42,7 +44,7 @@ if not ret: exit() # --- Preprocess image --- -image = cv2.resize(frame, (224, 224)) +image = cv2.resize(frame, size) image = image.astype(np.float32) / 255.0 # Normalize to [0, 1] image = np.expand_dims(image, axis=0) # Add batch dimension image = np.transpose(image, (0, 3, 1, 2)) # NHWC ? NCHW if required (check your model) diff --git a/python/pokedex_test.py b/python/pokedex_test.py index 4d3e1b8..bccf30f 100644 --- a/python/pokedex_test.py +++ b/python/pokedex_test.py @@ -16,9 +16,11 @@ args = parser.parse_args() if args.model == "1": h5_path = "../models/ResNet50/pokedex_ResNet50.h5" json_path = "../models/ResNet50/class_names.json" + size = (224,224) elif args.model == "2": h5_path = "../models/Xception/pokedex_Xception.h5" json_path = "../models/Xception/class_names.json" + size = (256,256) # --- Load class names from JSON --- with open(json_path, "r") as f: @@ -45,7 +47,7 @@ for i in range(4): img_path = os.path.join(class_folder, random_image) # --- Load & Preprocess Image --- - img = keras.utils.load_img(img_path, target_size=(224, 224)) # resize to match model input + img = keras.utils.load_img(img_path, target_size=size) # resize to match model input img_array = keras.utils.img_to_array(img) img_array = img_array / 255.0 # normalize if your model expects it img_array = tf.expand_dims(img_array, 0) -- GitLab