当前位置:   article > 正文

记录一下pytorch成功部署到android的版本_pytorch android

pytorch android

模型训练的pytorch==1.10.1(应该是不重要)

转换模型(生成model.ptl)的pytorch==1.8.0

(之前用官方给的pytorch-android项目改出现了在手机上运行闪退的情况,总的来说就是版本不匹配)

所以,我是直接新建了

在这个基础上改的,每一步都在手机上部署,最后查出是加载模型和推理这两个步骤导致闪退

android studio的配置:

gradle-wrapper.properties

  1. distributionBase=GRADLE_USER_HOME
  2. distributionPath=wrapper/dists
  3. distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
  4. zipStoreBase=GRADLE_USER_HOME
  5. zipStorePath=wrapper/dists

build.gradle.kts(Project)

  1. plugins {
  2. id("com.android.application") version "8.2.2" apply false
  3. }

build.gradle.kts(app)

  1. plugins {
  2. id("com.android.application")
  3. }
  4. android {
  5. namespace = "com.example.myapplication"
  6. compileSdk = 34
  7. defaultConfig {
  8. applicationId = "com.example.myapplication"
  9. minSdk = 30
  10. targetSdk = 34
  11. versionCode = 1
  12. versionName = "1.0"
  13. testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
  14. }
  15. buildTypes {
  16. release {
  17. isMinifyEnabled = false
  18. proguardFiles(
  19. getDefaultProguardFile("proguard-android-optimize.txt"),
  20. "proguard-rules.pro"
  21. )
  22. }
  23. }
  24. compileOptions {
  25. sourceCompatibility = JavaVersion.VERSION_1_8
  26. targetCompatibility = JavaVersion.VERSION_1_8
  27. }
  28. }
  29. dependencies {
  30. implementation("androidx.appcompat:appcompat:1.6.1")
  31. implementation("com.android.support:multidex:2.0.1")
  32. implementation("com.google.android.material:material:1.9.0")
  33. implementation("androidx.constraintlayout:constraintlayout:2.1.4")
  34. testImplementation("junit:junit:4.13.2")
  35. androidTestImplementation("androidx.test.ext:junit:1.1.5")
  36. androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
  37. implementation("org.pytorch:pytorch_android_lite:1.9.0")
  38. implementation("org.pytorch:pytorch_android_torchvision:1.9.0")
  39. }

gradle.properties

  1. # Project-wide Gradle settings.
  2. # IDE (e.g. Android Studio) users:
  3. # Gradle settings configured through the IDE *will override*
  4. # any settings specified in this file.
  5. # For more details on how to configure your build environment visit
  6. # http://www.gradle.org/docs/current/userguide/build_environment.html
  7. # Specifies the JVM arguments used for the daemon process.
  8. # The setting is particularly useful for tweaking memory settings.
  9. org.gradle.jvmargs=-Xmx4608m -Dfile.encoding=UTF-8
  10. # When configured, Gradle will run in incubating parallel mode.
  11. # This option should only be used with decoupled projects. More details, visit
  12. # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
  13. # org.gradle.parallel=true
  14. # AndroidX package structure to make it clearer which packages are bundled with the
  15. # Android operating system, and which are packaged with your app's APK
  16. # https://developer.android.com/topic/libraries/support-library/androidx-rn
  17. android.useAndroidX=true
  18. android.enableJetifier=true
  19. # Enables namespacing of each library's R class so that its R class includes only the
  20. # resources declared in the library itself and none from the library's dependencies,
  21. # thereby reducing the size of the R class for that library
  22. android.nonTransitiveRClass=true

读取模型

module = LiteModuleLoader.load(assetFilePath(this, "model.ptl"));

部分推理的代码

(这也是一个问题,关于数据类型的)

参考:Pytorch mobile app error · Issue #2666 · pytorch/vision · GitHub

  1. final Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(bitmap,
  2. TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_STD_RGB, MemoryFormat.CHANNELS_LAST);
  3. IValue inputs = IValue.from(inputTensor);
  4. IValue[] outputs = module.forward(inputs).toTuple();

在修改的过程中,出现问题了,先做下:clean project、rebuild、关掉as再打开。

太玄学了,希望以后不要再和安卓打照面,新年快乐。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/767362
推荐阅读
相关标签
  

闽ICP备14008679号