কাস্টম মডেল স্থাপন এবং পরিচালনা করুন

আপনি Firebase কনসোল বা Firebase Admin Python এবং Node.js SDK ব্যবহার করে কাস্টম মডেল এবং AutoML-প্রশিক্ষিত মডেল স্থাপন ও পরিচালনা করতে পারেন। If you just want to deploy a model and occasionally update it, it's usually simplest to use the Firebase console. The Admin SDK can be helpful when integrating with build pipelines, working with Colab or Jupyter notebooks, and other workflows.

Firebase কনসোলে মডেল স্থাপন এবং পরিচালনা করুন

টেনসরফ্লো লাইট মডেল

Firebase কনসোল ব্যবহার করে একটি টেনসরফ্লো লাইট মডেল স্থাপন করতে:

  1. Firebase কনসোলে Firebase ML কাস্টম মডেল পৃষ্ঠাটি খুলুন।
  2. কাস্টম মডেল যোগ করুন ক্লিক করুন (বা অন্য মডেল যোগ করুন )।
  3. আপনার ফায়ারবেস প্রকল্পে আপনার মডেল সনাক্ত করতে ব্যবহৃত হবে এমন একটি নাম উল্লেখ করুন, তারপরে টেনসরফ্লো লাইট মডেল ফাইলটি আপলোড করুন (সাধারণত .tflite বা .lite এ শেষ হয়)।

আপনি আপনার মডেল স্থাপন করার পরে, আপনি এটি কাস্টম পৃষ্ঠায় খুঁজে পেতে পারেন। সেখান থেকে, আপনি একটি নতুন ফাইলের সাথে মডেল আপডেট করা, মডেলটি ডাউনলোড করা এবং আপনার প্রকল্প থেকে মডেলটি মুছে ফেলার মতো কাজগুলি সম্পূর্ণ করতে পারেন।

ফায়ারবেস অ্যাডমিন এসডিকে দিয়ে মডেলগুলি স্থাপন করুন এবং পরিচালনা করুন

এই বিভাগটি দেখায় যে আপনি কীভাবে অ্যাডমিন এসডিকে সহ সাধারণ মডেল স্থাপনা এবং পরিচালনার কাজগুলি সম্পূর্ণ করতে পারেন। অতিরিক্ত সহায়তার জন্য পাইথন বা নোড.জেএসের জন্য এসডিকে রেফারেন্স দেখুন।

ব্যবহারের ক্ষেত্রে এসডিকে উদাহরণগুলির জন্য, পাইথন কুইকস্টার্ট নমুনা এবং নোড.জেএস কুইকস্টার্ট নমুনা দেখুন।

আপনি শুরু করার আগে

  1. আপনার যদি ইতিমধ্যে ফায়ারবেস প্রকল্প না থাকে তবে Firebase কনসোলে একটি নতুন প্রকল্প তৈরি করুন। তারপরে, আপনার প্রকল্পটি খুলুন এবং নিম্নলিখিতগুলি করুন:

    1. সেটিংস পৃষ্ঠায়, একটি পরিষেবা অ্যাকাউন্ট তৈরি করুন এবং পরিষেবা অ্যাকাউন্ট কী ফাইলটি ডাউনলোড করুন। এই ফাইলটি সুরক্ষিত রাখুন, যেহেতু এটি আপনার প্রকল্পে প্রশাসকের অ্যাক্সেস দেয়।

    2. স্টোরেজ পৃষ্ঠায়, Cloud Storage সক্ষম করুন। আপনার বালতি নাম নোট নিন.

      আপনার ফায়ারবেস প্রকল্পে যুক্ত করার সময় আপনার সাময়িকভাবে মডেল ফাইলগুলি সঞ্চয় করতে Cloud Storage বালতি দরকার। আপনি যদি ব্লেজ প্ল্যানে থাকেন তবে আপনি এই উদ্দেশ্যে ডিফল্ট ছাড়া অন্য একটি বালতি তৈরি এবং ব্যবহার করতে পারেন।

    3. Firebase ML পৃষ্ঠায়, আপনি যদি এখনও Firebase ML সক্ষম না করে থাকেন তবে শুরু করুন ক্লিক করুন৷

  2. Google APIs কনসোলে , আপনার Firebase প্রকল্প খুলুন এবং Firebase ML API সক্ষম করুন৷

  3. অ্যাডমিন 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',
      })
    

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

মডেল স্থাপন

টেনসরফ্লো লাইট ফাইল

