Android 圖片產生指南

,瞭解如何調查及移除這項存取權。

MediaPipe 圖片產生器工作可讓您根據文字提示生成圖片。這個 工作會運用文字轉圖片模型,以擴散技術產生圖片。

這項工作接受文字提示做為輸入內容,以及選用的條件圖片 模型可以擴增並在生成時參考圖片產生器 也能根據訓練期間提供給模型的具體概念 訓練、訓練或重新訓練詳情請參閱自訂 LoRA

如需上述指示中所述的程式碼範例,請前往 GitHub。 進一步瞭解功能、模型和設定選項 請參閱總覽

程式碼範例

MediaPipe 工作範例程式碼是圖片產生器的基本實作 App Engine 應用程式你可以將這款應用程式做為自己 Android 的起點 或是修改現有應用程式時參考。圖片產生器範例 程式碼 GitHub

下載程式碼

以下說明如何建立範例的本機副本 git 指令列工具編寫程式碼。

如要下載範例程式碼,請按照下列步驟操作:

  1. 使用下列指令複製 git 存放區:
    git clone https://github.com/google-ai-edge/mediapipe-samples
    
  2. 您也可以設定 Git 執行個體來使用稀疏結帳功能, 僅限圖片產生器範例應用程式的檔案:
    cd mediapipe
    git sparse-checkout init --cone
    git sparse-checkout set examples/image_generator/android
    

建立範例程式碼的本機版本後,您可以匯入專案 然後執行應用程式如需操作說明,請參閱 Android

重要元件

下列檔案包含這個圖片生成範例的重要程式碼 應用程式:

設定

本節說明設定開發環境的重要步驟,以及 專門使用 Image Generator 的程式碼專案如需 設定開發環境以使用 MediaPipe 工作,包括: 平台版本需求,請參閱這份指南 Android

依附元件

圖片產生器工作會使用 com.google.mediapipe:tasks-vision-image-generator 程式庫。新增這個依附元件 新增到 Android 應用程式的 build.gradle 檔案:

dependencies {
    implementation 'com.google.mediapipe:tasks-vision-image-generator:latest.release'
}

如果是搭載 Android 12 (API 31) 以上版本的裝置,請新增原生 OpenCL 程式庫 AndroidManifest.xml 的依附元件。詳情請參閱 uses-native-library敬上 標記之前。

部分 Android 裝置可能還需要額外程式庫:

<uses-native-library android:name="libOpenCL.so" android:required="false" />
<uses-native-library android:name="libOpenCL-car.so" android:required="false"/>
<uses-native-library android:name="libOpenCL-pixel.so" android:required="false" />

型號

MediaPipe 圖片產生器工作需要經過訓練且彼此相容的基礎模型, 透過這項工作下載模型後,請安裝必要的依附元件並 將多個模型轉換為合適的格式。再將已轉換的 安裝於 Android 裝置

如要進一步瞭解圖片產生器可用的已訓練模型,請參閱工作 總覽的「模型」一節

下載基礎模型

圖像產生器中的基礎模型必須與 runwayml/stable-diffusion-v1-5 EMA-only 模型格式,依據以下項目 模型: runwayml/stable-diffusion-v1-5.

安裝依附元件並轉換模型

$ pip install torch typing_extensions numpy Pillow requests pytorch_lightning absl-py

執行 convert.py敬上 指令碼:

$ python3 convert.py --ckpt_path <ckpt_path> --output_path <output_path>

將已轉換的模型推送至裝置

<output_path> 資料夾的內容推送至 Android 裝置。

$ adb shell rm -r /data/local/tmp/image_generator/ # Remove any previously loaded weights
$ adb shell mkdir -p /data/local/tmp/image_generator/
$ adb push <output_path>/. /data/local/tmp/image_generator/bins

下載外掛程式模型並新增 LoRA 權重 (選用)

如果您想使用外掛程式模型,請確認模型 。若是需要其他模型的外掛程式,外掛程式 模型必須封裝在 APK 中,或是隨選下載。外掛程式模型 是輕量級 (約 23MB),且可直接附在 APK 中。不過,我們 建議按需求下載外掛程式模型

如果已自訂模型 LoRA 隨選下載。如要 相關資訊,請參閱 LoRA 權重外掛程式模型

建立工作

MediaPipe 圖片產生器工作會使用 createFromOptions() 函式來設定 工作。createFromOptions() 函式可接受設定值 只要設定成「自動重新啟動」 和「在主機維護期間」選項即可如要進一步瞭解設定選項,請參閱設定 選項

設定選項

這項工作有下列 Android 應用程式設定選項:

選項名稱 說明 值範圍
imageGeneratorModelDirectory 儲存模型權重的圖片產生器模型目錄。 PATH
loraWeightsFilePath 設定 LoRA 權重檔案的路徑。選用,僅適用於 我們使用 LoRA 自訂了模型 PATH
errorListener 設定選用的錯誤事件監聽器。 N/A

