U-2-Net
U-2-Net copied to clipboard
Convert U2netp.pth to U2netp.mlmodel
Hello Everyone, I am using u2netp.pth for my iOS app and converted to mlmodel. It works as we'll but I need to support arbitrary image input, in other words, any random size image should be inputed and output based on input size. It works as well for square size. but landscape, portrait size doesn't work.
Coremltools provide flexible input size feature to support arbitrary size problem but it doesn't work, just make errors.
I listed code here. net = U2NETP(3,1) net.load_state_dict(torch.load('u2netp.pth', map_location='cpu')) net.cpu() net.eval()
# Create a test input.
example_input = torch.rand(1, 3, default_size, default_size) * 255
input_shape = ct.Shape(shape=(1, 3, ct.RangeDim(1, 320, default=320), ct.RangeDim(1, 320, default=320)))
# Trace and convert the model.
traced_model = torch.jit.trace(net, example_input)
model = ct.convert(traced_model,
inputs=[ct.ImageType(
name="input_1",
shape= input_shape,
scale=1 / 255.0 / 0.226,
bias = (-0.485 / 0.229, -0.456 / 0.224, -0.406 / 0.225),
channel_first = True
)]))
Environment: Python 3.8 Coremltools: 4.1, 5.0 torch: 1.9.0 MacOS: 12.5.1
Error
Traceback (most recent call last):
File "/Volumes/DATA/ConversionToCoreML/main.py", line 112, in
What I need to know, is it possible to support flexible input image type for U2Net when convert cormel model?
Thanks.