Exemplo n.º 1
0
def rnet_serving(image):
    host = '192.168.1.253'
    port = 9000

    channel = implementations.insecure_channel(host, port)
    stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

    request = predict_pb2.PredictRequest()

    request.model_spec.name = 'mtcnn'

    request.model_spec.signature_name = 'rnet_predict'

    image_data_arr = generate_input_string(image)
    image_data_arr = np.asarray(image_data_arr).squeeze()
    num_image = image_data_arr.shape[0]

    dims = [tensor_shape_pb2.TensorShapeProto.Dim(size=num_image)]
    tensor_shape_proto = tensor_shape_pb2.TensorShapeProto(dim=dims)
    tensor_proto = tensor_pb2.TensorProto(
        dtype=types_pb2.DT_STRING,
        tensor_shape=tensor_shape_proto,
        string_val=[image_data for image_data in image_data_arr])
    request.inputs['images'].CopyFrom(tensor_proto)

    result = stub.Predict(request, 10.0)
    return [
        MakeNdarray(result.outputs['result1']),
        MakeNdarray(result.outputs['result2'])
    ]
Exemplo n.º 2
0
def facenet_serving(images):
    host = '172.20.15.85'
    port = 8500

    channel = implementations.insecure_channel(host, port)
    stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

    request = predict_pb2.PredictRequest()

    request.model_spec.name = '1'

    request.model_spec.signature_name = 'calculate_embeddings'

    image_data_arr = np.asarray(images)
    #print('image_data_arr',image_data_arr)
    #print('image_data',image_data_arr.dtype)
    input_size = image_data_arr.shape[0]

    dims = [tensor_shape_pb2.TensorShapeProto.Dim(size=input_size)]
    print('dims',dims)
    tensor_shape_proto = tensor_shape_pb2.TensorShapeProto(dim=dims)

    tensor_proto = tensor_pb2.TensorProto(
        dtype=types_pb2.DT_STRING,
        tensor_shape=tensor_shape_proto,
        string_val=[image_data for image_data in image_data_arr])
    print('tensor_proto', tensor_proto)
    request.inputs['images'].CopyFrom(tensor_proto)
    result = stub.Predict(request, 10.0)
    print(MakeNdarray(result.outputs['embeddings']))
    return MakeNdarray(result.outputs['embeddings'])
Exemplo n.º 3
0
def onet_serving(image, channel):
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'mtcnn'

    request.model_spec.signature_name = 'onet_predict'

    image_data_arr = generate_input_string(image)
    image_data_arr = np.asarray(image_data_arr).squeeze()
    num_image = image_data_arr.shape[0]

    dims = [tensor_shape_pb2.TensorShapeProto.Dim(size=num_image)]
    tensor_shape_proto = tensor_shape_pb2.TensorShapeProto(dim=dims)
    tensor_proto = tensor_pb2.TensorProto(
        dtype=types_pb2.DT_STRING,
        tensor_shape=tensor_shape_proto,
        string_val=[image_data for image_data in image_data_arr])
    request.inputs['images'].CopyFrom(tensor_proto)

    result = stub.Predict(request, 10.0)

    return [
        MakeNdarray(result.outputs['result1']),
        MakeNdarray(result.outputs['result2']),
        MakeNdarray(result.outputs['result3'])
    ]
Exemplo n.º 4
0
class TensorflowNode(object):

  # Keyed by old attribute names.
  attr_translator = {
    "_output_shapes": lambda self, x: list(map(lambda shape: get_tf_shape_as_list(shape.dim), x.list.shape)),
    "shape": lambda self, x: get_tf_shape_as_list(x.shape.dim),
    "T": lambda self, x: self.type_converter(x),
    "dtype": lambda self, x: self.type_converter(x),
    "value": lambda self, x: MakeNdarray(x.tensor),
    "seed2": lambda self, x: float(x.i),
    "seed": lambda self, x: float(x.i),
    "keep_dims": lambda self, x: int(x.b),
    "squeeze_dims": lambda self, x: list(x.list.i),
  }

  def __init__(self, node_proto):
    # storing a referece to the original protobuf object
    self.node_proto = node_proto
    self.name = node_proto.name
    self.op = node_proto.op
    self.inputs = list(node_proto.input)
    self.attr = {}
    for key, val in node_proto.attr.items():
      new_key = key

      if key in TF_ATTR_TO_ONNX_ATTR.keys():
        new_key = TF_ATTR_TO_ONNX_ATTR[key]

      if key in self.attr_translator.keys():
        self.attr[new_key] = self.attr_translator[key](self, val)
      else:
        self.attr[new_key] = val

  def type_converter(self, x):
    return TF_TYPE_TO_ONNX_TYPE[tf.as_dtype(x.type)]
