In a previous tutorial, we saw how to use the open-source GitHub project Mask_RCNN with Keras and TensorFlow 1.14. In this tutorial, the project is inspected to replace the TensorFlow 1.14 features by those compatible with TensorFlow 2.0.
Specifically, we'll cover:
- Four edits to make predictions with Mask R-CNN using TensorFlow 2.0
- Five edits to train Mask R-CNN with TensorFlow 2.0
- A summary of all the changes to be made
- Conclusion
Before starting, check out the previous tutorial to download and run the Mask_RCNN project.
Bring this project to life
Edits to Make Predictions with Mask R-CNN Using TensorFlow 2.0
The Mask_RCNN project works only with TensorFlow 1.13. Because TensorFlow 2.0 offers more features and enhancements, developers are looking to migrate to TensorFlow 2.0.
Some tools may help in automatically convert TensorFlow 1.0 code to TensorFlow 2.0 but they are not guaranteed to produce a fully functional code. Check the upgrade script offered by Google.
In this section, the required changes to the Mask R-CNN project are discussed so that it fully supports TensorFlow 2.0 for making predictions (i.e. when the mode parameter in the mrcnn.model.MaskRCNN class constructor is set to inference). In a later section, more edits are applied to train the Mask R-CNN model in TensorFlow 2.0 (i.e. when the mode parameter in the mrcnn.model.MaskRCNN class constructor is set to training).
If you have TensorFlow 2.0 installed, running the following code block to perform inference will raise exceptions. Please consider downloading the trained weights mask_rcnn_coco.h5 from this link.
The next subsections discuss the required changes to support TensorFlow 2.0 and to solve all the exceptions.
1. tf.log()
Running the previous code, an exception is raised from this line in the mrcnn.model.log2_graph() function:
The exception text is given below. It indicates that TensorFlow has no attribute called log().
In TensorFlow 1.0, the log() function was available at the root of the library. Due to reorganizing some functions in TensorFlow 2.0, the log() function is moved into the tensorflow.math module. So, rather than using tf.log(), simply use tf.math.log().
To fix the issue, just locate the mrcnn.model.log2_graph() function. Here is its code:
Replace each tf.log by tf.math.log. The new function should be:
2. tf.sets.set_intersection()
After running the code again, another exception is raised when executing this line inside the mrcnn.model.refine_detections_graph() function:
The exception is given below.
The issue occurs because the set_intersection() function in TensorFlow 1.0 is renamed to intersection() in TensorFlow 2.0.
To fix the issue, just use tf.sets.intersection() rather than tf.sets.set_intersection(). The new line is:
Note that tf.sets.set_intersection() is used in another location. So, search for all of its occurrences and replace each one by tf.sets.intersection().
3. tf.sparse_tensor_to_dense()
After the two above issues are solved, running the code again gives an exception in this line from the mrcnn.model.refine_detections_graph() function:
Here is the exception. The function sparse_tensor_to_dense() in TensorFlow 1.0 is accessible through the tf.sparse module (tf.sparse.to_dense).
To fix it, replace each occurrence of tf.sparse_tensor_to_dense by tf.sparse.to_dense. The new line should be:
Do that for all occurrences of tf.sparse_tensor_to_dense.
4. tf.to_float()
There is another exception raised due to this line inside the mrcnn.model.load_image_gt() function:
The exception below occurs because the to_float() function in TensorFlow 1.0 does not exist in TensorFlow 2.0.
As a replacement for the to_float() function in TensorFlow 2.0, use the tf.cast() function as follows:
To fix the exception, replace the previous line by the next line:
Summary of changes for inference
To make predictions using Mask R-CNN in TensorFlow 2.0, there are 4 changes to be made in the mrcnn.model script:
- Replace
tf.log()bytf.math.log() - Replace
tf.sets.set_intersection()bytf.sets.intersection() - Replace
tf.sparse_tensor_to_dense()bytf.sparse.to_dense() - Replace
tf.to_float()bytf.cast([value], tf.float32)
After making all of these changes, the code we saw at the beginning of this article can successfully run in TensorFlow 2.0.
Edits to Train Mask R-CNN Using TensorFlow 2.0
Assuming that you have TensorFlow 2.0 installed, running the code block below to train Mask R-CNN on the Kangaroo Dataset will raise a number of exceptions. This section inspects the changes to be made to train Mask R-CNN in TensorFlow 2.0.
Please consider downloading the Kangaroo dataset, in addition to the weights mask_rcnn_coco.h5 from this link.
1. tf.random_shuffle()
After running the previous code, an exception is raised when the following line inside the mrcnn.model.detection_targets_graph() function is executed.
The exception is given below, which indicates that no function is named random_shuffle().
Due to the new organization of TensorFlow 2.0 functions, the tf.random_shuffle() function in TensorFlow 1.0 is replaced by the shuffle() method in the tf.random module. Thus, tf.random_shuffle() should be replaced by tf.random.shuffle().
The previous line should be:
Please check all occurrences of tf.random_shuffle() and make the necessary change.
2. tf.log
There is an exception raised from the next line inside the mrcnn.utils.box_refinement_graph() function.
The exception, given below, indicates that the function tf.log() does not exist.
In TensorFlow 2.0, the log() function is moved into the math module. Thus, tf.log() should be replaced by tf.math.log(). The previous line should be:
Make this change for all occurrences of the tf.log() function.
3. Tensor Membership
An exception is raised when executing the next if statement inside the compile() method in the mrccn.model.MaskRCNN class.
The exception is listed below. Let's explain its meaning.
Both layer.output and self.keras_model.losses are tensors. The previous line checks the membership of the layer.output tensor inside the self.keras_model.losses tensor. The result of the membership operation is another tensor, and Python uses it as a bool type, which is impossible.
According to the following code, the purpose of the if statement is to check whether the layer's loss exists within the self.keras_model.losses tensor. If not, then the code appends it to the self.keras_model.losses tensor.
Outside the for loop, the self.keras_model.losses tensor is empty. Thus, it has no loss functions at all. As a result, the if statement might be ignored. The solution is to comment the if statement according to the next code.
Let's discuss another exception.
4. metrics_tensors
There is an exception raised after executing the next line inside the compile() method in the mrccn.model.MaskRCNN class.
According to the following error, there is no attribute named metrics_tensors in the keras_model attribute.
The solution is to add metrics_tensors to the beginning of the compile() method.
The next section discusses the last change to be made.
5. Save Training Logs
There is an exception raised after executing the next line inside the set_log_dir() method within the mrcnn.model.MaskRCNN class.
The exception is given below, and indicates there is a problem creating a directory.
The exception can be solved by manually specifying a valid directory. For example, the next directory is valid for my PC. Try to specify a valid directory for yours.
This is the last change to be made so that the Mask_RCNN project can train the Mask R-CNN model in TensorFlow 2.0. The training code prepared previously can now be executed in TensorFlow 2.0.
Summary of changes to train Mask R-CNN in TensorFlow 2.0
To train the Mask R-CNN model using the Mask_RCNN project in TensorFlow 2.0, there are 5 changes to be made in the mrcnn.model script:
- Replace
tf.random_shuffle()withtf.random.shuffle() - Replace
tf.log()withtf.math.log() - Comment out an
ifstatement inside thecompile()method. - Initialize the
metrics_tensorsattribute at the beginning of thecompile()method. - Assign a valid directory to the
self.log_dirattribute.
Conclusion
This tutorial edited the open-source Mask_RCNN project so that the Mask R-CNN model is able to be trained and perform inference using TensorFlow 2.0.
To train the Mask R-CNN model in TensorFlow 2.0, a total of 9 changes were applied: 4 to support making predictions, and 5 to enable training.