13 มิถุนายน 2563

Published มิถุนายน 13, 2563 by with 3 comments

ทำ Image Recognition ง่าย ๆ ด้วย detecto

ตัวอย่างการใช้งานกับรูปภาพ
ตัวอย่างการใช้งานกับรูปภาพ ภาพ : https://github.com/alankbi/detecto
สวัสดีผู้อ่านทุกท่านครับ หลาย ๆ ท่านที่ทำ AI สาย CV คงหนีไม่พันทำระบบจดจำวัตถุหรือ Image Recognition วันนี้เรามีวิธีการทำต่าง ๆ อย่าง Deep Learning ที่เป็นที่นิยมกันในปัจจุบัน แต่การทำระบบจดจำวัตถุในภาพนั้น กลับไม่ง่ายเท่าไร ยิ่งหากต้องทำข้อมูลสำหรับจดจำที่โมเดลปัจจุบันไม่มี หรือ ต้องการใช้เฉพาะด้านจะยิ่งยากขึ้นไปอีก

วันนี้ผมจะมาแนะนำเครื่องมือทำระบบจดจำวัตถุง่าย ๆ ด้วย detecto กันครับ

detecto เป็นไลบารีสร้างโมเดล computer vision กับ PyTorch สำหรับงาน Image Recognition รองรับ transfer learning กับข้อมูลที่สร้างขึ้นมาใหม่ รองรับรูปภาพและวิดีโอ

ตัวอย่างการใช้งานกับวิดีโอ
ตัวอย่างการใช้งานกับวิดีโอ ภาพ : https://github.com/alankbi/detecto
และเป็น MIT License อีกด้วย สามารถเอาไปปรับแต่งอะไรได้เลย

การทำงาน ตัว detecto ใช้โมเดล pre-trained Faster R-CNN ResNet-50 FPN ในการทำงาน

การติดตั้ง detecto

ก่อนอื่นให้ติดตั้ง PyTorch ก่อน โดยเข้าไปอ่านวิธีติดตั้ง PyTorch ได้ที่ pytorch.org

พอติดตั้ง PyTorch เสร็จแล้ว สามารถติดตั้ง detecto ได้ด้วยคำสั่ง
pip install detecto

จากนั้น เรามาลองใช้งานกันดูครับ
หากเรามีภาพ
Photo by an_vision on Unsplash
เรามาเขียนโค้ดสำหรับ Image Recognition รูปภาพนี้กัน
Google Colab : https://colab.research.google.com/drive/1q8U1cgzFc8LxDKUBCxInMBoqfHzKV2tV?usp=sharing

โค้ด
from detecto.core import Model
from detecto import utils, visualize

model = Model() # เรียกใช้งานโมเดล

image = utils.read_image('photo-1568702846914-96b305d2aaeb?ixlib=rb-1.2.1') # ดึงข้อมูลไฟล์รูปภาพเข้ามา

labels, boxes, scores = model.predict(image) # รับ labels, พิกเซส และ scores
predictions = model.predict_top(image) # รับเฉพาะ labels ที่มี scores สูงสุด

visualize.show_labeled_image(image, boxes, labels) # โชว์ผลลัพธ์ภาพบน Ipython notebook

ผลลัพธ์
นอกจากนั้น ยังรองรับ Image Recognition สำหรับวิดีโอและ live วิดีโออีกด้วย สามารถอ่านเพิ่มเติมได้ที่ https://detecto.readthedocs.io/en/latest/?badge=latest


ต่อไป เรามาลองสร้างชุดข้อมูลรูปภาพของเราเองเพื่อนำมา transfer learning ด้วย detecto กัน

ขอขอบคุณภาพจาก SpaceX (CC BY-NC) source : https://www.flickr.com/photos/spacex/

ขั้นตอนแรก เรากำหนดโจทย์ จะทำระบบตรวจจับตำแหน่งจรวดอวกาศ

ขั้นตอนที่สอง หารูปภาพจรวด (ยิ่งมากยิ่งดี) แต่สำหรับบทความนี้ขอเลือกรูปมาแค่บางส่วน ขนาดชุดข้อมูลเล็ก ให้นำภาพทั้งหมดมาอยู่ในโฟลเดอร์ images ที่อยู่ถัดจากโฟลเดอร์ project

ขั้นตอนที่สาม โหลด labelimg จาก https://tzutalin.github.io/labelImg/ มาจะได้หน้าต่างดังนี้
หากตรง PascalVOC เป็น yolo ให้คลิกให้เป็น PascalVOC
  คลิกให้เป็น
