Skip to content

Commit

Permalink
Custom Detection Models - Training and Inference
Browse files Browse the repository at this point in the history
  • Loading branch information
OlafenwaMoses committed Jul 30, 2019
1 parent 99eb9ab commit 0c7e4af
Show file tree
Hide file tree
Showing 42 changed files with 3,724 additions and 374 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.db
.idea
*.pyc
imageai.egg-info
dist
build
577 changes: 332 additions & 245 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A python library built to empower developers to build applications and systems w

<img src="logo1.png" style="width: 500px; height: auto; margin-left: 50px; " /> <br>

A <b>DeepQuest AI</b> project <a href="https://deepquestai.com" >https://deepquestai.com </a>.
An <b>DeepQuest AI</b> project <a href="https://deepquestai.com" >https://deepquestai.com </a>.
Developed and Maintained by [Moses Olafenwa](https://twitter.com/OlafenwaMoses) and [John Olafenwa](https://twitter.com/johnolafenwa), brothers, creators of [TorchFusion](https://github.com/johnolafenwa/TorchFusion), Authors of [Introduction to Deep Computer Vision](https://john.aicommons.science/deepvision) and creators of [DeepStack AI Server](https://deepstack.cc).
<hr>

Expand Down
96 changes: 37 additions & 59 deletions build/lib/imageai/Detection/__init__.py

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions build/lib/imageai/Prediction/Custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def setModelTypeAsInceptionV3(self):

def setDataDirectory(self, data_directory=""):
"""
'setDataDirectory()' is required to set the path to which the data/dataset to be used for
'setDataDirectory()' is required to set the path to which the data/dataset to be used for
training is kept. The directory can have any name, but it must have 'train' and 'test'
sub-directory. In the 'train' and 'test' sub-directories, there must be sub-directories
with each having it's name corresponds to the name/label of the object whose images are
Expand All @@ -99,8 +99,9 @@ def setDataDirectory(self, data_directory=""):
>> class4 >> class4_test_images
>> class5 >> class5_test_images
:return:
"""
:param data_directory:
:return:
"""

self.__data_dir = data_directory
self.__train_dir = os.path.join(self.__data_dir, "train")
Expand Down Expand Up @@ -139,7 +140,7 @@ def lr_schedule(self, epoch):
def trainModel(self, num_objects, num_experiments=200, enhance_data=False, batch_size = 32, initial_learning_rate=1e-3, show_network_summary=False, training_image_size = 224, continue_from_model=None, transfer_from_model=None, transfer_with_full_training=True, initial_num_objects = None, save_full_model = False):

"""
'trainModel()' function starts the actual training. It accepts the following values:
'trainModel()' function starts the model actual training. It accepts the following values:
- num_objects , which is the number of classes present in the dataset that is to be used for training
- num_experiments , also known as epochs, it is the number of times the network will train on all the training dataset
- enhance_data (optional) , this is used to modify the dataset and create more instance of the training set to enhance the training result
Expand Down
Binary file removed dist/imageai-2.0.2-py3-none-any.whl
Binary file not shown.
Binary file removed dist/imageai-2.0.3-py3-none-any.whl
Binary file not shown.
Binary file removed dist/imageai-2.0.3.tar.gz
Binary file not shown.
22 changes: 22 additions & 0 deletions examples/custom_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from imageai.Detection.Custom import CustomObjectDetection

detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("hololens-ex-60--loss-2.76.h5") # download via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/hololens-ex-60--loss-2.76.h5
detector.setJsonPath("detection_config.json") # download via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/detection_config.json
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image="holo2.jpg", output_image_path="holo2-detected.jpg")
for detection in detections:
print(detection["name"], " : ", detection["percentage_probability"], " : ", detection["box_points"])


"""
EXAMPLE RESULT
hololens : 39.69653248786926 : [611, 74, 751, 154]
hololens : 87.6643180847168 : [23, 46, 90, 79]
hololens : 89.25175070762634 : [191, 66, 243, 95]
hololens : 64.49641585350037 : [437, 81, 514, 133]
hololens : 91.78624749183655 : [380, 113, 423, 138]
"""
29 changes: 29 additions & 0 deletions examples/custom_detection_array_input_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from imageai.Detection.Custom import CustomObjectDetection
import cv2