Exemplo n.º 5
0
def pnet_serving(image, FLAGS):
    print('image shape ', np.shape(image))
    channel = grpc.insecure_channel(FLAGS.server)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'mtcnn'
    request.model_spec.signature_name = 'prediction_pnet_signature'
    request.inputs['pnet_images'].CopyFrom(
        tf.contrib.util.make_tensor_proto(image, shape=None, dtype=tf.float32))

    result = stub.Predict(request, 10.0)  # 10 secs timeout

    return [
        MakeNdarray(result.outputs['result1']),
        MakeNdarray(result.outputs['result2'])
    ]
Exemplo n.º 6
0
def const_handler(converter: TensorFlowConverter, tf_op: "tf.Operation"):
    tensor = tf_op.outputs[0]
    shape = [
        Placeholder() if dim.value is None else dim.value
        for dim in tensor.shape.dims
    ]
    value = MakeNdarray(tf_op.get_attr("value"))

    if len(shape) == 0:
        # Scalar variable
        variable = ConstantVariable(value.reshape([1]), Order([None]))

    else:
        variable = ConstantVariable(value, Order([None] * len(shape)))

    converter.set_variable(tensor, variable)
def rnet_serving(image):

    channel = grpc.insecure_channel("0.0.0.0:8500")
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

    request = predict_pb2.PredictRequest()

    request.model_spec.name = 'mtcnn'

    request.model_spec.signature_name = 'rnet_predict'

    tp = tf.make_tensor_proto(image, dtype=tf.float32, shape=image.shape)

    request.inputs['images'].CopyFrom(tp)
    result = stub.Predict(request, 10.0)
    return [MakeNdarray(result.outputs['result1']),
            MakeNdarray(result.outputs['result2'])]
def run(host, port, image, model, signature_name):
    # Read an image
    image_dir = image
    img_list = []
    class_name = []
    for file in os.listdir(image_dir):
        img = imread(os.path.join(image_dir, file))
        img = img.astype(np.float32)
        cla = file.split('.')[0]
        class_name.append(cla)
        img_list.append(img)
    images = np.stack(img_list)
    start = time.time()
    # Call prediction model to make prediction on the image
    channel = grpc.insecure_channel('{host}:{port}'.format(host=host,
                                                           port=port))
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    request = predict_pb2.PredictRequest()
    request.model_spec.name = model
    request.model_spec.signature_name = signature_name
    request.inputs['images'].CopyFrom(
        make_tensor_proto(images, shape=[images.shape[0], 160, 160, 3]))
    result = stub.Predict(request, 10.0)
    out = MakeNdarray(result.outputs['embeddings'])
    end = time.time()
    time_diff = end - start
    #print('out',out)
    print('time elapased: {}'.format(time_diff))
    # Reference:
    # How to access nested values
    # https://stackoverflow.com/questions/44785847/how-to-retrieve-float-val-from-a-predictresponse-object
    start = time.time()
    emb = out
    # emb = np.asarray(emb).squeeze()
    f = h5py.File(r'C:\Users\zxg\Desktop\facenettest\embeddings1.h5py', 'a')
    class_arr = [i for i in class_name]
    print('class_arr', class_arr)
    for i in range(len(class_arr)):
        f.create_dataset(class_arr[i], data=emb[i])
    # print('姓名:', class_arr[i])
    #print('特征:', emb[i])
    f.close()
    end = time.time() - start
    print('time:', end)
    return MakeNdarray(result.outputs['embeddings'])
def send_image_to_serving(input_content, url="localhost:8500", verbose=True):
    """
    입력 이미지를 Tensorflow Serving으로 송신하는 Function

    :param input_content: bytes of jpg or png
    :param url: GRPC server URL
    :param verbose:
    :return:
    """
    # (1) Serving의 입력값을 구성하기
    channel = grpc.insecure_channel(url)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'serving'
    request.model_spec.signature_name = 'serving_default'

    tensor_proto = tf.make_tensor_proto(input_content) # 이미지 byte 파일을 tensor proto로 변경
    request.inputs['image'].CopyFrom(tensor_proto)

    s = time.time()

    # (2) Serving에 입력값 전달하기
    result_future = stub.Predict.future(request, 10.25)
    response = result_future.result() # Response를 받을 때까지 Waiting

    if verbose:
        print(f"서버에서 처리하는 데 걸린 시간 : {time.time() - s:.3f}")

    # (3) Serving의 출력값 받아오기
    visualize_content = response.outputs['visualize'].string_val[0]
    summary = response.outputs['summarize']

    # (4) Serving의 출력값을 Json Format으로 변경하기
    summary_arr = MakeNdarray(summary)[0]
    summary_df = pd.DataFrame(summary_arr,
                              columns=['name', 'x', 'y', 'w', 'h', "confidence",
                                       'pixelSize', 'estimatedSize',
                                       'estimatedHorizontalLength',
                                       'estimatedVerticalLength',
                                       'includeMyRoad'])
    # Drop NaN Cases
    summary_df = summary_df[summary_df.pixelSize > 0]

    # name을 숫자(0,1,2,3,4,5)에서 이름(car,bump,manhole,steel,pothole,crack)로 변경
    summary_df.loc[:, 'name'] = summary_df.loc[:, 'name'].astype(np.int)
    summary_df.loc[:, 'name'] = summary_df.loc[:, 'name'].map(INSTANCE_LABELS)

    summary_df.loc[:, ['x','y','w','h']] = summary_df.loc[:, ['x','y','w','h']].astype(np.int)
    summary_df.loc[:, 'pixelSize'] = summary_df.loc[:, 'pixelSize'].astype(np.int)
    summary_df.loc[:, 'includeMyRoad'] =summary_df.loc[:, 'includeMyRoad'].astype(np.bool)

    summary_content = {
        "objs": summary_df.to_dict('row')
    }
    return visualize_content, summary_content
