def train(model_name=None, hparams=None, train_csv_path=None, train_clip_dir=None,
          class_map_path=None, train_dir=None, sample_rate=None):
  """Runs the training loop."""
  print('\nTraining model:{} with hparams:{} and class map:{}'.format(model_name, hparams, class_map_path))
  print('Training data: clip dir {} and labels {}'.format(train_clip_dir, train_csv_path))
  print('Training dir {}\n'.format(train_dir))

  with tf.Graph().as_default():
    # Create the input pipeline.
    features, labels, num_classes, input_init = inputs.train_input(
        train_csv_path=train_csv_path, train_clip_dir=train_clip_dir, class_map_path=class_map_path,
        hparams=hparams, sample_rate=sample_rate)
    # Create the model in training mode.
    global_step, prediction, loss_tensor, train_op = model.define_model(
        model_name=model_name, features=features, labels=labels, num_classes=num_classes,
        hparams=hparams, training=True)

    # Define our own checkpoint saving hook, instead of using the built-in one,
    # so that we can specify additional checkpoint retention settings.
    saver = tf.train.Saver(
        max_to_keep=30, keep_checkpoint_every_n_hours=0.25)
    saver_hook = tf.train.CheckpointSaverHook(
        save_steps=250, checkpoint_dir=train_dir, saver=saver)

    summary_op = tf.summary.merge_all()
    summary_hook = tf.train.SummarySaverHook(
        save_steps=50, output_dir=train_dir, summary_op=summary_op)

    with tf.train.SingularMonitoredSession(hooks=[saver_hook, summary_hook],
                                           checkpoint_dir=train_dir) as sess:
      sess.raw_session().run(input_init)
      while not sess.should_stop():
        step, _, pred, loss = sess.run([global_step, train_op, prediction, loss_tensor])
        print(step, loss)
        sys.stdout.flush()
Beispiel #2
0
def eval(model_name=None,
         hparams=None,
         test_csv_path=None,
         test_clip_dir=None,
         class_map_path=None,
         checkpoint_path=None):
    with tf.Graph().as_default():
        label_class_index_table, num_classes = input.get_class_map(
            class_map_path)
        csv_record = tf.placeholder(tf.string, [])
        features, labels = input.record_to_labeled_log_mel_examples(
            csv_record,
            clip_dir=test_clip_dir,
            hparams=hparams,
            label_class_index_table=label_class_index_table,
            num_classes=num_classes)
        global_step, prediction, _, _ = model.define_model(
            model_name=model_name,
            features=features,
            num_classes=num_classes,
            hparams=hparams,
            training=False)

        saver = tf.train.Saver()

        with tf.train.SingularMonitoredSession(
                checkpoint_filename_with_path=checkpoint_path) as sess:
            class_map = {
                int(row[0]): row[1]
                for row in csv.reader(open(class_map_path))
            }
            ap_counts = defaultdict(int)
            ap_sums = defaultdict(float)
            test_records = open(test_csv_path).readlines()
            for (i, record) in enumerate(test_records[1:]):
                record = record.strip()
                actual, predicted = sess.run([labels, prediction],
                                             {csv_record: record})

                actual_class = np.argmax(actual[0])
                predicted = np.average(predicted, axis=0)
                predicted_classes = np.argsort(predicted)[::-1][:TOP_N]
                ap = avg_precision(actual=actual_class,
                                   predicted=predicted_classes)
                print(actual_class, predicted_classes, ap)

                ap_counts[actual_class] += 1
                ap_sums[actual_class] += ap

                if i % 50 == 0:
                    print_maps(ap_sums=ap_sums,
                               ap_counts=ap_counts,
                               class_map=class_map)
                sys.stdout.flush()

            print_maps(ap_sums=ap_sums,
                       ap_counts=ap_counts,
                       class_map=class_map)
Beispiel #3
0
def train(model_name=None,
          hparams=None,
          train_csv_path=None,
          train_clip_dir=None,
          class_map_path=None,
          train_dir=None,
          sample_rate=None):
    """Runs the training loop."""
    print('\nTraining model:{} with hparams:{} and class map:{}'.format(
        model_name, hparams, class_map_path))
    print('Training data: clip dir {} and labels {}'.format(
        train_clip_dir, train_csv_path))
    print('Training dir {}\n'.format(train_dir))

    with tf.Graph().as_default():
        # Create the input pipeline.
        features, labels, num_classes, input_init = inputs.train_input(
            train_csv_path=train_csv_path,
            train_clip_dir=train_clip_dir,
            class_map_path=class_map_path,
            hparams=hparams,
            sample_rate=sample_rate)
        # Create the model in training mode.
        global_step, prediction, loss_tensor, train_op = model.define_model(
            model_name=model_name,
            features=features,
            labels=labels,
            num_classes=num_classes,
            hparams=hparams,
            training=True)

        # Define our own checkpoint saving hook, instead of using the built-in one,
        # so that we can specify additional checkpoint retention settings.
        saver = tf.train.Saver(max_to_keep=30,
                               keep_checkpoint_every_n_hours=0.25)
        saver_hook = tf.train.CheckpointSaverHook(save_steps=250,
                                                  checkpoint_dir=train_dir,
                                                  saver=saver)

        summary_op = tf.summary.merge_all()
        summary_hook = tf.train.SummarySaverHook(save_steps=50,
                                                 output_dir=train_dir,
                                                 summary_op=summary_op)

        with tf.train.SingularMonitoredSession(
                hooks=[saver_hook,
                       summary_hook], checkpoint_dir=train_dir) as sess:
            sess.raw_session().run(input_init)
            while not sess.should_stop():
                step, _, pred, loss = sess.run(
                    [global_step, train_op, prediction, loss_tensor])
                print(step, loss)
                sys.stdout.flush()
Beispiel #4
0
    def __init__(self):
        self.TRAINING_DATA_DIRECTORY = "./dataset/train"
        self.VALIDATION_DATA_DIRECTORY = "./dataset/validation"
        self.EPOCHS = 10
        self.BATCH_SIZE = 64
        self.IMAGE_HEIGHT = 224
        self.IMAGE_WIDTH = 224
        self.CHANNELS = 3
        self.NUMBER_OF_TRAINING_IMAGES = 3198
        self.NUMBER_OF_VALIDATION_IMAGES = 100

        self.model = define_model()
        self.training_generator = None
        self.validation_generator = None
Beispiel #5
0
def inference(model_name=None,
              hparams=None,
              test_clip_dir=None,
              class_map_path=None,
              checkpoint_path=None,
              predictions_csv_path=None):
    with tf.Graph().as_default():
        _, num_classes = input.get_class_map(class_map_path)
        clip = tf.placeholder(tf.string, [])
        features = input.clip_to_log_mel_examples(clip,
                                                  clip_dir=test_clip_dir,
                                                  hparams=hparams)
        _, prediction, _, _ = model.define_model(model_name=model_name,
                                                 features=features,
                                                 num_classes=num_classes,
                                                 hparams=hparams,
                                                 training=False)

        saver = tf.train.Saver()

        with tf.train.SingularMonitoredSession(
                checkpoint_filename_with_path=checkpoint_path) as sess:
            class_map = {
                int(row[0]): row[1]
                for row in csv.reader(open(class_map_path))
            }
            test_clips = sorted(os.listdir(test_clip_dir))
            pred_writer = csv.DictWriter(open(predictions_csv_path, 'w'),
                                         fieldnames=['fname', 'label'])
            for (i, test_clip) in enumerate(test_clips):
                print(i, test_clip)
                sys.stdout.flush()
                # Hack to avoid passing empty files through the model.
                if os.path.getsize(os.path.join(test_clip_dir,
                                                test_clip)) == 44:
                    print('empty file, skipped model')
                    label = ''
                else:
                    predicted = sess.run(prediction, {clip: test_clip})
                    predicted = np.average(predicted, axis=0)
                    predicted_classes = np.argsort(predicted)[::-1][:TOP_N]
                    label = ' '.join([class_map[c] for c in predicted_classes])
                    pred_writer.writerow({'fname': test_clip, 'label': label})
                print(label)
                sys.stdout.flush()
Beispiel #6
0
def train(model_name=None,
          hparams=None,
          train_csv_path=None,
          train_clip_dir=None,
          class_map_path=None):
    with tf.Graph().as_default():
        features, labels, num_classes, input_init = input.train_input(
            train_csv_path=train_csv_path,
            train_clip_dir=train_clip_dir,
            class_map_path=class_map_path,
            hparams=hparams)
        global_step, prediction, loss_tensor, train_op = model.define_model(
            model_name=model_name,
            features=features,
            labels=labels,
            num_classes=num_classes,
            hparams=hparams,
            training=True)

        saver = tf.train.Saver(max_to_keep=30,
                               keep_checkpoint_every_n_hours=0.25)
        saver_hook = tf.train.CheckpointSaverHook(save_steps=250,
                                                  checkpoint_dir='train',
                                                  saver=saver)

        summary_op = tf.summary.merge_all()
        summary_hook = tf.train.SummarySaverHook(save_steps=50,
                                                 output_dir='train',
                                                 summary_op=summary_op)

        with tf.train.SingularMonitoredSession(
                hooks=[saver_hook, summary_hook]) as sess:
            sess.raw_session().run(input_init)
            while not sess.should_stop():
                step, _, loss = sess.run([global_step, train_op, loss_tensor])
                print(step, loss)
                sys.stdout.flush()
