if __name__ == "__main__": if len(sys.argv) < 6: print( "Error, Syntax: {0} [version] [prototype selection] [classwise/independent] [prototype number] [fold]" .format(sys.argv[0])) exit() version = sys.argv[1] selection = sys.argv[2] classwise = sys.argv[3] proto_number = int(sys.argv[4]) fold = int(sys.argv[5]) print("Starting: {}".format(version)) # load settings ns.load_settings_raw(version, "1d", 50, 2, fold) full_data_file = os.path.join("data", version + "-re-data.txt") full_label_file = os.path.join("data", version + "-re-labels.txt") # load data data_sets = input_data.read_data_sets(full_data_file, full_label_file, ns.IMAGE_SHAPE, test_ratio=0.1, validation_ratio=0.0, pickle=False, boring=False, fold=fold) no_classes = ns.NUM_CLASSES # print(proto_number)
def main(argv): if len(argv) < 5: print( "Error, Syntax: {0} [train/test] [dataset] [conv dim] [conv width] [fold]" .format(argv[0])) exit() global conv_shape conv_shape = int(argv[4]) dataset = argv[2] conv_dim = argv[3] test = argv[1] fold = int(argv[5]) ns.load_settings_raw(dataset, conv_dim, 50, 2, fold) run_name = "raw-fc1024-lr{0}-adam-{1}conv-{2}-fold{3}".format( ns.LEARNING_RATE, conv_shape, dataset, fold) # +"-"+nowtime print(run_name) data_sets = input_data.read_data_sets(ns.TRAINING_FILE, ns.TRAINING_LABEL, ns.IMAGE_SHAPE, test_file=ns.TEST_FILE, test_label=ns.TEST_LABEL, validation_ratio=0.0, pickle=False, boring=False) train_data = data_sets.train.images # Returns np.array train_labels = np.asarray(data_sets.train.labels, dtype=np.int32) eval_data = data_sets.test.images # Returns np.array eval_labels = np.asarray(data_sets.test.labels, dtype=np.int32) config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.4 # Create the Estimator if conv_dim == "1d": classifier = tf.estimator.Estimator( model_fn=cnn_model_1D, model_dir="models/" + run_name, config=tf.estimator.RunConfig(session_config=config)) else: classifier = tf.estimator.Estimator( model_fn=cnn_model_2D, model_dir="models/" + run_name, config=tf.estimator.RunConfig(session_config=config)) if test == "train": # train # Set up logging for predictions # Log the values in the "Softmax" tensor with label "probabilities" tensors_to_log = {"probabilities": "softmax_tensor"} logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=100) # Train the model train_input_fn = tf.estimator.inputs.numpy_input_fn( x={"x": train_data}, y=train_labels, batch_size=ns.BATCH_SIZE, num_epochs=None, shuffle=True) classifier.train(input_fn=train_input_fn, steps=ns.NUM_ITER, hooks=[logging_hook]) # Evaluate the model and print results eval_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": eval_data}, y=eval_labels, num_epochs=1, shuffle=False) eval_results = classifier.evaluate(input_fn=eval_input_fn) print(run_name) print(eval_results) else: # test # Evaluate the model and print results eval_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": eval_data}, y=eval_labels, num_epochs=1, shuffle=False) # eval_results = classifier.evaluate(input_fn=eval_input_fn) labels = eval_labels predictions = list(classifier.predict(input_fn=eval_input_fn)) predicted_classes = [p["classes"] for p in predictions] from sklearn.metrics import confusion_matrix, classification_report print(run_name) print(confusion_matrix(labels, predicted_classes)) print(classification_report(labels, predicted_classes))
output, cost, DTW, path = dtw.dtw(local_proto, local_sample, extended=True) for f in range(50): features[f, prototype] = cost[path[0][f]][path[1][f]] return features if __name__ == "__main__": for version in ["1a", "1b", "1c"]: print("Starting: {}".format(version)) # load settings ns.load_settings_raw(version, "1d") full_data_file = os.path.join("data", version + "-re-data.txt") full_label_file = os.path.join("data", version + "-re-labels.txt") # load data data_sets = input_data.read_data_sets(full_data_file, full_label_file, ns.IMAGE_SHAPE, test_ratio=0.1, validation_ratio=0.0, pickle=False, boring=False) # proto_factor is number of same class prototypes proto_factor = 5 if version == "1a" else 2 no_classes = ns.NUM_CLASSES