Generate random int in 3D array - python-3.x

l would like to generate a random 3d array containing random integers (coordinates) in the intervalle [0,100].
so, coordinates=dim(30,10,2)
What l have tried ?
coordinates = [[random.randint(0,100), random.randint(0,100)] for _i in range(30)]
which returns
array([[97, 68],
[11, 23],
[47, 99],
[52, 58],
[95, 60],
[89, 29],
[71, 47],
[80, 52],
[ 7, 83],
[30, 87],
[53, 96],
[70, 33],
[36, 12],
[15, 52],
[30, 76],
[61, 52],
[87, 99],
[19, 74],
[37, 63],
[40, 2],
[ 8, 84],
[70, 32],
[63, 8],
[98, 89],
[27, 12],
[75, 59],
[76, 17],
[27, 12],
[48, 61],
[39, 98]])
of shape (30,10)
What l'm supposed to get ?
dim=(30,10,2) rather than (30,10)

Use the size parameter:
import numpy as np
coordinates = np.random.randint(0, 100, size=(30, 10, 2))
will produce a NumPy array with integer values between 0 and 100 and of shape (30, 10, 2).

Related

How can one define a function ony a numpy matrix of vectors, that returns a value for every vector?

I have a 3-dimensional numpy array:
import numpy as np
threeDimArray = np.arange(24).reshape((3, 2, 4))
print(threeDimArray)
The print-statement returns:
[[[ 0 1 2 3]
[ 4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]
[[16 17 18 19]
[20 21 22 23]]]
I define a function that is supposed to calculate the sum for every vector on the array and replace these vectors with these calculated sums:
def myOperation():
img_temp=threeDimArray.copy()
nrows=img_temp.shape[0]
ncolumns = img_temp.shape[1]
for j in range(ncolumns):
for i in range(nrows):
img_temp[i][j]=sum(img_temp[i][j])
return(img_temp)
The function is intended to return this:
[[6,22],
[38,54],
[70,86]]
Instead it returns this:
[[[ 6, 6, 6, 6],
[22, 22, 22, 22]],
[[38, 38, 38, 38],
[54, 54, 54, 54]],
[[70, 70, 70, 70],
[86, 86, 86, 86]]]
Why does it do this?
How can I change the function to return what I described?
In [133]: arr = np.arange(24).reshape(3,2,4)
In [134]: arr
Out[134]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11],
[12, 13, 14, 15]],
[[16, 17, 18, 19],
[20, 21, 22, 23]]])
With a numpy array, you don't have to iterate (in python). Let the sum method do it - with axis to specify how:
In [135]: arr.sum(axis=-1)
Out[135]:
array([[ 6, 22],
[38, 54],
[70, 86]])
In your code you make img_temp to be the same shape as the source:
In [138]: def myOperation(arr):
...: img_temp=arr.copy()
...: nrows=img_temp.shape[0]
...: ncolumns = img_temp.shape[1]
...: for j in range(ncolumns):
...: for i in range(nrows):
...: img_temp[i][j]=sum(img_temp[i][j])
...: return(img_temp)
...:
In [139]: myOperation(arr)
Out[139]:
array([[[ 6, 6, 6, 6],
[22, 22, 22, 22]],
[[38, 38, 38, 38],
[54, 54, 54, 54]],
[[70, 70, 70, 70],
[86, 86, 86, 86]]])
Just select one column on the last dimension, and you get what you want:
In [140]: myOperation(arr)[:,:,0]
Out[140]:
array([[ 6, 22],
[38, 54],
[70, 86]])
Here's a version of your function that does what you want:
In [143]: def myOperation(arr):
...: nrows,ncolumns = arr.shape[:2]
...: img_temp=np.zeros((nrows,ncolumns), arr.dtype)
...: for j in range(ncolumns):
...: for i in range(nrows):
...: img_temp[i,j]=sum(arr[i,j])
...: return(img_temp)
...:
In [144]: myOperation(arr)
Out[144]:
array([[ 6, 22],
[38, 54],
[70, 86]])

randomly sample from a high dimensional array along with a specific dimension

