PyTorch and Tensorflow functional model definitions

Overview

functional-zoo

Model definitions and pretrained weights for PyTorch and Tensorflow

PyTorch, unlike lua torch, has autograd in it's core, so using modular structure of torch.nn modules is not necessary, one can easily allocate needed Variables and write a function that utilizes them, which is sometimes more convenient. This repo contains model definitions in this functional way, with pretrained weights for some models.

Weights are serialized as a dict of arrays in hdf5, so should be easily loadable in other frameworks. Thanks to @edgarriba we have cpp_parser for loading weights in C++.

More models coming! We also plan to add definitions for other frameworks in future, probably tiny-dnn first. Contributions are welcome.

See also imagenet classification with PyTorch demo.ipynb

Models

All models were validated to produce reported accuracy using imagenet-validation.py script (depends on OpenCV python bindings).

To load weights in Python first do pip install hickle, then:

import hickle as hkl
weights = hkl.load('resnet-18-export.hkl')

Unfortunately, hickle py3 support is not yet ready, so the models will be resaved in torch pickle format with torch.utils.model_zoo.load_url support, e.g.:

weights = model_zoo.load_url('https://s3.amazonaws.com/modelzoo-networks/wide-resnet-50-2-export-5ae25d50.pth')

Also, make_dot was moved to a separate package: PyTorchViz

Folded

Models below have batch_norm parameters and statistics folded into convolutional layers for speed. It is not recommended to use them for finetuning.

ImageNet

model notebook val error download size
VGG-16 vgg-16.ipynb 30.09, 10.69 url coming 528 MB
NIN nin-export.ipynb 32.96, 12.29 url 33 MB
ResNet-18 (fb) resnet-18-export.ipynb 30.43, 10.76 url 42 MB
ResNet-18-AT resnet-18-at-export.ipynb 29.44, 10.12 url 44.1 MB
ResNet-34 (fb) resnet-34-export.ipynb 26.72, 8.74 url 78.3 MB
WRN-50-2 wide-resnet-50-2-export.ipynb 22.0, 6.05 url 246 MB

Fast Neural Style

Notebook: fast-neural-style.ipynb

Models:

model download size
candy.hkl url 7.1 MB
feathers.hkl url 7.1 MB
wave.hkl url 7.1 MB

Models with batch normalization

Coming

