Vismayavs commited on
Commit
7a53047
1 Parent(s): 0898bb1

Upload 14 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* 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
 
 
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
36
+ IMDB[[:space:]]Dataset.csv 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
+
IMDB Dataset.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dfc447764f82be365fa9c2beef4e8df89d3919e3da95f5088004797d79695aa2
3
+ size 66212309
Perceptron.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from tqdm import tqdm
3
+ import joblib
4
+
5
+
6
+ class Perceptron:
7
+
8
+ def __init__(self,learning_rate=0.01, epochs=100,activation_function='step'):
9
+ self.bias = 0
10
+ self.learning_rate = learning_rate
11
+ self.max_epochs = epochs
12
+ self.activation_function = activation_function
13
+
14
+
15
+ def activate(self, x):
16
+ if self.activation_function == 'step':
17
+ return 1 if x >= 0 else 0
18
+ elif self.activation_function == 'sigmoid':
19
+ return 1 if (1 / (1 + np.exp(-x)))>=0.5 else 0
20
+ elif self.activation_function == 'relu':
21
+ return 1 if max(0,x)>=0.5 else 0
22
+
23
+ def fit(self, X, y):
24
+ n_features = X.shape[1]
25
+ self.weights = np.random.randint(n_features, size=(n_features))
26
+ for epoch in tqdm(range(self.max_epochs)):
27
+ for i in range(len(X)):
28
+ inputs = X[i]
29
+ target = y[i]
30
+ weighted_sum = np.dot(inputs, self.weights) + self.bias
31
+ prediction = self.activate(weighted_sum)
32
+ print("Training Completed")
33
+
34
+ def predict(self, X):
35
+ predictions = []
36
+ for i in range(len(X)):
37
+ inputs = X[i]
38
+ weighted_sum = np.dot(inputs, self.weights) + self.bias
39
+ prediction = self.activate(weighted_sum)
40
+ predictions.append(prediction)
41
+ return predictions
42
+
43
+ # Save the Perceptron model
44
+ joblib.dump(Perceptron, 'perceptron_model.joblib')
45
+
46
+
47
+
48
+
49
+
50
+
51
+
backprop_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:08f18405b62db7924aebb1b734d1b1895d4b2a3b1f42b9b34651329488a80e1d
3
+ size 1896
class.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ from PIL import Image
4
+ from tensorflow.keras.models import load_model
5
+ import joblib
6
+ from tensorflow.keras.preprocessing.text import Tokenizer
7
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
8
+ from tensorflow.keras.applications.inception_v3 import preprocess_input
9
+ import cv2
10
+ from Perceptron import Perceptron
11
+ from sklearn.linear_model import Perceptron
12
+ import tensorflow as tf
13
+ import joblib
14
+ import pickle
15
+ from numpy import argmax
16
+
17
+
18
+ # Load the tokenizer using pickle
19
+ with open(r'tokeniser.pkl', 'rb') as handle:
20
+ loaded_tokenizer = pickle.load(handle)
21
+
22
+ # Load saved models
23
+ image_model = load_model('tumor_detection_model.h5')
24
+ dnn_model = load_model('imdb_model.h5')
25
+ loaded_model = tf.keras.models.load_model('sms_spam_detection_dnnmodel.h5')
26
+ perceptron_model = joblib.load('perceptron_model.joblib')
27
+ backprop_model = joblib.load('backprop_model.pkl')
28
+
29
+
30
+ # Streamlit app
31
+ st.title("Classification")
32
+
33
+ # Sidebar
34
+ task = st.sidebar.selectbox("Select Task", ["Tumor Detection", "Sentiment Classification"])
35
+
36
+ def preprocess_text(text):
37
+ tokenizer = Tokenizer()
38
+ tokenizer.fit_on_texts([text])
39
+ sequences = tokenizer.texts_to_sequences([text])
40
+ preprocessed_text = pad_sequences(sequences, maxlen=4)
41
+
42
+ return preprocessed_text
43
+
44
+
45
+
46
+ def predict_dnn(preprocessed_text):
47
+ preprocessed_text = preprocessed_text.reshape((1, 4)) # Adjust the shape according to your model's input shape
48
+
49
+ prediction = dnn_model.predict(preprocessed_text)
50
+ st.write("DNN Prediction:", prediction)
51
+
52
+
53
+
54
+ def predict_rnn(input_text):
55
+ # Process input text similarly to training data
56
+ encoded_input = loaded_tokenizer.texts_to_sequences([input_text])
57
+ padded_input = tf.keras.preprocessing.sequence.pad_sequences(encoded_input, maxlen=10, padding='post')
58
+ prediction = loaded_model.predict(padded_input)
59
+ if prediction > 0.5:
60
+ return "spam"
61
+ else:
62
+ return "ham"
63
+
64
+
65
+ def predict_custom_perceptron(preprocessed_text):
66
+ perceptron = CustomPerceptron(epochs=10) # Using the custom Perceptron
67
+ prediction = perceptron.predict(preprocessed_text)
68
+ st.write("Custom Perceptron Prediction:", prediction)
69
+
70
+ def predict_sklearn_perceptron(preprocessed_text):
71
+ perceptron = SklearnPerceptron() # Using the sklearn Perceptron
72
+ prediction = perceptron.predict(preprocessed_text)
73
+ st.write("Sklearn Perceptron Prediction:", prediction)
74
+
75
+ def predict_backpropagation(preprocessed_text):
76
+ prediction = backprop_model.predict(preprocessed_text)
77
+ st.write("Backpropagation Prediction:", prediction)
78
+
79
+ # make a prediction for CNN
80
+ def preprocess_image(image):
81
+ image = image.resize((299, 299))
82
+ image_array = np.array(image)
83
+ preprocessed_image = preprocess_input(image_array)
84
+
85
+ return preprocessed_image
86
+
87
+
88
+ def make_prediction_cnn(image, image_model):
89
+ img = image.resize((128, 128))
90
+ img_array = np.array(img)
91
+ img_array = img_array.reshape((1, img_array.shape[0], img_array.shape[1], img_array.shape[2]))
92
+
93
+ preprocessed_image = preprocess_input(img_array)
94
+ prediction = image_model.predict(preprocessed_image)
95
+
96
+ if prediction > 0.5:
97
+ st.write("Tumor Detected")
98
+ else:
99
+ st.write("No Tumor")
100
+
101
+ if task == "Sentiment Classification":
102
+ st.subheader("Choose Model")
103
+ model_choice = st.radio("Select Model", ["DNN", "RNN", "Perceptron", "Backpropagation"])
104
+
105
+ st.subheader("Text Input")
106
+ text_input = st.text_area("Enter Text")
107
+
108
+ if st.button("Predict"):
109
+ # Preprocess the text
110
+ preprocessed_text = preprocess_text(text_input)
111
+ if model_choice == "DNN":
112
+ predict_dnn(preprocessed_text)
113
+ elif model_choice == "RNN":
114
+ if text_input:
115
+ prediction_result = predict_rnn(text_input)
116
+ st.write(f"The message is classified as: {prediction_result}")
117
+ else:
118
+ st.write("Please enter some text for prediction")
119
+ elif model_choice == "Custom Perceptron":
120
+ predict_custom_perceptron(preprocessed_text)
121
+ elif model_choice == "Sklearn Perceptron":
122
+ predict_sklearn_perceptron(preprocessed_text)
123
+ elif model_choice == "Backpropagation":
124
+ predict_backpropagation(preprocessed_text)
125
+
126
+ else:
127
+ st.subheader("Choose Model")
128
+ model_choice = st.radio("Select Model", ["CNN"])
129
+
130
+ st.subheader("Image Input")
131
+ image_input = st.file_uploader("Choose an image...", type="jpg")
132
+
133
+ if image_input is not None:
134
+ image = Image.open(image_input)
135
+ st.image(image, caption="Uploaded Image.", use_column_width=True)
136
+
137
+ # Preprocess the image
138
+ preprocessed_image = preprocess_image(image)
139
+
140
+ if st.button("Predict"):
141
+ if model_choice == "CNN":
142
+ make_prediction_cnn(image, image_model)
dnn_main.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from tensorflow.keras import layers, models
3
+ from tensorflow.keras.datasets import imdb
4
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
5
+
6
+ # Load the IMDb dataset
7
+ (train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
8
+
9
+ # Pad sequences to a fixed length
10
+ max_length = 500
11
+ train_data = pad_sequences(train_data, maxlen=max_length)
12
+ test_data = pad_sequences(test_data, maxlen=max_length)
13
+
14
+ # Define the model
15
+ model = models.Sequential()
16
+ model.add(layers.Embedding(input_dim=10000, output_dim=16, input_length=max_length))
17
+ model.add(layers.Flatten())
18
+ model.add(layers.Dense(32, activation='relu'))
19
+ model.add(layers.Dense(1, activation='sigmoid'))
20
+
21
+ # Compile the model
22
+ model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
23
+
24
+ # Display the model summary
25
+ model.summary()
26
+
27
+ # Train the model
28
+ history = model.fit(train_data, train_labels, epochs=5, batch_size=32, validation_split=0.2)
29
+
30
+ # Evaluate the model on the test set
31
+ test_loss, test_accuracy = model.evaluate(test_data, test_labels)
32
+ print(f'Test Accuracy: {test_accuracy * 100:.2f}%')
imdb_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:074b0397ced57e43c8d7a3a75d123088f1fb34e5b864f555c929523570112108
3
+ size 5024248
imdb_perceptron_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:94098089d5f8b390533c214ddf2804469db9772089ac429c336a02f2d44927c6
3
+ size 1063
lstm_imdb_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b03fc488fed00a614e9c9d85b4bfc4c3de4bf51f950ab3fdbc959cc8736f456c
3
+ size 2594296
perceptron_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2107f3f53a82a656bbdd42e2c6030ef645299421845ef9408b2db5afca1869f5
3
+ size 29
sms_spam_detection_dnnmodel.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:98a69a1ccd2e7048bb72447cff024b354fa7cdec602de3d0b31f6129963951f9
3
+ size 3160600
smsspam.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import matplotlib.pyplot as plt
3
+ import seaborn as sns
4
+ from sklearn.model_selection import train_test_split
5
+ import tensorflow as tf
6
+ from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
7
+
8
+ print("---------------------- Downloading Dataset -------------------------\n")
9
+
10
+ dataset = pd.read_csv('https://raw.githubusercontent.com/adityaiiitmk/Datasets/master/SMSSpamCollection',sep='\t',names=['label','message'])
11
+
12
+ print("---------------------- -------------------------\n")
13
+ print(dataset.head())
14
+ print("---------------------- -------------------------")
15
+ print(dataset.groupby('label').describe())
16
+ print("---------------------- -------------------------")
17
+ dataset['label'] = dataset['label'].map( {'spam': 1, 'ham': 0} )
18
+ X = dataset['message'].values
19
+ y = dataset['label'].values
20
+
21
+ print("---------------------- Train Test Split -------------------------\n")
22
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
23
+ tokeniser = tf.keras.preprocessing.text.Tokenizer()
24
+ tokeniser.fit_on_texts(X_train) #learn vocabulary assigning unique int to each word
25
+ encoded_train = tokeniser.texts_to_sequences(X_train)
26
+ encoded_test = tokeniser.texts_to_sequences(X_test)
27
+ print(encoded_train[0:2])
28
+ print("---------------------- Padding -------------------------\n")
29
+ max_length = 10 #legth of sequence
30
+ padded_train = tf.keras.preprocessing.sequence.pad_sequences(encoded_train, maxlen=max_length, padding='post')
31
+ padded_test = tf.keras.preprocessing.sequence.pad_sequences(encoded_test, maxlen=max_length, padding='post')
32
+ print(padded_train[0:2])
33
+
34
+ print("---------------------- -------------------------\n")
35
+
36
+ vocab_size = len(tokeniser.word_index)+1
37
+
38
+ # define the model
39
+
40
+ print("---------------------- Modelling -------------------------\n")
41
+
42
+
43
+ model=tf.keras.models.Sequential([
44
+ tf.keras.layers.Embedding(input_dim=vocab_size,output_dim= 24, input_length=max_length),
45
+ tf.keras.layers.SimpleRNN(24, return_sequences=False), #return last time output
46
+ tf.keras.layers.Dense(64, activation='relu'),
47
+ tf.keras.layers.Dense(32, activation='relu'),
48
+ tf.keras.layers.Dense(1, activation='sigmoid')
49
+ ])
50
+
51
+ # compile the model
52
+ model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
53
+
54
+ print("---------------------- -------------------------\n")
55
+
56
+ # summarize the model
57
+ print(model.summary())
58
+
59
+ print("---------------------- -------------------------\n")
60
+
61
+ early_stop = tf.keras.callbacks.EarlyStopping(monitor='accuracy', mode='min', patience=10) #callback set of functions can applied different stages of training
62
+
63
+ print("---------------------- Training -------------------------\n")
64
+
65
+ # fit the model
66
+ model.fit(x=padded_train,
67
+ y=y_train,
68
+ epochs=50,
69
+ validation_data=(padded_test, y_test),
70
+ callbacks=[early_stop]
71
+ )
72
+ print("---------------------- -------------------------\n")
73
+
74
+
75
+ def c_report(y_true, y_pred):
76
+ print("Classification Report")
77
+ print(classification_report(y_true, y_pred))
78
+ acc_sc = accuracy_score(y_true, y_pred)
79
+ print(f"Accuracy : {str(round(acc_sc,2)*100)}")
80
+ return acc_sc
81
+
82
+ def plot_confusion_matrix(y_true, y_pred):
83
+ mtx = confusion_matrix(y_true, y_pred)
84
+ sns.heatmap(mtx, annot=True, fmt='d', linewidths=.5, cmap="Blues", cbar=False)
85
+ plt.ylabel('True label')
86
+ plt.xlabel('Predicted label')
87
+ plt.savefig(r"C:\Users\Dell\Documents\Sem !!!\Deep Learning\App\Results\test.jpg")
88
+
89
+
90
+ preds = (model.predict(padded_test) > 0.5).astype("int32")
91
+ c_report(y_test, preds)
92
+
93
+ plot_confusion_matrix(y_test, preds)
94
+ model.save("RNN/results/model/spam_model")
tokeniser.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:64c70b8c735b6a8d9d6afda7908154c7b8398e94bcf6cdcb1f70977f93686405
3
+ size 290462
tumor_detection_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:95e3cd70401c053d4f32ca737a74f097d3877e4d4244480c230a6b43c7e4eba0
3
+ size 391811360