arr = np.array([
[0.15, 0.71, 0.26, -0.11, -2.03],
[0.13, 0.44, -0.11, -0.23, 0.19],
[-1.44, 0.51, 0.42, 2.58, -0.88],
[-1.18, 2.73, 2.35, 0.21, -0.29],
[-1.64, -0.37, 0.27, 0.57, 1.82]
])
arr_filter = np.array([
[0.08, 0.27, -0.24],
[-0.25, -0.11, -0.18],
[0.44, 1.87, 1.18]
])
import numpy as np
rng = np.random.default_rng(123)
arr = rng.normal(size=(33,33,3)).round(2)
print(arr)
# [
# [[-0.99 -0.37 1.29]
# [ 0.19 0.92 0.58]
# [-0.64 0.54 -0.32]
# ...
# [ 1.23 0.15 0.48]
# [-0.15 1.32 -1.22]
# [-0.3 -1.17 0.83]]
#
# [[ 0.85 -0.52 1.66]
# [-0.3 -1.38 -0.28]
# [ 0.36 -0.23 2.27]
# ...
# [ 1.52 0.49 0.7 ]
# [ 0.85 -0.91 0.12]
# [ 0.15 -0.16 -1.09]]
#
# ...
#
# [[-0.38 -0.45 1. ]
# [-0.58 2.18 0.36]
# [-0.44 -2.77 0.82]
# ...
# [-1.58 -0.5 -0.52]
# [-0.86 -0.54 -0.26]
# [ 0.23 0.81 -0.4 ]]
#
# [[-1.98 -0.22 -0.79]
# [-0.01 -1.47 -0.83]
# [-0.87 0.3 -0.82]
# ...
# [ 1.42 0.7 -0.75]
# [-0.81 1.68 -0.82]
# [-0.93 0.28 -1.61]]
# ]
filter = rng.normal(size=(5,5,3)).round(2)
print(filter)
#[
# [[-1.47 0.74 0.58]
# [ 0.46 2.37 0.79]
# [ 1.15 -1.11 -0.29]
# [-0.98 0.29 0.44]
# [-0.44 -0.03 0.69]]
#
# [[ 0.34 0.67 -0.11]
# [-0.71 -1. -0.88]
# [ 0.61 0.49 -0.27]
# [ 0.12 -1.56 0.11]
# [ 0.32 -0.98 0.46]]
#
# [[-1.03 0.58 0.08]
# [ 0.89 0.86 1.49]
# [-0.4 0.86 -0.29]
# [ 0.07 -0.09 -0.87]
# [ 0.2 1.22 -0.27]]
#
# [[ 1.1 -2.61 1.64]
# [-1.15 0.47 1.44]
# [-1.45 0.39 1.37]
# [ 0.13 -0.1 0.04]
# [ 0.27 0.57 0.57]]
#
# [[-0.61 -0.41 0.93]
# [ 1.47 -0.07 -0.29]
# [ 0.49 1.02 0.2 ]
# [ 0.16 0.95 0.52]
# [ 1.11 0.13 -0.17]]
# ]
mport numpy as np
def convolution_3d(arr, arr_filter, stride=2):
arr_height, arr_width, arr_depth = arr.shape
arr_filter_height, arr_filter_width, _ = arr_filter.shape
output_height = (arr_height - arr_filter_height) // stride + 1
output_width = (arr_width - arr_filter_width) // stride + 1
output = np.zeros((output_height, output_width))
for i in range(output_height):
for j in range(output_width):
h_start = i * stride
h_end = h_start + arr_filter_height
w_start = j * stride
w_end = w_start + arr_filter_width
arr_slice = arr[h_start:h_end, w_start:w_end, :]
output[i, j] = np.sum(arr_slice * arr_filter)
return output
rng = np.random.default_rng(123)
arr = rng.normal(size=(33,33,3)).round(2)
arr_filter = rng.normal(size=(5,5,3)).round(2)
result = convolution_3d(arr, arr_filter, stride=2)
print(f"Результат свертки: \n{result}")
import numpy as np
rng = np.random.default_rng(123)
arr = rng.normal(size=(33,33,3)).round(2)
arr_filter = rng.normal(size=(5,5,3)).round(2)
stride = 2
result_size = 15
result = np.zeros((result_size, result_size))
for i in range(result_size):
for j in range(result_size):
x_start = i * stride
y_start = j * stride
x_end = x_start + arr_filter.shape[0]
y_end = y_start + arr_filter.shape[1]
sub_array = arr[x_start:x_end, y_start:y_end, :]
result[i, j] = np.sum(sub_array * arr_filter)
print(f"Результат свертки: \n{result}")