image_array = cv2.imread("holo2.jpg")

detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("hololens-ex-60--loss-2.76.h5") # download via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/hololens-ex-60--loss-2.76.h5
detector.setJsonPath("detection_config.json") # download via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/detection_config.json
detector.loadModel()
detected_image, detections = detector.detectObjectsFromImage(input_image=image_array, input_type="array", output_type="array")

for eachObject in detections:
print(eachObject["name"], " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"])

cv2.imshow("Main Image", detected_image)
cv2.waitKey()
cv2.destroyAllWindows()


"""
SAMPLE RESULT
hololens : 39.69653248786926 : [611, 74, 751, 154]
hololens : 87.6643180847168 : [23, 46, 90, 79]
hololens : 89.25175070762634 : [191, 66, 243, 95]
hololens : 64.49641585350037 : [437, 81, 514, 133]
hololens : 91.78624749183655 : [380, 113, 423, 138]
"""
37 changes: 37 additions & 0 deletions examples/custom_detection_extract_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from imageai.Detection.Custom import CustomObjectDetection

detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("hololens-ex-60--loss-2.76.h5") # download via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/hololens-ex-60--loss-2.76.h5
detector.setJsonPath("detection_config.json") # download via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/detection_config.json
detector.loadModel()
detections, extracted_objects_array = detector.detectObjectsFromImage(input_image="holo2.jpg", output_image_path="holo2-detected.jpg", extract_detected_objects=True)

for detection, object_path in zip(detections, extracted_objects_array):
print(object_path)
print(detection["name"], " : ", detection["percentage_probability"], " : ", detection["box_points"])
print("---------------")

"""
SAMPLE RESULT
holo2-detected.jpg-objects\hololens-1.jpg
hololens : 39.69653248786926 : [611, 74, 751, 154]
---------------
holo2-detected.jpg-objects\hololens-1.jpg
hololens : 87.6643180847168 : [23, 46, 90, 79]
---------------
holo2-detected.jpg-objects\hololens-1.jpg
hololens : 89.25175070762634 : [191, 66, 243, 95]
---------------
holo2-detected.jpg-objects\hololens-1.jpg
hololens : 64.49641585350037 : [437, 81, 514, 133]
---------------
holo2-detected.jpg-objects\hololens-1.jpg
hololens : 91.78624749183655 : [380, 113, 423, 138]
---------------
"""
37 changes: 37 additions & 0 deletions examples/custom_detection_from_array_extract_objects_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from imageai.Detection.Custom import CustomObjectDetection
import cv2

image_array = cv2.imread("holo2.jpg")

detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("hololens-ex-60--loss-2.76.h5") # download via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/hololens-ex-60--loss-2.76.h5
detector.setJsonPath("detection_config.json") # download via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/detection_config.json
detector.loadModel()
detected_image, detections, extracted_objects = detector.detectObjectsFromImage(input_image=image_array, extract_detected_objects=True, input_type="array", output_type="array")


for eachObject in detections:
print(eachObject["name"], " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"])


cv2.imshow("Main Image", detected_image)
count = 0
for img in extracted_objects:
count += 1

cv2.imshow("Window" + str(count), img)

cv2.waitKey()
cv2.destroyAllWindows()


"""
SAMPLE RESULT
hololens : 39.69653248786926 : [611, 74, 751, 154]
hololens : 87.6643180847168 : [23, 46, 90, 79]
hololens : 89.25175070762634 : [191, 66, 243, 95]
hololens : 64.49641585350037 : [437, 81, 514, 133]
hololens : 91.78624749183655 : [380, 113, 423, 138]
"""
36 changes: 36 additions & 0 deletions examples/custom_detection_from_file_extract_objects_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from imageai.Detection.Custom import CustomObjectDetection
import cv2


detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("hololens-ex-60--loss-2.76.h5") # download via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/hololens-ex-60--loss-2.76.h5
detector.setJsonPath("detection_config.json") # download via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/detection_config.json
detector.loadModel()
detected_image, detections, extracted_objects = detector.detectObjectsFromImage(input_image="holo2.jpg", extract_detected_objects=True, output_type="array")


for eachObject in detections:
print(eachObject["name"], " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"])

cv2.imshow("Main Image", detected_image)
count = 0
for img in extracted_objects:
count += 1

cv2.imshow("Window" + str(count), img)

cv2.waitKey()
cv2.destroyAllWindows()


"""
SAMPLE RESULT
hololens : 39.69653248786926 : [611, 74, 751, 154]
hololens : 87.6643180847168 : [23, 46, 90, 79]
hololens : 89.25175070762634 : [191, 66, 243, 95]
hololens : 64.49641585350037 : [437, 81, 514, 133]
hololens : 91.78624749183655 : [380, 113, 423, 138]
"""
70 changes: 70 additions & 0 deletions examples/custom_detection_multi_model_evaluation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from imageai.Detection.Custom import DetectionModelTrainer

trainer = DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory="hololens")
trainer.evaluateModel(model_path="hololens/models", json_path="hololens/json/detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5)



"""
SAMPLE RESULT
Model File: hololens/models/detection_model-ex-07--loss-4.42.h5
Using IoU : 0.5
Using Object Threshold : 0.3
Using Non-Maximum Suppression : 0.5
hololens: 0.9231
mAP: 0.9231
===============================
Model File: hololens/models/detection_model-ex-10--loss-3.95.h5
Using IoU : 0.5
Using Object Threshold : 0.3
Using Non-Maximum Suppression : 0.5
hololens: 0.9725
mAP: 0.9725
===============================
Model File: hololens/models/detection_model-ex-05--loss-5.26.h5
Using IoU : 0.5
Using Object Threshold : 0.3
Using Non-Maximum Suppression : 0.5
hololens: 0.9204
mAP: 0.9204
===============================
Model File: hololens/models/detection_model-ex-03--loss-6.44.h5
Using IoU : 0.5
Using Object Threshold : 0.3
Using Non-Maximum Suppression : 0.5
hololens: 0.8120
mAP: 0.8120
===============================
Model File: hololens/models/detection_model-ex-18--loss-2.96.h5
Using IoU : 0.5
Using Object Threshold : 0.3
Using Non-Maximum Suppression : 0.5
hololens: 0.9431
mAP: 0.9431
===============================
Model File: hololens/models/detection_model-ex-17--loss-3.10.h5
Using IoU : 0.5
Using Object Threshold : 0.3
Using Non-Maximum Suppression : 0.5
hololens: 0.9404
mAP: 0.9404
===============================
Model File: hololens/models/detection_model-ex-08--loss-4.16.h5
Using IoU : 0.5
Using Object Threshold : 0.3
Using Non-Maximum Suppression : 0.5
hololens: 0.9725
mAP: 0.9725
===============================
"""
25 changes: 25 additions & 0 deletions examples/custom_detection_single_model_evaluation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from imageai.Detection.Custom import DetectionModelTrainer

trainer = DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory="hololens")
trainer.evaluateModel(model_path="hololens-ex-60--loss-2.76.h5", json_path="detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5)

# download JSON file via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/detection_config.json
# download detection model via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/hololens-ex-60--loss-2.76.h5



"""
SAMPLE RESULT
Model File: hololens_detection_model-ex-09--loss-4.01.h5
Using IoU : 0.5
Using Object Threshold : 0.3
Using Non-Maximum Suppression : 0.5
hololens: 0.9613
mAP: 0.9613
===============================
"""
40 changes: 40 additions & 0 deletions examples/custom_detection_train.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from imageai.Detection.Custom import DetectionModelTrainer

trainer = DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory="hololens")
trainer.setTrainConfig(object_names_array=["hololens"], batch_size=4, num_experiments=200, train_from_pretrained_model="pretrained-yolov3.h5") #download pre-trained model via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/pretrained-yolov3.h5
trainer.trainModel()



