-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom Detection Models - Training and Inference
- Loading branch information
1 parent
99eb9ab
commit 0c7e4af
Showing
42 changed files
with
3,724 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
*.db | ||
.idea | ||
*.pyc | ||
imageai.egg-info | ||
dist | ||
build |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
examples/custom_detection_from_array_extract_objects_array.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
examples/custom_detection_from_file_extract_objects_array.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
=============================== | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
=============================== | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.