numpy-ndarray

Fill Bounding Boxes in 2D array

徘徊边缘 提交于 2019-12-04 17:04:10
问题 I have a 2D numpy array which looks like array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0.

Pytorch: Why is the memory occupied by the `tensor` variable so small?

拟墨画扇 提交于 2019-12-04 12:57:30
In Pytorch 1.0.0, I found that a tensor variable occupies very small memory. I wonder how it stores so much data. Here's the code. a = np.random.randn(1, 1, 128, 256) b = torch.tensor(a, device=torch.device('cpu')) a_size = sys.getsizeof(a) b_size = sys.getsizeof(b) a_size is 262288. b_size is 72. The answer is in two parts. From the documentation of sys.getsizeof , firstly All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific. so it could be that for tensors __sizeof__ is undefined or defined

Concatenation of 2 1D `numpy` Arrays Along 2nd Axis

蓝咒 提交于 2019-12-04 02:42:42
Executing import numpy as np t1 = np.arange(1,10) t2 = np.arange(11,20) t3 = np.concatenate((t1,t2),axis=1) results in a Traceback (most recent call last): File "<ipython-input-264-85078aa26398>", line 1, in <module> t3 = np.concatenate((t1,t2),axis=1) IndexError: axis 1 out of bounds [0, 1) why does it report that axis 1 is out of bounds? Your title explains it - a 1d array does not have a 2nd axis! But having said that, on my system as on @Oliver W. s, it does not produce an error In [655]: np.concatenate((t1,t2),axis=1) Out[655]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17

Indexes of first occurrences of zeroes in Python list

早过忘川 提交于 2019-12-04 02:33:34
问题 I have a list like this, import numpy as np myList = [0.0 , 0.0, 0.0, 2.0, 2.0, 0.0, 2.5, 0.0, 0.0, 0.0, 3.0, 0.0] I can find the index of non zero occurrence like below. I = np.nonzero(myList) for i in I: print(i) Can I find the index of first occurrence of zeros? something like below, [0, 5, 7, 11] 回答1: Since NumPy tagged, here are two ways - In [44]: m = np.r_[False,np.equal(myList,0)] In [45]: np.flatnonzero(m[:-1]<m[1:]) Out[45]: array([ 0, 5, 7, 11]) If the input is an array, becomes a

How to convert Tensor to ndarray (tensor with adversarial images inside)

巧了我就是萌 提交于 2019-12-02 09:04:38
问题 NOTE: I already have tried solutions from different SO questions with no success, details follow. I'm studying cleverhans Pyhton tutorials, focusing on this code (keras model case). I have a base keras knowledge but I've just started with Tensorflow (total newbie). I'm trying to visualize the adversial images generated in this piece of code (quote from the linked cleverhans sources): # Initialize the Fast Gradient Sign Method (FGSM) attack object and graph fgsm = FastGradientMethod(wrap, sess

Writting in sub-ndarray of a ndarray in the most pythonian way. Python 2

坚强是说给别人听的谎言 提交于 2019-12-02 09:01:44
问题 I have a ndarray like this one: number_of_rows = 3 number_of_columns = 3 a = np.arange(number_of_rows*number_of_columns).reshape(number_of_rows,number_of_columns) a array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) But I want something like this: array([[0, 100, 101], [3, 102, 103], [6, 7, 8]]) To do that I want to avoid to do it one by one, I rather prefer to do it in arrays or matrices, because later I want to extend the code. Nothe I have change a submatrix of the initial matrix (in mathematical

How to convert an opencv Mat into a numpy.ndarray?

做~自己de王妃 提交于 2019-12-02 03:53:42
I have a code written in java (android) that open the camera of the phone and show frames. The below code represents the method in which we can retrieve frames. The project used Chaquopy to interpret python code. @Override public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) { mRgba = inputFrame.rgba(); Python py = Python.getInstance(); PyObject pym = (PyObject) py.getModule("MyPythonClass").callAttr("call",mRgba); return mRgba; } The python code is used to retrieve the frame (represented by "mRgba" which is a Mat in the java code) for further treatment The problem is to

Slicing NumPy array given start and end indices for generic dimensions

流过昼夜 提交于 2019-12-02 03:51:27
问题 Given a numpy array x of shape (N_1...N_k) where k is arbitrary, and 2 arrays : start_indices=[a_1,...,a_k], end_indices=[b_1,...b_k], where `0<=a_i<b_i<=N_i`. I want to slice x as follows: x[a_1:b_1,...,a_k:b_k] . Lets say : x is of shape `(1000, 1000, 1000)` start_indices=[450,0,400] end_indices=[550,1000,600]. I want the output to be equal x[450:550,0:1000,400:600] . For example I tried to define : slice_arrays = (np.arange(start_indices[i], end_indices[i]) for i in range(k)) and use x

Writting in sub-ndarray of a ndarray in the most pythonian way. Python 2

為{幸葍}努か 提交于 2019-12-02 03:36:02
I have a ndarray like this one: number_of_rows = 3 number_of_columns = 3 a = np.arange(number_of_rows*number_of_columns).reshape(number_of_rows,number_of_columns) a array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) But I want something like this: array([[0, 100, 101], [3, 102, 103], [6, 7, 8]]) To do that I want to avoid to do it one by one, I rather prefer to do it in arrays or matrices, because later I want to extend the code. Nothe I have change a submatrix of the initial matrix (in mathematical terms, in terms of this example ndarray). In the example the columns considered are [1,2] and the rows [0

How to create or fill an numpy array with another array?

*爱你&永不变心* 提交于 2019-12-01 23:59:43
问题 How to create an numpy array with shape [2, 2, 3] , where the elements at axis 2 is another array, for example [1, 2, 3] ? So I would like to do something like this invalid code: a = np.arange(1, 4) b = np.full((3, 3), a) Resulting in an array like: [[[ 1. 2. 3.] [ 1. 2. 3.]] [[ 1. 2. 3.] [ 1. 2. 3.]]] Could of course make the loop for filling like, but thought there may be a shortcut: for y in range(b.shape[0]): for x in range(b.shape[1]): b[y, x, :] = a 回答1: There are multiple ways to