Exemplo n.º 1
0
    def test_model22_1(self):
        try:
            import onnx
        except:
            unittest.TestCase.skipTest(self, "onnx not found in the libraries")
        from onnx import numpy_helper
        import numpy as np

        model1 = Sequential(self.s, model_table='Simple_CNN1')
        model1.add(InputLayer(3, 224, 224))
        model1.add(Conv2d(8, 7, act='identity', include_bias=False))
        model1.add(Reshape(height=448, width=448, depth=2))
        model1.add(Dense(2))
        model1.add(OutputLayer(act='softmax', n=2))

        if self.data_dir is None:
            unittest.TestCase.skipTest(self, "DLPY_DATA_DIR is not set in the environment variables")

        caslib, path = caslibify(self.s, path=self.data_dir+'images.sashdat', task='load')

        self.s.table.loadtable(caslib=caslib,
                               casout={'name': 'eee', 'replace': True},
                               path=path)

        r = model1.fit(data='eee', inputs='_image_', target='_label_', max_epochs=1)
        self.assertTrue(r.severity == 0)

        model1.deploy(self.data_dir_local, output_format='onnx')

        model_path = os.path.join(self.data_dir_local, 'Simple_CNN1.onnx')
        m = onnx.load(model_path)
        self.assertEqual(m.graph.node[1].op_type, 'Reshape')
        init = numpy_helper.to_array(m.graph.initializer[1])
        self.assertTrue(np.array_equal(init, [ -1,  2, 448, 448]))
Exemplo n.º 2
0
 def test_reshape_layer1(self):
     dict1 = Reshape(name='reshape',
                     width=1,
                     height=2,
                     depth=3,
                     src_layers=[Dense(name='fc',
                                       n=100)]).to_model_params()
     self.assertTrue(self.sample_syntax['reshape1'] == dict1)
Exemplo n.º 3
0
    def test_model_crnn_bug(self):
        model = Sequential(self.s, model_table='crnn')
        model.add(InputLayer(3,256,16))
        model.add(Reshape(height=16,width=256,depth=3))

        model.add(Conv2d(64,3,3,stride=1,padding=1))                # size = 16x256x64
        model.add(Pooling(2,2,2))                                   # size = 8x128x64

        model.add(Conv2d(128,3,3,stride=1,padding=1))               # size = 8x128x128
        model.add(Pooling(2,2,2))                                   # size = 4x64x128

        model.add(Conv2d(256,3,3,stride=1,padding=1,act='IDENTITY')) # size = 4x64x256
        model.add(BN(act='RELU'))                   # size = 4x64x256

        model.add(Conv2d(256,3,3,stride=1,padding=1))              # size = 4x64x256


        model.add(Pooling(1,2,stride_horizontal=1, stride_vertical=2))



        #, padding=1))           #  size = 2x64x256
        #model.add(Pooling(1,2,stride=2,stride_horizontal=1, stride_vertical=2,))           # size = 2x64x256

        model.add(Conv2d(512,3,3,stride=1,padding=1, act='IDENTITY')) # size = 2x64x512
        model.add(BN(act='RELU'))

        model.add(Conv2d(512,3,3,stride=1,padding=1))              # size = 2x64x512
        model.add(Pooling(1,2,stride_horizontal=1, stride_vertical=2)) #, padding=1))           # size = 1x64x512
        #model.add(Pooling(1,2,stride=2,stride_horizontal=1, stride_vertical=2,))           # size = 1x64x512

        model.add(Conv2d(512,3,3,stride=1,padding=1, act='IDENTITY')) # size = 1x64x512
        model.add(BN(act='RELU'))

        model.add(Reshape(order='DWH',width=64, height=512, depth=1))

        model.add(Recurrent(512,output_type='SAMELENGTH'))

        model.add(OutputLayer(error='CTC'))

        model.print_summary()
