Skip to content

Commit

Permalink
Image generation test app (#244)
Browse files Browse the repository at this point in the history
* create image generation test app

* updated to work with iterations and api

* update new ui

* split samples into multiples for image generation, got face plugin working

* plugins working

* solo app for plugins, lora, and diffusion

* final features working

* updates

* update feedback

* seed range removed, but positive only

* add readme

* fix paths

* general cleanup

---------

Co-authored-by: Duy Mai M <[email protected]>
  • Loading branch information
PaulTR and duy-maimanh authored Sep 29, 2023
1 parent 54d2a65 commit 3512d22
Show file tree
Hide file tree
Showing 46 changed files with 2,988 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/image_generation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
34 changes: 34 additions & 0 deletions examples/image_generation/android/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# MediaPipe Image Generation

This app is a demonstration and sample of using MediaPipe to generate new images based on a text input.

There are three primary ways that you can use this new demo and MediaPipe Task:

1. Standard diffusion to generate new images based on a text prompt.

![Diffusion example](images/diffusion.gif?raw=true "Diffusion example")


2. Diffusion with a plugin that works with other existing tasks and models to provide structure for your new generations.

![Plugin example](images/plugin.gif?raw=true "Plugin example")

3. Diffusion with Low-Rank Adaptation (LoRA) weights that allow you to create images of specific concepts that you pre-define for your unique use-cases.

![LoRA example](images/lora.gif?raw=true "LoRA example")

## Build the demo using Android Studio

To perform image generation, you will need to download or build an image model that uses the Stable Diffusion v1.5 architecture. You can find a list of open models on the [official documentation page](https://developers.google.com/mediapipe/solutions/vision/image_generator#install_and_run_the_image_generator_demo_app).

After you have your model downloaded, you can run the conversion script listed in the official documentation to prepare it for use with this sample application. You will also need to copy this converted model to your Android device.

Optionally, you can create a new set of weights to use with the LoRA option, adding a new and desired bias to your image generations. These weights will need to be stored on your Android device, and you can find a link to an official set of LoRA weights in the Task's documentation.

### Building

When your models/weights are ready, copy them to your development device. For this example the files are loaded into the `/data/local/tmp/image_generator/bins` directory.

To use the face, edge, or depth plugins, you will need additional models stored in the app's `assets` directory. These will be automatically downloaded and installed with your APK through the `download_models.gradle` build script located in this project.

An example weights file can be found [here](https://storage.googleapis.com/mediapipe-models/image_generator/LoRA_weights/teapot_lora.task) for the key term 'monadikos teapot'.
1 change: 1 addition & 0 deletions examples/image_generation/android/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
54 changes: 54 additions & 0 deletions examples/image_generation/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'de.undercouch.download'
}

android {
namespace 'com.google.mediapipe.examples.imagegeneration'
compileSdk 33

defaultConfig {
applicationId "com.google.mediapipe.examples.imagegeneration"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}

// Downloads the TFLite and Task files used for plugins
project.ext.ASSET_DIR = projectDir.toString() + '/src/main/assets'
apply from: 'download_tasks.gradle'

dependencies {

implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.activity:activity-ktx:1.7.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'com.google.mediapipe:tasks-vision-image-generator:0.10.5.1'
}
46 changes: 46 additions & 0 deletions examples/image_generation/android/app/download_tasks.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
task downloadFaceTaskFile(type: Download) {
src 'https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task'
dest project.ext.ASSET_DIR + '/face_landmarker.task'
overwrite false
}

task downloadFacePluginFile(type: Download) {
src 'https://storage.googleapis.com/mediapipe-models/image_generator/plugin_models/float32/latest/face_landmark_plugin.tflite'
dest project.ext.ASSET_DIR + '/face_landmark_plugin.tflite'
overwrite false
}

task downloadEdgePluginFile(type: Download) {
src 'https://storage.googleapis.com/mediapipe-models/image_generator/plugin_models/float32/latest/canny_edge_plugin.tflite'
dest project.ext.ASSET_DIR + '/canny_edge_plugin.tflite'
overwrite false
}

task downloadDepthModelFile(type: Download) {
src 'https://storage.googleapis.com/mediapipe-models/image_generator/condition_image_models/float16/latest/depth_512_512_fp16_opt_w_metadata.tflite'
dest project.ext.ASSET_DIR + '/depth_model.tflite'
overwrite false
}

task downloadDepthPluginFile(type: Download) {
src 'https://storage.googleapis.com/mediapipe-models/image_generator/plugin_models/float32/latest/depth_plugin.tflite'
dest project.ext.ASSET_DIR + '/depth_plugin.tflite'
overwrite false
}

preBuild.dependsOn downloadFaceTaskFile, downloadFacePluginFile, downloadEdgePluginFile, downloadDepthModelFile, downloadDepthPluginFile
21 changes: 21 additions & 0 deletions examples/image_generation/android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
36 changes: 36 additions & 0 deletions examples/image_generation/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ImageGeneration"
tools:targetApi="31" >
<!-- Some devices, like the Pixel 6, may need to actively declare the use of native libs -->
<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" />

<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".diffusion.DiffusionActivity"
android:keepScreenOn="true" />
<activity android:name=".plugins.PluginActivity"
android:keepScreenOn="true" />
<activity android:name=".loraweights.LoRAWeightActivity"
android:keepScreenOn="true" />
</application>

</manifest>
Loading

0 comments on commit 3512d22

Please sign in to comment.