コード例 #1
0
def create_model():

    # Set up standard WideResNet-16-10 model.
    model = WideResidualNetwork(depth=16,
                                width=10,
                                weights=None,
                                input_shape=input_shape,
                                classes=num_classes,
                                dropout_rate=0.01)

    # WideResNet model that is included with Keras is optimized for inference.
    # Add L2 weight decay & adjust BN settings.
    model_config = model.get_config()
    for layer, layer_config in zip(model.layers, model_config['layers']):
        if hasattr(layer, 'kernel_regularizer'):
            regularizer = keras.regularizers.l2(args.wd)
            layer_config['config']['kernel_regularizer'] = \
                {'class_name': regularizer.__class__.__name__,
                 'config': regularizer.get_config()}
        if type(layer) == keras.layers.BatchNormalization:
            layer_config['config']['momentum'] = 0.9
            layer_config['config']['epsilon'] = 1e-5

    model = keras.models.Model.from_config(model_config)

    opt = keras.optimizers.SGD(lr=args.base_lr)

    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=opt,
                  metrics=['accuracy'])

    return model
コード例 #2
0
def resnet(input_shape, num_classes, wide=False):
    if wide:
        model = WideResidualNetwork(depth=28,
                                    width=8,
                                    dropout_rate=0.0,
                                    weights=None,
                                    input_shape=input_shape,
                                    classes=num_classes)
    else:
        model = ResNet18(input_shape=input_shape, classes=num_classes)

    model.summary()
    return model
コード例 #3
0
ファイル: train.py プロジェクト: darkreapyre/HaaS-GitOps
                                    featurewise_std_normalization=True)
test_gen.mean = train_gen.mean
test_gen.std = train_gen.std
test_iter = test_gen.flow(x_test, y_test, batch_size=args.val_batch_size)

# Restore from a previous checkpoint, if initial_epoch is specified.
# Horovod: restore on the first worker which will broadcast both model and optimizer weights
# to other workers.
if resume_from_epoch > 0 and hvd.rank() == 0:
    model = hvd.load_model(
        args.checkpoint_format.format(epoch=resume_from_epoch))
else:
    # Set up standard WideResNet-16-10 model.
    model = WideResidualNetwork(depth=16,
                                width=10,
                                weights=None,
                                input_shape=input_shape,
                                classes=num_classes,
                                dropout_rate=0.01)

    # WideResNet model that is included with Keras is optimized for inference.
    # Add L2 weight decay & adjust BN settings.
    model_config = model.get_config()
    for layer, layer_config in zip(model.layers, model_config['layers']):
        if hasattr(layer, 'kernel_regularizer'):
            regularizer = keras.regularizers.l2(args.wd)
            layer_config['config']['kernel_regularizer'] = \
                {'class_name': regularizer.__class__.__name__,
                 'config': regularizer.get_config()}
        if type(layer) == keras.layers.BatchNormalization:
            layer_config['config']['momentum'] = 0.9
            layer_config['config']['epsilon'] = 1e-5
コード例 #4
0
tempY = testY
trainY = kutils.to_categorical(trainY)
testY = kutils.to_categorical(testY)

generator = ImageDataGenerator(
    rotation_range=10,
    width_shift_range=5.0 / 32,
    height_shift_range=5.0 / 32,
    horizontal_flip=True,
)

generator.fit(trainX, seed=0, augment=True)

# We will be training the model, therefore no need to load weights
model = WideResidualNetwork(depth=28, width=8, dropout_rate=0.0, weights=None)

model.summary()

model.compile(loss="categorical_crossentropy",
              optimizer="adam",
              metrics=["acc"])
print("Finished compiling")
model_checkpoint = callbacks.ModelCheckpoint(
    "WRN-28-8 Weights.h5",
    monitor="val_acc",
    save_best_only=True,
    save_weights_only=True,
)
model.fit_generator(
    generator.flow(trainX, trainY, batch_size=batch_size),
コード例 #5
0
ファイル: cifar10_wide_resnet.py プロジェクト: rasi5050/keras
testX = testX.astype('float32')
testX /= 255.0

tempY = testY
trainY = kutils.to_categorical(trainY)
testY = kutils.to_categorical(testY)

generator = ImageDataGenerator(rotation_range=10,
                               width_shift_range=5. / 32,
                               height_shift_range=5. / 32,
                               horizontal_flip=True)

generator.fit(trainX, seed=0, augment=True)

# We will be training the model, therefore no need to load weights
model = WideResidualNetwork(depth=28, width=8, dropout_rate=0.0, weights=None)

model.summary()

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['acc'])
print('Finished compiling')

model.fit_generator(generator.flow(trainX, trainY, batch_size=batch_size),
                    steps_per_epoch=len(trainX) // batch_size,
                    epochs=epochs,
                    callbacks=[
                        callbacks.ModelCheckpoint('WRN-28-8 Weights.h5',
                                                  monitor='val_acc',
                                                  save_best_only=True,
コード例 #6
0
testX = testX.astype('float32')
testX /= 255.0

tempY = testY
trainY = kutils.to_categorical(trainY)
testY = kutils.to_categorical(testY)

generator = ImageDataGenerator(rotation_range=10,
                               width_shift_range=5. / 32,
                               height_shift_range=5. / 32,
                               horizontal_flip=True)

generator.fit(trainX, seed=0, augment=True)

# We will be training the model, therefore no need to load weights
model = WideResidualNetwork(depth=28, width=8, dropout_rate=0.0, weights=None)

model.summary()

model.compile(loss="categorical_crossentropy",
              optimizer="adam",
              metrics=["accuracy"])
print("Finished compiling")

model.fit_generator(
    generator.flow(trainX, trainY, batch_size=batch_size),
    samples_per_epoch=len(trainX),
    nb_epoch=nb_epoch,
    callbacks=[
        callbacks.ModelCheckpoint("WRN-28-8 Weights.h5",
                                  monitor="val_acc",