Exemplo n.º 4
0
 def test_formant_name_function(self):
     ol = Reshape()
     ol.format_name(block_num=1, local_count=7)
     self.assertTrue(ol.name == 'Reshape1_7')
Exemplo n.º 5
0
 def test_reshape_layer2(self):
     if not __dev__:
         with self.assertRaises(DLPyError):
             Reshape(not_a_parameter=1)
Exemplo n.º 6
0
    def _MBConvBlock(inputs, in_channels, out_channels, ksize, stride, expansion, se_ratio, stage_id, block_id,
                     noskip=False, activation_fn='relu'):
        '''
        Inverted Residual Block

        Parameters
        ----------
        inputs: input tensor
            Speecify input tensor for block.
        in_channels: integer
            Specifies the number of input tensor's channel.
        out_channels: integer
            Specifies the number of output tensor's channel
        ksize:
            Specifies the kernel size of the convolution
        stride: integer
            Specifies the stride of the convolution
        expansion: double
            Specifies the expansion factor for the input layer.
        se_ratio: double
            Specifies the ratio to squeeze the input filters for squeeze-and-excitation block.
        stage_id: integer
            Specifies stage id for naming layers
        block_id:
            Specifies block id for naming layers
        noskip: bool
            Specifies whether the skip connection is used. By default, the skip connection is used.
        activation_fn:
            Specifies activation function
        '''

        # mobilenetv2 block is also known as inverted residual block, which consists of three convolutions:
        # the first is 1*1 convolution for expansion
        # the second is depthwise convolution
        # the third is 1*1 convolution without any non-linearity for projection

        x = inputs
        prefix = 'stage_{}_block_{}'.format(stage_id, block_id)
        n_groups = in_channels  # for expansion=1, n_groups might be different from pointwise_filters

        if expansion > 1:
            # For MobileNet V2, expansion>1 when stage>0
            n_groups = int(expansion * in_channels)  ## update n_groups
            x = Conv2d(n_groups, 1, include_bias=False, act='identity',
                       name=prefix + 'expand')(x)
            x = BN(name=prefix + 'expand_BN', act='identity')(x)

        # Depthwise convolution
        x = GroupConv2d(n_groups, n_groups, ksize, stride=stride, act='identity',
                        include_bias=False, name=prefix + 'depthwise')(x)
        x = BN(name=prefix + 'depthwise_BN', act=activation_fn)(x)

        # Squeeze-Excitation
        if 0 < se_ratio <= 1:
            se_input = x  # features to be squeezed
            x = GlobalAveragePooling2D(name=prefix + "global_avg_pool")(x)
            # Squeeze
            channels_se = max(1, int(in_channels * se_ratio))
            x = Conv2d(channels_se, 1, include_bias=True, act=activation_fn, name=prefix + 'squeeze')(x)
            x = Conv2d(n_groups, 1, include_bias=True, act='sigmoid', name=prefix + 'excitation')(x)
            x = Reshape(name=prefix + 'reshape', width=n_groups, height=1, depth=1)(x)
            x = Scale(name=prefix + 'scale')([se_input, x])  # x = out*w

        # Project
        x = Conv2d(out_channels, 1, include_bias=False, act='identity', name=prefix + 'project')(x)
        x = BN(name=prefix + 'project_BN', act='identity')(x)  # identity activation on narrow tensor
        # Prepare output for MBConv block
        if in_channels == out_channels and stride == 1 and (not noskip):
            # dropout can be added.
            return Res(name=prefix + 'add_se_residual')([x, inputs])
        else:
            return x
