def run():
    with grpc.insecure_channel('unix:///tmp/sagemaker_edge_agent_example.sock') as channel:

        edge_manager_client = agent_pb2_grpc.AgentStub(channel)

        try:
            response = edge_manager_client.LoadModel(
                LoadModelRequest(url=model_url, name=model_name))
        except Exception as e:
            print(e)
            print('Model already loaded!')

        response = edge_manager_client.ListModels(ListModelsRequest())

        response = edge_manager_client.DescribeModel(
            DescribeModelRequest(name=model_name))

        # Mean and Std deviation of the RGB colors (collected from Imagenet dataset)
        mean = [123.68, 116.779, 103.939]
        std = [58.393, 57.12, 57.375]

        img = cv2.imread(image_url)
        frame = resize_short_within(img, short=SIZE, max_size=SIZE * 2)
        nn_input_size = SIZE
        nn_input = cv2.resize(frame, (nn_input_size, int(nn_input_size / 4 * 3)))
        nn_input = cv2.copyMakeBorder(nn_input, int(nn_input_size / 8), int(nn_input_size / 8),
                                      0, 0, cv2.BORDER_CONSTANT, value=(0, 0, 0))
        copy_frame = nn_input[:]
        nn_input = nn_input.astype('float32')
        nn_input = nn_input.reshape((nn_input_size * nn_input_size, 3))
        scaled_frame = np.transpose(nn_input)
        scaled_frame[0, :] = scaled_frame[0, :] - mean[0]
        scaled_frame[0, :] = scaled_frame[0, :] / std[0]
        scaled_frame[1, :] = scaled_frame[1, :] - mean[1]
        scaled_frame[1, :] = scaled_frame[1, :] / std[1]
        scaled_frame[2, :] = scaled_frame[2, :] - mean[2]
        scaled_frame[2, :] = scaled_frame[2, :] / std[2]

        request = PredictRequest(name=model_name, tensors=[Tensor(tensor_metadata=TensorMetadata(
            name=bytes(tensor_name, 'utf-8'), data_type=5, shape=tensor_shape), byte_data=scaled_frame.tobytes())])

        response = edge_manager_client.Predict(request)

        # read output tensors
        i = 0
        detections = []

        for t in response.tensors:
            print("Flattened RAW Output Tensor : " + str(i + 1))
            i += 1
            deserialized_bytes = np.frombuffer(t.byte_data, dtype=np.float32)
            detections.append(np.asarray(deserialized_bytes))

        print(detections)
        # convert the bounding boxes
        new_list = []
        for index, item in enumerate(detections[2]):
            if index % 4 == 0:
                new_list.append(detections[2][index - 4:index])
        detections[2] = new_list[1:]

        # get objects, scores, bboxes
        objects = detections[0]
        scores = detections[1]
        bounding_boxes = new_list[1:]

        print(objects)
        print(scores)
        print(bounding_boxes)

        response = edge_manager_client.UnLoadModel(
            UnLoadModelRequest(name=model_name))
def load_model(name, url):
    load_request = LoadModelRequest()
    load_request.url = url
    load_request.name = name
    config_utils.agent_client.LoadModel(load_request)
예제 #3
0
std=[58.393,57.12,57.375]

#Need to wait for sagemaker.edge.agent to start serving requests
print("sleeping for sometime before loading")
#time.sleep(30)
print("Waking up")

#Create a channel
channel = grpc.insecure_channel('unix:///tmp/sagemaker_edge_agent_example.sock')
print('getting stubs!')
edge_manager_client = agent_pb2_grpc.AgentStub(channel)

print('calling LoadModel!')
try:
    response = edge_manager_client.LoadModel(
        LoadModelRequest(url=model_url, name=model_name))
except Exception as e:
    print(e)
    print('model already loaded!')

print('calling ListModels!')
response = edge_manager_client.ListModels(ListModelsRequest())

print('calling DescribeModel')
response = edge_manager_client.DescribeModel(
    DescribeModelRequest(name=model_name))

def greengrass_hello_world_run():
    global edge_manager_client        
    print('running now!')
    try:
