Пример #1
0
def build_model(weight_path):
    #base_model = ResNet50(input_shape=(224,224,3), weights=None, include_top=False)
    #base_model.load_weights(weight_path, by_name=True)
    #X = base_model.output
    #predictions = Dense(10, activation='softmax')(X)
    model = ResNet18(Input((224, 224, 3)), classes=10)
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=["accuracy"])
    return model
Пример #2
0
    train_labels_categorical = keras.utils.to_categorical(train_labels,
                                                          num_classes=10)
    vali_labels_categorical = keras.utils.to_categorical(vali_labels,
                                                         num_classes=10)

    train_images_2d = train_images.values.reshape(train_images.shape[0], 28,
                                                  28, 1)
    vali_images_2d = vali_images.values.reshape(vali_images.shape[0], 28, 28,
                                                1)

    test_images_2d = X_test.values.reshape(X_test.shape[0], 28, 28, 1)

    shape, classes = (28, 28, 1), 10

    x = keras.layers.Input(shape)
    model = ResNet18(x, classes=classes)

    RMS = RMSprop(lr=0.0001)
    model.compile("adam", "categorical_crossentropy", ["accuracy"])
    # model.compile(optimizer=RMS, loss="binary_crossentropy",metrics=['accuracy'])

    # model = Sequential()
    # model.add(Conv2D(32, (3, 3),padding='same', activation='relu', input_shape=(28, 28, 1)))
    # model.add(Conv2D(32, (3, 3),padding='same', activation='relu'))
    # model.add(MaxPooling2D(pool_size=(2, 2)))
    # model.add(Dropout(0.25))
    #
    # model.add(Conv2D(64, (3, 3),padding='same', activation='relu'))
    # model.add(Conv2D(64, (3, 3),padding='same', activation='relu'))
    # model.add(MaxPooling2D(pool_size=(2, 2)))
    # model.add(Dropout(0.25))
Пример #3
0
def centernet(num_classes, backbone='resnet50', input_size=512, max_objects=100, score_threshold=0.1, nms=True):
    assert backbone in ['resnet18', 'resnet34', 'resnet50']
    output_size = input_size // 4
    image_input = Input(shape=(None, None, 3))
    hm_input = Input(shape=(output_size, output_size, num_classes))
    wh_input = Input(shape=(max_objects, 2))
    reg_input = Input(shape=(max_objects, 2))
    reg_mask_input = Input(shape=(max_objects,))
    index_input = Input(shape=(max_objects,))

    if backbone == 'resnet18':
        resnet = ResNet18(image_input, include_top=False, freeze_bn=True)
    elif backbone == 'resnet34':
        resnet = ResNet34(image_input, include_top=False, freeze_bn=True)
    else:
        resnet = ResNet50(image_input, include_top=False, freeze_bn=True)

    # C5 (b, 16, 16, 512)
    C2, C3, C4, C5 = resnet.outputs

    C5 = Dropout(rate=0.5)(C5)
    C4 = Dropout(rate=0.4)(C4)
    C3 = Dropout(rate=0.3)(C3)
    C2 = Dropout(rate=0.2)(C2)
    x = C5

    # decoder
    x = Conv2D(256, 1, padding='same', use_bias=False,
               kernel_initializer='he_normal',
               kernel_regularizer=l2(5e-4))(UpSampling2D()(x))
    x = BatchNormalization()(x)
    x = ReLU()(x)
    x = Concatenate()([C4, x])
    x = Conv2D(256, 3, padding='same', use_bias=False,
               kernel_initializer='he_normal',
               kernel_regularizer=l2(5e-4))(x)
    x = BatchNormalization()(x)
    # (b, 32, 32, 512)
    x = ReLU()(x)

    x = Conv2D(128, 1, padding='same', use_bias=False,
               kernel_initializer='he_normal',
               kernel_regularizer=l2(5e-4))(UpSampling2D()(x))
    x = BatchNormalization()(x)
    x = ReLU()(x)
    x = Concatenate()([C3, x])
    x = Conv2D(128, 3, padding='same', use_bias=False,
               kernel_initializer='he_normal',
               kernel_regularizer=l2(5e-4))(x)
    x = BatchNormalization()(x)
    # (b, 64, 64, 128)
    x = ReLU()(x)

    x = Conv2D(64, 1, padding='same', use_bias=False,
               kernel_initializer='he_normal',
               kernel_regularizer=l2(5e-4))(UpSampling2D()(x))
    x = BatchNormalization()(x)
    x = ReLU()(x)
    x = Concatenate()([C2, x])
    x = Conv2D(64, 3, padding='same', use_bias=False,
               kernel_initializer='he_normal',
               kernel_regularizer=l2(5e-4))(x)
    x = BatchNormalization()(x)
    # (b, 128, 128, 512)
    x = ReLU()(x)

    # hm header
    y1 = Conv2D(64, 3, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(x)
    y1 = BatchNormalization()(y1)
    y1 = ReLU()(y1)
    y1 = Conv2D(num_classes, 1, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4), activation='sigmoid')(y1)

    # wh header
    y2 = Conv2D(64, 3, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(x)
    y2 = BatchNormalization()(y2)
    y2 = ReLU()(y2)
    y2 = Conv2D(2, 1, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(y2)

    # reg header
    y3 = Conv2D(64, 3, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(x)
    y3 = BatchNormalization()(y3)
    y3 = ReLU()(y3)
    y3 = Conv2D(2, 1, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(y3)

    loss_ = Lambda(loss, name='centernet_loss')(
        [y1, y2, y3, hm_input, wh_input, reg_input, reg_mask_input, index_input])
    model = Model(inputs=[image_input, hm_input, wh_input, reg_input, reg_mask_input, index_input], outputs=[loss_])

    # detections = decode(y1, y2, y3)
    detections = Lambda(lambda x: decode(*x,
                                         max_objects=max_objects,
                                         score_threshold=score_threshold,
                                         nms=nms,
                                         num_classes=num_classes))([y1, y2, y3])
    prediction_model = Model(inputs=image_input, outputs=detections)
    debug_model = Model(inputs=image_input, outputs=[y1, y2, y3])
    return model, prediction_model, debug_model
Пример #4
0
# Model version

n = 34
assert n in (18, 34), 'N must be 18 or 34'

depth = n
version = 1

# model name, depth and version
model_type = 'ResNet%dv%d' % (depth, version)

# Load the cifar10 data

# Input image dimensions

# Normalize data

# If subtract pixel mean is enabled

# Convert class vectors to binary class matrices

def lr_schedule(epoch):
    pass

if n==18:
    model = ResNet18(input_shape=input_shape,depth=depth)
else:
    model = ResNet34(input_shape = input_shape,depth=depth)

model.compile(loss='categorical_crossentropy',optimizer=AdaBound())