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

การประเมินและทำนายข้อมูลด้วย TensorFlow และโมเดล Pre-trained


แนวคิด

ใช้ evaluate และ predict ประเมินและทำนายข้อมูล Flatten batch ภาพ ใช้ sigmoid ได้ logits

เรียนรู้การจำแนกแมว-สุนัขด้วย Transfer Learning จาก pre-trained network แนวคิดเดียวกัน: โมเดลที่เทรนบนข้อมูลใหญ่ ใช้เป็น generic feature extractor ได้

รันบน Google Colab

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

print("Evaluation and prediction")
loss, accuracy = model.evaluate(test_dataset)
print('Test accuracy is :', accuracy)
print("The batch of image from test set is retrieved")
image_batch, label_batch = test_dataset.as_numpy_iterator().next()
predictions = model.predict_on_batch(image_batch).flatten()
print("The sigmoid function is applied on the model, it returns logits")
predictions = tf.nn.sigmoid(predictions)
predictions = tf.where(predictions < 0.5, 0, 1)
print('Predictions are:\n', predictions.numpy())
print('Labels are:\n', label_batch)

Credit: TensorFlow Tutorials

ผลลัพธ์

Evaluation and prediction
6/6 [==============================] - 3s 516ms/step - loss: 0.0276 - accuracy: 0.9844
Test accuracy is : 0.984375
The batch of image from test set is retrieved
The sigmoid function is applied on the model, it returns logits
Predictions are:
[1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 1 0 0 1 1 1 0 1 0 1]
Labels are:
[1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 1 0 0 1 1 1 0 1 0 1]

คำอธิบาย

  • โมเดลพร้อมทำนายและประเมินข้อมูล
  • ทำนายโดยส่งภาพเข้าไป
  • ทำนายว่าเป็นแมวหรือสุนัข