Comments
  • Error in visualize code

    Error in visualize code

    I'm seeing this:

    TypeError: Don't know how to create Python object for N5torch8autograd8VariableE
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 15, in add_nodes
      File "<stdin>", line 15, in add_nodes
      File "<stdin>", line 15, in add_nodes
      File "<stdin>", line 15, in add_nodes
      File "<stdin>", line 11, in add_nodes
    

    For reference with line numbers, I have the function as:

    >>> def add_nodes(var):
    ...         print(var)
    ...         print("didthatone")
    ...         if var not in seen:
    ...             if isinstance(var, Variable):
    ...                 value = '('+(', ').join(['%d'% v for v in var.size()])+')'
    ...                 dot.node(str(id(var)), str(value), fillcolor='lightblue')
    ...             else:
    ...                 dot.node(str(id(var)), str(type(var).__name__))
    ...             seen.add(var)
    ...             if hasattr(var, 'previous_functions'):
    ...                 for u in var.previous_functions:
    ...                     print(u)
    ...                     dot.edge(str(id(u[0])), str(id(var)))
    ...                     add_nodes(u[0])
    
    opened by elbamos 9
  • Issue with output torch Variable in visualize.py throwing an attribute error (grad_fn)

    Issue with output torch Variable in visualize.py throwing an attribute error (grad_fn)

    Hey, so I am facing the same issue as issue #14 . Although what you said is correct about the model.state_dict(), the error about grad_fn arises from var, which is the output variable of the model and not the params dictionary. Any ideas on how to fix this?

    As for the model.state_dict() part, I think it can be converted to the required (name, Variable) format using;

    params = model.state_dict()
    for k in sorted(params.keys()):
    	v = params[k]
    	params[k] = autograd.Variable(v, requires_grad=True)
    

    I printed this out, and it seemed fine.

    Need help to remove the var.grad_fn attribute error though. Thanks.

    opened by ritvikshrivastava 3
  • Show how to use models for fine-tuning

    Show how to use models for fine-tuning

    The readme explains that all the models listed so far have bn params integrated into the conv weights, for performance, thus making these models not useful for fine-tuning. Fine-tuning is, perhaps, the most common thing that people use pre-trained models for, so it would be great it you could do one of the following:

    • Explain how to separate out the bn params in the existing models
    • Show how to re-create the models so that others can do so without the bn param integration
    • Make every model available in both integrated and standard format

    Many thanks for this great project!

    opened by jph00 3
  • Runtime error in make_dot

    Runtime error in make_dot

    Hi,

    thanks a lot for making that function available! However, when I try calling make_dot(loss) I get the error below.

    Any idea how that could be fixed?

    Thanks again!

    ---------------------------------------------------------------------------
    RuntimeError                              Traceback (most recent call last)
    <ipython-input-10-b7f0e150648d> in <module>()
          1 from visualize import make_dot
    ----> 2 make_dot(loss)
    
    /home/carnd/basic_pytorch/visualize.py in make_dot(var, params)
         55                     dot.edge(str(id(t)), str(id(var)))
         56                     add_nodes(t)
    ---> 57     add_nodes(var.grad_fn)
         58     return dot
    
    /home/carnd/basic_pytorch/visualize.py in add_nodes(var)
         50                     if u[0] is not None:
         51                         dot.edge(str(id(u[0])), str(id(var)))
    ---> 52                         add_nodes(u[0])
         53             if hasattr(var, 'saved_tensors'):
         54                 for t in var.saved_tensors:
    
    /home/carnd/basic_pytorch/visualize.py in add_nodes(var)
         50                     if u[0] is not None:
         51                         dot.edge(str(id(u[0])), str(id(var)))
    ---> 52                         add_nodes(u[0])
         53             if hasattr(var, 'saved_tensors'):
         54                 for t in var.saved_tensors:
    
    /home/carnd/basic_pytorch/visualize.py in add_nodes(var)
         50                     if u[0] is not None:
         51                         dot.edge(str(id(u[0])), str(id(var)))
    ---> 52                         add_nodes(u[0])
         53             if hasattr(var, 'saved_tensors'):
         54                 for t in var.saved_tensors:
    
    /home/carnd/basic_pytorch/visualize.py in add_nodes(var)
         50                     if u[0] is not None:
         51                         dot.edge(str(id(u[0])), str(id(var)))
    ---> 52                         add_nodes(u[0])
         53             if hasattr(var, 'saved_tensors'):
         54                 for t in var.saved_tensors:
    
    /home/carnd/basic_pytorch/visualize.py in add_nodes(var)
         50                     if u[0] is not None:
         51                         dot.edge(str(id(u[0])), str(id(var)))
    ---> 52                         add_nodes(u[0])
         53             if hasattr(var, 'saved_tensors'):
         54                 for t in var.saved_tensors:
    
    /home/carnd/basic_pytorch/visualize.py in add_nodes(var)
         51                         dot.edge(str(id(u[0])), str(id(var)))
         52                         add_nodes(u[0])
    ---> 53             if hasattr(var, 'saved_tensors'):
         54                 for t in var.saved_tensors:
         55                     dot.edge(str(id(t)), str(id(var)))
    
    RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.
    
    opened by ZmeiGorynych 2
  • tensorflow image input format and means

    tensorflow image input format and means

    Hi @szagoruyko,

    First of all thanks for converting the wide resnet model to tensorflow :)

    I have some question about the details in WRN50 model:

    1. The Input image, is it RGB, BGR, or others? I saw the inputs.permute(0,2,3,1) and its kinda unintuitive.
    2. What about image mean?
    opened by ardiya 2
  • Update visualizer

    Update visualizer

    As pytorch autograd has been refactored, old visualizer doesn't work, so this fixes and adds new nodes:

    • blue nodes are the ones that need grad
    • orange nodes hold tensors in save_for_backward, useful for tracking memory usage For now I don't add None nodes for nodes that don't need grad, which would be nice to fix, but requires changes in autograd engine.

    fixes https://github.com/szagoruyko/functional-zoo/issues/7 will merge as soon as https://github.com/pytorch/pytorch/pull/1506 is in

    screen shot 2017-05-08 at 09 14 09
    opened by szagoruyko 2
  • Preprocessing to use with pretrained weights

    Preprocessing to use with pretrained weights

    Hello, thanks for sharing your code!

    I'd like to know what preprocessing you've used to produce the ImageNet weights. Can you confirm that you used meanstd normalization?

    All the best, Carl

    opened by lemairecarl 1
  • Where is the wide-resnet-50-2-export.pth?

    Where is the wide-resnet-50-2-export.pth?

    I try to export the wide-resnet-50-2 in tensorflow by using your code in https://github.com/szagoruyko/functional-zoo/blob/master/wide-resnet-50-2-export.ipynb But it seems like I cannot find out where to download the paramters, can you tell me where the .pth is ?

    opened by dzk9528 1
  • get AttributeError: grad_fn when use make_dot

    get AttributeError: grad_fn when use make_dot

    when I use following code to visualize the torch pretrained resnet18 model, I get AttributeError: grad_fn, at line: add_nodes(var.grad_fn)

    input=torch.randn(1,3,224,224)  
    model=models.resnet18(pretrained=True)
    for param in model.parameters():
         param.requires_grad = True
    params = model.state_dict()
    y=model(Variable(input))
    print y
    dot=make_dot(y, params)
    
    opened by Pinnh 1
  • Error reading a hdf5 file cpp_parser

    Error reading a hdf5 file cpp_parser

    HDF5-DIAG: Error detected in HDF5 (1.8.16) thread 139655733122880:
      #000: ../../../src/H5G.c line 467 in H5Gopen2(): unable to open group
        major: Symbol table
        minor: Can't open object
      #001: ../../../src/H5Gint.c line 320 in H5G__open_name(): group not found
        major: Symbol table
        minor: Object not found
      #002: ../../../src/H5Gloc.c line 430 in H5G_loc_find(): can't find object
        major: Symbol table
        minor: Object not found
      #003: ../../../src/H5Gtraverse.c line 861 in H5G_traverse(): internal path traversal failed
        major: Symbol table
        minor: Object not found
      #004: ../../../src/H5Gtraverse.c line 641 in H5G_traverse_real(): traversal operator failed
        major: Symbol table
        minor: Callback failed
      #005: ../../../src/H5Gloc.c line 385 in H5G_loc_find_cb(): object 'data_0' doesn't exist
        major: Symbol table
        minor: Object not found
    terminate called after throwing an instance of 'H5::FileIException'
    Aborted (core dumped)
    
    

    I am trying to read the weights of yolo network ./hdf5_app /home/myst/yolo/yolo.h5

    opened by srk97 1
  • Update attribute names in visualize.py

    Update attribute names in visualize.py

    In the latest version of PyTorch, the 'previous_functions' attribute has been renamed to 'next_functions' and the 'creator' attribute has been renamed to 'grad_fn'. This PR accounts for this while not breaking existing functionality.

    opened by mjacar 1
  • RuntimeError: Cannot open file. This file was likely created with Python 2 and an old hickle version.

    RuntimeError: Cannot open file. This file was likely created with Python 2 and an old hickle version.

    File "/usr/local/lib/python3.6/dist-packages/hickle/hickle.py", line 526, in load raise RuntimeError("Cannot open file. This file was likely" RuntimeError: Cannot open file. This file was likely created with Python 2 and an old hickle version.

    How can we debug in this situation above? It is problem of using python3 but unsupported from hickle.

    hickle.py

     elif VER_MAJOR == 2:
                        if six.PY2:
                            warnings.warn("Hickle file appears to be old version (v2), attempting legacy loading...")
                            import hickle_legacy2
                            return hickle_legacy2.load(fileobj, safe=safe)
                        else:
                            raise RuntimeError("Cannot open file. This file was likely"
                                               " created with Python 2 and an old hickle version.")
                    # There is an unfortunate period of time where hickle 2.1.0 claims VERSION = int(3)
                    # For backward compatibility we really need to catch this.
                    # Actual hickle v3 files are versioned as A.B.C (e.g. 3.1.0)
    
    opened by derelearnro 0
  • Pretrained model

    Pretrained model

    When trying to load the weights of the pretrained wide resnet for ImageNet, using Tensorflow, I get the error message: "RuntimeError: Cannot open file. This file was likely created with Python 2 and an old hickle version."

    I haven't found any solution to this. Can you help me find weights that can be used with python 3.6? Thanks, Jenny

    opened by jennyzu 0
  • need torch in order to run

    need torch in order to run "Tensorflow model definition"

    Hi,

    In the "Tensorflow model definition" section, the parameters are taken from torch: (params = {k: v.numpy() for k,v in torch.load('wide-resnet-50-2-export.pth').items()})

    How can I run this without using torch? Thanks!

    opened by jennyzu 0
  • Extract features from pre-trained model

    Extract features from pre-trained model

    To extract features from an arbitrary layer with say, VGG19, given an input image "elephant.jpg", the code in keras+TF is simply:

    from keras.applications.vgg19 import VGG19 from keras.preprocessing import image from keras.applications.vgg19 import preprocess_input from keras.models import Model import numpy as np

    base_model = VGG19(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('block4_pool').output)

    img_path = 'elephant.jpg'

    img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x)

    block4_pool_features = model.predict(x)

    This can be turned into a function to support multiple calls. How is this achieved in PyTorch?

    opened by dineshbvadhia 1
