项目作者: chaithanya-varshu

项目描述 :
Custom Objct Detection : Training and Testing
高级语言: Python
项目地址: git://github.com/chaithanya-varshu/cctv-detection.git
创建时间: 2020-03-18T17:24:01Z
项目社区:https://github.com/chaithanya-varshu/cctv-detection

开源协议:

下载


TensorFlow-Custom-Object-Detection : Training and Testing



Object detection allows for the recognition, detection, and localization of multiple objects within an image. It provides us a much better understanding of an image as a whole as apposed to just visual recognition.



Requirements




You need to install all the below libraries in your python environment. You can do this in Python IDLE using simple pip command.

tensorflow, opencv, keras, imageai


1. Label the images


Collect the images of different CC cameras, preferably in hundreds to create better model. You can use internet to find images or take pictures from your own devices.


Install the labelimg library and use the labelimg command to open the GUI application to label the images.

  1. >>> labelimg



We have to load each image one by one and mark the cc cams in the pictures and mark them as ‘CC Cameras’. This cannot be automated. This will create xml files for each respective image wich same name as image name.


It will look like below.

2. Dataset Preparation


Have the folder structure as below. Refer to my repository for data. Both train and validation folders can be placed in a folder named dataset.

  1. >> train >> images >> img_1.jpg (shows Object_1)
  2. >> images >> img_2.jpg (shows Object_2)
  3. >> images >> img_3.jpg (shows Object_1, Object_3 and Object_n)
  4. >> annotations >> img_1.xml (describes Object_1)
  5. >> annotations >> img_2.xml (describes Object_2)
  6. >> annotations >> img_3.xml (describes Object_1, Object_3 and Object_n)
  7. >> validation >> images >> img_151.jpg (shows Object_1, Object_3 and Object_n)
  8. >> images >> img_152.jpg (shows Object_2)
  9. >> images >> img_153.jpg (shows Object_1)
  10. >> annotations >> img_151.xml (describes Object_1, Object_3 and Object_n)
  11. >> annotations >> img_152.xml (describes Object_2)
  12. >> annotations >> img_153.xml (describes Object_1)


3. Training the model


Use the following code to train the model, this code reads each image and its respective annotation and gets trained well.

  1. from imageai.Detection.Custom import DetectionModelTrainer
  2. trainer = DetectionModelTrainer()
  3. trainer.setModelTypeAsYOLOv3()
  4. trainer.setDataDirectory(data_directory="dataset")
  5. trainer.setTrainConfig(object_names_array=["CC Camera","Nest Cam"], batch_size=4, num_experiments=200,train_from_pretrained_model="pretrained-yolov3.h5")
  6. trainer.trainModel()



When i labeled the data using labelimg, i have used 2 classes ‘CC Camera’ and ‘Nest Cam’. So here in object_names_array i have given same.


I used number of experiments as 200 so it trains the model 200 times. We can use a pretrained model, it is optional.


It takes much time based on your system speed.


After successful training a folder named ‘models’ will be created and you will see several models created in your dataset folder.


The output you will see something as below

  1. Generating anchor boxes for training images and annotation...
  2. Average IOU for 9 anchors: 0.80
  3. Anchor Boxes generated.
  4. Detection configuration saved in dataset\json\detection_config.json
  5. Training on: ['CC Camera', 'Nest Cam']
  6. Training with Batch Size: 4
  7. Number of Experiments: 200
  8. Epoch 1/5
  9. 1/72 [..............................] - ETA: 1:43:08 - loss: 709.7822 - yolo_layer_1_loss: 94.0103 - yolo_layer_2_loss: 200.1460 - yolo_layer_3_loss: 415.6259
  10. 2/72 [..............................] - ETA: 1:08:38 - loss: 708.1501 - yolo_layer_1_loss: 94.5144 - yolo_layer_2_loss: 197.7319 - yolo_layer_3_loss: 415.9039
  11. 3/72 [>.............................] - ETA: 56:15


4. Choosing the Best Model


We need to calculate the mAP value of each model based on these values we have to choose the best model to validate.

Use the below code to find the mAP values.

  1. from imageai.Detection.Custom import DetectionModelTrainer
  2. trainer = DetectionModelTrainer()
  3. trainer.setModelTypeAsYOLOv3()
  4. trainer.setDataDirectory(data_directory="dataset")
  5. metrics = trainer.evaluateModel(model_path="dataset/models", json_path="dataset/json/detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5)
  6. print(metrics)


We will get output something like below

  1. Model File: dataset/models\detection_model-ex-001--loss-0499.077.h5
  2. Using IoU : 0.5
  3. Using Object Threshold : 0.3
  4. Using Non-Maximum Suppression : 0.5
  5. CC Camera: 0.0678
  6. Nest Cam: 0.0324
  7. mAP: 0.0774
  8. ===============================
  9. Model File: dataset/models\detection_model-ex-002--loss-0150.152.h5
  10. Using IoU : 0.5
  11. Using Object Threshold : 0.3
  12. Using Non-Maximum Suppression : 0.5
  13. CC Camera: 0.0124
  14. Nest Cam: 0.0330
  15. mAP: 0.0880


5. Validating the Model


Now we will give an input image of a random cc camera, then our model will detect where are the cc cameras in that picture.

To do that we have to provide the path where we are placing our image in the code.

use the following code to validate the best model chosen above.

  1. from imageai.Detection.Custom import CustomObjectDetection
  2. detector = CustomObjectDetection()
  3. detector.setModelTypeAsYOLOv3()
  4. detector.setModelPath("dataset/models/detection_model-ex-003--loss-0022.462.h5")
  5. detector.setJsonPath("dataset/json/detection_config.json")
  6. detector.loadModel()
  7. detections = detector.detectObjectsFromImage(input_image="dataset/validation/images/test1.jpg", output_image_path="dataset/validation/images/test1-detected.jpg")
  8. for detection in detections:
  9. print(detection["name"], " : ", detection["percentage_probability"], " : ", detection["box_points"])


You will see output something like

  1. Nest Cam : 50.69343447685242 : [75, 153, 255, 224]
  2. CC Camera : 59.53064560890198 : [232, 184, 286, 227]