Catch up on highlights from Firebase at Google I/O 2023. Learn more

कस्टम मॉडल तैनात और प्रबंधित करें

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

Firebase कंसोल में मॉडलों को परिनियोजित और प्रबंधित करें

टेंसरफ्लो लाइट मॉडल

Firebase कंसोल का उपयोग करके TensorFlow Lite मॉडल परिनियोजित करने के लिए:

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

अपने मॉडल को लागू करने के बाद, आप इसे कस्टम पेज पर पा सकते हैं। वहां से, आप मॉडल को एक नई फ़ाइल के साथ अपडेट करने, मॉडल को डाउनलोड करने और अपने प्रोजेक्ट से मॉडल को हटाने जैसे कार्यों को पूरा कर सकते हैं।

Firebase Admin SDK के साथ मॉडलों को परिनियोजित और प्रबंधित करें

यह अनुभाग दिखाता है कि आप व्यवस्थापक SDK के साथ सामान्य मॉडल परिनियोजन और प्रबंधन कार्यों को कैसे पूरा कर सकते हैं। अतिरिक्त सहायता के लिए Python या Node.js के लिए SDK संदर्भ देखें।

उपयोग में आने वाले SDK के उदाहरणों के लिए, Python QuickStart नमूना और Node.js QuickStart नमूना देखें।

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

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

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

    2. स्टोरेज पेज पर, क्लाउड स्टोरेज को सक्षम करें। अपने बकेट के नाम पर ध्यान दें।

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

    3. अगर आपने अभी तक Firebase ML को सक्षम नहीं किया है, तो Firebase ML पृष्ठ पर, प्रारंभ करें पर क्लिक करें।

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

  3. एडमिन SDK को इंस्टॉल और इनिशियलाइज़ करें

    जब आप SDK को इनिशियलाइज़ करते हैं, तो अपने सर्विस अकाउंट क्रेडेंशियल्स और क्लाउड स्टोरेज बकेट निर्दिष्ट करें जिसका उपयोग आप अपने मॉडल को स्टोर करने के लिए करना चाहते हैं:

    अजगर

    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 मॉडल को परिनियोजित करने के लिए, इसे अपने प्रोजेक्ट पर अपलोड करें और फिर इसे प्रकाशित करें:

अजगर

# 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 मॉडल

पायथन एसडीके के साथ, आप एक मॉडल को TensorFlow सहेजे गए मॉडल प्रारूप से TensorFlow Lite में बदल सकते हैं और इसे एक ही चरण में अपने क्लाउड स्टोरेज बकेट में अपलोड कर सकते हैं। फिर, इसे उसी तरह परिनियोजित करें जैसे आप 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)

यदि आपके पास केरस मॉडल है, तो आप इसे टेंसरफ्लो लाइट में भी परिवर्तित कर सकते हैं और इसे एक ही चरण में अपलोड कर सकते हैं। आप HDF5 फ़ाइल में सहेजे गए Keras मॉडल का उपयोग कर सकते हैं:

अजगर

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

या, आप सीधे अपने प्रशिक्षण स्क्रिप्ट से केरस मॉडल को रूपांतरित और अपलोड कर सकते हैं:

अजगर

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 मॉडल

यदि आपने AutoML क्लाउड API या Google क्लाउड कंसोल UI के साथ एज मॉडल को प्रशिक्षित किया है, तो आप एडमिन SDK का उपयोग करके मॉडल को Firebase में परिनियोजित कर सकते हैं।

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

projects/PROJECT_NUMBER/locations/STORAGE_LOCATION/models/MODEL_ID
PROJECT_NUMBER क्लाउड स्टोरेज बकेट का प्रोजेक्ट नंबर जिसमें मॉडल है। यह आपका Firebase प्रोजेक्ट या कोई अन्य Google क्लाउड प्रोजेक्ट हो सकता है। आप यह मान Firebase कंसोल या Google क्लाउड कंसोल डैशबोर्ड के सेटिंग पृष्ठ पर पा सकते हैं।
STORAGE_LOCATION मॉडल वाले क्लाउड स्टोरेज बकेट का संसाधन स्थान। यह मान हमेशा us-central1 है।
MODEL_ID मॉडल की आईडी, जो आपको ऑटोएमएल क्लाउड एपीआई से मिली है।

अजगर

# 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

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 मॉडल फ़ाइल अपडेट कर सकते हैं:

अजगर

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