mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary:
To achive this, I modified the blob name scheme defined in a layer.
Before it was scope/fc_w and scope/fc_w_auto_0 (if there is another fc
within the same scope).
Now I change it to scope/fc/w and scope/fc_auto_0/w.
That is, we rely on the uniqueness of the scoped layer name to define
names for blobs.
I also overwrote the create_param method in LayerModelHelper to let it
use the resolved name for blobs given the sharingparameter context.
There are some details such as making the initializer more structured
that I need to finalize.
Reviewed By: kennyhorror
Differential Revision: D5435132
fbshipit-source-id: a0525f5ea0977e255dd5ea765b38913f5951d455
75 lines
2.5 KiB
Python
75 lines
2.5 KiB
Python
## @package concat
|
|
# Module caffe2.python.layers.concat
|
|
from __future__ import absolute_import
|
|
from __future__ import division
|
|
from __future__ import print_function
|
|
from __future__ import unicode_literals
|
|
|
|
from caffe2.python import schema
|
|
from caffe2.python.layers.layers import (
|
|
ModelLayer,
|
|
)
|
|
from future.utils import viewitems
|
|
import numpy as np
|
|
|
|
|
|
class Concat(ModelLayer):
|
|
|
|
def __init__(self, model, input_record, axis=1, add_axis=0,
|
|
name='concat', **kwargs):
|
|
super(Concat, self).__init__(model, name, input_record, **kwargs)
|
|
self.axis = axis
|
|
self.add_axis = add_axis
|
|
assert not (axis == 0 and add_axis == 1), \
|
|
"It's not allowed to add axis=0"
|
|
assert isinstance(input_record, schema.Struct),\
|
|
"Incorrect input type. Excpected Struct, but received: {0}".\
|
|
format(input_record)
|
|
|
|
shapes = []
|
|
for field_name, field_type in viewitems(input_record.fields):
|
|
assert isinstance(field_type, schema.Scalar),\
|
|
"Incorrect input type for {}. Excpected Scalar, but got: {}".\
|
|
format(field_name, field_type)
|
|
# Assume that first dimension is batch, so actual axis in shape is
|
|
# axis - 1
|
|
assert len(field_type.field_type().shape) >= axis,\
|
|
"Concat expects that limited dimensions of the input tensor"
|
|
shapes.append(list(field_type.field_type().shape))
|
|
|
|
if add_axis:
|
|
for i in range(len(shapes)):
|
|
shapes[i].insert(axis, 1)
|
|
|
|
if axis == 0:
|
|
self.output_schema = schema.from_blob_list(
|
|
input_record[0],
|
|
[self.get_next_blob_reference('output')]
|
|
)
|
|
return
|
|
|
|
concat_dim = 0
|
|
for shape in shapes:
|
|
concat_dim += shape[axis - 1]
|
|
shape[axis - 1] = 0
|
|
assert shape == shapes[0],\
|
|
"Shapes {0} and {1} are not compatible for Concat".\
|
|
format(shape, shapes[0])
|
|
output_dims = shapes[0]
|
|
output_dims[axis - 1] = concat_dim
|
|
|
|
self.output_schema = schema.Scalar(
|
|
(np.float32, output_dims),
|
|
self.get_next_blob_reference('output'))
|
|
|
|
def add_ops(self, net):
|
|
net.Concat(
|
|
self.input_record.field_blobs(),
|
|
[
|
|
self.output_schema.field_blobs()[0],
|
|
self.output_schema.field_blobs()[0] + "_concat_dims"
|
|
],
|
|
axis=self.axis,
|
|
add_axis=self.add_axis,
|
|
)
|