Delete app.py
Browse files
app.py
DELETED
@@ -1,239 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
|
3 |
-
import numpy as np
|
4 |
-
import matplotlib.pyplot as plt
|
5 |
-
import pandas as pd
|
6 |
-
import seaborn as sns
|
7 |
-
import warnings
|
8 |
-
warnings.filterwarnings('ignore')
|
9 |
-
# %matplotlib inline
|
10 |
-
|
11 |
-
import tensorflow
|
12 |
-
print (tensorflow.__version__)
|
13 |
-
|
14 |
-
st.header("Welcome to the Generative Playground")
|
15 |
-
|
16 |
-
from tensorflow.keras.datasets import mnist,cifar10
|
17 |
-
|
18 |
-
option = st.selectbox(
|
19 |
-
"Which model would you like to get prediction with?",
|
20 |
-
("None","Auto-Regressor", "Auto-Encoder", "Diffusion-Model","Other"))
|
21 |
-
|
22 |
-
st.write("You selected:", option)
|
23 |
-
|
24 |
-
if option == "None":
|
25 |
-
st.write("Please Select the model to get the fun prediction.... :)")
|
26 |
-
|
27 |
-
if option == "Auto-Encoder":
|
28 |
-
st.write("It is under development")
|
29 |
-
st.write("Stay tune... Comming soon... :)")
|
30 |
-
|
31 |
-
if option == "Other":
|
32 |
-
st.write("Stay tune... Updating soon... :)")
|
33 |
-
|
34 |
-
if option == "Diffusion-Model":
|
35 |
-
st.write("It is under development")
|
36 |
-
st.write("Stay tune... Comming soon... :)")
|
37 |
-
|
38 |
-
if option == "Auto-Regressor":
|
39 |
-
if st.button("Run"):
|
40 |
-
st.write("Running Auto-Regressor")
|
41 |
-
|
42 |
-
st.write("trained on --> cifar-10 dataset, RTX-GPU's, 50-epochs")
|
43 |
-
st.write("This is trail model, updated version will be updated consicutively.")
|
44 |
-
|
45 |
-
(trainX, trainy), (testX, testy) = cifar10.load_data()
|
46 |
-
|
47 |
-
print('Training data shapes: X=%s, y=%s' % (trainX.shape, trainy.shape))
|
48 |
-
print('Testing data shapes: X=%s, y=%s' % (testX.shape, testy.shape))
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
for k in range(4):
|
53 |
-
fig = plt.figure(figsize=(9,6))
|
54 |
-
for j in range(9):
|
55 |
-
i = np.random.randint(0, 10000)
|
56 |
-
plt.subplot(990 + 1 + j)
|
57 |
-
plt.imshow(trainX[i], cmap='gray_r')
|
58 |
-
# st.pyplot(fig)
|
59 |
-
plt.axis('off')
|
60 |
-
#plt.title(trainy[i])
|
61 |
-
plt.show()
|
62 |
-
st.pyplot(fig)
|
63 |
-
|
64 |
-
|
65 |
-
# asdfaf
|
66 |
-
|
67 |
-
trainX = np.where(trainX < (0.33 * 256), 0, 1)
|
68 |
-
train_data = trainX.astype(np.float32)
|
69 |
-
|
70 |
-
testX = np.where(testX < (0.33 * 256), 0, 1)
|
71 |
-
test_data = testX.astype(np.float32)
|
72 |
-
|
73 |
-
train_data = np.reshape(train_data, (50000, 32, 32, 3))
|
74 |
-
test_data = np.reshape(test_data, (10000, 32, 32, 3))
|
75 |
-
|
76 |
-
print (train_data.shape, test_data.shape)
|
77 |
-
|
78 |
-
|
79 |
-
import tensorflow
|
80 |
-
|
81 |
-
class PixelConvLayer(tensorflow.keras.layers.Layer):
|
82 |
-
def __init__(self, mask_type, **kwargs):
|
83 |
-
super(PixelConvLayer, self).__init__()
|
84 |
-
self.mask_type = mask_type
|
85 |
-
self.conv = tensorflow.keras.layers.Conv2D(**kwargs)
|
86 |
-
|
87 |
-
def build(self, input_shape):
|
88 |
-
# Build the conv2d layer to initialize kernel variables
|
89 |
-
self.conv.build(input_shape)
|
90 |
-
# Use the initialized kernel to create the mask
|
91 |
-
kernel_shape = self.conv.kernel.get_shape()
|
92 |
-
self.mask = np.zeros(shape=kernel_shape)
|
93 |
-
self.mask[: kernel_shape[0] // 2, ...] = 1.0
|
94 |
-
self.mask[kernel_shape[0] // 2, : kernel_shape[1] // 2, ...] = 1.0
|
95 |
-
if self.mask_type == "B":
|
96 |
-
self.mask[kernel_shape[0] // 2, kernel_shape[1] // 2, ...] = 1.0
|
97 |
-
|
98 |
-
def call(self, inputs):
|
99 |
-
self.conv.kernel.assign(self.conv.kernel * self.mask)
|
100 |
-
return self.conv(inputs)
|
101 |
-
|
102 |
-
|
103 |
-
# Next, we build our residual block layer.
|
104 |
-
# This is just a normal residual block, but based on the PixelConvLayer.
|
105 |
-
class ResidualBlock(tensorflow.keras.layers.Layer):
|
106 |
-
def __init__(self, filters, **kwargs):
|
107 |
-
super(ResidualBlock, self).__init__(**kwargs)
|
108 |
-
self.conv1 = tensorflow.keras.layers.Conv2D(
|
109 |
-
filters=filters, kernel_size=1, activation="relu"
|
110 |
-
)
|
111 |
-
self.pixel_conv = PixelConvLayer(
|
112 |
-
mask_type="B",
|
113 |
-
filters=filters // 2,
|
114 |
-
kernel_size=3,
|
115 |
-
activation="relu",
|
116 |
-
padding="same",
|
117 |
-
)
|
118 |
-
self.conv2 = tensorflow.keras.layers.Conv2D(
|
119 |
-
filters=filters, kernel_size=1, activation="relu"
|
120 |
-
)
|
121 |
-
|
122 |
-
def call(self, inputs):
|
123 |
-
x = self.conv1(inputs)
|
124 |
-
x = self.pixel_conv(x)
|
125 |
-
x = self.conv2(x)
|
126 |
-
return tensorflow.keras.layers.add([inputs, x])
|
127 |
-
|
128 |
-
inputs = tensorflow.keras.Input(shape=(32,32,3))
|
129 |
-
x = PixelConvLayer(
|
130 |
-
mask_type="A", filters=128, kernel_size=7, activation="relu", padding="same"
|
131 |
-
)(inputs)
|
132 |
-
|
133 |
-
for _ in range(5):
|
134 |
-
x = ResidualBlock(filters=128)(x)
|
135 |
-
|
136 |
-
for _ in range(2):
|
137 |
-
x = PixelConvLayer(
|
138 |
-
mask_type="B",
|
139 |
-
filters=128,
|
140 |
-
kernel_size=1,
|
141 |
-
strides=1,
|
142 |
-
activation="relu",
|
143 |
-
padding="valid",
|
144 |
-
)(x)
|
145 |
-
|
146 |
-
out = tensorflow.keras.layers.Conv2D(
|
147 |
-
filters=3, kernel_size=1, strides=1, activation="sigmoid", padding="valid"
|
148 |
-
)(x)
|
149 |
-
|
150 |
-
pixel_cnn = tensorflow.keras.Model(inputs, out)
|
151 |
-
pixel_cnn.summary()
|
152 |
-
|
153 |
-
adam = tensorflow.keras.optimizers.Adam(learning_rate=0.0005)
|
154 |
-
pixel_cnn.compile(optimizer=adam, loss="binary_crossentropy")
|
155 |
-
|
156 |
-
|
157 |
-
# %%
|
158 |
-
import os
|
159 |
-
checkpoint_path = "training_1/cp.ckpt"
|
160 |
-
# checkpoint_path = "training_1/cp.weights.h5"
|
161 |
-
checkpoint_dir = os.path.dirname(checkpoint_path)
|
162 |
-
|
163 |
-
|
164 |
-
pixel_cnn.load_weights(checkpoint_path)
|
165 |
-
|
166 |
-
|
167 |
-
# %% [markdown]
|
168 |
-
# # Display Results 81 images
|
169 |
-
|
170 |
-
# %%
|
171 |
-
# from IPython.display import Image, display
|
172 |
-
from tqdm import tqdm
|
173 |
-
|
174 |
-
|
175 |
-
# Create an empty array of pixels.
|
176 |
-
batch = 1
|
177 |
-
pixels = np.zeros(shape=(batch,) + (pixel_cnn.input_shape)[1:])
|
178 |
-
batch, rows, cols, channels = pixels.shape
|
179 |
-
|
180 |
-
print(pixels.shape)
|
181 |
-
|
182 |
-
|
183 |
-
import time
|
184 |
-
|
185 |
-
# progress_text = "Operation in progress. Please wait."
|
186 |
-
# my_bar = st.progress(0, progress_text)
|
187 |
-
st.caption("Generating..... pls.. wait.. :)")
|
188 |
-
my_bar = st.progress(0)
|
189 |
-
|
190 |
-
|
191 |
-
# Iterate over the pixels because generation has to be done sequentially pixel by pixel.
|
192 |
-
for row in tqdm(range(rows)):
|
193 |
-
for col in range(cols):
|
194 |
-
for channel in range(channels):
|
195 |
-
time.sleep(0.01)
|
196 |
-
# Feed the whole array and retrieving the pixel value probabilities for the next
|
197 |
-
# pixel.
|
198 |
-
probs = pixel_cnn.predict(pixels)[:, row, col, channel]
|
199 |
-
# Use the probabilities to pick pixel values and append the values to the image
|
200 |
-
# frame.
|
201 |
-
pixels[:, row, col, channel] = tensorflow.math.ceil(
|
202 |
-
probs - tensorflow.random.uniform(probs.shape)
|
203 |
-
)
|
204 |
-
my_bar.progress(int(row*3.125))
|
205 |
-
# if row<rows/2:
|
206 |
-
# my_bar.progress((rows+1)*2)
|
207 |
-
# else:
|
208 |
-
# my_bar.progress(row+51)
|
209 |
-
|
210 |
-
my_bar.progress(100)
|
211 |
-
time.sleep(1)
|
212 |
-
|
213 |
-
|
214 |
-
from PIL import Image
|
215 |
-
# figout = plt.figure(figsize=(9,6))
|
216 |
-
# st.image(Image.fromarray((pixels[-1] * 255).astype(np.uint8), 'RGB').show(),caption="image")
|
217 |
-
# Convert the generated pixel array to an image
|
218 |
-
generated_image = Image.fromarray((pixels[-1] * 255).astype(np.uint8), 'RGB')
|
219 |
-
|
220 |
-
# Display the image using Streamlit
|
221 |
-
st.image(generated_image, caption="Generated Image")
|
222 |
-
|
223 |
-
# counter = 0
|
224 |
-
# for i in range(4):
|
225 |
-
# figout = plt.figure(figsize=(9,6))
|
226 |
-
# for j in range(9):
|
227 |
-
# plt.subplot(990 + 1 + j)
|
228 |
-
# plt.imshow(pixels[counter,:,:,0])#, cmap='gray_r')
|
229 |
-
# counter += 1
|
230 |
-
# plt.axis('off')
|
231 |
-
# plt.show()
|
232 |
-
# st.pyplot(figout)
|
233 |
-
|
234 |
-
# %%
|
235 |
-
# else:
|
236 |
-
# st.write("Not Available")
|
237 |
-
|
238 |
-
|
239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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