Ejemplo n.º 1
0
def AlexNet(weights_path=None, heatmap=False):
    if heatmap:
        inputs = Input(shape=(3,None,None))
#        inputs = Input(shape=(None, None, 3))
    else:
        inputs = Input(shape=(3, 227, 227))
#        inputs = Input(shape=(227, 227, 3))

    conv_1 = Conv2D(96, 11, 11,strides=(4,4),activation='relu',
                           name='conv_1')(inputs)

    conv_2 = MaxPooling2D((3, 3), strides=(2,2))(conv_1)
    conv_2 = crosschannelnormalization(name="convpool_1")(conv_2)
    conv_2 = ZeroPadding2D((2,2))(conv_2)
    conv_2 = merge([
        Conv2D(128,5,5,activation="relu",name='conv_2_'+str(i+1))(
            splittensor(ratio_split=2,id_split=i)(conv_2)
        ) for i in range(2)], mode='concat',concat_axis=1,name="conv_2")

    conv_3 = MaxPooling2D((3, 3), strides=(2, 2))(conv_2)
    conv_3 = crosschannelnormalization()(conv_3)
    conv_3 = ZeroPadding2D((1,1))(conv_3)
    conv_3 = Conv2D(384,3,3,activation='relu',name='conv_3')(conv_3)

    conv_4 = ZeroPadding2D((1,1))(conv_3)
    conv_4 = merge([
        Conv2D(192,3,3,activation="relu",name='conv_4_'+str(i+1))(
            splittensor(ratio_split=2,id_split=i)(conv_4)
        ) for i in range(2)], mode='concat',concat_axis=1,name="conv_4")

    conv_5 = ZeroPadding2D((1,1))(conv_4)
    conv_5 = merge([
        Conv2D(128,3,3,activation="relu",name='conv_5_'+str(i+1))(
            splittensor(ratio_split=2,id_split=i)(conv_5)
        ) for i in range(2)], mode='concat',concat_axis=1,name="conv_5")

    dense_1 = MaxPooling2D((3, 3), strides=(2,2),name="convpool_5")(conv_5)

    if heatmap:
        dense_1 = Conv2D(4096,6,6,activation="relu",name="dense_1")(dense_1)
        dense_2 = Conv2D(4096,1,1,activation="relu",name="dense_2")(dense_1)
        dense_3 = Conv2D(1000, 1,1,name="dense_3")(dense_2)
        prediction = Softmax4D(axis=1,name="softmax")(dense_3)
    else:
        dense_1 = Flatten(name="flatten")(dense_1)
        dense_1 = Dense(4096, activation='relu',name='dense_1')(dense_1)
        dense_2 = Dropout(0.5)(dense_1)
        dense_2 = Dense(4096, activation='relu',name='dense_2')(dense_2)
        dense_3 = Dropout(0.5)(dense_2)
        dense_3 = Dense(1000,name='dense_3')(dense_3)
        prediction = Activation("softmax",name="softmax")(dense_3)


    model = Model(input=inputs, output=prediction)

    if weights_path:
        model.load_weights(weights_path)

    return model
Ejemplo n.º 2
0
def VGG_16(weights_path=None, heatmap=False):
    model = Sequential()
    if heatmap:
        model.add(ZeroPadding2D((1, 1), input_shape=(3, None, None)))
    else:
        model.add(ZeroPadding2D((1, 1), input_shape=(3, 224, 224)))
    model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    if heatmap:
        model.add(Convolution2D(4096, 7, 7, activation="relu", name="dense_1"))
        model.add(Convolution2D(4096, 1, 1, activation="relu", name="dense_2"))
        model.add(Convolution2D(1000, 1, 1, name="dense_3"))
        model.add(Softmax4D(axis=1, name="softmax"))
    else:
        model.add(Flatten(name="flatten"))
        model.add(Dense(4096, activation='relu', name='dense_1'))
        model.add(Dropout(0.5))
        model.add(Dense(4096, activation='relu', name='dense_2'))
        model.add(Dropout(0.5))
        model.add(Dense(1000, name='dense_3'))
        model.add(Activation("softmax", name="softmax"))

    if weights_path:
        model.load_weights(weights_path)
    return model
