nsfw_model icon indicating copy to clipboard operation
nsfw_model copied to clipboard

Error

Open mady143 opened this issue 4 years ago • 6 comments

Hi @GantMan @jessetrana @txyugood @TechnikEmpire @sickerin
I had converted my hdf5 model to tflite model and imlemented in my flutter appliaction and i am getting an error like
Caused by: java.lang.IllegalArgumentException: Cannot copy between a TensorFlowLite tensor with shape [1, 38] and a Java object with shape [1, 40] could you help me to resolve this

Thanks and Regards, Manikantha Sekhar.

mady143 avatar Jul 06 '20 08:07 mady143

@mady143 My best guess is that you've changed dimensions somewhere and shouldn't have.

You've either modified:

  1. Input image size.
  2. Input image number of channels.
  3. Input batch size.
  4. Output size.

But this is just a guess. You're going to need to provide a lot more details to get any help, like how you converted, your original model, the code you used to train and convert, etc.

Without more information, IMO this is not an issue with this repo but rather a common issue in training and converting models of any kind.

TechnikEmpire avatar Jul 06 '20 08:07 TechnikEmpire

I think @TechnikEmpire brought up some good points. @mady143 can you provide more details around this?

wingman-jr-addon avatar Jul 10 '20 04:07 wingman-jr-addon

Hi @TechnikEmpire @wingman-jr-addon @GantMan @jessetrana

bellow is my flutter code:

// import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; import 'dart:io'; import 'package:tflite/tflite.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('TFLite Example'), backgroundColor: Colors.transparent), body: Center(child: MyImagePicker()))); } }

class MyImagePicker extends StatefulWidget { @override MyImagePickerState createState() => MyImagePickerState(); }

class MyImagePickerState extends State { File imageURI; String result; String path;

Future getImageFromCamera() async { var image = await ImagePicker.pickImage(source: ImageSource.camera);

setState(() {
  imageURI = image;
  path = image.path;
});

}

Future getImageFromGallery() async { var image = await ImagePicker.pickImage(source: ImageSource.gallery);

setState(() {
  imageURI = image;
  path = image.path;
  print("path");
  print(path);
});

}

Future classifyImage() async { await Tflite.loadModel( model: "assets/Flutter_Plant_Disease_Model.tflite", labels: "assets/plant_diseases_labels.txt"); var output = await Tflite.runModelOnImage(path: path); print("output"); print(output); setState(() { result = output.toString();

  print("result");
  print(result);
});

}

@override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ imageURI == null ? Text('No image selected.') : Image.file(imageURI, width: 300, height: 200, fit: BoxFit.cover), Container( margin: EdgeInsets.fromLTRB(0, 30, 0, 20), child: RaisedButton( onPressed: () => getImageFromCamera(), child: Text('Click Here To Select Image From Camera'), textColor: Colors.white, color: Colors.blue, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), )), Container( margin: EdgeInsets.fromLTRB(0, 0, 0, 0), child: RaisedButton( onPressed: () => getImageFromGallery(), child: Text('Click Here To Select Image From Gallery'), textColor: Colors.white, color: Colors.blue, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), )), Container( margin: EdgeInsets.fromLTRB(0, 30, 0, 20), child: RaisedButton( onPressed: () => classifyImage(), child: Text('Classify Image'), textColor: Colors.white, color: Colors.blue, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), )), result == null ? Text('Result') : Text(result) ]))); } }

Actually AlexNetModel.hdf5 model which i was converting to tf lite mdoel

before converting i want to show the inputs and outputs of AlexNetModel.hdf5 are

Model Inputs: Tensor("conv2d_1_input:0", shape=(?, 224, 224, 3), dtype=float32) Model Outputs: Tensor("dense_4/Softmax:0", shape=(?, 38), dtype=float32)

Now i am converting AlexNetModel.hdf5 to tflite model as bellow

import tensorflow as tf converter = tf.lite.TFLiteConverter.from_keras_model_file('./AlexNetModel.hdf5') model = converter.convert() file = open( 'Flutter_Plant_Disease_Model.tflite' , 'wb' ) file.write( model )

now again i am seeing the inputs and outputs of Flutter_Plant_Disease_Model.tflite tflite model as bellow

input_details: [{'name': 'conv2d_1_input', 'index': 35, 'shape': array([ 1, 224, 224, 3], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}] output_details: [{'name': 'dense_4/Softmax', 'index': 59, 'shape': array([ 1, 38], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}]

so when i was using Flutter_Plant_Disease_Model.tflite model in flutter i was getting the error Caused by: java.lang.IllegalArgumentException: Cannot copy between a TensorFlowLite tensor with shape [1, 38] and a Java object with shape [1, 40]

so please help me to resolve this issue

Thanks & Regards, Manikantha Sekhar

mady143 avatar Jul 10 '20 05:07 mady143

First, to help towards an answer to your question. It sounded a bit like this one over here - might be good to make sure that labels.txt has the right length etc.

Second, I'm failing to see how a plant disease model has anything to do with this repository about the NSFW model. It sounds off-topic and more like something that should be on StackOverflow. Are the two connected somehow @mady143 ?

wingman-jr-addon avatar Jul 11 '20 02:07 wingman-jr-addon

Hi @wingman-jr-addon , I resolved the issue by replacing the label.txt file with another file though its working fine and i am display the output without the error

but i want to know that i want to get the model from serve and also i tried to import my model in Firebase ML server but its showing the file could not exceeds with 100 mb but my tflite model is more that 100mb could you explain me how to resolve and i want to get the model access from server.

Thanks and Regards, Manikantha Sekhar..

mady143 avatar Jul 13 '20 04:07 mady143

@mady143 I'm glad to hear you were able to resolve your label.txt issue. I can understand that you'd like to get your solution deployed as quickly as possible. However, you're currently doing two things that seem to go a bit against the way GitHub issues work.

  1. You're asking questions about a different model doesn't seem to have anything to do with the nsfw_model repository.
  2. You're asking followup questions not related to the original issue in the same thread.

These go against the recommendations found in the GitHub Community Guidelines to "Be clear and stay on topic". Based on that, I would recommend that this issue get closed out - it's not an actual bug, problem, feature request, or usage problem of nsfw_model so it's not good to have it counted in the system that way.

There are probably several different options about what to do next, but I think asking on one of the StackExchange family of sites might be your best option. Good luck! (And perhaps you will find that simply training a smaller model than AlexNet such as MobileNetV2 will solve your 100MB problem instead... 😄 )

wingman-jr-addon avatar Jul 13 '20 06:07 wingman-jr-addon