Hi erik !
I'm stuck with an error when running the .blob I've generated. The code is shown below:
from pathlib import Path
import torch
from torch import nn
import kornia
import onnx
from onnxsim import simplify
import blobconverter
name = 'concat'
class Model(nn.Module):
def forward(self, img1, img2):
img1bw = kornia.color.rgb_to_grayscale(img1, rgb_weights=torch.tensor([0.299, 0.587, 0.114]))
img2bw = kornia.color.rgb_to_grayscale(img2, rgb_weights=torch.tensor([0.299, 0.587, 0.114]))
# Global Parameters - Screening
band = 20 # Band of height to be analyzed for the images (# of pixels)
end = img1bw.shape[2] - band
val = 0
rg = range(0, end, 1) # Range for y0 - starting point for image being screened
diff = []
# Band of reference
ref_gray = torch.narrow(img2bw, 2, 0, band)
for y0 in rg:
# Band of comparison
gray = torch.narrow(img1bw, 2, y0, band)
# Absolute difference between bands
diff.append( torch.mean(torch.abs(torch.sub(gray, ref_gray))) )
val = rg[diff.index(min(diff))] # Cut height
if val == 0:
val = 50
img1cut = torch.narrow(img1, 2, 0, val)
imgfin = torch.cat((img1cut, img2), 2)
return imgfin
# Define the expected input shape (dummy input)
shape = (1, 3, 300, 300)
model = Model()
X = torch.ones(shape, dtype=torch.float32)
path = Path("out/")
path.mkdir(parents=True, exist_ok=True)
onnx_path = str(path / (name + '.onnx'))
print(f"Writing to {onnx_path}")
torch.onnx.export(
model,
(X, X),
onnx_path,
opset_version=12,
input_names = ['img1', 'img2'],
do_constant_folding=True,
)
onnx_simplified_path = str(path / (name + '_simplified.onnx'))
# Use onnx-simplifier to simplify the onnx model
onnx_model = onnx.load(onnx_path)
model_simp, check = simplify(onnx_model)
onnx.save(model_simp, onnx_simplified_path)
# Use blobconverter to convert onnx->IR->blob
blobconverter.from_onnx(
model=onnx_simplified_path,
data_type="FP16",
shaves=6,
use_cache=False,
output_dir=path,
optimizer_params=[]
)
The 'val' variable should never be 0, but if I dont include the if val == 0: val = 50 an error is shown and the .blob is not generated. When running it, I'm always finding val to be 0 and then it is assuming the 50 value.
Any ideas on why I'm always getting a 0 when running the rg[diff.index(min(diff))] function? Maybe something to do with the FP16 limitation?
Thank you in advance!