RaspberryPis Tensorflow 分布式训练1

为什么 目前自己采购了35块树莓派4core4G用来计算Rosetta@home,等到COVID-2019过去之后,会用这些开发板测试TF项目,因为TF的生态链齐全,所以在工业上会考虑使用TF来做为最终的产品技术使用方案。 未来的趋势会面向边缘计算领域,像自动驾驶,智能家居,家庭医疗辅助系统,农业生产,制造业零部件质量检测,工业机械磨损检测等等,都会考虑到数据的实时接受和传输,还有计算成本,在机器学习上,如果依靠云计算平台,去辅助上述这些项目,那么就需要考虑本地到服务中心的网络,带宽延迟,数据安全性,计算实时性的问题。如果依托边缘计算,采用工业的微控制器,在微控制器上部署模型以及Tensorflow Lite用于模型的推演,在本地解决计算高可用问题,无需将数据传递到公网上,减少带宽的消耗,从而降低计算成本。 Tensorflow aarch64 源码构建 安装依赖 apt-get install libatlas3-base libopenblas-dev libopenblas-base libblas-dev gcc gfortran python3-dev libgfortran5 g++ libhdf5-dev libfreetype-dev build-essential openjdk-11-jdk zip unzip python3-h5py python3-numpy python3-pip sudo pip3 install keras_preprocessing keras_applications 安装 Bazel install-compile-bootstrap-unix bazel 官方未给出arm64架构的二进制文件,所以需要自己手工编译 下载 bazel-2.0.0-dist.zip 运行 EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" bash ./compile.sh 拷贝 output/bazel 到 /usr/local/bin/hazel 编译 Tensorflow Raspberry pi 4B 上安装的系统是Ubuntu 20.04 ARM64架构,Python Version: 3.8,Tensorflow官方未给出对应版本的python whl安装包,所以需要自己手动从源码构建 git clone https://github.com/tensorflow/tensorflow.git git checkout v2.2.0 ./configure 配置选项 编译操作之前需要增加swap分区,4G系统内存编译是完全不够的,建议swap设置6G 最好能够单独增加一块USB3转SATA的移动硬盘用来单独增加SWAP分区 fallocate -l 6G /swapfile chmod 0600 /swapfile mkswap /swapfile swapon /swapfile 执行编译操作 bazel build --config=noaws --config=nogcp --config=nohdfs --config=nonccl --config=monolithic --config=v2 --local_cpu_resources=3 //tensorflow/tools/pip_package:build_pip_package 由于是直接在4核4G的Raspberry pi 上构建,所以需要耐心等待,编译时间大概在 15 - 25 小时之间 ☕️☕️☕️ 编译完成之后执行构建pip安装包 bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg tensorflow-2.2.0-cp38-cp38-linux_aarch64.whl 安装包构建完成 最后执行 pip install tensorflow-2.2.0-cp38-cp38-linux_aarch64.whl TF 测试 使用官方的最简MNIST教程中的代码 ...

June 3, 2020 · 5 min · alexchen

认识 Goold Coral Dev Board

参考文档: Coral Dev Board 硬件配置与布局 Edge TPU SOM Quad Core ARM Cortex A53 Google Edge TPU ML accelerator 8GB eMMC 1GB LPDDR4 OS Mendel Linux Video 39Pin MIPI DSI Display 24Pin MIPI CSI-2 Camera VPU (Video Processing Unit) 4K p60 H.265 decoder 4K p60 && 4K p30 AVC/H.264 decoder 1080 p60 MPEG-2, MPEG-4p2, VC-1, VP8, RV9, AVS, MJPEG, H.263 decoder 图片取自Coral官网 GPIO 布局 文档 原型设计和工业生产环境 Coral的产品线 原型设计: CoralDev Board 开发板 ...

May 31, 2020 · 1 min · alexchen

Mathemtica Machine Learning Convolutional Neural Networks(1)