There has a 3-dimensional array x of shape (2000,60,5). If we think it represents a video, the 2000 can represent 2000 frames. I would like to randomly sample it along with the first dimension, i.e., get a set of frame samples. For instance, how to get an array of (500,60,5) which is randomly sampled from x along with the first dimension?
You can pass x as the first argument of the choice method. If you don't want repeated frames in your sample, use replace=False.
For example,
In [10]: x = np.arange(72).reshape(9, 2, 4) # Small array for the demo.
In [11]: x
Out[11]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11],
[12, 13, 14, 15]],
[[16, 17, 18, 19],
[20, 21, 22, 23]],
[[24, 25, 26, 27],
[28, 29, 30, 31]],
[[32, 33, 34, 35],
[36, 37, 38, 39]],
[[40, 41, 42, 43],
[44, 45, 46, 47]],
[[48, 49, 50, 51],
[52, 53, 54, 55]],
[[56, 57, 58, 59],
[60, 61, 62, 63]],
[[64, 65, 66, 67],
[68, 69, 70, 71]]])
Sample "frames" from x with the choice method of NumPy random generator instance.
In [12]: rng = np.random.default_rng()
In [13]: rng.choice(x, size=3)
Out[13]:
array([[[40, 41, 42, 43],
[44, 45, 46, 47]],
[[40, 41, 42, 43],
[44, 45, 46, 47]],
[[16, 17, 18, 19],
[20, 21, 22, 23]]])
In [14]: rng.choice(x, size=3, replace=False)
Out[14]:
array([[[ 8, 9, 10, 11],
[12, 13, 14, 15]],
[[32, 33, 34, 35],
[36, 37, 38, 39]],
[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]]])
Note that the frames will be in random order; if you want to preserve the order, you could use choice to generate an array of indices, then use the sorted indices to pull the frames out of x.

How to print the data of the Tensors' formed in the data pipeline process ? (tf.data.Dataset.map -<class 'tensorflow.python.framework.ops.Tensor'> )

