Spaces:
Sleeping
Sleeping
Upload 15 files
Browse files- BackPropogation1.py +53 -0
- Perceptron.py +46 -0
- app.py +159 -0
- backprop_model.pkl +3 -0
- backrpop_tokeniser.pkl +3 -0
- dnn_model.pkl +3 -0
- dnn_tokeniser.pkl +3 -0
- lstm_model.pkl +3 -0
- lstm_tokeniser.pkl +3 -0
- perceptron_model_saved.pkl +3 -0
- perceptron_tokeniser_saved.pkl +3 -0
- requirements.txt +0 -0
- rnn_model.pkl +3 -0
- rnn_tokeniser.pkl +3 -0
- tumor_detection_model.pkl +3 -0
BackPropogation1.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 |
+
|
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 |
+
|
app.py
ADDED
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import numpy as np
|
3 |
+
import streamlit as st
|
4 |
+
import cv2
|
5 |
+
import tensorflow as tf
|
6 |
+
from tqdm import tqdm
|
7 |
+
from PIL import Image
|
8 |
+
import os
|
9 |
+
from tensorflow.keras.preprocessing import sequence
|
10 |
+
|
11 |
+
st.title("DL-Classifier")
|
12 |
+
|
13 |
+
task1 = st.selectbox('Select One',("Choose any","Sentiment Classification", 'Tumor Detection'))
|
14 |
+
|
15 |
+
|
16 |
+
#choosing tumor detection
|
17 |
+
#CNN
|
18 |
+
if task1=="Tumor Detection":
|
19 |
+
st.subheader("Tumor Detection")
|
20 |
+
with open("tumor_detection_model.pkl", "rb") as model_file:
|
21 |
+
cnn_model = pickle.load(model_file)
|
22 |
+
|
23 |
+
img =st.file_uploader("choose the image",type=('jpg','jpeg','png'))
|
24 |
+
def cnn_make_prediction(img,cnn_model):
|
25 |
+
img=Image.open(img)
|
26 |
+
img=img.resize((128,128))
|
27 |
+
img=np.array(img)
|
28 |
+
input_img = np.expand_dims(img, axis=0)
|
29 |
+
res = cnn_model.predict(input_img)
|
30 |
+
if res:
|
31 |
+
return"Tumor"
|
32 |
+
else:
|
33 |
+
return"No Tumor"
|
34 |
+
if img != None:
|
35 |
+
img_f="D:/SEM 3/DL/DL-ALGORITHMS/CNN/tumor_detection/tumordata/pred/"
|
36 |
+
img_p=img_f + img.name
|
37 |
+
pred=cnn_make_prediction(img_p,cnn_model)
|
38 |
+
st.write(pred)
|
39 |
+
|
40 |
+
|
41 |
+
#choosing classification
|
42 |
+
|
43 |
+
|
44 |
+
if task1=="Sentiment Classification":
|
45 |
+
st.subheader("Sentiment Classification")
|
46 |
+
clss_model= st.radio("Select Classification Model:",("RNN","DNN","Backpropagation",'Perceptron','LSTM'))
|
47 |
+
select_model=None
|
48 |
+
|
49 |
+
if clss_model=="RNN":
|
50 |
+
with open("rnn_model.pkl",'rb') as model_file:
|
51 |
+
rnn_model=pickle.load(model_file)
|
52 |
+
with open("rnn_tokeniser.pkl",'rb') as tokeniser_file:
|
53 |
+
rnn_tokeniser=pickle.load(tokeniser_file)
|
54 |
+
|
55 |
+
st.subheader('Spam Classification')
|
56 |
+
|
57 |
+
input=st.text_input("Enter your message here:")
|
58 |
+
def rnn_pred(input):
|
59 |
+
max_length=10
|
60 |
+
encoded_test = rnn_tokeniser.texts_to_sequences(input)
|
61 |
+
padded_test = tf.keras.preprocessing.sequence.pad_sequences(encoded_test, maxlen=max_length, padding='post')
|
62 |
+
predict= (rnn_model.predict(padded_test) > 0.5).astype("int32")
|
63 |
+
if predict:
|
64 |
+
return "Spam "
|
65 |
+
else:
|
66 |
+
return "Not Spam"
|
67 |
+
if st.button('Check'):
|
68 |
+
pred=rnn_pred([input])
|
69 |
+
st.write(pred)
|
70 |
+
|
71 |
+
if clss_model=='Perceptron':
|
72 |
+
with open("perceptron_model_saved.pkl",'rb') as model_file:
|
73 |
+
percep_model=pickle.load(model_file)
|
74 |
+
with open('perceptron_tokeniser_saved.pkl','rb') as model_file:
|
75 |
+
percep_token=pickle.load(model_file)
|
76 |
+
st.subheader('Spam Classification')
|
77 |
+
input= st.text_input("Enter your text here")
|
78 |
+
|
79 |
+
def percep_pred(input):
|
80 |
+
encoded_test_p = percep_token.texts_to_sequences([input])
|
81 |
+
padded_test_p = tf.keras.preprocessing.sequence.pad_sequences(encoded_test_p, maxlen=10)
|
82 |
+
predict_p= percep_model.predict(padded_test_p)
|
83 |
+
if predict_p:
|
84 |
+
return "Spam"
|
85 |
+
else:
|
86 |
+
return "Not Spam"
|
87 |
+
if st.button("Check"):
|
88 |
+
percep_pred([input])
|
89 |
+
|
90 |
+
|
91 |
+
if clss_model=="Backpropagation":
|
92 |
+
with open('backprop_model.pkl','rb') as model_file:
|
93 |
+
bp_model=pickle.load(model_file)
|
94 |
+
with open('backrpop_tokeniser.pkl','rb') as model_file:
|
95 |
+
bp_tokeniser=pickle.load(model_file)
|
96 |
+
st.subheader('Spam Classification')
|
97 |
+
input= st.text_input("Enter your text here")
|
98 |
+
|
99 |
+
|
100 |
+
def back_pred(input):
|
101 |
+
encoded_test = bp_tokeniser.texts_to_sequences([input])
|
102 |
+
padded_test = tf.keras.preprocessing.sequence.pad_sequences(encoded_test, maxlen=10)
|
103 |
+
predict= bp_model.predict(padded_test)
|
104 |
+
if predict:
|
105 |
+
return "Spam"
|
106 |
+
else:
|
107 |
+
return "Not Spam"
|
108 |
+
if st.button("Check"):
|
109 |
+
back_pred([input])
|
110 |
+
|
111 |
+
if clss_model=="DNN":
|
112 |
+
with open("dnn_model.pkl",'rb') as file:
|
113 |
+
dnn_model=pickle.load(file)
|
114 |
+
|
115 |
+
with open("dnn_tokeniser.pkl",'rb') as file:
|
116 |
+
dnn_tokeniser=pickle.load(file)
|
117 |
+
st.subheader('Spam Classification')
|
118 |
+
input= st.text_input("Enter your text here")
|
119 |
+
|
120 |
+
def dnn_pred(input):
|
121 |
+
encoded_test = dnn_tokeniser.texts_to_sequences([input])
|
122 |
+
padded_test = tf.keras.preprocessing.sequence.pad_sequences(encoded_test, maxlen=500)
|
123 |
+
predict= dnn_model.predict(padded_test)
|
124 |
+
if predict:
|
125 |
+
return "Spam"
|
126 |
+
else:
|
127 |
+
return "Not Spam"
|
128 |
+
if st.button('Check'):
|
129 |
+
pred=dnn_pred([input])
|
130 |
+
st.write(pred)
|
131 |
+
|
132 |
+
|
133 |
+
if clss_model=="LSTM":
|
134 |
+
with open("lstm_model.pkl",'rb') as file:
|
135 |
+
lstm_model=pickle.load(file)
|
136 |
+
|
137 |
+
with open("lstm_tokeniser.pkl",'rb') as file:
|
138 |
+
lstm_tokeniser=pickle.load(file)
|
139 |
+
st.subheader('Movie Review Classification')
|
140 |
+
inp=st.text_area("Enter your review")
|
141 |
+
def lstm_make_predictions(inp, model):
|
142 |
+
inp = lstm_tokeniser.texts_to_sequences(inp)
|
143 |
+
inp = sequence.pad_sequences(inp, maxlen=500)
|
144 |
+
res = (model.predict(inp) > 0.5).astype("int32")
|
145 |
+
if res:
|
146 |
+
return "Negative"
|
147 |
+
else:
|
148 |
+
return "Positive"
|
149 |
+
|
150 |
+
|
151 |
+
if st.button('Check'):
|
152 |
+
pred = lstm_make_predictions([inp], lstm_model)
|
153 |
+
st.write(pred)
|
154 |
+
|
155 |
+
|
156 |
+
|
157 |
+
|
158 |
+
|
159 |
+
|
backprop_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:76ec9dae06b98632e027387f645de353a2ec8d733a79e4587beeb7f270d2e747
|
3 |
+
size 390
|
backrpop_tokeniser.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6dc8a96a4dd3c3b02ae19fd740d88469784d5c165bad72d1cd1a211efc42e94a
|
3 |
+
size 287385
|
dnn_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5c2cb7680fa297625820b5698068ed9acafdaa65e504ac808da4fc9a977bc475
|
3 |
+
size 445643
|
dnn_tokeniser.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bcef82280c645819987b58ab1ddfba4f5f3b048b93fc177a3ce9ec55c2bba1e6
|
3 |
+
size 4534143
|
lstm_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:34d2af94bb18560996a29b10530528d3a4c8755380f897882caf37282cd04ae1
|
3 |
+
size 41216539
|
lstm_tokeniser.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1149196b3b59169f3bbe5fa55a184df7d239d737a856a792e2597594d7fae6cb
|
3 |
+
size 4534143
|
perceptron_model_saved.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5b9fac521decb73695cec1adb3402541ce6821215ceb8e5691a648be4ea731df
|
3 |
+
size 300
|
perceptron_tokeniser_saved.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:851757a901176072232088a73a7bdd9c926215c6dad1b42d2dbf88ecacf7dbe4
|
3 |
+
size 287385
|
requirements.txt
ADDED
File without changes
|
rnn_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3933a9e187faa83e4df1b1cd9b492ebbc09066fa9974a5c2632426768a984ea5
|
3 |
+
size 2255782
|
rnn_tokeniser.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:be568b0e4886340e8fbad531b229ad9612927977a16a324ab36f611f6a06a53f
|
3 |
+
size 290462
|
tumor_detection_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3909f292d40346b161116207bbae1de4b2ff0e7ae0ade400baac0676c0ba7d80
|
3 |
+
size 391803384
|