android-demo-app icon indicating copy to clipboard operation
android-demo-app copied to clipboard

How to create custom model for the PyTorchDemoApplication?Thanks

Open btdan opened this issue 5 years ago • 7 comments

Hi, I want to learn about how to apply pytorch model on andorid platform. And this android-demo-app is very useful to me. The PyTorchDemoApp has already been deployed on my android mobile ,and it can be runned successfully. But I want to know how to create a custom model with my own Image data. When I copy the model.pt from HelloWorldApp, the PyTorchDemoApp crashes and tells me " Sorry There is an error" Can anyone tell me how to create a custom model? Thanks very much.

btdan avatar Dec 20 '19 08:12 btdan

@btdan if you have your own model, then you need to serialized the model.

In your Jupyter notebook, you can run load your model and serialized it using Torch script.

Example:

model = torchvision.models.resnet18(pretrained=True)
model.eval()
example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save("app/src/main/assets/model.pt")

The saved model from the last line can be used in android app.

vsay01 avatar Feb 07 '20 02:02 vsay01

If I am serializing my own model for the NLP activity, I'm not 100% sure how I would trace it?

What kind of example would I need to create?

Mijawel avatar Feb 12 '20 03:02 Mijawel

If I am serializing my own model for the NLP activity, I'm not 100% sure how I would trace it?

What kind of example would I need to create?

Hi, did you find the way to custom NLP activity? How to define the example?

anbo724 avatar Mar 16 '20 12:03 anbo724

Hi, did you find the way to custom NLP activity? How to define the example?

Not sure I understand your question?

Mijawel avatar Mar 23 '20 00:03 Mijawel

@btdan if you have your own model, then you need to serialized the model.

In your Jupyter notebook, you can run load your model and serialized it using Torch script.

Example:

model = torchvision.models.resnet18(pretrained=True)
model.eval()
example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save("app/src/main/assets/model.pt")

The saved model from the last line can be used in android app.

Hi @vsay01 I tried to serialized the model using this script but getting error please tell me what should I do .. Thanks in advance !! image

Siddhijain16 avatar Dec 30 '20 08:12 Siddhijain16

@Siddhijain16 Did you convert custom model for android app? I tried below code but getting different error ; `from detectron2.modeling import build_model import torch import torchvision

model = build_model(cfg) model.eval() example = torch.rand(1, 3, 224, 224) traced_script_module = torch.jit.trace(model, example) traced_script_module.save("drive/Detectron2/final-mobile-model.pt")`

Error ; ` ----> 9 traced_script_module = torch.jit.trace(model, example) /usr/local/lib/python3.6/dist-packages/detectron2/modeling/meta_arch/rcnn.py in (.0) 219 Normalize, pad and batch the input images. 220 """ --> 221 images = [x["image"].to(self.device) for x in batched_inputs] 222 images = [(x - self.pixel_mean) / self.pixel_std for x in images] 223 images = ImageList.from_tensors(images, self.backbone.size_divisibility)

IndexError: too many indices for tensor of dimension 3 `

Muratoter avatar Feb 23 '21 12:02 Muratoter

I already have my trained model .pt file, could anyone help me to deploy it on android? Model- Resnet56 Dataset- CIFAR-10 Platform- PyTorch

I am not clear wrt to serializability of model as mentioned by @vsay01

Narayan2407 avatar Jun 27 '21 18:06 Narayan2407