xValentim commited on
Commit
b8dc7e7
·
1 Parent(s): 85e869a

Add app and requirements

Browse files
Files changed (2) hide show
  1. app.py +67 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import dependencies
2
+ import tensorflow as tf
3
+ from tensorflow import keras
4
+
5
+ import numpy as np
6
+ import pandas as pd
7
+ import matplotlib.pyplot as plt
8
+
9
+ import gradio as gr
10
+ import os
11
+
12
+ (X_train_full, y_train_full), (X_test, y_test) = keras.datasets.mnist.load_data()
13
+
14
+ class Sampling(tf.keras.layers.Layer):
15
+ def call(self, inputs):
16
+ mean, log_var = inputs
17
+ return tf.random.normal(tf.shape(log_var)) * tf.exp(log_var / 2) + mean
18
+
19
+ custom_objects = {'Sampling': Sampling}
20
+
21
+ variational_ae = keras.models.load_model('./vae_autoencoder.keras', custom_objects=custom_objects)
22
+ variational_encoder = keras.models.load_model('./vae_encoder.keras', custom_objects=custom_objects)
23
+ variational_decoder = keras.models.load_model('./vae_decoder.keras', custom_objects=custom_objects)
24
+
25
+ def get_center_of_mass(df):
26
+ cms = []
27
+ labels = df.label.unique()
28
+ for label in sorted(labels):
29
+ cm = df[df.label == label][df.columns[:-1]].sum() / df[df.label == label].shape[0]
30
+ cms.append(cm.values)
31
+ return cms
32
+
33
+ _, _, X_test_encoded = variational_encoder(X_test.reshape(-1, 28, 28, 1))
34
+ df = pd.concat([pd.DataFrame(X_test_encoded), pd.DataFrame(y_test, columns=['label'])], axis=1)
35
+ cms = get_center_of_mass(df)
36
+ imgs_cm = [np.clip(variational_decoder(cms[i].reshape(1, -1))[0], 0.0, 255.0) for i in range(10)]
37
+
38
+ many = []
39
+ for i in range(10):#range(4, len(cms) - 5):
40
+ A, B = cms[i - 1].copy(), cms[i].copy()
41
+ versor = (B - A) / np.linalg.norm(B - A)
42
+ d = np.linspace(0, np.linalg.norm(B - A), 100)
43
+ for i in range(100):
44
+ row = A + d[i] * versor
45
+ many.append(row.copy())
46
+
47
+ many = tf.convert_to_tensor(tf.squeeze(np.array(many)))
48
+ images = variational_decoder(many).numpy()
49
+ images_converted = []
50
+
51
+ for x in images:
52
+ new_x = ((x - x.min()) / (x - x.min()).max()) * 255
53
+ new_x = new_x.astype(np.uint8)
54
+ images_converted.append(new_x)
55
+
56
+ images_converted = np.array(images_converted)
57
+
58
+
59
+ def image_classifier(value):
60
+ return np.clip(((variational_decoder(many[(value * 10)].numpy().reshape(1, -1))[0]) * 255), 0, 255).astype(int)[:, :, 0]
61
+
62
+ input_value_d = gr.Slider(minimum=0, maximum=99, step=1)
63
+ demo = gr.Interface(fn=image_classifier, inputs=input_value_d, outputs="image", live=True)
64
+
65
+ # Launch the interface
66
+ if __name__ == "__main__":
67
+ demo.launch(server_port=501, debug=True, server_name='localhost')
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ tensorflow
2
+ pandas
3
+ numpy
4
+ gradio
5
+ matplotlib