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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"import numpy as np\n",
|
| 10 |
+
"def load_binary_masks(bin_file_path):\n",
|
| 11 |
+
" with open(bin_file_path, 'rb') as f:\n",
|
| 12 |
+
" shape = np.fromfile(f, dtype=np.int32, count=3)\n",
|
| 13 |
+
" # 读取掩码数据,使用uint16类型\n",
|
| 14 |
+
" binary_masks = np.fromfile(f, dtype=np.uint16)\n",
|
| 15 |
+
" binary_masks = binary_masks.reshape(shape)\n",
|
| 16 |
+
" return binary_masks"
|
| 17 |
+
]
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "code",
|
| 21 |
+
"execution_count": 19,
|
| 22 |
+
"metadata": {},
|
| 23 |
+
"outputs": [],
|
| 24 |
+
"source": [
|
| 25 |
+
"orig= load_binary_masks('/nfs/dataset-ofs-voyager-research/shichen/project/video_diffusion/ConsisID/workdirs/step4_track_masks_data/515d576284baf2cb5ecc534f3105f3fb_0_107/tracking_mask_results/1/masks.bin')"
|
| 26 |
+
]
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"cell_type": "code",
|
| 30 |
+
"execution_count": 20,
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"outputs": [],
|
| 33 |
+
"source": [
|
| 34 |
+
"orig = orig.astype(np.uint8)"
|
| 35 |
+
]
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"cell_type": "code",
|
| 39 |
+
"execution_count": 24,
|
| 40 |
+
"metadata": {},
|
| 41 |
+
"outputs": [],
|
| 42 |
+
"source": [
|
| 43 |
+
"import numpy as np\n",
|
| 44 |
+
"import blosc\n",
|
| 45 |
+
"import os\n",
|
| 46 |
+
"\n",
|
| 47 |
+
"def compress_3d_direct(binary_volume, output_file='3d_direct.bin'):\n",
|
| 48 |
+
" \"\"\"直接压缩整个3D体积\"\"\"\n",
|
| 49 |
+
" # 先用np.packbits进行基础压缩\n",
|
| 50 |
+
" packed = np.packbits(binary_volume)\n",
|
| 51 |
+
" \n",
|
| 52 |
+
" # 使用专门的压缩算法\n",
|
| 53 |
+
" compressed = blosc.compress(packed.tobytes(), \n",
|
| 54 |
+
" typesize=1, \n",
|
| 55 |
+
" cname='zstd', \n",
|
| 56 |
+
" clevel=9,\n",
|
| 57 |
+
" shuffle=blosc.BITSHUFFLE) # 位级混排,对二值数据更有效\n",
|
| 58 |
+
" \n",
|
| 59 |
+
" # 保存压缩数据和元数据\n",
|
| 60 |
+
" with open(output_file, 'wb') as f:\n",
|
| 61 |
+
" # 保存元数据(形状)\n",
|
| 62 |
+
" shape_info = np.array(binary_volume.shape, dtype=np.int32)\n",
|
| 63 |
+
" f.write(shape_info.tobytes())\n",
|
| 64 |
+
" # 保存压缩数据\n",
|
| 65 |
+
" f.write(compressed)\n",
|
| 66 |
+
" \n",
|
| 67 |
+
" # 计算压缩比\n",
|
| 68 |
+
" original_size = binary_volume.nbytes\n",
|
| 69 |
+
" compressed_size = os.path.getsize(output_file)\n",
|
| 70 |
+
" return original_size, compressed_size, original_size/compressed_size"
|
| 71 |
+
]
|
| 72 |
+
},
|
| 73 |
+
{
|
| 74 |
+
"cell_type": "code",
|
| 75 |
+
"execution_count": null,
|
| 76 |
+
"metadata": {},
|
| 77 |
+
"outputs": [
|
| 78 |
+
{
|
| 79 |
+
"data": {
|
| 80 |
+
"text/plain": [
|
| 81 |
+
"(99688320, 121135, 822.9522433648409)"
|
| 82 |
+
]
|
| 83 |
+
},
|
| 84 |
+
"execution_count": 25,
|
| 85 |
+
"metadata": {},
|
| 86 |
+
"output_type": "execute_result"
|
| 87 |
+
}
|
| 88 |
+
],
|
| 89 |
+
"source": [
|
| 90 |
+
"compress_3d_direct(orig)"
|
| 91 |
+
]
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"cell_type": "code",
|
| 95 |
+
"execution_count": 26,
|
| 96 |
+
"metadata": {},
|
| 97 |
+
"outputs": [],
|
| 98 |
+
"source": [
|
| 99 |
+
"import numpy as np\n",
|
| 100 |
+
"import blosc\n",
|
| 101 |
+
"import os\n",
|
| 102 |
+
"\n",
|
| 103 |
+
"def decompress_3d_direct(input_file='3d_direct.bin'):\n",
|
| 104 |
+
" \"\"\"解压缩由compress_3d_direct压缩的3D二值数组\"\"\"\n",
|
| 105 |
+
" with open(input_file, 'rb') as f:\n",
|
| 106 |
+
" # 读取元数据(形状信息)\n",
|
| 107 |
+
" # 假设形状是3维的,每个维度是32位整数(4字节)\n",
|
| 108 |
+
" shape_bytes = f.read(3 * 4) # 3个int32\n",
|
| 109 |
+
" shape_info = np.frombuffer(shape_bytes, dtype=np.int32)\n",
|
| 110 |
+
" \n",
|
| 111 |
+
" # 读取压缩数据\n",
|
| 112 |
+
" compressed_data = f.read()\n",
|
| 113 |
+
" \n",
|
| 114 |
+
" # 解压缩blosc数据\n",
|
| 115 |
+
" decompressed_bytes = blosc.decompress(compressed_data)\n",
|
| 116 |
+
" \n",
|
| 117 |
+
" # 将字节转换回numpy数组(仍是打包的位)\n",
|
| 118 |
+
" packed_array = np.frombuffer(decompressed_bytes, dtype=np.uint8)\n",
|
| 119 |
+
" \n",
|
| 120 |
+
" # 计算原始数组中的元素总数\n",
|
| 121 |
+
" total_elements = shape_info[0] * shape_info[1] * shape_info[2]\n",
|
| 122 |
+
" \n",
|
| 123 |
+
" # 解开位打包,还原为布尔数组\n",
|
| 124 |
+
" unpacked = np.unpackbits(packed_array)\n",
|
| 125 |
+
" \n",
|
| 126 |
+
" # 可能需要截断多余的位(unpackbits总是产生8的倍数长度)\n",
|
| 127 |
+
" if len(unpacked) > total_elements:\n",
|
| 128 |
+
" unpacked = unpacked[:total_elements]\n",
|
| 129 |
+
" \n",
|
| 130 |
+
" # 重塑为原始形状\n",
|
| 131 |
+
" result = unpacked.reshape(tuple(shape_info)).astype(np.bool_)\n",
|
| 132 |
+
" \n",
|
| 133 |
+
" return result\n",
|
| 134 |
+
"\n",
|
| 135 |
+
"# 验证压缩和解压是否正确\n",
|
| 136 |
+
"def verify_compression(original_array, input_file='3d_direct.bin'):\n",
|
| 137 |
+
" \"\"\"验证压缩和解压是否无损\"\"\"\n",
|
| 138 |
+
" # 解压缩\n",
|
| 139 |
+
" decompressed = decompress_3d_direct(input_file)\n",
|
| 140 |
+
" \n",
|
| 141 |
+
" # 检查形状是否相同\n",
|
| 142 |
+
" shape_match = original_array.shape == decompressed.shape\n",
|
| 143 |
+
" \n",
|
| 144 |
+
" # 检查内容是否相同\n",
|
| 145 |
+
" content_match = np.array_equal(original_array, decompressed)\n",
|
| 146 |
+
" \n",
|
| 147 |
+
" print(f\"形状匹配: {shape_match}\")\n",
|
| 148 |
+
" print(f\"内容匹配: {content_match}\")\n",
|
| 149 |
+
" \n",
|
| 150 |
+
" if not content_match:\n",
|
| 151 |
+
" # 找出不匹配的元素数量\n",
|
| 152 |
+
" diff_count = np.sum(original_array != decompressed)\n",
|
| 153 |
+
" total_elements = np.prod(original_array.shape)\n",
|
| 154 |
+
" print(f\"不匹配元素: {diff_count}/{total_elements} ({diff_count/total_elements*100:.6f}%)\")\n",
|
| 155 |
+
" \n",
|
| 156 |
+
" return shape_match and content_match"
|
| 157 |
+
]
|
| 158 |
+
},
|
| 159 |
+
{
|
| 160 |
+
"cell_type": "code",
|
| 161 |
+
"execution_count": 27,
|
| 162 |
+
"metadata": {},
|
| 163 |
+
"outputs": [],
|
| 164 |
+
"source": [
|
| 165 |
+
"tmp = decompress_3d_direct()"
|
| 166 |
+
]
|
| 167 |
+
},
|
| 168 |
+
{
|
| 169 |
+
"cell_type": "code",
|
| 170 |
+
"execution_count": 32,
|
| 171 |
+
"metadata": {},
|
| 172 |
+
"outputs": [
|
| 173 |
+
{
|
| 174 |
+
"data": {
|
| 175 |
+
"text/plain": [
|
| 176 |
+
"True"
|
| 177 |
+
]
|
| 178 |
+
},
|
| 179 |
+
"execution_count": 32,
|
| 180 |
+
"metadata": {},
|
| 181 |
+
"output_type": "execute_result"
|
| 182 |
+
}
|
| 183 |
+
],
|
| 184 |
+
"source": [
|
| 185 |
+
"(tmp.astype(np.uint8) == orig).all()"
|
| 186 |
+
]
|
| 187 |
+
},
|
| 188 |
+
{
|
| 189 |
+
"cell_type": "code",
|
| 190 |
+
"execution_count": null,
|
| 191 |
+
"metadata": {},
|
| 192 |
+
"outputs": [],
|
| 193 |
+
"source": []
|
| 194 |
+
}
|
| 195 |
+
],
|
| 196 |
+
"metadata": {
|
| 197 |
+
"kernelspec": {
|
| 198 |
+
"display_name": "dino",
|
| 199 |
+
"language": "python",
|
| 200 |
+
"name": "python3"
|
| 201 |
+
},
|
| 202 |
+
"language_info": {
|
| 203 |
+
"codemirror_mode": {
|
| 204 |
+
"name": "ipython",
|
| 205 |
+
"version": 3
|
| 206 |
+
},
|
| 207 |
+
"file_extension": ".py",
|
| 208 |
+
"mimetype": "text/x-python",
|
| 209 |
+
"name": "python",
|
| 210 |
+
"nbconvert_exporter": "python",
|
| 211 |
+
"pygments_lexer": "ipython3",
|
| 212 |
+
"version": "3.8.19"
|
| 213 |
+
}
|
| 214 |
+
},
|
| 215 |
+
"nbformat": 4,
|
| 216 |
+
"nbformat_minor": 2
|
| 217 |
+
}
|