Nikhithapotnuru commited on
Commit
8d5b066
·
verified ·
1 Parent(s): 0ec8502

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -62
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
- model = keras.models.load_model("cnn_lenet.keras")
12
-
13
- # Sidebar upload
14
- uploaded_file = st.sidebar.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
15
-
16
- # Load MNIST for fallback
17
- (x_train, y_train), (x_test, y_test) = mnist.load_data()
18
-
19
- # Preprocess uploaded image or fallback
20
- if uploaded_file is not None:
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], outputs=model.layers[0].output)
50
- fmap = sub.predict(img_array)
51
- fig, ax = plt.subplots(6, 1, figsize=(8, 6))
52
  for i in range(6):
53
- ax[i].imshow(fmap[0, :, :, i], cmap="gray")
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], outputs=model.layers[1].output)
60
- fmap = max1.predict(img_array)
61
- fig, ax1 = plt.subplots(6, 1, figsize=(8, 6))
62
  for i in range(6):
63
- ax1[i].imshow(fmap[0, :, :, i], cmap="gray")
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], outputs=model.layers[2].output)
70
- fmap = sub1.predict(img_array)
71
- fig, ax2 = plt.subplots(16, 1, figsize=(8, 6))
72
  for i in range(16):
73
- ax2[i].imshow(fmap[0, :, :, i], cmap="gray")
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], outputs=model.layers[3].output)
80
- fmap = max2.predict(img_array)
81
- fig, ax3 = plt.subplots(16, 1, figsize=(8, 6))
82
  for i in range(16):
83
- ax3[i].imshow(fmap[0, :, :, i], cmap="gray")
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], outputs=model.layers[4].output)
90
- fmap = sub3.predict(img_array)
91
- fig, ax4 = plt.subplots(30, 4, figsize=(12, 20)) # grid for 120 filters
92
- idx = 0
93
- for r in range(30):
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
+