這項工作也支援外掛程式模型,可讓使用者加入條件圖片 基礎模型可以擴增並做為參考 生成圖像這些狀況圖片可以是臉部地標、邊緣輪廓, 預估深度預估結果,模型會參考這些資料做為額外情境和資訊 生成圖像

在基礎模型中新增外掛程式模型時,請一併設定外掛程式 只要設定成「自動重新啟動」 和「在主機維護期間」選項即可臉部地標外掛程式使用 Canny 邊緣的 faceConditionOptions 外掛程式會使用 edgeConditionOptions,而 Depth 外掛程式會使用 depthConditionOptions

Canny 邊緣選項

edgeConditionOptions 中設定下列選項。

選項名稱 說明 值範圍 預設值
threshold1 混合程序的第一個門檻。 Float 100
threshold2 水療程序的第二個門檻。 Float 200
apertureSize Sobel 運算子的光圈大小。範圍通常介於 3 至 7 之間。 Integer 3
l2Gradient 無論是否使用 L2 正規數來計算圖片漸層大小 而不是預設的 L1 常式 BOOLEAN False
EdgePluginModelBaseOptions 設定路徑的 BaseOptions 物件 外掛程式和外掛程式 BaseOptions 個物件 N/A

如要進一步瞭解這些設定選項的運作方式,請參閱: Canny 邊緣偵測工具

臉孔地標選項

faceConditionOptions 中設定下列選項。

選項名稱 說明 值範圍 預設值
minFaceDetectionConfidence 臉部偵測的最低可信度分數 。 Float [0.0,1.0] 0.5
minFacePresenceConfidence 呈現臉孔的最低可信度分數 臉部地標偵測結果傳回值。 Float [0.0,1.0] 0.5
faceModelBaseOptions 設定路徑的 BaseOptions 物件 來建立條件圖片的模型 BaseOptions 個物件 N/A
FacePluginModelBaseOptions 設定路徑的 BaseOptions 物件 外掛程式和外掛程式 BaseOptions 個物件 N/A

如要進一步瞭解這些設定選項的運作方式,請參閱 臉部地標工作

深度選項

depthConditionOptions 中設定下列選項。

選項名稱 說明 值範圍 預設值
depthModelBaseOptions 設定路徑的 BaseOptions 物件 來建立條件圖片的模型 BaseOptions 個物件 N/A
depthPluginModelBaseOptions 設定路徑的 BaseOptions 物件 外掛程式和外掛程式 BaseOptions 個物件 N/A

只使用基礎模型建立

val options = ImageGeneratorOptions.builder()
    .setImageGeneratorModelDirectory(modelPath)
    .build()

imageGenerator = ImageGenerator.createFromOptions(context, options)

使用外掛程式建立

如要套用選用的外掛程式模型,請設定基本選項 使用 setPluginModelBaseOptions 的外掛程式模型。如果外掛程式模型需要 用於建立條件映像檔的其他下載模型,請在 BaseOptions

臉部地標

val options = ImageGeneratorOptions.builder()
    .setImageGeneratorModelDirectory(modelPath)
    .build()

val faceModelBaseOptions = BaseOptions.builder()
    .setModelAssetPath("face_landmarker.task")
    .build()

val facePluginModelBaseOptions = BaseOptions.builder()
    .setModelAssetPath("face_landmark_plugin.tflite")
    .build()

val faceConditionOptions = FaceConditionOptions.builder()
    .setFaceModelBaseOptions(faceModelBaseOptions)
    .setPluginModelBaseOptions(facePluginModelBaseOptions)
    .setMinFaceDetectionConfidence(0.3f)
    .setMinFacePresenceConfidence(0.3f)
    .build()

val conditionOptions = ConditionOptions.builder()
    .setFaceConditionOptions(faceConditionOptions)
    .build()

imageGenerator =
    ImageGenerator.createFromOptions(context, options, conditionOptions)
    

Canny Edge

val options = ImageGeneratorOptions.builder()
    .setImageGeneratorModelDirectory(modelPath)
    .build()

val edgePluginModelBaseOptions = BaseOptions.builder()
    .setModelAssetPath("canny_edge_plugin.tflite")
    .build()

val edgeConditionOptions = EdgeConditionOptions.builder()
    .setThreshold1(100.0f)
    .setThreshold2(100.0f)
    .setApertureSize(3)
    .setL2Gradient(false)
    .setPluginModelBaseOptions(edgePluginModelBaseOptions)
    .build()

val conditionOptions = ConditionOptions.builder()
    .setEdgeConditionOptions(edgeConditionOptions)
    .build()

imageGenerator =
    ImageGenerator.createFromOptions(context, options, conditionOptions)
    

厚度

val options = ImageGeneratorOptions.builder()
    .setImageGeneratorModelDirectory(modelPath)
    .build()

val depthModelBaseOptions = BaseOptions.builder()
    .setModelAssetPath("depth_model.tflite")
    .build()

val depthPluginModelBaseOptions = BaseOptions.builder()
    .setModelAssetPath("depth_plugin.tflite")
    .build()

