szagoruyko / functional-zoo Goto Github PK
View Code? Open in Web Editor NEWPyTorch and Tensorflow functional model definitions
PyTorch and Tensorflow functional model definitions
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?
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 ?
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
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!
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
The visualizer has been working properly, so good.
But after I updated pytorch, a model containing Conv2d is not property rendered:
The first Conv2d layer is shown as 'ConvNdBackward' and the graph is not traversed further.
If I remove all Conv2d, it works well again.
Thank you.
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)
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)
Hi @szagoruyko,
First of all thanks for converting the wide resnet model to tensorflow :)
I have some question about the details in WRN50 model:
inputs.permute(0,2,3,1)
and its kinda unintuitive.Hi, methods previous_functions
and creator
of class Variable seem not to exist anymore.
Could there be any substitute?
Best,
Anton
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
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.
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])
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.
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:
Many thanks for this great project!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.