├── 5.1. Evaluation on Density Estimation ├── Readme.md ├── VAE_TwoLayer │ └── model.ckpt-3280.pt ├── infer.py ├── model.py └── train.py ├── 5.2. Evaluation on Bit Allocation ├── Checkpoints │ └── download_compressai_models.py ├── DCVC │ ├── Tools │ │ └── flowlib.py │ ├── download_compressai_models.py │ ├── flow_pretrain_np │ │ ├── modelL1_F-1-bias.npy │ │ ├── modelL1_F-1-weight.npy │ │ ├── modelL1_F-2-bias.npy │ │ ├── modelL1_F-2-weight.npy │ │ ├── modelL1_F-3-bias.npy │ │ ├── modelL1_F-3-weight.npy │ │ ├── modelL1_F-4-bias.npy │ │ ├── modelL1_F-4-weight.npy │ │ ├── modelL1_F-5-bias.npy │ │ ├── modelL1_F-5-weight.npy │ │ ├── modelL2_F-1-bias.npy │ │ ├── modelL2_F-1-weight.npy │ │ ├── modelL2_F-2-bias.npy │ │ ├── modelL2_F-2-weight.npy │ │ ├── modelL2_F-3-bias.npy │ │ ├── modelL2_F-3-weight.npy │ │ ├── modelL2_F-4-bias.npy │ │ ├── modelL2_F-4-weight.npy │ │ ├── modelL2_F-5-bias.npy │ │ ├── modelL2_F-5-weight.npy │ │ ├── modelL3_F-1-bias.npy │ │ ├── modelL3_F-1-weight.npy │ │ ├── modelL3_F-2-bias.npy │ │ ├── modelL3_F-2-weight.npy │ │ ├── modelL3_F-3-bias.npy │ │ ├── modelL3_F-3-weight.npy │ │ ├── modelL3_F-4-bias.npy │ │ ├── modelL3_F-4-weight.npy │ │ ├── modelL3_F-5-bias.npy │ │ ├── modelL3_F-5-weight.npy │ │ ├── modelL4_F-1-bias.npy │ │ ├── modelL4_F-1-weight.npy │ │ ├── modelL4_F-2-bias.npy │ │ ├── modelL4_F-2-weight.npy │ │ ├── modelL4_F-3-bias.npy │ │ ├── modelL4_F-3-weight.npy │ │ ├── modelL4_F-4-bias.npy │ │ ├── modelL4_F-4-weight.npy │ │ ├── modelL4_F-5-bias.npy │ │ ├── modelL4_F-5-weight.npy │ │ ├── modelL5_F-1-bias.npy │ │ ├── modelL5_F-1-weight.npy │ │ ├── modelL5_F-2-bias.npy │ │ ├── modelL5_F-2-weight.npy │ │ ├── modelL5_F-3-bias.npy │ │ ├── modelL5_F-3-weight.npy │ │ ├── modelL5_F-4-bias.npy │ │ ├── modelL5_F-4-weight.npy │ │ ├── modelL5_F-5-bias.npy │ │ ├── modelL5_F-5-weight.npy │ │ ├── modelL6_F-1-bias.npy │ │ ├── modelL6_F-1-weight.npy │ │ ├── modelL6_F-2-bias.npy │ │ ├── modelL6_F-2-weight.npy │ │ ├── modelL6_F-3-bias.npy │ │ ├── modelL6_F-3-weight.npy │ │ ├── modelL6_F-4-bias.npy │ │ ├── modelL6_F-4-weight.npy │ │ ├── modelL6_F-5-bias.npy │ │ └── modelL6_F-5-weight.npy │ ├── ms_ssim_torch.py │ └── src │ │ ├── cpp │ │ ├── 3rdparty │ │ │ ├── CMakeLists.txt │ │ │ ├── pybind11 │ │ │ │ ├── CMakeLists.txt │ │ │ │ └── CMakeLists.txt.in │ │ │ └── ryg_rans │ │ │ │ ├── CMakeLists.txt │ │ │ │ └── CMakeLists.txt.in │ │ ├── CMakeLists.txt │ │ ├── ops │ │ │ ├── CMakeLists.txt │ │ │ └── ops.cpp │ │ └── rans │ │ │ ├── CMakeLists.txt │ │ │ ├── rans_interface.cpp │ │ │ └── rans_interface.hpp │ │ ├── entropy_models │ │ ├── entropy_models.py │ │ └── video_entropy_models.py │ │ ├── layers │ │ ├── gdn.py │ │ └── layers.py │ │ ├── models │ │ ├── DCVC_net.py │ │ ├── DCVC_net_dcvc.py │ │ ├── DCVC_net_new.py │ │ ├── priors.py │ │ ├── utils.py │ │ ├── video_net.py │ │ └── waseda.py │ │ ├── ops │ │ ├── bound_ops.py │ │ └── parametrizers.py │ │ ├── utils │ │ ├── Dataset.py │ │ ├── Utils.py │ │ ├── Utils_dcvc.py │ │ ├── common.py │ │ ├── ms_ssim_torch.py │ │ └── stream_helper.py │ │ └── zoo │ │ └── image.py ├── DVC │ ├── __init__.py │ ├── examples │ │ └── flow_pretrain_np │ │ │ ├── modelL1_F-1-bias.npy │ │ │ ├── modelL1_F-1-weight.npy │ │ │ ├── modelL1_F-2-bias.npy │ │ │ ├── modelL1_F-2-weight.npy │ │ │ ├── modelL1_F-3-bias.npy │ │ │ ├── modelL1_F-3-weight.npy │ │ │ ├── modelL1_F-4-bias.npy │ │ │ ├── modelL1_F-4-weight.npy │ │ │ ├── modelL1_F-5-bias.npy │ │ │ ├── modelL1_F-5-weight.npy │ │ │ ├── modelL2_F-1-bias.npy │ │ │ ├── modelL2_F-1-weight.npy │ │ │ ├── modelL2_F-2-bias.npy │ │ │ ├── modelL2_F-2-weight.npy │ │ │ ├── modelL2_F-3-bias.npy │ │ │ ├── modelL2_F-3-weight.npy │ │ │ ├── modelL2_F-4-bias.npy │ │ │ ├── modelL2_F-4-weight.npy │ │ │ ├── modelL2_F-5-bias.npy │ │ │ ├── modelL2_F-5-weight.npy │ │ │ ├── modelL3_F-1-bias.npy │ │ │ ├── modelL3_F-1-weight.npy │ │ │ ├── modelL3_F-2-bias.npy │ │ │ ├── modelL3_F-2-weight.npy │ │ │ ├── modelL3_F-3-bias.npy │ │ │ ├── modelL3_F-3-weight.npy │ │ │ ├── modelL3_F-4-bias.npy │ │ │ ├── modelL3_F-4-weight.npy │ │ │ ├── modelL3_F-5-bias.npy │ │ │ ├── modelL3_F-5-weight.npy │ │ │ ├── modelL4_F-1-bias.npy │ │ │ ├── modelL4_F-1-weight.npy │ │ │ ├── modelL4_F-2-bias.npy │ │ │ ├── modelL4_F-2-weight.npy │ │ │ ├── modelL4_F-3-bias.npy │ │ │ ├── modelL4_F-3-weight.npy │ │ │ ├── modelL4_F-4-bias.npy │ │ │ ├── modelL4_F-4-weight.npy │ │ │ ├── modelL4_F-5-bias.npy │ │ │ ├── modelL4_F-5-weight.npy │ │ │ ├── modelL5_F-1-bias.npy │ │ │ ├── modelL5_F-1-weight.npy │ │ │ ├── modelL5_F-2-bias.npy │ │ │ ├── modelL5_F-2-weight.npy │ │ │ ├── modelL5_F-3-bias.npy │ │ │ ├── modelL5_F-3-weight.npy │ │ │ ├── modelL5_F-4-bias.npy │ │ │ ├── modelL5_F-4-weight.npy │ │ │ ├── modelL5_F-5-bias.npy │ │ │ ├── modelL5_F-5-weight.npy │ │ │ ├── modelL6_F-1-bias.npy │ │ │ ├── modelL6_F-1-weight.npy │ │ │ ├── modelL6_F-2-bias.npy │ │ │ ├── modelL6_F-2-weight.npy │ │ │ ├── modelL6_F-3-bias.npy │ │ │ ├── modelL6_F-3-weight.npy │ │ │ ├── modelL6_F-4-bias.npy │ │ │ ├── modelL6_F-4-weight.npy │ │ │ ├── modelL6_F-5-bias.npy │ │ │ └── modelL6_F-5-weight.npy │ ├── net.py │ └── subnet │ │ ├── GDN.py │ │ ├── __init__.py │ │ ├── analysis.py │ │ ├── analysis_mv.py │ │ ├── analysis_prior.py │ │ ├── basics.py │ │ ├── bitEstimator.py │ │ ├── endecoder.py │ │ ├── flowlib.py │ │ ├── ms_ssim_torch.py │ │ ├── sga.py │ │ ├── synthesis.py │ │ ├── synthesis_mv.py │ │ ├── synthesis_prior.py │ │ └── vis_flow.py ├── HSTEM │ ├── __init__.py │ └── src │ │ ├── cpp │ │ ├── 3rdparty │ │ │ ├── CMakeLists.txt │ │ │ ├── pybind11 │ │ │ │ ├── CMakeLists.txt │ │ │ │ └── CMakeLists.txt.in │ │ │ └── ryg_rans │ │ │ │ ├── CMakeLists.txt │ │ │ │ └── CMakeLists.txt.in │ │ ├── CMakeLists.txt │ │ ├── ops │ │ │ ├── CMakeLists.txt │ │ │ └── ops.cpp │ │ └── rans │ │ │ ├── CMakeLists.txt │ │ │ ├── rans_interface.cpp │ │ │ └── rans_interface.hpp │ │ ├── entropy_models │ │ └── entropy_models.py │ │ ├── layers │ │ └── layers.py │ │ ├── models │ │ ├── common_model.py │ │ ├── image_model.py │ │ ├── video_model.py │ │ ├── video_model_q.py │ │ └── video_net.py │ │ ├── ryg_rans │ │ ├── LICENSE │ │ ├── Makefile │ │ ├── README │ │ ├── book1 │ │ ├── main.cpp │ │ ├── main64.cpp │ │ ├── main_alias.cpp │ │ ├── main_simd.cpp │ │ ├── platform.h │ │ ├── rans64.h │ │ ├── rans_byte.h │ │ └── rans_word_sse41.h │ │ └── utils │ │ ├── Dataset.py │ │ ├── Utils.py │ │ ├── common.py │ │ ├── png_reader.py │ │ └── stream_helper.py ├── I_model │ ├── cpp │ │ ├── 3rdparty │ │ │ ├── CMakeLists.txt │ │ │ ├── pybind11 │ │ │ │ ├── CMakeLists.txt │ │ │ │ └── CMakeLists.txt.in │ │ │ └── ryg_rans │ │ │ │ ├── CMakeLists.txt │ │ │ │ └── CMakeLists.txt.in │ │ ├── CMakeLists.txt │ │ ├── ops │ │ │ ├── CMakeLists.txt │ │ │ └── ops.cpp │ │ └── rans │ │ │ ├── CMakeLists.txt │ │ │ ├── rans_interface.cpp │ │ │ └── rans_interface.hpp │ ├── entropy_models │ │ ├── entropy_models.py │ │ └── video_entropy_models.py │ ├── layers │ │ ├── gdn.py │ │ └── layers.py │ ├── models │ │ ├── priors.py │ │ ├── utils.py │ │ └── waseda.py │ ├── ops │ │ ├── bound_ops.py │ │ └── parametrizers.py │ ├── utils │ │ ├── common.py │ │ ├── ms_ssim_torch.py │ │ └── stream_helper.py │ └── zoo │ │ └── image.py ├── Readme.md ├── Tools │ ├── filelists │ │ ├── B.txt │ │ ├── C.txt │ │ ├── D.txt │ │ ├── E.txt │ │ └── UVG.txt │ ├── flowlib.py │ └── ms_ssim_torch.py ├── __init__.py ├── dataset.py └── main.py ├── LICENSE └── Readme.md /5.1. Evaluation on Density Estimation/Readme.md: -------------------------------------------------------------------------------- 1 | ## Code for Bit Allocation using Optimization Sec 5.1 2 | * prerequisite 3 | * python 4 | * pytorch 5 | * numpy 6 | * pandas 7 | * compressai 8 | ## Dataset 9 | * the dataset used is MNIST and can be automatically downloaded from torchvision 10 | ## Obtain a 2 level VAE base model for SAVI 11 | * you may train the model by yourself 12 | ```bash 13 | python train.py --model=VAE_TwoLayer_Alt 14 | ``` 15 | * for more detailed cli information, do: 16 | ```bash 17 | python train.py --help 18 | ``` 19 | * or you can also directly adopt the pre-trained model we provide in ./VAE_TwoLayer/model.ckpt-3280.pt 20 | 21 | ## Perform SAVI on 2 level VAE 22 | * with a pre-trained checkpoint, you can run SAVI directly 23 | ```bash 24 | python infer.py --resume=$VAE_Checkpoint 25 | ``` 26 | * for more detailed cli information, do: 27 | ```bash 28 | python infer.py --help 29 | ``` 30 | -------------------------------------------------------------------------------- /5.1. Evaluation on Density Estimation/VAE_TwoLayer/model.ckpt-3280.pt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.1. Evaluation on Density Estimation/VAE_TwoLayer/model.ckpt-3280.pt -------------------------------------------------------------------------------- /5.1. Evaluation on Density Estimation/infer.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import random 3 | import numpy as np 4 | import torch 5 | import torch.nn as nn 6 | import torch.optim as optim 7 | import torch.nn.functional as F 8 | from torch.utils.data import DataLoader 9 | from torchvision import datasets, transforms, utils 10 | from tqdm import tqdm 11 | from pathlib import Path 12 | from model import BinarizeMnist, VAE_SingleLayer, VAE_TwoLayer 13 | import os 14 | os.environ["CUDA_VISIBLE_DEVICES"] = "0" 15 | 16 | def main(args): 17 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 18 | model = VAE_TwoLayer() 19 | model.to(device) 20 | checkpoint = torch.load(args.resume, map_location=lambda storage, loc: storage) 21 | model.load_state_dict(checkpoint["model"], strict=False) 22 | transform_test= transforms.Compose([ 23 | transforms.ToTensor(), 24 | BinarizeMnist() 25 | ]) 26 | test_dataset = datasets.MNIST("./MNIST", train=False, download=True, 27 | transform=transform_test) 28 | test_dataloader = DataLoader(test_dataset, batch_size=1, shuffle=False, drop_last=True, 29 | num_workers=args.num_workers, pin_memory=True) 30 | N = 1 * 28 * 28 31 | model.eval() 32 | average_elbo_favi = average_elbo_naive = average_eblo_appro = average_eblo_accur = 0 33 | valid_i = 1 34 | for i, (images, _) in enumerate(test_dataloader, 1): 35 | images = images.to(device) 36 | elbo_favi, elbo_naive, elbo_appro, elbo_accur = model.eval_savi(images, args.num_iter, args.learning_rate, "sgvb2", 3470) 37 | if torch.isnan(elbo_favi).any() or torch.isnan(elbo_naive).any() or torch.isnan(elbo_appro).any() or torch.isnan(elbo_accur).any(): 38 | # ignore nan sample at this time 39 | continue 40 | valid_i += 1 41 | elbo_favi = torch.mean(elbo_favi / N, dim=0) 42 | elbo_naive = torch.mean(elbo_naive / N, dim=0) 43 | elbo_appro = torch.mean(elbo_appro / N, dim=0) 44 | elbo_accur = torch.mean(elbo_accur / N, dim=0) 45 | average_elbo_favi += elbo_favi.item() 46 | average_elbo_naive += elbo_naive.item() 47 | average_eblo_appro += elbo_appro.item() 48 | average_eblo_accur += elbo_accur.item() 49 | if i == 500: 50 | break 51 | average_elbo_favi /= valid_i 52 | average_elbo_naive /= valid_i 53 | average_eblo_appro /= valid_i 54 | average_eblo_accur /= valid_i 55 | print("[test] elbo favi: {:.10f}, elbo naive: {:.10f}, elbo approx: {:.10f}, elbo accur: {:.10f}" 56 | .format(average_elbo_favi * N, average_elbo_naive * N, average_eblo_appro * N, average_eblo_accur * N)) 57 | 58 | 59 | if __name__ == "__main__": 60 | 61 | SEED=3470 62 | random.seed(SEED) 63 | np.random.seed(SEED) 64 | torch.manual_seed(SEED) 65 | torch.cuda.manual_seed(SEED) 66 | torch.backends.cudnn.deterministic=True 67 | parser = argparse.ArgumentParser() 68 | parser.add_argument("--num-workers", type=int, default=4, help="Number of dataloader workers.") 69 | parser.add_argument("--num-iter", type=int, default=50, help="Number of training steps.") 70 | parser.add_argument("--learning-rate", type=float, default=1e-3, help="Learning rate.") 71 | parser.add_argument("--resume", type=str, default="", help="model load path.") 72 | args = parser.parse_args() 73 | main(args) -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/Checkpoints/download_compressai_models.py: -------------------------------------------------------------------------------- 1 | import urllib.request 2 | 3 | # The model weights of intra coding come from CompressAI. 4 | root_url = "https://compressai.s3.amazonaws.com/models/v1/" 5 | 6 | model_names = [ 7 | "cheng2020-anchor-3.pth.tar", 8 | "cheng2020-anchor-4.pth.tar", 9 | "cheng2020-anchor-5.pth.tar", 10 | "cheng2020-anchor-6.pth.tar", 11 | ] 12 | 13 | for model in model_names: 14 | print(f"downloading {model}") 15 | urllib.request.urlretrieve(root_url+model, model) -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/download_compressai_models.py: -------------------------------------------------------------------------------- 1 | import urllib.request 2 | 3 | # The model weights of intra coding come from CompressAI. 4 | root_url = "https://compressai.s3.amazonaws.com/models/v1/" 5 | 6 | model_names = [ 7 | "bmshj2018-hyperprior-ms-ssim-3-92dd7878.pth.tar", 8 | "bmshj2018-hyperprior-ms-ssim-4-4377354e.pth.tar", 9 | "bmshj2018-hyperprior-ms-ssim-5-c34afc8d.pth.tar", 10 | "bmshj2018-hyperprior-ms-ssim-6-3a6d8229.pth.tar", 11 | "cheng2020-anchor-3.pth.tar", 12 | "cheng2020-anchor-4.pth.tar", 13 | "cheng2020-anchor-5.pth.tar", 14 | "cheng2020-anchor-6.pth.tar", 15 | ] 16 | 17 | for model in model_names: 18 | print(f"downloading {model}") 19 | urllib.request.urlretrieve(root_url+model, model) -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-1-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-1-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-1-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-1-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-2-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-2-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-2-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-2-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-3-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-3-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-3-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-3-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-4-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-4-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-4-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-4-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-5-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-5-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-5-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL1_F-5-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-1-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-1-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-1-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-1-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-2-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-2-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-2-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-2-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-3-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-3-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-3-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-3-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-4-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-4-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-4-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-4-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-5-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-5-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-5-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL2_F-5-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-1-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-1-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-1-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-1-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-2-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-2-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-2-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-2-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-3-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-3-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-3-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-3-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-4-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-4-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-4-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-4-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-5-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-5-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-5-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL3_F-5-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-1-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-1-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-1-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-1-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-2-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-2-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-2-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-2-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-3-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-3-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-3-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-3-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-4-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-4-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-4-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-4-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-5-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-5-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-5-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL4_F-5-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-1-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-1-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-1-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-1-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-2-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-2-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-2-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-2-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-3-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-3-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-3-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-3-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-4-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-4-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-4-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-4-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-5-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-5-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-5-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL5_F-5-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-1-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-1-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-1-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-1-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-2-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-2-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-2-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-2-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-3-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-3-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-3-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-3-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-4-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-4-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-4-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-4-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-5-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-5-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-5-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DCVC/flow_pretrain_np/modelL6_F-5-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/cpp/3rdparty/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(pybind11) 2 | add_subdirectory(ryg_rans) -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/cpp/3rdparty/pybind11/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # set(PYBIND11_PYTHON_VERSION 3.8 CACHE STRING "") 2 | configure_file(CMakeLists.txt.in pybind11-download/CMakeLists.txt) 3 | execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . 4 | RESULT_VARIABLE result 5 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pybind11-download ) 6 | if(result) 7 | message(FATAL_ERROR "CMake step for pybind11 failed: ${result}") 8 | endif() 9 | execute_process(COMMAND ${CMAKE_COMMAND} --build . 10 | RESULT_VARIABLE result 11 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pybind11-download ) 12 | if(result) 13 | message(FATAL_ERROR "Build step for pybind11 failed: ${result}") 14 | endif() 15 | 16 | add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/pybind11-src/ 17 | ${CMAKE_CURRENT_BINARY_DIR}/pybind11-build/ 18 | EXCLUDE_FROM_ALL) 19 | 20 | set(PYBIND11_INCLUDE 21 | ${CMAKE_CURRENT_BINARY_DIR}/pybind11-src/include/ 22 | CACHE INTERNAL "") 23 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/cpp/3rdparty/pybind11/CMakeLists.txt.in: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.6.3) 2 | 3 | project(pybind11-download NONE) 4 | 5 | include(ExternalProject) 6 | if(IS_DIRECTORY "${PROJECT_BINARY_DIR}/3rdparty/pybind11/pybind11-src/include") 7 | ExternalProject_Add(pybind11 8 | GIT_REPOSITORY https://github.com/pybind/pybind11.git 9 | GIT_TAG v2.6.1 10 | GIT_SHALLOW 1 11 | SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/pybind11-src" 12 | BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/pybind11-build" 13 | DOWNLOAD_COMMAND "" 14 | UPDATE_COMMAND "" 15 | CONFIGURE_COMMAND "" 16 | BUILD_COMMAND "" 17 | INSTALL_COMMAND "" 18 | TEST_COMMAND "" 19 | ) 20 | else() 21 | ExternalProject_Add(pybind11 22 | GIT_REPOSITORY https://github.com/pybind/pybind11.git 23 | GIT_TAG v2.6.1 24 | GIT_SHALLOW 1 25 | SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/pybind11-src" 26 | BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/pybind11-build" 27 | UPDATE_COMMAND "" 28 | CONFIGURE_COMMAND "" 29 | BUILD_COMMAND "" 30 | INSTALL_COMMAND "" 31 | TEST_COMMAND "" 32 | ) 33 | endif() 34 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/cpp/3rdparty/ryg_rans/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | configure_file(CMakeLists.txt.in ryg_rans-download/CMakeLists.txt) 2 | execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . 3 | RESULT_VARIABLE result 4 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-download ) 5 | if(result) 6 | message(FATAL_ERROR "CMake step for ryg_rans failed: ${result}") 7 | endif() 8 | execute_process(COMMAND ${CMAKE_COMMAND} --build . 9 | RESULT_VARIABLE result 10 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-download ) 11 | if(result) 12 | message(FATAL_ERROR "Build step for ryg_rans failed: ${result}") 13 | endif() 14 | 15 | # add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-src/ 16 | # ${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-build 17 | # EXCLUDE_FROM_ALL) 18 | 19 | set(RYG_RANS_INCLUDE 20 | ${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-src/ 21 | CACHE INTERNAL "") 22 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/cpp/3rdparty/ryg_rans/CMakeLists.txt.in: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.6.3) 2 | 3 | project(ryg_rans-download NONE) 4 | 5 | include(ExternalProject) 6 | if(EXISTS "${PROJECT_BINARY_DIR}/3rdparty/ryg_rans/ryg_rans-src/rans64.h") 7 | ExternalProject_Add(ryg_rans 8 | GIT_REPOSITORY https://github.com/rygorous/ryg_rans.git 9 | GIT_TAG c9d162d996fd600315af9ae8eb89d832576cb32d 10 | GIT_SHALLOW 1 11 | SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-src" 12 | BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-build" 13 | DOWNLOAD_COMMAND "" 14 | UPDATE_COMMAND "" 15 | CONFIGURE_COMMAND "" 16 | BUILD_COMMAND "" 17 | INSTALL_COMMAND "" 18 | TEST_COMMAND "" 19 | ) 20 | else() 21 | ExternalProject_Add(ryg_rans 22 | GIT_REPOSITORY https://github.com/rygorous/ryg_rans.git 23 | GIT_TAG c9d162d996fd600315af9ae8eb89d832576cb32d 24 | GIT_SHALLOW 1 25 | SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-src" 26 | BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-build" 27 | UPDATE_COMMAND "" 28 | CONFIGURE_COMMAND "" 29 | BUILD_COMMAND "" 30 | INSTALL_COMMAND "" 31 | TEST_COMMAND "" 32 | ) 33 | endif() 34 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/cpp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 3.6.3) 2 | project (ErrorRecovery) 3 | 4 | set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo" CACHE STRING "" FORCE) 5 | 6 | set(CMAKE_CXX_STANDARD 17) 7 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | set(CMAKE_CXX_EXTENSIONS OFF) 9 | 10 | # treat warning as error 11 | if (MSVC) 12 | add_compile_options(/W4 /WX) 13 | else() 14 | add_compile_options(-Wall -Wextra -pedantic -Werror) 15 | endif() 16 | 17 | # The sequence is tricky, put 3rd party first 18 | add_subdirectory(3rdparty) 19 | add_subdirectory (ops) 20 | add_subdirectory (rans) 21 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/cpp/ops/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.7) 2 | set(PROJECT_NAME MLCodec_CXX) 3 | project(${PROJECT_NAME}) 4 | 5 | set(cxx_source 6 | ops.cpp 7 | ) 8 | 9 | set(include_dirs 10 | ${CMAKE_CURRENT_SOURCE_DIR} 11 | ${PYBIND11_INCLUDE} 12 | ) 13 | 14 | pybind11_add_module(${PROJECT_NAME} ${cxx_source}) 15 | 16 | target_include_directories (${PROJECT_NAME} PUBLIC ${include_dirs}) 17 | 18 | # The post build argument is executed after make! 19 | add_custom_command( 20 | TARGET ${PROJECT_NAME} POST_BUILD 21 | COMMAND 22 | "${CMAKE_COMMAND}" -E copy 23 | "$" 24 | "${CMAKE_CURRENT_SOURCE_DIR}/../../entropy_models/" 25 | ) 26 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/cpp/ops/ops.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 InterDigital Communications, Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | std::vector pmf_to_quantized_cdf(const std::vector &pmf, 25 | int precision) { 26 | /* NOTE(begaintj): ported from `ryg_rans` public implementation. Not optimal 27 | * although it's only run once per model after training. See TF/compression 28 | * implementation for an optimized version. */ 29 | 30 | std::vector cdf(pmf.size() + 1); 31 | cdf[0] = 0; /* freq 0 */ 32 | 33 | std::transform(pmf.begin(), pmf.end(), cdf.begin() + 1, [=](float p) { 34 | return static_cast(std::round(p * (1 << precision)) + 0.5); 35 | }); 36 | 37 | const uint32_t total = std::accumulate(cdf.begin(), cdf.end(), 0); 38 | 39 | std::transform( 40 | cdf.begin(), cdf.end(), cdf.begin(), [precision, total](uint32_t p) { 41 | return static_cast((((1ull << precision) * p) / total)); 42 | }); 43 | 44 | std::partial_sum(cdf.begin(), cdf.end(), cdf.begin()); 45 | cdf.back() = 1 << precision; 46 | 47 | for (int i = 0; i < static_cast(cdf.size() - 1); ++i) { 48 | if (cdf[i] == cdf[i + 1]) { 49 | /* Try to steal frequency from low-frequency symbols */ 50 | uint32_t best_freq = ~0u; 51 | int best_steal = -1; 52 | for (int j = 0; j < static_cast(cdf.size()) - 1; ++j) { 53 | uint32_t freq = cdf[j + 1] - cdf[j]; 54 | if (freq > 1 && freq < best_freq) { 55 | best_freq = freq; 56 | best_steal = j; 57 | } 58 | } 59 | 60 | assert(best_steal != -1); 61 | 62 | if (best_steal < i) { 63 | for (int j = best_steal + 1; j <= i; ++j) { 64 | cdf[j]--; 65 | } 66 | } else { 67 | assert(best_steal > i); 68 | for (int j = i + 1; j <= best_steal; ++j) { 69 | cdf[j]++; 70 | } 71 | } 72 | } 73 | } 74 | 75 | assert(cdf[0] == 0); 76 | assert(cdf.back() == (1u << precision)); 77 | for (int i = 0; i < static_cast(cdf.size()) - 1; ++i) { 78 | assert(cdf[i + 1] > cdf[i]); 79 | } 80 | 81 | return cdf; 82 | } 83 | 84 | PYBIND11_MODULE(MLCodec_CXX, m) { 85 | m.attr("__name__") = "MLCodec_CXX"; 86 | 87 | m.doc() = "C++ utils"; 88 | 89 | m.def("pmf_to_quantized_cdf", &pmf_to_quantized_cdf, 90 | "Return quantized CDF for a given PMF"); 91 | } 92 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/cpp/rans/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.7) 2 | set(PROJECT_NAME MLCodec_rans) 3 | project(${PROJECT_NAME}) 4 | 5 | set(rans_source 6 | rans_interface.hpp 7 | rans_interface.cpp 8 | ) 9 | 10 | set(include_dirs 11 | ${CMAKE_CURRENT_SOURCE_DIR} 12 | ${PYBIND11_INCLUDE} 13 | ${RYG_RANS_INCLUDE} 14 | ) 15 | 16 | pybind11_add_module(${PROJECT_NAME} ${rans_source}) 17 | 18 | target_include_directories (${PROJECT_NAME} PUBLIC ${include_dirs}) 19 | 20 | # The post build argument is executed after make! 21 | add_custom_command( 22 | TARGET ${PROJECT_NAME} POST_BUILD 23 | COMMAND 24 | "${CMAKE_COMMAND}" -E copy 25 | "$" 26 | "${CMAKE_CURRENT_SOURCE_DIR}/../../entropy_models/" 27 | ) 28 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/cpp/rans/rans_interface.hpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 InterDigital Communications, Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #pragma once 17 | 18 | #include 19 | #include 20 | 21 | #ifdef __GNUC__ 22 | #pragma GCC diagnostic push 23 | #pragma GCC diagnostic ignored "-Wpedantic" 24 | #pragma GCC diagnostic ignored "-Wsign-compare" 25 | #elif _MSC_VER 26 | #pragma warning(push, 0) 27 | #endif 28 | 29 | #include 30 | 31 | #ifdef __GNUC__ 32 | #pragma GCC diagnostic pop 33 | #elif _MSC_VER 34 | #pragma warning(pop) 35 | #endif 36 | 37 | namespace py = pybind11; 38 | 39 | struct RansSymbol { 40 | uint16_t start; 41 | uint16_t range; 42 | bool bypass; // bypass flag to write raw bits to the stream 43 | }; 44 | 45 | /* NOTE: Warning, we buffer everything for now... In case of large files we 46 | * should split the bitstream into chunks... Or for a memory-bounded encoder 47 | **/ 48 | class BufferedRansEncoder { 49 | public: 50 | BufferedRansEncoder() = default; 51 | 52 | BufferedRansEncoder(const BufferedRansEncoder &) = delete; 53 | BufferedRansEncoder(BufferedRansEncoder &&) = delete; 54 | BufferedRansEncoder &operator=(const BufferedRansEncoder &) = delete; 55 | BufferedRansEncoder &operator=(BufferedRansEncoder &&) = delete; 56 | 57 | void encode_with_indexes(const std::vector &symbols, 58 | const std::vector &indexes, 59 | const std::vector> &cdfs, 60 | const std::vector &cdfs_sizes, 61 | const std::vector &offsets); 62 | py::bytes flush(); 63 | 64 | private: 65 | std::vector _syms; 66 | }; 67 | 68 | class RansEncoder { 69 | public: 70 | RansEncoder() = default; 71 | 72 | RansEncoder(const RansEncoder &) = delete; 73 | RansEncoder(RansEncoder &&) = delete; 74 | RansEncoder &operator=(const RansEncoder &) = delete; 75 | RansEncoder &operator=(RansEncoder &&) = delete; 76 | 77 | py::bytes encode_with_indexes(const std::vector &symbols, 78 | const std::vector &indexes, 79 | const std::vector> &cdfs, 80 | const std::vector &cdfs_sizes, 81 | const std::vector &offsets); 82 | }; 83 | 84 | class RansDecoder { 85 | public: 86 | RansDecoder() = default; 87 | 88 | RansDecoder(const RansDecoder &) = delete; 89 | RansDecoder(RansDecoder &&) = delete; 90 | RansDecoder &operator=(const RansDecoder &) = delete; 91 | RansDecoder &operator=(RansDecoder &&) = delete; 92 | 93 | std::vector 94 | decode_with_indexes(const std::string &encoded, 95 | const std::vector &indexes, 96 | const std::vector> &cdfs, 97 | const std::vector &cdfs_sizes, 98 | const std::vector &offsets); 99 | 100 | void set_stream(const std::string &stream); 101 | 102 | std::vector 103 | decode_stream(const std::vector &indexes, 104 | const std::vector> &cdfs, 105 | const std::vector &cdfs_sizes, 106 | const std::vector &offsets); 107 | 108 | 109 | private: 110 | Rans64State _rans; 111 | std::string _stream; 112 | uint32_t *_ptr; 113 | }; 114 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/layers/gdn.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import torch 16 | import torch.nn as nn 17 | import torch.nn.functional as F 18 | 19 | from ..ops.parametrizers import NonNegativeParametrizer 20 | 21 | 22 | class GDN(nn.Module): 23 | r"""Generalized Divisive Normalization layer. 24 | 25 | Introduced in `"Density Modeling of Images Using a Generalized Normalization 26 | Transformation" `_, 27 | by Balle Johannes, Valero Laparra, and Eero P. Simoncelli, (2016). 28 | 29 | .. math:: 30 | 31 | y[i] = \frac{x[i]}{\sqrt{\beta[i] + \sum_j(\gamma[j, i] * x[j]^2)}} 32 | 33 | """ 34 | 35 | def __init__(self, in_channels, inverse=False, beta_min=1e-6, gamma_init=0.1): 36 | super().__init__() 37 | 38 | beta_min = float(beta_min) 39 | gamma_init = float(gamma_init) 40 | self.inverse = bool(inverse) 41 | 42 | self.beta_reparam = NonNegativeParametrizer(minimum=beta_min) 43 | beta = torch.ones(in_channels) 44 | beta = self.beta_reparam.init(beta) 45 | self.beta = nn.Parameter(beta) 46 | 47 | self.gamma_reparam = NonNegativeParametrizer() 48 | gamma = gamma_init * torch.eye(in_channels) 49 | gamma = self.gamma_reparam.init(gamma) 50 | self.gamma = nn.Parameter(gamma) 51 | 52 | def forward(self, x): 53 | _, C, _, _ = x.size() 54 | 55 | beta = self.beta_reparam(self.beta) 56 | gamma = self.gamma_reparam(self.gamma) 57 | gamma = gamma.reshape(C, C, 1, 1) 58 | norm = F.conv2d(x ** 2, gamma, beta) 59 | 60 | if self.inverse: 61 | norm = torch.sqrt(norm) 62 | else: 63 | norm = torch.rsqrt(norm) 64 | 65 | out = x * norm 66 | 67 | return out 68 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/layers/layers.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import torch 16 | import torch.nn as nn 17 | 18 | from .gdn import GDN 19 | 20 | 21 | class MaskedConv2d(nn.Conv2d): 22 | r"""Masked 2D convolution implementation, mask future "unseen" pixels. 23 | Useful for building auto-regressive network components. 24 | 25 | Introduced in `"Conditional Image Generation with PixelCNN Decoders" 26 | `_. 27 | 28 | Inherits the same arguments as a `nn.Conv2d`. Use `mask_type='A'` for the 29 | first layer (which also masks the "current pixel"), `mask_type='B'` for the 30 | following layers. 31 | """ 32 | 33 | def __init__(self, *args, mask_type="A", **kwargs): 34 | super().__init__(*args, **kwargs) 35 | 36 | if mask_type not in ("A", "B"): 37 | raise ValueError(f'Invalid "mask_type" value "{mask_type}"') 38 | 39 | self.register_buffer("mask", torch.ones_like(self.weight.data)) 40 | _, _, h, w = self.mask.size() 41 | self.mask[:, :, h // 2, w // 2 + (mask_type == "B"):] = 0 42 | self.mask[:, :, h // 2 + 1:] = 0 43 | 44 | def forward(self, x): 45 | # TODO(begaintj): weight assigment is not supported by torchscript 46 | self.weight.data *= self.mask 47 | return super().forward(x) 48 | 49 | 50 | def conv3x3(in_ch, out_ch, stride=1): 51 | """3x3 convolution with padding.""" 52 | return nn.Conv2d(in_ch, out_ch, kernel_size=3, stride=stride, padding=1) 53 | 54 | 55 | def subpel_conv3x3(in_ch, out_ch, r=1): 56 | """3x3 sub-pixel convolution for up-sampling.""" 57 | return nn.Sequential( 58 | nn.Conv2d(in_ch, out_ch * r ** 2, kernel_size=3, padding=1), nn.PixelShuffle(r) 59 | ) 60 | 61 | 62 | def conv1x1(in_ch, out_ch, stride=1): 63 | """1x1 convolution.""" 64 | return nn.Conv2d(in_ch, out_ch, kernel_size=1, stride=stride) 65 | 66 | 67 | class ResidualBlockWithStride(nn.Module): 68 | """Residual block with a stride on the first convolution. 69 | 70 | Args: 71 | in_ch (int): number of input channels 72 | out_ch (int): number of output channels 73 | stride (int): stride value (default: 2) 74 | """ 75 | 76 | def __init__(self, in_ch, out_ch, stride=2): 77 | super().__init__() 78 | self.conv1 = conv3x3(in_ch, out_ch, stride=stride) 79 | self.leaky_relu = nn.LeakyReLU(inplace=True) 80 | self.conv2 = conv3x3(out_ch, out_ch) 81 | self.gdn = GDN(out_ch) 82 | if stride != 1: 83 | self.downsample = conv1x1(in_ch, out_ch, stride=stride) 84 | else: 85 | self.downsample = None 86 | 87 | def forward(self, x): 88 | identity = x 89 | out = self.conv1(x) 90 | out = self.leaky_relu(out) 91 | out = self.conv2(out) 92 | out = self.gdn(out) 93 | 94 | if self.downsample is not None: 95 | identity = self.downsample(x) 96 | 97 | out += identity 98 | return out 99 | 100 | 101 | class ResidualBlockUpsample(nn.Module): 102 | """Residual block with sub-pixel upsampling on the last convolution. 103 | 104 | Args: 105 | in_ch (int): number of input channels 106 | out_ch (int): number of output channels 107 | upsample (int): upsampling factor (default: 2) 108 | """ 109 | 110 | def __init__(self, in_ch, out_ch, upsample=2): 111 | super().__init__() 112 | self.subpel_conv = subpel_conv3x3(in_ch, out_ch, upsample) 113 | self.leaky_relu = nn.LeakyReLU(inplace=True) 114 | self.conv = conv3x3(out_ch, out_ch) 115 | self.igdn = GDN(out_ch, inverse=True) 116 | self.upsample = subpel_conv3x3(in_ch, out_ch, upsample) 117 | 118 | def forward(self, x): 119 | identity = x 120 | out = self.subpel_conv(x) 121 | out = self.leaky_relu(out) 122 | out = self.conv(out) 123 | out = self.igdn(out) 124 | identity = self.upsample(x) 125 | out += identity 126 | return out 127 | 128 | 129 | class ResidualBlock(nn.Module): 130 | """Simple residual block with two 3x3 convolutions. 131 | 132 | Args: 133 | in_ch (int): number of input channels 134 | out_ch (int): number of output channels 135 | """ 136 | 137 | def __init__(self, in_ch, out_ch): 138 | super().__init__() 139 | self.conv1 = conv3x3(in_ch, out_ch) 140 | self.leaky_relu = nn.LeakyReLU(inplace=True) 141 | self.conv2 = conv3x3(out_ch, out_ch) 142 | 143 | def forward(self, x): 144 | identity = x 145 | 146 | out = self.conv1(x) 147 | out = self.leaky_relu(out) 148 | out = self.conv2(out) 149 | out = self.leaky_relu(out) 150 | 151 | out = out + identity 152 | return out -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/models/utils.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import torch 16 | import torch.nn as nn 17 | 18 | 19 | def find_named_module(module, query): 20 | """Helper function to find a named module. Returns a `nn.Module` or `None` 21 | 22 | Args: 23 | module (nn.Module): the root module 24 | query (str): the module name to find 25 | 26 | Returns: 27 | nn.Module or None 28 | """ 29 | 30 | return next((m for n, m in module.named_modules() if n == query), None) 31 | 32 | 33 | def find_named_buffer(module, query): 34 | """Helper function to find a named buffer. Returns a `torch.Tensor` or `None` 35 | 36 | Args: 37 | module (nn.Module): the root module 38 | query (str): the buffer name to find 39 | 40 | Returns: 41 | torch.Tensor or None 42 | """ 43 | return next((b for n, b in module.named_buffers() if n == query), None) 44 | 45 | 46 | def _update_registered_buffer( 47 | module, 48 | buffer_name, 49 | state_dict_key, 50 | state_dict, 51 | policy="resize_if_empty", 52 | dtype=torch.int, 53 | ): 54 | new_size = state_dict[state_dict_key].size() 55 | registered_buf = find_named_buffer(module, buffer_name) 56 | 57 | if policy in ("resize_if_empty", "resize"): 58 | if registered_buf is None: 59 | raise RuntimeError(f'buffer "{buffer_name}" was not registered') 60 | 61 | if policy == "resize" or registered_buf.numel() == 0: 62 | registered_buf.resize_(new_size) 63 | 64 | elif policy == "register": 65 | if registered_buf is not None: 66 | raise RuntimeError(f'buffer "{buffer_name}" was already registered') 67 | 68 | module.register_buffer(buffer_name, torch.empty(new_size, dtype=dtype).fill_(0)) 69 | 70 | else: 71 | raise ValueError(f'Invalid policy "{policy}"') 72 | 73 | 74 | def update_registered_buffers( 75 | module, 76 | module_name, 77 | buffer_names, 78 | state_dict, 79 | policy="resize_if_empty", 80 | dtype=torch.int, 81 | ): 82 | """Update the registered buffers in a module according to the tensors sized 83 | in a state_dict. 84 | 85 | (There's no way in torch to directly load a buffer with a dynamic size) 86 | 87 | Args: 88 | module (nn.Module): the module 89 | module_name (str): module name in the state dict 90 | buffer_names (list(str)): list of the buffer names to resize in the module 91 | state_dict (dict): the state dict 92 | policy (str): Update policy, choose from 93 | ('resize_if_empty', 'resize', 'register') 94 | dtype (dtype): Type of buffer to be registered (when policy is 'register') 95 | """ 96 | valid_buffer_names = [n for n, _ in module.named_buffers()] 97 | for buffer_name in buffer_names: 98 | if buffer_name not in valid_buffer_names: 99 | raise ValueError(f'Invalid buffer name "{buffer_name}"') 100 | 101 | for buffer_name in buffer_names: 102 | _update_registered_buffer( 103 | module, 104 | buffer_name, 105 | f"{module_name}.{buffer_name}", 106 | state_dict, 107 | policy, 108 | dtype, 109 | ) 110 | 111 | 112 | def conv(in_channels, out_channels, kernel_size=5, stride=2): 113 | return nn.Conv2d( 114 | in_channels, 115 | out_channels, 116 | kernel_size=kernel_size, 117 | stride=stride, 118 | padding=kernel_size // 2, 119 | ) 120 | 121 | 122 | def deconv(in_channels, out_channels, kernel_size=5, stride=2): 123 | return nn.ConvTranspose2d( 124 | in_channels, 125 | out_channels, 126 | kernel_size=kernel_size, 127 | stride=stride, 128 | output_padding=stride - 1, 129 | padding=kernel_size // 2, 130 | ) 131 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/models/waseda.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import torch.nn as nn 16 | 17 | from ..layers.layers import ( 18 | ResidualBlock, 19 | ResidualBlockUpsample, 20 | ResidualBlockWithStride, 21 | conv3x3, 22 | subpel_conv3x3, 23 | ) 24 | 25 | from .priors import JointAutoregressiveHierarchicalPriors 26 | 27 | 28 | class Cheng2020Anchor(JointAutoregressiveHierarchicalPriors): 29 | """Anchor model variant from `"Learned Image Compression with 30 | Discretized Gaussian Mixture Likelihoods and Attention Modules" 31 | `_, by Zhengxue Cheng, Heming Sun, Masaru 32 | Takeuchi, Jiro Katto. 33 | 34 | Uses residual blocks with small convolutions (3x3 and 1x1), and sub-pixel 35 | convolutions for up-sampling. 36 | 37 | Args: 38 | N (int): Number of channels 39 | """ 40 | 41 | def __init__(self, N=192, **kwargs): 42 | super().__init__(N=N, M=N, **kwargs) 43 | 44 | self.g_a = nn.Sequential( 45 | ResidualBlockWithStride(3, N, stride=2), 46 | ResidualBlock(N, N), 47 | ResidualBlockWithStride(N, N, stride=2), 48 | ResidualBlock(N, N), 49 | ResidualBlockWithStride(N, N, stride=2), 50 | ResidualBlock(N, N), 51 | conv3x3(N, N, stride=2), 52 | ) 53 | 54 | self.h_a = nn.Sequential( 55 | conv3x3(N, N), 56 | nn.LeakyReLU(inplace=True), 57 | conv3x3(N, N), 58 | nn.LeakyReLU(inplace=True), 59 | conv3x3(N, N, stride=2), 60 | nn.LeakyReLU(inplace=True), 61 | conv3x3(N, N), 62 | nn.LeakyReLU(inplace=True), 63 | conv3x3(N, N, stride=2), 64 | ) 65 | 66 | self.h_s = nn.Sequential( 67 | conv3x3(N, N), 68 | nn.LeakyReLU(inplace=True), 69 | subpel_conv3x3(N, N, 2), 70 | nn.LeakyReLU(inplace=True), 71 | conv3x3(N, N * 3 // 2), 72 | nn.LeakyReLU(inplace=True), 73 | subpel_conv3x3(N * 3 // 2, N * 3 // 2, 2), 74 | nn.LeakyReLU(inplace=True), 75 | conv3x3(N * 3 // 2, N * 2), 76 | ) 77 | 78 | self.g_s = nn.Sequential( 79 | ResidualBlock(N, N), 80 | ResidualBlockUpsample(N, N, 2), 81 | ResidualBlock(N, N), 82 | ResidualBlockUpsample(N, N, 2), 83 | ResidualBlock(N, N), 84 | ResidualBlockUpsample(N, N, 2), 85 | ResidualBlock(N, N), 86 | subpel_conv3x3(N, 3, 2), 87 | ) 88 | 89 | @classmethod 90 | def from_state_dict(cls, state_dict): 91 | """Return a new model instance from `state_dict`.""" 92 | N = state_dict["g_a.0.conv1.weight"].size(0) 93 | net = cls(N) 94 | net.load_state_dict(state_dict) 95 | return net -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/ops/bound_ops.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import torch 16 | import torch.nn as nn 17 | 18 | 19 | class LowerBoundFunction(torch.autograd.Function): 20 | """Autograd function for the `LowerBound` operator.""" 21 | 22 | @staticmethod 23 | def forward(ctx, input_, bound): 24 | ctx.save_for_backward(input_, bound) 25 | return torch.max(input_, bound) 26 | 27 | @staticmethod 28 | def backward(ctx, grad_output): 29 | input_, bound = ctx.saved_tensors 30 | pass_through_if = (input_ >= bound) | (grad_output < 0) 31 | return pass_through_if.type(grad_output.dtype) * grad_output, None 32 | 33 | 34 | class LowerBound(nn.Module): 35 | """Lower bound operator, computes `torch.max(x, bound)` with a custom 36 | gradient. 37 | 38 | The derivative is replaced by the identity function when `x` is moved 39 | towards the `bound`, otherwise the gradient is kept to zero. 40 | """ 41 | 42 | def __init__(self, bound): 43 | super().__init__() 44 | self.register_buffer("bound", torch.Tensor([float(bound)])) 45 | 46 | @torch.jit.unused 47 | def lower_bound(self, x): 48 | return LowerBoundFunction.apply(x, self.bound) 49 | 50 | def forward(self, x): 51 | if torch.jit.is_scripting(): 52 | return torch.max(x, self.bound) 53 | return self.lower_bound(x) 54 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/ops/parametrizers.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import torch 16 | import torch.nn as nn 17 | 18 | from .bound_ops import LowerBound 19 | 20 | 21 | class NonNegativeParametrizer(nn.Module): 22 | """ 23 | Non negative reparametrization. 24 | 25 | Used for stability during training. 26 | """ 27 | 28 | def __init__(self, minimum=0, reparam_offset=2 ** -18): 29 | super().__init__() 30 | 31 | self.minimum = float(minimum) 32 | self.reparam_offset = float(reparam_offset) 33 | 34 | pedestal = self.reparam_offset ** 2 35 | self.register_buffer("pedestal", torch.Tensor([pedestal])) 36 | bound = (self.minimum + self.reparam_offset ** 2) ** 0.5 37 | self.lower_bound = LowerBound(bound) 38 | 39 | def init(self, x): 40 | return torch.sqrt(torch.max(x + self.pedestal, self.pedestal)) 41 | 42 | def forward(self, x): 43 | out = self.lower_bound(x) 44 | out = out ** 2 - self.pedestal 45 | return out 46 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/utils/Dataset.py: -------------------------------------------------------------------------------- 1 | import os 2 | import h5py 3 | import numpy as np 4 | import torch 5 | from torch.utils.data.dataset import Dataset, ConcatDataset 6 | 7 | 8 | class VimeoDataset(Dataset): 9 | def __init__(self, h5_file): 10 | super(VimeoDataset, self).__init__() 11 | self.h5 = h5py.File(name=h5_file, mode='r') 12 | self.file_list = list(self.h5.keys()) 13 | 14 | def __len__(self): 15 | return len(self.file_list) 16 | 17 | def __getitem__(self, idx): # 负责按索引取出某个数据,并对该数据做预处理 18 | frames = self.h5[self.file_list[idx]] 19 | frames = np.array(frames) 20 | frames = torch.from_numpy(frames) / 255.0 # scale to [0, 1] 21 | frames = frames.permute(0, 3, 1, 2).contiguous().float() 22 | return frames 23 | 24 | 25 | def is_h5(h5): 26 | return any(h5.endswith(extension) for extension in ['.hdf5', ]) 27 | 28 | 29 | def create_dataset(h5_folder): 30 | datasets = [VimeoDataset(os.path.join(h5_folder, h5)) for h5 in os.listdir(h5_folder) if is_h5(h5)] 31 | return ConcatDataset(datasets=datasets) 32 | 33 | # 34 | # tester = VimeoDataset('../Dataset/Train_1024_part_9.hdf5') 35 | # from PIL import Image 36 | # 37 | # aa = tester[2] 38 | # 39 | # for i in range(7): 40 | # a = aa[i] 41 | # 42 | # a = a.permute(1, 2, 0).contiguous() 43 | # a = a.numpy() * 255 44 | # p = a.astype(np.uint8) 45 | # p = Image.fromarray(p) 46 | # p.show() 47 | # input() 48 | # 49 | # tester = create_dataset('../Dataset/Eval') 50 | # print(len(tester)) 51 | # from PIL import Image 52 | # 53 | # aa = tester[90] 54 | # 55 | # for i in range(7): 56 | # a = aa[i] 57 | # 58 | # a = a.permute(1, 2, 0).contiguous() 59 | # a = a.numpy() * 255 60 | # p = a.astype(np.uint8) 61 | # p = Image.fromarray(p) 62 | # p.show() 63 | # input() 64 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/utils/common.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | 4 | def str2bool(v): 5 | if isinstance(v, bool): 6 | return v 7 | if v.lower() in ('yes', 'true', 't', 'y', '1'): 8 | return True 9 | elif v.lower() in ('no', 'false', 'f', 'n', '0'): 10 | return False 11 | else: 12 | raise argparse.ArgumentTypeError('Boolean value expected.') 13 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/utils/stream_helper.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import struct 16 | from pathlib import Path 17 | import torch 18 | import torch.nn.functional as F 19 | from PIL import Image 20 | from torchvision.transforms import ToPILImage, ToTensor 21 | 22 | 23 | def get_downsampled_shape(height, width, p): 24 | 25 | new_h = (height + p - 1) // p * p 26 | new_w = (width + p - 1) // p * p 27 | return int(new_h / p + 0.5), int(new_w / p + 0.5) 28 | 29 | 30 | def filesize(filepath: str) -> int: 31 | if not Path(filepath).is_file(): 32 | raise ValueError(f'Invalid file "{filepath}".') 33 | return Path(filepath).stat().st_size 34 | 35 | 36 | def load_image(filepath: str) -> Image.Image: 37 | return Image.open(filepath).convert("RGB") 38 | 39 | 40 | def img2torch(img: Image.Image) -> torch.Tensor: 41 | return ToTensor()(img).unsqueeze(0) 42 | 43 | 44 | def torch2img(x: torch.Tensor) -> Image.Image: 45 | return ToPILImage()(x.clamp_(0, 1).squeeze()) 46 | 47 | 48 | def write_uints(fd, values, fmt=">{:d}I"): 49 | fd.write(struct.pack(fmt.format(len(values)), *values)) 50 | 51 | 52 | def write_uchars(fd, values, fmt=">{:d}B"): 53 | fd.write(struct.pack(fmt.format(len(values)), *values)) 54 | 55 | 56 | def read_uints(fd, n, fmt=">{:d}I"): 57 | sz = struct.calcsize("I") 58 | return struct.unpack(fmt.format(n), fd.read(n * sz)) 59 | 60 | 61 | def read_uchars(fd, n, fmt=">{:d}B"): 62 | sz = struct.calcsize("B") 63 | return struct.unpack(fmt.format(n), fd.read(n * sz)) 64 | 65 | 66 | def write_bytes(fd, values, fmt=">{:d}s"): 67 | if len(values) == 0: 68 | return 69 | fd.write(struct.pack(fmt.format(len(values)), values)) 70 | 71 | 72 | def read_bytes(fd, n, fmt=">{:d}s"): 73 | sz = struct.calcsize("s") 74 | return struct.unpack(fmt.format(n), fd.read(n * sz))[0] 75 | 76 | 77 | def pad(x, p=2 ** 6): 78 | h, w = x.size(2), x.size(3) 79 | H = (h + p - 1) // p * p 80 | W = (w + p - 1) // p * p 81 | padding_left = (W - w) // 2 82 | padding_right = W - w - padding_left 83 | padding_top = (H - h) // 2 84 | padding_bottom = H - h - padding_top 85 | return F.pad( 86 | x, 87 | (padding_left, padding_right, padding_top, padding_bottom), 88 | mode="constant", 89 | value=0, 90 | ) 91 | 92 | 93 | def crop(x, size): 94 | H, W = x.size(2), x.size(3) 95 | h, w = size 96 | padding_left = (W - w) // 2 97 | padding_right = W - w - padding_left 98 | padding_top = (H - h) // 2 99 | padding_bottom = H - h - padding_top 100 | return F.pad( 101 | x, 102 | (-padding_left, -padding_right, -padding_top, -padding_bottom), 103 | mode="constant", 104 | value=0, 105 | ) 106 | 107 | 108 | def encode_i(height, width, y_string, z_string, output): 109 | with Path(output).open("wb") as f: 110 | y_string_length = len(y_string) 111 | z_string_length = len(z_string) 112 | 113 | write_uints(f, (height, width, y_string_length, z_string_length)) 114 | write_bytes(f, y_string) 115 | write_bytes(f, z_string) 116 | 117 | 118 | def decode_i(inputpath): 119 | with Path(inputpath).open("rb") as f: 120 | header = read_uints(f, 4) 121 | height = header[0] 122 | width = header[1] 123 | y_string_length = header[2] 124 | z_string_length = header[3] 125 | 126 | y_string = read_bytes(f, y_string_length) 127 | z_string = read_bytes(f, z_string_length) 128 | 129 | return height, width, y_string, z_string 130 | 131 | 132 | def encode_p(height, width, mv_y_string, mv_z_string, y_string, z_string, output): 133 | with Path(output).open("wb") as f: 134 | mv_y_string_length = len(mv_y_string) 135 | mv_z_string_length = len(mv_z_string) 136 | y_string_length = len(y_string) 137 | z_string_length = len(z_string) 138 | 139 | write_uints(f, (height, width, 140 | mv_y_string_length, mv_z_string_length, 141 | y_string_length, z_string_length)) 142 | write_bytes(f, mv_y_string) 143 | write_bytes(f, mv_z_string) 144 | write_bytes(f, y_string) 145 | write_bytes(f, z_string) 146 | 147 | 148 | def decode_p(inputpath): 149 | with Path(inputpath).open("rb") as f: 150 | header = read_uints(f, 6) 151 | height = header[0] 152 | width = header[1] 153 | mv_y_string_length = header[2] 154 | mv_z_string_length = header[3] 155 | y_string_length = header[4] 156 | z_string_length = header[5] 157 | 158 | mv_y_string = read_bytes(f, mv_y_string_length) 159 | mv_z_string = read_bytes(f, mv_z_string_length) 160 | y_string = read_bytes(f, y_string_length) 161 | z_string = read_bytes(f, z_string_length) 162 | 163 | return height, width, mv_y_string, mv_z_string, y_string, z_string 164 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DCVC/src/zoo/image.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from ..models.waseda import ( 16 | Cheng2020Anchor 17 | ) 18 | 19 | from ..models.priors import ( 20 | FactorizedPrior, 21 | ScaleHyperprior, 22 | MeanScaleHyperprior, 23 | JointAutoregressiveHierarchicalPriors 24 | ) 25 | 26 | model_architectures = { 27 | "bmshj2018-factorized": FactorizedPrior, 28 | "bmshj2018-hyperprior": ScaleHyperprior, 29 | "mbt2018-mean": MeanScaleHyperprior, 30 | "mbt2018": JointAutoregressiveHierarchicalPriors, 31 | "cheng2020-anchor": Cheng2020Anchor, 32 | } 33 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/__init__.py -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-1-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-1-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-1-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-1-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-2-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-2-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-2-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-2-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-3-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-3-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-3-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-3-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-4-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-4-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-4-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-4-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-5-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-5-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-5-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL1_F-5-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-1-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-1-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-1-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-1-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-2-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-2-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-2-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-2-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-3-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-3-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-3-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-3-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-4-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-4-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-4-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-4-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-5-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-5-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-5-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL2_F-5-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-1-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-1-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-1-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-1-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-2-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-2-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-2-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-2-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-3-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-3-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-3-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-3-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-4-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-4-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-4-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-4-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-5-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-5-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-5-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL3_F-5-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-1-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-1-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-1-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-1-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-2-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-2-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-2-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-2-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-3-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-3-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-3-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-3-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-4-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-4-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-4-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-4-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-5-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-5-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-5-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL4_F-5-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-1-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-1-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-1-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-1-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-2-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-2-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-2-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-2-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-3-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-3-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-3-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-3-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-4-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-4-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-4-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-4-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-5-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-5-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-5-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL5_F-5-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-1-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-1-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-1-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-1-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-2-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-2-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-2-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-2-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-3-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-3-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-3-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-3-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-4-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-4-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-4-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-4-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-5-bias.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-5-bias.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-5-weight.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/DVC/examples/flow_pretrain_np/modelL6_F-5-weight.npy -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/subnet/GDN.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.utils.data 3 | from torch import nn, optim 4 | from torch.nn import functional as F 5 | from torchvision.utils import save_image 6 | from torch.autograd import Function 7 | 8 | 9 | class LowerBound(Function): 10 | @staticmethod 11 | def forward(ctx, inputs, bound): 12 | b = torch.ones_like(inputs) * bound 13 | ctx.save_for_backward(inputs, b) 14 | return torch.max(inputs, b) 15 | 16 | @staticmethod 17 | def backward(ctx, grad_output): 18 | inputs, b = ctx.saved_tensors 19 | pass_through_1 = inputs >= b 20 | pass_through_2 = grad_output < 0 21 | 22 | pass_through = pass_through_1 | pass_through_2 23 | return pass_through.type(grad_output.dtype) * grad_output, None 24 | 25 | 26 | class GDN(nn.Module): 27 | """Generalized divisive normalization layer. 28 | y[i] = x[i] / sqrt(beta[i] + sum_j(gamma[j, i] * x[j])) 29 | """ 30 | 31 | def __init__(self, 32 | ch, 33 | inverse=False, 34 | beta_min=1e-6, 35 | gamma_init=0.1, 36 | reparam_offset=2**-18): 37 | super(GDN, self).__init__() 38 | self.inverse = inverse 39 | self.beta_min = beta_min 40 | self.gamma_init = gamma_init 41 | self.reparam_offset = reparam_offset 42 | 43 | self.build(ch) 44 | 45 | def build(self, ch): 46 | self.pedestal = self.reparam_offset**2 47 | self.beta_bound = ((self.beta_min + self.reparam_offset**2)**0.5) 48 | self.gamma_bound = self.reparam_offset 49 | 50 | # Create beta param 51 | beta = torch.sqrt(torch.ones(ch)+self.pedestal) 52 | self.beta = nn.Parameter(beta) 53 | 54 | # Create gamma param 55 | eye = torch.eye(ch) 56 | g = self.gamma_init*eye 57 | g = g + self.pedestal 58 | gamma = torch.sqrt(g) 59 | 60 | self.gamma = nn.Parameter(gamma) 61 | self.pedestal = self.pedestal 62 | 63 | def forward(self, inputs): 64 | unfold = False 65 | if inputs.dim() == 5: 66 | unfold = True 67 | bs, ch, d, w, h = inputs.size() 68 | inputs = inputs.view(bs, ch, d*w, h) 69 | 70 | _, ch, _, _ = inputs.size() 71 | 72 | # Beta bound and reparam 73 | beta = LowerBound.apply(self.beta, self.beta_bound) 74 | beta = beta**2 - self.pedestal 75 | 76 | # Gamma bound and reparam 77 | gamma = LowerBound.apply(self.gamma, self.gamma_bound) 78 | gamma = gamma**2 - self.pedestal 79 | gamma = gamma.view(ch, ch, 1, 1) 80 | 81 | # Norm pool calc 82 | norm_ = nn.functional.conv2d(inputs**2, gamma, beta) 83 | norm_ = torch.sqrt(norm_) 84 | 85 | # Apply norm 86 | if self.inverse: 87 | outputs = inputs * norm_ 88 | else: 89 | outputs = inputs / norm_ 90 | 91 | if unfold: 92 | outputs = outputs.view(bs, ch, d, w, h) 93 | return outputs 94 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/subnet/__init__.py: -------------------------------------------------------------------------------- 1 | from .GDN import GDN 2 | from .analysis import Analysis_net 3 | from .analysis_mv import Analysis_mv_net 4 | from .analysis_prior import Analysis_prior_net 5 | from .synthesis import Synthesis_net 6 | from .synthesis_mv import Synthesis_mv_net 7 | from .synthesis_prior import Synthesis_prior_net 8 | from .endecoder import ME_Spynet, flow_warp, Warp_net 9 | from .bitEstimator import BitEstimator 10 | from .basics import * 11 | from .ms_ssim_torch import ms_ssim, ssim 12 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/subnet/analysis.py: -------------------------------------------------------------------------------- 1 | #!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 2 | from .basics import * 3 | import pickle 4 | import os 5 | import codecs 6 | 7 | # gdn = tf.contrib.layers.gdn 8 | 9 | 10 | class Analysis_net(nn.Module): 11 | ''' 12 | Compress residual 13 | ''' 14 | def __init__(self): 15 | super(Analysis_net, self).__init__() 16 | self.conv1 = nn.Conv2d(3, out_channel_N, 5, stride=2, padding=2) 17 | torch.nn.init.xavier_normal_(self.conv1.weight.data, (math.sqrt(2 * (3 + out_channel_N) / (6)))) 18 | torch.nn.init.constant_(self.conv1.bias.data, 0.01) 19 | self.gdn1 = GDN(out_channel_N) 20 | self.conv2 = nn.Conv2d(out_channel_N, out_channel_N, 5, stride=2, padding=2) 21 | torch.nn.init.xavier_normal_(self.conv2.weight.data, math.sqrt(2)) 22 | torch.nn.init.constant_(self.conv2.bias.data, 0.01) 23 | self.gdn2 = GDN(out_channel_N) 24 | self.conv3 = nn.Conv2d(out_channel_N, out_channel_N, 5, stride=2, padding=2) 25 | torch.nn.init.xavier_normal_(self.conv3.weight.data, math.sqrt(2)) 26 | torch.nn.init.constant_(self.conv3.bias.data, 0.01) 27 | self.gdn3 = GDN(out_channel_N) 28 | self.conv4 = nn.Conv2d(out_channel_N, out_channel_M, 5, stride=2, padding=2) 29 | torch.nn.init.xavier_normal_(self.conv4.weight.data, (math.sqrt(2 * (out_channel_M + out_channel_N) / (out_channel_N + out_channel_N)))) 30 | torch.nn.init.constant_(self.conv4.bias.data, 0.01) 31 | # self.resEncoder = nn.Sequential( 32 | # nn.Conv2d(3, out_channel_N, 5, stride=2, padding=2),# how to initialize ??? 33 | # GDN(out_channel_N), 34 | # nn.Conv2d(out_channel_N, out_channel_N, 5, stride=2, padding=2),# how to initialize ??? 35 | # GDN(out_channel_N), 36 | # nn.Conv2d(out_channel_N, out_channel_N, 5, stride=2, padding=2),# how to initialize ??? 37 | # GDN(out_channel_N), 38 | # nn.Conv2d(out_channel_N, out_channel_M, 5, stride=2, padding=2),# how to initialize ??? 39 | # ) 40 | 41 | def forward(self, x): 42 | x = self.gdn1(self.conv1(x)) 43 | x = self.gdn2(self.conv2(x)) 44 | x = self.gdn3(self.conv3(x)) 45 | return self.conv4(x) 46 | 47 | 48 | def build_model(): 49 | input_image = Variable(torch.zeros([4, 3, 256, 256])) 50 | 51 | analysis_net = Analysis_net() 52 | feature = analysis_net(input_image) 53 | 54 | print(feature.size()) 55 | # feature = sess.run(weights) 56 | 57 | # print(weights_val) 58 | 59 | # gamma_val = sess.run(gamma) 60 | 61 | # print(gamma_val) 62 | 63 | 64 | if __name__ == '__main__': 65 | build_model() 66 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/subnet/analysis_mv.py: -------------------------------------------------------------------------------- 1 | from .basics import * 2 | # import pickle 3 | # import os 4 | # import codecs 5 | from .analysis import Analysis_net 6 | 7 | 8 | class Analysis_mv_net(nn.Module): 9 | ''' 10 | Compress motion 11 | ''' 12 | def __init__(self): 13 | super(Analysis_mv_net, self).__init__() 14 | self.conv1 = nn.Conv2d(2, out_channel_mv, 3, stride=2, padding=1) 15 | torch.nn.init.xavier_normal_(self.conv1.weight.data, (math.sqrt(2 * (2 + out_channel_mv) / (4)))) 16 | torch.nn.init.constant_(self.conv1.bias.data, 0.01) 17 | self.relu1 = nn.LeakyReLU(negative_slope=0.1) 18 | self.conv2 = nn.Conv2d(out_channel_mv, out_channel_mv, 3, stride=1, padding=1) 19 | torch.nn.init.xavier_normal_(self.conv2.weight.data, math.sqrt(2)) 20 | torch.nn.init.constant_(self.conv2.bias.data, 0.01) 21 | self.relu2 = nn.LeakyReLU(negative_slope=0.1) 22 | self.conv3 = nn.Conv2d(out_channel_mv, out_channel_mv, 3, stride=2, padding=1) 23 | torch.nn.init.xavier_normal_(self.conv3.weight.data, math.sqrt(2)) 24 | torch.nn.init.constant_(self.conv3.bias.data, 0.01) 25 | self.relu3 = nn.LeakyReLU(negative_slope=0.1) 26 | self.conv4 = nn.Conv2d(out_channel_mv, out_channel_mv, 3, stride=1, padding=1) 27 | torch.nn.init.xavier_normal_(self.conv4.weight.data, math.sqrt(2)) 28 | torch.nn.init.constant_(self.conv4.bias.data, 0.01) 29 | self.relu4 = nn.LeakyReLU(negative_slope=0.1) 30 | self.conv5 = nn.Conv2d(out_channel_mv, out_channel_mv, 3, stride=2, padding=1) 31 | torch.nn.init.xavier_normal_(self.conv5.weight.data, math.sqrt(2)) 32 | torch.nn.init.constant_(self.conv5.bias.data, 0.01) 33 | self.relu5 = nn.LeakyReLU(negative_slope=0.1) 34 | self.conv6 = nn.Conv2d(out_channel_mv, out_channel_mv, 3, stride=1, padding=1) 35 | torch.nn.init.xavier_normal_(self.conv6.weight.data, math.sqrt(2)) 36 | torch.nn.init.constant_(self.conv6.bias.data, 0.01) 37 | self.relu6 = nn.LeakyReLU(negative_slope=0.1) 38 | self.conv7 = nn.Conv2d(out_channel_mv, out_channel_mv, 3, stride=2, padding=1) 39 | torch.nn.init.xavier_normal_(self.conv7.weight.data, math.sqrt(2)) 40 | torch.nn.init.constant_(self.conv7.bias.data, 0.01) 41 | self.relu7 = nn.LeakyReLU(negative_slope=0.1) 42 | self.conv8 = nn.Conv2d(out_channel_mv, out_channel_mv, 3, stride=1, padding=1) 43 | torch.nn.init.xavier_normal_(self.conv8.weight.data, math.sqrt(2)) 44 | torch.nn.init.constant_(self.conv8.bias.data, 0.01) 45 | 46 | def forward(self, x): 47 | x = self.relu1(self.conv1(x)) 48 | x = self.relu2(self.conv2(x)) 49 | x = self.relu3(self.conv3(x)) 50 | x = self.relu4(self.conv4(x)) 51 | x = self.relu5(self.conv5(x)) 52 | x = self.relu6(self.conv6(x)) 53 | x = self.relu7(self.conv7(x)) 54 | return self.conv8(x) 55 | 56 | def build_model(): 57 | analysis_net = Analysis_net() 58 | analysis_mv_net = Analysis_mv_net() 59 | 60 | feature = torch.zeros([3, 2, 256, 256]) 61 | z = analysis_mv_net(feature) 62 | print("feature : ", feature.size()) 63 | print("z : ", z.size()) 64 | 65 | if __name__ == '__main__': 66 | build_model() -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/subnet/analysis_prior.py: -------------------------------------------------------------------------------- 1 | #!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 2 | from .basics import * 3 | # import pickle 4 | # import os 5 | # import codecs 6 | from .analysis import Analysis_net 7 | 8 | 9 | 10 | class Analysis_prior_net(nn.Module): 11 | ''' 12 | Compress residual prior 13 | ''' 14 | def __init__(self): 15 | super(Analysis_prior_net, self).__init__() 16 | self.conv1 = nn.Conv2d(out_channel_M, out_channel_N, 3, stride=1, padding=1) 17 | torch.nn.init.xavier_normal_(self.conv1.weight.data, (math.sqrt(2 * (out_channel_M + out_channel_N) / (out_channel_M + out_channel_M)))) 18 | torch.nn.init.constant_(self.conv1.bias.data, 0.01) 19 | self.relu1 = nn.ReLU() 20 | self.conv2 = nn.Conv2d(out_channel_N, out_channel_N, 5, stride=2, padding=2) 21 | torch.nn.init.xavier_normal_(self.conv2.weight.data, math.sqrt(2)) 22 | torch.nn.init.constant_(self.conv2.bias.data, 0.01) 23 | self.relu2 = nn.ReLU() 24 | self.conv3 = nn.Conv2d(out_channel_N, out_channel_N, 5, stride=2, padding=2) 25 | torch.nn.init.xavier_normal_(self.conv3.weight.data, math.sqrt(2)) 26 | torch.nn.init.constant_(self.conv3.bias.data, 0.01) 27 | # self.priorencoder = nn.Sequential( 28 | # nn.Conv2d(out_channel_M, out_channel_N, 3, stride=1, padding=1), 29 | # nn.ReLU(), 30 | # nn.Conv2d(out_channel_N, out_channel_N, 5, stride=2, padding=2), 31 | # nn.ReLU(), 32 | # nn.Conv2d(out_channel_N, out_channel_N, 5, stride=2, padding=2) 33 | # ) 34 | 35 | 36 | def forward(self, x): 37 | x = torch.abs(x) 38 | x = self.relu1(self.conv1(x)) 39 | x = self.relu2(self.conv2(x)) 40 | return self.conv3(x) 41 | 42 | 43 | def build_model(): 44 | input_image = torch.zeros([5, 3, 256, 256]) 45 | analysis_net = Analysis_net() 46 | analysis_prior_net = Analysis_prior_net() 47 | 48 | feature = analysis_net(input_image) 49 | z = analysis_prior_net(feature) 50 | 51 | print(input_image.size()) 52 | print(feature.size()) 53 | print(z.size()) 54 | 55 | 56 | 57 | if __name__ == '__main__': 58 | build_model() 59 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/subnet/basics.py: -------------------------------------------------------------------------------- 1 | #!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 2 | from __future__ import absolute_import 3 | from __future__ import division 4 | from __future__ import print_function 5 | from datetime import datetime 6 | import math 7 | import time 8 | # from .resample2d_package.resample2d import Resample2d 9 | from six.moves import xrange 10 | # import tensorflow as tf 11 | import numpy as np 12 | import torch.nn as nn 13 | import torch 14 | import torch.nn.functional as F 15 | import torchvision 16 | from .GDN import GDN 17 | from torch.autograd import Variable 18 | import imageio 19 | import datetime 20 | from .flowlib import flow_to_image 21 | 22 | out_channel_N = 64 23 | out_channel_M = 96 24 | # out_channel_N = 128 25 | # out_channel_M = 192 26 | out_channel_mv = 128 27 | 28 | 29 | def print_activations(t): 30 | print(t.op.name, ' ', t.get_shape().as_list()) 31 | 32 | 33 | # def leaky_relu(x, alpha=0.1): 34 | # # return tf.nn.tanh(x, name='tanh') 35 | # return tf.maximum(alpha * x, x, name='leaky_relu') 36 | 37 | 38 | def tensorimwrite(image, name='im'): 39 | # means = np.array([0.485, 0.456, 0.406]) 40 | # stds = np.array([0.229, 0.224, 0.225]) 41 | if len(image.size()) == 4: 42 | image = image[0] 43 | image = image.detach().cpu().numpy().transpose(1, 2, 0) 44 | image = image * 255 45 | imageio.imwrite(name + ".png", image.astype(np.uint8)) 46 | 47 | def relu(x): 48 | return x 49 | 50 | 51 | # def binary(x): 52 | # return tf.nn.sigmoid(x * 2) 53 | # # return tf.minimum(tf.round(tf.nn.sigmoid(x)) + 0.00000001 , 1.0 - ) 54 | # # return tf.nn.sigmoid(x,name='relu') 55 | 56 | 57 | # from tensorflow.contrib.layers.python.layers import batch_norm as batch_norm 58 | 59 | 60 | def yuv_import_444(filename, dims, numfrm, startfrm): 61 | fp = open(filename, 'rb') 62 | # fp=open(filename,'rb') 63 | 64 | blk_size = int(dims[0] * dims[1] * 3) 65 | fp.seek(blk_size * startfrm, 0) 66 | Y = [] 67 | U = [] 68 | V = [] 69 | # print(dims[0]) 70 | # print(dims[1]) 71 | d00 = dims[0] 72 | d01 = dims[1] 73 | # print(d00) 74 | # print(d01) 75 | Yt = np.zeros((dims[0], dims[1]), np.int, 'C') 76 | Ut = np.zeros((d00, d01), np.int, 'C') 77 | Vt = np.zeros((d00, d01), np.int, 'C') 78 | print(dims[0]) 79 | YUV = np.zeros((dims[0], dims[1], 3)) 80 | 81 | for m in range(dims[0]): 82 | for n in range(dims[1]): 83 | Yt[m, n] = ord(fp.read(1)) 84 | for m in range(d00): 85 | for n in range(d01): 86 | Ut[m, n] = ord(fp.read(1)) 87 | for m in range(d00): 88 | for n in range(d01): 89 | Vt[m, n] = ord(fp.read(1)) 90 | 91 | YUV[:, :, 0] = Yt 92 | YUV[:, :, 1] = Ut 93 | YUV[:, :, 2] = Vt 94 | fp.close() 95 | return YUV 96 | 97 | 98 | def CalcuPSNR(target, ref): 99 | diff = ref - target 100 | diff = diff.flatten('C') 101 | rmse = math.sqrt(np.mean(diff**2.)) 102 | return 20 * math.log10(1.0 / (rmse)) 103 | 104 | def MSE2PSNR(MSE): 105 | return 10 * math.log10(1.0 / (MSE)) 106 | 107 | def geti(lamb): 108 | if lamb == 2048: 109 | return 'H265L20' 110 | elif lamb == 1024: 111 | return 'H265L23' 112 | elif lamb == 512: 113 | return 'H265L26' 114 | elif lamb == 256: 115 | return 'H265L29' 116 | else: 117 | print("cannot find lambda : %d"%(lamb)) 118 | exit(0) 119 | 120 | 121 | def conv2d_same_padding(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1): 122 | # 函数中padding参数可以无视,实际实现的是padding=same的效果 123 | input_rows = input.size(2) 124 | filter_rows = weight.size(2) 125 | effective_filter_size_rows = (filter_rows - 1) * dilation[0] + 1 126 | out_rows = (input_rows + stride[0] - 1) // stride[0] 127 | padding_rows = max(0, (out_rows - 1) * stride[0] + 128 | (filter_rows - 1) * dilation[0] + 1 - input_rows) 129 | rows_odd = (padding_rows % 2 != 0) 130 | padding_cols = max(0, (out_rows - 1) * stride[0] + 131 | (filter_rows - 1) * dilation[0] + 1 - input_rows) 132 | cols_odd = (padding_rows % 2 != 0) 133 | 134 | if rows_odd or cols_odd: 135 | input = torch.pad(input, [0, int(cols_odd), 0, int(rows_odd)]) 136 | 137 | return F.conv2d(input, weight, bias, stride, 138 | padding=(padding_rows // 2, padding_cols // 2), 139 | dilation=dilation, groups=groups) -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/subnet/bitEstimator.py: -------------------------------------------------------------------------------- 1 | from .basics import * 2 | # import pickle 3 | # import os 4 | # import codecs 5 | 6 | class Bitparm(nn.Module): 7 | ''' 8 | save params 9 | ''' 10 | def __init__(self, channel, final=False): 11 | super(Bitparm, self).__init__() 12 | self.final = final 13 | self.h = nn.Parameter(torch.nn.init.normal_(torch.empty(channel).view(1, -1, 1, 1), 0, 0.01)) 14 | self.b = nn.Parameter(torch.nn.init.normal_(torch.empty(channel).view(1, -1, 1, 1), 0, 0.01)) 15 | if not final: 16 | self.a = nn.Parameter(torch.nn.init.normal_(torch.empty(channel).view(1, -1, 1, 1), 0, 0.01)) 17 | else: 18 | self.a = None 19 | 20 | def forward(self, x): 21 | if self.final: 22 | return F.sigmoid(x * F.softplus(self.h) + self.b) 23 | else: 24 | x = x * F.softplus(self.h) + self.b 25 | return x + F.tanh(x) * F.tanh(self.a) 26 | 27 | class BitEstimator(nn.Module): 28 | ''' 29 | Estimate bit 30 | ''' 31 | def __init__(self, channel): 32 | super(BitEstimator, self).__init__() 33 | self.f1 = Bitparm(channel) 34 | self.f2 = Bitparm(channel) 35 | self.f3 = Bitparm(channel) 36 | self.f4 = Bitparm(channel, True) 37 | 38 | def forward(self, x): 39 | x = self.f1(x) 40 | x = self.f2(x) 41 | x = self.f3(x) 42 | return self.f4(x) -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/subnet/sga.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import numpy as np 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | 7 | class Quantizator_SGA(nn.Module): 8 | """ 9 | https://github.com/mandt-lab/improving-inference-for-neural-image-compression/blob/c9b5c1354a38e0bb505fc34c6c8f27170f62a75b/sga.py#L110 10 | Stochastic Gumbeling Annealing 11 | sample() has no grad, so we choose STE to backward. We can also try other estimate func. 12 | """ 13 | 14 | def __init__(self, gap=1000, c=0.002): 15 | super(Quantizator_SGA, self).__init__() 16 | self.gap = gap 17 | self.c = c 18 | 19 | def annealed_temperature(self, t, r, ub, lb=1e-8, backend=np, scheme='exp', **kwargs): 20 | """ 21 | Return the temperature at time step t, based on a chosen annealing schedule. 22 | :param t: step/iteration number 23 | :param r: decay strength 24 | :param ub: maximum/init temperature 25 | :param lb: small const like 1e-8 to prevent numerical issue when temperature gets too close to 0 26 | :param backend: np or tf 27 | :param scheme: 28 | :param kwargs: 29 | :return: 30 | """ 31 | default_t0 = kwargs.get('t0') 32 | 33 | if scheme == 'exp': 34 | tau = backend.exp(-r * t) 35 | elif scheme == 'exp0': 36 | # Modified version of above that fixes temperature at ub for initial t0 iterations 37 | t0 = kwargs.get('t0', default_t0) 38 | tau = ub * backend.exp(-r * (t - t0)) 39 | elif scheme == 'linear': 40 | # Cool temperature linearly from ub after the initial t0 iterations 41 | t0 = kwargs.get('t0', default_t0) 42 | tau = -r * (t - t0) + ub 43 | else: 44 | raise NotImplementedError 45 | 46 | if backend is None: 47 | return min(max(tau, lb), ub) 48 | else: 49 | return backend.minimum(backend.maximum(tau, lb), ub) 50 | 51 | def forward(self, input, it=None, mode=None, total_it=None): 52 | if mode == "training": 53 | assert it is not None 54 | x_floor = torch.floor(input) 55 | x_ceil = torch.ceil(input) 56 | x_bds = torch.stack([x_floor, x_ceil], dim=-1) 57 | 58 | eps = 1e-5 59 | 60 | # TDOO: input outside 61 | annealing_scheme = 'exp0' 62 | annealing_rate = 1e-3 # default annealing_rate = 1e-3 63 | t0 = int(total_it * 0.35) # default t0 = 700 for 2000 iters 64 | T_ub = 0.5 65 | 66 | T = self.annealed_temperature(it, r=annealing_rate, ub=T_ub, scheme=annealing_scheme, t0=t0) 67 | 68 | x_interval1 = torch.clamp(input - x_floor, -1 + eps, 1 - eps) 69 | x_atanh1 = torch.log((1 + x_interval1) / (1 - x_interval1)) / 2 70 | x_interval2 = torch.clamp(x_ceil - input, -1 + eps, 1 - eps) 71 | x_atanh2 = torch.log((1 + x_interval2) / (1 - x_interval2)) / 2 72 | 73 | rx_logits = torch.stack([-x_atanh1 / T, -x_atanh2 / T], dim=-1) 74 | rx = F.softmax(rx_logits, dim=-1) # just for observation in tensorboard 75 | rx_dist = torch.distributions.RelaxedOneHotCategorical(T, rx) 76 | 77 | rx_sample = rx_dist.rsample() 78 | 79 | x_tilde = torch.sum(x_bds * rx_sample, dim=-1) 80 | return x_tilde 81 | else: 82 | return torch.round(input) -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/subnet/synthesis.py: -------------------------------------------------------------------------------- 1 | #!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 2 | from .basics import * 3 | import pickle 4 | import os 5 | import codecs 6 | from .analysis import Analysis_net 7 | 8 | class Synthesis_net(nn.Module): 9 | ''' 10 | Decode residual 11 | ''' 12 | def __init__(self): 13 | super(Synthesis_net, self).__init__() 14 | self.deconv1 = nn.ConvTranspose2d(out_channel_M, out_channel_N, 5, stride=2, padding=2, output_padding=1) 15 | torch.nn.init.xavier_normal_(self.deconv1.weight.data, (math.sqrt(2 * 1 * (out_channel_M + out_channel_N) / (out_channel_M + out_channel_M)))) 16 | torch.nn.init.constant_(self.deconv1.bias.data, 0.01) 17 | self.igdn1 = GDN(out_channel_N, inverse=True) 18 | self.deconv2 = nn.ConvTranspose2d(out_channel_N, out_channel_N, 5, stride=2, padding=2, output_padding=1) 19 | torch.nn.init.xavier_normal_(self.deconv2.weight.data, math.sqrt(2 * 1)) 20 | torch.nn.init.constant_(self.deconv2.bias.data, 0.01) 21 | self.igdn2 = GDN(out_channel_N, inverse=True) 22 | self.deconv3 = nn.ConvTranspose2d(out_channel_N, out_channel_N, 5, stride=2, padding=2, output_padding=1) 23 | torch.nn.init.xavier_normal_(self.deconv3.weight.data, math.sqrt(2 * 1)) 24 | torch.nn.init.constant_(self.deconv3.bias.data, 0.01) 25 | self.igdn3 = GDN(out_channel_N, inverse=True) 26 | self.deconv4 = nn.ConvTranspose2d(out_channel_N, 3, 5, stride=2, padding=2, output_padding=1) 27 | torch.nn.init.xavier_normal_(self.deconv4.weight.data, (math.sqrt(2 * 1 * (out_channel_N + 3) / (out_channel_N + out_channel_N)))) 28 | torch.nn.init.constant_(self.deconv4.bias.data, 0.01) 29 | 30 | def forward(self, x): 31 | x = self.igdn1(self.deconv1(x)) 32 | x = self.igdn2(self.deconv2(x)) 33 | x = self.igdn3(self.deconv3(x)) 34 | x = self.deconv4(x) 35 | return x 36 | 37 | 38 | def build_model(): 39 | input_image = torch.zeros([7,3,256,256]) 40 | analysis_net = Analysis_net() 41 | synthesis_net = Synthesis_net() 42 | feature = analysis_net(input_image) 43 | recon_image = synthesis_net(feature) 44 | 45 | print("input_image : ", input_image.size()) 46 | print("feature : ", feature.size()) 47 | print("recon_image : ", recon_image.size()) 48 | 49 | 50 | 51 | if __name__ == '__main__': 52 | build_model() 53 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/subnet/synthesis_mv.py: -------------------------------------------------------------------------------- 1 | #!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 2 | from .basics import * 3 | import pickle 4 | import os 5 | import codecs 6 | from .analysis_mv import Analysis_mv_net 7 | # gdn = tf.contrib.layers.gdn 8 | 9 | class Synthesis_mv_net(nn.Module): 10 | ''' 11 | Compress motion 12 | ''' 13 | def __init__(self): 14 | super(Synthesis_mv_net, self).__init__() 15 | self.deconv1 = nn.ConvTranspose2d(out_channel_mv, out_channel_mv, 3, stride=2, padding=1, output_padding=1) 16 | torch.nn.init.xavier_normal_(self.deconv1.weight.data, math.sqrt(2 * 1)) 17 | torch.nn.init.constant_(self.deconv1.bias.data, 0.01) 18 | self.relu1 = nn.LeakyReLU(negative_slope=0.1) 19 | self.deconv2 = nn.Conv2d(out_channel_mv, out_channel_mv, 3, stride=1, padding=1) 20 | torch.nn.init.xavier_normal_(self.deconv2.weight.data, math.sqrt(2 * 1)) 21 | torch.nn.init.constant_(self.deconv2.bias.data, 0.01) 22 | self.relu2 = nn.LeakyReLU(negative_slope=0.1) 23 | self.deconv3 = nn.ConvTranspose2d(out_channel_mv, out_channel_mv, 3, stride=2, padding=1, output_padding=1) 24 | torch.nn.init.xavier_normal_(self.deconv3.weight.data, math.sqrt(2 * 1)) 25 | torch.nn.init.constant_(self.deconv3.bias.data, 0.01) 26 | self.relu3 = nn.LeakyReLU(negative_slope=0.1) 27 | self.deconv4 = nn.Conv2d(out_channel_mv, out_channel_mv, 3, stride=1, padding=1) 28 | torch.nn.init.xavier_normal_(self.deconv4.weight.data, math.sqrt(2 * 1)) 29 | torch.nn.init.constant_(self.deconv4.bias.data, 0.01) 30 | self.relu4 = nn.LeakyReLU(negative_slope=0.1) 31 | self.deconv5 = nn.ConvTranspose2d(out_channel_mv, out_channel_mv, 3, stride=2, padding=1, output_padding=1) 32 | torch.nn.init.xavier_normal_(self.deconv5.weight.data, math.sqrt(2 * 1)) 33 | torch.nn.init.constant_(self.deconv5.bias.data, 0.01) 34 | self.relu5 = nn.LeakyReLU(negative_slope=0.1) 35 | self.deconv6 = nn.Conv2d(out_channel_mv, out_channel_mv, 3, stride=1, padding=1) 36 | torch.nn.init.xavier_normal_(self.deconv6.weight.data, math.sqrt(2 * 1)) 37 | torch.nn.init.constant_(self.deconv6.bias.data, 0.01) 38 | self.relu6 = nn.LeakyReLU(negative_slope=0.1) 39 | self.deconv7 = nn.ConvTranspose2d(out_channel_mv, out_channel_mv, 3, stride=2, padding=1, output_padding=1) 40 | torch.nn.init.xavier_normal_(self.deconv7.weight.data, math.sqrt(2 * 1)) 41 | torch.nn.init.constant_(self.deconv7.bias.data, 0.01) 42 | self.relu7 = nn.LeakyReLU(negative_slope=0.1) 43 | self.deconv8 = nn.Conv2d(out_channel_mv, 2, 3, stride=1, padding=1) 44 | torch.nn.init.xavier_normal_(self.deconv8.weight.data, (math.sqrt(2 * 1 * (out_channel_mv + 2) / (out_channel_mv + out_channel_mv)))) 45 | torch.nn.init.constant_(self.deconv8.bias.data, 0.01) 46 | 47 | 48 | def forward(self, x): 49 | x = self.relu1(self.deconv1(x)) 50 | x = self.relu2(self.deconv2(x)) 51 | x = self.relu3(self.deconv3(x)) 52 | x = self.relu4(self.deconv4(x)) 53 | x = self.relu5(self.deconv5(x)) 54 | x = self.relu6(self.deconv6(x)) 55 | x = self.relu7(self.deconv7(x)) 56 | return self.deconv8(x) 57 | 58 | 59 | 60 | def build_model(): 61 | input_image = torch.zeros([4, 2, 256, 256]) 62 | analysis_mv_net = Analysis_mv_net() 63 | synthesis_mv_net = Synthesis_mv_net() 64 | feature = analysis_mv_net(input_image) 65 | recon_image = synthesis_mv_net(feature) 66 | print(input_image.size()) 67 | print(feature.size()) 68 | print(recon_image.size()) 69 | 70 | 71 | 72 | 73 | if __name__ == '__main__': 74 | build_model() 75 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/subnet/synthesis_prior.py: -------------------------------------------------------------------------------- 1 | #!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 2 | from .basics import * 3 | import pickle 4 | import os 5 | import codecs 6 | from .analysis import Analysis_net 7 | from .analysis_prior import Analysis_prior_net 8 | from .synthesis import Synthesis_net 9 | 10 | 11 | class Synthesis_prior_net(nn.Module): 12 | ''' 13 | Decode residual prior 14 | ''' 15 | def __init__(self): 16 | super(Synthesis_prior_net, self).__init__() 17 | self.deconv1 = nn.ConvTranspose2d(out_channel_N, out_channel_N, 5, stride=2, padding=2, output_padding=1) 18 | torch.nn.init.xavier_normal_(self.deconv1.weight.data, math.sqrt(2 * 1)) 19 | torch.nn.init.constant_(self.deconv1.bias.data, 0.01) 20 | self.relu1 = nn.ReLU() 21 | self.deconv2 = nn.ConvTranspose2d(out_channel_N, out_channel_N, 5, stride=2, padding=2, output_padding=1) 22 | torch.nn.init.xavier_normal_(self.deconv2.weight.data, math.sqrt(2 * 1)) 23 | torch.nn.init.constant_(self.deconv2.bias.data, 0.01) 24 | self.relu2 = nn.ReLU() 25 | self.deconv3 = nn.ConvTranspose2d(out_channel_N, out_channel_M, 3, stride=1, padding=1) 26 | torch.nn.init.xavier_normal_(self.deconv3.weight.data, (math.sqrt(2 * 1 * (out_channel_M + out_channel_N) / (out_channel_N + out_channel_N)))) 27 | torch.nn.init.constant_(self.deconv3.bias.data, 0.01) 28 | 29 | 30 | 31 | def forward(self, x): 32 | x = self.relu1(self.deconv1(x)) 33 | x = self.relu2(self.deconv2(x)) 34 | return torch.exp(self.deconv3(x)) 35 | 36 | 37 | def build_model(): 38 | 39 | input_image = torch.zeros([7,3,256,256]) 40 | analysis_net = Analysis_net() 41 | analysis_prior_net = Analysis_prior_net() 42 | synthesis_net = Synthesis_net() 43 | synthesis_prior_net = Synthesis_prior_net() 44 | 45 | feature = analysis_net(input_image) 46 | z = analysis_prior_net(feature) 47 | 48 | compressed_z = torch.round(z) 49 | 50 | recon_sigma = synthesis_prior_net(compressed_z) 51 | 52 | 53 | compressed_feature_renorm = feature / recon_sigma 54 | compressed_feature_renorm = torch.round(compressed_feature_renorm) 55 | compressed_feature_denorm = compressed_feature_renorm * recon_sigma 56 | 57 | recon_image = synthesis_net(compressed_feature_denorm) 58 | 59 | print("input_image : ", input_image.size()) 60 | print("feature : ", feature.size()) 61 | print("z : ", z.size()) 62 | print("recon_sigma : ", recon_sigma.size()) 63 | print("recon_image : ", recon_image.size()) 64 | 65 | 66 | if __name__ == '__main__': 67 | build_model() 68 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/DVC/subnet/vis_flow.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | def vis_sparse_flow(flow, X=None, Y=None, path="flow.png"): 7 | flow = flow.copy() 8 | flow[:, :, 0] = -flow[:, :, 0] 9 | if X is None: 10 | height, width, _ = flow.shape 11 | xx = np.arange(0, height) 12 | yy = np.arange(0, width) 13 | X, Y = np.meshgrid(yy, xx) 14 | X = X.flatten() 15 | Y = Y.flatten() 16 | 17 | # sample 18 | sample_x = flow[:, :, 0] 19 | sample_y = flow[:, :, 1] 20 | sample_x = sample_x[:, :, np.newaxis] 21 | sample_y = sample_y[:, :, np.newaxis] 22 | 23 | new_flow = np.concatenate([sample_x, sample_y], axis=2) 24 | flow_x = new_flow[:, :, 0].flatten() 25 | flow_y = new_flow[:, :, 1].flatten() 26 | 27 | # display 28 | plt.cla() # Clean up the canvas left in the previous step, or it, otherwise overlapping areas will appear. 29 | ax = plt.gca() 30 | ax.xaxis.set_ticks_position('top') 31 | ax.invert_yaxis() 32 | plt.axis('off') 33 | ax.quiver(X, Y, flow_x, flow_y, color="#666666") 34 | ax.grid() 35 | # ax.legend() 36 | plt.draw() 37 | plt.savefig(path, format='pdf', dpi=300, bbox_inches='tight') 38 | # plt.show() 39 | 40 | 41 | def gen_flow_circle(center, height, width): 42 | x0, y0 = center 43 | if x0 >= height or y0 >= width: 44 | raise AttributeError('ERROR') 45 | flow = np.zeros((height, width, 2), dtype=np.float32) 46 | 47 | grid_x = np.tile(np.expand_dims(np.arange(width), 0), [height, 1]) 48 | grid_y = np.tile(np.expand_dims(np.arange(height), 1), [1, width]) 49 | 50 | grid_x0 = np.tile(np.array([x0]), [height, width]) 51 | grid_y0 = np.tile(np.array([y0]), [height, width]) 52 | 53 | flow[:, :, 0] = grid_x0 - grid_x 54 | flow[:, :, 1] = grid_y0 - grid_y 55 | 56 | return flow 57 | 58 | 59 | if __name__ == '__main__': 60 | center = [5, 5] 61 | flow = gen_flow_circle(center, height=11, width=11) 62 | flow = flow / 2 63 | vis_sparse_flow(flow) 64 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tongdaxu/Bit-Allocation-Using-Optimization/d691cfeeffae1fc9085789a02d8bcce3a2bc95d2/5.2. Evaluation on Bit Allocation/HSTEM/__init__.py -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/cpp/3rdparty/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. 2 | # Licensed under the MIT License. 3 | 4 | add_subdirectory(pybind11) 5 | add_subdirectory(ryg_rans) -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/cpp/3rdparty/pybind11/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. 2 | # Licensed under the MIT License. 3 | 4 | configure_file(CMakeLists.txt.in pybind11-download/CMakeLists.txt) 5 | execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . 6 | RESULT_VARIABLE result 7 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pybind11-download ) 8 | if(result) 9 | message(FATAL_ERROR "CMake step for pybind11 failed: ${result}") 10 | endif() 11 | execute_process(COMMAND ${CMAKE_COMMAND} --build . 12 | RESULT_VARIABLE result 13 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pybind11-download ) 14 | if(result) 15 | message(FATAL_ERROR "Build step for pybind11 failed: ${result}") 16 | endif() 17 | 18 | add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/pybind11-src/ 19 | ${CMAKE_CURRENT_BINARY_DIR}/pybind11-build/ 20 | EXCLUDE_FROM_ALL) 21 | 22 | set(PYBIND11_INCLUDE 23 | ${CMAKE_CURRENT_BINARY_DIR}/pybind11-src/include/ 24 | CACHE INTERNAL "") 25 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/cpp/3rdparty/pybind11/CMakeLists.txt.in: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.6.3) 2 | 3 | project(pybind11-download NONE) 4 | 5 | include(ExternalProject) 6 | if(IS_DIRECTORY "${PROJECT_BINARY_DIR}/3rdparty/pybind11/pybind11-src/include") 7 | ExternalProject_Add(pybind11 8 | GIT_REPOSITORY https://github.com/pybind/pybind11.git 9 | GIT_TAG v2.6.1 10 | GIT_SHALLOW 1 11 | SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/pybind11-src" 12 | BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/pybind11-build" 13 | DOWNLOAD_COMMAND "" 14 | UPDATE_COMMAND "" 15 | CONFIGURE_COMMAND "" 16 | BUILD_COMMAND "" 17 | INSTALL_COMMAND "" 18 | TEST_COMMAND "" 19 | ) 20 | else() 21 | ExternalProject_Add(pybind11 22 | GIT_REPOSITORY https://github.com/pybind/pybind11.git 23 | GIT_TAG v2.6.1 24 | GIT_SHALLOW 1 25 | SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/pybind11-src" 26 | BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/pybind11-build" 27 | UPDATE_COMMAND "" 28 | CONFIGURE_COMMAND "" 29 | BUILD_COMMAND "" 30 | INSTALL_COMMAND "" 31 | TEST_COMMAND "" 32 | ) 33 | endif() 34 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/cpp/3rdparty/ryg_rans/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. 2 | # Licensed under the MIT License. 3 | 4 | configure_file(CMakeLists.txt.in ryg_rans-download/CMakeLists.txt) 5 | execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . 6 | RESULT_VARIABLE result 7 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-download ) 8 | if(result) 9 | message(FATAL_ERROR "CMake step for ryg_rans failed: ${result}") 10 | endif() 11 | execute_process(COMMAND ${CMAKE_COMMAND} --build . 12 | RESULT_VARIABLE result 13 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-download ) 14 | if(result) 15 | message(FATAL_ERROR "Build step for ryg_rans failed: ${result}") 16 | endif() 17 | 18 | # add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-src/ 19 | # ${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-build 20 | # EXCLUDE_FROM_ALL) 21 | 22 | set(RYG_RANS_INCLUDE 23 | ${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-src/ 24 | CACHE INTERNAL "") 25 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/cpp/3rdparty/ryg_rans/CMakeLists.txt.in: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.6.3) 2 | 3 | project(ryg_rans-download NONE) 4 | 5 | include(ExternalProject) 6 | if(EXISTS "${PROJECT_BINARY_DIR}/3rdparty/ryg_rans/ryg_rans-src/rans64.h") 7 | ExternalProject_Add(ryg_rans 8 | GIT_REPOSITORY https://github.com/rygorous/ryg_rans.git 9 | GIT_TAG c9d162d996fd600315af9ae8eb89d832576cb32d 10 | GIT_SHALLOW 1 11 | SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-src" 12 | BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-build" 13 | DOWNLOAD_COMMAND "" 14 | UPDATE_COMMAND "" 15 | CONFIGURE_COMMAND "" 16 | BUILD_COMMAND "" 17 | INSTALL_COMMAND "" 18 | TEST_COMMAND "" 19 | ) 20 | else() 21 | ExternalProject_Add(ryg_rans 22 | GIT_REPOSITORY https://github.com/rygorous/ryg_rans.git 23 | GIT_TAG c9d162d996fd600315af9ae8eb89d832576cb32d 24 | GIT_SHALLOW 1 25 | SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-src" 26 | BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-build" 27 | UPDATE_COMMAND "" 28 | CONFIGURE_COMMAND "" 29 | BUILD_COMMAND "" 30 | INSTALL_COMMAND "" 31 | TEST_COMMAND "" 32 | ) 33 | endif() 34 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/cpp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. 2 | # Licensed under the MIT License. 3 | 4 | cmake_minimum_required (VERSION 3.6.3) 5 | project (ErrorRecovery) 6 | 7 | set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo" CACHE STRING "" FORCE) 8 | 9 | set(CMAKE_CXX_STANDARD 17) 10 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 11 | set(CMAKE_CXX_EXTENSIONS OFF) 12 | 13 | # treat warning as error 14 | if (MSVC) 15 | add_compile_options(/W4 /WX) 16 | else() 17 | add_compile_options(-Wall -Wextra -pedantic -Werror) 18 | endif() 19 | 20 | # The sequence is tricky, put 3rd party first 21 | add_subdirectory(3rdparty) 22 | add_subdirectory (ops) 23 | add_subdirectory (rans) 24 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/cpp/ops/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. 2 | # Licensed under the MIT License. 3 | 4 | cmake_minimum_required(VERSION 3.7) 5 | set(PROJECT_NAME MLCodec_CXX) 6 | project(${PROJECT_NAME}) 7 | 8 | set(cxx_source 9 | ops.cpp 10 | ) 11 | 12 | set(include_dirs 13 | ${CMAKE_CURRENT_SOURCE_DIR} 14 | ${PYBIND11_INCLUDE} 15 | ) 16 | 17 | pybind11_add_module(${PROJECT_NAME} ${cxx_source}) 18 | 19 | target_include_directories (${PROJECT_NAME} PUBLIC ${include_dirs}) 20 | 21 | # The post build argument is executed after make! 22 | add_custom_command( 23 | TARGET ${PROJECT_NAME} POST_BUILD 24 | COMMAND 25 | "${CMAKE_COMMAND}" -E copy 26 | "$" 27 | "${CMAKE_CURRENT_SOURCE_DIR}/../../entropy_models/" 28 | ) 29 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/cpp/ops/ops.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 InterDigital Communications, Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | std::vector pmf_to_quantized_cdf(const std::vector &pmf, 25 | int precision) { 26 | /* NOTE(begaintj): ported from `ryg_rans` public implementation. Not optimal 27 | * although it's only run once per model after training. See TF/compression 28 | * implementation for an optimized version. */ 29 | 30 | std::vector cdf(pmf.size() + 1); 31 | cdf[0] = 0; /* freq 0 */ 32 | 33 | std::transform(pmf.begin(), pmf.end(), cdf.begin() + 1, [=](float p) { 34 | return static_cast(std::round(p * (1 << precision)) + 0.5); 35 | }); 36 | 37 | const uint32_t total = std::accumulate(cdf.begin(), cdf.end(), 0); 38 | 39 | std::transform( 40 | cdf.begin(), cdf.end(), cdf.begin(), [precision, total](uint32_t p) { 41 | return static_cast((((1ull << precision) * p) / total)); 42 | }); 43 | 44 | std::partial_sum(cdf.begin(), cdf.end(), cdf.begin()); 45 | cdf.back() = 1 << precision; 46 | 47 | for (int i = 0; i < static_cast(cdf.size() - 1); ++i) { 48 | if (cdf[i] == cdf[i + 1]) { 49 | /* Try to steal frequency from low-frequency symbols */ 50 | uint32_t best_freq = ~0u; 51 | int best_steal = -1; 52 | for (int j = 0; j < static_cast(cdf.size()) - 1; ++j) { 53 | uint32_t freq = cdf[j + 1] - cdf[j]; 54 | if (freq > 1 && freq < best_freq) { 55 | best_freq = freq; 56 | best_steal = j; 57 | } 58 | } 59 | 60 | assert(best_steal != -1); 61 | 62 | if (best_steal < i) { 63 | for (int j = best_steal + 1; j <= i; ++j) { 64 | cdf[j]--; 65 | } 66 | } else { 67 | assert(best_steal > i); 68 | for (int j = i + 1; j <= best_steal; ++j) { 69 | cdf[j]++; 70 | } 71 | } 72 | } 73 | } 74 | 75 | assert(cdf[0] == 0); 76 | assert(cdf.back() == (1u << precision)); 77 | for (int i = 0; i < static_cast(cdf.size()) - 1; ++i) { 78 | assert(cdf[i + 1] > cdf[i]); 79 | } 80 | 81 | return cdf; 82 | } 83 | 84 | PYBIND11_MODULE(MLCodec_CXX, m) { 85 | m.attr("__name__") = "MLCodec_CXX"; 86 | 87 | m.doc() = "C++ utils"; 88 | 89 | m.def("pmf_to_quantized_cdf", &pmf_to_quantized_cdf, 90 | "Return quantized CDF for a given PMF"); 91 | } 92 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/cpp/rans/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. 2 | # Licensed under the MIT License. 3 | 4 | cmake_minimum_required(VERSION 3.7) 5 | set(PROJECT_NAME MLCodec_rans) 6 | project(${PROJECT_NAME}) 7 | 8 | set(rans_source 9 | rans_interface.hpp 10 | rans_interface.cpp 11 | ) 12 | 13 | set(include_dirs 14 | ${CMAKE_CURRENT_SOURCE_DIR} 15 | ${PYBIND11_INCLUDE} 16 | ${RYG_RANS_INCLUDE} 17 | ) 18 | 19 | pybind11_add_module(${PROJECT_NAME} ${rans_source}) 20 | 21 | target_include_directories (${PROJECT_NAME} PUBLIC ${include_dirs}) 22 | 23 | # The post build argument is executed after make! 24 | add_custom_command( 25 | TARGET ${PROJECT_NAME} POST_BUILD 26 | COMMAND 27 | "${CMAKE_COMMAND}" -E copy 28 | "$" 29 | "${CMAKE_CURRENT_SOURCE_DIR}/../../entropy_models/" 30 | ) 31 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/cpp/rans/rans_interface.hpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 InterDigital Communications, Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #pragma once 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #ifdef __GNUC__ 23 | #pragma GCC diagnostic push 24 | #pragma GCC diagnostic ignored "-Wpedantic" 25 | #pragma GCC diagnostic ignored "-Wsign-compare" 26 | #elif _MSC_VER 27 | #pragma warning(push, 0) 28 | #endif 29 | 30 | #include 31 | 32 | #ifdef __GNUC__ 33 | #pragma GCC diagnostic pop 34 | #elif _MSC_VER 35 | #pragma warning(pop) 36 | #endif 37 | 38 | namespace py = pybind11; 39 | 40 | struct RansSymbol { 41 | uint16_t start; 42 | uint16_t range; 43 | bool bypass; // bypass flag to write raw bits to the stream 44 | }; 45 | 46 | /* NOTE: Warning, we buffer everything for now... In case of large files we 47 | * should split the bitstream into chunks... Or for a memory-bounded encoder 48 | **/ 49 | class BufferedRansEncoder { 50 | public: 51 | BufferedRansEncoder() = default; 52 | 53 | BufferedRansEncoder(const BufferedRansEncoder &) = delete; 54 | BufferedRansEncoder(BufferedRansEncoder &&) = delete; 55 | BufferedRansEncoder &operator=(const BufferedRansEncoder &) = delete; 56 | BufferedRansEncoder &operator=(BufferedRansEncoder &&) = delete; 57 | 58 | void encode_with_indexes(const py::array_t &symbols, 59 | const py::array_t &indexes, 60 | const py::array_t &cdfs, 61 | const py::array_t &cdfs_sizes, 62 | const py::array_t &offsets); 63 | py::bytes flush(); 64 | void reset(); 65 | 66 | private: 67 | std::vector _syms; 68 | }; 69 | 70 | class RansDecoder { 71 | public: 72 | RansDecoder() = default; 73 | 74 | RansDecoder(const RansDecoder &) = delete; 75 | RansDecoder(RansDecoder &&) = delete; 76 | RansDecoder &operator=(const RansDecoder &) = delete; 77 | RansDecoder &operator=(RansDecoder &&) = delete; 78 | 79 | void set_stream(const std::string &stream); 80 | 81 | py::array_t 82 | decode_stream(const py::array_t &indexes, 83 | const py::array_t &cdfs, 84 | const py::array_t &cdfs_sizes, 85 | const py::array_t &offsets); 86 | 87 | private: 88 | Rans64State _rans; 89 | std::string _stream; 90 | uint32_t *_ptr; 91 | }; 92 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/layers/layers.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from torch import nn 16 | 17 | 18 | def conv3x3(in_ch, out_ch, stride=1): 19 | """3x3 convolution with padding.""" 20 | return nn.Conv2d(in_ch, out_ch, kernel_size=3, stride=stride, padding=1) 21 | 22 | 23 | def subpel_conv3x3(in_ch, out_ch, r=1): 24 | """3x3 sub-pixel convolution for up-sampling.""" 25 | return nn.Sequential( 26 | nn.Conv2d(in_ch, out_ch * r ** 2, kernel_size=3, padding=1), nn.PixelShuffle(r) 27 | ) 28 | 29 | 30 | def subpel_conv1x1(in_ch, out_ch, r=1): 31 | """1x1 sub-pixel convolution for up-sampling.""" 32 | return nn.Sequential( 33 | nn.Conv2d(in_ch, out_ch * r ** 2, kernel_size=1, padding=0), nn.PixelShuffle(r) 34 | ) 35 | 36 | 37 | def conv1x1(in_ch, out_ch, stride=1): 38 | """1x1 convolution.""" 39 | return nn.Conv2d(in_ch, out_ch, kernel_size=1, stride=stride) 40 | 41 | 42 | class ResidualBlockWithStride(nn.Module): 43 | """Residual block with a stride on the first convolution. 44 | 45 | Args: 46 | in_ch (int): number of input channels 47 | out_ch (int): number of output channels 48 | stride (int): stride value (default: 2) 49 | """ 50 | 51 | def __init__(self, in_ch, out_ch, stride=2): 52 | super().__init__() 53 | self.conv1 = conv3x3(in_ch, out_ch, stride=stride) 54 | self.leaky_relu = nn.LeakyReLU() 55 | self.conv2 = conv3x3(out_ch, out_ch) 56 | self.leaky_relu2 = nn.LeakyReLU(negative_slope=0.1) 57 | if stride != 1: 58 | self.downsample = conv1x1(in_ch, out_ch, stride=stride) 59 | else: 60 | self.downsample = None 61 | 62 | def forward(self, x): 63 | identity = x 64 | out = self.conv1(x) 65 | out = self.leaky_relu(out) 66 | out = self.conv2(out) 67 | out = self.leaky_relu2(out) 68 | 69 | if self.downsample is not None: 70 | identity = self.downsample(x) 71 | 72 | out += identity 73 | return out 74 | 75 | 76 | class ResidualBlockUpsample(nn.Module): 77 | """Residual block with sub-pixel upsampling on the last convolution. 78 | 79 | Args: 80 | in_ch (int): number of input channels 81 | out_ch (int): number of output channels 82 | upsample (int): upsampling factor (default: 2) 83 | """ 84 | 85 | def __init__(self, in_ch, out_ch, upsample=2): 86 | super().__init__() 87 | self.subpel_conv = subpel_conv1x1(in_ch, out_ch, upsample) 88 | self.leaky_relu = nn.LeakyReLU() 89 | self.conv = conv3x3(out_ch, out_ch) 90 | self.leaky_relu2 = nn.LeakyReLU(negative_slope=0.1) 91 | self.upsample = subpel_conv1x1(in_ch, out_ch, upsample) 92 | 93 | def forward(self, x): 94 | identity = x 95 | out = self.subpel_conv(x) 96 | out = self.leaky_relu(out) 97 | out = self.conv(out) 98 | out = self.leaky_relu2(out) 99 | identity = self.upsample(x) 100 | out += identity 101 | return out 102 | 103 | 104 | class ResidualBlock(nn.Module): 105 | """Simple residual block with two 3x3 convolutions. 106 | 107 | Args: 108 | in_ch (int): number of input channels 109 | out_ch (int): number of output channels 110 | """ 111 | 112 | def __init__(self, in_ch, out_ch, leaky_relu_slope=0.01): 113 | super().__init__() 114 | self.conv1 = conv3x3(in_ch, out_ch) 115 | self.leaky_relu = nn.LeakyReLU(negative_slope=leaky_relu_slope) 116 | self.conv2 = conv3x3(out_ch, out_ch) 117 | 118 | def forward(self, x): 119 | identity = x 120 | 121 | out = self.conv1(x) 122 | out = self.leaky_relu(out) 123 | out = self.conv2(out) 124 | out = self.leaky_relu(out) 125 | 126 | out = out + identity 127 | return out 128 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/ryg_rans/LICENSE: -------------------------------------------------------------------------------- 1 | To the extent possible under law, Fabian Giesen has waived all 2 | copyright and related or neighboring rights to ryg_rans, as 3 | per the terms of the CC0 license: 4 | 5 | https://creativecommons.org/publicdomain/zero/1.0 6 | 7 | This work is published from the United States. 8 | 9 | 10 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/ryg_rans/Makefile: -------------------------------------------------------------------------------- 1 | LIBS=-lm -lrt 2 | 3 | all: exam exam64 exam_simd_sse41 exam_alias 4 | 5 | exam: main.cpp platform.h rans_byte.h 6 | g++ -o $@ $< -O3 $(LIBS) 7 | 8 | exam64: main64.cpp platform.h rans64.h 9 | g++ -o $@ $< -O3 $(LIBS) 10 | 11 | exam_simd_sse41: main_simd.cpp platform.h rans_word_sse41.h 12 | g++ -o $@ $< -O3 -msse4.1 $(LIBS) 13 | 14 | exam_alias: main_alias.cpp platform.h rans_byte.h 15 | g++ -o $@ $< -O3 $(LIBS) 16 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/ryg_rans/README: -------------------------------------------------------------------------------- 1 | This is a public-domain implementation of several rANS variants. rANS is an 2 | entropy coder from the ANS family, as described in Jarek Duda's paper 3 | "Asymmetric numeral systems" (http://arxiv.org/abs/1311.2540). 4 | 5 | - "rans_byte.h" has a byte-aligned rANS encoder/decoder and some comments on 6 | how to use it. This implementation should work on all 32-bit architectures. 7 | "main.cpp" is an example program that shows how to use it. 8 | - "rans64.h" is a 64-bit version that emits entire 32-bit words at a time. It 9 | is (usually) a good deal faster than rans_byte on 64-bit architectures, and 10 | also makes for a very precise arithmetic coder (i.e. it gets quite close 11 | to entropy). The trade-off is that this version will be slower on 32-bit 12 | machines, and the output bitstream is not endian-neutral. "main64.cpp" is 13 | the corresponding example. 14 | - "rans_word_sse41.h" has a SIMD decoder (SSE 4.1 to be precise) that does IO 15 | in units of 16-bit words. It has less precision than either rans_byte or 16 | rans64 (meaning that it doesn't get as close to entropy) and requires 17 | at least 4 independent streams of data to be useful; however, it is also a 18 | good deal faster. "main_simd.cpp" shows how to use it. 19 | 20 | See my blog http://fgiesen.wordpress.com/ for some notes on the design. 21 | 22 | I've also written a paper on interleaving output streams from multiple entropy 23 | coders: 24 | 25 | http://arxiv.org/abs/1402.3392 26 | 27 | this documents the underlying design for "rans_word_sse41", and also shows how 28 | the same approach generalizes to e.g. GPU implementations, provided there are 29 | enough independent contexts coded at the same time to fill up a warp/wavefront 30 | or whatever your favorite GPU's terminology for its native SIMD width is. 31 | 32 | Finally, there's also "main_alias.cpp", which shows how to combine rANS with 33 | the alias method to get O(1) symbol lookup with table size proportional to the 34 | number of symbols. I presented an overview of the underlying idea here: 35 | 36 | http://fgiesen.wordpress.com/2014/02/18/rans-with-static-probability-distributions/ 37 | 38 | Results on my machine (Sandy Bridge i7-2600K) with rans_byte in 64-bit mode: 39 | 40 | ---- 41 | 42 | rANS encode: 43 | 12896496 clocks, 16.8 clocks/symbol (192.8MiB/s) 44 | 12486912 clocks, 16.2 clocks/symbol (199.2MiB/s) 45 | 12511975 clocks, 16.3 clocks/symbol (198.8MiB/s) 46 | 12660765 clocks, 16.5 clocks/symbol (196.4MiB/s) 47 | 12550285 clocks, 16.3 clocks/symbol (198.2MiB/s) 48 | rANS: 435113 bytes 49 | 17023550 clocks, 22.1 clocks/symbol (146.1MiB/s) 50 | 18081509 clocks, 23.5 clocks/symbol (137.5MiB/s) 51 | 16901632 clocks, 22.0 clocks/symbol (147.1MiB/s) 52 | 17166188 clocks, 22.3 clocks/symbol (144.9MiB/s) 53 | 17235859 clocks, 22.4 clocks/symbol (144.3MiB/s) 54 | decode ok! 55 | 56 | interleaved rANS encode: 57 | 9618004 clocks, 12.5 clocks/symbol (258.6MiB/s) 58 | 9488277 clocks, 12.3 clocks/symbol (262.1MiB/s) 59 | 9460194 clocks, 12.3 clocks/symbol (262.9MiB/s) 60 | 9582025 clocks, 12.5 clocks/symbol (259.5MiB/s) 61 | 9332017 clocks, 12.1 clocks/symbol (266.5MiB/s) 62 | interleaved rANS: 435117 bytes 63 | 10687601 clocks, 13.9 clocks/symbol (232.7MB/s) 64 | 10637918 clocks, 13.8 clocks/symbol (233.8MB/s) 65 | 10909652 clocks, 14.2 clocks/symbol (227.9MB/s) 66 | 10947637 clocks, 14.2 clocks/symbol (227.2MB/s) 67 | 10529464 clocks, 13.7 clocks/symbol (236.2MB/s) 68 | decode ok! 69 | 70 | ---- 71 | 72 | And here's rans64 in 64-bit mode: 73 | 74 | ---- 75 | 76 | rANS encode: 77 | 10256075 clocks, 13.3 clocks/symbol (242.3MiB/s) 78 | 10620132 clocks, 13.8 clocks/symbol (234.1MiB/s) 79 | 10043080 clocks, 13.1 clocks/symbol (247.6MiB/s) 80 | 9878205 clocks, 12.8 clocks/symbol (251.8MiB/s) 81 | 10122645 clocks, 13.2 clocks/symbol (245.7MiB/s) 82 | rANS: 435116 bytes 83 | 14244155 clocks, 18.5 clocks/symbol (174.6MiB/s) 84 | 15072524 clocks, 19.6 clocks/symbol (165.0MiB/s) 85 | 14787604 clocks, 19.2 clocks/symbol (168.2MiB/s) 86 | 14736556 clocks, 19.2 clocks/symbol (168.8MiB/s) 87 | 14686129 clocks, 19.1 clocks/symbol (169.3MiB/s) 88 | decode ok! 89 | 90 | interleaved rANS encode: 91 | 7691159 clocks, 10.0 clocks/symbol (323.3MiB/s) 92 | 7182692 clocks, 9.3 clocks/symbol (346.2MiB/s) 93 | 7060804 clocks, 9.2 clocks/symbol (352.2MiB/s) 94 | 6949201 clocks, 9.0 clocks/symbol (357.9MiB/s) 95 | 6876415 clocks, 8.9 clocks/symbol (361.6MiB/s) 96 | interleaved rANS: 435120 bytes 97 | 8133574 clocks, 10.6 clocks/symbol (305.7MB/s) 98 | 8631618 clocks, 11.2 clocks/symbol (288.1MB/s) 99 | 8643790 clocks, 11.2 clocks/symbol (287.7MB/s) 100 | 8449364 clocks, 11.0 clocks/symbol (294.3MB/s) 101 | 8331444 clocks, 10.8 clocks/symbol (298.5MB/s) 102 | decode ok! 103 | 104 | ---- 105 | 106 | Finally, here's the rans_word_sse41 decoder on an 8-way interleaved stream: 107 | 108 | ---- 109 | 110 | SIMD rANS: 435626 bytes 111 | 4597641 clocks, 6.0 clocks/symbol (540.8MB/s) 112 | 4514356 clocks, 5.9 clocks/symbol (550.8MB/s) 113 | 4780918 clocks, 6.2 clocks/symbol (520.1MB/s) 114 | 4532913 clocks, 5.9 clocks/symbol (548.5MB/s) 115 | 4554527 clocks, 5.9 clocks/symbol (545.9MB/s) 116 | decode ok! 117 | 118 | ---- 119 | 120 | There's also an experimental 16-way interleaved AVX2 version that hits 121 | faster rates still, developed by my colleague Won Chun; I will post it 122 | soon. 123 | 124 | Note that this is running "book1" which is a relatively short test, and 125 | the measurement setup is not great, so take the results with a grain 126 | of salt. 127 | 128 | -Fabian "ryg" Giesen, Feb 2014. 129 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/ryg_rans/platform.h: -------------------------------------------------------------------------------- 1 | // Just some platform utilities. 2 | #ifndef PLATFORM_H_INCLUDED 3 | #define PLATFORM_H_INCLUDED 4 | 5 | // x86 intrinsics (__rdtsc etc.) 6 | 7 | #if defined(_MSC_VER) 8 | 9 | #define _CRT_SECURE_NO_DEPRECATE 10 | #include 11 | #define ALIGNSPEC(type,name,alignment) __declspec(align(alignment)) type name 12 | 13 | #elif defined(__GNUC__) 14 | 15 | #include 16 | #define ALIGNSPEC(type,name,alignment) type name __attribute__((aligned(alignment))) 17 | 18 | #else 19 | #error Unknown compiler! 20 | #endif 21 | 22 | // Timer 23 | 24 | #if defined(_WIN32) 25 | 26 | #define WIN32_LEAN_AND_MEAN 27 | #define NOMINMAX 28 | #include 29 | 30 | #define PRIu64 "llu" 31 | 32 | double timer() 33 | { 34 | LARGE_INTEGER ctr, freq; 35 | QueryPerformanceCounter(&ctr); 36 | QueryPerformanceFrequency(&freq); 37 | return 1.0 * ctr.QuadPart / freq.QuadPart; 38 | } 39 | 40 | #elif defined(__linux__) 41 | 42 | #define __STDC_FORMAT_MACROS 43 | #include 44 | #include 45 | #include 46 | 47 | static inline double timer() 48 | { 49 | timespec ts; 50 | ts.tv_sec = 0; 51 | ts.tv_nsec = 0; 52 | int status = clock_gettime(CLOCK_MONOTONIC, &ts); 53 | assert(status == 0); 54 | return double(ts.tv_sec) + 1.0e-9 * double(ts.tv_nsec); 55 | } 56 | 57 | #else 58 | 59 | #error Unknown platform! 60 | 61 | #endif 62 | 63 | #endif // PLATFORM_H_INCLUDED 64 | 65 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/utils/Dataset.py: -------------------------------------------------------------------------------- 1 | import os 2 | import h5py 3 | import numpy as np 4 | import torch 5 | from torch.utils.data.dataset import Dataset, ConcatDataset 6 | 7 | 8 | class VimeoDataset(Dataset): 9 | def __init__(self, h5_file): 10 | super(VimeoDataset, self).__init__() 11 | self.h5 = h5py.File(name=h5_file, mode='r') 12 | self.file_list = list(self.h5.keys()) 13 | 14 | def __len__(self): 15 | return len(self.file_list) 16 | 17 | def __getitem__(self, idx): # 负责按索引取出某个数据,并对该数据做预处理 18 | frames = self.h5[self.file_list[idx]] 19 | frames = np.array(frames) 20 | frames = torch.from_numpy(frames) / 255.0 # scale to [0, 1] 21 | frames = frames.permute(0, 3, 1, 2).contiguous().float() 22 | return frames 23 | 24 | 25 | def is_h5(h5): 26 | return any(h5.endswith(extension) for extension in ['.hdf5', ]) 27 | 28 | 29 | def create_dataset(h5_folder): 30 | datasets = [VimeoDataset(os.path.join(h5_folder, h5)) for h5 in os.listdir(h5_folder) if is_h5(h5)] 31 | return ConcatDataset(datasets=datasets) 32 | 33 | # 34 | # tester = VimeoDataset('../Dataset/Train_1024_part_9.hdf5') 35 | # from PIL import Image 36 | # 37 | # aa = tester[2] 38 | # 39 | # for i in range(7): 40 | # a = aa[i] 41 | # 42 | # a = a.permute(1, 2, 0).contiguous() 43 | # a = a.numpy() * 255 44 | # p = a.astype(np.uint8) 45 | # p = Image.fromarray(p) 46 | # p.show() 47 | # input() 48 | # 49 | # tester = create_dataset('../Dataset/Eval') 50 | # print(len(tester)) 51 | # from PIL import Image 52 | # 53 | # aa = tester[90] 54 | # 55 | # for i in range(7): 56 | # a = aa[i] 57 | # 58 | # a = a.permute(1, 2, 0).contiguous() 59 | # a = a.numpy() * 255 60 | # p = a.astype(np.uint8) 61 | # p = Image.fromarray(p) 62 | # p.show() 63 | # input() 64 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/utils/common.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. 2 | # Licensed under the MIT License. 3 | 4 | import argparse 5 | import json 6 | import os 7 | from unittest.mock import patch 8 | 9 | import torch 10 | import numpy as np 11 | 12 | 13 | def str2bool(v): 14 | if isinstance(v, bool): 15 | return v 16 | if v.lower() in ('yes', 'true', 't', 'y', '1'): 17 | return True 18 | if v.lower() in ('no', 'false', 'f', 'n', '0'): 19 | return False 20 | 21 | raise argparse.ArgumentTypeError('Boolean value expected.') 22 | 23 | 24 | def interpolate_log(min_val, max_val, num, decending=True): 25 | assert max_val > min_val 26 | assert min_val > 0 27 | if decending: 28 | values = np.linspace(np.log(max_val), np.log(min_val), num) 29 | else: 30 | values = np.linspace(np.log(min_val), np.log(max_val), num) 31 | values = np.exp(values) 32 | return values 33 | 34 | 35 | def scale_list_to_str(scales): 36 | s = '' 37 | for scale in scales: 38 | s += f'{scale:.2f} ' 39 | 40 | return s 41 | 42 | 43 | def create_folder(path, print_if_create=False): 44 | if not os.path.exists(path): 45 | os.makedirs(path) 46 | if print_if_create: 47 | print(f"created folder: {path}") 48 | 49 | 50 | @patch('json.encoder.c_make_encoder', None) 51 | def dump_json(obj, fid, float_digits=-1, **kwargs): 52 | of = json.encoder._make_iterencode # pylint: disable=W0212 53 | 54 | def inner(*args, **kwargs): 55 | args = list(args) 56 | # fifth argument is float formater which we will replace 57 | args[4] = lambda o: format(o, '.%df' % float_digits) 58 | return of(*args, **kwargs) 59 | 60 | with patch('json.encoder._make_iterencode', wraps=inner): 61 | json.dump(obj, fid, **kwargs) 62 | 63 | 64 | def generate_log_json(frame_num, frame_types, bits, psnrs, ssims, 65 | frame_pixel_num, test_time): 66 | cur_ave_i_frame_bit = 0 67 | cur_ave_i_frame_psnr = 0 68 | cur_ave_i_frame_msssim = 0 69 | cur_ave_p_frame_bit = 0 70 | cur_ave_p_frame_psnr = 0 71 | cur_ave_p_frame_msssim = 0 72 | cur_i_frame_num = 0 73 | cur_p_frame_num = 0 74 | for idx in range(frame_num): 75 | if frame_types[idx] == 0: 76 | cur_ave_i_frame_bit += bits[idx] 77 | cur_ave_i_frame_psnr += psnrs[idx] 78 | cur_ave_i_frame_msssim += ssims[idx] 79 | cur_i_frame_num += 1 80 | else: 81 | cur_ave_p_frame_bit += bits[idx] 82 | cur_ave_p_frame_psnr += psnrs[idx] 83 | cur_ave_p_frame_msssim += ssims[idx] 84 | cur_p_frame_num += 1 85 | 86 | log_result = {} 87 | log_result['frame_pixel_num'] = frame_pixel_num 88 | log_result['i_frame_num'] = cur_i_frame_num 89 | log_result['p_frame_num'] = cur_p_frame_num 90 | log_result['ave_i_frame_bpp'] = cur_ave_i_frame_bit / cur_i_frame_num / frame_pixel_num 91 | log_result['ave_i_frame_psnr'] = cur_ave_i_frame_psnr / cur_i_frame_num 92 | log_result['ave_i_frame_msssim'] = cur_ave_i_frame_msssim / cur_i_frame_num 93 | log_result['frame_bpp'] = list(np.array(bits) / frame_pixel_num) 94 | log_result['frame_psnr'] = psnrs 95 | log_result['frame_msssim'] = ssims 96 | log_result['frame_type'] = frame_types 97 | log_result['test_time'] = test_time 98 | if cur_p_frame_num > 0: 99 | total_p_pixel_num = cur_p_frame_num * frame_pixel_num 100 | log_result['ave_p_frame_bpp'] = cur_ave_p_frame_bit / total_p_pixel_num 101 | log_result['ave_p_frame_psnr'] = cur_ave_p_frame_psnr / cur_p_frame_num 102 | log_result['ave_p_frame_msssim'] = cur_ave_p_frame_msssim / cur_p_frame_num 103 | else: 104 | log_result['ave_p_frame_bpp'] = 0 105 | log_result['ave_p_frame_psnr'] = 0 106 | log_result['ave_p_frame_msssim'] = 0 107 | log_result['ave_all_frame_bpp'] = (cur_ave_i_frame_bit + cur_ave_p_frame_bit) / \ 108 | (frame_num * frame_pixel_num) 109 | log_result['ave_all_frame_psnr'] = (cur_ave_i_frame_psnr + cur_ave_p_frame_psnr) / frame_num 110 | log_result['ave_all_frame_msssim'] = (cur_ave_i_frame_msssim + cur_ave_p_frame_msssim) / \ 111 | frame_num 112 | 113 | return log_result 114 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/utils/png_reader.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Microsoft Corporation. 2 | # Licensed under the MIT License. 3 | 4 | import os 5 | 6 | import numpy as np 7 | from PIL import Image 8 | 9 | 10 | class PNGReader(): 11 | def __init__(self, src_folder, width, height): 12 | self.src_folder = src_folder 13 | pngs = os.listdir(self.src_folder) 14 | self.width = width 15 | self.height = height 16 | if 'im1.png' in pngs: 17 | self.padding = 1 18 | elif 'im00001.png' in pngs: 19 | self.padding = 5 20 | else: 21 | raise ValueError('unknown image naming convention; please specify') 22 | self.current_frame_index = 1 23 | self.eof = False 24 | 25 | def read_one_frame(self, src_format="rgb"): 26 | def _none_exist_frame(): 27 | if src_format == "rgb": 28 | return None 29 | return None, None, None 30 | if self.eof: 31 | return _none_exist_frame() 32 | 33 | png_path = os.path.join(self.src_folder, 34 | f"im{str(self.current_frame_index).zfill(self.padding)}.png" 35 | ) 36 | if not os.path.exists(png_path): 37 | self.eof = True 38 | return _none_exist_frame() 39 | 40 | rgb = Image.open(png_path).convert('RGB') 41 | rgb = np.asarray(rgb).astype('float32').transpose(2, 0, 1) 42 | rgb = rgb / 255. 43 | _, height, width = rgb.shape 44 | assert height == self.height 45 | assert width == self.width 46 | 47 | self.current_frame_index += 1 48 | return rgb 49 | 50 | def close(self): 51 | self.current_frame_index = 1 52 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/HSTEM/src/utils/stream_helper.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import struct 16 | from pathlib import Path 17 | 18 | import numpy as np 19 | import torch 20 | from torch.nn.modules.utils import consume_prefix_in_state_dict_if_present 21 | 22 | 23 | def get_padding_size(height, width, p=64): 24 | new_h = (height + p - 1) // p * p 25 | new_w = (width + p - 1) // p * p 26 | # padding_left = (new_w - width) // 2 27 | padding_left = 0 28 | padding_right = new_w - width - padding_left 29 | # padding_top = (new_h - height) // 2 30 | padding_top = 0 31 | padding_bottom = new_h - height - padding_top 32 | return padding_left, padding_right, padding_top, padding_bottom 33 | 34 | 35 | def get_downsampled_shape(height, width, p): 36 | new_h = (height + p - 1) // p * p 37 | new_w = (width + p - 1) // p * p 38 | return int(new_h / p + 0.5), int(new_w / p + 0.5) 39 | 40 | 41 | def get_rounded_q(q_scale): 42 | q_scale = np.clip(q_scale, 0.01, 655.) 43 | q_index = int(np.round(q_scale * 100)) 44 | q_scale = q_index / 100 45 | return q_scale, q_index 46 | 47 | 48 | def get_state_dict(ckpt_path): 49 | ckpt = torch.load(ckpt_path, map_location=torch.device('cpu')) 50 | if "state_dict" in ckpt: 51 | ckpt = ckpt['state_dict'] 52 | if "net" in ckpt: 53 | ckpt = ckpt["net"] 54 | consume_prefix_in_state_dict_if_present(ckpt, prefix="module.") 55 | return ckpt 56 | 57 | 58 | def filesize(filepath: str) -> int: 59 | if not Path(filepath).is_file(): 60 | raise ValueError(f'Invalid file "{filepath}".') 61 | return Path(filepath).stat().st_size 62 | 63 | 64 | def write_uints(fd, values, fmt=">{:d}I"): 65 | fd.write(struct.pack(fmt.format(len(values)), *values)) 66 | 67 | 68 | def write_uchars(fd, values, fmt=">{:d}B"): 69 | fd.write(struct.pack(fmt.format(len(values)), *values)) 70 | 71 | 72 | def read_uints(fd, n, fmt=">{:d}I"): 73 | sz = struct.calcsize("I") 74 | return struct.unpack(fmt.format(n), fd.read(n * sz)) 75 | 76 | 77 | def read_uchars(fd, n, fmt=">{:d}B"): 78 | sz = struct.calcsize("B") 79 | return struct.unpack(fmt.format(n), fd.read(n * sz)) 80 | 81 | 82 | def write_bytes(fd, values, fmt=">{:d}s"): 83 | if len(values) == 0: 84 | return 85 | fd.write(struct.pack(fmt.format(len(values)), values)) 86 | 87 | 88 | def read_bytes(fd, n, fmt=">{:d}s"): 89 | sz = struct.calcsize("s") 90 | return struct.unpack(fmt.format(n), fd.read(n * sz))[0] 91 | 92 | 93 | def write_ushorts(fd, values, fmt=">{:d}H"): 94 | fd.write(struct.pack(fmt.format(len(values)), *values)) 95 | 96 | 97 | def read_ushorts(fd, n, fmt=">{:d}H"): 98 | sz = struct.calcsize("H") 99 | return struct.unpack(fmt.format(n), fd.read(n * sz)) 100 | 101 | 102 | def encode_i(height, width, q_index, bit_stream, output): 103 | with Path(output).open("wb") as f: 104 | stream_length = len(bit_stream) 105 | 106 | write_uints(f, (height, width)) 107 | write_ushorts(f, (q_index,)) 108 | write_uints(f, (stream_length,)) 109 | write_bytes(f, bit_stream) 110 | 111 | 112 | def decode_i(inputpath): 113 | with Path(inputpath).open("rb") as f: 114 | header = read_uints(f, 2) 115 | height = header[0] 116 | width = header[1] 117 | q_index = read_ushorts(f, 1)[0] 118 | stream_length = read_uints(f, 1)[0] 119 | 120 | bit_stream = read_bytes(f, stream_length) 121 | 122 | return height, width, q_index, bit_stream 123 | 124 | 125 | def encode_p(string, mv_y_q_index, y_q_index, output): 126 | with Path(output).open("wb") as f: 127 | string_length = len(string) 128 | write_ushorts(f, (mv_y_q_index, y_q_index)) 129 | write_uints(f, (string_length,)) 130 | write_bytes(f, string) 131 | 132 | 133 | def decode_p(inputpath): 134 | with Path(inputpath).open("rb") as f: 135 | header = read_ushorts(f, 2) 136 | mv_y_q_index = header[0] 137 | y_q_index = header[1] 138 | 139 | header = read_uints(f, 1) 140 | string_length = header[0] 141 | string = read_bytes(f, string_length) 142 | 143 | return mv_y_q_index, y_q_index, string 144 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/cpp/3rdparty/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(pybind11) 2 | add_subdirectory(ryg_rans) -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/cpp/3rdparty/pybind11/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # set(PYBIND11_PYTHON_VERSION 3.8 CACHE STRING "") 2 | configure_file(CMakeLists.txt.in pybind11-download/CMakeLists.txt) 3 | execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . 4 | RESULT_VARIABLE result 5 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pybind11-download ) 6 | if(result) 7 | message(FATAL_ERROR "CMake step for pybind11 failed: ${result}") 8 | endif() 9 | execute_process(COMMAND ${CMAKE_COMMAND} --build . 10 | RESULT_VARIABLE result 11 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pybind11-download ) 12 | if(result) 13 | message(FATAL_ERROR "Build step for pybind11 failed: ${result}") 14 | endif() 15 | 16 | add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/pybind11-src/ 17 | ${CMAKE_CURRENT_BINARY_DIR}/pybind11-build/ 18 | EXCLUDE_FROM_ALL) 19 | 20 | set(PYBIND11_INCLUDE 21 | ${CMAKE_CURRENT_BINARY_DIR}/pybind11-src/include/ 22 | CACHE INTERNAL "") 23 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/cpp/3rdparty/pybind11/CMakeLists.txt.in: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.6.3) 2 | 3 | project(pybind11-download NONE) 4 | 5 | include(ExternalProject) 6 | if(IS_DIRECTORY "${PROJECT_BINARY_DIR}/3rdparty/pybind11/pybind11-src/include") 7 | ExternalProject_Add(pybind11 8 | GIT_REPOSITORY https://github.com/pybind/pybind11.git 9 | GIT_TAG v2.6.1 10 | GIT_SHALLOW 1 11 | SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/pybind11-src" 12 | BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/pybind11-build" 13 | DOWNLOAD_COMMAND "" 14 | UPDATE_COMMAND "" 15 | CONFIGURE_COMMAND "" 16 | BUILD_COMMAND "" 17 | INSTALL_COMMAND "" 18 | TEST_COMMAND "" 19 | ) 20 | else() 21 | ExternalProject_Add(pybind11 22 | GIT_REPOSITORY https://github.com/pybind/pybind11.git 23 | GIT_TAG v2.6.1 24 | GIT_SHALLOW 1 25 | SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/pybind11-src" 26 | BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/pybind11-build" 27 | UPDATE_COMMAND "" 28 | CONFIGURE_COMMAND "" 29 | BUILD_COMMAND "" 30 | INSTALL_COMMAND "" 31 | TEST_COMMAND "" 32 | ) 33 | endif() 34 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/cpp/3rdparty/ryg_rans/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | configure_file(CMakeLists.txt.in ryg_rans-download/CMakeLists.txt) 2 | execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . 3 | RESULT_VARIABLE result 4 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-download ) 5 | if(result) 6 | message(FATAL_ERROR "CMake step for ryg_rans failed: ${result}") 7 | endif() 8 | execute_process(COMMAND ${CMAKE_COMMAND} --build . 9 | RESULT_VARIABLE result 10 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-download ) 11 | if(result) 12 | message(FATAL_ERROR "Build step for ryg_rans failed: ${result}") 13 | endif() 14 | 15 | # add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-src/ 16 | # ${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-build 17 | # EXCLUDE_FROM_ALL) 18 | 19 | set(RYG_RANS_INCLUDE 20 | ${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-src/ 21 | CACHE INTERNAL "") 22 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/cpp/3rdparty/ryg_rans/CMakeLists.txt.in: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.6.3) 2 | 3 | project(ryg_rans-download NONE) 4 | 5 | include(ExternalProject) 6 | if(EXISTS "${PROJECT_BINARY_DIR}/3rdparty/ryg_rans/ryg_rans-src/rans64.h") 7 | ExternalProject_Add(ryg_rans 8 | GIT_REPOSITORY https://github.com/rygorous/ryg_rans.git 9 | GIT_TAG c9d162d996fd600315af9ae8eb89d832576cb32d 10 | GIT_SHALLOW 1 11 | SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-src" 12 | BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-build" 13 | DOWNLOAD_COMMAND "" 14 | UPDATE_COMMAND "" 15 | CONFIGURE_COMMAND "" 16 | BUILD_COMMAND "" 17 | INSTALL_COMMAND "" 18 | TEST_COMMAND "" 19 | ) 20 | else() 21 | ExternalProject_Add(ryg_rans 22 | GIT_REPOSITORY https://github.com/rygorous/ryg_rans.git 23 | GIT_TAG c9d162d996fd600315af9ae8eb89d832576cb32d 24 | GIT_SHALLOW 1 25 | SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-src" 26 | BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/ryg_rans-build" 27 | UPDATE_COMMAND "" 28 | CONFIGURE_COMMAND "" 29 | BUILD_COMMAND "" 30 | INSTALL_COMMAND "" 31 | TEST_COMMAND "" 32 | ) 33 | endif() 34 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/cpp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 3.6.3) 2 | project (ErrorRecovery) 3 | 4 | set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo" CACHE STRING "" FORCE) 5 | 6 | set(CMAKE_CXX_STANDARD 17) 7 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | set(CMAKE_CXX_EXTENSIONS OFF) 9 | 10 | # treat warning as error 11 | if (MSVC) 12 | add_compile_options(/W4 /WX) 13 | else() 14 | add_compile_options(-Wall -Wextra -pedantic -Werror) 15 | endif() 16 | 17 | # The sequence is tricky, put 3rd party first 18 | add_subdirectory(3rdparty) 19 | add_subdirectory (ops) 20 | add_subdirectory (rans) 21 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/cpp/ops/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.7) 2 | set(PROJECT_NAME MLCodec_CXX) 3 | project(${PROJECT_NAME}) 4 | 5 | set(cxx_source 6 | ops.cpp 7 | ) 8 | 9 | set(include_dirs 10 | ${CMAKE_CURRENT_SOURCE_DIR} 11 | ${PYBIND11_INCLUDE} 12 | ) 13 | 14 | pybind11_add_module(${PROJECT_NAME} ${cxx_source}) 15 | 16 | target_include_directories (${PROJECT_NAME} PUBLIC ${include_dirs}) 17 | 18 | # The post build argument is executed after make! 19 | add_custom_command( 20 | TARGET ${PROJECT_NAME} POST_BUILD 21 | COMMAND 22 | "${CMAKE_COMMAND}" -E copy 23 | "$" 24 | "${CMAKE_CURRENT_SOURCE_DIR}/../../entropy_models/" 25 | ) 26 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/cpp/ops/ops.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 InterDigital Communications, Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | std::vector pmf_to_quantized_cdf(const std::vector &pmf, 25 | int precision) { 26 | /* NOTE(begaintj): ported from `ryg_rans` public implementation. Not optimal 27 | * although it's only run once per model after training. See TF/compression 28 | * implementation for an optimized version. */ 29 | 30 | std::vector cdf(pmf.size() + 1); 31 | cdf[0] = 0; /* freq 0 */ 32 | 33 | std::transform(pmf.begin(), pmf.end(), cdf.begin() + 1, [=](float p) { 34 | return static_cast(std::round(p * (1 << precision)) + 0.5); 35 | }); 36 | 37 | const uint32_t total = std::accumulate(cdf.begin(), cdf.end(), 0); 38 | 39 | std::transform( 40 | cdf.begin(), cdf.end(), cdf.begin(), [precision, total](uint32_t p) { 41 | return static_cast((((1ull << precision) * p) / total)); 42 | }); 43 | 44 | std::partial_sum(cdf.begin(), cdf.end(), cdf.begin()); 45 | cdf.back() = 1 << precision; 46 | 47 | for (int i = 0; i < static_cast(cdf.size() - 1); ++i) { 48 | if (cdf[i] == cdf[i + 1]) { 49 | /* Try to steal frequency from low-frequency symbols */ 50 | uint32_t best_freq = ~0u; 51 | int best_steal = -1; 52 | for (int j = 0; j < static_cast(cdf.size()) - 1; ++j) { 53 | uint32_t freq = cdf[j + 1] - cdf[j]; 54 | if (freq > 1 && freq < best_freq) { 55 | best_freq = freq; 56 | best_steal = j; 57 | } 58 | } 59 | 60 | assert(best_steal != -1); 61 | 62 | if (best_steal < i) { 63 | for (int j = best_steal + 1; j <= i; ++j) { 64 | cdf[j]--; 65 | } 66 | } else { 67 | assert(best_steal > i); 68 | for (int j = i + 1; j <= best_steal; ++j) { 69 | cdf[j]++; 70 | } 71 | } 72 | } 73 | } 74 | 75 | assert(cdf[0] == 0); 76 | assert(cdf.back() == (1u << precision)); 77 | for (int i = 0; i < static_cast(cdf.size()) - 1; ++i) { 78 | assert(cdf[i + 1] > cdf[i]); 79 | } 80 | 81 | return cdf; 82 | } 83 | 84 | PYBIND11_MODULE(MLCodec_CXX, m) { 85 | m.attr("__name__") = "MLCodec_CXX"; 86 | 87 | m.doc() = "C++ utils"; 88 | 89 | m.def("pmf_to_quantized_cdf", &pmf_to_quantized_cdf, 90 | "Return quantized CDF for a given PMF"); 91 | } 92 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/cpp/rans/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.7) 2 | set(PROJECT_NAME MLCodec_rans) 3 | project(${PROJECT_NAME}) 4 | 5 | set(rans_source 6 | rans_interface.hpp 7 | rans_interface.cpp 8 | ) 9 | 10 | set(include_dirs 11 | ${CMAKE_CURRENT_SOURCE_DIR} 12 | ${PYBIND11_INCLUDE} 13 | ${RYG_RANS_INCLUDE} 14 | ) 15 | 16 | pybind11_add_module(${PROJECT_NAME} ${rans_source}) 17 | 18 | target_include_directories (${PROJECT_NAME} PUBLIC ${include_dirs}) 19 | 20 | # The post build argument is executed after make! 21 | add_custom_command( 22 | TARGET ${PROJECT_NAME} POST_BUILD 23 | COMMAND 24 | "${CMAKE_COMMAND}" -E copy 25 | "$" 26 | "${CMAKE_CURRENT_SOURCE_DIR}/../../entropy_models/" 27 | ) 28 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/cpp/rans/rans_interface.hpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 InterDigital Communications, Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #pragma once 17 | 18 | #include 19 | #include 20 | 21 | #ifdef __GNUC__ 22 | #pragma GCC diagnostic push 23 | #pragma GCC diagnostic ignored "-Wpedantic" 24 | #pragma GCC diagnostic ignored "-Wsign-compare" 25 | #elif _MSC_VER 26 | #pragma warning(push, 0) 27 | #endif 28 | 29 | #include 30 | 31 | #ifdef __GNUC__ 32 | #pragma GCC diagnostic pop 33 | #elif _MSC_VER 34 | #pragma warning(pop) 35 | #endif 36 | 37 | namespace py = pybind11; 38 | 39 | struct RansSymbol { 40 | uint16_t start; 41 | uint16_t range; 42 | bool bypass; // bypass flag to write raw bits to the stream 43 | }; 44 | 45 | /* NOTE: Warning, we buffer everything for now... In case of large files we 46 | * should split the bitstream into chunks... Or for a memory-bounded encoder 47 | **/ 48 | class BufferedRansEncoder { 49 | public: 50 | BufferedRansEncoder() = default; 51 | 52 | BufferedRansEncoder(const BufferedRansEncoder &) = delete; 53 | BufferedRansEncoder(BufferedRansEncoder &&) = delete; 54 | BufferedRansEncoder &operator=(const BufferedRansEncoder &) = delete; 55 | BufferedRansEncoder &operator=(BufferedRansEncoder &&) = delete; 56 | 57 | void encode_with_indexes(const std::vector &symbols, 58 | const std::vector &indexes, 59 | const std::vector> &cdfs, 60 | const std::vector &cdfs_sizes, 61 | const std::vector &offsets); 62 | py::bytes flush(); 63 | 64 | private: 65 | std::vector _syms; 66 | }; 67 | 68 | class RansEncoder { 69 | public: 70 | RansEncoder() = default; 71 | 72 | RansEncoder(const RansEncoder &) = delete; 73 | RansEncoder(RansEncoder &&) = delete; 74 | RansEncoder &operator=(const RansEncoder &) = delete; 75 | RansEncoder &operator=(RansEncoder &&) = delete; 76 | 77 | py::bytes encode_with_indexes(const std::vector &symbols, 78 | const std::vector &indexes, 79 | const std::vector> &cdfs, 80 | const std::vector &cdfs_sizes, 81 | const std::vector &offsets); 82 | }; 83 | 84 | class RansDecoder { 85 | public: 86 | RansDecoder() = default; 87 | 88 | RansDecoder(const RansDecoder &) = delete; 89 | RansDecoder(RansDecoder &&) = delete; 90 | RansDecoder &operator=(const RansDecoder &) = delete; 91 | RansDecoder &operator=(RansDecoder &&) = delete; 92 | 93 | std::vector 94 | decode_with_indexes(const std::string &encoded, 95 | const std::vector &indexes, 96 | const std::vector> &cdfs, 97 | const std::vector &cdfs_sizes, 98 | const std::vector &offsets); 99 | 100 | void set_stream(const std::string &stream); 101 | 102 | std::vector 103 | decode_stream(const std::vector &indexes, 104 | const std::vector> &cdfs, 105 | const std::vector &cdfs_sizes, 106 | const std::vector &offsets); 107 | 108 | 109 | private: 110 | Rans64State _rans; 111 | std::string _stream; 112 | uint32_t *_ptr; 113 | }; 114 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/layers/gdn.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import torch 16 | import torch.nn as nn 17 | import torch.nn.functional as F 18 | 19 | from ..ops.parametrizers import NonNegativeParametrizer 20 | 21 | 22 | class GDN(nn.Module): 23 | r"""Generalized Divisive Normalization layer. 24 | 25 | Introduced in `"Density Modeling of Images Using a Generalized Normalization 26 | Transformation" `_, 27 | by Balle Johannes, Valero Laparra, and Eero P. Simoncelli, (2016). 28 | 29 | .. math:: 30 | 31 | y[i] = \frac{x[i]}{\sqrt{\beta[i] + \sum_j(\gamma[j, i] * x[j]^2)}} 32 | 33 | """ 34 | 35 | def __init__(self, in_channels, inverse=False, beta_min=1e-6, gamma_init=0.1): 36 | super().__init__() 37 | 38 | beta_min = float(beta_min) 39 | gamma_init = float(gamma_init) 40 | self.inverse = bool(inverse) 41 | 42 | self.beta_reparam = NonNegativeParametrizer(minimum=beta_min) 43 | beta = torch.ones(in_channels) 44 | beta = self.beta_reparam.init(beta) 45 | self.beta = nn.Parameter(beta) 46 | 47 | self.gamma_reparam = NonNegativeParametrizer() 48 | gamma = gamma_init * torch.eye(in_channels) 49 | gamma = self.gamma_reparam.init(gamma) 50 | self.gamma = nn.Parameter(gamma) 51 | 52 | def forward(self, x): 53 | _, C, _, _ = x.size() 54 | 55 | beta = self.beta_reparam(self.beta) 56 | gamma = self.gamma_reparam(self.gamma) 57 | gamma = gamma.reshape(C, C, 1, 1) 58 | norm = F.conv2d(x ** 2, gamma, beta) 59 | 60 | if self.inverse: 61 | norm = torch.sqrt(norm) 62 | else: 63 | norm = torch.rsqrt(norm) 64 | 65 | out = x * norm 66 | 67 | return out 68 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/layers/layers.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import torch 16 | import torch.nn as nn 17 | 18 | from .gdn import GDN 19 | 20 | 21 | class MaskedConv2d(nn.Conv2d): 22 | r"""Masked 2D convolution implementation, mask future "unseen" pixels. 23 | Useful for building auto-regressive network components. 24 | 25 | Introduced in `"Conditional Image Generation with PixelCNN Decoders" 26 | `_. 27 | 28 | Inherits the same arguments as a `nn.Conv2d`. Use `mask_type='A'` for the 29 | first layer (which also masks the "current pixel"), `mask_type='B'` for the 30 | following layers. 31 | """ 32 | 33 | def __init__(self, *args, mask_type="A", **kwargs): 34 | super().__init__(*args, **kwargs) 35 | 36 | if mask_type not in ("A", "B"): 37 | raise ValueError(f'Invalid "mask_type" value "{mask_type}"') 38 | 39 | self.register_buffer("mask", torch.ones_like(self.weight.data)) 40 | _, _, h, w = self.mask.size() 41 | self.mask[:, :, h // 2, w // 2 + (mask_type == "B"):] = 0 42 | self.mask[:, :, h // 2 + 1:] = 0 43 | 44 | def forward(self, x): 45 | # TODO(begaintj): weight assigment is not supported by torchscript 46 | self.weight.data *= self.mask 47 | return super().forward(x) 48 | 49 | 50 | def conv3x3(in_ch, out_ch, stride=1): 51 | """3x3 convolution with padding.""" 52 | return nn.Conv2d(in_ch, out_ch, kernel_size=3, stride=stride, padding=1) 53 | 54 | 55 | def subpel_conv3x3(in_ch, out_ch, r=1): 56 | """3x3 sub-pixel convolution for up-sampling.""" 57 | return nn.Sequential( 58 | nn.Conv2d(in_ch, out_ch * r ** 2, kernel_size=3, padding=1), nn.PixelShuffle(r) 59 | ) 60 | 61 | 62 | def conv1x1(in_ch, out_ch, stride=1): 63 | """1x1 convolution.""" 64 | return nn.Conv2d(in_ch, out_ch, kernel_size=1, stride=stride) 65 | 66 | 67 | class ResidualBlockWithStride(nn.Module): 68 | """Residual block with a stride on the first convolution. 69 | 70 | Args: 71 | in_ch (int): number of input channels 72 | out_ch (int): number of output channels 73 | stride (int): stride value (default: 2) 74 | """ 75 | 76 | def __init__(self, in_ch, out_ch, stride=2): 77 | super().__init__() 78 | self.conv1 = conv3x3(in_ch, out_ch, stride=stride) 79 | self.leaky_relu = nn.LeakyReLU(inplace=True) 80 | self.conv2 = conv3x3(out_ch, out_ch) 81 | self.gdn = GDN(out_ch) 82 | if stride != 1: 83 | self.downsample = conv1x1(in_ch, out_ch, stride=stride) 84 | else: 85 | self.downsample = None 86 | 87 | def forward(self, x): 88 | identity = x 89 | out = self.conv1(x) 90 | out = self.leaky_relu(out) 91 | out = self.conv2(out) 92 | out = self.gdn(out) 93 | 94 | if self.downsample is not None: 95 | identity = self.downsample(x) 96 | 97 | out += identity 98 | return out 99 | 100 | 101 | class ResidualBlockUpsample(nn.Module): 102 | """Residual block with sub-pixel upsampling on the last convolution. 103 | 104 | Args: 105 | in_ch (int): number of input channels 106 | out_ch (int): number of output channels 107 | upsample (int): upsampling factor (default: 2) 108 | """ 109 | 110 | def __init__(self, in_ch, out_ch, upsample=2): 111 | super().__init__() 112 | self.subpel_conv = subpel_conv3x3(in_ch, out_ch, upsample) 113 | self.leaky_relu = nn.LeakyReLU(inplace=True) 114 | self.conv = conv3x3(out_ch, out_ch) 115 | self.igdn = GDN(out_ch, inverse=True) 116 | self.upsample = subpel_conv3x3(in_ch, out_ch, upsample) 117 | 118 | def forward(self, x): 119 | identity = x 120 | out = self.subpel_conv(x) 121 | out = self.leaky_relu(out) 122 | out = self.conv(out) 123 | out = self.igdn(out) 124 | identity = self.upsample(x) 125 | out += identity 126 | return out 127 | 128 | 129 | class ResidualBlock(nn.Module): 130 | """Simple residual block with two 3x3 convolutions. 131 | 132 | Args: 133 | in_ch (int): number of input channels 134 | out_ch (int): number of output channels 135 | """ 136 | 137 | def __init__(self, in_ch, out_ch): 138 | super().__init__() 139 | self.conv1 = conv3x3(in_ch, out_ch) 140 | self.leaky_relu = nn.LeakyReLU(inplace=True) 141 | self.conv2 = conv3x3(out_ch, out_ch) 142 | 143 | def forward(self, x): 144 | identity = x 145 | 146 | out = self.conv1(x) 147 | out = self.leaky_relu(out) 148 | out = self.conv2(out) 149 | out = self.leaky_relu(out) 150 | 151 | out = out + identity 152 | return out -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/models/utils.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import torch 16 | import torch.nn as nn 17 | 18 | 19 | def find_named_module(module, query): 20 | """Helper function to find a named module. Returns a `nn.Module` or `None` 21 | 22 | Args: 23 | module (nn.Module): the root module 24 | query (str): the module name to find 25 | 26 | Returns: 27 | nn.Module or None 28 | """ 29 | 30 | return next((m for n, m in module.named_modules() if n == query), None) 31 | 32 | 33 | def find_named_buffer(module, query): 34 | """Helper function to find a named buffer. Returns a `torch.Tensor` or `None` 35 | 36 | Args: 37 | module (nn.Module): the root module 38 | query (str): the buffer name to find 39 | 40 | Returns: 41 | torch.Tensor or None 42 | """ 43 | return next((b for n, b in module.named_buffers() if n == query), None) 44 | 45 | 46 | def _update_registered_buffer( 47 | module, 48 | buffer_name, 49 | state_dict_key, 50 | state_dict, 51 | policy="resize_if_empty", 52 | dtype=torch.int, 53 | ): 54 | new_size = state_dict[state_dict_key].size() 55 | registered_buf = find_named_buffer(module, buffer_name) 56 | 57 | if policy in ("resize_if_empty", "resize"): 58 | if registered_buf is None: 59 | raise RuntimeError(f'buffer "{buffer_name}" was not registered') 60 | 61 | if policy == "resize" or registered_buf.numel() == 0: 62 | registered_buf.resize_(new_size) 63 | 64 | elif policy == "register": 65 | if registered_buf is not None: 66 | raise RuntimeError(f'buffer "{buffer_name}" was already registered') 67 | 68 | module.register_buffer(buffer_name, torch.empty(new_size, dtype=dtype).fill_(0)) 69 | 70 | else: 71 | raise ValueError(f'Invalid policy "{policy}"') 72 | 73 | 74 | def update_registered_buffers( 75 | module, 76 | module_name, 77 | buffer_names, 78 | state_dict, 79 | policy="resize_if_empty", 80 | dtype=torch.int, 81 | ): 82 | """Update the registered buffers in a module according to the tensors sized 83 | in a state_dict. 84 | 85 | (There's no way in torch to directly load a buffer with a dynamic size) 86 | 87 | Args: 88 | module (nn.Module): the module 89 | module_name (str): module name in the state dict 90 | buffer_names (list(str)): list of the buffer names to resize in the module 91 | state_dict (dict): the state dict 92 | policy (str): Update policy, choose from 93 | ('resize_if_empty', 'resize', 'register') 94 | dtype (dtype): Type of buffer to be registered (when policy is 'register') 95 | """ 96 | valid_buffer_names = [n for n, _ in module.named_buffers()] 97 | for buffer_name in buffer_names: 98 | if buffer_name not in valid_buffer_names: 99 | raise ValueError(f'Invalid buffer name "{buffer_name}"') 100 | 101 | for buffer_name in buffer_names: 102 | _update_registered_buffer( 103 | module, 104 | buffer_name, 105 | f"{module_name}.{buffer_name}", 106 | state_dict, 107 | policy, 108 | dtype, 109 | ) 110 | 111 | 112 | def conv(in_channels, out_channels, kernel_size=5, stride=2): 113 | return nn.Conv2d( 114 | in_channels, 115 | out_channels, 116 | kernel_size=kernel_size, 117 | stride=stride, 118 | padding=kernel_size // 2, 119 | ) 120 | 121 | 122 | def deconv(in_channels, out_channels, kernel_size=5, stride=2): 123 | return nn.ConvTranspose2d( 124 | in_channels, 125 | out_channels, 126 | kernel_size=kernel_size, 127 | stride=stride, 128 | output_padding=stride - 1, 129 | padding=kernel_size // 2, 130 | ) 131 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/models/waseda.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import torch.nn as nn 16 | 17 | from ..layers.layers import ( 18 | ResidualBlock, 19 | ResidualBlockUpsample, 20 | ResidualBlockWithStride, 21 | conv3x3, 22 | subpel_conv3x3, 23 | ) 24 | 25 | from .priors import JointAutoregressiveHierarchicalPriors 26 | 27 | 28 | class Cheng2020Anchor(JointAutoregressiveHierarchicalPriors): 29 | """Anchor model variant from `"Learned Image Compression with 30 | Discretized Gaussian Mixture Likelihoods and Attention Modules" 31 | `_, by Zhengxue Cheng, Heming Sun, Masaru 32 | Takeuchi, Jiro Katto. 33 | 34 | Uses residual blocks with small convolutions (3x3 and 1x1), and sub-pixel 35 | convolutions for up-sampling. 36 | 37 | Args: 38 | N (int): Number of channels 39 | """ 40 | 41 | def __init__(self, N=192, **kwargs): 42 | super().__init__(N=N, M=N, **kwargs) 43 | 44 | self.g_a = nn.Sequential( 45 | ResidualBlockWithStride(3, N, stride=2), 46 | ResidualBlock(N, N), 47 | ResidualBlockWithStride(N, N, stride=2), 48 | ResidualBlock(N, N), 49 | ResidualBlockWithStride(N, N, stride=2), 50 | ResidualBlock(N, N), 51 | conv3x3(N, N, stride=2), 52 | ) 53 | 54 | self.h_a = nn.Sequential( 55 | conv3x3(N, N), 56 | nn.LeakyReLU(inplace=True), 57 | conv3x3(N, N), 58 | nn.LeakyReLU(inplace=True), 59 | conv3x3(N, N, stride=2), 60 | nn.LeakyReLU(inplace=True), 61 | conv3x3(N, N), 62 | nn.LeakyReLU(inplace=True), 63 | conv3x3(N, N, stride=2), 64 | ) 65 | 66 | self.h_s = nn.Sequential( 67 | conv3x3(N, N), 68 | nn.LeakyReLU(inplace=True), 69 | subpel_conv3x3(N, N, 2), 70 | nn.LeakyReLU(inplace=True), 71 | conv3x3(N, N * 3 // 2), 72 | nn.LeakyReLU(inplace=True), 73 | subpel_conv3x3(N * 3 // 2, N * 3 // 2, 2), 74 | nn.LeakyReLU(inplace=True), 75 | conv3x3(N * 3 // 2, N * 2), 76 | ) 77 | 78 | self.g_s = nn.Sequential( 79 | ResidualBlock(N, N), 80 | ResidualBlockUpsample(N, N, 2), 81 | ResidualBlock(N, N), 82 | ResidualBlockUpsample(N, N, 2), 83 | ResidualBlock(N, N), 84 | ResidualBlockUpsample(N, N, 2), 85 | ResidualBlock(N, N), 86 | subpel_conv3x3(N, 3, 2), 87 | ) 88 | 89 | @classmethod 90 | def from_state_dict(cls, state_dict): 91 | """Return a new model instance from `state_dict`.""" 92 | N = state_dict["g_a.0.conv1.weight"].size(0) 93 | net = cls(N) 94 | net.load_state_dict(state_dict) 95 | return net -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/ops/bound_ops.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import torch 16 | import torch.nn as nn 17 | 18 | 19 | class LowerBoundFunction(torch.autograd.Function): 20 | """Autograd function for the `LowerBound` operator.""" 21 | 22 | @staticmethod 23 | def forward(ctx, input_, bound): 24 | ctx.save_for_backward(input_, bound) 25 | return torch.max(input_, bound) 26 | 27 | @staticmethod 28 | def backward(ctx, grad_output): 29 | input_, bound = ctx.saved_tensors 30 | pass_through_if = (input_ >= bound) | (grad_output < 0) 31 | return pass_through_if.type(grad_output.dtype) * grad_output, None 32 | 33 | 34 | class LowerBound(nn.Module): 35 | """Lower bound operator, computes `torch.max(x, bound)` with a custom 36 | gradient. 37 | 38 | The derivative is replaced by the identity function when `x` is moved 39 | towards the `bound`, otherwise the gradient is kept to zero. 40 | """ 41 | 42 | def __init__(self, bound): 43 | super().__init__() 44 | self.register_buffer("bound", torch.Tensor([float(bound)])) 45 | 46 | @torch.jit.unused 47 | def lower_bound(self, x): 48 | return LowerBoundFunction.apply(x, self.bound) 49 | 50 | def forward(self, x): 51 | if torch.jit.is_scripting(): 52 | return torch.max(x, self.bound) 53 | return self.lower_bound(x) 54 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/ops/parametrizers.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import torch 16 | import torch.nn as nn 17 | 18 | from .bound_ops import LowerBound 19 | 20 | 21 | class NonNegativeParametrizer(nn.Module): 22 | """ 23 | Non negative reparametrization. 24 | 25 | Used for stability during training. 26 | """ 27 | 28 | def __init__(self, minimum=0, reparam_offset=2 ** -18): 29 | super().__init__() 30 | 31 | self.minimum = float(minimum) 32 | self.reparam_offset = float(reparam_offset) 33 | 34 | pedestal = self.reparam_offset ** 2 35 | self.register_buffer("pedestal", torch.Tensor([pedestal])) 36 | bound = (self.minimum + self.reparam_offset ** 2) ** 0.5 37 | self.lower_bound = LowerBound(bound) 38 | 39 | def init(self, x): 40 | return torch.sqrt(torch.max(x + self.pedestal, self.pedestal)) 41 | 42 | def forward(self, x): 43 | out = self.lower_bound(x) 44 | out = out ** 2 - self.pedestal 45 | return out 46 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/utils/common.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | 4 | def str2bool(v): 5 | if isinstance(v, bool): 6 | return v 7 | if v.lower() in ('yes', 'true', 't', 'y', '1'): 8 | return True 9 | elif v.lower() in ('no', 'false', 'f', 'n', '0'): 10 | return False 11 | else: 12 | raise argparse.ArgumentTypeError('Boolean value expected.') 13 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/utils/stream_helper.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import struct 16 | from pathlib import Path 17 | import torch 18 | import torch.nn.functional as F 19 | from PIL import Image 20 | from torchvision.transforms import ToPILImage, ToTensor 21 | 22 | 23 | def get_downsampled_shape(height, width, p): 24 | 25 | new_h = (height + p - 1) // p * p 26 | new_w = (width + p - 1) // p * p 27 | return int(new_h / p + 0.5), int(new_w / p + 0.5) 28 | 29 | 30 | def filesize(filepath: str) -> int: 31 | if not Path(filepath).is_file(): 32 | raise ValueError(f'Invalid file "{filepath}".') 33 | return Path(filepath).stat().st_size 34 | 35 | 36 | def load_image(filepath: str) -> Image.Image: 37 | return Image.open(filepath).convert("RGB") 38 | 39 | 40 | def img2torch(img: Image.Image) -> torch.Tensor: 41 | return ToTensor()(img).unsqueeze(0) 42 | 43 | 44 | def torch2img(x: torch.Tensor) -> Image.Image: 45 | return ToPILImage()(x.clamp_(0, 1).squeeze()) 46 | 47 | 48 | def write_uints(fd, values, fmt=">{:d}I"): 49 | fd.write(struct.pack(fmt.format(len(values)), *values)) 50 | 51 | 52 | def write_uchars(fd, values, fmt=">{:d}B"): 53 | fd.write(struct.pack(fmt.format(len(values)), *values)) 54 | 55 | 56 | def read_uints(fd, n, fmt=">{:d}I"): 57 | sz = struct.calcsize("I") 58 | return struct.unpack(fmt.format(n), fd.read(n * sz)) 59 | 60 | 61 | def read_uchars(fd, n, fmt=">{:d}B"): 62 | sz = struct.calcsize("B") 63 | return struct.unpack(fmt.format(n), fd.read(n * sz)) 64 | 65 | 66 | def write_bytes(fd, values, fmt=">{:d}s"): 67 | if len(values) == 0: 68 | return 69 | fd.write(struct.pack(fmt.format(len(values)), values)) 70 | 71 | 72 | def read_bytes(fd, n, fmt=">{:d}s"): 73 | sz = struct.calcsize("s") 74 | return struct.unpack(fmt.format(n), fd.read(n * sz))[0] 75 | 76 | 77 | def pad(x, p=2 ** 6): 78 | h, w = x.size(2), x.size(3) 79 | H = (h + p - 1) // p * p 80 | W = (w + p - 1) // p * p 81 | padding_left = (W - w) // 2 82 | padding_right = W - w - padding_left 83 | padding_top = (H - h) // 2 84 | padding_bottom = H - h - padding_top 85 | return F.pad( 86 | x, 87 | (padding_left, padding_right, padding_top, padding_bottom), 88 | mode="constant", 89 | value=0, 90 | ) 91 | 92 | 93 | def crop(x, size): 94 | H, W = x.size(2), x.size(3) 95 | h, w = size 96 | padding_left = (W - w) // 2 97 | padding_right = W - w - padding_left 98 | padding_top = (H - h) // 2 99 | padding_bottom = H - h - padding_top 100 | return F.pad( 101 | x, 102 | (-padding_left, -padding_right, -padding_top, -padding_bottom), 103 | mode="constant", 104 | value=0, 105 | ) 106 | 107 | 108 | def encode_i(height, width, y_string, z_string, output): 109 | with Path(output).open("wb") as f: 110 | y_string_length = len(y_string) 111 | z_string_length = len(z_string) 112 | 113 | write_uints(f, (height, width, y_string_length, z_string_length)) 114 | write_bytes(f, y_string) 115 | write_bytes(f, z_string) 116 | 117 | 118 | def decode_i(inputpath): 119 | with Path(inputpath).open("rb") as f: 120 | header = read_uints(f, 4) 121 | height = header[0] 122 | width = header[1] 123 | y_string_length = header[2] 124 | z_string_length = header[3] 125 | 126 | y_string = read_bytes(f, y_string_length) 127 | z_string = read_bytes(f, z_string_length) 128 | 129 | return height, width, y_string, z_string 130 | 131 | 132 | def encode_p(height, width, mv_y_string, mv_z_string, y_string, z_string, output): 133 | with Path(output).open("wb") as f: 134 | mv_y_string_length = len(mv_y_string) 135 | mv_z_string_length = len(mv_z_string) 136 | y_string_length = len(y_string) 137 | z_string_length = len(z_string) 138 | 139 | write_uints(f, (height, width, 140 | mv_y_string_length, mv_z_string_length, 141 | y_string_length, z_string_length)) 142 | write_bytes(f, mv_y_string) 143 | write_bytes(f, mv_z_string) 144 | write_bytes(f, y_string) 145 | write_bytes(f, z_string) 146 | 147 | 148 | def decode_p(inputpath): 149 | with Path(inputpath).open("rb") as f: 150 | header = read_uints(f, 6) 151 | height = header[0] 152 | width = header[1] 153 | mv_y_string_length = header[2] 154 | mv_z_string_length = header[3] 155 | y_string_length = header[4] 156 | z_string_length = header[5] 157 | 158 | mv_y_string = read_bytes(f, mv_y_string_length) 159 | mv_z_string = read_bytes(f, mv_z_string_length) 160 | y_string = read_bytes(f, y_string_length) 161 | z_string = read_bytes(f, z_string_length) 162 | 163 | return height, width, mv_y_string, mv_z_string, y_string, z_string 164 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/I_model/zoo/image.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 InterDigital Communications, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from ..models.waseda import ( 16 | Cheng2020Anchor 17 | ) 18 | 19 | from ..models.priors import ( 20 | FactorizedPrior, 21 | ScaleHyperprior, 22 | MeanScaleHyperprior, 23 | JointAutoregressiveHierarchicalPriors 24 | ) 25 | 26 | model_architectures = { 27 | "bmshj2018-factorized": FactorizedPrior, 28 | "bmshj2018-hyperprior": ScaleHyperprior, 29 | "mbt2018-mean": MeanScaleHyperprior, 30 | "mbt2018": JointAutoregressiveHierarchicalPriors, 31 | "cheng2020-anchor": Cheng2020Anchor, 32 | } 33 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/Readme.md: -------------------------------------------------------------------------------- 1 | ## Code for Bit Allocation using Optimization Sec 5.2 2 | 3 | ## Requirements 4 | * python==3.8 5 | * pytorch==1.9 6 | * tqdm 7 | * compressai 8 | * PIL 9 | 10 | ## Dataset for testing 11 | * the dataset required is HEVC CTC Class BCDE and UVG, the sequences are as follows: 12 | * Class B: BasketballDrive_1920x1080_50.yuv, BQTerrace_1920x1080_60.yuv, Cactus_1920x1080_50.yuv, Kimono1_1920x1080_24.yuv, ParkScene_1920x1080_24.yuv 13 | * Class C: BasketballDrill_832x480_50.yuv, BQMall_832x480_60.yuv, PartyScene_832x480_50.yuv, RaceHorses_832x480_30.yuv 14 | * Class D: BasketballPass_416x240_50.yuv, BlowingBubbles_416x240_50.yuv, BQSquare_416x240_60.yuv, RaceHorses_416x240_30.yuv 15 | * Class E: FourPeople_1280x720_60.yuv, Johnny_1280x720_60.yuv, KristenAndSara_1280x720_60.yuv 16 | * UVG: Beauty_1920x1080_120fps_420_8bit_YUV.yuv, Jockey_1920x1080_120fps_420_8bit_YUV.yuv, YachtRide_1920x1080_120fps_420_8bit_YUV.yuv, Bosphorus_1920x1080_120fps_420_8bit_YUV.yuv, ReadySteadyGo_1920x1080_120fps_420_8bit_YUV.yuv, HoneyBee_1920x1080_120fps_420_8bit_YUV.yuv, ShakeNDry_1920x1080_120fps_420_8bit_YUV.yuv 17 | * they can be obtained from: https://hevc.hhi.fraunhofer.de/ and https://ultravideo.fi/#main 18 | * the dataset need to be splitted into png images and cut into multiplier of 64, we recommend the following command with ffmpeg 19 | ```bash 20 | ffmpeg -pix_fmt yuv420p -s $W2x$H2 -i $Sequence_name.yuv -vf crop=$W2:$H2:0:0 $Sequence_name/im%03d.png 21 | ``` 22 | * where H2, W2 are the cutted height and width into multiplier of 64. the path of png images should also be set when running test, see details in each folder 23 | * after that, set the root path of dataset in dataset.py line 19 and 59 24 | 25 | ## Pre-trained Baseline Model 26 | 27 | * Download following pre-trained models and put them into ./Checkpoints folder: 28 | * DVC: https://drive.google.com/drive/folders/1M54MPrAzaA0QVySnzUu9HZWx1bfIrTZ6), then rename them to ```DVC_{\lambda}.pth```; 29 | * DCVC: https://onedrive.live.com/redir?resid=2866592D5C55DF8C!1198&authkey=!AGZwZffbRsVcjSQ&e=iMeykH, then rename them to ```DCVC_{\lambda}.pth```; 30 | * HSTEM: https://1drv.ms/u/s!AozfVVwtWWYoiUAGk6xr-oELbodn?e=kry2Nk; 31 | * About intra coding for DVC and DCVC, download pre-trained models of Cheng et al. 2020 from CompressaAI via 32 | ```bash 33 | python -u ./Checkpoints/download_compressai_models.py 34 | ``` 35 | 36 | ## Perform bit allocation: Proposed-Scalable and Proposed-Approx 37 | 38 | * we provide code for proposed bit allocation method on DVC (Lu et al. 2019), DCVC (Li et al. 2021) and HSTEM (Li et al. 2022). 39 | * For example, the following instruction is used for testing Proposed-Scalable with DVC on HEVC Class D dataset: 40 | ``` bash 41 | python main.py --test_model="DVC" --test_lambdas=(2048, 1024, 512, 256) --factor=-1 --overlap=0 --test_class="HEVC_D" --gop_size=10 --test_gop_num=1, --optimize_range=1 42 | ``` 43 | * the gop_size parameter should be set to 10 for HEVC BCDE dataset, and 12 for UVG dataset 44 | * the optimize_range parameter is just the C parameter in Sec. 4.2. Setting it < gop size makes Proposed-Scalable, and set it == gop size makes Proposed-Approx. To reproduce the result in Tab. 1, use optimize_range = 2 45 | * As for DCVC (Li et al. 2021) or HSTEM, you only need to change the setting `--test_model="DCVC"` or `--test_model="HSTEM"`. 46 | * for more detailed CLI, use: 47 | ```bash 48 | python main.py --help 49 | ``` 50 | * If it occupies more memory than the maximum capacity of GPU, you can crop the input frame by changing `--factor` and `--overlap`. Here we don't provide the cropping mode for DVC because it's model size is very small. And if it's necessary to crop the input frame for DVC, please refer to the practice of DCVC or HSTEM in `main.py`. 51 | -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/Tools/filelists/B.txt: -------------------------------------------------------------------------------- 1 | BasketballDrive_1920x1024_50 2 | BQTerrace_1920x1024_60 3 | Cactus_1920x1024_50 4 | Kimono1_1920x1024_24 5 | ParkScene_1920x1024_24 -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/Tools/filelists/C.txt: -------------------------------------------------------------------------------- 1 | BasketballDrill_832x448_50 2 | BQMall_832x448_60 3 | PartyScene_832x448_50 4 | RaceHorses_832x448_30 -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/Tools/filelists/D.txt: -------------------------------------------------------------------------------- 1 | BasketballPass_416x240_50 2 | BlowingBubbles_416x240_50 3 | BQSquare_416x240_60 4 | RaceHorses_416x240_30 -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/Tools/filelists/E.txt: -------------------------------------------------------------------------------- 1 | FourPeople_1280x704_60 2 | KristenAndSara_1280x704_60 3 | Johnny_1280x704_60 -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/Tools/filelists/UVG.txt: -------------------------------------------------------------------------------- 1 | Beauty 2 | Bosphorus 3 | HoneyBee 4 | Jockey 5 | ReadySteadyGo 6 | ShakeNDry 7 | YachtRide -------------------------------------------------------------------------------- /5.2. Evaluation on Bit Allocation/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Tongda Xu and Han Gao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | ## Source code for Bit Allocation Using Optimization 2 | * each subfolder contains the source code to reproduce experimental results of each section 3 | * for details, see Readme.md in each subfolder --------------------------------------------------------------------------------