pytorch / android-demo-app

PyTorch android examples of usage in applications
1.47k stars 606 forks source link

How to create custom model for the PyTorchDemoApplication?Thanks #46

Open btdan opened 4 years ago

btdan commented 4 years ago

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.

vsay01 commented 4 years ago

@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.

Mijawel commented 4 years ago

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?

anbo724 commented 4 years ago

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?

Mijawel commented 4 years ago

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

Not sure I understand your question?

Siddhijain16 commented 3 years ago

@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

Muratoter commented 3 years ago

@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 `

Narayan2407 commented 3 years ago

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