def main(_): dataset = cfg.dataset input_shape, num_classes, use_test_queue = get_dataset_values( dataset, cfg.test_batch_size, is_training=False) tf.logging.info("Initializing CNN for {}...".format(dataset)) model = CNN(input_shape, num_classes, is_training=False, use_test_queue=use_test_queue) tf.logging.info("Finished initialization.") if not os.path.exists(cfg.logdir): os.mkdir(cfg.logdir) logdir = os.path.join(cfg.logdir, model.name) if not os.path.exists(logdir): os.mkdir(logdir) logdir = os.path.join(logdir, dataset) if not os.path.exists(logdir): os.mkdir(logdir) sv = tf.train.Supervisor(graph=model.graph, logdir=logdir, save_model_secs=0) tf.logging.info("Initialize evaluation...") evaluate(model, sv, dataset) tf.logging.info("Finished evaluation.")
def run(): args = get_args() train_set = read_data(data_folder / args.train) test_set = read_data(data_folder / args.test, col_names=("tweet_id", "text", "q1_label")) regular_solution = Naive_Bayes(train_set, test_set, ['yes', 'no'], False) filtered_solution = Naive_Bayes(train_set, test_set, ['yes', 'no'], True) output_trace(output_folder / "trace_NB-BOW-OV.txt", regular_solution) output_trace(output_folder / "trace_NB-BOW-FV.txt", filtered_solution) evaluate(output_folder / "eval_NB-BOW-OV.txt", regular_solution, "yes", "no") evaluate(output_folder / "eval_NB-BOW-FV.txt", filtered_solution, "yes", "no") #using sanitized input train_set_sanitized = sanitize(train_set) test_set_sanitized = sanitize(test_set) sanitized_solution = Naive_Bayes(train_set_sanitized, test_set_sanitized, ['yes', 'no'], False) output_trace(output_folder / "trace_NB-BOW-OV_sanitized.txt", sanitized_solution) evaluate(output_folder / "eval_NB-BOW-OV_sanitized.txt", sanitized_solution, "yes", "no")
def train(): with tf.Graph().as_default(): global_step = tf.Variable(0, trainable=False) # Get images and labels. images, labels = convnet.inputs(eval_data=False) # Build a Graph that computes the logits predictions from the # inference model. logits = convnet.inference(images) # Calculate loss. loss = convnet.loss(logits, labels) # Calculate accuracy top_k_op = tf.nn.in_top_k(logits, labels, 1) # Build a Graph that trains the model with one batch of examples and # updates the model parameters. train_op = convnet.train(loss, global_step) # Create a saver. saver = tf.train.Saver(tf.all_variables()) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.merge_all_summaries() # Build an initialization operation to run below. init = tf.initialize_all_variables() # Start running operations on the Graph. sess = tf.Session(config=tf.ConfigProto( log_device_placement=FLAGS.log_device_placement)) sess.run(init) # Load previously stored model from checkpoint ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir) if ckpt and ckpt.model_checkpoint_path: # Restores from checkpoint saver.restore(sess, ckpt.model_checkpoint_path) # Assuming model_checkpoint_path looks something like: # /my-favorite-path/cifar10_train/model.ckpt-0, # extract global_step from it. global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] print("Loading from checkpoint.Global step %s" % global_step) else: print("No checkpoint file found...Creating a new model...") stepfile = "/home/soms/EmotionMusic/MediaEval_Classification/stepfile.txt" if not os.path.exists(stepfile): print("No step file found.") step = 0 else: f = open(stepfile, "r") step = int(f.readlines()[0]) print("Step file step %d" % step) # Start the queue runners. tf.train.start_queue_runners(sess=sess) summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, sess.graph) while step < FLAGS.max_steps: #print(images.eval(session = sess).shape) start_time = time.time() _, loss_value, predictions = sess.run([train_op, loss, top_k_op]) duration = time.time() - start_time def signal_handler(signal, frame): f = open(stepfile, 'w') f.write(str(step)) print("Step file written to.") sys.exit(0) signal.signal(signal.SIGINT, signal_handler) assert not np.isnan(loss_value), 'Model diverged with loss = NaN' if step % 10 == 0: num_examples_per_step = 25 examples_per_sec = num_examples_per_step / duration sec_per_batch = float(duration) accuracy = float(np.sum(predictions)) / num_examples_per_step format_str = ( '%s: step %d, loss = %.2f accuracy@1 = %.2f (%.1f examples/sec; %.3f ' 'sec/batch)') print(format_str % (datetime.now(), step, loss_value, accuracy, examples_per_sec, sec_per_batch)) if step % 100 == 0: summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str, step) # Save the model checkpoint periodically. if step % 500 == 0 or (step + 1) == FLAGS.max_steps: checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) model_eval.evaluate(mode=1) step += 1