Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,26 +3,17 @@ import numpy as np
|
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import keras
|
| 5 |
from sklearn.preprocessing import MinMaxScaler
|
| 6 |
-
from keras.models import Sequential,Model
|
| 7 |
-
from keras.layers import Input,Dense,Conv2D,AveragePooling2D,Flatten
|
| 8 |
-
|
| 9 |
-
|
| 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 |
-
|
| 17 |
-
# Create the selectbox
|
| 18 |
-
selected_option = st.selectbox(
|
| 19 |
-
'Choose an option:',
|
| 20 |
-
options
|
| 21 |
-
)
|
| 22 |
|
| 23 |
conv_layers = [layer for layer in model.layers if isinstance(layer, Conv2D)]
|
| 24 |
|
| 25 |
-
plt.figure(figsize=(12, 4))
|
| 26 |
scaler = MinMaxScaler()
|
| 27 |
|
| 28 |
# for i in range(3):
|
|
@@ -32,9 +23,35 @@ for j in range(6):
|
|
| 32 |
norm_weights = scaler.fit_transform(weights)
|
| 33 |
plt.subplot(2,3,j+1)
|
| 34 |
plt.imshow(norm_weights,cmap='gray')
|
| 35 |
-
|
| 36 |
-
plt.title(f"Filter {j}")
|
| 37 |
plt.axis('off')
|
| 38 |
plt.tight_layout()
|
| 39 |
-
st.pyplot(
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import keras
|
| 5 |
from sklearn.preprocessing import MinMaxScaler
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
model = keras.models.load_model("model.keras")
|
| 8 |
|
| 9 |
uploaded_img = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 10 |
|
| 11 |
options = ['1st Convolution', '2nd Convolution', '3rd Convolution']
|
| 12 |
+
selected_option = st.selectbox('Choose an option:', options)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
conv_layers = [layer for layer in model.layers if isinstance(layer, Conv2D)]
|
| 15 |
|
| 16 |
+
fig = plt.figure(figsize=(12, 4))
|
| 17 |
scaler = MinMaxScaler()
|
| 18 |
|
| 19 |
# for i in range(3):
|
|
|
|
| 23 |
norm_weights = scaler.fit_transform(weights)
|
| 24 |
plt.subplot(2,3,j+1)
|
| 25 |
plt.imshow(norm_weights,cmap='gray')
|
| 26 |
+
plt.title(f"Conv Layer")
|
|
|
|
| 27 |
plt.axis('off')
|
| 28 |
plt.tight_layout()
|
| 29 |
+
st.pyplot(fig)
|
| 30 |
+
#plt.show();
|
| 31 |
+
|
| 32 |
+
if uploaded_img is not None:
|
| 33 |
+
#st.image(uploaded_img, caption="Uploaded Image", use_column_width=True)
|
| 34 |
+
file_bytes = np.frombuffer(uploaded_img.read(), np.uint8)
|
| 35 |
+
img = cv2.imdecode(file_bytes, cv2.IMREAD_GRAYSCALE)
|
| 36 |
+
img_resized = cv2.resize(img,(28,28),interpolation=cv2.INTER_AREA)
|
| 37 |
+
#img_norm = img_resized.astype('float32') / 255.0
|
| 38 |
+
input_img = img_resized.reshape(1,28,28,1)
|
| 39 |
+
st.image(img_resized, caption="Uploaded Image (Resized to 28x28)", use_column_width=True, channels="GRAY")
|
| 40 |
+
|
| 41 |
+
layer_ind = options.index(selected_option)
|
| 42 |
+
selected_layer = conv_layers[layer_ind]
|
| 43 |
+
|
| 44 |
+
func_model = Model(inputs = model.input, outputs = selected_layer.output)
|
| 45 |
+
|
| 46 |
+
fm = func_model.predict(input_img)
|
| 47 |
+
fm = fm[0]
|
| 48 |
+
|
| 49 |
+
fig1 = plt.figure(figsize=(12, 4))
|
| 50 |
+
for i in range(6):
|
| 51 |
+
plt.subplot(2, 3, i + 1)
|
| 52 |
+
plt.imshow(feature_maps[:, :, i], cmap='gray')
|
| 53 |
+
plt.title(f"Feature Map {i}")
|
| 54 |
+
plt.axis('off')
|
| 55 |
+
|
| 56 |
+
plt.tight_layout()
|
| 57 |
+
st.pyplot(fig1)
|