djl icon indicating copy to clipboard operation
djl copied to clipboard

YOLOv7 using PyTorch Engine: Failed to load PyTorch native library

Open meeeee12 opened this issue 2 years ago • 15 comments

I am trying to use YOLOv7 with PyTorch but I receive the error Failed to load PyTorch native library. I am running this on windows 10 with the following libraries: api-0.21.0.jar commons-compress-1.22.jar pytorch-engine-0.21.0.jar pytorch-jni-1.13.1-0.21.0.jar pytorch-model-zoo-0.21.0.jar pytorch-native-cpu-1.13.1.jar pytorch-native-cpu-1.13.1-win-x86_64.jar slf4j-api-2.0.6.jar slf4j-simple-2.0.6.jar

My code:

public class YoloNet {

    Predictor<Image, DetectedObjects> predictor;

    public YoloNet() {

        int imageSize = 640;
        Pipeline pipeline = new Pipeline();
        pipeline.add(new Resize(imageSize));
        pipeline.add(new ToTensor());

        List<String> synset = new ArrayList<>(0);
        try {
            synset = Files.readAllLines(Paths.get("coco.names"));
        } catch (IOException e) {
            System.err.println("Could not read names file!");
            e.printStackTrace();
        }

        Translator<Image, DetectedObjects> translator =  YoloTranslator
                .builder()
                .setPipeline(pipeline)
                .optSynset(synset)
                .optThreshold(0.4f)
                .addTransform(new Resize(640))
                .build();

        Criteria<Image, DetectedObjects> criteria =
                Criteria.builder()
                        .optApplication(Application.CV.OBJECT_DETECTION)
                        .setTypes(Image.class, DetectedObjects.class)
                        .optModelPath(Paths.get("yolov7.pt"))
                        .optTranslator(translator)
                        .optEngine("PyTorch")
                        .optProgress(new ProgressBar())
                        .build();

        try (ZooModel<Image, DetectedObjects> model = criteria.loadModel()) {
            predictor = model.newPredictor();

        } catch (ModelNotFoundException | MalformedModelException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void detect(Image image) {
        try {
            DetectedObjects detection = predictor.predict(image);
            System.out.println(detection);
        } catch (TranslateException e) {
            throw new RuntimeException(e);
        }
    }
}

criteria.loadModel() crashes with error output:

[main] WARN ai.djl.repository.SimpleRepository - Simple repository pointing to a non-archive file.
Loading:     100% |========================================|
Exception in thread "main" ai.djl.engine.EngineException: Failed to load PyTorch native library
	at ai.djl.pytorch.engine.PtEngine.newInstance(PtEngine.java:84)
	at ai.djl.pytorch.engine.PtEngineProvider.getEngine(PtEngineProvider.java:40)
	at ai.djl.engine.Engine.getEngine(Engine.java:187)
	at ai.djl.Model.newInstance(Model.java:99)
	at ai.djl.repository.zoo.BaseModelLoader.createModel(BaseModelLoader.java:191)
	at ai.djl.repository.zoo.BaseModelLoader.loadModel(BaseModelLoader.java:154)
	at ai.djl.repository.zoo.Criteria.loadModel(Criteria.java:172)
	at YoloNet.<init>(YoloNet.java:61)
	at YOLO.<init>(YOLO.java:105)
	at YOLO.main(YOLO.java:145)
Caused by: java.lang.NoClassDefFoundError: com/sun/jna/Library
	at java.base/java.lang.ClassLoader.defineClass1(Native Method)
	at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
	at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
	at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:800)
	at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:698)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	at ai.djl.util.cuda.CudaUtils.loadLibrary(CudaUtils.java:179)
	at ai.djl.util.cuda.CudaUtils.<clinit>(CudaUtils.java:33)
	at ai.djl.util.Platform.fromSystem(Platform.java:188)
	at ai.djl.util.Platform.fromSystem(Platform.java:148)
	at ai.djl.util.Platform.detectPlatform(Platform.java:68)
	at ai.djl.pytorch.jni.LibUtils.findNativeLibrary(LibUtils.java:274)
	at ai.djl.pytorch.jni.LibUtils.getLibTorch(LibUtils.java:89)
	at ai.djl.pytorch.jni.LibUtils.loadLibrary(LibUtils.java:77)
	at ai.djl.pytorch.engine.PtEngine.newInstance(PtEngine.java:53)
	... 9 more
Caused by: java.lang.ClassNotFoundException: com.sun.jna.Library
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	... 27 more

meeeee12 avatar Mar 15 '23 05:03 meeeee12

You didn't include JNA in your classpath.

I suggest you use a proper build system (e.g. maven or gradle) to build your project.

frankfliu avatar Mar 15 '23 05:03 frankfliu