Exemplo n.º 7
0
def YoloV2_MultiSize(conn,
                     anchors,
                     model_table='YoloV2-MultiSize',
                     n_channels=3,
                     width=416,
                     height=416,
                     scale=1.0 / 255,
                     random_mutation=None,
                     act='leaky',
                     act_detection='AUTO',
                     softmax_for_class_prob=True,
                     coord_type='YOLO',
                     max_label_per_image=30,
                     max_boxes=30,
                     n_classes=20,
                     predictions_per_grid=5,
                     do_sqrt=True,
                     grid_number=13,
                     coord_scale=None,
                     object_scale=None,
                     prediction_not_a_object_scale=None,
                     class_scale=None,
                     detection_threshold=None,
                     iou_threshold=None,
                     random_boxes=False,
                     match_anchor_size=None,
                     num_to_force_coord=None,
                     random_flip=None,
                     random_crop=None):
    '''
    Generates a deep learning model with the Yolov2 architecture.

    The model is same as Yolov2 proposed in original paper. In addition to
    Yolov2, the model adds a passthrough layer that brings feature from an
    earlier layer to lower resolution layer.

    Parameters
    ----------
    conn : CAS
        Specifies the connection of the CAS connection.
    anchors : list
        Specifies the anchor box values.
    model_table : string, optional
        Specifies the name of CAS table to store the model.
    n_channels : int, optional
        Specifies the number of the channels (i.e., depth) of the input layer.
        Default: 3
    width : int, optional
        Specifies the width of the input layer.
        Default: 416
    height : int, optional
        Specifies the height of the input layer.
        Default: 416
    scale : double, optional
        Specifies a scaling factor to be applied to each pixel intensity values.
        Default: 1.0 / 255
    random_mutation : string, optional
        Specifies how to apply data augmentations/mutations to the data in the
        input layer.
        Valid Values: 'none', 'random'
    act : string, optional
        Specifies the activation function for the batch normalization layers.
        Default: 'leaky'
    act_detection : string, optional
        Specifies the activation function for the detection layer.
        Valid Values: AUTO, IDENTITY, LOGISTIC, SIGMOID, TANH, RECTIFIER, RELU, SOFPLUS, ELU, LEAKY, FCMP
        Default: AUTO
    softmax_for_class_prob : bool, optional
        Specifies whether to perform Softmax on class probability per
        predicted object.
        Default: True
    coord_type : string, optional
        Specifies the format of how to represent bounding boxes. For example,
        a bounding box can be represented with the x and y locations of the
        top-left point as well as width and height of the rectangle.
        This format is the 'rect' format. We also support coco and yolo formats.
        Valid Values: 'rect', 'yolo', 'coco'
        Default: 'yolo'
    max_label_per_image : int, optional
        Specifies the maximum number of labels per image in the training.
        Default: 30
    max_boxes : int, optional
        Specifies the maximum number of overall predictions allowed in the
        detection layer.
        Default: 30
    n_classes : int, optional
        Specifies the number of classes. If None is assigned, the model will
        automatically detect the number of classes based on the training set.
        Default: 20
    predictions_per_grid : int, optional
        Specifies the amount of predictions will be done per grid.
        Default: 5
    do_sqrt : bool, optional
        Specifies whether to apply the SQRT function to width and height of
        the object for the cost function.
        Default: True
    grid_number : int, optional
        Specifies the amount of cells to be analyzed for an image. For example,
        if the value is 5, then the image will be divided into a 5 x 5 grid.
        Default: 13
    coord_scale : float, optional
        Specifies the weight for the cost function in the detection layer,
        when objects exist in the grid.
    object_scale : float, optional
        Specifies the weight for object detected for the cost function in
        the detection layer.
    prediction_not_a_object_scale : float, optional
        Specifies the weight for the cost function in the detection layer,
        when objects do not exist in the grid.
    class_scale : float, optional
        Specifies the weight for the class of object detected for the cost
        function in the detection layer.
    detection_threshold : float, optional
        Specifies the threshold for object detection.
    iou_threshold : float, optional
        Specifies the IOU Threshold of maximum suppression in object detection.
    random_boxes : bool, optional
        Randomizing boxes when loading the bounding box information. Default: False
    match_anchor_size : bool, optional
        Whether to force the predicted box match the anchor boxes in sizes for all predictions
    num_to_force_coord : int, optional
        The number of leading chunk of images in training when the algorithm forces predicted objects
        in each grid to be equal to the anchor box sizes, and located at the grid center
    random_flip : string, optional
        Specifies how to flip the data in the input layer when image data is
        used. Approximately half of the input data is subject to flipping.
        Valid Values: 'h', 'hv', 'v', 'none'
    random_crop : string, optional
        Specifies how to crop the data in the input layer when image data is
        used. Images are cropped to the values that are specified in the width
        and height parameters. Only the images with one or both dimensions
        that are larger than those sizes are cropped.
        Valid Values: 'none', 'unique', 'randomresized', 'resizethencrop'

    Returns
    -------
    :class:`Sequential`

    References
    ----------
    https://arxiv.org/pdf/1612.08242.pdf

    '''

    model = Sequential(conn=conn, model_table=model_table)

    parameters = locals()
    input_parameters = get_layer_options(input_layer_options, parameters)
    model.add(InputLayer(**input_parameters))

    # conv1 224 416
    model.add(Conv2d(32, width=3, act='identity', include_bias=False,
                     stride=1))
    model.add(BN(act=act))
    model.add(Pooling(width=2, height=2, stride=2, pool='max'))
    # conv2 112 208
    model.add(Conv2d(64, width=3, act='identity', include_bias=False,
                     stride=1))
    model.add(BN(act=act))
    model.add(Pooling(width=2, height=2, stride=2, pool='max'))
    # conv3 56 104
    model.add(
        Conv2d(128, width=3, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))
    # conv4 56 104
    model.add(Conv2d(64, width=1, act='identity', include_bias=False,
                     stride=1))
    model.add(BN(act=act))
    # conv5 56 104
    model.add(
        Conv2d(128, width=3, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))
    model.add(Pooling(width=2, height=2, stride=2, pool='max'))
    # conv6 28 52
    model.add(
        Conv2d(256, width=3, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))
    # conv7 28 52
    model.add(
        Conv2d(128, width=1, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))
    # conv8 28 52
    model.add(
        Conv2d(256, width=3, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))
    model.add(Pooling(width=2, height=2, stride=2, pool='max'))
    # conv9 14 26
    model.add(
        Conv2d(512, width=3, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))
    # conv10 14 26
    model.add(
        Conv2d(256, width=1, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))
    # conv11 14 26
    model.add(
        Conv2d(512, width=3, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))
    # conv12 14 26
    model.add(
        Conv2d(256, width=1, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))
    # conv13 14 26
    model.add(
        Conv2d(512, width=3, act='identity', include_bias=False, stride=1))
    pointLayer1 = BN(act=act, name='BN5_13')
    model.add(pointLayer1)
    model.add(Pooling(width=2, height=2, stride=2, pool='max'))
    # conv14 7 13
    model.add(
        Conv2d(1024, width=3, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))
    # conv15 7 13
    model.add(
        Conv2d(512, width=1, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))
    # conv16 7 13
    model.add(
        Conv2d(1024, width=3, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))
    # conv17 7 13
    model.add(
        Conv2d(512, width=1, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))
    # conv18 7 13
    model.add(
        Conv2d(1024, width=3, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))

    # conv19 7 13
    model.add(
        Conv2d(1024, width=3, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act, name='BN6_19'))
    # conv20 7 13
    model.add(
        Conv2d(1024, width=3, act='identity', include_bias=False, stride=1))
    pointLayer2 = BN(act=act, name='BN6_20')
    model.add(pointLayer2)

    # conv21 7 26 * 26 * 512 -> 26 * 26 * 64
    model.add(
        Conv2d(64,
               width=1,
               act='identity',
               include_bias=False,
               stride=1,
               src_layers=[pointLayer1]))
    model.add(BN(act=act))
    # reshape 26 * 26 * 64 -> 13 * 13 * 256
    pointLayer3 = Reshape(act='identity',
                          width=grid_number,
                          height=grid_number,
                          depth=256,
                          name='reshape1')
    model.add(pointLayer3)

    # concat
    model.add(Concat(act='identity', src_layers=[pointLayer2, pointLayer3]))

    # conv22 7 13
    model.add(
        Conv2d(1024, width=3, act='identity', include_bias=False, stride=1))
    model.add(BN(act=act))

    model.add(
        Conv2d((n_classes + 5) * predictions_per_grid,
               width=1,
               act='identity',
               include_bias=False,
               stride=1))

    model.add(
        Detection(act=act_detection,
                  detection_model_type='yolov2',
                  anchors=anchors,
                  softmax_for_class_prob=softmax_for_class_prob,
                  coord_type=coord_type,
                  class_number=n_classes,
                  grid_number=grid_number,
                  predictions_per_grid=predictions_per_grid,
                  do_sqrt=do_sqrt,
                  coord_scale=coord_scale,
                  object_scale=object_scale,
                  prediction_not_a_object_scale=prediction_not_a_object_scale,
                  class_scale=class_scale,
                  detection_threshold=detection_threshold,
                  iou_threshold=iou_threshold,
                  random_boxes=random_boxes,
                  max_label_per_image=max_label_per_image,
                  max_boxes=max_boxes,
                  match_anchor_size=match_anchor_size,
                  num_to_force_coord=num_to_force_coord))

    return model