Ejemplo n.º 3
0
def AlexNet(weights_path=None, heatmap=False):
    if heatmap:
        inputs = Input(shape=(3, None, None))
    else:
        inputs = Input(shape=(3, 227, 227))

    conv_1 = Convolution2D(96, kernel_size=(11, 11), subsample=(4, 4), activation='relu', data_format='channels_first',
                           name='conv_1')(inputs)

    conv_2 = MaxPooling2D((3, 3), strides=(2, 2))(conv_1)
    conv_2 = crosschannelnormalization(name='convpool_1')(conv_2)
    conv_2 = ZeroPadding2D((2, 2))(conv_2)
    conv_2 = merge([
                       Convolution2D(128, kernel_size=(5, 5), activation='relu', data_format='channels_first', name='conv_2_' + str(i + 1))(
                           splittensor(ratio_split=2, id_split=i)(conv_2)
                       ) for i in range(2)], mode='concat', concat_axis=1, name='conv_2')

    conv_3 = MaxPooling2D((3, 3), strides=(2, 2))(conv_2)
    conv_3 = crosschannelnormalization()(conv_3)
    conv_3 = ZeroPadding2D((1, 1))(conv_3)
    conv_3 = Convolution2D(384, kernel_size=(3, 3), activation='relu', data_format='channels_first', name='conv_3')(conv_3)

    conv_4 = ZeroPadding2D((1, 1))(conv_3)
    conv_4 = merge([
                       Convolution2D(192, kernel_size=(3, 3), activation='relu', data_format='channels_first', name='conv_4_' + str(i + 1))(
                           splittensor(ratio_split=2, id_split=i)(conv_4)
                       ) for i in range(2)], mode='concat', concat_axis=1, name='conv_4')

    conv_5 = ZeroPadding2D((1, 1))(conv_4)
    conv_5 = merge([
                       Convolution2D(128, kernel_size=(3, 3), activation='relu', data_format='channels_first', name='conv_5_' + str(i + 1))(
                           splittensor(ratio_split=2, id_split=i)(conv_5)
                       ) for i in range(2)], mode='concat', concat_axis=1, name='conv_5')

    dense_1 = MaxPooling2D((3, 3), strides=(2, 2), name='convpool_5')(conv_5)

    if heatmap:
        dense_1 = Convolution2D(4096, kernel_size=(6, 6), activation='relu', data_format='channels_first', name='dense_1')(dense_1)
        dense_2 = Convolution2D(4096, kernel_size=(1, 1), activation='relu', data_format='channels_first', name='dense_2')(dense_1)
        dense_3 = Convolution2D(1000, kernel_size=(1, 1), data_format='channels_first', name='dense_3')(dense_2)
        prediction = Softmax4D(axis=1, name='softmax')(dense_3)
    else:
        dense_1 = Flatten(name='flatten')(dense_1)
        dense_1 = Dense(4096, activation='relu', name='dense_1')(dense_1)
        dense_2 = Dropout(0.5)(dense_1)
        dense_2 = Dense(4096, activation='relu', name='dense_2')(dense_2)
        dense_3 = Dropout(0.5)(dense_2)
        dense_3 = Dense(1000, name='dense_3')(dense_3)
        prediction = Activation('softmax', name='softmax')(dense_3)

    model = Model(input=inputs, output=prediction)

    if weights_path:
        model.load_weights(weights_path)

    return model
