agrobot_base/Python/OAK/datasets/_10_convert_fastscnn.py

78 lines
2.6 KiB
Python
Raw Normal View History

2025-08-08 20:09:17 +00:00
import json
2025-08-07 18:23:53 +00:00
import os
import torch
2025-08-11 23:11:23 +00:00
from fast_scnn import FastSCNN, FastSCNNWithNorm
2025-08-07 18:23:53 +00:00
from utils import carregar_labelmap_completo
# ⚙️ Configurações
2025-08-08 20:09:17 +00:00
with open("config.json", "r") as f:
config = json.load(f)
MODELO = config["camera"]
MODEL_NAME = config["model_name"]
RESOLUCAO = config["resolucao"]
2025-08-11 23:11:23 +00:00
MAIN_CLASS_NAME = config["main_class_name"]
N_SHAVES = config["shaves"]
2025-09-15 10:23:07 +00:00
model_to_use = config["model_to_use"]
2025-08-08 20:09:17 +00:00
model_path = os.path.join(MODELO, "backup", config["modelo"], MODEL_NAME)
2025-08-07 18:23:53 +00:00
labelmap_path = os.path.join(MODELO, "dataset", "labelmap.txt")
2025-09-15 10:23:07 +00:00
model_name = ""
if model_to_use == "geral":
model_name = f"{MODEL_NAME}_best.pth"
elif model_to_use == "main_class":
MAIN_CLASS_NAME = config["main_class_name"]
model_name = f"{MODEL_NAME}_best_f1_{MAIN_CLASS_NAME}.pth"
elif model_to_use == "es":
ES_CLASSES_NAME = config["es_classes"]
model_name = f"{MODEL_NAME}_best_es_{ES_CLASSES_NAME}.pth"
else:
model_name = f"{MODEL_NAME}_best.pth"
2025-08-07 18:23:53 +00:00
2025-08-08 20:09:17 +00:00
dummy_input = torch.randn(1, 3, RESOLUCAO[1], RESOLUCAO[0]) # (batch, channels, height, width)
2025-08-07 18:23:53 +00:00
_, _, classes, _ = carregar_labelmap_completo(labelmap_path)
NUM_CLASSES = len(classes)
2025-08-11 23:11:23 +00:00
base = FastSCNNWithNorm(num_classes=NUM_CLASSES, to_rgb=True) # ajuste num_classes conforme seu labelmap
2025-09-15 10:23:07 +00:00
base.backbone.load_state_dict(torch.load(os.path.join(model_path, model_name), map_location="cpu"))
2025-08-11 23:11:23 +00:00
base.eval()
2025-08-07 18:23:53 +00:00
torch.onnx.export(
2025-08-11 23:11:23 +00:00
base,
2025-08-07 18:23:53 +00:00
dummy_input,
os.path.join(model_path, model_name + ".onnx"),
input_names=["input"],
output_names=["output"],
opset_version=11,
dynamic_axes=None
)
print(f"Modelo exportado para {os.path.join(model_path, model_name + '.onnx')} com sucesso!")
from openvino.tools.mo import convert_model
from openvino.runtime import serialize
ov_model = convert_model(
input_model=os.path.join(model_path, model_name + ".onnx"),
2025-08-08 20:09:17 +00:00
input_shape=[1, 3, RESOLUCAO[1], RESOLUCAO[0]],
layout="NCHW",
2025-08-07 18:23:53 +00:00
)
serialize(
ov_model,
os.path.join(model_path, model_name + ".xml"),
os.path.join(model_path, model_name + ".bin")
)
print("Conversão para IR concluída e arquivos salvos!")
import blobconverter
blob_path = blobconverter.from_openvino(
xml=os.path.join(model_path, model_name + ".xml"),
bin=os.path.join(model_path, model_name + ".bin"),
data_type="FP16",
shaves=N_SHAVES,
2025-08-08 20:09:17 +00:00
output_dir=model_path,
2025-08-11 23:11:23 +00:00
#compile_params=[
# "-ip U8", # entrada em bytes; compila a conversão interna p/ FP16
#"--mean_values=[123.675,116.28,103.53]",
#"--scale_values=[58.395,57.12,57.375]",
2025-08-08 20:09:17 +00:00
#"--reverse_input_channels" # pq você treinou em RGB
2025-08-11 23:11:23 +00:00
#],
2025-08-07 18:23:53 +00:00
)
print(f"Blob salvo em: {blob_path}")