Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update team_solutions.md #9

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
3e63ef0
Update team_solutions.md
ic3b3rger Jan 29, 2023
fd99237
Create a.txt
ic3b3rger Jan 29, 2023
a810f75
Add files via upload
ic3b3rger Jan 29, 2023
525823e
Create a.txt
ic3b3rger Jan 29, 2023
a3c8d49
Add files via upload
ic3b3rger Jan 29, 2023
7e08af4
Create a.txt
ic3b3rger Jan 29, 2023
4d619a4
Add files via upload
ic3b3rger Jan 29, 2023
e3407c3
Create assets
ic3b3rger Jan 29, 2023
7030f8f
Add files via upload
ic3b3rger Jan 29, 2023
a96e76d
Add files via upload
ic3b3rger Jan 29, 2023
d74b93a
Create plots.txt
ic3b3rger Jan 29, 2023
b6e5e11
Add files via upload
ic3b3rger Jan 29, 2023
32154cb
Create a.txt
ic3b3rger Jan 29, 2023
5001270
Delete assets directory
ic3b3rger Jan 29, 2023
adf11b5
Add files via upload
ic3b3rger Jan 29, 2023
2b823da
Delete a.txt
ic3b3rger Jan 29, 2023
66b618e
Delete a.txt
ic3b3rger Jan 29, 2023
044984e
Delete a.txt
ic3b3rger Jan 29, 2023
fedceae
Delete a.txt
ic3b3rger Jan 29, 2023
f5f52c3
Update README.md
ic3b3rger Jan 29, 2023
f4265f1
Slide deck for the project
ic3b3rger Jan 29, 2023
0d16778
Update README.md
ic3b3rger Jan 29, 2023
704d223
Update README.md
ic3b3rger Jan 29, 2023
5398739
Update README.md
ic3b3rger Jan 29, 2023
d0e9b6c
Update README.md
ic3b3rger Jan 29, 2023
33165f3
Update README.md
ic3b3rger Jan 29, 2023
572224e
Update README.md
ic3b3rger Jan 29, 2023
7540700
Update README.md
ic3b3rger Jan 29, 2023
f5a8dff
Create fig.png
ic3b3rger Jan 29, 2023
5262163
Add files via upload
ic3b3rger Jan 29, 2023
f3e95f7
Delete fig2.png
ic3b3rger Jan 29, 2023
3304186
Add files via upload
ic3b3rger Jan 29, 2023
95ff357
Update README.md
ic3b3rger Jan 29, 2023
f88997e
Update README.md
ic3b3rger Jan 29, 2023
71d64d5
Update README.md
ic3b3rger Jan 29, 2023
be809a3
Update README.md
ic3b3rger Jan 29, 2023
7df33fe
Add files via upload
ic3b3rger Jan 29, 2023
84b6275
Update README.md
ic3b3rger Jan 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added team_solutions/QNN_for_MRI.pdf
Binary file not shown.
31 changes: 31 additions & 0 deletions team_solutions/README.md
Original file line number Diff line number Diff line change
@@ -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

74 changes: 74 additions & 0 deletions team_solutions/app/not_trainable.py
Original file line number Diff line number Diff line change
@@ -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))
20 changes: 20 additions & 0 deletions team_solutions/app/trying_w_mri_data.py
Original file line number Diff line number Diff line change
@@ -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

Binary file added team_solutions/assets/adaptive-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added team_solutions/assets/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added team_solutions/assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added team_solutions/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added team_solutions/assets/splash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added team_solutions/assets/teaicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added team_solutions/assets/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
27 changes: 27 additions & 0 deletions team_solutions/backend/app.py
Original file line number Diff line number Diff line change
@@ -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"
29 changes: 29 additions & 0 deletions team_solutions/backend/imageConverter.py
Original file line number Diff line number Diff line change
@@ -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")
162 changes: 162 additions & 0 deletions team_solutions/frontend/App.js
Original file line number Diff line number Diff line change
@@ -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 (
<SafeAreaView style={styles.container}>
<View style={styles.headerBox}>
<Text style={styles.headerText}>
QNN Image Recognition
</Text>
<Image source={require("./assets/logo.png")}
style={{width: imgDim, height: imgDim}}/>
</View>
<View style={styles.container}>
{this.state.cameraRollPer ? (
<Button
title="Upload an Image"
disabled={this.state.disableButton}
onPress={async () => {
await this.pickMedia();
this.setState((s, p) => {
return {
cameraRollPer: s.cameraRollPer,
disableButton: false,
};
});
}}
/>
) : (
<Text>Camera Roll Permission Required ! </Text>
)}

<View style={styles.container}>
<Image source={require("./assets/test.jpg")}
style={{width: imgDim * 2.5, height: imgDim * 2.5}}/>
</View>
</View>
</SafeAreaView>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#00000",
alignItems: "center",
justifyContent:'space-evenly',
},
headerText: {
color: '#ffffff',
fontSize: headerFontSize,
fontWeight:'bold',
},
headerBox: {
flex: 0.25,
flexDirection:'row',
justifyContent:'space-evenly',
backgroundColor: 'skyblue',
width: '100%',
alignItems: 'center',
},
});
33 changes: 33 additions & 0 deletions team_solutions/frontend/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"expo": {
"name": "frontend",
"slug": "frontend",
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/logo.png",
"userInterfaceStyle": "light",
"splash": {
"image": "./assets/logo.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"updates": {
"fallbackToCacheTimeout": 0
},
"assetBundlePatterns": [
"**/*"
],
"ios": {
"supportsTablet": true
},
"android": {
"adaptiveIcon": {
"foregroundImage": "./assets/logo.png",
"backgroundColor": "#FFFFFF"
}
},
"web": {
"favicon": "./assets/teaicon.png"
}
}
}
1 change: 1 addition & 0 deletions team_solutions/frontend/assets
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a.txt
6 changes: 6 additions & 0 deletions team_solutions/frontend/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function(api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
};
};
Loading