Creating OpenTouch models
Two files, always
Every model is an .onnx network plus a .json config — nothing else.
Never bundled in the APK
Loaded at runtime from Android/data/com.opentouch.android/files/models/.
Official vs. unofficial
Official models are downloaded in the AI menu. Unofficial ones are zips you make and import yourself.
Same trainer, either way
Both use the MobileNetV3-Small scripts in the app repository (lasr-lab/opentouchapp), unless you bring your own ONNX network.
An OpenTouch model is an image classifier that runs on the phone while the sensor preview stays live.
Every model is two files: an .onnx network, and a .json file with the input
size, RGB normalization, and class labels. The app does not ship these files inside the APK — it
loads them from Android/data/com.opentouch.android/files/models/ on the device.
There are two ways a model gets there. The network and the JSON are the same kind of files either way; the difference is who publishes the zip, and how the phone receives it.
- Official models are trained and published by this project. The AI menu downloads them. The current ones are
key_finger,max_model, and three dummy sensor tests:DIGIT_testmodel,DIGIT360_testmodel, andGelSightMini_testmodel. Each test model has two classes,test 1andtest 2, both always 50%, and it runs only on the sensor in its name. Each zip is a file athttps://opentouch.org/models/<id>.zip. The mobile page does not show a download button for these files — the app requests the address directly. - Unofficial models are zip archives someone makes for their own classes. They are not hosted for the app. The user imports the zip from the phone with Import model files. An unofficial model never appears under Available to download.
What the phone expects
The runner in OnnxModelRunner.kt always does the same thing:
- Scale the sensor frame to
input_widthbyinput_height. - Read the pixels as RGB, each channel divided by 255 so the values sit between 0 and 1.
- Normalize each channel with
(value - mean) / std. - Run the network. The input tensor is float32, shape
[1, 3, height, width]. - Treat the output as one logit per class, in the same order as
labels, and apply softmax.
labels, input_width, input_height, mean, and
std are required. mean and std each have three numbers. The
published configs also store color_order and output_type, but the app does
not switch behavior from those fields — every model is RGB in, logits out.
{
"input_width": 224,
"input_height": 224,
"color_order": "RGB",
"mean": [0.485, 0.456, 0.406],
"std": [0.229, 0.224, 0.225],
"labels": ["cotton", "wool"],
"output_type": "logits"
}The label list has to match the network output order — a mismatch shows up as the right confidence on the wrong class name. The scripts in the app repository train MobileNetV3-Small at 224×224 with the ImageNet mean and standard deviation above. The backbone stays frozen; only the classifier head is trained. Official and unofficial models both use that setup, unless the unofficial network was trained somewhere else and already matches the contract above.
The JSON can also carry details that the app shows in the model's info sheet (the ⓘ button in the AI menu). They are optional, and the app runs the model the same way without them:
sensors— the sensors the model was trained for, for example["DIGIT"]. Use"DIGIT","DIGIT 360"or"GelSight Mini". Models for another sensor than the connected one are grayed out and listed last.description— one or two sentences on what the model recognises. It is shown under the model's name. Write it yourself.author— who made the model. Official models use"OpenTouch Team".version— for example"1.0".dataset— what the model was trained on, for example"about 800 DIGIT images, 9 classes".
Setup
From the root of the app repository (lasr-lab/opentouchapp), in PowerShell:
py -3.12 -m venv .model-venv
.\.model-venv\Scripts\python.exe -m pip install --upgrade pip
.\.model-venv\Scripts\python.exe -m pip install torch torchvision pillow onnx onnxscript onnxruntime
If the machine has a working NVIDIA CUDA install, use the matching PyTorch build from
pytorch.org
instead of the generic torch package. Training falls back to CPU when CUDA is not
available.
Photos are ordinary image files. Put one class in each folder — the folder name becomes the class label. Shoot the pictures with the same sensor and lighting you will use in the app, because the model only sees the live preview.
Official model: key_finger
key_finger answers one question: is the sensor looking at a key or a finger? It is a
small proof of concept. The class folders have to be named exactly finger and
key.
model_key_finger/dataset/finger/
model_key_finger/dataset/key/At least four images are required. Training keeps one image from each class for validation and trains on the rest for 15 epochs.
.\.model-venv\Scripts\python.exe model_key_finger\train_model.py
.\.model-venv\Scripts\python.exe model_key_finger\export_model.py
Training writes model_key_finger/output/key_finger_checkpoint.pth. Export writes
key_finger.onnx and rewrites model_config.json. ImageFolder orders the
classes alphabetically as finger, key — export swaps the logits so the
file the phone loads is ordered key, then finger.
Check one saved photo before packaging:
.\.model-venv\Scripts\python.exe model_key_finger\test_models.py .\model_key_finger\dataset\key\key.jpgThe script prints the PyTorch class and the ONNX class. They should be the same name.
Official model: max_model
max_model is the general material classifier. Each direct subfolder of the data directory
is one class. The set used for the published model is:
3dprint, cotton, curdory, finger, flower_fabric, nothing,
synthetic_leather, teddy, wool
The default run takes 100 images from each class, keeps 20 for validation, and trains on 80. The
selection uses a fixed seed, so the same folder produces the same split. Class names are sorted
alphabetically, and that order is the order stored in labels. In the published
max_model.zip, the label curdory is spelled corduroy.
.\.model-venv\Scripts\python.exe model_max_model\train_model.py `
--data-dir C:\path\to\dataset `
--images-per-class 100 `
--epochs 15
.\.model-venv\Scripts\python.exe model_max_model\export_model.py
Export reads model_max_model/output/max_model_checkpoint.pth and writes
max_model.onnx next to model_config.json.
Useful flags:
--images-per-class— how many photos to take from each folder. The minimum is 2. Every class needs at least that many files.--epochs— default 15.--batch-size— default 16.--no-pretrained— start from random weights instead of ImageNet weights.--output-dir— where the checkpoint and config are written. The default ismodel_max_model/output.
Package the zip
The phone installs a model from a zip. The package script copies the network and the config in under
the names the importer prefers, model.onnx and model.json. The zip file
itself is named after the .onnx file, and that name is the name shown in the AI menu.
.\tools\package-opentouch-model.ps1 `
-ModelPath .\model_key_finger\output\key_finger.onnx `
-ConfigPath .\model_key_finger\output\model_config.json.\tools\package-opentouch-model.ps1 `
-ModelPath .\model_max_model\output\max_model.onnx `
-ConfigPath .\model_max_model\output\model_config.json
Those commands write key_finger.zip and max_model.zip beside the
.onnx files. Copies that are ready to import also live in zipped_models/.
A zip may contain the two files at the top or inside one folder. If the entries are not named
model.onnx and model.json, the importer accepts a single .onnx
and a single .json. More than one of either, without those preferred names, is rejected.
Publish an official model
Publishing puts the zip where the app can download it. It does not add a button on the website. Visitors do not download official models from this page.
- The zip contains
model.onnxandmodel.json. The zip's file name is the model name, for exampleDIGIT360_testmodel.zip. - Place that zip in the website repo at
webpage/models/. No button is added on the page. The site serves the file athttps://opentouch.org/models/DIGIT360_testmodel.zip. - Push the website. GitHub Pages publishes the
webpagefolder. During that publish, every.zipinwebpage/models/is written intocatalog.jsonathttps://opentouch.org/models/catalog.json, together with thesensors,description,author,version,datasetandlabelsfrom itsmodel.json. The app shows those details before the model is downloaded, so fill them in before you publish. - The app reads that catalog when the AI menu opens. Available to download shows exactly the zips that are in the folder. Tapping a name downloads
https://opentouch.org/models/<id>.zipand unpacks it. Removing a zip and pushing the site removes it from the list. No edit in the app is required.
One app build that knows how to read catalog.json has to be installed first. After that,
new official models are only a zip in webpage/models/ plus a website push. Replacing an
existing zip and pushing again updates the file; a phone that already imported it keeps the old copy
until someone downloads that name again.
Do not add a download link or card in webpage/mobile/index.html. The files stay in webpage/models/ only so the app can fetch them.
Unofficial model
Use the max_model trainer. It already accepts any set of class folders, so an unofficial
model does not need a new script. The result stays a zip on the computer that made it — import
that zip from the phone. Do not copy it to webpage/models/. A zip in that folder is
published as an official download.
- Make one folder per class. Name each folder with the label you want on screen, using letters, numbers, and underscores. You need at least two classes.
- Put sensor photos in those folders. Two images per class is the minimum the trainer accepts — a useful model needs many more, taken the way the sensor will be used.
-
Train and export:
.\.model-venv\Scripts\python.exe model_max_model\train_model.py ` --data-dir C:\path\to\your-classes ` --output-dir .\model_max_model\output\your_model ` --images-per-class 100 ` --epochs 15 .\.model-venv\Scripts\python.exe model_max_model\export_model.py ` --output-dir .\model_max_model\output\your_model -
Rename
max_model.onnxto the name you want in the app, then package it. The zip's file name is the name the AI menu shows.Rename-Item .\model_max_model\output\your_model\max_model.onnx fabric.onnx .\tools\package-opentouch-model.ps1 ` -ModelPath .\model_max_model\output\your_model\fabric.onnx ` -ConfigPath .\model_max_model\output\your_model\model_config.json - On the phone, open the AI menu and choose Import model files, then select
fabric.zip. You can also open the zip from the download or file app — OpenTouch is registered for zip files, so Android can offer the app as the opener. The zip is unpacked into the model directory asfabric.onnxandfabric.json.
Importing the .onnx and the .json as two separate files also works. They
need the same base name, such as fabric.onnx and fabric.json — the app
cannot run from either file alone.
Selecting the model in the AI menu switches to AI mode and starts live classification. It pauses in Live mode and Data collection mode. Turn off model in the AI menu stops it, and it also stops when a sensor that the model was not trained for is connected.
A network you trained somewhere else
An unofficial model does not have to come from these scripts. Export it to ONNX yourself and write the JSON by hand, as long as all of this is true:
- one float32 input of shape
[1, 3, input_height, input_width] - pixel values already expected in the 0–1 RGB range, because the phone applies
meanandstditself - one output vector of raw logits, one number per entry in
labels, in that order meanandstdin the JSON are the ones this network was trained with
Then package the two files with tools/package-opentouch-model.ps1 and import the zip the same way.