示例#1
0
def do_inference(hostport, work_dir, concurrency, num_tests):
  """Tests PredictionService with concurrent requests.

  Args:
    hostport: Host:port address of the PredictionService.
    work_dir: The full path of working directory for test data set.
    concurrency: Maximum number of concurrent requests.
    num_tests: Number of test images to use.

  Returns:
    The classification error rate.

  Raises:
    IOError: An error occurred processing test data set.
  """
  test_data_set = mnist_input_data.read_data_sets(work_dir).test
  channel = grpc.insecure_channel(hostport)
  stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
  result_counter = _ResultCounter(num_tests, concurrency)
  for _ in range(num_tests):
    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'mnist'
    request.model_spec.signature_name = 'predict_images'
    image, label = test_data_set.next_batch(1)
    request.inputs['images'].CopyFrom(
        tf.contrib.util.make_tensor_proto(image[0], shape=[1, image[0].size]))
    result_counter.throttle()
    result_future = stub.Predict.future(request, 5.0)  # 5 seconds
    result_future.add_done_callback(
        _create_rpc_callback(label[0], result_counter))
  return result_counter.get_error_rate()
示例#2
0
def do_inference(hostport, work_dir, concurrency, num_tests):
    """Tests PredictionService with concurrent requests.
  Args:
    hostport: Host:port address of the PredictionService.
    work_dir: The full path of working directory for test data set.
    concurrency: Maximum number of concurrent requests.
    num_tests: Number of test images to use.
  Returns:
    The classification error rate.
  Raises:
    IOError: An error occurred processing test data set.
  """
    test_data_set = mnist_input_data.read_data_sets(work_dir).test
    channel = grpc.insecure_channel(hostport)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    result_counter = _ResultCounter(num_tests, concurrency)
    for _ in range(num_tests):
        request = predict_pb2.PredictRequest()
        request.model_spec.name = 'mnist'
        request.model_spec.signature_name = 'predict_images'
        image, label = test_data_set.next_batch(1)
        request.inputs['images'].CopyFrom(
            tf.contrib.util.make_tensor_proto(image[0],
                                              shape=[1, image[0].size]))
        result_counter.throttle()
        result_future = stub.Predict.future(request, 5.0)  # 5 seconds
        result_future.add_done_callback(
            _create_rpc_callback(label[0], result_counter))
    return result_counter.get_error_rate()
示例#3
0
    def __init__(self, input_dir, stop_at_record=-1, random=False):
        mnist_data = mnist_input_data.read_data_sets(input_dir, one_hot=True)

        self._samples = mnist_data.test.images
        self._labels = mnist_data.test.labels
        self._input_records = mnist_data.test.num_examples
        super(MnistStreamInput, self).__init__(self._input_records,
                                               stop_at_record, random)
示例#4
0
def main(_):
  # thread_num_array = [1, 2, 4, 8, 16, 32, 64, 128]
  # batch_size_2d_array = [[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024],
  #                        [1, 2, 4, 8, 16, 32, 64, 128, 256, 512],
  #                        [1, 2, 4, 8, 16, 32, 64, 128, 256],
  #                        [1, 2, 4, 8, 16, 32, 64, 128],
  #                        [1, 2, 4, 8, 16, 32, 64],
  #                        [1, 2, 4, 8, 16, 32],
  #                        [1, 2, 4, 8, 16],
  #                        [1, 2, 4, 8]]

  thread_num_array = [1, 2]
  batch_size_2d_array = [[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024],
                          [1, 2 ,4, 8, 16, 32, 64, 128, 256, 512]]

  test_data_set = mnist_input_data.read_data_sets(FLAGS.work_dir).train

  for i in range(len(thread_num_array)):
    thread_num = thread_num_array[i]
    batch_size_arrary = batch_size_2d_array[i]
    for batch_size in batch_size_arrary:
      time.sleep(2.0)

      image, label = test_data_set.next_batch(batch_size)

      run_num = FLAGS.num_tests / (thread_num * batch_size)
      # print("[INFO] thread_num = %d, batch_size = %d, run_num = %d" % (thread_num, batch_size, run_num))

      # warmup
      warmup_num = 3
      for i in range(warmup_num):
        start = time.time()
        do_inference(FLAGS.server, batch_size, 1, image, label, 0)
        end = time.time()
        duration = end - start
        # print("warmup duration = %f" % duration)

      # raw_input("Press Enter to continue...")

      start = time.time()

      thread_pool = []
      for i in range(thread_num):
        t = threading.Thread(target = do_inference, args = (FLAGS.server, batch_size, run_num, image, label, i))
        thread_pool.append(t)
        t.start()

      for t in thread_pool:
        t.join()

      end = time.time()
      print("overall duration for b = %3d, p = %3d is %.3f" % (batch_size, thread_num, end - start))
def main():
    n_out = 10
    learning_rate=0.01
    n_iter=10000
    batch_size=20
    validate_frequency = 100
    hiddenlayer_params = [500]
    activate_func = tf.tanh
    if 1:
        mnist = mnist_input_data.read_data_sets("../data/MNIST_data/", one_hot=True)
        model = MLP(mnist, n_out, learning_rate, n_iter, batch_size, \
            validate_frequency, hiddenlayer_params, activate_func)
        model.train_mnist()
def do_inference(hostport, work_dir, concurrency, num_tests):
    """Tests PredictionService with concurrent requests.

  Args:
    hostport: Host:port address of the PredictionService.
    work_dir: The full path of working directory for test data set.
    concurrency: Maximum number of concurrent requests.
    num_tests: Number of test images to use.

  Returns:
    The classification error rate.

  Raises:
    IOError: An error occurred processing test data set.
  """
    test_data_set = mnist_input_data.read_data_sets(work_dir).test
    channel = grpc.insecure_channel(hostport)
    #print('hostport:\n%s%%' % hostport);
    #print('channel:\n%s%%' % channel);
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    result_counter = _ResultCounter(num_tests, concurrency)
    for _ in range(num_tests):
        request = predict_pb2.PredictRequest()
        request.model_spec.name = 'mnist'
        request.model_spec.signature_name = 'predict_images'
        image, label = test_data_set.next_batch(1)
        #print('image base64 str:\n%s%%' % base64.encodestring(image[0]));
        request.inputs['images'].CopyFrom(
            tf.contrib.util.make_tensor_proto(image[0],
                                              shape=[1, image[0].size]))
        #print('request:\n%s%%' % request);
        #print('request:\n%s%%' % json.dumps(request));
        result_counter.throttle()
        #print('stub:\n%s%%' % stub);
        #print('stub.Predict:\n%s%%' % stub.Predict);
        result_future = stub.Predict.future(request, 5.0)  # 5 seconds
        #print('result_future:\n%s%%' % result_future);
        print('\n')
        print('label%s%%' % label)
        print('result_future.socres%s%%' %
              numpy.array(result_future.result().outputs['scores'].float_val))
        print('\n')
        result_future.add_done_callback(
            _create_rpc_callback(label[0], result_counter))
    return result_counter.get_error_rate()
示例#7
0
def do_inference(hostport, work_dir, concurrency, num_tests):
    test_data_set = mnist_input_data.read_data_sets(work_dir).test
    channel = grpc.insecure_channel(hostport)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    result_counter = _ResultCounter(num_tests, concurrency)
    for _ in range(num_tests):
        request = predict_pb2.PredictRequest()
        request.model_spec.name = 'my_model'
        request.model_spec.signature_name = 'predict_images'
        image, label = test_data_set.next_batch(1)
        request.inputs['images'].CopyFrom(
            tf.contrib.util.make_tensor_proto(image[0],
                                              shape=[1, image[0].size]))
        result_counter.throttle()
        result_future = stub.Predict.future(request, 5.0)  # 5 seconds
        result_future.add_done_callback(
            _create_rpc_callback(label[0], result_counter))
    return result_counter.get_error_rate()
