<jupyter_start><jupyter_text>Load PyTorch modelIn this tutorial, you learn how to load an existing PyTorch model and use it to run a prediction task.We will run the inference in DJL way with [example](https://pytorch.org/hub/pytorch_vision_resnet/) on the pytorch official website. PreparationThis tutorial requires the installation of Java Kernel. For more information on installing the Java Kernel, see the [README](https://github.com/deepjavalibrary/djl/blob/master/jupyter/README.md).<jupyter_code>// %mavenRepo snapshots https://oss.sonatype.org/content/repositories/snapshots/

%maven ai.djl:api:0.14.0
%maven ai.djl.pytorch:pytorch-engine:0.14.0
%maven org.slf4j:slf4j-api:1.7.32
%maven org.slf4j:slf4j-simple:1.7.32
%maven net.java.dev.jna:jna:5.8.0
import java.nio.file.*;
import java.awt.image.*;
import ai.djl.*;
import ai.djl.inference.*;
import ai.djl.modality.*;
import ai.djl.modality.cv.*;
import ai.djl.modality.cv.util.*;
import ai.djl.modality.cv.transform.*;
import ai.djl.modality.cv.translator.*;
import ai.djl.repository.zoo.*;
import ai.djl.translate.*;
import ai.djl.training.util.*;<jupyter_output><empty_output><jupyter_text>Step 1: Prepare your  modelThis tutorial assumes that you have a TorchScript model.DJL only supports the TorchScript format for loading models from PyTorch, so other models will need to be [converted](https://github.com/deepjavalibrary/djl/blob/master/docs/pytorch/how_to_convert_your_model_to_torchscript.md).A TorchScript model includes the model structure and all of the parameters.We will be using a pre-trained `resnet18` model. First, use the `DownloadUtils` to download the model files and save them in the `build/pytorch_models` folder<jupyter_code>DownloadUtils.download("https://djl-ai.s3.amazonaws.com/mlrepo/model/cv/image_classification/ai/djl/pytorch/resnet/0.0.1/traced_resnet18.pt.gz", "build/pytorch_models/resnet18/resnet18.pt", new ProgressBar());<jupyter_output><empty_output><jupyter_text>In order to do image classification, you will also need the synset.txt which stores the classification class labels. We will need the synset containing the Imagenet labels with which resnet18 was originally trained.<jupyter_code>DownloadUtils.download("https://djl-ai.s3.amazonaws.com/mlrepo/model/cv/image_classification/ai/djl/pytorch/synset.txt", "build/pytorch_models/resnet18/synset.txt", new ProgressBar());<jupyter_output><empty_output><jupyter_text>Step 2: Create a TranslatorWe will create a transformation pipeline which maps the transforms shown in the [PyTorch example](https://pytorch.org/hub/pytorch_vision_resnet/).```python...preprocess = transforms.Compose([    transforms.Resize(256),    transforms.CenterCrop(224),    transforms.ToTensor(),    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),])...```Then, we will use this pipeline to create the [`Translator`](https://javadoc.io/static/ai.djl/api/0.14.0/index.html?ai/djl/translate/Translator.html)<jupyter_code>Translator<Image, Classifications> translator = ImageClassificationTranslator.builder()
        .addTransform(new Resize(256))
        .addTransform(new CenterCrop(224, 224))
        .addTransform(new ToTensor())
        .addTransform(new Normalize(
            new float[] {0.485f, 0.456f, 0.406f},
            new float[] {0.229f, 0.224f, 0.225f}))
        .optApplySoftmax(true)
        .build();<jupyter_output><empty_output><jupyter_text>Step 3: Load your modelNext, we add some search criteria to find the resnet18 model and load it. In this case, we need to tell `Criteria` where to locate the model by calling `.optModelPath()` API.<jupyter_code>Criteria<Image, Classifications> criteria = Criteria.builder()
        .setTypes(Image.class, Classifications.class)
        .optModelPath(Paths.get("build/pytorch_models/resnet18"))
        .optOption("mapLocation", "true") // this model requires mapLocation for GPU
        .optTranslator(translator)
        .optProgress(new ProgressBar()).build();

ZooModel model = criteria.loadModel();<jupyter_output><empty_output><jupyter_text>Step 4: Load image for classificationWe will use a sample dog image to run our prediction on.<jupyter_code>var img = ImageFactory.getInstance().fromUrl("https://raw.githubusercontent.com/pytorch/hub/master/images/dog.jpg");
img.getWrappedImage()<jupyter_output><empty_output><jupyter_text>Step 5: Run inferenceLastly, we will need to create a predictor using our model and translator. Once we have a predictor, we simply need to call the predict method on our test image.<jupyter_code>Predictor<Image, Classifications> predictor = model.newPredictor();
Classifications classifications = predictor.predict(img);

classifications<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11848240779991522, 'fraction_numerical': 0.024586689275116574, 'mean_word_length': 5.275265957446808, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 9, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '21953', 'license': 'Apache-2.0', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1519, 'n_words': 367, 'path': 'jupyter/load_pytorch_model.ipynb', 'repo_name': 'docrozza/djl', 'size': '4718'}