Caffe2 - Python API
A deep learning, cross platform ML framework
concat.py
1 
3 from __future__ import absolute_import
4 from __future__ import division
5 from __future__ import print_function
6 from __future__ import unicode_literals
7 
8 from caffe2.python import schema
9 from caffe2.python.layers.layers import (
10  ModelLayer,
11 )
12 import numpy as np
13 
14 
15 class Concat(ModelLayer):
16 
17  def __init__(self, model, input_record, axis=1,
18  name='concat', **kwargs):
19  super(Concat, self).__init__(model, name, input_record, **kwargs)
20  self.axis = axis
21  assert isinstance(input_record, schema.Struct),\
22  "Incorrect input type. Excpected Struct, but received: {0}".\
23  format(input_record)
24 
25  shapes = []
26  for field_name, field_type in input_record.fields.items():
27  assert isinstance(field_type, schema.Scalar),\
28  "Incorrect input type for {}. Excpected Scalar, but got: {}".\
29  format(field_name, field_type)
30  # Assume that first dimension is batch, so actual axis in shape is
31  # axis - 1
32  assert len(field_type.field_type().shape) >= axis,\
33  "Concat expects that limited dimensions of the input tensor"
34  shapes.append(list(field_type.field_type().shape))
35 
36  concat_dim = 0
37  for shape in shapes:
38  concat_dim += shape[axis - 1]
39  shape[axis - 1] = 0
40  assert shape == shapes[0],\
41  "Shapes {0} and {1} are not compatible for Concat".\
42  format(shape, shapes[0])
43  output_dims = shapes[0]
44  output_dims[axis - 1] = concat_dim
45 
47  (np.float32, output_dims),
48  model.net.NextScopedBlob(name + '_output'))
49 
50  def add_ops(self, net):
51  net.Concat(
52  self.input_record.field_blobs(),
53  [
54  self.output_schema.field_blobs()[0],
55  self.output_schema.field_blobs()[0] + "_concat_dims"
56  ],
57  axis=self.axis,
58  )
def input_record(self)
Definition: layers.py:149