💻Object Detection with TensorFlow

Nonpavit Detbun 🔋🍺
3 min readApr 30, 2019

--

👻 Basic Custom Models 📈

Software :

Hardware :

  • GPU NVIDIA

TensorFlow Core :

Create Environment Python on Anaconda :

TensorFlow/models/research/

Install modules python on Env Anaconda (tfgpu1p12):

TensorFlow/models/research/object_detection

Project :

Download Project จาก tf:models แตกไฟล์และนำไปวางไว้ใน folder TensorFlow และ Download Models จาก models:zoo เลือก models ที่ต้องการนำไปใช้ แตกไฟล์และนำไปวางไว้ใน folder TensorFlow/models/research/object_detection

Question : ควรเลือก models อะไรมาใช้งาน ❓

การเลือก models ควรเลือกจากระบบที่ต้องการนำไปใช้งาน เช่น ถ้าต้องการนำไปใช้งานบน Android ควรเลือก models : ssd_mobilenet , ssd_inception , etc. สามารถตรวจสอบได้จาก library ที่ใช้งาน หรือจากตัว models เอง

Label Image Dataset :

สร้าง folder images นำ Image Dataset มาแบ่งเป็น 2 folder : train / test

แบ่ง Image Dataset เป็น train:test ด้วยอัตราส่วน 70:30 หรือ 80:20 ตามความเหมาะสมของจำนวนข้อมูล

สร้าง Tag Label ให้ Dataset ด้วยโปรแกรม labelImg โดนติด Tag ทุกรูปภาพทั้ง folder train / test

❗️ รูปภาพที่นำมา Training ไม่ควรต่างกันมากเกินไปเพราะจะทำให้เกิด loss เยอะมากและจะทำให้ TensorFlow ไม่สามารถตรวจความเข้ากันของรูปได้ เพราะฉะนั้นควรเลือกภาพก่อนนำมาติด Tag เช่น เมื่อไม่ได้เลือกรูปภาพก่อน แล้วติด Tag รูป Pikachu หันหน้าและหันหลังว่าเป็น Pikachu เหมือนกันจะทำให้ไม่สามารถหาความเหมือนกันได้ แต่ปัญหานี้สามารถแก้ไขได้ โดยเราจะมาลองกันในบทความต่อไป

❕ ก่อนทำ Tag Label ถ้าภาพมีขนาดใหญ่เกิน HD(1280x720) ควร Resize Image ให้เป็น HD(1280x720) ก่อน และแต่ละ models จะมีคำแนะนำอยู่ว่าขนาดการ Crop Tag Label รองรับที่ขนาดเท่าไรเช่น ssd_mobilenet จะมีขนาดประมาณใกล้เคียง 300x300 ถ้าใหญ่/เล็กเกินกว่านี้มากอาจจะทำให้ training ได้ผลลัพท์ที่ไม่ค่อยดี

  • Open Dir
  • Create\nRectBox
  • Tag Label
  • Save XML (แนะนำว่า Save ทุกครั้งที่ทำ Tag Label เสร็จ ถ้ากด Next Image ก่อน Save จะต้องกลับมาทำใหม่ เพราะระบบไม่ Auto Save ให้)
  • Next Image

All Step :

  • สร้าง folder inference_graph ไว้ที่ TensorFlow/models/research/object_detection/
  • สร้าง folder training ไว้ที่ TensorFlow/models/research/object_detection/
  • สร้างไฟล์ labelmap.pbtxt ไว้ที่ TensorFlow/models/research/object_detection/training
  • copy ไฟล์ .config ชื่อเดียวกับ models ที่จำมาใช้ ออกมาจาก folder samples/configs ใน TensorFlow/models/research/object_detection มาวางไว้ที่ TensorFlow/models/research/object_detection/training

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Edit .config :

  • num_classes ใน ssd ใน model : จำนวน Class Tag Label
  • fine_tune_checkpoint : Path Folder Models ที่โหลดมาจาก models:zoo ที่วางอยู่ใน TensorFlow/models/research/object_detection/
  • input_path ใน tf_record_input_reader ใน train_input_reader : Path ไฟล์ train.record ที่วางอยู่ใน TensorFlow/models/research/object_detection/
  • num_examples ใน eval_config : จำนวนรูปภาพใน folder images/test ที่วางอยู่ใน TensorFlow/models/research/object_detection/
  • input_path ใน tf_record_input_reader ใน eval_input_reader : Path ไฟล์ test.record ที่วางอยู่ใน TensorFlow/models/research/object_detection/
  • label_map_path ทั้งใน train_input_reader และ eval_input_reader : Path ไฟล์ labelmap.pbtxt ที่วางอยู่ใน TensorFlow/models/research/object_detection/training

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

  • นำไฟล์ train.py ออกมาจาก folder legacy ใน TensorFlow/models/research/object_detection มาวางไว้ที่ TensorFlow/models/research/object_detection
  • สร้างไฟล์ xml_to_csv.py ไว้ที่ TensorFlow/models/research/object_detection
  • สร้างไฟล์ generate_tfrecord.py ไว้ที่ TensorFlow/models/research/object_detection

แก้ไข code ในไฟล์ generate_tfrecord.py : line 32 ตาม Tag Label ที่ทำไว้ โดยลำดับการ return อ้างอิงจากไฟล์ labelmap.pbtxt

ถ้า Error NoneType ให้เปลี่ยน None เป็น return 0

Question : ควรหยุด Training เมื่อไร❓

  • เมื่อกราฟมีแนวโน้มถึงจุดอิ่มตัวแล้ว เช่น loss เริ่มไม่มีแนวโน้มว่าจะลดลงแล้ว แต่ตามทฤษฎีแล้ว loss ควรมีค่าน้อยกว่า 1
  • ลอง Training ใหม่หลายๆ models แล้วนำไปทดสอบกับระบบที่จะนำไปใช้

Test models training :

  • สร้างไฟล์ detection_image_v1.py และนำรูปภาพที่ต้องการทดสอบ ตั้งชื่อว่า test1 ชนิด jpg ไว้ที่ TensorFlow/models/research/object_detection รันไฟล์ detection_image_v1.py

--

--

Nonpavit Detbun 🔋🍺

I’m Electrical Engineering. 📟 I have an interest in Web App, Mobile App, IoT and Microcontroller 🧐