Imagen を使用して画像をアップスケールする

Vertex AI の Imagen を使用して画像をアップスケールすると、画質を損なうことなく画像のサイズを大きくできます。

imagen-4.0-upscale-preview は、プレビュー版で画像のアップスケーリングをサポートしています。

始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  8. 環境の認証を設定します。

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    REST

    このページの REST API サンプルをローカル開発環境で使用するには、gcloud CLI に指定した認証情報を使用します。

      Google Cloud CLI をインストールします。 インストール後、次のコマンドを実行して Google Cloud CLI を初期化します。

      gcloud init

      外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

    詳細については、 Google Cloud 認証ドキュメントの REST を使用して認証するをご覧ください。

    画像をアップスケールする

    コンソール

    1. Google Cloud コンソールで、[Vertex AI] > [Vertex AI Studio] > [メディアを生成] ページに移動します。

      [メディアを生成] に移動

    2. [Imagen] をクリックします。[メディア プレイグラウンド] ページが表示されます。

    3. [画像をアップロード] をクリックし、アップロードする画像を選択します。

    4. [画像をエクスポート]、[画像をアップスケールもする] の順にクリックします。

    5. スケーリング ファクタを選択して、[エクスポート] をクリックします。

    アップスケールされた画像に電子透かしが自動的に追加されます。 Google Cloud コンソールでは、画像アップスケールの電子透かしを無効にすることはできません。

    REST

    リクエストのデータを使用する前に、次のように置き換えます。

    • REGION: プロジェクトが配置されているリージョン。サポートされているリージョンの詳細については、Vertex AI の生成 AI のロケーションをご覧ください。
    • PROJECT_ID: 実際の Google Cloud プロジェクト ID。
    • BASE64_SUBJECT_IMAGE: Base64 でエンコードされた被写体の画像。
    • ADD_WATERMARK: 省略可能なブール値。透かし入りの画像を有効にするには true に、透かし入りの画像を無効にするには false に設定します。デフォルト値は true です。
    • GCS_IMAGE_PATH: 画像ファイルへの Cloud Storage パス。
    • GCS_OUTPUT_PATH: 生成された出力を保存する Cloud Storage パス。
    • OUTPUT_MIMETYPE: 画像の出力ファイル形式を定義する文字列(省略可)。使用できる値は、"image/png" または "image/jpeg" です。デフォルトは "image/png" です。
    • COMPRESSION_QUALITY: モデルが JPEG 画像に対して保持する詳細レベルを指定する省略可能な整数値。指定できる値の範囲は 0100 です。値が大きいほど、圧縮レベルが高くなります。デフォルトは 75 です。
    • UPSCALE_FACTOR: アップスケールされた画像のスケーリング ファクタ。指定できる値は次のとおりです。
      • "x2"
      • "x3"
      • "x4"

    HTTP メソッドと URL:

    POST https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/imagen-4.0-upscale-preview:predict

    リクエストの本文(JSON):

    {
      "instances": [
        {
          "image": {
            // use one of the following to specify the image to upscale
            "bytesBase64Encoded": "BASE64_SUBJECT_IMAGE"
            "gcsUri": "GCS_IMAGE_PATH"
            // end of base image input options
          },
        }
      ],
      "parameters": {
        "addWatermark": ADD_WATERMARK, // Optional
        "storageUri": "GCS_OUTPUT_PATH",
        "outputOptions": {  // Optional
          "mimeType": "OUTPUT_MIMETYPE",  // Optional
          "compressionQuality": COMPRESSION_QUALITY // Optional
        },
        "upscaleConfig": {
          "upscaleFactor": "UPSCALE_FACTOR"
        }
      }
    }
    

    リクエストを送信するには、次のいずれかのオプションを選択します。

    curl

    リクエスト本文を request.json という名前のファイルに保存して、次のコマンドを実行します。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/imagen-4.0-upscale-preview:predict"

    PowerShell

    リクエスト本文を request.json という名前のファイルに保存して、次のコマンドを実行します。

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/imagen-4.0-upscale-preview:predict" | Select-Object -Expand Content
    リクエストは画像オブジェクトを返します。この例では、画像オブジェクトが base64 でエンコードされた画像を含む予測オブジェクトとして返されます。
    {
      "predictions": [
        {
          "mimeType": "image/png",
          "bytesBase64Encoded": "BASE64_IMG_BYTES"
        }
      ]
    }
    
    
    

    次のステップ

    Imagen や Vertex AI のその他の生成 AI プロダクトに関する次の記事を読む。