Convolutional Neural Networks for Object Detection
To build a CNN using the MAGIST API using the functions provided by MAGIST Vision is extremely complicated. Most of the configuration for the model is done through the configuration files (Learn More). Through a default run configuration in the CNN class, you can train the entire model using 1 command. But, to gain finer control over the exact process, you have the option to individually call each subprocess. That is why this page is split into 2 sections: Basic and Advanced.
Basic
First, you have to import the MAGIST Lite Detector:
Warning
At this point, you MUST have your configuration regarding the CNN done in the config.json
file. If not, the rest of the code will not function correctly.
Now, you must initialize the CNN using the following line:
- Make sure to make the local path match your development environment.
The final step is to run it. There are two ways to do this and the latter one is recommended.
Execution on main thread (not recommended)
- The
MAGIST_CNN
class contains an__call__
method that supports directly calling the class.
This code will run on your current Python thread meaning that the script will remain on the CNN process until training is complete. Since training can take a significant amount of time, this method is not recommended.
Execution on detached thread (recommended)
- Import the multicore threading library.
- Instantiate the class and pass the local path to the config file. This path changes based on your development environment.
- Daemonize and detach the queue from main thread.
- Pass the
cnn
class object to the queue daemon along with a name and priority. Please not thatcnn
does not have parenthesis because we are passing an object and not the return of the function. The daemon will execute the function when the queue permits. Also note that the highest priority is 1, so setting the CNN lower allows the computer to finish more important tasks like image downloading or audio processing first. - When your code is finished, remember to join the detached thread with the main thread. Otherwise, the detached threads may continue to run in the background.
This method is recommended because you can continue running more prevalent processes in the foreground while off-loading the CPU-intensive CNN to the background.
Warning
Please not that cnn
does not have parenthesis because we are passing an object and not the return of the function. The daemon will execute the function when the queue permits.
Warning
When your code is finished, remember to join the detached thread with the main thread using queue.join_thread()
. Otherwise, the detached threads may continue to run in the background.
Advanced
This method has an identical end result except you will manually run individual commands to get fine-grain control over the exact CNN flow. Most of the functions that will be executed here are identical to the cnn.__call__()
or cnn()
.
Here is what cnn.__call__()
looks like:
- The data is being loaded from the configuration file. Ensure that the paths to the dataset are consistent. Also ensure that the data directory is structured as follows: Also note that MAGIST will automatically perform the train-test split when this function is called as dictated by the configuration file.
- This function will simply compile the model and prepare it for training.
- This function will initialize all Tensorflow callback that are setup including Tensorboard and TF Checkpoints.
- This is the function that will run the actual training procedure.
Warning
Please ensure that the data directory is structured as follows:
Otherwise, the data may load incorrectly or the program might crash altogether.Since that was a class definition, you must instantiate the class and call the code through arguments and returns like this:
You will have to follow a similar process to daemonize it:
Additional Methods
The actual CNN model is not a sequential but a standard TF model defined as a class. To access that class, you must use the hidden class as follows:
This class exposes one method (besides __init__
): call
. This function defines the forward pass of the model during training and prediction. It will accept the input as an argument and return the final output. __init__
will inherit the tf.keras.models.Model
class and initialize the model by building the network.
The MAGIST_CNN
class also has some hidden functions that run in the background when the higher-level functions are called: __train_step
and __train_step
. These have @tf.function
decorators that will optimize their execution and are meant to step training and run the forward pass.
The last additional function that MAGIST_CNN
can provide is the get_class_names()
method. It will just return an array of classes where the index corresponds with the output of the Tensorflow model itself.
Next Steps
Now that the model is trained, you can run predictions on the model. The next page will explore the prediction capabilities of MAGIST.