欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

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

发布时间:2023/12/3 45 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python batch_size_python 實現動態 batch size,多張圖片如何堆疊轉成指針 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

前陣子有發問,關於 python 動態 batch size 如何實現,目前解決之前問題

現在遇到的問題是當我把兩張圖片直接用 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]

# 对图片进行切分,取中间区域

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,多張圖片如何堆疊轉成指針的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。