日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python batch_size_python 實現動態 batch size,多張圖片如何堆疊轉成指針

發(fā)布時間:2023/12/3 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python batch_size_python 實現動態 batch size,多張圖片如何堆疊轉成指針 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前陣子有發(fā)問,關於 python 動態(tài) batch size 如何實現(xiàn),目前解決之前問題

現(xiàn)在遇到的問題是當我把兩張圖片直接用 numpy concat 堆疊在一起 進行 acl.util.numpy_to_ptr 轉換成指針

進行推理後,得到的結果只有第一張圖片是對的,第二張之後就是錯的

想確認 batch size 大於1時一該要如何堆疊,然後轉成 acl format

def?transfer_pic(input_path):

input_path?=?os.path.abspath(input_path)

image_file?=?Image.open(input_path)

image_file?=?image_file.resize((256,?256))

img?=?np.array(image_file)

height?=?img.shape[0]

width?=?img.shape[1]

#?對圖片進行切分,取中間區(qū)域

h_off?=?(height?-?224)?//?2

w_off?=?(width?-?224)?//?2

crop_img?=?img[h_off:height?-?h_off,?w_off:width?-?w_off,?:]

#?rgb?to?bgr,改變通道順序

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

shape?=?img.shape

img?=?img.astype("float16")

img[:,?:,?0]?-=?104

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

img[:,?:,?2]?-=?123

img?=?img.reshape([1]?+?list(shape))

img?=?img.transpose([0,?3,?1,?2])

result?=?np.frombuffer(img.tobytes(),?np.float16)

return?result

img1?=?transfer_pic("path1")

img2?=?transfer_pic("path2")

datasets?=?numpy.concatenate((img1,?img2),?axis=0)

temp_data_buffer?=?input_data

for?i,?item?in?enumerate(temp_data_buffer):

input_name?=?acl.mdl.get_input_name_by_index(model_desc,?i)

if?input_name?!=?dynamicFlag:

ptr?=?acl.util.numpy_to_ptr(datasets)

print(ptr)

ret?=?acl.rt.memcpy(item["buffer"],

item["size"],

ptr,

item["size"],

ACL_MEMCPY_HOST_TO_DEVICE)

check_ret("acl.rt.memcpy",?ret)

else:

print("")

假設圖片 224,224,3 經過 transfer_pic() 後得到 img.shape = (150528, )

把兩張圖片直接 numpy.concat 堆疊起來變成 datasets.shape = (301056, )

然後透過

ptr?=?acl.util.numpy_to_ptr(datasets)

print(ptr)

ret?=?acl.rt.memcpy(item["buffer"],

item["size"],

ptr,

item["size"],

ACL_MEMCPY_HOST_TO_DEVICE)

這個思路是否有什麼問題,才造成只有第一張圖片正確

附上推論的代碼

總結

以上是生活随笔為你收集整理的python batch_size_python 實現動態 batch size,多張圖片如何堆疊轉成指針的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。