คุณสามารถทำให้โมเดลที่กำหนดเองใช้งานได้และจัดการโมเดลดังกล่าวได้โดยใช้ คอนโซลFirebase หรือ Firebase Admin Python และ Node.js SDK หากเพียงต้องการทำให้โมเดลใช้งานได้และอัปเดตโมเดลเป็นครั้งคราว การใช้ คอนโซล มักจะเป็นวิธีที่ง่ายที่สุดFirebase Admin SDK อาจมีประโยชน์เมื่อผสานรวมกับไปป์ไลน์การสร้าง ทำงานกับ Colab หรือ Jupyter Notebook และเวิร์กโฟลว์อื่นๆ
ทำให้โมเดลใช้งานได้และจัดการโมเดลในคอนโซล Firebase
โมเดล TensorFlow Lite
วิธีทำให้โมเดล TensorFlow Lite ใช้งานได้โดยใช้คอนโซล Firebase
- เปิดหน้าโมเดลที่กำหนดเองFirebase MLใน Firebaseคอนโซล
- คลิกเพิ่มโมเดลที่กำหนดเอง (หรือเพิ่มโมเดลอื่น)
- ระบุชื่อที่จะใช้ระบุโมเดลในโปรเจ็กต์ Firebase แล้วอัปโหลดไฟล์โมเดล TensorFlow Lite (โดยปกติจะลงท้ายด้วย
.tfliteหรือ.lite)
หลังจากทำให้โมเดลใช้งานได้แล้ว คุณจะเห็นโมเดลในหน้า "กำหนดเอง" จากหน้านั้น คุณสามารถทำงานต่างๆ ให้เสร็จสมบูรณ์ได้ เช่น อัปเดตโมเดลด้วยไฟล์ใหม่ ดาวน์โหลดโมเดล และลบโมเดลออกจากโปรเจ็กต์
ทำให้โมเดลใช้งานได้และจัดการโมเดลด้วย Firebase Admin SDK
ส่วนนี้แสดงวิธีทำงานทั่วไปในการทำให้โมเดลใช้งานได้และจัดการโมเดลให้เสร็จสมบูรณ์ด้วย Admin SDK ดูความช่วยเหลือเพิ่มเติมได้ในข้อมูลอ้างอิง SDK สำหรับ Python หรือ Node.js
ดูตัวอย่างการใช้งาน SDK ได้ที่ ตัวอย่างการเริ่มต้นอย่างรวดเร็วของ Python และ ตัวอย่างการเริ่มต้นอย่างรวดเร็วของ Node.js
ก่อนเริ่มต้น
หากยังไม่มีโปรเจ็กต์ Firebase ให้สร้างโปรเจ็กต์ใหม่ใน Firebaseคอนโซล จากนั้นเปิดโปรเจ็กต์และทำดังนี้
ในหน้า การตั้งค่า ให้สร้างบัญชีบริการและ ดาวน์โหลดไฟล์คีย์บัญชีบริการ เก็บไฟล์นี้ไว้ในที่ปลอดภัย เนื่องจากไฟล์นี้ให้สิทธิ์เข้าถึงระดับผู้ดูแลระบบแก่โปรเจ็กต์
ในหน้าพื้นที่เก็บข้อมูล ให้เปิดใช้ Cloud Storage จดชื่อบัคเก็ตไว้
คุณต้องมีบัคเก็ต Cloud Storage เพื่อจัดเก็บไฟล์โมเดลชั่วคราว ขณะเพิ่มไฟล์ลงในโปรเจ็กต์ Firebase หากใช้แพ็กเกจ Blaze คุณสามารถสร้างและใช้บัคเก็ตอื่นที่ไม่ใช่บัคเก็ตเริ่มต้นเพื่อวัตถุประสงค์นี้ได้
ในหน้า Firebase ML ให้คลิก เริ่มต้นใช้งาน หากยังไม่ได้ เปิดใช้ Firebase ML
ใน คอนโซล Google APIs ให้เปิดโปรเจ็กต์ Firebase และเปิดใช้ Firebase ML API
-
เมื่อเริ่มต้น SDK ให้ระบุข้อมูลเข้าสู่ระบบบัญชีบริการและ บัคเก็ต Cloud Storage ที่ต้องการใช้จัดเก็บโมเดล
Python
import firebase_admin from firebase_admin import ml from firebase_admin import credentials firebase_admin.initialize_app( credentials.Certificate('/path/to/your/service_account_key.json'), options={ 'storageBucket': 'your-storage-bucket', })Node.js
const admin = require('firebase-admin'); const serviceAccount = require('/path/to/your/service_account_key.json'); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), storageBucket: 'your-storage-bucket', }); const ml = admin.machineLearning();
ทำให้โมเดลใช้งานได้
ไฟล์ TensorFlow Lite
หากต้องการทำให้โมเดล TensorFlow Lite ใช้งานได้จากไฟล์โมเดล ให้อัปโหลดไฟล์ไปยังโปรเจ็กต์ แล้วเผยแพร่ไฟล์โดยทำดังนี้
Python
# First, import and initialize the SDK as shown above.
# Load a tflite file and upload it to Cloud Storage
source = ml.TFLiteGCSModelSource.from_tflite_model_file('example.tflite')
# Create the model object
tflite_format = ml.TFLiteFormat(model_source=source)
model = ml.Model(
display_name="example_model", # This is the name you use from your app to load the model.
tags=["examples"], # Optional tags for easier management.
model_format=tflite_format)
# Add the model to your Firebase project and publish it
new_model = ml.create_model(model)
ml.publish_model(new_model.model_id)
Node.js
// First, import and initialize the SDK as shown above.
(async () => {
// Upload the tflite file to Cloud Storage
const storageBucket = admin.storage().bucket('your-storage-bucket');
const files = await storageBucket.upload('./example.tflite');
// Create the model object and add the model to your Firebase project.
const bucket = files[0].metadata.bucket;
const name = files[0].metadata.name;
const gcsUri = `gs:/⁠/${bucket}/${name}`;
const model = await ml.createModel({
displayName: 'example_model', // This is the name you use from your app to load the model.
tags: ['examples'], // Optional tags for easier management.
tfliteModel: { gcsTfliteUri: gcsUri },
});
// Publish the model.
await ml.publishModel(model.modelId);
process.exit();
})().catch(console.error);
โมเดล TensorFlow และ Keras
ด้วย Python SDK คุณสามารถแปลงโมเดลจากรูปแบบโมเดลที่บันทึกไว้ของ TensorFlow เป็น TensorFlow Lite และอัปโหลดไปยังบัคเก็ต Cloud Storage ได้ในขั้นตอนเดียว จากนั้นทำให้โมเดลใช้งานได้ด้วยวิธีเดียวกับที่คุณทำให้ไฟล์ TensorFlow Lite ใช้งานได้
Python
# First, import and initialize the SDK as shown above.
# Convert the model to TensorFlow Lite and upload it to Cloud Storage
source = ml.TFLiteGCSModelSource.from_saved_model('./model_directory')
# Create the model object
tflite_format = ml.TFLiteFormat(model_source=source)
model = ml.Model(
display_name="example_model", # This is the name you use from your app to load the model.
tags=["examples"], # Optional tags for easier management.
model_format=tflite_format)
# Add the model to your Firebase project and publish it
new_model = ml.create_model(model)
ml.publish_model(new_model.model_id)
หากมีโมเดล Keras คุณสามารถแปลงโมเดลเป็น TensorFlow Lite และอัปโหลดได้ในขั้นตอนเดียวเช่นกัน คุณสามารถใช้โมเดล Keras ที่บันทึกลงในไฟล์ HDF5 ได้โดยทำดังนี้
Python
import tensorflow as tf
# Load a Keras model, convert it to TensorFlow Lite, and upload it to Cloud Storage
model = tf.keras.models.load_model('your_model.h5')
source = ml.TFLiteGCSModelSource.from_keras_model(model)
# Create the model object, add the model to your project, and publish it. (See
# above.)
# ...
หรือคุณจะแปลงและอัปโหลดโมเดล Keras จากสคริปต์การฝึกโดยตรงก็ได้โดยทำดังนี้
Python
import tensorflow as tf
# Create a simple Keras model.
x = [-1, 0, 1, 2, 3, 4]
y = [-3, -1, 1, 3, 5, 7]
model = tf.keras.models.Sequential(
[tf.keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
model.fit(x, y, epochs=3)
# Convert the model to TensorFlow Lite and upload it to Cloud Storage
source = ml.TFLiteGCSModelSource.from_keras_model(model)
# Create the model object, add the model to your project, and publish it. (See
# above.)
# ...
แสดงรายการโมเดลของโปรเจ็กต์
คุณสามารถแสดงรายการโมเดลของโปรเจ็กต์ และเลือกกรองผลลัพธ์ได้โดยทำดังนี้
Python
# First, import and initialize the SDK as shown above.
face_detectors = ml.list_models(list_filter="tags: face_detector").iterate_all()
print("Face detection models:")
for model in face_detectors:
print('{} (ID: {})'.format(model.display_name, model.model_id))
Node.js
// First, import and initialize the SDK as shown above.
(async () => {
let listOptions = {filter: 'tags: face_detector'}
let models;
let pageToken = null;
do {
if (pageToken) listOptions.pageToken = pageToken;
({models, pageToken} = await ml.listModels(listOptions));
for (const model of models) {
console.log(`${model.displayName} (ID: ${model.modelId})`);
}
} while (pageToken != null);
process.exit();
})().catch(console.error);
คุณสามารถกรองตามช่องต่อไปนี้
| ช่อง | ตัวอย่าง |
|---|---|
display_name |
display_name = example_modeldisplay_name != example_modelชื่อที่แสดงทั้งหมดที่มีคำนำหน้า display_name : experimental_*
โปรดทราบว่าระบบรองรับเฉพาะการจับคู่คำนำหน้าเท่านั้น |
tags |
tags: face_detectortags: face_detector AND tags: experimental
|
state.published |
state.published = truestate.published = false
|
รวมตัวกรองด้วยตัวดำเนินการ AND, OR และ NOT รวมถึงวงเล็บเปิด ( และวงเล็บปิด )
อัปเดตโมเดล
หลังจากเพิ่มโมเดลลงในโปรเจ็กต์แล้ว คุณสามารถอัปเดตชื่อที่แสดง แท็ก และไฟล์โมเดล tflite ได้โดยทำดังนี้
Python
# First, import and initialize the SDK as shown above.
model = ... # Model object from create_model(), get_model(), or list_models()
# Update the model with a new tflite model.
source = ml.TFLiteGCSModelSource.from_tflite_model_file('example_v2.tflite')
model.model_format = ml.TFLiteFormat(model_source=source)
# Update the model's display name.
model.display_name = "example_model"
# Update the model's tags.
model.tags = ["examples", "new_models"]
# Add a new tag.
model.tags += "experimental"
# After you change the fields you want to update, save the model changes to
# Firebase and publish it.
updated_model = ml.update_model(model)
ml.publish_model(updated_model.model_id)
Node.js
// First, import and initialize the SDK as shown above.
(async () => {
const model = ... // Model object from createModel(), getModel(), or listModels()
// Upload a new tflite file to Cloud Storage.
const files = await storageBucket.upload('./example_v2.tflite');
const bucket = files[0].metadata.bucket;
const name = files[0].metadata.name;
// Update the model. Any fields you omit will be unchanged.
await ml.updateModel(model.modelId, {
displayName: 'example_model', // Update the model's display name.
tags: model.tags.concat(['new']), // Add a tag.
tfliteModel: {gcsTfliteUri: `gs:/⁠/${bucket}/${name}`},
});
process.exit();
})().catch(console.error);
เลิกเผยแพร่หรือลบโมเดล
หากต้องการเลิกเผยแพร่หรือลบโมเดล ให้ส่งรหัสโมเดลไปยังเมธอดเลิกเผยแพร่หรือลบ เมื่อเลิกเผยแพร่โมเดล โมเดลจะยังคงอยู่ในโปรเจ็กต์ แต่แอปจะดาวน์โหลดโมเดลไม่ได้ เมื่อลบโมเดล โมเดลจะถูกนำออกจากโปรเจ็กต์โดยสมบูรณ์ (โดยปกติแล้วเวิร์กโฟลว์มาตรฐานจะไม่มีการเลิกเผยแพร่โมเดล แต่คุณสามารถใช้การเลิกเผยแพร่เพื่อเลิกเผยแพร่โมเดลใหม่ที่คุณเผยแพร่โดยไม่ได้ตั้งใจและยังไม่ได้ใช้งานที่ใดก็ได้ทันที หรือในกรณีที่ผู้ใช้ได้รับผลกระทบมากกว่าหากดาวน์โหลดโมเดลที่ไม่ดีแทนที่จะได้รับข้อผิดพลาด "ไม่พบโมเดล")
หากยังไม่มีการอ้างอิงไปยังออบเจ็กต์โมเดล คุณอาจต้องรับรหัสโมเดลโดยการแสดงรายการโมเดลของโปรเจ็กต์ด้วยตัวกรอง ตัวอย่างเช่น หากต้องการลบโมเดลทั้งหมดที่มีแท็ก "face_detector" ให้ทำดังนี้
Python
# First, import and initialize the SDK as shown above.
face_detectors = ml.list_models(list_filter="tags: 'face_detector'").iterate_all()
for model in face_detectors:
ml.delete_model(model.model_id)
Node.js
// First, import and initialize the SDK as shown above.
(async () => {
let listOptions = {filter: 'tags: face_detector'}
let models;
let pageToken = null;
do {
if (pageToken) listOptions.pageToken = pageToken;
({models, pageToken} = await ml.listModels(listOptions));
for (const model of models) {
await ml.deleteModel(model.modelId);
}
} while (pageToken != null);
process.exit();
})().catch(console.error);