def pnet_serving(image):
    options = [('grpc.max_send_message_length', 103301268),('grpc.max_receive_message_length', 55558316)]
    channel = grpc.insecure_channel("0.0.0.0:8500", options = options)
    
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

    request = predict_pb2.PredictRequest()

    request.model_spec.name = 'mtcnn'

    request.model_spec.signature_name = 'pnet_predict'

    tp = tf.make_tensor_proto(image, dtype=tf.float32, shape=image.shape)

    request.inputs['images'].CopyFrom(tp)
    result = stub.Predict(request, 10.0)

    return [MakeNdarray(result.outputs['result1']),
            MakeNdarray(result.outputs['result2'])]
Exemplo n.º 11
0
def rnet_serving(image):
    host = '127.0.0.1'
    port = 9000

    channel = implementations.insecure_channel(host, port)
    stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

    request = predict_pb2.PredictRequest()

    request.model_spec.name = 'mtcnn'

    request.model_spec.signature_name = 'rnet_predict'

    tp = tf.make_tensor_proto(image, dtype=tf.float32, shape=image.shape)

    request.inputs['images'].CopyFrom(tp)

    result = stub.Predict(request, 10.0)
    return [
        MakeNdarray(result.outputs['result1']),
        MakeNdarray(result.outputs['result2'])
    ]
def run(host, port, image, model, signature_name):
    image_dir = image
    img_list = []
    class_name = []
    for file in os.listdir(image_dir):
        img = imread(os.path.join(image_dir, file))
        img = img.astype(np.float32)
        cla = file.split('.')[0]
        class_name.append(cla)
        img_list.append(img)
    images = np.stack(img_list)

    start = time.time()

    # Call prediction model to make prediction on the image
    channel = grpc.insecure_channel('{host}:{port}'.format(host=host,
                                                           port=port))
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    request = predict_pb2.PredictRequest()
    request.model_spec.name = model
    request.model_spec.signature_name = signature_name
    request.inputs['images'].CopyFrom(
        make_tensor_proto(images, shape=[images.shape[0], 160, 160, 3]))

    result = stub.Predict(request, 10.0)
    out = MakeNdarray(result.outputs['embeddings'])
    end = time.time()
    time_diff = end - start
    print('time elapased: {}'.format(time_diff))

    start = time.time()
    f = h5py.File(r'C:\Users\zxg\Desktop\facenettest\embeddings1.h5py', 'r')
    class_arr = [k for k in f.keys()]
    emb_arr = [f[k].value for k in f.keys()]
    print('emb_arr', emb_arr)
    emb = out
    # emb = np.asarray(emb).squeeze()
    for i in range(len(emb)):
        # print('特征',emb[i])
        diff = np.sum(np.square(emb[i] - emb_arr), axis=1)
        print(diff)
        min_diff = min(diff)
        print('距离:', min_diff)
        if min_diff < 1.24:
            index = np.argmin(diff)
            face_class = class_arr[index]
            print('识别结果:', face_class)
        else:
            print('不能识别')
    end = time.time() - start
    print('识别时间:', end)
def facenet_predict(image):

    options = [('grpc.max_send_message_length', 1000331268),
               ('grpc.max_receive_message_length', 555558316)]
    channel = grpc.insecure_channel("0.0.0.0:8500", options=options)

    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

    request = predict_pb2.PredictRequest()
    request.model_spec.name = "facenet"
    request.model_spec.signature_name = "serving_default"

    if image.ndim == 3:
        image = np.expand_dims(image, axis=0)
        image = prewhiten(image)

    tp = tf.make_tensor_proto(image, dtype=tf.float32, shape=image.shape)

    request.inputs["input_1"].CopyFrom(tp)
    result = stub.Predict(request, 10.0)

    return MakeNdarray(result.outputs['normalize'])
