Exemplo n.º 1
0
def main(_):
    model_class = models.get_model_class(FLAGS.model)

    # Look up the model configuration.
    assert (FLAGS.config_name is None) != (FLAGS.config_json is None), (
        "Exactly one of --config_name or --config_json is required.")
    config = (models.get_model_config(FLAGS.model, FLAGS.config_name) if
              FLAGS.config_name else config_util.parse_json(FLAGS.config_json))
    config = configdict.ConfigDict(config)

    # Create the estimator.
    estimator = estimator_util.create_estimator(model_class,
                                                config.hparams,
                                                model_dir=FLAGS.model_dir)

    # Read and process the input features.
    features = _process_tce(config.inputs.features)
    print(type(features))
    print(features)

    # Create an input function.
    def input_fn():
        return {
            "time_series_features":
            tf.estimator.inputs.numpy_input_fn(features,
                                               batch_size=1,
                                               shuffle=False,
                                               queue_capacity=1)()
        }

    # Generate the predictions.
    for predictions in estimator.predict(input_fn):
        assert len(predictions) == 1
        print("Prediction:", predictions[0])
Exemplo n.º 2
0
def main(_):
  model_class = models.get_model_class(FLAGS.model)

  # Look up the model configuration.
  assert (FLAGS.config_name is None) != (FLAGS.config_json is None), (
      "Exactly one of --config_name or --config_json is required.")
  config = (
      models.get_model_config(FLAGS.model, FLAGS.config_name)
      if FLAGS.config_name else config_util.parse_json(FLAGS.config_json))
  config = configdict.ConfigDict(config)

  # Create the estimator.
  estimator = estimator_util.create_estimator(
      model_class, config.hparams, model_dir=FLAGS.model_dir)

  # Read and process the input features.
  features = _process_tce(config.inputs.features)

  # Create an input function.
  def input_fn():
    return tf.data.Dataset.from_tensors({"time_series_features": features})

  # Generate the predictions.
  for predictions in estimator.predict(input_fn):
    assert len(predictions) == 1
    print("Prediction:", predictions[0])
Exemplo n.º 3
0
    def __init__(self, model_name, model_dir, config_name=None):
        """Initializes the DoFn.

    Args:
      model_name: Name of the model class.
      model_dir: Directory containing a model checkpoint.
      config_name: Optional name of the model configuration. If not specified,
        the file 'config.json' in model_dir is used.
    """
        # Look up the model class.
        model_class = models.get_model_class(model_name)

        # Find the latest checkpoint.
        checkpoint_file = tf.train.latest_checkpoint(model_dir)
        if not checkpoint_file:
            raise ValueError(
                "No checkpoint file found in: {}".format(model_dir))

        # Get the model configuration.
        if config_name:
            config = models.get_model_config(model_name, config_name)
        else:
            with tf.gfile.Open(os.path.join(model_dir, "config.json")) as f:
                config = json.load(f)
        config = configdict.ConfigDict(config)

        self.model_class = model_class
        self.checkpoint_file = checkpoint_file
        self.config = config
Exemplo n.º 4
0
def main(_):
  model_class = models.get_model_class(FLAGS.model)

  # Look up the model configuration.
  assert (FLAGS.config_name is None) != (FLAGS.config_json is None), (
      "Exactly one of --config_name or --config_json is required.")
  config = (
      models.get_model_config(FLAGS.model, FLAGS.config_name)
      if FLAGS.config_name else config_util.parse_json(FLAGS.config_json))

  config = configdict.ConfigDict(config)

  # Create the estimator.
  estimator = estimator_util.create_estimator(
      model_class, config.hparams, model_dir=FLAGS.model_dir)

  # Create an input function that reads the evaluation dataset.
  input_fn = estimator_util.create_input_fn(
      file_pattern=FLAGS.eval_files,
      input_config=config.inputs,
      mode=tf.estimator.ModeKeys.EVAL)

  # Run evaluation. This will log the result to stderr and also write a summary
  # file in the model_dir.
  estimator_util.evaluate(estimator, input_fn, eval_name=FLAGS.eval_name)
Exemplo n.º 5
0
def main(_):
    config = models.get_model_config(FLAGS.model, FLAGS.config_name)

    model_class = models.get_model_class(FLAGS.model)
    model = model_class(config)

    train(model, config)
def main(_):
    # Look up the model class.
    model_class = models.get_model_class(FLAGS.model)

    # Look up the model configuration.
    if (FLAGS.config_name is None) == (FLAGS.config_json is None):
        raise ValueError("Exactly one of config_name or config_json is required.")
    config = (
        models.get_model_config(FLAGS.model, FLAGS.config_name)
        if FLAGS.config_name else config_util.parse_json(FLAGS.config_json))
    config = configdict.ConfigDict(config)

    if FLAGS.average:
        model_dirs = glob.glob(FLAGS.model_dir+'/*')
    else:
        model_dirs = [FLAGS.model_dir]

    y_pred = defaultdict(list)
    for model_dir in model_dirs:
        # append a new prediction to y_pred for each TCE every time
        y_pred = predict(model_dir, config, model_class, y_pred)

    y_pred_average = []
    for tce in y_pred:
        average_pred = np.mean(y_pred[tce])
        y_pred_average.append([tce, average_pred])

    np.savetxt('prediction_'+FLAGS.suffix+'.txt', np.array(y_pred_average), fmt=['%d', '%4.3f'])
