in jetson_inference/artifacts/aws.greengrass.JetsonDLRImageClassification/1.0.0/inference.py [0:0]
def predict(image_data):
r"""
Predict image with DLR.
:param image: numpy array of the Image inference with.
"""
try:
# Run DLR to perform inference with DLC optimized model
model_output = dlr_model.run(image_data)
max_score_id = np.argmax(model_output)
max_score = np.max(model_output)
print("max score id:",max_score_id)
print("class:",labels[max_score_id])
print("max score",str(max_score))
probabilities = model_output[0][0]
sort_classes_by_probability = np.argsort(probabilities)[::-1]
results_file = "{}/{}.log".format(results_directory,os.path.basename(os.path.realpath(model_path)))
message = '{"class":"' + labels[max_score_id] + '"' + ',"confidence":"' + str(max_score) +'"}'
payload = {
"message": message,
"timestamp": datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
}
topic = "demo/topic"
if enableSendMessages:
ipc_client.new_publish_to_iot_core().activate(
request=PublishToIoTCoreRequest(topic_name=topic, qos='0',
payload=json.dumps(payload).encode()))
with open(results_file, 'a') as f:
print("{}: Top {} predictions with score {} or above ".format(str(
datetime.now()), max_no_of_results, score_threshold), file=f)
for i in sort_classes_by_probability[:max_no_of_results]:
if probabilities[i] >= score_threshold:
print("[ Class: {}, Score: {} ]".format(
labels[i], probabilities[i]), file=f)
except Exception as e:
print("Exception occurred during prediction: %s", e)