手机:小米5
linux环境:debian-testing
app:linux-deploy 2.0.1
网络:theano + keras
本来想弄tensorflow + tensorlayer的,无奈,github 上找不到合适的 tensorflow 包,按照 tensorflow on raspberry pi 的编译方式在手机上编译,又空间不够,在电脑上尝试交叉编译,结果。。。没办法只能换 backend 了,尝试安装 theano ,
没想到 pip 上居然有这个包,然后就开始尝试 theano + keras
嗯。。。成功是成功了,速度跟预料的一样。。。真的很慢。。。
好吧,开始 theano + keras 安装流程
- sudo apt install python3-numpy python3-scipy python3-six python3-pandas
- pip3 install –no-deps theano
- pip3 install –no-deps keras
- pip3 install pyyaml
- 执行 python -c “import keras”
- 执行 vim ~/.keras/keras.json
- 把 tensorflow 改成 theano,保存退出
- 你的 keras 正常工作了!赶紧跑个例程试试
解释一下,为什么不直接
pip3 install theano
pip3 install keras
因为
pip3 install theano 和 pip3 install keras 会自动安装依赖包 scipy,因为pip3 install scipy包安装会容易失败,
即使 apt install python3-scipy 后,仍然会重复安装 scipy 包,所以使用
pip3 install –no-deps theano 和 pip3 install –no-deps keras 来阻止自动安装 scipy 包
但是这样也会阻止安装其他依赖包,所以必须手动安装其他依赖包
附注1:pip3 install scipy 需要手动安装很多其他库,出错时错误信息也不怎么明显,安装时间很长,虽然有办法安装,但是不如 apt install python3-scipy 方便处理
附带一个简单完整的 keras 例子用来测试
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
# from keras import backend as K
batch_size = 32
num_classes = 10
epochs = 12
(x_train, y_train), (x_test, y_test) = mnist.load_data()
input_shape = (28*28*1,)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train = x_train.reshape(x_train.shape[0], input_shape[0])
x_test = x_test.reshape(x_test.shape[0], input_shape[0])
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
y_train = keras.utils.np_utils.to_categorical(y_train, num_classes)
y_test = keras.utils.np_utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Dense(32, input_shape=input_shape, activation='relu'))
model.add(Dropout(0.35))
# model.add(Flatten())
model.add(Dense(32, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.metrics.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs,
verbose=1, validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])