示例#8
0
def do_inference(hostport, work_dir, concurrency, batch_size, num_tests):
    """Tests PredictionService with concurrent requests.

  Args:
    hostport: Host:port address of the PredictionService.
    work_dir: The full path of working directory for test data set.
    concurrency: Maximum number of concurrent requests.
    num_tests: Number of test images to use.

  Returns:
    The classification error rate.

  Raises:
    IOError: An error occurred processing test data set.
  """
    test_data_set = mnist_input_data.read_data_sets(work_dir).test

    channel = grpc.insecure_channel(hostport)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

    result_counter = _ResultCounter(num_tests, concurrency)

    for _ in range(num_tests / batch_size):
        request = predict_pb2.PredictRequest()
        request.model_spec.name = 'mnist'
        request.model_spec.signature_name = 'predict_images'
        image, label = test_data_set.next_batch(batch_size)

        request.inputs['images'].CopyFrom(
            tf.contrib.util.make_tensor_proto(image, shape=image.shape))

        result = stub.Predict(request, 10.0)
        response = tensor_util.MakeNdarray(result.outputs['scores'])

        for i in range(len(label)):
            this_label = label[i]
            this_prediction = numpy.argmax(response[i])
            if this_label != this_prediction:
                result_counter.inc_error()
            result_counter.inc_done()
            result_counter.dec_active()

    return result_counter.get_error_rate()
示例#9
0
def train(sess):
    images = tf.placeholder('float', shape=[None, 28, 28, 1], name='images')
    x = tf.reshape(images, [-1, 784])
    y_ = tf.placeholder('float', shape=[None, 10])
    w = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))

    sess.run(tf.global_variables_initializer())
    scores = tf.nn.softmax(tf.matmul(x, w) + b, name='scores')
    cross_entropy = -tf.reduce_sum(y_ * tf.log(scores))
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(
        cross_entropy)

    mnist = mnist_input_data.read_data_sets(FLAGS.work_dir, one_hot=True)
    for _ in range(FLAGS.training_iteration):
        batch = mnist.train.next_batch(50)
        train_step.run(feed_dict={x: batch[0], y_: batch[1]})

    return (images, scores)
示例#10
0
def do_inference(hostport, work_dir, concurrency, num_tests):
    """Tests PredictionService over Tensor-Bridge.

  Args:
    hostport: Host:port address of the PredictionService.
    work_dir: The full path of working directory for test data set.
    concurrency: Maximum number of concurrent requests.
    num_tests: Number of test images to use.

  Returns:
    The classification error rate.

  Raises:
    IOError: An error occurred processing test data set.
  """
    test_data_set = mnist_input_data.read_data_sets(work_dir).test
    error = 0
    for _ in range(num_tests):
        request = predict_pb2.PredictRequest()
        request.model_spec.name = 'mnist'
        request.model_spec.signature_name = 'predict_images'
        image, label = test_data_set.next_batch(1)
        request.inputs['images'].CopyFrom(
            tf.contrib.util.make_tensor_proto(image[0],
                                              shape=[1, image[0].size]))

        response = requests.post(
            'http://' + hostport + '/tensor-bridge/v1/prediction',
            json=MessageToDict(request,
                               preserving_proto_field_name=True,
                               including_default_value_fields=True))

        result = ParseDict(response.json(),
                           predict_pb2.PredictResponse(),
                           ignore_unknown_fields=True)

        scores = numpy.array(result.outputs['scores'].float_val)
        prediction = numpy.argmax(scores)

        if label[0] != prediction:
            error += 1

    return error / num_tests
def main(_):
  # if FLAGS.num_tests > 10000:
  #   print('num_tests should not be greater than 10k')
  #   return
  # if not FLAGS.server:
  #   print('please specify server host:port')
  #   return
  # do_inference(FLAGS.server, FLAGS.work_dir,
  #                           FLAGS.concurrency, FLAGS.num_tests)


  hostport = FLAGS.server
  work_dir = FLAGS.work_dir
  concurrency = FLAGS.concurrency
  num_tests = FLAGS.num_tests

  test_data_set = mnist_input_data.read_data_sets(work_dir).test

  host, port = hostport.split(':')
  channel = implementations.insecure_channel(host, int(port))
  stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

  myFuncWarmUp(stub, 0, test_data_set)

  num_tests = 0
  tPool = []
  for i in range(num_tests):
    tPool.append(threading.Thread(target = myFuncParallel, args = (stub, i, test_data_set)))

  start = time.time()
  for i in range(num_tests):
    t = tPool[i]
    t.start()
    # time.sleep(2.0)

  for i in range(num_tests):
    t = tPool[i]
    t.join()

  end = time.time()

  print('\nFinished!')
  print('[Parallel] The total running time to run %d concurrent jobs is %s' % (num_tests, str(end - start)))
def do_inference(hostport, work_dir, concurrency, num_tests):
    test_data_set = mnist_input_data.read_data_sets(work_dir).test
    fs = []
    fl = []
    for _ in range(num_tests):
        image, label = test_data_set.next_batch(1)
        r = image[0].tolist()
        data = {
            "signature_name": "predict_images",
            "instances": [{
                "images": r
            }]
        }
        data1 = json.dumps(data)
        url = "http://localhost:8501/v1/models/my_model:predict"
        response = requests.post(url, data=data1)
        abcd = response.json()
        fs.append(numpy.argmax(abcd['predictions'][0]))
        fl.append(label)
    print(fs)
    return accuracy_score(fs, fl)
示例#13
0
def main(_):
    #if FLAGS.num_tests > 10000:
    #  print('num_tests should not be greater than 10k')
    #  return
    #if not FLAGS.server:
    #  print('please specify server host:port')
    #  return
    #error_rate = do_inference(FLAGS.server, FLAGS.work_dir,
    #                          FLAGS.concurrency, FLAGS.num_tests)
    #print('\nInference error rate: %s%%' % (error_rate * 100))

    test_data = mnist_input_data.read_data_sets(FLAGS.work_dir).test

    credentials = {}
    credentials['email'] = "*****@*****.**"
    credentials['password'] = "******"

    login_request = requests.post(
        'https://localhost:8181/hopsworks-api/api/auth/login',
        data=credentials,
        verify=False)

    token = login_request.headers['Authorization']

    thread_requests = FLAGS.num_tests / FLAGS.concurrency
    threads = []

    start = time.time()

    for i in range(0, FLAGS.concurrency):
        thread = threading.Thread(target=do_inference,
                                  args=(thread_requests, token, test_data))
        thread.start()
        threads.append(thread)

    for t in threads:
        t.join()

    end = time.time()
    print("Time:", end - start)
示例#14
0
def main(_):
    if not FLAGS.server:
        print('please specify server host:port')
        return
    if not FLAGS.model_name:
        print('please specify model_name')
        return

    test_data_set = mnist_input_data.read_data_sets(FLAGS.work_dir).test
    image, label = test_data_set.next_batch(1)

    start = time.time()
    print("The time start predicting: {}".format(start))

    do_inference(FLAGS.server, FLAGS.num_tests, image, label)

    end = time.time()
    duration = end - start
    qps = FLAGS.num_tests / duration
    print("\nThe time end predicting: {}".format(end))
    print("Total predict time: {}(s)".format(duration))
    print("Total predict num: {}".format(FLAGS.num_tests))
    print("qps: {}".format(qps))
示例#15
0
def do_inference(hostport, work_dir, batch_size, run_num, thread_id):
    durationSum = 0.0

    # test_data_set = mnist_input_data.read_data_sets(work_dir).test
    test_data_set = mnist_input_data.read_data_sets(work_dir).train

    channel = grpc.insecure_channel(hostport)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

    for i in range(run_num):
        start = time.time()
        request = predict_pb2.PredictRequest()
        request.model_spec.name = 'mnist'
        request.model_spec.signature_name = 'predict_images'
        image, label = test_data_set.next_batch(batch_size)

        request.inputs['images'].CopyFrom(
            tf.contrib.util.make_tensor_proto(image, shape=image.shape))

        result = stub.Predict(request, 10.0)
        end = time.time()
        duration = end - start
        durationSum += duration
