कस्टम मॉडल को Firebase कंसोल या Python और Node.js के लिए Firebase Admin SDK टूल का इस्तेमाल करके डिप्लॉय और मैनेज किया जा सकता है. अगर आपको सिर्फ़ कोई मॉडल डिप्लॉय करना है और उसे कभी-कभी अपडेट करना है, तो आम तौर पर Firebase कंसोल का इस्तेमाल करना सबसे आसान होता है. बिल्ड पाइपलाइन के साथ इंटिग्रेट करने, Colab या Jupyter नोटबुक के साथ काम करने, और अन्य वर्कफ़्लो के लिए, Admin SDK टूल मददगार साबित हो सकता है.
Firebase कंसोल में मॉडल डिप्लॉय और मैनेज करना
TensorFlow Lite मॉडल
Firebase कंसोल का इस्तेमाल करके, TensorFlow Lite मॉडल डिप्लॉय करने के लिए:
- कंसोल में, Firebase ML कस्टम मॉडल वाला पेज खोलें.Firebase
- कस्टम मॉडल जोड़ें (या कोई दूसरा मॉडल जोड़ें) पर क्लिक करें.
- कोई ऐसा नाम डालें जिसका इस्तेमाल, आपके Firebase प्रोजेक्ट में मॉडल की पहचान करने के लिए किया जाएगा. इसके बाद, TensorFlow Lite मॉडल की फ़ाइल अपलोड करें. आम तौर पर, इस फ़ाइल का नाम
.tfliteया.liteसे खत्म होता है.
मॉडल डिप्लॉय करने के बाद, यह कस्टम पेज पर दिखेगा. यहां से, मॉडल को नई फ़ाइल से अपडेट करने, मॉडल डाउनलोड करने, और मॉडल को अपने प्रोजेक्ट से मिटाने जैसे टास्क पूरे किए जा सकते हैं.
Firebase Admin SDK टूल की मदद से मॉडल डिप्लॉय और मैनेज करना
इस सेक्शन में, Admin SDK टूल की मदद से मॉडल डिप्लॉय करने और मैनेज करने से जुड़े सामान्य टास्क पूरे करने का तरीका बताया गया है. ज़्यादा मदद के लिए, Python या Node.js के लिए SDK रेफ़रंस देखें.
एसडीके टूल के इस्तेमाल के उदाहरण देखने के लिए, Python का क्विकस्टार्ट सैंपल और Node.js का क्विकस्टार्ट सैंपल देखें.
शुरू करने से पहले
अगर आपके पास पहले से कोई Firebase प्रोजेक्ट नहीं है, तो Firebase कंसोल में एक नया प्रोजेक्ट बनाएं. इसके बाद, अपना प्रोजेक्ट खोलें और यह काम करें:
सेटिंग पेज पर, कोई सेवा खाता बनाएं और सेवा खाते की कुंजी वाली फ़ाइल डाउनलोड करें. इस फ़ाइल को सुरक्षित रखें, क्योंकि इससे आपके प्रोजेक्ट को एडमिन का ऐक्सेस मिलता है.
स्टोरेज पेज पर, Cloud Storage चालू करें. अपने बकेट का नाम नोट करें.
अपने Firebase प्रोजेक्ट में मॉडल की फ़ाइलें जोड़ने के दौरान, उन्हें कुछ समय के लिए सेव करने के लिए, Cloud Storage बकेट की ज़रूरत होती है. अगर आपके पास Blaze प्लान है, तो इस मकसद के लिए डिफ़ॉल्ट बकेट के अलावा कोई दूसरा बकेट बनाया और इस्तेमाल किया जा सकता है.
Firebase ML पेज पर, शुरू करें पर क्लिक करें. यह तब करना है, जब आपने Firebase ML को अब तक चालू नहीं किया है.
Google APIs कंसोल में, अपना Firebase प्रोजेक्ट खोलें और Firebase ML API चालू करें.
Admin 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 में बदला जा सकता है. साथ ही, उसे एक ही चरण में अपलोड किया जा सकता है. HDF5 फ़ाइल में सेव किए गए Keras मॉडल का इस्तेमाल किया जा सकता है:
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);