def TextClassification(conn, model_table='text_classifier', neurons=10, n_blocks=3, rnn_type='gru'): ''' Generates a text classification model Parameters ---------- conn : CAS Specifies the CAS connection object. model_table : string, optional Specifies the name of CAS table to store the model. neurons : int, optional Specifies the number of neurons to be in each layer. Default: 10 n_blocks : int, optional Specifies the number of bidirectional blocks to be added to the model. Default: 3 rnn_type : string, optional Specifies the type of the rnn layer. Default: GRU Valid Values: RNN, LSTM, GRU Returns ------- :class:`Sequential` ''' conn.retrieve('loadactionset', _messagelevel='error', actionset='deeplearn') if n_blocks >= 2: model = Sequential(conn=conn, model_table=model_table) b = Bidirectional(n=neurons, name='bi_' + rnn_type + '_layer_', n_blocks=n_blocks - 1, rnn_type=rnn_type) model.add(b) model.add( Bidirectional( n=neurons, output_type='encoding', src_layers=b.get_last_layers(), rnn_type=rnn_type, name='bi_' + rnn_type + '_lastlayer_', )) model.add(OutputLayer()) elif n_blocks == 1: model = Sequential(conn=conn, model_table=model_table) model.add( Bidirectional(n=neurons, output_type='encoding', rnn_type=rnn_type)) model.add(OutputLayer()) else: raise DLPyError( 'The number of blocks for a text classification model should be at least 1.' ) return model
def LeNet5(conn, model_table='LENET5', n_classes=10, n_channels=1, width=28, height=28, scale=1.0 / 255, random_flip='none', random_crop='none', offsets=0): ''' Generates a deep learning model with the LeNet5 architecture. Parameters ---------- conn : CAS Specifies the CAS connection object. model_table : string, optional Specifies the name of CAS table to store the model. 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: 10 n_channels : int, optional Specifies the number of the channels (i.e., depth) of the input layer. Default: 1 width : int, optional Specifies the width of the input layer. Default: 28 height : int, optional Specifies the height of the input layer. Default: 28 scale : double, optional Specifies a scaling factor to be applied to each pixel intensity values. Default: 1.0 / 255 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', 'v', 'hv', 'none' Default: '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' Default: 'none' offsets : double or iter-of-doubles, optional Specifies an offset for each channel in the input data. The final input data is set after applying scaling and subtracting the specified offsets. Default: 0 Returns ------- :class:`Sequential` References ---------- http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf ''' conn.retrieve('loadactionset', _messagelevel='error', actionset='deeplearn') model = Sequential(conn=conn, model_table=model_table) model.add( InputLayer(n_channels=n_channels, width=width, height=height, scale=scale, offsets=offsets, random_flip=random_flip, random_crop=random_crop)) model.add(Conv2d(n_filters=6, width=5, height=5, stride=1)) model.add(Pooling(width=2, height=2, stride=2, pool='max')) model.add(Conv2d(n_filters=16, width=5, height=5, stride=1)) model.add(Pooling(width=2, height=2, stride=2, pool='max')) model.add(Dense(n=120)) model.add(Dense(n=84)) model.add(OutputLayer(n=n_classes)) return model
def DenseNet(conn, model_table='DenseNet', n_classes=None, conv_channel=16, growth_rate=12, n_blocks=4, n_cells=4, n_channels=3, width=32, height=32, scale=1, random_flip=None, random_crop=None, offsets=(85, 111, 139), random_mutation=None): ''' Generates a deep learning model with the DenseNet architecture. Parameters ---------- conn : CAS Specifies the connection of the CAS connection. model_table : string Specifies the name of CAS table to store the model. 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: None conv_channel : int, optional Specifies the number of filters of the first convolution layer. Default: 16 growth_rate : int, optional Specifies the growth rate of convolution layers. Default: 12 n_blocks : int, optional Specifies the number of DenseNet blocks. Default: 4 n_cells : int, optional Specifies the number of dense connection for each DenseNet block. Default: 4 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: 32 height : int, optional Specifies the height of the input layer. Default: 32 scale : double, optional Specifies a scaling factor to be applied to each pixel intensity values. Default: 1 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' offsets : double or iter-of-doubles, optional Specifies an offset for each channel in the input data. The final input data is set after applying scaling and subtracting the specified offsets. Default: (85, 111, 139) random_mutation : string, optional Specifies how to apply data augmentations/mutations to the data in the input layer. Valid Values: 'none', 'random' Returns ------- :class:`Sequential` References ---------- https://arxiv.org/pdf/1608.06993.pdf ''' conn.retrieve('loadactionset', _messagelevel='error', actionset='deeplearn') # get all the parms passed in parameters = locals() channel_in = conv_channel # number of channel of transition conv layer model = Sequential(conn=conn, model_table=model_table) # get the input parameters input_parameters = get_layer_options(input_layer_options, parameters) model.add(InputLayer(**input_parameters)) # Top layers model.add( Conv2d(conv_channel, width=3, act='identity', include_bias=False, stride=1)) for i in range(n_blocks): model.add( DenseNetBlock(n_cells=n_cells, kernel_size=3, n_filter=growth_rate, stride=1)) # transition block channel_in += (growth_rate * n_cells) model.add(BN(act='relu')) if i != (n_blocks - 1): model.add( Conv2d(channel_in, width=3, act='identity', include_bias=False, stride=1)) model.add(Pooling(width=2, height=2, pool='mean')) model.add(GlobalAveragePooling2D()) model.add(OutputLayer(act='softmax', n=n_classes)) return model
def Tiny_YoloV2(conn, anchors, model_table='Tiny-Yolov2', 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): ''' Generate a deep learning model with the Tiny Yolov2 architecture. Tiny Yolov2 is a very small model of Yolov2, so that it includes fewer numbers of convolutional layer and batch normalization 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) if input_parameters['width'] != input_parameters['height']: print( not_supported_feature('Non-square yolo model training', 'height=width')) input_parameters['height'] = input_parameters['width'] model.add(InputLayer(**input_parameters)) # conv1 416 448 model.add( Conv2d(n_filters=16, 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 208 224 model.add( Conv2d(n_filters=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')) # conv3 104 112 model.add( Conv2d(n_filters=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')) # conv4 52 56 model.add( Conv2d(n_filters=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')) # conv5 26 28 model.add( Conv2d(n_filters=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')) # conv6 13 14 model.add( Conv2d(n_filters=512, width=3, act='identity', include_bias=False, stride=1)) model.add(BN(act=act)) model.add(Pooling(width=2, height=2, stride=1, pool='max')) # conv7 13 model.add( Conv2d(n_filters=1024, width=3, act='identity', include_bias=False, stride=1)) model.add(BN(act=act)) # conv8 13 model.add( Conv2d(n_filters=512, 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
def test_model16(self): model = Sequential(self.s, model_table='Simple_CNN') model.add(layer=InputLayer(n_channels=1, height=10, width=10)) model.add(layer=Keypoints(n=10)) self.assertTrue(model.summary.loc[1, 'Number of Parameters'] == (1000, 10))
def test_model23(self): try: import onnx except: unittest.TestCase.skipTest(self, "onnx not found in the libraries") 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(BN(act='relu')) model1.add(Pooling(2)) model1.add(Conv2d(8, 7, act='identity', include_bias=False)) model1.add(BN(act='relu')) model1.add(Pooling(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, output_format='onnx')
def test_model1(self): try: import onnx except: unittest.TestCase.skipTest(self, "onnx not found in the libraries") model1 = Sequential(self.s, model_table='Simple_CNN1') model1.add(InputLayer(3, 224, 224)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Dense(16)) 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) import tempfile tmp_dir_to_dump = tempfile.gettempdir() model1.deploy(tmp_dir_to_dump, output_format='onnx') 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)
def test_model13b(self): model = Sequential(self.s, model_table='simple_cnn') model.add(layer=InputLayer(n_channels=1, height=10, width=10)) model.add(layer=OutputLayer(n=10, full_connect=False)) self.assertTrue(model.summary.loc[1, 'Number of Parameters'] == (0, 0)) model1 = Sequential(self.s, model_table='simple_cnn') model1.add(layer=InputLayer(n_channels=1, height=10, width=10)) model1.add(layer=OutputLayer(n=10, full_connect=True)) self.assertTrue(model1.summary.loc[1, 'Number of Parameters'] == (1000, 10)) model2 = Sequential(self.s, model_table='Simple_CNN') model2.add(layer=InputLayer(n_channels=1, height=10, width=10)) model2.add( layer=OutputLayer(n=10, full_connect=True, include_bias=False)) self.assertTrue(model2.summary.loc[1, 'Number of Parameters'] == (1000, 0)) model3 = Sequential(self.s, model_table='Simple_CNN') model3.add(layer=InputLayer(n_channels=1, height=10, width=10)) model3.add(layer=Conv2d(4, 3)) model3.add(layer=OutputLayer(n=10)) self.assertTrue(model3.summary.loc[2, 'Number of Parameters'] == (4000, 10)) model4 = Sequential(self.s, model_table='Simple_CNN') model4.add(layer=InputLayer(n_channels=1, height=10, width=10)) model4.add(layer=Conv2d(4, 3)) model4.add(layer=OutputLayer(n=10, full_connect=False)) self.assertTrue(model4.summary.loc[2, 'Number of Parameters'] == (0, 0))
def Darknet_Reference(conn, model_table='Darknet_Reference', n_classes=1000, act='leaky', n_channels=3, width=224, height=224, scale=1.0 / 255, random_flip='H', random_crop='UNIQUE', random_mutation=None): ''' Generates a deep learning model with the Darknet_Reference architecture. The head of the model except the last convolutional layer is same as the head of Tiny Yolov2. Darknet Reference is pre-trained model for ImageNet classification. Parameters ---------- conn : CAS Specifies the connection of the CAS connection. model_table : string Specifies the name of CAS table to store the model. 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: 1000 act : string Specifies the type of the activation function for the batch normalization layers and the final convolution layer. Default: 'leaky' 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: 224. height : int, optional Specifies the height of the input layer. Default: 224. scale : double, optional Specifies a scaling factor to be applied to each pixel intensity values. Default: 1.0 / 255 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' Default: 'h' 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' Default: 'unique' random_mutation : string, optional Specifies how to apply data augmentations/mutations to the data in the input layer. Valid Values: 'none', 'random' Returns ------- :class:`Sequential` ''' conn.retrieve('loadactionset', _messagelevel='error', actionset='deeplearn') # get all the parms passed in parameters = locals() model = Sequential(conn=conn, model_table=model_table) # get the input parameters input_parameters = get_layer_options(input_layer_options, parameters) model.add(InputLayer(**input_parameters)) # conv1 224 model.add(Conv2d(16, 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 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')) # conv3 56 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')) # conv4 28 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')) # conv5 14 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')) # conv6 7 model.add( Conv2d(512, width=3, act='identity', include_bias=False, stride=1)) model.add(BN(act=act)) model.add(Pooling(width=2, height=2, stride=1, pool='max')) # conv7 7 model.add( Conv2d(1024, width=3, act='identity', include_bias=False, stride=1)) model.add(BN(act=act)) # conv8 7 model.add(Conv2d(1000, width=1, act=act, include_bias=True, stride=1)) model.add(GlobalAveragePooling2D()) model.add(OutputLayer(act='softmax', n=n_classes)) return model
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)
def test_plot_ticks(self): model1 = Sequential(self.s, model_table='Simple_CNN1') model1.add(InputLayer(3, 224, 224)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Dense(16)) 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_', lr=0.001, max_epochs=5) # Test default tick_frequency value of 1 ax = model1.plot_training_history() self.assertEqual(len(ax.xaxis.majorTicks), model1.n_epochs) # Test even tick_frequency = 2 ax = model1.plot_training_history(tick_frequency=tick_frequency) self.assertEqual(len(ax.xaxis.majorTicks), model1.n_epochs // tick_frequency + 1) # Test odd tick_frequency = 3 ax = model1.plot_training_history(tick_frequency=tick_frequency) self.assertEqual(len(ax.xaxis.majorTicks), model1.n_epochs // tick_frequency + 1) # Test max tick_frequency = model1.n_epochs ax = model1.plot_training_history(tick_frequency=tick_frequency) self.assertEqual(len(ax.xaxis.majorTicks), model1.n_epochs // tick_frequency + 1) # Test 0 tick_frequency = 0 ax = model1.plot_training_history(tick_frequency=tick_frequency) self.assertEqual(len(ax.xaxis.majorTicks), model1.n_epochs)
def test_CyclicLR(self): model1 = Sequential(self.s, model_table='Simple_CNN1') model1.add(InputLayer(3, 224, 224)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Dense(16)) 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) lrs = CyclicLR(self.s, 'eee', 4, 1.0, 0.0000001, 0.01) solver = VanillaSolver(lr_scheduler=lrs) self.assertTrue(self.sample_syntax['CyclicLR'] == solver) optimizer = Optimizer(algorithm=solver, log_level=3, max_epochs=4, mini_batch_size=2) r = model1.fit(data='eee', inputs='_image_', target='_label_', optimizer=optimizer, n_threads=2) if r.severity > 0: for msg in r.messages: print(msg) self.assertTrue(r.severity <= 1)
def YoloV1(conn, model_table='YoloV1', n_channels=3, width=448, height=448, scale=1.0 / 255, random_mutation=None, act='leaky', dropout=0, 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=2, do_sqrt=True, grid_number=7, coord_scale=None, object_scale=None, prediction_not_a_object_scale=None, class_scale=None, detection_threshold=None, iou_threshold=None, random_boxes=False, random_flip=None, random_crop=None): ''' Generates a deep learning model with the Yolo V1 architecture. Parameters ---------- conn : CAS Specifies the connection of the CAS connection. 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: 448 height : int, optional Specifies the height of the input layer. Default: 448 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 to be used in the convolutional layer layers and the final convolution layer. Default: 'leaky' dropout: double, optional Specifies the drop out rate. Default: 0 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: 2 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: 7 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 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/1506.02640.pdf ''' model = Sequential(conn=conn, model_table=model_table) parameters = locals() input_parameters = get_layer_options(input_layer_options, parameters) if input_parameters['width'] != input_parameters['height']: print( not_supported_feature('Non-square yolo model training', 'height=width')) input_parameters['height'] = input_parameters['width'] model.add(InputLayer(**input_parameters)) # conv1 448 model.add(Conv2d(32, width=3, act=act, include_bias=False, stride=1)) model.add(Pooling(width=2, height=2, stride=2, pool='max')) # conv2 224 model.add(Conv2d(64, width=3, act=act, include_bias=False, stride=1)) model.add(Pooling(width=2, height=2, stride=2, pool='max')) # conv3 112 model.add(Conv2d(128, width=3, act=act, include_bias=False, stride=1)) # conv4 112 model.add(Conv2d(64, width=1, act=act, include_bias=False, stride=1)) # conv5 112 model.add(Conv2d(128, width=3, act=act, include_bias=False, stride=1)) model.add(Pooling(width=2, height=2, stride=2, pool='max')) # conv6 56 model.add(Conv2d(256, width=3, act=act, include_bias=False, stride=1)) # conv7 56 model.add(Conv2d(128, width=1, act=act, include_bias=False, stride=1)) # conv8 56 model.add(Conv2d(256, width=3, act=act, include_bias=False, stride=1)) model.add(Pooling(width=2, height=2, stride=2, pool='max')) # conv9 28 model.add(Conv2d(512, width=3, act=act, include_bias=False, stride=1)) # conv10 28 model.add(Conv2d(256, width=1, act=act, include_bias=False, stride=1)) # conv11 28 model.add(Conv2d(512, width=3, act=act, include_bias=False, stride=1)) # conv12 28 model.add(Conv2d(256, width=1, act=act, include_bias=False, stride=1)) # conv13 28 model.add(Conv2d(512, width=3, act=act, include_bias=False, stride=1)) model.add(Pooling(width=2, height=2, stride=2, pool='max')) # conv14 14 model.add(Conv2d(1024, width=3, act=act, include_bias=False, stride=1)) # conv15 14 model.add(Conv2d(512, width=1, act=act, include_bias=False, stride=1)) # conv16 14 model.add(Conv2d(1024, width=3, act=act, include_bias=False, stride=1)) # conv17 14 model.add(Conv2d(512, width=1, act=act, include_bias=False, stride=1)) # conv18 14 model.add(Conv2d(1024, width=3, act=act, include_bias=False, stride=1)) # conv19 14 model.add(Conv2d(1024, width=3, act=act, include_bias=False, stride=1)) # conv20 7 model.add(Conv2d(1024, width=3, act=act, include_bias=False, stride=2)) # conv21 7 model.add(Conv2d(1024, width=3, act=act, include_bias=False, stride=1)) # conv22 7 model.add(Conv2d(1024, width=3, act=act, include_bias=False, stride=1)) # conv23 7 model.add( Conv2d(256, width=3, act=act, include_bias=False, stride=1, dropout=dropout)) model.add( Dense(n=(n_classes + (5 * predictions_per_grid)) * grid_number * grid_number, act='identity')) model.add( Detection(act=act_detection, detection_model_type='yolov1', 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)) return model
def TextGeneration(conn, model_table='text_generator', neurons=10, max_output_length=15, n_blocks=3, rnn_type='gru'): ''' Generates a text generation model. Parameters ---------- conn : CAS Specifies the CAS connection object. model_table : string, optional Specifies the name of CAS table to store the model. neurons : int, optional Specifies the number of neurons to be in each layer. Default: 10 n_blocks : int, optional Specifies the number of bidirectional blocks to be added to the model. Default: 3 max_output_length : int, optional Specifies the maximum number of tokens to generate Default: 15 rnn_type : string, optional Specifies the type of the rnn layer. Default: GRU Valid Values: RNN, LSTM, GRU Returns ------- :class:`Sequential` ''' conn.retrieve('loadactionset', _messagelevel='error', actionset='deeplearn') if n_blocks >= 3: model = Sequential(conn=conn, model_table=model_table) b = Bidirectional(n=neurons, name='bi_' + rnn_type + '_layer_', n_blocks=n_blocks - 2, rnn_type=rnn_type) model.add(b) b2 = Bidirectional(n=neurons, output_type='encoding', src_layers=b.get_last_layers(), rnn_type=rnn_type, name='bi_' + rnn_type + '_lastlayer') model.add(b2) model.add( Recurrent(n=neurons, output_type='arbitrarylength', src_layers=b2.get_last_layers(), rnn_type=rnn_type, max_output_length=max_output_length)) model.add(OutputLayer()) elif n_blocks >= 2: model = Sequential(conn=conn, model_table=model_table) b2 = Bidirectional(n=neurons, output_type='encoding', rnn_type=rnn_type, name='bi_' + rnn_type + '_layer_') model.add(b2) model.add( Recurrent(n=neurons, output_type='arbitrarylength', src_layers=b2.get_last_layers(), rnn_type=rnn_type, max_output_length=max_output_length)) model.add(OutputLayer()) else: raise DLPyError( 'The number of blocks for a text generation model should be at least 2.' ) return model
def test_multiple_branch(self): from dlpy.sequential import Sequential model = Sequential(self.s, model_table='Simple_CNN') model.add(InputLayer(3, 48, 96, scale=1 / 255, random_mutation='none')) model.add(Conv2d(64, 7, include_bias=True, act='relu')) model.add(Pooling(2)) model.add(Conv2d(64, 3, include_bias=True, act='relu')) model.add(Pooling(2)) model.add(Conv2d(64, 3, include_bias=True, act='relu')) model.add(Pooling(2)) model.add(Conv2d(64, 3, include_bias=True, act='relu')) model.add(Pooling(2)) model.add(Dense(16)) model.add(OutputLayer(n=1, act='sigmoid')) branch = model.to_functional_model(stop_layers=model.layers[-1]) inp1 = Input(**branch.layers[0].config) # tensor branch1 = branch(inp1) # tensor inp2 = Input(**branch.layers[0].config) # tensor branch2 = branch(inp2) # tensor inp3 = Input(**branch.layers[0].config) # tensor branch3 = branch(inp3) # tensor triplet = OutputLayer(n=1)(branch1 + branch2 + branch3) triplet_model = Model(self.s, inputs=[inp1, inp2, inp3], outputs=triplet) triplet_model.compile() triplet_model.print_summary() self.assertEqual(len(triplet_model.layers), 31) triplet_model.share_weights({'Convo.1': ['Convo.1_2', 'Convo.1_3']}) triplet_model.compile()
def test_model12(self): model1 = Sequential(self.s, model_table='Simple_CNN1') model1.add(InputLayer(3, 224, 224)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Dense(16)) 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_', save_best_weights=True) self.assertTrue(r.severity == 0) r1 = model1.fit(data='eee', inputs='_image_', target='_label_', max_epochs=3) self.assertTrue(r1.severity == 0) r2 = model1.fit(data='eee', inputs='_image_', target='_label_', max_epochs=2, save_best_weights=True) self.assertTrue(r2.severity == 0) r3 = model1.predict(data='eee', use_best_weights=True) self.assertTrue(r3.severity == 0)
def test_model20(self): model1 = Sequential(self.s, model_table='Simple_CNN1') model1.add(InputLayer(3, 224, 224)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Dense(16)) 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.save_weights_csv(self.data_dir) weights_path = os.path.join(self.data_dir, 'Simple_CNN1_weights.csv') model1.deploy(self.data_dir, output_format='onnx', model_weights=weights_path)
def test_model14(self): model = Sequential(self.s, model_table='Simple_CNN') model.add(layer=InputLayer(n_channels=1, height=10, width=10)) model.add(layer=OutputLayer()) model.summary
def test_model22(self): model1 = Sequential(self.s, model_table='Simple_CNN1') model1.add(InputLayer(3, 224, 224)) model1.add(Conv2d(8, 7)) pool1 = Pooling(2) model1.add(pool1) conv1 = Conv2d(1, 1, act='identity', src_layers=[pool1]) model1.add(conv1) model1.add(Res(act='relu', src_layers=[conv1, pool1])) model1.add(Pooling(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, output_format='onnx')
def test_model1(self): model1 = Sequential(self.s, model_table='Simple_CNN1') model1.add(InputLayer(3, 224, 224)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Dense(16)) 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_', lr=0.001) if r.severity > 0: for msg in r.messages: print(msg) self.assertTrue(r.severity <= 1)
def test_sequential_conversion(self): from dlpy.sequential import Sequential model1 = Sequential(self.s) model1.add(InputLayer(3, 224, 224)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Dense(16)) model1.add(OutputLayer(act='softmax', n=2)) func_model = model1.to_functional_model() func_model.compile() func_model.print_summary()
def DenseNet121(conn, model_table='DENSENET121', n_classes=1000, conv_channel=64, growth_rate=32, n_cells=[6, 12, 24, 16], n_channels=3, reduction=0.5, width=224, height=224, scale=1, random_flip=None, random_crop=None, offsets=(103.939, 116.779, 123.68), random_mutation=None): ''' Generates a deep learning model with the DenseNet121 architecture. Parameters ---------- conn : CAS Specifies the connection of the CAS connection. model_table : string Specifies the name of CAS table to store the model. 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: 1000 conv_channel : int, optional Specifies the number of filters of the first convolution layer. Default: 64 growth_rate : int, optional Specifies the growth rate of convolution layers. Default: 32 n_cells : int array length=4, optional Specifies the number of dense connection for each DenseNet block. Default: [6, 12, 24, 16] reduction : double, optional Specifies the factor of transition blocks. Default: 0.5 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: 224. height : int, optional Specifies the height of the input layer. Default: 224. scale : double, optional Specifies a scaling factor to be applied to each pixel intensity values. Default: 1. 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' offsets : double or iter-of-doubles, optional Specifies an offset for each channel in the input data. The final input data is set after applying scaling and subtracting the specified offsets. Default: (103.939, 116.779, 123.68) random_mutation : string, optional Specifies how to apply data augmentations/mutations to the data in the input layer. Valid Values: 'none', 'random' Returns ------- :class:`Sequential` References ---------- https://arxiv.org/pdf/1608.06993.pdf ''' conn.retrieve('loadactionset', _messagelevel='error', actionset='deeplearn') # get all the parms passed in parameters = locals() n_blocks = len(n_cells) model = Sequential(conn=conn, model_table=model_table) # get the input parameters input_parameters = get_layer_options(input_layer_options, parameters) model.add(InputLayer(**input_parameters)) # Top layers model.add( Conv2d(conv_channel, width=7, act='identity', include_bias=False, stride=2)) model.add(BN(act='relu')) src_layer = Pooling(width=3, height=3, stride=2, padding=1, pool='max') model.add(src_layer) for i in range(n_blocks): for _ in range(n_cells[i]): model.add(BN(act='relu')) model.add( Conv2d(n_filters=growth_rate * 4, width=1, act='identity', stride=1, include_bias=False)) model.add(BN(act='relu')) src_layer2 = Conv2d(n_filters=growth_rate, width=3, act='identity', stride=1, include_bias=False) model.add(src_layer2) src_layer = Concat(act='identity', src_layers=[src_layer, src_layer2]) model.add(src_layer) conv_channel += growth_rate if i != (n_blocks - 1): # transition block conv_channel = int(conv_channel * reduction) model.add(BN(act='relu')) model.add( Conv2d(n_filters=conv_channel, width=1, act='identity', stride=1, include_bias=False)) src_layer = Pooling(width=2, height=2, stride=2, pool='mean') model.add(src_layer) model.add(BN(act='identity')) # Bottom Layers model.add(GlobalAveragePooling2D()) model.add(OutputLayer(act='softmax', n=n_classes)) return model
def test_stop_layers(self): from dlpy.sequential import Sequential model1 = Sequential(self.s) model1.add(InputLayer(3, 224, 224)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Dense(16)) model1.add(OutputLayer(act='softmax', n=2)) inputlayer = model1.layers[0] inp = Input(**inputlayer.config) func_model = model1.to_functional_model( stop_layers=[model1.layers[-1]]) x = func_model(inp) out = Keypoints(n=10)(x) func_model_keypoints = Model(self.s, inp, out) func_model_keypoints.compile() func_model_keypoints.print_summary()
def test_simple_cnn_seq2(self): model1 = Sequential(self.s, model_table='table8') model1.add(InputLayer(3, 224, 224)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Conv2d(8, 7)) model1.add(Pooling(2)) model1.add(Dense(16)) model1.add(OutputLayer(act='softmax', n=2)) model1.print_summary()
def InceptionV3(conn, model_table='InceptionV3', n_classes=1000, n_channels=3, width=299, height=299, scale=1, random_flip=None, random_crop=None, offsets=(103.939, 116.779, 123.68), pre_trained_weights=False, pre_trained_weights_file=None, include_top=False, random_mutation=None): ''' Generates a deep learning model with the Inceptionv3 architecture with batch normalization layers. Parameters ---------- conn : CAS Specifies the CAS connection object. model_table : string, optional Specifies the name of CAS table to store the model in. 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: 1000 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: 299 height : int, optional Specifies the height of the input layer. Default: 299 scale : double, optional Specifies a scaling factor to be applied to each pixel intensity values. Default: 1.0 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' offsets : double or iter-of-doubles, optional Specifies an offset for each channel in the input data. The final input data is set after applying scaling and subtracting the specified offsets. Default: (103.939, 116.779, 123.68) pre_trained_weights : bool, optional Specifies whether to use the pre-trained weights from ImageNet data set Default: False pre_trained_weights_file : string, optional Specifies the file name for the pretained weights. Must be a fully qualified file name of SAS-compatible file (*.caffemodel.h5) Note: Required when pre_train_weight=True. include_top : bool, optional Specifies whether to include pre-trained weights of the top layers, i.e. the FC layers Default: False random_mutation : string, optional Specifies how to apply data augmentations/mutations to the data in the input layer. Valid Values: 'none', 'random' Returns ------- :class:`Sequential` If `pre_train_weight` is `False` :class:`Model` If `pre_train_weight` is `True` References ---------- https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Szegedy_Rethinking_the_Inception_CVPR_2016_paper.pdf ''' conn.retrieve('loadactionset', _messagelevel='error', actionset='deeplearn') # get all the parms passed in parameters = locals() if not pre_trained_weights: model = Sequential(conn=conn, model_table=model_table) # get the input parameters input_parameters = get_layer_options(input_layer_options, parameters) model.add(InputLayer(**input_parameters)) # 299 x 299 x 3 model.add( Conv2d(n_filters=32, width=3, height=3, stride=2, act='identity', include_bias=False, padding=0)) model.add(BN(act='relu')) # 149 x 149 x 32 model.add( Conv2d(n_filters=32, width=3, height=3, stride=1, act='identity', include_bias=False, padding=0)) model.add(BN(act='relu')) # 147 x 147 x 32 model.add( Conv2d(n_filters=64, width=3, height=3, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) # 147 x 147 x 64 model.add(Pooling(width=3, height=3, stride=2, pool='max', padding=0)) # 73 x 73 x 64 model.add( Conv2d(n_filters=80, width=1, height=1, stride=1, act='identity', include_bias=False, padding=0)) model.add(BN(act='relu')) # 73 x 73 x 80 model.add( Conv2d(n_filters=192, width=3, height=3, stride=1, act='identity', include_bias=False, padding=0)) model.add(BN(act='relu')) # 71 x 71 x 192 pool2 = Pooling(width=3, height=3, stride=2, pool='max', padding=0) model.add(pool2) # mixed 0: output 35 x 35 x 256 # branch1x1 model.add( Conv2d(n_filters=64, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[pool2])) branch1x1 = BN(act='relu') model.add(branch1x1) # branch5x5 model.add( Conv2d(n_filters=48, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[pool2])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=64, width=5, height=5, stride=1, act='identity', include_bias=False)) branch5x5 = BN(act='relu') model.add(branch5x5) # branch3x3dbl model.add( Conv2d(n_filters=64, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[pool2])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=96, width=3, height=3, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=96, width=3, height=3, stride=1, act='identity', include_bias=False)) branch3x3dbl = BN(act='relu') model.add(branch3x3dbl) # branch_pool model.add( Pooling(width=3, height=3, stride=1, pool='average', src_layers=[pool2])) model.add( Conv2d(n_filters=32, width=1, height=1, stride=1, act='identity', include_bias=False)) branch_pool = BN(act='relu') model.add(branch_pool) # mixed0 concat concat = Concat( act='identity', src_layers=[branch1x1, branch5x5, branch3x3dbl, branch_pool]) model.add(concat) # mixed 1: output 35 x 35 x 288 # branch1x1 model.add( Conv2d(n_filters=64, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) branch1x1 = BN(act='relu') model.add(branch1x1) # branch5x5 model.add( Conv2d(n_filters=48, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=64, width=5, height=5, stride=1, act='identity', include_bias=False)) branch5x5 = BN(act='relu') model.add(branch5x5) # branch3x3dbl model.add( Conv2d(n_filters=64, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=96, width=3, height=3, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=96, width=3, height=3, stride=1, act='identity', include_bias=False)) branch3x3dbl = BN(act='relu') model.add(branch3x3dbl) # branch_pool model.add( Pooling(width=3, height=3, stride=1, pool='average', src_layers=[concat])) model.add( Conv2d(n_filters=64, width=1, height=1, stride=1, act='identity', include_bias=False)) branch_pool = BN(act='relu') model.add(branch_pool) # mixed1 concat concat = Concat( act='identity', src_layers=[branch1x1, branch5x5, branch3x3dbl, branch_pool]) model.add(concat) # mixed 2: output 35 x 35 x 288 # branch1x1 model.add( Conv2d(n_filters=64, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) branch1x1 = BN(act='relu') model.add(branch1x1) # branch5x5 model.add( Conv2d(n_filters=48, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=64, width=5, height=5, stride=1, act='identity', include_bias=False)) branch5x5 = BN(act='relu') model.add(branch5x5) # branch3x3dbl model.add( Conv2d(n_filters=64, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=96, width=3, height=3, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=96, width=3, height=3, stride=1, act='identity', include_bias=False)) branch3x3dbl = BN(act='relu') model.add(branch3x3dbl) # branch_pool model.add( Pooling(width=3, height=3, stride=1, pool='average', src_layers=[concat])) model.add( Conv2d(n_filters=64, width=1, height=1, stride=1, act='identity', include_bias=False)) branch_pool = BN(act='relu') model.add(branch_pool) # mixed2 concat concat = Concat( act='identity', src_layers=[branch1x1, branch5x5, branch3x3dbl, branch_pool]) model.add(concat) # mixed 3: output 17 x 17 x 768 # branch3x3 model.add( Conv2d(n_filters=384, width=3, height=3, stride=2, act='identity', include_bias=False, padding=0, src_layers=[concat])) branch3x3 = BN(act='relu') model.add(branch3x3) # branch3x3dbl model.add( Conv2d(n_filters=64, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=96, width=3, height=3, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=96, width=3, height=3, stride=2, act='identity', include_bias=False, padding=0)) branch3x3dbl = BN(act='relu') model.add(branch3x3dbl) # branch_pool branch_pool = Pooling(width=3, height=3, stride=2, pool='max', padding=0, src_layers=[concat]) model.add(branch_pool) # mixed3 concat concat = Concat(act='identity', src_layers=[branch3x3, branch3x3dbl, branch_pool]) model.add(concat) # mixed 4: output 17 x 17 x 768 # branch1x1 model.add( Conv2d(n_filters=192, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) branch1x1 = BN(act='relu') model.add(branch1x1) # branch7x7 model.add( Conv2d(n_filters=128, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=128, width=7, height=1, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=192, width=1, height=7, stride=1, act='identity', include_bias=False)) branch7x7 = BN(act='relu') model.add(branch7x7) # branch7x7dbl model.add( Conv2d(n_filters=128, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=128, width=1, height=7, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=128, width=7, height=1, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=128, width=1, height=7, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=192, width=7, height=1, stride=1, act='identity', include_bias=False)) branch7x7dbl = BN(act='relu') model.add(branch7x7dbl) # branch_pool model.add( Pooling(width=3, height=3, stride=1, pool='average', src_layers=[concat])) model.add( Conv2d(n_filters=192, width=1, height=1, stride=1, act='identity', include_bias=False)) branch_pool = BN(act='relu') model.add(branch_pool) # mixed4 concat concat = Concat( act='identity', src_layers=[branch1x1, branch7x7, branch7x7dbl, branch_pool]) model.add(concat) # mixed 5, 6: output 17 x 17 x 768 for i in range(2): # branch1x1 model.add( Conv2d(n_filters=192, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) branch1x1 = BN(act='relu') model.add(branch1x1) # branch7x7 model.add( Conv2d(n_filters=160, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=160, width=7, height=1, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=192, width=1, height=7, stride=1, act='identity', include_bias=False)) branch7x7 = BN(act='relu') model.add(branch7x7) # branch7x7dbl model.add( Conv2d(n_filters=160, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=160, width=1, height=7, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=160, width=7, height=1, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=160, width=1, height=7, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=192, width=7, height=1, stride=1, act='identity', include_bias=False)) branch7x7dbl = BN(act='relu') model.add(branch7x7dbl) # branch_pool model.add( Pooling(width=3, height=3, stride=1, pool='average', src_layers=[concat])) model.add( Conv2d(n_filters=192, width=1, height=1, stride=1, act='identity', include_bias=False)) branch_pool = BN(act='relu') model.add(branch_pool) # concat concat = Concat( act='identity', src_layers=[branch1x1, branch7x7, branch7x7dbl, branch_pool]) model.add(concat) # mixed 7: output 17 x 17 x 768 # branch1x1 model.add( Conv2d(n_filters=192, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) branch1x1 = BN(act='relu') model.add(branch1x1) # branch7x7 model.add( Conv2d(n_filters=192, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=192, width=7, height=1, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=192, width=1, height=7, stride=1, act='identity', include_bias=False)) branch7x7 = BN(act='relu') model.add(branch7x7) # branch7x7dbl model.add( Conv2d(n_filters=192, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=192, width=1, height=7, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=192, width=7, height=1, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=192, width=1, height=7, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=192, width=7, height=1, stride=1, act='identity', include_bias=False)) branch7x7dbl = BN(act='relu') model.add(branch7x7dbl) # branch_pool model.add( Pooling(width=3, height=3, stride=1, pool='average', src_layers=[concat])) model.add( Conv2d(n_filters=192, width=1, height=1, stride=1, act='identity', include_bias=False)) branch_pool = BN(act='relu') model.add(branch_pool) # mixed7 concat concat = Concat( act='identity', src_layers=[branch1x1, branch7x7, branch7x7dbl, branch_pool]) model.add(concat) # mixed 8: output 8 x 8 x 1280 # branch3x3 model.add( Conv2d(n_filters=192, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=320, width=3, height=3, stride=2, act='identity', include_bias=False, padding=0)) branch3x3 = BN(act='relu') model.add(branch3x3) # branch7x7x3 model.add( Conv2d(n_filters=192, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=192, width=7, height=1, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=192, width=1, height=7, stride=1, act='identity', include_bias=False)) model.add(BN(act='relu')) model.add( Conv2d(n_filters=192, width=3, height=3, stride=2, act='identity', include_bias=False, padding=0)) branch7x7x3 = BN(act='relu') model.add(branch7x7x3) # branch_pool branch_pool = Pooling(width=3, height=3, stride=2, pool='max', padding=0, src_layers=[concat]) model.add(branch_pool) # mixed8 concat concat = Concat(act='identity', src_layers=[branch3x3, branch7x7x3, branch_pool]) model.add(concat) # mixed 9, 10: output 8 x 8 x 2048 for i in range(2): # branch1x1 model.add( Conv2d(n_filters=320, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) branch1x1 = BN(act='relu') model.add(branch1x1) # branch3x3 model.add( Conv2d(n_filters=384, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) branch3x3 = BN(act='relu') model.add(branch3x3) model.add( Conv2d(n_filters=384, width=3, height=1, stride=1, act='identity', include_bias=False, src_layers=[branch3x3])) branch3x3_1 = BN(act='relu') model.add(branch3x3_1) model.add( Conv2d(n_filters=384, width=1, height=3, stride=1, act='identity', include_bias=False, src_layers=[branch3x3])) branch3x3_2 = BN(act='relu') model.add(branch3x3_2) branch3x3 = Concat(act='identity', src_layers=[branch3x3_1, branch3x3_2]) model.add(branch3x3) # branch3x3dbl model.add( Conv2d(n_filters=448, width=1, height=1, stride=1, act='identity', include_bias=False, src_layers=[concat])) model.add(BN(act='relu')) model.add( Conv2d(n_filters=384, width=3, height=3, stride=1, act='identity', include_bias=False)) branch3x3dbl = BN(act='relu') model.add(branch3x3dbl) model.add( Conv2d(n_filters=384, width=3, height=1, stride=1, act='identity', include_bias=False, src_layers=[branch3x3dbl])) branch3x3dbl_1 = BN(act='relu') model.add(branch3x3dbl_1) model.add( Conv2d(n_filters=384, width=1, height=3, stride=1, act='identity', include_bias=False, src_layers=[branch3x3dbl])) branch3x3dbl_2 = BN(act='relu') model.add(branch3x3dbl_2) branch3x3dbl = Concat(act='identity', src_layers=[branch3x3dbl_1, branch3x3dbl_2]) model.add(branch3x3dbl) # branch_pool model.add( Pooling(width=3, height=3, stride=1, pool='average', src_layers=[concat])) model.add( Conv2d(n_filters=192, width=1, height=1, stride=1, act='identity', include_bias=False)) branch_pool = BN(act='relu') model.add(branch_pool) # concat concat = Concat( act='identity', src_layers=[branch1x1, branch3x3, branch3x3dbl, branch_pool]) model.add(concat) # calculate dimensions for global average pooling w = max((width - 75) // 32 + 1, 1) h = max((height - 75) // 32 + 1, 1) # global average pooling model.add( Pooling(width=w, height=h, stride=1, pool='average', padding=0, src_layers=[concat])) # output layer model.add(OutputLayer(n=n_classes)) return model else: if pre_trained_weights_file is None: raise ValueError( '\nThe pre-trained weights file is not specified.\n' 'Please follow the steps below to attach the ' 'pre-trained weights:\n' '1. Go to the website ' 'https://support.sas.com/documentation/prod-p/vdmml/zip/ ' 'and download the associated weight file.\n' '2. Upload the *.h5 file to ' 'a server side directory which the CAS ' 'session has access to.\n' '3. Specify the pre_train_weight_file using ' 'the fully qualified server side path.') print('NOTE: Scale is set to 1/127.5, and offsets 1 to ' 'match Keras preprocessing.') model_cas = model_inceptionv3.InceptionV3_Model( s=conn, model_table=model_table, n_channels=n_channels, width=width, height=height, random_crop=random_crop, offsets=[1, 1, 1], random_flip=random_flip, random_mutation=random_mutation) if include_top: if n_classes != 1000: warnings.warn( 'If include_top = True, ' 'n_classes will be set to 1000.', RuntimeWarning) model = Model.from_table(model_cas) model.load_weights(path=pre_trained_weights_file, labels=True) return model else: model = Model.from_table(model_cas, display_note=False) model.load_weights(path=pre_trained_weights_file) weight_table_options = model.model_weights.to_table_params() weight_table_options.update(dict(where='_LayerID_<218')) model._retrieve_('table.partition', table=weight_table_options, casout=dict( replace=True, **model.model_weights.to_table_params())) model._retrieve_('deeplearn.removelayer', model=model_table, name='predictions') model._retrieve_('deeplearn.addlayer', model=model_table, name='predictions', layer=dict(type='output', n=n_classes, act='softmax'), srcLayers=['avg_pool']) model = Model.from_table(conn.CASTable(model_table)) return model