Hi, do you succesfully make inference with .fp16?

#1
by treksis - opened

Hi,

I tried to use codeformer .fp16 version with the script that is on the repo that you used to convert the onnx model.

https://github.com/harisreedhar/Face-Upscalers-ONNX

example.py

import os
import cv2
import numpy as np

from GPEN.GPEN import GPEN
from GFPGAN.GFPGAN import GFPGAN
from Codeformer.Codeformer import CodeFormer
from Restoreformer.Restoreformer import RestoreFormer

#codeformer = CodeFormer(model_path="codeformer.onnx", device="cpu")
codeformer = CodeFormer(model_path="codeformer.fp16.onnx", device="cpu")

image_directory = "./test_images"

enhanced_images = []

for filename in os.listdir(image_directory):
    if filename.lower().endswith(('.jpg', '.png')):
        image_path = os.path.join(image_directory, filename)
        img = cv2.imread(image_path)

        hstacked = np.hstack([
            cv2.resize(img, (512,512)),
            codeformer.enhance(img),
        ])

    enhanced_images.append(hstacked)

cv2.imwrite("output2.jpg", np.vstack(enhanced_images))

and I'm getting this error.

onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Unexpected input data type. Actual: (tensor(float)) , expected: (tensor(float16)) 

I tried changing preprocess function into np.float16, still I'm not able to inference it. If you succesfully made inference, would you mind to share the code?

Codeformer/Codeformer.py

def preprocess(self, img, w):
    img = cv2.resize(img, self.resolution, interpolation=cv2.INTER_LINEAR)
    img = img.astype(np.float16)[:,:,::-1] / 255.0  # Convert to float16
    img = img.transpose((2, 0, 1))
    img = (img - 0.5) / 0.5
    img = np.expand_dims(img, axis=0).astype(np.float16)  # Convert to float16
    w = np.array([w], dtype=np.float16)  # Convert to float16
    return img, w

Thank you

Fixed.


def preprocess(self, img, w):
        img = cv2.resize(img, self.resolution, interpolation=cv2.INTER_LINEAR)
        img = img.astype(np.float32)[:,:,::-1] / 255.0
        img = img.transpose((2, 0, 1))
        img = (img - 0.5) / 0.5
        img = np.expand_dims(img, axis=0).astype(np.float16) #only this one to changed.
        w = np.array([w], dtype=np.double)
        return img, w
treksis changed discussion status to closed

Sign up or log in to comment