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