示例#16
0
def main(_):
    if not FLAGS.server:
        print('please specify server host:port')
        return
    if not FLAGS.model_name:
        print('please specify model_name')
        return

    test_data_set = mnist_input_data.read_data_sets(FLAGS.work_dir).test
    image, label = test_data_set.next_batch(2)
    #print(image, label)
    #print(image.shape)
    #return
    #import ipdb; ipdb.set_trace()
    #do_inference(0, FLAGS.server, FLAGS.num_tests, image, label)

    pool = multiprocessing.Pool(processes=FLAGS.concurrency)
    results = []
    start_time = time.time()
    print("The time start predicting: {}".format(start_time))
    for process_num in range(FLAGS.concurrency):
        pool.apply_async(
            do_inference,
            (process_num, FLAGS.server, FLAGS.num_tests, image, label))
    pool.close()
    pool.join()

    global lock, finish_time, real_test_num
    with lock:
        duration = finish_time.value - start_time
        #total_num_tests = FLAGS.num_tests * FLAGS.concurrency
        total_num_tests = real_test_num.value
        qps = total_num_tests / duration
        print("The time end predicting: {}".format(finish_time.value))
        print("Total predict time: {}(s)".format(duration))
        print("Total predict num: {}".format(total_num_tests))
        print("qps: {}".format(qps))
示例#17
0
def main(_):
    # if len(sys.argv) < 2 or sys.argv[-1].startswith('-'):
    #     print('Usage: mnist_export.py [--training_iteration=x] '
    #           '[--model_version=y] export_dir')
    #     sys.exit(-1)
    if FLAGS.training_iteration <= 0:
        print('Please specify a positive value for training iteration.')
        sys.exit(-1)
    if FLAGS.model_version <= 0:
        print('Please specify a positive value for version number.')
        sys.exit(-1)

    # Train model
    print('Training model...')
    mnist = mnist_input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
    sess = tf.InteractiveSession()
    serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
    feature_configs = {'x': tf.FixedLenFeature(shape=[784], dtype=tf.float32), }
    tf_example = tf.parse_example(serialized_tf_example, feature_configs)
    x = tf.identity(tf_example['x'], name='x')  # use tf.identity() to assign name
    y_ = tf.placeholder('float', shape=[None, 10])
    w = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    sess.run(tf.global_variables_initializer())
    y = tf.nn.softmax(tf.matmul(x, w) + b, name='y')
    cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
    values, indices = tf.nn.top_k(y, 10)
    table = tf.contrib.lookup.index_to_string_table_from_tensor(
        tf.constant([str(i) for i in xrange(10)]))
    prediction_classes = table.lookup(tf.to_int64(indices))
    for _ in range(FLAGS.training_iteration):
        batch = mnist.train.next_batch(50)
        train_step.run(feed_dict={x: batch[0], y_: batch[1]})
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
    print('training accuracy %g' % sess.run(
        accuracy, feed_dict={
            x: mnist.test.images,
            y_: mnist.test.labels
        }))
    print('Done training!')
    print(sess.graph.as_graph_def())


    # Export model
    export_path_base = FLAGS.model_dir
    # path是路径+版本号,下次导出新版本的模型时,要把版本号设置成比这个大的数字
    export_path = os.path.join(
        tf.compat.as_bytes(export_path_base),
        tf.compat.as_bytes(str(FLAGS.model_version)))
    print('Exporting trained model to', export_path)

    # SavedModelBuilder saves a "snapshot" of the trained model to reliable storage so that it can be loaded later for inference.
    builder = tf.saved_model.builder.SavedModelBuilder(export_path)

    # Build the signature_def_map.
    classification_inputs = tf.saved_model.utils.build_tensor_info(
        serialized_tf_example)
    classification_outputs_classes = tf.saved_model.utils.build_tensor_info(
        prediction_classes)
    classification_outputs_scores = tf.saved_model.utils.build_tensor_info(values)

    # Define classification signature.
    classification_signature = (
        tf.saved_model.signature_def_utils.build_signature_def(
            inputs={
                tf.saved_model.signature_constants.CLASSIFY_INPUTS:
                    classification_inputs
            },
            outputs={
                tf.saved_model.signature_constants.CLASSIFY_OUTPUT_CLASSES:
                    classification_outputs_classes,
                tf.saved_model.signature_constants.CLASSIFY_OUTPUT_SCORES:
                    classification_outputs_scores
            },
            method_name=tf.saved_model.signature_constants.CLASSIFY_METHOD_NAME))

    # =========================================================================== #
    # Define prediction signature
    # images and scores are tensor alias names, they can be whatever unique strings you want
    # they will become the logical names of tensor x and y that you refer to for tensor binding when sending prediction requests later.
    # =========================================================================== #
    tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
    tensor_info_y = tf.saved_model.utils.build_tensor_info(y)

    prediction_signature = (
        tf.saved_model.signature_def_utils.build_signature_def(
            inputs={'images': tensor_info_x},
            outputs={'scores': tensor_info_y},
            method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))

    legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')

    # tags is the set of tags with which to save the meta graph.
    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.tag_constants.SERVING],
        signature_def_map={
            'predict_images':
                prediction_signature,
            tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
                classification_signature,
        },
        legacy_init_op=legacy_init_op)

    builder.save()

    print('Done exporting!')
