-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there way to import caffe model into tensorlayer #74
Comments
Yes. By using caffe-tensorflow . First, follow the instructions in caffe-tensorflow e.g. :
You can get prototxt and caffemodel here: Model Zoo. Then you get
The |
@JindongJiang what network graph for VGG19 do you use with that loading code? I guess one could also need to transpose the caffe weights as often in tensorflow/tensorlayer the network uses (width, height, channels) format instead of (channels, width, height) as in caffe for example? |
@joekr552 The graph I use comes from one of the TensorLayer examples. Well, without going deeper into caffe-tensorflow's code, I found by experimenting on jupyter notebook that caffe-tensorflow already did that for us. |
Aha, ok! thanks! Did you check the GBR and RGB channel ordering as well? |
@joekr552 I just check caffe-tensorflow code for you. The good news is that they did transpose the c, w, h. def transform_data(self):
if self.params is None:
transformers = [
# Reshape the parameters to TensorFlow's ordering
DataReshaper({
# (c_o, c_i, h, w) -> (h, w, c_i, c_o)
NodeKind.Convolution: (2, 3, 1, 0),
# (c_o, c_i) -> (c_i, c_o)
NodeKind.InnerProduct: (1, 0)
}),
# Pre-process batch normalization data
BatchNormPreprocessor(),
# Convert parameters to dictionaries
ParameterNamer(),
]
self.graph = self.graph.transformed(transformers)
self.params = {node.name: node.data for node in self.graph.nodes if node.data}
return self.params But I can't find the code where they transpose the BGR into RGB, so I think they did not. And that also explain why we should do the transpose by hand at the TL vgg19 example if tf.__version__ <= '0.11':
red, green, blue = tf.split(3, 3, rgb_scaled)
else: # TF 1.0
print(rgb_scaled)
red, green, blue = tf.split(rgb_scaled, 3, 3)
assert red.get_shape().as_list()[1:] == [224, 224, 1]
assert green.get_shape().as_list()[1:] == [224, 224, 1]
assert blue.get_shape().as_list()[1:] == [224, 224, 1]
if tf.__version__ <= '0.11':
bgr = tf.concat(3, [
blue - VGG_MEAN[0],
green - VGG_MEAN[1],
red - VGG_MEAN[2],
])
else:
bgr = tf.concat([
blue - VGG_MEAN[0],
green - VGG_MEAN[1],
red - VGG_MEAN[2],
], axis=3) |
thanks! |
Actually I am a bit confused on whatever the approach used to get the weights used in the vgg16 example in TL is a good approach: I don't think it is a valid approach, the approach you mentioned above does seem correct however. See my discussion post here> https://www.cs.toronto.edu/~frossard/post/vgg16/ |
You're welcome. But the post in your link seems more like a tutorial than an issue, where are your confusion. |
I am referring to my comment at the bottom of the page :
My issue was that in the blog post the author said he post-processed the weights after running the caffe-tensorflow code, which included a step explained as: "..reorder the filters in the first layer so that they are in RGB order (instead of the default BGR. " To me that is a strange thing to do as consecutive higher filters will assume that the first layer filters where in BGR order. An extreme example is that a "red cat" detector would become all confused. The approach you mentioned above, where the input is first transformed to BGR from RGB should be correct though. |
@joekr552 That's a tricky yet reasonable confusion, thank you so much for mention it here. But after a second thought, I figure that maybe the author's method is rational. Let me put it in this way:
That is, all neurons in the H receive some particular area of
in the first hidden layer, we know the later� hidden layer wouldn't face the problem you mentioned. |
Hmm, yes u are right. Thanks for the clarification. |
hi @JindongJiang , Could you please tell how to convert .npz model into .caffemodel. Thanks in advance |
Hi @prabhjot-singh-gogana , sorry for my late response, the graduate application takes much of my time. I might not be so familiar with TensorLayer now since I have switched to Tensorflow and PyTorch. But I think onnx could help converting the model, you may wanna try it. |
Hi @prabhjot-singh-gogana, Have you solved your conversion problem yet? I faced the similar issue about converting numpy .npz format model to .caffemodel. Can I discuss this problem with you? Many thanks for that! |
Dear all:
I find the tensor layer is resemble the caffe structure, both are based model block.
I just wonder if there is a way to load caffe model and initialize tensor layer model?
comments and suggestions are welcome
best regards
The text was updated successfully, but these errors were encountered: