def decompress(self, strings, min_v, max_v, shape, channels=None, device='cpu'): """Decompress values from their compressed string representations. Arguments: strings: A string `Tensor` vector containing the compressed data.(numpy) min_v & max_v.(numpy) shape: A `Tensor` vector of int32 type. Contains the shape of the tensor to be decompressed, excluding the batch dimension. [points, channels] (numpy) Returns: The decompressed `Tensor`. (torch) # TODO """ # To Tensorflow if min_v == max_v: values = torch.zeros(tuple(shape)).to(device) return values strings = tf.convert_to_tensor(strings, dtype=tf.string) shape = tf.convert_to_tensor(shape)# [points, channels] cdf = self._get_cdf(min_v, max_v, device) # range decode values = coder_ops.range_decode( strings, shape, cdf, precision=self._range_coder_precision) + min_v values = tf.reshape(values, shape) values = tf.cast(values, tf.float32) values = values.numpy() values = torch.from_numpy(values).to(device) return values
def decompress(self, strings, min_v, max_v, shape, channels=None): """Decompress values from their compressed string representations. Arguments: strings: A string `Tensor` vector containing the compressed data. shape: A `Tensor` vector of int32 type. Contains the shape of the tensor to be decompressed. [batch size, length, width, height, channels] min_v & max_v: minimum & maximum values. Returns: The decompressed `Tensor`. tf.float32. """ with tf.name_scope(self._name_scope()): strings = tf.convert_to_tensor(strings, dtype=tf.string) shape = tf.convert_to_tensor( shape, dtype='int32') # [batch size, length, width, height, channels] min_v = tf.convert_to_tensor(min_v, dtype='int32') max_v = tf.convert_to_tensor(max_v, dtype='int32') if self.built: ndim = self.input_spec.ndim if channels is None: channels = self.input_spec.axes[ndim - 1] else: channels = int(channels) ndim = shape.shape[0].value # ndim=5 input_shape = ndim * [None] input_shape[-1] = channels self.build(input_shape) code_length = tf.reduce_prod(shape) code_shape = (tf.constant(code_length // channels, dtype='int32'), tf.constant(channels, dtype='int32')) # shape=[-1, channel] # get cdf. cdf = self._get_cdf(min_v, max_v) # range decode. values = coder_ops.range_decode( strings, code_shape, cdf, precision=self._range_coder_precision) values = tf.cast(values, tf.int32) values = values + min_v values = tf.reshape(values, shape) values = tf.cast(values, tf.float32) return values
def testReadmeExample(self): data = random_ops.random_uniform((128, 128), 0, 10, dtype=dtypes.int32) histogram = math_ops.bincount(data, minlength=10, maxlength=10) cdf = math_ops.cumsum(histogram, exclusive=False) cdf = array_ops.pad(cdf, [[1, 0]]) cdf = array_ops.reshape(cdf, [1, 1, -1]) data = math_ops.cast(data, dtypes.int16) encoded = coder_ops.range_encode(data, cdf, precision=14) decoded = coder_ops.range_decode( encoded, array_ops.shape(data), cdf, precision=14) with self.test_session() as sess: self.assertAllEqual(*sess.run((data, decoded)))
def decompress(self, strings, loc, scale, min_v, max_v, datashape): """Decompress values from their compressed string representations. Arguments: strings: A string `Tensor` vector containing the compressed data. shape: A `Tensor` vector of int32 type. Contains the shape of the tensor to be decompressed. [batch size, length, width, height, channels] loc & scale: parameters of distributions. min_v & max_v: minimum & maximum values. Return: outputs [BatchSize, H, W, D, C] """ strings = tf.convert_to_tensor(strings, dtype=tf.string) datashape = tf.convert_to_tensor(datashape, dtype='int32') loc = tf.convert_to_tensor(loc, dtype=self.dtype) scale = tf.convert_to_tensor(scale, dtype=self.dtype) min_v = tf.convert_to_tensor(min_v, dtype='int32') max_v = tf.convert_to_tensor(max_v, dtype='int32') # reshape. channels = datashape[-1] loc = tf.reshape(loc, [-1, channels]) scale = tf.reshape(scale, [-1, channels]) # get cdf. cdf = self._get_cdf(loc, scale, min_v, max_v, datashape) # [BatchSizexHxWxD, C, N] # range decode. code_shape = (tf.reduce_prod(datashape) / channels, channels ) # shape=[-1, channel] values = coder_ops.range_decode(strings, code_shape, cdf, precision=self._range_coder_precision) values = tf.cast(values, tf.int32) values = values + min_v values = tf.reshape(values, datashape) values = tf.cast(values, "float32") return values
def loop_body(string): return coder_ops.range_decode( string, shape, cdf, precision=self.range_coder_precision)