Skip to content

EMNIST Detection built with TensorFlow

License

Notifications You must be signed in to change notification settings

jkee58/EMNIST-Detection

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

EMNIST detection with fine-tuned Mobilenet_v2-SSD on Raspberry Pi 4

TensorFlow 2.5.0 Python 3.8.8 Anaconda 4.10.1 Raspberry Pi 4

This repository is forked from tensorflow/models and modified by LIFOsitory

overview

Appended Directories and Files

  • custom_models
  • model_zoo
  • dataset
  • generate_dataset_old.py
  • generate_dataset.py
  • visualize_dataset.py
  • generate_tfrecord.py
  • generate_tflite.py
  • infer_ckpt.py
  • test_tflite.py
  • detect.tflite
  • detect_old.tflite

🧑 research μ•ˆμ—μ„œ μž‘μ—…ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

🧑 visualize_dataset.py을 μ œμ™Έν•˜κ³€ 레퍼런슀λ₯Ό λ°”νƒ•μœΌλ‘œ μ „λΆ€ 직접 μˆ˜μ •ν•˜κ±°λ‚˜ μƒμ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

🧑 dataset 파일이 λ„ˆλ¬΄ 큰 κ΄€κ³„λ‘œ train, test ν΄λ”λŠ” μ œμ™Έν•˜μ˜€μŠ΅λ‹ˆλ‹€. generate dataset.pyλ₯Ό 톡해 생성할 수 μžˆμŠ΅λ‹ˆλ‹€.

Install CUDA and cuDNN on Windows

CUDA TOOLKIT DOCUMENTATION

NVIDIA CUDNN DOCUMENTATION

Install Object Detection API with TensorFlow 2

Object Detection API with TensorFlow 2

Python Package Installation

cd models/research
# Compile protos.
protoc object_detection/protos/*.proto --python_out=.
# Install TensorFlow
pip install tensorflow-gpu
# Install TensorFlow Object Detection API.
cp object_detection/packages/tf2/setup.py .
python -m pip install --use-feature=2020-resolver .
# Test the installation.
python object_detection/builders/model_builder_tf2_test.py

Generate EMNIST letters Object Detection dataset

MNIST Object Detection dataset

Image of Dataset Example

Dataset for object detection on EMNIST letters with COCO 2017 Background. By default, the script generates a dataset with the following attributes:

  • 10,000 images in train. 1,000 images in test
  • 26 Classes(A ~ Z)
  • Between 1 and 5 letters per image
  • Gaussian Blur
  • Threshold 200
    • If the pixel value of EMNIST exceeds the threshold, replace it with COCO image

Generate dataset

Image of Dataset Generate Example

    python generate_dataset.py --data_dir="d:/tensorflow_dataset"

❗ Tensorflow Dataset을 톡해 μžλ™μœΌλ‘œ λ‹€μš΄λ‘œλ“œ λ˜λ„λ‘ κ΅¬μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

❗ COCO 2017을 μ‚¬μš©ν•˜λ―€λ‘œ λ‹€μš΄λ‘œλ“œ(25.20 GiB) 및 μ••μΆ• ν•΄μ œ μ‹œκ°„μ΄ 였래 κ±Έλ¦½λ‹ˆλ‹€.(1~2μ‹œκ°„)

Visualize dataset

The dataset can be visualized with the following command:

    python visualize_dataset.py

Generate the TFRecord file

Preparing Inputs

Image of TFRecord Generate Example

The dataset can be converted to TFRecord file with the following command:

    python generate_tfrecord.py

Train and Evaluate with TensorFlow 2

Training and evaluation guide (CPU, GPU, or TPU)

Training Command

A local training job can be run with the following command:

    python object_detection/model_main_tf2.py --pipeline_config_path="model_zoo/ssd_mobilenet_v2_320x320_coco17_tpu-8/pipeline.config" --model_dir="custom_models/ssd_mobilenet_v2_320x320_coco17_tpu-8" --alsologtostderr

πŸ’‘ Traing Step: 50000

Evaluation Command

A local evaluation job can be run with the following command:

    python object_detection/model_main_tf2.py --pipeline_config_path="model_zoo/ssd_mobilenet_v2_320x320_coco17_tpu-8/pipeline.config" --model_dir="custom_models/ssd_mobilenet_v2_320x320_coco17_tpu-8" --checkpoint_dir="custom_models\ssd_mobilenet_v2_320x320_coco17_tpu-8" --alsologtostderr

Image of Prediction Example

Running Tensorboard

Progress for training and eval jobs can be inspected using Tensorboard. If using the recommended directory structure, Tensorboard can be run using the following command:

    tensorboard --logdir="custom_models/ssd_mobilenet_v2_320x320_coco17_tpu-8"

Image of Loss Example

Run inference with checkpoint file

Run inference with models from the zoo

    python infer_ckpt.py --pipeline_config_path="model_zoo/ssd_mobilenet_v2_320x320_coco17_tpu-8/pipeline.config" --checkpoint_dir="custom_models/ssd_mobilenet_v2_320x320_coco17_tpu-8/" --checkpoint_number="ckpt-52" --test_image_path="dataset/emnist_letters_detection/test/images/541.jpg" 

Run TF2 Models on Raspberry Pi

Running TF2 Detection API Models on mobile

Export TFLite inference grpah

An intermediate SavedModel that can be used with the TFLite Converter via commandline or Python API can be generated with the following command:

    python object_detection/export_tflite_graph_tf2.py --pipeline_config_path "model_zoo/ssd_mobilenet_v2_320x320_coco17_tpu-8/pipeline.config" --trained_checkpoint_dir "custom_models/ssd_mobilenet_v2_320x320_coco17_tpu-8" --output_directory "custom_models/ssd_mobilenet_v2_320x320_coco17_tpu-8"

Convert to TFLite

The SavedModel can be converted to TFLite with the following command:

    python generate_tflite.py

You can infer the TFLite file with the following command:

    python test_tflite.py

Run TFLite Model on Raspberry Pi 4

C++

Pi image installation instructions

Python

TensorFlow Lite Python object detection example with Pi Camera

🚩 λ‘˜ λ‹€ μ†λ„λŠ” λΉ λ₯΄λ‚˜ 카메라 μ‚¬μš©μ‹œ 정확도가 ν˜„μ €νžˆ λ–¨μ–΄μ§‘λ‹ˆλ‹€. (Tγƒ˜To)

🚩 Dataset 생성 방식을 λ°”κΎΈκ±°λ‚˜ λ‹€λ₯Έ Dataset의 ν™œμš©μ„ κ³ λ €ν•΄λ΄μ•Όκ² μŠ΅λ‹ˆλ‹€.

Legacy

νŒŒμΌμ΄λ‚˜ 폴더λͺ… 뒀에 _oldκ°€ 뢙은 경우 λ‹€μŒμ˜ μ‘°κ±΄μ—μ„œ μƒμ„±λœ 파일:

  • fixed size 300 x 300 by resizing
  • 4 Classes(A ~ D)
  • Threshold 255
  • NO Random Crop
  • NO Random Horizontal Flip

Generate dataset

Image of Dataset Legacy Example

Evaluation

Image of Prediction Example

일반적인 κΈ€μž 이미지에 λŒ€ν•΄μ„œ ν•™μŠ΅ν•˜μ§€ μ•Šμ•„ ꡬ글 이미지에 λŒ€ν•΄μ„œλŠ” μ„±λŠ₯이 쒋지 λͺ»ν•¨.

A in google image

Reference