示例#18
0
        test_data_set = mnist_input_data.read_data_sets("model/data").test

        print (len(test_data_set.images))
        print(len(test_data_set.labels))

        predictor = tf.argmax(y, 1)

        acc, predictions = sess.run([accuracy, predictor], feed_dict={
            x: test_data_set.images
        })

        print('Accuracy', acc)
        print('Predictions', predictions)
        '''
        from tensorflow.core.framework import types_pb2, tensor_shape_pb2
        test_data_set = mnist_input_data.read_data_sets("model/data").test
        inputs = {
            'x':
            tf.TensorInfo(name='x:0',
                          dtype=types_pb2.DT_FLOAT,
                          tensor_shape=tensor_shape_pb2.TensorShapeProto())
        }
        outputs = {'y': tf.TensorInfo(name='y:0', dtype=types_pb2.DT_FLOAT)}
        signature_def = tf.saved_model.signature_def_utils.build_signature_def(
            inputs=inputs,
            outputs=outputs,
            method_name='tensorflow/serving/predict')

        saved_model_predictor = predictor.from_saved_model(
            export_dir="model/4", signature_def=signature_def)
示例#19
0
def read_image_arr(image_file):
    img = Image.open(image_file)

    img_array = np.array(img, dtype=np.uint8)
    # print(img_array)
    img_array = img_array.reshape(img_array.size)
    img_array = np.divide(np.mod(img_array, 255), 255.0)
    #img_array = np.divide(np.subtract(255.0, img_array), 255.0)
    # print(img_array)
    return img_array


if __name__ == '__main__':

    test_data_set = mnist_input_data.read_data_sets(input_dir).test
    num_tests = 1
    for idx in range(num_tests):
        image, label = test_data_set.next_batch(1)
        # print(image[0])
        # arr = np.asanyarray(image[0]).reshape(28, 28)
        # print(arr)
        print("############", type(image[0][0]))
        print("############", label[0])
        idx_num = "%05d" % int(idx)
        img_1 = trans_image(idx_num, image)

        # print(image[0])
        out_file = out_dir % idx_num
        img_2 = read_image_arr(out_file)
        print("############")
示例#20
0
def main():
    parser = argparse.ArgumentParser()
    add_parameters(parser)
    args = parser.parse_args()

    if args.training_iteration <= 0:
        print('Please specify a positive value for training iteration.')
        sys.exit(-1)

    # Read the train and test data sets
    mnist = mnist_input_data.read_data_sets(args.input_cache_dir, one_hot=True)

    ## MLOps start
    # Initialize the mlops library
    mlops.init()

    # Report the feature distribution for the training data
    train_images = mnist.train.images
    mlops.set_data_distribution_stat(train_images)

    # Initialize a table to track training accuracy and cost
    train_table = Table().name("Training Stats").cols(["Accuracy", "Cost"])
    ## MLOps end

    # Create the model
    sess = tf.InteractiveSession()
    serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
    feature_configs = {
        'x': tf.FixedLenFeature(shape=[784], dtype=tf.float32),
    }
    tf_example = tf.parse_example(serialized_tf_example, feature_configs)
    x = tf.identity(tf_example['x'],
                    name='x')  # use tf.identity() to assign name
    y_ = tf.placeholder('float', shape=[None, 10])
    w = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    sess.run(tf.global_variables_initializer())
    y = tf.nn.softmax(tf.matmul(x, w) + b, name='y')

    # Set the cost function and optimizer
    cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(
        cross_entropy)
    values, indices = tf.nn.top_k(y, 10)
    table = tf.contrib.lookup.index_to_string_table_from_tensor(
        tf.constant([str(i) for i in range(10)]))
    prediction_classes = table.lookup(tf.to_int64(indices))

    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))

    # Train the model
    print('Training model...')
    for i in range(args.training_iteration):
        batch = mnist.train.next_batch(50)
        _, train_cost, train_acc = sess.run(
            [train_step, cross_entropy, accuracy],
            feed_dict={
                x: batch[0],
                y_: batch[1]
            })

        # Display stats
        if (i + 1
            ) % args.display_step == 0 or i + 1 == args.training_iteration:
            # Report training accuracy and cost

            print("Training. step={}, accuracy={}, cost={}".format(
                i + 1, train_acc, train_cost))

            # MLOps start
            # multiply by 1 to convert into double
            train_table.add_row("Iterations: {}".format(i + 1),
                                [train_acc * 100, train_cost * 1])
            mlops.set_stat(train_table)
            # MLOps end

    print('Done training!')

    # Report final cost and accuracy on test set
    test_cost, test_acc = sess.run([cross_entropy, accuracy],
                                   feed_dict={
                                       x: mnist.test.images,
                                       y_: mnist.test.labels
                                   })
    print("Testing. accuracy={}, cost={}".format(test_acc, test_cost))

    ## MLOps start
    acc_table = Table().name("Test Accuracy").cols(["Accuracy"])
    acc_table.add_row("Total iterations: {}".format(args.training_iteration),
                      [test_acc])
    mlops.set_stat(acc_table)

    # Release mlops resources
    mlops.done()
    ## MLOps end

    # Export the trained model so it can be used for inference
    # WARNING(break-tutorial-inline-code): The following code snippet is
    # in-lined in tutorials, please update tutorial documents accordingly
    # whenever code changes.
    export_path = args.save_dir
    print('Exporting trained model to', export_path)
    builder = tf.saved_model.builder.SavedModelBuilder(export_path)

    # Build the signature_def_map.
    classification_inputs = tf.saved_model.utils.build_tensor_info(
        serialized_tf_example)
    classification_outputs_classes = tf.saved_model.utils.build_tensor_info(
        prediction_classes)
    classification_outputs_scores = tf.saved_model.utils.build_tensor_info(
        values)

    classification_signature = (
        tf.saved_model.signature_def_utils.build_signature_def(
            inputs={
                tf.saved_model.signature_constants.CLASSIFY_INPUTS:
                classification_inputs
            },
            outputs={
                tf.saved_model.signature_constants.CLASSIFY_OUTPUT_CLASSES:
                classification_outputs_classes,
                tf.saved_model.signature_constants.CLASSIFY_OUTPUT_SCORES:
                classification_outputs_scores
            },
            method_name=tf.saved_model.signature_constants.CLASSIFY_METHOD_NAME
        ))

    tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
    tensor_info_y = tf.saved_model.utils.build_tensor_info(y)

    prediction_signature = (
        tf.saved_model.signature_def_utils.build_signature_def(
            inputs={'inputs': tensor_info_x},
            outputs={'outputs': tensor_info_y},
            method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)
    )

    legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.tag_constants.SERVING],
        signature_def_map={
            'predict_images':
            prediction_signature,
            tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            classification_signature,
        },
        legacy_init_op=legacy_init_op)

    builder.save()

    print('Done exporting!')
示例#21
0
def main(_):
    if len(sys.argv) < 2 or sys.argv[-1].startswith('-'):
        print('Usage: mnist_export.py [--training_iteration=x] '
              '[--export_version=y] export_dir')
        sys.exit(-1)
    if FLAGS.training_iteration <= 0:
        print('Please specify a positive value for training iteration.')
        sys.exit(-1)
    if FLAGS.export_version <= 0:
        print('Please specify a positive value for version number.')
        sys.exit(-1)

    # Train model
    print('Training model...')
    mnist = mnist_input_data.read_data_sets(FLAGS.work_dir, one_hot=True)
    sess = tf.InteractiveSession()
    serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
    feature_configs = {
        'x': tf.FixedLenFeature(shape=[784], dtype=tf.float32),
    }
    tf_example = tf.parse_example(serialized_tf_example, feature_configs)
    x = tf.identity(tf_example['x'],
                    name='x')  # use tf.identity() to assign name
    y_ = tf.placeholder('float', shape=[None, 10])
    w = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    sess.run(tf.global_variables_initializer())
    y = tf.nn.softmax(tf.matmul(x, w) + b, name='y')
    cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(
        cross_entropy)
    values, indices = tf.nn.top_k(y, 10)
    table = tf.contrib.lookup.index_to_string_table_from_tensor(
        tf.constant([str(i) for i in range(10)]))
    prediction_classes = table.lookup(tf.to_int64(indices))
    for _ in range(FLAGS.training_iteration):
        batch = mnist.train.next_batch(50)
        train_step.run(feed_dict={x: batch[0], y_: batch[1]})
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
    print('training accuracy %g' % sess.run(accuracy,
                                            feed_dict={
                                                x: mnist.test.images,
                                                y_: mnist.test.labels
                                            }))
    print('Done training!')

    # Export model
    # WARNING(break-tutorial-inline-code): The following code snippet is
    # in-lined in tutorials, please update tutorial documents accordingly
    # whenever code changes.
    export_path = sys.argv[-1]
    print('Exporting trained model to %s' % export_path)
    init_op = tf.group(tf.tables_initializer(), name='init_op')
    saver = tf.train.Saver(sharded=True)
    model_exporter = exporter.Exporter(saver)
    model_exporter.init(
        sess.graph.as_graph_def(),
        init_op=init_op,
        default_graph_signature=exporter.classification_signature(
            input_tensor=serialized_tf_example,
            classes_tensor=prediction_classes,
            scores_tensor=values),
        named_graph_signatures={
            'inputs': exporter.generic_signature({'images': x}),
            'outputs': exporter.generic_signature({'scores': y})
        })
    model_exporter.export(export_path, tf.constant(FLAGS.export_version), sess)
    print('Done exporting!')
示例#22
0
def main(_):
    if len(sys.argv) < 2 or sys.argv[-1].startswith('-'):
        print(
            'Usage: mnist_export.py [--training_iteration=x] '
            '[--model_version=y] export_dir')
        sys.exit(-1)
    if FLAGS.training_iteration <= 0:
        print 'Please specify a positive value for training iteration.'
        sys.exit(-1)
    if FLAGS.model_version <= 0:
        print 'Please specify a positive value for version number.'
        sys.exit(-1)

    # Train model
    print 'Training model...'

    #Read the data and format it
    mnist = mnist_input_data.read_data_sets(FLAGS.work_dir, one_hot=True)
    sess = tf.InteractiveSession()
    serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
    feature_configs = {
        'x': tf.FixedLenFeature(shape=[784], dtype=tf.float32),
    }
    tf_example = tf.parse_example(serialized_tf_example, feature_configs)

    #Build model
    x = tf.identity(tf_example['x'],
                    name='x')  # use tf.identity() to assign name
    y_ = tf.placeholder('float', shape=[None, 10])
    w = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    sess.run(tf.global_variables_initializer())
    y = tf.nn.softmax(tf.matmul(x, w) + b, name='y')
    cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(
        cross_entropy)
    values, indices = tf.nn.top_k(y, 10)
    table = tf.contrib.lookup.index_to_string_table_from_tensor(
        tf.constant([str(i) for i in xrange(10)]))

    #train the model
    prediction_classes = table.lookup(tf.to_int64(indices))
    for _ in range(FLAGS.training_iteration):
        batch = mnist.train.next_batch(50)
        train_step.run(feed_dict={x: batch[0], y_: batch[1]})
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
    print 'training accuracy %g' % sess.run(accuracy,
                                            feed_dict={
                                                x: mnist.test.images,
                                                y_: mnist.test.labels
                                            })
    print 'Done training!'

    # Save the model

    #where to save to?
    export_path_base = sys.argv[-1]
    export_path = os.path.join(compat.as_bytes(export_path_base),
                               compat.as_bytes(str(FLAGS.model_version)))
    print 'Exporting trained model to', export_path

    #This creates a SERVABLE from our model
    #saves a "snapshot" of the trained model to reliable storage
    #so that it can be loaded later for inference.
    #can save as many version as necessary

    #the tensoroflow serving main file tensorflow_model_server
    #will create a SOURCE out of it, the source
    #can house state that is shared across multiple servables
    #or versions

    #we can later create a LOADER from it using tf.saved_model.loader.load

    #then the MANAGER decides how to handle its lifecycle

    builder = saved_model_builder.SavedModelBuilder(export_path)

    # Build the signature_def_map.
    #Signature specifies what type of model is being exported,
    #and the input/output tensors to bind to when running inference.
    #think of them as annotiations on the graph for serving
    #we can use them a number of ways
    #grabbing whatever inputs/outputs/models we want either on server
    #or via client
    classification_inputs = utils.build_tensor_info(serialized_tf_example)
    classification_outputs_classes = utils.build_tensor_info(
        prediction_classes)
    classification_outputs_scores = utils.build_tensor_info(values)

    classification_signature = signature_def_utils.build_signature_def(
        inputs={signature_constants.CLASSIFY_INPUTS: classification_inputs},
        outputs={
            signature_constants.CLASSIFY_OUTPUT_CLASSES:
            classification_outputs_classes,
            signature_constants.CLASSIFY_OUTPUT_SCORES:
            classification_outputs_scores
        },
        method_name=signature_constants.CLASSIFY_METHOD_NAME)

    tensor_info_x = utils.build_tensor_info(x)
    tensor_info_y = utils.build_tensor_info(y)

    prediction_signature = signature_def_utils.build_signature_def(
        inputs={'images': tensor_info_x},
        outputs={'scores': tensor_info_y},
        method_name=signature_constants.PREDICT_METHOD_NAME)

    legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')

    #add the sigs to the servable
    builder.add_meta_graph_and_variables(
        sess, [tag_constants.SERVING],
        signature_def_map={
            'predict_images':
            prediction_signature,
            signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            classification_signature,
        },
        legacy_init_op=legacy_init_op)

    #save it!
    builder.save()

    print 'Done exporting!'
示例#23
0
#encoding=utf-8
import os
import sys

import numpy as np
import tensorflow as tf
import mnist_input_data

mnist = mnist_input_data.read_data_sets("/tmp", one_hot=True)
sess = tf.InteractiveSession()

saved_model_dir = "./model/1"

meta_graph_def = tf.saved_model.loader.load(
    sess, [tf.saved_model.tag_constants.SERVING], saved_model_dir)

signature_key = "predict_images"
input_key = "images"
output_key = "scores"

# 从meta_graph_def中取出SignatureDef对象
signature = meta_graph_def.signature_def

# 从signature中找出具体输入输出的tensor name
x_tensor_name = signature[signature_key].inputs[input_key].name
y_tensor_name = signature[signature_key].outputs[output_key].name

# 获取tensor 并inference
x = sess.graph.get_tensor_by_name(x_tensor_name)
y = sess.graph.get_tensor_by_name(y_tensor_name)
'''
A Reccurent Neural Network (LSTM) implementation example using TensorFlow library.
This example is using the MNIST database of handwritten digits (http://yann.lecun.com/exdb/mnist/)
Long Short Term Memory paper: http://deeplearning.cs.cmu.edu/pdfs/Hochreiter97_lstm.pdf

Author: Aymeric Damien
Project: https://github.com/aymericdamien/TensorFlow-Examples/
'''

# Import MINST data
import mnist_input_data
import datetime

mnist = mnist_input_data.read_data_sets("/Users/rmencis/Temp", one_hot=True)

import tensorflow as tf
from tensorflow.models.rnn import rnn, rnn_cell
import numpy as np

'''
To classify images using a reccurent neural network, we consider every image row as a sequence of pixels.
Because MNIST image shape is 28*28px, we will then handle 28 sequences of 28 steps for every sample.
'''

# Parameters
learning_rate = 0.001
training_iters = 100000
batch_size = 100
display_time = 5

# Network Parameters
示例#25
0
def main(_):
  if len(sys.argv) < 2 or sys.argv[-1].startswith('-'):
    print('Usage: mnist_export.py [--training_iteration=x] '
          '[--model_version=y] export_dir')
    sys.exit(-1)
  if FLAGS.training_iteration <= 0:
    print('Please specify a positive value for training iteration.')
    sys.exit(-1)
  if FLAGS.model_version <= 0:
    print('Please specify a positive value for version number.')
    sys.exit(-1)

  # Train model
  print('Training model...')
  mnist = mnist_input_data.read_data_sets(FLAGS.work_dir, one_hot=True)
  sess = tf.InteractiveSession()
  serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
  feature_configs = {'x': tf.FixedLenFeature(shape=[784], dtype=tf.float32),}
  tf_example = tf.parse_example(serialized_tf_example, feature_configs)
  x = tf.identity(tf_example['x'], name='x')  # use tf.identity() to assign name
  y_ = tf.placeholder('float', shape=[None, 10])
  w = tf.Variable(tf.zeros([784, 10]))
  b = tf.Variable(tf.zeros([10]))
  sess.run(tf.global_variables_initializer())
  y = tf.nn.softmax(tf.matmul(x, w) + b, name='y')
  cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
  train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
  values, indices = tf.nn.top_k(y, 10)
  table = tf.contrib.lookup.index_to_string_table_from_tensor(
      tf.constant([str(i) for i in range(10)]))
  prediction_classes = table.lookup(tf.to_int64(indices))
  for _ in range(FLAGS.training_iteration):
    batch = mnist.train.next_batch(50)
    train_step.run(feed_dict={x: batch[0], y_: batch[1]})
  correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
  accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
  print('training accuracy %g' % sess.run(
      accuracy, feed_dict={
          x: mnist.test.images,
          y_: mnist.test.labels
      }))
  print('Done training!')

  # Export model
  # WARNING(break-tutorial-inline-code): The following code snippet is
  # in-lined in tutorials, please update tutorial documents accordingly
  # whenever code changes.
  export_path_base = sys.argv[-1]
  export_path = os.path.join(
      tf.compat.as_bytes(export_path_base),
      tf.compat.as_bytes(str(FLAGS.model_version)))
  print('Exporting trained model to', export_path)
  builder = tf.saved_model.builder.SavedModelBuilder(export_path)

  # Build the signature_def_map.
  classification_inputs = tf.saved_model.utils.build_tensor_info(
      serialized_tf_example)
  classification_outputs_classes = tf.saved_model.utils.build_tensor_info(
      prediction_classes)
  classification_outputs_scores = tf.saved_model.utils.build_tensor_info(values)

  classification_signature = (
      tf.saved_model.signature_def_utils.build_signature_def(
          inputs={
              tf.saved_model.signature_constants.CLASSIFY_INPUTS:
                  classification_inputs
          },
          outputs={
              tf.saved_model.signature_constants.CLASSIFY_OUTPUT_CLASSES:
                  classification_outputs_classes,
              tf.saved_model.signature_constants.CLASSIFY_OUTPUT_SCORES:
                  classification_outputs_scores
          },
          method_name=tf.saved_model.signature_constants.CLASSIFY_METHOD_NAME))

  tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
  tensor_info_y = tf.saved_model.utils.build_tensor_info(y)

  prediction_signature = (
      tf.saved_model.signature_def_utils.build_signature_def(
          inputs={'images': tensor_info_x},
          outputs={'scores': tensor_info_y},
          method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))

  legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
  builder.add_meta_graph_and_variables(
      sess, [tf.saved_model.tag_constants.SERVING],
      signature_def_map={
          'predict_images':
              prediction_signature,
          tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
              classification_signature,
      },
      legacy_init_op=legacy_init_op)

  builder.save()

  print('Done exporting!')
'''
A Bidirectional Reccurent Neural Network (LSTM) implementation example using TensorFlow library.
This example is using the MNIST database of handwritten digits (http://yann.lecun.com/exdb/mnist/)
Long Short Term Memory paper: http://deeplearning.cs.cmu.edu/pdfs/Hochreiter97_lstm.pdf

Author: Aymeric Damien
Project: https://github.com/aymericdamien/TensorFlow-Examples/
'''

# Import MINST data
import mnist_input_data

mnist = mnist_input_data.read_data_sets("/tmp/data/", one_hot=True)

import tensorflow as tf
from tensorflow.python.ops.constant_op import constant
from tensorflow.models.rnn import rnn, rnn_cell
import numpy as np

'''
To classify images using a bidirectional reccurent neural network, we consider every image row as a sequence of pixels.
Because MNIST image shape is 28*28px, we will then handle 28 sequences of 28 steps for every sample.
'''

# Parameters
learning_rate = 0.001
training_iters = 100000
batch_size = 128
display_step = 10

# Network Parameters
示例#27
0
    name: tf_example:0
The given SavedModel SignatureDef contains the following output(s):
outputs['classes'] tensor_info:
    dtype: DT_STRING
    shape: (-1, 10)
    name: index_to_string_Lookup:0
outputs['scores'] tensor_info:
    dtype: DT_FLOAT
    shape: (-1, 10)
    name: TopKV2:0
Method name is: tensorflow/serving/classify
'''

export_dir = 'model/1/'

mnist = mnist_input_data.read_data_sets('/tmp', one_hot=True)
image0 = mnist.test.images[0]
batch0 = mnist.test.next_batch(1)
print(image0.shape)

with tf.Session(graph=tf.Graph()) as sess:
    metagraph_def = tf.saved_model.loader.load(
        sess, [tf.saved_model.tag_constants.SERVING], export_dir)

    signature_def = metagraph_def.signature_def['predict_images']
    input_tensor = sess.graph.get_tensor_by_name(
        signature_def.inputs['images'].name)
    output_tensor = sess.graph.get_tensor_by_name(
        signature_def.outputs['scores'].name)

    def forward_images(images):
示例#28
0
def main(_):
  if len(sys.argv) < 2 or sys.argv[-1].startswith('-'):
    print('Usage: mnist_export.py [--training_iteration=x] '
          '[--model_version=y] export_dir')
    sys.exit(-1)
  if FLAGS.training_iteration <= 0:
    print 'Please specify a positive value for training iteration.'
    sys.exit(-1)
  if FLAGS.model_version <= 0:
    print 'Please specify a positive value for version number.'
    sys.exit(-1)

  # Train model
  print 'Training model...'
  mnist = mnist_input_data.read_data_sets(FLAGS.work_dir, one_hot=True)
  sess = tf.InteractiveSession()
  serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
  feature_configs = {'x': tf.FixedLenFeature(shape=[784], dtype=tf.float32),}
  tf_example = tf.parse_example(serialized_tf_example, feature_configs)
  x = tf.identity(tf_example['x'], name='x')  # use tf.identity() to assign name
  y_ = tf.placeholder('float', shape=[None, 10])
  w = tf.Variable(tf.zeros([784, 10]))
  b = tf.Variable(tf.zeros([10]))
  sess.run(tf.global_variables_initializer())
  y = tf.nn.softmax(tf.matmul(x, w) + b, name='y')
  cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
  train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
  values, indices = tf.nn.top_k(y, 10)
  table = tf.contrib.lookup.index_to_string_table_from_tensor(
      tf.constant([str(i) for i in xrange(10)]))
  prediction_classes = table.lookup(tf.to_int64(indices))
  for _ in range(FLAGS.training_iteration):
    batch = mnist.train.next_batch(50)
    train_step.run(feed_dict={x: batch[0], y_: batch[1]})
  correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
  accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
  print 'training accuracy %g' % sess.run(
      accuracy, feed_dict={x: mnist.test.images,
                           y_: mnist.test.labels})
  print 'Done training!'

  # Export model
  # WARNING(break-tutorial-inline-code): The following code snippet is
  # in-lined in tutorials, please update tutorial documents accordingly
  # whenever code changes.
  export_path_base = sys.argv[-1]
  export_path = os.path.join(
      tf.compat.as_bytes(export_path_base),
      tf.compat.as_bytes(str(FLAGS.model_version)))
  print 'Exporting trained model to', export_path
  builder = tf.saved_model.builder.SavedModelBuilder(export_path)

  # Build the signature_def_map.
  classification_inputs = tf.saved_model.utils.build_tensor_info(
      serialized_tf_example)
  classification_outputs_classes = tf.saved_model.utils.build_tensor_info(
      prediction_classes)
  classification_outputs_scores = tf.saved_model.utils.build_tensor_info(values)

  classification_signature = (
      tf.saved_model.signature_def_utils.build_signature_def(
          inputs={
              tf.saved_model.signature_constants.CLASSIFY_INPUTS:
                  classification_inputs
          },
          outputs={
              tf.saved_model.signature_constants.CLASSIFY_OUTPUT_CLASSES:
                  classification_outputs_classes,
              tf.saved_model.signature_constants.CLASSIFY_OUTPUT_SCORES:
                  classification_outputs_scores
          },
          method_name=tf.saved_model.signature_constants.CLASSIFY_METHOD_NAME))

  tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
  tensor_info_y = tf.saved_model.utils.build_tensor_info(y)

  prediction_signature = (
      tf.saved_model.signature_def_utils.build_signature_def(
          inputs={'images': tensor_info_x},
          outputs={'scores': tensor_info_y},
          method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))

  legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
  builder.add_meta_graph_and_variables(
      sess, [tf.saved_model.tag_constants.SERVING],
      signature_def_map={
          'predict_images':
              prediction_signature,
          tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
              classification_signature,
      },
      legacy_init_op=legacy_init_op)

  builder.save()

  print 'Done exporting!'
import mnist_input_data
import tensorflow as tf

mnist = mnist_input_data.read_data_sets("MNIST_data/", one_hot=True)


def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)


def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)


