All Projects → AsuharietYgvar → AppleNeuralHash2ONNX

AsuharietYgvar / AppleNeuralHash2ONNX

Licence: Apache-2.0 license
Convert Apple NeuralHash model for CSAM Detection to ONNX.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to AppleNeuralHash2ONNX

neuralhash-collisions
A catalog of naturally occurring images whose Apple NeuralHash is identical.
Stars: ✭ 145 (-90.05%)
Mutual labels:  apple, neuralhash
tigase-swift
(M) Tigase Swift XMPP client library
Stars: ✭ 53 (-96.36%)
Mutual labels:  apple
crossbow
Cross-Platform Rust Toolkit for Games 🏹
Stars: ✭ 80 (-94.51%)
Mutual labels:  apple
ARKitDemoPlayground
A demo of the ARKit Demo project from Xcode 9 as a Swift Playground
Stars: ✭ 47 (-96.77%)
Mutual labels:  apple
SwiftUIFormValidator
Declarative form validator for SwiftUI.
Stars: ✭ 34 (-97.67%)
Mutual labels:  apple
ScrollViewProxy
ScrollViewProxy for SwiftUI on iOS 13 and up
Stars: ✭ 135 (-90.73%)
Mutual labels:  apple
airplayreceiver
Open source implementation of AirPlay 2 Mirroring / Audio protocol.
Stars: ✭ 84 (-94.23%)
Mutual labels:  apple
MTJailed-Native
A terminal emulator with remote shell for non-jailbroken iOS devices
Stars: ✭ 24 (-98.35%)
Mutual labels:  apple
apple-slicer
Parses App Store Connect (formerly iTunes Connect) financial reports and splits sales by their legally accountable Apple subsidiaries
Stars: ✭ 34 (-97.67%)
Mutual labels:  apple
ios-spritekit-shader-sandbox
👾 Collection of custom effects for SpriteKit implemented using GLSL/Metal shaders.
Stars: ✭ 63 (-95.68%)
Mutual labels:  apple
osxapp vers
Get product name, exact product version and build version from an Apple Install [Mac OS X|OS X|macOS]*.app or from a mounted Mac OS X Install CD/DVD image
Stars: ✭ 12 (-99.18%)
Mutual labels:  apple
BDLocalizedDevicesModels
Apple devices model names localized.
Stars: ✭ 23 (-98.42%)
Mutual labels:  apple
WWDC
👩🏻‍💻 1일 1WWDC 영상 보기 🎬
Stars: ✭ 23 (-98.42%)
Mutual labels:  apple
onepile
Playground for the future of private notes and document management
Stars: ✭ 41 (-97.19%)
Mutual labels:  apple
aprenda-swift
Uma lista de conteúdos para você aprender Swift
Stars: ✭ 429 (-70.56%)
Mutual labels:  apple
brewfile
🍎 Brewfile to install softwares in macOS for engineers
Stars: ✭ 37 (-97.46%)
Mutual labels:  apple
humhub-oauth
Social OAuths built for the Social Platform HumHub
Stars: ✭ 16 (-98.9%)
Mutual labels:  apple
go-macho
Package macho implements access to and creation of Mach-O object files.
Stars: ✭ 68 (-95.33%)
Mutual labels:  apple
Nightscouter
An Native iOS app for displaying Nightscout website data.
Stars: ✭ 18 (-98.76%)
Mutual labels:  apple
Apple-App-Icons
This repository is for hosting Apple App Icons Sketch file, .sketch
Stars: ✭ 31 (-97.87%)
Mutual labels:  apple

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:

  1. Convert image to RGB.
  2. Resize image to 360x360.
  3. Normalize RGB values to [-1, 1] range.
  4. Perform inference on the NeuralHash model.
  5. Calculate dot product of a 96x128 matrix with the resulting vector of 128 floats.
  6. Apply binary step to the resulting 96 float vector.
  7. 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

OS

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)
  1. Download any .ipsw of a recent iOS build (14.7+) from ipsw.me.
  2. Unpack the file:
cd /path/to/ipsw/file
mkdir unpacked_ipsw
cd unpacked_ipsw
unzip ../*.ipsw
  1. Locate system image:
ls -lh

What you need is the largest .dmg file, for example 018-63036-003.dmg.

  1. Mount system image. On macOS simply open the file in Finder. On Linux run the following commands:
# 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

Required files are under /System/Library/Frameworks/Vision.framework/ in mounted path.

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

  1. Install required libraries:
pip install onnxruntime pillow
  1. 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-inch 2b186faa6b36ffcc4c4635e1
M1 Mac 2b5c6faa6bb7bdcc4c4731a1
iOS Simulator 2b5c6faa6bb6bdcc4c4731a1
ONNX Runtime 2b5c6faa6bb6bdcc4c4735a1

Credits

  • nhcalc for uncovering NeuralHash private API.
  • TNN for compiled Core ML to ONNX script.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].