Commit
·
ddc862e
1
Parent(s):
70b750a
Upload 11 files
Browse files- .gitattributes +4 -35
- BackPropogation.py +53 -0
- CNN_imdb.keras +3 -0
- RNN_imdb.keras +3 -0
- __init__.py +46 -0
- imdb_DNN.keras +3 -0
- imdb_LSTM.keras +3 -0
- imdb_back_prop.pkl +0 -0
- imdb_perceptron.pkl +0 -0
- perceptron.py +46 -0
- streamlitapp.py +82 -0
.gitattributes
CHANGED
@@ -1,35 +1,4 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
1 |
+
CNN_imdb.keras filter=lfs diff=lfs merge=lfs -text
|
2 |
+
imdb_DNN.keras filter=lfs diff=lfs merge=lfs -text
|
3 |
+
imdb_LSTM.keras filter=lfs diff=lfs merge=lfs -text
|
4 |
+
RNN_imdb.keras filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BackPropogation.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from tqdm import tqdm
|
3 |
+
|
4 |
+
|
5 |
+
class BackPropogation:
|
6 |
+
def __init__(self,learning_rate=0.01, epochs=100,activation_function='step'):
|
7 |
+
self.bias = 0
|
8 |
+
self.learning_rate = learning_rate
|
9 |
+
self.max_epochs = epochs
|
10 |
+
self.activation_function = activation_function
|
11 |
+
|
12 |
+
|
13 |
+
def activate(self, x):
|
14 |
+
if self.activation_function == 'step':
|
15 |
+
return 1 if x >= 0 else 0
|
16 |
+
elif self.activation_function == 'sigmoid':
|
17 |
+
return 1 if (1 / (1 + np.exp(-x)))>=0.5 else 0
|
18 |
+
elif self.activation_function == 'relu':
|
19 |
+
return 1 if max(0,x)>=0.5 else 0
|
20 |
+
|
21 |
+
def fit(self, X, y):
|
22 |
+
error_sum=0
|
23 |
+
n_features = X.shape[1]
|
24 |
+
self.weights = np.zeros((n_features))
|
25 |
+
for epoch in tqdm(range(self.max_epochs)):
|
26 |
+
for i in range(len(X)):
|
27 |
+
inputs = X[i]
|
28 |
+
target = y[i]
|
29 |
+
weighted_sum = np.dot(inputs, self.weights) + self.bias
|
30 |
+
prediction = self.activate(weighted_sum)
|
31 |
+
|
32 |
+
# Calculating loss and updating weights.
|
33 |
+
error = target - prediction
|
34 |
+
self.weights += self.learning_rate * error * inputs
|
35 |
+
self.bias += self.learning_rate * error
|
36 |
+
|
37 |
+
print(f"Updated Weights after epoch {epoch} with {self.weights}")
|
38 |
+
print("Training Completed")
|
39 |
+
|
40 |
+
def predict(self, X):
|
41 |
+
predictions = []
|
42 |
+
for i in range(len(X)):
|
43 |
+
inputs = X[i]
|
44 |
+
weighted_sum = np.dot(inputs, self.weights) + self.bias
|
45 |
+
prediction = self.activate(weighted_sum)
|
46 |
+
predictions.append(prediction)
|
47 |
+
return predictions
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
|
CNN_imdb.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1c38e27f28119cbbe9d563e1a841c463934dced245c42b160c8e7e5617c1678f
|
3 |
+
size 391803285
|
RNN_imdb.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:55ef888fd4263b4d4b23e8c2a7543ee0292c61b60ce75deac03ebe34d9e132b9
|
3 |
+
size 2017417
|
__init__.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from tqdm import tqdm
|
3 |
+
|
4 |
+
|
5 |
+
class Perceptron:
|
6 |
+
|
7 |
+
def __init__(self,learning_rate=0.01, epochs=100,activation_function='step'):
|
8 |
+
self.bias = 0
|
9 |
+
self.learning_rate = learning_rate
|
10 |
+
self.max_epochs = epochs
|
11 |
+
self.activation_function = activation_function
|
12 |
+
|
13 |
+
|
14 |
+
def activate(self, x):
|
15 |
+
if self.activation_function == 'step':
|
16 |
+
return 1 if x >= 0 else 0
|
17 |
+
elif self.activation_function == 'sigmoid':
|
18 |
+
return 1 if (1 / (1 + np.exp(-x)))>=0.5 else 0
|
19 |
+
elif self.activation_function == 'relu':
|
20 |
+
return 1 if max(0,x)>=0.5 else 0
|
21 |
+
|
22 |
+
def fit(self, X, y):
|
23 |
+
n_features = X.shape[1]
|
24 |
+
self.weights = np.random.randint(n_features, size=(n_features))
|
25 |
+
for epoch in tqdm(range(self.max_epochs)):
|
26 |
+
for i in range(len(X)):
|
27 |
+
inputs = X[i]
|
28 |
+
target = y[i]
|
29 |
+
weighted_sum = np.dot(inputs, self.weights) + self.bias
|
30 |
+
prediction = self.activate(weighted_sum)
|
31 |
+
print("Training Completed")
|
32 |
+
|
33 |
+
def predict(self, X):
|
34 |
+
predictions = []
|
35 |
+
for i in range(len(X)):
|
36 |
+
inputs = X[i]
|
37 |
+
weighted_sum = np.dot(inputs, self.weights) + self.bias
|
38 |
+
prediction = self.activate(weighted_sum)
|
39 |
+
predictions.append(prediction)
|
40 |
+
return predictions
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
|
imdb_DNN.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:50ed7b8145632dc6f350464cd0d8f8a9507b3a9fecd987f2faa9bcbaec420de8
|
3 |
+
size 10723181
|
imdb_LSTM.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:dc939b31b4a8eab19db386502fa1d090f6cd5d8d84c9c4170f7228b746bb5b7c
|
3 |
+
size 4185638
|
imdb_back_prop.pkl
ADDED
Binary file (4.3 kB). View file
|
|
imdb_perceptron.pkl
ADDED
Binary file (2.28 kB). View file
|
|
perceptron.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from tqdm import tqdm
|
3 |
+
|
4 |
+
|
5 |
+
class Perceptron:
|
6 |
+
|
7 |
+
def __init__(self,learning_rate=0.01, epochs=100,activation_function='step'):
|
8 |
+
self.bias = 0
|
9 |
+
self.learning_rate = learning_rate
|
10 |
+
self.max_epochs = epochs
|
11 |
+
self.activation_function = activation_function
|
12 |
+
|
13 |
+
|
14 |
+
def activate(self, x):
|
15 |
+
if self.activation_function == 'step':
|
16 |
+
return 1 if x >= 0 else 0
|
17 |
+
elif self.activation_function == 'sigmoid':
|
18 |
+
return 1 if (1 / (1 + np.exp(-x)))>=0.5 else 0
|
19 |
+
elif self.activation_function == 'relu':
|
20 |
+
return 1 if max(0,x)>=0.5 else 0
|
21 |
+
|
22 |
+
def fit(self, X, y):
|
23 |
+
n_features = X.shape[1]
|
24 |
+
self.weights = np.random.randint(n_features, size=(n_features))
|
25 |
+
for epoch in tqdm(range(self.max_epochs)):
|
26 |
+
for i in range(len(X)):
|
27 |
+
inputs = X[i]
|
28 |
+
target = y[i]
|
29 |
+
weighted_sum = np.dot(inputs, self.weights) + self.bias
|
30 |
+
prediction = self.activate(weighted_sum)
|
31 |
+
print("Training Completed")
|
32 |
+
|
33 |
+
def predict(self, X):
|
34 |
+
predictions = []
|
35 |
+
for i in range(len(X)):
|
36 |
+
inputs = X[i]
|
37 |
+
weighted_sum = np.dot(inputs, self.weights) + self.bias
|
38 |
+
prediction = self.activate(weighted_sum)
|
39 |
+
predictions.append(prediction)
|
40 |
+
return predictions
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
|
streamlitapp.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
from tensorflow.keras.datasets import imdb
|
6 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
7 |
+
import pickle
|
8 |
+
import sys
|
9 |
+
|
10 |
+
sys.path.append(r"E:\3rd sem\Deep Learning\Streamlit\Perceptron")
|
11 |
+
sys.path.append(r"E:\3rd sem\Deep Learning\Streamlit\Back_propagatin")
|
12 |
+
# Load word index for Sentiment Classification
|
13 |
+
word_to_index = imdb.get_word_index()
|
14 |
+
|
15 |
+
# Function to perform sentiment classification
|
16 |
+
def sentiment_classification(new_review_text, model):
|
17 |
+
max_review_length = 500
|
18 |
+
new_review_tokens = [word_to_index.get(word, 0) for word in new_review_text.split()]
|
19 |
+
new_review_tokens = pad_sequences([new_review_tokens], maxlen=max_review_length)
|
20 |
+
prediction = model.predict(new_review_tokens)
|
21 |
+
if type(prediction) == list:
|
22 |
+
prediction = prediction[0]
|
23 |
+
return "Positive" if prediction > 0.5 else "Negative"
|
24 |
+
|
25 |
+
# Function to perform tumor detection
|
26 |
+
def tumor_detection(img, model):
|
27 |
+
img = Image.open(img)
|
28 |
+
img=img.resize((128,128))
|
29 |
+
img=np.array(img)
|
30 |
+
input_img = np.expand_dims(img, axis=0)
|
31 |
+
res = model.predict(input_img)
|
32 |
+
return "Tumor Detected" if res else "No Tumor"
|
33 |
+
|
34 |
+
# Streamlit App
|
35 |
+
st.title("Deep Prediction")
|
36 |
+
|
37 |
+
# Choose between tasks
|
38 |
+
task = st.radio("Select Task", ("Sentiment Classification", "Tumor Detection"))
|
39 |
+
|
40 |
+
if task == "Sentiment Classification":
|
41 |
+
# Input box for new review
|
42 |
+
new_review_text = st.text_area("Enter a New Review:", value="")
|
43 |
+
if st.button("Submit") and not new_review_text.strip():
|
44 |
+
st.warning("Please enter a review.")
|
45 |
+
|
46 |
+
if new_review_text.strip():
|
47 |
+
st.subheader("Choose Model for Sentiment Classification")
|
48 |
+
model_option = st.selectbox("Select Model", ("Perceptron", "Backpropagation", "DNN", "RNN", "LSTM"))
|
49 |
+
|
50 |
+
# Load models dynamically based on the selected option
|
51 |
+
if model_option == "Perceptron":
|
52 |
+
with open('imdb_perceptron.pkl', 'rb') as file:
|
53 |
+
model = pickle.load(file)
|
54 |
+
elif model_option == "Backpropagation":
|
55 |
+
with open('imdb_back_prop.pkl', 'rb') as file:
|
56 |
+
model = pickle.load(file)
|
57 |
+
elif model_option == "DNN":
|
58 |
+
model = load_model('imdb_DNN.keras')
|
59 |
+
elif model_option == "RNN":
|
60 |
+
model = load_model('RNN_imdb.keras')
|
61 |
+
elif model_option == "LSTM":
|
62 |
+
model = load_model('imdb_LSTM.keras')
|
63 |
+
|
64 |
+
if st.button("Classify Sentiment"):
|
65 |
+
result = sentiment_classification(new_review_text, model)
|
66 |
+
st.subheader("Sentiment Classification Result")
|
67 |
+
st.write(f"**{result}**")
|
68 |
+
|
69 |
+
elif task == "Tumor Detection":
|
70 |
+
st.subheader("Tumor Detection")
|
71 |
+
uploaded_file = st.file_uploader("Choose a tumor image...", type=["jpg", "jpeg", "png"])
|
72 |
+
|
73 |
+
if uploaded_file is not None:
|
74 |
+
# Load the tumor detection model
|
75 |
+
model = load_model('CNN_imdb.keras')
|
76 |
+
st.image(uploaded_file, caption="Uploaded Image.", use_column_width=False, width=200)
|
77 |
+
st.write("")
|
78 |
+
|
79 |
+
if st.button("Detect Tumor"):
|
80 |
+
result = tumor_detection(uploaded_file, model)
|
81 |
+
st.subheader("Tumor Detection Result")
|
82 |
+
st.write(f"**{result}**")
|