From 3e9a831de3e96b5164f3f5a22eb2ee41cd3e4cb0 Mon Sep 17 00:00:00 2001
From: KaamalVN <76166181+KaamalVN@users.noreply.github.com>
Date: Thu, 8 Jun 2023 12:00:56 +0530
Subject: [PATCH 01/16] Add files via upload
---
..._image_processing_hyperparameter_tuning.py | 123 +++++++++++++++++
...dical_image_processing_initial_training.py | 127 +++++++++++++++++
src/model.py | 43 ++++++
src/open_api/openapi_model.py | 130 ++++++++++++++++++
4 files changed, 423 insertions(+)
create mode 100644 src/medical_image_processing_hyperparameter_tuning.py
create mode 100644 src/medical_image_processing_initial_training.py
create mode 100644 src/model.py
create mode 100644 src/open_api/openapi_model.py
diff --git a/src/medical_image_processing_hyperparameter_tuning.py b/src/medical_image_processing_hyperparameter_tuning.py
new file mode 100644
index 000000000..3f4350767
--- /dev/null
+++ b/src/medical_image_processing_hyperparameter_tuning.py
@@ -0,0 +1,123 @@
+import os
+import sys
+import argparse
+from pathlib import Path
+import time
+import warnings
+from cv2 import cv2
+import numpy as np
+from openvino.inference_engine import IECore
+import logging
+
+logging.basicConfig(level=logging.DEBUG)
+logger = logging.getLogger(__name__)
+warnings.filterwarnings("ignore")
+
+# Define variables with predefined values
+INPUT_IMAGE_SIZE = (300, 300)
+NUM_OUTPUT_CLASSES = 2
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser()
+ parser.add_argument("--datadir", type=str, default="./data/chest_xray", help="Provide the exact data path")
+ args = parser.parse_args()
+
+data_dir_path = args.datadir
+data_dir = Path("./data/chest_xray/train/NORMAL") and Path("./data/chest_xray/train/PNEUMONIA")
+data_files = []
+for p in data_dir.glob("**/*"):
+ if p.suffix in (".jpeg"):
+ data_files.append(p)
+try:
+ if len(data_files) == 0:
+ logger.info("Unable to find Images")
+except:
+ logger.info('Images not found or format not supported, execution failed')
+ sys.exit()
+
+
+# Define the method to create a path list where data needs to be read
+def create_path_list(abspath="None"):
+ pathlist = []
+ for (root, dirs, files) in os.walk(abspath):
+ for subdir in dirs:
+ for imgpath in os.listdir(os.path.join(abspath, subdir)):
+ if imgpath.endswith('.jpeg'):
+ img_append = os.path.join(abspath, subdir, imgpath)
+ pathlist.append(img_append)
+ break
+ return pathlist
+
+
+# Enter the paths of valid, training & testing just to make sure all the paths are correct
+ABS_VAL_PATH = os.path.join(data_dir_path, "val")
+logger.info("ABS_VAL_PATH is ============================================>%s", ABS_VAL_PATH)
+ABS_TRAIN_PATH = os.path.join(data_dir_path, "train")
+logger.info("ABS_TRAIN_PATH is ============================================>%s", ABS_TRAIN_PATH)
+ABS_TEST_PATH = os.path.join(data_dir_path, "test")
+logger.info("ABS_TEST_PATH is ============================================>%s", ABS_TEST_PATH)
+
+# Checking the train dataset
+if os.path.isdir(ABS_VAL_PATH) and os.path.isdir(ABS_TRAIN_PATH) and os.path.isdir(ABS_TEST_PATH):
+ logger.info("Data paths exist, executing the program")
+else:
+ logger.info("Valid path not found")
+ sys.exit()
+
+
+# Read the image from the path defined above
+def read_image(batch_size=4, LAST_INDEX=2, pathlist=None):
+ x_batch, y_batch = [], []
+ for imagepath in pathlist[LAST_INDEX:LAST_INDEX+batch_size]:
+ image = cv2.imread(imagepath)
+ image = cv2.resize(image, dsize=INPUT_IMAGE_SIZE)
+ image = image / 255.0
+ if imagepath.split('/')[-2] == 'NORMAL':
+ y = np.array([0, 1])
+ else:
+ y = np.array([1, 0])
+ x_batch.append(image)
+ y_batch.append(y)
+ x_batch_train = np.stack(x_batch, axis=0)
+ y_batch_train = np.stack(y_batch, axis=0)
+ return x_batch_train, y_batch_train
+
+
+# Placeholder code to check if Image is loading properly
+train_list = create_path_list(ABS_TRAIN_PATH)
+train_images, train_labels = read_image(batch_size=4, LAST_INDEX=0, pathlist=train_list)
+
+# Initialize the OpenVINO Inference Engine Core
+ie = IECore()
+
+# Load the IR files (XML and BIN) for the model
+model_xml = 'model.xml'
+model_bin = 'model.bin'
+net = ie.read_network(model=model_xml, weights=model_bin)
+
+# Get the input and output node names
+input_blob = next(iter(net.input_info))
+output_blob = next(iter(net.outputs))
+
+# Load the network onto the Intel hardware
+exec_net = ie.load_network(network=net, device_name='CPU')
+
+# Prepare the input image for inference
+input_data = np.expand_dims(train_images, axis=0)
+
+# Perform inference
+start_time = time.time()
+outputs = exec_net.infer(inputs={input_blob: input_data})
+end_time = time.time()
+inference_time = end_time - start_time
+
+# Process the outputs
+output_data = outputs[output_blob]
+predicted_classes = np.argmax(output_data, axis=1)
+
+# Print the predicted classes
+for predicted_class in predicted_classes:
+ print(f"Predicted class: {predicted_class}")
+
+# Print the inference time
+print(f"Inference time: {inference_time} seconds")
diff --git a/src/medical_image_processing_initial_training.py b/src/medical_image_processing_initial_training.py
new file mode 100644
index 000000000..d7beeeb37
--- /dev/null
+++ b/src/medical_image_processing_initial_training.py
@@ -0,0 +1,127 @@
+import os
+import sys
+import argparse
+from pathlib import Path
+import time
+import warnings
+from cv2 import cv2
+import numpy as np
+from openvino.inference_engine import IECore
+import logging
+
+logging.basicConfig(level=logging.DEBUG)
+logger = logging.getLogger(__name__)
+warnings.filterwarnings("ignore")
+
+# Defing variable with predefined value
+INPUT_IMAGE_SIZE = (300, 300)
+padding = "SAME"
+NUM_OUTPUT_CLASSES = 2
+code_batch_size = 20
+val_batch_size = 2
+learning_rate = 0.001
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser()
+ parser.add_argument("--datadir", type=str, default="./data/chest_xray", help="Provide the exact data path")
+ args = parser.parse_args()
+
+data_dir_path = args.datadir
+data_dir = Path("./data/chest_xray/train/NORMAL") and Path("./data/chest_xray/train/PNEUMONIA")
+data_files = []
+for p in data_dir.glob("**/*"):
+ if p.suffix in (".jpeg"):
+ data_files.append(p)
+try:
+ if(len(data_files) == 0):
+ logger.info("unable to find Images")
+except:
+ logger.info('Images not found or format not supported, execution failed')
+ sys.exit()
+
+
+# Defining the create path method where data needs to be read
+def create_path_list(abspath="None"):
+ pathlist = []
+ for (root, dirs, files) in os.walk(abspath):
+ for subdir in dirs:
+ for imgpath in os.listdir(os.path.join(abspath, subdir)):
+ if imgpath.endswith('.jpeg'):
+ img_append = os.path.join(abspath, subdir, imgpath)
+ pathlist.append(img_append)
+ break
+ return pathlist
+
+
+# Enter the paths of valid, training & testing just to make sure all the paths are correct
+ABS_VAL_PATH = data_dir_path + "/val"
+logger.info("ABS_VAL_PATH is ============================================>%s", ABS_VAL_PATH)
+ABS_TRAIN_PATH = data_dir_path + "/train"
+logger.info("ABS_TRAIN_PATH is ============================================>%s", ABS_TRAIN_PATH)
+ABS_TEST_PATH = data_dir_path + "/test"
+logger.info("ABS_TEST_PATH is ============================================>%s", ABS_TEST_PATH)
+
+# Checking the train dataset
+if os.path.isdir(ABS_VAL_PATH) and os.path.isdir(ABS_TRAIN_PATH) and os.path.isdir(ABS_TEST_PATH):
+ logger.info("Data paths exist, executing the program")
+else:
+ logger.info("Valid path not found")
+ sys.exit()
+
+
+# Read the image from the path defined above
+def read_image(batch_size=4, LAST_INDEX=2, pathlist=None):
+ x_batch, y_batch = [], []
+ for imagepath in pathlist[LAST_INDEX:LAST_INDEX+batch_size]:
+ image = cv2.imread(imagepath)
+ image = cv2.resize(image, dsize=INPUT_IMAGE_SIZE)
+ image = image / 255.0
+ if imagepath.split('/')[-2] == 'NORMAL':
+ y = np.array([0, 1])
+ else:
+ y = np.array([1, 0])
+ x_batch.append(image)
+ y_batch.append(y)
+ x_batch_train = np.stack(x_batch, axis=0)
+ y_batch_train = np.stack(y_batch, axis=0)
+ return x_batch_train, y_batch_train
+
+
+# Placeholder code to check if Image is loading properly
+train_list = create_path_list(ABS_TRAIN_PATH)
+train_images, train_labels = read_image(batch_size=4, LAST_INDEX=0, pathlist=train_list)
+
+# Initialize the OpenVINO Inference Engine Core
+ie = IECore()
+
+# Load the IR files (XML and BIN) for the model
+model_xml = 'model.xml'
+model_bin = 'model.bin'
+net = ie.read_network(model=model_xml, weights=model_bin)
+
+# Get the input and output node names
+input_blob = next(iter(net.input_info))
+output_blob = next(iter(net.outputs))
+
+# Load the network onto the Intel hardware
+exec_net = ie.load_network(network=net, device_name='CPU')
+
+# Prepare the input image for inference
+input_data = np.expand_dims(train_images, axis=0)
+
+# Perform inference
+start_time = time.time()
+outputs = exec_net.infer(inputs={input_blob: input_data})
+end_time = time.time()
+inference_time = end_time - start_time
+
+# Process the outputs
+output_data = outputs[output_blob]
+predicted_classes = np.argmax(output_data, axis=1)
+
+# Print the predicted classes
+for predicted_class in predicted_classes:
+ print(f"Predicted class: {predicted_class}")
+
+# Print the inference time
+print(f"Inference time: {inference_time} seconds")
diff --git a/src/model.py b/src/model.py
new file mode 100644
index 000000000..27e8169bd
--- /dev/null
+++ b/src/model.py
@@ -0,0 +1,43 @@
+import os
+import sys
+from pathlib import Path
+import warnings
+import argparse
+from openvino.inference_engine import IECore
+import logging
+
+logging.basicConfig(level=logging.DEBUG)
+logger = logging.getLogger(__name__)
+warnings.filterwarnings("ignore")
+
+MODEL_PATH = './model'
+model_meta_index = Path("./model/Medical_Diagnosis_CNN.meta") and Path("./model/Medical_Diagnosis_CNN.index")
+dir = os.path.dirname(os.path.realpath(__file__))
+
+def freeze_graph(model_dir, output_node_names):
+ if not os.path.exists(model_dir):
+ raise AssertionError("Export directory doesn't exist. Please specify an export directory: %s" % model_dir)
+
+ if not output_node_names:
+ print("You need to supply the name of a node to --output_node_names.")
+ return -1
+
+ model_xml = os.path.join(model_dir, 'model.xml')
+ model_bin = os.path.join(model_dir, 'model.bin')
+ output_model = os.path.join(model_dir, 'updated_model.xml')
+
+ ie = IECore()
+ net = ie.read_network(model=model_xml, weights=model_bin)
+ exec_net = ie.load_network(network=net, device_name='CPU')
+
+ exec_net.export(output_model)
+
+ print("Model exported successfully.")
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser()
+ parser.add_argument("--model_dir", type=str, default="./model/", help="Model folder to export")
+ parser.add_argument("--output_node_names", type=str, default="Softmax", help="The name of the output nodes, comma separated.")
+ args = parser.parse_args()
+
+ freeze_graph(args.model_dir, args.output_node_names)
diff --git a/src/open_api/openapi_model.py b/src/open_api/openapi_model.py
new file mode 100644
index 000000000..271ff056f
--- /dev/null
+++ b/src/open_api/openapi_model.py
@@ -0,0 +1,130 @@
+import os
+import sys
+import argparse
+from pathlib import Path
+import cv2
+import numpy as np
+from PIL import Image
+import warnings
+import logging
+from addict import Dict
+from compression.api import DataLoader, Metric
+from compression.engines.openvino_engine import OpenVINOEngine
+from compression.graph import load_model, save_model
+from compression.graph.model_utils import compress_model_weights
+from openvino.inference_engine import IECore
+
+logging.basicConfig(level=logging.DEBUG)
+logger = logging.getLogger(__name__)
+warnings.filterwarnings("ignore")
+
+
+class ClassificationDataLoader(DataLoader):
+ def __init__(self, data_source):
+ self.data_source = Path(data_source)
+ self.dataset = [p for p in self.data_source.glob("**/*") if p.suffix in (".png", ".jpeg")]
+ self.class_names = sorted([item.name for item in self.data_source.iterdir() if item.is_dir()])
+
+ def __len__(self):
+ return len(self.dataset)
+
+ def __getitem__(self, index):
+ filepath = self.dataset[index]
+ annotation = (index, self.class_names.index(filepath.parent.name))
+ image = self._read_image(filepath)
+ return annotation, image
+
+ def _read_image(self, index):
+ image = np.array(Image.open(index))
+ image = cv2.resize(image, (300, 300)).astype(np.float32)
+ image = image.transpose(2, 0, 1)
+ return image
+
+
+class Accuracy(Metric):
+ def __init__(self):
+ super().__init__()
+ self._name = "accuracy"
+ self._matches = []
+
+ @property
+ def value(self):
+ return {self._name: self._matches[-1]}
+
+ @property
+ def avg_value(self):
+ num_correct = np.count_nonzero(self._matches)
+ return {self._name: num_correct / len(self._matches)}
+
+ def update(self, output, target):
+ predict = np.argmax(output[0], axis=1)
+ match = predict == target
+ self._matches.append(match)
+
+ def reset(self):
+ self._matches = []
+
+ def get_attributes(self):
+ return {self._name: {"direction": "higher-better", "type": "accuracy"}}
+
+
+if __name__ == "__main__":
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--datapath', type=str, required=False, default=Path('/home/azureuser/oneAPI-MedicalDiagnosis-DL/data/chest_xray/val'), help='dataset path')
+ parser.add_argument('--modelpath', type=str, required=False, default=Path("model/Medical_Diagnosis_CNN.xml"), help='Model path trained')
+ FLAGS = parser.parse_args()
+ model_xml = Path(FLAGS.modelpath)
+ data_dir = Path(FLAGS.datapath)
+
+ if not model_xml.exists():
+ logger.info("Not Executing training notebook....")
+
+ model_config = Dict(
+ {
+ "model_name": "Medical_Diagnosis_CNN",
+ "model": str(model_xml),
+ "weights": str(model_xml.with_suffix(".bin")),
+ }
+ )
+
+ engine_config = Dict({"device": "CPU", "stat_requests_number": 2, "eval_requests_number": 2})
+
+ algorithms = [
+ {
+ "name": "DefaultQuantization",
+ "params": {
+ "target_device": "CPU",
+ "preset": "performance",
+ "stat_subset_size": 1000,
+ },
+ }
+ ]
+
+ model = load_model(model_config=model_config)
+ original_model = copy.deepcopy(model)
+
+ data_loader = ClassificationDataLoader(data_source=data_dir)
+
+ metric = Accuracy()
+
+ engine = OpenVINOEngine(config=engine_config, data_loader=data_loader, metric=metric)
+
+ pipeline = create_pipeline(algo_config=algorithms, engine=engine)
+
+ compressed_model = pipeline.run(model=model)
+
+ compress_model_weights(model=compressed_model)
+
+ compressed_model_paths = save_model(model=compressed_model, save_path=os.path.join(os.path.curdir, "model/optimized"))
+ compressed_model_xml = Path(compressed_model_paths[0]["model"])
+ logger.info(f"The quantized model is stored in {compressed_model_xml}")
+
+ original_metric_results = pipeline.evaluate(original_model)
+ if original_metric_results:
+ print(f"Accuracy of the original model: {next(iter(original_metric_results.values())):.5f}")
+ logger.info(f"Accuracy of the original model: {next(iter(original_metric_results.values()))}")
+
+ quantized_metric_results = pipeline.evaluate(compressed_model)
+ if quantized_metric_results:
+ print(f"Accuracy of the quantized model: {next(iter(quantized_metric_results.values())):.5f}")
+ logger.info(f"Accuracy of the quantized model: {next(iter(quantized_metric_results.values()))}")
From a3a710edd0586d7fe7c3be22519cbce43c5142c2 Mon Sep 17 00:00:00 2001
From: KaamalVN <76166181+KaamalVN@users.noreply.github.com>
Date: Thu, 8 Jun 2023 12:06:27 +0530
Subject: [PATCH 02/16] Update README.md
---
README.md | 40 +++++++++++++++++++++++++++++++++-------
1 file changed, 33 insertions(+), 7 deletions(-)
diff --git a/README.md b/README.md
index 81463bfd7..c32fa2859 100644
--- a/README.md
+++ b/README.md
@@ -1,17 +1,43 @@
# intel-oneAPI
-#### Team Name -
-#### Problem Statement -
-#### Team Leader Email -
+#### Team Name - Mind Crusaders
+#### Problem Statement - Medical Image Processing
+#### Team Leader Email - kaamal322@gmail.com
## A Brief of the Prototype:
- This section must include UML Daigrms and prototype description
+This project demonstrates the quantization of a Medical Image processing using Intel OpenVINO. Quantization is a compression technique that reduces the memory footprint and computation requirements of a neural network model while maintaining its accuracy. The quantized model can be deployed on resource-constrained devices without compromising performance.
## Tech Stack:
- List Down all technologies used to Build the prototype **Clearly mentioning Intel® AI Analytics Toolkits, it's libraries and the SYCL/DCP++ Libraries used**
+- Intel OpenVINO toolkit
+- Python 3.7 or above
+- OpenVINO Python API
+- OpenCV
+- NumPy
+- PIL
## Step-by-Step Code Execution Instructions:
- This Section must contain set of instructions required to clone and run the prototype, so that it can be tested and deeply analysed
+Step 1: Set up the Environment
+
+Ensure you have installed the required dependencies and libraries, including Intel oneAPI toolkits and OpenVINO.
+Make sure you have the appropriate Python environment set up with the necessary packages.
+Step 2: Prepare the Model
+
+Place your model files (model.xml and model.bin) in the "./model" directory.
+Confirm that the model files exist in the specified directory.
+Step 3: Run the Code
+
+Copy the provided code into a Python file (e.g., medical_image_processing.py).
+Open a terminal or command prompt and navigate to the directory where the Python file is located.
+Step 4: Execute the Code
+
+Run the Python script using the following command:
+python model.py
## What I Learned:
- Write about the biggest learning you had while developing the prototype
+I have learned the following key points:
+
+Model Export: The code demonstrates how to export a trained model for deployment using Intel's OpenVINO toolkit. It loads the model from the specified directory, consisting of the model files (model.xml and model.bin), and utilizes the IECore class to read the network and its weights.
+
+Inference Engine: The Intel IECore module is used to load the network onto the CPU device for inference. It provides a unified API to work with different deep learning frameworks and optimizes the execution of the network.
+
+Model Update and Export: After loading the network, the code executes the model on the CPU device to perform network inference. It then exports the updated model, saving it as "updated_model.xml" in the same directory.
From a563f19b36d96f9b3017893f5e208bdea47968df Mon Sep 17 00:00:00 2001
From: KaamalVN <76166181+KaamalVN@users.noreply.github.com>
Date: Thu, 8 Jun 2023 12:08:35 +0530
Subject: [PATCH 03/16] Add files via upload
---
Mind Crusaders.pptx | 2330 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 2330 insertions(+)
create mode 100644 Mind Crusaders.pptx
diff --git a/Mind Crusaders.pptx b/Mind Crusaders.pptx
new file mode 100644
index 000000000..803b97b22
--- /dev/null
+++ b/Mind Crusaders.pptx
@@ -0,0 +1,2330 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ / ... /
+ KaamalVN /
+ Intel-MIP /
+
+
+
+
+
+
+ Clear Command Palette
+
+
+
+
+
+
+
+ Tip:
+ Type # to search pull requests
+
+
+ Type ? for help and tips
+
+
+
+
+
+
+ Tip:
+ Type # to search issues
+
+
+ Type ? for help and tips
+
+
+
+
+
+
+ Tip:
+ Type # to search discussions
+
+
+ Type ? for help and tips
+
+
+
+
+
+
+ Tip:
+ Type ! to search projects
+
+
+ Type ? for help and tips
+
+
+
+
+
+
+ Tip:
+ Type @ to search teams
+
+
+ Type ? for help and tips
+
+
+
+
+
+
+ Tip:
+ Type @ to search people and organizations
+
+
+ Type ? for help and tips
+
+
+
+
+
+
+ Tip:
+ Type > to activate command mode
+
+
+ Type ? for help and tips
+
+
+
+
+
+
+ Tip:
+ Go to your accessibility settings to change your keyboard shortcuts
+
+
+ Type ? for help and tips
+
+
+
+
+
+
+ Tip:
+ Type author:@me to search your content
+
+
+ Type ? for help and tips
+
+
+
+
+
+
+ Tip:
+ Type is:pr to filter to pull requests
+
+
+ Type ? for help and tips
+
+
+
+
+
+
+ Tip:
+ Type is:issue to filter to issues
+
+
+ Type ? for help and tips
+
+
+
+
+
+
+ Tip:
+ Type is:project to filter to projects
+
+
+ Type ? for help and tips
+
+
+
+
+
+
+ Tip:
+ Type is:open to filter to open content
+
+
+ Type ? for help and tips
+
+
+
+
+
+
+ We’ve encountered an error and some results aren't available at this time. Type a new search or try again later.
+
+
+
+ No results matched your search
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Search for issues and pull requests
+
+ #
+
+
+
+ Search for issues, pull requests, discussions, and projects
+
+ #
+
+
+
+ Search for organizations, repositories, and users
+
+ @
+
+
+
+ Search for projects
+
+ !
+
+
+
+ Search for files
+
+ /
+
+
+
+ Activate command mode
+
+ >
+
+
+
+ Search your issues, pull requests, and discussions
+
+ # author:@me
+
+
+
+ Search your issues, pull requests, and discussions
+
+ # author:@me
+
+
+
+ Filter to pull requests
+
+ # is:pr
+
+
+
+ Filter to issues
+
+ # is:issue
+
+
+
+ Filter to discussions
+
+ # is:discussion
+
+
+
+ Filter to projects
+
+ # is:project
+
+
+
+ Filter to open issues, pull requests, and discussions
+
+ # is:open
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ You can’t perform that action at this time.
+
+
+
+
+
+
+
You signed in with another tab or window. Reload to refresh your session.
+
You signed out in another tab or window. Reload to refresh your session.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+