tensorflow to caffe, reshape layer
Platform (like ubuntu 16.04/win10): ubuntu 16.04 Python version: 3.5.5 Source framework with version (like Tensorflow 1.4.1 with GPU): tensorflow Destination framework with version (like CNTK 2.3 with GPU): caffe Pre-trained model path (webpath or webdisk path):
Running scripts:
Hello I have a question about reshape layer when convert tensorflow to caffe. The code in caffe_emitter.py is
def emit_Reshape(self, IR_node):
shape = IR_node.get_attr("_output_shapes")[0]
shape = shape_to_list(shape)
if shape:
dim_str = "'dim': {}".format(shape)
dim_str = " reshape_param={'shape': { " + dim_str + '} }'
self.add_body(1, "n.{:<15} = L.Reshape(n.{}, {})".format(
IR_node.variable_name,
self.parent_variable_name(IR_node),
dim_str
))
else:
IR_node.real_name = self.IR_graph.get_parent(IR_node.name, [0]).real_name
As we know, format of IR is NHWC, so the format of shape must be NHWC, like [1,20,20,64], but when mmdnn convert the reshape layer, it just do a shape_to_list operation, is it right? If so, there may be problems
tensorflow caffe
A layer [1,20,20,64] [1,64,2020]
reshape layer [1,10,10,256] [1,10,10,256]
B layer
So, from reshape layer, output shape of tensorflow is the same as output shape of caffe, but the data in memory is not the same. So how to solve the problem?
Hi @hnuhchen , thanks for your feedback, actually MMdnn does have some uncertainty on converting Reshape op from NHWC to NCHW. Reshape op is not like Concat, where we can just change the axis=3 to axis=1. Since the output_shape of Reshape is diverse, it's hard to locate NHWC data in NCHW format after Reshape, espicially when the output_shape is not 4-dims. We will look into this problem, and if you have some advice, make a pull request and we will test it.
@XiaoXYe thank you for you reply
I have a advice when convert reshape layer with 4-D output_shape.
tensorflow caffe
A layer [1,20,20,64] [1,64,20,20]
[1,20,20,64] # caffe permute(0,2,3,1)
reshape layer [1,10,10,256] [1,10,10,256]
[1,256,10,10] # caffe permute(0,3,1,2)
B layer
So, caffe model will add other two Permute layers. Can you help me whether it is right or not?
And i have no idea about how to convert reshape layer when the output_shape is not 4-D.
@hnuhchen Yes, it is a good idea to convert reshape layer when the input and output shape are both 4-D. Thanks for your advice.
@XiaoXYe, yes, but when the source framework is pytorch, we cannot do that. So IRToCaffe process should know what the source framework is, pytorch or tensorflow?
@hnuhchen No, we dont need to know the source framework format because MMdnn IR always have NHWC format.
@XiaoXYe, thank you, expect your solution.