[Huawei Developers] Sherlock F:
Image classification uses the transfer learning algorithm to perform minute-level learning training on hundreds of images in specific fields (such as vehicles and animals) based on the base classification model with good generalization capabilities, and can automatically generate a model for image classification. The generated model can automatically identify the category to which the image belongs. This is an auto generated model. What if we want to create our image classification model?

In Huawei ML Kit it is possible. The AI Create function in HiAI Foundation provides the transfer learning capability of image classification. With in-depth machine learning and model training, AI Create can help users accurately identify images. In this article we will create own image classification model and we will develop an Android application with using this model. Let’s start.

First of all we need some requirement for creating our model;

You need a Huawei account for create custom model. For more detail click here.

You will need HMS Toolkit. In Android Studio plugins find HMS Toolkit and add plugin into your Android Studio.

You will need Python in our computer. Install Python 3.7.5 version. Mindspore is not used in other versions.

And the last requirements is the model. You will need to find the dataset. You can use any dataset you want. I will use flower dataset. You can find my dataset in here.

Model Creation

Create a new project in Android Studio. Then click HMS on top of the Android Studio screen. Then open Coding Assistant.

1- In the Coding Assistant screen, go to AI and then click AI Create. Set the following parameters, then click Confirm.

Operation type : Select New Model

Model Deployment Location : Select Deployment Cloud.

After click confirm a browser will be opened to log into your Huawei account. After log into your account a window will opened as below.

2- Drag or add the image classification folders to the Please select train image folder area then set Output model file path and train parameters. If you have extensive experience in deep learning development, you can modify the parameter settings to improve the accuracy of the image recognition model. After preparation click Create Model to start training and generate an image classification model.

3- Then it will start training. You can follow the process on log screen:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/491/936/5190090000026491936.20201015071913.33080555786505990103660914227237:50511028005245:2800:36EE338D798DADA0E59D4F04338B607D5F175F3CCB2069388DE925058A7010B0.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/491/936/5190090000026491936.20201015071913.33080555786505990103660914227237:50511028005245:2800:36EE338D798DADA0E59D4F04338B607D5F175F3CCB2069388DE925058A7010B0.png[/IMG]

4- After training successfully completed you will see the screen like below:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/491/936/5190090000026491936.20201015072110.23483043131163616110145415763600:50511028005245:2800:FC2D98EBF7A96470FAB518924969B427201D643FC642EFD71C5191B97322AC55.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/491/936/5190090000026491936.20201015072110.23483043131163616110145415763600:50511028005245:2800:FC2D98EBF7A96470FAB518924969B427201D643FC642EFD71C5191B97322AC55.png[/IMG]

In this screen you can see the train result, train parameter and train dataset information of your model. You can give some test data for testing your model accuracy if you want. Here is the sample test results:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/491/936/5190090000026491936.20201015072146.46598471184842773618754524788024:50511028005245:2800:A95029C71B6A24DC12175AD35ADCFB3FD673B7041430618EA849787F1D95C166.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/491/936/5190090000026491936.20201015072146.46598471184842773618754524788024:50511028005245:2800:A95029C71B6A24DC12175AD35ADCFB3FD673B7041430618EA849787F1D95C166.png[/IMG]

5- After confirming that the training model is available, you can choose to generate a demo project.

Generate Demo: HMS Toolkit automatically generates a demo project, which automatically integrates the trained model. You can directly run and build the demo project to generate an APK file, and run the file on the simulator or real device to check the image classification performance.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/491/936/5190090000026491936.20201015073251.27391655599130620170697102899946:50511028005245:2800:FBCC25DDF282AA11A6EE4EB7299B5D71C2F7F5D2C3E5F7FCD720A0A985564871.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/491/936/5190090000026491936.20201015073251.27391655599130620170697102899946:50511028005245:2800:FBCC25DDF282AA11A6EE4EB7299B5D71C2F7F5D2C3E5F7FCD720A0A985564871.jpg[/IMG]

Using Model Without Generated Demo Project

If you want to use the model in your project you can follow the steps:

1- In your project create an Assests file:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/491/936/5190090000026491936.20201015073411.54835423437610458973588144172442:50511028005245:2800:4347AD6997497AE40929BDE47E91E93D8B7A7BA334682364ADAC23379299EDEE.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/491/936/5190090000026491936.20201015073411.54835423437610458973588144172442:50511028005245:2800:4347AD6997497AE40929BDE47E91E93D8B7A7BA334682364ADAC23379299EDEE.png[/IMG]

2- Then navigate to the folder path you chose in step 1 in Model Creation. Find your model the extension will be in the form of “.ms” . Then copy your model into Assets file. After we need one more file. Create a txt file containing your model tags. Then copy that file into Assets folder also.

3- Download and add the CustomModelHelper.kt file into your project. You can find repository in here:

https://github.com/iebayirli/AICreateCustomModel

Don’t forget the change the package name of CustomModelHelper class. After the ML Kit SDK is added, its errors will be fixed.

