Vertex AI এবং BigQuery ব্যবহার করে ছবির শ্রেণীবিভাগ শুরু করা

এই গাইডটি জেমিনি 2.5 ফ্ল্যাশ সহ Google ক্লাউডের ভার্টেক্স এআই প্ল্যাটফর্ম ব্যবহার করে প্রশিক্ষণের মডেল এবং চিত্রের সম্পদ শ্রেণীবদ্ধ করার জন্য একটি সম্পূর্ণ এন্ড-টু-এন্ড ওয়ার্কফ্লো প্রদান করে। আপনি ডেটা পুনরুদ্ধারের জন্য BigQuery, অ্যাসেট ম্যানেজমেন্টের জন্য ক্লাউড স্টোরেজ এবং Python Colab পরিবেশে মেশিন লার্নিং ইনফারেন্সের জন্য Vertex AI সংহত করতে শিখবেন।

কনফিগারেশন

কোড নমুনা চালানোর আগে নিম্নলিখিত প্রকল্প-নির্দিষ্ট ভেরিয়েবল সেট করুন:

PROJECT_ID = "PROJECT_ID"
REGION = "REGION "  # e.g., "us-central1"
LOCATION = "LOCATION "  # e.g., "us"
CUSTOMER_ID = "CUSTOMER_ID" # required to subscribe to the dataset

পরিবেশ সেটআপ

Google ক্লাউড পরিষেবাগুলি অ্যাক্সেস করতে প্রয়োজনীয় নির্ভরতা ইনস্টল করুন এবং প্রমাণীকরণ কনফিগার করুন:

# Install Google Cloud SDK dependencies for AI Platform integration
!pip install google-cloud-aiplatform google-cloud-storage google-cloud-bigquery google-cloud-bigquery-data-exchange -q

# Import core libraries for cloud services and machine learning operations
import json
import os
from google.cloud import bigquery
import vertexai
from vertexai.generative_models import GenerativeModel, Part

# Configure authentication for Google Cloud service access
# Initiates OAuth flow in new browser tab if authentication required
from google.colab import auth

if os.environ.get("VERTEX_PRODUCT") != "COLAB_ENTERPRISE":
  from google.colab import auth
  auth.authenticate_user(project_id=PROJECT_ID)

# Initialize Vertex AI client with project configuration
vertexai.init(project=PROJECT_ID, location=REGION)

print(f"Vertex AI initialized for project: {PROJECT_ID} in region: {REGION}")

Analytics হাব ডেটাসেটে সদস্যতা নিন

আপনাকে অবশ্যই Analytics হাব ডেটাসেটে সদস্যতা নিতে হবে।

from google.cloud import bigquery_data_exchange_v1beta1

ah_client = bigquery_data_exchange_v1beta1.AnalyticsHubServiceClient()

HUB_PROJECT_ID = 'maps-platform-analytics-hub'
DATA_EXCHANGE_ID = f"imagery_insights_exchange_{LOCATION}"
LINKED_DATASET_NAME = f"imagery_insights___preview___{LOCATION}"


# subscribe to the listing (create a linked dataset in your consumer project)
destination_dataset = bigquery_data_exchange_v1beta1.DestinationDataset()
destination_dataset.dataset_reference.dataset_id = LINKED_DATASET_NAME
destination_dataset.dataset_reference.project_id = PROJECT_ID
destination_dataset.location = LOCATION
LISTING_ID=f"imagery_insights_{CUSTOMER_ID.replace('-', '_')}__{LOCATION}"

published_listing = f"projects/{HUB_PROJECT_ID}/locations/{LOCATION}/dataExchanges/{DATA_EXCHANGE_ID}/listings/{LISTING_ID}"

request = bigquery_data_exchange_v1beta1.SubscribeListingRequest(
    destination_dataset=destination_dataset,
    name=published_listing,
)

# request the subscription
ah_client.subscribe_listing(request=request)

BigQuery-এর মাধ্যমে ডেটা এক্সট্রাকশন

latest_observations টেবিল থেকে Google ক্লাউড স্টোরেজ ইউআরআই বের করতে একটি BigQuery ক্যোয়ারী চালান। এই URI শ্রেণীবিভাগের জন্য সরাসরি Vertex AI মডেলে পাঠানো হবে।

# Initialize BigQuery client
bigquery_client = bigquery.Client(project=PROJECT_ID)

# Define SQL query to retrieve observation records from imagery dataset
query = f"""
SELECT
 *
FROM
 `{PROJECT_ID}.imagery_insights___preview___{LOCATION}.latest_observations`
LIMIT 10;
"""

print(f"Executing BigQuery query:\n{query}")

# Submit query job to BigQuery service and await completion
query_job = bigquery_client.query(query)

