anandhu-pk commited on
Commit
a403ce3
·
1 Parent(s): 20c0d27

Upload 15 files

Browse files
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
+
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 streamlit as st
2
+ import pickle
3
+ import tensorflow as tf
4
+ from tensorflow.keras.preprocessing import sequence
5
+ from tensorflow.keras.saving import load_model
6
+ import numpy as np
7
+ import cv2
8
+ from PIL import Image
9
+
10
+
11
+ st.title('Classifier System')
12
+ task = st.selectbox('Select Task', ['Choose one','Sentiment Classification', 'Tumor Detection'])
13
+
14
+ if task=='Tumor Detection':
15
+ st.subheader('Tumor Detection with CNN')
16
+ # CNN
17
+ #with open(r"E:\DUK\DUKSEM3\DEEP_LEARNING\ASSIGN1\Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis\CNN\cnn_model.pkl",'rb') as file:
18
+ #cnn_model = pickle.load(file)
19
+ cnn_model = load_model(r"E:\DUK\DUKSEM3\DEEP_LEARNING\ASSIGN1\Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis\CNN\cnn_model1.h5")
20
+
21
+ img = st.file_uploader('Upload image', type=['jpeg', 'jpg', 'png'])
22
+
23
+ def cnn_make_prediction(img,model):
24
+ img=Image.open(img)
25
+ img=img.resize((128,128))
26
+ img=np.array(img)
27
+ input_img = np.expand_dims(img, axis=0)
28
+ res = model.predict(input_img)
29
+ if res:
30
+ return "Tumor Detected"
31
+ else:
32
+ return "No Tumor Detected"
33
+
34
+
35
+ if img is not None:
36
+ st.image(img, caption = "Image preview")
37
+ if st.button('Submit'):
38
+ pred = cnn_make_prediction(img, cnn_model)
39
+ st.write(pred)
40
+
41
+
42
+ if task=='Sentiment Classification':
43
+ arcs = ['Perceptron', 'Backpropagation', 'DNN', 'RNN', 'LSTM']
44
+ arc = st.radio('Pick one:', arcs, horizontal=True)
45
+
46
+ if arc == arcs[0]:
47
+ # Perceptron
48
+ with open(r"E:\DUK\DUKSEM3\DEEP_LEARNING\ASSIGN1\Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis\Perceptron\pnn_model.pkl",'rb') as file:
49
+ perceptron = pickle.load(file)
50
+ with open(r"E:\DUK\DUKSEM3\DEEP_LEARNING\ASSIGN1\Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis\Perceptron\pnn_tokeniser.pkl",'rb') as file:
51
+ ppn_tokeniser = pickle.load(file)
52
+
53
+ def ppn_make_predictions(inp, model):
54
+ encoded_inp = ppn_tokeniser.texts_to_sequences([inp])
55
+ padded_inp = sequence.pad_sequences(encoded_inp, maxlen=500)
56
+ res = model.predict(padded_inp)
57
+ if res:
58
+ return "Not spam"
59
+ else:
60
+ return "Spam"
61
+
62
+ st.subheader('SMS spam Classification using Perceptron')
63
+ inp = st.text_area('Enter message')
64
+ if st.button('Check'):
65
+ pred = ppn_make_predictions([inp], perceptron)
66
+ st.write(pred)
67
+
68
+ elif arc == arcs[1]:
69
+ # BackPropogation
70
+ with open(r"E:\DUK\DUKSEM3\DEEP_LEARNING\ASSIGN1\Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis\BackPropagation\bpn_model.pkl",'rb') as file:
71
+ backprop = pickle.load(file)
72
+ with open(r"E:\DUK\DUKSEM3\DEEP_LEARNING\ASSIGN1\Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis\BackPropagation\bpn_tokeniser.pkl",'rb') as file:
73
+ bp_tokeniser = pickle.load(file)
74
+
75
+ def bp_make_predictions(inp, model):
76
+ encoded_inp = bp_tokeniser.texts_to_sequences([inp])
77
+ padded_inp = sequence.pad_sequences(encoded_inp, maxlen=500)
78
+ res = model.predict(padded_inp)
79
+ if res:
80
+ return "Not spam"
81
+ else:
82
+ return "Spam"
83
+
84
+ st.subheader('SMS spam Classification using Backpropagation')
85
+ inp = st.text_area('Enter message')
86
+ if st.button('Check'):
87
+ pred = bp_make_predictions([inp], backprop)
88
+ st.write(pred)
89
+
90
+
91
+ elif arc == arcs[2]:
92
+ # DNN
93
+ with open(r"E:\DUK\DUKSEM3\DEEP_LEARNING\ASSIGN1\Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis\DNN\dnn_model.pkl",'rb') as file:
94
+ dnn_model = pickle.load(file)
95
+ with open(r"E:\DUK\DUKSEM3\DEEP_LEARNING\ASSIGN1\Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis\DNN\dnn_tokeniser.pkl",'rb') as file:
96
+ dnn_tokeniser = pickle.load(file)
97
+
98
+ def dnn_make_predictions(inp, model):
99
+ inp = dnn_tokeniser.texts_to_sequences(inp)
100
+ inp = sequence.pad_sequences(inp, maxlen=500)
101
+ res = (model.predict(inp) > 0.5).astype("int32")
102
+ if res:
103
+ return "Not spam"
104
+ else:
105
+ return "Spam"
106
+
107
+ st.subheader('SMS spam Classification using DNN')
108
+ inp = st.text_area('Enter message')
109
+ if st.button('Check'):
110
+ pred = dnn_make_predictions([inp], dnn_model)
111
+ st.write(pred)
112
+
113
+
114
+ elif arc == arcs[3]:
115
+ # RNN
116
+ with open(r"E:\DUK\DUKSEM3\DEEP_LEARNING\ASSIGN1\Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis\RNN\rnn_model.pkl",'rb') as file:
117
+ rnn_model = pickle.load(file)
118
+
119
+ with open(r"E:\DUK\DUKSEM3\DEEP_LEARNING\ASSIGN1\Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis\RNN\rnn_tokeniser.pkl",'rb') as file:
120
+ rnn_tokeniser = pickle.load(file)
121
+
122
+ def rnn_make_predictions(inp, model):
123
+ encoded_inp = rnn_tokeniser.texts_to_sequences(inp)
124
+ padded_inp = sequence.pad_sequences(encoded_inp, maxlen=10, padding='post')
125
+ res = (model.predict(padded_inp) > 0.5).astype("int32")
126
+ if res:
127
+ return "Spam"
128
+ else:
129
+ return "Not spam"
130
+
131
+ st.subheader('SMS Spam Classification using RNN')
132
+ inp = st.text_area('Enter message')
133
+ if st.button('Check'):
134
+ pred = rnn_make_predictions([inp], rnn_model)
135
+ st.write(pred)
136
+
137
+
138
+ elif arc == arcs[4]:
139
+ # LSTM
140
+ with open(r"E:\DUK\DUKSEM3\DEEP_LEARNING\ASSIGN1\Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis\LSTM\lstm_model.pkl",'rb') as file:
141
+ lstm_model = pickle.load(file)
142
+
143
+ with open(r"E:\DUK\DUKSEM3\DEEP_LEARNING\ASSIGN1\Multi-Modal_classifier_Image_Classification_Sentiment_Sentiment_Analysis\LSTM\lstm_tokeniser.pkl",'rb') as file:
144
+ lstm_tokeniser = pickle.load(file)
145
+
146
+ def lstm_make_predictions(inp, model):
147
+ inp = lstm_tokeniser.texts_to_sequences(inp)
148
+ inp = sequence.pad_sequences(inp, maxlen=500)
149
+ res = (model.predict(inp) > 0.5).astype("int32")
150
+ if res:
151
+ return "Not spam"
152
+ else:
153
+ return "Spam"
154
+
155
+ st.subheader('SMS spam Classification using LSTM')
156
+ inp = st.text_area('Enter message')
157
+ if st.button('Check'):
158
+ pred = lstm_make_predictions([inp], lstm_model)
159
+ st.write(pred)
bpn_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:28e032769288ba5475fb55d8f3095a307ef6f78e1cda278aacc141d2aa2f7752
3
+ size 4300
bpn_tokeniser.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:657d8fe699afcd9ade12d4e347319b382aa4565772343845c600785ed97437ed
3
+ size 240973
cnn_model1.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f52777b9061b0891f0b4268c301b2e90b55f9e8bbf3daa1a41e1f5b19982ff45
3
+ size 391811360
dnn_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:883c57fdb6f20dad202012747b7dd96299bfc09726c78bc740c4cbeefd9138da
3
+ size 445644
dnn_tokeniser.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5fa862ba455ed4f56e64f584ce02928f89540ba001adefc32f3a17cd257dd8e4
3
+ size 287385
lstm_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:68d73f50f191f96ee20618e6d076baff7988c7679ce70d090aa2516ffceab2c4
3
+ size 3513113
lstm_tokeniser.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7f89d4a5a81f9f1ec173ad953ec5199271650041234e4c108fb3bc211362ebc1
3
+ size 287385
pnn_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4bc24f1b71170b633a1c5571b4703287616193588031ae0bff2cab94697a189a
3
+ size 2267
pnn_tokeniser.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:168b7bb3b2abbf09e8d525f91b3779579a0be69a964721fb31a073c869f2b257
3
+ size 287385
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ tensorflow
2
+ numpy
3
+ pillow
4
+ gdown
5
+ torch
rnn_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bc7abc340c0570b7996691fb98d6dfe9e9ee169925416fa31449a4a3c3f13b51
3
+ size 2230438
rnn_tokeniser.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a9c66a925743edae19d84670280a8c2d4e5d8fab6e41cdf2912b17817f149cbc
3
+ size 287385