Exemple #1
0
def schedule(dataset, lr):
    if dataset == 'cifar10':
        steps = [82, 123]
        lrs = [lr, lr / 10, lr / 100]
    else:
        raise ValueError('Schedule not defined for dataset: %s' % dataset)
    return Step(steps, lrs)
Exemple #2
0
def schedule(dataset, lr):
    if dataset == 'mnist':
        steps = [20]
        lrs = [lr, lr / 10]
    else:
        raise ValueError('Schedule not defined for dataset: %s.' % dataset)
    return Step(steps, lrs)
Exemple #3
0
def schedule(dataset, lr):
    if dataset in ['cifar10', 'cifar100']:
        steps = [25 * i for i in range(1, 10)]
        lrs = [lr / 2**i for i in range(10)]
    elif dataset == 'svhn':
        steps = [20, 30]
        lrs = [lr, lr / 10, lr / 100]
    else:
        raise ValueError('Schedule not defined for dataset: %s' % dataset)
    return Step(steps, lrs)
Exemple #4
0
    model = DenseNet(10, img_dim, depth, nb_dense_block, growth_rate,
                     nb_filter, dropout_rate, weight_decay)

    opt = SGD(lr=learning_rate, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy',
                  optimizer=opt,
                  metrics=["accuracy"])

    offset = 0
    save_path = os.path.join('./log', 'densenet')
    callbacks = []
    callbacks.append(ModelCheckpoint(save_path + '.h5'))
    steps = [nb_epoch / 2 - offset, 3 * nb_epoch / 4 - offset]
    schedule = Step(steps,
                    [learning_rate, 0.1 * learning_rate, 0.01 * learning_rate],
                    verbose=1)
    callbacks.append(schedule)

    hist1 = model.fit(X_train,
                      Y_train,
                      batch_size=batch_size,
                      epochs=nb_epoch,
                      verbose=0,
                      validation_data=(X_test, Y_test),
                      callbacks=callbacks,
                      shuffle=True)

    with open('./log/cifar10_hist', 'wb') as file:
        pickle.dump(hist1.history, file)
    train_set, _, test_set = cifar10.load('data/cifar10',
                                          valid_ratio=0.,
                                          gcn=False,
                                          zca=False)
    train_set['data'] = preprocess_data(train_set['data'])
    test_set['data'] = preprocess_data(test_set['data'])
    print 'Data loaded.'

    # Train for 300 epochs as in the paper
    nb_epoch = 300
    offset = 0
    # Define callbacks for checkpointing and scheduling
    callbacks = []
    callbacks.append(ModelCheckpoint(save_path + '.h5'))
    steps = [nb_epoch / 2 - offset, 3 * nb_epoch / 4 - offset]
    schedule = Step(steps, [0.1, 0.01, 0.001], verbose=1)
    callbacks.append(schedule)
    # Custom meta checkpoint that saves training information at each epoch.
    callbacks.append(MetaCheckpoint(save_path + '.meta', schedule=schedule))

    if augmented:
        data_gen = ImageDataGenerator(horizontal_flip=True,
                                      width_shift_range=0.125,
                                      height_shift_range=0.125,
                                      fill_mode='constant')
        data_iter = data_gen.flow(train_set['data'],
                                  train_set['labels'],
                                  batch_size=64,
                                  shuffle=True)

        model.fit_generator(data_iter,
                                                  valid_ratio=1. / 6,
                                                  normalize=True)

# Load our model. This is a simple model taken from tensorflow's tutorial
model = simple_model()

# Use the adam optimizer, since we are using a schedule callback. The
# learning rate we define here doesn't really matter
adam = optimizers.Adam(lr=1e-4)

model.compile(optimizer=adam,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# Callback that implements learning rate schedule
schedule = Step([20], [1e-4, 1e-5])

history = model.fit(train_set['data'],
                    train_set['labels'],
                    batch_size=128,
                    nb_epoch=40,
                    validation_data=(valid_set['data'], valid_set['labels']),
                    callbacks=[schedule],
                    verbose=2,
                    shuffle=True)

(loss, acc) = model.evaluate(test_set['data'],
                             test_set['labels'],
                             batch_size=128)
print('Test set loss = %g, accuracy = %g' % (loss, acc))