val depthConditionOptions =
    ConditionOptions.DepthConditionOptions.builder()
        .setDepthModelBaseOptions(depthModelBaseOptions)
        .setPluginModelBaseOptions(depthPluginModelBaseOptions)
        .build()

val conditionOptions = ConditionOptions.builder()
    .setDepthConditionOptions(depthConditionOptions)
    .build()

imageGenerator =
    ImageGenerator.createFromOptions(context, options, conditionOptions)
    

使用 LoRA 權重建立

如果要加入 LoRA 權重,請使用 loraWeightsFilePath 參數 指向路徑位置

val options = ImageGeneratorOptions.builder()
    .setLoraWeightsFilePath(weightsPath)
    .setImageGeneratorModelDirectory(modelPath)
    .build()

imageGenerator = ImageGenerator.createFromOptions(context, options)

準備資料

圖片產生器接受下列輸入內容:

  • prompt (必要):描述要產生圖片的文字提示。
  • iterations (必填):產生圖片的疊代總數。A 罩杯 很好的起點是 20
  • seed (必要):圖片產生時使用的隨機種子。
  • condition_image (選用):模型做為參考用的圖片 。只有在使用外掛程式模型時適用。
  • condition type (選用):與工作搭配使用的外掛程式模型類型。 只有在使用外掛程式模型時適用。

僅包含基礎模型的輸入內容

fun setInput(prompt: String, iteration: Int, seed: Int) {
    imageGenerator.setInputs(prompt, iteration, seed)
}

使用外掛程式輸入

如果套用選用的外掛程式模型,請一併使用 conditionType 參數以便選擇外掛程式模型,而 sourceConditionImage 參數要設為 生成條件圖片

選項名稱 說明
conditionType 套用至基礎模型的外掛程式模型。 {"FACE", "EDGE", "DEPTH"}
sourceConditionImage 用於建立條件映像檔的來源映像檔。 MPImage 個物件

如果您使用的是外掛程式模型,請使用 createConditionImage 建立 條件圖片:

fun createConditionImage(
    inputImage: MPImage,
    conditionType: ConditionType
): Bitmap {
    val result =
        imageGenerator.createConditionImage(inputImage, conditionType)
    return BitmapExtractor.extract(result)
}

建立條件圖片後,請連同 模型提示、種子和疊代次數

imageGenerator.setInputs(
    prompt,
    conditionalImage,
    conditionType,
    iteration,
    seed
)

採用 LoRA 權重的輸入內容

如果您使用的是 LoRA 權重,請務必在以下情況發生時,確認符記出現在文字提示中: 訓練圖片中 權重

fun setInput(prompt: String, iteration: Int, seed: Int) {
    imageGenerator.setInputs(prompt, iteration, seed)
}

執行工作

使用 generate() 方法,即可利用 請參閱上一節的說明這會產生一張生成的圖片。

僅使用基礎模型產生

fun generate(prompt: String, iteration: Int, seed: Int): Bitmap {
    val result = imageGenerator.generate(prompt, iteration, seed)
    val bitmap = BitmapExtractor.extract(result?.generatedImage())
    return bitmap
}

使用外掛程式產生

fun generate(
    prompt: String,
    inputImage: MPImage,
    conditionType: ConditionType,
    iteration: Int,
    seed: Int
): Bitmap {
    val result = imageGenerator.generate(
        prompt,
        inputImage,
        conditionType,
        iteration,
        seed
    )
    val bitmap = BitmapExtractor.extract(result?.generatedImage())
    return bitmap
}

使用 LoRA 權重產生

使用以 LoRA 權重自訂的模型產生圖片的過程 類似於標準基礎模型的流程確認權杖 並執行相同程式碼

fun generate(prompt: String, iteration: Int, seed: Int): Bitmap {
    val result = imageGenerator.generate(prompt, iteration, seed)
    val bitmap = BitmapExtractor.extract(result?.generatedImage())
    return bitmap
}

疊代產生

圖片產生器也可輸出 疊代,如 iterations 輸入參數所定義。檢視這些 中繼結果,請呼叫 setInputs 方法,然後呼叫 execute() 以執行 每個步驟。將 showResult 參數設為 true 以顯示中繼 也就是預測結果

fun execute(showResult: Boolean): Bitmap {
    val result = imageGenerator.execute(showResult)

    val bitmap =
        BitmapExtractor.extract(result.generatedImage())

    return bitmap
}

處理及顯示結果

圖片產生器會傳回 ImageGeneratorResult,其中包含產生的 圖片、完成時間的時間戳記,以及條件式圖片 (如果有的話) 是系統提供的輸入內容

val bitmap = BitmapExtractor.extract(result.generatedImage())

下列圖片是根據下列輸入內容產生,僅使用 生成式模型

輸入內容:

  • 提示:「戴著毛茸茸寬鬆帽子的彩色卡通浣熊 拿著樹枝在森林裡行走,可以看到 75 度的動畫 繪畫」
  • 種子:312687592
  • 疊代作業:20

系統產生的圖片: