VSCode + Remote - SSHでRaspberry Pi開発
Visual Studio Code拡張のRemote Developmentの中の1つ、Remote - SSHが、知らぬ間にARM/ARM64に対応していたので、どんな感じで動くか試しました。
VSCode拡張 Remote Development
リモートマシンのフォルダ(ディレクトリ)やDockerコンテナを、都度ファイルコピーとかの操作をすることなく遠隔で表示、編集などをするための、VSCode拡張パックです。
これはパックで、中身は3つのVSCode拡張が入っています。
- Remote - SSH
物理/仮想のリモートマシンにSSHで接続して、ソースコードを表示、編集などをすることができます。 - Remote - Containers
Dockerコンテナ (略 - Remote - WSL
WSL(Windows Subsystem for Linux) (略
Remote DevelopmentできるLinux
リリース当初はx86_64しか対応していなかったと思うのですが、、、
なにげなくドキュメントを見ていたら、ARM/ARM64が追加されていました!!
こりゃ、いいじゃん!
ってことで、試すことに。
いざ接続!
VSCodeを最新(1.38.0)にして、Remote - SSHが最新(0.46.0)になっていることを確認。
configをこんな感じに設定して、
Remote ExplorerでRaspberry Piに接続しようとすると、自動的にVSCode ServerをRaspberry Piにインストールされて、
Raspberry Piのファイルを、母艦から編集できるようになりました。
IntelliSense
何故か、IntelliSenseが機能しない。
VSCodeのExtensionsを見ると、SSH: RASPIとLOCALに分かれていて、LOCALのC# ExtensionにInstall in SSH: raspi
の文字が。
なんだろ、VSCode Server側にExtensionが入る必要があるのかな?
Install in SSH: raspi
をクリックしてみたところ、SSH: RASPIのところにC# Extensionが追加された、
けど、IntelliSense機能せずorz
Terminal
ターミナルは普通に動きます。
Debugging
うーむ、launch.jsonをがんばって設定してみるが、動かん。
余談
言語がC#(NETCore)じゃなくてPythonだと、デバッグもできるみたい。(Scott Hanselmanのブログ)
.NET Core 3.0 SDKをRaspberry Piで動かしてみた
OSインストール
最新のRaspbianをダウンロードしてmicroSD焼きます。
現時点の最新、2019-07-10-raspbian-buster-lite.imgを使いました。
あと、sudo apt update
とsudo apt upgrade
も忘れずに実行。
NETCore 3.0インストール
最新のNET Core 3.0をダウンロード、インストールします。
現時点の最新はv3.0.100-preview9。
まずはダウンロード。
wget https://download.visualstudio.microsoft.com/download/pr/33387ff2-7687-4564-a90d-28b9539e0d3b/f75a063b80fc5a94a4d35689aee8b108/dotnet-sdk-3.0.100-preview9-014004-linux-arm.tar.gz
RaspbianはARM32なので、Linuxの.NETCore BinariesにあるARM32をクリックすると、ダウンロードURLを得ることができます。
約113Mbytesのファイルでした。
次に、ダウンロードしたファイルを解凍します。
mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-3.0.100-preview9-014004-linux-arm.tar.gz -C $HOME/dotnet
最後に、環境変数DOTNET_ROOTとPATHに、解凍したディレクトリを設定します。
これは.bashrcの最後に以下を追加してログインし直し。
export DOTNET_ROOT=$HOME/dotnet export PATH=$PATH:$HOME/dotnet
Hello World!
.NETCoreでコンパイル、実行できるか確認します。
dotnet new console -o myApp cd myApp dotnet run
Hello World!と表示されれば正常です。
Single-file executables
2回目以降でも、相変わらず起動に時間がかかるのですが、
.NETCore3から使えるようになった(?)、Single-file executablesで、どれくらい起動時間が短縮されるか見てみましょう。
dotnet publish /p:PublishSingleFile=true
で実行ファイルを生成すると、
エラーorz
あ、-r
オプションでRuntime Identifierというのを指定する必要があるみたい。
気を取り直して、リトライ。
dotnet publish -r linux-arm /p:PublishSingleFile=true
できたっぽい。
起動がクッソ速くなった。
仕組みについては、ここに書いてあるようです。
ML.NET Object Detection
ML.NET Samplesにある、Object Detectionを動かしてみました。
Object Detectionサンプル
学習済みモデルTiny Yolo2を.NET Coreコンソールアプリ(+ML.NET)で動かすサンプルプログラムです。
実行
Visual StudioでObjectDetection.slnを開いて実行するだけ。
こんな感じで、画像に含まれるオブジェクトをコンソールに表示します。
=====Identify the objects in the images===== .....The objects in the image image1.jpg are detected as below.... car and its Confidence score: 0.9697261 car and its Confidence score: 0.6674223 person and its Confidence score: 0.5226038 car and its Confidence score: 0.5224892 car and its Confidence score: 0.4675334 .....The objects in the image image2.jpg are detected as below.... cat and its Confidence score: 0.6461141 cat and its Confidence score: 0.6400049 .....The objects in the image image3.jpg are detected as below.... chair and its Confidence score: 0.8405777 chair and its Confidence score: 0.7963627 diningtable and its Confidence score: 0.6056049 diningtable and its Confidence score: 0.3737413 .....The objects in the image image4.jpg are detected as below.... dog and its Confidence score: 0.7608147 person and its Confidence score: 0.6321325 dog and its Confidence score: 0.5967442 person and its Confidence score: 0.5730396 person and its Confidence score: 0.5551758 ========= End of Process..Hit any Key ========
また、assets/images/output フォルダに、オブジェクトを検知した結果画像を保存します。
処理時間
ここかな?というところに、Stopwatchを追加して測りました。
var sw = new System.Diagnostics.Stopwatch(); sw.Restart(); IList<YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i); sw.Stop(); Console.WriteLine($"Elapsed time = {sw.ElapsedMilliseconds}");
Surface Pro 4(Core i7-6650U@2.20GHz)で、この結果でした。
Elapsed time = 1485 .....The objects in the image image1.jpg are detected as below.... Elapsed time = 1300 .....The objects in the image image2.jpg are detected as below.... Elapsed time = 1299 .....The objects in the image image3.jpg are detected as below.... Elapsed time = 1277 .....The objects in the image image4.jpg are detected as below....
Learn ML.NETのGet started in 10 minutesをやってみた
某氏と話していて「ML.NETいけるんじゃね?」(←だいぶ省略)と興味が湧いてきたのだが、Machine Learningよくわからないマンなので、、、
とりあえず、Get startedがあったので、Windowsでやってみました。
- Visual Studioをインストール → VS2019 16.2.3
- ML.NET Model Builder拡張をインストール → 16.0.1908.1402
- コンソールアプリ(.NET Core)プロジェクトを新規作成 → このときにソリューションとプロジェクトを同じディレクトリに配置しないこと
- プロジェクトにMachine Learningを追加 → Sentiment Analysisを選択
Time to trainの時間内に、さまざまなModelを試すみたい。
10秒
30秒
60秒
最後にCodeを選択すると、自動的にソリューションにプロジェクトが2つ追加されました。
ML.NET Model BuilderでTrainして、その結果がmyMLAppML.Modelライブラリとして生成されているようです。
で、myMLAppML.Modelライブラリを利用して推論するサンプルアプリがmyMLAppML.ConsoleAppアプリ。
myMLAppML.ConsoleAppアプリのProgram.csを見ると、
MLContext mlContext = new MLContext(); ITransformer mlModel = mlContext.Model.Load(GetAbsolutePath(MODEL_FILEPATH), out DataViewSchema inputSchema); var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
で、MODEL_FILEPATHからpredEngineを生成して、
ModelInput sampleData = CreateSingleDataSample(mlContext, DATA_FILEPATH); ModelOutput predictionResult = predEngine.Predict(sampleData);
で、ModelInputをpredEngineに通してModelOutputを得ています。
MODEL_FILEPATHはMLModel.zipを指しており、これが学習済みモデルのようです。
MLModel.zipの中身は、
なんだろ?
ML.NETのクラスをシリアライズした感じなのかな?
Visual Studioさえ入っていれば、VS拡張でササッとできてお手軽でした。
【メモ】IoT Plug and Playのデバイス実装コード
IoT Plug and PlayのAZ3166向けサンプルコードを調査。
★ ... Azure IoT SDK for C
☆ ... Azure IoT Device Workbench生成コード
main.ino
setup() connectIoTService() registerDeviceRetry() registerDevice() IoTHub_Init()★ prov_dev_set_symmetric_key_info()★ prov_dev_security_init()★ Prov_Device_LL_Create()★ Prov_Device_LL_Register_Device()★ Prov_Device_LL_DoWork()★ IoTHub_Deinit()★ pnp_device_initialize()☆ ---> pnp_device.h/.c loop() pnp_device_run()☆ ---> pnp_device.h/.c is_error_max()☆ invokeDevKitPeripheral()
pnp_device.h/.c
pnp_device_initialize() tickcounter_create()★ DigitalTwinClientHelper_InitializeDeviceHandle()☆ IoTHub_Init()★ IoTHubDeviceClient_LL_CreateFromConnectionString()★ DigitalTwin_DeviceClient_LL_CreateFromDeviceHandle()★ DeviceinfoInterface_Create()☆ DigitalTwin_InterfaceClient_Create()★ SensorsInterface_Create()☆ DigitalTwin_InterfaceClient_Create()★ LedsInterface_Create()☆ DigitalTwin_InterfaceClient_Create()★ DigitalTwin_InterfaceClient_SetCommandsCallback()★ ScreenInterface_Create()☆ DigitalTwin_InterfaceClient_Create()★ DigitalTwin_InterfaceClient_SetCommandsCallback()★ SettingsInterface_Create()☆ DigitalTwin_InterfaceClient_Create()★ DigitalTwin_InterfaceClient_SetPropertiesUpdatedCallback()★ DigitalTwinClientHelper_RegisterInterfacesAndWait()☆ DigitalTwin_DeviceClient_LL_RegisterInterfacesAsync()★ DigitalTwin_DeviceClient_LL_DoWork()★ DigitalTwinClientHelper_Check()☆ DigitalTwin_DeviceClient_LL_DoWork()★ DeviceinfoInterface_Property_ReportAll()☆ tickcounter_get_current_ms()★ pnp_device_run() SensorsInterface_Telemetry_SendAll()☆ DigitalTwinClientHelper_Check()☆ pnp_device_close()
digitaltwin_device_client_ll.h
DigitalTwin_DeviceClient_LL_CreateFromDeviceHandle() DigitalTwin_DeviceClient_LL_RegisterInterfacesAsync() DigitalTwin_DeviceClient_LL_DoWork() DigitalTwin_DeviceClient_LL_Destroy()
digialtwin_interface_client.h
DigitalTwin_InterfaceClient_Create() DigitalTwin_InterfaceClient_SetPropertiesUpdatedCallback() DigitalTwin_InterfaceClient_SetCommandsCallback() DigitalTwin_InterfaceClient_SendTelemetryAsync() DigitalTwin_InterfaceClient_ReportPropertyAsync() DigitalTwin_InterfaceClient_UpdateAsyncCommandStatusAsync() DigitalTwin_InterfaceClient_Destroy()
考察
- スケッチ -> Azure IoT Device Workbench生成コード -> Azure IoT SDK for C
- 接続は、Privisoning -> IoTHubDeviceClient -> DigitalTwinDeviceClient
- 低レベルは従来のIoTHubDeviceClientで、その上にDigitalTwinDeviceClientの模様
- (Device Capability Modelから)Azure IoT Device Workbenchで生成したコードを使うと、項目毎のコールバック実装になる。(mxchip_iot_devkit_impl.h/.c)
Mbed Studioを使ってみた
前提条件
Windowsの場合は、Gitをインストールしておく必要があります。参照
サポートしているボード
今は、Mbed Enabledのボード全てではなく、一部のボードのみ対応しているようです。参照
何気に、Seeed Wio 3Gも入っていますね!
STMのNucleo/Discoveryシリーズが少ないのは意外でした。
インストール
ここからダウンロードして実行するだけ。
現時点のダウンロードファイルはMbedStudio-0.5.3.exe
で、約800MBでした。
Getting Started
使い方をさらっと学ぶために、Getting startedをやってみましょう。
- ワークスペースを指定
- プログラムを作成(
mbed-os-example-blinky
を選択) - ターゲットを選択(
FRDM-K64F(K64F)
を選択) - プロファイルを選択(
Debug
を選択) - ビルド
- 実行
うーん、GUI快適♪
感じたこと
- mbed-cliも簡単にインストールできるようになっているようですが、こちらのほうがツールが別々にインストールされず1パックになっているので良さげ。
- やっぱGUI使いやすい。
- mbed-osのインポートやバージョン切り替えがメチャ早。(メニューにLibrary cacheという文字があるが何者か分からず。mbed-cliのrepository cachingのことかな?)
- VSCodeのExtensionを追加することができない。これは悲しい。
結論は、かなりオススメなツールでした。
しばらく使ってみようと思います。