shubham680 commited on
Commit
7f6b662
·
verified ·
1 Parent(s): 960aec6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -142
app.py CHANGED
@@ -1,105 +1,5 @@
1
- # import streamlit as st
2
- # import numpy as np
3
- # import matplotlib.pyplot as plt
4
- # import keras
5
- # from sklearn.preprocessing import MinMaxScaler
6
- # from keras.models import Model
7
- # from keras.layers import Conv2D
8
- # import cv2
9
- # import tensorflow as tf
10
-
11
- # model = keras.models.load_model("model.keras")
12
-
13
- # # uploaded_img = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
14
-
15
- # # options = ['1st Convolution', '2nd Convolution', '3rd Convolution']
16
- # # selected_option = st.selectbox('Choose an option:', options)
17
-
18
- # st.sidebar.title("Controls")
19
- # uploaded_img = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
20
- # options = ['1st Convolution', '2nd Convolution', '3rd Convolution']
21
- # selected_option = st.sidebar.selectbox('Select convolution layer:', options)
22
-
23
-
24
- # conv_layers = [layer for layer in model.layers if isinstance(layer, Conv2D)]
25
- # layer_ind = options.index(selected_option)
26
- # selected_layer = conv_layers[layer_ind]
27
-
28
- # fig = plt.figure(figsize=(12, 4))
29
-
30
- # #layer_ind = options.index(selected_option)
31
- # # selected_layer = conv_layers[layer_ind]
32
- # scaler = MinMaxScaler()
33
-
34
- # # for i in range(3):
35
- # for j in range(6):
36
- # layer=selected_layer
37
- # weights=layer.get_weights()[0][:,:,0,j]
38
- # norm_weights = scaler.fit_transform(weights)
39
- # plt.subplot(2,3,j+1)
40
- # plt.imshow(norm_weights,cmap='gray')
41
- # plt.title(f"Filters {j+1}")
42
- # plt.axis('off')
43
- # plt.tight_layout()
44
- # st.pyplot(fig)
45
-
46
-
47
- # if uploaded_img is not None:
48
- # #st.image(uploaded_img, caption="Uploaded Image", use_column_width=True)
49
- # file_bytes = np.frombuffer(uploaded_img.read(), np.uint8)
50
- # img = cv2.imdecode(file_bytes, cv2.IMREAD_GRAYSCALE)
51
- # img_resized = cv2.resize(img,(28,28),interpolation=cv2.INTER_AREA)
52
- # #img_norm = img_resized.astype('float32') / 255.0
53
- # input_img = img_resized.reshape(1,28,28,1)
54
-
55
-
56
-
57
- # st.image(img_resized, caption="Uploaded Image (Resized to 28x28)", use_container_width =True, channels="GRAY")
58
-
59
- # #layer_ind = options.index(selected_option)
60
- # # selected_layer = conv_layers[layer_ind]
61
-
62
- # #func_model = Model(inputs = model.layers[0].input, outputs = model.selected_layer.output)
63
- # func_model = Model(inputs = model.layers[0].input, outputs = selected_layer.output)
64
-
65
-
66
- # fm = func_model.predict(input_img)
67
- # fm = fm[0]
68
-
69
- # if layer_ind == 0:
70
- # fig1 = plt.figure(figsize=(12, 4))
71
- # for i in range(6):
72
- # plt.subplot(2, 3, i + 1)
73
- # plt.imshow(fm[:, :, i], cmap='gray')
74
- # plt.title(f"Feature Map {i+1}")
75
- # plt.axis('off')
76
-
77
- # elif layer_ind == 1:
78
- # fig1 = plt.figure(figsize=(25, 15))
79
- # for i in range(16):
80
- # plt.subplot(2, 8, i + 1)
81
- # plt.imshow(fm[:, :, i], cmap='gray')
82
- # plt.title(f"Feature Map {i+1}")
83
- # plt.axis('off')
84
-
85
- # elif layer_ind == 2:
86
- # fig1 = plt.figure(figsize=(100, 50))
87
- # for i in range(120):
88
- # plt.subplot(12,10, i + 1)
89
- # plt.imshow(fm[:, :, i],cmap="gray")
90
- # plt.title(f"Feature Map {i+1}")
91
- # plt.axis('off')
92
-
93
- # plt.tight_layout()
94
- # st.pyplot(fig1)
95
-
96
-
97
-
98
-
99
-
100
-
101
  import streamlit as st
