Beispiel #1
0
    def _build(self):
        height, width, channels = (self._max_len, self._voc_size, 1)
        output_channels = 8

        self._conv1 = ph.Conv2D('conv1',
                                input_size=(height, width, channels),
                                output_channels=output_channels,
                                filter_height=3,
                                filter_width=width,
                                stride_height=1,
                                stride_width=1)
        self._pool1 = ph.Pool2D('pool1',
                                input_size=(height, width, channels),
                                filter_height=3,
                                filter_width=3,
                                stride_height=2,
                                stride_width=2,
                                pool_type='avg')
        height, width, channels = self._pool1.output_size
        output_channels *= 2

        self._conv2 = ph.Conv2D('conv2',
                                input_size=(height, width, channels),
                                output_channels=output_channels,
                                filter_height=3,
                                filter_width=width,
                                stride_height=1,
                                stride_width=1)
        self._pool2 = ph.Pool2D('pool2',
                                input_size=(height, width, channels),
                                filter_height=3,
                                filter_width=3,
                                stride_height=2,
                                stride_width=2,
                                pool_type='avg')
        height, width, channels = self._pool2.output_size
        output_channels *= 2

        self._conv3 = ph.Conv2D('conv3',
                                input_size=(height, width, channels),
                                output_channels=output_channels,
                                filter_height=3,
                                filter_width=width,
                                stride_height=1,
                                stride_width=1)
        self._pool3 = ph.Pool2D('pool3',
                                input_size=(height, width, channels),
                                filter_height=3,
                                filter_width=3,
                                stride_height=2,
                                stride_width=2,
                                pool_type='avg')

        self._dense1 = ph.Linear('dense1',
                                 input_size=self._pool3.flat_size,
                                 output_size=self._hidden_size)

        self._dense2 = ph.Linear('dense2',
                                 input_size=self._hidden_size,
                                 output_size=1)
Beispiel #2
0
 def _build(self):
     self._layers = list()
     input_size = (self._height, self._width, self._channels)
     output_channels = self._output_channels1
     for i in range(self._num_layers):
         layer = ph.Conv2D(
             'conv%d' % (i + 1),
             input_size, output_channels,
             self._kernel_size, self._kernel_size,
             2, 2
         )
         self._layers.append(layer)
         input_size = layer.output_size
         output_channels *= 2
     self._fc = ph.Linear('fc', self._layers[-1].flat_size, self._output_size)
Beispiel #3
0
    def _build(self):
        if not isinstance(self._channels, (tuple, list)):
            self._channels = [self._channels]

        self._char_emb = ph.Embedding('char_emb',
                                      voc_size=self._voc_size,
                                      emb_size=self._emb_size)

        self._conv_layers = []
        current_size = self._emb_size
        for i, state_size in enumerate([*self._channels, self._emb_size]):
            layer = ph.Conv2D(
                f'conv2d_{i}',
                input_size=(None, None, current_size),
                output_channels=state_size,
                filter_height=self._kernel_height,
                filter_width=1,
                stride_height=1,
                stride_width=1,
            )
            self._conv_layers.append(layer)
            current_size = state_size

        self._norm = LayerNorm('norm', size=self._emb_size)
