diff --git a/team_solutions/QNN_for_MRI.pdf b/team_solutions/QNN_for_MRI.pdf new file mode 100644 index 0000000..954b91e Binary files /dev/null and b/team_solutions/QNN_for_MRI.pdf differ diff --git a/team_solutions/README.md b/team_solutions/README.md new file mode 100644 index 0000000..0e94840 --- /dev/null +++ b/team_solutions/README.md @@ -0,0 +1,31 @@ +# MRI classification using Quanvolutional layer and QNN +*Members*: *Chinmay Tompe, Joshua B., Kevin Tang, Max Huber, Zain Mughal* + +v0.0 Submission for iQuHack 2023 + +![team logo](https://github.com/chinmaytompe/2023_CovalentxIBM/blob/main/team_solutions/images/figure1.png) + + + +CMD TO RUN FRONTEND: +$ expo start + +CMD TO RUN BACKEND: +$ flask run -h 10.189.37.233 + +Our approach to the ColvalentxIBM challenge'c call for internet and people driven applications includes providing a quantum assisted and quantum enabled Neural Network to classify MRI brain scans with some and no artificates realted to Alzeihmer's (non, mild and moderate). We implemented an existing solution for image classification ( https://ieeexplore.ieee.org/abstract/document/9643516 ) that reviews various configurations of models for image classfication on the MNIST and Fashion MNIST datasets. + +The authors of the paper used max pooling and dimension reduction on the MNIST data images to redeuce it down to 14x14 from 28x28, however, given our applicationn in medical diagnosis, resolution was essential to be preserved. + +*Description of files* + +![architecture](https://github.com/chinmaytompe/2023_CovalentxIBM/blob/main/team_solutions/images/figure0.png) + +For **Training**: run '''bash python run.py ''' trains the QNN and saves the model in results. + +*Backends tested on*: + +1. IBM QASM simulator +2. IBM_OSLO +3. Pennylane default simulator + diff --git a/team_solutions/app/not_trainable.py b/team_solutions/app/not_trainable.py new file mode 100644 index 0000000..b32d717 --- /dev/null +++ b/team_solutions/app/not_trainable.py @@ -0,0 +1,74 @@ +import torch +import torch.nn as nn +import numpy as np +import pennylane as qml + +torch.manual_seed(0) + +n_qubits = 4 +n_layers = 4 +n_class = 3 +n_features = 36608 +var_per_qubit = n_qubits +kernel_size = n_qubits +stride = 4 + +dev = qml.device("default.qubit", wires = n_qubits) + +def circuit(inputs, weights): + encoding_gates = ['RZ', 'RX']*int(var_per_qubit/2) + for qub in range(n_qubits): + qml.Hadamard(wires = qub) + for i in range(var_per_qubit): + if (qub * var_per_qubit + i) < len(inputs): + exec('qml.{}({}, wires = {})'.format(encoding_gates[i], inputs[qub * var_per_qubit + i], qub)) + else: #load nothing + pass + + for l in range(n_layers): + for i in range(n_qubits): + qml.CRZ(weights[l, i], wires = [i, (i + 1) % n_qubits]) + #qml.CNOT(wires = [i, (i + 1) % n_qubits]) + for j in range(n_qubits, 2*n_qubits): + qml.RY(weights[l, j], wires = j % n_qubits) + + _expectations = [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)] + return _expectations + #return qml.expval(qml.PauliZ(0)) + + +class Net(nn.Module): + # define nn + def __init__(self): + super(Net, self).__init__() + + weight_shapes = {"weights": (n_layers, 2 * n_qubits)} + qnode = qml.QNode(circuit, dev, interface = 'torch', diff_method = 'adjoint') + self.ql1 = qml.qnn.TorchLayer(qnode, weight_shapes) + for p in self.ql1.parameters(): + p.requires_grad=False + self.ql1.weights.data.uniform_(-np.pi, np.pi) + + self.fc1 = nn.Linear(9152, n_class * 2) + self.lr1 = nn.LeakyReLU(0.1) + self.fc2 = nn.Linear(n_class * 2, n_class) + + def forward(self, X): + bs = X.shape[0] + X = X.view(bs, 208, 176) + XL = [] + + for i in range(0, 208, stride): + for j in range(0, 176, stride): + XL.append(self.ql1(torch.flatten(X[:, i:i+kernel_size, j:j+kernel_size], start_dim = 1))) + + X = torch.cat(XL, dim = 1) + X = self.fc1(X) + X = self.lr1(X) + X = self.fc2(X) + return X + +if __name__ == '__main__': + network = Net() + random_input = torch.rand(1, n_features) + print(network(random_input)) diff --git a/team_solutions/app/trying_w_mri_data.py b/team_solutions/app/trying_w_mri_data.py new file mode 100644 index 0000000..6f079dd --- /dev/null +++ b/team_solutions/app/trying_w_mri_data.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +"""Trying w MRI Data + +Automatically generated by Colaboratory. + +Original file is located at + https://colab.research.google.com/drive/147tFM3MNyxC6gi31qUhkm3629Uqm5RuT +""" + +!git clone https://github.com/mahabubul-alam/iccad_2021_invited_QML + + + +!pip install pennylane + +# Commented out IPython magic to ensure Python compatibility. +# %cd iccad_2021_invited_QML/ + +!python run.py + diff --git a/team_solutions/assets/adaptive-icon.png b/team_solutions/assets/adaptive-icon.png new file mode 100644 index 0000000..03d6f6b Binary files /dev/null and b/team_solutions/assets/adaptive-icon.png differ diff --git a/team_solutions/assets/favicon.png b/team_solutions/assets/favicon.png new file mode 100644 index 0000000..e75f697 Binary files /dev/null and b/team_solutions/assets/favicon.png differ diff --git a/team_solutions/assets/icon.png b/team_solutions/assets/icon.png new file mode 100644 index 0000000..a0b1526 Binary files /dev/null and b/team_solutions/assets/icon.png differ diff --git a/team_solutions/assets/logo.png b/team_solutions/assets/logo.png new file mode 100644 index 0000000..161e9cd Binary files /dev/null and b/team_solutions/assets/logo.png differ diff --git a/team_solutions/assets/splash.png b/team_solutions/assets/splash.png new file mode 100644 index 0000000..0e89705 Binary files /dev/null and b/team_solutions/assets/splash.png differ diff --git a/team_solutions/assets/teaicon.png b/team_solutions/assets/teaicon.png new file mode 100644 index 0000000..0a0c3df Binary files /dev/null and b/team_solutions/assets/teaicon.png differ diff --git a/team_solutions/assets/test.jpg b/team_solutions/assets/test.jpg new file mode 100644 index 0000000..f2c922b Binary files /dev/null and b/team_solutions/assets/test.jpg differ diff --git a/team_solutions/backend/__pycache__/app.cpython-38.pyc b/team_solutions/backend/__pycache__/app.cpython-38.pyc new file mode 100644 index 0000000..9fc1aef Binary files /dev/null and b/team_solutions/backend/__pycache__/app.cpython-38.pyc differ diff --git a/team_solutions/backend/app.py b/team_solutions/backend/app.py new file mode 100644 index 0000000..8b1a2d8 --- /dev/null +++ b/team_solutions/backend/app.py @@ -0,0 +1,27 @@ +from flask import Flask, Response, request, jsonify +from io import BytesIO +import base64 +from flask_cors import CORS, cross_origin +import os +import sys + +app = Flask(__name__) +cors = CORS(app) + + +@app.route("/image", methods=['GET', 'POST']) +def image(): + if(request.method == "POST"): + bytesOfImage = request.get_data() + with open('image.jpeg', 'wb') as out: + out.write(bytesOfImage) + return "Image read" + + +@app.route("/video", methods=['GET', 'POST']) +def video(): + if(request.method == "POST"): + bytesOfVideo = request.get_data() + with open('video.mp4', 'wb') as out: + out.write(bytesOfVideo) + return "Video read" \ No newline at end of file diff --git a/team_solutions/backend/imageConverter.py b/team_solutions/backend/imageConverter.py new file mode 100644 index 0000000..8eb7e38 --- /dev/null +++ b/team_solutions/backend/imageConverter.py @@ -0,0 +1,29 @@ +# import the modules +import os +from os import listdir + +from PIL import Image, ImageOps; +import numpy as np; + +total = list() + +counter = 0 + +# 0 is non demented +# 1 is mild demented +# 2 is moderate demented + +# get the path/directory +folder_dir = r"C:\IQHack 2023\Training\Test" +print('Start') +for images in os.listdir(folder_dir): + + temp = Image.open(folder_dir + "\\" + images) + img = ImageOps.grayscale(temp) + + data = np.array(img).ravel() + data = np.append(data, (2)) + total.append(data) + +np.savetxt("Test.csv", total, delimiter=",") +print("Done") \ No newline at end of file diff --git a/team_solutions/frontend/App.js b/team_solutions/frontend/App.js new file mode 100644 index 0000000..fd1c2d3 --- /dev/null +++ b/team_solutions/frontend/App.js @@ -0,0 +1,162 @@ +import React, {Component} from "react"; +import {Button, Image, SafeAreaView, StyleSheet, Text, View, Dimensions} from "react-native"; + +const windowWidth = Dimensions.get('window').width; +const windowHeight = Dimensions.get('window').height; + +let imgDim = 150; +let headerFontSize = 55; + +if (windowWidth > 1000) { + imgDim = 200; + headerFontSize = 70; +} else { + imgDim = 100; + headerFontSize = 40; +} + +//Importing the installed libraries +import * as FS from "expo-file-system"; +import * as ImagePicker from "expo-image-picker"; + +export default class App extends Component { + constructor(props) { + super(props); + + this.state = { + cameraRollPer: null, + disableButton: false, + }; + } + + async componentDidMount() { + const {status} = await ImagePicker.requestMediaLibraryPermissionsAsync(); + this.setState((state, props) => { + return { + cameraRollPer: status === "granted", + disableButton: false, + }; + }); + } + + uriToBase64 = async (uri) => { + return await FS.readAsStringAsync(uri, { + encoding: FS.EncodingType.Base64, + }); + }; + + pickMedia = async () => { + this.setState((state, props) => { + return { + cameraRollPer: state.cameraRollPer, + disableButton: true, + }; + }); + let result = await ImagePicker.launchImageLibraryAsync({ + mediaTypes: ImagePicker.MediaTypeOptions.All, + base64: true, + }); + if (result.cancelled) { + return; + } + if (result.type == "image") { + await this.toServer({ + type: result.type, + base64: result.base64, + uri: result.uri, + }); + } else { + let base64 = await this.uriToBase64(result.uri); + await this.toServer({ + type: result.type, + base64: base64, + uri: result.uri, + }); + } + }; + + toServer = async (mediaFile) => { + let type = mediaFile.type; + let schema = "http://"; + let host = "10.189.37.233"; + let route = ""; + let port = "5000"; + let url = ""; + let content_type = ""; + type === "image" + ? ((route = "/image"), (content_type = "image/jpeg")) + : ((route = "/video"), (content_type = "video/mp4")); + url = schema + host + ":" + port + route; + + let response = await FS.uploadAsync(url, mediaFile.uri, { + headers: { + "content-type": content_type, + }, + httpMethod: "POST", + uploadType: FS.FileSystemUploadType.BINARY_CONTENT, + }); + + console.log(response.headers); + console.log(response.body); + }; + + render() { + return ( + + + + QNN Image Recognition + + + + + {this.state.cameraRollPer ? ( +