science このプロダクトまたは機能は、pre-GA(一般提供前)のプレビュー版です。一般提供前のプロダクトと機能では、サポートが制限されることがあります。また、一般提供前のプロダクトや機能の変更は、他の一般提供前のバージョンと互換性がない可能性があります。pre-GA のサービスには、Google Maps Platform Service Specific Terms が適用されます。詳細については、リリース ステージの説明をご覧ください。
このガイドでは、Gemini 2.5 Flash を使用して Google Cloud の Vertex AI Platform でモデルをトレーニングし、画像アセットを分類するための完全なエンドツーエンドのワークフローについて説明します。このコースでは、Python Colab 環境で、データ取得に BigQuery を、アセット管理に Cloud Storage を、機械学習推論に 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
# Install Google Cloud SDK dependencies for AI Platform integration!pipinstallgoogle-cloud-aiplatformgoogle-cloud-storagegoogle-cloud-bigquerygoogle-cloud-bigquery-data-exchange-q# Import core libraries for cloud services and machine learning operationsimportjsonimportosfromgoogle.cloudimportbigqueryimportvertexaifromvertexai.generative_modelsimportGenerativeModel,Part# Configure authentication for Google Cloud service access# Initiates OAuth flow in new browser tab if authentication requiredfromgoogle.colabimportauthifos.environ.get("VERTEX_PRODUCT")!="COLAB_ENTERPRISE":fromgoogle.colabimportauthauth.authenticate_user(project_id=PROJECT_ID)# Initialize Vertex AI client with project configurationvertexai.init(project=PROJECT_ID,location=REGION)print(f"Vertex AI initialized for project: {PROJECT_ID} in region: {REGION}")
Analytics Hub データセットを定期購入する
また、Analytics Hub データセットに登録する必要があります。
fromgoogle.cloudimportbigquery_data_exchange_v1beta1ah_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_NAMEdestination_dataset.dataset_reference.project_id=PROJECT_IDdestination_dataset.location=LOCATIONLISTING_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 subscriptionah_client.subscribe_listing(request=request)
BigQuery を使用したデータ抽出
BigQuery クエリを実行して、latest_observations テーブルから Google Cloud Storage URI を抽出します。これらの URI は、分類のために Vertex AI モデルに直接渡されます。
# Initialize BigQuery clientbigquery_client=bigquery.Client(project=PROJECT_ID)# Define SQL query to retrieve observation records from imagery datasetquery=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 completionquery_job=bigquery_client.query(query)# Transform query results into structured data format for downstream processing# Convert BigQuery Row objects to dictionary representations for enhanced accessibilityquery_response_data=[]forrowinquery_job:query_response_data.append(dict(row))# Extract Cloud Storage URIs from result set, filtering null valuesgcs_uris=[item.get("gcs_uri")foriteminquery_response_dataifitem.get("gcs_uri")]print(f"BigQuery query returned {len(query_response_data)} records.")print(f"Extracted {len(gcs_uris)} GCS URIs:")foruriingcs_uris:print(uri)
画像分類関数
このヘルパー関数は、Vertex AI の Gemini 2.5 Flash モデルを使用して画像の分類を処理します。
defclassify_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 operationsmodel=GenerativeModel("gemini-2.5-flash")# Construct multimodal Part object from Cloud Storage reference# Note: MIME type may need dynamic inference for mixed image formatsimage_part=Part.from_uri(uri=gcs_uri,mime_type="image/jpeg")# Execute multimodal inference request with combined visual and textual inputsresponses=model.generate_content([image_part,prompt])returnresponses.textexceptExceptionase:print(f"Error classifying image from URI {gcs_uri}: {e}")return"Classification failed."
Batch Image Classification
抽出されたすべての URI を処理して分類を生成します。
classification_results=[]# Execute batch classification pipeline across all extracted GCS URIsforuriingcs_uris:print(f"\nProcessing: {uri}")# Define comprehensive classification prompt for detailed feature extractionclassification_prompt="Describe this image in detail, focusing on any objects, signs, or features visible."# Invoke Gemini model for multimodal inference on current assetresult=classify_image_with_gemini(uri,classification_prompt)# Aggregate structured results for downstream analytics and reportingclassification_results.append({"gcs_uri":uri,"classification":result})print(f"Classification for {uri}:\n{result}")
次のステップ
画像を分類したら、次の高度なワークフローを検討してください。
モデル ファインチューニング: 分類結果を使用してカスタムモデルをトレーニングします。
自動処理: 新しい画像を自動的に分類するように Cloud Functions を設定します。
データ分析: 分類パターンに対して統計分析を実行します。
インテグレーション: 結果をダウンストリーム アプリケーションに接続します。
トラブルシューティング
一般的な問題と解決策:
認証エラー: 適切な IAM ロールと API の有効化を確認します。
レート制限: 大規模なバッチ処理に指数バックオフを実装します。
メモリ制約: 大規模なデータセットの場合は、画像を小さなバッチで処理します。
URI 形式エラー: GCS URI が gs://bucket-name/path/to/image 形式に従っていることを確認します。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-05 UTC。"],[],[],null,["# Getting Started with Image Classification Using Vertex AI and BigQuery\n\nThis guide provides a complete end-to-end workflow for training models and\nclassifying imagery assets using Google Cloud's Vertex AI platform with Gemini\n2.5 Flash. You'll learn to integrate BigQuery for data retrieval, Cloud Storage\nfor asset management, and Vertex AI for machine learning inference in a\nPython Colab environment.\n| **Important:** Use Colab Enterprise, as its longer [idle shutdown\n| time](https://cloud.google.com/colab/docs/idle-shutdown) is important for training machine learning models.\n\nConfiguration\n-------------\n\nSet the following project-specific variables before running the code samples: \n\n PROJECT_ID = \"\u003cvar label=\"project ID\" translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\"\n REGION = \"\u003cvar label=\"region\" translate=\"no\"\u003eREGION\u003c/var\u003e \" # e.g., \"us-central1\"\n LOCATION = \"\u003cvar label=\"location\" translate=\"no\"\u003eLOCATION\u003c/var\u003e \" # e.g., \"us\"\n CUSTOMER_ID = \"\u003cvar label=\"customer ID\" translate=\"no\"\u003eCUSTOMER_ID\u003c/var\u003e\" # required to subscribe to the dataset\n\nEnvironment Setup\n-----------------\n\nInstall required dependencies and configure authentication to access Google\nCloud services: \n\n # Install Google Cloud SDK dependencies for AI Platform integration\n !pip install google-cloud-aiplatform google-cloud-storage google-cloud-bigquery google-cloud-bigquery-data-exchange -q\n\n # Import core libraries for cloud services and machine learning operations\n import json\n import os\n from google.cloud import bigquery\n import vertexai\n from vertexai.generative_models import GenerativeModel, Part\n\n # Configure authentication for Google Cloud service access\n # Initiates OAuth flow in new browser tab if authentication required\n from google.colab import auth\n\n if os.environ.get(\"VERTEX_PRODUCT\") != \"COLAB_ENTERPRISE\":\n from google.colab import auth\n auth.authenticate_user(project_id=PROJECT_ID)\n\n # Initialize Vertex AI client with project configuration\n vertexai.init(project=PROJECT_ID, location=REGION)\n\n print(f\"Vertex AI initialized for project: {PROJECT_ID} in region: {REGION}\")\n\nSubscribe to the Analytics Hub dataset\n--------------------------------------\n\nYou must also subscribe to the Analytics Hub dataset. \n\n from google.cloud import bigquery_data_exchange_v1beta1\n\n ah_client = bigquery_data_exchange_v1beta1.AnalyticsHubServiceClient()\n\n HUB_PROJECT_ID = 'maps-platform-analytics-hub'\n DATA_EXCHANGE_ID = f\"imagery_insights_exchange_{LOCATION}\"\n LINKED_DATASET_NAME = f\"imagery_insights___preview___{LOCATION}\"\n\n\n # subscribe to the listing (create a linked dataset in your consumer project)\n destination_dataset = bigquery_data_exchange_v1beta1.DestinationDataset()\n destination_dataset.dataset_reference.dataset_id = LINKED_DATASET_NAME\n destination_dataset.dataset_reference.project_id = PROJECT_ID\n destination_dataset.location = LOCATION\n LISTING_ID=f\"imagery_insights_{CUSTOMER_ID.replace('-', '_')}__{LOCATION}\"\n\n published_listing = f\"projects/{HUB_PROJECT_ID}/locations/{LOCATION}/dataExchanges/{DATA_EXCHANGE_ID}/listings/{LISTING_ID}\"\n\n request = bigquery_data_exchange_v1beta1.SubscribeListingRequest(\n destination_dataset=destination_dataset,\n name=published_listing,\n )\n\n # request the subscription\n ah_client.subscribe_listing(request=request)\n\nData Extraction with BigQuery\n-----------------------------\n\nExecute a BigQuery query to extract Google Cloud Storage URIs from the\n`latest_observations` table. These URIs will be passed directly to the Vertex AI\nmodel for classification.\n**Note:** The model requires GCS URIs rather than downloaded image files. This approach optimizes data transfer and processing efficiency. \n\n # Initialize BigQuery client\n bigquery_client = bigquery.Client(project=PROJECT_ID)\n\n # Define SQL query to retrieve observation records from imagery dataset\n query = f\"\"\"\n SELECT\n *\n FROM\n `{PROJECT_ID}.imagery_insights___preview___{LOCATION}.latest_observations`\n LIMIT 10;\n \"\"\"\n\n print(f\"Executing BigQuery query:\\n{query}\")\n\n # Submit query job to BigQuery service and await completion\n query_job = bigquery_client.query(query)\n\n # Transform query results into structured data format for downstream processing\n # Convert BigQuery Row objects to dictionary representations for enhanced accessibility\n query_response_data = []\n for row in query_job:\n query_response_data.append(dict(row))\n\n # Extract Cloud Storage URIs from result set, filtering null values\n gcs_uris = [item.get(\"gcs_uri\") for item in query_response_data if item.get(\"gcs_uri\")]\n\n print(f\"BigQuery query returned {len(query_response_data)} records.\")\n print(f\"Extracted {len(gcs_uris)} GCS URIs:\")\n for uri in gcs_uris:\n print(uri)\n\nImage Classification Function\n-----------------------------\n\nThis helper function handles the classification of images using Vertex AI's\nGemini 2.5 Flash model: \n\n def classify_image_with_gemini(gcs_uri: str, prompt: str = \"What is in this image?\") -\u003e str:\n \"\"\"\n Performs multimodal image classification using Vertex AI's Gemini 2.5 Flash model.\n\n Leverages direct Cloud Storage integration to process image assets without local\n download requirements, enabling scalable batch processing workflows.\n\n Args:\n gcs_uri (str): Fully qualified Google Cloud Storage URI \n (format: gs://bucket-name/path/to/image.jpg)\n prompt (str): Natural language instruction for classification task execution\n\n Returns:\n str: Generated textual description from the generative model, or error message\n if classification pipeline fails\n\n Raises:\n Exception: Captures service-level errors and returns structured failure response\n \"\"\"\n try:\n # Instantiate Gemini 2.5 Flash model for inference operations\n model = GenerativeModel(\"gemini-2.5-flash\")\n\n # Construct multimodal Part object from Cloud Storage reference\n # Note: MIME type may need dynamic inference for mixed image formats\n image_part = Part.from_uri(uri=gcs_uri, mime_type=\"image/jpeg\")\n\n # Execute multimodal inference request with combined visual and textual inputs\n responses = model.generate_content([image_part, prompt])\n return responses.text\n except Exception as e:\n print(f\"Error classifying image from URI {gcs_uri}: {e}\")\n return \"Classification failed.\"\n\nBatch Image Classification\n--------------------------\n\nProcess all extracted URIs and generate classifications: \n\n classification_results = []\n\n # Execute batch classification pipeline across all extracted GCS URIs\n for uri in gcs_uris:\n print(f\"\\nProcessing: {uri}\")\n\n # Define comprehensive classification prompt for detailed feature extraction\n classification_prompt = \"Describe this image in detail, focusing on any objects, signs, or features visible.\"\n\n # Invoke Gemini model for multimodal inference on current asset\n result = classify_image_with_gemini(uri, classification_prompt)\n\n # Aggregate structured results for downstream analytics and reporting\n classification_results.append({\"gcs_uri\": uri, \"classification\": result})\n\n print(f\"Classification for {uri}:\\n{result}\")\n\nNext Steps\n----------\n\nWith your images classified, consider these advanced workflows:\n\n- **Model Fine-tuning**: Use classification results to train custom models.\n- **Automated Processing**: Set up Cloud Functions to classify new images automatically.\n- **Data Analysis**: Perform statistical analysis on classification patterns.\n- **Integration**: Connect results to downstream applications.\n\nTroubleshooting\n---------------\n\nCommon issues and solutions:\n\n- **Authentication errors**: Ensure proper IAM roles and API enablement.\n- **Rate limiting**: Implement exponential backoff for large batches.\n- **Memory constraints**: Process images in smaller batches for large datasets.\n- **URI format errors** : Verify GCS URIs follow the format `gs://bucket-name/path/to/image`.\n\nFor additional support, refer to the [Vertex AI\ndocumentation](https://cloud.google.com/vertex-ai/docs) and [BigQuery\ndocumentation](https://cloud.google.com/bigquery/docs)."]]