Knowledgebase

Deep Learning Pydroid3 + Keras Print

  • internetivo, python, keras, deep, learning, pydroid, pydroid3, tutorial
  • 2

This tutorial is based on the Android mobile platform. On the PyDroid3 software, Python3 language is used in conjunction with the Keras framework to develop a deep learning app. This article mainly involves the construction of the development environment on the mobile phone, as well as a sample code.

Ready to work
1. You need an Android phone on hand. The performance should not be too weak, because running the deep learning code is still very good. I use Xiaomi 8, running cnn will heat for a long time.
2. Download the PyDroid3 mobile app
3. The phone needs to be connected, and at least 1G storage, because you want to download some dependencies

Software Installation
1. Install the downloaded PyDroid. In order to facilitate the demonstration, I uninstalled the app from the phone and took the whole demonstration process
2. After installing PyDroid, open the app, it will automatically install Python3, wait a moment, you can test whether python works
3. Test python function

Enter the test code in the middle input box:

print("Hello World")

Note that the brackets () and the double quotation marks "" should be entered using the punctuation marks below the English input method, otherwise an error will be reported, and this should be noted later when the Code is on the phone.
After the input code is completed, click the yellow button in the lower right corner to run. If there is no error, you will see

Hello World

 


Development environment


Dependent library installation
Click on the upper right corner to display more menus, select the Pip option, you can find common libraries in QUICK INSTALL, click INSTALL to install them, and wait a moment when installing (the speed is indeed compared) Slow, everyone needs to wait patiently), and then exit the interface when prompted to complete the installation.


You could install some packages:
numpy, pandas, cython, scipy

Keras environment installation
Careful students can find that Keras can be installed in the above interface, but because Keras needs Theano as the backend (that is, Theano needs to run normally), so we need to install Theano first. Enter the input box under INSTALL
theano


Then the app will search for the download itself (be careful not to enter the wrong one, you may not find the package), wait for the same, after the installation is complete, exit the interface, if
Prompt error, maybe a network cause, then wait for another input to install. It is recommended to use the command line as it is easy to find in the software

In the menu select the Terminal option and enter Terminal
Enter: complete one and then enter the next item

pip3 install --upgrade pip
pip3 install theano
pip3 install keras

After the installation is complete, you can test the keras function and start a deep learning app

  • Test code
# coding: utf-8
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flatten
from keras.optimizers import Adam
np.random.seed(1337)
 
# download the mnist
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
 
# data pre-processing
X_train = X_train.reshape(-1, 1, 28, 28)/255
X_test = X_test.reshape(-1, 1, 28, 28)/255
Y_train = np_utils.to_categorical(Y_train, num_classes=10)
Y_test = np_utils.to_categorical(Y_test, num_classes=10)
 
# build CNN
model = Sequential()
 
# conv layer 1 output shape(32, 28, 28)
model.add(Convolution2D(filters=32,
                       kernel_size=5,
                       strides=1,
                       padding='same',
                       batch_input_shape=(None, 1, 28, 28),
                       data_format='channels_first'))
model.add(Activation('relu'))
 
# pooling layer1 (max pooling) output shape(32, 14, 14)
model.add(MaxPooling2D(pool_size=2, 
                       strides=2, 
                       padding='same', 
                       data_format='channels_first'))
 
# conv layer 2 output shape (64, 14, 14)
model.add(Convolution2D(64, 5, 
                        strides=1, 
                        padding='same', 
                        data_format='channels_first'))
model.add(Activation('relu'))
 
# pooling layer 2 (max pooling) output shape (64, 7, 7)
model.add(MaxPooling2D(2, 2, 'same', 
                       data_format='channels_first'))
 
# full connected layer 1 input shape (64*7*7=3136), output shape (1024)
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
 
# full connected layer 2 to shape (10) for 10 classes
model.add(Dense(10))
model.add(Activation('softmax'))
model.summary()
# define optimizer
adam = Adam(lr=1e-4)
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])
 
# training
print ('Training')
model.fit(X_train, Y_train, epochs=1, batch_size=16)
 
# testing
print ('Testing')
loss, accuracy = model.evaluate(X_test, Y_test)
print ('loss, accuracy: ', (loss, accuracy))

 

 




 


Was this answer helpful?
Back