python batch_size_python 實現動態 batch size,多張圖片如何堆疊轉成指針
前陣子有發(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,多張圖片如何堆疊轉成指針的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《荷花》读后感
- 下一篇: 繁体字网名一个字113个