Beispiel #4
0
 def _build(self):
     # conv1 padding=SAME
     self._conv1_1 = ph.Conv2D('conv1_1',
                               input_size=[self._height, self._width, 3],
                               output_channels=64,
                               filter_height=3,
                               filter_width=3,
                               stride_width=1,
                               stride_height=1,
                               padding='SAME')
     # conv1_2 padding=SAME
     self._conv1_2 = ph.Conv2D('conv1_2',
                               input_size=self._conv1_1.output_size,
                               output_channels=64,
                               filter_height=3,
                               filter_width=3,
                               stride_width=1,
                               stride_height=1,
                               padding='SAME')
     self._pool1 = ph.Pool2D('pool1',
                             input_size=self._conv1_2.output_size,
                             filter_height=2,
                             filter_width=2,
                             stride_height=2,
                             stride_width=2,
                             padding='SAME',
                             pool_type='max')
     #
     # conv2 padding=SAME
     self._conv2_1 = ph.Conv2D('conv2_1',
                               input_size=self._pool1.output_size,
                               output_channels=128,
                               filter_height=3,
                               filter_width=3,
                               stride_width=1,
                               stride_height=1,
                               padding='SAME')
     self._conv2_2 = ph.Conv2D('conv2_2',
                               input_size=self._conv2_1.output_size,
                               output_channels=128,
                               filter_height=3,
                               filter_width=3,
                               stride_width=1,
                               stride_height=1,
                               padding='SAME')
     self._pool2 = ph.Pool2D('pool2',
                             input_size=self._conv2_2.output_size,
                             filter_height=2,
                             filter_width=2,
                             stride_height=2,
                             stride_width=2,
                             padding='SAME',
                             pool_type='max')
     #
     # conv3 padding=SAME
     self._conv3_1 = ph.Conv2D('conv3_1',
                               input_size=self._pool2.output_size,
                               output_channels=256,
                               filter_height=3,
                               filter_width=3,
                               stride_width=1,
                               stride_height=1,
                               padding='SAME')
     self._conv3_2 = ph.Conv2D('conv3_2',
                               input_size=self._conv3_1.output_size,
                               output_channels=256,
                               filter_height=3,
                               filter_width=3,
                               stride_width=1,
                               stride_height=1,
                               padding='SAME')
     self._conv3_3 = ph.Conv2D('conv3_3',
                               input_size=self._conv3_2.output_size,
                               output_channels=256,
                               filter_height=3,
                               filter_width=3,
                               stride_width=1,
                               stride_height=1,
                               padding='SAME')
     self._pool3 = ph.Pool2D('pool3',
                             input_size=self._conv3_3.output_size,
                             filter_height=2,
                             filter_width=2,
                             stride_height=2,
                             stride_width=2,
                             padding='SAME',
                             pool_type='max')
     #
     # conv4 padding=SAME
     self._conv4_1 = ph.Conv2D('conv4_1',
                               input_size=self._pool3.output_size,
                               output_channels=512,
                               filter_height=3,
                               filter_width=3,
                               stride_width=1,
                               stride_height=1,
                               padding='SAME')
     self._conv4_2 = ph.Conv2D('conv4_2',
                               input_size=self._conv4_1.output_size,
                               output_channels=512,
                               filter_height=3,
                               filter_width=3,
                               stride_width=1,
                               stride_height=1,
                               padding='SAME')
     self._conv4_3 = ph.Conv2D('conv4_3',
                               input_size=self._conv4_2.output_size,
                               output_channels=512,
                               filter_height=3,
                               filter_width=3,
                               stride_width=1,
                               stride_height=1,
                               padding='SAME')
     self._pool4 = ph.Pool2D('pool4',
                             input_size=self._conv4_3.output_size,
                             filter_height=2,
                             filter_width=2,
                             stride_height=2,
                             stride_width=2,
                             padding='SAME',
                             pool_type='max')
     #
     # conv5 padding=SAME
     self._conv5_1 = ph.Conv2D('conv5_1',
                               input_size=self._pool4.output_size,
                               output_channels=512,
                               filter_height=3,
                               filter_width=3,
                               stride_width=1,
                               stride_height=1,
                               padding='SAME')
     self._conv5_2 = ph.Conv2D('conv5_2',
                               input_size=self._conv5_1.output_size,
                               output_channels=512,
                               filter_height=3,
                               filter_width=3,
                               stride_width=1,
                               stride_height=1,
                               padding='SAME')
     self._conv5_3 = ph.Conv2D('conv5_3',
                               input_size=self._conv5_2.output_size,
                               output_channels=512,
                               filter_height=3,
                               filter_width=3,
                               stride_width=1,
                               stride_height=1,
                               padding='SAME')
     self._pool5 = ph.Pool2D('pool5',
                             input_size=self._conv5_3.output_size,
                             filter_height=2,
                             filter_width=2,
                             stride_height=2,
                             stride_width=2,
                             padding='SAME',
                             pool_type='max')
     #
     # fc layer
     self._fc6 = ph.Linear('fc6',
                           input_size=self._pool5.flat_size,
                           output_size=4096)
     self._fc7 = ph.Linear('fc7',
                           input_size=self._fc6.output_size,
                           output_size=4096)
     self._fc8 = ph.Linear('fc8',
                           input_size=self._fc7.output_size,
                           output_size=1000,
                           w_init=ph.init.RandomNormal(stddev=1e-4))