Beispiel #7
0
from utilits import load_data, preprocess_data
from model import define_model, train_and_validate

if __name__ == "__main__":
    # 下载数据
    # download()

    # 加载数据
    X_train, y_train, X_test, y_test, class_names = load_data('data')

    # 数据预处理
    # 模型使用规模为[N, 28, 28, 1]的批处理,输出规模为[N, 100]的概率
    X_train, y_train, X_test, y_test = preprocess_data(X_train, y_train,
                                                       X_test, y_test,
                                                       class_names)

    # 定义模型
    model = define_model(X_train.shape[1:])

    # 训练并测试模型
    model = train_and_validate(X_train, y_train, X_test, y_test, model)

    # 保存h5格式的模型
    model.save('quick_draw.h5')
Beispiel #8
0
def main(_):

    config = tf.ConfigProto(inter_op_parallelism_threads=num_inter_op_threads,
                            intra_op_parallelism_threads=num_intra_op_threads)

    run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
    run_metadata = tf.RunMetadata()  # For Tensorflow trace

    cluster = tf.train.ClusterSpec({"ps": ps_list, "worker": worker_list})
    server = tf.train.Server(cluster, job_name=job_name, task_index=task_index)

    is_sync = (FLAGS.is_sync == 1)  # Synchronous or asynchronous updates
    is_chief = (task_index == 0)  # Am I the chief node (always task 0)

    greedy = tf.contrib.training.GreedyLoadBalancingStrategy(
        num_tasks=len(ps_hosts), load_fn=tf.contrib.training.byte_size_load_fn)

    if job_name == "ps":

        with tf.device(
                tf.train.replica_device_setter(
                    worker_device="/job:ps/task:{}".format(task_index),
                    ps_tasks=len(ps_hosts),
                    ps_strategy=greedy,
                    cluster=cluster)):

            sess = tf.Session(server.target, config=config)
            queue = create_done_queue(task_index)

            print("*" * 30)
            print("\nParameter server #{} on {}.\n\n" \
             "Waiting on workers to finish.\n\nPress CTRL-\\ to terminate early.\n"  \
             .format(task_index, ps_hosts[task_index]))
            print("*" * 30)

            # wait until all workers are done
            for i in range(len(worker_hosts)):
                sess.run(queue.dequeue())
                print("Worker #{} reports job finished.".format(i))

            print("Parameter server #{} is quitting".format(task_index))
            print("Training complete.")

    elif job_name == "worker":

        if is_chief:
            print("I am chief worker {} with task #{}".format(
                worker_hosts[task_index], task_index))
        else:
            print("I am worker {} with task #{}".format(
                worker_hosts[task_index], task_index))

        if len(ps_list) > 0:
            setDevice = tf.train.replica_device_setter(
                worker_device="/job:worker/task:{}".format(task_index),
                ps_tasks=len(ps_hosts),
                ps_strategy=greedy,
                cluster=cluster)
        else:
            setDevice = "/cpu:0"  # No parameter server so put variables on chief worker

        with tf.device(setDevice):

            global_step = tf.Variable(0, name="global_step", trainable=False)

            # Load the data
            imgs_train, msks_train, imgs_test, msks_test = load_all_data()
            train_length = imgs_train.shape[0]  # Number of train datasets
            test_length = imgs_test.shape[0]  # Number of test datasets
            """
			BEGIN: Define our model
			"""

            imgs = tf.placeholder(tf.float32,
                                  shape=(None, msks_train.shape[1],
                                         msks_train.shape[2],
                                         msks_train.shape[3]))

            msks = tf.placeholder(tf.float32,
                                  shape=(None, msks_train.shape[1],
                                         msks_train.shape[2],
                                         msks_train.shape[3]))

            preds = define_model(imgs, FLAGS.use_upsampling,
                                 settings_dist.OUT_CHANNEL_NO)

            print('Model defined')

            loss_value = dice_coef_loss(msks, preds)
            dice_value = dice_coef(msks, preds)

            sensitivity_value = sensitivity(msks, preds)
            specificity_value = specificity(msks, preds)

            test_loss_value = tf.placeholder(tf.float32, ())
            test_dice_value = tf.placeholder(tf.float32, ())

            test_sensitivity_value = tf.placeholder(tf.float32, ())
            test_specificity_value = tf.placeholder(tf.float32, ())
            """
			END: Define our model
			"""

            # Decay learning rate from initial_learn_rate to initial_learn_rate*fraction in decay_steps global steps
            if FLAGS.const_learningrate:
                learning_rate = tf.convert_to_tensor(FLAGS.learning_rate,
                                                     dtype=tf.float32)
            else:
                learning_rate = tf.train.exponential_decay(FLAGS.learning_rate,
                                                           global_step,
                                                           FLAGS.decay_steps,
                                                           FLAGS.lr_fraction,
                                                           staircase=False)

            # Compensate learning rate for asynchronous distributed
            # THEORY: We need to cut the learning rate by at least the number
            # of workers since there are likely to be that many times increased
            # parameter updates.
            # if not is_sync:
            # 	learning_rate /= len(worker_hosts)
            # 	optimizer = tf.train.GradientDescentOptimizer(learning_rate)
            # 	#optimizer = tf.train.AdagradOptimizer(learning_rate)
            # else:
            # 	optimizer = tf.train.AdamOptimizer(learning_rate)

            optimizer = tf.train.AdamOptimizer(learning_rate)

            grads_and_vars = optimizer.compute_gradients(loss_value)
            if is_sync:

                rep_op = tf.train.SyncReplicasOptimizer(
                    optimizer,
                    replicas_to_aggregate=len(worker_hosts),
                    total_num_replicas=len(worker_hosts),
                    use_locking=True)

                train_op = rep_op.apply_gradients(grads_and_vars,
                                                  global_step=global_step)

                init_token_op = rep_op.get_init_tokens_op()

                chief_queue_runner = rep_op.get_chief_queue_runner()

            else:

                train_op = optimizer.apply_gradients(grads_and_vars,
                                                     global_step=global_step)

            init_op = tf.global_variables_initializer()

            saver = tf.train.Saver()

            # These are the values we wish to print to TensorBoard

            tf.summary.scalar("loss", loss_value)
            tf.summary.histogram("loss", loss_value)
            tf.summary.scalar("dice", dice_value)
            tf.summary.histogram("dice", dice_value)

            tf.summary.scalar("sensitivity", sensitivity_value)
            tf.summary.histogram("sensitivity", sensitivity_value)
            tf.summary.scalar("specificity", specificity_value)
            tf.summary.histogram("specificity", specificity_value)

            tf.summary.image("predictions",
                             preds,
                             max_outputs=settings_dist.TENSORBOARD_IMAGES)
            tf.summary.image("ground_truth",
                             msks,
                             max_outputs=settings_dist.TENSORBOARD_IMAGES)
            tf.summary.image("images",
                             imgs,
                             max_outputs=settings_dist.TENSORBOARD_IMAGES)

            print("Loading epoch")
            epoch = get_epoch(batch_size, imgs_train, msks_train)
            num_batches = len(epoch)
            print("Loaded")

            # Print the percent steps complete to TensorBoard
            #   so that we know how much of the training remains.
            num_steps_tf = tf.constant(num_batches * FLAGS.epochs, tf.float32)
            percent_done_value = tf.constant(100.0) * tf.to_float(
                global_step) / num_steps_tf
            tf.summary.scalar("percent_complete", percent_done_value)

        # Need to remove the checkpoint directory before each new run
        # import shutil
        # shutil.rmtree(CHECKPOINT_DIRECTORY, ignore_errors=True)

        # Send a signal to the ps when done by simply updating a queue in the shared graph
        enq_ops = []
        for q in create_done_queues():
            qop = q.enqueue(1)
            enq_ops.append(qop)

        # Only the chief does the summary
        if is_chief:
            summary_op = tf.summary.merge_all()
        else:
            summary_op = None

        # Add summaries for test data
        # These summary ops are not part of the merge all op.
        # This way we can call these separately.
        test_loss_value = tf.placeholder(tf.float32, ())
        test_dice_value = tf.placeholder(tf.float32, ())

        test_loss_summary = tf.summary.scalar("loss_test", test_loss_value)
        test_dice_summary = tf.summary.scalar("dice_test", test_dice_value)

        test_sens_summary = tf.summary.scalar("sensitivity_test",
                                              test_sensitivity_value)
        test_spec_summary = tf.summary.scalar("specificity_test",
                                              test_specificity_value)

        # TODO:  Theoretically I can pass the summary_op into
        # the Supervisor and have it handle the TensorBoard
        # log entries. However, doing so seems to hang the code.
        # For now, I just handle the summary calls explicitly.
        # import time
        # logDirName = CHECKPOINT_DIRECTORY + "/run" + \
        # 			time.strftime("_%Y%m%d_%H%M%S")

        if FLAGS.use_upsampling:
            method_up = "upsample2D"
        else:
            method_up = "conv2DTranspose"

        logDirName = CHECKPOINT_DIRECTORY + "/unet," + \
           "lr={},{},intra={},inter={}".format(FLAGS.learning_rate,
           method_up, num_intra_op_threads,
           num_inter_op_threads)

        sv = tf.train.Supervisor(
            is_chief=is_chief,
            logdir=logDirName,
            init_op=init_op,
            summary_op=None,
            saver=saver,
            global_step=global_step,
            save_model_secs=60  # Save the model (with weights) everty 60 seconds
        )

        # TODO:
        # I'd like to use managed_session for this as it is more abstract
        # and probably less sensitive to changes from the TF team. However,
        # I am finding that the chief worker hangs on exit if I use managed_session.
        with sv.prepare_or_wait_for_session(server.target,
                                            config=config) as sess:
            #with sv.managed_session(server.target) as sess:

            if sv.is_chief and is_sync:
                sv.start_queue_runners(sess, [chief_queue_runner])
                sess.run(init_token_op)

            step = 0

            progressbar = trange(num_batches * FLAGS.epochs)
            last_step = 0

            # Start TensorBoard on the chief worker
            if sv.is_chief:
                cmd = 'tensorboard --logdir={}'.format(CHECKPOINT_DIRECTORY)
                tb_process = subprocess.Popen(cmd,
                                              stdout=subprocess.PIPE,
                                              shell=True,
                                              preexec_fn=os.setsid)

            while (not sv.should_stop()) and (step <
                                              (num_batches * FLAGS.epochs)):

                batch_idx = step % num_batches  # Which batch is the epoch?

                data = epoch[batch_idx, 0]
                labels = epoch[batch_idx, 1]

                # For n workers, break up the batch into n sections
                # Send each worker a different section of the batch
                data_range = int(batch_size / len(worker_hosts))
                start = data_range * task_index
                end = start + data_range

                feed_dict = {imgs: data[start:end], msks: labels[start:end]}

                history, loss_v, dice_v, step = sess.run(
                    [train_op, loss_value, dice_value, global_step],
                    feed_dict=feed_dict)

                # Print summary only on chief
                if sv.is_chief:

                    summary = sess.run(summary_op, feed_dict=feed_dict)
                    sv.summary_computed(sess, summary)  # Update the summary

                    # Calculate metric on test dataset every epoch
                    if (batch_idx == 0) and (step > num_batches):

                        dice_v_test = 0.0
                        loss_v_test = 0.0
                        sens_v_test = 0.0
                        spec_v_test = 0.0

                        for idx in tqdm(
                                range(0, imgs_test.shape[0] - batch_size,
                                      batch_size),
                                desc="Calculating metrics on test dataset",
                                leave=False):
                            x_test = imgs_test[idx:(idx + batch_size)]
                            y_test = msks_test[idx:(idx + batch_size)]

                            feed_dict = {imgs: x_test, msks: y_test}

                            l_v, d_v, st_v, sp_v = sess.run(
                                [
                                    loss_value, dice_value, sensitivity_value,
                                    specificity_value
                                ],
                                feed_dict=feed_dict)

                            dice_v_test += d_v / (test_length // batch_size)
                            loss_v_test += l_v / (test_length // batch_size)
                            sens_v_test += st_v / (test_length // batch_size)
                            spec_v_test += sp_v / (test_length // batch_size)


                        print("\nEpoch {} of {}: TEST DATASET\nloss = {:.4f}\nDice = {:.4f}\n" \
                         "Sensitivity = {:.4f}\nSpecificity = {:.4f}" \
                         .format((step // num_batches), FLAGS.epochs,
                          loss_v_test, dice_v_test, sens_v_test, spec_v_test))

                        # Add our test summary metrics to TensorBoard
                        sv.summary_computed(
                            sess,
                            sess.run(test_loss_summary,
                                     feed_dict={test_loss_value: loss_v_test}))
                        sv.summary_computed(
                            sess,
                            sess.run(test_dice_summary,
                                     feed_dict={test_dice_value: dice_v_test}))
                        sv.summary_computed(
                            sess,
                            sess.run(test_sens_summary,
                                     feed_dict={
                                         test_sensitivity_value: sens_v_test
                                     }))
                        sv.summary_computed(
                            sess,
                            sess.run(test_spec_summary,
                                     feed_dict={
                                         test_specificity_value: spec_v_test
                                     }))

                        saver.save(
                            sess,
                            CHECKPOINT_DIRECTORY + "/last_good_model.cpkt")

                # Shuffle every epoch
                if (batch_idx == 0) and (step > num_batches):

                    print("Shuffling epoch")
                    epoch = get_epoch(batch_size, imgs_train, msks_train)

                # Print the loss and dice metric in the progress bar.
                progressbar.set_description(
                    "(loss={:.4f}, dice={:.4f})".format(loss_v, dice_v))
                progressbar.update(step - last_step)
                last_step = step

            # Perform the final test set metric
            if sv.is_chief:

                dice_v_test = 0.0
                loss_v_test = 0.0

                for idx in tqdm(range(0, imgs_test.shape[0] - batch_size,
                                      batch_size),
                                desc="Calculating metrics on test dataset",
                                leave=False):
                    x_test = imgs_test[idx:(idx + batch_size)]
                    y_test = msks_test[idx:(idx + batch_size)]

                    feed_dict = {imgs: x_test, msks: y_test}

                    l_v, d_v = sess.run([loss_value, dice_value],
                                        feed_dict=feed_dict)

                    dice_v_test += d_v / (test_length // batch_size)
                    loss_v_test += l_v / (test_length // batch_size)


                print("\nEpoch {} of {}: Test loss = {:.4f}, Test Dice = {:.4f}" \
                 .format((step // num_batches), FLAGS.epochs,
                  loss_v_test, dice_v_test))

                sv.summary_computed(
                    sess,
                    sess.run(test_loss_summary,
                             feed_dict={test_loss_value: loss_v_test}))
                sv.summary_computed(
                    sess,
                    sess.run(test_dice_summary,
                             feed_dict={test_dice_value: dice_v_test}))

                saver.save(sess,
                           CHECKPOINT_DIRECTORY + "/last_good_model.cpkt")

            if sv.is_chief:
                export_model(
                    sess, imgs, preds
                )  # Save the final model as protbuf for TensorFlow Serving

                os.killpg(os.getpgid(tb_process.pid),
                          signal.SIGTERM)  # Stop TensorBoard process

            # Send a signal to the ps when done by simply updating a queue in the shared graph
            for op in enq_ops:
                sess.run(
                    op
                )  # Send the "work completed" signal to the parameter server

        print("\n\nFinished work on this node.")
        import time
        time.sleep(3)  # Sleep for 3 seconds then exit

        sv.request_stop()
Beispiel #9
0
def train_model():
    learning_rate = 0.0001
    batch_size = 128
    c_dim = 1
    n_batch = 10000 // batch_size

    x_input = tf.placeholder(tf.float32, [None, None, None, c_dim],
                             name='x_input')
    y_label2 = tf.placeholder(tf.float32, [None, None, None, c_dim],
                              name='y_label2')
    x1_bic_add = tf.placeholder(tf.float32, [None, None, None, c_dim],
                                name='x1_bic_add')

    output = define_model(x_input, x1_bic_add)

    mse_loss = tf.reduce_mean(tf.square(y_label2 - output))
    train = tf.train.GradientDescentOptimizer(learning_rate).minimize(mse_loss)

    saver = tf.train.Saver(max_to_keep=4)
    tf.add_to_collection("predict", output)

    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)

        # load train set
        x1d = load_h5('Y_dataset.h5', 'train_cut_l')
        x2d = load_h5('Y_dataset.h5', 'train_cut_h')
        x1_b = load_h5('Y_dataset.h5', 'train_cut_b')
        x1_data = x1d[:10000, :, :, :] / 255
        x2_data = x2d[:10000, :, :, :] / 255
        x1_bic = x1_b[:10000, :, :, :] / 255
        print(x1_data.shape, x2_data.shape, x1_bic.shape)

        counter = 0

        for i in range(2001):
            for idx in range(0, n_batch):
                batch_images = x1_data[idx * batch_size:(idx + 1) * batch_size]
                batch_bic = x1_bic[idx * batch_size:(idx + 1) * batch_size]
                batch_labels = x2_data[idx * batch_size:(idx + 1) * batch_size]

                sess.run(train,
                         feed_dict={
                             x_input: batch_images,
                             y_label2: batch_labels,
                             x1_bic_add: batch_bic
                         })
                counter += 1
                if counter % 50 == 0:
                    print(
                        'Epoh', i, 'n_batch', idx, 'Train loss:',
                        sess.run(mse_loss,
                                 feed_dict={
                                     x_input: batch_images,
                                     y_label2: batch_labels,
                                     x1_bic_add: batch_bic
                                 }))
            rands(x1_data, x2_data, x1_bic)

            if i % 10 == 0:
                saver.save(sess,
                           r'./checkpoint/' + "model_conv/my-model",
                           global_step=i)
                print("save the model")
def evaluate(model_name=None, hparams=None, eval_csv_path=None, eval_clip_dir=None,
             class_map_path=None, checkpoint_path=None):
  """Runs the evaluation loop."""
  print('\nEvaluation for model:{} with hparams:{} and class map:{}'.format(model_name, hparams, class_map_path))
  print('Evaluation data: clip dir {} and labels {}'.format(eval_clip_dir, eval_csv_path))
  print('Checkpoint: {}\n'.format(checkpoint_path))

  with tf.Graph().as_default():
    label_class_index_table, num_classes = inputs.get_class_map(class_map_path)
    csv_record = tf.placeholder(tf.string, [])  # fed during evaluation loop.

    # Use a simpler in-order input pipeline for eval than the one used in
    # training, since we don't want to shuffle examples across clips.
    # The features consist of a batch of all possible framed log mel spectrum
    # examples from the same clip. The labels in this case will contain a batch
    # of identical 1-hot vectors.
    features, labels = inputs.record_to_labeled_log_mel_examples(
        csv_record, clip_dir=eval_clip_dir, hparams=hparams,
        label_class_index_table=label_class_index_table, num_classes=num_classes)

    # Create the model in prediction mode.
    global_step, prediction, _, _ = model.define_model(
        model_name=model_name, features=features, num_classes=num_classes,
        hparams=hparams, training=False)

    with tf.train.SingularMonitoredSession(checkpoint_filename_with_path=checkpoint_path) as sess:
      # Read in class map CSV into a class index -> class name map.
      class_map = {int(row[0]): row[1] for row in csv.reader(open(class_map_path))}

      # Keep running counters to aid printing incremental AP stats.
      ap_counts = defaultdict(int)  # maps class index to the number of clips with that label.
      ap_sums = defaultdict(float)  # maps class index to the sum of AP for all clips with that label.

      # Read in the validation CSV, skippign the header.
      eval_records = open(eval_csv_path).readlines()[1:]
      # Shuffle the lines so that as we print incremental stats, we get good
      # coverage across classes and get a quick initial impression of how well
      # the model is doing even before evaluation is completely done.
      random.shuffle(eval_records)

      for (i,record) in enumerate(eval_records):
        record = record.strip()
        actual, predicted = sess.run([labels, prediction], {csv_record: record})

        # By construction, actual consists of identical rows, where each row is
        # the same 1-hot label (because we are looking at features from the same
        # clip). np.argmax() of any of the rows (and in particular [0]) will
        # give us the class index corresponding to the label.
        actual_class = np.argmax(actual[0])

        predicted_classes = get_top_predicted_classes(predicted)

        # Compute AP for this item, update running counters/sums, and print the
        # prediction vs actual.
        ap = avg_precision(actual=actual_class, predicted=predicted_classes)
        ap_counts[actual_class] += 1
        ap_sums[actual_class] += ap
        print(class_map[actual_class], [class_map[index] for index in predicted_classes], ap)

        # Periodically print per-class and overall AP stats.
        if i % 50 == 0:
          print_maps(ap_sums=ap_sums, ap_counts=ap_counts, class_map=class_map)
        sys.stdout.flush()

      # Final AP stats.
      print_maps(ap_sums=ap_sums, ap_counts=ap_counts, class_map=class_map)
Beispiel #11
0
def train(model_name=None,
          hparams=None,
          class_map_path=None,
          train_csv_path=None,
          train_clip_dir=None,
          train_dir=None,
          epoch_batches=None,
          warmstart_checkpoint=None,
          warmstart_include_scopes=None,
          warmstart_exclude_scopes=None):
    """Runs the training loop."""
    print('\nTraining model:{} with hparams:{} and class map:{}'.format(
        model_name, hparams, class_map_path))
    print('Training data: clip dir {} and labels {}'.format(
        train_clip_dir, train_csv_path))
    print('Training dir {}\n'.format(train_dir))

    with tf.Graph().as_default():
        # Create the input pipeline.
        features, labels, num_classes, input_init = inputs.train_input(
            train_csv_path=train_csv_path,
            train_clip_dir=train_clip_dir,
            class_map_path=class_map_path,
            hparams=hparams)
        # Create the model in training mode.
        global_step, prediction, loss_tensor, train_op = model.define_model(
            model_name=model_name,
            features=features,
            labels=labels,
            num_classes=num_classes,
            hparams=hparams,
            epoch_batches=epoch_batches,
            training=True)

        # Define our own checkpoint saving hook, instead of using the built-in one,
        # so that we can specify additional checkpoint retention settings.
        saver = tf.train.Saver(max_to_keep=10000,
                               keep_checkpoint_every_n_hours=0.25)
        saver_hook = tf.train.CheckpointSaverHook(save_steps=100,
                                                  checkpoint_dir=train_dir,
                                                  saver=saver)

        summary_op = tf.summary.merge_all()
        summary_hook = tf.train.SummarySaverHook(save_steps=10,
                                                 output_dir=train_dir,
                                                 summary_op=summary_op)

        if hparams.warmstart:
            var_include_scopes = warmstart_include_scopes
            if not var_include_scopes: var_include_scopes = None
            var_exclude_scopes = warmstart_exclude_scopes
            if not var_exclude_scopes: var_exclude_scopes = None
            restore_vars = tf.contrib.framework.get_variables_to_restore(
                include=var_include_scopes, exclude=var_exclude_scopes)
            # Only restore trainable variables, we don't want to restore
            # batch-norm or optimizer-specific local variables.
            trainable_vars = set(
                tf.contrib.framework.get_trainable_variables())
            restore_vars = [
                var for var in restore_vars if var in trainable_vars
            ]

            print('Warm-start: restoring variables:\n%s\n' %
                  '\n'.join([x.name for x in restore_vars]))
            print('Warm-start: restoring from ', warmstart_checkpoint)
            assert restore_vars, 'No warm-start variables to restore!'
            restore_op, feed_dict = tf.contrib.framework.assign_from_checkpoint(
                model_path=warmstart_checkpoint,
                var_list=restore_vars,
                ignore_missing_vars=True)

            scaffold = tf.train.Scaffold(init_fn=lambda scaffold, session:
                                         session.run(restore_op, feed_dict),
                                         summary_op=summary_op,
                                         saver=saver)
        else:
            scaffold = None

        with tf.train.SingularMonitoredSession(
                hooks=[saver_hook, summary_hook],
                checkpoint_dir=train_dir,
                scaffold=scaffold) as sess:
            sess.raw_session().run(input_init)
            while not sess.should_stop():
                step, _, pred, loss = sess.run(
                    [global_step, train_op, prediction, loss_tensor])
                print(step, loss)
                sys.stdout.flush()
def train_and_evaluate(model_name=None, hparams=None, class_map_path=None, train_csv_path=None, train_clip_dir=None,
                       train_dir=None, epoch_batches=None, warmstart_checkpoint=None,
                       warmstart_include_scopes=None, warmstart_exclude_scopes=None,
                       eval_csv_path=None, eval_clip_dir=None, eval_dir=None):
    """Runs the training loop."""
    print('\nTraining model:{} with hparams:{} and class map:{}'.format(model_name, hparams, class_map_path))
    print('Training data: clip dir {} and labels {}'.format(train_clip_dir, train_csv_path))
    print('Training dir {}\n'.format(train_dir))

    class_map = {int(row[0]): row[1] for row in csv.reader(open(class_map_path))}

    with tf.Graph().as_default():
        # Create the input pipeline.
        features, labels, num_classes, input_init = inputs.train_input(
            train_csv_path=train_csv_path, train_clip_dir=train_clip_dir, class_map_path=class_map_path,
            hparams=hparams)
        # Create the model in training mode.
        global_step, prediction, loss_tensor, train_op = model.define_model(
            model_name=model_name, features=features, labels=labels, num_classes=num_classes,
            hparams=hparams, epoch_batches=epoch_batches, training=True)

        # evaluation graph
        label_class_index_table, num_classes = inputs.get_class_map(class_map_path)
        csv_record = tf.placeholder(tf.string, [])  # fed during evaluation loop.

        eval_features, eval_labels = inputs.record_to_labeled_log_mel_examples(
            csv_record, clip_dir=eval_clip_dir, hparams=hparams,
            label_class_index_table=label_class_index_table, num_classes=num_classes)

        # Create the model in prediction mode.
        global_step, eval_predictions, eval_loss_tensor, _ = model.define_model(
            model_name=model_name, features=eval_features, labels=eval_labels, num_classes=num_classes,
            hparams=hparams, training=False, evaluating=True)

        # Write evaluation graph to checkpoint directory.
        tf.train.write_graph(tf.get_default_graph().as_graph_def(add_shapes=True),
                             eval_dir, 'eval.pbtxt')

        eval_writer = tf.summary.FileWriter(eval_dir, tf.get_default_graph())

        # Define our own checkpoint saving hook, instead of using the built-in one,
        # so that we can specify additional checkpoint retention settings.
        saver = tf.train.Saver(
            max_to_keep=10, keep_checkpoint_every_n_hours=0.25)
        saver_hook = tf.train.CheckpointSaverHook(
            save_steps=100, checkpoint_dir=train_dir, saver=saver)

        summary_op = tf.summary.merge_all()
        summary_hook = tf.train.SummarySaverHook(
            save_steps=10, output_dir=train_dir, summary_op=summary_op)

        if hparams.warmstart:
            var_include_scopes = warmstart_include_scopes
            if not var_include_scopes: var_include_scopes = None
            var_exclude_scopes = warmstart_exclude_scopes
            if not var_exclude_scopes: var_exclude_scopes = None
            restore_vars = tf.contrib.framework.get_variables_to_restore(
                include=var_include_scopes, exclude=var_exclude_scopes)
            # Only restore trainable variables, we don't want to restore
            # batch-norm or optimizer-specific local variables.
            trainable_vars = set(tf.contrib.framework.get_trainable_variables())
            restore_vars = [var for var in restore_vars if var in trainable_vars]

            print('Warm-start: restoring variables:\n%s\n' % '\n'.join([x.name for x in restore_vars]))
            print('Warm-start: restoring from ', warmstart_checkpoint)
            assert restore_vars, 'No warm-start variables to restore!'
            restore_op, feed_dict = tf.contrib.framework.assign_from_checkpoint(
                model_path=warmstart_checkpoint, var_list=restore_vars, ignore_missing_vars=True)

            scaffold = tf.train.Scaffold(
                init_fn=lambda scaffold, session: session.run(restore_op, feed_dict),
                summary_op=summary_op, saver=saver)
        else:
            scaffold = None

        with tf.train.SingularMonitoredSession(hooks=[saver_hook, summary_hook],
                                               checkpoint_dir=train_dir,
                                               scaffold=scaffold,
                                               config=tf.ConfigProto(log_device_placement=True)) as sess:
            sess.raw_session().run(input_init)
            while not sess.should_stop():

                # train
                step, _, pred, loss = sess.run([global_step, train_op, prediction, loss_tensor])
                print(step, loss)
                sys.stdout.flush()

                # evaluates every 100 steps
                if step > 0 and step % 100 == 0:
                    # Loop through all checkpoints in the training directory.
                    checkpoint_state = tf.train.get_checkpoint_state(train_dir)

                    lwlrap = eval_batch(eval_csv_path, sess, eval_labels, eval_predictions, csv_record, step, eval_writer, class_map, eval_loss_tensor)

                        
Beispiel #13
0
from parameters import medical_centers, medics, modules, days, notification_rates, time_between, medical_centers_boxes, fine_cost, action_plan_cost
from model import define_model
from get_output import get_model_output

params = {
    'medical_centers': medical_centers,
    'medics': medics,
    'modules': modules,
    'days': days,
    'notification_rates': notification_rates,
    'time_between': time_between,
    'medical_centers_boxes': medical_centers_boxes,
    'fine_cost': fine_cost,
    'action_plan_cost': action_plan_cost
}

model = define_model(**params)

model.optimize()

get_model_output(model)
def predict(model_name=None,
            hparams=None,
            test_clip_dir=None,
            class_map_path=None,
            checkpoint_path=None,
            predictions_csv_path=None):
    """Runs the prediction loop, producting prediction output in Kaggle submission format."""
    print('\nPrediction for model:{} with hparams:{} and class map:{}'.format(
        model_name, hparams, class_map_path))
    print('Prediction data: clip dir {} and checkpoint {}'.format(
        test_clip_dir, checkpoint_path))
    print('Predictions in CSV {}\n'.format(predictions_csv_path))

    with tf.Graph().as_default():
        _, num_classes = inputs.get_class_map(class_map_path)
        clip = tf.placeholder(tf.string, [])  # Fed during prediction loop.

        # Use a simpler in-order input pipeline without labels for prediction
        # compared to the one used in training. The features consist of a batch of
        # all possible framed log mel spectrum examples from the same clip.
        features = inputs.clip_to_log_mel_examples(clip,
                                                   clip_dir=test_clip_dir,
                                                   hparams=hparams)

        # Creates the model in prediction mode.
        _, prediction, _, _ = model.define_model(model_name=model_name,
                                                 features=features,
                                                 num_classes=num_classes,
                                                 hparams=hparams,
                                                 training=False)

        with tf.train.SingularMonitoredSession(
                checkpoint_filename_with_path=checkpoint_path) as sess:
            # Read in class map CSV into a class index -> class name map.
            class_map = {
                int(row[0]): row[1]
                for row in csv.reader(open(class_map_path))
            }

            test_clips = sorted(os.listdir(test_clip_dir))
            pred_writer = csv.DictWriter(open(predictions_csv_path, 'w'),
                                         fieldnames=['fname', 'label'])
            pred_writer.writeheader()

            for (i, test_clip) in enumerate(test_clips):
                print(i, test_clip)
                sys.stdout.flush()

                # Hack to avoid passing empty files through the model.
                if os.path.getsize(os.path.join(test_clip_dir,
                                                test_clip)) == 44:
                    print('empty file, skipped model')
                    label = ''
                else:
                    predicted = sess.run(prediction, {clip: test_clip})
                    predicted_classes = evaluation.get_top_predicted_classes(
                        predicted)
                    label = ' '.join([class_map[c] for c in predicted_classes])

                pred_writer.writerow({'fname': test_clip, 'label': label})
                print(label)
                sys.stdout.flush()
def evaluate(model_name=None,
             hparams=None,
             eval_csv_path=None,
             eval_clip_dir=None,
             class_map_path=None,
             checkpoint_path=None):
    """Runs the evaluation loop."""
    print('\nEvaluation for model:{} with hparams:{} and class map:{}'.format(
        model_name, hparams, class_map_path))
    print('Evaluation data: clip dir {} and labels {}'.format(
        eval_clip_dir, eval_csv_path))
    print('Checkpoint: {}\n'.format(checkpoint_path))

    with tf.Graph().as_default():
        label_class_index_table, num_classes = inputs.get_class_map(
            class_map_path)
        csv_record = tf.placeholder(tf.string,
                                    [])  # fed during evaluation loop.

        # Use a simpler in-order input pipeline for eval than the one used in
        # training, since we don't want to shuffle examples across clips.
        # The features consist of a batch of all possible framed log mel spectrum
        # examples from the same clip. The labels in this case will contain a batch
        # of identical 1-hot vectors.
        features, labels = inputs.record_to_labeled_log_mel_examples(
            csv_record,
            clip_dir=eval_clip_dir,
            hparams=hparams,
            label_class_index_table=label_class_index_table,
            num_classes=num_classes)

        # Create the model in prediction mode.
        global_step, prediction, _, _ = model.define_model(
            model_name=model_name,
            features=features,
            num_classes=num_classes,
            hparams=hparams,
            training=False)

        with tf.train.SingularMonitoredSession(
                checkpoint_filename_with_path=checkpoint_path) as sess:
            # Read in class map CSV into a class index -> class name map.
            class_map = {
                int(row[0]): row[1]
                for row in csv.reader(open(class_map_path))
            }

            # Keep running counters to aid printing incremental AP stats.
            ap_counts = defaultdict(
                int
            )  # maps class index to the number of clips with that label.
            ap_sums = defaultdict(
                float
            )  # maps class index to the sum of AP for all clips with that label.

            # Read in the validation CSV, skippign the header.
            eval_records = open(eval_csv_path).readlines()[1:]
            # Shuffle the lines so that as we print incremental stats, we get good
            # coverage across classes and get a quick initial impression of how well
            # the model is doing even before evaluation is completely done.
            random.shuffle(eval_records)

            for (i, record) in enumerate(eval_records):
                record = record.strip()
                actual, predicted = sess.run([labels, prediction],
                                             {csv_record: record})

                # By construction, actual consists of identical rows, where each row is
                # the same 1-hot label (because we are looking at features from the same
                # clip). np.argmax() of any of the rows (and in particular [0]) will
                # give us the class index corresponding to the label.
                actual_class = np.argmax(actual[0])

                predicted_classes = get_top_predicted_classes(predicted)

                # Compute AP for this item, update running counters/sums, and print the
                # prediction vs actual.
                ap = avg_precision(actual=actual_class,
                                   predicted=predicted_classes)
                ap_counts[actual_class] += 1
                ap_sums[actual_class] += ap
                print(class_map[actual_class],
                      [class_map[index] for index in predicted_classes], ap)

                # Periodically print per-class and overall AP stats.
                if i % 50 == 0:
                    print_maps(ap_sums=ap_sums,
                               ap_counts=ap_counts,
                               class_map=class_map)
                sys.stdout.flush()

            # Final AP stats.
            print_maps(ap_sums=ap_sums,
                       ap_counts=ap_counts,
                       class_map=class_map)
Beispiel #16
0
def main():
    faces, label = input_faces()
    n_class = len(list(set(label)))
    N_faces = len(faces)

    print(n_class), N_faces

    with tf.Graph().as_default():
        # 入力画像のプレースホルダー
        images = tf.placeholder(tf.float32,
                                [BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, 1])

        # ラベル 0: 1:
        labels = tf.placeholder(tf.int64, shape=(BATCH_SIZE, ))

        # モデルの定義
        model = define_model(images=images)
        # 誤差関数
        loss = define_loss(model, labels=labels)

        # 誤差を保存、TensorBoardで可視化できる。
        tf.scalar_summary("loss", loss)
        # 学習の演算を定義する。
        train_op = training(loss, learning_rate=0.01)
        # 評価方法を定義、正解数を計算する。
        evaluation_op = evaluation(model, labels)
        # TensorBoardへの記載
        tf.image_summary('images', images, max_images=100)

        summary = tf.merge_all_summaries()
        saver = tf.train.Saver()

        # 変数初期化を行う。
        with tf.Session() as session:
            # 全ての変数を初期化する。
            tf.initialize_all_variables().run()
            # 計算グラフを定義
            summary_writer = tf.train.SummaryWriter("./log", session.graph)

            count = 0

            # 学習を行う。 何周学習を行うか
            for epoch in range(EPOCHS):
                perm = np.random.permutation(N_faces)

                loss_value = 0.0

                # バッチ学習を行う。
                for index, step in enumerate(
                        range(0, N_faces - BATCH_SIZE, BATCH_SIZE)):
                    start_time = time.time()
                    batch_start = step
                    batch_end = step + BATCH_SIZE

                    feed_dict = {
                        images: faces[perm[batch_start:batch_end]],
                        labels: label[perm[batch_start:batch_end]]
                    }

                    # ①演算を行う。
                    _, loss_value, eval_value = session.run(
                        [train_op, loss, evaluation_op], feed_dict=feed_dict)

                    duration = time.time() - start_time
                    count += 1

                    print('Step %d: loss = %.2f (%.3f sec)' %
                          (index + epoch * N_faces // BATCH_SIZE, loss_value,
                           duration))
                    summary_str = session.run(summary, feed_dict=feed_dict)
                    summary_writer.add_summary(
                        summary_str, index + epoch * N_faces // BATCH_SIZE)
                    summary_writer.flush()

                # 学習が一周完了した段階で出力する。
                print(epoch, loss_value)

            # ②学習結果を保存する。
            saver.save(session, "model.ckpt")
encoder = LabelEncoder()
onehot = OneHotEncoder(handle_unknown='ignore')

# Fit Transform X and y
X = cv.fit_transform(X).toarray()
print('Shape of the features X : ', X.shape)
y = encoder.fit_transform(y)
y = onehot.fit_transform(y.reshape(-1, 1)).toarray()
print('Shape of the Label y : ', y.shape)

# Train Test Split (0.8, 0.2)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)

n_words = X_train.shape[1]
# Create Model
model = define_model(n_words)

# Train the model.
model.fit(X_train,
          y_train,
          validation_data=(X_test, y_test),
          epochs=100,
          verbose=2)

# Predict Outcome
pred = model.predict(X_test)

print(pred.shape)
print(type(pred))
y_pred = []
for i in range(pred.shape[0]):
Beispiel #18
0
from tensorflow.keras.callbacks import LearningRateScheduler
from model import define_model
import h5py
from sklearn.metrics import log_loss, f1_score

df_model=define_model()
df_model.load_weights('./MesoInception_DF')
f2f_model=define_model()
f2f_model.load_weights('./MesoInception_F2F')

file = h5py.File('train2.h5','r')

X,y,Val_X, Val_y = file['train_imgs'], file['train_gts'],file['test_imgs'],file['test_gts']
y = y[:len(X)]
Val_y = Val_y[:len(Val_X)]
print(X.shape,y.shape,Val_X.shape,Val_y.shape)
lrs=[1e-3,5e-4,1e-4]
def schedule(epoch):
    return lrs[epoch]

LOAD_PRETRAIN=True
import gc
kfolds=5
losses=[]
if LOAD_PRETRAIN:
    import keras.backend as K
    df_models=[]
    f2f_models=[]
    i=0
    while len(df_models)<kfolds:
        model=define_model((150,150,3))
                                                 img_width=img_width)
        validation_images, validation_labels = decode_data(
            val_test_batch,
            validation_filenames,
            'validation',
            img_height=img_height,
            img_width=img_width)

        images_placeholder = tf.placeholder(
            tf.float32, shape=[None, img_height, img_width])
        labels_placeholder = tf.placeholder(tf.int32, shape=None)
        dropout_placeholder = tf.placeholder(tf.bool, shape=())

        # ops
        train_op, loss, accuracy, softmax_logits, merged = define_model(
            images_placeholder, labels_placeholder, dropout_placeholder, eta,
            num_classes)

        # visualization and saving
        saver = tf.train.Saver()
        train_writer = tf.summary.FileWriter(
            'summaries/' + model_name + '/train', sess.graph)
        val_writer = tf.summary.FileWriter('summaries/' + model_name +
                                           '/validation')

        init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())
        sess.run(init_op)

        if os.path.isdir('models/' + model_name):
            saver.restore(sess, 'models/' + model_name + '/model.ckpt')
                    help='optional arguement, needed if training with global features\
                          Of the format [x,y,z] corresponding to voxel dimenstions',required=False)
    parser.add_argument('--train_directory', type=str,
                    help='Directory with pointclouds for training')
    parser.add_argument('--test_directory', type=str,
                    help='Directory with pointclouds for testing')

    args = parser.parse_args()

    if args.train_with_global_features:
        
        voxel_size = args.voxel_size.split(",")
        voxel_size = [int(x) for x in voxel_size]
        
        input_size = (voxel_size[0] * voxel_size[1] * voxel_size[2])+33
        model = define_model("global",input_size)
        
        X_train,y_train=feature_extraction_with_voxel_occupancy(args.train_directory,voxel_size,1000)

        X_test,y_test=feature_extraction_with_voxel_occupancy(args.test_directory,voxel_size,1000)
        
    if args.train_with_local_features:
        
        model=define_model("local",input_size=33)
        
        X_train,y_train=feature_extraction(args.train_directory)

        X_test,y_test=feature_extraction(args.test_directory)

    model.compile(optimizer='adam', loss='binary_crossentropy',metrics=['accuracy'])
                             epoch_to_use)  #Added here

#trained_model = os.path.join('./trained_model',opt.model,'epoch_24.pth')
data_dir = opt.data_dir

#epoch_to_use = 'epoch_'+str(opt.epoch)+'.pth'

#trained_model = '/home/ghumphries/Projects/whale/trained_model/MODEL_NO_W1/'+epoch_to_use
#data_dir = '/home/ghumphries/Projects/whale/Data/fulldata/test/single_whale'

test_transforms = s.data_transforms['test']
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
torch.set_default_tensor_type('torch.cuda.FloatTensor')

#model_ft = torchvision.models.resnet18(pretrained=True)
model_ft = define_model(name=opt.modtype)

## Scrubbed these because they're in model.py now
#num_ftrs = model_ft.fc.in_features
#model_ft.fc = nn.Linear(num_ftrs, 2)

model_ft = model_ft.to(device)

model_ft.load_state_dict(torch.load(trained_model))
model_ft.eval()

#image_dataset = datasets.ImageFolder(data_dir, s.data_transforms['test'])
image_datasets = ImageFolderWithPaths(data_dir, s.data_transforms['test'])
#image_datasets = datasets.ImageFolder(data_dir, s.data_transforms['test'])
dataloaders = torch.utils.data.DataLoader(image_datasets,
                                          batch_size=10,
Beispiel #22
0
import random
from collections import deque

import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')

state_size = [None, 80, 80, 4]
single_image_shape = [1, 80, 80, 4]
disc_rate = 0.99

sess = tf.Session()

with tf.name_scope("active_model"):
    inp = tf.placeholder(dtype=tf.float32, shape=state_size)
    model = define_model(inp)
    action_choice = tf.reshape(tf.multinomial(model.output, num_samples=1),
                               shape=[1])
    greedy_action = tf.argmax(model.output)

with tf.name_scope("frozen_model"):
    inp_frozen = tf.placeholder(dtype=tf.float32, shape=state_size)
    frozen_model = define_model(inp_frozen)
    frozen_model.trainable = False


def loss(a, r):
    with tf.name_scope("loss"):

        rng = tf.reshape(tf.range(tf.shape(a)[0]), shape=[-1])
        indices = tf.stack([rng, a], axis=1)
def predict(model_name=None, hparams=None, test_clip_dir=None,
            class_map_path=None, checkpoint_path=None, predictions_csv_path=None):
        

      soundfile = "The Fast and the Furious.wav" 
      samplingFrequency, signalData = wavfile.read(soundfile)
      start = 0
      end = 1
      count = 0
      while True:
          tmp = signalData[start*samplingFrequency : end*samplingFrequency]
          file_name = "divide_audio/"+str(count).zfill(8) + ".wav"
          write(file_name,44100,tmp)
          count += 1
          start += 1
          end += 1
          if (end * samplingFrequency) > signalData.size:
              start = 0
              end = 1
              break
            
      
      with tf.Graph().as_default():
        _, num_classes = inputs.get_class_map(class_map_path)
        clip = tf.placeholder(tf.string, [])  # Fed during prediction loop.

        # Use a simpler in-order input pipeline without labels for prediction
        # compared to the one used in training. The features consist of a batch of
        # all possible framed log mel spectrum examples from the same clip.
        features = inputs.clip_to_log_mel_examples(
            clip, clip_dir=test_clip_dir, hparams=hparams)

        # Creates the model in prediction mode.
        _, prediction, _, _ = model.define_model(
            model_name=model_name, features=features, num_classes=num_classes,
            hparams=hparams, training=False)


        f = open("Total.txt",'w')
        with tf.train.SingularMonitoredSession(checkpoint_filename_with_path=checkpoint_path) as sess:
            class_map = {int(row[0]): row[1] for row in csv.reader(open(class_map_path))}

            test_clips = sorted(os.listdir(test_clip_dir))


            for (i, test_clip) in enumerate(test_clips):
                print(i, test_clip)
                sys.stdout.flush()

            # Hack to avoid passing empty files through the model.
                if os.path.getsize(os.path.join(test_clip_dir, test_clip)) == 44:
                    print('empty file, skipped model')
                    label = ''
                else:
                    predicted = sess.run(prediction, {clip: test_clip})
                    predicted_classes = evaluation.get_top_predicted_classes(predicted)
                    label = ' '.join([class_map[c] for c in predicted_classes])

                f.write('audio/residential_area/a001.wav\t' + str(start) +"\t" + str(end) + "\t" + label + "\n")
                start += 1
                end += 1
                sys.stdout.flush()

        f.close()

        
            
filename3 = 'C:/Users/admin/Downloads/imageCpation/features.pkl'
test_features = loadData.load_photo_features(filename3, test)
print("Photos: train = %d" % len(test_features))

#prepare tokenizer
#tokenizer = loadData.create_tokenizer(test_descriptions)
#vocab_size = len(tokenizer.word_index) + 1
#print("Vocabulary size : %d" %vocab_size)

#prepare the sequences
X1test, X2test, ytest = loadData.create_sequences(tokenizer, max_length,
                                                  test_descriptions,
                                                  test_features, vocab_size)

#define the model
model = model.define_model(vocab_size, max_length)

#define checkpoint callback
filepath = 'model-ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5'
checkpoint = ModelCheckpoint(filepath,
                             monitor='val_loss',
                             verbose=1,
                             save_best_only=True,
                             mode='min')

#fit the model
model.fit([X1train, X2train],
          ytrain,
          epochs=20,
          verbose=2,
          callbacks=[checkpoint],
Beispiel #25
0
from utils import BS,BASE_DIR
from generator import csv_image_generator
from keras.models import load_model
from keras.metrics import categorical_accuracy
from sklearn.metrics import accuracy_score
from utils import BASE_DIR, encoding
import numpy as np
import cv2
import os
from utils import SAMPLE_DIR
import pickle
from model import define_model

model = define_model()
model.load_weights('model_1.h5')
with open('X_test_1.pkl', 'rb') as f:
    X_test = pickle.load(f)
f.close()
with open('y_test_1.pkl', 'rb') as f:
    y_test = pickle.load(f)
f.close()
y_pred = model.predict(X_test)
y_pred = (y_pred > 0.5)
idx = np.argmax(y_pred, axis=-1)
y_pred = np.zeros(y_pred.shape)
y_pred[np.arange(y_pred.shape[0]), idx] = 1
# acc = categorical_accuracy(y_test, y_pred)
y = []
for i in range(0, len(y_test)):
    lab = [0, 0, 0, 0, 0, 0, 0, 0]
    lab[encoding[y_test[i]] - 1] = 1
from utils import extract_spectrogram_windows
from scipy.stats import mode

dictionary = sorted(os.listdir('downloaded_data'))
model_name = 'model1'
num_classes = len(dictionary)
img_width = 512
img_height = 128

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    images_placeholder = tf.placeholder(tf.float32, shape=[None, img_height, img_width])
    labels_placeholder = tf.placeholder(tf.int32, shape=None)
    dropout_placeholder = tf.placeholder(tf.bool, shape=())

    _, _, _, softmax_logits, _ = define_model(images_placeholder, labels_placeholder, dropout_placeholder, eta=0.001, num_classes=num_classes)
    classes = tf.argmax(softmax_logits, axis=1)

    saver = tf.train.Saver()
    if os.path.isdir('models/' + model_name):
        saver.restore(sess, 'models/' + model_name + '/model.ckpt')
        print('Model read')
    else:
        raise Exception("Model not found! Check if model_name is correct!")

    while True:
        filename = input("Type filename (or 'q' to quit): ")
        path = os.path.join('sample_audio', filename)
        if filename == 'q':
            break
        elif os.path.isfile(path):
Beispiel #27
0
def evaluate(model_name=None,
             hparams=None,
             class_map_path=None,
             eval_csv_path=None,
             eval_clip_dir=None,
             eval_dir=None,
             train_dir=None):
    """Runs the evaluation loop."""
    print('\nEvaluation for model:{} with hparams:{} and class map:{}'.format(
        model_name, hparams, class_map_path))
    print('Evaluation data: clip dir {} and labels {}'.format(
        eval_clip_dir, eval_csv_path))

    # Read in class map CSV into a class index -> class name map.
    class_map = {
        int(row[0]): row[1]
        for row in csv.reader(open(class_map_path))
    }

    with tf.Graph().as_default():
        label_class_index_table, num_classes = inputs.get_class_map(
            class_map_path)
        csv_record = tf.placeholder(tf.string,
                                    [])  # fed during evaluation loop.

        # Use a simpler in-order input pipeline for eval than the one used in
        # training, since we don't want to shuffle examples across clips.
        # The features consist of a batch of all possible framed log mel spectrum
        # examples from the same clip. The labels in this case will contain a batch
        # of identical 1-hot vectors.
        features, labels = inputs.record_to_labeled_log_mel_examples(
            csv_record,
            clip_dir=eval_clip_dir,
            hparams=hparams,
            label_class_index_table=label_class_index_table,
            num_classes=num_classes)

        # Create the model in prediction mode.
        global_step, prediction, _, _ = model.define_model(
            model_name=model_name,
            features=features,
            num_classes=num_classes,
            hparams=hparams,
            training=False)

        # Write evaluation graph to checkpoint directory.
        tf.train.write_graph(
            tf.get_default_graph().as_graph_def(add_shapes=True), eval_dir,
            'eval.pbtxt')

        summary_writer = tf.summary.FileWriter(eval_dir,
                                               tf.get_default_graph())

        # Loop through all checkpoints in the training directory.
        checkpoint_state = tf.train.get_checkpoint_state(train_dir)
        for checkpoint_path in checkpoint_state.all_model_checkpoint_paths:
            checkpoint_num = get_checkpoint_num(checkpoint_path)
            if eval_done(eval_dir, checkpoint_num):
                print("Checkpoint %d already evaluated, skipping" %
                      checkpoint_num)
                continue

            lwlrap = eval_checkpoint(checkpoint_path, eval_dir, summary_writer,
                                     eval_csv_path, class_map, csv_record,
                                     global_step, labels, prediction)
            write_eval_marker(eval_dir, checkpoint_num, lwlrap)
Beispiel #28
0
sess = tf.Session(config=config)
tf.keras.backend.set_session(sess)

global_step = tf.Variable(0, name="global_step", trainable=False)

# Define the shape of the input images
# For segmentation models, the label (mask) is the same shape.
shape = (None, args.dim_length, args.dim_length, args.dim_length,
         args.num_channels)
img = tf.placeholder(tf.float32, shape=shape)  # Input tensor
msk = tf.placeholder(tf.float32, shape=shape)  # Label tensor

# Define the model
# Predict the output mask
preds = define_model(img,
                     learning_rate=args.lr,
                     use_upsampling=args.use_upsampling,
                     print_summary=args.print_model)

#  Performance metrics for model
loss = dice_coef_loss(msk,
                      preds)  # Loss is the dice between mask and prediction
dice_score = dice_coef(msk, preds)
sensitivity_score = sensitivity(msk, preds)
specificity_score = specificity(msk, preds)

train_op = tf.train.AdamOptimizer(args.lr).minimize(loss,
                                                    global_step=global_step)

# Just feed completely random data in for the benchmark testing
imgs = np.random.rand(args.bz, args.dim_length, args.dim_length,
                      args.dim_length, args.num_channels)
        else:
            np.append(X, [image], axis=0)
        y.append(sam)

combined = list(zip(X, y))
random.shuffle(combined)
X[:], y[:] = zip(*combined)
# skf = StratifiedKFold(n_splits=10, shuffle=True)
# skf.get_n_splits(X, y)
X_test = np.array(X)
y_test = np.array(y)

accs = []

model = None
model = define_model()  # 8分类网络、新型网络、深度分离网络
model.summary()

with open("X_train" + ".pkl", 'wb') as f:
    pickle.dump(X_train, f, protocol=4)
f.close()
with open("X_test" + ".pkl", 'wb') as f:
    pickle.dump(X_test, f, protocol=4)
f.close()
with open("y_train" + ".pkl", 'wb') as f:
    pickle.dump(y_train, f, protocol=4)
f.close()
with open("y_test" + ".pkl", 'wb') as f:
    pickle.dump(y_test, f, protocol=4)
f.close()
import cv2

BATCH_SIZE = 1
NUM_CHANNELS = 1
IMAGE_SIZE = 28

cap = cv2.VideoCapture(0)
cascade_path = "INPUT YOUR FACE MODEL PATH"
cascade = cv2.CascadeClassifier(cascade_path)

with tf.Graph().as_default():
    # 入力画像のプレースホルダ
    images = tf.placeholder(tf.float32,
                            [BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, 1])
    # モデル定義
    model = define_model(images=images)
    # 出力定義
    softmax_op = tf.nn.softmax(model)
    # セーバーの初期化
    saver = tf.train.Saver()
    # 変数初期化を行う。
    with tf.Session() as session:
        # モデルの読み出し
        saver.restore(session, "./model.ckpt")

        # ユーザが割り込むまで、実行し続ける。
        while True:
            response, frame = cap.read()
            image_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            scale_height = 512.0 / image_gray.shape[1]
            scale_width = 512.0 / image_gray.shape[0]
Beispiel #31
0
def main(_):

    start_time = datetime.now()
    tf.logging.info("Data path: {}".format(FLAGS.data_path))
    tf.logging.info("Output path: {}".format(FLAGS.output_path))
    tf.logging.info("Starting at: {}".format(start_time))
    tf.logging.info("Batch size: {} images per step".format(FLAGS.batch_size))

    last_epoch_start_time = start_time

    # Load datasets
    imgs_train, msks_train, imgs_test, msks_test = load_datasets(FLAGS)

    if not FLAGS.no_horovod:
        # Initialize Horovod.
        hvd.init()

    # Define model
    model = define_model(imgs_train.shape, msks_train.shape, FLAGS)

    if not FLAGS.no_horovod:
        # Horovod: adjust learning rate based on number workers
        opt = tf.train.AdamOptimizer(learning_rate=FLAGS.learningrate,
                                     epsilon=tf.keras.backend.epsilon())
        #opt = tf.train.RMSPropOptimizer(0.0001 * hvd.size())
        # tf.logging.info("HOROVOD: New learning rate is {}".\
        #         format(FLAGS.learningrate * hvd.size()))
    else:
        opt = tf.train.AdamOptimizer(learning_rate=FLAGS.learningrate,
                                     epsilon=tf.keras.backend.epsilon())
        #opt = tf.train.RMSPropOptimizer(0.0001)

    # Wrap optimizer with Horovod Distributed Optimizer.
    if not FLAGS.no_horovod:
        tf.logging.info("HOROVOD: Wrapped optimizer")
        opt = hvd.DistributedOptimizer(opt)

    global_step = tf.train.get_or_create_global_step()
    train_op = opt.minimize(model["loss"], global_step=global_step)

    train_length = len(imgs_train)
    total_steps = (FLAGS.epochs * train_length) // FLAGS.batch_size
    if not FLAGS.no_horovod:
        last_step = total_steps // hvd.size()
        validation_steps = train_length // FLAGS.batch_size // hvd.size()
    else:
        last_step = total_steps
        validation_steps = train_length // FLAGS.batch_size

    def formatter_log(tensors):
        """
        Format the log output
        """
        if FLAGS.no_horovod:
            logstring = "Step {} of {}: " \
               " training Dice loss = {:.4f}," \
               " training Dice = {:.4f}".format(tensors["step"],
               last_step,
               tensors["loss"], tensors["dice"])
        else:
            logstring = "HOROVOD (Worker #{}), Step {} of {}: " \
               " training Dice loss = {:.4f}," \
               " training Dice = {:.4f}".format(
               hvd.rank(),
               tensors["step"],
               last_step,
               tensors["loss"], tensors["dice"])

        return logstring

    hooks = [
        tf.train.StopAtStepHook(last_step=last_step),

        # Prints the loss and step every log_steps steps
        tf.train.LoggingTensorHook(tensors={
            "step": global_step,
            "loss": model["loss"],
            "dice": model["metric_dice"]
        },
                                   every_n_iter=FLAGS.log_steps,
                                   formatter=formatter_log),
    ]

    # Horovod: BroadcastGlobalVariablesHook broadcasts
    # initial variable states from rank 0 to all other
    # processes. This is necessary to ensure consistent
    # initialization of all workers when training is
    # started with random weights
    # or restored from a checkpoint.
    if not FLAGS.no_horovod:
        hooks.append(hvd.BroadcastGlobalVariablesHook(0))

        # Horovod: save checkpoints only on worker 0 to prevent other workers from
        # corrupting them.
        if hvd.rank() == 0:
            checkpoint_dir = "{}/{}-workers/{}".format(
                FLAGS.output_path, hvd.size(),
                datetime.now().strftime("%Y%m%d-%H%M%S"))
            print(checkpoint_dir)
        else:
            checkpoint_dir = None

    else:
        checkpoint_dir = "{}/no_hvd/{}".format(
            FLAGS.output_path,
            datetime.now().strftime("%Y%m%d-%H%M%S"))

    # The MonitoredTrainingSession takes care of session initialization,
    # restoring from a checkpoint, saving to a checkpoint,
    # and closing when done or an error occurs.
    current_step = 0
    startidx = 0
    epoch_idx = 0

    with tf.train.MonitoredTrainingSession(
            checkpoint_dir=checkpoint_dir,
            hooks=hooks,
            save_summaries_steps=FLAGS.log_steps,
            log_step_count_steps=FLAGS.log_steps,
            config=config) as mon_sess:

        while not mon_sess.should_stop():

            # Run a training step synchronously.
            image_, mask_ = get_batch(imgs_train, msks_train, FLAGS.batch_size)

            # Do batch in order
            # stopidx = startidx + FLAGS.batch_size
            # if (stopidx > train_length):
            #     stopidx = train_length
            #
            # image_ = imgs_train[startidx:stopidx]
            # mask_  = msks_train[startidx:stopidx]

            mon_sess.run(train_op,
                         feed_dict={
                             model["input"]: image_,
                             model["label"]: mask_
                         })

            current_step += 1
            # # Get next batch (loop around if at end)
            # startidx += FLAGS.batch_size
            # if (startidx > train_length):
            #     startidx = 0

    stop_time = datetime.now()
    tf.logging.info("Stopping at: {}".format(stop_time))
    tf.logging.info("Elapsed time was: {}".format(stop_time - start_time))
Beispiel #32
0
#print(len(train))
train_descriptions = u.load_clean_dict(train)
#print(len(train_descriptions))
train_features = u.load_photos('features.pkl', train)
#print(len(train_features))

tokenizer = u.create_tokenizer(train_descriptions)
vocab_size = len(tokenizer.word_index) + 1
#print('Vocabulary Size: %d' % vocab_size)
maxlen = u.max_length(train_descriptions)

train = u.make_set(m.TRAIN_IMG_NAME)
#print('Dataset: %d' % len(train))
train_descriptions = u.load_clean_dict(train)
#print('Descriptions: train=%d' % len(train_descriptions))
train_features = u.load_photos('features.pkl', train)
#print('Photos: train=%d' % len(train_features))
vocab_size = len(tokenizer.word_index) + 1

model = mo.define_model(vocab_size, maxlen)
### Since I already trained this, I am loading a model instead of training once again
model = m.load_model('model_9.h5')
epochs = 10
steps = len(train_descriptions)
### Do uncomment if the model is to be trained again
#for i in range(epochs):
#generator = data_generator(train_descriptions, train_features, tokenizer, max_length, vocab_size)

#model.fit_generator(generator, epochs=1, steps_per_epoch=steps, verbose=1)

#model.save('model_' + str(i) + '.h5')