fbelderink / flutter_pytorch_mobile

A flutter plugin for pytorch model inference. Supports image models as well as custom models.
https://pub.dev/packages/pytorch_mobile
Other
101 stars 52 forks source link

Get variable is not initialised error #31

Open Heidar-An opened 1 year ago

Heidar-An commented 1 year ago

I'm trying to use your flutter package for a custom model but I get that my model is not initialised. Is this a regular occurrence?

Let me show you some of my code:

import 'package:pytorch_mobile/pytorch_mobile.dart';
import 'package:pytorch_mobile/model.dart';

I've put pytorch_mobile in my dependencies and added my assets:

  assets:
    - assets/models/[fine-tuning_0.pt](http://fine-tuning_0.pt/)
    - assets/models/

I put both the actual asset and the directory because it wasn't working, so i just put both, I've tried the combinations and it doesn't work.

@override
  void initState() {
       loadPytorchModel().whenComplete(() => log("THIS IS DONE"));

Future<void> loadPytorchModel() async {
    mobileNetModel =
        await PyTorchMobile.loadModel('assets/models/[fine-tuning_0.pt](http://fine-tuning_0.pt/)');
  }

 Future<void> takePicture() async {
    image = ....
    // Ensure that model has been initialised
    log("this works...");
    if (mobileNetModel == null) {
      await loadPytorchModel();
    }
    List? prediction = await mobileNetModel!
        .getImagePredictionList(image, decodedImage.width, decodedImage.height);

If I put the if statement checking if the model is null, then nothing happens, if I remove it, I get "mobileNetModel" is uninitialised.