กด Open Dir แล้วเลือก โฟลเดอร์ images ที่เก็บรูปภาพจรวดของเรา
ให้คลิก Create\nRectBox แล้วเลือกบริเวณในภาพที่เป็นจรวด
จากนั้นจะมีช่องให้พิมพ์ label บริเวณรูปภาพที่ต้องการ ในนี้เป็น rocket แล้วกด OK
 
จากนั้นกด Save ทำแบบนี้จบครบทุกรูป
ตอนนี้ในโฟลเดอร์ images จะมีทั้งไฟล์รูปภาพและไฟล์ xml สำหรับนำไปสอนโมเดล
project/
 images/
 .. รูปภาพ.jpg
 .. รูปภาพ.xml
ต่อไปให้สร้างโฟลเดอร์ train_labels กับ val_labels ใน project จะได้
project/
 train_labels/
 val_labels/
 images/
 .. รูปภาพ.jpg
 .. รูปภาพ.xml

แล้วเขียนโค้ดสำหรับแบ่งไฟล์ xml ในโฟลเดอร์ project กัน แล้วรัน
import glob
from shutil import copyfile
from sklearn.model_selection import train_test_split


path = './images/'
files = [f.split('images\\')[-1].replace('.xml','') for f in glob.glob(path + "*.xml", recursive=True)]
files.sort()
train,val=train_test_split(files, test_size=0.1, random_state=42)

for i in files:
    copyfile('./images/'+i+'.xml','./train_labels/'+i+'.xml')

for i in val:
    copyfile('./images/'+i+'.xml','./val_labels/'+i+'.xml')

จากนั้น ไปขั้นตอนต่อไปกัน

ขั้นตอนที่สี่ ฝึกสอนโมเดล โดยใช้โค้ดดังนี้
from detecto import core, utils
from torchvision import transforms
import matplotlib.pyplot as plt

# Convert XML files to CSV format
utils.xml_to_csv('train_labels/', 'train_labels.csv')
utils.xml_to_csv('val_labels/', 'val_labels.csv')

# Define custom transforms to apply to your dataset
custom_transforms = transforms.Compose([
    transforms.ToPILImage(),
    transforms.Resize(800),
    transforms.ColorJitter(saturation=0.3),
    transforms.ToTensor(),
    utils.normalize_transform(),
])

# Pass in a CSV file instead of XML files for faster Dataset initialization speeds
dataset = core.Dataset('train_labels.csv', 'images/', transform=custom_transforms)
val_dataset = core.Dataset('val_labels.csv', 'val_images')  # Validation dataset for training

# Create your own DataLoader with custom options
loader = core.DataLoader(dataset, batch_size=2, shuffle=True)

model = core.Model(['rocket'])
losses = model.fit(loader, val_dataset, epochs=5, learning_rate=0.001, verbose=True)

plt.plot(losses)  # Visualize loss throughout training
plt.show()

model.save('model_weights.pth')  # Save model to a file

# Directly access underlying torchvision model for even more control
torch_model = model.get_internal_model()
print(type(torch_model))

เพียงเท่านี้เราก็ได้โมเดล Image Recognition ตรวจจับจรวดอวกาศแล้ว ส่วนเวลาเรียกใช้งานเพียงแค่ใช้คำสั่ง
from detecto.core import Model
from detecto import utils, visualize

labels = ['rocket']  # อย่าลืมพิมพ์ label ที่เราสร้าง
model = Model.load('model_weights.pth', labels)

image = utils.read_image('photo-rocket.jpg') # ดึงข้อมูลไฟล์รูปภาพเข้ามา

labels, boxes, scores = model.predict(image) # รับ labels, พิกเซส และ scores
predictions = model.predict_top(image) # รับเฉพาะ labels ที่มี scores สูงสุด

visualize.show_labeled_image(image, boxes, labels) # โชว์ผลลัพธ์ภาพบน Ipython notebook



อ่านเอกสารเพิ่มเติมได้ที่ https://detecto.readthedocs.io
GitHub : https://github.com/alankbi/detecto

3 ความคิดเห็น:

  1. ผม train ข้อมูลเองแล้วมีปัญหา runtimeerror : expected object of scalar type Long but got scalar type Float for argument #2 'other'

    ต้องแก้ยังไงดีครับ

    ตอบลบ
  2. ผมลองใช้ window 10 pip install detect​o ได้ปกติ ใช้ python.3.8.3 แต่เวลาทำงานไม่สามารถ import module detecto

    ตอบลบ
  3. ผมติดตั้ง pytorch, detect​oบนwin10 ได้ครับแต่เนียกใช้ detect​oไม่ได้

    ตอบลบ

แสดงความคิดเห็นได้ครับ :)