- added image module with sub image and normalize

- improved tests
This commit is contained in:
2026-01-10 14:12:29 +01:00
parent b6a511e33c
commit f7e3693f45
3 changed files with 68 additions and 4 deletions
+46
View File
@@ -0,0 +1,46 @@
from rbm.matrix import Mat, np
def normalize(image: Mat):
image_shape = image.shape
mean_training_batch = np.reshape(np.repeat(np.mean(image, axis=1), image_shape[1], axis=0), image_shape)
var_training_batch = np.reshape(np.repeat(np.std(image, axis=1), image_shape[1], axis=0), image_shape)
result = (image - mean_training_batch) / var_training_batch
return result
class SubImage:
def __init__(self, sx: int, sy: int, px: int, py: int):
self.sx = sx
self.sy = sy
self.px = px
self.py = py
self.x = 0
self.y = 0
def check(self, image: Mat):
nx = (image.shape[0] - self.sx)
ny = (image.shape[1] - self.sy)
if nx % self.px:
raise ValueError
if ny % self.py:
raise ValueError
nx = int(nx / self.px) + 1
ny = int(ny / self.py) + 1
return nx, ny
def __call__(self, images: Mat):
return self.process_image(images)
def process_image(self, image: Mat):
nx, ny = self.check(image[0, 0, :, :])
result = np.zeros((image.shape[0]*nx*ny, image.shape[1], self.sx, self.sy))
k = 0
for n in range(image.shape[0]):
for x in range(0, image.shape[2] - self.sx + 1, self.px):
for y in range(0, image.shape[3] - self.sy + 1, self.py):
for c in range(image.shape[1]):
si = image[n, c, x:x+self.sx, y:y+self.sy]
result[k, c] = si
k += 1
return result