|
import streamlit as st |
|
|
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
import pandas as pd |
|
import seaborn as sns |
|
import warnings |
|
warnings.filterwarnings('ignore') |
|
|
|
|
|
import tensorflow |
|
print (tensorflow.__version__) |
|
|
|
st.header("Welcome to the Generative Playground") |
|
|
|
from tensorflow.keras.datasets import mnist,cifar10 |
|
|
|
option = st.selectbox( |
|
"Which model would you like to get prediction with?", |
|
("None","Auto-Regressor", "Auto-Encoder", "Diffusion-Model","Other")) |
|
|
|
st.write("You selected:", option) |
|
|
|
if option == "None": |
|
st.write("Please Select the model to get the fun prediction.... :)") |
|
|
|
if option == "Auto-Encoder": |
|
st.write("It is under development") |
|
st.write("Stay tune... Comming soon... :)") |
|
|
|
if option == "Other": |
|
st.write("Stay tune... Updating soon... :)") |
|
|
|
if option == "Diffusion-Model": |
|
st.write("It is under development") |
|
st.write("Stay tune... Comming soon... :)") |
|
|
|
if option == "Auto-Regressor": |
|
if st.button("Run"): |
|
st.write("Running Auto-Regressor") |
|
|
|
st.write("trained on --> cifar-10 dataset, RTX-GPU's, 50-epochs") |
|
st.write("This is trail model, updated version will be updated consicutively.") |
|
|
|
(trainX, trainy), (testX, testy) = cifar10.load_data() |
|
|
|
print('Training data shapes: X=%s, y=%s' % (trainX.shape, trainy.shape)) |
|
print('Testing data shapes: X=%s, y=%s' % (testX.shape, testy.shape)) |
|
|
|
|
|
|
|
for k in range(4): |
|
fig = plt.figure(figsize=(9,6)) |
|
for j in range(9): |
|
i = np.random.randint(0, 10000) |
|
plt.subplot(990 + 1 + j) |
|
plt.imshow(trainX[i], cmap='gray_r') |
|
|
|
plt.axis('off') |
|
|
|
plt.show() |
|
st.pyplot(fig) |
|
|
|
|
|
|
|
|
|
trainX = np.where(trainX < (0.33 * 256), 0, 1) |
|
train_data = trainX.astype(np.float32) |
|
|
|
testX = np.where(testX < (0.33 * 256), 0, 1) |
|
test_data = testX.astype(np.float32) |
|
|
|
train_data = np.reshape(train_data, (50000, 32, 32, 3)) |
|
test_data = np.reshape(test_data, (10000, 32, 32, 3)) |
|
|
|
print (train_data.shape, test_data.shape) |
|
|
|
|
|
import tensorflow |
|
|
|
class PixelConvLayer(tensorflow.keras.layers.Layer): |
|
def __init__(self, mask_type, **kwargs): |
|
super(PixelConvLayer, self).__init__() |
|
self.mask_type = mask_type |
|
self.conv = tensorflow.keras.layers.Conv2D(**kwargs) |
|
|
|
def build(self, input_shape): |
|
|
|
self.conv.build(input_shape) |
|
|
|
kernel_shape = self.conv.kernel.get_shape() |
|
self.mask = np.zeros(shape=kernel_shape) |
|
self.mask[: kernel_shape[0] // 2, ...] = 1.0 |
|
self.mask[kernel_shape[0] // 2, : kernel_shape[1] // 2, ...] = 1.0 |
|
if self.mask_type == "B": |
|
self.mask[kernel_shape[0] // 2, kernel_shape[1] // 2, ...] = 1.0 |
|
|
|
def call(self, inputs): |
|
self.conv.kernel.assign(self.conv.kernel * self.mask) |
|
return self.conv(inputs) |
|
|
|
|
|
|
|
|
|
class ResidualBlock(tensorflow.keras.layers.Layer): |
|
def __init__(self, filters, **kwargs): |
|
super(ResidualBlock, self).__init__(**kwargs) |
|
self.conv1 = tensorflow.keras.layers.Conv2D( |
|
filters=filters, kernel_size=1, activation="relu" |
|
) |
|
self.pixel_conv = PixelConvLayer( |
|
mask_type="B", |
|
filters=filters // 2, |
|
kernel_size=3, |
|
activation="relu", |
|
padding="same", |
|
) |
|
self.conv2 = tensorflow.keras.layers.Conv2D( |
|
filters=filters, kernel_size=1, activation="relu" |
|
) |
|
|
|
def call(self, inputs): |
|
x = self.conv1(inputs) |
|
x = self.pixel_conv(x) |
|
x = self.conv2(x) |
|
return tensorflow.keras.layers.add([inputs, x]) |
|
|
|
inputs = tensorflow.keras.Input(shape=(32,32,3)) |
|
x = PixelConvLayer( |
|
mask_type="A", filters=128, kernel_size=7, activation="relu", padding="same" |
|
)(inputs) |
|
|
|
for _ in range(5): |
|
x = ResidualBlock(filters=128)(x) |
|
|
|
for _ in range(2): |
|
x = PixelConvLayer( |
|
mask_type="B", |
|
filters=128, |
|
kernel_size=1, |
|
strides=1, |
|
activation="relu", |
|
padding="valid", |
|
)(x) |
|
|
|
out = tensorflow.keras.layers.Conv2D( |
|
filters=3, kernel_size=1, strides=1, activation="sigmoid", padding="valid" |
|
)(x) |
|
|
|
pixel_cnn = tensorflow.keras.Model(inputs, out) |
|
pixel_cnn.summary() |
|
|
|
adam = tensorflow.keras.optimizers.Adam(learning_rate=0.0005) |
|
pixel_cnn.compile(optimizer=adam, loss="binary_crossentropy") |
|
|
|
|
|
|
|
import os |
|
checkpoint_path = "training_1/cp.ckpt" |
|
|
|
checkpoint_dir = os.path.dirname(checkpoint_path) |
|
|
|
|
|
pixel_cnn.load_weights(checkpoint_path) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from tqdm import tqdm |
|
|
|
|
|
|
|
batch = 1 |
|
pixels = np.zeros(shape=(batch,) + (pixel_cnn.input_shape)[1:]) |
|
batch, rows, cols, channels = pixels.shape |
|
|
|
print(pixels.shape) |
|
|
|
|
|
import time |
|
|
|
|
|
|
|
st.caption("Generating..... pls.. wait.. :)") |
|
my_bar = st.progress(0) |
|
|
|
|
|
|
|
for row in tqdm(range(rows)): |
|
for col in range(cols): |
|
for channel in range(channels): |
|
time.sleep(0.01) |
|
|
|
|
|
probs = pixel_cnn.predict(pixels)[:, row, col, channel] |
|
|
|
|
|
pixels[:, row, col, channel] = tensorflow.math.ceil( |
|
probs - tensorflow.random.uniform(probs.shape) |
|
) |
|
my_bar.progress(int(row*3.125)) |
|
|
|
|
|
|
|
|
|
|
|
my_bar.progress(100) |
|
time.sleep(1) |
|
|
|
|
|
from PIL import Image |
|
|
|
|
|
|
|
generated_image = Image.fromarray((pixels[-1] * 255).astype(np.uint8), 'RGB') |
|
|
|
|
|
st.image(generated_image, caption="Generated Image") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|