Ejemplo n.º 4
0
def AlexNet(outdim=1000, weights_path=None, heatmap=False, l1=0, l2=0, usemil=False, usemymil=False, k=1., usemysoftmil=False, softmink=1., softmaxk=1.,\
    sparsemil=False, sparsemill1=0., sparsemill2=0., saveact=False):
    l1factor = l1
    l2factor = l2
    if heatmap:
        inputs = Input(shape=(3, None, None))
    else:
        inputs = Input(shape=(3, 227, 227))

    conv_1 = Convolution2D(96,
                           11,
                           11,
                           subsample=(4, 4),
                           activation='relu',
                           W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                           name='conv_1')(inputs)

    conv_2 = MaxPooling2D((3, 3), strides=(2, 2))(conv_1)
    conv_2 = crosschannelnormalization(name="convpool_1")(conv_2)
    conv_2 = ZeroPadding2D((2, 2))(conv_2)
    conv_2 = merge([
        Convolution2D(128,
                      5,
                      5,
                      activation="relu",
                      name='conv_2_' + str(i + 1),
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor))(
                          splittensor(ratio_split=2, id_split=i)(conv_2))
        for i in range(2)
    ],
                   mode='concat',
                   concat_axis=1,
                   name="conv_2")

    conv_3 = MaxPooling2D((3, 3), strides=(2, 2))(conv_2)
    conv_3 = crosschannelnormalization()(conv_3)
    conv_3 = ZeroPadding2D((1, 1))(conv_3)
    conv_3 = Convolution2D(384,
                           3,
                           3,
                           activation='relu',
                           name='conv_3',
                           W_regularizer=l1l2(l1=l1factor,
                                              l2=l2factor))(conv_3)

    conv_4 = ZeroPadding2D((1, 1))(conv_3)
    conv_4 = merge([
        Convolution2D(192,
                      3,
                      3,
                      activation="relu",
                      name='conv_4_' + str(i + 1),
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor))(
                          splittensor(ratio_split=2, id_split=i)(conv_4))
        for i in range(2)
    ],
                   mode='concat',
                   concat_axis=1,
                   name="conv_4")

    conv_5 = ZeroPadding2D((1, 1))(conv_4)
    conv_5 = merge([
        Convolution2D(128,
                      3,
                      3,
                      activation="relu",
                      name='conv_5_' + str(i + 1),
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor))(
                          splittensor(ratio_split=2, id_split=i)(conv_5))
        for i in range(2)
    ],
                   mode='concat',
                   concat_axis=1,
                   name="conv_5")

    dense_1 = MaxPooling2D((3, 3), strides=(2, 2), name="convpool_5")(conv_5)

    if heatmap:
        dense_1 = Convolution2D(4096,
                                6,
                                6,
                                activation="relu",
                                name="dense_1",
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_1)
        dense_2 = Convolution2D(4096,
                                1,
                                1,
                                activation="relu",
                                name="dense_2",
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_1)
        dense_3 = Convolution2D(outdim,
                                1,
                                1,
                                name="dense_3",
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_2)
        prediction = Softmax4D(axis=1, name="softmax")(dense_3)
    elif usemil:
        dense_1 = Convolution2D(128,
                                1,
                                1,
                                activation='relu',
                                name='mil_1',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_1)
        dense_2 = Convolution2D(128,
                                1,
                                1,
                                activation='relu',
                                name='mil_2',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_1)
        dense_3 = Convolution2D(outdim,
                                1,
                                1,
                                name='mil_3',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_2)
        prediction_1 = Softmax4D(axis=1, name='softmax')(dense_3)
        #prediction = Flatten(name='flatten')(prediction_1)
        #dense_3 = Dense(outdim,name='dense_3',W_regularizer=l1l2(l1=l1factor, l2=l2factor))(prediction)
        #prediction = Activation("softmax",name="softmax2")(dense_3)

        prediction_1 = MaxPooling2D((6, 6), name='output')(prediction_1)
        prediction = Flatten(name='flatten')(prediction_1)
        prediction = Recalc(axis=1, name='Recalcmil')(prediction)
    elif usemymil:
        dense_1 = Convolution2D(128,
                                1,
                                1,
                                activation='relu',
                                name='mil_1',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_1)
        dense_2 = Convolution2D(128,
                                1,
                                1,
                                activation='relu',
                                name='mil_2',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_1)
        dense_3 = Convolution2D(1,
                                1,
                                1,
                                activation='sigmoid',
                                name='mil_3',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_2)
        #prediction_1 = Softmax4D(axis=1, name='softmax')(dense_3)
        #prediction = ExtractDim(axis=1, name='extract')(prediction_1)
        prediction = Flatten(name='flatten')(dense_3)
        prediction = ReRank(k=k, label=1, name='output')(prediction)
    elif usemysoftmil:
        dense_1 = Convolution2D(128,
                                1,
                                1,
                                activation='relu',
                                name='mil_1',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_1)
        dense_2 = Convolution2D(128,
                                1,
                                1,
                                activation='relu',
                                name='mil_2',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_1)
        dense_3 = Convolution2D(1,
                                1,
                                1,
                                activation='sigmoid',
                                name='mil_3',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_2)
        #prediction_1 = Softmax4D(axis=1, name='softmax')(dense_3)
        #prediction = ExtractDim(axis=1, name='extract')(prediction_1)
        prediction = Flatten(name='flatten')(dense_3)
        prediction = SoftReRank(softmink=softmink,
                                softmaxk=softmaxk,
                                label=1,
                                name='output')(prediction)
    elif sparsemil:
        dense_1 = Convolution2D(128,
                                1,
                                1,
                                activation='relu',
                                name='mil_1',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_1)
        dense_2 = Convolution2D(128,
                                1,
                                1,
                                activation='relu',
                                name='mil_2',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_1)
        prediction_1 = Convolution2D(1,1,1,activation='sigmoid',name='mil_3',W_regularizer=l1l2(l1=l1factor, l2=l2factor),\
            activity_regularizer=activity_l1l2(l1=sparsemill1, l2=sparsemill2))(dense_2)
        #        prediction_1 = Softmax4D(axis=1, name='softmax')(prediction_1)
        #dense_3 = Convolution2D(outdim,1,1,name='mil_3',W_regularizer=l1l2(l1=l1factor, l2=l2factor))(dense_2)
        #prediction_1 = Softmax4D(axis=1, name='softmax')(dense_3)
        #prediction_1 = ActivityRegularizerOneDim(l1=sparsemill1, l2=sparsemill2)(prediction_1)
        #prediction = MaxPooling2D((6,6), name='output')(prediction_1)
        #        prediction_1 = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same', name='smooth', \
        #            W_regularizer=l1l2(l1=l1factor, l2=l2factor), activity_regularizer=activity_l1l2(l1=sparsemill1, l2=sparsemill2))(prediction_1)
        prediction = Flatten(name='flatten')(prediction_1)
        if saveact:
            model = Model(input=inputs, output=prediction)
            return model
        prediction = RecalcExpand(axis=1, name='Recalcmil')(prediction)
    else:
        dense_1 = Flatten(name="flatten")(dense_1)
        dense_1 = Dense(4096,
                        activation='relu',
                        name='dense_1',
                        W_regularizer=l1l2(l1=l1factor, l2=l2factor))(dense_1)
        dense_2 = Dropout(0.5)(dense_1)
        dense_2 = Dense(4096,
                        activation='relu',
                        name='dense_2',
                        W_regularizer=l1l2(l1=l1factor, l2=l2factor))(dense_2)
        dense_3 = Dropout(0.5)(dense_2)
        dense_3 = Dense(outdim,
                        name='dense_3',
                        W_regularizer=l1l2(l1=l1factor, l2=l2factor))(dense_3)
        prediction = Activation("softmax", name="softmax")(dense_3)

    model = Model(input=inputs, output=prediction)

    if weights_path:
        model.load_weights(weights_path)

    return model
Ejemplo n.º 5
0
def VGG_19(outdim=1000,
           weights_path=None,
           heatmap=False,
           l1=0,
           l2=0,
           usemil=False,
           usemymil=False,
           k=1.):
    model = Sequential()

    if heatmap:
        model.add(ZeroPadding2D((1, 1), input_shape=(3, None, None)))
    else:
        model.add(ZeroPadding2D((1, 1), input_shape=(3, 224, 224)))
    model.add(
        Convolution2D(64,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv1_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(64,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv1_2'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(128,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv2_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(128,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv2_2'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(256,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv3_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(256,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv3_2'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(256,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv3_3'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(256,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv3_4'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv4_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv4_2'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv4_3'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv4_4'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv5_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv5_2'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv5_3'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(
        Convolution2D(512,
                      3,
                      3,
                      activation='relu',
                      W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                      name='conv5_4'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    if heatmap:
        model.add(
            Convolution2D(4096,
                          7,
                          7,
                          activation="relu",
                          W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                          name="dense_1"))
        model.add(
            Convolution2D(4096,
                          1,
                          1,
                          activation="relu",
                          W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                          name="dense_2"))
        model.add(
            Convolution2D(outdim,
                          1,
                          1,
                          W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                          name="dense_3"))
        model.add(Softmax4D(axis=1, name="softmax"))
    elif usemil:
        dense_1 = Convolution2D(128,
                                1,
                                1,
                                activation='relu',
                                name='mil_1',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_1)
        dense_2 = Convolution2D(128,
                                1,
                                1,
                                activation='relu',
                                name='mil_2',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_1)
        dense_3 = Convolution2D(outdim,
                                1,
                                1,
                                name='mil_3',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_2)
        prediction_1 = Softmax4D(axis=1, name='softmax')(dense_3)
        prediction = Flatten(name='flatten')(prediction_1)
        dense_3 = Dense(outdim,
                        name='dense_3',
                        W_regularizer=l1l2(l1=l1factor,
                                           l2=l2factor))(prediction)
        prediction = Activation("softmax", name="softmax2")(dense_3)
        #prediction = MaxPooling2D((6,6), name='output')(prediction_1)
        #prediction = Flatten(name='flatten')(prediction)
        prediction = RecalcExpand(axis=1, name='Recalcmil')(prediction)
    elif usemymil:
        dense_1 = Convolution2D(128,
                                1,
                                1,
                                activation='relu',
                                name='mil_1',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_1)
        dense_2 = Convolution2D(128,
                                1,
                                1,
                                activation='relu',
                                name='mil_2',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_1)
        dense_3 = Convolution2D(1,
                                1,
                                1,
                                activation='sigmoid',
                                name='mil_3',
                                W_regularizer=l1l2(l1=l1factor,
                                                   l2=l2factor))(dense_2)
        #prediction_1 = Softmax4D(axis=1, name='softmax')(dense_3)
        prediction = Flatten(name='flatten')(dense_3)
        prediction = ReRank(k=k, label=1, name='output')(prediction)
    else:
        model.add(Flatten())
        model.add(
            Dense(4096,
                  activation='relu',
                  W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                  name='dense_1'))
        model.add(Dropout(0.5))
        model.add(
            Dense(4096,
                  activation='relu',
                  W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                  name='dense_2'))
        model.add(Dropout(0.5))
        model.add(
            Dense(outdim,
                  W_regularizer=l1l2(l1=l1factor, l2=l2factor),
                  name='dense_3'))
        model.add(Activation("softmax"))

    if weights_path:
        model.load_weights(weights_path)

    return model
    def _emptySequential(layer_dim=[], heatmap=False):
        default_layer_dim = [
            1000, 4096, 4096, 512, 512, 512, 512, 512, 512, 256, 256, 256, 128,
            128, 64, 64
        ]
        layer_dim = layer_dim + default_layer_dim[len(layer_dim):]
        model = Sequential()

        if heatmap:
            model.add(ZeroPadding2D((1, 1), input_shape=(3, None, None)))
        else:
            model.add(ZeroPadding2D((1, 1), input_shape=(3, 224, 224)))

        model.add(
            Convolution2D(layer_dim[-1],
                          3,
                          3,
                          activation='relu',
                          name=VGG16Layers.conv1a))
        model.add(ZeroPadding2D((1, 1)))
        model.add(
            Convolution2D(layer_dim[-2],
                          3,
                          3,
                          activation='relu',
                          name=VGG16Layers.conv1b))  # H' = W' = 224
        model.add(MaxPooling2D((2, 2), strides=(2, 2), name=VGG16Layers.pool1))
        # W' = 112

        model.add(ZeroPadding2D((1, 1)))
        model.add(
            Convolution2D(layer_dim[-3],
                          3,
                          3,
                          activation='relu',
                          name=VGG16Layers.conv2a))  # W' = 112
        model.add(ZeroPadding2D((1, 1)))
        model.add(
            Convolution2D(layer_dim[-4],
                          3,
                          3,
                          activation='relu',
                          name=VGG16Layers.conv2b))  # W' = 112
        model.add(MaxPooling2D((2, 2), strides=(2, 2), name=VGG16Layers.pool2))
        # W' = 56

        model.add(ZeroPadding2D((1, 1)))
        model.add(
            Convolution2D(layer_dim[-5],
                          3,
                          3,
                          activation='relu',
                          name=VGG16Layers.conv3a))  # W' = 56
        model.add(ZeroPadding2D((1, 1)))
        model.add(
            Convolution2D(layer_dim[-6],
                          3,
                          3,
                          activation='relu',
                          name=VGG16Layers.conv3b))  # W' = 56
        model.add(ZeroPadding2D((1, 1)))
        model.add(
            Convolution2D(layer_dim[-7],
                          3,
                          3,
                          activation='relu',
                          name=VGG16Layers.conv3c))  # W' = 56
        model.add(MaxPooling2D((2, 2), strides=(2, 2), name=VGG16Layers.pool3))
        # W' = 28

        model.add(ZeroPadding2D((1, 1)))
        model.add(
            Convolution2D(layer_dim[-8],
                          3,
                          3,
                          activation='relu',
                          name=VGG16Layers.conv4a))  # W' = 28
        model.add(ZeroPadding2D((1, 1)))
        model.add(
            Convolution2D(layer_dim[-9],
                          3,
                          3,
                          activation='relu',
                          name=VGG16Layers.conv4b))  # W' = 28
        model.add(ZeroPadding2D((1, 1)))
        model.add(
            Convolution2D(layer_dim[-10],
                          3,
                          3,
                          activation='relu',
                          name=VGG16Layers.conv4c))  # W' = 28
        model.add(MaxPooling2D((2, 2), strides=(2, 2), name=VGG16Layers.pool4))
        # W' = 14

        model.add(ZeroPadding2D((1, 1)))
        model.add(
            Convolution2D(layer_dim[-11],
                          3,
                          3,
                          activation='relu',
                          name=VGG16Layers.conv5a))  # W' = 14
        model.add(ZeroPadding2D((1, 1)))
        model.add(
            Convolution2D(layer_dim[-12],
                          3,
                          3,
                          activation='relu',
                          name=VGG16Layers.conv5b))  # W' = 14
        model.add(ZeroPadding2D((1, 1)))
        model.add(
            Convolution2D(layer_dim[-13],
                          3,
                          3,
                          activation='relu',
                          name=VGG16Layers.conv5c))  # W' = 14
        model.add(MaxPooling2D((2, 2), strides=(2, 2), name=VGG16Layers.pool5))
        # W' = 7

        if heatmap:
            model.add(
                Convolution2D(layer_dim[-14],
                              7,
                              7,
                              activation="relu",
                              name=VGG16Layers.dense1))
            model.add(
                Convolution2D(layer_dim[-15],
                              1,
                              1,
                              activation="relu",
                              name=VGG16Layers.dense2))
            model.add(
                Convolution2D(layer_dim[-16], 1, 1, name=VGG16Layers.dense3))
            model.add(Softmax4D(axis=1, name=VGG16Layers.softmax))
        else:

            # in: 512x49x1
            model.add(Flatten(name="flatten"))
            # out: 25088
            model.add(
                Dense(layer_dim[-14],
                      activation='relu',
                      name=VGG16Layers.dense1))
            model.add(Dropout(0.5, name=VGG16Layers.drop1))
            # fcd = fullyconnected-dropout # Dropout is applied to Dropout input -> we are dropping out one of the 4096 neurons
            # out: 4096x1x1           (512x49x4096 weights)

            # in: 4096x1x1
            model.add(
                Dense(layer_dim[-15],
                      activation='relu',
                      name=VGG16Layers.dense2))
            model.add(Dropout(0.5, name=VGG16Layers.drop2))
            # Dropout is applied to Dropout input -> we are dropping out one of the 4096 neurons
            # out:  out: 4096x1x1      (4096x4096 weights ~=16M)

            # in: 4096x1x1
            model.add(Dense(layer_dim[-16], name=VGG16Layers.dense3))
            model.add(Activation("softmax", name=VGG16Layers.softmax))
            # out: 1000x1x1                 (1000x4096 weights ~= 4M)

        return model