Showing preview only (1,146K chars total). Download the full file or copy to clipboard to get everything.
Repository: OlafenwaMoses/ImageAI
Branch: master
Commit: 2156d1a39a19
Files: 152
Total size: 1.1 MB
Directory structure:
gitextract_hhet6k5q/
├── .codecov.yml
├── .github/
│ ├── FUNDING.yml
│ └── workflows/
│ └── build.yml
├── .gitignore
├── .travis.yml
├── BACKEND_MIGRATION.md
├── LICENSE
├── MANIFEST.in
├── README.md
├── examples/
│ ├── camera_feed_detection.py
│ ├── custom_detection.py
│ ├── custom_detection_array_input_output.py
│ ├── custom_detection_extract_objects.py
│ ├── custom_detection_from_array_extract_objects_array.py
│ ├── custom_detection_from_file_extract_objects_array.py
│ ├── custom_detection_train.py
│ ├── custom_detection_video.py
│ ├── custom_model_prediction.py
│ ├── custom_model_training.py
│ ├── image_custom_object_detection.py
│ ├── image_prediction.py
│ ├── object_detection.py
│ ├── video_analysis_per_frame.py
│ ├── video_analysis_per_second.py
│ ├── video_custom_object_detection.py
│ └── video_object_detection.py
├── imageai/
│ ├── Classification/
│ │ ├── CUSTOMCLASSIFICATION.md
│ │ ├── CUSTOMTRAINING.md
│ │ ├── Custom/
│ │ │ ├── __init__.py
│ │ │ ├── data_transformation.py
│ │ │ └── training_params.py
│ │ ├── README.md
│ │ ├── __init__.py
│ │ └── imagenet_classes.txt
│ ├── Detection/
│ │ ├── Custom/
│ │ │ ├── CUSTOMDETECTION.md
│ │ │ ├── CUSTOMDETECTIONTRAINING.md
│ │ │ ├── CUSTOMVIDEODETECTION.md
│ │ │ ├── __init__.py
│ │ │ └── yolo/
│ │ │ ├── __init__.py
│ │ │ ├── compute_loss.py
│ │ │ ├── custom_anchors.py
│ │ │ ├── dataset.py
│ │ │ ├── metric.py
│ │ │ └── validate.py
│ │ ├── README.md
│ │ ├── VIDEO.md
│ │ ├── __init__.py
│ │ ├── coco91_classes.txt
│ │ └── coco_classes.txt
│ ├── __init__.py
│ ├── backend_check/
│ │ ├── __init__.py
│ │ ├── backend_check.py
│ │ └── model_extension.py
│ ├── densenet121/
│ │ └── __init__.py
│ ├── inceptionv3/
│ │ └── __init__.py
│ ├── mobilenetv2/
│ │ └── __init__.py
│ ├── resnet50/
│ │ └── __init__.py
│ ├── retinanet/
│ │ ├── __init__.py
│ │ └── utils.py
│ └── yolov3/
│ ├── __init__.py
│ ├── tiny_yolov3.py
│ ├── utils.py
│ └── yolov3.py
├── imageai_tf_deprecated/
│ ├── Classification/
│ │ ├── CUSTOMCLASSIFICATION.md
│ │ ├── CUSTOMTRAINING.md
│ │ ├── Custom/
│ │ │ └── __init__.py
│ │ ├── README.md
│ │ └── __init__.py
│ ├── Detection/
│ │ ├── Custom/
│ │ │ ├── CUSTOMDETECTION.md
│ │ │ ├── CUSTOMDETECTIONTRAINING.md
│ │ │ ├── CUSTOMVIDEODETECTION.md
│ │ │ ├── __init__.py
│ │ │ ├── callbacks.py
│ │ │ ├── evaluate.py
│ │ │ ├── gen_anchors.py
│ │ │ ├── generator.py
│ │ │ ├── utils/
│ │ │ │ ├── __init__.py
│ │ │ │ ├── bbox.py
│ │ │ │ ├── colors.py
│ │ │ │ ├── image.py
│ │ │ │ ├── multi_gpu_model.py
│ │ │ │ └── utils.py
│ │ │ └── voc.py
│ │ ├── README.md
│ │ ├── VIDEO.md
│ │ ├── YOLO/
│ │ │ ├── __init__.py
│ │ │ ├── utils.py
│ │ │ └── yolov3.py
│ │ ├── __init__.py
│ │ └── keras_retinanet/
│ │ ├── __init__.py
│ │ ├── backend/
│ │ │ ├── __init__.py
│ │ │ └── backend.py
│ │ ├── bin/
│ │ │ ├── __init__.py
│ │ │ ├── convert_model.py
│ │ │ ├── debug.py
│ │ │ ├── evaluate.py
│ │ │ └── train.py
│ │ ├── callbacks/
│ │ │ ├── __init__.py
│ │ │ ├── coco.py
│ │ │ ├── common.py
│ │ │ └── eval.py
│ │ ├── initializers.py
│ │ ├── layers/
│ │ │ ├── __init__.py
│ │ │ ├── _misc.py
│ │ │ └── filter_detections.py
│ │ ├── losses.py
│ │ ├── models/
│ │ │ ├── __init__.py
│ │ │ ├── densenet.py
│ │ │ ├── effnet.py
│ │ │ ├── mobilenet.py
│ │ │ ├── resnet.py
│ │ │ ├── retinanet.py
│ │ │ ├── senet.py
│ │ │ └── vgg.py
│ │ ├── preprocessing/
│ │ │ ├── __init__.py
│ │ │ ├── coco.py
│ │ │ ├── csv_generator.py
│ │ │ ├── generator.py
│ │ │ ├── kitti.py
│ │ │ ├── open_images.py
│ │ │ └── pascal_voc.py
│ │ └── utils/
│ │ ├── __init__.py
│ │ ├── anchors.py
│ │ ├── coco_eval.py
│ │ ├── colors.py
│ │ ├── compute_overlap.pyx
│ │ ├── config.py
│ │ ├── eval.py
│ │ ├── gpu.py
│ │ ├── image.py
│ │ ├── model.py
│ │ ├── tf_version.py
│ │ ├── transform.py
│ │ └── visualization.py
│ ├── Prediction/
│ │ ├── Custom/
│ │ │ ├── __init__.py
│ │ │ └── custom_utils.py
│ │ ├── __init__.py
│ │ └── imagenet_utils.py
│ └── __init__.py
├── requirements.txt
├── requirements_extra.txt
├── requirements_gpu.txt
├── scripts/
│ └── pascal_voc_to_yolo.py
├── setup.py
└── test/
├── test_custom_classification.py
├── test_custom_classification_training.py
├── test_custom_detection_training.py
├── test_custom_object_detection.py
├── test_custom_video_detection.py
├── test_image_classification.py
├── test_object_detection.py
└── test_video_object_detection.py
================================================
FILE CONTENTS
================================================
================================================
FILE: .codecov.yml
================================================
codecov:
notify:
require_ci_to_pass: yes
coverage:
precision: 2
round: down
range: "30...100"
status:
project: yes
patch: yes
changes: no
parsers:
gcov:
branch_detection:
conditional: yes
loop: yes
method: no
macro: no
comment:
layout: "header, diff"
behavior: default
require_changes: no
================================================
FILE: .github/FUNDING.yml
================================================
github: OlafenwaMoses
================================================
FILE: .github/workflows/build.yml
================================================
name: Build and Testing
on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
UnitestPython37:
name: Python3.7 Tests
runs-on: ubuntu-latest
# needs: None
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.7'
cache: 'pip'
- name: Install Dependencies
run: |
pip install -r requirements.txt
pip install -r requirements_extra.txt
- name: Download and Setup Resources
env:
CI: false
run: |
sudo apt-get update
sudo apt-get install unzip -y
mkdir test/data-models
mkdir test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/densenet121-a639ec97.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/densenet121-idenprof-test_acc_0.82550_epoch-95.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/inception_v3-idenprof-test_acc_0.81050_epoch-92.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/inception_v3_google-1a9a5a14.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/mobilenet_v2-b0353104.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/mobilenet_v2-idenprof-test_acc_0.85300_epoch-92.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/resnet50-19c8e357.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/resnet50-idenprof-test_acc_0.78200_epoch-91.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/retinanet_resnet50_fpn_coco-eeacb38b.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/tiny-yolov3.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/tiny_yolov3_number-plate-dataset-imageai_mAP-0.22595_epoch-20.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3_number-plate-dataset-imageai_mAP-0.57145_epoch-11.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/idenprof.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/number-plate-dataset-imageai_tiny_yolov3_detection_config.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/number-plate-dataset-imageai_yolov3_detection_config.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/idenprof_model_classes.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/test-resources-v3/data-datasets.zip -P test
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/test-resources-v3/data-images.zip -P test
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/test-resources-v3/data-videos.zip -P test
unzip test/data-datasets.zip -d test
unzip test/data-images.zip -d test
unzip test/data-videos.zip -d test
- name: Run Unittest
run: |
pytest test -vvv
UnitestPython38:
name: Python3.8 Tests
runs-on: ubuntu-latest
# needs: None
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.8'
cache: 'pip'
- name: Install Dependencies
run: |
pip install -r requirements.txt
pip install -r requirements_extra.txt
- name: Download and Setup Resources
env:
CI: false
run: |
sudo apt-get update
sudo apt-get install unzip -y
mkdir test/data-models
mkdir test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/densenet121-a639ec97.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/densenet121-idenprof-test_acc_0.82550_epoch-95.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/inception_v3-idenprof-test_acc_0.81050_epoch-92.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/inception_v3_google-1a9a5a14.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/mobilenet_v2-b0353104.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/mobilenet_v2-idenprof-test_acc_0.85300_epoch-92.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/resnet50-19c8e357.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/resnet50-idenprof-test_acc_0.78200_epoch-91.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/retinanet_resnet50_fpn_coco-eeacb38b.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/tiny-yolov3.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/tiny_yolov3_number-plate-dataset-imageai_mAP-0.22595_epoch-20.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3_number-plate-dataset-imageai_mAP-0.57145_epoch-11.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/idenprof.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/number-plate-dataset-imageai_tiny_yolov3_detection_config.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/number-plate-dataset-imageai_yolov3_detection_config.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/idenprof_model_classes.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/test-resources-v3/data-datasets.zip -P test
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/test-resources-v3/data-images.zip -P test
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/test-resources-v3/data-videos.zip -P test
unzip test/data-datasets.zip -d test
unzip test/data-images.zip -d test
unzip test/data-videos.zip -d test
- name: Run Unittest
run: |
pytest test -vvv
UnitestPython39:
name: Python3.9 Tests
runs-on: ubuntu-latest
# needs: None
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.9'
cache: 'pip'
- name: Install Dependencies
run: |
pip install -r requirements.txt
pip install -r requirements_extra.txt
- name: Download and Setup Resources
env:
CI: false
run: |
sudo apt-get update
sudo apt-get install unzip -y
mkdir test/data-models
mkdir test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/densenet121-a639ec97.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/densenet121-idenprof-test_acc_0.82550_epoch-95.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/inception_v3-idenprof-test_acc_0.81050_epoch-92.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/inception_v3_google-1a9a5a14.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/mobilenet_v2-b0353104.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/mobilenet_v2-idenprof-test_acc_0.85300_epoch-92.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/resnet50-19c8e357.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/resnet50-idenprof-test_acc_0.78200_epoch-91.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/retinanet_resnet50_fpn_coco-eeacb38b.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/tiny-yolov3.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/tiny_yolov3_number-plate-dataset-imageai_mAP-0.22595_epoch-20.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3_number-plate-dataset-imageai_mAP-0.57145_epoch-11.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/idenprof.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/number-plate-dataset-imageai_tiny_yolov3_detection_config.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/number-plate-dataset-imageai_yolov3_detection_config.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/idenprof_model_classes.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/test-resources-v3/data-datasets.zip -P test
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/test-resources-v3/data-images.zip -P test
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/test-resources-v3/data-videos.zip -P test
unzip test/data-datasets.zip -d test
unzip test/data-images.zip -d test
unzip test/data-videos.zip -d test
- name: Run Unittest
run: |
pytest test -vvv
UnitestPython310:
name: Python3.10 Tests
runs-on: ubuntu-latest
# needs: None
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
cache: 'pip'
- name: Install Dependencies
run: |
pip install -r requirements.txt
pip install -r requirements_extra.txt
- name: Download and Setup Resources
env:
CI: false
run: |
sudo apt-get update
sudo apt-get install unzip -y
mkdir test/data-models
mkdir test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/densenet121-a639ec97.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/densenet121-idenprof-test_acc_0.82550_epoch-95.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/inception_v3-idenprof-test_acc_0.81050_epoch-92.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/inception_v3_google-1a9a5a14.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/mobilenet_v2-b0353104.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/mobilenet_v2-idenprof-test_acc_0.85300_epoch-92.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/resnet50-19c8e357.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/resnet50-idenprof-test_acc_0.78200_epoch-91.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/retinanet_resnet50_fpn_coco-eeacb38b.pth -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/tiny-yolov3.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/tiny_yolov3_number-plate-dataset-imageai_mAP-0.22595_epoch-20.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3_number-plate-dataset-imageai_mAP-0.57145_epoch-11.pt -P test/data-models
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/idenprof.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/number-plate-dataset-imageai_tiny_yolov3_detection_config.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/number-plate-dataset-imageai_yolov3_detection_config.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/idenprof_model_classes.json -P test/data-json
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/test-resources-v3/data-datasets.zip -P test
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/test-resources-v3/data-images.zip -P test
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/test-resources-v3/data-videos.zip -P test
unzip test/data-datasets.zip -d test
unzip test/data-images.zip -d test
unzip test/data-videos.zip -d test
- name: Run Unittest
run: |
pytest test -vvv
================================================
FILE: .gitignore
================================================
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
# Other files and folders
test/data-models
test/data-images
test/data-json
test/data-videos
test/data-datasets
experiment
================================================
FILE: .travis.yml
================================================
dist: xenial
sudo: required
language: python
python:
- '3.7.6'
install:
- pip install -r requirements.txt
- pip install pytest
- pip install pytest-cov
script:
- python setup.py install
- cd test
- mkdir data-models
- mkdir data-temp
- wget -P data-models/ https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/DenseNet-BC-121-32.h5
- wget -P data-models/ https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/inception_v3_weights_tf_dim_ordering_tf_kernels.h5
- wget -P data-models/ https://github.com/OlafenwaMoses/ImageAI/releases/download/essentials-v5/resnet50_imagenet_tf.2.0.h5
- wget -P data-models/ https://github.com/OlafenwaMoses/ImageAI/releases/download/essentials-v5/mobilenet_v2.h5
- wget -P data-models/ https://github.com/OlafenwaMoses/ImageAI/releases/download/models-v3/idenprof_densenet-0.763500.h5
- wget -P data-models/ https://github.com/OlafenwaMoses/ImageAI/releases/download/models-v3/idenprof_full_resnet_ex-001_acc-0.119792.h5
- wget -P data-models/ https://github.com/OlafenwaMoses/ImageAI/releases/download/essentials-v5/idenprof_resnet_ex-056_acc-0.993062.h5
- wget -P data-models/ https://github.com/OlafenwaMoses/ImageAI/releases/download/essentials-v5/resnet50_coco_best_v2.1.0.h5
- wget -P data-models/ https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo.h5
- wget -P data-models/ https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo-tiny.h5
- wget -P data-models/ https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/pretrained-yolov3.h5
- wget -P data-models/ https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/hololens-ex-60--loss-2.76.h5
- pytest -v --cov
after_script:
- bash <(curl -s https://codecov.io/bash)
================================================
FILE: BACKEND_MIGRATION.md
================================================
# Overview
In December 2022, ImageAI `3.0.2` was released which effected the change from Tensorflow backend to PyTorch backend. This change allows ImageAI to support `Python 3.7` up to `Python 3.10` for all its features and deprecates a number of functionalities for this and future versions of ImageAI.
# Deprecated functionalities
- Tensorflow backend no longer supported. Now replaced with PyTorch
- All `.h5` pretrained models and custom trained `.h5` models no longer supported. If you still intend to use these models, see the `Using Tensorflow backend` section.
- `Speed mode` have been removed from model loading
- Custom detection model training dataset format changed to YOLO format from Pascal VOC. To convert your dataset to YOLO format, see the `Convert Pascal VOC dataset to YOLO format` section.
- Enhance data for custom classification model training now removed
- Detection model training standalone evaluation now removed
# Using Tensorflow backend
To use Tensorflow backend, do the following
- Install Python 3.7
- Install Tensorflow
- CPU: `pip install tensorflow==2.4.0`
- GPU: `pip install tensorflow-gpu==2.4.0`
- Install other dependencies: `pip install keras==2.4.3 numpy==1.19.3 pillow==7.0.0 scipy==1.4.1 h5py==2.10.0 matplotlib==3.3.2 opencv-python keras-resnet==0.2.0`
- Install ImageAI **2.1.6**: `pip install imageai==2.1.6`
- Download the Tensorflow models from the releases below
- [Models for Image Recognition and Object Detection](https://github.com/OlafenwaMoses/ImageAI/releases/tag/1.0)
- [TF2.x Models [ Exclusives ]](https://github.com/OlafenwaMoses/ImageAI/releases/tag/essentials-v5)
# Convert Pascal VOC dataset to YOLO format
Because ImageAI now uses `YOLO format` for training custom object detection models; should you need to train a new model with the new ImageAI version, you will need to convert your `Pascal VOC` datasets to YOLO format by doing the following
- Run the command below
```
python scripts/pascal_voc_to_yolo.py --dataset_dir <path_to_your_dataset_folder>
```
- Once completed, you will find the YOLO version of the dataset next to your Pascal VOC dataset.
- E.g, if your dataset is in `C:/Users/Troublemaker/Documents/datasets/headset`, your conversion command will be
```
python scripts/pascal_voc_to_yolo.py --dataset_dir C:/Users/Troublemaker/Documents/datasets/headset
```
and once completed, the output will be in `C:/Users/Troublemaker/Documents/datasets/headset-yolo`
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2019 MOSES OLAFENWA
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: MANIFEST.in
================================================
recursive-include imageai/Detection *.txt
recursive-include imageai/Classification *.txt
================================================
FILE: README.md
================================================
# ImageAI (v3.0.3)
[](https://travis-ci.com/OlafenwaMoses/ImageAI) [](https://github.com/OlafenwaMoses/ImageAI/blob/master/LICENSE) [](https://badge.fury.io/py/imageai) [](https://pepy.tech/project/imageai) [](https://pepy.tech/project/imageai)
An open-source python library built to empower developers to build applications and systems with self-contained Deep Learning and Computer Vision capabilities using simple and few lines of code.
If you will like to sponsor this project, kindly visit the <strong>[Github sponsor page](https://github.com/sponsors/OlafenwaMoses)</strong>.
## ---------------------------------------------------
## Introducing Jarvis and TheiaEngine.
We the creators of ImageAI are glad to announce 2 new AI projects to provide state-of-the-art Generative AI, LLM and Image Understanding on your personal computer and servers.
[](https://jarvis.genxr.co)
Install Jarvis on PC/Mac to setup limitless access to LLM powered AI Chats for your every day work, research and generative AI needs with 100% privacy and full offline capability.
Visit [https://jarvis.genxr.co](https://jarvis.genxr.co/) to get started.
[](https://www.genxr.co/theia-engine)
[TheiaEngine](https://www.genxr.co/theia-engine), the next-generation computer Vision AI API capable of all Generative and Understanding computer vision tasks in a single API call and available via REST API to all programming languages. Features include
- **Detect 300+ objects** ( 220 more objects than ImageAI)
- **Provide answers to any content or context questions** asked on an image
- very useful to get information on any object, action or information without needing to train a new custom model for every tasks
- **Generate scene description and summary**
- **Convert 2D image to 3D pointcloud and triangular mesh**
- **Semantic Scene mapping of objects, walls, floors, etc**
- **Stateless Face recognition and emotion detection**
- **Image generation and augmentation from prompt**
- etc.
Visit [https://www.genxr.co/theia-engine](https://www.genxr.co/theia-engine) to try the demo and join in the beta testing today.
## ---------------------------------------------------

Developed and maintained by [Moses Olafenwa](https://twitter.com/OlafenwaMoses)
---
Built with simplicity in mind, **ImageAI**
supports a list of state-of-the-art Machine Learning algorithms for image prediction, custom image prediction, object detection, video detection, video object tracking
and image predictions trainings. **ImageAI** currently supports image prediction and training using 4 different Machine Learning algorithms
trained on the ImageNet-1000 dataset. **ImageAI** also supports object detection, video detection and object tracking using RetinaNet, YOLOv3 and TinyYOLOv3 trained on COCO dataset. Finally, **ImageAI** allows you to train custom models for performing detection and recognition of new objects.
Eventually, **ImageAI** will provide support for a wider and more specialized aspects of Computer Vision
**New Release : ImageAI 3.0.2**
What's new:
- PyTorch backend
- TinyYOLOv3 model training
### TABLE OF CONTENTS
- <a href="#installation" > :white_square_button: Installation</a>
- <a href="#features" > :white_square_button: Features</a>
- <a href="#documentation" > :white_square_button: Documentation</a>
- <a href="#sponsors" > :white_square_button: Sponsors</a>
- <a href="#sample" > :white_square_button: Projects Built on ImageAI</a>
- <a href="#real-time-and-high-performance-implementation" > :white_square_button: High Performance Implementation</a>
- <a href="#recommendation" > :white_square_button: AI Practice Recommendations</a>
- <a href="#contact" > :white_square_button: Contact Developers</a>
- <a href="#citation" > :white_square_button: Citation</a>
- <a href="#ref" > :white_square_button: References</a>
## Installation
<div id="installation"></div>
To install ImageAI, run the python installation instruction below in the command line:
- [Download and Install](https://www.python.org/downloads/) **Python 3.7**, **Python 3.8**, **Python 3.9** or **Python 3.10**
- Install dependencies
- **CPU**: Download [requirements.txt](https://github.com/OlafenwaMoses/ImageAI/blob/master/requirements.txt) file and install via the command
```
pip install -r requirements.txt
```
or simply copy and run the command below
```
pip install cython pillow>=7.0.0 numpy>=1.18.1 opencv-python>=4.1.2 torch>=1.9.0 --extra-index-url https://download.pytorch.org/whl/cpu torchvision>=0.10.0 --extra-index-url https://download.pytorch.org/whl/cpu pytest==7.1.3 tqdm==4.64.1 scipy>=1.7.3 matplotlib>=3.4.3 mock==4.0.3
```
- **GPU/CUDA**: Download [requirements_gpu.txt](https://github.com/OlafenwaMoses/ImageAI/blob/master/requirements_gpu.txt) file and install via the command
```
pip install -r requirements_gpu.txt
```
or smiply copy and run the command below
```
pip install cython pillow>=7.0.0 numpy>=1.18.1 opencv-python>=4.1.2 torch>=1.9.0 --extra-index-url https://download.pytorch.org/whl/cu102 torchvision>=0.10.0 --extra-index-url https://download.pytorch.org/whl/cu102 pytest==7.1.3 tqdm==4.64.1 scipy>=1.7.3 matplotlib>=3.4.3 mock==4.0.3
```
- If you plan to train custom AI models, download [requirements_extra.txt](https://github.com/OlafenwaMoses/ImageAI/blob/master/requirements_extra.txt) file and install via the command
```
pip install -r requirements_extra.txt
```
or simply copy and run the command below
```
pip install pycocotools@git+https://github.com/gautamchitnis/cocoapi.git@cocodataset-master#subdirectory=PythonAPI
```
- Then run the command below to install ImageAI
```
pip install imageai --upgrade
```
## Features
<div id="features"></div>
<table>
<tr>
<td><h2> Image Classification</h2> </td>
</tr>
<tr>
<td><img src="data-images/1.jpg" >
<h4>ImageAI provides 4 different algorithms and model types to perform image prediction, trained on the ImageNet-1000 dataset. The 4 algorithms provided for image prediction include MobileNetV2, ResNet50, InceptionV3 and DenseNet121.
Click the link below to see the full sample codes, explanations and best practices guide.</h4>
<a href="imageai/Classification"> >>> Get Started</a>
</td>
</tr>
</table>
<div id="features"></div>
<table>
<tr>
<td><h2> Object Detection </h2> </td>
</tr>
<tr>
<td>
<img src="data-images/image2new.jpg">
<h4>ImageAI provides very convenient and powerful methods to perform object detection on images and extract each object from the image. The object detection class provides support for RetinaNet, YOLOv3 and TinyYOLOv3, with options to adjust for state of the art performance or real time processing. Click the link below to see the full sample codes, explanations and best practices guide.</h4>
<a href="imageai/Detection"> >>> Get Started</a>
</td>
</tr>
</table>
<table>
<tr>
<td><h2> Video Object Detection & Analysis</h2> </td>
</tr>
<tr>
<td><img src="data-images/video_analysis_visualization.jpg">
<h4>ImageAI provides very convenient and powerful methods to perform object detection in videos. The video object detection class provided only supports the current state-of-the-art RetinaNet. Click the link to see the full videos, sample codes, explanations and best practices guide.</h4>
<a href="imageai/Detection/VIDEO.md"> >>> Get Started</a>
</td>
</tr>
</table>
<table>
<tr>
<td><h2> Custom Classification model training </h2> </td>
</tr>
<tr>
<td>
<img src="data-images/idenprof.jpg">
<h4>ImageAI provides classes and methods for you to train a new model that can be used to perform prediction on your own custom objects. You can train your custom models using MobileNetV2, ResNet50, InceptionV3 and DenseNet in 5 lines of code. Click the link below to see the guide to preparing training images, sample training codes, explanations and best practices.</h4>
<a href="imageai/Classification/CUSTOMTRAINING.md"> >>> Get Started</a>
</td>
</tr>
</table>
<table>
<tr>
<td><h2> Custom Model Classification</h2> </td>
</tr>
<tr>
<td><img src="data-images/4.jpg">
<h4>ImageAI provides classes and methods for you to run image prediction your own custom objects using your own model trained with ImageAI Model Training class. You can use your custom models trained with MobileNetV2, ResNet50, InceptionV3 and DenseNet and the JSON file containing the mapping of the custom object names. Click the link below to see the guide to sample training codes, explanations, and best practices guide.</h4>
<a href="imageai/Classification/CUSTOMCLASSIFICATION.md"> >>> Get Started</a>
</td>
</tr>
</table>
<table>
<tr>
<td><h2> Custom Detection Model Training </h2> </td>
</tr>
<tr>
<td>
<img src="data-images/headsets.jpg">
<h4>ImageAI provides classes and methods for you to train new YOLOv3 or TinyYOLOv3 object detection models on your custom dataset. This means you can train a model to detect literally any object of interest by providing the images, the annotations and training with ImageAI. Click the link below to see the guide to sample training codes, explanations, and best practices guide.</h4>
<a href="imageai/Detection/Custom/CUSTOMDETECTIONTRAINING.md"> >>> Get Started</a>
</td>
</tr>
</table>
<table>
<tr>
<td><h2> Custom Object Detection</h2> </td>
</tr>
<tr>
<td><img src="data-images/holo2-detected.jpg">
<h4>ImageAI now provides classes and methods for you detect and recognize your own custom objects in images using your own model trained with the DetectionModelTrainer class. You can use your custom trained YOLOv3 or TinyYOLOv3 model and the **.json** file generated during the training. Click the link below to see the guide to sample training codes, explanations, and best practices guide.</h4>
<a href="imageai/Detection/Custom/CUSTOMDETECTION.md"> >>> Get Started</a>
</td>
</tr>
</table>
<table>
<tr>
<td><h2> Custom Video Object Detection & Analysis </h2> </td>
</tr>
<tr>
<td>
<img src="data-images/customvideodetection.gif">
<h4>ImageAI now provides classes and methods for you detect and recognize your own custom objects in images using your own model trained with the DetectionModelTrainer class. You can use your custom trained YOLOv3 or TinyYOLOv3 model and the **.json** file generated during the training. Click the link below to see the guide to sample training codes, explanations, and best practices guide.</h4>
<a href="imageai/Detection/Custom/CUSTOMVIDEODETECTION.md"> >>> Get Started</a>
</td>
</tr>
</table>
## Documentation
<div id="documentation"></div>
We have provided full documentation for all **ImageAI** classes and functions. Visit the link below:
- Documentation - **English Version** [https://imageai.readthedocs.io](https://imageai.readthedocs.io)
## Sponsors
<div id="sponsors"></div>
## Real-Time and High Performance Implementation
<div id="performance"></div>
**ImageAI** provides abstracted and convenient implementations of state-of-the-art Computer Vision technologies. All of **ImageAI** implementations and code can work on any computer system with moderate CPU capacity. However, the speed of processing for operations like image prediction, object detection and others on CPU is slow and not suitable for real-time applications. To perform real-time Computer Vision operations with high performance, you need to use GPU enabled technologies.
**ImageAI** uses the PyTorch backbone for it's Computer Vision operations. PyTorch supports both CPUs and GPUs ( Specifically NVIDIA GPUs. You can get one for your PC or get a PC that has one) for machine learning and artificial intelligence algorithms' implementations.
## Projects Built on ImageAI
<div id="sample"></div>
## AI Practice Recommendations
<div id="recommendation"></div>
For anyone interested in building AI systems and using them for business, economic, social and research purposes, it is critical that the person knows the likely positive, negative and unprecedented impacts the use of such technologies will have.
They must also be aware of approaches and practices recommended by experienced industry experts to ensure every use of AI brings overall benefit to mankind.
We therefore recommend to everyone that wishes to use ImageAI and other AI tools and resources to read Microsoft's January 2018 publication on AI titled "The Future Computed : Artificial Intelligence and its role in society".
Kindly follow the link below to download the publication.
[https://blogs.microsoft.com/blog/2018/01/17/future-computed-artificial-intelligence-role-society](https://blogs.microsoft.com/blog/2018/01/17/future-computed-artificial-intelligence-role-society/)
### Contact Developer
<div id="contact"></div>
- **Moses Olafenwa**
* _Email:_ guymodscientist@gmail.com
* _Twitter:_ [@OlafenwaMoses](https://twitter.com/OlafenwaMoses)
* _Medium:_ [@guymodscientist](https://medium.com/@guymodscientist)
* _Facebook:_ [moses.olafenwa](https://facebook.com/moses.olafenwa)
- **John Olafenwa**
* _Email:_ johnolafenwa@gmail.com
* _Website:_ [https://john.aicommons.science](https://john.aicommons.science)
* _Twitter:_ [@johnolafenwa](https://twitter.com/johnolafenwa)
* _Medium:_ [@johnolafenwa](https://medium.com/@johnolafenwa)
* _Facebook:_ [olafenwajohn](https://facebook.com/olafenwajohn)
### Citation
<div id="citation"></div>
You can cite **ImageAI** in your projects and research papers via the **BibTeX** entry below.
```
@misc {ImageAI,
author = "Moses",
title = "ImageAI, an open source python library built to empower developers to build applications and systems with self-contained Computer Vision capabilities",
url = "https://github.com/OlafenwaMoses/ImageAI",
month = "mar",
year = "2018--"
}
```
### References
<div id="ref"></div>
1. Somshubra Majumdar, DenseNet Implementation of the paper, Densely Connected Convolutional Networks in Keras
[https://github.com/titu1994/DenseNet](https://github.com/titu1994/DenseNet)
2. Broad Institute of MIT and Harvard, Keras package for deep residual networks
[https://github.com/broadinstitute/keras-resnet](https://github.com/broadinstitute/keras-resnet)
3. Fizyr, Keras implementation of RetinaNet object detection
[https://github.com/fizyr/keras-retinanet](https://github.com/fizyr/keras-retinanet)
4. Francois Chollet, Keras code and weights files for popular deeplearning models
[https://github.com/fchollet/deep-learning-models](https://github.com/fchollet/deep-learning-models)
5. Forrest N. et al, SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size
[https://arxiv.org/abs/1602.07360](https://arxiv.org/abs/1602.07360)
6. Kaiming H. et al, Deep Residual Learning for Image Recognition
[https://arxiv.org/abs/1512.03385](https://arxiv.org/abs/1512.03385)
7. Szegedy. et al, Rethinking the Inception Architecture for Computer Vision
[https://arxiv.org/abs/1512.00567](https://arxiv.org/abs/1512.00567)
8. Gao. et al, Densely Connected Convolutional Networks
[https://arxiv.org/abs/1608.06993](https://arxiv.org/abs/1608.06993)
9. Tsung-Yi. et al, Focal Loss for Dense Object Detection
[https://arxiv.org/abs/1708.02002](https://arxiv.org/abs/1708.02002)
10. O Russakovsky et al, ImageNet Large Scale Visual Recognition Challenge
[https://arxiv.org/abs/1409.0575](https://arxiv.org/abs/1409.0575)
11. TY Lin et al, Microsoft COCO: Common Objects in Context
[https://arxiv.org/abs/1405.0312](https://arxiv.org/abs/1405.0312)
12. Moses & John Olafenwa, A collection of images of identifiable professionals.
[https://github.com/OlafenwaMoses/IdenProf](https://github.com/OlafenwaMoses/IdenProf)
13. Joseph Redmon and Ali Farhadi, YOLOv3: An Incremental Improvement.
[https://arxiv.org/abs/1804.02767](https://arxiv.org/abs/1804.02767)
14. Experiencor, Training and Detecting Objects with YOLO3
[https://github.com/experiencor/keras-yolo3](https://github.com/experiencor/keras-yolo3)
15. MobileNetV2: Inverted Residuals and Linear Bottlenecks
[https://arxiv.org/abs/1801.04381](https://arxiv.org/abs/1801.04381)
16. YOLOv3 in PyTorch > ONNX > CoreML > TFLite [https://github.com/ultralytics/yolov3](https://github.com/ultralytics/yolov3)
================================================
FILE: examples/camera_feed_detection.py
================================================
from imageai.Detection import VideoObjectDetection
import os
import cv2
execution_path = os.getcwd()
camera = cv2.VideoCapture(0)
detector = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path , "yolov3.pt")) # Download the model via this link https://github.com/OlafenwaMoses/ImageAI/releases/tag/1.0
detector.loadModel()
video_path = detector.detectObjectsFromVideo(camera_input=camera,
output_file_path=os.path.join(execution_path, "camera_detected_video")
, frames_per_second=20, log_progress=True, minimum_percentage_probability=30)
print(video_path)
================================================
FILE: examples/custom_detection.py
================================================
from imageai.Detection.Custom import CustomObjectDetection
detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt") # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt
detector.setJsonPath("hololens-yolo_yolov3_detection_config.json") # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/hololens-yolo_yolov3_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]
"""
================================================
FILE: examples/custom_detection_array_input_output.py
================================================
from imageai.Detection.Custom import CustomObjectDetection
import cv2
image_array = cv2.imread("holo2.jpg")
detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt") # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt
detector.setJsonPath("hololens-yolo_yolov3_detection_config.json") # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/hololens-yolo_yolov3_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]
"""
================================================
FILE: examples/custom_detection_extract_objects.py
================================================
from imageai.Detection.Custom import CustomObjectDetection
detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt") # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt
detector.setJsonPath("hololens-yolo_yolov3_detection_config.json") # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/hololens-yolo_yolov3_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-objects\hololens-1.jpg
hololens : 39.69653248786926 : [611, 74, 751, 154]
---------------
holo2-detected-objects\hololens-1.jpg
hololens : 87.6643180847168 : [23, 46, 90, 79]
---------------
holo2-detected-objects\hololens-1.jpg
hololens : 89.25175070762634 : [191, 66, 243, 95]
---------------
holo2-detected-objects\hololens-1.jpg
hololens : 64.49641585350037 : [437, 81, 514, 133]
---------------
holo2-detected-objects\hololens-1.jpg
hololens : 91.78624749183655 : [380, 113, 423, 138]
---------------
"""
================================================
FILE: examples/custom_detection_from_array_extract_objects_array.py
================================================
from imageai.Detection.Custom import CustomObjectDetection
import cv2
image_array = cv2.imread("holo2.jpg")
detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt") # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt
detector.setJsonPath("hololens-yolo_yolov3_detection_config.json") # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/hololens-yolo_yolov3_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]
"""
================================================
FILE: examples/custom_detection_from_file_extract_objects_array.py
================================================
from imageai.Detection.Custom import CustomObjectDetection
import cv2
detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt") # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt
detector.setJsonPath("hololens-yolo_yolov3_detection_config.json") # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/hololens-yolo_yolov3_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]
"""
================================================
FILE: examples/custom_detection_train.py
================================================
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="yolov3.pt")
#download pre-trained model via https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3.pt
# If you are training to detect more than 1 object, set names of objects above like object_names_array=["hololens", "google-glass", "oculus", "magic-leap"]
trainer.trainModel()
"""
SAMPLE RESULT
Generating anchor boxes for training images...
thr=0.25: 1.0000 best possible recall, 6.93 anchors past thr
n=9, img_size=416, metric_all=0.463/0.856-mean/best, past_thr=0.549-mean:
====================
Pretrained YOLOv3 model loaded to initialize weights
====================
Epoch 1/100
----------
Train:
30it [00:14, 2.09it/s]
box loss-> 0.09820, object loss-> 0.27985, class loss-> 0.00000
Validation:
15it [01:45, 7.05s/it]
recall: 0.085714 precision: 0.000364 mAP@0.5: 0.000186, mAP@0.5-0.95: 0.000030
Epoch 2/100
----------
Train:
30it [00:07, 4.25it/s]
box loss-> 0.08691, object loss-> 0.07011, class loss-> 0.00000
Validation:
15it [01:37, 6.53s/it]
recall: 0.214286 precision: 0.000854 mAP@0.5: 0.000516, mAP@0.5-0.95: 0.000111
"""
================================================
FILE: examples/custom_detection_video.py
================================================
from imageai.Detection.Custom import CustomVideoObjectDetection
import os
execution_path = os.getcwd()
video_detector = CustomVideoObjectDetection()
video_detector.setModelTypeAsYOLOv3()
video_detector.setModelPath("yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt") # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt
video_detector.setJsonPath("hololens-yolo_yolov3_detection_config.json") # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/hololens-yolo_yolov3_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)
================================================
FILE: examples/custom_model_prediction.py
================================================
from imageai.Classification.Custom import CustomImageClassification
import os
execution_path = os.getcwd()
prediction = CustomImageClassification()
prediction.setModelTypeAsResNet50()
prediction.setModelPath(os.path.join(execution_path, "resnet50-idenprof-test_acc_0.78200_epoch-91.pt")) # Download the model via this link https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/resnet50-idenprof-test_acc_0.78200_epoch-91.pt
prediction.setJsonPath(os.path.join(execution_path, "idenprof_model_classes.json")) # Download from here https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/idenprof_model_classes.json
prediction.loadModel(num_objects=10)
predictions, probabilities = prediction.classifyImage(os.path.join(execution_path, "9.jpg"), result_count=5)
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction , " : " , eachProbability)
================================================
FILE: examples/custom_model_training.py
================================================
from imageai.Classification.Custom import ClassificationModelTrainer
model_trainer = ClassificationModelTrainer()
model_trainer.setModelTypeAsResNet50()
model_trainer.setDataDirectory("idenprof")
model_trainer.trainModel(num_experiments=200, batch_size=32)
================================================
FILE: examples/image_custom_object_detection.py
================================================
from imageai.Detection import ObjectDetection
import os
from time import time
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath( os.path.join(execution_path , "yolov3.pt")) # Download the model via this link https://github.com/OlafenwaMoses/ImageAI/releases/tag/1.0
detector.loadModel()
our_time = time()
custom = detector.CustomObjects(bicycle=True, backpack=True)
detections = detector.detectCustomObjectsFromImage( custom_objects=custom, input_image=os.path.join(execution_path , "7.jpg"), output_image_path=os.path.join(execution_path , "7-detected.jpg"), minimum_percentage_probability=40)
for eachObject in detections:
print(eachObject["name"] , " : " , eachObject["percentage_probability"], " : ", eachObject["box_points"] )
print("--------------------------------")
================================================
FILE: examples/image_prediction.py
================================================
from imageai.Classification import ImageClassification
import os
execution_path = os.getcwd()
prediction = ImageClassification()
prediction.setModelTypeAsResNet50()
prediction.setModelPath(os.path.join(execution_path, "resnet50-19c8e357.pth")) # Download the model via this link https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/resnet50-19c8e357.pth
prediction.loadModel()
predictions, probabilities = prediction.classifyImage(os.path.join(execution_path, "1.jpg"), result_count=10)
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction , " : " , eachProbability)
================================================
FILE: examples/object_detection.py
================================================
from imageai.Detection import ObjectDetection
import os
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "retinanet_resnet50_fpn_coco-eeacb38b.pth")) # Download the model via this link https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/retinanet_resnet50_fpn_coco-eeacb38b.pth
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "2.jpg"), output_image_path=os.path.join(execution_path , "2_detected.jpg"), minimum_percentage_probability=40)
for eachObject in detections:
print(eachObject["name"] , " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"] )
print("--------------------------------")
================================================
FILE: examples/video_analysis_per_frame.py
================================================
from imageai.Detection import VideoObjectDetection
import os
from matplotlib import pyplot as plt
execution_path = os.getcwd()
color_index = {'bus': 'red', 'handbag': 'steelblue', 'giraffe': 'orange', 'spoon': 'gray', 'cup': 'yellow', 'chair': 'green', 'elephant': 'pink', 'truck': 'indigo', 'motorcycle': 'azure', 'refrigerator': 'gold', 'keyboard': 'violet', 'cow': 'magenta', 'mouse': 'crimson', 'sports ball': 'raspberry', 'horse': 'maroon', 'cat': 'orchid', 'boat': 'slateblue', 'hot dog': 'navy', 'apple': 'cobalt', 'parking meter': 'aliceblue', 'sandwich': 'skyblue', 'skis': 'deepskyblue', 'microwave': 'peacock', 'knife': 'cadetblue', 'baseball bat': 'cyan', 'oven': 'lightcyan', 'carrot': 'coldgrey', 'scissors': 'seagreen', 'sheep': 'deepgreen', 'toothbrush': 'cobaltgreen', 'fire hydrant': 'limegreen', 'remote': 'forestgreen', 'bicycle': 'olivedrab', 'toilet': 'ivory', 'tv': 'khaki', 'skateboard': 'palegoldenrod', 'train': 'cornsilk', 'zebra': 'wheat', 'tie': 'burlywood', 'orange': 'melon', 'bird': 'bisque', 'dining table': 'chocolate', 'hair drier': 'sandybrown', 'cell phone': 'sienna', 'sink': 'coral', 'bench': 'salmon', 'bottle': 'brown', 'car': 'silver', 'bowl': 'maroon', 'tennis racket': 'palevilotered', 'airplane': 'lavenderblush', 'pizza': 'hotpink', 'umbrella': 'deeppink', 'bear': 'plum', 'fork': 'purple', 'laptop': 'indigo', 'vase': 'mediumpurple', 'baseball glove': 'slateblue', 'traffic light': 'mediumblue', 'bed': 'navy', 'broccoli': 'royalblue', 'backpack': 'slategray', 'snowboard': 'skyblue', 'kite': 'cadetblue', 'teddy bear': 'peacock', 'clock': 'lightcyan', 'wine glass': 'teal', 'frisbee': 'aquamarine', 'donut': 'mincream', 'suitcase': 'seagreen', 'dog': 'springgreen', 'banana': 'emeraldgreen', 'person': 'honeydew', 'surfboard': 'palegreen', 'cake': 'sapgreen', 'book': 'lawngreen', 'potted plant': 'greenyellow', 'toaster': 'ivory', 'stop sign': 'beige', 'couch': 'khaki'}
resized = False
def forFrame(frame_number, output_array, output_count, returned_frame):
plt.clf()
this_colors = []
labels = []
sizes = []
counter = 0
for eachItem in output_count:
counter += 1
labels.append(eachItem + " = " + str(output_count[eachItem]))
sizes.append(output_count[eachItem])
this_colors.append(color_index[eachItem])
global resized
if (resized == False):
manager = plt.get_current_fig_manager()
manager.resize(width=1000, height=500)
resized = True
plt.subplot(1, 2, 1)
plt.title("Frame : " + str(frame_number))
plt.axis("off")
plt.imshow(returned_frame, interpolation="none")
plt.subplot(1, 2, 2)
plt.title("Analysis: " + str(frame_number))
plt.pie(sizes, labels=labels, colors=this_colors, shadow=True, startangle=140, autopct="%1.1f%%")
plt.pause(0.01)
video_detector = VideoObjectDetection()
video_detector.setModelTypeAsYOLOv3()
video_detector.setModelPath(os.path.join(execution_path, "yolov3.pt")) # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3.pt
video_detector.loadModel()
plt.show()
video_detector.detectObjectsFromVideo(input_file_path=os.path.join(execution_path, "traffic.mp4"), output_file_path=os.path.join(execution_path, "video_frame_analysis") , frames_per_second=20, per_frame_function=forFrame, minimum_percentage_probability=30, return_detected_frame=True)
================================================
FILE: examples/video_analysis_per_second.py
================================================
from imageai.Detection import VideoObjectDetection
import os
from matplotlib import pyplot as plt
execution_path = os.getcwd()
color_index = {'bus': 'red', 'handbag': 'steelblue', 'giraffe': 'orange', 'spoon': 'gray', 'cup': 'yellow', 'chair': 'green', 'elephant': 'pink', 'truck': 'indigo', 'motorcycle': 'azure', 'refrigerator': 'gold', 'keyboard': 'violet', 'cow': 'magenta', 'mouse': 'crimson', 'sports ball': 'raspberry', 'horse': 'maroon', 'cat': 'orchid', 'boat': 'slateblue', 'hot dog': 'navy', 'apple': 'cobalt', 'parking meter': 'aliceblue', 'sandwich': 'skyblue', 'skis': 'deepskyblue', 'microwave': 'peacock', 'knife': 'cadetblue', 'baseball bat': 'cyan', 'oven': 'lightcyan', 'carrot': 'coldgrey', 'scissors': 'seagreen', 'sheep': 'deepgreen', 'toothbrush': 'cobaltgreen', 'fire hydrant': 'limegreen', 'remote': 'forestgreen', 'bicycle': 'olivedrab', 'toilet': 'ivory', 'tv': 'khaki', 'skateboard': 'palegoldenrod', 'train': 'cornsilk', 'zebra': 'wheat', 'tie': 'burlywood', 'orange': 'melon', 'bird': 'bisque', 'dining table': 'chocolate', 'hair drier': 'sandybrown', 'cell phone': 'sienna', 'sink': 'coral', 'bench': 'salmon', 'bottle': 'brown', 'car': 'silver', 'bowl': 'maroon', 'tennis racket': 'palevilotered', 'airplane': 'lavenderblush', 'pizza': 'hotpink', 'umbrella': 'deeppink', 'bear': 'plum', 'fork': 'purple', 'laptop': 'indigo', 'vase': 'mediumpurple', 'baseball glove': 'slateblue', 'traffic light': 'mediumblue', 'bed': 'navy', 'broccoli': 'royalblue', 'backpack': 'slategray', 'snowboard': 'skyblue', 'kite': 'cadetblue', 'teddy bear': 'peacock', 'clock': 'lightcyan', 'wine glass': 'teal', 'frisbee': 'aquamarine', 'donut': 'mincream', 'suitcase': 'seagreen', 'dog': 'springgreen', 'banana': 'emeraldgreen', 'person': 'honeydew', 'surfboard': 'palegreen', 'cake': 'sapgreen', 'book': 'lawngreen', 'potted plant': 'greenyellow', 'toaster': 'ivory', 'stop sign': 'beige', 'couch': 'khaki'}
resized = False
def forSecond(frame_number, output_arrays, count_arrays, average_count, returned_frame):
plt.clf()
this_colors = []
labels = []
sizes = []
counter = 0
for eachItem in average_count:
counter += 1
labels.append(eachItem + " = " + str(average_count[eachItem]))
sizes.append(average_count[eachItem])
this_colors.append(color_index[eachItem])
global resized
if (resized == False):
manager = plt.get_current_fig_manager()
manager.resize(width=1000, height=500)
resized = True
plt.subplot(1, 2, 1)
plt.title("Second : " + str(frame_number))
plt.axis("off")
plt.imshow(returned_frame, interpolation="none")
plt.subplot(1, 2, 2)
plt.title("Analysis: " + str(frame_number))
plt.pie(sizes, labels=labels, colors=this_colors, shadow=True, startangle=140, autopct="%1.1f%%")
plt.pause(0.01)
video_detector = VideoObjectDetection()
video_detector.setModelTypeAsYOLOv3()
video_detector.setModelPath(os.path.join(execution_path, "yolov3.pt")) # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3.pt
video_detector.loadModel()
plt.show()
video_detector.detectObjectsFromVideo(input_file_path=os.path.join(execution_path, "traffic.mp4"), output_file_path=os.path.join(execution_path, "video_second_analysis") , frames_per_second=20, per_second_function=forSecond, minimum_percentage_probability=30, return_detected_frame=True, log_progress=True)
================================================
FILE: examples/video_custom_object_detection.py
================================================
from imageai.Detection import VideoObjectDetection
import os
execution_path = os.getcwd()
detector = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path, "yolov3.pt")) # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3.pt
detector.loadModel()
custom = detector.CustomObjects(person=True, motorcycle=True, bus=True)
video_path = detector.detectCustomObjectsFromVideo(custom_objects=custom, input_file_path=os.path.join(execution_path, "traffic.mp4"),
output_file_path=os.path.join(execution_path, "traffic_detected_custom")
, frames_per_second=20, log_progress=True)
print(video_path)
================================================
FILE: examples/video_object_detection.py
================================================
from imageai.Detection import VideoObjectDetection
import os
execution_path = os.getcwd()
detector = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path, "yolov3.pt")) # https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3.pt
detector.loadModel()
video_path = detector.detectObjectsFromVideo(input_file_path=os.path.join(execution_path, "traffic.mp4"),
output_file_path=os.path.join(execution_path, "traffic_detected")
, frames_per_second=20, log_progress=True)
print(video_path)
================================================
FILE: imageai/Classification/CUSTOMCLASSIFICATION.md
================================================
# ImageAI : Custom Image Classification
ImageAI provides 4 different algorithms and model types to perform custom image prediction using your custom models.
You will be able to use your model trained with **ImageAI** and the corresponding model_class JSON file to predict custom objects
that you have trained the model on.
### TABLE OF CONTENTS
- <a href="#customprediction" > :white_square_button: Custom Model Prediction</a>
- <a href="#custompredictionfullmodel" > :white_square_button: Custom Model Prediction with Full Model (NEW)</a>
### Custom Model Prediction
<div id="customprediction"></div>
In this example, we will be using the model trained for 20 experiments on **IdenProf**, a dataset of uniformed professionals and achieved 65.17% accuracy on the test dataset.
(You can use your own trained model and generated JSON file. This 'class' is provided mainly for the purpose to use your own custom models.)
Download the ResNet model of the model and JSON files in links below:
- [**ResNet50**](https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/resnet50-idenprof-test_acc_0.78200_epoch-91.pt) _(Size = 90.4 mb)_
- [**idenprof_model_class.json file**](https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/idenprof_model_classes.json)
Great!
Once you have downloaded this model file and the JSON file, start a new python project, and then copy the model file and the JSON file to your project folder where your python files (.py files) will be.
Download the image below, or take any image on your computer that include any of the following professionals(Chef, Doctor, Engineer, Farmer, Fireman, Judge, Mechanic, Pilot, Police and Waiter) and copy it to your python project's folder.
Then create a python file and give it a name; an example is **FirstCustomPrediction.py**.
Then write the code below into the python file:
### FirstCustomPrediction.py
```python
from imageai.Classification.Custom import CustomImageClassification
import os
execution_path = os.getcwd()
prediction = CustomImageClassification()
prediction.setModelTypeAsResNet50()
prediction.setModelPath(os.path.join(execution_path, "resnet50-idenprof-test_acc_0.78200_epoch-91.pt"))
prediction.setJsonPath(os.path.join(execution_path, "idenprof_model_class.json"))
prediction.loadModel(num_objects=10)
predictions, probabilities = prediction.classifyImage(os.path.join(execution_path, "4.jpg"), result_count=5)
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction + " : " + eachProbability)
```
**Sample Result:**

```
mechanic : 76.82620286941528
chef : 10.106072574853897
waiter : 4.036874696612358
police : 2.6663416996598244
pilot : 2.239348366856575
```
The code above works as follows:
```python
from imageai.Classification.Custom import CustomImageClassification
import os
```
The code above imports the **ImageAI** library for custom image prediction and the python **os** class.
```python
execution_path = os.getcwd()
```
The above line obtains the path to the folder that contains your python file (in this example, your FirstCustomPrediction.py).
```python
prediction = CustomImageClassification()
prediction.setModelTypeAsResNet50()
prediction.setModelPath(os.path.join(execution_path, "resnet50-idenprof-test_acc_0.78200_epoch-91.pt"))
prediction.setJsonPath(os.path.join(execution_path, "idenprof_model_class.json"))
prediction.loadModel(num_objects=10)
```
In the lines above, we created and instance of the `CustomImageClassification()`
class in the first line, then we set the model type of the prediction object to ResNet by caling the `.setModelTypeAsResNet50()`
in the second line, we set the model path of the prediction object to the path of the custom model file (`resnet50-idenprof-test_acc_0.78200_epoch-91.pt`) we copied to the python file folder
in the third line, we set the path to the idenprof_model_class.json of the model, we load the model and parse the number of objected that can be predicted in the model.
```python
predictions, probabilities = prediction.classifyImage(os.path.join(execution_path, "4.jpg"), result_count=5)
```
In the above line, we defined 2 variables to be equal to the function called to predict an image, which is the `.classifyImage()` function, into which we parsed the path to our image and also state the number of prediction results we want to have (values from 1 to 10 in this case) parsing `result_count=5`. The `.classifyImage()` function will return 2 array objects with the first (**predictions**) being an array of predictions and the second (**percentage_probabilities**) being an array of the corresponding percentage probability for each prediction.
```python
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction + " : " + eachProbability)
```
The above line obtains each object in the **predictions** array, and also obtains the corresponding percentage probability from the **percentage_probabilities**, and finally prints the result of both to console.
**CustomImageClassification** class also supports the multiple predictions, input types and prediction speeds that are contained
in the **ImageClassification** class. Follow this [link](README.md) to see all the details.
### Documentation
We have provided full documentation for all **ImageAI** classes and functions in 3 major languages. Find links below:**
* Documentation - **English Version [https://imageai.readthedocs.io](https://imageai.readthedocs.io)**
================================================
FILE: imageai/Classification/CUSTOMTRAINING.md
================================================
# ImageAI : Custom Prediction Model Training
## ---------------------------------------------------
## Introducing Jarvis and TheiaEngine.
We the creators of ImageAI are glad to announce 2 new AI projects to provide state-of-the-art Generative AI, LLM and Image Understanding on your personal computer and servers.
[](https://jarvis.genxr.co)
Install Jarvis on PC/Mac to setup limitless access to LLM powered AI Chats for your every day work, research and generative AI needs with 100% privacy and full offline capability.
Visit [https://jarvis.genxr.co](https://jarvis.genxr.co/) to get started.
[]((https://www.genxr.co/theia-engine))
[TheiaEngine](https://www.genxr.co/theia-engine), the next-generation computer Vision AI API capable of all Generative and Understanding computer vision tasks in a single API call and available via REST API to all programming languages. Features include
- **Detect 300+ objects** ( 220 more objects than ImageAI)
- **Provide answers to any content or context questions** asked on an image
- very useful to get information on any object, action or information without needing to train a new custom model for every tasks
- **Generate scene description and summary**
- **Convert 2D image to 3D pointcloud and triangular mesh**
- **Semantic Scene mapping of objects, walls, floors, etc**
- **Stateless Face recognition and emotion detection**
- **Image generation and augmentation from prompt**
- etc.
Visit [https://www.genxr.co/theia-engine](https://www.genxr.co/theia-engine) to try the demo and join in the beta testing today.
## ---------------------------------------------------
**ImageAI** provides the most simple and powerful approach to training custom image prediction models
using state-of-the-art SqueezeNet, ResNet50, InceptionV3 and DenseNet
which you can load into the `imageai.Classification.Custom.CustomImageClassification` class. This allows
you to train your own model on any set of images that corresponds to any type of objects/persons.
The training process generates a JSON file that maps the objects types in your image dataset
and creates lots of models. You will then pick the model with the highest accuracy and perform custom
image prediction using the model and the JSON file generated.
### TABLE OF CONTENTS
- <a href="#customtraining" > :white_square_button: Custom Model Training Prediction</a>
- <a href="#savefullmodel" > :white_square_button: Saving Full Custom Model </a>
- <a href="#idenproftraining" > :white_square_button: Training on the IdenProf Dataset</a>
- <a href="#continuoustraining" > :white_square_button: Continuous Model Training </a>
- <a href="#transferlearning" > :white_square_button: Transfer Learning (Training from a pre-trained model)</a>
### Custom Model Training
<div id="customtraining"></div>
Because model training is a compute intensive tasks, we strongly advise you perform this experiment using a computer with a NVIDIA GPU and the GPU version of Tensorflow installed. Performing model training on CPU will my take hours or days. With NVIDIA GPU powered computer system, this will take a few hours. You can use Google Colab for this experiment as it has an NVIDIA K80 GPU available.
To train a custom prediction model, you need to prepare the images you want to use to train the model.
You will prepare the images as follows:
1. Create a dataset folder with the name you will like your dataset to be called (e.g pets)
2. In the dataset folder, create a folder by the name **train**
3. In the dataset folder, create a folder by the name **test**
4. In the train folder, create a folder for each object you want to the model to predict and give the folder a name that corresponds to the respective object name (e.g dog, cat, squirrel, snake)
5. In the test folder, create a folder for each object you want to the model to predict and give
the folder a name that corresponds to the respective object name (e.g dog, cat, squirrel, snake)
6. In each folder present in the train folder, put the images of each object in its respective folder. This images are the ones to be used to train the model To produce a model that can perform well in practical applications, I recommend you about 500 or more images per object. 1000 images per object is just great
7. In each folder present in the test folder, put about 100 to 200 images of each object in its respective folder. These images are the ones to be used to test the model as it trains
8. Once you have done this, the structure of your image dataset folder should look like below:
```
pets//train//dog//dog-train-images
pets//train//cat//cat-train-images
pets//train//squirrel//squirrel-train-images
pets//train//snake//snake-train-images
pets//test//dog//dog-test-images
pets//test//cat//cat-test-images
pets//test//squirrel//squirrel-test-images
pets//test//snake//snake-test-images
```
9. Then your training code goes as follows:
```python
from imageai.Classification.Custom import ClassificationModelTrainer
model_trainer = ClassificationModelTrainer()
model_trainer.setModelTypeAsResNet50()
model_trainer.setDataDirectory("pets")
model_trainer.trainModel(num_objects=4, num_experiments=100, enhance_data=True, batch_size=32, show_network_summary=True)
```
Yes! Just 5 lines of code and you can train any of the available 4 state-of-the-art Deep Learning algorithms on your custom dataset.
Now lets take a look at how the code above works.
```python
from imageai.Classification.Custom import ClassificationModelTrainer
model_trainer = ClassificationModelTrainer()
model_trainer.setModelTypeAsResNet50()
model_trainer.setDataDirectory("pets")
```
In the first line, we import the **ImageAI** model training class, then we define the model trainer in the second line,
we set the network type in the third line and set the path to the image dataset we want to train the network on.
```python
model_trainer.trainModel(num_experiments=100, batch_size=32)
```
In the code above, we start the training process. The parameters stated in the function are as below:
- **num_experiments** : this is to state the number of times the network will train over all the training images,
which is also called epochs
- **batch_size** : This is to state the number of images the network will process at ones. The images
are processed in batches until they are exhausted per each experiment performed.
When you start the training, you should see something like this in the console:
```
==================================================
Training with GPU
==================================================
Epoch 1/100
----------
100%|█████████████████████████████████████████████████████████████████████████████████| 282/282 [02:15<00:00, 2.08it/s]
train Loss: 3.8062 Accuracy: 0.1178
100%|███████████████████████████████████████████████████████████████████████████████████| 63/63 [00:26<00:00, 2.36it/s]
test Loss: 2.2829 Accuracy: 0.1215
Epoch 2/100
----------
100%|█████████████████████████████████████████████████████████████████████████████████| 282/282 [01:57<00:00, 2.40it/s]
train Loss: 2.2682 Accuracy: 0.1303
100%|███████████████████████████████████████████████████████████████████████████████████| 63/63 [00:20<00:00, 3.07it/s]
test Loss: 2.2388 Accuracy: 0.1470
```
Let us explain the details shown above:
1. The line **Epoch 1/100** means the network is training the first experiment of the targeted 100
2. The line `1/25 [>.............................] - ETA: 52s - loss: 2.3026 - acc: 0.2500` represents the number of batches that has been trained in the present experiment
3. The best model is automatically saved to `<dataset-directory>/models>`
Once you are done training your custom model, you can use the "CustomImageClassification" class to perform image prediction with your model. Simply follow the link below.
[imageai/Classification/CUSTOMCLASSIFICATION.md](https://github.com/OlafenwaMoses/ImageAI/blob/master/imageai/Classification/CUSTOMCLASSIFICATION.md)
### Documentation
We have provided full documentation for all **ImageAI** classes and functions. Find links below:
* Documentation - **English Version [https://imageai.readthedocs.io](https://imageai.readthedocs.io)**
================================================
FILE: imageai/Classification/Custom/__init__.py
================================================
import time, warnings
import os
import copy
import re
import json
from typing import List, Tuple, Union
from PIL import Image
import numpy as np
import torch
import torch.nn as nn
from torch.optim import lr_scheduler
from torchvision import datasets
from torchvision import transforms
from torchvision.models import mobilenet_v2, inception_v3, resnet50, densenet121
from torchvision.models.inception import InceptionOutputs
from .data_transformation import data_transforms1, data_transforms2
from .training_params import resnet50_train_params, densenet121_train_params, inception_v3_train_params, mobilenet_v2_train_params
from tqdm import tqdm
from ...backend_check.model_extension import extension_check
class ClassificationModelTrainer():
"""
This is the Classification Model training class, that allows you to define a deep learning network
from the 4 available networks types supported by ImageAI which are MobileNetv2, ResNet50,
InceptionV3 and DenseNet121 and then train on custom image data.
"""
def __init__(self) -> None:
self.__model_type = ""
self.__device = "cuda" if torch.cuda.is_available() else "cpu"
self.__data_dir = ""
self.__data_loaders = None
self.__class_names = None
self.__dataset_sizes = None
self.__dataset_name = ""
self.__model = None
self.__optimizer = None
self.__lr_scheduler = None
self.__loss_fn = nn.CrossEntropyLoss()
self.__transfer_learning_mode = "fine_tune_all"
self.__model_path = ""
self.__training_params = None
def __set_training_param(self) -> None:
if not self.__model_type:
raise RuntimeError("The model type is not set!!!")
self.__model = self.__training_params["model"]
optimizer = self.__training_params["optimizer"]
lr_decay_rate = self.__training_params["lr_decay_rate"]
lr_step_size = self.__training_params["lr_step_size"]
lr = self.__training_params["lr"]
weight_decay = self.__training_params["weight_decay"]
if self.__model_path:
self.__set_transfer_learning_mode()
print("==> Transfer learning enabled")
# change the last linear layer to have output features of
# same size as the number of unique classes in the new
# dataset.
if self.__model_type == "mobilenet_v2":
in_features = self.__model.classifier[1].in_features
self.__model.classifier[1] = nn.Linear(in_features, len(self.__class_names))
elif self.__model_type == "densenet121":
in_features = self.__model.classifier.in_features
self.__model.classifier = nn.Linear(in_features, len(self.__class_names))
else:
in_features = self.__model.fc.in_features
self.__model.fc = nn.Linear(in_features, len(self.__class_names))
self.__model.to(self.__device)
self.__optimizer = optimizer(
self.__model.parameters(),
lr=lr,
momentum=0.9,
weight_decay=weight_decay
)
if lr_decay_rate and lr_step_size:
self.__lr_scheduler = lr_scheduler.StepLR(
self.__optimizer,
gamma=lr_decay_rate,
step_size=lr_step_size
)
def __set_transfer_learning_mode(self) -> None:
state_dict = torch.load(self.__model_path)
if self.__model_type == "densenet121":
# '.'s are no longer allowed in module names, but previous densenet layers
# as provided by the pytorch organization has names that uses '.'s.
pattern = re.compile(
r"^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\."
"(?:weight|bias|running_mean|running_var))$"
)
for key in list(state_dict.keys()):
res = pattern.match(key)
if res:
new_key = res.group(1) + res.group(2)
state_dict[new_key] = state_dict[key]
del state_dict[key]
self.__model.load_state_dict(state_dict)
self.__model.to(self.__device)
if self.__transfer_learning_mode == "freeze_all":
for param in self.__model.parameters():
param.requires_grad = False
def __load_data(self, batch_size : int = 8) -> None:
if not self.__data_dir:
raise RuntimeError("The dataset directory not yet set.")
image_dataset = {
x:datasets.ImageFolder(
os.path.join(self.__data_dir, x),
data_transforms2[x] if self.__model_type=="inception_v3" else data_transforms1[x]
)
for x in ["train", "test"]
}
self.__data_loaders = {
x:torch.utils.data.DataLoader(
image_dataset[x], batch_size=batch_size,
shuffle=True
)
for x in ["train", "test"]
}
self.__dataset_sizes = {x:len(image_dataset[x]) for x in ["train", "test"]}
self.__class_names = image_dataset["train"].classes
self.__dataset_name = os.path.basename(self.__data_dir.rstrip(os.path.sep))
def setDataDirectory(self, data_directory : str = "") -> None:
"""
Sets the directory that contains the training and test dataset. The data directory should contain 'train' and 'test' subdirectories
for the training and test datasets.
In each of these subdirectories, each object must have a dedicated folder and the folder containing images for the object.
The structure of the 'test' and 'train' folder must be as follows:
>> train >> class1 >> class1_train_images
>> class2 >> class2_train_images
>> class3 >> class3_train_images
>> class4 >> class4_train_images
>> class5 >> class5_train_images
>> test >> class1 >> class1_test_images
>> class2 >> class2_test_images
>> class3 >> class3_test_images
>> class4 >> class4_test_images
>> class5 >> class5_test_images
"""
if os.path.isdir(data_directory):
self.__data_dir = data_directory
return
raise ValueError("expected a path to a directory")
def setModelTypeAsMobileNetV2(self) -> None:
"""
'setModelTypeAsMobileNetV2()' is used to set the model type to the MobileNetV2 model.
:return:
"""
self.__model_type = "mobilenet_v2"
self.__training_params = mobilenet_v2_train_params()
def setModelTypeAsResNet50(self) -> None:
"""
'setModelTypeAsResNet50()' is used to set the model type to the ResNet50 model.
:return:
"""
self.__model_type = "resnet50"
self.__training_params = resnet50_train_params()
def setModelTypeAsInceptionV3(self) -> None:
"""
'setModelTypeAsInceptionV3()' is used to set the model type to the InceptionV3 model.
:return:
"""
self.__model_type = "inception_v3"
self.__training_params = inception_v3_train_params()
def setModelTypeAsDenseNet121(self) -> None:
"""
'setModelTypeAsDenseNet()' is used to set the model type to the DenseNet model.
:return:
"""
self.__model_type = "densenet121"
self.__training_params = densenet121_train_params()
def freezeAllLayers(self) -> None:
"""
Set the transfer learning mode to freeze all layers.
NOTE: The last layer (fully connected layer) is trainable.
"""
self.__transfer_learning_mode = "freeze_all"
def fineTuneAllLayers(self) -> None:
"""
Sets the transfer learning mode to fine-tune the pretrained weights
"""
self.__transfer_learning_mode = "fine_tune_all"
def trainModel(
self,
num_experiments : int = 100,
batch_size : int = 8,
model_directory : str = None,
transfer_from_model: str = None,
verbose : bool = True
) -> None:
"""
'trainModel()' function starts the model actual training. It accepts the following values:
- num_experiments: Also known as epochs, is the number of times the network will process all the images in the training dataset
- batch_size: The number of image data that will be loaded into memory at once during training
- model_directory: Location where json mapping and trained models will be saved
- transfer_from_model: Path to a pre-trained imagenet model that corresponds to the training model type
- verbose: Option to enable/disable training logs
:param num_experiments:
:param batch_size:
:model_directory:
:transfer_from_model:
:verbose:
:return:
"""
# Load dataset
self.__load_data(batch_size)
# Check and effect transfer learning if enabled
if transfer_from_model:
extension_check(transfer_from_model)
self.__model_path = transfer_from_model
# Load training parameters for the specified model type
self.__set_training_param()
# Create output directory to save trained models and json mappings
if not model_directory:
model_directory = os.path.join(self.__data_dir, "models")
if not os.path.exists(model_directory):
os.mkdir(model_directory)
# Dump class mappings to json file
with open(os.path.join(model_directory, f"{self.__dataset_name}_model_classes.json"), "w") as f:
classes_dict = {}
class_list = sorted(self.__class_names)
for i in range(len(class_list)):
classes_dict[str(i)] = class_list[i]
json.dump(classes_dict, f)
# Prep model weights for training
since = time.time()
best_model_weights = copy.deepcopy(self.__model.state_dict())
best_acc = 0.0
prev_save_name, recent_save_name = "", ""
# Device check and log
print("=" * 50)
print("Training with GPU") if self.__device == "cuda" else print("Training with CPU. This might cause slower train.")
print("=" * 50)
for epoch in range(num_experiments):
if verbose:
print(f"Epoch {epoch + 1}/{num_experiments}", "-"*10, sep="\n")
# each epoch has a training and test phase
for phase in ["train", "test"]:
if phase == "train":
self.__model.train()
else:
self.__model.eval()
running_loss = 0.0
running_corrects = 0
# Iterate on the dataset in batches
for imgs, labels in tqdm(self.__data_loaders[phase]):
imgs = imgs.to(self.__device)
labels = labels.to(self.__device)
self.__optimizer.zero_grad()
with torch.set_grad_enabled(phase == "train"):
output = self.__model(imgs)
if self.__model_type == "inception_v3" and type(output) == InceptionOutputs:
output = output[0]
_, preds = torch.max(output, 1)
loss = self.__loss_fn(output, labels)
if phase=="train":
loss.backward()
self.__optimizer.step()
running_loss += loss.item() * imgs.size(0)
running_corrects += torch.sum(preds==labels.data)
# Compute accuracy and loss metrics post epoch training
if phase == "train" and isinstance(self.__lr_scheduler, torch.optim.lr_scheduler.StepLR):
self.__lr_scheduler.step()
epoch_loss = running_loss / self.__dataset_sizes[phase]
epoch_acc = running_corrects.double() / self.__dataset_sizes[phase]
if verbose:
print(f"{phase} Loss: {epoch_loss:.4f} Accuracy: {epoch_acc:.4f}")
if phase == "test" and epoch_acc > best_acc:
best_acc = epoch_acc
recent_save_name = self.__model_type+f"-{self.__dataset_name}-test_acc_{best_acc:.5f}_epoch-{epoch}.pt"
if prev_save_name:
os.remove(os.path.join(model_directory, prev_save_name))
best_model_weights = copy.deepcopy(self.__model.state_dict())
torch.save(
best_model_weights, os.path.join(model_directory, recent_save_name)
)
prev_save_name = recent_save_name
time_elapsed = time.time() - since
print(f"Training completed in {time_elapsed//60:.0f}m {time_elapsed % 60:.0f}s")
print(f"Best test accuracy: {best_acc:.4f}")
class CustomImageClassification:
"""
An implementation that allows for easy classification of images
using the state of the art computer vision classification model
trained on custom data.
The class provides 4 different classification models which are ResNet50, DensesNet121, InceptionV3 and MobileNetV2.
The following functions are required to be called before a classification can be made
* At least of of the following and it must correspond to the model set in the setModelPath()
[setModelTypeAsMobileNetV2(), setModelTypeAsResNet(), setModelTypeAsDenseNet, setModelTypeAsInceptionV3]
* setModelPath: This is used to specify the absolute path to the trained model file.
* setJsonPath: This is used to specify the absolute path to the
json file saved during the training of the custom model.
* useCPU (Optional): If you will like to force the image classification to be performed on CPU, call this function.
* loadModel: Used to load the trained model weights and json data.
* classifyImage(): Used for classifying an image.
"""
def __init__(self) -> None:
self.__model = None
self.__model_type = ""
self.__model_loaded = False
self.__device = "cuda" if torch.cuda.is_available() else "cpu"
self.__json_path = None
self.__class_names = None
self.__model_loaded = False
def __load_image(self, image_input: Union[str, np.ndarray, Image.Image]) -> torch.Tensor:
images = []
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
if type(image_input) == str:
if os.path.isfile(image_input):
img = Image.open(image_input).convert("RGB")
images.append(preprocess(img))
else:
raise ValueError(f"image path '{image_input}' is not found or a valid file")
elif type(image_input) == np.ndarray:
img = Image.fromarray(image_input).convert("RGB")
images.append(preprocess(img))
elif "PIL" in str(type(image_input)):
img = image_input.convert("RGB")
images.append(preprocess(img))
else:
raise ValueError(f"Invalid image input format")
return torch.stack(images)
def __load_classes(self):
if self.__json_path:
with open(self.__json_path, 'r') as f:
self.__class_names = list(json.load(f).values())
else:
raise ValueError("Invalid json path. Set a valid json mapping path by calling the 'setJsonPath()' function")
def setModelPath(self, path : str) -> None:
"""
Sets the path to the pretrained weight.
"""
if os.path.isfile(path):
extension_check(path)
self.__model_path = path
self.__model_loaded = False
else:
raise ValueError(
f"The path '{path}' isn't a valid file. Ensure you specify the path to a valid trained model file."
)
def setJsonPath(self, path : str) -> None:
"""
Sets the path to the pretrained weight.
"""
if os.path.isfile(path):
self.__json_path = path
else:
raise ValueError(
"parameter path should be a valid path to the json mapping file."
)
def setModelTypeAsMobileNetV2(self) -> None:
"""
'setModelTypeAsMobileNetV2()' is used to set the model type to the MobileNetV2 model.
:return:
"""
self.__model_type = "mobilenet_v2"
def setModelTypeAsResNet50(self) -> None:
"""
'setModelTypeAsResNet50()' is used to set the model type to the ResNet50 model.
:return:
"""
self.__model_type = "resnet50"
def setModelTypeAsInceptionV3(self) -> None:
"""
'setModelTypeAsInceptionV3()' is used to set the model type to the InceptionV3 model.
:return:
"""
self.__model_type = "inception_v3"
def setModelTypeAsDenseNet121(self) -> None:
"""
'setModelTypeAsDenseNet121()' is used to set the model type to the DenseNet121 model.
:return:
"""
self.__model_type = "densenet121"
def useCPU(self):
"""
Used to force classification to be done on CPU.
By default, classification will occur on GPU compute if available else CPU compute.
"""
self.__device = "cpu"
if self.__model_loaded:
self.__model_loaded = False
self.loadModel()
def loadModel(self) -> None:
"""
'loadModel()' function is used to load the model weights into the model architecture from the file path defined
in the setModelPath() function.
:return:
"""
if not self.__model_loaded:
self.__load_classes()
try:
# change the last layer of the networks to conform to the number
# of unique classes in the custom dataset used to train the custom
# model
if self.__model_type == "resnet50":
self.__model = resnet50(pretrained=False)
in_features = self.__model.fc.in_features
self.__model.fc = nn.Linear(in_features, len(self.__class_names))
elif self.__model_type == "mobilenet_v2":
self.__model = mobilenet_v2(pretrained=False)
in_features = self.__model.classifier[1].in_features
self.__model.classifier[1] = nn.Linear(in_features, len(self.__class_names))
elif self.__model_type == "inception_v3":
self.__model = inception_v3(pretrained=False)
in_features = self.__model.fc.in_features
self.__model.fc = nn.Linear(in_features, len(self.__class_names))
elif self.__model_type == "densenet121":
self.__model = densenet121(pretrained=False)
in_features = self.__model.classifier.in_features
self.__model.classifier = nn.Linear(in_features, len(self.__class_names))
else:
raise RuntimeError("Unknown model type.\nEnsure the model type is properly set.")
state_dict = torch.load(self.__model_path, map_location=self.__device)
if self.__model_type == "densenet121":
# '.'s are no longer allowed in module names, but previous densenet layers
# as provided by the pytorch organization has names that uses '.'s.
pattern = re.compile(
r"^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\."
"(?:weight|bias|running_mean|running_var))$"
)
for key in list(state_dict.keys()):
res = pattern.match(key)
if res:
new_key = res.group(1) + res.group(2)
state_dict[new_key] = state_dict[key]
del state_dict[key]
self.__model.load_state_dict(state_dict)
self.__model.to(self.__device).eval()
self.__model_loaded = True
except Exception as e:
raise Exception("Weight loading failed.\nEnsure the model path is"
" set and the weight file is in the specified model path.")
def classifyImage(self, image_input: Union[str, np.ndarray, Image.Image], result_count: int) -> Tuple[List[str], List[float]]:
"""
'classifyImage()' function is used to classify a given image by receiving the following arguments:
* image_input: file path, numpy array or PIL image of the input image.
* result_count (optional) , the number of classifications to be sent which must be whole numbers between 1 and total number of classes the model is trained to classify.
This function returns 2 arrays namely 'classification_results' and 'classification_probabilities'. The 'classification_results'
contains possible objects classes arranged in descending of their percentage probabilities. The 'classification_probabilities'
contains the percentage probability of each object class. The position of each object class in the 'classification_results'
array corresponds with the positions of the percentage probability in the 'classification_probabilities' array.
:param image_input:
:param result_count:
:return classification_results, classification_probabilities:
"""
if not self.__model_loaded:
raise RuntimeError(
"Model not yet loaded. You need to call '.loadModel()' before performing image classification"
)
images = self.__load_image(image_input)
images = images.to(self.__device)
with torch.no_grad():
output = self.__model(images)
probabilities = torch.softmax(output, dim=1)
topN_prob, topN_catid = torch.topk(probabilities, result_count)
predictions = [
[
(self.__class_names[topN_catid[i][j]], topN_prob[i][j].item()*100)
for j in range(topN_prob.shape[1])
]
for i in range(topN_prob.shape[0])
]
labels_pred = []
probabilities_pred = []
for idx, pred in enumerate(predictions):
for label, score in pred:
labels_pred.append(label)
probabilities_pred.append(round(score, 4))
return labels_pred, probabilities_pred
================================================
FILE: imageai/Classification/Custom/data_transformation.py
================================================
from torchvision import transforms
data_transforms1 = {
"train":transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(
[0.485, 0.456, 0.406],
[0.229, 0.224, 0.225]
)
]),
"test": transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
[0.485, 0.456, 0.406],
[0.229, 0.224, 0.225]
)
])
}
data_transforms2 = {
"train":transforms.Compose([
transforms.RandomResizedCrop(299),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(
[0.485, 0.456, 0.406],
[0.229, 0.224, 0.225]
)
]),
"test": transforms.Compose([
transforms.Resize(299),
transforms.CenterCrop(299),
transforms.ToTensor(),
transforms.Normalize(
[0.485, 0.456, 0.406],
[0.229, 0.224, 0.225]
)
])
}
================================================
FILE: imageai/Classification/Custom/training_params.py
================================================
import torch
from torch.optim import SGD
from torchvision.models import resnet50, inception_v3, mobilenet_v2, densenet121
model = resnet50(pretrained=False)
def resnet50_train_params():
model = resnet50(pretrained=False)
return {
"model": model,
"optimizer": SGD,
"weight_decay":1e-4,
"lr":0.1,
"lr_decay_rate": None,
"lr_step_size": None
}
def inception_v3_train_params():
model = inception_v3(pretrained=False, init_weights=False)
return {
"model": model,
"optimizer": SGD,
"weight_decay":0,
"lr":0.045,
"lr_decay_rate": 0.94,
"lr_step_size":2
}
def mobilenet_v2_train_params():
model = mobilenet_v2(pretrained=False)
return {
"model": model,
"optimizer": SGD,
"weight_decay":4e-5,
"lr":0.045,
"lr_decay_rate": 0.98,
"lr_step_size":1
}
def densenet121_train_params():
model = densenet121(pretrained=False)
return {
"model": model,
"optimizer": SGD,
"weight_decay":1e-4,
"lr":0.1,
"lr_decay_rate": None,
"lr_step_size":None,
}
================================================
FILE: imageai/Classification/README.md
================================================
# ImageAI : Image Classification
## ---------------------------------------------------
## Introducing Jarvis and TheiaEngine.
We the creators of ImageAI are glad to announce 2 new AI projects to provide state-of-the-art Generative AI, LLM and Image Understanding on your personal computer and servers.
[](https://jarvis.genxr.co)
Install Jarvis on PC/Mac to setup limitless access to LLM powered AI Chats for your every day work, research and generative AI needs with 100% privacy and full offline capability.
Visit [https://jarvis.genxr.co](https://jarvis.genxr.co/) to get started.
[](https://www.genxr.co/theia-engine)
[TheiaEngine](https://www.genxr.co/theia-engine), the next-generation computer Vision AI API capable of all Generative and Understanding computer vision tasks in a single API call and available via REST API to all programming languages. Features include
- **Detect 300+ objects** ( 220 more objects than ImageAI)
- **Provide answers to any content or context questions** asked on an image
- very useful to get information on any object, action or information without needing to train a new custom model for every tasks
- **Generate scene description and summary**
- **Convert 2D image to 3D pointcloud and triangular mesh**
- **Semantic Scene mapping of objects, walls, floors, etc**
- **Stateless Face recognition and emotion detection**
- **Image generation and augmentation from prompt**
- etc.
Visit [https://www.genxr.co/theia-engine](https://www.genxr.co/theia-engine) to try the demo and join in the beta testing today.
## ---------------------------------------------------
### TABLE OF CONTENTS
- <a href="#firstprediction" > :white_square_button: First Prediction</a>
- <a href="#documentation" > :white_square_button: Documentation</a>
ImageAI provides 4 different algorithms and model types to perform image prediction.
To perform image prediction on any picture, take the following simple steps. The 4 algorithms provided for
image prediction include **MobileNetV2**, **ResNet50**, **InceptionV3** and **DenseNet121**. Each of these
algorithms have individual model files which you must use depending on the choice of your algorithm. To download the
model file for your choice of algorithm, click on any of the links below:
- **[MobileNetV2](https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/mobilenet_v2-b0353104.pth)** _(Size = 4.82 mb, fastest prediction time and moderate accuracy)_
- **[ResNet50](https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/resnet50-19c8e357.pth)** by Microsoft Research _(Size = 98 mb, fast prediction time and high accuracy)_
- **[InceptionV3](https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/inception_v3_google-1a9a5a14.pth)** by Google Brain team _(Size = 91.6 mb, slow prediction time and higher accuracy)_
- **[DenseNet121](https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/densenet121-a639ec97.pth)** by Facebook AI Research _(Size = 31.6 mb, slower prediction time and highest accuracy)_
Great! Once you have downloaded this model file, start a new python project, and then copy the model file to your project
folder where your python files (.py files) will be . Download the image below, or take any image on your computer
and copy it to your python project's folder. Then create a python file and give it a name; an example is `FirstPrediction.py`.
Then write the code below into the python file:
### FirstPrediction.py
<div id="firstprediction" ></div>
```python
from imageai.Classification import ImageClassification
import os
execution_path = os.getcwd()
prediction = ImageClassification()
prediction.setModelTypeAsResNet50()
prediction.setModelPath(os.path.join(execution_path, "resnet50-19c8e357.pth"))
prediction.loadModel()
predictions, probabilities = prediction.classifyImage(os.path.join(execution_path, "1.jpg"), result_count=5 )
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction , " : " , eachProbability)
```
Sample Result:

```
convertible : 52.459555864334106
sports_car : 37.61284649372101
pickup : 3.1751200556755066
car_wheel : 1.817505806684494
minivan : 1.7487050965428352
```
The code above works as follows:
```python
from imageai.Classification import ImageClassification
import os
```
The code above imports the `ImageAI` library and the python `os` class.
```python
execution_path = os.getcwd()
```
The above line obtains the path to the folder that contains your python file (in this example, your FirstPrediction.py).
```python
prediction = ImageClassification()
prediction.setModelTypeAsResNet50()
prediction.setModelPath(os.path.join(execution_path, "resnet50-19c8e357.pth"))
```
In the lines above, we created and instance of the `ImagePrediction()` class in the first line, then we set the model type of the prediction object to ResNet by caling the `.setModelTypeAsResNet50()` in the second line and then we set the model path of the prediction object to the path of the model file (`resnet50-19c8e357.pth`) we copied to the python file folder in the third line.
```python
predictions, probabilities = prediction.classifyImage(os.path.join(execution_path, "1.jpg"), result_count=5 )
```
In the above line, we defined 2 variables to be equal to the function called to predict an image, which is the `.classifyImage()` function, into which we parsed the path to our image and also state the number of prediction results we want to have (values from 1 to 1000) parsing `result_count=5`. The `.classifyImage()` function will return 2 array objects with the first (**predictions**) being an array of predictions and the second (**percentage_probabilities**) being an array of the corresponding percentage probability for each prediction.
```python
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction, " : " , eachProbability)
```
The above line obtains each object in the **predictions** array, and also obtains the corresponding percentage probability from the **percentage_probabilities**, and finally prints the result of both to console.
### Documentation
We have provided full documentation for all **ImageAI** classes and functions. Find links below:**
* Documentation - **English Version [https://imageai.readthedocs.io](https://imageai.readthedocs.io)**
================================================
FILE: imageai/Classification/__init__.py
================================================
import os, re
from typing import Union
from typing import List, Tuple
import numpy as np
import torch
from torchvision.models import resnet50, densenet121, mobilenet_v2, inception_v3
import torch.nn.functional as F
from torchvision import transforms
from PIL import Image
import traceback
from ..backend_check.model_extension import extension_check
classification_models = {
"resnet50": {
"model": resnet50(pretrained=False)
},
"densenet121": {
"model": densenet121(pretrained=False)
},
"inceptionv3": {
"model": inception_v3(pretrained=False)
},
"mobilenetv2": {
"model": mobilenet_v2(pretrained=False)
}
}
class ImageClassification:
"""
This is the image classification class in the ImageAI library. It allows you to classify objects into all the 1000 different classes in the ImageNet dataset [ https://www.kaggle.com/c/imagenet-object-localization-challenge/overview/description ].
The class provides 4 different classification models which are ResNet50, DensesNet121, InceptionV3 and MobileNetV2.
The following functions are required to be called before a classification can be made
* At least of of the following and it must correspond to the model set in the setModelPath()
[setModelTypeAsMobileNetV2(), setModelTypeAsResNet(), setModelTypeAsDenseNet, setModelTypeAsInceptionV3]
* setModelPath: This is used to specify the absolute path to a pretrained model file. Download any of the files in this release -> https://github.com/OlafenwaMoses/ImageAI/releases/tag/3.0.0-pretrained
* useCPU (Optional): If you will like to force the image classification to be performed on CPU, call this function.
* loadModel: Used to load the pretrained model weights
* classifyImage(): Used for classifying an image.
"""
def __init__(self) -> None:
self.__model_type:str = None
self.__model:Union[resnet50, densenet121, mobilenet_v2, inception_v3] = None
self.__model_path: str = None
self.__classes_path: str = os.path.join(os.path.dirname(os.path.abspath(__file__)), "imagenet_classes.txt")
self.__model_loaded: bool = False
self.__device: str = "cuda" if torch.cuda.is_available() else "cpu"
self.__classes: List[str] = []
def setModelPath(self, path: str):
"""
'setModelPath()' function is required and is used to set the file path to the model adopted from the list of the
available 4 model types. The model path must correspond to the model type set for the classification instance object.
:param model_path:
:return:
"""
if os.path.isfile(path):
extension_check(path)
self.__model_path = path
else:
raise ValueError(
f"The path '{path}' isn't a valid file. Ensure you specify the path to a valid trained model file."
)
def __load_classes(self) -> List[str]:
with open(self.__classes_path) as f:
self.__classes = [c.strip() for c in f.readlines()]
def __load_image(self, image_input: Union[str, np.ndarray, Image.Image]) -> torch.Tensor:
images = []
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
if type(image_input) == str:
if os.path.isfile(image_input):
img = Image.open(image_input).convert("RGB")
images.append(preprocess(img))
else:
raise ValueError(f"image path '{image_input}' is not found or a valid file")
elif type(image_input) == np.ndarray:
img = Image.fromarray(image_input).convert("RGB")
images.append(preprocess(img))
elif "PIL" in str(type(image_input)):
img = image_input.convert("RGB")
images.append(preprocess(img))
else:
raise ValueError(f"Invalid image input format")
return torch.stack(images)
def setModelTypeAsResNet50(self):
"""
'setModelTypeAsResNet50()' is used to set the model type to the ResNet50 model.
:return:
"""
if self.__model_type == None:
self.__model_type = "resnet50"
def setModelTypeAsDenseNet121(self):
"""
'setModelTypeAsDenseNet121()' is used to set the model type to the DenseNet121 model.
:return:
"""
if self.__model_type == None:
self.__model_type = "densenet121"
def setModelTypeAsInceptionV3(self):
"""
'setModelTypeAsInceptionV3()' is used to set the model type to the InceptionV3 model.
:return:
"""
if self.__model_type == None:
self.__model_type = "inceptionv3"
def setModelTypeAsMobileNetV2(self):
"""
'setModelTypeAsMobileNetV2()' is used to set the model type to the MobileNetV2 model.
:return:
"""
if self.__model_type == None:
self.__model_type = "mobilenetv2"
def useCPU(self):
"""
Used to force classification to be done on CPU.
By default, classification will occur on GPU compute if available else CPU compute.
"""
self.__device = "cpu"
if self.__model_loaded:
self.__model_loaded = False
self.loadModel()
def loadModel(self):
"""
'loadModel()' function is used to load the model weights into the model architecture from the file path defined
in the setModelPath() function.
:return:
"""
if not self.__model_loaded:
try:
if self.__model_path == None:
raise ValueError(
"Model path not specified. Call '.setModelPath()' and parse the path to the model file before loading the model."
)
if self.__model_type in classification_models.keys():
self.__model = classification_models[self.__model_type]["model"]
else:
raise ValueError(
f"Model type '{self.__model_type}' not supported."
)
state_dict = torch.load(self.__model_path)
if self.__model_type == "densenet121":
# '.'s are no longer allowed in module names, but previous densenet layers
# as provided by the Pytorch's model zoon has names that uses '.'s.
pattern = re.compile(
r"^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\."
"(?:weight|bias|running_mean|running_var))$"
)
for key in list(state_dict.keys()):
res = pattern.match(key)
if res:
new_key = res.group(1) + res.group(2)
state_dict[new_key] = state_dict[key]
del state_dict[key]
self.__model.load_state_dict(
state_dict
)
self.__model.to(self.__device)
self.__model_loaded = True
self.__model.eval()
self.__load_classes()
except Exception:
print(traceback.print_exc())
print("Weight loading failed.\nEnsure the model path is"
" set and the weight file is in the specified model path.")
def classifyImage(self, image_input: Union[str, np.ndarray, Image.Image], result_count: int=5) -> Tuple[List[str], List[float]]:
"""
'classifyImage()' function is used to classify a given image by receiving the following arguments:
* image_input: file path, numpy array or PIL image of the input image.
* result_count (optional) , the number of classifications to be sent which must be whole numbers between
1 and 1000. The default is 5.
This function returns 2 arrays namely 'classification_results' and 'classification_probabilities'. The 'classification_results'
contains possible objects classes arranged in descending of their percentage probabilities. The 'classification_probabilities'
contains the percentage probability of each object class. The position of each object class in the 'classification_results'
array corresponds with the positions of the percentage probability in the 'classification_probabilities' array.
:param image_input:
:param result_count:
:return classification_results, classification_probabilities:
"""
if not self.__model_loaded:
raise RuntimeError(
"Model not yet loaded. You need to call '.loadModel()' before performing image classification"
)
images = self.__load_image(image_input)
images = images.to(self.__device)
with torch.no_grad():
output = self.__model(images)
probabilities = torch.softmax(output, dim=1)
topN_prob, topN_catid = torch.topk(probabilities, result_count)
predictions = [
[
(self.__classes[topN_catid[i][j]], topN_prob[i][j].item()*100)
for j in range(topN_prob.shape[1])
]
for i in range(topN_prob.shape[0])
]
labels_pred = []
probabilities_pred = []
for idx, pred in enumerate(predictions):
for label, score in pred:
labels_pred.append(label)
probabilities_pred.append(round(score, 4))
return labels_pred, probabilities_pred
================================================
FILE: imageai/Classification/imagenet_classes.txt
================================================
tench
goldfish
great white shark
tiger shark
hammerhead
electric ray
stingray
cock
hen
ostrich
brambling
goldfinch
house finch
junco
indigo bunting
robin
bulbul
jay
magpie
chickadee
water ouzel
kite
bald eagle
vulture
great grey owl
European fire salamander
common newt
eft
spotted salamander
axolotl
bullfrog
tree frog
tailed frog
loggerhead
leatherback turtle
mud turtle
terrapin
box turtle
banded gecko
common iguana
American chameleon
whiptail
agama
frilled lizard
alligator lizard
Gila monster
green lizard
African chameleon
Komodo dragon
African crocodile
American alligator
triceratops
thunder snake
ringneck snake
hognose snake
green snake
king snake
garter snake
water snake
vine snake
night snake
boa constrictor
rock python
Indian cobra
green mamba
sea snake
horned viper
diamondback
sidewinder
trilobite
harvestman
scorpion
black and gold garden spider
barn spider
garden spider
black widow
tarantula
wolf spider
tick
centipede
black grouse
ptarmigan
ruffed grouse
prairie chicken
peacock
quail
partridge
African grey
macaw
sulphur-crested cockatoo
lorikeet
coucal
bee eater
hornbill
hummingbird
jacamar
toucan
drake
red-breasted merganser
goose
black swan
tusker
echidna
platypus
wallaby
koala
wombat
jellyfish
sea anemone
brain coral
flatworm
nematode
conch
snail
slug
sea slug
chiton
chambered nautilus
Dungeness crab
rock crab
fiddler crab
king crab
American lobster
spiny lobster
crayfish
hermit crab
isopod
white stork
black stork
spoonbill
flamingo
little blue heron
American egret
bittern
crane
limpkin
European gallinule
American coot
bustard
ruddy turnstone
red-backed sandpiper
redshank
dowitcher
oystercatcher
pelican
king penguin
albatross
grey whale
killer whale
dugong
sea lion
Chihuahua
Japanese spaniel
Maltese dog
Pekinese
Shih-Tzu
Blenheim spaniel
papillon
toy terrier
Rhodesian ridgeback
Afghan hound
basset
beagle
bloodhound
bluetick
black-and-tan coonhound
Walker hound
English foxhound
redbone
borzoi
Irish wolfhound
Italian greyhound
whippet
Ibizan hound
Norwegian elkhound
otterhound
Saluki
Scottish deerhound
Weimaraner
Staffordshire bullterrier
American Staffordshire terrier
Bedlington terrier
Border terrier
Kerry blue terrier
Irish terrier
Norfolk terrier
Norwich terrier
Yorkshire terrier
wire-haired fox terrier
Lakeland terrier
Sealyham terrier
Airedale
cairn
Australian terrier
Dandie Dinmont
Boston bull
miniature schnauzer
giant schnauzer
standard schnauzer
Scotch terrier
Tibetan terrier
silky terrier
soft-coated wheaten terrier
West Highland white terrier
Lhasa
flat-coated retriever
curly-coated retriever
golden retriever
Labrador retriever
Chesapeake Bay retriever
German short-haired pointer
vizsla
English setter
Irish setter
Gordon setter
Brittany spaniel
clumber
English springer
Welsh springer spaniel
cocker spaniel
Sussex spaniel
Irish water spaniel
kuvasz
schipperke
groenendael
malinois
briard
kelpie
komondor
Old English sheepdog
Shetland sheepdog
collie
Border collie
Bouvier des Flandres
Rottweiler
German shepherd
Doberman
miniature pinscher
Greater Swiss Mountain dog
Bernese mountain dog
Appenzeller
EntleBucher
boxer
bull mastiff
Tibetan mastiff
French bulldog
Great Dane
Saint Bernard
Eskimo dog
malamute
Siberian husky
dalmatian
affenpinscher
basenji
pug
Leonberg
Newfoundland
Great Pyrenees
Samoyed
Pomeranian
chow
keeshond
Brabancon griffon
Pembroke
Cardigan
toy poodle
miniature poodle
standard poodle
Mexican hairless
timber wolf
white wolf
red wolf
coyote
dingo
dhole
African hunting dog
hyena
red fox
kit fox
Arctic fox
grey fox
tabby
tiger cat
Persian cat
Siamese cat
Egyptian cat
cougar
lynx
leopard
snow leopard
jaguar
lion
tiger
cheetah
brown bear
American black bear
ice bear
sloth bear
mongoose
meerkat
tiger beetle
ladybug
ground beetle
long-horned beetle
leaf beetle
dung beetle
rhinoceros beetle
weevil
fly
bee
ant
grasshopper
cricket
walking stick
cockroach
mantis
cicada
leafhopper
lacewing
dragonfly
damselfly
admiral
ringlet
monarch
cabbage butterfly
sulphur butterfly
lycaenid
starfish
sea urchin
sea cucumber
wood rabbit
hare
Angora
hamster
porcupine
fox squirrel
marmot
beaver
guinea pig
sorrel
zebra
hog
wild boar
warthog
hippopotamus
ox
water buffalo
bison
ram
bighorn
ibex
hartebeest
impala
gazelle
Arabian camel
llama
weasel
mink
polecat
black-footed ferret
otter
skunk
badger
armadillo
three-toed sloth
orangutan
gorilla
chimpanzee
gibbon
siamang
guenon
patas
baboon
macaque
langur
colobus
proboscis monkey
marmoset
capuchin
howler monkey
titi
spider monkey
squirrel monkey
Madagascar cat
indri
Indian elephant
African elephant
lesser panda
giant panda
barracouta
eel
coho
rock beauty
anemone fish
sturgeon
gar
lionfish
puffer
abacus
abaya
academic gown
accordion
acoustic guitar
aircraft carrier
airliner
airship
altar
ambulance
amphibian
analog clock
apiary
apron
ashcan
assault rifle
backpack
bakery
balance beam
balloon
ballpoint
Band Aid
banjo
bannister
barbell
barber chair
barbershop
barn
barometer
barrel
barrow
baseball
basketball
bassinet
bassoon
bathing cap
bath towel
bathtub
beach wagon
beacon
beaker
bearskin
beer bottle
beer glass
bell cote
bib
bicycle-built-for-two
bikini
binder
binoculars
birdhouse
boathouse
bobsled
bolo tie
bonnet
bookcase
bookshop
bottlecap
bow
bow tie
brass
brassiere
breakwater
breastplate
broom
bucket
buckle
bulletproof vest
bullet train
butcher shop
cab
caldron
candle
cannon
canoe
can opener
cardigan
car mirror
carousel
carpenter's kit
carton
car wheel
cash machine
cassette
cassette player
castle
catamaran
CD player
cello
cellular telephone
chain
chainlink fence
chain mail
chain saw
chest
chiffonier
chime
china cabinet
Christmas stocking
church
cinema
cleaver
cliff dwelling
cloak
clog
cocktail shaker
coffee mug
coffeepot
coil
combination lock
computer keyboard
confectionery
container ship
convertible
corkscrew
cornet
cowboy boot
cowboy hat
cradle
crane
crash helmet
crate
crib
Crock Pot
croquet ball
crutch
cuirass
dam
desk
desktop computer
dial telephone
diaper
digital clock
digital watch
dining table
dishrag
dishwasher
disk brake
dock
dogsled
dome
doormat
drilling platform
drum
drumstick
dumbbell
Dutch oven
electric fan
electric guitar
electric locomotive
entertainment center
envelope
espresso maker
face powder
feather boa
file
fireboat
fire engine
fire screen
flagpole
flute
folding chair
football helmet
forklift
fountain
fountain pen
four-poster
freight car
French horn
frying pan
fur coat
garbage truck
gasmask
gas pump
goblet
go-kart
golf ball
golfcart
gondola
gong
gown
grand piano
greenhouse
grille
grocery store
guillotine
hair slide
hair spray
half track
hammer
hamper
hand blower
hand-held computer
handkerchief
hard disc
harmonica
harp
harvester
hatchet
holster
home theater
honeycomb
hook
hoopskirt
horizontal bar
horse cart
hourglass
iPod
iron
jack-o'-lantern
jean
jeep
jersey
jigsaw puzzle
jinrikisha
joystick
kimono
knee pad
knot
lab coat
ladle
lampshade
laptop
lawn mower
lens cap
letter opener
library
lifeboat
lighter
limousine
liner
lipstick
Loafer
lotion
loudspeaker
loupe
lumbermill
magnetic compass
mailbag
mailbox
maillot
maillot
manhole cover
maraca
marimba
mask
matchstick
maypole
maze
measuring cup
medicine chest
megalith
microphone
microwave
military uniform
milk can
minibus
miniskirt
minivan
missile
mitten
mixing bowl
mobile home
Model T
modem
monastery
monitor
moped
mortar
mortarboard
mosque
mosquito net
motor scooter
mountain bike
mountain tent
mouse
mousetrap
moving van
muzzle
nail
neck brace
necklace
nipple
notebook
obelisk
oboe
ocarina
odometer
oil filter
organ
oscilloscope
overskirt
oxcart
oxygen mask
packet
paddle
paddlewheel
padlock
paintbrush
pajama
palace
panpipe
paper towel
parachute
parallel bars
park bench
parking meter
passenger car
patio
pay-phone
pedestal
pencil box
pencil sharpener
perfume
Petri dish
photocopier
pick
pickelhaube
picket fence
pickup
pier
piggy bank
pill bottle
pillow
ping-pong ball
pinwheel
pirate
pitcher
plane
planetarium
plastic bag
plate rack
plow
plunger
Polaroid camera
pole
police van
poncho
pool table
pop bottle
pot
potter's wheel
power drill
prayer rug
printer
prison
projectile
projector
puck
punching bag
purse
quill
quilt
racer
racket
radiator
radio
radio telescope
rain barrel
recreational vehicle
reel
reflex camera
refrigerator
remote control
restaurant
revolver
rifle
rocking chair
rotisserie
rubber eraser
rugby ball
rule
running shoe
safe
safety pin
saltshaker
sandal
sarong
sax
scabbard
scale
school bus
schooner
scoreboard
screen
screw
screwdriver
seat belt
sewing machine
shield
shoe shop
shoji
shopping basket
shopping cart
shovel
shower cap
shower curtain
ski
ski mask
sleeping bag
slide rule
sliding door
slot
snorkel
snowmobile
snowplow
soap dispenser
soccer ball
sock
solar dish
sombrero
soup bowl
space bar
space heater
space shuttle
spatula
speedboat
spider web
spindle
sports car
spotlight
stage
steam locomotive
steel arch bridge
steel drum
stethoscope
stole
stone wall
stopwatch
stove
strainer
streetcar
stretcher
studio couch
stupa
submarine
suit
sundial
sunglass
sunglasses
sunscreen
suspension bridge
swab
sweatshirt
swimming trunks
swing
switch
syringe
table lamp
tank
tape player
teapot
teddy
television
tennis ball
thatch
theater curtain
thimble
thresher
throne
tile roof
toaster
tobacco shop
toilet seat
torch
totem pole
tow truck
toyshop
tractor
trailer truck
tray
trench coat
tricycle
trimaran
tripod
triumphal arch
trolleybus
trombone
tub
turnstile
typewriter keyboard
umbrella
unicycle
upright
vacuum
vase
vault
velvet
vending machine
vestment
viaduct
violin
volleyball
waffle iron
wall clock
wallet
wardrobe
warplane
washbasin
washer
water bottle
water jug
water tower
whiskey jug
whistle
wig
window screen
window shade
Windsor tie
wine bottle
wing
wok
wooden spoon
wool
worm fence
wreck
yawl
yurt
web site
comic book
crossword puzzle
street sign
traffic light
book jacket
menu
plate
guacamole
consomme
hot pot
trifle
ice cream
ice lolly
French loaf
bagel
pretzel
cheeseburger
hotdog
mashed potato
head cabbage
broccoli
cauliflower
zucchini
spaghetti squash
acorn squash
butternut squash
cucumber
artichoke
bell pepper
cardoon
mushroom
Granny Smith
strawberry
orange
lemon
fig
pineapple
banana
jackfruit
custard apple
pomegranate
hay
carbonara
chocolate sauce
dough
meat loaf
pizza
potpie
burrito
red wine
espresso
cup
eggnog
alp
bubble
cliff
coral reef
geyser
lakeside
promontory
sandbar
seashore
valley
volcano
ballplayer
groom
scuba diver
rapeseed
daisy
yellow lady's slipper
corn
acorn
hip
buckeye
coral fungus
agaric
gyromitra
stinkhorn
earthstar
hen-of-the-woods
bolete
ear
toilet tissue
================================================
FILE: imageai/Detection/Custom/CUSTOMDETECTION.md
================================================
# ImageAI : Custom Object Detection
### TABLE OF CONTENTS
- <a href="#customdetection" > :white_square_button: Custom Object Detection</a>
- <a href="#objectextraction" > :white_square_button: Object Detection, Extraction and Fine-tune</a>
- <a href="#hidingdetails" > :white_square_button: Hiding/Showing Object Name and Probability</a>
- <a href="#inputoutputtype" > :white_square_button: Image Input & Output Types</a>
- <a href="#documentation" > :white_square_button: Documentation</a>
ImageAI provides very convenient and powerful methods to perform object detection on images and extract each object from the image using your own **custom YOLOv3 or TinyYOLOv3 model** and the corresponding **.json** generated during the training. To test the custom object detection, you can download a sample custom model we have trained to detect the Hololens headset and its **.json** file via the links below:
* [**yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt**](https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt) _(Size = 236 mb)_
* [**hololens-yolo_yolov3_detection_config.json**](https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/hololens-yolo_yolov3_detection_config.json)
Once you download the custom object detection model file, you should copy the model file to the your project folder where your **.py** files will be.
Then create a python file and give it a name; an example is FirstCustomDetection.py. Then write the code below into the python file:
### FirstCustomDetection.py
<div id="customdetection" ></div>
```python
from imageai.Detection.Custom import CustomObjectDetection
detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt")
detector.setJsonPath("hololens-yolo_yolov3_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"])
```
Sample Result - Input:

Output:

```
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]
```
Let us make a breakdown of the object detection code that we used above.
```python
from imageai.Detection.Custom import CustomObjectDetection
detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
```
In the 3 lines above , we import the **ImageAI custom object detection** class in the first line, created the class instance on the second line and set the model type to YOLOv3.
```python
detector.setModelPath("yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt")
detector.setJsonPath("hololens-yolo_yolov3_detection_config.json")
detector.loadModel()
```
In the 3 lines above, we specified the file path to our downloaded model file in the first line , specified the path to our **hololens-yolo_yolov3_detection_config.json** file in the second line and loaded the model on the third line.
```python
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"])
```
In the 3 lines above, we ran the `detectObjectsFromImage()` function and parse in the path to our test image, and the path to the new
image which the function will save. Then the function returns an array of dictionaries with each dictionary corresponding
to the number of objects detected in the image. Each dictionary has the properties `name` (name of the object),
`percentage_probability` (percentage probability of the detection) and `box_points` (the x1,y1,x2 and y2 coordinates of the bounding box of the object).
### Object Detection, Extraction and Fine-tune
<div id="objectextraction" ></div>
In the examples we used above, we ran the object detection on an image and it
returned the detected objects in an array as well as save a new image with rectangular markers drawn on each object. In our next examples, we will be able to extract each object from the input image and save it independently.
In the example code below which is very identical to the previous object detection code, we will save each object detected as a separate image.
```python
from imageai.Detection.Custom import CustomObjectDetection
detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt")
detector.setJsonPath("hololens-yolo_yolov3_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: Output Images







Let us review the part of the code that perform the object detection and extract the images:
```python
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("---------------")
```
In the above above lines, we called the `detectObjectsFromImage()` , parse in the input image path, output image part, and an
extra parameter `extract_detected_objects=True`. This parameter states that the function should extract each object detected from the image
and save it has a seperate image. The parameter is false by default. Once set to `true`, the function will create a directory
which is the `output image path + "-objects"`. Then it saves all the extracted images into this new directory with
each image's name being the `detected object name + "-" + a number` which corresponds to the order at which the objects
were detected.
This new parameter we set to extract and save detected objects as an image will make the function to return 2 values. The
first is the array of dictionaries with each dictionary corresponding to a detected object. The second is an array of the paths
to the saved images of each object detected and extracted, and they are arranged in order at which the objects are in the
first array.
### And one important feature you need to know!
You will recall that the percentage probability
for each detected object is sent back by the `detectObjectsFromImage()` function. The function has a parameter
`minimum_percentage_probability` , whose default value is `30` (value ranges between 0 - 100) , but it set to 30 in this example. That means the function will only return a detected
object if it's percentage probability is **30 or above**. The value was kept at this number to ensure the integrity of the
detection results. You fine-tune the object
detection by setting `minimum_percentage_probability` equal to a smaller value to detect more number of objects or higher value to detect less number of objects.
### Hiding/Showing Object Name and Probability
<div id="hidingdetails"></div>
**ImageAI** provides options to hide the name of objects detected and/or the percentage probability from being shown on the saved/returned detected image. Using the `detectObjectsFromImage()` and `detectCustomObjectsFromImage()` functions, the parameters `'display_object_name'` and `'display_percentage_probability'` can be set to True of False individually. Take a look at the code below:
```python
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "holo2.jpg"), output_image_path=os.path.join(execution_path , "holo2_nodetails.jpg"), minimum_percentage_probability=30, display_percentage_probability=False, display_object_name=False)
```
In the above code, we specified that both the object name and percentage probability should not be shown. As you can see in the result below, both the names of the objects and their individual percentage probability is not shown in the detected image.
**Result**

### Image Input & Output Types
<div id="inputoutputtype"></div>
**ImageAI** custom object detection supports 2 input types of inputs which are **file path to image file**(default) and **numpy array of an image**
as well as 2 types of output which are image **file**(default) and numpy **array **.
This means you can now perform object detection in production applications such as on a web server and system
that returns file in any of the above stated formats.
To perform object detection with numpy array input, you just need to state the input type
in the `.detectObjectsFromImage()` function. See example below.
```python
detections = detector.detectObjectsFromImage(input_type="array", input_image=image_array , output_image_path=os.path.join(execution_path , "holo2-detected.jpg")) # For numpy array input type
```
To perform object detection with numpy array output you just need to state the output type
in the `.detectObjectsFromImage()` function. See example below.
```python
detected_image_array, detections = detector.detectObjectsFromImage(output_type="array", input_image="holo2.jpg" ) # For numpy array output type
```
### Documentation
<div id="documentation" ></div>
We have provided full documentation for all **ImageAI** classes and functions. Find links below:
* Documentation - **English Version** [https://imageai.readthedocs.io](https://imageai.readthedocs.io)
================================================
FILE: imageai/Detection/Custom/CUSTOMDETECTIONTRAINING.md
================================================
# ImageAI : Custom Detection Model Training
## ---------------------------------------------------
## Introducing Jarvis and TheiaEngine.
We the creators of ImageAI are glad to announce 2 new AI projects to provide state-of-the-art Generative AI, LLM and Image Understanding on your personal computer and servers.
[](https://jarvis.genxr.co)
Install Jarvis on PC/Mac to setup limitless access to LLM powered AI Chats for your every day work, research and generative AI needs with 100% privacy and full offline capability.
Visit [https://jarvis.genxr.co](https://jarvis.genxr.co/) to get started.
[](https://www.genxr.co/theia-engine)
[TheiaEngine](https://www.genxr.co/theia-engine), the next-generation computer Vision AI API capable of all Generative and Understanding computer vision tasks in a single API call and available via REST API to all programming languages. Features include
- **Detect 300+ objects** ( 220 more objects than ImageAI)
- **Provide answers to any content or context questions** asked on an image
- very useful to get information on any object, action or information without needing to train a new custom model for every tasks
- **Generate scene description and summary**
- **Convert 2D image to 3D pointcloud and triangular mesh**
- **Semantic Scene mapping of objects, walls, floors, etc**
- **Stateless Face recognition and emotion detection**
- **Image generation and augmentation from prompt**
- etc.
Visit [https://www.genxr.co/theia-engine](https://www.genxr.co/theia-engine) to try the demo and join in the beta testing today.
## ---------------------------------------------------
**ImageAI** provides the most simple and powerful approach to training custom object detection models
using the YOLOv3 architeture, which
which you can load into the `imageai.Detection.Custom.CustomObjectDetection` class. This allows
you to train your own **YOLOv3** or **TinyYOLOv3** model on any set of images that corresponds to any type of objects of interest.
The training process generates a JSON file that maps the objects names in your image dataset and the detection anchors, as well as creates lots of models. In choosing the best model for your custom object detection task, an `evaluateModel()` function has been provided to compute the **mAP** of your saved models by allowing you to state your desired **IoU** and **Non-maximum Suppression** values. Then you can perform custom
object detection using the model and the JSON file generated.
### TABLE OF CONTENTS
- <a href="#preparingdataset" > :white_square_button: Preparing your custom dataset</a>
- <a href="#trainingdataset" > :white_square_button: Training on your custom Dataset</a>
- <a href="#evaluatingmodels" > :white_square_button: Evaluating your saved detection models' mAP</a>
### Preparing your custom dataset
<div id="preparingdataset"></div>
To train a custom detection model, you need to prepare the images you want to use to train the model.
You will prepare the images as follows:
1. Decide the type of object(s) you want to detect and collect about **200 (minimum recommendation)** or more picture of each of the object(s)
2. Once you have collected the images, you need to annotate the object(s) in the images. **ImageAI** uses the **YOLO** for image annotation. You can generate this annotation for your images using the easy to use [**LabelImg**](https://github.com/tzutalin/labelImg) image annotation tool, available for Windows, Linux and MacOS systems. Open the link below to install the annotation tool. See: [https://github.com/tzutalin/labelImg](https://github.com/tzutalin/labelImg)
3. When you are done annotating your images, **annotation .txt** files will be generated for each image in your dataset. The **annotation .txt** file describes each or **all** of the objects in the image. For example, if each image your image names are **image(1).jpg**, **image(2).jpg**, **image(3).jpg** till **image(z).jpg**; the corresponding annotation for each of the images will be **image(1).txt**, **image(2).txt**, **image(3).txt** till **image(z).txt**.
4. Once you have the annotations for all your images, create a folder for your dataset (E.g headsets) and in this parent folder, create child folders **train** and **validation**
5. In the train folder, create **images** and **annotations**
sub-folders. Put about 70-80% of your dataset of each object's images in the **images** folder and put the corresponding annotations for these images in the **annotations** folder.
6. In the validation folder, create **images** and **annotations** sub-folders. Put the rest of your dataset images in the **images** folder and put the corresponding annotations for these images in the **annotations** folder.
7. Once you have done this, the structure of your image dataset folder should look like below:
```
>> train >> images >> img_1.jpg (shows Object_1)
>> images >> img_2.jpg (shows Object_2)
>> images >> img_3.jpg (shows Object_1, Object_3 and Object_n)
>> annotations >> img_1.txt (describes Object_1)
>> annotations >> img_2.txt (describes Object_2)
>> annotations >> img_3.txt (describes Object_1, Object_3 and Object_n)
>> validation >> images >> img_151.jpg (shows Object_1, Object_3 and Object_n)
>> images >> img_152.jpg (shows Object_2)
>> images >> img_153.jpg (shows Object_1)
>> annotations >> img_151.txt (describes Object_1, Object_3 and Object_n)
>> annotations >> img_152.txt (describes Object_2)
>> annotations >> img_153.txt (describes Object_1)
```
8. You can train your custom detection model completely from scratch or use transfer learning (recommended for better accuracy) from a pre-trained YOLOv3 model. Also, we have provided a sample annotated Hololens and Headsets (Hololens and Oculus) dataset for you to train with. Download the pre-trained YOLOv3 model and the sample datasets in the link below.
Download dataset `hololens-yolo.zip` [here](https://github.com/OlafenwaMoses/ImageAI/releases/tag/test-resources-v3) and pre-trained model `yolov3.pt` [here](https://github.com/OlafenwaMoses/ImageAI/releases/tag/3.0.0-pretrained)
### Training on your custom dataset
<div id="trainingdataset"></div>
Before you start training your custom detection model, kindly take note of the following:
- The default **batch_size** is 4. If you are training with **Google Colab**, this will be fine. However, I will advice you use a more powerful GPU than the K80 offered by Colab as the higher your **batch_size (8, 16)**, the better the accuracy of your detection model.
Then your training code goes as follows:
```python
from imageai.Detection.Custom import DetectionModelTrainer
trainer = DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory="hololens-yolo")
trainer.setTrainConfig(object_names_array=["hololens"], batch_size=4, num_experiments=200, train_from_pretrained_model="yolov3.pt")
# In the above,when training for detecting multiple objects,
#set object_names_array=["object1", "object2", "object3",..."objectz"]
trainer.trainModel()
```
Yes! Just 6 lines of code and you can train object detection models on your custom dataset.
Now lets take a look at how the code above works.
```python
from imageai.Detection.Custom import DetectionModelTrainer
trainer = DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory="hololens-yolo")
```
In the first line, we import the **ImageAI** detection model training class, then we define the model trainer in the second line,
we set the network type in the third line and set the path to the image dataset we want to train the network on.
```python
trainer.setTrainConfig(object_names_array=["hololens"], batch_size=4, num_experiments=200, train_from_pretrained_model="yolov3.pt")
```
In the line above, we configured our detection model trainer. The parameters we stated in the function as as below:
- **num_objects** : this is an array containing the names of the objects in our dataset
- **batch_size** : this is to state the batch size for the training
- **num_experiments** : this is to state the number of times the network will train over all the training images,
which is also called epochs
- **train_from_pretrained_model(optional)** : this is to train using transfer learning from a pre-trained **YOLOv3** model
```python
trainer.trainModel()
```
When you start the training, you should see something like this in the console:
```
Generating anchor boxes for training images...
thr=0.25: 1.0000 best possible recall, 6.93 anchors past thr
n=9, img_size=416, metric_all=0.463/0.856-mean/best, past_thr=0.549-mean:
====================
Pretrained YOLOv3 model loaded to initialize weights
====================
Epoch 1/100
----------
Train:
30it [00:14, 2.09it/s]
box loss-> 0.09820, object loss-> 0.27985, class loss-> 0.00000
Validation:
15it [01:45, 7.05s/it]
recall: 0.085714 precision: 0.000364 mAP@0.5: 0.000186, mAP@0.5-0.95: 0.000030
Epoch 2/100
----------
Train:
30it [00:07, 4.25it/s]
box loss-> 0.08691, object loss-> 0.07011, class loss-> 0.00000
Validation:
15it [01:37, 6.53s/it]
recall: 0.214286 precision: 0.000854 mAP@0.5: 0.000516, mAP@0.5-0.95: 0.000111
.
.
.
.
```
Let us explain the details shown above:
```
Generating anchor boxes for training images...
thr=0.25: 1.0000 best possible recall, 6.93 anchors past thr
n=9, img_size=416, metric_all=0.463/0.856-mean/best, past_thr=0.549-mean:
====================
Pretrained YOLOv3 model loaded to initialize weights
====================
```
The above details signifies the following:
- **ImageAI** autogenerates the best match detection **anchor boxes** for your image dataset.
- A the pretrained **yolov3.pt** was loaded to initalize the weights used to train the model.
```
Epoch 1/100
----------
Train:
30it [00:14, 2.09it/s]
box loss-> 0.09820, object loss-> 0.27985, class loss-> 0.00000
Validation:
15it [01:45, 7.05s/it]
recall: 0.085714 precision: 0.000364 mAP@0.5: 0.000186, mAP@0.5-0.95: 0.000030
Epoch 2/100
----------
Train:
30it [00:07, 4.25it/s]
box loss-> 0.08691, object loss-> 0.07011, class loss-> 0.00000
Validation:
15it [01:37, 6.53s/it]
recall: 0.214286 precision: 0.000854 mAP@0.5: 0.000516, mAP@0.5-0.95: 0.000111
```
- The above signifies the progress of the training.
- For each experiment (Epoch), a number of metrics are computed. The important once fo chosing an accuate models is detailed below
- The bounding box loss `box loss` is reported and expected to drop as the training progresses
- The object localization loss `object loss` is reported and expected to drop as the training progresses
- The class loss `class loss` is reported and expected to drop as the training progresses. If the class loss persists at 0.0000, it's because your dataset has a single class.
- The `mAP50` and `mAP0.5-0.95` metrics are expected to increase. This signifies the models accuracy increases. There might be flunctuations in these metrics sometimes.
- For each increase in the `mAP50` after an experiment, a model is saved in the **hololens-yolo/models** folder. The higher the mAP50, the better the model.
Once you are done training, you can visit the link below for performing object detection with your **custom detection model** and **detection_config.json** file.
[Detection/Custom/CUSTOMDETECTION.md](./CUSTOMDETECTION.md)
### >> Documentation
<div id="documentation" ></div>
We have provided full documentation for all **ImageAI** classes and functions. Find links below:
* Documentation - **English Version** [https://imageai.readthedocs.io](https://imageai.readthedocs.io)
================================================
FILE: imageai/Detection/Custom/CUSTOMVIDEODETECTION.md
================================================
# ImageAI : Custom Video Object Detection, Tracking and Analysis
### TABLE OF CONTENTS
- <a href="#videodetection" > :white_square_button: First Custom Video Object Detection</a>
- <a href="#camerainputs" > :white_square_button: Camera / Live Stream Video Detection</a>
- <a href="#videoanalysis" > :white_square_button: Video Analysis</a>
- <a href="#hidingdetails" > :white_square_button: Hiding/Showing Object Name and Probability</a>
- <a href="#videodetectionintervals" > :white_square_button: Frame Detection Intervals</a>
- <a href="#detectiontimeout" > :white_square_button: Video Detection Timeout (NEW)</a>
- <a href="#documentation" > :white_square_button: Documentation</a>
ImageAI provides convenient, flexible and powerful methods to perform object detection on videos using your own **custom YOLOv3 model** and the corresponding **.json** file generated during the training. This version of **ImageAI** provides commercial grade video objects detection features, which include but not limited to device/IP camera inputs, per frame, per second, per minute and entire video analysis for storing in databases and/or real-time visualizations and for future insights.
To test the custom video object detection,you can download a sample custom model we have trained to detect the Hololens headset and its **.json** file via the links below:
* [**yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt**](https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt) _(Size = 236 mb)_
* [**hololens-yolo_yolov3_detection_config.json**](https://github.com/OlafenwaMoses/ImageAI/releases/download/3.0.0-pretrained/hololens-yolo_yolov3_detection_config.json)
Because video object detection is a compute intensive tasks, we advise you perform this experiment using a computer with a NVIDIA GPU and the GPU version of Tensorflow
installed. Performing Video Object Detection CPU will be slower than using an NVIDIA GPU powered computer. You can use Google Colab for this
experiment as it has an NVIDIA K80 GPU available for free.
<br/>
Once you download the custom object detection model and JSON files, you should copy the model and the JSON files to the your project folder where your .py files will be.
Then create a python file and give it a name; an example is FirstCustomVideoObjectDetection.py. Then write the code below into the python file: <br/>
### FirstCustomVideoObjectDetection.py
<div id="videodetection" ></div>
```python
from imageai.Detection.Custom import CustomVideoObjectDetection
import os
execution_path = os.getcwd()
video_detector = CustomVideoObjectDetection()
video_detector.setModelTypeAsYOLOv3()
video_detector.setModelPath("yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt")
video_detector.setJsonPath("hololens-yolo_yolov3_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)
```
[**Input Video**](../../../data-videos/holo1.mp4)
[](../../../data-videos/holo1.mp4)
[**Output Video**](https://www.youtube.com/watch?v=4o5GyAR4Mpw)
[](https://www.youtube.com/watch?v=4o5GyAR4Mpw)
Let us make a breakdown of the object detection code that we used above.
```python
from imageai.Detection.Custom import CustomVideoObjectDetection
import os
execution_path = os.getcwd()
```
In the 3 lines above , we import the **ImageAI custom video object detection** class in the first line, import the **os** in the second line and obtained
the path to folder where our python file runs.
```python
video_detector = CustomVideoObjectDetection()
video_detector.setModelTypeAsYOLOv3()
video_detector.setModelPath("yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt")
video_detector.setJsonPath("hololens-yolo_yolov3_detection_config.json")
video_detector.loadModel()
```
In the 4 lines above, we created a new instance of the `CustomVideoObjectDetection` class in the first line, set the model type to YOLOv3 in the second line,
set the model path to our custom YOLOv3 model file in the third line, specified the path to the model's corresponding **hololens-yolo_yolov3_detection_config.json** in the fourth line and load the model in the fifth line.
```python
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)
```
In the code above, we ran the `detectObjectsFromVideo()` function and parse in the path to our video,the path to the new
video (without the extension, it saves a .mp4 video by default) which the function will save, the number of frames per second (fps) that
you we desire the output video to have and option to log the progress of the detection in the console. Then the function returns a the path to the saved video
which contains boxes and percentage probabilities rendered on objects detected in the video.
### Camera / Live Stream Video Detection
<div id="camerainputs"></div>
**ImageAI** now allows live-video detection with support for camera inputs. Using **OpenCV**'s **VideoCapture()** function, you can load live-video streams from a device camera, cameras connected by cable or IP cameras, and parse it into **ImageAI**'s **detectObjectsFromVideo()** function. All features that are supported for detecting objects in a video file is also available for detecting objects in a camera's live-video feed. Find below an example of detecting live-video feed from the device camera.
```python
from imageai.Detection.Custom import CustomVideoObjectDetection
import os
import cv2
execution_path = os.getcwd()
camera = cv2.VideoCapture(0)
video_detector = CustomVideoObjectDetection()
video_detector.setModelTypeAsYOLOv3()
video_detector.setModelPath("yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt")
video_detector.setJsonPath("hololens-yolo_yolov3_detection_config.json")
video_detector.loadModel()
video_detector.detectObjectsFromVideo(camera_input=camera,
output_file_path=os.path.join(execution_path, "holo1-detected3"),
frames_per_second=20,
minimum_percentage_probability=40,
log_progress=True)
```
The difference in the code above and the code for the detection of a video file is that we defined an **OpenCV VideoCapture** instance and loaded the default device camera into it. Then we parsed the camera we defined into the parameter **camera_input** which replaces the **input_file_path** that is used for video file.
### Video Analysis
<div id="videoanalysis"></div>
**ImageAI** now provide commercial-grade video analysis in the Custom Video Object Detection class, for both video file inputs and camera inputs. This feature allows developers to obtain deep insights into any video processed with **ImageAI**. This insights can be visualized in real-time, stored in a NoSQL database for future review or analysis. <br/>
For video analysis, the **detectObjectsFromVideo()** now allows you to state your own defined functions which will be executed for every frame, seconds and/or minute of the video detected as well as a state a function that will be executed at the end of a video detection. Once this functions are stated, they will receive raw but comprehensive analytical data on the index of the frame/second/minute, objects detected (name, percentage_probability and box_points), number of instances of each unique object detected and average number of occurrence of each unique object detected over a second/minute and entire video.
To obtain the video analysis, all you need to do is specify a function, state the corresponding parameters it will be receiving and parse the function name into the **per_frame_function**, **per_second_function**, **per_minute_function** and **video_complete_function** parameters in the detection function. Find below examples of video analysis functions.
```python
def forFrame(frame_number, output_array, output_count):
print("FOR FRAME " , frame_number)
print("Output for each object : ", output_array)
print("Output count for unique objects : ", output_count)
print("------------END OF A FRAME --------------")
def forSeconds(second_number, output_arrays, count_arrays, average_output_count):
print("SECOND : ", second_number)
print("Array for the outputs of each frame ", output_arrays)
print("Array for output count for unique objects in each frame : ", count_arrays)
print("Output average count for unique objects in the last second: ", average_output_count)
print("------------END OF A SECOND --------------")
def forMinute(minute_number, output_arrays, count_arrays, average_output_count):
print("MINUTE : ", minute_number)
print("Array for the outputs of each frame ", output_arrays)
print("Array for output count for unique objects in each frame : ", count_arrays)
print("Output average count for unique objects in the last minute: ", average_output_count)
print("------------END OF A MINUTE --------------")
video_detector = CustomVideoObjectDetection()
video_detector.setModelTypeAsYOLOv3()
video_detector.setModelPath("yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt")
video_detector.setJsonPath("hololens-yolo_yolov3_detection_config.json")
video_detector.loadModel()
video_detector.detectObjectsFromVideo(camera_input=camera,
output_file_path=os.path.join(execution_path, "holo1-detected3"),
frames_per_second=20, per_second_function=forSeconds, per_frame_function = forFrame, per_minute_function= forMinute,
minimum_percentage_probability=40,
log_progress=True)
```
**ImageAI** also allows you to obtain complete analysis of the entire video processed. All you need is to define a function like the forSecond or forMinute function and set the **video_complete_function** parameter into your **.detectObjectsFromVideo()** function. The same values for the per_second-function and per_minute_function will be returned. The difference is that no index will be returned and the other 3 values will be returned, and the 3 values will cover all frames in the video. Below is a sample function:
```python
def forFull(output_arrays, count_arrays, average_output_count):
#Perform action on the 3 parameters returned into the function
video_detector.detectObjectsFromVideo(camera_input=camera,
output_file_path=os.path.join(execution_path, "holo1-detected3"),
video_complete_function=forFull,
minimum_percentage_probability=40,
log_progress=True)
```
**FINAL NOTE ON VIDEO ANALYSIS** : **ImageAI** allows you to obtain the detected video frame as a Numpy array at each frame, second and minute function. All you need to do is specify one more parameter in your function and set **return_detected_frame=True** in your **detectObjectsFromVideo()** function. Once this is set, the extra parameter you sepecified in your function will be the Numpy array of the detected frame. See a sample below:
```python
def forFrame(frame_number, output_array, output_count, detected_frame):
print("FOR FRAME " , frame_number)
print("Output for each object : ", output_array)
print("Output count for unique objects : ", output_count)
print("Returned Objects is : ", type(detected_frame))
print("------------END OF A FRAME --------------")
video_detector.detectObjectsFromVideo(camera_input=camera,
output_file_path=os.path.join(execution_path, "holo1-detected3"),
per_frame_function=forFrame,
minimum_percentage_probability=40,
log_progress=True, return_detected_frame=True)
```
### Frame Detection Intervals
<div id="videodetectionintervals" ></div>
The above video objects detection task are optimized for frame-real-time object detections that ensures that objects in every frame
of the video is detected. **ImageAI** provides you the option to adjust the video frame detections which can speed up
your video detection process. When calling the `.detectObjectsFromVideo()`, you can
specify at which frame interval detections should be made. By setting the **frame_detection_interval** parameter to be
equal to 5 or 20, that means the object detections in the video will be updated after 5 frames or 20 frames.
If your output video **frames_per_second** is set to 20, that means the object detections in the video will
be updated once in every quarter of a second or every second. This is useful in case scenarios where the available
compute is less powerful and speeds of moving objects are low. This ensures you can have objects detected as second-real-time
, half-a-second-real-time or whichever way suits your needs.
### Custom Video Detection Timeout
<div id="detectiontimeout"></div>
**ImageAI** now allows you to set a timeout in seconds for detection of objects in videos or camera live feed. To set a timeout for your video detection code, all you need to do is specify the `detection_timeout` parameter in the `detectObjectsFromVideo()` function to the number of desired seconds. In the example code below, we set `detection_timeout` to 120 seconds (2 minutes).
```python
from imageai.Detection.Custom import CustomVideoObjectDetection
import os
import cv2
execution_path = os.getcwd()
camera = cv2.VideoCapture(0)
video_detector = CustomVideoObjectDetection()
video_detector.setModelTypeAsYOLOv3()
video_detector.setModelPath("yolov3_hololens-yolo_mAP-0.82726_epoch-73.pt")
video_detector.setJsonPath("hololens-yolo_yolov3_detection_config.json")
video_detector.loadModel()
video_detector.detectObjectsFromVideo(camera_input=camera,
output_file_path=os.path.join(execution_path, "holo1-detected3"),
frames_per_second=20, minimum_percentage_probability=40,
detection_timeout=120)
```
### >> Documentation
<div id="documentation" ></div>
We have provided full documentation for all **ImageAI** classes and functions. Find links below:
* Documentation - **English Version** [https://imageai.readthedocs.io](https://imageai.readthedocs.io)**
================================================
FILE: imageai/Detection/Custom/__init__.py
================================================
import os
import time
import math
import json
import warnings
from typing import List, Union, Tuple, Dict
from collections import defaultdict
import numpy as np
from PIL import Image
import cv2
import torch
from torch.cuda import amp
from torch.utils.data import DataLoader
from torch.optim import SGD, lr_scheduler
from tqdm import tqdm
from .yolo.dataset import LoadImagesAndLabels
from .yolo.custom_anchors import generate_anchors
from .yolo.compute_loss import compute_loss
from .yolo import validate
from ...yolov3.tiny_yolov3 import YoloV3Tiny
from ...yolov3.yolov3 import YoloV3
from ...yolov3.utils import draw_bbox_and_label, get_predictions, prepare_image
from ...backend_check.model_extension import extension_check
class DetectionModelTrainer:
"""
This is the Detection Model training class, which allows you to train object detection models
on image datasets that are in YOLO format, using the YOLOv3.
"""
def __init__(self) -> None:
self.__device = "cuda" if torch.cuda.is_available() else "cpu"
self.__cuda = (self.__device != "cpu")
self.__model_type = ""
self.__model = None
self.__optimizer = None
self.__data_dir = ""
self.__classes: List[str] = None
self.__num_classes = None
self.__anchors = None
self.__dataset_name = None
self.__mini_batch_size: int = None
self.__scaler = amp.GradScaler(enabled=self.__cuda)
self.__lr_lambda = None
self.__custom_train_dataset = None
self.__custom_val_dataset = None
self.__train_loader = None
self.__val_loader = None
self.__model_path: str = None
self.__epochs: int = None
self.__output_models_dir: str = None
self.__output_json_dir: str = None
def __set_training_param(self, epochs : int, accumulate : int) -> None:
# self.__lr_lambda = lambda x : ((1 - math.cos(x * math.pi / epochs)) / 2 ) * (0.1 - 1.0) + 1.0
self.__lr_lambda = lambda x: (1 - x / (epochs - 1)) * (1.0 - 0.01) + 0.01
self.__anchors = generate_anchors(
self.__custom_train_dataset,
n=9 if self.__model_type=="yolov3" else 6
)
self.__anchors = [round(i) for i in self.__anchors.reshape(-1).tolist()]
if self.__model_type == "yolov3":
self.__model = YoloV3(
num_classes=self.__num_classes,
anchors=self.__anchors,
device=self.__device
)
elif self.__model_type == "tiny-yolov3":
self.__model = YoloV3Tiny(
num_classes=self.__num_classes,
anchors=self.__anchors,
device=self.__device
)
if self.__model_path:
self.__load_model()
w_d = (5e-4) * (self.__mini_batch_size * accumulate / 64) # scale weight decay
g0, g1, g2 = [], [], [] # optimizer parameter groups
for m in self.__model.modules():
if hasattr(m, 'bias') and isinstance(m.bias, torch.nn.Parameter): # bias
g2.append(m.bias)
if isinstance(m, torch.nn.BatchNorm2d): # weight (no decay)
g0.append(m.weight)
elif hasattr(m, 'weight') and isinstance(m.weight, torch.nn.Parameter): # weight (with decay)
g1.append(m.weight)
self.__optimizer = SGD(
g0,
lr=1e-2,
momentum=0.6,
# weight_decay=w_d,
nesterov=True
)
self.__optimizer.add_param_group({'params': g1, 'weight_decay': w_d}) # add g1 with weight_decay
self.__optimizer.add_param_group({'params': g2}) # add g2 (biases)
self.__lr_scheduler = lr_scheduler.LambdaLR(
self.__optimizer,
lr_lambda=self.__lr_lambda
)
del g0, g1, g2
self.__model.to(self.__device)
def __load_model(self) -> None:
try:
state_dict = torch.load(self.__model_path, map_location=self.__device)
# check against cases where number of classes differs, causing the
# channel of the convolutional layer just before the detection layer
# to differ.
new_state_dict = {k:v for k,v in state_dict.items() if k in self.__model.state_dict().keys() and v.shape==self.__model.state_dict()[k].shape}
self.__model.load_state_dict(new_state_dict, strict=False)
print("="*20)
print("Pretrained YOLOv3 model loaded to initialize weights")
print("="*20)
except Exception as e:
print("="*20)
print("pretrained weight loading failed. Defaulting to using random weight.")
print("="*20)
def __load_data(self) -> None:
self.__num_classes = len(self.__classes)
self.__dataset_name = os.path.basename(os.path.dirname(self.__data_dir+os.path.sep))
self.__custom_train_dataset = LoadImagesAndLabels(self.__data_dir, train=True)
self.__custom_val_dataset = LoadImagesAndLabels(self.__data_dir, train=False)
self.__train_loader = DataLoader(
self.__custom_train_dataset, batch_size=self.__mini_batch_size,
shuffle=True,
collate_fn=self.__custom_train_dataset.collate_fn
)
self.__val_loader = DataLoader(
self.__custom_val_dataset, batch_size=self.__mini_batch_size//2,
shuffle=True, collate_fn=self.__custom_val_dataset.collate_fn
)
def setModelTypeAsYOLOv3(self) -> None:
"""
'setModelTypeAsYOLOv3()' is used to set the model type to the YOLOv3 model.
:return:
"""
self.__model_type = "yolov3"
def setModelTypeAsTinyYOLOv3(self) -> None:
"""
'setModelTypeAsTinyYOLOv3()' is used to set the model type to the TinyYOLOv3 model.
:return:
"""
self.__model_type = "tiny-yolov3"
def setDataDirectory(self, data_directory: str):
"""
'setDataDirectory()' is required to set the path to which the data/dataset to be used for training is kept. The input dataset must be in the YOLO format. The directory can have any name, but it must have 'train' and 'validation'
sub-directory. In the 'train' and 'validation' sub-directories, there must be 'images' and 'annotations'
sub-directories respectively. The 'images' folder will contain the pictures for the dataset and the
'annotations' folder will contain the TXT files with details of the annotations for each image in the
'images folder'.
N.B: Strictly take note that the filenames (without the extension) of the pictures in the 'images folder'
must be the same as the filenames (except the extension) of their corresponding annotation TXT files in
the 'annotations' folder.
The structure of the 'train' and 'validation' folder must be as follows:
>> train >> images >> img_1.jpg
>> images >> img_2.jpg
>> images >> img_3.jpg
>> annotations >> img_1.txt
>> annotations >> img_2.txt
>> annotations >> img_3.txt
>> validation >> images >> img_151.jpg
>> images >> img_152.jpg
>> images >> img_153.jpg
>> annotations >> img_151.txt
>> annotations >> img_152.txt
>> annotations >> img_153.txt
:param data_directory:
:return:
"""
if os.path.isdir(data_directory):
self.__data_dir = data_directory
else:
raise ValueError(
"The parameter passed should point to a valid directory"
)
def setTrainConfig(self, object_names_array: List[str], batch_size: int=4, num_experiments=100, train_from_pretrained_model: str = None):
"""
'setTrainConfig()' function allows you to set the properties for the training instances. It accepts the following values:
- object_names_array , this is an array of the names of the different objects in your dataset, in the index order your dataset is annotated
- batch_size (optional), this is the batch size for the training instance
- num_experiments (optional), also known as epochs, it is the number of times the network will train on all the training dataset
- train_from_pretrained_model (optional), this is used to perform transfer learning by specifying the path to a pre-trained YOLOv3 or TinyYOLOv3 model
:param object_names_array:
:param batch_size:
:param num_experiments:
:param train_from_pretrained_model:
:return:
"""
self.__model_path = train_from_pretrained_model
if self.__model_path:
extension_check(self.__model_path)
self.__classes = object_names_array
self.__mini_batch_size = batch_size
self.__epochs = num_experiments
self.__output_models_dir = os.path.join(self.__data_dir, "models")
self.__output_json_dir = os.path.join(self.__data_dir, "json")
def trainModel(self) -> None:
"""
'trainModel()' function starts the actual model training. Once the training starts, the training instance
creates 3 sub-folders in your dataset folder which are:
- json, where the JSON configuration file for using your trained model is stored
- models, where your trained models are stored once they are generated after each improved experiments
- cache , where temporary traing configuraton files are stored
:return:
"""
self.__load_data()
os.makedirs(self.__output_models_dir, exist_ok=True)
os.makedirs(self.__output_json_dir, exist_ok=True)
mp, mr, map50, map50_95, best_fitness = 0, 0, 0, 0, 0.0
nbs = 64 # norminal batch size
nb = len(self.__train_loader) # number of batches
nw = max(3 * nb, 1000) # number of warmup iterations.
last_opt_step = -1
prev_save_name, recent_save_name = "", ""
accumulate = max(round(nbs / self.__mini_batch_size), 1) # accumulate loss before optimizing.
self.__set_training_param(self.__epochs, accumulate)
with open(os.path.join(self.__output_json_dir, f"{self.__dataset_name}_{self.__model_type}_detection_config.json"), "w") as configWriter:
json.dump(
{
"labels": self.__classes,
"anchors": self.__anchors
},
configWriter
)
since = time.time()
self.__lr_scheduler.last_epoch = -1
for epoch in range(1, self.__epochs+1):
self.__optimizer.zero_grad()
mloss = torch.zeros(3, device=self.__device)
print(f"Epoch {epoch}/{self.__epochs}", "-"*10, sep="\n")
for phase in ["train", "validation"]:
if phase=="train":
self.__model.train()
print("Train: ")
for batch_i, (data, anns) in tqdm(enumerate(self.__train_loader)):
batches_done = batch_i + nb * epoch
data = data.to(self.__device)
anns = anns.to(self.__device)
# warmup
if batches_done <= nw:
xi = [0, nw] # x interp
accumulate = max(1, np.interp(batches_done, xi, [1, nbs / self.__mini_batch_size]).round())
for j, x in enumerate(self.__optimizer.param_groups):
# bias lr falls from 0.1 to lr0, all other lrs rise from 0.0 to lr0
x['lr'] = np.interp(batches_done, xi, [0.1 if j == 2 else 0.0, 0.01 * self.__lr_lambda(epoch)])
if 'momentum' in x:
x['momentum'] = np.interp(batches_done, xi, [0.8, 0.9])
with amp.autocast(enabled=self.__cuda):
_ = self.__model(data)
loss_layers = self.__model.get_loss_layers()
loss, loss_components = compute_loss(loss_layers, anns.detach(), self.__device)
self.__scaler.scale(loss).backward()
mloss = (mloss * batch_i + loss_components) / (batch_i + 1)
# Optimize
if batches_done - last_opt_step >= accumulate:
self.__scaler.step(self.__optimizer) # optimizer.step
self.__scaler.update()
self.__optimizer.zero_grad()
last_opt_step = batches_done
print(f" box loss-> {float(mloss[0]):.5f}, object loss-> {float(mloss[1]):.5f}, class loss-> {float(mloss[2]):.5f}")
self.__lr_scheduler.step()
else:
self.__model.eval()
print("Validation:")
mp, mr, map50, map50_95 = validate.run(
self.__model, self.__val_loader,
self.__num_classes, device=self.__device
)
print(f" recall: {mr:0.6f} precision: {mp:0.6f} mAP@0.5: {map50:0.6f}, mAP@0.5-0.95: {map50_95:0.6f}" "\n")
if map50 > best_fitness:
best_fitness = map50
recent_save_name = self.__model_type+f"_{self.__dataset_name}_mAP-{best_fitness:0.5f}_epoch-{epoch}.pt"
if prev_save_name:
os.remove(os.path.join(self.__output_models_dir, prev_save_name))
torch.save(
self.__model.state_dict(),
os.path.join(self.__output_models_dir, recent_save_name)
)
prev_save_name = recent_save_name
if epoch == self.__epochs:
torch.save(
self.__model.state_dict(),
os.path.join(self.__output_models_dir, self.__model_type+f"_{self.__dataset_name}_last.pt")
)
elapsed_time = time.time() - since
print(f"Training completed in {elapsed_time//60:.0f}m {elapsed_time % 60:.0f}s")
torch.cuda.empty_cache()
class CustomObjectDetection:
"""
This is the object detection class for using your custom trained models.
It supports your custom trained YOLOv3 and TinyYOLOv3 model and allows
to you to perform object detection in images.
"""
def __init__(self) -> None:
self.__device = "cuda" if torch.cuda.is_available() else "cpu"
self.__anchors: List[int] = None
self.__classes: List[str] = None
self.__model = None
self.__model_loaded: bool = False
self.__model_path: str = None
self.__json_path: str = None
self.__model_type: str = None
self.__nms_score = 0.4
self.__objectness_score = 0.4
def setModelTypeAsYOLOv3(self) -> None:
"""
'setModelTypeAsYOLOv3()' is used to set the model type to the YOLOv3 model.
:return:
"""
self.__model_type = "yolov3"
def setModelTypeAsTinyYOLOv3(self) -> None:
"""
'setModelTypeAsTinyYOLOv3()' is used to set the model type to the TinyYOLOv3 model.
:return:
"""
self.__model_type = "tiny-yolov3"
def setModelPath(self, model_path: str):
if os.path.isfile(model_path):
extension_check(model_path)
self.__model_path = model_path
self.__model_loaded = False
else:
raise ValueError(
"invalid path, path not pointing to the weightfile."
) from None
self.__model_path = model_path
def setJsonPath(self, configuration_json: str):
self.__json_path = configuration_json
def __load_classes_and_anchors(self) -> List[str]:
with open(self.__json_path) as f:
json_config = json.load(f)
self.__anchors = json_config["anchors"]
self.__classes = json_config["labels"]
def __load_image_yolo(self, input_image : Union[str, np.ndarray, Image.Image]) -> Tuple[List[str], List[np.ndarray], torch.Tensor, torch.Tensor]:
"""
Loads image/images from the given path. If the given path is a directory,
this function only load the images in the directory (it does noot visit the
subdirectories).
"""
allowed_exts = ["jpg", "jpeg", "png"]
fnames = []
original_dims = []
inputs = []
original_imgs = []
if type(input_image) == str:
if os.path.isfile(input_image):
if input_image.rsplit('.')[-1].lower() in allowed_exts:
img = cv2.imread(input_image)
else:
raise ValueError(f"image path '{input_image}' is not found or a valid file")
elif type(input_image) == np.ndarray:
img = input_image
elif "PIL" in str(type(input_image)):
img = np.asarray(input_image)
else:
raise ValueError(f"Invalid image input format")
img_h, img_w, _ = img.shape
original_imgs.append(np.array(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)).astype(np.uint8))
original_dims.append((img_w, img_h))
if type(input_image) == str:
fnames.append(os.path.basename(input_image))
else:
fnames.append("")
inputs.append(prepare_image(img, (416, 416)))
if original_dims:
return (
fnames,
original_imgs,
torch.FloatTensor(original_dims).repeat(1,2).to(self.__device),
torch.cat(inputs, 0).to(self.__device)
)
raise RuntimeError(
f"Error loading image."
"\nEnsure the file is a valid image,"
" allowed file extensions are .jpg, .jpeg, .png"
)
def useCPU(self):
"""
Used to force classification to be done on CPU.
By default, classification will occur on GPU compute if available else CPU compute.
"""
self.__device = "cpu"
if self.__model_loaded:
self.__model_loaded = False
self.loadModel()
def loadModel(self) -> None:
"""
Loads the pretrained weights in the specified model path.
"""
self.__load_classes_and_anchors()
if self.__model_type == "yolov3":
self.__model = YoloV3(
anchors=self.__anchors,
num_classes=len(self.__classes),
device=self.__device
)
elif self.__model_type == "tiny-yolov3":
self.__model = YoloV3Tiny(
anchors=self.__anchors,
num_classes=len(self.__classes),
device=self.__device
)
else:
raise ValueError(f"Invalid model type. Call setModelTypeAsYOLOv3() or setModelTypeAsTinyYOLOv3() to set a model type before loading the model")
self.__model.to(self.__device)
state_dict = torch.load(self.__model_path, map_location=self.__device)
try:
self.__model.load_state_dict(state_dict)
self.__model_loaded = True
self.__model.to(self.__device).eval()
except Exception as e:
raise RuntimeError(f"Invalid weights!!! {e}")
def detectObjectsFromImage(self,
input_image: Union[str, np.ndarray, Image.Image],
output_image_path: str=None,
output_type: str ="file",
extract_detected_objects: bool=False, minimum_percentage_probability: int=40,
display_percentage_probability: bool=True, display_object_name: bool=True,
display_box: bool=True,
custom_objects: List=None,
nms_treshold: float= 0.4,
objectness_treshold: float= 0.4,
) -> Union[List[List[Tuple[str, float, Dict[str, int]]]], np.ndarray, List[np.ndarray], List[str]]:
"""
Detects objects in an image using the unique classes provided
by COCO.
:param input_image: path to an image file, cv2 image or PIL image
:param output_image_path: path to save input image with predictions rendered
:param output_type: type of output for rendered image. Acceptable values are 'file' and 'array` ( a cv2 image )
:param extract_detected_objects: extract each object based on the output type
:param minimum_percentage_probability: the minimum confidence a detected object must have
:param display_percentage_probability: to diplay/not display the confidence on rendered image
:param display_object_name: to diplay/not display the object name on rendered image
:param display_box: to diplay/not display the object bounding box on rendered image
:param custom_objects: a dictionary of detectable objects set to boolean values
:returns: A list of tuples containing the label of detected object and the
confidence.
"""
self.__nms_score = nms_treshold
self.__objectness_score = objectness_treshold
self.__model.eval()
if not self.__model_loaded:
if self.__model_path:
warnings.warn(
"Model path has changed but pretrained weights in the"
" new path is yet to be loaded.",
ResourceWarning
)
else:
raise RuntimeError(
"Model path isn't set, pretrained weights aren't used."
)
predictions = defaultdict(lambda : [])
if self.__model_type == "yolov3" or self.__model_type == "tiny-yolov3":
fnames, original_imgs, input_dims, imgs = self.__load_image_yolo(input_image)
with torch.no_grad():
output = self.__model(imgs)
output = get_predictions(
pred=output.to(self.__device), num_classes=len(self.__classes),
nms_confidence_level=self.__nms_score, objectness_confidence= self.__objectness_score,
device=self.__device
)
if output is None:
if output_type == "array":
if extract_detected_objects:
return original_imgs[0], [], []
else:
return original_imgs[0], []
else:
if extract_detected_objects:
return original_imgs[0], []
else:
return []
# scale the output to match the dimension of the original image
input_dims = torch.index_select(input_dims, 0, output[:, 0].long())
scaling_factor = torch.min(416 / input_dims, 1)[0].view(-1, 1)
output[:, [1,3]] -= (416 - (scaling_factor * input_dims[:, 0].view(-1,1))) / 2
output[:, [2,4]] -= (416 - (scaling_factor * input_dims[:, 1].view(-1,1))) / 2
output[:, 1:5] /= scaling_factor
#clip bounding box for those that extended outside the detected image.
for idx in range(output.shape[0]):
output[idx, [1,3]] = torch.clamp(output[idx, [1,3]], 0.0, input_dims[idx, 0])
output[idx, [2,4]] = torch.clamp(output[idx, [2,4]], 0.0, input_dims[idx, 1])
for pred in output:
pred_label = self.__classes[int(pred[-1])]
if custom_objects:
if pred_label.replace(" ", "_") in custom_objects.keys():
if not custom_objects[pred_label.replace(" ", "_")]:
continue
else:
continue
predictions[int(pred[0])].append((
pred_label,
float(pred[-2]),
{k:v for k,v in zip(["x1", "y1", "x2", "y2"], map(int, pred[1:5]))},
))
# Render detection on copy of input image
original_input_image = None
output_image_array = None
extracted_objects = []
if self.__model_type == "yolov3" or self.__model_type == "tiny-yolov3":
original_input_image = cv2.cvtColor(original_imgs[0], cv2.COLOR_RGB2BGR)
if isinstance(output, torch.Tensor):
for pred in output:
percentage_conf = round(float(pred[-2]) * 100, 2)
if percentage_conf < minimum_percentage_probability:
continue
displayed_label = ""
if display_object_name:
displayed_label = f"{self.__classes[int(pred[-1].item())]} : "
if display_percentage_probability:
displayed_label += f" {percentage_conf}%"
original_imgs[int(pred[0].item())] = draw_bbox_and_label(pred[1:5].int() if display_box else None,
displayed_label,
original_imgs[int(pred[0].item())]
)
output_image_array = cv2.cvtColor(original_imgs[0], cv2.COLOR_RGB2BGR)
# Format predictions for function reponse
predictions_batch = list(predictions.values())
predictions_list = predictions_batch[0] if len(predictions_batch) > 0 else []
min_probability = minimum_percentage_probability / 100
if output_type == "file":
if output_image_path:
cv2.imwrite(output_image_path, output_image_array)
if extract_detected_objects:
extraction_dir = ".".join(output_image_path.split(".")[:-1]) + "-extracted"
os.mkdir(extraction_dir)
count = 0
for obj_prediction in predictions_list:
if obj_prediction[1] >= min_probability:
count += 1
extracted_path = os.path.join(
extraction_dir,
".".join(os.path.basename(output_image_path).split(".")[:-
gitextract_hhet6k5q/
├── .codecov.yml
├── .github/
│ ├── FUNDING.yml
│ └── workflows/
│ └── build.yml
├── .gitignore
├── .travis.yml
├── BACKEND_MIGRATION.md
├── LICENSE
├── MANIFEST.in
├── README.md
├── examples/
│ ├── camera_feed_detection.py
│ ├── custom_detection.py
│ ├── custom_detection_array_input_output.py
│ ├── custom_detection_extract_objects.py
│ ├── custom_detection_from_array_extract_objects_array.py
│ ├── custom_detection_from_file_extract_objects_array.py
│ ├── custom_detection_train.py
│ ├── custom_detection_video.py
│ ├── custom_model_prediction.py
│ ├── custom_model_training.py
│ ├── image_custom_object_detection.py
│ ├── image_prediction.py
│ ├── object_detection.py
│ ├── video_analysis_per_frame.py
│ ├── video_analysis_per_second.py
│ ├── video_custom_object_detection.py
│ └── video_object_detection.py
├── imageai/
│ ├── Classification/
│ │ ├── CUSTOMCLASSIFICATION.md
│ │ ├── CUSTOMTRAINING.md
│ │ ├── Custom/
│ │ │ ├── __init__.py
│ │ │ ├── data_transformation.py
│ │ │ └── training_params.py
│ │ ├── README.md
│ │ ├── __init__.py
│ │ └── imagenet_classes.txt
│ ├── Detection/
│ │ ├── Custom/
│ │ │ ├── CUSTOMDETECTION.md
│ │ │ ├── CUSTOMDETECTIONTRAINING.md
│ │ │ ├── CUSTOMVIDEODETECTION.md
│ │ │ ├── __init__.py
│ │ │ └── yolo/
│ │ │ ├── __init__.py
│ │ │ ├── compute_loss.py
│ │ │ ├── custom_anchors.py
│ │ │ ├── dataset.py
│ │ │ ├── metric.py
│ │ │ └── validate.py
│ │ ├── README.md
│ │ ├── VIDEO.md
│ │ ├── __init__.py
│ │ ├── coco91_classes.txt
│ │ └── coco_classes.txt
│ ├── __init__.py
│ ├── backend_check/
│ │ ├── __init__.py
│ │ ├── backend_check.py
│ │ └── model_extension.py
│ ├── densenet121/
│ │ └── __init__.py
│ ├── inceptionv3/
│ │ └── __init__.py
│ ├── mobilenetv2/
│ │ └── __init__.py
│ ├── resnet50/
│ │ └── __init__.py
│ ├── retinanet/
│ │ ├── __init__.py
│ │ └── utils.py
│ └── yolov3/
│ ├── __init__.py
│ ├── tiny_yolov3.py
│ ├── utils.py
│ └── yolov3.py
├── imageai_tf_deprecated/
│ ├── Classification/
│ │ ├── CUSTOMCLASSIFICATION.md
│ │ ├── CUSTOMTRAINING.md
│ │ ├── Custom/
│ │ │ └── __init__.py
│ │ ├── README.md
│ │ └── __init__.py
│ ├── Detection/
│ │ ├── Custom/
│ │ │ ├── CUSTOMDETECTION.md
│ │ │ ├── CUSTOMDETECTIONTRAINING.md
│ │ │ ├── CUSTOMVIDEODETECTION.md
│ │ │ ├── __init__.py
│ │ │ ├── callbacks.py
│ │ │ ├── evaluate.py
│ │ │ ├── gen_anchors.py
│ │ │ ├── generator.py
│ │ │ ├── utils/
│ │ │ │ ├── __init__.py
│ │ │ │ ├── bbox.py
│ │ │ │ ├── colors.py
│ │ │ │ ├── image.py
│ │ │ │ ├── multi_gpu_model.py
│ │ │ │ └── utils.py
│ │ │ └── voc.py
│ │ ├── README.md
│ │ ├── VIDEO.md
│ │ ├── YOLO/
│ │ │ ├── __init__.py
│ │ │ ├── utils.py
│ │ │ └── yolov3.py
│ │ ├── __init__.py
│ │ └── keras_retinanet/
│ │ ├── __init__.py
│ │ ├── backend/
│ │ │ ├── __init__.py
│ │ │ └── backend.py
│ │ ├── bin/
│ │ │ ├── __init__.py
│ │ │ ├── convert_model.py
│ │ │ ├── debug.py
│ │ │ ├── evaluate.py
│ │ │ └── train.py
│ │ ├── callbacks/
│ │ │ ├── __init__.py
│ │ │ ├── coco.py
│ │ │ ├── common.py
│ │ │ └── eval.py
│ │ ├── initializers.py
│ │ ├── layers/
│ │ │ ├── __init__.py
│ │ │ ├── _misc.py
│ │ │ └── filter_detections.py
│ │ ├── losses.py
│ │ ├── models/
│ │ │ ├── __init__.py
│ │ │ ├── densenet.py
│ │ │ ├── effnet.py
│ │ │ ├── mobilenet.py
│ │ │ ├── resnet.py
│ │ │ ├── retinanet.py
│ │ │ ├── senet.py
│ │ │ └── vgg.py
│ │ ├── preprocessing/
│ │ │ ├── __init__.py
│ │ │ ├── coco.py
│ │ │ ├── csv_generator.py
│ │ │ ├── generator.py
│ │ │ ├── kitti.py
│ │ │ ├── open_images.py
│ │ │ └── pascal_voc.py
│ │ └── utils/
│ │ ├── __init__.py
│ │ ├── anchors.py
│ │ ├── coco_eval.py
│ │ ├── colors.py
│ │ ├── compute_overlap.pyx
│ │ ├── config.py
│ │ ├── eval.py
│ │ ├── gpu.py
│ │ ├── image.py
│ │ ├── model.py
│ │ ├── tf_version.py
│ │ ├── transform.py
│ │ └── visualization.py
│ ├── Prediction/
│ │ ├── Custom/
│ │ │ ├── __init__.py
│ │ │ └── custom_utils.py
│ │ ├── __init__.py
│ │ └── imagenet_utils.py
│ └── __init__.py
├── requirements.txt
├── requirements_extra.txt
├── requirements_gpu.txt
├── scripts/
│ └── pascal_voc_to_yolo.py
├── setup.py
└── test/
├── test_custom_classification.py
├── test_custom_classification_training.py
├── test_custom_detection_training.py
├── test_custom_object_detection.py
├── test_custom_video_detection.py
├── test_image_classification.py
├── test_object_detection.py
└── test_video_object_detection.py
SYMBOL INDEX (732 symbols across 87 files)
FILE: examples/video_analysis_per_frame.py
function forFrame (line 13) | def forFrame(frame_number, output_array, output_count, returned_frame):
FILE: examples/video_analysis_per_second.py
function forSecond (line 13) | def forSecond(frame_number, output_arrays, count_arrays, average_count, ...
FILE: imageai/Classification/Custom/__init__.py
class ClassificationModelTrainer (line 26) | class ClassificationModelTrainer():
method __init__ (line 33) | def __init__(self) -> None:
method __set_training_param (line 49) | def __set_training_param(self) -> None:
method __set_transfer_learning_mode (line 90) | def __set_transfer_learning_mode(self) -> None:
method __load_data (line 114) | def __load_data(self, batch_size : int = 8) -> None:
method setDataDirectory (line 136) | def setDataDirectory(self, data_directory : str = "") -> None:
method setModelTypeAsMobileNetV2 (line 162) | def setModelTypeAsMobileNetV2(self) -> None:
method setModelTypeAsResNet50 (line 170) | def setModelTypeAsResNet50(self) -> None:
method setModelTypeAsInceptionV3 (line 178) | def setModelTypeAsInceptionV3(self) -> None:
method setModelTypeAsDenseNet121 (line 186) | def setModelTypeAsDenseNet121(self) -> None:
method freezeAllLayers (line 194) | def freezeAllLayers(self) -> None:
method fineTuneAllLayers (line 202) | def fineTuneAllLayers(self) -> None:
method trainModel (line 208) | def trainModel(
class CustomImageClassification (line 333) | class CustomImageClassification:
method __init__ (line 357) | def __init__(self) -> None:
method __load_image (line 366) | def __load_image(self, image_input: Union[str, np.ndarray, Image.Image...
method __load_classes (line 391) | def __load_classes(self):
method setModelPath (line 398) | def setModelPath(self, path : str) -> None:
method setJsonPath (line 411) | def setJsonPath(self, path : str) -> None:
method setModelTypeAsMobileNetV2 (line 422) | def setModelTypeAsMobileNetV2(self) -> None:
method setModelTypeAsResNet50 (line 429) | def setModelTypeAsResNet50(self) -> None:
method setModelTypeAsInceptionV3 (line 436) | def setModelTypeAsInceptionV3(self) -> None:
method setModelTypeAsDenseNet121 (line 443) | def setModelTypeAsDenseNet121(self) -> None:
method useCPU (line 450) | def useCPU(self):
method loadModel (line 460) | def loadModel(self) -> None:
method classifyImage (line 516) | def classifyImage(self, image_input: Union[str, np.ndarray, Image.Imag...
FILE: imageai/Classification/Custom/training_params.py
function resnet50_train_params (line 8) | def resnet50_train_params():
function inception_v3_train_params (line 19) | def inception_v3_train_params():
function mobilenet_v2_train_params (line 31) | def mobilenet_v2_train_params():
function densenet121_train_params (line 43) | def densenet121_train_params():
FILE: imageai/Classification/__init__.py
class ImageClassification (line 28) | class ImageClassification:
method __init__ (line 48) | def __init__(self) -> None:
method setModelPath (line 57) | def setModelPath(self, path: str):
method __load_classes (line 72) | def __load_classes(self) -> List[str]:
method __load_image (line 76) | def __load_image(self, image_input: Union[str, np.ndarray, Image.Image...
method setModelTypeAsResNet50 (line 101) | def setModelTypeAsResNet50(self):
method setModelTypeAsDenseNet121 (line 109) | def setModelTypeAsDenseNet121(self):
method setModelTypeAsInceptionV3 (line 117) | def setModelTypeAsInceptionV3(self):
method setModelTypeAsMobileNetV2 (line 125) | def setModelTypeAsMobileNetV2(self):
method useCPU (line 133) | def useCPU(self):
method loadModel (line 143) | def loadModel(self):
method classifyImage (line 191) | def classifyImage(self, image_input: Union[str, np.ndarray, Image.Imag...
FILE: imageai/Detection/Custom/__init__.py
class DetectionModelTrainer (line 29) | class DetectionModelTrainer:
method __init__ (line 35) | def __init__(self) -> None:
method __set_training_param (line 59) | def __set_training_param(self, epochs : int, accumulate : int) -> None:
method __load_model (line 108) | def __load_model(self) -> None:
method __load_data (line 124) | def __load_data(self) -> None:
method setModelTypeAsYOLOv3 (line 139) | def setModelTypeAsYOLOv3(self) -> None:
method setModelTypeAsTinyYOLOv3 (line 146) | def setModelTypeAsTinyYOLOv3(self) -> None:
method setDataDirectory (line 153) | def setDataDirectory(self, data_directory: str):
method setTrainConfig (line 185) | def setTrainConfig(self, object_names_array: List[str], batch_size: in...
method trainModel (line 207) | def trainModel(self) -> None:
class CustomObjectDetection (line 322) | class CustomObjectDetection:
method __init__ (line 328) | def __init__(self) -> None:
method setModelTypeAsYOLOv3 (line 340) | def setModelTypeAsYOLOv3(self) -> None:
method setModelTypeAsTinyYOLOv3 (line 347) | def setModelTypeAsTinyYOLOv3(self) -> None:
method setModelPath (line 354) | def setModelPath(self, model_path: str):
method setJsonPath (line 365) | def setJsonPath(self, configuration_json: str):
method __load_classes_and_anchors (line 368) | def __load_classes_and_anchors(self) -> List[str]:
method __load_image_yolo (line 375) | def __load_image_yolo(self, input_image : Union[str, np.ndarray, Image...
method useCPU (line 422) | def useCPU(self):
method loadModel (line 433) | def loadModel(self) -> None:
method detectObjectsFromImage (line 465) | def detectObjectsFromImage(self,
class CustomVideoObjectDetection (line 646) | class CustomVideoObjectDetection:
method __init__ (line 660) | def __init__(self):
method setModelTypeAsYOLOv3 (line 663) | def setModelTypeAsYOLOv3(self):
method setModelTypeAsTinyYOLOv3 (line 666) | def setModelTypeAsTinyYOLOv3(self):
method setModelPath (line 669) | def setModelPath(self, model_path: str):
method setJsonPath (line 673) | def setJsonPath(self, configuration_json: str):
method loadModel (line 676) | def loadModel(self):
method useCPU (line 679) | def useCPU(self):
method detectObjectsFromVideo (line 682) | def detectObjectsFromVideo(self, input_file_path="", camera_input=None...
FILE: imageai/Detection/Custom/yolo/compute_loss.py
function bbox_iou (line 8) | def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=Fal...
function compute_loss (line 55) | def compute_loss(loss_layers, targets, device="cpu"):
function build_targets (line 129) | def build_targets(p, targets, loss_layers, device="cpu"):
FILE: imageai/Detection/Custom/yolo/custom_anchors.py
function generate_anchors (line 9) | def generate_anchors(dataset, n=9, img_size=416, thr=4.0, gen=1000, verb...
FILE: imageai/Detection/Custom/yolo/dataset.py
class LoadImagesAndLabels (line 13) | class LoadImagesAndLabels(Dataset):
method __init__ (line 15) | def __init__(self, path : str, net_dim=(416, 416), train=True):
method __len__ (line 42) | def __len__(self) -> int:
method __img_path2label_path (line 45) | def __img_path2label_path(self, path : str) -> str:
method __getitem__ (line 49) | def __getitem__(self, idx) -> Tuple[torch.Tensor, torch.Tensor]:
method __xywhn2xyxy (line 57) | def __xywhn2xyxy(self, nlabel : torch.Tensor, width : int, height : in...
method __load_data (line 70) | def __load_data(self, img_path : str, label : np.ndarray) -> Tuple[tor...
method __load_raw_label (line 77) | def __load_raw_label(self, label_path : str):
method __process_label (line 87) | def __process_label(self, label : np.ndarray, image_width : int, image...
method collate_fn (line 121) | def collate_fn(self, batch) -> Tuple[torch.Tensor, torch.Tensor]:
FILE: imageai/Detection/Custom/yolo/metric.py
function ap_per_class (line 9) | def ap_per_class(tp, conf, pred_cls, target_cls):
function compute_ap (line 63) | def compute_ap(recall, precision):
FILE: imageai/Detection/Custom/yolo/validate.py
function xywh2xyxy (line 14) | def xywh2xyxy(box_coord : torch.Tensor):
function process_batch (line 27) | def process_batch(detections, labels, iouv):
function run (line 53) | def run(model, val_dataloader, num_class, net_dim=416, nms_thresh=0.6, o...
FILE: imageai/Detection/__init__.py
class ImageReadMode (line 25) | class ImageReadMode(Enum):
class ObjectDetection (line 42) | class ObjectDetection:
method __init__ (line 61) | def __init__(self) -> None:
method __load_classes (line 75) | def __load_classes(self, path: str) -> List[str]:
method __load_image_yolo (line 80) | def __load_image_yolo(self, input_image : Union[str, np.ndarray, Image...
method __save_temp_img (line 122) | def __save_temp_img(self, input_image : Union[np.ndarray, Image.Image]...
method __load_image_retinanet (line 139) | def __load_image_retinanet(self, input_image : str) -> Tuple[List[str]...
method setModelTypeAsYOLOv3 (line 174) | def setModelTypeAsYOLOv3(self):
method setModelTypeAsTinyYOLOv3 (line 182) | def setModelTypeAsTinyYOLOv3(self):
method setModelTypeAsRetinaNet (line 190) | def setModelTypeAsRetinaNet(self):
method setModelPath (line 198) | def setModelPath(self, path: str) -> None:
method useCPU (line 214) | def useCPU(self):
method loadModel (line 225) | def loadModel(self) -> None:
method CustomObjects (line 263) | def CustomObjects(self, **kwargs):
method detectObjectsFromImage (line 294) | def detectObjectsFromImage(self,
class VideoObjectDetection (line 541) | class VideoObjectDetection:
method __init__ (line 556) | def __init__(self):
method setModelTypeAsYOLOv3 (line 559) | def setModelTypeAsYOLOv3(self):
method setModelTypeAsTinyYOLOv3 (line 562) | def setModelTypeAsTinyYOLOv3(self):
method setModelTypeAsRetinaNet (line 565) | def setModelTypeAsRetinaNet(self):
method setModelPath (line 568) | def setModelPath(self, model_path: str):
method loadModel (line 572) | def loadModel(self):
method useCPU (line 575) | def useCPU(self):
method CustomObjects (line 578) | def CustomObjects(self, **kwargs):
method detectObjectsFromVideo (line 581) | def detectObjectsFromVideo(self, input_file_path="", camera_input=None...
FILE: imageai/backend_check/model_extension.py
function extension_check (line 3) | def extension_check(file_path: str):
FILE: imageai/densenet121/__init__.py
class DenseNet121Pretrained (line 12) | class DenseNet121Pretrained:
method __init__ (line 17) | def __init__(self, label_path : str) -> None:
method __load_classes (line 24) | def __load_classes(self, path : str) -> List[str]:
method __load_image (line 29) | def __load_image(self, image_path : str) -> Tuple[List[str], torch.Ten...
method set_model_path (line 72) | def set_model_path(self, path : str) -> None:
method load_model (line 84) | def load_model(self) -> None:
method classify (line 111) | def classify(self, image_path : str, top_n : int = 5, verbose : bool =...
FILE: imageai/inceptionv3/__init__.py
class InceptionV3Pretrained (line 12) | class InceptionV3Pretrained:
method __init__ (line 17) | def __init__(self, label_path : str) -> None:
method __load_classes (line 24) | def __load_classes(self, path : str) -> List[str]:
method __load_image (line 29) | def __load_image(self, image_path : str) -> Tuple[List[str], torch.Ten...
method set_model_path (line 72) | def set_model_path(self, path : str) -> None:
method load_model (line 84) | def load_model(self) -> None:
method classify (line 99) | def classify(self, image_path : str, top_n : int = 5, verbose : bool =...
FILE: imageai/mobilenetv2/__init__.py
class MobileNetV2Pretrained (line 12) | class MobileNetV2Pretrained:
method __init__ (line 17) | def __init__(self, label_path : str) -> None:
method __load_classes (line 24) | def __load_classes(self, path : str) -> List[str]:
method __load_image (line 29) | def __load_image(self, image_path : str) -> Tuple[List[str], torch.Ten...
method set_model_path (line 72) | def set_model_path(self, path : str) -> None:
method load_model (line 84) | def load_model(self) -> None:
method classify (line 99) | def classify(self, image_path : str, top_n : int = 5, verbose : bool =...
FILE: imageai/resnet50/__init__.py
class ResNet50Pretrained (line 11) | class ResNet50Pretrained:
method __init__ (line 16) | def __init__(self, label_path : str) -> None:
method __load_classes (line 23) | def __load_classes(self, path : str) -> List[str]:
method __load_image (line 28) | def __load_image(self, image_path : str) -> Tuple[List[str], torch.Ten...
method set_model_path (line 71) | def set_model_path(self, path : str) -> None:
method load_model (line 83) | def load_model(self) -> None:
method classify (line 98) | def classify(self, image_path : str, top_n : int = 5, verbose : bool =...
FILE: imageai/retinanet/utils.py
function read_file (line 11) | def read_file(path: str) -> torch.Tensor:
function decode_image (line 25) | def decode_image(input: torch.Tensor, mode: ImageReadMode = ImageReadMod...
function read_image (line 47) | def read_image(path: str, mode: ImageReadMode = ImageReadMode.UNCHANGED)...
function _generate_color_palette (line 67) | def _generate_color_palette(num_objects: int):
function make_grid (line 72) | def make_grid(
function draw_bounding_boxes_and_labels (line 180) | def draw_bounding_boxes_and_labels(
function tensor_to_ndarray (line 269) | def tensor_to_ndarray(
FILE: imageai/yolov3/tiny_yolov3.py
class YoloV3Tiny (line 10) | class YoloV3Tiny(nn.Module):
method __init__ (line 12) | def __init__(
method get_loss_layers (line 64) | def get_loss_layers(self) -> List[torch.Tensor]:
method __route_layer (line 67) | def __route_layer(self, y1 : torch.Tensor, y2 : Optional[torch.Tensor]...
method forward (line 72) | def forward(self, x : torch.Tensor) -> torch.Tensor:
FILE: imageai/yolov3/utils.py
function draw_bbox_and_label (line 10) | def draw_bbox_and_label(x : torch.Tensor, label : str, img : np.ndarray)...
function letterbox_image (line 23) | def letterbox_image(
function prepare_image (line 46) | def prepare_image(
function bbox_iou (line 57) | def bbox_iou(bbox1 : torch.Tensor, bbox2 : torch.Tensor, device="cpu"):
function transform_prediction (line 77) | def transform_prediction(
function get_predictions (line 160) | def get_predictions(
FILE: imageai/yolov3/yolov3.py
function noop (line 10) | def noop(x):
class DetectionLayer (line 13) | class DetectionLayer(nn.Module):
method __init__ (line 15) | def __init__(
method __get_anchors (line 41) | def __get_anchors(
method forward (line 48) | def forward(self, x : torch.Tensor):
class ConvLayer (line 75) | class ConvLayer(nn.Module):
method __init__ (line 77) | def __init__(self, in_f : int, out_f : int, kernel_size : int = 3,
method forward (line 89) | def forward(self, x : torch.Tensor):
class YoloV3 (line 92) | class YoloV3(nn.Module):
method __init__ (line 94) | def __init__(
method get_loss_layers (line 243) | def get_loss_layers(self) -> List[torch.Tensor]:
method __route_layer (line 246) | def __route_layer(self, y1 : torch.Tensor, y2 : Optional[torch.Tensor]...
method __shortcut_layer (line 251) | def __shortcut_layer(self,
method forward (line 258) | def forward(self, x : torch.Tensor) -> torch.Tensor:
FILE: imageai_tf_deprecated/Classification/Custom/__init__.py
class ClassificationModelTrainer (line 10) | class ClassificationModelTrainer:
method __init__ (line 17) | def __init__(self):
method setModelTypeAsSqueezeNet (line 31) | def setModelTypeAsSqueezeNet(self):
method setModelTypeAsMobileNetV2 (line 34) | def setModelTypeAsMobileNetV2(self):
method setModelTypeAsResNet (line 43) | def setModelTypeAsResNet(self):
method setModelTypeAsResNet50 (line 46) | def setModelTypeAsResNet50(self):
method setModelTypeAsDenseNet (line 56) | def setModelTypeAsDenseNet(self):
method setModelTypeAsDenseNet121 (line 59) | def setModelTypeAsDenseNet121(self):
method setModelTypeAsInceptionV3 (line 67) | def setModelTypeAsInceptionV3(self):
method setDataDirectory (line 75) | def setDataDirectory(self, data_directory="", train_subdirectory="trai...
method lr_schedule (line 119) | def lr_schedule(self, epoch):
method trainModel (line 147) | def trainModel(self, num_objects, num_experiments=200, enhance_data=Fa...
class CustomImageClassification (line 401) | class CustomImageClassification:
method __init__ (line 417) | def __init__(self):
method setModelPath (line 427) | def setModelPath(self, model_path):
method setJsonPath (line 437) | def setJsonPath(self, model_json):
method setModelTypeAsMobileNetV2 (line 446) | def setModelTypeAsMobileNetV2(self):
method setModelTypeAsResNet50 (line 454) | def setModelTypeAsResNet50(self):
method setModelTypeAsDenseNet121 (line 462) | def setModelTypeAsDenseNet121(self):
method setModelTypeAsInceptionV3 (line 470) | def setModelTypeAsInceptionV3(self):
method loadModel (line 478) | def loadModel(self, classification_speed="normal", num_objects=10):
method loadFullModel (line 541) | def loadFullModel(self, classification_speed="normal", num_objects=10):
method getModels (line 572) | def getModels(self):
method classifyImage (line 580) | def classifyImage(self, image_input, result_count=5, input_type="file"):
method predictImage (line 664) | def predictImage(self, image_input, result_count=5, input_type="file"):
FILE: imageai_tf_deprecated/Classification/__init__.py
class ImageClassification (line 7) | class ImageClassification:
method __init__ (line 22) | def __init__(self):
method setModelPath (line 29) | def setModelPath(self, model_path):
method setModelTypeAsSqueezeNet (line 39) | def setModelTypeAsSqueezeNet(self):
method setModelTypeAsMobileNetV2 (line 42) | def setModelTypeAsMobileNetV2(self):
method setModelTypeAsResNet (line 51) | def setModelTypeAsResNet(self):
method setModelTypeAsResNet50 (line 54) | def setModelTypeAsResNet50(self):
method setModelTypeAsDenseNet (line 63) | def setModelTypeAsDenseNet(self):
method setModelTypeAsDenseNet121 (line 66) | def setModelTypeAsDenseNet121(self):
method setModelTypeAsInceptionV3 (line 74) | def setModelTypeAsInceptionV3(self):
method loadModel (line 82) | def loadModel(self, classification_speed="normal"):
method classifyImage (line 146) | def classifyImage(self, image_input, result_count=5, input_type="file"):
method predictImage (line 231) | def predictImage(self, image_input, result_count=5, input_type="file"):
FILE: imageai_tf_deprecated/Detection/Custom/__init__.py
class DetectionModelTrainer (line 25) | class DetectionModelTrainer:
method __init__ (line 32) | def __init__(self):
method setModelTypeAsYOLOv3 (line 70) | def setModelTypeAsYOLOv3(self):
method setDataDirectory (line 78) | def setDataDirectory(self, data_directory):
method setGpuUsage (line 134) | def setGpuUsage(self, train_gpus):
method setTrainConfig (line 152) | def setTrainConfig(self, object_names_array, batch_size=4, num_experi...
method trainModel (line 198) | def trainModel(self):
method evaluateModel (line 320) | def evaluateModel(self, model_path, json_path, batch_size=4, iou_thres...
method _create_training_instances (line 456) | def _create_training_instances(self,
method _create_callbacks (line 511) | def _create_callbacks(self, saved_weights_name, model_to_save):
method _create_model (line 537) | def _create_model(
class CustomObjectDetection (line 607) | class CustomObjectDetection:
method __init__ (line 613) | def __init__(self):
method setModelTypeAsYOLOv3 (line 625) | def setModelTypeAsYOLOv3(self):
method setModelPath (line 632) | def setModelPath(self, detection_model_path):
method setJsonPath (line 641) | def setJsonPath(self, configuration_json):
method loadModel (line 649) | def loadModel(self):
method detectObjectsFromImage (line 668) | def detectObjectsFromImage(self, input_image="", output_image_path="",...
class CustomVideoObjectDetection (line 861) | class CustomVideoObjectDetection:
method __init__ (line 870) | def __init__(self):
method setModelTypeAsYOLOv3 (line 883) | def setModelTypeAsYOLOv3(self):
method setModelPath (line 893) | def setModelPath(self, detection_model_path):
method setJsonPath (line 903) | def setJsonPath(self, configuration_json):
method loadModel (line 912) | def loadModel(self):
method detectObjectsFromVideo (line 931) | def detectObjectsFromVideo(self, input_file_path="", camera_input=None...
class BoundBox (line 1211) | class BoundBox:
method __init__ (line 1212) | def __init__(self, xmin, ymin, xmax, ymax, objness=None, classes=None):
method get_label (line 1222) | def get_label(self):
method get_score (line 1228) | def get_score(self):
class CustomDetectionUtils (line 1235) | class CustomDetectionUtils:
method __init__ (line 1236) | def __init__(self, labels):
method _sigmoid (line 1247) | def _sigmoid(x):
method decode_netout (line 1250) | def decode_netout(self, netout, anchors, obj_thresh, net_h, net_w):
method correct_yolo_boxes (line 1284) | def correct_yolo_boxes(boxes, image_h, image_w, net_h, net_w):
method _interval_overlap (line 1294) | def _interval_overlap(self, interval_a, interval_b):
method bbox_iou (line 1308) | def bbox_iou(self, box1, box2):
method do_nms (line 1322) | def do_nms(self, boxes, nms_thresh):
method get_boxes (line 1342) | def get_boxes(self, boxes, labels, thresh):
method label_color (line 1356) | def label_color(self, label):
method draw_boxes_and_caption (line 1372) | def draw_boxes_and_caption(self, image_frame, v_boxes, v_labels, v_sco...
FILE: imageai_tf_deprecated/Detection/Custom/callbacks.py
class CustomTensorBoard (line 6) | class CustomTensorBoard(TensorBoard):
method __init__ (line 9) | def __init__(self, log_every=1, **kwargs):
method on_batch_end (line 14) | def on_batch_end(self, batch, logs=None):
class CustomModelCheckpoint (line 29) | class CustomModelCheckpoint(ModelCheckpoint):
method __init__ (line 32) | def __init__(self, model_to_save, **kwargs):
method on_epoch_end (line 36) | def on_epoch_end(self, epoch, logs=None):
FILE: imageai_tf_deprecated/Detection/Custom/evaluate.py
function _main_ (line 12) | def _main_(args):
FILE: imageai_tf_deprecated/Detection/Custom/gen_anchors.py
function IOU (line 7) | def IOU(ann, centroids):
function avg_IOU (line 27) | def avg_IOU(anns, centroids):
function run_kmeans (line 37) | def run_kmeans(ann_dims, anchor_num):
function generateAnchors (line 73) | def generateAnchors(train_annotation_folder, train_image_folder, train_c...
FILE: imageai_tf_deprecated/Detection/Custom/generator.py
class BatchGenerator (line 8) | class BatchGenerator(Sequence):
method __init__ (line 9) | def __init__(self,
method __len__ (line 38) | def __len__(self):
method __getitem__ (line 41) | def __getitem__(self, idx):
method _get_net_size (line 150) | def _get_net_size(self, idx):
method _aug_image (line 158) | def _aug_image(self, instance, net_h, net_w):
method on_epoch_end (line 201) | def on_epoch_end(self):
method num_classes (line 205) | def num_classes(self):
method size (line 208) | def size(self):
method get_anchors (line 211) | def get_anchors(self):
method load_annotation (line 219) | def load_annotation(self, i):
method load_image (line 231) | def load_image(self, i):
FILE: imageai_tf_deprecated/Detection/Custom/utils/bbox.py
class BoundBox (line 6) | class BoundBox:
method __init__ (line 7) | def __init__(self, xmin, ymin, xmax, ymax, c = None, classes = None):
method get_label (line 19) | def get_label(self):
method get_score (line 25) | def get_score(self):
function _interval_overlap (line 31) | def _interval_overlap(interval_a, interval_b):
function bbox_iou (line 46) | def bbox_iou(box1, box2):
function draw_boxes (line 62) | def draw_boxes(image, boxes, labels, obj_thresh, quiet=True):
FILE: imageai_tf_deprecated/Detection/Custom/utils/colors.py
function get_color (line 1) | def get_color(label):
FILE: imageai_tf_deprecated/Detection/Custom/utils/image.py
function _rand_scale (line 6) | def _rand_scale(scale):
function _constrain (line 11) | def _constrain(min_v, max_v, value):
function random_flip (line 22) | def random_flip(image, flip):
function correct_bounding_boxes (line 28) | def correct_bounding_boxes(boxes, new_w, new_h, net_w, net_h, dx, dy, fl...
function random_distort_image (line 58) | def random_distort_image(image, hue=18, saturation=1.5, exposure=1.5):
function apply_random_scale_and_crop (line 80) | def apply_random_scale_and_crop(image, new_w, new_h, net_w, net_h, dx, dy):
FILE: imageai_tf_deprecated/Detection/Custom/utils/multi_gpu_model.py
function multi_gpu_model (line 6) | def multi_gpu_model(model, gpus):
FILE: imageai_tf_deprecated/Detection/Custom/utils/utils.py
function _sigmoid (line 8) | def _sigmoid(x):
function makedirs (line 12) | def makedirs(path):
function evaluate (line 20) | def evaluate(model,
function correct_yolo_boxes (line 136) | def correct_yolo_boxes(boxes, image_h, image_w, net_h, net_w):
function do_nms (line 154) | def do_nms(boxes, nms_thresh):
function decode_netout (line 175) | def decode_netout(netout, anchors, obj_thresh, net_h, net_w):
function preprocess_input (line 217) | def preprocess_input(image, net_h, net_w):
function normalize (line 239) | def normalize(image):
function get_yolo_boxes (line 243) | def get_yolo_boxes(model, images, net_h, net_w, anchors, obj_thresh, nms...
function compute_overlap (line 276) | def compute_overlap(a, b):
function compute_ap (line 304) | def compute_ap(recall, precision):
function _softmax (line 332) | def _softmax(x, axis=-1):
FILE: imageai_tf_deprecated/Detection/Custom/voc.py
function parse_voc_annotation (line 6) | def parse_voc_annotation(ann_dir, img_dir, cache_name, labels=[]):
FILE: imageai_tf_deprecated/Detection/YOLO/utils.py
function yolo_head (line 8) | def yolo_head(feats, anchors, num_classes, input_shape, calc_loss=False):
function yolo_correct_boxes (line 36) | def yolo_correct_boxes(box_xy, box_wh, input_shape, image_shape):
function yolo_boxes_and_scores (line 62) | def yolo_boxes_and_scores(feats, anchors, num_classes, input_shape, imag...
function yolo_eval (line 73) | def yolo_eval(yolo_outputs,
function letterbox_image (line 118) | def letterbox_image(image, size):
function correct_yolo_boxes (line 133) | def correct_yolo_boxes(boxes, image_h, image_w, net_h, net_w):
class BoundBox (line 152) | class BoundBox:
method __init__ (line 153) | def __init__(self, xmin, ymin, xmax, ymax, objness = None, classes = N...
method get_label (line 165) | def get_label(self):
method get_score (line 171) | def get_score(self):
function _interval_overlap (line 178) | def _interval_overlap(interval_a, interval_b):
function _sigmoid (line 193) | def _sigmoid(x):
function bbox_iou (line 196) | def bbox_iou(box1, box2):
function do_nms (line 210) | def do_nms(boxes, nms_thresh):
function decode_netout (line 230) | def decode_netout(netout, anchors, obj_thresh, nms_thresh, net_h, net_w):
function preprocess_input (line 272) | def preprocess_input(image, input_shape):
function retrieve_yolo_detections (line 294) | def retrieve_yolo_detections(yolo_result, anchors, min_probability, nms_...
function draw_boxes (line 344) | def draw_boxes(image, box_points, draw_box, label, percentage_probabilit...
FILE: imageai_tf_deprecated/Detection/YOLO/yolov3.py
class YoloLayer (line 14) | class YoloLayer(Layer):
method __init__ (line 15) | def __init__(self, anchors, max_grid, batch_size, warmup_batches, igno...
method build (line 37) | def build(self, input_shape):
method call (line 40) | def call(self, x):
method compute_output_shape (line 189) | def compute_output_shape(self, input_shape):
function dummy_loss (line 194) | def dummy_loss(y_true, y_pred):
function NetworkConv2D_BN_Leaky (line 197) | def NetworkConv2D_BN_Leaky(input, channels, kernel_size, kernel_regulari...
function residual_block (line 204) | def residual_block(input, channels, num_blocks):
function darknet (line 215) | def darknet(input):
function last_layers (line 226) | def last_layers(input, channels_in, channels_out, layer_name=""):
function yolov3_base (line 241) | def yolov3_base(input, num_anchors, num_classes):
function yolov3_main (line 261) | def yolov3_main(input, num_anchors, num_classes):
function yolov3_train (line 268) | def yolov3_train(num_classes,
function tiny_yolov3_main (line 330) | def tiny_yolov3_main(input, num_anchors, num_classes):
function dummy_loss (line 360) | def dummy_loss(y_true, y_pred):
FILE: imageai_tf_deprecated/Detection/__init__.py
class ObjectDetection (line 21) | class ObjectDetection:
method __init__ (line 37) | def __init__(self):
method setModelTypeAsRetinaNet (line 85) | def setModelTypeAsRetinaNet(self):
method setModelTypeAsYOLOv3 (line 93) | def setModelTypeAsYOLOv3(self):
method setModelTypeAsTinyYOLOv3 (line 102) | def setModelTypeAsTinyYOLOv3(self):
method setModelPath (line 111) | def setModelPath(self, model_path):
method loadModel (line 123) | def loadModel(self, detection_speed="normal"):
method detectObjectsFromImage (line 200) | def detectObjectsFromImage(self, input_image="", output_image_path="",...
method CustomObjects (line 394) | def CustomObjects(self, person=False, bicycle=False, car=False, motorc...
method detectCustomObjectsFromImage (line 464) | def detectCustomObjectsFromImage(self, input_image="", output_image_pa...
class VideoObjectDetection (line 485) | class VideoObjectDetection:
method __init__ (line 502) | def __init__(self):
method setModelTypeAsRetinaNet (line 539) | def setModelTypeAsRetinaNet(self):
method setModelTypeAsYOLOv3 (line 547) | def setModelTypeAsYOLOv3(self):
method setModelTypeAsTinyYOLOv3 (line 555) | def setModelTypeAsTinyYOLOv3(self):
method setModelPath (line 563) | def setModelPath(self, model_path):
method loadModel (line 575) | def loadModel(self, detection_speed="normal"):
method detectObjectsFromVideo (line 605) | def detectObjectsFromVideo(self, input_file_path="", camera_input=None...
method CustomObjects (line 888) | def CustomObjects(self, person=False, bicycle=False, car=False, motorc...
method detectCustomObjectsFromVideo (line 958) | def detectCustomObjectsFromVideo(self, input_file_path="", camera_inpu...
FILE: imageai_tf_deprecated/Detection/keras_retinanet/backend/backend.py
function bbox_transform_inv (line 21) | def bbox_transform_inv(boxes, deltas, mean=None, std=None):
function shift (line 55) | def shift(shape, stride, anchors):
function map_fn (line 88) | def map_fn(*args, **kwargs):
function resize_images (line 107) | def resize_images(images, size, method='bilinear', align_corners=False):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/bin/convert_model.py
function parse_args (line 36) | def parse_args(args):
function main (line 53) | def main(args=None):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/bin/debug.py
function create_generator (line 52) | def create_generator(args):
function parse_args (line 140) | def parse_args(args):
function run (line 191) | def run(generator, args, anchor_params, pyramid_levels):
function make_output_path (line 267) | def make_output_path(output_dir, image_path, flatten = False):
function main (line 289) | def main(args=None):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/bin/evaluate.py
function create_generator (line 38) | def create_generator(args, preprocess_image):
function parse_args (line 81) | def parse_args(args):
function main (line 116) | def main(args=None):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/bin/train.py
function makedirs (line 53) | def makedirs(path):
function model_with_weights (line 64) | def model_with_weights(model, weights, skip_mismatch):
function create_models (line 77) | def create_models(backbone_retinanet, num_classes, weights, multi_gpu=0,
function create_callbacks (line 133) | def create_callbacks(model, training_model, prediction_model, validation...
function create_generators (line 221) | def create_generators(args, preprocess_image):
function check_args (line 359) | def check_args(parsed_args):
function parse_args (line 390) | def parse_args(args):
function main (line 462) | def main(args=None):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/callbacks/coco.py
class CocoEval (line 21) | class CocoEval(keras.callbacks.Callback):
method __init__ (line 24) | def __init__(self, generator, tensorboard=None, threshold=0.05):
method on_epoch_end (line 38) | def on_epoch_end(self, epoch, logs=None):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/callbacks/common.py
class RedirectModel (line 4) | class RedirectModel(keras.callbacks.Callback):
method __init__ (line 19) | def __init__(self,
method on_epoch_begin (line 27) | def on_epoch_begin(self, epoch, logs=None):
method on_epoch_end (line 30) | def on_epoch_end(self, epoch, logs=None):
method on_batch_begin (line 33) | def on_batch_begin(self, batch, logs=None):
method on_batch_end (line 36) | def on_batch_end(self, batch, logs=None):
method on_train_begin (line 39) | def on_train_begin(self, logs=None):
method on_train_end (line 45) | def on_train_end(self, logs=None):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/callbacks/eval.py
class Evaluate (line 21) | class Evaluate(keras.callbacks.Callback):
method __init__ (line 25) | def __init__(
method on_epoch_end (line 59) | def on_epoch_end(self, epoch, logs=None):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/initializers.py
class PriorProbability (line 22) | class PriorProbability(keras.initializers.Initializer):
method __init__ (line 26) | def __init__(self, probability=0.01):
method get_config (line 29) | def get_config(self):
method __call__ (line 34) | def __call__(self, shape, dtype=None):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/layers/_misc.py
class Anchors (line 25) | class Anchors(keras.layers.Layer):
method __init__ (line 29) | def __init__(self, size, stride, ratios=None, scales=None, *args, **kw...
method call (line 61) | def call(self, inputs, **kwargs):
method compute_output_shape (line 74) | def compute_output_shape(self, input_shape):
method get_config (line 85) | def get_config(self):
class UpsampleLike (line 97) | class UpsampleLike(keras.layers.Layer):
method call (line 101) | def call(self, inputs, **kwargs):
method compute_output_shape (line 112) | def compute_output_shape(self, input_shape):
class RegressBoxes (line 119) | class RegressBoxes(keras.layers.Layer):
method __init__ (line 123) | def __init__(self, mean=None, std=None, *args, **kwargs):
method call (line 149) | def call(self, inputs, **kwargs):
method compute_output_shape (line 153) | def compute_output_shape(self, input_shape):
method get_config (line 156) | def get_config(self):
class ClipBoxes (line 166) | class ClipBoxes(keras.layers.Layer):
method call (line 169) | def call(self, inputs, **kwargs):
method compute_output_shape (line 185) | def compute_output_shape(self, input_shape):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/layers/filter_detections.py
function filter_detections (line 22) | def filter_detections(
class FilterDetections (line 116) | class FilterDetections(keras.layers.Layer):
method __init__ (line 120) | def __init__(
method call (line 148) | def call(self, inputs, **kwargs):
method compute_output_shape (line 189) | def compute_output_shape(self, input_shape):
method compute_mask (line 207) | def compute_mask(self, inputs, mask=None):
method get_config (line 212) | def get_config(self):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/losses.py
function focal (line 21) | def focal(alpha=0.25, gamma=2.0, cutoff=0.5):
function smooth_l1 (line 71) | def smooth_l1(sigma=3.0):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/models/__init__.py
class Backbone (line 5) | class Backbone(object):
method __init__ (line 8) | def __init__(self, backbone):
method retinanet (line 27) | def retinanet(self, *args, **kwargs):
method download_imagenet (line 32) | def download_imagenet(self):
method validate (line 37) | def validate(self):
method preprocess_image (line 42) | def preprocess_image(self, inputs):
function backbone (line 49) | def backbone(backbone_name):
function load_model (line 70) | def load_model(filepath, backbone_name='resnet50'):
function convert_model (line 90) | def convert_model(model, nms=True, class_specific_filter=True, anchor_pa...
function assert_training_model (line 111) | def assert_training_model(model):
function check_training_model (line 118) | def check_training_model(model):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/models/densenet.py
class DenseNetBackbone (line 31) | class DenseNetBackbone(Backbone):
method retinanet (line 35) | def retinanet(self, *args, **kwargs):
method download_imagenet (line 40) | def download_imagenet(self):
method validate (line 57) | def validate(self):
method preprocess_image (line 65) | def preprocess_image(self, inputs):
function densenet_retinanet (line 71) | def densenet_retinanet(num_classes, backbone='densenet121', inputs=None,...
FILE: imageai_tf_deprecated/Detection/keras_retinanet/models/effnet.py
class EfficientNetBackbone (line 24) | class EfficientNetBackbone(Backbone):
method __init__ (line 28) | def __init__(self, backbone):
method retinanet (line 32) | def retinanet(self, *args, **kwargs):
method download_imagenet (line 37) | def download_imagenet(self):
method validate (line 49) | def validate(self):
method preprocess_image (line 59) | def preprocess_image(self, inputs):
function effnet_retinanet (line 65) | def effnet_retinanet(num_classes, backbone='EfficientNetB0', inputs=None...
function EfficientNetB0_retinanet (line 130) | def EfficientNetB0_retinanet(num_classes, inputs=None, **kwargs):
function EfficientNetB1_retinanet (line 134) | def EfficientNetB1_retinanet(num_classes, inputs=None, **kwargs):
function EfficientNetB2_retinanet (line 138) | def EfficientNetB2_retinanet(num_classes, inputs=None, **kwargs):
function EfficientNetB3_retinanet (line 142) | def EfficientNetB3_retinanet(num_classes, inputs=None, **kwargs):
function EfficientNetB4_retinanet (line 146) | def EfficientNetB4_retinanet(num_classes, inputs=None, **kwargs):
function EfficientNetB5_retinanet (line 150) | def EfficientNetB5_retinanet(num_classes, inputs=None, **kwargs):
function EfficientNetB6_retinanet (line 154) | def EfficientNetB6_retinanet(num_classes, inputs=None, **kwargs):
function EfficientNetB7_retinanet (line 158) | def EfficientNetB7_retinanet(num_classes, inputs=None, **kwargs):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/models/mobilenet.py
class MobileNetBackbone (line 24) | class MobileNetBackbone(Backbone):
method retinanet (line 30) | def retinanet(self, *args, **kwargs):
method download_imagenet (line 35) | def download_imagenet(self):
method validate (line 64) | def validate(self):
method preprocess_image (line 72) | def preprocess_image(self, inputs):
function mobilenet_retinanet (line 78) | def mobilenet_retinanet(num_classes, backbone='mobilenet224_1.0', inputs...
FILE: imageai_tf_deprecated/Detection/keras_retinanet/models/resnet.py
class ResNetBackbone (line 26) | class ResNetBackbone(Backbone):
method __init__ (line 30) | def __init__(self, backbone):
method retinanet (line 34) | def retinanet(self, *args, **kwargs):
method download_imagenet (line 39) | def download_imagenet(self):
method validate (line 62) | def validate(self):
method preprocess_image (line 71) | def preprocess_image(self, inputs):
function resnet_retinanet (line 77) | def resnet_retinanet(num_classes, backbone='resnet50', inputs=None, modi...
function resnet50_retinanet (line 122) | def resnet50_retinanet(num_classes, inputs=None, **kwargs):
function resnet101_retinanet (line 126) | def resnet101_retinanet(num_classes, inputs=None, **kwargs):
function resnet152_retinanet (line 130) | def resnet152_retinanet(num_classes, inputs=None, **kwargs):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/models/retinanet.py
function default_classification_model (line 24) | def default_classification_model(
function default_regression_model (line 82) | def default_regression_model(num_values, num_anchors, pyramid_feature_si...
function __create_pyramid_features (line 127) | def __create_pyramid_features(backbone_layers, pyramid_levels, feature_s...
function default_submodels (line 184) | def default_submodels(num_classes, num_anchors):
function __build_model_pyramid (line 202) | def __build_model_pyramid(name, model, features):
function __build_pyramid (line 216) | def __build_pyramid(models, features):
function __build_anchors (line 229) | def __build_anchors(anchor_parameters, features):
function retinanet (line 257) | def retinanet(
function retinanet_bbox (line 316) | def retinanet_bbox(
FILE: imageai_tf_deprecated/Detection/keras_retinanet/models/senet.py
class SeBackbone (line 24) | class SeBackbone(Backbone):
method __init__ (line 28) | def __init__(self, backbone):
method retinanet (line 32) | def retinanet(self, *args, **kwargs):
method download_imagenet (line 37) | def download_imagenet(self):
method validate (line 52) | def validate(self):
method preprocess_image (line 62) | def preprocess_image(self, inputs):
function senet_retinanet (line 68) | def senet_retinanet(num_classes, backbone='seresnext50', inputs=None, mo...
function seresnet18_retinanet (line 132) | def seresnet18_retinanet(num_classes, inputs=None, **kwargs):
function seresnet34_retinanet (line 136) | def seresnet34_retinanet(num_classes, inputs=None, **kwargs):
function seresnet50_retinanet (line 140) | def seresnet50_retinanet(num_classes, inputs=None, **kwargs):
function seresnet101_retinanet (line 144) | def seresnet101_retinanet(num_classes, inputs=None, **kwargs):
function seresnet152_retinanet (line 148) | def seresnet152_retinanet(num_classes, inputs=None, **kwargs):
function seresnext50_retinanet (line 152) | def seresnext50_retinanet(num_classes, inputs=None, **kwargs):
function seresnext101_retinanet (line 156) | def seresnext101_retinanet(num_classes, inputs=None, **kwargs):
function senet154_retinanet (line 160) | def senet154_retinanet(num_classes, inputs=None, **kwargs):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/models/vgg.py
class VGGBackbone (line 25) | class VGGBackbone(Backbone):
method retinanet (line 29) | def retinanet(self, *args, **kwargs):
method download_imagenet (line 34) | def download_imagenet(self):
method validate (line 54) | def validate(self):
method preprocess_image (line 62) | def preprocess_image(self, inputs):
function vgg_retinanet (line 68) | def vgg_retinanet(num_classes, backbone='vgg16', inputs=None, modifier=N...
FILE: imageai_tf_deprecated/Detection/keras_retinanet/preprocessing/coco.py
class CocoGenerator (line 26) | class CocoGenerator(Generator):
method __init__ (line 32) | def __init__(self, data_dir, set_name, **kwargs):
method load_classes (line 48) | def load_classes(self):
method size (line 68) | def size(self):
method num_classes (line 73) | def num_classes(self):
method has_label (line 78) | def has_label(self, label):
method has_name (line 83) | def has_name(self, name):
method name_to_label (line 88) | def name_to_label(self, name):
method label_to_name (line 93) | def label_to_name(self, label):
method coco_label_to_label (line 98) | def coco_label_to_label(self, coco_label):
method coco_label_to_name (line 104) | def coco_label_to_name(self, coco_label):
method label_to_coco_label (line 109) | def label_to_coco_label(self, label):
method image_path (line 114) | def image_path(self, image_index):
method image_aspect_ratio (line 121) | def image_aspect_ratio(self, image_index):
method load_image (line 127) | def load_image(self, image_index):
method load_annotations (line 133) | def load_annotations(self, image_index):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/preprocessing/csv_generator.py
function _parse (line 31) | def _parse(value, function, fmt):
function _read_classes (line 45) | def _read_classes(csv_reader):
function _read_annotations (line 64) | def _read_annotations(csv_reader, classes):
function _open_for_csv (line 102) | def _open_for_csv(path):
class CSVGenerator (line 114) | class CSVGenerator(Generator):
method __init__ (line 120) | def __init__(
method size (line 163) | def size(self):
method num_classes (line 168) | def num_classes(self):
method has_label (line 173) | def has_label(self, label):
method has_name (line 178) | def has_name(self, name):
method name_to_label (line 183) | def name_to_label(self, name):
method label_to_name (line 188) | def label_to_name(self, label):
method image_path (line 193) | def image_path(self, image_index):
method image_aspect_ratio (line 198) | def image_aspect_ratio(self, image_index):
method load_image (line 205) | def load_image(self, image_index):
method load_annotations (line 210) | def load_annotations(self, image_index):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/preprocessing/generator.py
class Generator (line 39) | class Generator(keras.utils.Sequence):
method __init__ (line 43) | def __init__(
method on_epoch_end (line 95) | def on_epoch_end(self):
method size (line 99) | def size(self):
method num_classes (line 104) | def num_classes(self):
method has_label (line 109) | def has_label(self, label):
method has_name (line 114) | def has_name(self, name):
method name_to_label (line 119) | def name_to_label(self, name):
method label_to_name (line 124) | def label_to_name(self, label):
method image_aspect_ratio (line 129) | def image_aspect_ratio(self, image_index):
method image_path (line 134) | def image_path(self, image_index):
method load_image (line 139) | def load_image(self, image_index):
method load_annotations (line 144) | def load_annotations(self, image_index):
method load_annotations_group (line 149) | def load_annotations_group(self, group):
method filter_annotations (line 160) | def filter_annotations(self, image_group, annotations_group, group):
method load_image_group (line 187) | def load_image_group(self, group):
method random_visual_effect_group_entry (line 192) | def random_visual_effect_group_entry(self, image, annotations):
method random_visual_effect_group (line 200) | def random_visual_effect_group(self, image_group, annotations_group):
method random_transform_group_entry (line 217) | def random_transform_group_entry(self, image, annotations, transform=N...
method random_transform_group (line 235) | def random_transform_group(self, image_group, annotations_group):
method resize_image (line 247) | def resize_image(self, image):
method preprocess_group_entry (line 255) | def preprocess_group_entry(self, image, annotations):
method preprocess_group (line 272) | def preprocess_group(self, image_group, annotations_group):
method group_images (line 283) | def group_images(self):
method compute_inputs (line 296) | def compute_inputs(self, image_group):
method generate_anchors (line 314) | def generate_anchors(self, image_shape):
method compute_targets (line 324) | def compute_targets(self, image_group, annotations_group):
method compute_input_output (line 340) | def compute_input_output(self, group):
method __len__ (line 367) | def __len__(self):
method __getitem__ (line 374) | def __getitem__(self, index):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/preprocessing/kitti.py
class KittiGenerator (line 39) | class KittiGenerator(Generator):
method __init__ (line 45) | def __init__(
method size (line 108) | def size(self):
method num_classes (line 113) | def num_classes(self):
method has_label (line 118) | def has_label(self, label):
method has_name (line 123) | def has_name(self, name):
method name_to_label (line 128) | def name_to_label(self, name):
method label_to_name (line 133) | def label_to_name(self, label):
method image_aspect_ratio (line 138) | def image_aspect_ratio(self, image_index):
method image_path (line 145) | def image_path(self, image_index):
method load_image (line 150) | def load_image(self, image_index):
method load_annotations (line 155) | def load_annotations(self, image_index):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/preprocessing/open_images.py
function load_hierarchy (line 29) | def load_hierarchy(metadata_dir, version='v4'):
function load_hierarchy_children (line 45) | def load_hierarchy_children(hierarchy):
function find_hierarchy_parent (line 58) | def find_hierarchy_parent(hierarchy, parent_cls):
function get_labels (line 70) | def get_labels(metadata_dir, version='v4'):
function generate_images_annotations_json (line 110) | def generate_images_annotations_json(main_dir, metadata_dir, subset, cls...
class OpenImagesGenerator (line 213) | class OpenImagesGenerator(Generator):
method __init__ (line 214) | def __init__(
method __filter_data (line 256) | def __filter_data(self, id_to_labels, cls_index, labels_filter=None, p...
method size (line 321) | def size(self):
method num_classes (line 324) | def num_classes(self):
method has_label (line 327) | def has_label(self, label):
method has_name (line 332) | def has_name(self, name):
method name_to_label (line 337) | def name_to_label(self, name):
method label_to_name (line 340) | def label_to_name(self, label):
method image_aspect_ratio (line 343) | def image_aspect_ratio(self, image_index):
method image_path (line 348) | def image_path(self, image_index):
method load_image (line 352) | def load_image(self, image_index):
method load_annotations (line 355) | def load_annotations(self, image_index):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/preprocessing/pascal_voc.py
function _findNode (line 54) | def _findNode(parent, name, debug_name=None, parse=None):
class PascalVocGenerator (line 69) | class PascalVocGenerator(Generator):
method __init__ (line 75) | def __init__(
method size (line 105) | def size(self):
method num_classes (line 110) | def num_classes(self):
method has_label (line 115) | def has_label(self, label):
method has_name (line 120) | def has_name(self, name):
method name_to_label (line 125) | def name_to_label(self, name):
method label_to_name (line 130) | def label_to_name(self, label):
method image_aspect_ratio (line 135) | def image_aspect_ratio(self, image_index):
method image_path (line 142) | def image_path(self, image_index):
method load_image (line 147) | def load_image(self, image_index):
method __parse_annotation (line 152) | def __parse_annotation(self, element):
method __parse_annotations (line 173) | def __parse_annotations(self, xml_root):
method load_annotations (line 193) | def load_annotations(self, image_index):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/utils/anchors.py
class AnchorParameters (line 23) | class AnchorParameters:
method __init__ (line 32) | def __init__(self, sizes, strides, ratios, scales):
method num_anchors (line 38) | def num_anchors(self):
function anchor_targets_bbox (line 53) | def anchor_targets_bbox(
function layer_shapes (line 119) | def layer_shapes(image_shape, model):
function make_shapes_callback (line 147) | def make_shapes_callback(model):
function guess_shapes (line 158) | def guess_shapes(image_shape, pyramid_levels):
function anchors_for_shape (line 173) | def anchors_for_shape(
function shift (line 215) | def shift(shape, stride, anchors):
function generate_anchors (line 247) | def generate_anchors(base_size=16, ratios=None, scales=None):
function bbox_transform (line 281) | def bbox_transform(anchors, gt_boxes, mean=None, std=None):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/utils/coco_eval.py
function evaluate_coco (line 27) | def evaluate_coco(generator, model, threshold=0.05):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/utils/colors.py
function label_color (line 4) | def label_color(label):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/utils/config.py
function read_config_file (line 23) | def read_config_file(config_path):
function parse_anchor_parameters (line 44) | def parse_anchor_parameters(config):
function parse_pyramid_levels (line 54) | def parse_pyramid_levels(config):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/utils/eval.py
function _compute_ap (line 30) | def _compute_ap(recall, precision):
function _get_detections (line 59) | def _get_detections(generator, model, score_threshold=0.05, max_detectio...
function _get_annotations (line 126) | def _get_annotations(generator):
function evaluate (line 153) | def evaluate(
FILE: imageai_tf_deprecated/Detection/keras_retinanet/utils/gpu.py
function setup_gpu (line 20) | def setup_gpu(gpu_id):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/utils/image.py
function read_image_bgr (line 25) | def read_image_bgr(path):
function preprocess_image (line 36) | def preprocess_image(x, mode='caffe'):
function adjust_transform_for_image (line 64) | def adjust_transform_for_image(transform, image, relative_translation):
class TransformParameters (line 84) | class TransformParameters:
method __init__ (line 94) | def __init__(
method cvBorderMode (line 106) | def cvBorderMode(self):
method cvInterpolation (line 116) | def cvInterpolation(self):
function apply_transform (line 129) | def apply_transform(matrix, image, params):
function compute_resize_scale (line 154) | def compute_resize_scale(image_shape, min_side=800, max_side=1333):
function resize_image (line 180) | def resize_image(img, min_side=800, max_side=1333):
function _uniform (line 199) | def _uniform(val_range):
function _check_range (line 208) | def _check_range(val_range, min_val=None, max_val=None):
function _clip (line 224) | def _clip(image):
class VisualEffect (line 234) | class VisualEffect:
method __init__ (line 244) | def __init__(
method __call__ (line 256) | def __call__(self, image):
function random_visual_effect_generator (line 282) | def random_visual_effect_generator(
function adjust_contrast (line 315) | def adjust_contrast(image, factor):
function adjust_brightness (line 326) | def adjust_brightness(image, delta):
function adjust_hue (line 336) | def adjust_hue(image, delta):
function adjust_saturation (line 348) | def adjust_saturation(image, factor):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/utils/model.py
function freeze (line 18) | def freeze(model):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/utils/tf_version.py
function tf_version (line 26) | def tf_version():
function tf_version_ok (line 34) | def tf_version_ok(minimum_tf_version=MINIMUM_TF_VERSION, blacklisted=BLA...
function assert_tf_version (line 40) | def assert_tf_version(minimum_tf_version=MINIMUM_TF_VERSION, blacklisted...
function check_tf_version (line 48) | def check_tf_version():
FILE: imageai_tf_deprecated/Detection/keras_retinanet/utils/transform.py
function colvec (line 22) | def colvec(*args):
function transform_aabb (line 27) | def transform_aabb(transform, aabb):
function _random_vector (line 57) | def _random_vector(min, max, prng=DEFAULT_PRNG):
function rotation (line 70) | def rotation(angle):
function random_rotation (line 84) | def random_rotation(min, max, prng=DEFAULT_PRNG):
function translation (line 96) | def translation(translation):
function random_translation (line 110) | def random_translation(min, max, prng=DEFAULT_PRNG):
function shear (line 122) | def shear(angle):
function random_shear (line 136) | def random_shear(min, max, prng=DEFAULT_PRNG):
function scaling (line 148) | def scaling(factor):
function random_scaling (line 162) | def random_scaling(min, max, prng=DEFAULT_PRNG):
function random_flip (line 174) | def random_flip(flip_x_chance, flip_y_chance, prng=DEFAULT_PRNG):
function change_transform_origin (line 189) | def change_transform_origin(transform, center):
function random_transform (line 202) | def random_transform(
function random_transform_generator (line 252) | def random_transform_generator(prng=None, **kwargs):
FILE: imageai_tf_deprecated/Detection/keras_retinanet/utils/visualization.py
function draw_box (line 23) | def draw_box(image, box, color, thickness=2):
function draw_caption (line 36) | def draw_caption(image, box, caption):
function draw_boxes (line 49) | def draw_boxes(image, boxes, color, thickness=2):
function draw_detections (line 62) | def draw_detections(image, boxes, scores, labels, color=None, label_to_n...
function draw_annotations (line 85) | def draw_annotations(image, annotations, color=(0, 255, 0), label_to_nam...
FILE: imageai_tf_deprecated/Prediction/Custom/__init__.py
class ModelTraining (line 7) | class ModelTraining(ClassificationModelTrainer):
method __call__ (line 12) | def __call__(self):
class CustomImagePrediction (line 15) | class CustomImagePrediction(CustomImageClassification):
method __call__ (line 21) | def __call__(self):
FILE: imageai_tf_deprecated/Prediction/Custom/custom_utils.py
function preprocess_input (line 7) | def preprocess_input(x):
function decode_predictions (line 25) | def decode_predictions(preds, top=5, model_json=""):
FILE: imageai_tf_deprecated/Prediction/__init__.py
class ImagePrediction (line 5) | class ImagePrediction(ImageClassification):
method __call__ (line 11) | def __call__(self):
FILE: imageai_tf_deprecated/Prediction/imagenet_utils.py
function preprocess_input (line 5) | def preprocess_input(x):
function decode_predictions (line 27) | def decode_predictions(preds, top=5):
FILE: scripts/pascal_voc_to_yolo.py
function convert (line 15) | def convert(size, box):
function convert_annotation (line 28) | def convert_annotation(input_ann_path):
function main (line 56) | def main(dataset_dir: str):
FILE: test/test_custom_classification.py
function test_recognition_model_mobilenetv2 (line 21) | def test_recognition_model_mobilenetv2(image_input):
function test_recognition_model_resnet (line 44) | def test_recognition_model_resnet(image_input):
function test_recognition_model_inceptionv3 (line 66) | def test_recognition_model_inceptionv3(image_input):
function test_recognition_model_densenet (line 88) | def test_recognition_model_densenet(image_input):
FILE: test/test_custom_classification_training.py
function test_resnet50_training (line 35) | def test_resnet50_training(transfer_learning):
function test_densenet121_training (line 78) | def test_densenet121_training(transfer_learning):
function test_inceptionv3_training (line 121) | def test_inceptionv3_training(transfer_learning):
function test_mobilenetv2_training (line 163) | def test_mobilenetv2_training(transfer_learning):
FILE: test/test_custom_detection_training.py
function delete_cache (line 22) | def delete_cache(dirs: list):
function test_yolov3_training (line 37) | def test_yolov3_training(transfer_learning):
function test_tiny_yolov3_training (line 66) | def test_tiny_yolov3_training(transfer_learning):
FILE: test/test_custom_object_detection.py
function delete_cache (line 17) | def delete_cache(paths: List[str]):
function test_object_detection_yolov3 (line 35) | def test_object_detection_yolov3(input_image, output_type, extract_objec...
function test_object_detection_tiny_yolov3 (line 103) | def test_object_detection_tiny_yolov3(input_image, output_type, extract_...
FILE: test/test_custom_video_detection.py
class CallbackFunctions (line 18) | class CallbackFunctions:
method forFrame (line 19) | def forFrame(frame_number, output_array, output_count, detected_frame):
method forSecond (line 33) | def forSecond(second_number, output_arrays, count_arrays, average_outp...
function delete_cache (line 57) | def delete_cache(files: List[str]):
function test_video_detection_yolov3 (line 65) | def test_video_detection_yolov3():
function test_video_detection_tiny_yolov3 (line 81) | def test_video_detection_tiny_yolov3():
function test_video_detection_yolo_analysis (line 97) | def test_video_detection_yolo_analysis():
FILE: test/test_image_classification.py
function test_recognition_model_mobilenetv2 (line 21) | def test_recognition_model_mobilenetv2(image_input):
function test_recognition_model_resnet (line 43) | def test_recognition_model_resnet(image_input):
function test_recognition_model_inceptionv3 (line 64) | def test_recognition_model_inceptionv3(image_input):
function test_recognition_model_densenet (line 85) | def test_recognition_model_densenet(image_input):
FILE: test/test_object_detection.py
function delete_cache (line 16) | def delete_cache(paths: List[str]):
function test_object_detection_retinanet (line 35) | def test_object_detection_retinanet(input_image, output_type, extract_ob...
function test_object_detection_yolov3 (line 102) | def test_object_detection_yolov3(input_image, output_type, extract_objec...
function test_object_detection_tiny_yolov3 (line 170) | def test_object_detection_tiny_yolov3(input_image, output_type, extract_...
function test_object_detection_retinanet_custom_objects (line 237) | def test_object_detection_retinanet_custom_objects(input_image):
function test_object_detection_yolov3_custom_objects (line 263) | def test_object_detection_yolov3_custom_objects(input_image):
function test_object_detection_tiny_yolov3_custom_objects (line 289) | def test_object_detection_tiny_yolov3_custom_objects(input_image):
FILE: test/test_video_object_detection.py
class CallbackFunctions (line 18) | class CallbackFunctions:
method forFrame (line 19) | def forFrame(frame_number, output_array, output_count, detected_frame):
method forSecond (line 33) | def forSecond(second_number, output_arrays, count_arrays, average_outp...
function delete_cache (line 57) | def delete_cache(files: List[str]):
function test_video_detection_retinanet (line 63) | def test_video_detection_retinanet():
function test_video_detection_retinanet_custom_objects (line 79) | def test_video_detection_retinanet_custom_objects():
function test_video_detection_yolov3 (line 103) | def test_video_detection_yolov3():
function test_video_detection_tiny_yolov3 (line 119) | def test_video_detection_tiny_yolov3():
function test_video_detection_retinanet_analysis (line 134) | def test_video_detection_retinanet_analysis():
Condensed preview — 152 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (1,167K chars).
[
{
"path": ".codecov.yml",
"chars": 355,
"preview": "codecov:\n notify:\n require_ci_to_pass: yes\n\ncoverage:\n precision: 2\n round: down\n range: \"30...100\"\n\n status:\n "
},
{
"path": ".github/FUNDING.yml",
"chars": 21,
"preview": "github: OlafenwaMoses"
},
{
"path": ".github/workflows/build.yml",
"chars": 14655,
"preview": "name: Build and Testing\n\non:\n push:\n branches: [master]\n pull_request:\n branches: [master]\n\njobs:\n UnitestPytho"
},
{
"path": ".gitignore",
"chars": 3201,
"preview": "# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py.class\n\n# C extensions\n*.so\n\n# Distribution / packagi"
},
{
"path": ".travis.yml",
"chars": 1788,
"preview": "dist: xenial\nsudo: required\nlanguage: python\npython:\n - '3.7.6'\ninstall:\n - pip install -r requirements.txt\n - pip in"
},
{
"path": "BACKEND_MIGRATION.md",
"chars": 2490,
"preview": "# Overview\n\nIn December 2022, ImageAI `3.0.2` was released which effected the change from Tensorflow backend to PyTorch "
},
{
"path": "LICENSE",
"chars": 1071,
"preview": "MIT License\n\nCopyright (c) 2019 MOSES OLAFENWA\n\nPermission is hereby granted, free of charge, to any person obtaining a "
},
{
"path": "MANIFEST.in",
"chars": 88,
"preview": "recursive-include imageai/Detection *.txt\nrecursive-include imageai/Classification *.txt"
},
{
"path": "README.md",
"chars": 16877,
"preview": "# ImageAI (v3.0.3)\n\n\n\n[](https://travis-ci"
},
{
"path": "examples/camera_feed_detection.py",
"chars": 676,
"preview": "from imageai.Detection import VideoObjectDetection\nimport os\nimport cv2\n\nexecution_path = os.getcwd()\n\ncamera = cv2.Vide"
},
{
"path": "examples/custom_detection.py",
"chars": 1070,
"preview": "from imageai.Detection.Custom import CustomObjectDetection\n\ndetector = CustomObjectDetection()\ndetector.setModelTypeAsYO"
},
{
"path": "examples/custom_detection_array_input_output.py",
"chars": 1220,
"preview": "from imageai.Detection.Custom import CustomObjectDetection\nimport cv2\n\nimage_array = cv2.imread(\"holo2.jpg\")\n\ndetector ="
},
{
"path": "examples/custom_detection_extract_objects.py",
"chars": 1493,
"preview": "from imageai.Detection.Custom import CustomObjectDetection\n\ndetector = CustomObjectDetection()\ndetector.setModelTypeAsYO"
},
{
"path": "examples/custom_detection_from_array_extract_objects_array.py",
"chars": 1372,
"preview": "from imageai.Detection.Custom import CustomObjectDetection\nimport cv2\n\nimage_array = cv2.imread(\"holo2.jpg\")\n\ndetector ="
},
{
"path": "examples/custom_detection_from_file_extract_objects_array.py",
"chars": 1315,
"preview": "from imageai.Detection.Custom import CustomObjectDetection\nimport cv2\n\n\ndetector = CustomObjectDetection()\ndetector.setM"
},
{
"path": "examples/custom_detection_train.py",
"chars": 1391,
"preview": "from imageai.Detection.Custom import DetectionModelTrainer\n\ntrainer = DetectionModelTrainer()\ntrainer.setModelTypeAsYOLO"
},
{
"path": "examples/custom_detection_video.py",
"chars": 986,
"preview": "from imageai.Detection.Custom import CustomVideoObjectDetection\nimport os\n\nexecution_path = os.getcwd()\n\nvideo_detector "
},
{
"path": "examples/custom_model_prediction.py",
"chars": 928,
"preview": "from imageai.Classification.Custom import CustomImageClassification\nimport os\n\nexecution_path = os.getcwd()\n\nprediction "
},
{
"path": "examples/custom_model_training.py",
"chars": 259,
"preview": "from imageai.Classification.Custom import ClassificationModelTrainer\n\n\nmodel_trainer = ClassificationModelTrainer()\nmode"
},
{
"path": "examples/image_custom_object_detection.py",
"chars": 850,
"preview": "from imageai.Detection import ObjectDetection\nimport os\nfrom time import time\n\nexecution_path = os.getcwd()\n\ndetector = "
},
{
"path": "examples/image_prediction.py",
"chars": 636,
"preview": "from imageai.Classification import ImageClassification\nimport os\n\nexecution_path = os.getcwd()\n\nprediction = ImageClassi"
},
{
"path": "examples/object_detection.py",
"chars": 807,
"preview": "from imageai.Detection import ObjectDetection\nimport os\n\nexecution_path = os.getcwd()\n\ndetector = ObjectDetection()\ndete"
},
{
"path": "examples/video_analysis_per_frame.py",
"chars": 3395,
"preview": "from imageai.Detection import VideoObjectDetection\nimport os\nfrom matplotlib import pyplot as plt\n\n\nexecution_path = os."
},
{
"path": "examples/video_analysis_per_second.py",
"chars": 3438,
"preview": "from imageai.Detection import VideoObjectDetection\nimport os\nfrom matplotlib import pyplot as plt\n\n\nexecution_path = os."
},
{
"path": "examples/video_custom_object_detection.py",
"chars": 738,
"preview": "from imageai.Detection import VideoObjectDetection\nimport os\n\nexecution_path = os.getcwd()\n\ndetector = VideoObjectDetect"
},
{
"path": "examples/video_object_detection.py",
"chars": 629,
"preview": "from imageai.Detection import VideoObjectDetection\nimport os\n\nexecution_path = os.getcwd()\n\ndetector = VideoObjectDetect"
},
{
"path": "imageai/Classification/CUSTOMCLASSIFICATION.md",
"chars": 5576,
"preview": "# ImageAI : Custom Image Classification\n\nImageAI provides 4 different algorithms and model types to perform custom image"
},
{
"path": "imageai/Classification/CUSTOMTRAINING.md",
"chars": 8328,
"preview": "# ImageAI : Custom Prediction Model Training \n\n## ---------------------------------------------------\n## Introducing Jar"
},
{
"path": "imageai/Classification/Custom/__init__.py",
"chars": 23552,
"preview": "import time, warnings\nimport os\nimport copy\nimport re\nimport json\nfrom typing import List, Tuple, Union\nfrom PIL import "
},
{
"path": "imageai/Classification/Custom/data_transformation.py",
"chars": 1817,
"preview": "from torchvision import transforms\n\ndata_transforms1 = {\n \"train\":transforms.Compose([\n "
},
{
"path": "imageai/Classification/Custom/training_params.py",
"chars": 1292,
"preview": "import torch\nfrom torch.optim import SGD\nfrom torchvision.models import resnet50, inception_v3, mobilenet_v2, densenet12"
},
{
"path": "imageai/Classification/README.md",
"chars": 6513,
"preview": "# ImageAI : Image Classification\n\n\n## ---------------------------------------------------\n## Introducing Jarvis and Thei"
},
{
"path": "imageai/Classification/__init__.py",
"chars": 10032,
"preview": "import os, re\nfrom typing import Union\nfrom typing import List, Tuple\nimport numpy as np\nimport torch\nfrom torchvision.m"
},
{
"path": "imageai/Classification/imagenet_classes.txt",
"chars": 10472,
"preview": "tench\ngoldfish\ngreat white shark\ntiger shark\nhammerhead\nelectric ray\nstingray\ncock\nhen\nostrich\nbrambling\ngoldfinch\nhouse"
},
{
"path": "imageai/Detection/Custom/CUSTOMDETECTION.md",
"chars": 10677,
"preview": "# ImageAI : Custom Object Detection\n\n### TABLE OF CONTENTS\n\n- <a href=\"#customdetection\" > :white_square_button: Custom "
},
{
"path": "imageai/Detection/Custom/CUSTOMDETECTIONTRAINING.md",
"chars": 11988,
"preview": "# ImageAI : Custom Detection Model Training \n\n## ---------------------------------------------------\n## Introducing Jarv"
},
{
"path": "imageai/Detection/Custom/CUSTOMVIDEODETECTION.md",
"chars": 15236,
"preview": "# ImageAI : Custom Video Object Detection, Tracking and Analysis\n\n### TABLE OF CONTENTS\n\n- <a href=\"#videodetection\" > "
},
{
"path": "imageai/Detection/Custom/__init__.py",
"chars": 49115,
"preview": "import os\nimport time\nimport math\nimport json\nimport warnings\nfrom typing import List, Union, Tuple, Dict\nfrom collectio"
},
{
"path": "imageai/Detection/Custom/yolo/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "imageai/Detection/Custom/yolo/compute_loss.py",
"chars": 9937,
"preview": "import math\n\nimport torch\nimport torch.nn as nn\n\n# This new loss function is based on https://github.com/ultralytics/yol"
},
{
"path": "imageai/Detection/Custom/yolo/custom_anchors.py",
"chars": 3257,
"preview": "import random\n\nimport torch\nimport numpy as np\nfrom scipy.cluster.vq import kmeans\n\n# This new anchor generator function"
},
{
"path": "imageai/Detection/Custom/yolo/dataset.py",
"chars": 5137,
"preview": "import os\nimport warnings\nfrom typing import Tuple, List\n\nimport cv2 as cv\nimport numpy as np\nimport torch\nfrom torch.ut"
},
{
"path": "imageai/Detection/Custom/yolo/metric.py",
"chars": 2939,
"preview": "import math\nimport warnings\n\nimport numpy as np\nimport torch\n\n# This new metric functions is based on https://github.com"
},
{
"path": "imageai/Detection/Custom/yolo/validate.py",
"chars": 4582,
"preview": "import os\n\nimport numpy as np\nimport torch\nfrom torchvision.ops import box_iou\n\nfrom ....yolov3.utils import get_predict"
},
{
"path": "imageai/Detection/README.md",
"chars": 18050,
"preview": "# ImageAI : Object Detection\n\n## ---------------------------------------------------\n## Introducing Jarvis and TheiaEngi"
},
{
"path": "imageai/Detection/VIDEO.md",
"chars": 29788,
"preview": "# ImageAI : Video Object Detection, Tracking and Analysis\n\n## ---------------------------------------------------\n## In"
},
{
"path": "imageai/Detection/__init__.py",
"chars": 44038,
"preview": "import os, warnings\nfrom tkinter import Image\nfrom collections import defaultdict\nfrom typing import List, Tuple, Dict, "
},
{
"path": "imageai/Detection/coco91_classes.txt",
"chars": 712,
"preview": "unlabeled\nperson\nbicycle\ncar\nmotorcycle\nairplane\nbus\ntrain\ntruck\nboat\ntraffic light\nfire hydrant\nstreet sign\nstop sign\np"
},
{
"path": "imageai/Detection/coco_classes.txt",
"chars": 624,
"preview": "person\nbicycle\ncar\nmotorbike\naeroplane\nbus\ntrain\ntruck\nboat\ntraffic light\nfire hydrant\nstop sign\nparking meter\nbench\nbir"
},
{
"path": "imageai/__init__.py",
"chars": 40,
"preview": "from .backend_check import backend_check"
},
{
"path": "imageai/backend_check/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "imageai/backend_check/backend_check.py",
"chars": 682,
"preview": "try:\n import torch\n import torchvision\nexcept:\n try:\n import tensorflow\n import keras\n\n ra"
},
{
"path": "imageai/backend_check/model_extension.py",
"chars": 638,
"preview": "import os\n\ndef extension_check(file_path: str):\n if file_path.endswith(\".h5\"):\n raise RuntimeError(\"You are tr"
},
{
"path": "imageai/densenet121/__init__.py",
"chars": 6480,
"preview": "import os, warnings\nfrom pathlib import Path\nfrom typing import List, Tuple\n\nimport torch, torchvision\nimport torch.nn.f"
},
{
"path": "imageai/inceptionv3/__init__.py",
"chars": 6334,
"preview": "import os, warnings\nfrom pathlib import Path\nfrom typing import List, Tuple\n\nimport torch, torchvision\nimport torch.nn.f"
},
{
"path": "imageai/mobilenetv2/__init__.py",
"chars": 6147,
"preview": "import os, warnings\nfrom pathlib import Path\nfrom typing import List, Tuple\n\nimport torch, torchvision\nimport torch.nn.f"
},
{
"path": "imageai/resnet50/__init__.py",
"chars": 6126,
"preview": "import os, warnings\nfrom typing import List, Tuple\n\nimport torch, torchvision\nimport torch.nn.functional as F\nfrom torch"
},
{
"path": "imageai/retinanet/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "imageai/retinanet/utils.py",
"chars": 11668,
"preview": "\nfrom torchvision.io import ImageReadMode\nimport torch\nfrom PIL import Image, ImageColor, ImageDraw, ImageFont\nfrom typi"
},
{
"path": "imageai/yolov3/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "imageai/yolov3/tiny_yolov3.py",
"chars": 3189,
"preview": "from typing import Union, List, Tuple, Optional\n\nimport torch\nimport torch.nn as nn\nimport numpy as np\n\nfrom .yolov3 imp"
},
{
"path": "imageai/yolov3/utils.py",
"chars": 8665,
"preview": "import math\nfrom typing import Union, List, Tuple\n\nimport torch\nimport numpy as np\nimport cv2 as cv\nfrom torchvision.ops"
},
{
"path": "imageai/yolov3/yolov3.py",
"chars": 13776,
"preview": "from typing import Union, List, Tuple, Optional\n\nimport torch\nimport torch.nn as nn\nimport numpy as np\n\nfrom .utils impo"
},
{
"path": "imageai_tf_deprecated/Classification/CUSTOMCLASSIFICATION.md",
"chars": 7597,
"preview": "# ImageAI : Custom Image Classification\nA **DeepQuest AI** project <a href=\"https://deepquestai.com\" >https://deepquesta"
},
{
"path": "imageai_tf_deprecated/Classification/CUSTOMTRAINING.md",
"chars": 15531,
"preview": "# ImageAI : Custom Prediction Model Training \n\n---\n\n**ImageAI** provides the most simple and powerful approach to traini"
},
{
"path": "imageai_tf_deprecated/Classification/Custom/__init__.py",
"chars": 32990,
"preview": "import tensorflow as tf\nfrom PIL import Image\nimport time\nimport numpy as np\nimport os\nimport warnings\nfrom matplotlib.c"
},
{
"path": "imageai_tf_deprecated/Classification/README.md",
"chars": 11405,
"preview": "# ImageAI : Image Prediction\nA **DeepQuest AI** project [https://deepquestai.com](https://deepquestai.com)\n\n---\n\n### TAB"
},
{
"path": "imageai_tf_deprecated/Classification/__init__.py",
"chars": 12074,
"preview": "import tensorflow as tf\nfrom PIL import Image\nimport numpy as np\nfrom matplotlib.cbook import deprecated\n\n\nclass ImageCl"
},
{
"path": "imageai_tf_deprecated/Detection/Custom/CUSTOMDETECTION.md",
"chars": 10815,
"preview": "# ImageAI : Custom Object Detection\n\nAn **DeepQuest AI** project [https://deepquestai.com](https://deepquestai.com)\n\n---"
},
{
"path": "imageai_tf_deprecated/Detection/Custom/CUSTOMDETECTIONTRAINING.md",
"chars": 19272,
"preview": "# ImageAI : Custom Detection Model Training \n\n---\n\n**ImageAI** provides the most simple and powerful approach to trainin"
},
{
"path": "imageai_tf_deprecated/Detection/Custom/CUSTOMVIDEODETECTION.md",
"chars": 15304,
"preview": "# ImageAI : Custom Video Object Detection, Tracking and Analysis\n\nAn **DeepQuest AI** project [https://deepquestai.com]"
},
{
"path": "imageai_tf_deprecated/Detection/Custom/__init__.py",
"chars": 66083,
"preview": "import os\nimport re\nimport numpy as np\nimport json\nfrom imageai.Detection.Custom.voc import parse_voc_annotation\nfrom im"
},
{
"path": "imageai_tf_deprecated/Detection/Custom/callbacks.py",
"chars": 3147,
"preview": "from tensorflow.keras.callbacks import TensorBoard, ModelCheckpoint\nimport tensorflow as tf\nimport numpy as np\nimport wa"
},
{
"path": "imageai_tf_deprecated/Detection/Custom/evaluate.py",
"chars": 2421,
"preview": "#! /usr/bin/env python\n\nimport argparse\nimport os\nimport json\nfrom imageai.Detection.Custom.voc import parse_voc_annotat"
},
{
"path": "imageai_tf_deprecated/Detection/Custom/gen_anchors.py",
"chars": 3661,
"preview": "import random\nimport numpy as np\n\nfrom imageai.Detection.Custom.voc import parse_voc_annotation\n\n\ndef IOU(ann, centroids"
},
{
"path": "imageai_tf_deprecated/Detection/Custom/generator.py",
"chars": 9591,
"preview": "import cv2\nimport copy\nimport numpy as np\nfrom tensorflow.keras.utils import Sequence\nfrom imageai.Detection.Custom.util"
},
{
"path": "imageai_tf_deprecated/Detection/Custom/utils/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "imageai_tf_deprecated/Detection/Custom/utils/bbox.py",
"chars": 2951,
"preview": "import numpy as np\nimport os\nimport cv2\nfrom .colors import get_color\n\nclass BoundBox:\n def __init__(self, xmin, ymin"
},
{
"path": "imageai_tf_deprecated/Detection/Custom/utils/colors.py",
"chars": 2404,
"preview": "def get_color(label):\n \"\"\" Return a color from a set of predefined colors. Contains 80 colors in total.\n code orig"
},
{
"path": "imageai_tf_deprecated/Detection/Custom/utils/image.py",
"chars": 2956,
"preview": "import cv2\nimport numpy as np\nimport copy\n\n\ndef _rand_scale(scale):\n scale = np.random.uniform(1, scale)\n return s"
},
{
"path": "imageai_tf_deprecated/Detection/Custom/utils/multi_gpu_model.py",
"chars": 2139,
"preview": "from keras.layers import Lambda, concatenate\nfrom keras.models import Model\nimport tensorflow as tf\n\n\ndef multi_gpu_mode"
},
{
"path": "imageai_tf_deprecated/Detection/Custom/utils/utils.py",
"chars": 11868,
"preview": "import cv2\nimport numpy as np\nimport os\nfrom .bbox import BoundBox, bbox_iou\nfrom scipy.special import expit\n\n\ndef _sigm"
},
{
"path": "imageai_tf_deprecated/Detection/Custom/voc.py",
"chars": 2772,
"preview": "import os\nimport xml.etree.ElementTree as ET\nimport pickle\n\n\ndef parse_voc_annotation(ann_dir, img_dir, cache_name, labe"
},
{
"path": "imageai_tf_deprecated/Detection/README.md",
"chars": 17370,
"preview": "# ImageAI : Object Detection\n\nA **DeepQuest AI** project [https://deepquestai.com](https://deepquestai.com)\n\n### TABLE O"
},
{
"path": "imageai_tf_deprecated/Detection/VIDEO.md",
"chars": 33140,
"preview": "# ImageAI : Video Object Detection, Tracking and Analysis\n\nA **DeepQuest AI** project [https://deepquestai.com](https:/"
},
{
"path": "imageai_tf_deprecated/Detection/YOLO/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "imageai_tf_deprecated/Detection/YOLO/utils.py",
"chars": 11766,
"preview": "import tensorflow as tf\nfrom keras import backend as K\nimport numpy as np\nfrom PIL import Image\nimport cv2\n\n\ndef yolo_he"
},
{
"path": "imageai_tf_deprecated/Detection/YOLO/yolov3.py",
"chars": 16842,
"preview": "from tensorflow.keras.layers import Conv2D, MaxPool2D, Add, ZeroPadding2D, UpSampling2D, Concatenate, LeakyReLU, Lambda\n"
},
{
"path": "imageai_tf_deprecated/Detection/__init__.py",
"chars": 59480,
"preview": "import cv2\nfrom imageai.Detection.keras_retinanet import models as retinanet_models\nfrom imageai.Detection.keras_retinan"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/backend/__init__.py",
"chars": 42,
"preview": "from .backend import * # noqa: F401,F403\n"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/backend/backend.py",
"chars": 4912,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/bin/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/bin/convert_model.py",
"chars": 3821,
"preview": "#!/usr/bin/env python\n\n\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/bin/debug.py",
"chars": 13478,
"preview": "#!/usr/bin/env python\n\n\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/bin/evaluate.py",
"chars": 8238,
"preview": "#!/usr/bin/env python\n\n\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/bin/train.py",
"chars": 23735,
"preview": "#!/usr/bin/env python\n\n\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/callbacks/__init__.py",
"chars": 41,
"preview": "from .common import * # noqa: F401,F403\n"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/callbacks/coco.py",
"chars": 2841,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/callbacks/common.py",
"chars": 1433,
"preview": "from tensorflow import keras\n\n\nclass RedirectModel(keras.callbacks.Callback):\n \"\"\"Callback which wraps another callba"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/callbacks/eval.py",
"chars": 4038,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/initializers.py",
"chars": 1149,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/layers/__init__.py",
"chars": 142,
"preview": "from ._misc import RegressBoxes, UpsampleLike, Anchors, ClipBoxes # noqa: F401\nfrom .filter_detections import FilterDet"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/layers/_misc.py",
"chars": 6912,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/layers/filter_detections.py",
"chars": 9795,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/losses.py",
"chars": 4762,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/models/__init__.py",
"chars": 4755,
"preview": "from __future__ import print_function\nimport sys\n\n\nclass Backbone(object):\n \"\"\" This class stores additional informat"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/models/densenet.py",
"chars": 4403,
"preview": "\"\"\"\nCopyright 2018 vidosits (https://github.com/vidosits/)\n\nLicensed under the Apache License, Version 2.0 (the \"License"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/models/effnet.py",
"chars": 6778,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/models/mobilenet.py",
"chars": 4456,
"preview": "\"\"\"\nCopyright 2017-2018 lvaleriu (https://github.com/lvaleriu/)\n\nLicensed under the Apache License, Version 2.0 (the \"Li"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/models/resnet.py",
"chars": 4913,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/models/retinanet.py",
"chars": 16776,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/models/senet.py",
"chars": 6524,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/models/vgg.py",
"chars": 3925,
"preview": "\"\"\"\nCopyright 2017-2018 cgratie (https://github.com/cgratie/)\n\nLicensed under the Apache License, Version 2.0 (the \"Lice"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/preprocessing/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/preprocessing/coco.py",
"chars": 5457,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/preprocessing/csv_generator.py",
"chars": 7634,
"preview": "\"\"\"\nCopyright 2017-2018 yhenon (https://github.com/yhenon/)\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed unde"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/preprocessing/generator.py",
"chars": 15412,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/preprocessing/kitti.py",
"chars": 5795,
"preview": "\"\"\"\nCopyright 2017-2018 lvaleriu (https://github.com/lvaleriu/)\n\nLicensed under the Apache License, Version 2.0 (the \"Li"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/preprocessing/open_images.py",
"chars": 14050,
"preview": "\"\"\"\nCopyright 2017-2018 lvaleriu (https://github.com/lvaleriu/)\n\nLicensed under the Apache License, Version 2.0 (the \"Li"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/preprocessing/pascal_voc.py",
"chars": 6791,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/utils/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/utils/anchors.py",
"chars": 12143,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/utils/coco_eval.py",
"chars": 3233,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/utils/colors.py",
"chars": 2656,
"preview": "import warnings\n\n\ndef label_color(label):\n \"\"\" Return a color from a set of predefined colors. Contains 80 colors in "
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/utils/compute_overlap.pyx",
"chars": 1655,
"preview": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under "
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/utils/config.py",
"chars": 2101,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/utils/eval.py",
"chars": 9696,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/utils/gpu.py",
"chars": 1659,
"preview": "\"\"\"\nCopyright 2017-2019 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/utils/image.py",
"chars": 11651,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/utils/model.py",
"chars": 953,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/utils/tf_version.py",
"chars": 1899,
"preview": "\"\"\"\nCopyright 2017-2019 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/utils/transform.py",
"chars": 10437,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Detection/keras_retinanet/utils/visualization.py",
"chars": 4222,
"preview": "\"\"\"\nCopyright 2017-2018 Fizyr (https://fizyr.com)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou m"
},
{
"path": "imageai_tf_deprecated/Prediction/Custom/__init__.py",
"chars": 495,
"preview": "from ...Classification.Custom import ClassificationModelTrainer, CustomImageClassification\n\n\n\n\n\nclass ModelTraining(Clas"
},
{
"path": "imageai_tf_deprecated/Prediction/Custom/custom_utils.py",
"chars": 787,
"preview": "import json\n\nCLASS_INDEX = None\n\n\n\ndef preprocess_input(x):\n \"\"\"Preprocesses a tensor encoding a batch of images.\n\n "
},
{
"path": "imageai_tf_deprecated/Prediction/__init__.py",
"chars": 268,
"preview": "from ..Classification import ImageClassification\nfrom matplotlib.cbook import deprecated\n\n\nclass ImagePrediction(ImageCl"
},
{
"path": "imageai_tf_deprecated/Prediction/imagenet_utils.py",
"chars": 36955,
"preview": "CLASS_INDEX = None\n\n\n\ndef preprocess_input(x):\n \"\"\"Preprocesses a tensor encoding a batch of images.\n\n # Arguments"
},
{
"path": "imageai_tf_deprecated/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "requirements.txt",
"chars": 268,
"preview": "cython\npillow>=7.0.0\nnumpy>=1.18.1\nopencv-python>=4.1.2\ntorch>=1.9.0 --extra-index-url https://download.pytorch.org/whl/"
},
{
"path": "requirements_extra.txt",
"chars": 102,
"preview": "pycocotools@git+https://github.com/gautamchitnis/cocoapi.git@cocodataset-master#subdirectory=PythonAPI"
},
{
"path": "requirements_gpu.txt",
"chars": 272,
"preview": "cython\npillow>=7.0.0\nnumpy>=1.18.1\nopencv-python>=4.1.2\ntorch>=1.9.0 --extra-index-url https://download.pytorch.org/whl/"
},
{
"path": "scripts/pascal_voc_to_yolo.py",
"chars": 4220,
"preview": "import glob\nimport os\nimport argparse\nimport pickle\nimport xml.etree.ElementTree as ET\nfrom os import listdir, getcwd\nfr"
},
{
"path": "setup.py",
"chars": 542,
"preview": "from setuptools import setup,find_packages\n\nsetup(name=\"imageai\",\n version='3.0.3',\n description='A python lib"
},
{
"path": "test/test_custom_classification.py",
"chars": 4000,
"preview": "import os, sys\nimport cv2\nfrom PIL import Image\nimport pytest\nfrom os.path import dirname\nsys.path.insert(1, os.path.joi"
},
{
"path": "test/test_custom_classification_training.py",
"chars": 4581,
"preview": "import os, sys\nimport cv2\nimport shutil\nfrom PIL import Image\nimport pytest\nfrom os.path import dirname\nsys.path.insert("
},
{
"path": "test/test_custom_detection_training.py",
"chars": 2446,
"preview": "import os, sys\nimport shutil\nimport pytest\nfrom os.path import dirname\nsys.path.insert(1, os.path.join(dirname(dirname(o"
},
{
"path": "test/test_custom_object_detection.py",
"chars": 6939,
"preview": "import os, sys\nfrom typing import List\nimport shutil\nimport cv2\nimport uuid\nfrom PIL import Image\nimport numpy as np\nimp"
},
{
"path": "test/test_custom_video_detection.py",
"chars": 5000,
"preview": "import os, sys\nfrom typing import List\nfrom numpy import ndarray\nfrom os.path import dirname\nfrom mock import patch\nsys."
},
{
"path": "test/test_image_classification.py",
"chars": 3414,
"preview": "import os, sys\nimport cv2\nfrom PIL import Image\nimport pytest\nfrom os.path import dirname\nsys.path.insert(1, os.path.joi"
},
{
"path": "test/test_object_detection.py",
"chars": 12811,
"preview": "import os, sys\nfrom typing import List\nimport shutil\nimport cv2\nimport uuid\nfrom PIL import Image\nimport numpy as np\nimp"
},
{
"path": "test/test_video_object_detection.py",
"chars": 5897,
"preview": "import os, sys\nfrom typing import List\nfrom numpy import ndarray\nfrom os.path import dirname\nfrom mock import patch\nsys."
}
]
About this extraction
This page contains the full source code of the OlafenwaMoses/ImageAI GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 152 files (1.1 MB), approximately 271.1k tokens, and a symbol index with 732 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.