← all repositories
gmalivenko/pytorch2keras

Escape PyTorch's gravity without rewriting your model

A converter that traces your PyTorch graph and emits a Keras model, because sometimes you need TensorFlow's ecosystem without the rewrite.

pytorch2keras
Velocity · 7d
+0.3
★ / day
Trend
steady
star history

What it does

pytorch2keras takes a PyTorch nn.Module, traces it with jit.trace using a dummy input, and walks the resulting graph to build an equivalent Keras model. You get a standard Keras object you can save, inspect, or push through the rest of the TensorFlow pipeline—including, with some elbow grease, TensorFlow.js.

The interesting bit

The converter doesn’t just dump weights; it reconstructs the computation graph layer by layer, mapping PyTorch ops to Keras equivalents. It also tries to be shape-agnostic: set H and W to None and it claims to handle fully convolutional networks. The TensorFlow.js path is documented in unusual detail—complete with a StackOverflow-derived freeze_session helper—suggesting the author actually suffered through this workflow personally.

Key highlights

  • Supports ResNet, VGG, DenseNet, MobileNet v2, and other standard architectures
  • Handles common 2D convolutions, normalizations, activations, and element-wise ops
  • names='short' flag specifically for TensorFlow.js compatibility
  • Shape-agnostic mode for variable input sizes (experimental)
  • MIT licensed, pip-installable

Caveats

  • Requires channels_first data format in your Keras config; the README calls this an “important notice” but it’s really a hard requirement
  • change_ordering and name_policy features are marked experimental
  • No ONNX involvement despite the topic tag—this is pure graph tracing, not IR translation
  • Python 2.7 still listed as supported, which may signal maintenance lag

Verdict

Worth a look if you’re stuck with a PyTorch-trained model and a deployment target that only speaks Keras or TF.js. Skip it if you’re starting fresh—pick one framework and commit.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.