question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Simple custom model is not working on android

See original GitHub issue

Tutorial Select

Prepare Custom Model

Feedback

Hello, always thanks for contributions! This is somewhat custom problem i think, so that i don’t know that i can question. My simple tensor manipulation model is not working on my react-native app, but it is working successfully at my python code.

My adb logcat of several code trials sometimes got output memory consumptions problem or my java script code sometimes got output [Error: Exception in HostFunction: vector] or std::bad_alloc or just app crashes.

Can i get some help?..

Model export at python code:

import torch, torchvision
import os
from typing import List, Dict
import cv2
import torchvision.transforms.functional as F

class PostCD(torch.nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, inputs: torch.Tensor, iouThreshold: float):
        #shape e.g., "shape": [1, 25200, 16]
        
        inputs = inputs[inputs[:,:,4] > iouThreshold]
        max_class_tensor = torch.transpose(torch.argmax(inputs[:,5:], dim=1).unsqueeze(0),0,1)
        outputs = torch.cat((inputs[:,:5], max_class_tensor), 1).unsqueeze(0)
        return outputs

    
pcd = PostCD()
scripted_model = torch.jit.script(pcd)

data = [[247.37535095214844, 525.0855712890625, 129.0428924560547, 31.98747444152832, 0.8668637275695801, 0.0027807741425931454, 0.002542165108025074, 0.00628146156668663, 0.0026534167118370533, 0.002410427201539278, 0.0035515064373612404, 0.00348650268279016, 0.006360366474837065, 0.002872081473469734, 0.0036974542308598757, 0.9905574321746826],
        [247.37535095214844, 525.0855712890625, 129.0428924560547, 31.98747444152832, 0.8668637275695801, 0.9027807741425931454, 0.002542165108025074, 0.00628146156668663, 0.0026534167118370533, 0.002410427201539278, 0.0035515064373612404, 0.00348650268279016, 0.006360366474837065, 0.002872081473469734, 0.0036974542308598757, 0.005574321746826],
        [247.37535095214844, 525.0855712890625, 129.0428924560547, 31.98747444152832, 0.1, 0.0027807741425931454, 0.002542165108025074, 0.00628146156668663, 0.0026534167118370533, 0.002410427201539278, 0.0035515064373612404, 0.00348650268279016, 0.006360366474837065, 0.002872081473469734, 0.0036974542308598757, 0.9905574321746826],
        [247.37535095214844, 525.0855712890625, 129.0428924560547, 31.98747444152832, 0.1, 0.0027807741425931454, 0.9, 0.00628146156668663, 0.0026534167118370533, 0.002410427201539278, 0.0035515064373612404, 0.00348650268279016, 0.006360366474837065, 0.002872081473469734, 0.0036974542308598757, 0.0004000],
        [247.37535095214844, 525.0855712890625, 129.0428924560547, 31.98747444152832, 0.1, 0.0027807741425931454, 0.9, 0.00628146156668663, 0.0026534167118370533, 0.002410427201539278, 0.0035515064373612404, 0.00348650268279016, 0.006360366474837065, 0.002872081473469734, 0.0036974542308598757, 0.0004000],
        [247.37535095214844, 525.0855712890625, 129.0428924560547, 31.98747444152832, 0.1, 0.0027807741425931454, 0.9, 0.00628146156668663, 0.0026534167118370533, 0.002410427201539278, 0.0035515064373612404, 0.00348650268279016, 0.006360366474837065, 0.002872081473469734, 0.0036974542308598757, 0.0004000],
        ]
x_data = torch.tensor(data).unsqueeze(0)

print(x_data)
print(x_data.shape, end='\n\n')
    
outputs = scripted_model(x_data, 0.3)

print(outputs)
print(outputs.shape)

scripted_model.save("post_cd.pt")

import torch
from torch.utils.mobile_optimizer import optimize_for_mobile

optimized_scripted_module=optimize_for_mobile(scripted_model)
optimized_scripted_module._save_for_lite_interpreter("post_cd.ptl")