# Create the model
x = tf.placeholder(tf.float32, [None, 784])
W_fc1 = weight_variable([784, 200])
b_fc1 = bias_variable([200])
W_fc2 = weight_variable([200, 200])
b_fc2 = bias_variable([200])
W_out = weight_variable([200, 10])
b_out = bias_variable([10])
hidden_1 = tf.nn.relu(tf.matmul(x, W_fc1) + b_fc1)
hidden_2 = tf.nn.relu(tf.matmul(hidden_1, W_fc2) + b_fc2)
y = tf.nn.softmax(tf.matmul(hidden_2, W_out) + b_out)

# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10])
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
示例#30
0
def run_training():
    """Train MNIST for a number of steps."""
    # Get the sets of images and labels for training, validation, and
    # test on MNIST.
    data_sets = mnist_input_data.read_data_sets(FLAGS.input_data_dir,
                                                FLAGS.fake_data)

    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():
        # Generate placeholders for the images and labels.
        images_placeholder, labels_placeholder = placeholder_inputs(
            FLAGS.batch_size)

        # Build a Graph that computes predictions from the inference model.
        logits = mnist.inference(images_placeholder, FLAGS.hidden1,
                                 FLAGS.hidden2)

        # Add to the Graph the Ops for loss calculation.
        loss = mnist.loss(logits, labels_placeholder)

        # Add to the Graph the Ops that calculate and apply gradients.
        train_op = mnist.training(loss, FLAGS.learning_rate)

        # Add the Op to compare the logits to the labels during evaluation.
        eval_correct = mnist.evaluation(logits, labels_placeholder)

        # Build the summary Tensor based on the TF collection of Summaries.
        summary = tf.summary.merge_all()

        # Add the variable initializer Op.
        init = tf.global_variables_initializer()

        # Create a saver for writing training checkpoints.
        saver = tf.train.Saver()

        # Create a session for running Ops on the Graph.
        sess = tf.Session()

        # Instantiate a SummaryWriter to output summaries and the Graph.
        summary_writer = tf.summary.FileWriter(FLAGS.log_dir, sess.graph)

        # And then after everything is built:

        # Run the Op to initialize the variables.
        sess.run(init)

        # Start the training loop.
        for step in xrange(FLAGS.max_steps):
            start_time = time.time()

            # Fill a feed dictionary with the actual set of images and labels
            # for this particular training step.
            feed_dict = fill_feed_dict(data_sets.train, images_placeholder,
                                       labels_placeholder)

            # Run one step of the model.  The return values are the activations
            # from the `train_op` (which is discarded) and the `loss` Op.  To
            # inspect the values of your Ops or variables, you may include them
            # in the list passed to sess.run() and the value tensors will be
            # returned in the tuple from the call.
            _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)

            duration = time.time() - start_time

            # Write the summaries and print an overview fairly often.
            if step % 100 == 0:
                # Print status to stdout.
                print('Step %d: loss = %.2f (%.3f sec)' %
                      (step, loss_value, duration))
                # Update the events file.
                summary_str = sess.run(summary, feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, step)
                summary_writer.flush()

            # Save a checkpoint and evaluate the model periodically.
            if (step + 1) % 1000 == 0 or (step + 1) == FLAGS.max_steps:
                checkpoint_file = os.path.join(FLAGS.log_dir, 'model.ckpt')
                saver.save(sess, checkpoint_file, global_step=step)
                # Evaluate against the training set.
                print('Training Data Eval:')
                do_eval(sess, eval_correct, images_placeholder,
                        labels_placeholder, data_sets.train)
                # Evaluate against the validation set.
                print('Validation Data Eval:')
                do_eval(sess, eval_correct, images_placeholder,
                        labels_placeholder, data_sets.validation)
                # Evaluate against the test set.
                print('Test Data Eval:')
                do_eval(sess, eval_correct, images_placeholder,
                        labels_placeholder, data_sets.test)