예제 #4
0
def run():
    global edge_manager_client
    ipc_client = awsiot.greengrasscoreipc.connect()

    try:
        response = edge_manager_client.LoadModel(
            LoadModelRequest(url=model_url, name=model_name))
    except Exception as e:
        print('Model failed to load.')
        print(e)

    while (True):
        time.sleep(30)
        print('New prediction')

        image_url = image_urls[random.randint(0, 3)]
        print('Picked ' + image_url + ' to perform inference on')

        # Scale image / preprocess
        img = cv2.imread(image_url)
        frame = resize_short_within(img, short=SIZE, max_size=SIZE * 2)
        nn_input_size = SIZE
        nn_input = cv2.resize(frame,
                              (nn_input_size, int(nn_input_size / 4 * 3)))
        nn_input = cv2.copyMakeBorder(nn_input,
                                      int(nn_input_size / 8),
                                      int(nn_input_size / 8),
                                      0,
                                      0,
                                      cv2.BORDER_CONSTANT,
                                      value=(0, 0, 0))
        copy_frame = nn_input[:]
        nn_input = nn_input.astype('float32')
        nn_input = nn_input.reshape((nn_input_size * nn_input_size, 3))
        scaled_frame = np.transpose(nn_input)

        # Call prediction
        request = PredictRequest(name=model_name,
                                 tensors=[
                                     Tensor(tensor_metadata=TensorMetadata(
                                         name=tensor_name,
                                         data_type=5,
                                         shape=tensor_shape),
                                            byte_data=scaled_frame.tobytes())
                                 ])

        try:
            response = edge_manager_client.Predict(request)
        except Exception as e:
            print('Prediction failed')
            print(e)

        # read output tensors and append them to matrix
        detections = []
        for t in response.tensors:
            deserialized_bytes = np.frombuffer(t.byte_data, dtype=np.float32)
            detections.append(np.asarray(deserialized_bytes))

        # Get the highest confidence inference result
        index = np.argmax(detections[0])
        result = class_labels[index]
        confidence = detections[0][index]

        # Print results in local log
        print('Result is ', result)
        print('Confidence is ', confidence)

        # Publish highest confidence result to AWS IoT Core
        print('Got inference results, publishing to AWS IoT Core')
        message = {"result": result, "confidence": str(confidence)}
        request = PublishToIoTCoreRequest()
        request.topic_name = inference_result_topic
        request.payload = bytes(json.dumps(message), "utf-8")
        request.qos = QOS.AT_LEAST_ONCE
        operation = ipc_client.new_publish_to_iot_core()
        operation.activate(request)
        future = operation.get_response()
        future.result(10)

        # capture inference results in S3 if enabled
        if capture_inference:
            print('Capturing inference data in Amazon S3')
            now = time.time()
            seconds = int(now)
            nanos = int((now - seconds) * 10**9)
            timestamp = Timestamp(seconds=seconds, nanos=nanos)
            request = CaptureDataRequest(
                model_name=model_name,
                capture_id=str(uuid.uuid4()),
                inference_timestamp=timestamp,
                input_tensors=[
                    Tensor(tensor_metadata=TensorMetadata(name="input",
                                                          data_type=5,
                                                          shape=tensor_shape),
                           byte_data=scaled_frame.tobytes())
                ],
                output_tensors=[
                    Tensor(tensor_metadata=TensorMetadata(name="output",
                                                          data_type=5,
                                                          shape=[1, 257]),
                           byte_data=detections[0].tobytes())
                ])
            try:
                response = edge_manager_client.CaptureData(request)
            except Exception as e:
                print('CaptureData request failed')
                print(e)
def run():
    global edge_manager_client
    try:
        cap = cv2.VideoCapture(stream_path)
        fps = cap.get(cv2.CAP_PROP_FPS)
        ret, captured_frame = cap.read()
    except Exception as e:
        print('Stream failed to open.')
        cap.release()
        print(e)
        exit (-1)

    try:
        response = edge_manager_client.LoadModel(
            LoadModelRequest(url=model_url, name=model_name))
    except Exception as e:
        print('Model failed to load.')
        print(e)

    while (cap.isOpened() and ret):

        frameId = cap.get(1)
        ret, frame = cap.read()

        #perform inference once per second
        if (frameId % math.floor(fps) == 0 ):
            img = preprocess_frame(frame)
            
            try:
                before = time.time()
                request = PredictRequest(name=model_name, tensors=[Tensor(tensor_metadata=TensorMetadata(
                    name=tensor_name, data_type=5, shape=tensor_shape), byte_data=img.tobytes())])
                response = edge_manager_client.Predict(request)
                after = time.time()
                performance = ((after)-(before))*1000
                detections = process_output_tensor(response)
                message = build_message(detections, performance, model_name)
                publish_results_to_iot_core (message)

            except Exception as e:
                print('Prediction failed')
                print(e)

            if capture_inference:
                print ('Capturing inference data in Amazon S3')
                now = time.time()
                seconds = int(now)
                nanos = int((now - seconds) * 10**9)
                timestamp = Timestamp(seconds=seconds, nanos=nanos)
                request = CaptureDataRequest(
                    model_name=model_name,
                    capture_id=str(uuid.uuid4()),
                    inference_timestamp=timestamp,
                    input_tensors=[Tensor(tensor_metadata=TensorMetadata(name="input", data_type=5, shape=tensor_shape), 
                        byte_data=img.tobytes())],
                    output_tensors=[Tensor(tensor_metadata=TensorMetadata(name="output", data_type=5, shape=[1,257]), 
                        byte_data=detections[0].tobytes())]
                )
                try:
                    response = edge_manager_client.CaptureData(request)
                except Exception as e:
                    print('CaptureData request failed')
                    print(e)