Skip to content

Commit

Permalink
Global refactoring
Browse files Browse the repository at this point in the history
Update the app name, upload quantized model, update FAB colors, add instructions to the home screen
  • Loading branch information
slipnitskaya committed Mar 15, 2021
1 parent 329e67b commit 4a0c1e2
Show file tree
Hide file tree
Showing 21 changed files with 293 additions and 250 deletions.
8 changes: 5 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "com.example.caption_assistant"
applicationId "com.example.image_tagger"
minSdkVersion 27
targetSdkVersion 30
versionCode 1
Expand Down Expand Up @@ -78,8 +78,10 @@ dependencies {
implementation group: 'org.bytedeco', name: 'openblas', version: openBLASVersion, classifier: "android-x86"
implementation group: 'org.bytedeco', name: 'openblas', version: openBLASVersion, classifier: "android-x86_64"

implementation 'org.pytorch:pytorch_android:1.7.1'
implementation 'org.pytorch:pytorch_android_torchvision:1.7.1'
def pyTorchVersion = '1.7.1'

implementation "org.pytorch:pytorch_android:$pyTorchVersion"
implementation "org.pytorch:pytorch_android_torchvision:$pyTorchVersion"

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.caption_assistant
package com.example.image_tagger

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.caption_assistant">
package="com.example.image_tagger">

<uses-permission
android:name="android.permission.CAMERA" />
Expand All @@ -9,7 +9,7 @@
android:required="false" />

<application
android:name=".CaptionAssistant"
android:name="com.example.image_tagger.ImageTagger"
android:allowBackup="true"
android:fullBackupContent="@xml/backup_descriptor"
android:icon="@mipmap/ic_launcher"
Expand All @@ -20,15 +20,15 @@
android:theme="@style/Theme.CaptionAssistant">

<activity
android:name=".MainActivity"
android:name="com.example.image_tagger.MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CaptionActivity"
android:name="com.example.image_tagger.TaggerActivity"
android:screenOrientation="portrait">
</activity>

Expand Down
200 changes: 0 additions & 200 deletions app/src/main/assets/caption_assistant.csv

This file was deleted.

200 changes: 200 additions & 0 deletions app/src/main/assets/image_tagger.csv

Large diffs are not rendered by default.

Binary file modified app/src/main/assets/model.pt
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.caption_assistant
package com.example.image_tagger

import android.graphics.Bitmap
import kotlinx.coroutines.Dispatchers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.example.caption_assistant
package com.example.image_tagger

object Constants {
const val IMAGE_COUNTER_KEY = "ImageId"
const val IMAGE_FILENAME = "Image"
const val IMAGE_FOLDER_NAME = "Images"
const val CSV_NAME = "caption_assistant.csv"
const val DATABASE_NAME = "caption_assistant.db"
const val CSV_NAME = "image_tagger.csv"
const val DATABASE_NAME = "image_tagger.db"
const val MODEL_NAME = "model.pt"
const val NUM_CHANNELS = 3
const val INPUT_HEIGHT = 256
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.caption_assistant
package com.example.image_tagger

import kotlinx.coroutines.*
import org.nd4j.linalg.api.buffer.DataType
Expand All @@ -11,7 +11,7 @@ import kotlin.math.ln

class GaussianNaiveBayes(private val coroutineScope: CoroutineScope) {

private fun calculatePosterior(
private fun calculateConditionalProba(
x: INDArray, classStatistics: Statistics
): Float {
return with(x.sub(classStatistics.mean)) {
Expand All @@ -25,10 +25,13 @@ class GaussianNaiveBayes(private val coroutineScope: CoroutineScope) {
}
}

private suspend fun calculatePosteriors(x: INDArray, statistics: List<Statistics>): Map<Int, Float> {
private suspend fun calculateConditionalProbas(x: INDArray, statistics: List<Statistics>): Map<Int, Float> {
val (classIds, jobs) = statistics.map { classStatistics ->
Pair(
classStatistics.cid, coroutineScope.async(Dispatchers.Default) { calculatePosterior(x, classStatistics) }
classStatistics.cid,
coroutineScope.async(Dispatchers.Default) {
calculateConditionalProba(x, classStatistics)
}
)
}.unzip()

Expand All @@ -54,13 +57,13 @@ class GaussianNaiveBayes(private val coroutineScope: CoroutineScope) {
}
.toMap()

val posteriors = calculatePosteriors(x, statistics)
val conditionals = calculateConditionalProbas(x, statistics)

return (priors.keys + posteriors.keys)
return (priors.keys + conditionals.keys)
.associateWith { classId ->
sequenceOf(priors[classId], posteriors[classId])
sequenceOf(priors[classId], conditionals[classId])
.filterNotNull()
.reduce { prior, posterior -> prior + posterior }
.reduce { prior, conditional -> prior + conditional }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.caption_assistant
package com.example.image_tagger

import android.app.Application
import android.content.Context
Expand All @@ -14,11 +14,11 @@ import org.nd4j.linalg.factory.Nd4j
import java.io.File
import java.io.FileOutputStream

class CaptionAssistant : Application() {
class ImageTagger : Application() {

companion object {
lateinit var mediaFolder: File
lateinit var db: CaptionDB
lateinit var db: TagsDB
var cnn: CNN? = null
}

Expand All @@ -28,7 +28,7 @@ class CaptionAssistant : Application() {
db = Room
.databaseBuilder(
applicationContext,
CaptionDB::class.java,
TagsDB::class.java,
Constants.DATABASE_NAME
)
.allowMainThreadQueries()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.caption_assistant
package com.example.image_tagger

import android.Manifest
import android.content.Context
Expand All @@ -13,9 +13,11 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.FileProvider
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton
Expand All @@ -28,6 +30,7 @@ class MainActivity : AppCompatActivity() {
private val permissionsRequired = listOf(Manifest.permission.CAMERA to Utils.Action.CAPTURE_IMAGE())
private val imageExtensions = listOf("jpg", "png", "gif")
private lateinit var imageFeed: RecyclerView
private lateinit var uploadImageText: TextView
private lateinit var fabUpload: FloatingActionButton
private lateinit var fabCamera: FloatingActionButton
lateinit var listOfImages: MutableList<File>
Expand All @@ -48,6 +51,7 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)

imageFeed = findViewById(R.id.image_feed)
uploadImageText = findViewById(R.id.upload_image_text)
fabUpload = findViewById(R.id.fab_upload)
fabCamera = findViewById(R.id.fab_camera)

Expand Down Expand Up @@ -89,6 +93,7 @@ class MainActivity : AppCompatActivity() {
}
imageFeed.adapter = ImageListAdapter(this)

uploadImageText.isVisible = listOfImages.isEmpty()

checkPermissions()
}
Expand All @@ -112,6 +117,7 @@ class MainActivity : AppCompatActivity() {
}
listOfImages.clear()
listOfImages.addAll(getImageFiles(imageDir).sorted())
uploadImageText.isVisible = listOfImages.isEmpty()
imageFeed.adapter?.notifyDataSetChanged()
imageFeed.layoutManager?.scrollToPosition(listOfImages.size - 1)
}
Expand Down Expand Up @@ -160,7 +166,7 @@ class ImageListAdapter(private val parent: MainActivity) : RecyclerView.Adapter<
init {
imageView.setOnClickListener {
val imageUri = it.tag as Uri
val captionIntent = Intent(parent.baseContext, CaptionActivity::class.java)
val captionIntent = Intent(parent.baseContext, TaggerActivity::class.java)
captionIntent.putExtra("ImageURI", imageUri)
parent.startActivity(captionIntent)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.caption_assistant
package com.example.image_tagger

import androidx.room.*
import com.daveanthonythomas.moshipack.MoshiPack
Expand Down Expand Up @@ -55,6 +55,6 @@ class Converters {

@Database(entities = [Statistics::class], version = 2)
@TypeConverters(Converters::class)
abstract class CaptionDB : RoomDatabase() {
abstract class TagsDB : RoomDatabase() {
abstract fun statisticsDao(): StatisticsDao
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.caption_assistant
package com.example.image_tagger

import android.app.Dialog
import android.content.Context
Expand All @@ -13,7 +13,7 @@ import java.util.*

class TagChooserDialog(context: Context) : Dialog(context) {

val parent: CaptionActivity = context as CaptionActivity
val parent: TaggerActivity = context as TaggerActivity

private lateinit var tagList: ListView
private lateinit var tagMapAdapter: TagMapAdapter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.caption_assistant
package com.example.image_tagger

import android.net.Uri
import android.os.Bundle
Expand All @@ -19,7 +19,7 @@ import org.nd4j.linalg.api.ndarray.INDArray
import org.nd4j.linalg.factory.Nd4j
import kotlin.coroutines.CoroutineContext

class CaptionActivity : AppCompatActivity(), CoroutineScope by MainScope() {
class TaggerActivity : AppCompatActivity(), CoroutineScope by MainScope() {

private var imageUri: Uri? = null
private var inferenceIsRunning = false
Expand All @@ -32,7 +32,7 @@ class CaptionActivity : AppCompatActivity(), CoroutineScope by MainScope() {
private lateinit var confirmButton: Button
private lateinit var predictionsChipGroup: ChipGroup

private val statsTable by lazy { CaptionAssistant.db.statisticsDao() }
private val statsTable by lazy { ImageTagger.db.statisticsDao() }
private val gnb by lazy { GaussianNaiveBayes(this) }

private lateinit var job: Job
Expand All @@ -43,7 +43,7 @@ class CaptionActivity : AppCompatActivity(), CoroutineScope by MainScope() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_caption)
setContentView(R.layout.activity_tagger)

imageView = findViewById(R.id.chosen_image)
progressBar = findViewById(R.id.progress_bar)
Expand All @@ -66,7 +66,7 @@ class CaptionActivity : AppCompatActivity(), CoroutineScope by MainScope() {
launch {
enableBackgroundTaskRunning()

with(TagChooserDialog(this@CaptionActivity)) {
with(TagChooserDialog(this@TaggerActivity)) {
title = "Choose appropriate tag"
setCanceledOnTouchOutside(false)
show()
Expand All @@ -85,7 +85,7 @@ class CaptionActivity : AppCompatActivity(), CoroutineScope by MainScope() {
.toMap()
)

hideKeyboard(this@CaptionActivity.currentFocus ?: View(this@CaptionActivity))
hideKeyboard(this@TaggerActivity.currentFocus ?: View(this@TaggerActivity))
finish()
}
}
Expand Down Expand Up @@ -117,7 +117,7 @@ class CaptionActivity : AppCompatActivity(), CoroutineScope by MainScope() {
bitmap?.let { bm ->
imageView.setImageBitmap(bm)

CaptionAssistant.cnn?.let { cnn ->
ImageTagger.cnn?.let { cnn ->
val embeddingVector = cnn.forward(bm)

val statistics = withContext(Dispatchers.IO) { statsTable.getStatistics() }
Expand Down Expand Up @@ -205,7 +205,7 @@ class CaptionActivity : AppCompatActivity(), CoroutineScope by MainScope() {
setTitle("Interrupt running process?")
setPositiveButton(android.R.string.ok) { _, _ ->
job.cancel()
this@CaptionActivity.disableBackgroundTaskRunning()
this@TaggerActivity.disableBackgroundTaskRunning()
}
setNegativeButton(android.R.string.cancel) { _, _ -> }
show()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.caption_assistant
package com.example.image_tagger

import android.graphics.Bitmap
import java.io.ByteArrayOutputStream
Expand Down
Binary file removed app/src/main/res/drawable/background.png
Binary file not shown.
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_baseline_add_circle_outline_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M13,7h-2v4L7,11v2h4v4h2v-4h4v-2h-4L13,7zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8z"/>
</vector>
Loading

0 comments on commit 4a0c1e2

Please sign in to comment.