Home

Awesome

JAX ResNet - Implementations and Checkpoints for ResNet Variants

Build & Tests

A Flax (Linen) implementation of ResNet (He et al. 2015), Wide ResNet (Zagoruyko & Komodakis 2016), ResNeXt (Xie et al. 2017), ResNet-D (He et al. 2020), and ResNeSt (Zhang et al. 2020). The code is modular so you can mix and match the various stem, residual, and bottleneck implementations.

Installation

You can install this package from PyPI:

pip install jax-resnet

Or directly from GitHub:

pip install --upgrade git+https://github.com/n2cholas/jax-resnet.git

Usage

See the bottom of jax-resnet/resnet.py for the available aliases/options for the ResNet variants (all models are in Flax)

Pretrained checkpoints from torch.hub are available for the following networks:

The models are tested to have the same intermediate activations and outputs as the torch.hub implementations, except ResNeSt-50 Fast, whose activations don't match exactly but the final accuracy does.

A pretrained checkpoint for ResNetD-50 is available from fast.ai. The activations do not match exactly, but the final accuracy matches.

import jax.numpy as jnp
from jax_resnet import pretrained_resnest

ResNeSt50, variables = pretrained_resnest(50)
model = ResNeSt50()
out = model.apply(variables,
                  jnp.ones((32, 224, 224, 3)),  # ImageNet sized inputs.
                  mutable=False)  # Ensure `batch_stats` aren't updated.

You must install PyTorch yourself (instructions) to use these functions.

Transfer Learning

To extract a subset of the model, you can use Sequential(model.layers[start:end]).

The slice_variables function (found in in common.py) allows you to extract the corresponding subset of the variables dict. Check out that docstring for more information.

Checkpoint Accuracies

The top 1 and top 5 accuracies reported below are on the ImageNet2012 validation split. The data was preprocessed as in the official PyTorch example.

ModelSizeTop 1Top 5
ResNet1869.75%89.06%
3473.29%91.42%
5076.13%92.86%
10177.37%93.53%
15278.30%94.04%
Wide ResNet5078.48%94.08%
10178.88%94.29%
ResNeXt5077.60%93.70%
10179.30%94.51%
ResNet-D5077.57%93.85%
<!-- |ResNeSt | 50| 80.97%| 95.38%| | | 101| 82.17%| 95.97%| | | 200| 82.35%| 96.11%| | | 269| 79.19%| 94.53%| -->

The ResNeSt validation data was preprocessed as in zhang1989/ResNeSt.

ModelSizeCrop SizeTop 1Top 5
ResNeSt-Fast5022480.53%95.34%
ResNeSt5022481.05%95.42%
10125682.82%96.32%
20032083.84%96.86%
26941684.53%96.98%

References