Automate download and unzip of the model file (#14853)

TESTING

Used Android Studio 3.1.3, NDK r17b and Pixel XL API 24 emulator.

Blocked from testing the built app due to this issue: https://github.com/tensorflow/tensorflow/issues/18658

Did a ./gradlew clean.  Deleted intermediate download and unzipped versions of the model:
```
$ rm app/build/intermediates/mobilenet_v1_224_android_quant_2017_11_08.zip
$ rm app/src/main/assets/mobilenet_quant_v1_224.tflite
```

Built the app and confirmed the model got downloaded and unzipped:
```
$ ./gradlew assemble
<snip>
:app:downloadModel
Downloading https://storage.googleapis.com/download.tensorflow.org/models/tflite/mobilenet_v1_224_android_quant_2017_11_08.zip
:app:unzipModel
Unzipping build/intermediates/mobilenet_v1_224_android_quant_2017_11_08.zip
:app:preBuild
<snip>
```

Deleted the model file from the assets folder and checked it gets unzipped again from the intermediate storage location:
```
$ ./gradlew assemble
<snip>
:app:downloadModel UP-TO-DATE
:app:unzipModel
Unzipping build/intermediates/mobilenet_v1_224_android_quant_2017_11_08.zip
:app:preBuild
<snip>
```

Built it again and check it doesn't get downloaded or unzipped again:
```
$ ./gradlew assemble
<snip>
:app:downloadModel UP-TO-DATE
:app:unzipModel UP-TO-DATE
<snip>
```
This commit is contained in:
Dan J 2018-06-17 18:10:58 -04:00 committed by drpngx
parent ab9b1341a9
commit 8e86dcd1c5
2 changed files with 47 additions and 12 deletions

View File

@ -56,3 +56,39 @@ dependencies {
testCompile 'junit:junit:4.12'
}
def modelDownloadUrl = "https://storage.googleapis.com/download.tensorflow.org/models/tflite/mobilenet_v1_224_android_quant_2017_11_08.zip"
def localCache = "build/intermediates/mobilenet_v1_224_android_quant_2017_11_08.zip"
def targetFolder = "src/main/assets"
task downloadModel(type: DownloadUrlTask) {
doFirst {
println "Downloading ${modelDownloadUrl}"
}
sourceUrl = "${modelDownloadUrl}"
target = file("${localCache}")
}
task unzipModel(type: Copy, dependsOn: 'downloadModel') {
doFirst {
println "Unzipping ${localCache}"
}
from zipTree("${localCache}")
into "${targetFolder}"
}
// Ensure the model file is downloaded and extracted before every build
preBuild.dependsOn unzipModel
class DownloadUrlTask extends DefaultTask {
@Input
String sourceUrl
@OutputFile
File target
@TaskAction
void download() {
ant.get(src: sourceUrl, dest: target)
}
}

View File

@ -44,23 +44,22 @@ app:
Android Studio project.
* Install all the Gradle extensions it requests.
To get a model, either:
Now you can build and run the demo app.
* Download the quantized [Mobilenet TensorFlow Lite model](https://storage.googleapis.com/download.tensorflow.org/models/tflite/mobilenet_v1_224_android_quant_2017_11_08.zip)
and unzip and copy `mobilenet_quant_v1_224.tflite` to the assets directory:
`tensorflow/contrib/lite/java/demo/app/src/main/assets/`.
* Or, download the floating point [Inception-v3 model](https://storage.googleapis.com/download.tensorflow.org/models/tflite/inception_v3_slim_2016_android_2017_11_10.zip)
and unzip and copy `inceptionv3_non_slim_2015.tflite` to the assets
directory. Change the chosen classifier in
[Camera2BasicFragment.java](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/java/demo/app/src/main/java/com/example/android/tflitecamerademo/Camera2BasicFragment.java)<br>
from: `classifier = new ImageClassifierQuantizedMobileNet(getActivity());`<br>
to: `classifier = new ImageClassifierFloatInception(getActivity());`.
Now you can build and run the demo app.
The build process downloads the quantized [Mobilenet TensorFlow Lite model](https://storage.googleapis.com/download.tensorflow.org/models/tflite/mobilenet_v1_224_android_quant_2017_11_08.zip), and unzips it into the assets directory: `tensorflow/contrib/lite/java/demo/app/src/main/assets/`.
Some additional details are available on the
[TF Lite Android App page](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite/java/demo/README.md).
### Using other models
To use a different model:
* Download the floating point [Inception-v3 model](https://storage.googleapis.com/download.tensorflow.org/models/tflite/inception_v3_slim_2016_android_2017_11_10.zip).
* Unzip and copy `inceptionv3_non_slim_2015.tflite` to the assets directory.
* Change the chosen classifier in [Camera2BasicFragment.java](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/java/demo/app/src/main/java/com/example/android/tflitecamerademo/Camera2BasicFragment.java)<br>
from: `classifier = new ImageClassifierQuantizedMobileNet(getActivity());`<br>
to: `classifier = new ImageClassifierFloatInception(getActivity());`.
## Build TensorFlow Lite and the demo app from source