Python output:

tensor([[[2.4738e+02, 5.2509e+02, 1.2904e+02, 3.1987e+01, 8.6686e-01,
          2.7808e-03, 2.5422e-03, 6.2815e-03, 2.6534e-03, 2.4104e-03,
          3.5515e-03, 3.4865e-03, 6.3604e-03, 2.8721e-03, 3.6975e-03,
          9.9056e-01],
         [2.4738e+02, 5.2509e+02, 1.2904e+02, 3.1987e+01, 8.6686e-01,
          9.0278e-01, 2.5422e-03, 6.2815e-03, 2.6534e-03, 2.4104e-03,
          3.5515e-03, 3.4865e-03, 6.3604e-03, 2.8721e-03, 3.6975e-03,
          5.5743e-03],
         [2.4738e+02, 5.2509e+02, 1.2904e+02, 3.1987e+01, 1.0000e-01,
          2.7808e-03, 2.5422e-03, 6.2815e-03, 2.6534e-03, 2.4104e-03,
          3.5515e-03, 3.4865e-03, 6.3604e-03, 2.8721e-03, 3.6975e-03,
          9.9056e-01],
         [2.4738e+02, 5.2509e+02, 1.2904e+02, 3.1987e+01, 1.0000e-01,
          2.7808e-03, 9.0000e-01, 6.2815e-03, 2.6534e-03, 2.4104e-03,
          3.5515e-03, 3.4865e-03, 6.3604e-03, 2.8721e-03, 3.6975e-03,
          4.0000e-04],
         [2.4738e+02, 5.2509e+02, 1.2904e+02, 3.1987e+01, 1.0000e-01,
          2.7808e-03, 9.0000e-01, 6.2815e-03, 2.6534e-03, 2.4104e-03,
          3.5515e-03, 3.4865e-03, 6.3604e-03, 2.8721e-03, 3.6975e-03,
          4.0000e-04],
         [2.4738e+02, 5.2509e+02, 1.2904e+02, 3.1987e+01, 1.0000e-01,
          2.7808e-03, 9.0000e-01, 6.2815e-03, 2.6534e-03, 2.4104e-03,
          3.5515e-03, 3.4865e-03, 6.3604e-03, 2.8721e-03, 3.6975e-03,
          4.0000e-04]]])
torch.Size([1, 6, 16])

tensor([[[247.3754, 525.0856, 129.0429,  31.9875,   0.8669,  10.0000],
         [247.3754, 525.0856, 129.0429,  31.9875,   0.8669,   0.0000]]])
torch.Size([1, 2, 6])

This is app code:

const MODEL_URL = localPath;
let pcd_model = null;

async function testPCD(){
    if (pcd_model == null) {
        const filePath = await MobileModel.download(require(MODEL_URL));
        pcd_model = await torch.jit._loadForMobile(filePath);
        console.log('Model successfully loaded');
    }


    var data = [[247.37535095214844, 525.0855712890625, 129.0428924560547, 31.98747444152832, 0.8668637275695801, 0.0027807741425931454, 0.002542165108025074, 0.00628146156668663, 0.0026534167118370533, 0.002410427201539278, 0.0035515064373612404, 0.00348650268279016, 0.006360366474837065, 0.002872081473469734, 0.0036974542308598757, 0.9905574321746826],
                [247.37535095214844, 525.0855712890625, 129.0428924560547, 31.98747444152832, 0.8668637275695801, 0.9027807741425931454, 0.002542165108025074, 0.00628146156668663, 0.0026534167118370533, 0.002410427201539278, 0.0035515064373612404, 0.00348650268279016, 0.006360366474837065, 0.002872081473469734, 0.0036974542308598757, 0.005574321746826],
                [247.37535095214844, 525.0855712890625, 129.0428924560547, 31.98747444152832, 0.1, 0.0027807741425931454, 0.002542165108025074, 0.00628146156668663, 0.0026534167118370533, 0.002410427201539278, 0.0035515064373612404, 0.00348650268279016, 0.006360366474837065, 0.002872081473469734, 0.0036974542308598757, 0.9905574321746826],
                [247.37535095214844, 525.0855712890625, 129.0428924560547, 31.98747444152832, 0.1, 0.0027807741425931454, 0.9, 0.00628146156668663, 0.0026534167118370533, 0.002410427201539278, 0.0035515064373612404, 0.00348650268279016, 0.006360366474837065, 0.002872081473469734, 0.0036974542308598757, 0.0004000],
                [247.37535095214844, 525.0855712890625, 129.0428924560547, 31.98747444152832, 0.1, 0.0027807741425931454, 0.9, 0.00628146156668663, 0.0026534167118370533, 0.002410427201539278, 0.0035515064373612404, 0.00348650268279016, 0.006360366474837065, 0.002872081473469734, 0.0036974542308598757, 0.0004000],
                [247.37535095214844, 525.0855712890625, 129.0428924560547, 31.98747444152832, 0.1, 0.0027807741425931454, 0.9, 0.00628146156668663, 0.0026534167118370533, 0.002410427201539278, 0.0035515064373612404, 0.00348650268279016, 0.006360366474837065, 0.002872081473469734, 0.0036974542308598757, 0.0004000],
                ]
    var x_data = torch.tensor(data).unsqueeze(0);
    console.log(x_data);
    console.log(x_data.shape);

    try{
        const startInferencTime = global.performance.now();

        const outputs = await pcd_model.forward(x_data, 0.3);

        console.log(outputs);
        console.log(outputs.shape);

        const inferenceTime = global.performance.now() - startInferencTime;
        console.log(`inference time ${inferenceTime.toFixed(3)} ms`);
    }
    catch(err){
        console.log(err);
    }
}

Node.js output:

 LOG  Model successfully loaded
 LOG  {"abs": [Function abs], "add": [Function add], "argmax": [Function argmax], "argmin": [Function argmin], "clamp": [Function clamp], "contiguous": [Function contiguous], "data": [Function data], "div": [Function div], "dtype": "float32", "expand": [Function expand], "flip": [Function flip], "item": [Function item], "mul": [Function mul], "permute": [Function permute], "reshape": [Function reshape], "shape": [1, 6, 16], "size": [Function size], "softmax": [Function softmax], "sqrt": [Function sqrt], "squeeze": [Function squeeze], "stride": [Function stride], "sub": [Function sub], "sum": [Function sum], "to": [Function to], "toString": [Function toString], "topk": [Function topk], "unsqueeze": [Function unsqueeze]}
 LOG  [1, 6, 16]

-> App crashes

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nh9kcommented, Nov 29, 2022

@raedle, I made a mistake. My PyTorch version was 1.7.1. !conda list command in jupyter notebook did not work, so I used conda list instead. PyTorch version 1.10.0, 1.11.0, and 1.12.1 were working fine on android! Thank you so much!

0reactions
raedlecommented, Nov 28, 2022

@nh9k an older PyTorch version could indeed be the cause. Can you try PyTorch 1.12.1 and report back? Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot upload custom model from Firebase for android app
According to spec for working with ML on android devices I have uploaded my custom model on https://console.firebase.google.com/project and ...
Read more >
Android build failed using custom tensorflow model. Conflict ...
Right, the problem is that the model expects a buffer of shape 300x300x3, but you're feeding it an image buffer of shape 224x224x3....
Read more >
Tflite custom model not working : r/FTC - Reddit
Tflite custom model not working. I created a tflite model, but when I run it through code, the camera stream goes black and...
Read more >
Known issues with Android Studio and Android Gradle Plugin
Find out about current known issues with Android Studio and the Android Gradle ... To work around this issue, we recommend commenting out...
Read more >
Detect, track and classify objects with a custom classification ...
The model is not part your APK. It is hosted by uploading to Firebase Machine Learning. The model is available immediately, even when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found