mattritchey commited on
Commit
d2fc011
·
1 Parent(s): d929d14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -9
app.py CHANGED
@@ -9,6 +9,16 @@ import streamlit as st
9
  from PIL import Image
10
  import pandas as pd
11
  import numpy as np
 
 
 
 
 
 
 
 
 
 
12
 
13
  model_type = st.sidebar.selectbox(
14
  'Select Model', ('VGG16', 'VGG19', 'ResNet50V2', 'MobileNetV2'))
@@ -18,7 +28,7 @@ model_type2 = models[model_type]
18
 
19
  top_n = st.sidebar.selectbox('Number of Results', (3, 5, 10))
20
  results = st.sidebar.selectbox('Display Summary', ('No','Yes'))
21
-
22
 
23
  exec(f'from keras.applications.{model_type2} import {model_type}')
24
  exec(
@@ -31,7 +41,7 @@ try:
31
  img = Image.open(img_path)
32
  except:
33
  img = Image.open('dog.jpg')
34
- st.image(img)
35
 
36
  img = img.resize((224, 224)) # Resize to match VGG16 input size
37
  x = np.array(img)
@@ -50,11 +60,77 @@ df = df[['Object', 'Percent Certainty']]
50
  df['Percent Certainty'] = df['Percent Certainty'].apply(
51
  lambda x: '{:.2%}'.format(x))
52
 
53
- st.dataframe(df)
54
 
55
- if results=='Yes':
56
- stringlist = []
57
- model.summary(print_fn=lambda x: stringlist.append(x))
58
- short_model_summary = "\n".join(stringlist)
59
- print(short_model_summary)
60
- st.write(short_model_summary)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  from PIL import Image
10
  import pandas as pd
11
  import numpy as np
12
+ from keras import layers
13
+ import matplotlib.pyplot as plt
14
+
15
+ def get_img_array(img_path, target_size):
16
+ array = keras.utils.img_to_array(img)
17
+ array = np.expand_dims(array, axis=0)
18
+ return array
19
+ st.set_page_config(layout="wide")
20
+
21
+
22
 
23
  model_type = st.sidebar.selectbox(
24
  'Select Model', ('VGG16', 'VGG19', 'ResNet50V2', 'MobileNetV2'))
 
28
 
29
  top_n = st.sidebar.selectbox('Number of Results', (3, 5, 10))
30
  results = st.sidebar.selectbox('Display Summary', ('No','Yes'))
31
+ display = st.sidebar.selectbox('Display Filtered Images', ('No','Yes'))
32
 
33
  exec(f'from keras.applications.{model_type2} import {model_type}')
34
  exec(
 
41
  img = Image.open(img_path)
42
  except:
43
  img = Image.open('dog.jpg')
44
+
45
 
46
  img = img.resize((224, 224)) # Resize to match VGG16 input size
47
  x = np.array(img)
 
60
  df['Percent Certainty'] = df['Percent Certainty'].apply(
61
  lambda x: '{:.2%}'.format(x))
62
 
63
+ # with st.container():
64
 
65
+ with st.container():
66
+ col1, col2 = st.columns((1,3))
67
+ with col1:
68
+ st.image(img,width=400)
69
+ with col2:
70
+ st.dataframe(df)
71
+
72
+
73
+ with st.container():
74
+ col1, col2 = st.columns((2, 4))
75
+ if results=='Yes':
76
+ with col1:
77
+ stringlist = []
78
+ model.summary(print_fn=lambda x: stringlist.append(x))
79
+ short_model_summary = "\n".join(stringlist)
80
+ print(short_model_summary)
81
+ st.write(short_model_summary)
82
+
83
+
84
+ if display =='Yes':
85
+ img_tensor = get_img_array(img, target_size=(224, 224))
86
+ layer_outputs = []
87
+ layer_names = []
88
+ for layer in model.layers:
89
+ if isinstance(layer, (layers.Conv2D, layers.MaxPooling2D)):
90
+ layer_outputs.append(layer.output)
91
+ layer_names.append(layer.name)
92
+ activation_model = keras.Model(inputs=model.input, outputs=layer_outputs)
93
+
94
+ activations = activation_model.predict(img_tensor)
95
+
96
+ first_layer_activation = activations[0]
97
+
98
+ plt.matshow(first_layer_activation[0, :, :, 5], cmap="viridis")
99
+
100
+ images_per_row = 16
101
+ all_pngs=[]
102
+ for layer_name, layer_activation in zip(layer_names, activations):
103
+ n_features = layer_activation.shape[-1]
104
+ size = layer_activation.shape[1]
105
+ n_cols = n_features // images_per_row
106
+ display_grid = np.zeros(((size + 1) * n_cols - 1,
107
+ images_per_row * (size + 1) - 1))
108
+ for col in range(n_cols):
109
+ for row in range(images_per_row):
110
+ channel_index = col * images_per_row + row
111
+ channel_image = layer_activation[0, :, :, channel_index].copy()
112
+ if channel_image.sum() != 0:
113
+ channel_image -= channel_image.mean()
114
+ channel_image /= channel_image.std()
115
+ channel_image *= 64
116
+ channel_image += 128
117
+ channel_image = np.clip(channel_image, 0, 255).astype("uint8")
118
+ display_grid[
119
+ col * (size + 1): (col + 1) * size + col,
120
+ row * (size + 1) : (row + 1) * size + row] = channel_image
121
+ scale = 1. / size
122
+ plt.figure(figsize=(scale * display_grid.shape[1],
123
+ scale * display_grid.shape[0]))
124
+ plt.title(layer_name)
125
+ plt.grid(False)
126
+ plt.axis("off")
127
+ plt.imshow(display_grid, aspect="auto", cmap="viridis")
128
+
129
+ filename=f'{layer_name}.png'
130
+ plt.savefig(f'{layer_name}.png')
131
+ all_pngs.append(filename)
132
+ with col2:
133
+ for i in all_pngs: st.image(i)
134
+
135
+
136
+