2022年4月19日火曜日

マイクロソフトは曲者になりつつあります。

 と言っても日本語環境なのでガラパゴス化と言った方が性格かも?

殆どのソフトでは半角や全角文字等も数字の変換が問題ないのですが、機械学習に使うpandas(python)は見事に認識しません。

ハマっているのはこうです。

ValueError: invalid literal for int() with base 10: '5,300.00'

エクセルで取り込んだデータの数字が5,300.00でした念の為データフレームの列を正規化df['見積金額'].str.normalize("NFKC")としてもエクセルの',''.00'は半角としても文字と認識するようで5,300.00を型変換しても上記の様なエラーが出ます。

pandasは数字として認識しません。5300にしてやっとstr()からint()に変換出来ました。

国内の会社でデータサイエンスをしている人には常識かもしれませんが、かなりの時間悩みました。

強制的に変換するとNaNになってしまいます。

#df['見積単価'] = pd.to_numeric(df['見積単価'], errors='coerce')

変にコピペできない様にコメントアウトしています。

マルチインデックスにしても上手く順番通りにならないエラーはまだ不明です。

pandasのメソッド自体が間違っているのかでも悩むのにこの手のエラーはタチが悪いです。


df['見積金額'] = df['見積金額'].str.replace(',','')
df['見積金額'] = df['見積金額'].str.rstrip('0')
df['見積金額'] = df['見積金額'].str.rstrip('.')
df['見積金額']=df['見積金額'].fillna(0)
df['見積金額']
df['見積金額'].astype('int')
0            5300
1         7163000
2               0
3               0
4               0
           ...   
217849    5836700
217850    6517200
217851    1954400
217853          0
217854          0
Name: 見積金額, Length: 194891, dtype: int64

整数型に変換出来ました。

2022年4月14日木曜日

サーマルステレオカメラ

サーマルカメラを左右に1セットつけて3D画像にする実験を大工大の学生にご協力していただきました。

iPhoneアプリはアップデートしましたが全てのモデルには対応出来ていない状況です。
FLIR社に掲載されたプロジェクトですが、アプリとして正式なリリースをしていないので再度実験して問題点を整理中です。

  • まず左右のセットに電力を均等に配分しないと画像のバランスが崩れています。
  • デザインもある程度考慮する必要があります。
  • 立体感についてですが、センサーの距離は離すと立体感が強くなりますが、あまり距離を離すと不自然な感覚で酔うようになります。
今回アプリ側には3点測量で距離を計算する機能を追加しています。5m前後までの測定が限界で誤差は大きいですが、例えば炎の様な物質でないものの測定はこの方法しかできません。
例えば火事の消化活動で消火対象までの距離が分からないと適切に消火剤を散布できません。
実践的な用途でも評価されると思います。FLIR社はこの機能についてのレポートをホームページから削除したので軍事的な要素があると思われます。ドローンを兵器としている実践では効果が出ていますが、技術がその様な形で使用されるのは非常に残念です。

大工大と大商のアーリーアダプターコミュニティでサーマルステレオカメラを使ってスポーツやお化け屋敷の提案をして頂きました。アプリや機器をリリースすれば予想外な使われ方もあり出せるものは発表しようと思います。
技術的な問題で正式版は遅れています。アプリに使えるライブラリーやOSのバージョンアップや機器別の設定等が複雑なので整理中です。




 


2022年3月16日水曜日

DXとは?Googleの無料サービスを使う事です。



まず必須条件はクラウドサービスにする事。

システムの安全性は問題は殆ど無く、むしろ人為ミスによる情報漏洩やセキュリティの問題が憂慮されます。その為には扱う全ての人はクラウドやセキュリティの知識は教育が必要です。

国内の有料クラウドサービスのサイボーズやkintounはカスタマイズが出来るとはいえ費用は可成り掛かります。

その点Google Workspaceは無料で必要なサービスは網羅されており必要に応じて簡単なプログラムを追加する事が出来有料サービスの部分もカバーが可能です。

海外の有料クラウドサービスもありますが資料がオンラインで使うにもプログラマーとしてのスキルも必要になります。