একটি মডেল ফাইল থেকে একটি টেনসরফ্লো লাইট মডেল স্থাপন করতে, এটি আপনার প্রকল্পে আপলোড করুন এবং তারপরে এটি প্রকাশ করুন:

পাইথন

# 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 and Keras models

পাইথন এসডিকে দিয়ে, আপনি টেনসরফ্লো সেভড মডেল ফর্ম্যাট থেকে একটি মডেলকে টেনসরফ্লো লাইটে রূপান্তর করতে পারেন এবং এটি একক পদক্ষেপে আপনার Cloud Storage বালতিতে আপলোড করতে পারেন। তারপরে, আপনি যেভাবে একটি টেনসরফ্লো লাইট ফাইল স্থাপন করেন সেভাবে এটি স্থাপন করুন।

পাইথন

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

If you have a Keras model, you can also convert it to TensorFlow Lite and upload it in a single step. You can use a Keras model saved to an HDF5 file:

পাইথন

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

Or, you can convert and upload a Keras model straight from your training script:

পাইথন

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 models

আপনি যদি অটোমেল ক্লাউড এপিআই বা Google Cloud কনসোল ইউআইয়ের সাথে একটি এজ মডেল প্রশিক্ষণ দেন তবে আপনি অ্যাডমিন এসডিকে ব্যবহার করে মডেলটি ফায়ারবেসে স্থাপন করতে পারেন।

You will need to specify the model's resource identifier, which is a string that looks like the following example:

projects/PROJECT_NUMBER/locations/STORAGE_LOCATION/models/MODEL_ID
PROJECT_NUMBER মডেল রয়েছে এমন Cloud Storage বালতি প্রকল্পের সংখ্যা। এটি আপনার ফায়ারবেস প্রকল্প বা অন্য কোনও Google Cloud প্রকল্প হতে পারে। আপনি Firebase কনসোল বা Google Cloud কনসোল ড্যাশবোর্ডের সেটিংস পৃষ্ঠায় এই মানটি খুঁজে পেতে পারেন।
STORAGE_LOCATION Cloud Storage বালতিটির রিসোর্স অবস্থান যাতে মডেল রয়েছে। এই মান সবসময় 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)

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

আপনার প্রকল্পের মডেল তালিকা

আপনি আপনার প্রকল্পের মডেলগুলি তালিকাবদ্ধ করতে পারেন, plason চ্ছিকভাবে ফলাফলগুলি ফিল্টার করে:

পাইথন

# 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

অপারেটর এবং NOT ( ( , ) ) এর সাথে ফিল্টারগুলি একত্রিত করুন OR AND ,))।

আপডেট মডেল

আপনি আপনার প্রকল্পে একটি মডেল যোগ করার পরে, আপনি তার প্রদর্শন নাম, ট্যাগ এবং 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)

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

Unpublish or delete models

একটি মডেল অপ্রকাশিত বা মুছে ফেলতে, অপ্রকাশিত বা মুছে ফেলার পদ্ধতিতে মডেল আইডি পাস করুন। আপনি যখন একটি মডেল অপ্রকাশিত করেন, তখন এটি আপনার প্রজেক্টে থেকে যায়, কিন্তু আপনার অ্যাপ্লিকেশানগুলি ডাউনলোড করার জন্য উপলব্ধ নয়৷ When you delete a model, it's completely removed from your project. (কোনও মডেলকে অপ্রকাশিত করা কোনও স্ট্যান্ডার্ড ওয়ার্কফ্লোতে প্রত্যাশিত নয়, তবে আপনি এটি দুর্ঘটনাক্রমে প্রকাশিত একটি নতুন মডেলকে অবিলম্বে অপ্রকাশিত করতে ব্যবহার করতে পারেন এবং এখনও কোথাও ব্যবহার করা হচ্ছে না, বা এমন ক্ষেত্রে যেখানে ব্যবহারকারীদের পক্ষে "খারাপ" ডাউনলোড করা খারাপ এটি খারাপ। model than to get model-not-found errors.)

যদি আপনার কাছে এখনও মডেল অবজেক্টের একটি রেফারেন্স না থাকে, তাহলে আপনাকে সম্ভবত একটি ফিল্টারের সাথে আপনার প্রকল্পের মডেলগুলি তালিকাভুক্ত করে মডেল আইডি পেতে হবে। উদাহরণস্বরূপ, "ফেস_ডেটেক্টর" ট্যাগযুক্ত সমস্ত মডেল মুছতে:

পাইথন

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