IoTセンサー値から、tensorFlowを使った予測値の GoogleHome読み上げ機能を前回公開しましたが、
定期的に、最新のtensorFlow結果を更新する為に
機械学習の演算処理や、server更新処理を Lan上の機械学習serverで行っていましたので
python server実装あたりの内容となります。
# 予定している構成
前回と同じ構成、機械学習処理は、
web-APIを使って
IoTから更新されるセンサー値を読み込み
tensorFlow結果を更新する処理を追加しています。
*) ボードPC(nano Pi NEO 51MB)に、機能を追加
python 2.7
# Linux version
>uname -
Linux NanoPi-NEO 4.11.2 #266 SMP Thu Jun 29 17:46:10 CST 2017 armv7l armv7l armv7l GNU/Linux
# code ,python 2.7 、 一部分ですが。
main 処理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import requests | |
import json | |
import time | |
import sys | |
import gc | |
import traceback | |
import api_func | |
import com_func | |
import ai_func | |
mAPI_KEY="your-Key" | |
if __name__ == "__main__": | |
clsAI =ai_func.ai_funcClass() | |
key=mAPI_KEY | |
try: | |
field=1 | |
clsAI.proc_run(key , field ) | |
field=2 | |
clsAI.proc_run(key , field ) | |
field=4 | |
clsAI.proc_run(key , field ) | |
except: | |
print "--------------------------------------------" | |
print traceback.format_exc(sys.exc_info()[2]) | |
print "--------------------------------------------" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import json | |
from urllib2 import urlopen | |
import api_func | |
#ai_func | |
class ai_funcClass: | |
def __init__(self): | |
print "" | |
def proc_run(self ,field ): | |
cls = api_func.api_funcClass() | |
#get_apiData() | |
#exit() | |
# Model parameters | |
W = tf.Variable([0.0], dtype=tf.float32) | |
b = tf.Variable([0.0], dtype=tf.float32) | |
# Model input and output | |
x = tf.placeholder(tf.float32) | |
y = tf.placeholder(tf.float32) | |
linear_model = W*x + b | |
# loss | |
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares | |
# optimizer | |
optimizer = tf.train.GradientDescentOptimizer(0.01) | |
train = optimizer.minimize(loss) | |
# training data | |
y_train = cls.get_apiData(field ) | |
cDim=[] | |
iCt=0 | |
for xRow in range(len(y_train ) ): | |
cDim.append( float(iCt)/100.0 ) | |
iCt +=1 | |
x_train = cDim | |
print(x_train) | |
print(y_train) | |
# training loop | |
print('#Start traning.') | |
init = tf.global_variables_initializer() | |
sess = tf.Session() | |
sess.run(init) # reset values to wrong | |
for i in range(1000): | |
sess.run(train, {x: x_train, y: y_train}) | |
if i % 100 == 0: | |
print( i, sess.run(W), sess.run(b) ) | |
# evaluate training accuracy | |
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train}) | |
if(len(curr_W)) >0: | |
print('W=' + str(curr_W[0])) | |
if(len(curr_b ) >0): | |
print( 'b='+ str(curr_b[0] )) | |
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss)) | |
#update | |
if((len(curr_W) > 0) and (len(curr_b) > 0)): | |
cls.update(field , curr_W[0],curr_b[0] ) | |
*) 下記の cronに変更しています。
rasPi/BLE serverと同様に、起動処理を追加。
参考 の設定方法:
http://knaka0209.blogspot.jp/2017/07/raspi-6-BLE.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description = tesorFlow Learning server | |
[Service] | |
ExecStart=/usr/bin/python /usr/local/yosoku_update/yosoku_update.py | |
Restart=always | |
Type=simple | |
[Install] | |
WantedBy=multi-user.target |
### update :2018/01/19
常駐サービスの連続稼動テストした結果
tensorFlow 1.1で、メモリーリークが見られましたので、
cron起動に一旦変更しています。
*)最新 tensorflow -version は、対策されているかもです。
(nanoPi /rasPi インストール不可能みたいですが)
調整方法は、変数をdel 処理と、gc呼出で。
メモリ領域の削除で対応予定していたのですが、
なかなか消費メモリ量が減らず。。 cronに変えています
この変更のデメリットは、1サイクルの実行終了前に、次の実行処理が
二重に起動される可能性がありますので、起動間隔をあけておく必要があります。
設定例:
cron設定の確認
> sudo crontab -l
cron設定 (nanoで設定しました。)
> sudo crontab -e
例ですが、下記を追加しました。毎時10分に起動した場合です。
10 * * * * /usr/bin/python /home/pi/work/tensorflow/yosoku_update/yosoku_update_one.py
*)上記の呼出コードも、修正しています。
cronから呼ばれ
ループ無しの、単発実行 => 終了になります。
# 関連、
tensorFlow/機械学習 関連まとめ
http://knaka0209.blogspot.jp/2018/01/tensorflow-matome.html
*)IoT側はBLE使用しています
device:
http://knaka0209.blogspot.jp/2017/09/RN4020-4.html
nano Pi gateway:
http://knaka0209.blogspot.jp/2017/07/nanoPi-2.html
0 件のコメント:
コメントを投稿