有料サービスはいずれにしてもリスクがあります。サービスが突然中止される事もあります。

その点Googleがなくなる事はリスクとしては低く、サービスの中止や変更もありますが、基本使われない機能のサービスが中止される事が殆どです。

マイクロソフトもクラウドサービスがありますがofficeや過去の商品の連携で時代にあっていないのが現状です。outlookなどの単体サービスを有料で使う方が現実的です。


さて具体的にGoogleサービスを使う理由はスプレッドシート(エクセル)、 ドキュメント(ワード)、スライド(パワーポイント)とオフィスの機能は網羅されておりそれぞればオンライン上で連携出来ます。それぞれの資料はGoogleドライブ(オンラインストーレッジ)にセキュアーな状態で保存されて必要な人がアクセスできる様になっています。しかも無料です。

G mailやその他オンラインミーティングのアプリもあります。

例えば表計算ソフトの中のセルやドキュメントの特定の部分にコメントをつけて適宜担当者に修正を依頼すると担当者にメールでそれぞれの資料の特定の部分にコメントがついた状態で確認出来ます。担当者が必要な修正を加えてチェックをつければ依頼者に完了のメールが行きます。また期日の設定はGoogleカレンダーに反映されて必要に応じてリマインドされます。

印刷物にチェックをいれて人が管理するとミスや失念される事は避けられません。

その意味ではGoogleのサービスは常に改良や機能追加があるので使わない手はないです。

DXについて下記の様な問題がありますが、抽象的です。なので便利なサービスを使いながら理解していくのが良いと思います。


「DXとは、私たちが顧客へのより高い・新しい価値提供を行うための手段です」

  • IT・制度の壁
    • データインフラの壁
    • 組織の壁
  • 人の壁
    • ケイパビリティ(能力)の壁
    • マインドセットの壁

社内のデータインフラにはDXを推進する上でいくつか重大な設計・運用上の問題がありました。
一言で言えば、データ活用できる形でデータが溜まっていない、かつ接続できない状態でした。

IT担当者や利用者に色々とヒアリングしてみた結果として分かったことは、「データ活用するという意識もリテラシーも無いまま、短期的なニーズベースでデータインフラを作ってきた」ことが原因ということでした。
これは後述の人材育成にも関わってくる問題で、個人的には今年の最優先課題でした。


例えば、自動化が進めば工数が減る、というのは費用対効果として数字で算出できますが、その空いた時間(デジタル化によって生まれた”余力”)でできること(デジタルを前提とした新規ビジネスの検討等)の定量的な評価はできません。
一方で、DXは単なるデジタル化や自動化ではないので、目指したいのはそういう後者の取り組みになるはずですが、そこで費用対効果を突き詰めてしまうと足元のデータインフラ改善は全く進まなくなってしまいます。


2022年3月8日火曜日

機械学習の実践。



実践的な事を考えていますが中々社内でも理解されているか分かりません。

どう言う事か理解してもらえないと社内データの収集もままならない感じです。

来年の売上予測や来月の予測をしても役に立つのか?

物流の最適化もあります。

例えば客先、客先担当者、容量別、納品場所等で問題自身を確率で予測すれば対策の役に立つかもしれません。また在庫の日数や連絡の状況で費用が掛かっています。それも予測出来れば対策の役に立つかもしれません。コストを削減出来れば当社及び客先にも貢献出来る筈。

それぞれの項目の因果関係をヒートマップで分析さたら分かりやすいか?



必要なデータは欠損やデータをダミー(平均値や中央値)で補ってそれを機械学習にかけてどの様に出るかをご覧頂いてデータの意味を理解する必要があります。

どの予測器が良いかも分かりませんので、基本にかえってタイタニック乗員の生存率から進めて行きます。欠損値やダミー変数の扱いも含めて進めて行きます。 

出典: フリー百科事典『ウィキペディア(Wikipedia)』
機械学習の教科書問題なのでインターンが来てくれる様になればkaggleに参加する事とその経緯をブログに纏めて貰う事を最初の問題にしたいと思います。


2022年2月23日水曜日

色々な盤を作って来た訳で…

 もちろん太陽光関連の特殊な盤をはじめ、水力発電所の力率調整器、スターリングエンジンを使ったバイオマス制御盤、海外の太陽光キュービクル。

