zygalaxy 发表于 2024-7-12 10:43

Sipeed MAix BiT AIoT 开发套件测评-基础功能测试

<div class='showpostmsg'> 本帖最后由 zygalaxy 于 2024-7-12 17:31 编辑

# K210核心功能验证

## 时间规划
- **评测第三天至第四天**

## 内容要点

### CPU性能测试

- 运行一系列基准测试代码,以评估处理器的运算速度与效率。
这里使用MicroPython开发,所以主要在开发板上运行MicroPython的性能测试代码。

由于时间的限制就从github上面找了一个micropython benchmarks的仓库
这是原作者:(https://github.com/shaoziyang/micropython_benchmarks "https://github.com/shaoziyang/micropython_benchmarks")

这是下面的测试的代码:

需要注意的是因为是maixpy的固件,获取时钟频率的方法是
```python
from Maix import freq
try:
    print('System freq: {:.1f} MHz'.format(freq.get()))
except:
    print('System freq: {:.1f} MHz'.format(freq.get_cpu()))
```
注意自行在源代码中更改

```python
import time
import machine
import gc
from Maix import freq

def pi(places=100):
extra = 8
one = 10 ** (places+extra)
t, c, n, na, d, da = 3*one, 3*one, 1, 0, 0, 24

while t > 1:
    n, na, d, da = n+na, na+8, d+da, da+32
    t = t * n // d
    c += t
return c // (10 ** extra)

def pi_test(n = 5000):
    t1 = time.ticks_ms()
    t = pi(n)
    t2 = time.ticks_ms()
    r = time.ticks_diff(t2, t1)/1000
    print('Pi', n, 'digit calculation: ', r, 's')
    return '%.2f'%r

def int_add_test(n = 1000000, a = 12345, b = 56789):
    t1 = time.ticks_ms()
    sum = 0
    for i in range(n):
      sum = a + b
    t2 = time.ticks_ms()
    r = time.ticks_diff(t2, t1)/1000
    print('Integer Add test', n, 'times: ', r, 's')
    return '%.2f'%r

def float_add_test(n=1000000, a = 1234.5678, b = 5678.1234):
    t1 = time.ticks_ms()
    sum = 0
    for i in range(n):
      sum = a + b
    t2 = time.ticks_ms()
    r = time.ticks_diff(t2, t1)/1000
    print('Float Add test', n, 'times:', r, 's')
    return '%.2f'%r

def int_mul_test(n=1000000, a = 12345, b = 56789):
    t1 = time.ticks_ms()
    sum = 0
    for i in range(n):
      sum = a * b
    t2 = time.ticks_ms()
    r = time.ticks_diff(t2, t1)/1000
    print('Integer Mul test', n, 'times: ', r, 's')
    return '%.2f'%r

def float_mul_test(n=1000000, a = 1234.5678, b = 5678.1234):
    t1 = time.ticks_ms()
    sum = 0
    for i in range(n):
      sum = a * b
    t2 = time.ticks_ms()
    r = time.ticks_diff(t2, t1)/1000
    print('Float Mul test', n, 'times: ', r, 's')
    return '%.2f'%r

def int_div_test(n=1000000, a = 123456, b = 567):
    t1 = time.ticks_ms()
    sum = 0
    for i in range(n):
      sum = a // b
    t2 = time.ticks_ms()
    r = time.ticks_diff(t2, t1)/1000
    print('Integer Div test', n, 'times: ', r, 's')
    return '%.2f'%r

def float_div_test(n=1000000, a = 12345.678, b = 56.789):
    t1 = time.ticks_ms()
    sum = 0
    for i in range(n):
      sum = a / b
    t2 = time.ticks_ms()
    r = time.ticks_diff(t2, t1)/1000
    print('Float Div test', n, 'times: ', r, 's')
    return '%.2f'%r

def mem():
    r = gc.mem_free()
    print('free memory:', r)

print('Speed test')
try:
    print('System freq: {:.1f} MHz'.format(freq.get()))
except:
    print('System freq: {:.1f} MHz'.format(freq.get_cpu()))
       
print('\nCalcaulate integer addition')
gc.collect()
mem()
d1 = int_add_test()
d2 = int_add_test()
d3 = int_add_test()
r_int_add =min(d1, d2, d3)
print('Integer addition test result: ', r_int_add, 's')
mem()

print('\nCalcaulate float addition')
gc.collect()
mem()
d1 = float_add_test()
d2 = float_add_test()
d3 = float_add_test()
r_float_add = min(d1, d2, d3)
print('Float addition test result: ', r_float_add, 's')
mem()

print('\nCalcaulate integer multiplication')
gc.collect()
mem()
d1 = int_mul_test()
d2 = int_mul_test()
d3 = int_mul_test()
r_int_mul = min(d1, d2, d3)
print('Integer multiplication test result: ', r_int_mul, 's')
mem()

print('\nCalcaulate float multiplication')
gc.collect()
mem()
d1 = float_mul_test()
d2 = float_mul_test()
d3 = float_mul_test()
r_float_mul = min(d1, d2, d3)
print('Float multiplication test result: ', r_float_mul, 's')
mem()

print('\nCalcaulate integer division')
gc.collect()
mem()
d1 = int_div_test()
d2 = int_div_test()
d3 = int_div_test()
r_int_div = min(d1, d2, d3)
print('Integer division test result: ', r_int_div, 's')
mem()

print('\nCalcaulate float division')
gc.collect()
mem()
d1 = float_div_test()
d2 = float_div_test()
d3 = float_div_test()
r_float_div = min(d1, d2, d3)
print('Float division test result: ', r_float_div, 's')
mem()

print('\nCalcaulate Pi 1000 digit')
gc.collect()
mem()
try:
    d1 = pi_test(1000)
    d2 = pi_test(1000)
    d3 = pi_test(1000)
    r_pi_1000 = min(d1, d2, d3)
    print('1000 digit Pi calculation result: ', r_pi_1000, 's')
    mem()
except:
    r_pi_1000 = None
    print('calculation error')

print('Test result:')
print('Integer addition test result: ', r_int_add, 's')
print('Float addition test result: ', r_float_add, 's')
print('Integer multiplication test result: ', r_int_mul, 's')
print('Float multiplication test result: ', r_float_mul, 's')
print('Integer division test result: ', r_int_div, 's')
print('Float division test result: ', r_float_div, 's')
print('1000 digit Pi calculation result: ', r_pi_1000, 's')
```
跑完的结果如下:

通过数据能看出来CPU的性能是不错的

```python
>>> Speed test
System freq: 403.0 MHz

Calcaulate integer addition
free memory: 508064
Integer Add test 1000000 times:1.33 s
Integer Add test 1000000 times:1.331 s
Integer Add test 1000000 times:1.329 s
Integer addition test result:1.33 s
free memory: 507680

Calcaulate float addition
free memory: 507776
Float Add test 1000000 times: 2.616 s
Float Add test 1000000 times: 2.623 s
Float Add test 1000000 times: 2.62 s
Float addition test result:2.62 s
free memory: 411136

Calcaulate integer multiplication
free memory: 507616
Integer Mul test 1000000 times:1.522 s
Integer Mul test 1000000 times:1.504 s
Integer Mul test 1000000 times:1.521 s
Integer multiplication test result:1.50 s
free memory: 507328

Calcaulate float multiplication
free memory: 507552
Float Mul test 1000000 times:2.637 s
Float Mul test 1000000 times:2.634 s
Float Mul test 1000000 times:2.641 s
Float multiplication test result:2.63 s
free memory: 390848

Calcaulate integer division
free memory: 507488
Integer Div test 1000000 times:1.421 s
Integer Div test 1000000 times:1.37 s
Integer Div test 1000000 times:1.371 s
Integer division test result:1.37 s
free memory: 507200

Calcaulate float division
free memory: 507424
Float Div test 1000000 times:2.696 s
Float Div test 1000000 times:2.7 s
Float Div test 1000000 times:2.697 s
Float division test result:2.70 s
free memory: 366688

Calcaulate Pi 1000 digit
free memory: 507360
Pi 1000 digit calculation:0.126 s
Pi 1000 digit calculation:0.125 s
Pi 1000 digit calculation:0.122 s
1000 digit Pi calculation result:0.12 s
free memory: 218880
Test result:
Integer addition test result:1.33 s
Float addition test result:2.62 s
Integer multiplication test result:1.50 s
Float multiplication test result:2.63 s
Integer division test result:1.37 s
Float division test result:2.70 s
1000 digit Pi calculation result:0.12 s
```


### KPU应用尝试

- 利用预置模型进行图像识别或简单物体检测,评估AI加速器的性能。

这里我们去(https://maixhub.com/ "maixhub") 找一个人脸检测的模型进行测试 -人脸检测模型https://maixhub.com/model/zoo/68

解压下载后的的压缩包可以找到要运行的模型和测试模型的代码

### 文末可以下载



模型我们可以放在flash中读取并运行也可以放在SD卡中进行读取并运行,这里为了检测就直接使用`kflash`工具刷入指定的flash地址。



然后将代码放入IDE进行运行

注意这里的屏幕可能会出现反转和镜像,修改如下代码为
```
        # sensor.set_hmirror(sensor_hmirror)
    # sensor.set_vflip(sensor_vflip)
        sensor.set_hmirror(True)
    sensor.set_vflip(True)
        ```

```python

import sensor, image, lcd, time
import KPU as kpu
import gc, sys

def lcd_show_except(e):
    import uio
    err_str = uio.StringIO()
    sys.print_exception(e, err_str)
    err_str = err_str.getvalue()
    img = image.Image(size=(224,224))
    img.draw_string(0, 10, err_str, scale=1, color=(0xff,0x00,0x00))
    lcd.display(img)

def main(model_addr=0x300000, lcd_rotation=0, sensor_hmirror=False, sensor_vflip=False):
    try:
      sensor.reset()
    except Exception as e:
      raise Exception("sensor reset fail, please check hardware connection, or hardware damaged! err: {}".format(e))
    sensor.set_pixformat(sensor.RGB565)
    sensor.set_framesize(sensor.QVGA)
    sensor.set_hmirror(sensor_hmirror)
    sensor.set_vflip(sensor_vflip)
    sensor.run(1)

    lcd.init(type=1)
    lcd.rotation(lcd_rotation)
    lcd.clear(lcd.WHITE)

    anchors = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)
    try:
      task = None
      task = kpu.load(model_addr)
      kpu.init_yolo2(task, 0.5, 0.3, 5, anchors) # threshold:, nms_value:
      while(True):
            img = sensor.snapshot()
            t = time.ticks_ms()
            objects = kpu.run_yolo2(task, img)
            t = time.ticks_ms() - t
            if objects:
                for obj in objects:
                  img.draw_rectangle(obj.rect())
            img.draw_string(0, 200, "t:%dms" %(t), scale=2)
            lcd.display(img)
    except Exception as e:
      raise e
    finally:
      if not task is None:
            kpu.deinit(task)


if __name__ == "__main__":
    try:
      main( model_addr=0x300000, lcd_rotation=0, sensor_hmirror=False, sensor_vflip=False)
      # main(model_addr="/sd/m.kmodel")
    except Exception as e:
      sys.print_exception(e)
      lcd_show_except(e)
    finally:
      gc.collect()

```
此时我们寻找一张全家福查看识别的结果





我们可以看到单人人脸检测的帧率还是非常高的,而且在多人的情况下,帧率也在一个比较理想的范围,由此可见其kpu算力的强大。



- k210还有一些其他比较强大的功能,如色块的检测

```python
import sensor
import image
import lcd
import time
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
green_threshold   = (0,   80,-70,   -10,   -0,   30)
while True:
    img=sensor.snapshot()
    blobs = img.find_blobs()
    if blobs:
      for b in blobs:
            tmp=img.draw_rectangle(b)
            tmp=img.draw_cross(b, b)
            c=img.get_pixel(b, b)
    lcd.display(img)

```

### I/O与外设交互

- 测试GPIO控制、SPI、I2C等接口功能,确保与外部设备通信无碍。

目前手头只有I2C的光照强度传感器BH1750,所以以为基础来测试k210的I2C功能。

如下代码可以查看当前光照强度:

```python
from machine import I2C
import time

# 初始化I2C
i2c = I2C(I2C.I2C0, freq=100000, scl=25, sda=24)

def read_bh1750():
    # 发送测量命令
    i2c.writeto(0x23, bytearray())
    time.sleep(0.018) # 等待测量完成
    # 读取数据
    data = i2c.readfrom(0x23, 2)
    lux = ((data << 8) | data) / 1.2
    return lux

# 持续读取光照强度
while True:
    lux = read_bh1750()
    print("Lux:", lux)
    time.sleep(1) # 每秒读取一次,可以根据需要调整时间间隔
       

```
可以看到已经能够读取当前的光照强度了



光照强度的检测在识别中还是很重要的一个因素,当环境光较暗的时候可以通过补光来达到更好的识别效果。

其他的接口与功能会在后续的项目中进行使用~
---</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                               
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>

Jacktang 发表于 2024-7-13 09:11

<p>光照强度的检测在识别中还是很重要的一个因素,当环境光较暗的时候可以通过补光来达到更好的识别效果。</p>

<p>这就是补光的意义</p>
页: [1]
查看完整版本: Sipeed MAix BiT AIoT 开发套件测评-基础功能测试