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

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

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

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

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

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

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

फायरबेस एडमिन एसडीके के साथ मॉडल तैनात और प्रबंधित करें

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

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

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

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

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

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

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

    3. यदि आपने अभी तक फायरबेस एमएल सक्षम नहीं किया है तो फायरबेस एमएल पृष्ठ पर आरंभ करें पर क्लिक करें।

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

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

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

    अजगर

    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 लाइट फ़ाइलें

एक मॉडल फ़ाइल से 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 सहेजे गए मॉडल प्रारूप से 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 फ़ाइल में सहेजे गए केरस मॉडल का उपयोग कर सकते हैं:

अजगर

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

ऑटोएमएल टेन्सरफ्लो लाइट मॉडल

यदि आपने एज मॉडल को ऑटोएमएल क्लाउड एपीआई या Google क्लाउड कंसोल यूआई के साथ प्रशिक्षित किया है, तो आप एडमिन एसडीके का उपयोग करके मॉडल को फायरबेस पर तैनात कर सकते हैं।

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

projects/PROJECT_NUMBER/locations/STORAGE_LOCATION/models/MODEL_ID
PROJECT_NUMBER क्लाउड स्टोरेज बकेट का प्रोजेक्ट नंबर जिसमें मॉडल शामिल है। यह आपका फायरबेस प्रोजेक्ट या कोई अन्य Google क्लाउड प्रोजेक्ट हो सकता है। आप यह मान फ़ायरबेस कंसोल या 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);