numpy ascontiguousarra study notes

Table of contents

numpy ascontiguousarra function

Conversion command:

ascontiguousarray equivalent effect:

ascontiguousarray study notes


ascontiguousarrayThe function converts an array stored in discontinuous memory into an array stored in continuous memory, which makes the operation faster.

When used on the Ascend development version, the prediction result is incorrect due to memory inconsistency.

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
print(a.flags) # c_contiguous为True,数组a为C连续性

b = np.ascontiguousarray(a)
print(b)
print(b.flags) # c_contiguous为True,数组b为C连续性

c = np.ascontiguousarray(a, dtype=np.float32)
print(c)
print(c.flags) # c_contiguous为True,数组c为C连续性且元素类型变为np.float32

Conversion command:

 atc --model=plate.onnx --framework=5 --output=plate_rec_color_bs1 --input_format=NCHW --input_shape="images:1,3,48,168" --log=info --soc_version=Ascend310P3

img = np.ascontiguousarray(img)

ascontiguousarray equivalent effect:

img3.tofile("temp.bin")
img4 = np.fromfile("temp.bin", dtype=np.float32)  # 从bin文件中读取图片

ascontiguousarray学习笔记

1. ascontiguousarrayThe function converts an array with discontinuous storage in memory into an array with continuous storage in memory, making the operation faster.

For example, if we generate a two-dimensional array, Numpy can .flagscheck whether an array is C continuous or Fortran continuous through familiarity.

import numpy as np
arr = np.arange(12).reshape(3,4)
flags = arr.flags
print("",arr)
print(flags)

output:

 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

We can see that  C_CONTIGUOUS: True means that the rows are continuous, and F_CONTIGUOUS: False means that the columns are not continuous. Similarly, if we perform arr.T   or arr.transpose(1,0) , the columns are continuous and the rows are discontinuous.

import numpy as np
arr = np.arange(12).reshape(3,4)
arr1 = arr.transpose(1,0)
flags = arr1.flags
print("",arr1)
print(flags)

output:

 [[ 0  4  8]
 [ 1  5  9]
 [ 2  6 10]
 [ 3  7 11]]
  C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

If done on the line slice即进行切割, the continuity is changed to be neither C nor Fortran continuous:

import numpy as np
arr = np.arange(12).reshape(3,4)
arr1 = arr[:,0:2]
flags = arr1.flags
print("",arr1)
print(flags)

output:

 [[0 1]
 [4 5]
 [8 9]]
  C_CONTIGUOUS : False
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

At this point, using ascontiguousarraya function, it can be made continuous:

import numpy as np
arr = np.arange(12).reshape(3,4)
arr1 = arr[:,0:2]
arr2 = np.ascontiguousarray(arr1)
flags = arr2.flags
print("",arr2)
print(flags)

output:

[[0 1]
 [4 5]
 [8 9]]
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

C_CONTIGUOUS : True

C_CONTIGUOUS:真

Guess you like

Origin blog.csdn.net/jacke121/article/details/132178786