Skip to main content

model <> string conversions with json

Project description

uninas

This repository provides a framework for creating model instances from the UNINas search space and exploring (walking through) that space programmatically. It supports JSON-defined architectures, conversion between JSON and model objects, and search workflows over valid configurations.


Model JSON Format

The model description JSON defines the structural and hyperparameter configuration of a model architecture within the UniNAS search space.

Reference papers:


Example — CoAtNet-like Configuration

{
  "img_size": 224,
  "num_classes": 1000,
  "drop_rate": 0.0,
  "embed_dim": [96, 192, 384, 768],
  "depths": [2, 3, 5, 2],
  "model_str": "[[\"E\", \"E\"], [\"E\", \"E\", \"E\"], [\"T\", \"T\", \"T\", \"T\", \"T\"], [\"T\", \"T\"]]",
  "stem_width": [32, 64]
}

Field Meaning

The following fields define standard architectural parameters:

  • img_size — input image resolution (square)
  • num_classes — number of output classes
  • drop_rate — dropout rate
  • embed_dim — per-stage embedding/channel dimensions
  • depths — number of blocks per stage
  • stem_width — channel widths of stem layers

The model_str field encodes the stage-by-stage, block-by-block layout as a stringified nested array.

In the example above "[[\"E\", \"E\"], [\"E\", \"E\", \"E\"], [\"T\", \"T\", \"T\", \"T\", \"T\"], [\"T\", \"T\"]]" stands for:

  • Stage 1: 2 EfficientNet-like blocks
  • Stage 2: 3 EfficientNet-like blocks
  • Stage 3: 5 Transformer-like blocks
  • Stage 4: 2 Transformer-like blocks

Block Type Codes in model_str

Each character represents a block type:

  • T — Transformer block
  • E — EfficientNet-like block
  • R — ResNet-like block

Moreover, instead of single characters, we can specify a fully custom block from the UniNAS search space via a structured block definition: {"subblock1": XXX, "subblock2": YYY}, where XXX and YYY are recursive graph representations produced via each Node’s .to_string() method.

Additional examples can be found in examples/

  • model_coatnet.json -- CoAtNet,
  • model_efficientnet.json -- EfficientNet,
  • model_resnet.json -- ResNet,
  • model_transformer.json -- Transformer
  • model_example.json -- Custom model

Code

json-to-model (and back) is performed via

from uninas import UNIModel, UNIModelCfg
import json

# Json to model
model_path_in = 'examples/model_example.json'
model_path_out = 'examples/model_example_out.json'
with open(model_path_in, "r") as f:
    model_string = f.read()
model_cfg = UNIModelCfg.from_string(model_string)
model = UNIModel(model_cfg)

# Model to json
model_str = model.to_string()
with open(model_path_out, "w") as f:
    json.dump(json.loads(model_str), f, indent=2)

You can load an initial model from JSON, perform a search in the UniNAS search space with specific search parameters, and save the resulting model configuration:

python test.py --model-init <MODEL_JSON_PATH>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uninas-1.2.0.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uninas-1.2.0-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file uninas-1.2.0.tar.gz.

File metadata

  • Download URL: uninas-1.2.0.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for uninas-1.2.0.tar.gz
Algorithm Hash digest
SHA256 302e62ae0700be3c2833c74972c7b6f3d36372536f065e81969aa1689535c65d
MD5 b62eb05975b9c2eeea5e27dd6eb03c19
BLAKE2b-256 156ed80963308ef2707dcc87e718deee4156184ca8638e945ef0436889df2624

See more details on using hashes here.

File details

Details for the file uninas-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: uninas-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 22.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for uninas-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e5937fee2b6b67caf4aa4f1840197be817644f6f289cde9ebf3d95942bf03df5
MD5 90329d8c200ecbb409830eeabc278290
BLAKE2b-256 66d56844f751140c18916856e2cd6b7ddb96991e1027321afd8c2afc41fcdb9c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page