python numba库是什么,如何给python代码加速?JIT、autoit、LLVM
生活随笔
收集整理的這篇文章主要介紹了
python numba库是什么,如何给python代码加速?JIT、autoit、LLVM
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
參考文章1:一行代碼讓Python的運(yùn)行速度提高100倍
參考文章2:[Python技巧]如何加快循環(huán)操作和Numpy數(shù)組運(yùn)算速度
示例:遍歷深度圖的每個(gè)像素值是否為0,不用jit,要多耗時(shí)約500ms,用了jit,耗時(shí)基本可忽略不計(jì)了
# -*- coding: utf-8 -*- """ @File : 191218_obstacle_detection_測(cè)試加過(guò)濾器.py @Time : 2019/12/18 11:47 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """import time import numpy as np import pyrealsense2 as rs import cv2 import sys from numba import jit, vectorize, int64, int32, autojitclass ObstacleDetection(object):def __init__(self):# self.cam_serials = ['838212073161', '827312071726']self.cam_serials = ['838212073161']# @jit(nopython=True)# @jit# @vectorize([int64(int64, int64)], target='parallel')@autojitdef traversing_pixels(self, depth_image):num_black = 0all_pixels = 0for pixel in depth_image.ravel():all_pixels += 1if pixel == 0:num_black += 1return [all_pixels, num_black]def obstacle_detection(self):# 攝像頭個(gè)數(shù)(在這里設(shè)置所需使用攝像頭的總個(gè)數(shù))cam_num = 6ctx = rs.context()'''連續(xù)驗(yàn)證機(jī)制'''# D·C 1911202:創(chuàng)建最大驗(yàn)證次數(shù)max_veri_times;創(chuàng)建連續(xù)穩(wěn)定值continuous_stable_value,用于判斷設(shè)備重置后是否處于穩(wěn)定狀態(tài)max_veri_times = 100continuous_stable_value = 10print('\n', end='')print('開(kāi)始連續(xù)驗(yàn)證,連續(xù)驗(yàn)證穩(wěn)定值:{},最大驗(yàn)證次數(shù):{}:'.format(continuous_stable_value, max_veri_times))continuous_value = 0veri_times = 0while True:devices = ctx.query_devices()connected_cam_num = len(devices)if connected_cam_num == cam_num:continuous_value += 1if continuous_value == continuous_stable_value:breakelse:continuous_value = 0veri_times += 1if veri_times == max_veri_times:print("檢測(cè)超時(shí),請(qǐng)檢查攝像頭連接!")sys.exit()print('攝像頭個(gè)數(shù):{}'.format(connected_cam_num))'''循環(huán)reset攝像頭'''# hardware_reset()后是不是應(yīng)該延遲一段時(shí)間?不延遲就會(huì)報(bào)錯(cuò)print('\n', end='')print('開(kāi)始初始化攝像頭:')for dev in ctx.query_devices():# 先將設(shè)備的序列號(hào)放進(jìn)一個(gè)變量里,免得在下面for循環(huán)里訪問(wèn)設(shè)備的信息過(guò)多(雖然不知道它會(huì)不會(huì)每次都重新訪問(wèn))dev_serial = dev.get_info(rs.camera_info.serial_number)# 匹配序列號(hào),重置我們需重置的特定攝像頭(注意兩個(gè)for循環(huán)順序,哪個(gè)在外哪個(gè)在內(nèi)很重要,不然會(huì)導(dǎo)致剛重置的攝像頭又被訪問(wèn)導(dǎo)致報(bào)錯(cuò))for serial in self.cam_serials:if serial == dev_serial:dev.hardware_reset()# 像下面這條語(yǔ)句居然不會(huì)報(bào)錯(cuò),不是剛剛才重置了dev嗎?莫非區(qū)別在于沒(méi)有通過(guò)for循環(huán)ctx.query_devices()去訪問(wèn)?# 是不是剛重置后可以通過(guò)ctx.query_devices()去查看有這個(gè)設(shè)備,但是卻沒(méi)有存儲(chǔ)設(shè)備地址?如果是這樣,# 也就能夠解釋為啥能夠通過(guò)len(ctx.query_devices())函數(shù)獲取設(shè)備數(shù)量,但訪問(wèn)序列號(hào)等信息就會(huì)報(bào)錯(cuò)的原因了print('攝像頭{}初始化成功'.format(dev.get_info(rs.camera_info.serial_number)))'''連續(xù)驗(yàn)證機(jī)制'''# D·C 1911202:創(chuàng)建最大驗(yàn)證次數(shù)max_veri_times;創(chuàng)建連續(xù)穩(wěn)定值continuous_stable_value,用于判斷設(shè)備重置后是否處于穩(wěn)定狀態(tài)print('\n', end='')print('開(kāi)始連續(xù)驗(yàn)證,連續(xù)驗(yàn)證穩(wěn)定值:{},最大驗(yàn)證次數(shù):{}:'.format(continuous_stable_value, max_veri_times))continuous_value = 0veri_times = 0while True:devices = ctx.query_devices()connected_cam_num = len(devices)if connected_cam_num == cam_num:continuous_value += 1if continuous_value == continuous_stable_value:breakelse:continuous_value = 0veri_times += 1if veri_times == max_veri_times:print("檢測(cè)超時(shí),請(qǐng)檢查攝像頭連接!")sys.exit()print('攝像頭個(gè)數(shù):{}'.format(connected_cam_num))'''配置各個(gè)攝像頭的基本對(duì)象'''for i in range(len(self.cam_serials)):locals()['pipeline' + str(i + 1)] = rs.pipeline(ctx)locals()['config' + str(i + 1)] = rs.config()locals()['config' + str(i + 1)].enable_device(self.cam_serials[i])locals()['config' + str(i + 1)].enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)locals()['config' + str(i + 1)].enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)locals()['pipeline' + str(i + 1)].start(locals()['config' + str(i + 1)])# 創(chuàng)建對(duì)齊對(duì)象(深度對(duì)齊顏色)locals()['align' + str(i + 1)] = rs.align(rs.stream.color)'''運(yùn)行攝像頭'''try:while True:start_time = time.time()for i in range(len(self.cam_serials)):locals()['frames' + str(i + 1)] = locals()['pipeline' + str(i + 1)].wait_for_frames()# 獲取對(duì)齊幀集locals()['aligned_frames' + str(i + 1)] = locals()['align' + str(i + 1)].process(locals()['frames' + str(i + 1)])# 獲取對(duì)齊后的深度幀和彩色幀locals()['aligned_depth_frame' + str(i + 1)] = locals()['aligned_frames' + str(i + 1)].get_depth_frame()locals()['color_frame' + str(i + 1)] = locals()['aligned_frames' + str(i + 1)].get_color_frame()if not locals()['aligned_depth_frame' + str(i + 1)] or not locals()['color_frame' + str(i + 1)]:continue# 獲取顏色幀內(nèi)參locals()['color_profile' + str(i + 1)] = locals()['color_frame' + str(i + 1)].get_profile()locals()['cvsprofile' + str(i + 1)] = rs.video_stream_profile(locals()['color_profile' + str(i + 1)])locals()['color_intrin' + str(i + 1)] = locals()['cvsprofile' + str(i + 1)].get_intrinsics()locals()['color_intrin_part' + str(i + 1)] = [locals()['color_intrin' + str(i + 1)].ppx,locals()['color_intrin' + str(i + 1)].ppy,locals()['color_intrin' + str(i + 1)].fx,locals()['color_intrin' + str(i + 1)].fy]# 【空間過(guò)濾器】locals()['spatial' + str(i + 1)] = rs.spatial_filter()locals()['spatial' + str(i + 1)].set_option(rs.option.filter_magnitude, 5)locals()['spatial' + str(i + 1)].set_option(rs.option.filter_smooth_alpha, 1)locals()['spatial' + str(i + 1)].set_option(rs.option.filter_smooth_delta, 50)locals()['spatial' + str(i + 1)].set_option(rs.option.holes_fill, 3)locals()['filtered_depth' + str(i + 1)] = locals()['spatial' + str(i + 1)].process(locals()['aligned_depth_frame' + str(i + 1)])locals()['depth_image' + str(i + 1)] = np.asanyarray(locals()['filtered_depth' + str(i + 1)].get_data())locals()['color_image' + str(i + 1)] = np.asanyarray(locals()['color_frame' + str(i + 1)].get_data())# locals()['depth_image' + str(i + 1)] = np.asanyarray(# locals()['aligned_depth_frame' + str(i + 1)].get_data())# 【打印深度值看看、全部打印顯示】# np.set_printoptions(threshold=np.inf)# print(locals()['depth_image' + str(i + 1)])# 【計(jì)算深度圖數(shù)據(jù)中的0值】[locals()['all_pixels' + str(i + 1)], locals()['num_black' + str(i + 1)]] = self.traversing_pixels(locals()['depth_image' + str(i + 1)])# num_black = 0# all_pixels = 0# for row in range(480):# for colume in range(640):# all_pixels += 1# if locals()['depth_image' + str(i + 1)][row, colume] == 0:# num_black += 1print('depth_image分辨率:{}'.format(locals()['depth_image' + str(i + 1)].shape))# print('depth_image:{}'.format(num_black))# print('depth_image:{}'.format(num_black / all_pixels))print('depth_image:{}'.format(locals()['num_black' + str(i + 1)]))print('depth_image:{}'.format(locals()['num_black' + str(i + 1)] / locals()['all_pixels' + str(i + 1)]))# 以下這種卡的不行(get_distance()函數(shù)會(huì)把窗口搞崩潰(即使不很卡))# for row in range(locals()['aligned_depth_frame' + str(i + 1)].get_height()):# for colume in range(locals()['aligned_depth_frame' + str(i + 1)].get_width()):# all_pixels += 1# if locals()['depth_image' + str(i + 1)][row, colume] == 0:# # if locals()[# # 'aligned_depth_frame' + str(i + 1)].get_distance(row, colume) == 0:# num_black += 1# for pixel in locals()['depth_image' + str(i + 1)].ravel():# all_pixels += 1# if pixel == 0:# num_black += 1# print('depth_image分辨率:{}'.format(locals()['depth_image' + str(i + 1)].shape))# print('depth_image:{}'.format(num_black))# print('depth_image:{}'.format(num_black / all_pixels))locals()['depth_colormap' + str(i + 1)] = cv2.applyColorMap(cv2.convertScaleAbs(locals()['depth_image' + str(i + 1)], alpha=0.0425),cv2.COLORMAP_JET)locals()['image' + str(i + 1)] = np.hstack((locals()['color_image' + str(i + 1)], locals()['depth_colormap' + str(i + 1)]))cv2.imshow('win{}'.format(i + 1), locals()['image' + str(i + 1)])cv2.waitKey(1)end_time = time.time()print('單幀運(yùn)行時(shí)間:{}'.format(end_time - start_time))finally:for i in range(len(self.cam_serials)):locals()['pipeline' + str(i + 1)].stop()if __name__ == '__main__':ObstacleDetection().obstacle_detection()就是程序會(huì)有警告,不知啥原因:
D:\20191031_tensorflow_yolov3\python\python.exe D:/20191211_obstacle_detection/obstacle_detection/191218_obstacle_detection_測(cè)試加過(guò)濾器.py D:\20191031_tensorflow_yolov3\python\lib\site-packages\numba\decorators.py:33: NumbaDeprecationWarning: autojit is deprecated, use jit instead, which provides the same functionality. For more information visit http://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-numba-autojitwarnings.warn(NumbaDeprecationWarning(msg))開(kāi)始連續(xù)驗(yàn)證,連續(xù)驗(yàn)證穩(wěn)定值:10,最大驗(yàn)證次數(shù):100: 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6開(kāi)始初始化攝像頭: 攝像頭838212073161初始化成功開(kāi)始連續(xù)驗(yàn)證,連續(xù)驗(yàn)證穩(wěn)定值:10,最大驗(yàn)證次數(shù):100: 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 攝像頭個(gè)數(shù):6 D:/20191211_obstacle_detection/obstacle_detection/191218_obstacle_detection_測(cè)試加過(guò)濾器.py:27: NumbaWarning: Compilation is falling back to object mode WITH looplifting enabled because Function "traversing_pixels" failed type inference due to: non-precise type pyobject [1] During: typing of argument at D:/20191211_obstacle_detection/obstacle_detection/191218_obstacle_detection_測(cè)試加過(guò)濾器.py (29)File "191218_obstacle_detection_測(cè)試加過(guò)濾器.py", line 29:def traversing_pixels(self, depth_image):num_black = 0^@autojit D:/20191211_obstacle_detection/obstacle_detection/191218_obstacle_detection_測(cè)試加過(guò)濾器.py:27: NumbaWarning: Compilation is falling back to object mode WITHOUT looplifting enabled because Function "traversing_pixels" failed type inference due to: cannot determine Numba type of <class 'numba.dispatcher.LiftedLoop'>File "191218_obstacle_detection_測(cè)試加過(guò)濾器.py", line 31:def traversing_pixels(self, depth_image):<source elided>all_pixels = 0for pixel in depth_image.ravel():^@autojit D:\20191031_tensorflow_yolov3\python\lib\site-packages\numba\object_mode_passes.py:178: NumbaWarning: Function "traversing_pixels" was compiled in object mode without forceobj=True, but has lifted loops.File "191218_obstacle_detection_測(cè)試加過(guò)濾器.py", line 28:@autojitdef traversing_pixels(self, depth_image):^state.func_ir.loc)) D:\20191031_tensorflow_yolov3\python\lib\site-packages\numba\object_mode_passes.py:187: NumbaDeprecationWarning: Fall-back from the nopython compilation path to the object mode compilation path has been detected, this is deprecated behaviour.For more information visit http://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jitFile "191218_obstacle_detection_測(cè)試加過(guò)濾器.py", line 28:@autojitdef traversing_pixels(self, depth_image):^warnings.warn(errors.NumbaDeprecationWarning(msg, state.func_ir.loc)) depth_image分辨率:(480, 640) depth_image:6606 depth_image:0.02150390625 單幀運(yùn)行時(shí)間:1.2407138347625732 depth_image分辨率:(480, 640) depth_image:3357 depth_image:0.010927734375 單幀運(yùn)行時(shí)間:0.05056428909301758 depth_image分辨率:(480, 640) depth_image:3107 depth_image:0.010113932291666667 單幀運(yùn)行時(shí)間:0.04268836975097656 ...總結(jié)
以上是生活随笔為你收集整理的python numba库是什么,如何给python代码加速?JIT、autoit、LLVM的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: cython是什么,如何使用?
- 下一篇: python opencv cv2.Vi