예제 #1
0
    def __init__(self, num_classes=1000):
        super(AlexNet, self).__init__()

        self.features = nn.Sequential(
            nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=0),
            nn.ReLU(inplace=True),
            LRN(local_size=5, alpha=0.0001, beta=0.75),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(96, 256, kernel_size=5, padding=2, groups=2),
            nn.ReLU(inplace=True),
            LRN(local_size=5, alpha=0.0001, beta=0.75),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(256, 384, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 384, kernel_size=3, padding=1, groups=2),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1, groups=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )

        self.classifier = nn.Sequential(
            nn.Linear(256 * 5 * 5, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, num_classes),
        )
예제 #2
0
def handle_concat(spec, bottoms):
    local_size = spec.lrn_param.local_size
    alpha = spec.lrn_param.alpha
    beta = spec.lrn_param.beta
    return LRN(alpha=alpha,
               beta=beta,
               n=local_size,
               name=spec.name,
               data_format='channels_first')(bottoms)
예제 #3
0
def make_layers(cfg, batch_norm=False):
    layers = []
    inchannels = 3
    print(cfg)
    for v in cfg:
        if v == 'M':
            layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
        elif v == 'L':
            layers += [LRN(local_size=5, alpha=0.0001, beta=0.75)]
        else:
            f = int(v.split('_')[0])
            s = int(v.split('_')[1])
            if batch_norm:
                if f is 1:
                    layers += [conv1x1(inchannels, s), nn.BatchNorm2d(s), nn.ReLU(inplace = True)]
                else:
                    layers += [conv3x3(inchannels, s), nn.BatchNorm2d(s), nn.ReLU(inplace=True)]
            else:
                if f is 1:
                    layers += [conv1x1(inchannels, s), nn.ReLU(inplace = True)]
                else:
                    layers += [conv3x3(inchannels, s), nn.ReLU(inplace=True)]
            inchannels = s
    return nn.Sequential(*layers)
예제 #4
0
def create_googlenet(weights_path=None):
    # creates GoogLeNet a.k.a. Inception v1 (Szegedy, 2015)
    input = Input(shape=(3, 224, 224))

    input_pad = ZeroPadding2D(padding=(3, 3))(input)
    conv1_7x7_s2 = Conv2D(64, (7, 7),
                          strides=(2, 2),
                          padding='valid',
                          activation='relu',
                          name='conv1/7x7_s2',
                          kernel_regularizer=l2(0.0002))(input_pad)
    conv1_zero_pad = ZeroPadding2D(padding=(1, 1))(conv1_7x7_s2)
    pool1_helper = PoolHelper()(conv1_zero_pad)
    pool1_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                strides=(2, 2),
                                padding='valid',
                                name='pool1/3x3_s2')(pool1_helper)
    pool1_norm1 = LRN(name='pool1/norm1')(pool1_3x3_s2)

    conv2_3x3_reduce = Conv2D(64, (1, 1),
                              padding='same',
                              activation='relu',
                              name='conv2/3x3_reduce',
                              kernel_regularizer=l2(0.0002))(pool1_norm1)
    conv2_3x3 = Conv2D(192, (3, 3),
                       padding='same',
                       activation='relu',
                       name='conv2/3x3',
                       kernel_regularizer=l2(0.0002))(conv2_3x3_reduce)
    conv2_norm2 = LRN(name='conv2/norm2')(conv2_3x3)
    conv2_zero_pad = ZeroPadding2D(padding=(1, 1))(conv2_norm2)
    pool2_helper = PoolHelper()(conv2_zero_pad)
    pool2_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                strides=(2, 2),
                                padding='valid',
                                name='pool2/3x3_s2')(pool2_helper)

    inception_3a_1x1 = Conv2D(64, (1, 1),
                              padding='same',
                              activation='relu',
                              name='inception_3a/1x1',
                              kernel_regularizer=l2(0.0002))(pool2_3x3_s2)
    inception_3a_3x3_reduce = Conv2D(
        96, (1, 1),
        padding='same',
        activation='relu',
        name='inception_3a/3x3_reduce',
        kernel_regularizer=l2(0.0002))(pool2_3x3_s2)
    inception_3a_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_3a_3x3_reduce)
    inception_3a_3x3 = Conv2D(
        128, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_3a/3x3',
        kernel_regularizer=l2(0.0002))(inception_3a_3x3_pad)
    inception_3a_5x5_reduce = Conv2D(
        16, (1, 1),
        padding='same',
        activation='relu',
        name='inception_3a/5x5_reduce',
        kernel_regularizer=l2(0.0002))(pool2_3x3_s2)
    inception_3a_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_3a_5x5_reduce)
    inception_3a_5x5 = Conv2D(
        32, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_3a/5x5',
        kernel_regularizer=l2(0.0002))(inception_3a_5x5_pad)
    inception_3a_pool = MaxPooling2D(pool_size=(3, 3),
                                     strides=(1, 1),
                                     padding='same',
                                     name='inception_3a/pool')(pool2_3x3_s2)
    inception_3a_pool_proj = Conv2D(
        32, (1, 1),
        padding='same',
        activation='relu',
        name='inception_3a/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_3a_pool)
    inception_3a_output = Concatenate(axis=1, name='inception_3a/output')([
        inception_3a_1x1, inception_3a_3x3, inception_3a_5x5,
        inception_3a_pool_proj
    ])

    inception_3b_1x1 = Conv2D(
        128, (1, 1),
        padding='same',
        activation='relu',
        name='inception_3b/1x1',
        kernel_regularizer=l2(0.0002))(inception_3a_output)
    inception_3b_3x3_reduce = Conv2D(
        128, (1, 1),
        padding='same',
        activation='relu',
        name='inception_3b/3x3_reduce',
        kernel_regularizer=l2(0.0002))(inception_3a_output)
    inception_3b_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_3b_3x3_reduce)
    inception_3b_3x3 = Conv2D(
        192, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_3b/3x3',
        kernel_regularizer=l2(0.0002))(inception_3b_3x3_pad)
    inception_3b_5x5_reduce = Conv2D(
        32, (1, 1),
        padding='same',
        activation='relu',
        name='inception_3b/5x5_reduce',
        kernel_regularizer=l2(0.0002))(inception_3a_output)
    inception_3b_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_3b_5x5_reduce)
    inception_3b_5x5 = Conv2D(
        96, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_3b/5x5',
        kernel_regularizer=l2(0.0002))(inception_3b_5x5_pad)
    inception_3b_pool = MaxPooling2D(
        pool_size=(3, 3),
        strides=(1, 1),
        padding='same',
        name='inception_3b/pool')(inception_3a_output)
    inception_3b_pool_proj = Conv2D(
        64, (1, 1),
        padding='same',
        activation='relu',
        name='inception_3b/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_3b_pool)
    inception_3b_output = Concatenate(axis=1, name='inception_3b/output')([
        inception_3b_1x1, inception_3b_3x3, inception_3b_5x5,
        inception_3b_pool_proj
    ])

    inception_3b_output_zero_pad = ZeroPadding2D(
        padding=(1, 1))(inception_3b_output)
    pool3_helper = PoolHelper()(inception_3b_output_zero_pad)
    pool3_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                strides=(2, 2),
                                padding='valid',
                                name='pool3/3x3_s2')(pool3_helper)

    inception_4a_1x1 = Conv2D(192, (1, 1),
                              padding='same',
                              activation='relu',
                              name='inception_4a/1x1',
                              kernel_regularizer=l2(0.0002))(pool3_3x3_s2)
    inception_4a_3x3_reduce = Conv2D(
        96, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4a/3x3_reduce',
        kernel_regularizer=l2(0.0002))(pool3_3x3_s2)
    inception_4a_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_4a_3x3_reduce)
    inception_4a_3x3 = Conv2D(
        208, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_4a/3x3',
        kernel_regularizer=l2(0.0002))(inception_4a_3x3_pad)
    inception_4a_5x5_reduce = Conv2D(
        16, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4a/5x5_reduce',
        kernel_regularizer=l2(0.0002))(pool3_3x3_s2)
    inception_4a_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_4a_5x5_reduce)
    inception_4a_5x5 = Conv2D(
        48, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_4a/5x5',
        kernel_regularizer=l2(0.0002))(inception_4a_5x5_pad)
    inception_4a_pool = MaxPooling2D(pool_size=(3, 3),
                                     strides=(1, 1),
                                     padding='same',
                                     name='inception_4a/pool')(pool3_3x3_s2)
    inception_4a_pool_proj = Conv2D(
        64, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4a/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_4a_pool)
    inception_4a_output = Concatenate(axis=1, name='inception_4a/output')([
        inception_4a_1x1, inception_4a_3x3, inception_4a_5x5,
        inception_4a_pool_proj
    ])

    loss1_ave_pool = AveragePooling2D(
        pool_size=(5, 5), strides=(3, 3),
        name='loss1/ave_pool')(inception_4a_output)
    loss1_conv = Conv2D(128, (1, 1),
                        padding='same',
                        activation='relu',
                        name='loss1/conv',
                        kernel_regularizer=l2(0.0002))(loss1_ave_pool)
    loss1_flat = Flatten()(loss1_conv)
    loss1_fc = Dense(1024,
                     activation='relu',
                     name='loss1/fc',
                     kernel_regularizer=l2(0.0002))(loss1_flat)
    loss1_drop_fc = Dropout(rate=0.7)(loss1_fc)
    loss1_classifier = Dense(1000,
                             name='loss1/classifier',
                             kernel_regularizer=l2(0.0002))(loss1_drop_fc)
    loss1_classifier_act = Activation('softmax')(loss1_classifier)

    inception_4b_1x1 = Conv2D(
        160, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4b/1x1',
        kernel_regularizer=l2(0.0002))(inception_4a_output)
    inception_4b_3x3_reduce = Conv2D(
        112, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4b/3x3_reduce',
        kernel_regularizer=l2(0.0002))(inception_4a_output)
    inception_4b_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_4b_3x3_reduce)
    inception_4b_3x3 = Conv2D(
        224, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_4b/3x3',
        kernel_regularizer=l2(0.0002))(inception_4b_3x3_pad)
    inception_4b_5x5_reduce = Conv2D(
        24, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4b/5x5_reduce',
        kernel_regularizer=l2(0.0002))(inception_4a_output)
    inception_4b_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_4b_5x5_reduce)
    inception_4b_5x5 = Conv2D(
        64, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_4b/5x5',
        kernel_regularizer=l2(0.0002))(inception_4b_5x5_pad)
    inception_4b_pool = MaxPooling2D(
        pool_size=(3, 3),
        strides=(1, 1),
        padding='same',
        name='inception_4b/pool')(inception_4a_output)
    inception_4b_pool_proj = Conv2D(
        64, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4b/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_4b_pool)
    inception_4b_output = Concatenate(axis=1, name='inception_4b/output')([
        inception_4b_1x1, inception_4b_3x3, inception_4b_5x5,
        inception_4b_pool_proj
    ])

    inception_4c_1x1 = Conv2D(
        128, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4c/1x1',
        kernel_regularizer=l2(0.0002))(inception_4b_output)
    inception_4c_3x3_reduce = Conv2D(
        128, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4c/3x3_reduce',
        kernel_regularizer=l2(0.0002))(inception_4b_output)
    inception_4c_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_4c_3x3_reduce)
    inception_4c_3x3 = Conv2D(
        256, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_4c/3x3',
        kernel_regularizer=l2(0.0002))(inception_4c_3x3_pad)
    inception_4c_5x5_reduce = Conv2D(
        24, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4c/5x5_reduce',
        kernel_regularizer=l2(0.0002))(inception_4b_output)
    inception_4c_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_4c_5x5_reduce)
    inception_4c_5x5 = Conv2D(
        64, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_4c/5x5',
        kernel_regularizer=l2(0.0002))(inception_4c_5x5_pad)
    inception_4c_pool = MaxPooling2D(
        pool_size=(3, 3),
        strides=(1, 1),
        padding='same',
        name='inception_4c/pool')(inception_4b_output)
    inception_4c_pool_proj = Conv2D(
        64, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4c/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_4c_pool)
    inception_4c_output = Concatenate(axis=1, name='inception_4c/output')([
        inception_4c_1x1, inception_4c_3x3, inception_4c_5x5,
        inception_4c_pool_proj
    ])

    inception_4d_1x1 = Conv2D(
        112, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4d/1x1',
        kernel_regularizer=l2(0.0002))(inception_4c_output)
    inception_4d_3x3_reduce = Conv2D(
        144, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4d/3x3_reduce',
        kernel_regularizer=l2(0.0002))(inception_4c_output)
    inception_4d_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_4d_3x3_reduce)
    inception_4d_3x3 = Conv2D(
        288, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_4d/3x3',
        kernel_regularizer=l2(0.0002))(inception_4d_3x3_pad)
    inception_4d_5x5_reduce = Conv2D(
        32, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4d/5x5_reduce',
        kernel_regularizer=l2(0.0002))(inception_4c_output)
    inception_4d_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_4d_5x5_reduce)
    inception_4d_5x5 = Conv2D(
        64, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_4d/5x5',
        kernel_regularizer=l2(0.0002))(inception_4d_5x5_pad)
    inception_4d_pool = MaxPooling2D(
        pool_size=(3, 3),
        strides=(1, 1),
        padding='same',
        name='inception_4d/pool')(inception_4c_output)
    inception_4d_pool_proj = Conv2D(
        64, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4d/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_4d_pool)
    inception_4d_output = Concatenate(axis=1, name='inception_4d/output')([
        inception_4d_1x1, inception_4d_3x3, inception_4d_5x5,
        inception_4d_pool_proj
    ])

    loss2_ave_pool = AveragePooling2D(
        pool_size=(5, 5), strides=(3, 3),
        name='loss2/ave_pool')(inception_4d_output)
    loss2_conv = Conv2D(128, (1, 1),
                        padding='same',
                        activation='relu',
                        name='loss2/conv',
                        kernel_regularizer=l2(0.0002))(loss2_ave_pool)
    loss2_flat = Flatten()(loss2_conv)
    loss2_fc = Dense(1024,
                     activation='relu',
                     name='loss2/fc',
                     kernel_regularizer=l2(0.0002))(loss2_flat)
    loss2_drop_fc = Dropout(rate=0.7)(loss2_fc)
    loss2_classifier = Dense(1000,
                             name='loss2/classifier',
                             kernel_regularizer=l2(0.0002))(loss2_drop_fc)
    loss2_classifier_act = Activation('softmax')(loss2_classifier)

    inception_4e_1x1 = Conv2D(
        256, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4e/1x1',
        kernel_regularizer=l2(0.0002))(inception_4d_output)
    inception_4e_3x3_reduce = Conv2D(
        160, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4e/3x3_reduce',
        kernel_regularizer=l2(0.0002))(inception_4d_output)
    inception_4e_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_4e_3x3_reduce)
    inception_4e_3x3 = Conv2D(
        320, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_4e/3x3',
        kernel_regularizer=l2(0.0002))(inception_4e_3x3_pad)
    inception_4e_5x5_reduce = Conv2D(
        32, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4e/5x5_reduce',
        kernel_regularizer=l2(0.0002))(inception_4d_output)
    inception_4e_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_4e_5x5_reduce)
    inception_4e_5x5 = Conv2D(
        128, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_4e/5x5',
        kernel_regularizer=l2(0.0002))(inception_4e_5x5_pad)
    inception_4e_pool = MaxPooling2D(
        pool_size=(3, 3),
        strides=(1, 1),
        padding='same',
        name='inception_4e/pool')(inception_4d_output)
    inception_4e_pool_proj = Conv2D(
        128, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4e/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_4e_pool)
    inception_4e_output = Concatenate(axis=1, name='inception_4e/output')([
        inception_4e_1x1, inception_4e_3x3, inception_4e_5x5,
        inception_4e_pool_proj
    ])

    inception_4e_output_zero_pad = ZeroPadding2D(
        padding=(1, 1))(inception_4e_output)
    pool4_helper = PoolHelper()(inception_4e_output_zero_pad)
    pool4_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                strides=(2, 2),
                                padding='valid',
                                name='pool4/3x3_s2')(pool4_helper)

    inception_5a_1x1 = Conv2D(256, (1, 1),
                              padding='same',
                              activation='relu',
                              name='inception_5a/1x1',
                              kernel_regularizer=l2(0.0002))(pool4_3x3_s2)
    inception_5a_3x3_reduce = Conv2D(
        160, (1, 1),
        padding='same',
        activation='relu',
        name='inception_5a/3x3_reduce',
        kernel_regularizer=l2(0.0002))(pool4_3x3_s2)
    inception_5a_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_5a_3x3_reduce)
    inception_5a_3x3 = Conv2D(
        320, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_5a/3x3',
        kernel_regularizer=l2(0.0002))(inception_5a_3x3_pad)
    inception_5a_5x5_reduce = Conv2D(
        32, (1, 1),
        padding='same',
        activation='relu',
        name='inception_5a/5x5_reduce',
        kernel_regularizer=l2(0.0002))(pool4_3x3_s2)
    inception_5a_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_5a_5x5_reduce)
    inception_5a_5x5 = Conv2D(
        128, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_5a/5x5',
        kernel_regularizer=l2(0.0002))(inception_5a_5x5_pad)
    inception_5a_pool = MaxPooling2D(pool_size=(3, 3),
                                     strides=(1, 1),
                                     padding='same',
                                     name='inception_5a/pool')(pool4_3x3_s2)
    inception_5a_pool_proj = Conv2D(
        128, (1, 1),
        padding='same',
        activation='relu',
        name='inception_5a/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_5a_pool)
    inception_5a_output = Concatenate(axis=1, name='inception_5a/output')([
        inception_5a_1x1, inception_5a_3x3, inception_5a_5x5,
        inception_5a_pool_proj
    ])

    inception_5b_1x1 = Conv2D(
        384, (1, 1),
        padding='same',
        activation='relu',
        name='inception_5b/1x1',
        kernel_regularizer=l2(0.0002))(inception_5a_output)
    inception_5b_3x3_reduce = Conv2D(
        192, (1, 1),
        padding='same',
        activation='relu',
        name='inception_5b/3x3_reduce',
        kernel_regularizer=l2(0.0002))(inception_5a_output)
    inception_5b_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_5b_3x3_reduce)
    inception_5b_3x3 = Conv2D(
        384, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_5b/3x3',
        kernel_regularizer=l2(0.0002))(inception_5b_3x3_pad)
    inception_5b_5x5_reduce = Conv2D(
        48, (1, 1),
        padding='same',
        activation='relu',
        name='inception_5b/5x5_reduce',
        kernel_regularizer=l2(0.0002))(inception_5a_output)
    inception_5b_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_5b_5x5_reduce)
    inception_5b_5x5 = Conv2D(
        128, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_5b/5x5',
        kernel_regularizer=l2(0.0002))(inception_5b_5x5_pad)
    inception_5b_pool = MaxPooling2D(
        pool_size=(3, 3),
        strides=(1, 1),
        padding='same',
        name='inception_5b/pool')(inception_5a_output)
    inception_5b_pool_proj = Conv2D(
        128, (1, 1),
        padding='same',
        activation='relu',
        name='inception_5b/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_5b_pool)
    inception_5b_output = Concatenate(axis=1, name='inception_5b/output')([
        inception_5b_1x1, inception_5b_3x3, inception_5b_5x5,
        inception_5b_pool_proj
    ])

    pool5_7x7_s1 = AveragePooling2D(pool_size=(7, 7),
                                    strides=(1, 1),
                                    name='pool5/7x7_s2')(inception_5b_output)
    loss3_flat = Flatten()(pool5_7x7_s1)
    pool5_drop_7x7_s1 = Dropout(rate=0.4)(loss3_flat)
    loss3_classifier = Dense(1000,
                             name='loss3/classifier',
                             kernel_regularizer=l2(0.0002))(pool5_drop_7x7_s1)
    loss3_classifier_act = Activation('softmax', name='prob')(loss3_classifier)

    googlenet = Model(inputs=input,
                      outputs=[
                          loss1_classifier_act, loss2_classifier_act,
                          loss3_classifier_act
                      ])

    if weights_path:
        googlenet.load_weights(weights_path)

    if keras.backend.backend() == 'tensorflow':
        # convert the convolutional kernels for tensorflow
        ops = []
        for layer in googlenet.layers:
            if layer.__class__.__name__ == 'Conv2D':
                original_w = K.get_value(layer.kernel)
                converted_w = convert_kernel(original_w)
                ops.append(tf.assign(layer.kernel, converted_w).op)
        K.get_session().run(ops)

    return googlenet
    def build(input_shape, num_classes):

        input = Input(shape=input_shape)

        conv1_7x7 = Conv2D(filters=64,
                           kernel_size=(7, 7),
                           strides=(2, 2),
                           padding='same',
                           activation='relu',
                           name='conv1/7x7_s2')(input)
        pool1_3x3 = MaxPooling2D(pool_size=(3, 3),
                                 strides=(2, 2),
                                 padding='same',
                                 name='pool1/3x3_s2')(conv1_7x7)
        pool1_norm1 = LRN(name='pool1/norm1')(pool1_3x3)
        conv2_3x3_reduce = Conv2D(filters=64,
                                  kernel_size=(1, 1),
                                  padding='valid',
                                  activation='relu',
                                  name='conv2/3x3_reduce')(pool1_norm1)
        conv2_3x3 = Conv2D(filters=192,
                           kernel_size=(3, 3),
                           padding='same',
                           activation='relu',
                           name='conv2/3x3')(conv2_3x3_reduce)
        conv2_norm2 = LRN(name='conv2/norm2')(conv2_3x3)
        pool2_3x3 = MaxPooling2D(pool_size=(3, 3),
                                 strides=(2, 2),
                                 padding='same',
                                 name='pool2/3x3_s2')(conv2_norm2)

        inception_3a_1x1 = Conv2D(filters=64,
                                  kernel_size=(1, 1),
                                  padding='same',
                                  activation='relu',
                                  name='inception_3a/1x1')(pool2_3x3)
        inception_3a_3x3_reduce = Conv2D(
            filters=96,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_3a/3x3_reduce')(pool2_3x3)
        inception_3a_3x3 = Conv2D(
            filters=128,
            kernel_size=(3, 3),
            padding='same',
            activation='relu',
            name='inception_3a/3x3')(inception_3a_3x3_reduce)
        inception_3a_5x5_reduce = Conv2D(
            filters=16,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_3a/5x5_reduce')(pool2_3x3)
        inception_3a_5x5 = Conv2D(
            filters=32,
            kernel_size=(5, 5),
            padding='same',
            activation='relu',
            name='inception_3a/5x5')(inception_3a_5x5_reduce)
        inception_3a_pool = MaxPooling2D(pool_size=(3, 3),
                                         strides=(1, 1),
                                         padding='same',
                                         name='inception_3a/pool')(pool2_3x3)
        inception_3a_pool_proj = Conv2D(
            filters=32,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_3a/pool_proj')(inception_3a_pool)
        inception_3a_output = Concatenate(axis=-1,
                                          name='inception_3a/output')([
                                              inception_3a_1x1,
                                              inception_3a_3x3,
                                              inception_3a_5x5,
                                              inception_3a_pool_proj
                                          ])

        inception_3b_1x1 = Conv2D(filters=128,
                                  kernel_size=(1, 1),
                                  padding='same',
                                  activation='relu',
                                  name='inception_3b/1x1')(inception_3a_output)
        inception_3b_3x3_reduce = Conv2D(
            filters=128,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_3b/3x3_reduce')(inception_3a_output)
        inception_3b_3x3 = Conv2D(
            filters=192,
            kernel_size=(3, 3),
            padding='same',
            activation='relu',
            name='inception_3b/3x3')(inception_3b_3x3_reduce)
        inception_3b_5x5_reduce = Conv2D(
            filters=32,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_3b/5x5_reduce')(inception_3a_output)
        inception_3b_5x5 = Conv2D(
            filters=96,
            kernel_size=(5, 5),
            padding='same',
            activation='relu',
            name='inception_3b/5x5')(inception_3b_5x5_reduce)
        inception_3b_pool = MaxPooling2D(
            pool_size=(3, 3),
            strides=(1, 1),
            padding='same',
            name='inception_3b/pool')(inception_3a_output)
        inception_3b_pool_proj = Conv2D(
            filters=64,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_3b/pool_proj')(inception_3b_pool)
        inception_3b_output = Concatenate(axis=-1,
                                          name='inception_3b/output')([
                                              inception_3b_1x1,
                                              inception_3b_3x3,
                                              inception_3b_5x5,
                                              inception_3b_pool_proj
                                          ])

        inception_4a_1x1 = Conv2D(filters=192,
                                  kernel_size=(1, 1),
                                  padding='same',
                                  activation='relu',
                                  name='inception_4a/1x1')(inception_3b_output)
        inception_4a_3x3_reduce = Conv2D(
            filters=96,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4a/3x3_reduce')(inception_3b_output)
        inception_4a_3x3 = Conv2D(
            filters=208,
            kernel_size=(3, 3),
            padding='same',
            activation='relu',
            name='inception_4a/3x3',
            kernel_regularizer=l2(0.0002))(inception_4a_3x3_reduce)
        inception_4a_5x5_reduce = Conv2D(
            filters=16,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4a/5x5_reduce')(inception_3b_output)
        inception_4a_5x5 = Conv2D(
            filters=48,
            kernel_size=(5, 5),
            padding='same',
            activation='relu',
            name='inception_4a/5x5')(inception_4a_5x5_reduce)
        inception_4a_pool = MaxPooling2D(
            pool_size=(3, 3),
            strides=(1, 1),
            padding='same',
            name='inception_4a/pool')(inception_3b_output)
        inception_4a_pool_proj = Conv2D(
            filters=64,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4a/pool_proj')(inception_4a_pool)
        inception_4a_output = Concatenate(axis=-1,
                                          name='inception_4a/output')([
                                              inception_4a_1x1,
                                              inception_4a_3x3,
                                              inception_4a_5x5,
                                              inception_4a_pool_proj
                                          ])

        loss1_ave_pool = AveragePooling2D(
            pool_size=(5, 5), strides=(3, 3),
            name='loss1/ave_pool')(inception_4a_output)
        loss1_conv = Conv2D(filters=128,
                            kernel_size=(1, 1),
                            padding='same',
                            activation='relu',
                            name='loss1/conv')(loss1_ave_pool)
        loss1_fc = Dense(1024, activation='relu', name='loss1/fc')(loss1_conv)
        loss1_drop_fc = Dropout(rate=0.5)(loss1_fc)
        loss1_classifier = Dense(num_classes,
                                 name='loss1/classifier')(loss1_drop_fc)
        loss1_classifier_act = Activation('softmax')(loss1_classifier)

        inception_4b_1x1 = Conv2D(filters=160,
                                  kernel_size=(1, 1),
                                  padding='same',
                                  activation='relu',
                                  name='inception_4b/1x1')(inception_4a_output)
        inception_4b_3x3_reduce = Conv2D(
            filters=112,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4b/3x3_reduce')(inception_4a_output)
        inception_4b_3x3 = Conv2D(
            filters=224,
            kernel_size=(3, 3),
            padding='same',
            activation='relu',
            name='inception_4b/3x3')(inception_4b_3x3_reduce)
        inception_4b_5x5_reduce = Conv2D(
            filters=24,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4b/5x5_reduce')(inception_4a_output)
        inception_4b_5x5 = Conv2D(
            filters=64,
            kernel_size=(5, 5),
            padding='same',
            activation='relu',
            name='inception_4b/5x5')(inception_4b_5x5_reduce)
        inception_4b_pool = MaxPooling2D(
            pool_size=(3, 3),
            strides=(1, 1),
            padding='same',
            name='inception_4b/pool')(inception_4a_output)
        inception_4b_pool_proj = Conv2D(
            filters=64,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4b/pool_proj')(inception_4b_pool)
        inception_4b_output = Concatenate(axis=-1,
                                          name='inception_4b/output')([
                                              inception_4b_1x1,
                                              inception_4b_3x3,
                                              inception_4b_5x5,
                                              inception_4b_pool_proj
                                          ])

        inception_4c_1x1 = Conv2D(filters=128,
                                  kernel_size=(1, 1),
                                  padding='same',
                                  activation='relu',
                                  name='inception_4c/1x1')(inception_4b_output)
        inception_4c_3x3_reduce = Conv2D(
            filters=128,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4c/3x3_reduce')(inception_4b_output)
        inception_4c_3x3 = Conv2D(
            filters=256,
            kernel_size=(3, 3),
            padding='same',
            activation='relu',
            name='inception_4c/3x3')(inception_4c_3x3_reduce)
        inception_4c_5x5_reduce = Conv2D(
            filters=24,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4c/5x5_reduce')(inception_4b_output)
        inception_4c_5x5 = Conv2D(
            filters=64,
            kernel_size=(5, 5),
            padding='same',
            activation='relu',
            name='inception_4c/5x5')(inception_4c_5x5_reduce)
        inception_4c_pool = MaxPooling2D(
            pool_size=(3, 3),
            strides=(1, 1),
            padding='same',
            name='inception_4c/pool')(inception_4b_output)
        inception_4c_pool_proj = Conv2D(
            filters=64,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4c/pool_proj')(inception_4c_pool)
        inception_4c_output = Concatenate(axis=-1,
                                          name='inception_4c/output')([
                                              inception_4c_1x1,
                                              inception_4c_3x3,
                                              inception_4c_5x5,
                                              inception_4c_pool_proj
                                          ])

        inception_4d_1x1 = Conv2D(filters=112,
                                  kernel_size=(1, 1),
                                  padding='same',
                                  activation='relu',
                                  name='inception_4d/1x1')(inception_4c_output)
        inception_4d_3x3_reduce = Conv2D(
            filters=144,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4d/3x3_reduce')(inception_4c_output)
        inception_4d_3x3 = Conv2D(
            filters=288,
            kernel_size=(3, 3),
            padding='same',
            activation='relu',
            name='inception_4d/3x3')(inception_4d_3x3_reduce)
        inception_4d_5x5_reduce = Conv2D(
            filters=32,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4d/5x5_reduce')(inception_4c_output)
        inception_4d_5x5 = Conv2D(
            filters=64,
            kernel_size=(5, 5),
            padding='same',
            activation='relu',
            name='inception_4d/5x5')(inception_4d_5x5_reduce)
        inception_4d_pool = MaxPooling2D(
            pool_size=(3, 3),
            strides=(1, 1),
            padding='same',
            name='inception_4d/pool')(inception_4c_output)
        inception_4d_pool_proj = Conv2D(
            filters=64,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4d/pool_proj')(inception_4d_pool)
        inception_4d_output = Concatenate(axis=-1,
                                          name='inception_4d/output')([
                                              inception_4d_1x1,
                                              inception_4d_3x3,
                                              inception_4d_5x5,
                                              inception_4d_pool_proj
                                          ])

        loss2_ave_pool = AveragePooling2D(
            pool_size=(5, 5), strides=(3, 3),
            name='loss2/ave_pool')(inception_4d_output)
        loss2_conv = Conv2D(filters=128,
                            kernel_size=(1, 1),
                            padding='same',
                            activation='relu',
                            name='loss2/conv')(loss2_ave_pool)
        loss2_fc = Dense(1024, activation='relu', name='loss2/fc')(loss2_conv)
        loss2_drop_fc = Dropout(rate=0.5)(loss2_fc)
        loss2_classifier = Dense(num_classes,
                                 name='loss2/classifier')(loss2_drop_fc)
        loss2_classifier_act = Activation('softmax')(loss2_classifier)

        inception_4e_1x1 = Conv2D(filters=256,
                                  kernel_size=(1, 1),
                                  padding='same',
                                  activation='relu',
                                  name='inception_4e/1x1')(inception_4d_output)
        inception_4e_3x3_reduce = Conv2D(
            filters=160,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4e/3x3_reduce')(inception_4d_output)
        inception_4e_3x3 = Conv2D(
            filters=320,
            kernel_size=(3, 3),
            padding='same',
            activation='relu',
            name='inception_4e/3x3')(inception_4e_3x3_reduce)
        inception_4e_5x5_reduce = Conv2D(
            filters=32,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4e/5x5_reduce')(inception_4d_output)
        inception_4e_5x5 = Conv2D(
            filters=128,
            kernel_size=(5, 5),
            padding='same',
            activation='relu',
            name='inception_4e/5x5')(inception_4e_5x5_reduce)
        inception_4e_pool = MaxPooling2D(
            pool_size=(3, 3),
            strides=(1, 1),
            padding='same',
            name='inception_4e/pool')(inception_4d_output)
        inception_4e_pool_proj = Conv2D(
            filters=128,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_4e/pool_proj')(inception_4e_pool)
        inception_4e_output = Concatenate(axis=-1,
                                          name='inception_4e/output')([
                                              inception_4e_1x1,
                                              inception_4e_3x3,
                                              inception_4e_5x5,
                                              inception_4e_pool_proj
                                          ])

        inception_5a_1x1 = Conv2D(filters=256,
                                  kernel_size=(1, 1),
                                  padding='same',
                                  activation='relu',
                                  name='inception_5a/1x1')(inception_4e_output)
        inception_5a_3x3_reduce = Conv2D(
            filters=160,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_5a/3x3_reduce')(inception_4e_output)
        inception_5a_3x3 = Conv2D(
            filters=320,
            kernel_size=(3, 3),
            padding='same',
            activation='relu',
            name='inception_5a/3x3')(inception_5a_3x3_reduce)
        inception_5a_5x5_reduce = Conv2D(
            filters=32,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_5a/5x5_reduce')(inception_4e_output)
        inception_5a_5x5 = Conv2D(
            filters=128,
            kernel_size=(5, 5),
            padding='same',
            activation='relu',
            name='inception_5a/5x5')(inception_5a_5x5_reduce)
        inception_5a_pool = MaxPooling2D(
            pool_size=(3, 3),
            strides=(1, 1),
            padding='same',
            name='inception_5a/pool')(inception_4e_output)
        inception_5a_pool_proj = Conv2D(
            filters=128,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_5a/pool_proj')(inception_5a_pool)
        inception_5a_output = Concatenate(axis=-1,
                                          name='inception_5a/output')([
                                              inception_5a_1x1,
                                              inception_5a_3x3,
                                              inception_5a_5x5,
                                              inception_5a_pool_proj
                                          ])

        inception_5b_1x1 = Conv2D(filters=384,
                                  kernel_size=(1, 1),
                                  padding='same',
                                  activation='relu',
                                  name='inception_5b/1x1')(inception_5a_output)
        inception_5b_3x3_reduce = Conv2D(
            filters=192,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_5b/3x3_reduce')(inception_5a_output)
        inception_5b_3x3 = Conv2D(
            filters=384,
            kernel_size=(3, 3),
            padding='same',
            activation='relu',
            name='inception_5b/3x3')(inception_5b_3x3_reduce)
        inception_5b_5x5_reduce = Conv2D(
            filters=48,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_5b/5x5_reduce')(inception_5a_output)
        inception_5b_5x5 = Conv2D(
            filters=128,
            kernel_size=(5, 5),
            padding='same',
            activation='relu',
            name='inception_5b/5x5')(inception_5b_5x5_reduce)
        inception_5b_pool = MaxPooling2D(
            pool_size=(3, 3),
            strides=(1, 1),
            padding='same',
            name='inception_5b/pool')(inception_5a_output)
        inception_5b_pool_proj = Conv2D(
            filters=128,
            kernel_size=(1, 1),
            padding='same',
            activation='relu',
            name='inception_5b/pool_proj')(inception_5b_pool)
        inception_5b_output = Concatenate(axis=-1,
                                          name='inception_5b/output')([
                                              inception_5b_1x1,
                                              inception_5b_3x3,
                                              inception_5b_5x5,
                                              inception_5b_pool_proj
                                          ])

        pool5_7x7_s1 = AveragePooling2D(
            pool_size=(7, 7), strides=(1, 1),
            name='pool5/7x7_s2')(inception_5b_output)
        pool5_drop_7x7_s1 = Dropout(rate=0.5)(pool5_7x7_s1)
        loss3_classifier = Dense(num_classes,
                                 name='loss3/classifier')(pool5_drop_7x7_s1)
        loss3_classifier_act = Activation('softmax',
                                          name='prob')(loss3_classifier)

        inception_v1 = Model(inputs=input,
                             outputs=[
                                 loss1_classifier_act, loss2_classifier_act,
                                 loss3_classifier_act
                             ])

        return inception_v1
예제 #6
0
y_train_valid = keras.utils.to_categorical(y_train_valid, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()

model.add(ZeroPadding2D(padding=(3, 3)))
model.add(
    Conv2D(64, (7, 7),
           strides=(2, 2),
           padding='valid',
           activation='relu',
           kernel_regularizer=l2(0.0002)))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(PoolHelper())
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='valid'))
model.add(LRN())

model.add(
    Conv2D(64, (1, 1),
           padding='same',
           activation='relu',
           kernel_regularizer=l2(0.0002)))
model.add(
    Conv2D(192, (3, 3),
           padding='same',
           activation='relu',
           kernel_regularizer=l2(0.0002)))
model.add(LRN())
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(PoolHelper())
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='valid'))
예제 #7
0
def googlenet(input_shape=(224, 224, 3), classes=7, weights_path=None):
    # shape, classes 수정

    input = Input(input_shape)

    input_pad = ZeroPadding2D(padding=(3, 3))(input)
    conv1_7x7_s2 = Conv2D(64, (7, 7),
                          strides=(2, 2),
                          padding='valid',
                          activation='relu',
                          name='conv1/7x7_s2',
                          kernel_regularizer=l2(0.0002))(input_pad)
    conv1_zero_pad = ZeroPadding2D(padding=(1, 1))(conv1_7x7_s2)
    pool1_helper = PoolHelper()(
        conv1_zero_pad)  # 짝수를 하나씩 지움으로써 홀수차원으로 만든다. 직접 만든함수.
    pool1_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                strides=(2, 2),
                                padding='valid',
                                name='pool1/3x3_s2')(pool1_helper)
    pool1_norm1 = LRN(name='pool1/norm1')(pool1_3x3_s2)

    conv2_3x3_reduce = Conv2D(64, (1, 1),
                              padding='same',
                              activation='relu',
                              name='conv2/3x3_reduce',
                              kernel_regularizer=l2(0.0002))(pool1_norm1)
    conv2_3x3 = Conv2D(192, (3, 3),
                       padding='same',
                       activation='relu',
                       name='conv2/3x3',
                       kernel_regularizer=l2(0.0002))(conv2_3x3_reduce)
    conv2_norm2 = LRN(name='conv2/norm2')(conv2_3x3)
    conv2_zero_pad = ZeroPadding2D(padding=(1, 1))(conv2_norm2)
    pool2_helper = PoolHelper()(conv2_zero_pad)
    pool2_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                strides=(2, 2),
                                padding='valid',
                                name='pool2/3x3_s2')(pool2_helper)

    inception_3a_1x1 = Conv2D(64, (1, 1),
                              padding='same',
                              activation='relu',
                              name='inception_3a/1x1',
                              kernel_regularizer=l2(0.0002))(pool2_3x3_s2)
    inception_3a_3x3_reduce = Conv2D(
        96, (1, 1),
        padding='same',
        activation='relu',
        name='inception_3a/3x3_reduce',
        kernel_regularizer=l2(0.0002))(pool2_3x3_s2)
    inception_3a_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_3a_3x3_reduce)
    inception_3a_3x3 = Conv2D(
        128, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_3a/3x3',
        kernel_regularizer=l2(0.0002))(inception_3a_3x3_pad)
    inception_3a_5x5_reduce = Conv2D(
        16, (1, 1),
        padding='same',
        activation='relu',
        name='inception_3a/5x5_reduce',
        kernel_regularizer=l2(0.0002))(pool2_3x3_s2)
    inception_3a_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_3a_5x5_reduce)
    inception_3a_5x5 = Conv2D(
        32, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_3a/5x5',
        kernel_regularizer=l2(0.0002))(inception_3a_5x5_pad)
    inception_3a_pool = MaxPooling2D(pool_size=(3, 3),
                                     strides=(1, 1),
                                     padding='same',
                                     name='inception_3a/pool')(pool2_3x3_s2)
    inception_3a_pool_proj = Conv2D(
        32, (1, 1),
        padding='same',
        activation='relu',
        name='inception_3a/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_3a_pool)
    inception_3a_output = Concatenate(axis=-1, name='inception_3a/output')([
        inception_3a_1x1, inception_3a_3x3, inception_3a_5x5,
        inception_3a_pool_proj
    ])
    # Concatenate axis 수정.

    inception_3b_1x1 = Conv2D(
        128, (1, 1),
        padding='same',
        activation='relu',
        name='inception_3b/1x1',
        kernel_regularizer=l2(0.0002))(inception_3a_output)
    inception_3b_3x3_reduce = Conv2D(
        128, (1, 1),
        padding='same',
        activation='relu',
        name='inception_3b/3x3_reduce',
        kernel_regularizer=l2(0.0002))(inception_3a_output)
    inception_3b_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_3b_3x3_reduce)
    inception_3b_3x3 = Conv2D(
        192, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_3b/3x3',
        kernel_regularizer=l2(0.0002))(inception_3b_3x3_pad)
    inception_3b_5x5_reduce = Conv2D(
        32, (1, 1),
        padding='same',
        activation='relu',
        name='inception_3b/5x5_reduce',
        kernel_regularizer=l2(0.0002))(inception_3a_output)
    inception_3b_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_3b_5x5_reduce)
    inception_3b_5x5 = Conv2D(
        96, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_3b/5x5',
        kernel_regularizer=l2(0.0002))(inception_3b_5x5_pad)
    inception_3b_pool = MaxPooling2D(
        pool_size=(3, 3),
        strides=(1, 1),
        padding='same',
        name='inception_3b/pool')(inception_3a_output)
    inception_3b_pool_proj = Conv2D(
        64, (1, 1),
        padding='same',
        activation='relu',
        name='inception_3b/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_3b_pool)
    inception_3b_output = Concatenate(axis=-1, name='inception_3b/output')([
        inception_3b_1x1, inception_3b_3x3, inception_3b_5x5,
        inception_3b_pool_proj
    ])

    inception_3b_output_zero_pad = ZeroPadding2D(
        padding=(1, 1))(inception_3b_output)
    pool3_helper = PoolHelper()(inception_3b_output_zero_pad)
    pool3_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                strides=(2, 2),
                                padding='valid',
                                name='pool3/3x3_s2')(pool3_helper)

    inception_4a_1x1 = Conv2D(192, (1, 1),
                              padding='same',
                              activation='relu',
                              name='inception_4a/1x1',
                              kernel_regularizer=l2(0.0002))(pool3_3x3_s2)
    inception_4a_3x3_reduce = Conv2D(
        96, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4a/3x3_reduce',
        kernel_regularizer=l2(0.0002))(pool3_3x3_s2)
    inception_4a_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_4a_3x3_reduce)
    inception_4a_3x3 = Conv2D(
        208, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_4a/3x3',
        kernel_regularizer=l2(0.0002))(inception_4a_3x3_pad)
    inception_4a_5x5_reduce = Conv2D(
        16, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4a/5x5_reduce',
        kernel_regularizer=l2(0.0002))(pool3_3x3_s2)
    inception_4a_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_4a_5x5_reduce)
    inception_4a_5x5 = Conv2D(
        48, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_4a/5x5',
        kernel_regularizer=l2(0.0002))(inception_4a_5x5_pad)
    inception_4a_pool = MaxPooling2D(pool_size=(3, 3),
                                     strides=(1, 1),
                                     padding='same',
                                     name='inception_4a/pool')(pool3_3x3_s2)
    inception_4a_pool_proj = Conv2D(
        64, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4a/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_4a_pool)
    inception_4a_output = Concatenate(axis=-1, name='inception_4a/output')([
        inception_4a_1x1, inception_4a_3x3, inception_4a_5x5,
        inception_4a_pool_proj
    ])

    loss1_ave_pool = AveragePooling2D(
        pool_size=(5, 5), strides=(3, 3),
        name='loss1/ave_pool')(inception_4a_output)
    loss1_conv = Conv2D(128, (1, 1),
                        padding='same',
                        activation='relu',
                        name='loss1/conv',
                        kernel_regularizer=l2(0.0002))(loss1_ave_pool)
    loss1_flat = Flatten()(loss1_conv)
    loss1_fc = Dense(1024,
                     activation='relu',
                     name='loss1/fc',
                     kernel_regularizer=l2(0.0002))(loss1_flat)
    loss1_drop_fc = Dropout(rate=0.7)(loss1_fc)
    loss1_classifier = Dense(classes,
                             name='loss1/classifier',
                             kernel_regularizer=l2(0.0002))(loss1_drop_fc)
    loss1_classifier_act = Activation('softmax')(loss1_classifier)

    inception_4b_1x1 = Conv2D(
        160, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4b/1x1',
        kernel_regularizer=l2(0.0002))(inception_4a_output)
    inception_4b_3x3_reduce = Conv2D(
        112, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4b/3x3_reduce',
        kernel_regularizer=l2(0.0002))(inception_4a_output)
    inception_4b_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_4b_3x3_reduce)
    inception_4b_3x3 = Conv2D(
        224, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_4b/3x3',
        kernel_regularizer=l2(0.0002))(inception_4b_3x3_pad)
    inception_4b_5x5_reduce = Conv2D(
        24, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4b/5x5_reduce',
        kernel_regularizer=l2(0.0002))(inception_4a_output)
    inception_4b_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_4b_5x5_reduce)
    inception_4b_5x5 = Conv2D(
        64, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_4b/5x5',
        kernel_regularizer=l2(0.0002))(inception_4b_5x5_pad)
    inception_4b_pool = MaxPooling2D(
        pool_size=(3, 3),
        strides=(1, 1),
        padding='same',
        name='inception_4b/pool')(inception_4a_output)
    inception_4b_pool_proj = Conv2D(
        64, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4b/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_4b_pool)
    inception_4b_output = Concatenate(axis=-1, name='inception_4b/output')([
        inception_4b_1x1, inception_4b_3x3, inception_4b_5x5,
        inception_4b_pool_proj
    ])

    inception_4c_1x1 = Conv2D(
        128, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4c/1x1',
        kernel_regularizer=l2(0.0002))(inception_4b_output)
    inception_4c_3x3_reduce = Conv2D(
        128, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4c/3x3_reduce',
        kernel_regularizer=l2(0.0002))(inception_4b_output)
    inception_4c_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_4c_3x3_reduce)
    inception_4c_3x3 = Conv2D(
        256, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_4c/3x3',
        kernel_regularizer=l2(0.0002))(inception_4c_3x3_pad)
    inception_4c_5x5_reduce = Conv2D(
        24, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4c/5x5_reduce',
        kernel_regularizer=l2(0.0002))(inception_4b_output)
    inception_4c_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_4c_5x5_reduce)
    inception_4c_5x5 = Conv2D(
        64, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_4c/5x5',
        kernel_regularizer=l2(0.0002))(inception_4c_5x5_pad)
    inception_4c_pool = MaxPooling2D(
        pool_size=(3, 3),
        strides=(1, 1),
        padding='same',
        name='inception_4c/pool')(inception_4b_output)
    inception_4c_pool_proj = Conv2D(
        64, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4c/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_4c_pool)
    inception_4c_output = Concatenate(axis=-1, name='inception_4c/output')([
        inception_4c_1x1, inception_4c_3x3, inception_4c_5x5,
        inception_4c_pool_proj
    ])

    inception_4d_1x1 = Conv2D(
        112, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4d/1x1',
        kernel_regularizer=l2(0.0002))(inception_4c_output)
    inception_4d_3x3_reduce = Conv2D(
        144, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4d/3x3_reduce',
        kernel_regularizer=l2(0.0002))(inception_4c_output)
    inception_4d_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_4d_3x3_reduce)
    inception_4d_3x3 = Conv2D(
        288, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_4d/3x3',
        kernel_regularizer=l2(0.0002))(inception_4d_3x3_pad)
    inception_4d_5x5_reduce = Conv2D(
        32, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4d/5x5_reduce',
        kernel_regularizer=l2(0.0002))(inception_4c_output)
    inception_4d_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_4d_5x5_reduce)
    inception_4d_5x5 = Conv2D(
        64, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_4d/5x5',
        kernel_regularizer=l2(0.0002))(inception_4d_5x5_pad)
    inception_4d_pool = MaxPooling2D(
        pool_size=(3, 3),
        strides=(1, 1),
        padding='same',
        name='inception_4d/pool')(inception_4c_output)
    inception_4d_pool_proj = Conv2D(
        64, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4d/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_4d_pool)
    inception_4d_output = Concatenate(axis=-1, name='inception_4d/output')([
        inception_4d_1x1, inception_4d_3x3, inception_4d_5x5,
        inception_4d_pool_proj
    ])

    loss2_ave_pool = AveragePooling2D(
        pool_size=(5, 5), strides=(3, 3),
        name='loss2/ave_pool')(inception_4d_output)
    loss2_conv = Conv2D(128, (1, 1),
                        padding='same',
                        activation='relu',
                        name='loss2/conv',
                        kernel_regularizer=l2(0.0002))(loss2_ave_pool)
    loss2_flat = Flatten()(loss2_conv)
    loss2_fc = Dense(1024,
                     activation='relu',
                     name='loss2/fc',
                     kernel_regularizer=l2(0.0002))(loss2_flat)
    loss2_drop_fc = Dropout(rate=0.7)(loss2_fc)
    loss2_classifier = Dense(classes,
                             name='loss2/classifier',
                             kernel_regularizer=l2(0.0002))(loss2_drop_fc)
    loss2_classifier_act = Activation('softmax')(loss2_classifier)

    inception_4e_1x1 = Conv2D(
        256, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4e/1x1',
        kernel_regularizer=l2(0.0002))(inception_4d_output)
    inception_4e_3x3_reduce = Conv2D(
        160, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4e/3x3_reduce',
        kernel_regularizer=l2(0.0002))(inception_4d_output)
    inception_4e_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_4e_3x3_reduce)
    inception_4e_3x3 = Conv2D(
        320, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_4e/3x3',
        kernel_regularizer=l2(0.0002))(inception_4e_3x3_pad)
    inception_4e_5x5_reduce = Conv2D(
        32, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4e/5x5_reduce',
        kernel_regularizer=l2(0.0002))(inception_4d_output)
    inception_4e_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_4e_5x5_reduce)
    inception_4e_5x5 = Conv2D(
        128, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_4e/5x5',
        kernel_regularizer=l2(0.0002))(inception_4e_5x5_pad)
    inception_4e_pool = MaxPooling2D(
        pool_size=(3, 3),
        strides=(1, 1),
        padding='same',
        name='inception_4e/pool')(inception_4d_output)
    inception_4e_pool_proj = Conv2D(
        128, (1, 1),
        padding='same',
        activation='relu',
        name='inception_4e/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_4e_pool)
    inception_4e_output = Concatenate(axis=-1, name='inception_4e/output')([
        inception_4e_1x1, inception_4e_3x3, inception_4e_5x5,
        inception_4e_pool_proj
    ])

    inception_4e_output_zero_pad = ZeroPadding2D(
        padding=(1, 1))(inception_4e_output)
    pool4_helper = PoolHelper()(inception_4e_output_zero_pad)
    pool4_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                strides=(2, 2),
                                padding='valid',
                                name='pool4/3x3_s2')(pool4_helper)

    inception_5a_1x1 = Conv2D(256, (1, 1),
                              padding='same',
                              activation='relu',
                              name='inception_5a/1x1',
                              kernel_regularizer=l2(0.0002))(pool4_3x3_s2)
    inception_5a_3x3_reduce = Conv2D(
        160, (1, 1),
        padding='same',
        activation='relu',
        name='inception_5a/3x3_reduce',
        kernel_regularizer=l2(0.0002))(pool4_3x3_s2)
    inception_5a_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_5a_3x3_reduce)
    inception_5a_3x3 = Conv2D(
        320, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_5a/3x3',
        kernel_regularizer=l2(0.0002))(inception_5a_3x3_pad)
    inception_5a_5x5_reduce = Conv2D(
        32, (1, 1),
        padding='same',
        activation='relu',
        name='inception_5a/5x5_reduce',
        kernel_regularizer=l2(0.0002))(pool4_3x3_s2)
    inception_5a_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_5a_5x5_reduce)
    inception_5a_5x5 = Conv2D(
        128, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_5a/5x5',
        kernel_regularizer=l2(0.0002))(inception_5a_5x5_pad)
    inception_5a_pool = MaxPooling2D(pool_size=(3, 3),
                                     strides=(1, 1),
                                     padding='same',
                                     name='inception_5a/pool')(pool4_3x3_s2)
    inception_5a_pool_proj = Conv2D(
        128, (1, 1),
        padding='same',
        activation='relu',
        name='inception_5a/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_5a_pool)
    inception_5a_output = Concatenate(axis=-1, name='inception_5a/output')([
        inception_5a_1x1, inception_5a_3x3, inception_5a_5x5,
        inception_5a_pool_proj
    ])

    inception_5b_1x1 = Conv2D(
        384, (1, 1),
        padding='same',
        activation='relu',
        name='inception_5b/1x1',
        kernel_regularizer=l2(0.0002))(inception_5a_output)
    inception_5b_3x3_reduce = Conv2D(
        192, (1, 1),
        padding='same',
        activation='relu',
        name='inception_5b/3x3_reduce',
        kernel_regularizer=l2(0.0002))(inception_5a_output)
    inception_5b_3x3_pad = ZeroPadding2D(padding=(1,
                                                  1))(inception_5b_3x3_reduce)
    inception_5b_3x3 = Conv2D(
        384, (3, 3),
        padding='valid',
        activation='relu',
        name='inception_5b/3x3',
        kernel_regularizer=l2(0.0002))(inception_5b_3x3_pad)
    inception_5b_5x5_reduce = Conv2D(
        48, (1, 1),
        padding='same',
        activation='relu',
        name='inception_5b/5x5_reduce',
        kernel_regularizer=l2(0.0002))(inception_5a_output)
    inception_5b_5x5_pad = ZeroPadding2D(padding=(2,
                                                  2))(inception_5b_5x5_reduce)
    inception_5b_5x5 = Conv2D(
        128, (5, 5),
        padding='valid',
        activation='relu',
        name='inception_5b/5x5',
        kernel_regularizer=l2(0.0002))(inception_5b_5x5_pad)
    inception_5b_pool = MaxPooling2D(
        pool_size=(3, 3),
        strides=(1, 1),
        padding='same',
        name='inception_5b/pool')(inception_5a_output)
    inception_5b_pool_proj = Conv2D(
        128, (1, 1),
        padding='same',
        activation='relu',
        name='inception_5b/pool_proj',
        kernel_regularizer=l2(0.0002))(inception_5b_pool)
    inception_5b_output = Concatenate(axis=-1, name='inception_5b/output')([
        inception_5b_1x1, inception_5b_3x3, inception_5b_5x5,
        inception_5b_pool_proj
    ])

    pool5_7x7_s1 = AveragePooling2D(pool_size=(7, 7),
                                    strides=(1, 1),
                                    name='pool5/7x7_s2')(inception_5b_output)
    loss3_flat = Flatten()(pool5_7x7_s1)
    pool5_drop_7x7_s1 = Dropout(rate=0.4)(loss3_flat)
    loss3_classifier = Dense(classes,
                             name='loss3/classifier',
                             kernel_regularizer=l2(0.0002))(pool5_drop_7x7_s1)
    loss3_classifier_act = Activation('softmax', name='prob')(loss3_classifier)

    googlenet = Model(inputs=input,
                      outputs=[
                          loss1_classifier_act, loss2_classifier_act,
                          loss3_classifier_act
                      ])

    if weights_path:
        googlenet.load_weights(weights_path)

    # if tf.keras.backend.backend() == 'tensorflow':
    #     # 우리는 tf.keras를 쓰므로, 이상황은 늘 True.
    #     # 또한, 아래의 코드도 시행할 필요가 없다.
    #     # 혹시모를 , 나중의 상황에 대비해 코드만 남겨놓는다.
    #
    #     ops = []
    #     for layer in googlenet.layers:
    #         if layer.__class__.__name__ == 'Conv2D': # layer의 class의 이름이 'conv2d'이면 ~
    #             original_w = K.get_value(layer.kernel)
    #             converted_w = convert_kernel(original_w)
    #             ops.append(tf.assign(layer.kernel, converted_w).op)
    #     K.get_session().run(ops)

    return googlenet
예제 #8
0
def create_cnn_model(weights_path=None):
    # creates our cnn model
    #filters which total weights is “n*m*k*l” (Here the input has l=32 feature maps as inputs, k=64 feature maps as outputs)
    #Then there is a term called bias for each feature map. So, the total number of parameters are “(n*m*l+1)*k”.
    '''
    PARAMETERS
    https://towardsdatascience.com/understanding-and-calculating-the-number-of-parameters-in-convolution-neural-networks-cnns-fc88790d530d
    https://medium.com/@shashikachamod4u/calculate-output-size-and-number-of-trainable-parameters-in-a-convolution-layer-1d64cae6c009
    https://medium.com/@iamvarman/how-to-calculate-the-number-of-parameters-in-the-cnn-5bd55364d7ca
    https://cs231n.github.io/convolutional-networks/
    '''

    input = Input(shape=(1, IMG_WIDTH, IMG_HEIGHT))
    input_pad = ZeroPadding2D(padding=(3, 3))(input)

    conv1_1_3x3_s1 = Conv2D(32, (3, 3),
                            strides=(1, 1),
                            padding='same',
                            activation='relu',
                            name='conv1_1/3x3_s1',
                            kernel_regularizer=l2(l2_regulizer))(input_pad)
    conv1_2_3x3_s1 = Conv2D(
        32, (3, 3),
        strides=(1, 1),
        padding='same',
        activation='relu',
        name='conv1_2/3x3_s1',
        kernel_regularizer=l2(l2_regulizer))(conv1_1_3x3_s1)
    conv1_zero_pad = ZeroPadding2D(padding=(1, 1))(conv1_2_3x3_s1)
    pool1_helper = PoolHelper()(conv1_zero_pad)
    pool1_2_2x2_s1 = MaxPooling2D(pool_size=(2, 2),
                                  strides=(1, 1),
                                  padding='same',
                                  name='pool1/2x2_s1')(pool1_helper)
    pool1_norm1 = LRN(name='pool1/norm1')(pool1_2_2x2_s1)

    conv2_1_3x3_reduce = Conv2D(
        64, (1, 1),
        padding='same',
        activation='relu',
        name='conv2_1/3x3_reduce',
        kernel_regularizer=l2(l2_regulizer))(pool1_norm1)
    conv2_2_3x3 = Conv2D(
        64, (3, 3),
        padding='same',
        activation='relu',
        name='conv2_2/3x3',
        kernel_regularizer=l2(l2_regulizer))(conv2_1_3x3_reduce)
    conv2_norm2 = LRN(name='conv2/norm2')(conv2_2_3x3)
    conv2_zero_pad = ZeroPadding2D(padding=(1, 1))(conv2_norm2)
    pool2_helper = PoolHelper()(conv2_zero_pad)
    pool2_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                strides=(2, 2),
                                padding='same',
                                name='pool2/3x3_s2')(pool2_helper)

    conv3_1_3x3_s1 = Conv2D(128, (3, 3),
                            strides=(1, 1),
                            padding='same',
                            activation='relu',
                            name='conv3_1/3x3_s1',
                            kernel_regularizer=l2(l2_regulizer))(pool2_3x3_s2)
    conv3_2_3x3_s1 = Conv2D(
        128, (3, 3),
        strides=(1, 1),
        padding='same',
        activation='relu',
        name='conv3_2/3x3_s1',
        kernel_regularizer=l2(l2_regulizer))(conv3_1_3x3_s1)
    conv3_zero_pad = ZeroPadding2D(padding=(1, 1))(conv3_2_3x3_s1)
    pool3_helper = PoolHelper()(conv3_zero_pad)
    pool3_2_2x2_s1 = MaxPooling2D(pool_size=(2, 2),
                                  strides=(1, 1),
                                  padding='same',
                                  name='pool3/2x2_s1')(pool3_helper)
    pool3_norm1 = LRN(name='pool3/norm1')(pool3_2_2x2_s1)

    conv4_1_3x3_reduce = Conv2D(
        256, (1, 1),
        padding='same',
        activation='relu',
        name='conv4_1/3x3_reduce',
        kernel_regularizer=l2(l2_regulizer))(pool3_norm1)
    conv4_2_3x3 = Conv2D(
        256, (3, 3),
        padding='same',
        activation='relu',
        name='conv4_2/3x3',
        kernel_regularizer=l2(l2_regulizer))(conv4_1_3x3_reduce)
    conv4_norm2 = LRN(name='conv4/norm2')(conv4_2_3x3)
    conv4_zero_pad = ZeroPadding2D(padding=(1, 1))(conv4_norm2)
    pool4_helper = PoolHelper()(conv4_zero_pad)
    pool4_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                strides=(2, 2),
                                padding='same',
                                name='pool4/3x3_s2')(pool4_helper)

    conv5_1_3x3_s1 = Conv2D(512, (3, 3),
                            strides=(1, 1),
                            padding='same',
                            activation='relu',
                            name='conv5_1/3x3_s1',
                            kernel_regularizer=l2(l2_regulizer))(pool4_3x3_s2)
    conv5_2_3x3_s1 = Conv2D(
        512, (3, 3),
        strides=(1, 1),
        padding='same',
        activation='relu',
        name='conv5_2/3x3_s1',
        kernel_regularizer=l2(l2_regulizer))(conv5_1_3x3_s1)
    conv5_zero_pad = ZeroPadding2D(padding=(1, 1))(conv5_2_3x3_s1)
    pool5_helper = PoolHelper()(conv5_zero_pad)
    pool5_2_2x2_s1 = MaxPooling2D(pool_size=(2, 2),
                                  strides=(1, 1),
                                  padding='same',
                                  name='pool5/2x2_s1')(pool5_helper)
    pool5_norm1 = LRN(name='pool5/norm1')(pool5_2_2x2_s1)

    conv6_1_3x3_reduce = Conv2D(
        1024, (1, 1),
        padding='same',
        activation='relu',
        name='conv6_1/3x3_reduce',
        kernel_regularizer=l2(l2_regulizer))(pool5_norm1)
    conv6_2_3x3 = Conv2D(
        1024, (3, 3),
        padding='same',
        activation='relu',
        name='conv6_2/3x3',
        kernel_regularizer=l2(l2_regulizer))(conv6_1_3x3_reduce)
    conv6_norm2 = LRN(name='conv6/norm2')(conv6_2_3x3)
    conv6_zero_pad = ZeroPadding2D(padding=(1, 1))(conv6_norm2)
    pool6_helper = PoolHelper()(conv6_zero_pad)
    pool6_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                strides=(2, 2),
                                padding='same',
                                name='pool6/3x3_s2')(pool6_helper)

    pool7_2x2_s1 = AveragePooling2D(pool_size=(2, 2),
                                    strides=(1, 1),
                                    name='pool7/2x2_s1')(pool6_3x3_s2)

    loss_flat = Flatten()(pool7_2x2_s1)
    pool7_drop_2x2_s1 = Dropout(rate=0.5)(loss_flat)
    loss_classifier = Dense(
        num_classes,
        name='loss3/classifier',
        kernel_regularizer=l2(l2_regulizer))(pool7_drop_2x2_s1)
    loss_classifier_act = Activation('softmax', name='prob')(loss_classifier)

    mynet = Model(inputs=input, outputs=[loss_classifier_act])

    if weights_path:
        mynet.load_weights(weights_path)

    if keras.backend.backend() == 'tensorflow':
        # convert the convolutional kernels for tensorflow
        ops = []
        for layer in mynet.layers:
            if layer.__class__.__name__ == 'Conv2D':
                original_w = K.get_value(layer.kernel)
                converted_w = convert_kernel(original_w)
                ops.append(tf.assign(layer.kernel, converted_w).op)
        K.get_session().run(ops)

    return mynet
예제 #9
0
                ops.append(tf.assign(layer.kernel, converted_w).op)
        K.get_session().run(ops)

    return mynet


if __name__ == "__main__":

    filepath2 = filepath + aug
    filepath += aug + "_weights-{epoch:02d}-{loss:.4f}.hdf5"
    # Test pretrained model
    if checkpoint == 'n':
        model = create_cnn_model()  #'googlenet_weights.h5'

    elif checkpoint == 'model':
        custom_objects = {'PoolHelper': PoolHelper(), 'LRN': LRN()}
        filepath2 += "my_cnn_model.h5"
        model = tf.keras.models.load_model(filepath2,
                                           custom_objects=custom_objects)
    else:
        filepath2 += "my_cnn_weights.h5"
        model = create_cnn_model(filepath2)

    if optimizer == 'sgd':
        optim = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    else:
        optim = Adam(
            lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-8
        )  #epsilon=1e-08, for Keras; epsilon=1e-08 for tensorflow; epsilon=1e-8 for Tocrch or MxNet
    model.compile(
        optimizer=optim,