कस्टम मॉडल डिप्लॉय और मैनेज करना

आपके पास इनमें से किसी एक का इस्तेमाल करके, कस्टम मॉडल और AutoML से ट्रेन किए गए मॉडल को डिप्लॉय और मैनेज करने का विकल्प होता है Firebase कंसोल या Firebase Admin Python और Node.js SDK टूल. अगर आपको आप मॉडल को डिप्लॉय करना चाहते हैं और समय-समय पर उसे अपडेट करना चाहते हैं, तो यह आम तौर पर में Firebase कंसोल का इस्तेमाल किया जा सकता है. एडमिन SDK टूल से इंटिग्रेट करने पर, आपको मदद मिल सकती है पाइपलाइन बनाना, Colab या Jupyter notebook, और दूसरे वर्कफ़्लो के साथ काम करना.

Firebase कंसोल में मॉडल डिप्लॉय और मैनेज करना

TensorFlow Lite के मॉडल

Firebase कंसोल का इस्तेमाल करके, TensorFlow Lite मॉडल को डिप्लॉय करने के लिए:

  1. Firebase एमएल कस्टम मॉडल पेज खोलें Firebase कंसोल.
  2. कस्टम मॉडल जोड़ें पर क्लिक करें (या कोई दूसरा मॉडल जोड़ें) पर क्लिक करें.
  3. कोई नाम तय करें, जिसका इस्तेमाल आपके Firebase में आपके मॉडल की पहचान करने के लिए किया जाएगा प्रोजेक्ट करें, फिर TensorFlow Lite मॉडल फ़ाइल (आम तौर पर इसके अंत में खत्म होने वाली .tflite या .lite).

अपने मॉडल को डिप्लॉय करने के बाद, उसे कस्टम पेज पर देखा जा सकता है. इसके बाद, आपको नई फ़ाइल से मॉडल को अपडेट करने, मॉडल, और अपने प्रोजेक्ट से मॉडल हटाना होगा.

Firebase एडमिन SDK टूल की मदद से, मॉडल डिप्लॉय और मैनेज करना

इस सेक्शन में, सामान्य मॉडल के डिप्लॉयमेंट और मैनेजमेंट को पूरा करने का तरीका बताया गया है एडमिन SDK से किए गए टास्क. Python के लिए SDK टूल का रेफ़रंस देखें या ज़्यादा मदद के लिए Node.js का इस्तेमाल करें.

इस्तेमाल किए जा रहे SDK टूल के उदाहरण देखने के लिए, यहां जाएं: Python क्विकस्टार्ट सैंपल और Node.js क्विकस्टार्ट सैंपल.

शुरू करने से पहले

  1. अगर आपके पास पहले से कोई Firebase प्रोजेक्ट नहीं है, तो Firebase कंसोल. इसके बाद, अपना प्रोजेक्ट खोलें और ये काम करें:

    1. सेटिंग पेज पर, सेवा खाता बनाएं और सेवा खाते की कुंजी फ़ाइल डाउनलोड करें. इस फ़ाइल को सुरक्षित रखें, क्योंकि यह आपके प्रोजेक्ट का एडमिन ऐक्सेस देता है.

    2. स्टोरेज पेज पर, Cloud Storage चालू करें. अपने बकेट का नाम.

      मॉडल की फ़ाइलों को कुछ समय के लिए सेव करने के लिए, आपको Cloud Storage बकेट की ज़रूरत होगी इन्हें अपने Firebase प्रोजेक्ट में जोड़ते समय. अगर आप Blaze पर हैं प्लान है, तो आपके पास इसके लिए डिफ़ॉल्ट बकेट के अलावा अन्य बकेट बनाने और इस्तेमाल करने का विकल्प होगा उद्देश्य है.

    3. अगर आपने Firebase एमएल पेज पर अभी तक कोई गतिविधि नहीं की है, तो शुरू करें पर क्लिक करें Firebase एमएल चालू की गई.

  2. Google API कंसोल में, अपना Firebase खोलें प्रोजेक्ट करें और Firebase ML API चालू करें.

  3. एडमिन SDK टूल को इंस्टॉल और शुरू करें.

    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.)
# ...

AutoML TensorFlow Lite मॉडल

अगर आपने Edge मॉडल को AutoML Cloud API की मदद से ट्रेनिंग दी है या Google Cloud Console यूज़र इंटरफ़ेस (यूआई) की मदद से, मॉडल को Firebase में डिप्लॉय किया जा सकता है, तो एडमिन SDK टूल.

आपको मॉडल के संसाधन पहचानकर्ता को तय करना होगा, जो कि एक ऐसी स्ट्रिंग है नीचे दिया गया उदाहरण कैसा दिखता है:

projects/PROJECT_NUMBER/locations/STORAGE_LOCATION/models/MODEL_ID
PROJECT_NUMBER Cloud Storage बकेट का प्रोजेक्ट नंबर जिसमें model. यह आपका Firebase प्रोजेक्ट या कोई दूसरा Google Cloud हो सकता है प्रोजेक्ट. आपको यह मान इसके सेटिंग पेज पर मिलेगा Firebase कंसोल या Google Cloud Console डैशबोर्ड.
STORAGE_LOCATION Cloud Storage बकेट के संसाधन की जगह, जिसमें यह शामिल है मॉडल. यह वैल्यू हमेशा us-central1 होती है.
MODEL_ID मॉडल का आईडी, जो आपको AutoML Cloud API से मिला है.

Python

# 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)

Node.js

// 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);

अपने प्रोजेक्ट के मॉडल की सूची बनाएं

अपने प्रोजेक्ट के मॉडल की सूची बनाई जा सकती है. इसके अलावा, नतीजे फ़िल्टर करने का विकल्प भी चुना जा सकता है:

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_model
display_name != example_model

experimental_ प्रीफ़िक्स वाले सभी डिसप्ले नेम:

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 मॉडल फ़ाइल:

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. (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)

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);