Example #1
0
def load_teacher_model(model_path, training_data):
    model = build_cnn(training_data)
    model.load_weights(model_path)
    # remove softmax
    model.layers.pop()
    model = Model(model.input, model.layers[-1].output)
    # now model outputs logits
    return model
Example #2
0
    args = parser.parse_args()

    bin_dir = os.path.dirname(os.path.realpath(__file__)) + '/bin'
    if not os.path.exists(bin_dir):
        os.makedirs(bin_dir)

    training_data = load_data(args.file,
                              width=args.width,
                              height=args.height,
                              max_=args.max,
                              verbose=True)  # args.verbose)

    if args.model == 'cnn':
        label = '10cnn_%d_%d_%d' % (32, 128, args.epochs)
        model = build_cnn(training_data,
                          width=args.width,
                          height=args.height,
                          verbose=True)  # args.verbose)
        train(model, label, training_data, epochs=args.epochs)
    elif args.model == 'mlp':
        label = '10mlp_%d_%d' % (32, args.epochs)
        model = build_mlp(training_data,
                          width=args.width,
                          height=args.height,
                          verbose=True)  # args.verbose)
        train(model, label, training_data, epochs=args.epochs)
    elif args.model == 'student':
        model = build_mlp(training_data,
                          width=args.width,
                          height=args.height,
                          verbose=True)  # args.verbose)
        temp = 2.0
Example #3
0
# Split the data
(trainAttrX, testAttrX, trainImagesX,
 testImagesX) = train_test_split(df, images, test_size=0.25, random_state=42)

# Normalization
max_price = trainAttrX["price"].max()
trainY = trainAttrX["price"] / max_price
testY = testAttrX["price"] / max_price

(trainAttrX, testAttrX) = datasets.process_attributes(df, trainAttrX,
                                                      testAttrX)

# Create network
mlp = models.build_mlp(trainAttrX.shape[1], False)
cnn = models.build_cnn(64, 64, 3, regress=False)

concatenated = concatenate([mlp.output, cnn.output])

x = Dense(4, activation="relu")(concatenated)
x = Dense(1, activation="linear")(x)

model = Model(inputs=[mlp.input, cnn.input], outputs=x)

optimizer = Adam(lr=1e-3, decay=1e-3 / 200)
model.compile(loss="mean_absolute_percentage_error", optimizer=optimizer)