Beispiel #5
0
 def _build(self):
     ################################################################################
     # -> (55, 55, 96)
     # -> (27, 27, 96)
     ################################################################################
     self._conv_1 = ph.Conv2D(
         'conv_1',
         input_size=[self._height, self._width, 3],
         output_channels=96,
         filter_height=11, filter_width=11, stride_width=4, stride_height=4,
         padding='VALID'
     )
     self._pool_1 = ph.Pool2D(
         'pool_1',
         input_size=self._conv_1.output_size,
         filter_height=3, filter_width=3, stride_height=2, stride_width=2,
         padding='VALID',
         pool_type='max'
     )
     ################################################################################
     # -> (27, 27, 256)
     # -> (13, 13, 256)
     ################################################################################
     self._conv_2 = ph.GroupConv2D(
         'conv_2',
         input_size=self._pool_1.output_size,
         output_channels=256,
         num_groups=2,
         filter_height=5, filter_width=5, stride_height=1, stride_width=1,
         padding='SAME'
     )
     self._pool_2 = ph.Pool2D(
         'pool_2',
         input_size=self._conv_2.output_size,
         filter_height=3, filter_width=3, stride_height=2, stride_width=2,
         padding='VALID', pool_type='max'
     )
     ################################################################################
     # -> (13, 13, 384)
     ################################################################################
     self._conv_3 = ph.Conv2D(
         'conv_3',
         input_size=self._pool_2.output_size,
         output_channels=384,
         filter_width=3, filter_height=3, stride_width=1, stride_height=1,
         padding='SAME'
     )
     ################################################################################
     # -> (13, 13, 384)
     ################################################################################
     self._conv_4 = ph.GroupConv2D(
         'conv_4',
         input_size=self._conv_3.output_size,
         output_channels=384,
         num_groups=2,
         filter_width=3, filter_height=3, stride_width=1, stride_height=1,
         padding='SAME'
     )
     ################################################################################
     # -> (13, 13, 256)
     # -> (6, 6, 256)
     ################################################################################
     self._conv_5 = ph.GroupConv2D(
         'conv_5',
         input_size=self._conv_4.output_size,
         output_channels=256,
         num_groups=2,
         filter_width=3, filter_height=3, stride_width=1, stride_height=1,
         padding='SAME'
     )
     self._pool_5 = ph.Pool2D(
         'pool_5',
         input_size=self._conv_5.output_size,
         filter_height=3, filter_width=3, stride_height=2, stride_width=2,
         padding='VALID', pool_type='max'
     )
     #
     # fc layer
     self._dense_6 = ph.Linear('dense_6', input_size=self._pool_5.flat_size, output_size=4096)
     self._dense_7 = ph.Linear('dense_7', input_size=self._dense_6.output_size, output_size=4096)
     self._dense_8 = ph.Linear('dense_8', input_size=self._dense_7.output_size, output_size=1000)