I have switched to maven using these dependencies:

        <dependency>
            <groupId>ai.djl</groupId>
            <artifactId>api</artifactId>
            <version>0.21.0</version>
        </dependency>

        <dependency>
            <groupId>ai.djl.pytorch</groupId>
            <artifactId>pytorch-engine</artifactId>
            <version>0.21.0</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.6</version>
        </dependency>

Now I get:

Exception in thread "main" ai.djl.engine.EngineException: PytorchStreamReader failed locating file constants.pkl: file not found
	at ai.djl.pytorch.jni.PyTorchLibrary.moduleLoad(Native Method)
	at ai.djl.pytorch.jni.JniUtils.loadModule(JniUtils.java:1661)
	at ai.djl.pytorch.engine.PtModel.load(PtModel.java:92)
	at ai.djl.repository.zoo.BaseModelLoader.loadModel(BaseModelLoader.java:161)
	at ai.djl.repository.zoo.Criteria.loadModel(Criteria.java:172)
	at YoloNet.<init>(YoloNet.java:60)
	at YOLO.<init>(YOLO.java:106)
	at YOLO.main(YOLO.java:146)

meeeee12 avatar Mar 15 '23 06:03 meeeee12

You need use jit trace to trace your model, see: https://docs.djl.ai/master/docs/pytorch/how_to_convert_your_model_to_torchscript.html

For yolo model, you'd better use yolo provided tool to export model to torchscript model.

frankfliu avatar Mar 15 '23 06:03 frankfliu

Can you use yolov5, we have yolov5s in our model zoo (PyTorch and onnx). You can use them directly.

frankfliu avatar Mar 15 '23 06:03 frankfliu

I exported the model to torchscript and criteria.loadModel() is now working but now when I run predictor.predict(image) I get a new exception:

java.lang.RuntimeException: ai.djl.translate.TranslateException: ai.djl.engine.EngineException: The following operation failed in the TorchScript interpreter.
Traceback of TorchScript, serialized code (most recent call last):
  File "code/__torch__/models/yolo.py", line 221, in forward
    model104 = self.model
    _0 = getattr(model104, "0")
    _106 = (_2).forward((_1).forward((_0).forward(x, ), ), )
                                      ~~~~~~~~~~~ <--- HERE
    _107 = (_3).forward(_106, )
    _108 = (_4).forward(_107, )
  File "code/__torch__/models/common.py", line 12, in forward
    act = self.act
    conv = self.conv
    _0 = (act).forward((conv).forward(x, ), )
                        ~~~~~~~~~~~~~ <--- HERE
    return _0
class Concat(Module):
  File "code/__torch__/torch/nn/modules/conv.py", line 12, in forward
    bias = self.bias
    weight = self.weight
    x0 = torch._convolution(x, weight, bias, [1, 1], [1, 1], [1, 1], False, [0, 0], 1, False, False, True, True)
         ~~~~~~~~~~~~~~~~~~ <--- HERE
    return x0

Traceback of TorchScript, original code (most recent call last):
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/conv.py(459): _conv_forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/conv.py(463): forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1182): _slow_forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1194): _call_impl
/content/yolov7/models/common.py(111): fuseforward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1182): _slow_forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1194): _call_impl
/content/yolov7/models/yolo.py(625): forward_once
/content/yolov7/models/yolo.py(599): forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1182): _slow_forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1194): _call_impl
/usr/local/lib/python3.9/dist-packages/torch/jit/_trace.py(976): trace_module
/usr/local/lib/python3.9/dist-packages/torch/jit/_trace.py(759): trace
/content/yolov7/export.py(77): <module>
RuntimeError: Given groups=1, weight of size [32, 3, 3, 3], expected input[1, 640, 640, 640] to have 3 channels, but got 640 channels instead

	at YoloNet.detect(YoloNet.java:94)
	at YOLO$1.drop(YOLO.java:135)
	at java.desktop/sun.awt.dnd.SunDropTargetContextPeer.processDropMessage(SunDropTargetContextPeer.java:547)
	at java.desktop/sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchDropEvent(SunDropTargetContextPeer.java:863)
	at java.desktop/sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchEvent(SunDropTargetContextPeer.java:787)
	at java.desktop/sun.awt.dnd.SunDropTargetEvent.dispatch(SunDropTargetEvent.java:48)
	at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4876)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4843)
	at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
	at java.desktop/java.awt.LightweightDispatcher.processDropTargetEvent(Container.java:4621)
	at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4483)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
	at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4843)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: ai.djl.translate.TranslateException: ai.djl.engine.EngineException: The following operation failed in the TorchScript interpreter.