102
- import numpy as np
103
  import matplotlib.pyplot as plt
104
  import keras
105
  from sklearn.preprocessing import MinMaxScaler
@@ -108,76 +8,90 @@ from keras.layers import Conv2D
108
  import cv2
109
  import tensorflow as tf
110
 
111
- # -----------------------------
112
- # Helper function to normalize feature maps
113
- def normalize_feature_map(fm):
114
- fm_min = np.min(fm)
115
- fm_max = np.max(fm)
116
- if fm_max - fm_min == 0:
117
- return np.zeros_like(fm)
118
- return (fm - fm_min) / (fm_max - fm_min)
119
- # -----------------------------
120
-
121
- # Load model
122
  model = keras.models.load_model("model.keras")
123
 
124
- # Sidebar UI
 
 
 
 
125
  st.sidebar.title("Controls")
126
  uploaded_img = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
127
  options = ['1st Convolution', '2nd Convolution', '3rd Convolution']
128
  selected_option = st.sidebar.selectbox('Select convolution layer:', options)
129
 
130
- # Extract Conv2D layers from model
131
  conv_layers = [layer for layer in model.layers if isinstance(layer, Conv2D)]
132
  layer_ind = options.index(selected_option)
133
  selected_layer = conv_layers[layer_ind]
134
 
135
- # Show filters (kernels) from selected layer
136
  fig = plt.figure(figsize=(12, 4))
 
 
 
137
  scaler = MinMaxScaler()
138
 
 
139
  for j in range(6):
140
- weights = selected_layer.get_weights()[0][:, :, 0, j]
 
141
  norm_weights = scaler.fit_transform(weights)
142
- plt.subplot(2, 3, j + 1)
143
- plt.imshow(norm_weights, cmap='gray')
144
- plt.title(f"Filter {j+1}")
145
  plt.axis('off')
146
-
147
  plt.tight_layout()
148
  st.pyplot(fig)
149
 
150
- # Process uploaded image
151
  if uploaded_img is not None:
152
- # Read image from uploader
153
  file_bytes = np.frombuffer(uploaded_img.read(), np.uint8)
154
  img = cv2.imdecode(file_bytes, cv2.IMREAD_GRAYSCALE)
155
- img_resized = cv2.resize(img, (28, 28), interpolation=cv2.INTER_AREA)
 
 
 
 
156
 
157
- # Normalize input image
158
- img_norm = img_resized.astype('float32') / 255.0
159
- input_img = img_norm.reshape(1, 28, 28, 1)
160
 
161
- # Display resized input
162
- st.image(img_resized, caption="Uploaded Image (Resized to 28x28)", use_container_width=True, channels="GRAY")
163
 
164
- # Create intermediate model to get output from selected conv layer
165
- func_model = Model(inputs=model.layers[0].input, outputs=selected_layer.output)
166
- fm = func_model.predict(input_img)[0] # shape: (H, W, C)
167
 
168
- # Display feature maps
169
- num_feature_maps = fm.shape[-1]
170
 
171
- # Automatically adjust grid size
172
- cols = 8 if layer_ind == 1 else (10 if layer_ind == 2 else 3)
173
- rows = int(np.ceil(num_feature_maps / cols))
174
 
175
- fig1 = plt.figure(figsize=(cols * 3, rows * 3))
176
- for i in range(num_feature_maps):
177
- plt.subplot(rows, cols, i + 1)
178
- plt.imshow(normalize_feature_map(fm[:, :, i]), cmap='gray')
179
- plt.title(f"Feature Map {i+1}")
180
- plt.axis('off')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
 
182
  plt.tight_layout()
