Caffe2 - Python API
A deep learning, cross platform ML framework
split.py
1 ## @package split
2 # Module caffe2.python.layers.split
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 
13 
14 class Split(ModelLayer):
15 
16  def __init__(self, model, input_record, num_splits, axis=1,
17  name='split', **kwargs):
18  super(Split, self).__init__(model, name, input_record, **kwargs)
19  self.axis = axis
20  # Assume that first dimension is batch, so actual axis in shape is
21  # axis - 1
22  axis -= 1
23  assert axis >= 0
24 
25  assert isinstance(input_record, schema.Scalar),\
26  "Incorrect input type. Excpected Scalar, but received: {0}".\
27  format(input_record)
28 
29  input_shape = input_record.field_type().shape
30  assert len(input_shape) >= axis
31  assert input_shape[axis] % num_splits == 0
32 
33  output_shape = list(input_shape)
34  output_shape[axis] = int(output_shape[axis] / num_splits)
35 
36  data_type = input_record.field_type().base
37 
38  output_scalars = [
40  (data_type, output_shape),
41  self.get_next_blob_reference('output_{}'.format(i)),
42  )
43  for i in range(num_splits)
44  ]
45  self.output_schema = schema.Tuple(*output_scalars)
46 
47  def add_ops(self, net):
48  net.Split(
49  self.input_record.field_blobs(),
50  self.output_schema.field_blobs(),
51  axis=self.axis,
52  )