Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
def mock_encoder(image):
|
6 |
+
"""Simulates encoding an image into a latent representation."""
|
7 |
+
return np.random.normal(0, 1, (1, 2)), np.random.normal(0, 1, (1, 2)), np.random.normal(0, 1, (1, 2))
|
8 |
+
|
9 |
+
def mock_decoder(latent_representation):
|
10 |
+
"""Simulates decoding a latent representation back into an image."""
|
11 |
+
return np.random.rand(28, 28, 1) * 255 # Random image for demonstration
|
12 |
+
|
13 |
+
def latent_space_augmentation(image, encoder, decoder, noise_scale=0.1):
|
14 |
+
z_mean, z_log_var, _ = encoder(image)
|
15 |
+
epsilon = np.random.normal(size=z_mean.shape)
|
16 |
+
z_augmented = z_mean + np.exp(0.5 * z_log_var) * epsilon * noise_scale
|
17 |
+
augmented_image = decoder(z_augmented)
|
18 |
+
return np.squeeze(augmented_image)
|
19 |
+
|
20 |
+
st.title("VAE-based Image Augmentation Demo")
|
21 |
+
|
22 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
23 |
+
if uploaded_image is not None:
|
24 |
+
image = Image.open(uploaded_image).convert('L')
|
25 |
+
image = image.resize((28, 28)) # Resizing for simplicity
|
26 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
27 |
+
|
28 |
+
if st.button("Augment Image"):
|
29 |
+
# Convert PIL image to numpy array
|
30 |
+
image_array = np.array(image) / 255.0 # Normalize the image
|
31 |
+
image_array = image_array.reshape((28, 28, 1)) # Reshape for the mock encoder/decoder
|
32 |
+
|
33 |
+
# Perform augmentation
|
34 |
+
augmented_image = latent_space_augmentation(image_array, mock_encoder, mock_decoder)
|
35 |
+
|
36 |
+
# Display the augmented image
|
37 |
+
st.image(augmented_image, caption="Augmented Image", clamp=True, use_column_width=True)
|