Posenet loads extremely slowly

Viewed 181

I'm building an Android (Android 10) app on Expo and using the Tensorflow Posenet model. The problem is that the model takes over two minutes to load on my phone. My code is below. I commented out most of the other functions so the only part I actually care about is the useEffect function. Over 2 minutes elapse between the appearance of "TF is ready" and "Posenet model loaded" in my console.

My suspicion is that this code isn't actually the issue, because my friend who cloned my repo claims that Posenet loads in a few second for him. However, our phones have similar specs; the difference shouldn't be that drastic. So how can I make Posenet load normally?

import React, { useState, useEffect } from "react";
import { ActivityIndicator, Text, View, ScrollView, StyleSheet, Button, Platform, Dimensions } from "react-native";
import Constants from "expo-constants";

// camera
import { Camera } from "expo-camera";

// tensorflow
import * as tf from "@tensorflow/tfjs";
import * as posenet from "@tensorflow-models/posenet";
import { cameraWithTensors } from "@tensorflow/tfjs-react-native";

export default function App() {

    //Tensorflow and Permissions
    const [posenetModel, setPosenetModel] = useState(null);
    const [frameworkReady, setFrameworkReady] = useState(false);

    const TensorCamera = cameraWithTensors(Camera);
    let requestAnimationFrameId = 0;

    //performance hacks (Platform dependent)
    const textureDims = { width: 1600, height: 1200 };
    const tensorDims = { width: 152, height: 200 };

    const [ctx, setCanvasContext] = useState(null);

    const [debugText, setDebugText] = useState("");


    useEffect(() => {
        if (!frameworkReady) {
            (async () => {

                // check permissions
                const { status } = await Camera.requestPermissionsAsync();
                console.log(`permissions status: ${status}`);

                // we must always wait for the Tensorflow API to be ready before any TF operation...
                await tf.ready();
                console.log("TF is ready");

                // load the mobilenet model and save it in state
                setPosenetModel(await posenet.load());
                console.log("Posenet model loaded");

                setFrameworkReady(true);
            })();
        }
    }, []);


    return (
    
        <View style={styles.container}>
            <View style={styles.body}>
                {/*renderCameraView()*/}
                {/*<Canvas ref={handleCanvas} style={styles.canvas} />*/}
            </View>
            <Text>{debugText}</Text>
        </View>
    );
}
0 Answers
Related