คุณสามารถทําให้โมเดลที่กําหนดเองและโมเดลที่ฝึกด้วย AutoML ใช้งานได้ รวมถึงจัดการโมเดลดังกล่าวได้โดยใช้Firebaseคอนโซลหรือ Firebase Admin Python และ Node.js SDK หากคุณเพียงต้องการติดตั้งใช้งานโมเดลและอัปเดตเป็นครั้งคราว การใช้Firebaseคอนโซลมักจะเป็นวิธีที่ง่ายที่สุด Admin SDK มีประโยชน์เมื่อผสานรวมกับ ไปป์ไลน์การสร้าง ทำงานกับ Colab หรือสมุดบันทึก Jupyter และเวิร์กโฟลว์อื่นๆ
ติดตั้งใช้งานและจัดการโมเดลในคอนโซล 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ที่เก็บข้อมูลที่คุณต้องการใช้จัดเก็บโมเดล
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', })
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 จากไฟล์โมเดล ให้อัปโหลดไปยังโปรเจ็กต์ แล้วเผยแพร่
# 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)
// 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
# 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 ได้โดยทำดังนี้
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 จากสคริปต์การฝึกโดยตรงก็ได้
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.)
# ...
โมเดล TensorFlow Lite ของ AutoML
หากฝึกโมเดล Edge ด้วย AutoML Cloud API หรือด้วย UI ของคอนโซล Google Cloud คุณจะทำให้โมเดลใช้งานได้ใน Firebase โดยใช้ Admin SDK ได้
คุณจะต้องระบุตัวระบุทรัพยากรของโมเดล ซึ่งเป็นสตริงที่มีลักษณะคล้ายตัวอย่างต่อไปนี้
projects/PROJECT_NUMBER /locations/STORAGE_LOCATION /models/MODEL_ID
PROJECT_NUMBER |
หมายเลขโปรเจ็กต์ของCloud Storageที่เก็บข้อมูลที่มี โมเดล ซึ่งอาจเป็นโปรเจ็กต์ Firebase หรือGoogle Cloud โปรเจ็กต์อื่น คุณดูค่านี้ได้ในหน้าการตั้งค่าของ Firebase คอนโซลหรือแดชบอร์ดคอนโซล Google Cloud |
STORAGE_LOCATION |
ตำแหน่งทรัพยากรของCloud Storageที่เก็บข้อมูลที่มีโมเดล ค่านี้จะเป็น us-central1 เสมอ |
MODEL_ID |
รหัสของโมเดลที่คุณได้รับจาก AutoML Cloud API |
# First, import and initialize the SDK as shown above.
# Get a reference to the AutoML model
source = ml.TFLiteAutoMlSource('projects/{}/locations/{}/models/{}'.format(
# See above for information on these values.
project_number,
storage_location,
model_id
))
# Create the model object
tflite_format = ml.TFLiteFormat(model_source=source)
model = ml.Model(
display_name="example_model", # This is the name you will 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)
new_model.wait_for_unlocked()
ml.publish_model(new_model.model_id)
// First, import and initialize the SDK as shown above.
(async () => {
// Get a reference to the AutoML model. See above for information on these
// values.
const automlModel = `projects/${projectNumber}/locations/${storageLocation}/models/${modelId}`;
// Create the model object and add the model to your Firebase project.
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: { automlModel: automlModel },
});
// Wait for the model to be ready.
await model.waitForUnlocked();
// Publish the model.
await ml.publishModel(model.modelId);
process.exit();
})().catch(console.error);
แสดงรายการโมเดลของโปรเจ็กต์
คุณสามารถแสดงรายการโมเดลของโปรเจ็กต์และกรองผลลัพธ์ได้ (ไม่บังคับ) โดยทำดังนี้
# 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))
// 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_model display_name != example_model ชื่อที่แสดงทั้งหมดที่มีคำนำหน้า display_name : experimental_*
โปรดทราบว่าระบบรองรับเฉพาะการจับคู่คำนำหน้า |
tags |
tags: face_detector tags: face_detector AND tags: experimental
|
state.published |
state.published = true state.published = false
|
รวมตัวกรองกับตัวดำเนินการ AND
, OR
และ NOT
รวมถึงวงเล็บ ((
,
)
)
อัปเดตโมเดล
หลังจากเพิ่มโมเดลลงในโปรเจ็กต์แล้ว คุณจะอัปเดตชื่อที่แสดง แท็ก และไฟล์โมเดล tflite
ได้โดยทำดังนี้
# 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. (You could also update with a
# `TFLiteAutoMlSource`)
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)
// 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" ให้ทำดังนี้
# 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)
// 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);