Convolutional neural networks About convolution can be abstracted into mathematical expressions: I can represent the matrix of an image, K is the convolution kernel The computational layer of the convolution Object Recogition with Gradient-Based Learning input layer Input Layer Convolution Layer Conv Layer Restricted Layer RELU Layer pooling layer Pooling Layer full connected layer Creating a network Basic learning to distinguish between dogs and cats (* Create model *) myCatDogModel = NetChain[{ ConvolutionLayer[32, 3], Ramp, PoolingLayer[2, 2], ConvolutionLayer[64, 3], Ramp, PoolingLayer[2, 2], FlattenLayer[], 128, Ramp, 2, SoftmaxLayer[]}, "Input" -> NetEncoder[{"Image", {224, 224}, ColorSpace -> "RGB"}] , "Output" -> NetDecoder[{"Class", {"cat", "dog"}}]] (* Collect samples, training set and test set *) (* Convert data sets to Association format *) dataSetsConvert[dateSets_] := Module[ {}, File[#] -> StringSplit[FileBaseName[#], "."] [[1]] & /@ dateSets] traingDataFiles = RandomSample[dataSetsConvert[traingData]]; testDataFiles = RandomSample[dataSetsConvert[testData]]; SetDirectory[ "/Users/alexchen/datasets/Convolutional_Neural_Networks/dataset"]; FileNames["*.jpg", "training_set/cats/"]; FileNames["*.jpg", "training_set/dogs/"]; traingData = Join[FileNames["*.jpg", "training_set/cats/"], FileNames["*.jpg", "training_set/dogs/"]]; testData = Join[FileNames["*.jpg", "test_set/cats"], FileNames["*.jpg", "test_set/dogs"]]; (* Generate training and validation sets *) traingDataFiles = RandomSample[dataSetsConvert[traingData]]; testDataFiles = RandomSample[dataSetsConvert[testData]]; (* sample is randomly selected for display *) RandomSample[traingDataFiles, 5] (* train the model *) mytrainedModel = NetTrain[catdogModel, traingDataFiles, All, ValidationSet -> testDataFiles, MaxTrainingRounds -> 20] (* Generate training model evaluation report *) myModelPlot = mytrainedModel["FinalPlots"] (* Generate the trained model to calculate the model correctness *) myTrainedNet = mytrainedModel["TrainedNet"] ClassifierMeasurements[myTrainedNet, testDataFiles, "Accuracy"] ...

May 22, 2020 · 8 min · alexchen

jetson nano 初级入门

开启 Hello AI World 之旅 官方项目的源码地址 jetson nano 硬件配置 GPU架构: 128-core Maxwell CPU: Quad-core ARM A57 @ 1.43 GHz Memory: 4 GB 64-bit LPDDR4 25.6 GB/s Storage: microSD Card Video Encode: 4K @ 30 | 4x 1080p @ 30 | 9x 720p @ 30 (H.264/H.265) Video Decode: 4K @ 60 | 2x 4K @ 30 | 8x 1080p @ 30 | 18x 720p @ 30 (H.264/H.265) Camera: 2x MIPI CSI-2 DPHY lanes ...

May 18, 2020 · 1 min · alexchen

人工神经网络的反向传播-梯度下降算法

关于人工神经网络 人工神经网络模型判断手写数字 我们首先在Mathematica中徒手打造一个人工神经网络,然后对该神经网络进行训练,最后用训练得到的模型来判断图片上的手写数字是多少。 trainingData = ResourceData["MNIST", "TrainingData"]; testData = ResourceData["MNIST", "TestData"]; net = NetInitialize@NetChain[{ FlattenLayer[], LinearLayer[500], ElementwiseLayer["Sigmoid"], LinearLayer[100], ElementwiseLayer["Sigmoid"], LinearLayer[10], SoftmaxLayer[]}, "Input" -> NetEncoder[{"Image", {12, 12}, "ColorSpace" -> "Grayscale"}], "Output" -> NetDecoder[{"Class", Range[0, 9]}]] NetEncoder告诉我们输入的单通道12*12的图像 NetDecoder告诉我们我们的神经网络模型最后输出的是一个分类,类别分别是0-9中的一个数字 网络层的模型为: FlattenLayer[] 图像中的像素数据即两纬数据排列成一纬数据做为输入 LinearLayer[500] 第一层线性层,输出数据为500个,限定函数为Sigmoid LinearLayer[100] 第二层线性层,输出数据为100个,限定函数为Sigmoid LinearLayer[10] 最后一层输出,因为我们需要有10个分类,所以这里设定是10,限定函数为 Softmax 神经网络层形象的展示如下: 训练数据标记,左边是实际的手写数字,右边是对应标签 RandomSample[trainingData, 10] 在未对模型进行训练前,使用随机的权重来判断图片,我们可以确定错误率接近100% 对模型进行训练 trained = NetTrain[net, trainingData, ValidationSet -> testData, MaxTrainingRounds -> 50 ] 从以上图表中我们大致可以看出正确率已经接近97% 再次预测上述图片,模型已经可以正确预测给出的图片数字为6 measurements = ClassifierMeasurements[trained, testData] 运行 measurements[“Accuracy”] 我们可以得到模型的正确率为98.02% ...

May 9, 2020 · 1 min · alexchen