Owner
Sergey Zagoruyko
Sergey Zagoruyko
This repo is the code release of EMNLP 2021 conference paper "Connect-the-Dots: Bridging Semantics between Words and Definitions via Aligning Word Sense Inventories".

Connect-the-Dots: Bridging Semantics between Words and Definitions via Aligning Word Sense Inventories This repo is the code release of EMNLP 2021 con

null 12 Nov 22, 2022
Functional TensorFlow Implementation of Singular Value Decomposition for paper Fast Graph Learning

tf-fsvd TensorFlow Implementation of Functional Singular Value Decomposition for paper Fast Graph Learning with Unique Optimal Solutions Cite If you f

Sami Abu-El-Haija 14 Nov 25, 2021
In this project we investigate the performance of the SetCon model on realistic video footage. Therefore, we implemented the model in PyTorch and tested the model on two example videos.

Contrastive Learning of Object Representations Supervisor: Prof. Dr. Gemma Roig Institutions: Goethe University CVAI - Computational Vision & Artifici

Dirk Neuhäuser 6 Dec 8, 2022
Robust Video Matting in PyTorch, TensorFlow, TensorFlow.js, ONNX, CoreML!

Robust Video Matting in PyTorch, TensorFlow, TensorFlow.js, ONNX, CoreML!