主要な部品が入手困難になってきて得意な商品の生産が難しくなったので他に切替えると行ってもそう簡単にいかないとはいえ何もしないと前には進みません。

といってなんでもやりますと営業した所で足元をみられるだけです。作りたいものを作るのではなく作って欲しいものを作るのが望まれる訳です。

社内はいろいろなデータが保存されています。データサイエンスはそのデータから価値を見出すのです。

例えば今まで作った図面を閲覧出来る様にすれば可能性を信じていくらかの新規案件が来るかもしれません。ある程度纏まって出来ない時間にできる事を模索する時なのかもしれません。

もちろん図面を模倣される為に情報を開示すれば良い訳ではありません。例えば図面をサムネイル化してイメージを掴む程度で公開すればデータも軽く多く閲覧出来、データを有効活用出来ます。

それぞれを分類して検索しやすくするのも工夫が必要で手間も時間も掛かります。


2022年2月10日木曜日

学習データの変換方法。

作業で再現するのも大変ですが、これを一から作るのはもっと大変です。

 もちろんすんなりいきません。

作ったときと今とではバージョンが違うのでそれだけでも一苦労です。

ここまで来るのも一苦労、これからまだ作業と検証が必要。


2回目以降の手順シェルスクリプトで

source activate yad2k

keras-yolo3-masterフォルダに入ってから

python convert.py wiwao_train.cfg wiwao_train_6000.weights model_data/murou.h5

2回目はconda activate downgradeしてから

cd yolo_coreml

coreml_model = coremltools.converters.keras.convert('/Users/wiwao/Documents/keras-yolo3-master/model_data/murou.h5', input_names='input1', image_input_names='input1',  image_scale=1/255.)

一旦今のファイルを消す

(base) wiwaoMacBookPro:bin wiwao$ sudo rm -i w_t.mlmodel

それから新規ファイルをコピーする。

sudo cp w_t.mlmodel /Applications/Xcode.app/Contents/Developer/usr/bin

python coreml.py


pip でインストールすると仮想環境では使えない。yolov3はpython3.7でtensorflow==1.14でないと使えない。


git clone https://github.com/allanzelener/yad2k.git

cd yad2k

To replicate the conda environment:

conda env create -f environment.yml

で重なっているファイルは全て消す

ResolvePackageNotFound: 

 

結局下記でインストール出来た。

name: yad2k

channels:

- defaults

dependencies:

- pip:

  - keras==2.0.3

  - protobuf==3.2.0

  - pydot-ng==1.0.0


  - theano==0.9.0

prefix: /home/wiwao/anaconda3/envs/yad2k

prefixは上記の様に自分の環境に合わせる。


source activate yad2k

YAD2Kも下記の様にバージョン指定して必要なものは合わせてインストール

conda install keras==2.2.4

conda install -c anaconda tensorflow==1.14


keras-yolo3-masterフォルダに入ってから、以降もanaconda環境のYAD2K中で作業を行います。


まずconvert.py を実行しますが、このままではエラーが出てしまうので、convert.pyの88行目をinput_layer = Input(shape=(416, 416, 3))に変更します。


ウェイトをダンロードしてから下記を必要に応じて変更

python convert.py yolov3.cfg yolov3.weights model_data/yolo.h5

次に、同じディレクトリへ該当するweightsファイル、そして学習で使ったcfgファイルを入れておきます。

cfgファイルのはじめに設定しているパラメーターを bach=1 subdivisions=1に変更しておき、以下のコマンドで 新たにh5ファイルを作成します。cfg、weightファイル名はコピーしたそれぞれのファイル名に変更し、.h5ファイルは好きな名称を付けます。

この例ではdataフォルダにh5ファイルができます。


conda の(downgrade) でpythonのバージョンを3.7.11に落として

Apple 提供のcoremltoolsをインストールします

anaconda install coremltools

新たにフォルダを作成し(yolo_coremlと仮定)

mkdir yolo_coreml

cd yolo_coreml


wiwaoMacBookPro:yolo_coreml wiwao$ conda install coremltools