"""
SAMPLE RESULT
Using TensorFlow backend.
Generating anchor boxes for training images and annotation...
Average IOU for 9 anchors: 0.78
Anchor Boxes generated.
Detection configuration saved in hololens/json/detection_config.json
Training on: ['hololens']
Training with Batch Size: 4
Number of Experiments: 200
Epoch 1/200
- 733s - loss: 34.8253 - yolo_layer_1_loss: 6.0920 - yolo_layer_2_loss: 11.1064 - yolo_layer_3_loss: 17.6269 - val_loss: 20.5028 - val_yolo_layer_1_loss: 4.0171 - val_yolo_layer_2_loss: 7.5175 - val_yolo_layer_3_loss: 8.9683
Epoch 2/200
- 648s - loss: 11.1396 - yolo_layer_1_loss: 2.1209 - yolo_layer_2_loss: 4.0063 - yolo_layer_3_loss: 5.0124 - val_loss: 7.6188 - val_yolo_layer_1_loss: 1.8513 - val_yolo_layer_2_loss: 2.2446 - val_yolo_layer_3_loss: 3.5229
Epoch 3/200
- 674s - loss: 6.4360 - yolo_layer_1_loss: 1.3500 - yolo_layer_2_loss: 2.2343 - yolo_layer_3_loss: 2.8518 - val_loss: 7.2326 - val_yolo_layer_1_loss: 1.8762 - val_yolo_layer_2_loss: 2.3802 - val_yolo_layer_3_loss: 2.9762
Epoch 4/200
- 634s - loss: 5.3801 - yolo_layer_1_loss: 1.0323 - yolo_layer_2_loss: 1.7854 - yolo_layer_3_loss: 2.5624 - val_loss: 6.3730 - val_yolo_layer_1_loss: 1.4272 - val_yolo_layer_2_loss: 2.0534 - val_yolo_layer_3_loss: 2.8924
Epoch 5/200
- 645s - loss: 5.2569 - yolo_layer_1_loss: 0.9953 - yolo_layer_2_loss: 1.8611 - yolo_layer_3_loss: 2.4005 - val_loss: 6.0458 - val_yolo_layer_1_loss: 1.7037 - val_yolo_layer_2_loss: 1.9754 - val_yolo_layer_3_loss: 2.3667
Epoch 6/200
- 655s - loss: 4.7582 - yolo_layer_1_loss: 0.9959 - yolo_layer_2_loss: 1.5986 - yolo_layer_3_loss: 2.1637 - val_loss: 5.8313 - val_yolo_layer_1_loss: 1.1880 - val_yolo_layer_2_loss: 1.9962 - val_yolo_layer_3_loss: 2.6471
Epoch 7/200
"""

16 changes: 16 additions & 0 deletions examples/custom_detection_video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from imageai.Detection.Custom import CustomVideoObjectDetection
import os

execution_path = os.getcwd()

video_detector = CustomVideoObjectDetection()
video_detector.setModelTypeAsYOLOv3()
video_detector.setModelPath("hololens-ex-60--loss-2.76.h5") # download via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/hololens-ex-60--loss-2.76.h5
video_detector.setJsonPath("detection_config.json") # download via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/detection_config.json
video_detector.loadModel()

video_detector.detectObjectsFromVideo(input_file_path="holo1.mp4",
output_file_path=os.path.join(execution_path, "holo1-detected3"),
frames_per_second=20,
minimum_percentage_probability=40,
log_progress=True)
9 changes: 9 additions & 0 deletions examples/detection_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"labels" : [
"hololens"
],
"anchors" : [
[160, 171, 261, 167, 301, 285],
[78, 86, 104, 69, 138, 105],
[37, 32, 58, 61, 62, 44]]
}
2 changes: 1 addition & 1 deletion imageai.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 1.0
Name: imageai
Version: 2.0.3
Version: 2.1.0
Summary: A python library built to empower developers to build applications and systems with self-contained Computer Vision capabilities
Home-page: https://github.com/OlafenwaMoses/ImageAI
Author: Moses Olafenwa and John Olafenwa
Expand Down

0 comments on commit 0c7e4af

Please sign in to comment.