Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,21 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from keras.datasets import mnist
|
| 3 |
from sklearn.preprocessing import MinMaxScaler
|
| 4 |
-
from keras.models import Model
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
-
import numpy as np
|
| 7 |
-
from tensorflow import keras
|
| 8 |
-
from PIL import Image
|
| 9 |
-
|
| 10 |
m = MinMaxScaler()
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
(
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
image = Image.open(uploaded_file).convert("L") # grayscale
|
| 22 |
-
image = image.resize((28, 28)) # resize to model input
|
| 23 |
-
img_array = np.array(image) / 255.0
|
| 24 |
-
img_array = img_array.reshape(1, 28, 28, 1) # reshape for CNN
|
| 25 |
-
st.success("✅ Successfully uploaded image")
|
| 26 |
-
st.image(image, caption="Uploaded Image (28x28)", use_column_width=True)
|
| 27 |
-
else:
|
| 28 |
-
img_array = x_train[0:1].reshape(1, 28, 28, 1)
|
| 29 |
-
st.info("ℹ️ No image uploaded, using MNIST sample instead")
|
| 30 |
-
st.image(x_train[0], caption="MNIST Sample", use_column_width=True)
|
| 31 |
-
|
| 32 |
-
# Button to visualize
|
| 33 |
-
if st.button("Visualize Layers"):
|
| 34 |
-
col1, col2, col3, col4, col5, col6 = st.columns(6)
|
| 35 |
-
|
| 36 |
with col1:
|
| 37 |
st.write("Filters")
|
| 38 |
-
fig, axs = plt.subplots(6, 1, figsize=(8, 6))
|
| 39 |
for i in range(6):
|
| 40 |
axs[i].imshow(
|
| 41 |
m.fit_transform(model.layers[0].weights[0][:, :, :, i][:, :, 0]),
|
|
@@ -43,57 +23,46 @@ if st.button("Visualize Layers"):
|
|
| 43 |
)
|
| 44 |
axs[i].axis("off")
|
| 45 |
st.pyplot(fig)
|
| 46 |
-
|
| 47 |
with col2:
|
| 48 |
st.write("Conv2D Layer-1")
|
| 49 |
-
sub = Model(inputs=model.inputs[0],
|
| 50 |
-
|
| 51 |
-
fig, ax = plt.subplots(6, 1, figsize=(8, 6))
|
| 52 |
for i in range(6):
|
| 53 |
-
ax[i].imshow(
|
| 54 |
ax[i].axis("off")
|
| 55 |
st.pyplot(fig)
|
| 56 |
-
|
| 57 |
with col3:
|
| 58 |
st.write("Average Pooling Layer-1")
|
| 59 |
-
max1 = Model(inputs=model.inputs[0],
|
| 60 |
-
|
| 61 |
-
fig, ax1 = plt.subplots(6, 1, figsize=(8, 6))
|
| 62 |
for i in range(6):
|
| 63 |
-
ax1[i].imshow(
|
| 64 |
ax1[i].axis("off")
|
| 65 |
st.pyplot(fig)
|
| 66 |
-
|
| 67 |
with col4:
|
| 68 |
st.write("Conv2D Layer-2")
|
| 69 |
-
sub1 = Model(inputs=model.inputs[0],
|
| 70 |
-
|
| 71 |
-
fig, ax2 = plt.subplots(16, 1, figsize=(8, 6))
|
| 72 |
for i in range(16):
|
| 73 |
-
ax2[i].imshow(
|
| 74 |
ax2[i].axis("off")
|
| 75 |
st.pyplot(fig)
|
| 76 |
-
|
| 77 |
with col5:
|
| 78 |
st.write("Average Pooling Layer-2")
|
| 79 |
-
max2 = Model(inputs=model.inputs[0],
|
| 80 |
-
|
| 81 |
-
fig, ax3 = plt.subplots(16, 1, figsize=(8, 6))
|
| 82 |
for i in range(16):
|
| 83 |
-
ax3[i].imshow(
|
| 84 |
ax3[i].axis("off")
|
| 85 |
st.pyplot(fig)
|
| 86 |
-
|
| 87 |
with col6:
|
| 88 |
st.write("Conv2D Layer-3")
|
| 89 |
-
sub3 = Model(inputs=model.inputs[0],
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
for c in range(4):
|
| 95 |
-
if idx < 120:
|
| 96 |
-
ax4[r, c].imshow(fmap[0, :, :, idx], cmap="gray")
|
| 97 |
-
ax4[r, c].axis("off")
|
| 98 |
-
idx += 1
|
| 99 |
st.pyplot(fig)
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from keras.datasets import mnist
|
| 3 |
from sklearn.preprocessing import MinMaxScaler
|
| 4 |
+
from keras.models import Sequential,Model
|
| 5 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
m = MinMaxScaler()
|
| 7 |
+
from tensorflow import keras
|
| 8 |
+
model = keras.models.load_model('cnn_lenet.keras')
|
| 9 |
+
from keras.layers import Conv2D,MaxPooling2D,AveragePooling2D,InputLayer,Dense,Flatten
|
| 10 |
+
option = st.sidebar.selectbox("Datasets",["Select dataset","Hand Writen Digit Dataset"])
|
| 11 |
+
if option == "Hand Writen Digit Dataset":
|
| 12 |
+
(x_train,y_train),(x_test,y_test) = mnist.load_data()
|
| 13 |
+
st.write("Successfully Load the Dataset")
|
| 14 |
+
if st.button("Train"):
|
| 15 |
+
fig, axs = plt.subplots(6, 1, figsize=(8, 6))
|
| 16 |
+
col1,col2,col3,col4,col5,col6 = st.columns(6)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
with col1:
|
| 18 |
st.write("Filters")
|
|
|
|
| 19 |
for i in range(6):
|
| 20 |
axs[i].imshow(
|
| 21 |
m.fit_transform(model.layers[0].weights[0][:, :, :, i][:, :, 0]),
|
|
|
|
| 23 |
)
|
| 24 |
axs[i].axis("off")
|
| 25 |
st.pyplot(fig)
|
|
|
|
| 26 |
with col2:
|
| 27 |
st.write("Conv2D Layer-1")
|
| 28 |
+
sub = Model(inputs=model.inputs[0],outputs=model.layers[0].output)
|
| 29 |
+
fig,ax = plt.subplots(6,1,figsize=(8,6))
|
|
|
|
| 30 |
for i in range(6):
|
| 31 |
+
ax[i].imshow(sub.predict(x_train[0:1,:].reshape(1,28,28,1))[0,:,:,i],cmap="gray")
|
| 32 |
ax[i].axis("off")
|
| 33 |
st.pyplot(fig)
|
|
|
|
| 34 |
with col3:
|
| 35 |
st.write("Average Pooling Layer-1")
|
| 36 |
+
max1 = Model(inputs=model.inputs[0],outputs=model.layers[1].output)
|
| 37 |
+
fig,ax1 = plt.subplots(6,1,figsize=(8,6))
|
|
|
|
| 38 |
for i in range(6):
|
| 39 |
+
ax1[i].imshow(max1.predict(x_train[0:1,:].reshape(1,28,28,1))[0,:,:,i],cmap="gray")
|
| 40 |
ax1[i].axis("off")
|
| 41 |
st.pyplot(fig)
|
| 42 |
+
|
| 43 |
with col4:
|
| 44 |
st.write("Conv2D Layer-2")
|
| 45 |
+
sub1 = Model(inputs=model.inputs[0],outputs=model.layers[2].output)
|
| 46 |
+
fig,ax2 = plt.subplots(16,1,figsize=(8,6))
|
|
|
|
| 47 |
for i in range(16):
|
| 48 |
+
ax2[i].imshow(sub1.predict(x_train[0:1,:].reshape(1,28,28,1))[0,:,:,i],cmap="gray")
|
| 49 |
ax2[i].axis("off")
|
| 50 |
st.pyplot(fig)
|
|
|
|
| 51 |
with col5:
|
| 52 |
st.write("Average Pooling Layer-2")
|
| 53 |
+
max2 = Model(inputs=model.inputs[0],outputs=model.layers[3].output)
|
| 54 |
+
fig,ax3 = plt.subplots(16,1,figsize=(8,6))
|
|
|
|
| 55 |
for i in range(16):
|
| 56 |
+
ax3[i].imshow(max2.predict(x_train[0:1,:].reshape(1,28,28,1))[0,:,:,i],cmap="gray")
|
| 57 |
ax3[i].axis("off")
|
| 58 |
st.pyplot(fig)
|
|
|
|
| 59 |
with col6:
|
| 60 |
st.write("Conv2D Layer-3")
|
| 61 |
+
sub3 = Model(inputs=model.inputs[0],outputs=model.layers[4].output)
|
| 62 |
+
fig,ax4 = plt.subplots(120,1,figsize=(8,6))
|
| 63 |
+
for i in range(120):
|
| 64 |
+
ax4[i].imshow(sub3.predict(x_train[0:1,:].reshape(1,28,28,1))[0,:,:,i],cmap="gray")
|
| 65 |
+
ax4[i].axis("off")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
st.pyplot(fig)
|
| 67 |
+
|
| 68 |
+
|