model.fit([trainAttrX, trainImagesX],
          trainY,
          validation_data=([testAttrX, testImagesX], testY),
          epochs=200,
Example #4
0
def exp( args ):
    '''
    alg     -- elu, qelu, relu, qrelu
    lrate   -- learning rate
    dropout -- whether to dropout
    batch_size -- number of samples in a mini batch
    epochs  -- how many epochs to train
    '''

    counter = Counter()
    callbacks = [ counter ]

    if args.arch == 'mlp':
        model = models.build_mlp( args, counter )

    elif args.arch == 'cnn':
        model = models.build_cnn( args, counter )

    elif args.arch == 'resnet':
        model = models.build_resnet( args, counter )

    elif args.arch == 'siamese':
        model = models.build_siamese( args, counter )

    elif args.arch == 'ae':
        model = models.build_ae( args, counter )

    else:
        raise RuntimeError( 'unknown architecture' )

    if args.arch == 'resnet':
        if args.alg[0] == 'n':
            optimizer = add_gradient_noise( keras.optimizers.Adam )( lr=lr_schedule(0), noise_eta=args.init_v, counter=counter )
        else:
            optimizer = keras.optimizers.Adam( lr=lr_schedule(0) )

        lr_scheduler = LearningRateScheduler( lr_schedule )
        lr_reducer = ReduceLROnPlateau( factor=np.sqrt(0.1),
                                         cooldown=0,
                                         patience=5,
                                         min_lr=0.5e-6 )
        callbacks += [ lr_reducer, lr_scheduler ]

    elif args.alg[0] == 'n':
        optimizer = add_gradient_noise( keras.optimizers.SGD )( lr=args.lrate, decay=1e-6, momentum=0.5, nesterov=False, noise_eta=args.init_v, counter=counter )

    else:
        optimizer = keras.optimizers.SGD( lr=args.lrate, decay=1e-6, momentum=0.5, nesterov=False )

    if args.arch == 'siamese':
        model.compile( loss=contrastive_loss, optimizer=optimizer, metrics=[siamese_accuracy] )

        tr_pairs, tr_y = data.train_generator
        te_pairs, te_y = data.test_generator

        history = model.fit( [tr_pairs[:, 0], tr_pairs[:, 1]], tr_y,
                             batch_size=args.batch_size,
                             epochs=args.epochs,
                             callbacks=callbacks,
                             validation_data=( [te_pairs[:, 0], te_pairs[:, 1]], te_y),
                             verbose=args.verbose )

        return history.history['loss'], history.history['val_siamese_accuracy']

    elif args.arch == 'ae':
        model.compile( loss=keras.losses.mse,
                       optimizer=optimizer )
        history = model.fit_generator(
                        data.train_generator,
                        steps_per_epoch=(data.x_train.shape[0]//args.batch_size+1),
                        epochs=args.epochs,
                        callbacks=callbacks,
                        validation_data=data.test_generator,
                        validation_steps=data.x_test.shape[0]//args.test_batch_size,
                        verbose=args.verbose )

        return history.history['loss'], history.history['val_loss']

    else:
        model.compile( loss=keras.losses.categorical_crossentropy,
                       optimizer=optimizer,
                       metrics=['accuracy'] )
        history = model.fit_generator(
                        data.train_generator,
                        steps_per_epoch=(data.x_train.shape[0]//args.batch_size+1),
                        epochs=args.epochs,
                        callbacks=callbacks,
                        validation_data=data.test_generator,
                        validation_steps=data.x_test.shape[0]//args.test_batch_size,
                        verbose=args.verbose )

        return history.history['loss'], history.history['val_acc']
Example #5
0
# MONITOR = 'loss'

TRAIN_DATA = './data/image.npy'

TRAIN_FOLDER = './model_chk/auto_en_' + ENCODER + utils.get_time_mark()
CHECK_POINT_NAME = TRAIN_FOLDER + '/model.{epoch:04d}-{' + MONITOR + ':.4f}.h5'
LOG_NAME = TRAIN_FOLDER + '/log.csv'

if not os.path.exists(TRAIN_FOLDER):
    os.makedirs(TRAIN_FOLDER)

train = np.load(TRAIN_DATA) / 255.
train = utils.shuffle(train)
if ENCODER == 'cnn':
    train = train.reshape(-1, 28, 28, 1)
    model = models.build_cnn()
else:
    model = models.build_dnn()

model.summary()

callback = [
    TensorBoard(TRAIN_FOLDER),
    CSVLogger(LOG_NAME, append=True),
    ReduceLROnPlateau(MONITOR,
                      factor=0.1,
                      patience=int(PATIENCE / 2),
                      verbose=1),
    EarlyStopping(MONITOR, patience=PATIENCE),
    ModelCheckpoint(CHECK_POINT_NAME, monitor=MONITOR, period=1)
]
from models import build_cnn
from train import fit_model
from dataset import load_dataset
from evaluate import evaluate_model
from options import opts

if __name__ == '__main__':

    # read dataset
    train_data, val_data, test_data, train_label, val_label, test_label = load_dataset(opts)
    
    # training
    if opts['run'] == 'train' or opts['run'] == 'both':
        # build model
        model = build_cnn(opts)
        # fit model
        fit_model(model, train_data, train_label, val_data, val_label, opts)
    
    # testing
    if opts['run'] == 'test' or opts['run'] == 'both':
        if opts['run'] == 'test':
            model = build_cnn(opts)
        # test
        evaluate_model(model, test_data, test_label, opts)