def predict(image_data): PAYLOAD = {} PAYLOAD["timestamp"] = str(datetime.now(tz=timezone.utc)) PAYLOAD["inference-type"] = "object-detection" PAYLOAD[ "inference-description"] = "Top {} predictions with score {} or above ".format( config_utils.MAX_NO_OF_RESULTS, config_utils.SCORE_THRESHOLD) PAYLOAD["inference-results"] = [] input_tensors = [ Tensor( tensor_metadata=TensorMetadata( name=config_utils.tensor_name, data_type=5, shape=config_utils.tensor_shape, ), byte_data=image_data.tobytes(), ) ] request = PredictRequest( name=config_utils.MODEL_NAME, tensors=input_tensors, ) response = config_utils.agent_client.Predict(request) output_tensors = response.tensors capture_data(input_tensors, output_tensors) detections = [] for t in output_tensors: deserialized_bytes = np.frombuffer(t.byte_data, dtype=np.float32) detections.append(np.asarray(deserialized_bytes).tolist()) # Get 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:] ids, scores, bboxes = detections predicted = [] for i, cl in enumerate(scores): if len(predicted) == config_utils.MAX_NO_OF_RESULTS: break class_id = int(ids[i]) if (cl >= config_utils.SCORE_THRESHOLD) and (class_id not in predicted): predicted.append(class_id) result = { "Label": str(labels[class_id]), "Score": str(cl), "Boundaries": str(bboxes[i]), } PAYLOAD["inference-results"].append(result) config_utils.logger.info(dumps(PAYLOAD)) if config_utils.TOPIC.strip() != "": ipc_utils.IPCUtils().publish_results_to_cloud(PAYLOAD) else: config_utils.logger.warn( "No topic set to publish the inference results to the cloud.")
def predict(image_data): PAYLOAD = {} PAYLOAD["timestamp"] = str(datetime.now(tz=timezone.utc)) PAYLOAD["inference-type"] = "image-classification" PAYLOAD[ "inference-description"] = "Top {} predictions with score {} or above ".format( config_utils.MAX_NO_OF_RESULTS, config_utils.SCORE_THRESHOLD) PAYLOAD["inference-results"] = [] input_tensors = [ Tensor( tensor_metadata=TensorMetadata( name=config_utils.tensor_name, data_type=5, shape=config_utils.tensor_shape, ), byte_data=image_data.tobytes(), ) ] request = PredictRequest( name=config_utils.MODEL_NAME, tensors=input_tensors, ) response = config_utils.agent_client.Predict(request) output_tensors = response.tensors capture_data(input_tensors, output_tensors) predictions = [] for t in output_tensors: deserialized_bytes = np.frombuffer(t.byte_data, dtype=np.float32) predictions.append(np.asarray(deserialized_bytes).tolist()) probabilities = predictions[0] sort_classes_by_probability = np.argsort(probabilities)[::-1] for i in sort_classes_by_probability[:config_utils.MAX_NO_OF_RESULTS]: if probabilities[i] >= config_utils.SCORE_THRESHOLD: result = {"Label": str(labels[i]), "Score": str(probabilities[i])} PAYLOAD["inference-results"].append(result) config_utils.logger.info(dumps(PAYLOAD)) if config_utils.TOPIC.strip() != "": ipc_utils.IPCUtils().publish_results_to_cloud(PAYLOAD) else: config_utils.logger.info( "No topic set to publish the inference results to the cloud.")
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 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 greengrass_hello_world_run(): global edge_manager_client print('running now!') try: if not cap.isOpened(): print("Cannot open camera\n") exit(1) ret, img = cap.read() #if reading from file #img = cv2.imread(img_url) print('calling PredictRequest on images !') #resize input before serving frame = resize_short_within(img, short=512) nn_input_size = input_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] print("SHAPE:" + str(img.shape)) request = PredictRequest(name=model_name, tensors=[Tensor(tensor_metadata=TensorMetadata( name=tensor_name, data_type=5, shape=tensor_shape), byte_data=scaled_frame.tobytes())]) print("Calling Predict on the Image...") response = edge_manager_client.Predict(request) #read output tensors i = 0 output_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) output_detections.append(np.asarray(deserialized_bytes)) print(output_detections) #convert the bounding boxes new_list = [] for index,item in enumerate(output_detections[2]): if index % 4 == 0: new_list.append(output_detections[2][index-4:index]) output_detections[2] = new_list[1:] #write to an input image visualize_detection(copy_frame, output_detections, classes=object_categories, thresh=0.2) #save outputs save_path = os.path.join(os.getcwd(), "./", "output.jpg") cv2.imwrite(save_path, copy_frame, [int(cv2.IMWRITE_JPEG_QUALITY), 100]) except Exception as e: traceback.print_exc() channel.close() logger.error("Failed to Run: " + repr(e)) # Asynchronously schedule this function to be run again in X seconds Timer(2, greengrass_hello_world_run).start()
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)