Skip to content

Commit 3dd1437

Browse files
committed
add some feature
1 parent eb51d53 commit 3dd1437

File tree

7 files changed

+125
-0
lines changed

7 files changed

+125
-0
lines changed

subs/ai/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ android {
3737
androidResources {
3838
noCompress = 'tflite'
3939
}
40+
buildFeatures {
41+
viewBinding true
42+
}
4043

4144
}
4245

@@ -55,4 +58,7 @@ dependencies {
5558
exclude group: "com.google.inject", module: "guice"
5659
}
5760
implementation 'com.google.android.gms:play-services-tasks:18.2.0'
61+
62+
implementation 'org.pytorch:pytorch_android_lite:1.9.0'
63+
implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'
5864
}

subs/ai/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<activity
66
android:name=".DigitalClassificationActivity"
77
android:exported="true" />
8+
<activity
9+
android:name=".GanActivity"
10+
android:exported="true" />
811
</application>
912

1013
</manifest>

subs/ai/src/main/assets/dcgan.pt

13.7 MB
Binary file not shown.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.engineer.ai
2+
3+
import android.content.Context
4+
import java.io.File
5+
import java.io.FileOutputStream
6+
import java.io.IOException
7+
8+
object AndroidAssetsFileUtil {
9+
10+
11+
fun assetFilePath(context: Context, assetName: String): String {
12+
val file = File(context.filesDir, assetName)
13+
if (file.exists() && file.length() > 0) {
14+
return file.absolutePath
15+
}
16+
17+
return context.assets.open(assetName).use { inputStream ->
18+
FileOutputStream(file).use { outputStream ->
19+
val buffer = ByteArray(4 * 1024)
20+
var read: Int
21+
while (inputStream.read(buffer).also { read = it } != -1) {
22+
outputStream.write(buffer, 0, read)
23+
}
24+
outputStream.flush()
25+
}
26+
file.absolutePath
27+
}
28+
}
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.engineer.ai
2+
3+
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
5+
import com.engineer.ai.databinding.ActivityGanBinding
6+
import org.pytorch.IValue
7+
import org.pytorch.Module
8+
import org.pytorch.Tensor
9+
import java.util.Random
10+
11+
12+
class GanActivity : AppCompatActivity() {
13+
private lateinit var module: Module
14+
private val modelName = "dcgan.pt"
15+
16+
private lateinit var viewBinding: ActivityGanBinding
17+
override fun onCreate(savedInstanceState: Bundle?) {
18+
super.onCreate(savedInstanceState)
19+
viewBinding = ActivityGanBinding.inflate(layoutInflater)
20+
setContentView(viewBinding.root)
21+
initModel()
22+
viewBinding.gen.setOnClickListener {
23+
genImage()
24+
}
25+
}
26+
27+
private fun genImage() {
28+
val zDim = intArrayOf(1, 100)
29+
val outDims = intArrayOf(64, 64, 3)
30+
31+
val z = FloatArray(zDim[0] * outDims[1])
32+
val random = Random(System.currentTimeMillis())
33+
z[0] = random.nextGaussian().toFloat()
34+
val shape = longArrayOf(1, 100)
35+
val tensor = Tensor.fromBlob(z, shape)
36+
37+
val resultT = module.forward(IValue.from(tensor)).toTensor()
38+
val resultArray = resultT.dataAsFloatArray
39+
40+
// val img = floatArrayOf(outDims[0],outDims[1],outDims[2])
41+
42+
}
43+
44+
private fun initModel() {
45+
module = Module.load(AndroidAssetsFileUtil.assetFilePath(this, modelName))
46+
}
47+
48+
49+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.engineer.ai
2+
3+
import org.pytorch.Tensor
4+
5+
class Mina {
6+
fun kk() {
7+
val zDim: IntArray = intArrayOf(1, 100)
8+
val outDims: IntArray = intArrayOf(64, 64, 3)
9+
10+
val z = FloatArray(zDim[0] * outDims[1])
11+
12+
val shape = longArrayOf(1, 100)
13+
14+
val tensor = Tensor.fromBlob(z, shape)
15+
}
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent">
6+
7+
8+
<Button
9+
android:id="@+id/gen"
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content"
12+
android:layout_margin="10dp"
13+
android:text="gan_gen"
14+
app:layout_constraintBottom_toBottomOf="parent" />
15+
16+
<ImageView
17+
android:id="@+id/gan_result"
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content"
20+
app:layout_constraintBottom_toTopOf="@id/gen" />
21+
22+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)