def DeeplabV3(backbond, input_shape=(224, 224, 3), classes=20, **kwargs): input_shape = tuple(input_shape) deeplab = Sequential(name='deeplabv3') deeplab.add_module('backbond', backbond) deeplab.add_module('classifier', DeepLabHead(classes=classes, num_filters=128)) deeplab.add_module( 'upsample', Upsampling2d(scale_factor=16, mode='bilinear', align_corners=False)) model = ImageSegmentationModel(input_shape=input_shape, output=deeplab) return model
def MobileNet(input_shape=(224, 224, 3), classes=1000, use_bias=False, width_mult=1.0, round_nearest=8, include_top=True, model_name='', **kwargs): input_filters = 32 last_filters = 1280 mobilenet = Sequential(name='mobilenet') inverted_residual_setting = [ # t, c, n, s [1, 16, 1, 1], [6, 24, 2, 2], [6, 32, 3, 2], [6, 64, 4, 2], [6, 96, 3, 1], [6, 160, 3, 2], [6, 320, 1, 1], ] input_filters = _make_divisible(input_filters * width_mult, round_nearest) last_filters = _make_divisible(last_filters * max(1.0, width_mult), round_nearest) features = [] features.append( Conv2d_Block((3, 3), num_filters=input_filters, strides=2, auto_pad=True, padding_mode='zero', normalization='batch', activation='relu6', name='first_layer')) for t, c, n, s in inverted_residual_setting: output_filters = _make_divisible(c * width_mult, round_nearest) for i in range(n): strides = s if i == 0 else 1 features.append( inverted_residual(input_filters, num_filters=output_filters, strides=strides, expansion=t, name='irb_{0}'.format(i))) input_filters = output_filters features.append( Conv2d_Block((1, 1), last_filters, auto_pad=True, padding_mode='zero', normalization='batch', activation='relu6', name='last_layer')) mobilenet.add_module('features', Sequential(*features, name='features')) mobilenet.add_module('gap', GlobalAvgPool2d()) if include_top: mobilenet.add_module('drop', Dropout(0.2)) mobilenet.add_module('fc', Dense((classes), activation=None)) mobilenet.add_module('softmax', SoftMax(name='softmax')) model = ImageClassificationModel(input_shape=input_shape, output=mobilenet) model.signature = get_signature(model.model.forward) with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'imagenet_labels1.txt'), 'r', encoding='utf-8-sig') as f: labels = [l.rstrip() for l in f] model.class_names = labels model.preprocess_flow = [ resize((224, 224), keep_aspect=True), normalize(127.5, 127.5) ] # model.summary() return model
def make_vgg_layers(cfg, num_classes=1000, input_shape=(224, 224, 3), include_top=True): layers = [] in_channels = 3 block = 1 conv = 1 vgg = Sequential() for v in cfg: if v == 'M': vgg.add_module( 'block{0}_pool'.format(block), MaxPool2d(kernel_size=2, strides=2, use_bias=True, name='block{0}_pool'.format(block))) block += 1 conv = 1 else: if len(vgg) == 0: vgg.add_module( 'block{0}_conv{1}'.format(block, conv), Conv2d((3, 3), v, auto_pad=True, activation=None, use_bias=True, name='block{0}_conv{1}'.format(block, conv))) else: vgg.add_module( 'block{0}_conv{1}'.format(block, conv), Conv2d((3, 3), v, auto_pad=True, activation=None, use_bias=True, name='block{0}_conv{1}'.format(block, conv))) vgg.add_module('block{0}_relu{1}'.format(block, conv), Relu(name='block{0}_relu{1}'.format(block, conv))) conv += 1 in_channels = v if include_top == True: vgg.add_module('flattened', Flatten()) vgg.add_module('fc1', Dense(4096, use_bias=True, activation='relu')) vgg.add_module('drop1', Dropout(0.5)) vgg.add_module('fc2', Dense(4096, use_bias=True, activation='relu')) vgg.add_module('drop2', Dropout(0.5)) vgg.add_module('fc3', Dense(num_classes, use_bias=True, activation='softmax')) model = ImageClassificationModel(input_shape=input_shape, output=vgg) model.signature = get_signature(model.model.forward) with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'imagenet_labels1.txt'), 'r', encoding='utf-8-sig') as f: labels = [l.rstrip() for l in f] model.class_names = labels model.preprocess_flow = [ Resize((input_shape[0], input_shape[1]), keep_aspect=True), to_bgr(), Normalize([103.939, 116.779, 123.68], [1, 1, 1]) ] # model.summary() return model
def DenseNet(blocks, growth_rate=32, initial_filters=64, include_top=True, pretrained=True, input_shape=(224, 224, 3), num_classes=1000, name='', **kwargs): """' Instantiates the DenseNet architecture. Optionally loads weights pre-trained on ImageNet. Args blocks (tuple/ list of int ): numbers of building blocks for the dense layers. growth_rate (int):The growth rate regulates how much new information each layer contributes to the global state initial_filters (int): the channel of the first convolution layer pretrained (bool): If True, returns a model pre-trained on ImageNet. input_shape (tuple or list): the default input image size in CHW order (C, H, W) num_classes (int): number of classes name (string): anme of the model Returns A trident image classification model instance. """ densenet = Sequential() densenet.add_module( 'conv1/conv', Conv2d_Block((7, 7), num_filters=initial_filters, strides=2, use_bias=False, auto_pad=True, padding_mode='zero', activation='relu', normalization='batch', name='conv1/conv')) densenet.add_module('maxpool', (MaxPool2d( (3, 3), strides=2, auto_pad=True, padding_mode='zero'))) densenet.add_module('denseblock1', DenseBlock(blocks[0], growth_rate=growth_rate)) densenet.add_module('transitiondown1', Transition(0.5)) densenet.add_module('denseblock2', DenseBlock(blocks[1], growth_rate=growth_rate)) densenet.add_module('transitiondown2', Transition(0.5)) densenet.add_module('denseblock3', DenseBlock(blocks[2], growth_rate=growth_rate)) densenet.add_module('transitiondown3', Transition(0.5)) densenet.add_module('denseblock4', DenseBlock(blocks[3], growth_rate=growth_rate)) densenet.add_module('classifier_norm', BatchNorm2d(name='classifier_norm')) densenet.add_module('classifier_relu', Relu(name='classifier_relu')) densenet.add_module('avg_pool', GlobalAvgPool2d(name='avg_pool')) if include_top: densenet.add_module( 'classifier', Dense(num_classes, activation=None, name='classifier')) densenet.add_module('softmax', SoftMax(name='softmax')) densenet.name = name model = ImageClassificationModel(input_shape=input_shape, output=densenet) with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'imagenet_labels1.txt'), 'r', encoding='utf-8-sig') as f: labels = [l.rstrip() for l in f] model.class_names = labels model.preprocess_flow = [ resize((input_shape[0], input_shape[1]), keep_aspect=True), normalize(0, 255), normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ] # model.summary() return model