將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 |