Exemplo n.º 8
0
    def test_model4(self):
        try:
            import onnx
            from onnx import numpy_helper
        except:
            unittest.TestCase.skipTest(self, "onnx not found in the libraries")

        import numpy as np

        model1 = Sequential(self.s, model_table='Simple_CNN1')
        model1.add(InputLayer(3, 224, 224))
        model1.add(Conv2d(8, 7, act='identity', include_bias=False))
        model1.add(Reshape(height=448, width=448, depth=2, act='IDENTITY'))
        model1.add(Reshape(height=448, width=448, depth=2, act='RECTIFIER'))
        model1.add(Conv2d(8, 7, act='identity', include_bias=False))
        model1.add(BN(act='relu'))
        model1.add(Dense(2))
        model1.add(OutputLayer(act='softmax', n=2))

        if self.data_dir is None:
            unittest.TestCase.skipTest(
                self, "DLPY_DATA_DIR is not set in the environment variables")

        caslib, path, tmp_caslib = caslibify(self.s,
                                             path=self.data_dir +
                                             'images.sashdat',
                                             task='load')

        self.s.table.loadtable(caslib=caslib,
                               casout={
                                   'name': 'eee',
                                   'replace': True
                               },
                               path=path)

        r = model1.fit(data='eee',
                       inputs='_image_',
                       target='_label_',
                       max_epochs=1)
        self.assertTrue(r.severity == 0)

        if self.data_dir_local is None:
            unittest.TestCase.skipTest(
                self,
                "DLPY_DATA_DIR_LOCAL is not set in the environment variables")

        #model1.deploy(self.data_dir_local, output_format='onnx')

        import tempfile
        tmp_dir_to_dump = tempfile.gettempdir()

        model1.deploy(tmp_dir_to_dump, output_format='onnx')
        import os
        model_path = os.path.join(tmp_dir_to_dump, 'Simple_CNN1.onnx')

        m = onnx.load(model_path)
        self.assertEqual(m.graph.node[1].op_type, 'Reshape')
        init = numpy_helper.to_array(m.graph.initializer[1])
        self.assertTrue(np.array_equal(init, [-1, 2, 448, 448]))

        import os
        os.remove(os.path.join(tmp_dir_to_dump, "Simple_CNN1.onnx"))

        if (caslib is not None) and tmp_caslib:
            self.s.retrieve('table.dropcaslib',
                            message_level='error',
                            caslib=caslib)