

GitHub - AsuharietYgvar/AppleNeuralHash2ONNX: Convert Apple NeuralHash model for...
source link: https://github.com/AsuharietYgvar/AppleNeuralHash2ONNX
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

AppleNeuralHash2ONNX
Convert Apple NeuralHash model for CSAM Detection to ONNX.
Intro
Apple NeuralHash is a perceptual hashing method for images based on neural networks. It can tolerate image resize and compression. The steps of hashing is as the following:
- Convert image to RGB.
- Resize image to
360x360
. - Normalize RGB values to
[-1, 1]
range. - Perform inference on the NeuralHash model.
- Calculate dot product of a
96x128
matrix with the resulting vector of 128 floats. - Apply binary step to the resulting 96 float vector.
- Convert the vector of 1.0 and 0.0 to bits, resulting in 96-bit binary data.
In this project, we convert Apple's NeuralHash model to ONNX format. A demo script for testing the model is also included.
Prerequisite
Both macOS and Linux will work. In the following sections Debian is used for Linux example.
LZFSE decoder
- macOS: Install by running
brew install lzfse
. - Linux: Build and install from lzfse source.
Python
Python 3.6 and above should work. Install the following dependencies:
pip install onnx coremltools
Conversion Guide
Step 1: Get NeuralHash model
You will need 4 files from a recent macOS or iOS build:
- neuralhash_128x96_seed1.dat
- NeuralHashv3b-current.espresso.net
- NeuralHashv3b-current.espresso.shape
- NeuralHashv3b-current.espresso.weights
Option 1: From macOS or jailbroken iOS device (Recommended)
If you have a recent version of macOS (11.4+) or jailbroken iOS (14.7+) installed, simply grab these files from /System/Library/Frameworks/Vision.framework/Resources/
(on macOS) or /System/Library/Frameworks/Vision.framework/
(on iOS).
Option 2: From iOS IPSW (click to reveal)
Put them under the same directory:
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 .
Step 2: Decode model structure and shapes
Normally compiled Core ML models store structure in model.espresso.net
and shapes in model.espresso.shape
, both in JSON. It's the same for NeuralHash model but compressed with 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
Step 3: Convert model to ONNX
cd .. git clone https://github.com/AsuharietYgvar/TNN.git cd TNN python3 tools/onnx2tnn/onnx-coreml/coreml2onnx.py ../NeuralHash
The resulting model is NeuralHash/model.onnx
.
Usage
Inspect model
Netron is a perfect tool for this purpose.
Calculate neural hash with onnxruntime
- Install required libraries:
pip install onnxruntime pillow
- Run
nnhash.py
on an image:
python3 nnhash.py /path/to/model.onnx /path/to/neuralhash_128x96_seed1.dat image.jpg
Example output:
ab14febaa837b6c1484c35e6
Note: Neural hash generated here might be a few bits off from one generated on an iOS device. This is expected since different iOS devices generate slightly different hashes anyway. The reason is that neural networks are based on floating-point calculations. The accuracy is highly dependent on the hardware. For smaller networks it won't make any difference. But NeuralHash has 200+ layers, resulting in significant cumulative errors.
Device Hash iPad Pro 10.5-inch2b186faa6b36ffcc4c4635e1
M1 Mac
2b5c6faa6bb7bdcc4c4731a1
iOS Simulator
2b5c6faa6bb6bdcc4c4731a1
ONNX Runtime
2b5c6faa6bb6bdcc4c4735a1
Credits
Recommend
-
166
Convert Sketch files to React Native components Do you have designs in Sketch, and need to turn those into a mobile app? This will take those designs, and automatically create React Native components.
-
204
VIM as IDE by Damian Ziobro Configuration of VIM + plugins - it works as fully functional Integrated Development Environment (IDE)
-
42
How to Convert Your Keras Model to ONNX Intuition I love Keras for its simplicity. With about 10 minutes, I can build a deep learning model with its sequential or functional API with elegant code. However, Keras always load...
-
6
Copy link dxoigmn commented
-
17
neural-hash-collider Find target hash collisions for Apple's NeuralHa...
-
2
nhcalc Compute NeuralHash for a given image. Works with macOS 11.3 or later. Behind the scene, the implementation invokes the API provided by the Vision framework to compute the NeuralHash for a given image. The...
-
7
Files Permalink Latest commit message Commit time
-
21
How to Convert Apple Keynote Files to PowerPoint Presentations By Hamlin Rozario Published 2 hours ago Apple's Keynote files ar...
-
8
How to convert TensorFlow model and run it with OpenVINO™ Toolkit ...
-
4
model_int8_convert ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK