神经计算棒使用步骤

1. GRAPH_PATH:graph文件路径;

2. IMAGE_PATH:要分类的图片的路径;

3. IMAGE_DIM:由选择的神经网络定义的图像尺寸;  例:GoogLeNet uses 224x224 pixels, AlexNet uses 227x227 pixels

4. IMAGE_STDDEV:由选择的神经网络定义的标准差(标度值) 例:GoogLeNet uses no scaling factor, InceptionV3 uses 128 (stddev = 1/128)

5. IMAGE_MEAN: 平均减法是深度学习中常用的一种技术,用于对数据进行中心处理  

例:ILSVRC dataset, the mean is B = 102 Green = 117 Red = 123

使用NCS做图像分类的5个步骤:

 mvnc库中引入mvncapi模块

import mvnc.mvncapi as mvnc

Step 1:

   NCS插入应用处理器(Ubuntu笔记本电脑/台式机)USB端口时,它将自身列为USB设备。将调用API来查找枚举的NCS设备:

    # Look for enumerated Intel Movidius NCS device(s); quit program if none found.

devices = mvnc.EnumerateDevices()

if len( devices ) == 0:

print( 'No devices found' )

quit()

  如果插入了多个NCS,还需要选择一个NCS并打开:

    # Get a handle to the first enumerated device and open it

device = mvnc.Device( devices[0] )

device.OpenDevice()

Step2:加载graph文件到NCS

    # Read the graph file into a buffer

with open( GRAPH_PATH, mode='rb' ) as f:

blob = f.read()

 

# Load the graph buffer into the NCS

graph = device.AllocateGraph( blob )

Step3: 将单个图像加载到Intel Movidius NCS上以运行推理

图像预处理:

1. 调整图像大小/裁剪图像以匹配预先训练的网络定义的尺寸。

        GoogLeNet uses 224x224 pixels, AlexNet uses 227x227 pixels.

2. 每个通道的平均值(蓝色,绿色和红色)从整个数据集中减去。这是深度学习中常用的一种技术,可以集中数据。

3. 将图像转换为半精度浮点数(fp16)数组,并使用LoadTensor函数调用将图像加载到NCS上。skimage库可以在一行代码中完成此操作。

# Read & resize image [Image size is defined during training]

img = print_img = skimage.io.imread( IMAGES_PATH )

img = skimage.transform.resize( img, IMAGE_DIM, preserve_range=True )

 

# Convert RGB to BGR [skimage reads image in RGB, but Caffe uses BGR]

img = img[:, :, ::-1]

 

# Mean subtraction & scaling [A common technique used to center the data]

img = img.astype( numpy.float32 )

img = ( img - IMAGE_MEAN ) * IMAGE_STDDEV

 

# Load the image as a half-precision floating point array

graph.LoadTensor( img.astype( numpy.float16 ), 'user object' )

Step4: NCS读取并打印推理结果

    # Get the results from NCS

output, userobj = graph.GetResult()

 

# Print the results

print('\n------- predictions --------')

labels = numpy.loadtxt( LABELS_FILE_PATH, str, delimiter = '\t' )

order = output.argsort()[::-1][:6]

for i in range( 0, 5 ):

print ('prediction ' + str(i) + ' is ' + labels[order[i]])

 

# Display the image on which inference was performed

skimage.io.imshow( IMAGES_PATH )

skimage.io.show( )

Step 5: 卸载图形并关闭设备

为了避免内存泄漏和/或分段错误,我们应该关闭所有打开的文件或资源并释放所有使用的内存。

graph.DeallocateGraph()

device.CloseDevice()

原文: Build an Image Classifier in 5 steps

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值