Peter Lin 6.5k Jan 4, 2023
Robust Video Matting in PyTorch, TensorFlow, TensorFlow.js, ONNX, CoreML!

Robust Video Matting (RVM) English | 中文 Official repository for the paper Robust High-Resolution Video Matting with Temporal Guidance. RVM is specific

flow-dev 2 Aug 21, 2022
🔮 A refreshing functional take on deep learning, compatible with your favorite libraries

Thinc: A refreshing functional take on deep learning, compatible with your favorite libraries From the makers of spaCy, Prodigy and FastAPI Thinc is a

Explosion 2.6k Dec 30, 2022
fklearn: Functional Machine Learning

fklearn: Functional Machine Learning fklearn uses functional programming principles to make it easier to solve real problems with Machine Learning. Th

nubank 1.4k Dec 7, 2022
Deep functional residue identification

DeepFRI Deep functional residue identification Citing @article {Gligorijevic2019, author = {Gligorijevic, Vladimir and Renfrew, P. Douglas and Koscio

Flatiron Institute 156 Dec 25, 2022
Data from "HateCheck: Functional Tests for Hate Speech Detection Models" (Röttger et al., ACL 2021)

In this repo, you can find the data from our ACL 2021 paper "HateCheck: Functional Tests for Hate Speech Detection Models". "test_suite_cases.csv" con

Paul Röttger 43 Nov 11, 2022
Recovering Brain Structure Network Using Functional Connectivity

Recovering-Brain-Structure-Network-Using-Functional-Connectivity Framework: Papers: This repository provides a PyTorch implementation of the models ad

null 5 Nov 30, 2022
MMdnn is a set of tools to help users inter-operate among different deep learning frameworks. E.g. model conversion and visualization. Convert models between Caffe, Keras, MXNet, Tensorflow, CNTK, PyTorch Onnx and CoreML.

MMdnn MMdnn is a comprehensive and cross-framework tool to convert, visualize and diagnose deep learning (DL) models. The "MM" stands for model manage

Microsoft 5.7k Jan 9, 2023
Step by Step on how to create an vision recognition model using LOBE.ai, export the model and run the model in an Azure Function

Step by Step on how to create an vision recognition model using LOBE.ai, export the model and run the model in an Azure Function

El Bruno 3 Mar 30, 2022
Deploy tensorflow graphs for fast evaluation and export to tensorflow-less environments running numpy.

Deploy tensorflow graphs for fast evaluation and export to tensorflow-less environments running numpy. Now with tensorflow 1.0 support. Evaluation usa

Marcel R. 349 Aug 6, 2022
TensorFlow Ranking is a library for Learning-to-Rank (LTR) techniques on the TensorFlow platform

TensorFlow Ranking is a library for Learning-to-Rank (LTR) techniques on the TensorFlow platform

null 2.6k Jan 4, 2023
Convert Pytorch model to onnx or tflite, and the converted model can be visualized by Netron

Convert Pytorch model to onnx or tflite, and the converted model can be visualized by Netron

Roxbili 5 Nov 19, 2022
😇A pyTorch implementation of the DeepMoji model: state-of-the-art deep learning model for analyzing sentiment, emotion, sarcasm etc

------ Update September 2018 ------ It's been a year since TorchMoji and DeepMoji were released. We're trying to understand how it's being used such t

Hugging Face 865 Dec 24, 2022
An efficient and effective learning to rank algorithm by mining information across ranking candidates. This repository contains the tensorflow implementation of SERank model. The code is developed based on TF-Ranking.

SERank An efficient and effective learning to rank algorithm by mining information across ranking candidates. This repository contains the tensorflow

Zhihu 44 Oct 20, 2022
Python script for performing depth completion from sparse depth and rgb images using the msg_chn_wacv20. model in Tensorflow Lite.

TFLite-msg_chn_wacv20-depth-completion Python script for performing depth completion from sparse depth and rgb images using the msg_chn_wacv20. model

Ibai Gorordo 2 Oct 4, 2021