Traceback of TorchScript, serialized code (most recent call last):
  File "code/__torch__/models/yolo.py", line 221, in forward
    model104 = self.model
    _0 = getattr(model104, "0")
    _106 = (_2).forward((_1).forward((_0).forward(x, ), ), )
                                      ~~~~~~~~~~~ <--- HERE
    _107 = (_3).forward(_106, )
    _108 = (_4).forward(_107, )
  File "code/__torch__/models/common.py", line 12, in forward
    act = self.act
    conv = self.conv
    _0 = (act).forward((conv).forward(x, ), )
                        ~~~~~~~~~~~~~ <--- HERE
    return _0
class Concat(Module):
  File "code/__torch__/torch/nn/modules/conv.py", line 12, in forward
    bias = self.bias
    weight = self.weight
    x0 = torch._convolution(x, weight, bias, [1, 1], [1, 1], [1, 1], False, [0, 0], 1, False, False, True, True)
         ~~~~~~~~~~~~~~~~~~ <--- HERE
    return x0

Traceback of TorchScript, original code (most recent call last):
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/conv.py(459): _conv_forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/conv.py(463): forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1182): _slow_forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1194): _call_impl
/content/yolov7/models/common.py(111): fuseforward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1182): _slow_forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1194): _call_impl
/content/yolov7/models/yolo.py(625): forward_once
/content/yolov7/models/yolo.py(599): forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1182): _slow_forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1194): _call_impl
/usr/local/lib/python3.9/dist-packages/torch/jit/_trace.py(976): trace_module
/usr/local/lib/python3.9/dist-packages/torch/jit/_trace.py(759): trace
/content/yolov7/export.py(77): <module>
RuntimeError: Given groups=1, weight of size [32, 3, 3, 3], expected input[1, 640, 640, 640] to have 3 channels, but got 640 channels instead

	at ai.djl.inference.Predictor.batchPredict(Predictor.java:189)
	at ai.djl.inference.Predictor.predict(Predictor.java:126)
	at YoloNet.detect(YoloNet.java:91)
	... 31 more
Caused by: ai.djl.engine.EngineException: The following operation failed in the TorchScript interpreter.
Traceback of TorchScript, serialized code (most recent call last):
  File "code/__torch__/models/yolo.py", line 221, in forward
    model104 = self.model
    _0 = getattr(model104, "0")
    _106 = (_2).forward((_1).forward((_0).forward(x, ), ), )
                                      ~~~~~~~~~~~ <--- HERE
    _107 = (_3).forward(_106, )
    _108 = (_4).forward(_107, )
  File "code/__torch__/models/common.py", line 12, in forward
    act = self.act
    conv = self.conv
    _0 = (act).forward((conv).forward(x, ), )
                        ~~~~~~~~~~~~~ <--- HERE
    return _0
class Concat(Module):
  File "code/__torch__/torch/nn/modules/conv.py", line 12, in forward
    bias = self.bias
    weight = self.weight
    x0 = torch._convolution(x, weight, bias, [1, 1], [1, 1], [1, 1], False, [0, 0], 1, False, False, True, True)
         ~~~~~~~~~~~~~~~~~~ <--- HERE
    return x0

Traceback of TorchScript, original code (most recent call last):
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/conv.py(459): _conv_forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/conv.py(463): forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1182): _slow_forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1194): _call_impl
/content/yolov7/models/common.py(111): fuseforward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1182): _slow_forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1194): _call_impl
/content/yolov7/models/yolo.py(625): forward_once
/content/yolov7/models/yolo.py(599): forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1182): _slow_forward
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py(1194): _call_impl
/usr/local/lib/python3.9/dist-packages/torch/jit/_trace.py(976): trace_module
/usr/local/lib/python3.9/dist-packages/torch/jit/_trace.py(759): trace
/content/yolov7/export.py(77): <module>
RuntimeError: Given groups=1, weight of size [32, 3, 3, 3], expected input[1, 640, 640, 640] to have 3 channels, but got 640 channels instead

	at ai.djl.pytorch.jni.PyTorchLibrary.moduleRunMethod(Native Method)
	at ai.djl.pytorch.jni.IValueUtils.forward(IValueUtils.java:53)
	at ai.djl.pytorch.engine.PtSymbolBlock.forwardInternal(PtSymbolBlock.java:145)
	at ai.djl.nn.AbstractBaseBlock.forward(AbstractBaseBlock.java:79)
	at ai.djl.nn.Block.forward(Block.java:127)
	at ai.djl.inference.Predictor.predictInternal(Predictor.java:140)
	at ai.djl.inference.Predictor.batchPredict(Predictor.java:180)
	... 33 more

meeeee12 avatar Mar 15 '23 09:03 meeeee12

removing .addTransform(new Resize(640)) from the translator fixed the issue but now I get:

java.lang.RuntimeException: ai.djl.translate.TranslateException: java.lang.IndexOutOfBoundsException: Index 96 out of bounds for length 80
	at YoloNet.detect(YoloNet.java:94)
	at YOLO$1.drop(YOLO.java:135)
	at java.desktop/sun.awt.dnd.SunDropTargetContextPeer.processDropMessage(SunDropTargetContextPeer.java:547)
	at java.desktop/sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchDropEvent(SunDropTargetContextPeer.java:863)
	at java.desktop/sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchEvent(SunDropTargetContextPeer.java:787)
	at java.desktop/sun.awt.dnd.SunDropTargetEvent.dispatch(SunDropTargetEvent.java:48)
	at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4876)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4843)
	at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
	at java.desktop/java.awt.LightweightDispatcher.processDropTargetEvent(Container.java:4621)
	at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4483)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
	at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4843)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: ai.djl.translate.TranslateException: java.lang.IndexOutOfBoundsException: Index 96 out of bounds for length 80
	at ai.djl.inference.Predictor.batchPredict(Predictor.java:189)
	at ai.djl.inference.Predictor.predict(Predictor.java:126)
	at YoloNet.detect(YoloNet.java:91)
	... 31 more
Caused by: java.lang.IndexOutOfBoundsException: Index 96 out of bounds for length 80
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:372)
	at java.base/java.util.ArrayList.get(ArrayList.java:459)
	at ai.djl.modality.cv.translator.YoloTranslator.processOutput(YoloTranslator.java:64)
	at ai.djl.modality.cv.translator.YoloTranslator.processOutput(YoloTranslator.java:28)
	at ai.djl.inference.Predictor.processOutputs(Predictor.java:225)
	at ai.djl.inference.Predictor.batchPredict(Predictor.java:183)
	... 33 more

Looks like it is expecting the synset to have 96 items but the model is the default example and it only has 80 classes.

meeeee12 avatar Mar 15 '23 09:03 meeeee12

When you load the Model, you will need to provide the list of available classes to your translator. You can use the method .optSynset(...) or .optSynsetUrl(...) on the YoloTranslator.Builder to do this

zachgk avatar Mar 15 '23 18:03 zachgk

I'm setting the .optSynset(...), I posted my code in the first comment. I tried re-exporting to torchscript using different export settings and it changed to IndexOutOfBoundsException for 2102 out of 2100, so it appears to have something to do witth my export settings but I dont know what export settings to use.

meeeee12 avatar Mar 16 '23 01:03 meeeee12

`public static DetectedObjects predict() throws IOException { Path imageFile = Paths.get("/Users/iotinall/Desktop/LHQJW/K44464075_1_20230605T121021Z.jpg"); Image img = ImageFactory.getInstance().fromFile(imageFile); Criteria<Image, DetectedObjects> criteria=Criteria.builder() .setTypes(Image.class,DetectedObjects.class) .optModelPath(Paths.get("/Users/iotinall/Desktop/torchmodel")) .optModelName("best.torchscript.pt") .optTranslatorFactory( new YoloTranslatorFactory()) .optEngine("PyTorch") .build();

    try (ZooModel<Image, DetectedObjects> model = criteria.loadModel()) {
        try (Predictor<Image, DetectedObjects> predictor = model.newPredictor()) {
            DetectedObjects detection = predictor.predict(img);
            saveBoundingBoxImage(img, detection);
            return detection;
        } catch (TranslateException e) {
            throw new RuntimeException(e);
        }
    } catch (ModelNotFoundException e) {
        throw new RuntimeException(e);
    } catch (MalformedModelException e) {
        throw new RuntimeException(e);
    }
}`

I use the djl to load the pytorch torchscript model ,the synset.txt content is : image now I run the program ,get the error image

IoTinall avatar Jul 24 '23 11:07 IoTinall

We didn't test YoloTranslator with yolov7, there might some difference in yolov7 output tensor layout.

Do you know what's the model's expected image size?

frankfliu avatar Jul 24 '23 16:07 frankfliu

We didn't test YoloTranslator with yolov7, there might some difference in yolov7 output tensor layout.

Do you know what's the model's expected image size?

When I trained the model, I set the image size to 800 * 800

IoTinall avatar Jul 25 '23 01:07 IoTinall

You didn't set image size in your Translator, the default will be resize to 224 x 224

frankfliu avatar Jul 25 '23 02:07 frankfliu

You didn't set image size in your Translator, the default will be resize to 224 x 224 image After setting the image size, the program now reports an error image

IoTinall avatar Jul 25 '23 04:07 IoTinall

How many classes when you train your model? The number of classes in synset must match to classes in your training

frankfliu avatar Jul 25 '23 07:07 frankfliu

How many classes when you train your model? The number of classes in synset must match to classes in your training

three number of classes

IoTinall avatar Jul 25 '23 07:07 IoTinall

closing old issues. Feel free to open an new issue if you have questions.

frankfliu avatar Jul 11 '24 00:07 frankfliu