Beispiel #6
0
 def _build(self):
     #
     # conv1 padding=VALID
     self._conv1 = ph.Conv2D('conv1',
                             input_size=[self._height, self._width, 3],
                             output_channels=96,
                             filter_height=11,
                             filter_width=11,
                             stride_width=4,
                             stride_height=4,
                             padding='VALID')
     self._pool1 = ph.Pool2D('pool1',
                             input_size=self._conv1.output_size,
                             filter_height=3,
                             filter_width=3,
                             stride_height=2,
                             stride_width=2,
                             padding='VALID',
                             pool_type='max')
     #
     # conv2, 这里是拆分训练的
     self._conv2 = ph.GroupConv2D('conv2',
                                  input_size=self._pool1.output_size,
                                  output_channels=256,
                                  num_groups=2,
                                  filter_height=5,
                                  filter_width=5,
                                  stride_height=1,
                                  stride_width=1)
     self._pool2 = ph.Pool2D('pool2',
                             input_size=self._conv2.output_size,
                             filter_height=3,
                             filter_width=3,
                             stride_height=2,
                             stride_width=2,
                             padding='VALID',
                             pool_type='max')
     #
     # conv3
     self._conv3 = ph.Conv2D('conv3',
                             input_size=self._pool2.output_size,
                             output_channels=384,
                             filter_width=3,
                             filter_height=3,
                             stride_width=1,
                             stride_height=1)
     #
     # conv4, 这里是拆分训练的
     self._conv4 = ph.GroupConv2D('conv4',
                                  input_size=self._conv3.output_size,
                                  output_channels=384,
                                  num_groups=2,
                                  filter_width=3,
                                  filter_height=3,
                                  stride_width=1,
                                  stride_height=1)
     #
     # conv5, 这里是拆分训练的
     self._conv5 = ph.GroupConv2D('conv5',
                                  input_size=self._conv4.output_size,
                                  output_channels=256,
                                  num_groups=2,
                                  filter_width=3,
                                  filter_height=3,
                                  stride_width=1,
                                  stride_height=1)
     self._pool5 = ph.Pool2D('pool5',
                             input_size=self._conv5.output_size,
                             filter_height=3,
                             filter_width=3,
                             stride_height=2,
                             stride_width=2,
                             padding='VALID',
                             pool_type='max')
     #
     # fc layer
     self._fc6 = ph.Linear('fc6',
                           input_size=self._pool5.flat_size,
                           output_size=4096)
     self._fc7 = ph.Linear('fc7',
                           input_size=self._fc6.output_size,
                           output_size=4096)
     self._fc8 = ph.Linear('fc8',
                           input_size=self._fc7.output_size,
                           output_size=1000,
                           w_init=ph.RandomNormal(stddev=1e-4))
     print(self._fc8.output_size)
Beispiel #7
0
 def _build(self):
     self._c1 = ph.Conv2D('c1', self._input_size, 64, 3, 3)
     self._c2 = ph.Conv2D('c2', self._c1.output_size, 64, 3, 3)
     self._p1 = ph.Pool2D('p1', self._c2.output_size, 2, 2)
     #
     self._c3 = ph.Conv2D('c3', self._p1.output_size, 128, 3, 3)
     self._c4 = ph.Conv2D('c4', self._c3.output_size, 128, 3, 3)
     self._p2 = ph.Pool2D('p2', self._c4.output_size, 2, 2)
     #
     self._c5 = ph.Conv2D('c5', self._p2.output_size, 256, 3, 3)
     self._c6 = ph.Conv2D('c6', self._c5.output_size, 256, 3, 3)
     self._c7 = ph.Conv2D('c7', self._c6.output_size, 256, 3, 3)
     self._p3 = ph.Pool2D('p3', self._c7.output_size, 2, 2)
     #
     self._c8 = ph.Conv2D('c8', self._p3.output_size, 512, 3, 3)
     self._c9 = ph.Conv2D('c9', self._c8.output_size, 512, 3, 3)
     self._c10 = ph.Conv2D('c10', self._c9.output_size, 512, 3, 3)
     self._p4 = ph.Pool2D('p4', self._c10.output_size, 2, 2)
     #
     self._c11 = ph.Conv2D('c11', self._p4.output_size, 512, 3, 3)
     self._c12 = ph.Conv2D('c12', self._c11.output_size, 512, 3, 3)
     self._c13 = ph.Conv2D('c13', self._c12.output_size, 512, 3, 3)
     self._p5 = ph.Pool2D('p5', self._c13.output_size, 2, 2)
     #
     self._h1 = ph.Linear('h1',
                          self._p5.flat_size,
                          4096,
                          weight_initializer=ph.RandomNormal(stddev=1e-4))
     self._h2 = ph.Linear('h2',
                          self._h1.output_size,
                          4096,
                          weight_initializer=ph.RandomNormal(stddev=1e-4))
     self._h3 = ph.Linear('h3',
                          self._h2.output_size,
                          self._output_size,
                          weight_initializer=ph.RandomNormal(stddev=1e-4))