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

การเพิ่ม Classification Head ด้วย TensorFlow และ Python


แนวคิด

ใช้ tf.keras.Sequential ที่มีเลเยอร์ Dense ร่วมกับ feature extractor model ที่กำหนดไว้ก่อนหน้า

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

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

print("Attaching a classification head")
num_classes = len(class_names)
model = tf.keras.Sequential([
    feature_extractor_layer,
    tf.keras.layers.Dense(num_classes)
])
print("The base architecture of the model")
model.summary()
print("The predictions are made")
predictions = model(image_batch)
print("The dimensions of the predictions")
predictions.shape

Credit: TensorFlow Tutorials

ผลลัพธ์

Attaching a classification head
The base architecture of the model
Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
keras_layer_1 (KerasLayer)   (None, 1280)              2257984
dense_3 (Dense)              (None, 5)                 6405
=================================================================
Total params: 2,264,389
Trainable params: 6,405
Non-trainable params: 2,257,984
_________________________________________________________________
The predictions are made
The dimensions of the predictions
TensorShape([32, 5])

คำอธิบาย

  • ต่อ Classification Head เข้ากับโมเดล
  • ดูสถาปัตยะพื้นฐานด้วย summary()
  • ทำการทำนายและตรวจสอบมิติของผลลัพธ์
  • ข้อมูลแสดงบนคอนโซล