def make_smoothing(X_train, n_train, args): X_s = None X_s_path = os.path.join(args.data_path, 'X_s.npy') do_smoothing = True if os.path.isfile(X_s_path): print("\nLoading smoothed data ...") X_s = np.load(X_s_path) print("Checking augmented data ...") if len(X_s) == n_train: do_smoothing = False if do_smoothing: print("\nSmoothing data ...") X_m = X_train.mean(axis=0) X_train -= X_m with Stopwatch(verbose=True) as s: [U, s, Vh] = svd(X_train, full_matrices=False, compute_uv=True, overwrite_a=True, check_finite=False) s[-1000:] = 0. X_s = U.dot(np.diag(s).dot(Vh)) X_s += X_m # save to disk np.save(X_s_path, X_s) print("\n") return X_s
def make_augmentation(X_train, y_train, n_train, args): X_aug = None X_aug_path = os.path.join(args.data_path, 'X_aug.npy') y_train = y_train.tolist() * 10 RNG(seed=1337).shuffle(y_train) augment = True if os.path.isfile(X_aug_path): print("\nLoading augmented data ...") X_aug = np.load(X_aug_path) print("Checking augmented data ...") if len(X_aug) == 10 * n_train: augment = False if augment: print("\nAugmenting data ...") s = Stopwatch(verbose=True).start() X_aug = np.zeros((10 * n_train, 32, 32, 3), dtype=np.float32) X_train = im_unflatten(X_train) X_aug[:n_train] = X_train for i in xrange(n_train): for k, offset in enumerate(((1, 0), (-1, 0), (0, 1), (0, -1))): img = X_train[i].copy() X_aug[(k + 1) * n_train + i] = shift(img, offset=offset) for i in xrange(5 * n_train): X_aug[5 * n_train + i] = horizontal_mirror(X_aug[i].copy()) # shuffle once again RNG(seed=1337).shuffle(X_aug) # convert to 'uint8' type to save disk space X_aug *= 255. X_aug = X_aug.astype('uint8') # flatten to (10 * `n_train`, 3072) shape X_aug = im_flatten(X_aug) # save to disk np.save(X_aug_path, X_aug) s.elapsed() print("\n") return X_aug, y_train
def make_mlp(training_data, validation_data, test_data, W, args): (X_train, y_train) = training_data (X_val, y_val) = validation_data (X_test, y_test) = test_data (W, hb) = W dense_params = {} if W is not None and hb is not None: dense_params['weights'] = (W, hb) # define and initialize MLP model mlp = Sequential([ Dense(5000, input_shape=(3 * 32 * 32,), kernel_regularizer=regularizers.l2(args.mlp_l2), kernel_initializer=glorot_uniform(seed=3333), **dense_params), BN(), Activation('relu'), Dropout(args.mlp_dropout, seed=4444), Dense(10, kernel_initializer=glorot_uniform(seed=5555)), Activation('softmax'), ]) mlp.compile(optimizer=MultiAdam(lr=0.001, lr_multipliers={'dense_1': args.mlp_lrm[0], 'dense_2': args.mlp_lrm[1]}), loss='categorical_crossentropy', metrics=['accuracy']) # train and evaluate classifier with Stopwatch(verbose=True) as s: early_stopping = EarlyStopping(monitor=args.mlp_val_metric, patience=12, verbose=2) reduce_lr = ReduceLROnPlateau(monitor=args.mlp_val_metric, factor=0.2, verbose=2, patience=6, min_lr=1e-5) callbacks = [early_stopping, reduce_lr] try: mlp.fit(X_train, one_hot(y_train, n_classes=10), epochs=args.mlp_epochs, batch_size=args.mlp_batch_size, shuffle=False, validation_data=(X_val, one_hot(y_val, n_classes=10)), callbacks=callbacks) except KeyboardInterrupt: pass y_pred = mlp.predict(X_test) y_pred = unhot(one_hot_decision_function(y_pred), n_classes=10) print("Test accuracy: {:.4f}".format(accuracy_score(y_test, y_pred))) # save predictions, targets, and fine-tuned weights np.save(args.mlp_save_prefix + 'y_pred.npy', y_pred) np.save(args.mlp_save_prefix + 'y_test.npy', y_test) W_finetuned, _ = mlp.layers[0].get_weights() np.save(args.mlp_save_prefix + 'W_finetuned.npy', W_finetuned)
BN(), Activation('relu'), Dropout(args.mlp_dropout, seed=4444), Dense(10, kernel_initializer=glorot_uniform(seed=5555)), Activation('softmax'), ]) mlp.compile(optimizer=MultiAdam(lr=0.001, lr_multipliers={ 'dense_1': args.mlp_lrm[0], 'dense_2': args.mlp_lrm[1] }), loss='categorical_crossentropy', metrics=['accuracy']) # train and evaluate classifier with Stopwatch(verbose=True) as s: early_stopping = EarlyStopping(monitor=args.mlp_val_metric, patience=6, verbose=2) reduce_lr = ReduceLROnPlateau(monitor=args.mlp_val_metric, factor=0.2, verbose=2, patience=3, min_lr=1e-5) callbacks = [early_stopping, reduce_lr] try: mlp.fit(X_train, one_hot(y_train, n_classes=10), epochs=args.mlp_epochs, batch_size=args.mlp_batch_size, shuffle=False,