示例#31
0
def train():
    with tf.device("/gpu:0"):
        # Import data
        mnist = mnist_input_data.read_data_sets(FLAGS.data_dir, one_hot=True)

        sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                                log_device_placement=True))
        # Create a multilayer model.

        # Input placeholders
        with tf.name_scope('input'):
            x = tf.placeholder(tf.float32, [None, 784], name='x-input')
            y_ = tf.placeholder(tf.float32, [None, 10], name='y-input')

        with tf.name_scope('input_reshape'):
            image_shaped_input = tf.reshape(x, [-1, 28, 28, 1])
            tf.summary.image('input', image_shaped_input, 10)

        # We can't initialize these variables to 0 - the network will get stuck.
        def weight_variable(shape):
            """Create a weight variable with appropriate initialization."""
            initial = tf.truncated_normal(shape, stddev=0.1)
            return tf.Variable(initial)

        def bias_variable(shape):
            """Create a bias variable with appropriate initialization."""
            initial = tf.constant(0.1, shape=shape)
            return tf.Variable(initial)

        def variable_summaries(var):
            """Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""
            with tf.name_scope('summaries'):
                mean = tf.reduce_mean(var)
                tf.summary.scalar('mean', mean)
                with tf.name_scope('stddev'):
                    stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
                tf.summary.scalar('stddev', stddev)
                tf.summary.scalar('max', tf.reduce_max(var))
                tf.summary.scalar('min', tf.reduce_min(var))
                tf.summary.histogram('histogram', var)

        def nn_layer(input_tensor,
                     input_dim,
                     output_dim,
                     layer_name,
                     act=tf.nn.relu):
            """Reusable code for making a simple neural net layer. 
     
        It does a matrix multiply, bias add, and then uses relu to nonlinearize. 
        It also sets up name scoping so that the resultant graph is easy to read, 
        and adds a number of summary ops. 
        """
            # Adding a name scope ensures logical grouping of the layers in the graph.
            with tf.name_scope(layer_name):
                # This Variable will hold the state of the weights for the layer
                with tf.name_scope('weights'):
                    weights = weight_variable([input_dim, output_dim])
                    variable_summaries(weights)
                with tf.name_scope('biases'):
                    biases = bias_variable([output_dim])
                    variable_summaries(biases)
                with tf.name_scope('Wx_plus_b'):
                    preactivate = tf.matmul(input_tensor, weights) + biases
                    tf.summary.histogram('pre_activations', preactivate)
                activations = act(preactivate, name='activation')
                tf.summary.histogram('activations', activations)
                return activations

        hidden1 = nn_layer(x, 784, 500, 'layer1')

        with tf.name_scope('dropout'):
            keep_prob = tf.placeholder(tf.float32)
            tf.summary.scalar('dropout_keep_probability', keep_prob)
            dropped = tf.nn.dropout(hidden1, keep_prob)

        # Do not apply softmax activation yet, see below.
        y = nn_layer(dropped, 500, 10, 'layer2', act=tf.identity)

        with tf.name_scope('cross_entropy'):
            # The raw formulation of cross-entropy,
            #
            # tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.softmax(y)),
            #                               reduction_indices=[1]))
            #
            # can be numerically unstable.
            #
            # So here we use tf.nn.softmax_cross_entropy_with_logits on the
            # raw outputs of the nn_layer above, and then average across
            # the batch.
            diff = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)
            with tf.name_scope('total'):
                cross_entropy = tf.reduce_mean(diff)
        tf.summary.scalar('cross_entropy', cross_entropy)

        with tf.name_scope('train'):
            train_step = tf.train.AdamOptimizer(
                FLAGS.learning_rate).minimize(cross_entropy)

        with tf.name_scope('accuracy'):
            with tf.name_scope('correct_prediction'):
                correct_prediction = tf.equal(tf.argmax(y, 1),
                                              tf.argmax(y_, 1))
            with tf.name_scope('accuracy'):
                accuracy = tf.reduce_mean(
                    tf.cast(correct_prediction, tf.float32))
        tf.summary.scalar('accuracy', accuracy)

        # Merge all the summaries and write them out to /tmp/mnist_logs (by default)
        merged = tf.summary.merge_all()
        train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train',
                                             sess.graph)
        test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test')
        sess.run(tf.global_variables_initializer())

        # Train the model, and also write summaries.
        # Every 10th step, measure test-set accuracy, and write test summaries
        # All other steps, run train_step on training data, & add training summaries

        def feed_dict(train):
            """Make a TensorFlow feed_dict: maps data onto Tensor placeholders."""
            if train or FLAGS.fake_data:
                xs, ys = mnist.train.next_batch(100, fake_data=FLAGS.fake_data)
                k = FLAGS.dropout
            else:
                xs, ys = mnist.test.images, mnist.test.labels
                k = 1.0
            return {x: xs, y_: ys, keep_prob: k}

        for i in range(FLAGS.max_steps):
            if i % 10 == 0:  # Record summaries and test-set accuracy
                summary, acc = sess.run([merged, accuracy],
                                        feed_dict=feed_dict(False))
                test_writer.add_summary(summary, i)
                print('Accuracy at step %s: %s' % (i, acc))
            else:  # Record train set summaries, and train
                if i % 100 == 99:  # Record execution stats
                    run_metadata = tf.RunMetadata()
                    summary, _ = sess.run([merged, train_step],
                                          feed_dict=feed_dict(True))
                    train_writer.add_run_metadata(run_metadata, 'step%03d' % i)
                    train_writer.add_summary(summary, i)
                    print('Adding run metadata for', i)
                else:  # Record a summary
                    summary, _ = sess.run([merged, train_step],
                                          feed_dict=feed_dict(True))
                    train_writer.add_summary(summary, i)
        train_writer.close()
        test_writer.close()

    # Export model
    # WARNING(break-tutorial-inline-code): The following code snippet is
    # in-lined in tutorials, please update tutorial documents accordingly
    # whenever code changes.
    with tf.device("/cpu:0"):
        export_path = FLAGS.model_dir + '/1'
        print('Exporting trained model to', export_path)
        builder = tf.saved_model.builder.SavedModelBuilder(export_path)

        tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
        tensor_info_y = tf.saved_model.utils.build_tensor_info(y)
        tensor_info_keepp = tf.saved_model.utils.build_tensor_info(keep_prob)

        prediction_signature = (
            tf.saved_model.signature_def_utils.build_signature_def(
                inputs={
                    'images': tensor_info_x,
                    'keep_prob': tensor_info_keepp
                },
                outputs={'scores': tensor_info_y},
                method_name=tf.saved_model.signature_constants.
                PREDICT_METHOD_NAME))

        legacy_init_op = tf.group(tf.tables_initializer(),
                                  name='legacy_init_op')
        builder.add_meta_graph_and_variables(
            sess, [tf.saved_model.tag_constants.SERVING],
            signature_def_map={
                'predict_images': prediction_signature,
            },
            clear_devices=True,
            legacy_init_op=legacy_init_op)

        builder.save()

        print('Done exporting!')
示例#32
0
tf.app.flags.DEFINE_integer('model_version', 1, 'version number of the model.')
tf.app.flags.DEFINE_string('work_dir', '/tmp', 'Working directory.')
FLAGS = tf.app.flags.FLAGS

sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
# x = tf.reshape(images, [-1, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
w = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

sess.run(tf.global_variables_initializer())
scores = tf.nn.softmax(tf.matmul(x, w) + b, name='scores')
cross_entropy = -tf.reduce_sum(y_ * tf.log(scores))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

mnist = mnist_input_data.read_data_sets(FLAGS.work_dir, one_hot=True)
for _ in range(FLAGS.training_iteration):
    batch = mnist.train.next_batch(50)
    train_step.run(feed_dict={x: batch[0], y_: batch[1]})

# print("train_step==")
# print(train_step)
# print("scores==")
# print(scores)
# print(scores.eval())

sess.close()

示例#33
0
def main(args):
    prog_start_time = time.time()
    # handle parameters
    parser = argparse.ArgumentParser()
    add_parameters(parser)
    args = parser.parse_args()
    print('Training for %i iterations' % args.iterations)

    # Load training and eval data
    mnist_data = mnist_input_data.read_data_sets(args.input_dir, one_hot=True)
    serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
    feature_configs = {
        'x': tf.FixedLenFeature(shape=[784], dtype=tf.float32),
    }
    tf_example = tf.parse_example(serialized_tf_example, feature_configs)
    x = tf.identity(tf_example['x'],
                    name='x')  # use tf.identity() to assign name

    y_ = tf.placeholder('float', shape=[None, 10])

    # Create the model
    model = cnn_model(x)
    y = tf.identity(model, name='y')

    # Calculate Loss (for both TRAIN and EVAL modes)
    loss = tf.losses.softmax_cross_entropy(onehot_labels=y_, logits=y)

    # Training
    train_step = tf.train.GradientDescentOptimizer(
        learning_rate=args.step_size).minimize(loss)

    # Evaluation
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))

    # Track cost and accuracy for the training and test sets in TensorBoard
    tb_writer = tf.summary.FileWriter(args.tf_log,
                                      graph=tf.get_default_graph())
    train_cost_op = tf.summary.scalar("train_cost", loss)
    train_acc_op = tf.summary.scalar("train_accuracy", accuracy)
    train_stats_op = tf.summary.merge([train_cost_op, train_acc_op])

    test_cost_op = tf.summary.scalar("test_cost", loss)
    test_acc_op = tf.summary.scalar("test_accuracy", accuracy)
    test_stats_op = tf.summary.merge([test_cost_op, test_acc_op])

    # Train the model
    training_start_time = time.time()
    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    for i in range(args.iterations):
        batch = mnist_data.train.next_batch(args.batch_size)
        _, train_summary, train_cost, train_acc = sess.run(
            [train_step, train_stats_op, loss, accuracy],
            feed_dict={
                x: batch[0],
                y_: batch[1]
            })

        if (i % args.stats_interval == 0 or i == (args.iterations - 1)):
            test_acc, test_cost, test_summary = sess.run(
                [accuracy, loss, test_stats_op],
                feed_dict={
                    x: mnist_data.test.images,
                    y_: mnist_data.test.labels
                })
            print("step=", i, "accuracy: train=", train_acc, "test=", test_acc,
                  "cost: train=", train_cost, "test=", test_cost)
            tb_writer.add_summary(train_summary, i)
            tb_writer.add_summary(test_summary, i)
            tb_writer.flush()
    training_elapsed_time = time.time() - training_start_time

    # Save the model
    export_path = args.save_dir
    print('Exporting trained model to', export_path)
    builder = tf.saved_model.builder.SavedModelBuilder(export_path)

    values, indices = tf.nn.top_k(y, 10)
    table = tf.contrib.lookup.index_to_string_table_from_tensor(
        tf.constant([str(i) for i in range(10)]))
    prediction_classes = table.lookup(tf.to_int64(indices))

    # Build the signature_def_map.
    classification_inputs = tf.saved_model.utils.build_tensor_info(
        serialized_tf_example)
    classification_outputs_classes = tf.saved_model.utils.build_tensor_info(
        prediction_classes)
    classification_outputs_scores = tf.saved_model.utils.build_tensor_info(
        values)

    classification_signature = (
        tf.saved_model.signature_def_utils.build_signature_def(
            inputs={
                tf.saved_model.signature_constants.CLASSIFY_INPUTS:
                classification_inputs
            },
            outputs={
                tf.saved_model.signature_constants.CLASSIFY_OUTPUT_CLASSES:
                classification_outputs_classes,
                tf.saved_model.signature_constants.CLASSIFY_OUTPUT_SCORES:
                classification_outputs_scores
            },
            method_name=tf.saved_model.signature_constants.CLASSIFY_METHOD_NAME
        ))

    tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
    tensor_info_y = tf.saved_model.utils.build_tensor_info(y)

    prediction_signature = (
        tf.saved_model.signature_def_utils.build_signature_def(
            inputs={'inputs': tensor_info_x},
            outputs={'outputs': tensor_info_y},
            method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)
    )

    legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')

    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.tag_constants.SERVING],
        signature_def_map={
            'predict_images':
            prediction_signature,
            tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            classification_signature,
        },
        legacy_init_op=legacy_init_op)

    builder.save(as_text=args.use_text)
    prog_elapsed_time = time.time() - prog_start_time
    print("Training seconds: {}. Total seconds: {}".format(
        training_elapsed_time, prog_elapsed_time))
示例#34
0
def main(_):
    if len(sys.argv) < 2 or sys.argv[-1].startswith('-'):
        print('Usage: mnist_saved_model.py [--training_iteration=x] '
              '[--model_version=y] export_dir')
        sys.exit(-1)
    if FLAGS.training_iteration <= 0:
        print('Please specify a positive value for training iteration.')
        sys.exit(-1)
    if FLAGS.model_version <= 0:
        print('Please specify a positive value for version number.')
        sys.exit(-1)

    # Train model
    print('Training model...')
    mnist = mnist_input_data.read_data_sets(FLAGS.work_dir, one_hot=True)

    sess = tf.InteractiveSession()

    x = tf.placeholder('float', shape=[None, 784])
    y_ = tf.placeholder('float', shape=[None, 10])
    w = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    sess.run(tf.global_variables_initializer())
    y = tf.nn.softmax(tf.matmul(x, w) + b, name='y')
    cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(
        cross_entropy)

    for _ in range(FLAGS.training_iteration):
        batch = mnist.train.next_batch(50)
        train_step.run(feed_dict={x: batch[0], y_: batch[1]})
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
    print('training accuracy %g' % sess.run(accuracy,
                                            feed_dict={
                                                x: mnist.test.images,
                                                y_: mnist.test.labels
                                            }))
    print('Done training!')

    # Export model
    # WARNING(break-tutorial-inline-code): The following code snippet is
    # in-lined in tutorials, please update tutorial documents accordingly
    # whenever code changes.

    export_path_base = sys.argv[-1]
    export_path = os.path.join(tf.compat.as_bytes(export_path_base),
                               tf.compat.as_bytes(str(FLAGS.model_version)))
    print('Exporting trained model to', export_path)
    builder = tf.saved_model.builder.SavedModelBuilder(export_path)

    # 生成 输入tensor x 和 输出tensor y的 tensor info
    tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
    tensor_info_y = tf.saved_model.utils.build_tensor_info(y)

    # 生成prediction_signature
    prediction_signature = (
        tf.saved_model.signature_def_utils.build_signature_def(
            # 客户端request的时候 输入的key 要与设置的相同
            inputs={'images': tensor_info_x},
            # 获得的response 结构体中 通过 设置的key('score')字段获得结果
            outputs={'scores': tensor_info_y},
            method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)
    )

    # 定义 meta
    builder.add_meta_graph_and_variables(
        sess,
        [tf.saved_model.tag_constants.SERVING],
        signature_def_map={
            # 用于客户端request时 的 signature_name
            'predict_images': prediction_signature,
        },
        main_op=tf.tables_initializer(),
        # 向上兼容
        strip_default_attrs=True)

    builder.save()

    print('Done exporting!')