# Transform query results into structured data format for downstream processing
# Convert BigQuery Row objects to dictionary representations for enhanced accessibility
query_response_data = []
for row in query_job:
   query_response_data.append(dict(row))

# Extract Cloud Storage URIs from result set, filtering null values
gcs_uris = [item.get("gcs_uri") for item in query_response_data if item.get("gcs_uri")]

print(f"BigQuery query returned {len(query_response_data)} records.")
print(f"Extracted {len(gcs_uris)} GCS URIs:")
for uri in gcs_uris:
   print(uri)

চিত্র শ্রেণীবিভাগ ফাংশন

এই হেল্পার ফাংশনটি Vertex AI এর Gemini 2.5 Flash মডেল ব্যবহার করে ছবির শ্রেণীবিভাগ পরিচালনা করে:

def classify_image_with_gemini(gcs_uri: str, prompt: str = "What is in this image?") -> str:
   """
   Performs multimodal image classification using Vertex AI's Gemini 2.5 Flash model.

   Leverages direct Cloud Storage integration to process image assets without local
   download requirements, enabling scalable batch processing workflows.

   Args:
       gcs_uri (str): Fully qualified Google Cloud Storage URI 
                     (format: gs://bucket-name/path/to/image.jpg)
       prompt (str): Natural language instruction for classification task execution

   Returns:
       str: Generated textual description from the generative model, or error message
            if classification pipeline fails

   Raises:
       Exception: Captures service-level errors and returns structured failure response
   """
   try:
       # Instantiate Gemini 2.5 Flash model for inference operations
       model = GenerativeModel("gemini-2.5-flash")

       # Construct multimodal Part object from Cloud Storage reference
       # Note: MIME type may need dynamic inference for mixed image formats
       image_part = Part.from_uri(uri=gcs_uri, mime_type="image/jpeg")

       # Execute multimodal inference request with combined visual and textual inputs
       responses = model.generate_content([image_part, prompt])
       return responses.text
   except Exception as e:
       print(f"Error classifying image from URI {gcs_uri}: {e}")
       return "Classification failed."

ব্যাচ ইমেজ শ্রেণীবিভাগ

সমস্ত নিষ্কাশিত ইউআরআই প্রক্রিয়া করুন এবং শ্রেণীবিভাগ তৈরি করুন:

classification_results = []

# Execute batch classification pipeline across all extracted GCS URIs
for uri in gcs_uris:
   print(f"\nProcessing: {uri}")

   # Define comprehensive classification prompt for detailed feature extraction
   classification_prompt = "Describe this image in detail, focusing on any objects, signs, or features visible."

   # Invoke Gemini model for multimodal inference on current asset
   result = classify_image_with_gemini(uri, classification_prompt)

   # Aggregate structured results for downstream analytics and reporting
   classification_results.append({"gcs_uri": uri, "classification": result})

   print(f"Classification for {uri}:\n{result}")

পরবর্তী পদক্ষেপ

আপনার ছবি শ্রেণীবদ্ধ করে, এই উন্নত কর্মপ্রবাহ বিবেচনা করুন:

  • মডেল ফাইন-টিউনিং : কাস্টম মডেল প্রশিক্ষণের জন্য শ্রেণীবিভাগ ফলাফল ব্যবহার করুন।
  • স্বয়ংক্রিয় প্রক্রিয়াকরণ : নতুন ছবি স্বয়ংক্রিয়ভাবে শ্রেণীবদ্ধ করতে ক্লাউড ফাংশন সেট আপ করুন।
  • ডেটা বিশ্লেষণ : শ্রেণীবিভাগের ধরণগুলিতে পরিসংখ্যানগত বিশ্লেষণ সম্পাদন করুন।
  • ইন্টিগ্রেশন : ডাউনস্ট্রিম অ্যাপ্লিকেশনের সাথে ফলাফল সংযুক্ত করুন।

সমস্যা সমাধান

সাধারণ সমস্যা এবং সমাধান:

  • প্রমাণীকরণ ত্রুটি : যথাযথ IAM ভূমিকা এবং API সক্ষমতা নিশ্চিত করুন।
  • হার সীমিত করা : বড় ব্যাচের জন্য সূচকীয় ব্যাকঅফ প্রয়োগ করুন।
  • মেমরির সীমাবদ্ধতা : বড় ডেটাসেটের জন্য ছোট ব্যাচে ছবি প্রসেস করুন।
  • URI ফরম্যাট ত্রুটি : GCS URI গুলি gs://bucket-name/path/to/image ফর্ম্যাট অনুসরণ করে যাচাই করুন।

অতিরিক্ত সহায়তার জন্য, Vertex AI ডকুমেন্টেশন এবং BigQuery ডকুমেন্টেশন দেখুন।