conda install keras==2.2.4

conda install -c anaconda tensorflow==1.14


仮想環境を行き来するせいか絶対パスに書き換える


import coremltools

 

coreml_model = coremltools.converters.keras.convert('/Users/wiwao/Documents/keras-yolo3-master/model_data/yolo.h5', input_names='input1', image_input_names='input1',  image_scale=1/255.)

coreml_model.input_description['input1'] = 'Input image'

 

coreml_model.author = 'Original paper: Joseph Redmon, Ali Farhadi'

coreml_model.license = 'Public Domain'

coreml_model.short_description = "The YOLOv3 network from the paper 'YOLOv3: An Incremental Improvement'"

 

coreml_model.save('w_t.mlmodel')


python coreml.py

の実行で、フォルダ内にw_t.mlmodelファイルが作成されます。多少時間がかかります。

## Package Plan ##


  environment location: /Users/wiwao/opt/anaconda3/envs/downgrade


  added / updated specs:

    - coremltools



The following packages will be downloaded:


    package                    |            build

    ---------------------------|-----------------

    coremltools-4.1            |   py37hf099865_1         2.9 MB

    ------------------------------------------------------------

                                           Total:         2.9 MB


The following NEW packages will be INSTALLED:


  coremltools        pkgs/main/osx-64::coremltools-4.1-py37hf099865_1


The following packages will be UPDATED:


  ca-certificates    anaconda::ca-certificates-2020.10.14-0 --> pkgs/main::ca-certificates-2021.10.26-hecd8cb5_2

  certifi                anaconda::certifi-2020.6.20-py37_0 --> pkgs/main::certifi-2021.10.8-py37hecd8cb5_2



Proceed ([y]/n)? y


cp w_t.mlmodel /Applications/Xcode.app/Contents/Developer/usr/bin

/Applications/Xcode.app/Contents/Developer/usr/bin/coremlc compile w_t.mlmodel /Users/wiwao/yolo_coreml


(base) wiwaoMacBookPro:bin wiwao$ /Applications/Xcode.app/Contents/Developer/usr/bin/coremlc compile w_t.mlmodel /Users/wiwao/yolo_coreml

/Users/wiwao/yolo_coreml/w_t.mlmodelc/coremldata.bin

/Users/wiwao/yolo_coreml/w_t.mlmodelc/model.espresso.weights

/Users/wiwao/yolo_coreml/w_t.mlmodelc/model.espresso.shape

/Users/wiwao/yolo_coreml/w_t.mlmodelc/model.espresso.net

/Users/wiwao/yolo_coreml/w_t.mlmodelc/model/coremldata.bin

/Users/wiwao/yolo_coreml/w_t.mlmodelc/neural_network_optionals/coremldata.bin

(base) wiwaoMacBookPro:bin wiwao$ 

How to make YOLOv3 Pre-learned files.

ここからは十分に経験と知識のある方、NVIDIAGPUを積んだLinuxマシンが動く環境が使える方が対象です。最初は非常に複雑なインストール作業を伴いますが、2回目以降はさほどでもありません。概要は以下の通りです。内容を理解した段階でMac側の一連の作業をPython等にまとめると効率が上がります。

linuxマシンで学習->学習済みweightsファイルをmacにコピー->h5ファイルに変換->coremlファイルに変換->coremlcファイルに変換->txtファイルを3つ作成-->zipに圧縮 

