การสร้าง custom object detection model ด้วย tensorflow ( part 4 ใช้ model )

Nattawat Songsom
3 min readOct 21, 2021

--

จาก บทความที่แล้ว เราจะได้ custom object detection model มาแล้ว ดังนั้นในบทความนี้เราจะลองมาใช้ model นั้นกัน

install library

โดยในการใช้ model เราจะรันบนเครื่อง local (ในบทความนี้จะใช้ windows) ดังนั้นเราต้องมา setup ตามลำดับนี้

note การ install จะคล้ายกับบทความก่อนหน้า ดังนั้นจะไม่ได้อธิบายละเอียดมาก

  1. install python

ในบทความนี้จะใช้ python 3.9.6

2. install tensorflow

ให้เปิด terminal ด้วยสิทธิ์ admin แล้วรันคำสั่ง

pip install --ignore-installed --upgrade tensorflow==2.5.0

จากนั้น test ด้วยคำสั่ง

python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

โดยจะ print ข้อมูล device ออกมา

3. Downloading the TensorFlow Model Garden

ให้สร้าง folder Tensorflow ขึ้นมา

download TensorFlow Object Detection API จาก link นี้ (จะ download ในชื่อ models-master)

จากนั้นนำ models-master ไปวางในโฟลเดอร์ tensorflow และเปลื่ยนชื่อเป็น models จะได้โครงสร้างตามนี้

TensorFlow/
└─ models/
├─ community/
├─ official/
├─ orbit/
├─ research/
└── ...

4. install protobuf

ให้ download จาก link โดยในบทความนี้จะใช้ protoc-3.18.1-win64.zip จากนั้นให้แตกไฟล์และชี้ path โดยตอนนี้จะสามารถเรียก protoc ใน terminal ได้แล้ว

จากนั้นให้เข้าไปยังโฟลเดอร์ TensorFlow/models/research และรันคำสั่ง

protoc object_detection/protos/*.proto --python_out=.

5. install coco API

ให้เปิด terminal ด้วยสิทธ์ admin แล้วรันคำสั่ง

pip install cython
pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI

6. Install the Object Detection API

ให้ copy ไฟล์ setup.py จาก object_detection/packages/tf2 มาวางในโฟลเดอร์ TensorFlow/models/research

จากนั้นรันคำสั่ง

python -m pip install --use-feature=2020-resolver .

โดยเมื่อ install เสร็จแล้วให้ test โดยการรัน

python object_detection/builders/model_builder_tf2_test.py

setup environment

  1. สร้างโฟลเดอร์ inputs จากนั้นใส่รูปที่จะ detect ลงไป

2. สร้างโฟลเดอร์ models จากนั้น download models ที่เราได้จาก บทความที่แล้ว ใส่ลงไป

3. สร้างไฟล์ label_map.pbtxt โดยมีรายละเอียดตามนี้

โดยให้ id map กับ label_map.pbtxt ที่ใช้ตอน train นะ

4. สร้างโฟลเดอร์ outputs เพื่อเก็บรูป output

โดยจาก 1–4 จะได้โครงสร้างโฟลเดอร์ตามนี้

using model

ใช้ code ตามนี้

โอเคมาลองดู code แต่ละส่วนกัน

ให้เรากำหนด path ของ input, model และ label_map ตรงนี้

ต่อมาเราจะ load model มาตั้งไว้ ซึ่งส่วนนี้จะใช้เวลานานหน่อย

ต่อมา load label_map โดยโปรแกรมเราจะนำเลขที่ทายได้จากโมเดล มาแปลงเป็น text อิงจาก label_map ที่เรากำหนด

อันนี้เป็นฟังชันก์แปลงรูปเป็น numpy array ให้ tensorflow เอาไปใช้ต่อเฉยๆ

ส่วนนี้เป็นส่วนการวาดกล่องบนรูป โดยเราสามารถกำหนดคะแนนขั้นต่ำที่จะให้วาดรูปได้ เช่นจากภาพนี้ model จะต้องมั่นใจ 30% ขึ้นไป ว่าเป็นวัตถุชนิดนั้น แล้วจึงจะวาดกล่องบนรูป

และสุดท้าย นำรูปที่วาดกล่องแล้วไป save ลงไฟล์

result

โอเค การรันบน local จะประมาณนี้นะ เดี๋ยวเราค่อยไปเขียน flask เพื่อทำ object detection api กัน

ref Object Detection From TF2 Saved Model — TensorFlow 2 Object Detection API tutorial documentation (tensorflow-object-detection-api-tutorial.readthedocs.io)

--

--

No responses yet