Skip to content

Could not find an implementation for LeakyRelu(6) in onnx runtime #587

Open
@SamedHrmn

Description

@SamedHrmn

Hi, I was converting GFPGANv1.3.pth to onnx format. But I got a error when I try to inference.

onnx: 1.17.0
onnxruntime: 1.19.2
torch: 2.4.1+cu121
onnxsim: 0.4.36

import torch
import onnx
import onnxruntime as ort
from onnxsim import simplify

from gfpgan.archs.gfpganv1_clean_arch import GFPGANv1Clean


def convert_static_GFPGANv1Clean_1_3_onnx():
    onnx_path = "./pretrained/GFPGANv1.3.onnx"
    sim_onnx_path = "./pretrained/GFPGANv1.3_sim.onnx"
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    model_path = 'experiments/pretrained_models/GFPGANv1.3.pth'

    inference_model = GFPGANv1Clean(
        out_size=512,
        num_style_feat=512,
        channel_multiplier=2,
        decoder_load_path=None,
        fix_decoder=False,
        num_mlp=8,
        input_is_latent=True,
        different_w=True,
        narrow=1,
        sft_half=True).to(device)
    
    loadnet = torch.load(model_path)
    if 'params_ema' in loadnet:
        keyname = 'params_ema'
    else:
        keyname = 'params'
    #inference_model.load_state_dict(loadnet[keyname], strict=True)
    inference_model.load_state_dict(loadnet[keyname], strict=False)
    
    
    inference_model = inference_model.eval()
    
    img_path = input_image_path
    input_img = cv2.imread(img_path, cv2.IMREAD_COLOR)
    img = cv2.resize(input_img, (512, 512))
    cropped_face_t = img2tensor(img / 255., bgr2rgb=True, float32=True)
    normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
    cropped_face_t = cropped_face_t.unsqueeze(0).to(device)
    
    mat1 = torch.randn(3, 512, 512).cpu()  # moving the tensor to cpu
    mat1 = mat1.unsqueeze(0).to(device)
    return_rgb=False
    
    torch.onnx.export(inference_model,  # model being run
                      (cropped_face_t, return_rgb),  # model input (or a tuple for multiple inputs)
                      onnx_path,  # where to save the model (can be a file or file-like object)
                      export_params=True,  # store the trained parameter weights inside the model file
                      opset_version=11,  # the ONNX version to export the model to
                      do_constant_folding=True,  # whether to execute constant folding for optimization
                      verbose=True,
                      input_names=['input'],  # the model's input names
                      output_names=['out_ab']  # the model's output names
                      )

    print("export GFPGANv1.3 onnx done.")
    onnx_model = onnx.load(onnx_path)
    onnx.checker.check_model(onnx_model)

    model_simp, check = simplify(onnx_model, check_n=3)
    onnx.save(model_simp, sim_onnx_path)
    print("export GFPGANv1.3 onnx sim done.")


convert_static_GFPGANv1Clean_1_3_onnx()

// Inference
import onnxruntime as ort

session = ort.InferenceSession("pretrained/GFPGANv1.3.onnx")

# Prepare input
input_name = session.get_inputs()[0].name
input_data = prepare_input_data(input_image_path)

# Run inference
outputs = session.run(None, {input_name: input_data})

# Process the output
output = post_process_output(outputs)
output_im = Image.fromarray(output)

Error:

[/usr/local/lib/python3.10/dist-packages/onnxruntime/capi/onnxruntime_inference_collection.py](https://localhost:8080/#) in _create_inference_session(self, providers, provider_options, disabled_optimizers)
    489 
    490         # initialize the C++ InferenceSession
--> 491         sess.initialize_session(providers, provider_options, disabled_optimizers)
    492 
    493         self._sess = sess

NotImplemented: [ONNXRuntimeError] : 9 : NOT_IMPLEMENTED : Could not find an implementation for LeakyRelu(6) node with name '/stylegan_decoder/style_conv1/activate/LeakyRelu'
`` `

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions