Python Picsell-training reference

👉 HERE

The minimal confidence threshold to keep the detection.This repository is a wrapper around the Tensorflow object-detection API to make it a bit more user friendly. Please keep in mind that you should not use the object-detection API from the official repo since small changes were made to the source code.

Picsell_utils module

This module provides us with useful functions for the object-detection API.

Genereate TFRecord files

The Tensorflow Object Detection API uses TFRecord file format as input for the data. This function helps to create them.

create_record_files(label_path,
record_dir,
tfExample_generator,
annotation_type)

Args: label_path: Path to the label map file record_dir: Path used to write the records files. tfExample_generator: Use the generator from the Picsell.ia SDK by default or provide your own generator. annotation_type: "polygon" or "rectangle", depending on your project type. Polygon will compute the masks from your polygon-type annotations.

Protobuf configuration edit

Update number of classes

Update the number of classes inside the protobuf configuration dictionnary depending on the number of classes inside the label map.

update_num_classes(config_dict, label_map)

Args : config_dict: A configuration dictionnary loaded from the protobuf file with config_util.get_configs_from_pipeline_file(). label_map: Protobuf label map loaded with label_map_util.load_labelmap() Raises: ValueError if the backbone architecture isn't known.

Set up image resizer shape

Update the image resizer shapes.

set_image_resizer(config_dict, shape)

Args: config_dict: A configuration dictionnary loaded from the protobuf file with config_util.get_configs_from_pipeline_file(). shape: The new shape for the image resizer. [max_dim, min_dim]for the keep_aspect_ratio_resizer(default resizer for faster_rcnnbackbone). [width, height]for the fixed_shape_resizer(default resizer for SSDbackbone) Raises: ValueErrorif the backbone architecture isn't known

Edit evaluation configuration

Update the eval_config protobuf message from a config_dict. Checks if the metrics_set is the right one then update the evaluation number.

edit_eval_config(config_dict, annotation_type,
eval_number)

Args: config_dict: A configuration dictionnary loaded from the protobuf file with config_util.get_configs_from_pipeline_file(). annotation_type: Should be either "rectangle" or "polygon". Depends on your project type. eval_number: The number of images you want to run your evaluation on. Raises: ValueErrorWrong annotation type provided. If you didn't provide the right annotation_type ValueErroreval_number isn't an int. If you didn't provide a int for the eval_number.

Update the required paths

Update the different paths required for the whole configuration.

update_different_paths(config_dict,
ckpt_path, label_map_path,
train_record_path,
eval_record_path)

Args: config_dict: A configuration dictionnary loaded from the protobuf file with config_util.get_configs_from_pipeline_file(). ckpt_path: Path to your checkpoint. label_map_path: Path to your label map. train_record_path: Path to your train record file. eval_record_path: Path to your eval record file.

Edit the mask type

Update the configuration to take into consideration the right mask_type. By default we record mask as "PNG_MASKS".

edit_masks(config_dict, mask_type="PNG_MASKS")

Args: config_dict: A configuration dictionnary loaded from the protobuf file with config_util.get_configs_from_pipeline_file(). mask_type: String name to identify mask type, either "PNG_MASKS" or "NUMERICAL_MASKS". Raises: ValueErrorif the mask type isn't known.

Wrapper function to edit the configuration

Edit the essential values inside the base configuration protobuf file provided with an object-detection/segmentation checkpoint. This configuration file is what will entirely define your model, pre-processing, training, evaluation etc. It is the most important file of a model with the checkpoint file and should never be deleted. This is why it is saved in almost every directory where you did something to keep redondancy but also to be sure to have the right config file used at this moment. For advanced users, if you want to dwell deep inside the configuration file you should read the proto definitions inside the proto directory of the object-detection API.

edit_config(model_selected, config_output_dir,
num_steps, label_map_path, record_dir,
eval_number, annotation_type, batch_size=None,
learning_rate=None, resizer_size=None,
incremental_or_transfer="transfer")

Args: Required: model_selected: The checkpoint you want to resume from. config_output_dir: The path where you want to save your edited protobuf configuration file. num_steps: The number of steps you want to train on. label_map_path: The path to your label_map.pbtxt file. record_dir: The path to the directory where your TFRecord files are saved. eval_number: The number of images you want to evaluate on. annotation_type: Should be either "rectangle" or "polygon", depending on how you annotated your images. Optional: batch_size: The batch size you want to use. If not provided it will use the previous one. learning_rate: The learning rate you want to use for the training. If not provided it will use the previous one. Please see config_utils.update_initial_learning_rate() inside the object_detection API for indepth details on what happens when updating it. resizer_size: The shape used to update your image resizer. Please see set_image_resizer() for more details on this. If not provided it will use the previous one. incremental_or_transfer: Either "transfer" or "incremental", in the first case it will re-init the required variables from the checkpoints in order to do transfer learning, in the second case it will keep all variables to resume a training.

Train

train(master='', save_summaries_secs=30, task=0, num_clones=1, clone_on_cpu=False, worker_replicas=1, ps_tasks=0,
ckpt_dir='', conf_dir='', train_config_path='', input_config_path='', model_config_path='')

Evaluate

Evaluate your model according to the eval_config proto message in the configuration file.

evaluate(eval_dir, config_dir, checkpoint_dir, eval_training_data=False)

Args: Required: eval_dir: The directory where the tfevent file will be saved. config_dir: The protobuf configuration directory. checkpoint_dir: The directory where the checkpoint you want to evaluate is. Optionals: eval_training_data: If set to True the evaluation will be run on the training dataset.

Returns: A dictionnary of metrics ready to be sent to the picsell.ia platform.

Retrieve the logs

Get a dictionnary of scalars from the tfevent file inside the training directory.

tfevents_to_dict(path)

Args: path: The path to the training directory where a tfevent file is saved. Returns: A dictionnary of scalars logs.

Export model as a TF SavedModel

Export your checkpoint to a saved_model.pb file

export_infer_graph(ckpt_dir, exported_model_dir,
pipeline_config_path,
write_inference_graph=False,
input_type="image_tensor", input_shape=None)

Args: Required: ckpt_dir: The directory where your checkpoint to export is located. exported_model_dir: The directory where you want to save your model. pipeline_config_path: The directory where your protobuf configuration is located.

Inference on list of images

Use your exported model to infer on a list of images path and save the results.

infer(path, exported_model_dir,
label_map_path, results_dir,
disp=True, num_infer=5, min_score_thresh=0.7, from_tfrecords=False):

Args: Required: path_list: A list of images paths to infer on. Can be set to clt.record_dir to infer on the eval.record file with the flag from_tfrecords set to True. exported_model_dir: The path used to saved your model. label_mapt_path: The path to your label_map file. results_dir: The directory where you want to save your infered images. from_tfrecords: Set this to True if you want to infer on the eval.record file Optional: disp: Set to False if you are not in an interactive python environment. Will display image in the environment if set to True. num_infer: The number of images you want to infer on. min_score_tresh: min confidence score to display prediction