赞
踩
模型训练的pytorch==1.10.1(应该是不重要)
转换模型(生成model.ptl)的pytorch==1.8.0
(之前用官方给的pytorch-android项目改出现了在手机上运行闪退的情况,总的来说就是版本不匹配)
所以,我是直接新建了
在这个基础上改的,每一步都在手机上部署,最后查出是加载模型和推理这两个步骤导致闪退
android studio的配置:
gradle-wrapper.properties
- distributionBase=GRADLE_USER_HOME
- distributionPath=wrapper/dists
- distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
- zipStoreBase=GRADLE_USER_HOME
- zipStorePath=wrapper/dists
build.gradle.kts(Project)
- plugins {
- id("com.android.application") version "8.2.2" apply false
- }
build.gradle.kts(app)
- plugins {
- id("com.android.application")
- }
-
-
- android {
- namespace = "com.example.myapplication"
- compileSdk = 34
-
- defaultConfig {
- applicationId = "com.example.myapplication"
- minSdk = 30
- targetSdk = 34
- versionCode = 1
- versionName = "1.0"
- testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
- }
-
- buildTypes {
- release {
- isMinifyEnabled = false
- proguardFiles(
- getDefaultProguardFile("proguard-android-optimize.txt"),
- "proguard-rules.pro"
- )
- }
- }
- compileOptions {
- sourceCompatibility = JavaVersion.VERSION_1_8
- targetCompatibility = JavaVersion.VERSION_1_8
- }
- }
-
- dependencies {
- implementation("androidx.appcompat:appcompat:1.6.1")
- implementation("com.android.support:multidex:2.0.1")
- implementation("com.google.android.material:material:1.9.0")
- implementation("androidx.constraintlayout:constraintlayout:2.1.4")
- testImplementation("junit:junit:4.13.2")
- androidTestImplementation("androidx.test.ext:junit:1.1.5")
- androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
- implementation("org.pytorch:pytorch_android_lite:1.9.0")
- implementation("org.pytorch:pytorch_android_torchvision:1.9.0")
- }
gradle.properties
- # Project-wide Gradle settings.
- # IDE (e.g. Android Studio) users:
- # Gradle settings configured through the IDE *will override*
- # any settings specified in this file.
- # For more details on how to configure your build environment visit
- # http://www.gradle.org/docs/current/userguide/build_environment.html
- # Specifies the JVM arguments used for the daemon process.
- # The setting is particularly useful for tweaking memory settings.
- org.gradle.jvmargs=-Xmx4608m -Dfile.encoding=UTF-8
- # When configured, Gradle will run in incubating parallel mode.
- # This option should only be used with decoupled projects. More details, visit
- # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
- # org.gradle.parallel=true
- # AndroidX package structure to make it clearer which packages are bundled with the
- # Android operating system, and which are packaged with your app's APK
- # https://developer.android.com/topic/libraries/support-library/androidx-rn
- android.useAndroidX=true
- android.enableJetifier=true
- # Enables namespacing of each library's R class so that its R class includes only the
- # resources declared in the library itself and none from the library's dependencies,
- # thereby reducing the size of the R class for that library
- android.nonTransitiveRClass=true
读取模型
module = LiteModuleLoader.load(assetFilePath(this, "model.ptl"));
部分推理的代码
(这也是一个问题,关于数据类型的)
参考:Pytorch mobile app error · Issue #2666 · pytorch/vision · GitHub
- final Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(bitmap,
- TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_STD_RGB, MemoryFormat.CHANNELS_LAST);
- IValue inputs = IValue.from(inputTensor);
- IValue[] outputs = module.forward(inputs).toTuple();
在修改的过程中,出现问题了,先做下:clean project、rebuild、关掉as再打开。
太玄学了,希望以后不要再和安卓打照面,新年快乐。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。