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.