Exemplo n.º 14
0
for detection in dets:
    faces.append(sp(image, detection.rect))
faces_list = np.asarray(dlib.get_face_chips(image, faces, 160))
=# for face in faces_list:
# 	window = dlib.image_window()
# 	window.set_image(face)
# 	dlib.hit_enter_to_continue()
=

request = predict_pb2.PredictRequest()
request.model_spec.name = 'saved_model'
request.inputs['in'].CopyFrom(tf.make_tensor_proto(faces_list, dtype=tf.float32))
request.inputs['phase'].CopyFrom(tf.make_tensor_proto(False, dtype=tf.bool))
result = stub.Predict(request, 60.0)
# result_np = predictResponse_into_nparray(result, 'out')
base_emb = MakeNdarray(result.outputs['out'])


image2 = dlib.load_rgb_image('/home/billyzheng/Downloads/billy2.jpeg')
# image2 = scipy.misc.imresize(image2, 0.5)
dets2 = cnn_detector(image2, 1)
faces2 = dlib.full_object_detections()
for detection2 in dets2:
	faces2.append(sp(image2, detection2.rect))
faces_list2 = np.asarray(dlib.get_face_chips(image2, faces2, 160))
# for face in faces_list2:
# 	window = dlib.image_window()
# 	window.set_image(face)
# 	dlib.hit_enter_to_continue()

request2 = predict_pb2.PredictRequest()
Exemplo n.º 15
0
from onnx_tf.common import data_type

# Keyed by old attribute names.
__tf_attr_translator = {
    "_output_shapes":
    lambda x: list(
        map(lambda shape: get_tf_shape_as_list(shape.dim), x.list.shape)),
    "shape":
    lambda x: get_tf_shape_as_list(x.shape.dim),
    "T":
    lambda x: data_type.tf2onnx(x.type),
    "dtype":
    lambda x: data_type.tf2onnx(x.type),
    "value":
    lambda x: MakeNdarray(x.tensor),
    "seed2":
    lambda x: float(x.i),
    "seed":
    lambda x: float(x.i),
    "keep_dims":
    lambda x: int(x.b),
    "squeeze_dims":
    lambda x: list(x.list.i),
}

__onnx_attr_translator = {
    "axis": lambda x: int(x),
    "axes": lambda x: [int(a) for a in x],
    "dtype": lambda x: data_type.onnx2tf(x),
    "keepdims": lambda x: bool(x),
Exemplo n.º 16
0
def quantize_graph_def(graph_def,
                       skip=None,
                       output_nodes=None,
                       rel_tol=None,
                       only=None):
    """
  :type graph_def: GraphDef
  :type skip: set|list
  :type output_nodes: list
  :type rel_tol: float
  :type only: str
  :return: QuantizedGraph
  """
    if output_nodes is not None and len(output_nodes) > 0:
        graph_def = extract_sub_graph(graph_def, output_nodes)

    nodes = []
    items = []
    for node in graph_def.node:
        # check skip
        if should_skip(node, skip):
            nodes.append(node)
            continue

        # try convert to constant
        try:
            value = MakeNdarray(node.attr['value'].tensor)  # type: np.ndarray
        except TypeError:
            nodes.append(node)
            continue

        # check repeated field
        same_value = all_same_value(value, rel_tol)
        if same_value is not None:
            nodes.append(
                const_node(node.attr['dtype'].type,
                           np.array([same_value], dtype=value.dtype),
                           value.shape))
            continue

        # check data size
        elif value.size < 4096:
            nodes.append(node)
            continue

        # finally
        processed_node = NodeDef()
        processed_node.name = node.name
        processed_node.op = 'Placeholder'
        processed_node.attr['dtype'].type = node.attr['dtype'].type
        processed_node.attr['shape'].shape.CopyFrom(
            as_shape(value.shape).as_proto())
        nodes.append(processed_node)

        item = QuantizedItem()
        item.name = node.name
        item.dtype = node.attr['dtype'].type
        item.shape.extend(value.shape)
        print('quantize {}'.format(node.name))
        _fill(item, value, only=only)
        items.append(item)
    graph = QuantizedGraph()
    graph.graph.versions.CopyFrom(graph_def.versions)
    graph.graph.library.CopyFrom(graph_def.library)
    graph.graph.node.extend(nodes)
    graph.items.extend(items)
    return graph
Exemplo n.º 17
0
def _make_ndarray(tensor):
	if _TENSORFLOW_AVAILABLE:
		return make_ndarray(tensor)
	return MakeNdarray(tensor)