将CSAM检测的Apple Neuralhash模型转换为ONNX。
Apple Neuralhash是一种基于神经网络的图像的感知散列方法。它可以忍受图像调整大小和压缩。哈希的步骤如下:
360x360 。[-1, 1]范围。96x128矩阵的点产物,其结果为128个浮子。在这个项目中,我们将Apple的Neuralhash模型转换为ONNX格式。还包括用于测试模型的演示脚本。
MacOS和Linux都可以工作。在以下各节中,debian用于Linux示例。
brew install lzfse 。Python 3.6及以上应该有效。安装以下依赖项:
pip install onnx coremltools您将需要最近的MACOS或iOS构建中的4个文件:
选项1:来自MacOS或越狱iOS设备(推荐)
如果您已安装了最近版本的MacOS(11.4+)或越狱iOS(14.7+),只需从/System/Library/Frameworks/Vision.framework/Resources/ (在MacOS)或/System/Library/Frameworks/Vision.framework/ ios)中获取这些文件。
.ipsw 。 cd /path/to/ipsw/file
mkdir unpacked_ipsw
cd unpacked_ipsw
unzip ../ * .ipswls -lh您需要的是最大的.dmg文件,例如018-63036-003.dmg 。
# Build and install apfs-fuse
sudo apt install fuse libfuse3-dev bzip2 libbz2-dev cmake g++ git libattr1-dev zlib1g-dev
git clone https://github.com/sgan81/apfs-fuse.git
cd apfs-fuse
git submodule init
git submodule update
mkdir build
cd build
cmake ..
make
sudo make install
sudo ln -s /bin/fusermount /bin/fusermount3
# Mount image
mkdir rootfs
apfs-fuse 018-63036-003.dmg rootfs所需的文件在/System/Library/Frameworks/Vision.framework/下。
将它们放在同一目录下:
mkdir NeuralHash
cd NeuralHash
cp /System/Library/Frameworks/Vision.framework/Resources/NeuralHashv3b-current.espresso. * .
cp /System/Library/Frameworks/Vision.framework/Resources/neuralhash_128x96_seed1.dat .通常在JSON中编制了model.espresso.net和model.espresso.shape的Core ML模型存储结构。神经模型的模型是相同的,但被LZFSE压缩。
dd if=NeuralHashv3b-current.espresso.net bs=4 skip=7 | lzfse -decode -o model.espresso.net
dd if=NeuralHashv3b-current.espresso.shape bs=4 skip=7 | lzfse -decode -o model.espresso.shape
cp NeuralHashv3b-current.espresso.weights model.espresso.weights cd ..
git clone https://github.com/AsuharietYgvar/TNN.git
cd TNN
python3 tools/onnx2tnn/onnx-coreml/coreml2onnx.py ../NeuralHash结果模型是NeuralHash/model.onnx 。
Netron是实现此目的的理想工具。
pip install onnxruntime pillownnhash.py : python3 nnhash.py /path/to/model.onnx /path/to/neuralhash_128x96_seed1.dat image.jpg示例输出:
ab14febaa837b6c1484c35e6
注意:这里生成的神经哈希与iOS设备上生成的神经可能有几点。这是可以预期的,因为无论如何,不同的iOS设备会产生略有不同的哈希。原因是神经网络基于浮点计算。准确性高度取决于硬件。对于较小的网络,这不会有任何区别。但是Neuralhash具有200多层,导致了严重的累积错误。
| 设备 | 哈希 |
|---|---|
| iPad Pro 10.5英寸 | 2b186faa6b36ffcc4c4635e1 |
| M1 Mac | 2b5c6faa6bb7bdcc4c4731a1 |
| iOS模拟器 | 2b5c6faa6bb6bdcc4c4731a1 |
| ONNX运行时 | 2b5c6faa6bb6bdcc4c4735a1 |