Esempio n. 1
0
def get_model(path, n_class):
    from models.mobilenet_v2 import MobileNetV2
    fullnet = MobileNetV2(num_classes=1000)

    # fullnet not pruned
    if not args.isfullnetpruned:
        fullnet.load_state_dict(torch.load(args.load_path))
        net = MobileNetV2_prescreen(fullnet)
        del fullnet
    # fullnet is pruned model: used for iterative pruning
    else:
        if args.isfullnetpruned:
            net = MobileNetV2_prescreen(fullnet)
            checkpoint = torch.load(path, map_location='cpu')
            if 'state_dict' in checkpoint.keys():
                checkpoint = checkpoint['state_dict']
            from collections import OrderedDict
            new_state_dict = OrderedDict()
            for k, v in checkpoint.items():
                if k[0:6] == 'module':
                    name = k[7:]  # remove module.
                else:
                    name = k
                new_state_dict[name] = v

            net.load_state_dict(new_state_dict)
            del fullnet

    if args.n_gpu > 1:
        net = torch.nn.DataParallel(net, device_ids=gpu_list)
        net = net.to(device)
    else:
        net = net.to(device)

    return net
Esempio n. 2
0
def model_fn(FLAGS, objective, optimizer, metrics):

    input_layer = Input(shape=(FLAGS.input_size, FLAGS.input_size, 3))

    base_model1 = Xception(weights="imagenet",
                           include_top=False,
                           pooling='avg',
                           input_shape=(FLAGS.input_size, FLAGS.input_size, 3),
                           classes=FLAGS.num_classes)
    base_model2 = MobileNetV2(weights='imagenet',
                              include_top=False,
                              pooling='avg',
                              alpha=1.4,
                              input_shape=(FLAGS.input_size, FLAGS.input_size, 3),
                              classes=FLAGS.num_classes,
                              )
    base_model3 = DenseNet201(weights="imagenet",
                              include_top=False,
                              pooling='avg',
                              input_shape=(FLAGS.input_size, FLAGS.input_size, 3),
                              classes=FLAGS.num_classes)
    x1 = base_model1(inputs=input_layer)
    x2 = base_model2(inputs=input_layer)
    x3 = base_model3(inputs=input_layer)

    # 值得修改的地方
    x1 = Dense(FLAGS.num_classes, activation='relu')(x1)
    x2 = Dense(FLAGS.num_classes, activation='relu')(x2)
    x3 = Dense(FLAGS.num_classes, activation='relu')(x3)
    x = concatenate([x1, x2, x3], axis=1)

    predictions = Dense(FLAGS.num_classes, activation='softmax')(x)
    model = Model(inputs=input_layer, outputs=predictions)
    model.compile(loss=objective, optimizer=optimizer, metrics=metrics)
    return model
    def build_model(self):
        print("build mobile_net_v2")
        mobile_v2_model = MobileNetV2(input_shape=(224, 224, 3),
                                      alpha=1.0,
                                      include_top=False,
                                      weights=None)
        x = mobile_v2_model.output
        x = GlobalAveragePooling2D()(x)
        outputs = Dense(self.config.num_output,
                        use_bias=True,
                        name='Logits_77')(x)

        self.model = Model(inputs=mobile_v2_model.inputs, outputs=outputs)
        if hasattr(self.config, "weights"):
            print("loading weights from", self.config.weights)
            self.model.load_weights(self.config.weights, by_name=True)
        self.model.compile(
            loss=self.face_loss,
            optimizer=self.config.optimizer,
            metrics=[self.nme],
        )
Esempio n. 4
0
def get_model_and_checkpoint(model, dataset, checkpoint_path, n_gpu=1):
    if model == 'mobilenet' and dataset == 'imagenet':
        from models.mobilenet import MobileNet
        net = MobileNet(n_class=1000)
    elif model == 'mobilenetv2' and dataset == 'imagenet':
        from models.mobilenet_v2 import MobileNetV2
        net = MobileNetV2(n_class=1000)
    else:
        raise NotImplementedError
    if torch.cuda.is_available():
        sd = torch.load(checkpoint_path)
    else:
        sd = torch.load(checkpoint_path, map_location=torch.device('cpu'))
    if 'state_dict' in sd:  # a checkpoint but not a state_dict
        sd = sd['state_dict']
    sd = {k.replace('module.', ''): v for k, v in sd.items()}
    net.load_state_dict(sd)
    if torch.cuda.is_available():
        net = net.cuda()
    if torch.cuda.is_available() and n_gpu > 1:
        net = torch.nn.DataParallel(net, range(n_gpu))

    return net, deepcopy(
        net.state_dict())  # deepcopy的时候会将对象的每一层复制一个单独的个体出来,与之前的个体完全没有关系了