Exemplo n.º 7
0
def main(_):
  model_class = models.get_model_class(FLAGS.model)

  # Look up the model configuration.
  assert (FLAGS.config_name is None) != (FLAGS.config_json is None), (
      "Exactly one of --config_name or --config_json is required.")
  config = (
      models.get_model_config(FLAGS.model, FLAGS.config_name)
      if FLAGS.config_name else config_util.parse_json(FLAGS.config_json))
  config = configdict.ConfigDict(config)

  # Create the estimator.
  estimator = estimator_util.create_estimator(
      model_class, config.hparams, model_dir=FLAGS.model_dir)

  # Read and process the input features.
  features = _process_tce(config.inputs.features)

  # Create an input function.
  def input_fn():
    return {
        "time_series_features":
            tf.estimator.inputs.numpy_input_fn(
                features, batch_size=1, shuffle=False, queue_capacity=1)()
    }

  # Generate the predictions.
  for predictions in estimator.predict(input_fn):
    assert len(predictions) == 1
    print("Prediction:", predictions[0])
Exemplo n.º 8
0
def main(_):
  model_class = models.get_model_class(FLAGS.model)

  # Look up the model configuration.
  assert (FLAGS.config_name is None) != (FLAGS.config_json is None), (
      "Exactly one of --config_name or --config_json is required.")
  config = (
      models.get_model_config(FLAGS.model, FLAGS.config_name)
      if FLAGS.config_name else config_util.parse_json(FLAGS.config_json))

  config = configdict.ConfigDict(config)

  # Create the estimator.
  estimator = estimator_util.create_estimator(
      model_class, config.hparams, model_dir=FLAGS.model_dir)
  
  # Print no. of trainable parameters to console.
  var_names = [v for v in estimator.get_variable_names()]
  n_params = np.sum([len(estimator.get_variable_value(v).flatten()) for v in var_names])
  print("Trainable parameters in model:", int(n_params))
  
  # Create an input function that reads the evaluation dataset.
  input_fn = estimator_util.create_input_fn(
      file_pattern=FLAGS.eval_files,
      input_config=config.inputs,
      mode=tf.estimator.ModeKeys.EVAL)

  # Run evaluation. This will log the result to stderr and also write a summary
  # file in the model_dir.
  estimator_util.evaluate(estimator, input_fn, eval_name=FLAGS.eval_name)
Exemplo n.º 9
0
def main(_):
  config = models.get_model_config(FLAGS.model, FLAGS.config_name)

  model_class = models.get_model_class(FLAGS.model) 
  study = study_config(config)

  client = initialize_client()
  print('Study:')
  pprint.pprint(create_study(client, study))

  tune(client, model_class, config, FLAGS.ensemble_count)
    
  print('All done. Study name:', study_name())
def main(_):
  model_class = models.get_model_class(FLAGS.model)

  # Look up the model configuration.
  assert (FLAGS.config_name is None) != (FLAGS.config_json is None), (
      "Exactly one of --config_name or --config_json is required.")
  config = (
      models.get_model_config(FLAGS.model, FLAGS.config_name)
      if FLAGS.config_name else config_util.parse_json(FLAGS.config_json))

  config = configdict.ConfigDict(config)
  config_util.log_and_save_config(config, FLAGS.model_dir)

  # Create the estimator.
  run_config = tf.estimator.RunConfig(keep_checkpoint_max=1)
  estimator = estimator_util.create_estimator(model_class, config.hparams,
                                              run_config, FLAGS.model_dir)

  # Create an input function that reads the training dataset. We iterate through
  # the dataset once at a time if we are alternating with evaluation, otherwise
  # we iterate infinitely.
  train_input_fn = estimator_util.create_input_fn(
      file_pattern=FLAGS.train_files,
      input_config=config.inputs,
      mode=tf.estimator.ModeKeys.TRAIN,
      shuffle_values_buffer=FLAGS.shuffle_buffer_size,
      repeat=1 if FLAGS.eval_files else None)

  if not FLAGS.eval_files:
    estimator.train(train_input_fn, max_steps=FLAGS.train_steps)
  else:
    eval_input_fn = estimator_util.create_input_fn(
        file_pattern=FLAGS.eval_files,
        input_config=config.inputs,
        mode=tf.estimator.ModeKeys.EVAL)
    eval_args = {
        "val": (eval_input_fn, None)  # eval_name: (input_fn, eval_steps)
    }

    for _ in estimator_runner.continuous_train_and_eval(
        estimator=estimator,
        train_input_fn=train_input_fn,
        eval_args=eval_args,
        train_steps=FLAGS.train_steps):
      # continuous_train_and_eval() yields evaluation metrics after each
      # training epoch. We don't do anything here.
      pass