183
  st.pyplot(fig1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import numpy as np
3
  import matplotlib.pyplot as plt
4
  import keras
5
  from sklearn.preprocessing import MinMaxScaler
 
8
  import cv2
9
  import tensorflow as tf
10
 
 
 
 
 
 
 
 
 
 
 
 
11
  model = keras.models.load_model("model.keras")
12
 
13
+ # uploaded_img = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
14
+
15
+ # options = ['1st Convolution', '2nd Convolution', '3rd Convolution']
16
+ # selected_option = st.selectbox('Choose an option:', options)
17
+
18
  st.sidebar.title("Controls")
19
  uploaded_img = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
20
  options = ['1st Convolution', '2nd Convolution', '3rd Convolution']
21
  selected_option = st.sidebar.selectbox('Select convolution layer:', options)
22
 
23
+
24
  conv_layers = [layer for layer in model.layers if isinstance(layer, Conv2D)]
25
  layer_ind = options.index(selected_option)
26
  selected_layer = conv_layers[layer_ind]
27
 
 
28
  fig = plt.figure(figsize=(12, 4))
29
+
30
+ #layer_ind = options.index(selected_option)
31
+ # selected_layer = conv_layers[layer_ind]
32
  scaler = MinMaxScaler()
33
 
34
+ # for i in range(3):
35
  for j in range(6):
36
+ layer=selected_layer
37
+ weights=layer.get_weights()[0][:,:,0,j]
38
  norm_weights = scaler.fit_transform(weights)
39
+ plt.subplot(2,3,j+1)
40
+ plt.imshow(norm_weights,cmap='gray')
41
+ plt.title(f"Filters {j+1}")
42
  plt.axis('off')
 
43
  plt.tight_layout()
44
  st.pyplot(fig)
45
 
46
+
47
  if uploaded_img is not None:
48
+ #st.image(uploaded_img, caption="Uploaded Image", use_column_width=True)
49
  file_bytes = np.frombuffer(uploaded_img.read(), np.uint8)
50
  img = cv2.imdecode(file_bytes, cv2.IMREAD_GRAYSCALE)
51
+ img_resized = cv2.resize(img,(28,28),interpolation=cv2.INTER_AREA)
52
+ #img_norm = img_resized.astype('float32') / 255.0
53
+ input_img = img_resized.reshape(1,28,28,1)
54
+
55
+
56
 
57
+ st.image(img_resized, caption="Uploaded Image (Resized to 28x28)", use_container_width =True, channels="GRAY")
 
 
58
 
59
+ #layer_ind = options.index(selected_option)
60
+ # selected_layer = conv_layers[layer_ind]
61
 
62
+ #func_model = Model(inputs = model.layers[0].input, outputs = model.selected_layer.output)
63
+ func_model = Model(inputs = model.layers[0].input, outputs = selected_layer.output)
 
64
 
 
 
65
 
66
+ fm = func_model.predict(input_img)
67
+ fm = fm[0]
 
68
 
69
+ if layer_ind == 0:
70
+ fig1 = plt.figure(figsize=(12, 4))
71
+ for i in range(6):
72
+ plt.subplot(2, 3, i + 1)
73
+ plt.imshow(fm[:, :, i], cmap='gray')
74
+ plt.title(f"Feature Map {i+1}")
75
+ plt.axis('off')
76
+
77
+ elif layer_ind == 1:
78
+ fig1 = plt.figure(figsize=(25, 15))
79
+ for i in range(16):
80
+ plt.subplot(2, 8, i + 1)
81
+ plt.imshow(fm[:, :, i], cmap='gray')
82
+ plt.title(f"Feature Map {i+1}")
83
+ plt.axis('off')
84
+
85
+ elif layer_ind == 2:
86
+ fig1 = plt.figure(figsize=(100, 50))
87
+ for i in range(120):
88
+ plt.subplot(12,10, i + 1)
89
+ plt.imshow(fm[:, :, i],cmap="gray")
90
+ plt.title(f"Feature Map {i+1}")
91
+ plt.axis('off')
92
 
93
  plt.tight_layout()
94
  st.pyplot(fig1)
95
+
96
+
97
+