Esempio n. 5
0
def build_model(net='MobileNet',
                input_shape=(224, 224, 3),
                siamese_weights=None,
                share=True):
    if net == 'MobileNet':
        base_model = MobileNet(include_top=False, input_shape=input_shape)
    elif net == 'MobileNetV2':
        base_model = MobileNetV2(include_top=False, input_shape=input_shape)
    elif net == 'NASNetMobile':
        base_model = NASNetMobile(include_top=False, input_shape=input_shape)
    elif net == 'ResNet18':
        base_model = ResNet18(include_top=False, input_shape=input_shape)
    elif net == 'ResNet18V2':
        base_model = ResNet18V2(include_top=False, input_shape=input_shape)
    elif net == 'ResNet34':
        base_model = ResNet34(include_top=False, input_shape=input_shape)
    elif net == 'ResNet34V2':
        base_model = ResNet34V2(include_top=False, input_shape=input_shape)
    elif net == 'DenseNet21':
        base_model = DenseNet(include_top=False,
                              blocks=[2, 2, 2, 2],
                              input_shape=input_shape,
                              name='a')
        if share == False:
            base_model_b = DenseNet(include_top=False,
                                    blocks=[2, 2, 2, 2],
                                    input_shape=input_shape,
                                    name='b')
    elif net == 'DenseNet69':
        base_model = DenseNet(include_top=False,
                              blocks=[6, 8, 10, 8],
                              input_shape=input_shape)
        if share == False:
            base_model_b = DenseNet(include_top=False,
                                    blocks=[6, 8, 10, 8],
                                    input_shape=input_shape,
                                    name='b')
    elif net == 'DenseNet109':
        base_model = DenseNet(include_top=False,
                              blocks=[6, 12, 18, 16],
                              input_shape=input_shape)
        if share == False:
            base_model_b = DenseNet(include_top=False,
                                    blocks=[6, 12, 18, 16],
                                    input_shape=input_shape,
                                    name='b')
    elif net == 'DenseShuffleV1_57_373':
        base_model = DenseShuffleV1(include_top=False,
                                    blocks=[6, 8, 12],
                                    input_shape=input_shape,
                                    num_shuffle_units=[3, 7, 3],
                                    scale_factor=1.0,
                                    bottleneck_ratio=1,
                                    dropout_rate=0.5)
    elif net == 'DenseShuffleV2_57_373':
        base_model = DenseShuffleV2(include_top=False,
                                    blocks=[6, 8, 12],
                                    input_shape=input_shape,
                                    num_shuffle_units=[3, 7, 3],
                                    scale_factor=1.0,
                                    bottleneck_ratio=1,
                                    dropout_rate=0.5)
    elif net == 'DenseShuffleV2_49_353':
        base_model = DenseShuffleV2(include_top=False,
                                    blocks=[6, 8, 8],
                                    input_shape=input_shape,
                                    num_shuffle_units=[3, 5, 3],
                                    scale_factor=1.0,
                                    bottleneck_ratio=1,
                                    dropout_rate=0.5)
    elif net == 'DenseShuffleV2_17_232':
        base_model = DenseShuffleV2(include_top=False,
                                    blocks=[2, 2, 2],
                                    input_shape=input_shape,
                                    num_shuffle_units=[2, 3, 2],
                                    scale_factor=1.0,
                                    bottleneck_ratio=1,
                                    dropout_rate=0.5)
    elif net == 'ShuffleNetV2':
        base_model = ShuffleNetV2(include_top=False,
                                  scale_factor=1.0,
                                  pooling='avg',
                                  input_shape=input_shape,
                                  num_shuffle_units=[3, 7, 3],
                                  bottleneck_ratio=1)
    elif net == 'ShuffleNet':
        base_model = ShuffleNet(include_top=False,
                                scale_factor=1.0,
                                pooling='avg',
                                input_shape=input_shape,
                                num_shuffle_units=[3, 7, 3],
                                bottleneck_ratio=1)
    elif net == 'MobileNetV3Small':
        base_model = MobileNetV3Small(include_top=False,
                                      input_shape=input_shape)
    elif net == 'SqueezeNet':
        base_model = SqueezeNet(include_top=False, input_shape=input_shape)
    else:
        print('the network name you have entered is not supported yet')
        sys.exit()

    input_a = keras.layers.Input(shape=input_shape, name='input_a')
    input_b = keras.layers.Input(shape=input_shape, name='input_b')
    processed_a = base_model(input_a)

    if share:
        processed_b = base_model(input_b)
    else:
        processed_b = base_model_b(input_b)

    #processed_a = keras.layers.Activation('sigmoid', name='sigmoid_a')(processed_a)
    #processed_b = keras.layers.Activation('sigmoid', name='sigmoid_b')(processed_b)
    normalize = keras.layers.Lambda(lambda x: K.l2_normalize(x, axis=-1),
                                    name='normalize')
    processed_a = normalize(processed_a)
    processed_b = normalize(processed_b)
    distance = keras.layers.Lambda(euclidean_distance,
                                   output_shape=eucl_dist_output_shape,
                                   name='dist')([processed_a, processed_b])
    model = keras.models.Model([input_a, input_b], distance)
    if siamese_weights is not None:
        print('load siamses weights ....')
        model.load_weights(siamese_weights)
    print('hahahaha')
    return model