使用 Imagen 提高图片分辨率

您可以使用 Imagen on Vertex AI 来放大图片,这样可以增加图片的大小,而不会降低图片质量。

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

      如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 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 图片保留的细节级别。接受的值范围为:0 - 100。值越高,表示压缩级别越高。默认值为 75
    • UPSCALE_FACTOR:放大后图片的缩放比例。应用放大系数后,图片的最终输出分辨率不得超过 1,700 万像素。接受以下值:
      • "x2"
      • "x3"
      • "x4"

    HTTP 方法和网址:

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

    请求 JSON 正文:

    {
      "instances": [
        {
          "prompt": "Upscale the image",
          "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": {
        "mode": "upscale",
        "storageUri": "GCS_OUTPUT_PATH",
        "outputOptions": {
          "mimeType": "OUTPUT_MIMETYPE",
          "compressionQuality": COMPRESSION_QUALITY
        },
        "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 产品的文章: