def get_features(model, train_test_data, train_validation_data, max_cardinality, score_function): repetitions = 5 max_score = 0 max_features = [] for r in range(repetitions): selected_features = [] selected_features_set = set() current_score = 0 for i in range(max_cardinality): feature_scores = get_feature_scores(model, train_validation_data, score_function, selected_features, selected_features_set, current_score) max_features = np.argpartition(feature_scores, -max_cardinality)[-max_cardinality:] positive_features = [ f for f in max_features if feature_scores[f] >= 0 ] skip_probability = 1 - len(positive_features) / max_cardinality if np.random.uniform() > skip_probability: feature_index = positive_features[np.random.randint( len(positive_features))] selected_features.append(feature_index) selected_features_set.add(feature_index) current_score = feature_scores[feature_index] score = model_accuracy(model, selected_features, train_validation_data) if max_score > score: max_features = list(selected_features) max_score = score return model_accuracy(model, max_features, train_test_data)
def forward_selection(model, train_test_data, train_validation_data, cardinality, score_function): selected_features = [] selected_features_set = set() current_score = 0 for i in range(cardinality): feature_scores = get_feature_scores(model, train_validation_data, score_function, selected_features, selected_features_set, current_score) feature_index = np.argmax(feature_scores) # if feature_scores[feature_index] > 0: selected_features.append(feature_index) selected_features_set.add(feature_index) current_score = feature_scores[feature_index] return model_accuracy(model, selected_features, train_test_data)
def test(model_file): tf.reset_default_graph() # Load CIFAR10 dataset cifar10 = CIFAR10() x_test = cifar10.x_test y_test = cifar10.y_test y_test_onehot = cifar10.y_test_onehot num_classes = cifar10.num_classes input_size = cifar10.input_size model = CNN(input_size=input_size, num_classes=num_classes, optimizer='Adam') model.load(filepath=model_file) test_prediction_onehot = model.test(data=x_test) test_prediction = np.argmax(test_prediction_onehot, axis=1).reshape( (-1, 1)) test_accuracy = model_accuracy(label=y_test, prediction=test_prediction) print('Test Accuracy: %f' % test_accuracy)
def test_from_frozen_graph(model_filepath): tf.reset_default_graph() # Load CIFAR10 dataset cifar10 = CIFAR10() x_test = cifar10.x_test y_test = cifar10.y_test y_test_onehot = cifar10.y_test_onehot num_classes = cifar10.num_classes input_size = cifar10.input_size # Test 500 samples x_test = x_test[0:500] y_test = y_test[0:500] model = CNN(model_filepath=model_filepath) test_prediction_onehot = model.test(data=x_test) test_prediction = np.argmax(test_prediction_onehot, axis=1).reshape( (-1, 1)) test_accuracy = model_accuracy(label=y_test, prediction=test_prediction) print('Test Accuracy: %f' % test_accuracy)
def train(learning_rate, learning_rate_decay, dropout_rate, mini_batch_size, epochs, optimizer, random_seed, model_directory, model_filename, log_directory): np.random.seed(random_seed) if not os.path.exists(log_directory): os.makedirs(log_directory) # Load CIFAR10 dataset cifar10 = CIFAR10() x_train = cifar10.x_train y_train = cifar10.y_train y_train_onehot = cifar10.y_train_onehot x_valid = cifar10.x_valid y_valid = cifar10.y_valid y_valid_onehot = cifar10.y_valid_onehot num_classes = cifar10.num_classes input_size = cifar10.input_size print('CIFAR10 Input Image Size: {}'.format(input_size)) model = CNN(input_size=input_size, num_classes=num_classes, optimizer=optimizer) train_accuracy_log = list() valid_accuracy_log = list() train_loss_log = list() for epoch in range(epochs): print('Epoch: %d' % epoch) learning_rate *= learning_rate_decay # Prepare mini batches on train set shuffled_idx = np.arange(len(x_train)) np.random.shuffle(shuffled_idx) mini_batch_idx = [ shuffled_idx[k:k + mini_batch_size] for k in range(0, len(x_train), mini_batch_size) ] # Validate on validation set valid_prediction_onehot = model.test(data=x_valid) valid_prediction = np.argmax(valid_prediction_onehot, axis=1).reshape( (-1, 1)) valid_accuracy = model_accuracy(label=y_valid, prediction=valid_prediction) print('Validation Accuracy: %f' % valid_accuracy) valid_accuracy_log.append(valid_accuracy) # Train on train set for i, idx in enumerate(mini_batch_idx): train_loss = model.train(data=x_train[idx], label=y_train_onehot[idx], learning_rate=learning_rate, dropout_rate=dropout_rate) if i % 200 == 0: train_prediction_onehot = model.test(data=x_train[idx]) train_prediction = np.argmax(train_prediction_onehot, axis=1).reshape((-1, 1)) train_accuracy = model_accuracy(label=y_train[idx], prediction=train_prediction) print('Training Loss: %f, Training Accuracy: %f' % (train_loss, train_accuracy)) if i == 0: train_accuracy_log.append(train_accuracy) train_loss_log.append(train_loss) model.save(directory=model_directory, filename=model_filename) print('Trained model saved successfully') model.save_as_pb(directory=model_directory, filename=model_filename) print('Trained model saved as pb successfully') # The directory should not exist before calling this method signature_dir = os.path.join(model_directory, 'signature') assert (not os.path.exists(signature_dir)) model.save_signature(directory=signature_dir) print('Trained model with signature saved successfully') plot_curve(train_losses = train_loss_log, train_accuracies = train_accuracy_log, valid_accuracies = valid_accuracy_log, \ filename = os.path.join(log_directory, 'training_curve.png'))
def wrapper(args): model, selected_features, candidate, data = args model = svm.SVC(kernel='rbf', gamma='auto') features = selected_features + [candidate] return utils.model_accuracy(model, features, data)