Magika standard_v3_0 模型输出内容类型全量清单解析:213 个 Content Type Label 与识别原理
【免费下载链接】magikaFast and accurate AI powered file content types detection项目地址: https://gitcode.com/GitHub_Trending/ma/magika
本文围绕 Magika 当前默认深度学习模型standard_v3_0的输出标签空间展开:完整列出模型可能输出的 213 个内容类型标签(Content Type Label),并结合该模型自带的 config.min.json 与 Python 端推理实现 magika.py,讲清标签的生成链路、预测模式、置信度阈值与输出结构。读完你将能准确解读 Magika 的任意识别结果,并能在自己的代码中直接使用get_supported_content_types()查询当前模型的全部可输出类型。
一、standard_v3_0:当前默认模型的输出标签全集
Magika 在 Python 端将standard_v3_0设为默认模型(见 magika.py 中的DEFAULT_MODEL_NAME = "standard_v3_0",模型文件存放在 assets/models/standard_v3_0/)。该模型的配置 config.min.json 中,target_labels_space字段定义了模型输出层的完整标签空间,共 213 个标签,与模型自带 README(assets/models/standard_v3_0/README.md)列出的清单一一对应。
与上一代standard_v2_1相比,v3.0 的标签空间在末尾新增了randombytes、randomtxt、symlinktext三个内部标签(见 standard_v2_1/config.min.json 中仅有unknown的对比),并通过overwrite_map将它们映射回用户可见的常规标签(详见下文“覆盖映射”小节),同时为handlebars、markdown增加了专属高置信度阈值。
1.1 完整标签清单(按索引 1–106)
下表完整继承自模型 README,索引、标签名(Content Type Label)与说明(Description)均保持原样:
| Index | Content Type Label | Description |
|---|---|---|
| 1 | 3gp | 3GPP multimedia file |
| 2 | ace | ACE archive |
| 3 | ai | Adobe Illustrator Artwork |
| 4 | aidl | Android Interface Definition Language |
| 5 | apk | Android package |
| 6 | applebplist | Apple binary property list |
| 7 | appleplist | Apple property list |
| 8 | asm | Assembly |
| 9 | asp | ASP source |
| 10 | autohotkey | AutoHotKey script |
| 11 | autoit | AutoIt script |
| 12 | awk | Awk |
| 13 | batch | DOS batch file |
| 14 | bazel | Bazel build file |
| 15 | bib | BibTeX |
| 16 | bmp | BMP image data |
| 17 | bzip | bzip2 compressed data |
| 18 | c | C source |
| 19 | cab | Microsoft Cabinet archive data |
| 20 | cat | Windows Catalog file |
| 21 | chm | MS Windows HtmlHelp Data |
| 22 | clojure | Clojure |
| 23 | cmake | CMake build file |
| 24 | cobol | Cobol |
| 25 | coff | Intel 80386 COFF |
| 26 | coffeescript | CoffeeScript |
| 27 | cpp | C++ source |
| 28 | crt | Certificates (binary format) |
| 29 | crx | Google Chrome extension |
| 30 | cs | C# source |
| 31 | csproj | .NET project config |
| 32 | css | CSS source |
| 33 | csv | CSV document |
| 34 | dart | Dart source |
| 35 | deb | Debian binary package |
| 36 | dex | Dalvik dex file |
| 37 | dicom | DICOM |
| 38 | diff | Diff file |
| 39 | dm | Dream Maker |
| 40 | dmg | Apple disk image |
| 41 | doc | Microsoft Word CDF document |
| 42 | dockerfile | Dockerfile |
| 43 | docx | Microsoft Word 2007+ document |
| 44 | dsstore | Application Desktop Services Store |
| 45 | dwg | Autocad Drawing |
| 46 | dxf | Audocad Drawing Exchange Format |
| 47 | elf | ELF executable |
| 48 | elixir | Elixir script |
| 49 | emf | Windows Enhanced Metafile image data |
| 50 | eml | RFC 822 mail |
| 51 | epub | EPUB document |
| 52 | erb | Embedded Ruby source |
| 53 | erlang | Erlang source |
| 54 | flac | FLAC audio bitstream data |
| 55 | flv | Flash Video |
| 56 | fortran | Fortran |
| 57 | gemfile | Gemfile file |
| 58 | gemspec | Gemspec file |
| 59 | gif | GIF image data |
| 60 | gitattributes | Gitattributes file |
| 61 | gitmodules | Gitmodules file |
| 62 | go | Golang source |
| 63 | gradle | Gradle source |
| 64 | groovy | Groovy source |
| 65 | gzip | gzip compressed data |
| 66 | h5 | Hierarchical Data Format v5 |
| 67 | handlebars | Handlebars source |
| 68 | haskell | Haskell source |
| 69 | hcl | HashiCorp configuration language |
| 70 | hlp | MS Windows help |
| 71 | htaccess | Apache access configuration |
| 72 | html | HTML document |
| 73 | icns | Mac OS X icon |
| 74 | ico | MS Windows icon resource |
| 75 | ics | Internet Calendaring and Scheduling |
| 76 | ignorefile | Ignorefile |
| 77 | ini | INI configuration file |
| 78 | internetshortcut | MS Windows Internet shortcut |
| 79 | ipynb | Jupyter notebook |
| 80 | iso | ISO 9660 CD-ROM filesystem data |
| 81 | jar | Java archive data (JAR) |
| 82 | java | Java source |
| 83 | javabytecode | Java compiled bytecode |
| 84 | javascript | JavaScript source |
| 85 | jinja | Jinja template |
| 86 | jp2 | jpeg2000 |
| 87 | jpeg | JPEG image data |
| 88 | json | JSON document |
| 89 | jsonl | JSONL document |
| 90 | julia | Julia source |
| 91 | kotlin | Kotlin source |
| 92 | latex | LaTeX document |
| 93 | lha | LHarc archive |
| 94 | lisp | Lisp source |
| 95 | lnk | MS Windows shortcut |
| 96 | lua | Lua |
| 97 | m3u | M3U playlist |
| 98 | m4 | GNU Macro |
| 99 | macho | Mach-O executable |
| 100 | makefile | Makefile source |
| 101 | markdown | Markdown document |
| 102 | matlab | Matlab Source |
| 103 | mht | MHTML document |
| 104 | midi | Midi |
| 105 | mkv | Matroska |
| 106 | mp3 | MP3 media file |
1.2 完整标签清单(按索引 107–213)
| Index | Content Type Label | Description |
|---|---|---|
| 107 | mp4 | MP4 media file |
| 108 | mscompress | MS Compress archive data |
| 109 | msi | Microsoft Installer file |
| 110 | mum | Windows Update Package file |
| 111 | npy | Numpy Array |
| 112 | npz | Numpy Arrays Archive |
| 113 | nupkg | NuGet Package |
| 114 | objectivec | ObjectiveC source |
| 115 | ocaml | OCaml |
| 116 | odp | OpenDocument Presentation |
| 117 | ods | OpenDocument Spreadsheet |
| 118 | odt | OpenDocument Text |
| 119 | ogg | Ogg data |
| 120 | one | One Note |
| 121 | onnx | Open Neural Network Exchange |
| 122 | otf | OpenType font |
| 123 | outlook | MS Outlook Message |
| 124 | parquet | Apache Parquet |
| 125 | pascal | Pascal source |
| 126 | pcap | pcap capture file |
| 127 | pdb | Windows Program Database |
| 128 | PDF document | |
| 129 | pebin | PE Windows executable |
| 130 | pem | PEM certificate |
| 131 | perl | Perl source |
| 132 | php | PHP source |
| 133 | pickle | Python pickle |
| 134 | png | PNG image |
| 135 | po | Portable Object (PO) for i18n |
| 136 | postscript | PostScript document |
| 137 | powershell | Powershell source |
| 138 | ppt | Microsoft PowerPoint CDF document |
| 139 | pptx | Microsoft PowerPoint 2007+ document |
| 140 | prolog | Prolog source |
| 141 | proteindb | Protein DB |
| 142 | proto | Protocol buffer definition |
| 143 | psd | Adobe Photoshop |
| 144 | python | Python source |
| 145 | pythonbytecode | Python compiled bytecode |
| 146 | pytorch | Pytorch storage file |
| 147 | qt | QuickTime |
| 148 | r | R (language) |
| 149 | rar | RAR archive data |
| 150 | rdf | Resource Description Framework document (RDF) |
| 151 | rpm | RedHat Package Manager archive (RPM) |
| 152 | rst | ReStructuredText document |
| 153 | rtf | Rich Text Format document |
| 154 | ruby | Ruby source |
| 155 | rust | Rust source |
| 156 | scala | Scala source |
| 157 | scss | SCSS source |
| 158 | sevenzip | 7-zip archive data |
| 159 | sgml | sgml |
| 160 | shell | Shell script |
| 161 | smali | Smali source |
| 162 | snap | Snap archive |
| 163 | solidity | Solidity source |
| 164 | sql | SQL source |
| 165 | sqlite | SQLITE database |
| 166 | squashfs | Squash filesystem |
| 167 | srt | SubRip Text Format |
| 168 | stlbinary | Stereolithography CAD (binary) |
| 169 | stltext | Stereolithography CAD (text) |
| 170 | sum | Checksum file |
| 171 | svg | SVG Scalable Vector Graphics image data |
| 172 | swf | Small Web File |
| 173 | swift | Swift |
| 174 | tar | POSIX tar archive |
| 175 | tcl | Tickle |
| 176 | textproto | Text protocol buffer |
| 177 | tga | Targa image data |
| 178 | thumbsdb | Windows thumbnail cache |
| 179 | tiff | TIFF image data |
| 180 | toml | Tom's obvious, minimal language |
| 181 | torrent | BitTorrent file |
| 182 | tsv | TSV document |
| 183 | ttf | TrueType Font data |
| 184 | twig | Twig template |
| 185 | txt | Generic text document |
| 186 | typescript | Typescript |
| 187 | unknown | Unknown binary data |
| 188 | vba | MS Visual Basic source (VBA) |
| 189 | vcxproj | Visual Studio MSBuild project |
| 190 | verilog | Verilog source |
| 191 | vhdl | VHDL source |
| 192 | vtt | Web Video Text Tracks |
| 193 | vue | Vue source |
| 194 | wasm | Web Assembly |
| 195 | wav | Waveform Audio file (WAV) |
| 196 | webm | WebM media file |
| 197 | webp | WebP media file |
| 198 | winregistry | Windows Registry text |
| 199 | wmf | Windows metafile |
| 200 | woff | Web Open Font Format |
| 201 | woff2 | Web Open Font Format v2 |
| 202 | xar | XAR archive compressed data |
| 203 | xls | Microsoft Excel CDF document |
| 204 | xlsb | Microsoft Excel 2007+ document (binary format) |
| 205 | xlsx | Microsoft Excel 2007+ document |
| 206 | xml | XML document |
| 207 | xpi | Compressed installation archive (XPI) |
| 208 | xz | XZ compressed data |
| 209 | yaml | YAML source |
| 210 | yara | YARA rule |
| 211 | zig | Zig source |
| 212 | zip | Zip archive data |
| 213 | zlibstream | zlib compressed data |
注意:该清单是模型输出层所能输出的全部标签,但并不是 Magika 工具最终可能返回的全部标签。诸如directory、empty、symlink、undefined等标签由工具层直接判定,不经过深度学习模型(详见下文“非模型路径”小节)。
二、从模型输出到最终标签:一条标签的完整生命周期
理解这份清单的意义,关键在于弄清模型预测的标签如何变成你最终看到的label。核心实现在 magika.py 的_get_output_ct_label_from_dl_result()方法,其处理顺序如下:
- 覆盖映射(overwrite_map):先将模型输出的标签经
overwrite_map重写。standard_v3_0 的映射为{"randomtxt": "txt", "randombytes": "unknown", "symlinktext": "txt"}——即模型在内部用三个额外标签细粒度建模随机文本、随机二进制与符号链接文本,但对用户统一呈现为常规的txt/unknown。 - 预测模式判定(prediction_mode):根据置信度分数与对应阈值决定是否采信模型结论。
- 兜底降级:若分数不足,文本类文件回退为
txt,二进制类回退为unknown。
2.1 三种预测模式(PredictionMode)
预测模式通过 Python 接口的prediction_mode参数或 CLI 的--prediction-mode指定,逻辑同样位于 magika.py:
| 模式 | 判定条件 | 行为 |
|---|---|---|
BEST_GUESS | 无条件 | 无论分数高低,直接采信模型预测的标签 |
HIGH_CONFIDENCE(默认) | score >= thresholds[ct],无专属阈值时用medium_confidence_threshold | 分数达标才采信,否则降级为txt/unknown |
MEDIUM_CONFIDENCE | score >= medium_confidence_threshold | 使用统一宽松阈值(0.5)判定,分数不达标则降级 |
2.2 standard_v3_0 的阈值配置
来自 config.min.json:
medium_confidence_threshold: 0.5:绝大多数类型的默认高置信度阈值。thresholds: {"handlebars": 0.9, "latex": 0.95, "markdown": 0.9, "pascal": 0.95}:这四类文本格式与普通代码文本容易混淆,模型为它们单独设定了更苛刻的高置信度门槛,降低误报率。min_file_size_for_dl: 8:小于等于 8 字节的文件(或去除空白后有效内容不足 8 字节)不进入深度学习推理。padding_token: 256、block_size: 4096:特征提取相关的填充标记与单次读取上限。beg_size: 1024、mid_size: 0、end_size: 1024:v3.0 仅取文件开头 1024 字节与结尾 1024 字节作为模型输入,不再取中段;use_inputs_at_offsets: false表示不额外采样 0x8000/0x8800/0x9000/0x9800 偏移(v2.1 同样为 false,这些偏移位点是为 ISO/UDF 类文件预留的特征)。
特征提取的完整实现(开头 lstrip、结尾 rstrip、padding 补齐等细节)可参见 magika.py 的_extract_features_from_seekable(),其要点是:只读取文件开头与结尾各至多block_size字节,避免将整个文件载入内存。
2.3 非模型路径:清单之外的标签
以下标签不经过深度学习模型,而是由工具层直接判定(见 magika.py):
empty:0 字节文件,score固定为 1.0。directory:目录,score固定为 1.0。symlink:配合no_dereference=True(CLI 为-n/--no-dereference)时不解析符号链接,直接返回。undefined:dl块中代表“未使用深度学习”,例如过小文件、目录、空文件等场景,此时dl.label为undefined,而output.label为实际判定结果。txt/unknown:文件过小(<= min_file_size_for_dl)时,直接尝试 UTF-8 解码,可解码判为txt,否则判为unknown(见 magika.py)。
因此在实际使用中,你会看到output.label的取值空间 = 上表 213 个标签 + 上述工具层标签。
三、输出结构:dl 与 output 的双层设计
所有语言绑定(Python、JS、Rust、Go 及 CLI)返回统一的输出结构,官方说明见 docs/magika_output.md。以识别一个 JS 文件为例:
$ magika tests_data/basic/javascript/code.js --json [ { "path": "tests_data/basic/javascript/code.js", "result": { "status": "ok", "value": { "dl": { "description": "JavaScript source", "extensions": ["js", "mjs", "cjs"], "group": "code", "is_text": true, "label": "javascript", "mime_type": "application/javascript" }, "output": { "description": "JavaScript source", "extensions": ["js", "mjs", "cjs"], "group": "code", "is_text": true, "label": "javascript", "mime_type": "application/javascript" }, "score": 0.9710000157356262 } } } ]解读要点:
path:本次预测对应的文件路径(批量扫描多个文件时用于区分)。result.status:ok表示扫描成功;文件不存在、权限不足等场景返回错误状态。score:模型预测的置信度(0~1)。dl块:深度学习模型的原始预测信息,label对应上表 213 个标签之一(未走模型时为undefined)。output块:Magika 工具层综合模型预测、置信度、预测模式后给出的最终结果,是普通业务代码应直接使用的字段。is_text:是否为文本类型,决定低置信度时回退到txt还是unknown。
官方建议“大多数客户端只消费output.label”,原始dl预测主要用于调试。关于为何以label而非description/mime_type作为集成锚点,可参见 docs/faq.md。
四、在代码中查询与使用这套标签
4.1 查询当前模型支持的完整标签列表
Python 端提供了直接接口(magika.py):
from pathlib import Path from magika import Magika m = Magika(model_dir=Path("assets/models/standard_v3_0")) supported = m.get_supported_content_types() print(len(supported)) # 213 print(supported) # 与 README 清单一一对应的 ContentTypeLabel 列表ContentTypeLabel枚举的完整定义见 python/src/magika/types/content_type_label.py,其中包含全部候选标签(模型实际只支持子集,以上表为准)。
4.2 标签对应的元数据
每个标签对应的描述、MIME 类型、扩展名、分组与is_text信息记录在知识库 python/src/magika/config/content_types_kb.min.json 中(仓库根目录另有可读版本 assets/content_types_kb.min.json)。模型推理时由_load_content_types_kb()载入(magika.py):文本类型默认 MIME 为text/plain,二进制默认application/octet-stream;未提供 description 时回退为标签名本身。
4.3 实战:解析一次识别结果
from pathlib import Path from magika import Magika m = Magika() # 默认加载 standard_v3_0 res = m.identify_path(Path("tests_data/basic/javascript/code.js")) print(res.prediction.dl.label) # javascript(模型原始输出) print(res.prediction.output.label) # javascript(最终输出,业务应使用) print(res.prediction.score) # 置信度,如 0.971...上述测试样本位于 tests_data/basic/javascript/code.js,Python 端测试用例见 python/tests/test_magika_python_module.py。
五、与其它内置模型的差异速览
仓库内还提供了多个模型,可通过model_dir参数切换:
| 模型 | 输入特征 | 标签数 | 特性 |
|---|---|---|---|
standard_v3_0(默认) | beg 1024 + end 1024 | 213 | 阈值含handlebars/markdown专属配置;overwrite_map映射randombytes/randomtxt/symlinktext |
standard_v2_1 | beg 2048 + end 2048 | 213 | 不含randombytes/randomtxt/symlinktext标签;overwrite_map为空 |
fast_v2_1 | beg 512 + end 512 | 213 | 输入更少,速度优先(见 assets/models/fast_v2_1/config.min.json) |
三者的medium_confidence_threshold均为 0.5,block_size均为 4096。模型的训练元信息(如 100 个 epoch)记录在 assets/models/standard_v3_0/metadata.json。
六、小结
standard_v3_0的 213 个输出标签构成了 Magika 文件类型识别能力的边界:深度学习模型在这 213 个类别间做概率分布预测,工具层再依据overwrite_map、专属阈值与预测模式做二次裁决,最终通过dl/output双层结构同时暴露原始预测与可信结果。无论是直接阅读 assets/models/standard_v3_0/README.md 中的清单,还是调用get_supported_content_types()在代码中动态获取,你都可以精准掌握当前模型“能识别什么”,从而在设计文件分类、内容安全扫描、上传校验等系统时做出正确的集成决策。
【免费下载链接】magikaFast and accurate AI powered file content types detection项目地址: https://gitcode.com/GitHub_Trending/ma/magika
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考