I am creating a custom object detection sample for android, I used the ssd_mobilenet_v1_coco pretrained model for transfer learning and got a decent accuracy. I also successfully managed to export the model.ckpt-XXXX to a .pb tflite graph using this line of code in the terminal (ran from the object_detection folder after cloning Tensorflow Object Detection API from github):
python export_tflite_ssd_graph.py --pipeline_config_path=training/ssd_mobilenet_v1_coco.config --trained_checkpoint_prefix=training/model.ckpt-40500--output_directory=tflite --add_postprocessing_op=true
The above created a folder tflite and it contained 2 files :
tflite_graph.pb
tflite_graph.pbtxt
However, when I want to convert the tflite_graph.pb to detect.tflite I get the following error and the program ends abruptly:
"TOCO failed. See console for info.\n%s\n%s\n" % (stdout, stderr))
tensorflow.lite.python.convert.ConverterError: TOCO failed.
.
.
.
Check failed: input_array_dims[i] == input_array_proto.shape().dims(i) (300 vs. 128)
Fatal Python error: Aborted
.
.
.
This is the command I used to convert the .pb to .tflite:
tflite_convert --graph_def_file=tflite/tflite_graph.pb --output_file=tflite/detect.tflite --input_shapes=1,128,128,3 --input_arrays=normalized_input_image_tensor --output_arrays=TFLite_Detection_PostProcess,TFLite_Detection_PostProcess:1,TFLite_Detection_PostProcess:2,TFLite_Detection_PostProcess:3 --allow_custom_ops
The images I used had a size of 128x128 hence why I assumed that would be the input_shapes. I do have Toco installed as well.
Any help or advice will be highly appreciated.
So upon doing additional research, I found out that it was because the config file of the model was looking at images of sizes 300 x 300. So I changed the images dimension in the config file to 128 and it worked.
Related
After using YOLOv5 to train model weights as .pt file,
how can I convert the weights file (model.pt) to hdf5 file (model.h5)?
Running python train.py --batch 16 --epochs 3 --data mydata.yaml --weights yolov5s.pt, the result is given by best.pt file at subfolder of YOLOv5, how can I convert it to h5 file?
Do this installation and get started here
This could be a lengthy procedure ... as you are aware that .pt only contains weights and not model architecture hence your model class should also be present in your conversion code
Edit: New links are added
I am using the TF Object detection API. I am using TF2.4.1, python 3.6, and want to train the mobilenet_v2
I downloaded the official SSD MobileNet v2 320x320 here
When running the training from
/tensorflow/model/research/object_detection/model_main_tf2.py
I get the follownig error:
... File
"/home/base/anaconda3/envs/AIOD/lib/python3.6/site-packages/object_detection/builders/model_builder.py",
line 251, in _check_feature_extractor_exists
'Tensorflow'.format(feature_extractor_type))
ValueError: ssd_mobilenet_v2 is not supported. See model_builder.py
for features extractors compatible with different versions of
Tensorflow
in my pipeline.config file the fteature extractor is unchanged as:
feature_extractor {
type: 'ssd_mobilenet_v2'
...
}
in the ... /model_builder.py I find that the SSD_FEATURE_EXTRACTOR_CLASS_MAP, which is checked in this case, includes the ssd_mobilenet_v2 (Line 185 in model_builder).
Before, I had a problem, that the pipeline.config file could not be found even so I gave the exact location. Copying the pipeline.config file to the tensorflow/models/own_model_Dir made that error dissapear. So maybe, I have to put the model I downloaded into a specfic folder?
Questions
Does anyone know how to solve this?
Do you know where I have to place the model? Do I also have to place it into the tensorflow/models dir?
The solution is that SSD_FEATURE_EXTRACTOR_CLASS_MAP is under if tf_version.is_tf1():
as I run with TF2.4.1 I am not tf1.
To avoid this either use TF<2 (even though it says in the name model_main_tf2.py) or
you chose as feature extractor in your pipeline.config
e.g., ssd_mobilenet_v2_keras. I still have to figure out which is fitting, but that would be a soultion as this extractor is in SSD_KERAS_FEATURE_EXTRACTOR_CLASS_MAP which is under if tf_version.is_tf2():
... Of ourse I ran into toher problems, but that is not for this thread.
EDIT:
Another solution is to download and use the correct modelfile. SOmehow I managed to download a TF2 model but used a previously downloaded TF1 file :-S. All of the above is still correct.
Screenshot of the errorI have trained my own YOLO model with Darkflow and got the .pb file and the .meta file for license plate recognition. What i am trying now is to implement an android App that can use this model. To do so i have decided to convert it into tflite but always get this error about "tags=". I've checked the tags of my model with 'saved_model_cli show --dir=./' and i've got no tags. What should i do to fix this error ?
I am trying an example of car evaluation classification from
http://archive.ics.uci.edu/ml/datasets/Car+Evaluation
I have successfully trained model and printing predictions successfully
using following code
I am following this page for converting .pb model to .tflite
I have successfully build frozen graph
bazel build tensorflow/python/tools:freeze_graph
Now I am facing problems in running following command
bazel-bin/tensorflow/python/tools/freeze_graph\
--input_graph=/CarEvaluation/mobilenet_v1_224.pb \
--input_checkpoint=/CarEvaluation/checkpoints/mobilenet-10202.ckpt \
--input_binary=true --output_graph=/CarEvaluation/frozen_mobilenet_v1_224.pb \
--output_node_names=CarEvaluation/Predictions/Reshape_1
Problem is that in model directory I have .pbtxt file instead of .pb
and also I couldn't find .ckpt file in model directory, I have a simple checkpoint file and several .ckpt meta and index files with some number as suffix.
I have tried running above command with .pbtxt file and I am getting this exception
input_graph_def.ParseFromString(f.read())
google.protobuf.message.DecodeError: Error parsing message
Use the .pbtxt and the highest numbered .ckpt
i.e. something like:
bazel-bin/tensorflow/python/tools/freeze_graph\
--input_graph=/CarEvaluation/mobilenet_v1_224.pbtxt \
--input_checkpoint=/CarEvaluation/checkpoints/mobilenet-10202.ckpt-2000 \
--input_binary=true --output_graph=/CarEvaluation/frozen_mobilenet_v1_224.pb \
--output_node_names=CarEvaluation/Predictions/Reshape_1
As far as I have understood from the freeze_graph code, when you want to use it with a pbtxt file, you need to omit the --input_binary=true option, as the input file is no longer a binary one.
I have trained new model on top of ssd_mobilenet_v1_coco for a custom data set. This model works fine in tensorflow. But now I want to use this in OpenCV.
net = cv2.dnn.readNetFromTensorflow("model/frozen_inference_graph.pb", "model/protobuf.pbtxt")
detections = net.forward()
So for the config file I convert frozen_graph to pbtxt and add it. But then I got the following error
[libprotobuf ERROR /home/chamath/Projects/opencv/opencv/3rdparty/protobuf/src/google/protobuf/text_format.cc:298] Error parsing text-format tensorflow.GraphDef: 731:5: Unknown enumeration value of "DT_RESOURCE" for field "type".
As suggested here I try to use this config file mentioned in the thread but when I use it object detection is not working properly. Incorrect number of squares detected and they are misplaced.
Is there any method to create a pbtxt config file that works with OpenCV? Or any suggestions how to make my model work in OpenCV?
Is likely that you havent generate the propper graph, after training.
You have to convert the graph like this:
python ../opencv/samples/dnn/tf_text_graph_ssd.py --input
trained-inference-graphs/inference_graph_v5.pb/frozen_inference_graph.pb
--output trained-inference-graphs/inference_graph_v5.pb/graph.pbtxt
Then pass the .pb and the graph.pbtxt to DNN.readNetFromTensorflow that should work for you :)