4- After completing the add steps, we need to add maven to the project level build.gradle file to get the ML Kit SDKs. Your gradle file should be like this:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
maven { url "https://developer.huawei.com/repo/" }
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://developer.huawei.com/repo/" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

5- Next, we are adding ML Kit SDKs into our app level build.gradle. And don’t forget the add aaptOption. Your app level build.gradle file should be like this:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.iebayirli.aicreatecustommodel"
minSdkVersion 26
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
kotlinOptions{
jvmTarget= "1.8"
}
aaptOptions {
noCompress "ms"
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.huawei.hms:ml-computer-model-executor:2.0.3.301'
implementation 'mindspore:mindspore-lite:0.0.7.000'
def activity_version = "1.2.0-alpha04"
// Kotlin
implementation "androidx.activity:activity-ktx:$activity_version"
}

6- Let’s create the layout first:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline1"
android:orientation="horizontal"
app:layout_constraintGuide_percent=".65"/>

<ImageView
android:id="@+id/ivImage"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="3:4"
android:layout_margin="16dp"
android:scaleType="fitXY"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/guideline1"/>

<TextView
android:id="@+id/tvResult"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="16dp"
android:autoSizeTextType="uniform"
android:background="@android:color/white"
android:autoSizeMinTextSize="12sp"
android:autoSizeMaxTextSize="36sp"
android:autoSizeStepGranularity="2sp"
android:gravity="center_horizontal|center_vertical"
app:layout_constraintTop_toTopOf="@+id/guideline1"
app:layout_constraintBottom_toTopOf="@+id/btnRunModel"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

<Button
android:id="@+id/btnRunModel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Pick Image and Run"
android:textAllCaps="false"
android:background="#ffd9b3"
android:layout_marginBottom="8dp"
app:layout_constraintWidth_percent=".75"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

7- Then lets create const values in our activity. We are creating four values. First value is for permission. Other values are relevant to our model. Your code should look like this:

companion object {
const val readExternalPermission = android.Manifest.permission.READ_EXTERNAL_STORAGE
const val modelName = "flowers"
const val modelFullName = "flowers" + ".ms"
const val labelName = "labels.txt"
}

8- Then we create the CustomModelHelper example. We indicate the information of our model and where we want to download the model:

private val customModelHelper by lazy {
CustomModelHelper(
this,
modelName,
modelFullName,
labelName,
LoadModelFrom.ASSETS_PATH
)
}

9- After, we are creating two ActivityResultLauncher instances for gallery permission and image picking with using Activity Result API:

private val galleryPermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
if (!it)
finish()
}

private val getContent =
registerForActivityResult(ActivityResultContracts.GetContent()) {
val inputBitmap = MediaStore.Images.Media.getBitmap(
contentResolver,
it
)
ivImage.setImageBitmap(inputBitmap)
customModelHelper.exec(inputBitmap, onSuccess = { str ->
tvResult.text = str
})
}

In getContent instance. We are converting selected uri to bitmap and calling the CustomModelHelper exec() method. If the process successfully finish we update textView.

10- After creating instances the only thing we need to is launching ActivityResultLauncher instances into onCreate():

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

galleryPermission.launch(readExternalPermission)

btnRunModel.setOnClickListener {
getContent.launch(
"image/*"
)
}
}

11- Let’s bring them all the pieces together. Here is our MainActivity:

package com.iebayirli.aicreatecustommodel
import android.os.Bundle
import android.provider.MediaStore
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {

private val customModelHelper by lazy {
CustomModelHelper(
this,
modelName,
modelFullName,
labelName,
LoadModelFrom.ASSETS_PATH
)
}

private val galleryPermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
if (!it)
finish()
}

private val getContent =
registerForActivityResult(ActivityResultContracts.GetContent()) {
val inputBitmap = MediaStore.Images.Media.getBitmap(
contentResolver,
it
)
ivImage.setImageBitmap(inputBitmap)
customModelHelper.exec(inputBitmap, onSuccess = { str ->
tvResult.text = str
})
}

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

galleryPermission.launch(readExternalPermission)

btnRunModel.setOnClickListener {
getContent.launch(
"image/*"
)
}

}

companion object {
const val readExternalPermission = android.Manifest.permission.READ_EXTERNAL_STORAGE
const val modelName = "flowers"
const val modelFullName = "flowers" + ".ms"
const val labelName = "labels.txt"
}
}

Summary

In summary, we learned how to create a custom image classification model. We used HMS Toolkit for model training. After model training and creation we learned how to use our model in our application. If you want more information about Huawei ML Kit you find in here.

Here is the output:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/491/936/5190090000026491936.20201015074331.53437899687658268927717063600379:50511028005245:2800:825AD00C766F31BDDA4846DC92085A3304743D6F88F36725183A6D11DDCC8C54.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/491/936/5190090000026491936.20201015074331.53437899687658268927717063600379:50511028005245:2800:825AD00C766F31BDDA4846DC92085A3304743D6F88F36725183A6D11DDCC8C54.gif[/IMG]

https://github.com/iebayirli/AICreateCustomModel