def __init__(self, method): if not isinstance(method, str): raise ValueError(f'Invalid method type "{type(method)}"') from compressai import available_entropy_coders if method not in available_entropy_coders(): methods = ", ".join(available_entropy_coders()) raise ValueError(f'Unknown entropy coder "{method}"' f" (available: {methods})") if method == "ans": from compressai import ans encoder = ans.RansEncoder() decoder = ans.RansDecoder() elif method == "rangecoder": import range_coder encoder = range_coder.RangeEncoder() decoder = range_coder.RangeDecoder() self.name = method self._encoder = encoder self._decoder = decoder
def compress_image(self, code_list: List[torch.Tensor], savepath="default.dci"): """ Args: code_list: List of encodings for image savepath: path to save the image to Returns: Nothing, writes compressed image savepath """ if not self.cdf: raise Exception( "Cannot compress image without cdf function (hint: call build_latent_distribution)" ) flat_code_list = [] for x in code_list: # Holds each flattened featured to be concatenated after the loop assert x.shape[ 0] == 1, "can only compress one image at a time, for now" flat_code_list.append(x.long().flatten()) full_code = torch.cat(flat_code_list) + self.min_val # use RangeEncoder to write compressed representation to file encoder = rc.RangeEncoder(savepath) code_list = full_code.tolist() encoder.encode(code_list, self.cdf) encoder.close()
def __init__(self, method): if not isinstance(method, str): raise ValueError(f'Invalid method type "{type(method)}"') from compressai import available_entropy_coders if method not in available_entropy_coders(): methods = ', '.join(available_entropy_coders()) raise ValueError(f'Unknown entropy coder "{method}"' f' (available: {methods})') if method == 'ans': from compressai import ans # pylint: disable=E0611 encoder = ans.RansEncoder() decoder = ans.RansDecoder() elif method == 'rangecoder': import range_coder # pylint: disable=E0401 encoder = range_coder.RangeEncoder() decoder = range_coder.RangeDecoder() self._encoder = encoder self._decoder = decoder
def apply_range_encoder(seq_data, encodepath, args, config): resolution = config['resolution'] prob = np.load('data_info/distribution_info_{}.npy'.format(args.model_num)) # Avoid zero prob modified_freq = prob * resolution + 1 modified_prob = modified_freq / np.sum(modified_freq) # print(modified_prob) cum_freq = range_coder.prob_to_cum_freq(modified_prob, resolution=resolution) # print('-----') # print(cum_freq) # cum_freq = [0] + [i for i in range(1, 256 + 1)] range_encoder = range_coder.RangeEncoder(encodepath) # Whether cum_freq resolution influences performance ? range_encoder.encode(seq_data, cum_freq) range_encoder.close()