def main(_):
    model_class = models.get_model_class(FLAGS.model)

    # Look up the model configuration.
    assert (FLAGS.config_name is None) != (FLAGS.config_json is None), (
        "Exactly one of --config_name or --config_json is required.")
    config = (models.get_model_config(FLAGS.model, FLAGS.config_name) if
              FLAGS.config_name else config_util.parse_json(FLAGS.config_json))
    config = configdict.ConfigDict(config)

    # Create the estimator.
    estimator = estimator_util.create_estimator(model_class,
                                                config.hparams,
                                                model_dir=FLAGS.model_dir)

    # Read and process the input features.
    tce_table = pd.read_csv(FLAGS.input_tce_csv_file,
                            header=0,
                            usecols=[0, 1, 2, 3, 7, 8, 9, 10, 11, 12, 13, 18],
                            dtype={
                                'Sectors': int,
                                'camera': int,
                                'ccd': int
                            })

    for ind, tce in tce_table.iterrows():
        features = _process_tce(config.inputs.features, tce)

        # Create an input function.
        def input_fn():
            return {
                "time_series_features":
                tf.compat.v1.estimator.inputs.numpy_input_fn(
                    features, batch_size=1, shuffle=False, queue_capacity=1)()
            }

        # Generate the predictions.
        for predictions in estimator.predict(input_fn):
            assert len(predictions) == 1
            print(tce.tic_id, "Prediction:", predictions[0])

            print(str(tce.tic_id) + ' ' + str(predictions[0]),
                  file=open(FLAGS.output_file, 'a'))
Exemplo n.º 12
0
def main(_):
    # Look up the model class.
    model_class = models.get_model_class(FLAGS.model)

    # Look up the model configuration.
    if (FLAGS.config_name is None) == (FLAGS.config_json is None):
        raise ValueError(
            "Exactly one of config_name or config_json is required.")
    config = (models.get_model_config(FLAGS.model, FLAGS.config_name) if
              FLAGS.config_name else config_util.parse_json(FLAGS.config_json))
    config = configdict.ConfigDict(config)

    if FLAGS.average:
        model_dirs = glob.glob(FLAGS.model_dir + '/*')
    else:
        model_dirs = [FLAGS.model_dir]

    y_pred = defaultdict(list)
    true_disp = defaultdict(list)
    for model_dir in model_dirs:
        # append a new prediction to y_pred for each TCE every time
        y_pred, true_disp = predict(model_dir, config, model_class, y_pred,
                                    true_disp)

    y_pred_average = []
    is_pc = []
    cnt = 0
    num_tces = len(y_pred)

    for tce in y_pred:
        # Plotting takes quite long. There's probably a better way to do it.
        if cnt % 100 == 0:
            tf.logging.info("Averaging %d of %d", cnt, num_tces)

        disposition = true_disp[tce]
        y_true = disposition in ['PC', 'EB']
        is_pc.append(disposition == 'PC')
        average_pred = np.mean(y_pred[tce])

        if average_pred >= 0.1:
            label = "PC/EB"
        else:
            label = "junk"

        if FLAGS.plot and disposition == 'PC' and average_pred < 0.1:
            kepid, sector = tce.split('-')
            ex = find_tce(int(kepid), int(sector))
            plot_tce(ex.features.feature["tic_id"].int64_list.value[0],
                     ex.features.feature['Sectors'].int64_list.value[0], label,
                     average_pred)

        y_pred_average.append([y_true, average_pred])
        cnt += 1

    threshold = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
    y_true = np.array(y_pred_average)[:, 0]
    y_pred = np.array(y_pred_average)[:, 1]
    is_pc = np.array(is_pc)

    for t in threshold:
        tp = len(np.where((y_true == 1) & (y_pred >= t))[0])
        fp = len(np.where((y_true == 0) & (y_pred >= t))[0])
        fn = len(np.where((y_true == 1) & (y_pred < t))[0])
        tn = len(np.where((y_true == 0) & (y_pred < t))[0])
        precision = float(tp) / (tp + fp)
        recall = float(tp) / (tp + fn)
        print(1 - float(fp) / (tn + fp))

        num_pc = len(np.where((is_pc == True) & (y_pred < t))[0])

        print(
            "Threshold %s: precision=%s, recall=%s. Number of PCs in FNs = %s"
            % (t, precision, recall, num_pc))

    np.savetxt('true_vs_pred_' + FLAGS.suffix + '.txt',
               np.array(y_pred_average),
               fmt=['%f', '%4.3f'])
    pc_count = len(np.where(is_pc == True)[0])
    print('Total %s PCs' % pc_count)
Exemplo n.º 13
0
def create_config(model_name="AstroCNNModel", config_name="local_global"):
    config = (models.get_model_config(model_name, config_name)
              if config_name else config_util.parse_json("config_json"))
    return configdict.ConfigDict(config)