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