def tensor_gray_4D_to_image(tensor,do_colorize = False,do_scale=False,force_rows_dim3=None,force_rows_dim2=None): sub_image = tensor_gray_3D_to_image(tensor[:, :, :, 0],force_rows_dim2) h, w = sub_image.shape[0], sub_image.shape[1] if force_rows_dim3 is None: R = tools_image.numerical_devisor(tensor.shape[3]) else: R = force_rows_dim3 C = int(tensor.shape[3]/R) if do_colorize: image = numpy.zeros((h * R, w * C, 3), dtype=numpy.float32) else: image = numpy.zeros((h * R, w * C), dtype=numpy.float32) for i in range (0,tensor.shape[3]): col, row = i % C, int(i / C) sub_image = tensor_gray_3D_to_image(tensor[:,:,:,i]) if do_colorize: sub_image = tools_image.saturate(sub_image) apply_blue_shift(sub_image,(col+row)%2) if do_colorize: image[h * row:h * row + h, w * col:w * col + w,:] = sub_image[:, :, :] else: image[h * row:h * row + h, w * col:w * col + w] = sub_image[:, :] if do_scale==True: image -= image.min() image *= 255 / image.max() return image
def tensor_gray_3D_to_image(tensor, do_colorize=False, do_scale=False, force_rows_dim2=None): if force_rows_dim2 is None: rows = tools_image.numerical_devisor(tensor.shape[2]) else: rows = force_rows_dim2 h, w, R, C = tensor.shape[0], tensor.shape[1], rows, int(tensor.shape[2] / rows) image = numpy.zeros((h * R, w * C), dtype=numpy.float32) for i in range(0, tensor.shape[2]): col, row = i % C, int(i / C) image[h * row:h * row + h, w * col:w * col + w] = tensor[:, :, i] if do_colorize: image = colorize_chess(image, tensor.shape[0], tensor.shape[1]) if do_scale == True: image -= image.min() image *= 255 / image.max() return image.astype(dtype=numpy.uint8)
def image_to_tensor_color_4D(image, shape): tensor = numpy.zeros(shape, numpy.float32) h, w = shape[0], shape[1] rows = tools_image.numerical_devisor(shape[3]) C = int(tensor.shape[3] / rows) for i in range(0, 96): col, row = i % C, int(i / C) tensor[:, :, :, i] = image[h * row:h * row + h, w * col:w * col + w] return tensor
def tensor_color_4D_to_image(tensor): rows = tools_image.numerical_devisor(tensor.shape[3]) h, w, R, C = tensor.shape[0], tensor.shape[1], rows, int(tensor.shape[3] / rows) image = numpy.zeros((h * R, w * C, tensor.shape[2]), dtype=numpy.float32) for i in range(0, tensor.shape[3]): col, row = i % C, int(i / C) image[h * row:h * row + h, w * col:w * col + w, :] = tensor[:, :, :, i] return image
def tensor_gray_1D_to_image(tensor,orientation = 'landscape'): rows = tools_image.numerical_devisor(tensor.shape[0]) cols = int(tensor.shape[0] / rows) if orientation=='landscape': image = numpy.reshape(tensor,(numpy.minimum(rows,cols),numpy.maximum(rows, cols))) else: image = numpy.reshape(tensor, (numpy.maximum(rows, cols), numpy.minimum(rows, cols))) return image
def tensor_gray_3D_to_image(tensor, do_colorize=False): rows = tools_image.numerical_devisor(tensor.shape[2]) h, w, R, C = tensor.shape[0], tensor.shape[1], rows, int(tensor.shape[2] / rows) image = numpy.zeros((h * R, w * C), dtype=numpy.float32) for i in range(0, tensor.shape[2]): col, row = i % C, int(i / C) image[h * row:h * row + h, w * col:w * col + w] = tensor[:, :, i] if do_colorize: image = colorize_chess(image, tensor.shape[0], tensor.shape[1]) return image
def init_model(self, input_dim, out_dim, grayscale): self.model = Sequential() rows = tools_image.numerical_devisor(int(input_dim)) cols = int(input_dim / rows) self.model.add(Reshape((rows, cols, 1), input_shape=[input_dim])) self.model.add(Conv2D(32, (2, 2), activation='linear', padding='same')) self.model.add(UpSampling2D((2, 2))) self.model.add(Flatten()) self.model.add(Dense((out_dim), activation='linear')) self.model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy']) return
def tensor_color_4D_to_image(tensor,do_chess=False,force_rows_dim3=None,force_rows_dim2=None): if force_rows_dim2 is None: rows = tools_image.numerical_devisor(tensor.shape[3]) else: rows = force_rows_dim2 h, w, R, C = tensor.shape[0], tensor.shape[1], rows, int(tensor.shape[3] / rows) image = numpy.zeros((h * R,w * C, tensor.shape[2]),dtype=numpy.float32) for i in range(0, tensor.shape[3]): col, row = i % C, int(i / C) sub_image = tensor[:, :, :, i] if do_chess: apply_blue_shift(sub_image,(col+row)%2) image[h * row:h * row + h, w * col:w * col + w, :] = sub_image return image
def tensor_gray_4D_to_image(tensor, do_colorize=False): sub_image = tensor_gray_3D_to_image(tensor[:, :, :, 0]) h, w = sub_image.shape[0], sub_image.shape[1] R = tools_image.numerical_devisor(tensor.shape[3]) C = int(tensor.shape[3] / R) if do_colorize: image = numpy.zeros((h * R, w * C, 3), dtype=numpy.float32) else: image = numpy.zeros((h * R, w * C), dtype=numpy.float32) for i in range(0, tensor.shape[3]): col, row = i % C, int(i / C) sub_image = tensor_gray_3D_to_image(tensor[:, :, :, i], do_colorize) if do_colorize: image[h * row:h * row + h, w * col:w * col + w, :] = sub_image[:, :, :] else: image[h * row:h * row + h, w * col:w * col + w] = sub_image[:, :] return image