I try to see the process of data pipeline with tensorflow2
My code is working but I can't print some value in this pipeline steps. (especially inside of .map(read_image) )
How can I print values inside read_image functions? (called with .map() method)
def read_image(image_paths, label_map_paths):
# firstly I want to print => image_paths values
# print(type(image_paths)) -> <class 'tensorflow.python.framework.ops.Tensor'>
img_raw = tf.io.read_file(image_paths)
# print(img_raw) ?
# print(type(img_raw)) -> <class 'tensorflow.python.framework.ops.Tensor'>
image = tf.image.decode_jpeg(img_raw)
#print(type(image)) -> <class 'tensorflow.python.framework.ops.Tensor'>
#print(image) ?
I can print training_ds values with code below but I can't print inside of .map(read_image) function
def get_training_dataset(training_image_paths, training_label_map_paths):
training_ds = tf.data.Dataset.from_tensor_slices((image_paths,label_map_paths))
for z in training_ds.take(3):
print(z)
training_ds = training_ds.map(read_image)
for x in training_ds.take(1):
print(x)
output 1
(<tf.Tensor: shape=(), dtype=string, numpy=b'dataset1/images_prepped_train/0016E5_06330.png'>, <tf.Tensor: shape=(), dtype=string, numpy=b'dataset1/annotations_prepped_train/0016E5_06330.png'>)
(<tf.Tensor: shape=(), dtype=string, numpy=b'dataset1/images_prepped_train/0016E5_06360.png'>,
output2 :
(<tf.Tensor: shape=(360, 480, 3), dtype=uint8, numpy=
array([[[16, 16, 16],
[16, 16, 16],
[12, 12, 12],
...,
[15, 19, 20],
[17, 18, 20],
[17, 18, 22]],
[[16, 16, 16],
[14, 14, 14],
[14, 14, 14],
...,
[15, 19, 20],
[18, 19, 21],
[19, 20, 22]],
[[14, 14, 14],
[14, 14, 14],
[15, 15, 15],
...,
[15, 19, 20],
[17, 18, 20],
[16, 17, 20]],
...,
[[16, 17, 19],
[16, 17, 19],
[16, 17, 19],
...,
[30, 40, 42],
[26, 37, 37],
[21, 33, 38]],
[[16, 17, 19],
[16, 17, 19],
[16, 17, 19],
...,
[27, 37, 40],
[24, 36, 39],
[21, 33, 38]],
[[16, 17, 19],
[15, 16, 18],
[15, 16, 18],
...,
[22, 34, 38],
[23, 35, 38],
[22, 32, 38]]], dtype=uint8)>, <tf.Tensor: shape=(360, 480, 1), dtype=uint8, numpy=
array([[[ 1],
[ 1],
[ 1],
...,
[ 1],
[ 1],
[ 1]],
[[ 1],
[ 1],
[ 1],
...,
[ 1],
[ 1],
[ 1]],
[[ 1],
[ 1],
[ 1],
...,
[ 1],
[ 1],
[ 1]],
...,
[[ 4],
[ 4],
[ 4],
...,
[11],
[11],
[11]],
[[ 4],
[ 4],
[ 4],
...,
[11],
[11],
[11]],
[[ 4],
[ 4],
[ 4],
...,
[11],
[11],
[11]]], dtype=uint8)>)
training_image_paths = [dataset1/images_prepped_train/0016E5_07740.png,
dataset1/images_prepped_train/0016E5_07710.png
dataset1/images_prepped_train/0016E5_07790.png]
training_label_map_paths = [dataset1/images_prepped_train/0016E5_08460.png,
dataset1/images_prepped_train/0016E5_08490.png,
dataset1/images_prepped_train/0016E5_08520.png]
training_dataset = get_training_dataset(training_image_paths, training_label_map_paths)
tf.print prints both using this code.
import tensorflow as tf
def read_image(image_paths,label_paths):
tf.print(image_paths)
img_raw = tf.io.read_file(image_paths)
image = tf.image.decode_jpeg(img_raw)
print_data(image)
return image
def print_data(image):
tf.print(image)
image_paths = tf.constant(['/Users/my/Documents/Dataset/jpg/image_00001.jpg'])
label_paths = tf.constant([0])
training_ds = tf.data.Dataset.from_tensor_slices((image_paths,label_paths))
training_ds = training_ds.map(read_image)
for i in training_ds:
pass

How to sort YOLOv4 bounding box?

I have trained the yolov4 from alexeyab darknet repo to detect characters in a number plate. It segments the character correctly but the bounding boxes are in random order. How can I sort the bounding box from top left to bottom right for image like this: (This is not actual image used but this is a photoshoped image for sample Nepali License Number Plate because of confidential data)
I've tried: (from pyimagesearch)
def sort_bbox(bbox, method="left-to-right"):
# initialize the reverse flag and sort index
reverse = False
i = 0
# handle if we need to sort in reverse
if method == "right-to-left" or method == "bottom-to-top":
reverse = True
# handle if we are sorting against the y-coordinate rather than
# the x-coordinate of the bounding box
if method == "top-to-bottom" or method == "bottom-to-top":
i = 1
# construct the list of bounding boxes and sort them from top to
# bottom
boundingBoxes = sorted(bbox, key=lambda b: b[1], reverse=reverse)
# return the list of sorted contours and bounding boxes
return boundingBoxes
but didn't sort the bounding boxes. It's still in random order.
I have bounding box from yolov4 detection like this: unsorted bounding boxes in xywh: [[50, 12, 15, 18], [66, 10, 15, 19], [87, 10, 19, 20], [21, 12, 24, 19], [51, 12, 15, 17], [51, 12, 15, 18], [66, 12, 15, 18], [86, 11, 19, 19], [39, 32, 27, 29], [68, 33, 28, 27], [97, 31, 28, 30], [12, 37, 24, 25], [11, 35, 25, 27], [40, 34, 27, 28], [68, 33, 27, 27], [97, 33, 28, 28]]
and from above sorting code: [[66, 10, 15, 19], [87, 10, 19, 20], [86, 11, 19, 19], [50, 12, 15, 18], [21, 12, 24, 19], [51, 12, 15, 17], [51, 12, 15, 18], [66, 12, 15, 18], [97, 31, 28, 30], [39, 32, 27, 29], [68, 33, 28, 27], [68, 33, 27, 27], [97, 33, 28, 28], [40, 34, 27, 28], [11, 35, 25, 27], [12, 37, 24, 25]]
What I want is the bounding box of: बा २ प ८ ८ ८ ८
Any Help will be very much appreciated.

Haskell: Convert String representation of Integer List of Lists to Integer List of Lists

I have a file that contains a list of lists,
namely [[39, 40, 1], [-39, -40, 1], [-39, 40, -1], [1, 41, 2], [-1, -41, 2], [-1, 41, -2], [2, 42, 3], [-2, -42, 3], [-2, 42, -3], [3, 43, 4], [-3, -43, 4], [-3, 43, -4], [4, 44, 5], [-4, -44, 5], [-4, 44, -5], [5, 45, 6], [-5, -45, 6], [-5, 45, -6], [6, 46, 7], [-6, -46, 7], [6, -46, -7], [7, 47, 8], [-7, -47, 8], [7, -47, -8], [-7, 47, -8], [8, 48, 9], [8, -48, -9], [-8, 48, -9], [9, 49, 10], [-9, -49, 10], [9, -49, -10], [-9, 49, -10], [10, 50, 11], [10, -50, -11], [-10, 50, -11], [11, 51, 12], [-11, -51, 12], [11, -51, -12], [-11, 51, -12], [-12, -52, 13], [12, -52, -13], [-12, 52, -13], [-13, -53, 14], [13, -53, -14], [-13, 53, -14], [14, 54, 15], [-14, -54, 15], [14, -54, -15], [-14, 54, -15], [-15, -55, 16], [15, -55, -16], [-15, 55, -16], [16, 56, 17], [-16, -56, 17], [16, -56, -17], [-16, 56, -17], [17, 57, 18], [-17, -57, 18], [-17, 57, -18], [18, 58, 19], [-18, -58, 19], [18, -58, -19], [-18, 58, -19], [19, 59, 60], [-19, -59, 60], [-19, 59, -60], [20, 59, 60], [-20, -59, 60], [20, -59, -60], [-20, 59, -60], [21, 58, 20], [-21, -58, 20], [21, -58, -20], [-21, 58, -20], [22, 57, 21], [-22, -57, 21], [22, -57, -21], [-22, 57, -21], [23, 56, 22], [23, -56, -22], [-23, 56, -22], [24, 55, 23], [-24, -55, 23], [24, -55, -23], [-24, 55, -23], [-25, -54, 24], [25, -54, -24], [-25, 54, -24], [26, 53, 25], [-26, -53, 25], [26, -53, -25], [-26, 53, -25], [27, 52, 26], [-27, -52, 26], [27, -52, -26], [-27, 52, -26], [28, 51, 27], [-28, -51, 27], [-28, 51, -27], [29, 50, 28], [-29, -50, 28], [29, -50, -28], [-29, 50, -28], [30, 49, 29], [-30, -49, 29], [30, -49,-29], [-30, 49, -29], [31, 48, 30], [-31, -48, 30], [31, -48, -30], [-31, 48, -30], [32, 47, 31], [-32, -47, 31], [32, -47, -31], [-32, 47, -31], [33, 46, 32], [33, -46, -32], [-33, 46, -32], [34, 45, 33], [-34, -45, 33], [34, -45, -33], [-34, 45, -33], [35, 44, 34], [-35, -44, 34], [35, -44, -34], [-35, 44, -34],[36, 43, 35], [-36, -43, 35], [-36, 43, -35], [37, 42, 36], [-37, -42, 36], [37, -42, -36], [-37, 42, -36], [38, 41, 37], [-38, -41, 37], [38, -41, -37], [39, 40, -38], [-39, -40, -38], [39, -40, 38], [-39, 40, 38]]
I have figured out how to import the file but am having trouble converting it from a String -> [[Int]].
How would I go about do so?
You can use the read or readMaybe function.
For example,
b :: [[Int]]
b = read "[[3, 4], [4, -5]]"

Resources