Exemplo n.º 1
0
#!/usr/bin/python3

import sys, os

import numpy as np

import common

# Load data
prefix = "t10k"
images = common.read_images(prefix)
labels = common.read_labels(prefix)

# Load result
try:
    result_filepath = sys.argv[1]
    result = np.loadtxt(result_filepath).astype("float32")
    result = result.reshape([result.shape[0], 1])
except IndexError:
    print("Usage: " + os.path.basename(__file__) + " <result_filepath>")
    sys.exit(1)

# Get false indices
false_indices = np.argwhere(labels != result)[:, 0]

# Debugging
debug_dir = "debugging"
common.create_dir_if_not_exists(debug_dir)
for i in false_indices:
    common.debug(debug_dir, i, images[i], labels[i][0], result[i][0])
Exemplo n.º 2
0
def main(FLAGS):
  labels = common.read_labels(FLAGS.labels)
  labels = labels[:20]

  # build and compile a Keras ResNet model
  input_shape = tuple(FLAGS.input_shape) + (1,)
  model = tf.keras.applications.ResNet50(
    input_shape=input_shape,
    classes=len(labels)+1,
    weights=None)
  optimizer = tf.train.AdamOptimizer(learning_rate=FLAGS.learning_rate)
  model.compile(
    optimizer=optimizer,
    loss='categorical_crossentropy',
    metrics=['categorical_accuracy'])

  print('Training for {} steps'.format(FLAGS.max_steps))
  print(model.summary())

  # convert this model to a TF Estimator
  config = tf.estimator.RunConfig(
    save_summary_steps=FLAGS.log_steps,
    save_checkpoints_secs=FLAGS.eval_throttle_secs,
    log_step_count_steps=FLAGS.log_steps,
    tf_random_seed=0,
    model_dir=os.path.join(FLAGS.tensorboard_dir, FLAGS.model_name),
    train_distribute=tf.contrib.distribute.MirroredStrategy(
      num_gpus=FLAGS.num_gpus))
  estimator = tf.keras.estimator.model_to_estimator(model, config=config)

  # train our estimator with data from our input_fn
  train_spec = tf.estimator.TrainSpec(
    input_fn=lambda : input_fn(
      FLAGS.train_data,
      labels,
      FLAGS.batch_size,
      FLAGS.num_epochs,
      FLAGS.input_shape,
      stats=FLAGS.pixel_wise_stats),
    max_steps=FLAGS.max_steps) 
  eval_spec = tf.estimator.EvalSpec(
    input_fn=lambda : input_fn(
      FLAGS.valid_data,
      labels,
      FLAGS.batch_size*8,
      1,
      FLAGS.input_shape,
      stats=FLAGS.pixel_wise_stats),
    throttle_secs=FLAGS.eval_throttle_secs)
  tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)

  model_base = os.path.join(FLAGS.model_store_dir, FLAGS.model_name)
  # export our model
  export_base = os.path.join(model_base, str(FLAGS.model_version))
  tf.io.gfile.makedirs(export_base)

  # `export_saved_model` creates a timestamped directory by default
  # need to change it to fit format expected by TRTIS
  export_timestamp = estimator.export_saved_model(
    export_base,
    lambda : serving_input_receiver_fn(
      stats=FLAGS.pixel_wise_stats, spec_shape=FLAGS.input_shape))

  export_dir = os.path.join(export_base, 'model.savedmodel')
  print('Exporting saved_model to {}'.format(export_dir))

  if FLAGS.use_trt:
    print("Accelerating graph for inference with TensorRT")
    trt.create_inference_graph(
      input_graph_def=None,
      outputs=None,
      input_saved_model_dir=export_timestamp,
      input_saved_model_tags=["serve"],
      output_saved_model_dir=export_dir,
      max_batch_size=FLAGS.max_batch_size,
      max_workspace_size_bytes=1<<25,
      precision_mode=FLAGS.trt_precision)
    tf.io.gfile.rmtree(export_timestamp)
  else:
    tf.io.gfile.move(export_timestamp, export_dir)

  # export config.pbtxt and labels.txt for trtis model store
  model_input = model_config_pb2.ModelInput(
    name='audio_input',
    data_type=model_config_pb2.TYPE_FP32,
    dims=[common._SAMPLE_RATE])
  model_output = model_config_pb2.ModelOutput(
    name=model.output.name.split("/")[0],
    data_type=model_config_pb2.TYPE_FP32,
    dims=[len(labels)+1],
    label_filename='labels.txt')
  model_instance_group = model_config_pb2.ModelInstanceGroup(
    count=FLAGS.count)

  model_config = model_config_pb2.ModelConfig(
    name=FLAGS.model_name,
    max_batch_size=FLAGS.max_batch_size,
    platform='tensorflow_savedmodel',
    input=[model_input],
    output=[model_output],
    instance_group=[model_instance_group])

  config_export_path = os.path.join(model_base, 'config.pbtxt')
  print('Exporting model config to {}'.format(config_export_path))
  with tf.io.gfile.GFile(config_export_path, 'wb') as f:
    f.write(str(model_config))

  labels_export_path = os.path.join(model_base, 'labels.txt')
  common.write_labels(labels+['unknown'], labels_export_path)
Exemplo n.º 3
0
import common

# Metadata
batch_size = 128
epochs = 1000
training_dir = "training"
checkpoint_format = "weights.{epoch:04d}-{val_loss:.2f}.h5"
period = 5
shift_range = 2

# Create checkpoint directory if not exists
common.create_dir_if_not_exists(training_dir)

# Load data
images = common.read_images("train")
ori_labels = common.read_labels("train")
new_labels = common.category2binary(ori_labels)

# Load test
test_images = common.read_images("t10k")
test_labels = common.category2binary(common.read_labels("t10k"))

# Create model
model = common.create_model()

# Summary model
print("=" * 80)
model.summary()
input("Press Enter to continue...")
print("=" * 80)