<jupyter_start><jupyter_text>About this exampleThis example shows how you can deploy COVID-Net model to analyze X-Ray scans of chest X-rays to detect COVID. By using BlindAI, people can send data for the AI to analyze their medical images without having to fear privacy leaks.COVID-Net is a deep CNN to detect COVID from chest X-rays. You can learn more about it on the [COVID-Net repository](https://github.com/lindawangg/COVID-Net).More information on this use case can be found on our blog post [Confidential medical image analysis with COVID-Net and BlindAI](https://blog.mithrilsecurity.io/confidential-covidnet-with-blindai/).  Installing dependencies Install the dependencies this example needs.<jupyter_code>!pip install pillow numpy opencv-python onnxruntime matplotlib<jupyter_output><jupyter_text>Install the latest version of BlindAI.<jupyter_code>!pip install blindai<jupyter_output><jupyter_text>Preparing the model For this example, we will directly download a Covid-NET model that has already been trained. The model is already in ONNX file so no need to export it.Because the file is rather big, the download might take some time.<jupyter_code>!wget --quiet --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1Rzl_XpV_kBw-lzu_5xYpc8briFd7fjvc' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1Rzl_XpV_kBw-lzu_5xYpc8briFd7fjvc" -O COVID-Net-CXR-2.onnx && rm -rf /tmp/cookies.txt<jupyter_output><empty_output><jupyter_text>Deployment on BlindAI Please make sure the **server is running**. To launch the server, refer to the [Launching the server](https://docs.mithrilsecurity.io/getting-started/quick-start/run-the-blindai-server) documentation page. If you have followed the steps and have the Docker image ready, this mean you simply have to run `docker run -it -p 50051:50051 -p 50052:50052 mithrilsecuritysas/blindai-server-sim:latest` So the first thing we need to do is to connect securely to the BlindAI server instance. Here we will use simulation mode for ease of use. This means that we do not leverage the hardware security propertiers of secure enclaves, but we do not need to run the Docker image with a specific hardware.If you wish to run this example in hardware mode, you need to prepare the `host_server.pem` and `policy.toml` files. Learn more on the [Deploy on Hardware](https://docs.mithrilsecurity.io/getting-started/deploy-on-hardware) documentation page.<jupyter_code>from blindai.client import BlindAiClient, ModelDatumType

# Launch client
client = BlindAiClient()

# Simulation mode
client.connect_server(addr="localhost", simulation=True)

# Hardware mode
# client.connect_server(addr="localhost", policy="./policy.toml", certificate="./host_server.pem")<jupyter_output>WARNING:root:Untrusted server certificate check bypassed
WARNING:root:Attestation process is bypassed: running without requesting and checking attestation<jupyter_text>Then, upload the model inside the BlindAI server. This simply means uploading the ONNX file created before.When uploading the model, we have to precise the shape of the input and the data type. In this case, because we use a CNN model, we simply need to send floats of the image normalized between 0 and 1.<jupyter_code>client.upload_model(model="./COVID-Net-CXR-2.onnx", shape=(1,480,480,3), dtype=ModelDatumType.F32)<jupyter_output><empty_output><jupyter_text>Sending data for  confidential prediction Now it's time to check it's working live!We will just prepare some input for the model inside the secure enclave of BlindAI to process it. First, we need to fetch the CXR image to send to the model.<jupyter_code>!wget --quiet https://raw.githubusercontent.com/lindawangg/COVID-Net/master/assets/ex-covid.jpeg<jupyter_output><empty_output><jupyter_text>We will use the same preprocessing functions as in the [COVID-Net repository](https://github.com/lindawangg/COVID-Net).<jupyter_code># This uses OpenCV for image processing
import cv2

def crop_top(img, percent=0.15):
    offset = int(img.shape[0] * percent)
    return img[offset:]

def central_crop(img):
    size = min(img.shape[0], img.shape[1])
    offset_h = int((img.shape[0] - size) / 2)
    offset_w = int((img.shape[1] - size) / 2)
    return img[offset_h:offset_h + size, offset_w:offset_w + size]

def process_image_file(filepath, size, top_percent=0.08, crop=True):
    img = cv2.imread(filepath)
    img = crop_top(img, percent=top_percent)
    if crop:
        img = central_crop(img)
    img = cv2.resize(img, (size, size))
    return img<jupyter_output><empty_output><jupyter_text>We can now load the image we have downloaded and preprocess it.<jupyter_code>import numpy as np

img = process_image_file("./ex-covid.jpeg", size=480)
img = img.astype("float32") / 255.0
img = img[np.newaxis,:,:,:]<jupyter_output><empty_output><jupyter_text>We can have a look at the model input.<jupyter_code>import matplotlib.pyplot as plt

plt.imshow(img[0])<jupyter_output><empty_output><jupyter_text>For data marshalling reason, we will flatten the image and convert it to a list.<jupyter_code>flattened_img = img.flatten().tolist()<jupyter_output><empty_output><jupyter_text>We can then send the data to be processed by the BlindAI server!<jupyter_code>response = client.run_model(flattened_img)
plt.imshow(img[0])
plt.title(f"Probability of COVID positivity: {response.output[1]}")

print(response.output)<jupyter_output>[0.012251622974872589, 0.987748384475708]<jupyter_text>Here we can compare the results against the original prediction.<jupyter_code>import onnxruntime

ort_session = onnxruntime.InferenceSession("COVID-Net-CXR-2.onnx")
ort_inputs = {ort_session.get_inputs()[0].name: img}

onnx_outputs = ort_session.run(None, ort_inputs)
print(f"Probability of COVID positivity from original model: {onnx_outputs[0][0][1]}")<jupyter_output>Probability of COVID positivity from original model: 0.9877482652664185
{'fraction_non_alphanumeric': 0.09948683992716438, 'fraction_numerical': 0.02151961595762291, 'mean_word_length': 5.661521499448732, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '247109', 'license': 'Apache-2.0', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1926, 'n_words': 593, 'path': 'examples/covidnet/BlindAI-COVID-Net.ipynb', 'repo_name': 'mithril-security/blindai', 'size': '6041'}