From here, it is for those who have sufficient Deep learning experience and knowledge, and who can use an environment where Linux machines with NVIDIA GPUs can run. The first involves a very complex installation(according to https://github.com/AlexeyAB/darknet), but not much after the second time. The outline is as follows. When you understand the contents, you can improve the efficiency by grouping a series of tasks on the Mac side into Python.

Learning on linux machine-> Copy learned weights file to mac-> Convert to h5 file-> Convert to coreml file-> Convert to coremlc file-> Create three txt files-> Compress to zip

4.1 録画した赤外線画像データをアノテーションする。Annotating LEPTON images and learning

学習データ画像はThermal Cam3xで録画したファイルを使います。アノテーション及び学習方法はこのページを参照して下さい。

YOLOオリジナルデータの学習 - Take’s diary

YOLOオリジナルデータの学習その2(追加学習) - Take’s diary

さらに詳しい内容は以下URLを参考にして下さい。

darknet/cfg at master · AlexeyAB/darknet · GitHub

 

iOSで実行するためには特に以下の設定が必要です。

  • 設定ファイル(CFGファイル)は yolov3-voc.cfg又はyolov3.cfgを使用します。
  • 入力画像の大きさは with=416 heights=416 限定です。
  • YOLOv3.cfgの設定ファイルは参考ページにも説明していますが、classがらみで合計10カ所以上の変更が必要です
  • 学習の収束を早めるための初期weightsファイルは、darknet53.cov.74をダウンロードして使用します。

The learning data image uses a file recorded with Thermal Cam3x. Please refer to this page for annotation and learning methods.

YOLOオリジナルデータの学習 – Take’s diary

YOLOオリジナルデータの学習その2(追加学習) – Take’s diary

Please refer to the following URL for further details.

darknet/cfg at master · AlexeyAB/darknet · GitHub

In order to run on iOS, the following settings are required.

Use yolov3-voc.cfg or yolov3.cfg as the configuration file (CFG file).

The size of the input image is limited to with = 416 heights = 416.

The configuration file of YOLOv3.cfg is also explained on the reference page, but it is necessary to change a total of 10 or more locations in relation to the class

Download and use darknet53.cov.74 as the initial weights file to speed up learning convergence.

4.2 MacにYAD2Kをインストールする。Install YAD2K on your Mac.

以下python3.7用のanaconda環境を作り、YAD2K及びkerasをインストールします。インストール方法は各々冒頭に記述されています。ただし最初のKerasインストール時はTensorflowインストールコマンドのみ conda install -c anaconda tensorflow==1.14 つまりtensorflowのバージョンは1.14にして下さい。

Create Anaconda environment for python3.7 below and install YAD2K and keras. Each installation method is described at the beginning. However, when installing Keras for the first time, only the Tensorflow installation command is used. Conda install -canaconda tensorflow == 1.14 That is, tensorflow version should be 1.14.

GitHub - allanzelener/YAD2K: YAD2K: Yet Another Darknet 2 Keras

GitHub - qqwweee/keras-yolo3: A Keras implementation of YOLOv3 (Tensorflow backend)

4.3 Kares を使ってYOLO学習済みファイルをコンバートする Convert files created in 4.3 to mlmodel

keras-yolo3-masterフォルダに入ってから、以降もanaconda環境の中で作業を行います。

まずconvert.py を実行しますが、このままではエラーが出てしまうので、convert.pyの88行目をinput_layer = Input(shape=(416, 416, 3))に変更します。

次に、同じディレクトリへ該当するweightsファイル、そして学習で使ったcfgファイルを入れておきます。

cfgファイルのはじめに設定しているパラメーターを bach=1 subdivisions=1に変更しておき、以下のコマンドで 新たにh5ファイルを作成します。cfg、weightファイル名はコピーしたそれぞれのファイル名に変更し、.h5ファイルは好きな名称を付けます。

この例ではdataフォルダにh5ファイルができます。

After entering the keras-yolo3-master folder, the following work is also performed in the anaconda environment.

First of all, convert.py is executed, but an error will occur if this is done, so change the 88th line of convert.py to input_layer = Input (shape = (416, 416, 3)).

Next, put the corresponding weights file in the same directory and the cfg file used for learning.

Change the parameter set at the beginning of the cfg file to bach = 1 subdivisions = 1, and create a new h5 file with the following command. Change the cfg and weight file names to the copied file names, and give the .h5 file a name of your choice.

In this example, an h5 file is created in the data folder.

./convert.py yolov3.cfg yolov3.weights model_data/yolo.h5

4.4 4.3でできたファイルをmlmodelに変換する

Apple 提供のcoremltoolsをインストールします

pip install coremltools

新たにフォルダを作成し(yolo_coremlと仮定)

mkdir yolo_coreml

cd yolo_coreml

以下のpythonプログラムを作成します

この中で3行目の ../../keras-yolo3-master/model_data/yolo.h5 は先ほど作成したh5ファイルを指定して下さい。

Install coremltools provided by Apple

pip install coremltools

Create a new folder (assuming yolo_coreml)

mkdir yolo_coreml

cd yolo_coreml

Create the following python program

In this, please specify the h5 file created earlier for ../../keras-yolo3-master/model_data/FLIR_J.h5 on the third line.

coreml.py

--------------------------------------------------------------------------------------------------

import coremltools

 

coreml_model = coremltools.converters.keras.convert('../../keras-yolo3-master/model_data/yolo.h5', input_names='input1', image_input_names='input1',  image_scale=1/255.)

coreml_model.input_description['input1'] = 'Input image'

 

coreml_model.author = 'Original paper: Joseph Redmon, Ali Farhadi'

coreml_model.license = 'Public Domain'

coreml_model.short_description = "The YOLOv3 network from the paper 'YOLOv3: An Incremental Improvement'"

 

coreml_model.save('w_t.mlmodel')

 --------------------------------------------------------------------------------------------------

python coreml.py

の実行で、フォルダ内にw_t.mlmodelファイルが作成されます。多少時間がかかります。

python coreml.py

Execution will create a w_t.mlmodel file in the folder.

4.5 標準では32ビットデータなのでデータ容量を減らしたい場合は16bitに変換する Standard is 32-bit data, so if you want to reduce the data capacity, convert it to 16-bit

16bitモデルは認識率が多少落ちるので、用途に応じて対応して下さい。16bitモデルに変更する場合は以下のpythonプログラムを作成して実行します。

The 16bit model has a slightly lower recognition rate. To change to the 16bit model, create and execute the following python program.

convert16.py

--------------------------------------------------------------------------------------------------

import coremltools

model_spec = coremltools.utils.load_spec('./w_t.mlmodel')

model_fp16_spec = coremltools.utils.convert_neural_network_spec_weights_to_fp16(model_spec)

coremltools.utils.save_spec(model_fp16_spec, 'w_t.mlmodel')

--------------------------------------------------------------------------------------------------

4.6 mlmodelをmlmodelcに変換する Convert mlmodel to mlmodelc

mlmodelコンパイルしてmlmodelcファイル(実はフォルダ)を作成します。必ず最終ファイル名はw_t.mlmodelcであることを確認して下さい。

Compile mlmodel and create mlmodelc file (actually folder). Make sure that the final file name is w_t.mlmodelc.

/Applications/Xcode.app/Contents/Developer/usr/bin/coremlc compile w_t.mlmodel ./

4.7 さらに3つのファイルを用意する Prepare 3 more files

w_t.names   : 学習に使ったクラス名称ファイルで、ファイル名称を変更して使用します。

w_t.txt   :「3.認識結果アラートの設定方法」を参考にして下さい・

REDME.txt :名称、内容とも任意で、実際には使用しません。今回の作業ファイルはすべて同じ名称にしなければないので、後から確認できるようにデータ内容の説明を記入して下さい。

w_t.names : The class name file used for learning is used by changing the file name.

w_t.txt        : Refer to “3. Setting method of recognition result alert”.

REDME.txt : Name and content are optional and are not actually used. All work files must have the same name, so please enter a description of the data so that you can check it later.

4.8 合計4つのファイルを新たに作成したw_tフォルダに移動する Move a total of 4 files to the newly created w_t folder

w_tフォルダを作成して、中にw_t.mlmodelcフォルダ、w_t.names、w_t.txt.、README.txtをコピーします。

Create a w_t folder and copy the w_t.mlmodelc folder, w_t.names, w_t.txt., and README.txt into it.

4.9 フォルダをzipに変換して自分のアカウントを持つicloudへuplodeする Convert folder to zip and uplode to icloudwith your account

w_tフォルダをzipに圧縮します。iPhoneから一番使いやすいダウンロード先はiCloudです。今回のThermal Cam3xはフォルダ名がw_t限定なので、モデル毎にフォルダ管理して下さい。

Compress the w_t folder to zip. The easiest download location for iPhone is iCloud. Since this Thermal Cam3x folder name is limited to w_t, please manage folders for each model.