Issue Using TensorFlow Self-Trained Model in DNN Module

asked 2018-05-21 15:31:15 -0600

oonyalo gravatar image

I am trying to use a model I trained myself with OpenCV's DNN module based off of these examples:

I used "ssd_mobilenet_v1_coco_2017_11_17" as the checkpoint for my model. I also only trained it for one class.

After I exported the model I used the tf_text_graph_ssd.py file to try and generate the config file:

python c:\code\opencv\opencv\samples\dnn\tf_text_graph_ssd.py --input=C:\code\video-object-detection\models\mobilenet_ssd_v1_balls\frozen_inference_graph.pb --output=C:\code\video-object-detection\models\mobilenet_ssd_v1_balls\ssd_mobilenet_v1_balls_2018_05_20.pbtxt --num_classes=1

but I got this error:

2018-05-21 16:01:18.354975: I T:\src\github\tensorflow\tensorflow\tools\graph_transforms\transform_graph.cc:264] Applying sort_by_execution_order
Traceback (most recent call last):
  File "c:\code\opencv\opencv\samples\dnn\tf_text_graph_ssd.py", line 137, in <module>
assert(graph_def.node[0].op == 'Placeholder')
AssertionError

I did some research and kept coming across the solution to transform the graph so I tried building TensorFlow's graph_transforms tool

bazel build tensorflow/tools/graph_transforms:transform_graph

but got this error

ERROR: C:/code/tensorflow/tensorflow/tensorflow/cc/BUILD:422:1: Linking of rule '//tensorflow/cc:ops/no_op_gen_cc' failed (Exit 1181)

Of course, I'm sure the bazel build error has nothing to do with OpenCV but I thought I'd add the information

Basically, I need help creating a .pbtxt config file from my exported frozen_inference_graph.pb file.

I tried using the graph.pbtxt that was generated during training but I get this error

[libprotobuf ERROR D:\Build\OpenCV\opencv-3.4.1\3rdparty\protobuf\src\google\protobuf\text_format.cc:288] Error parsing text-format tensorflow.GraphDef: 1178:9: Unknown enumeration value of "DT_RESOURCE" for field "type".
OpenCV(3.4.1) Error: Unspecified error (FAILED: ReadProtoFromTextFile(param_file, param). Failed to parse GraphDef file: models/mobilenet_ssd_v1_balls/graph.pbtxt) in cv::dnn::ReadTFNetParamsFromTextFileOrDie, file D:\Build\OpenCV\opencv-3.4.1\modules\dnn\src\tensorflow\tf_io.cpp, line 54

cv2.error: OpenCV(3.4.1) D:\Build\OpenCV\opencv-3.4.1\modules\dnn\src\tensorflow\tf_io.cpp:54: error: (-2) FAILED: ReadProtoFromTextFile(param_file, param). Failed to parse GraphDef file: models/mobilenet_ssd_v1_balls/graph.pbtxt in function cv::dnn::ReadTFNetParamsFromTextFileOrDie

and I tried running cv::dnn::readNetFromTensorflow without a config file at all and I get this error:

OpenCV(3.4.1) Error: Unspecified error (Unknown layer type Cast in op ToFloat) in cv::dnn::experimental_dnn_v4::`anonymous-namespace'::TFImporter::populateNet, file D:\Build\OpenCV\opencv-3.4.1\modules\dnn\src\tensorflow\tf_importer.cpp, line 1582

cv2.error: OpenCV(3.4.1) D:\Build\OpenCV\opencv-3.4.1\modules\dnn\src\tensorflow\tf_importer.cpp:1582: error: (-2) Unknown layer type Cast in op ToFloat in function cv::dnn::experimental_dnn_v4::`anonymous-namespace'::TFImporter::populateNet

Any help is greatly appreciated.

edit retag flag offensive close merge delete

Comments

I solved it. It had something to do with being on Windows.

I had to build transform_graph using the Ubuntu shell and then I ran transform_graph on my frozen_inference_graph.pb file.

After that I ran tf_text_graph_ssd.py on the transformed graph from the Command Prompt.

oonyalo gravatar imageoonyalo ( 2018-05-21 20:36:18 -0600 )edit

Any chance that you could share your code for training the DNN from scratch in Google TensorFlow? I would be very grateful.

sjhalayka gravatar imagesjhalayka ( 2018-05-21 21:06:39 -0600 )edit
1

@sjhalayka the steps to prepare the data is here: https://github.com/sem-onyalo/ml-sand.... I'm currently writing up steps on the end-to-end process. I'll post the link to that once I'm done.

oonyalo gravatar imageoonyalo ( 2018-05-22 22:29:19 -0600 )edit

@oonyalo -- thank you so much for the code! And thanks for showing me that ipnyb files show up on GitHub!

sjhalayka gravatar imagesjhalayka ( 2018-05-23 07:38:55 -0600 )edit

@sjhalayka no problem :)

oonyalo gravatar imageoonyalo ( 2018-05-23 12:36:48 -0600 )edit

@oonyalo -- Have you ever tried to write a DNN that differentiates between cats and dogs, or a DNN that performs the XOR operation? My ultimate aim is to be able to use a DNN to do one of these simplest of tasks, to boil it down to its essence. That way, AI noobs like myself would have a fighting chance, with all of the simplified code at our disposal -- it would definitely raise the bar of knowledge. :D

sjhalayka gravatar imagesjhalayka ( 2018-05-23 12:42:50 -0600 )edit

@sjhalayka No but I have wrote one that differentiated between handwritten digits (i.e. 0-9), which under the hood is a classification problem just like differentiating between cats and dogs. I got the model from a book called Make Your Own Neural Network. I am an AI noob too and that book is probably the best I've seen in regards to an introductory level neural network tutorial book. Not sure what you mean by an XOR DNN though. Can you be more specific?

oonyalo gravatar imageoonyalo ( 2018-05-23 13:53:56 -0600 )edit

@oonyalo -- Thank you for the book recommendation. Does the book cover DNNs?

The XOR DNN would have 2 input neurons (bits) and 2 output neurons (using one-hot encoding). The network is trained to do the XOR operation on the 2 input neurons. The XOR operation is:

input: 0, 0 --> output neuron 0 is hot
input: 0, 1 --> output neuron 1 is hot
input: 1, 0 --> output neuron 1 is hot
input: 1, 1 --> output neuron 0 is hot

Any chance that you would be able to post your digit classification code to GitHub? :)

sjhalayka gravatar imagesjhalayka ( 2018-05-23 14:05:21 -0600 )edit

@sjhalayka ok see, I've never built that or any NN logic gate for that matter but it shouldn't be difficult at all. In fact you could probably build a fairly simple one that did all the logic gates. Here is the source code for that book, which should give you an indication of what I'm talking about. You'd probably still use three layers, although your shouldn't need as many hidden layer nodes, and then you just change the number of input and output nodes to 2 just like you said. Then just create a csv file with your XOR data like so:

0,0,0
1,0,1
1,1,0
0,1,1

where 0th position is the output and 1st and 2nd positions are inputs A and B

oonyalo gravatar imageoonyalo ( 2018-05-23 14:35:21 -0600 )edit

@sjhalayka ^ I might be leaving out some specifics but that should be the general idea. Let me know if you have any problems, I'd be interested to see it up and running.

oonyalo gravatar imageoonyalo ( 2018-05-23 14:37:09 -0600 )edit