Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การเพิ่ม Batch Dimension และส่งภาพเข้โมเดลด้วย TensorFlow


แนวคิด

แปลงภาพเป็น NumPy array เพิ่ม batch dimension แล้วส่งเข้โมเดลทำนาย

แนวคิด Transfer Learning, TensorFlow Hub, Google Colab เหมือนเดิม

ตัวอย่างโค้ด

grace_hopper = np.array(grace_hopper)/255.0
print("The dimensions of the image are")
print(grace_hopper.shape)
result = classifier.predict(grace_hopper[np.newaxis, ...])
print("The dimensions of the resultant image are")
print(result.shape)
predicted_class = np.argmax(result[0], axis=-1)
print("The predicted class is")
print(predicted_class)

Credit: TensorFlow Tutorials

ผลลัพธ์

The dimensions of the image are
(224, 224, 3)
The dimensions of the resultant image are
(1, 1001)
The predicted class is
819

คำอธิบาย

  • เพิ่ม batch dimension ด้วย np.newaxis
  • ส่งภาพเข้โมเดลได้ผลลัพธ์เป็นเวกเตอร์ 1001 ค่า (logits)
  • แต่ละค่าแสดงความน่าจะเป็นของแต่ละคลาส