charulp2499 commited on
Commit
d8542c7
·
verified ·
1 Parent(s): 1e6236f

Delete autoregressor_app.py

Browse files
Files changed (1) hide show
  1. autoregressor_app.py +0 -195
autoregressor_app.py DELETED
@@ -1,195 +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
- st.write("This is an autoregressor model on cifar10 data set, with 50 epochs and 16 batch size trained only. RTX GPU is used to train the model.")
16
-
17
- from tensorflow.keras.datasets import mnist,cifar10
18
-
19
-
20
- (trainX, trainy), (testX, testy) = cifar10.load_data()
21
-
22
- print('Training data shapes: X=%s, y=%s' % (trainX.shape, trainy.shape))
23
- print('Testing data shapes: X=%s, y=%s' % (testX.shape, testy.shape))
24
-
25
-
26
-
27
- for k in range(4):
28
- fig = plt.figure(figsize=(9,6))
29
- for j in range(9):
30
- i = np.random.randint(0, 10000)
31
- plt.subplot(990 + 1 + j)
32
- plt.imshow(trainX[i], cmap='gray_r')
33
- # st.pyplot(fig)
34
- plt.axis('off')
35
- #plt.title(trainy[i])
36
- plt.show()
37
- st.pyplot(fig)
38
-
39
-
40
- # asdfaf
41
-
42
- trainX = np.where(trainX < (0.33 * 256), 0, 1)
43
- train_data = trainX.astype(np.float32)
44
-
45
- testX = np.where(testX < (0.33 * 256), 0, 1)
46
- test_data = testX.astype(np.float32)
47
-
48
- train_data = np.reshape(train_data, (50000, 32, 32, 3))
49
- test_data = np.reshape(test_data, (10000, 32, 32, 3))
50
-
51
- print (train_data.shape, test_data.shape)
52
-
53
-
54
- import tensorflow
55
-
56
- class PixelConvLayer(tensorflow.keras.layers.Layer):
57
- def __init__(self, mask_type, **kwargs):
58
- super(PixelConvLayer, self).__init__()
59
- self.mask_type = mask_type
60
- self.conv = tensorflow.keras.layers.Conv2D(**kwargs)
61
-
62
- def build(self, input_shape):
63
- # Build the conv2d layer to initialize kernel variables
64
- self.conv.build(input_shape)
65
- # Use the initialized kernel to create the mask
66
- kernel_shape = self.conv.kernel.get_shape()
67
- self.mask = np.zeros(shape=kernel_shape)
68
- self.mask[: kernel_shape[0] // 2, ...] = 1.0
69
- self.mask[kernel_shape[0] // 2, : kernel_shape[1] // 2, ...] = 1.0
70
- if self.mask_type == "B":
71
- self.mask[kernel_shape[0] // 2, kernel_shape[1] // 2, ...] = 1.0
72
-
73
- def call(self, inputs):
74
- self.conv.kernel.assign(self.conv.kernel * self.mask)
75
- return self.conv(inputs)
76
-
77
-
78
- # Next, we build our residual block layer.
79
- # This is just a normal residual block, but based on the PixelConvLayer.
80
- class ResidualBlock(tensorflow.keras.layers.Layer):
81
- def __init__(self, filters, **kwargs):
82
- super(ResidualBlock, self).__init__(**kwargs)
83
- self.conv1 = tensorflow.keras.layers.Conv2D(
84
- filters=filters, kernel_size=1, activation="relu"
85
- )
86
- self.pixel_conv = PixelConvLayer(
87
- mask_type="B",
88
- filters=filters // 2,
89
- kernel_size=3,
90
- activation="relu",
91
- padding="same",
92
- )
93
- self.conv2 = tensorflow.keras.layers.Conv2D(
94
- filters=filters, kernel_size=1, activation="relu"
95
- )
96
-
97
- def call(self, inputs):
98
- x = self.conv1(inputs)
99
- x = self.pixel_conv(x)
100
- x = self.conv2(x)
101
- return tensorflow.keras.layers.add([inputs, x])
102
-
103
- inputs = tensorflow.keras.Input(shape=(32,32,3))
104
- x = PixelConvLayer(
105
- mask_type="A", filters=128, kernel_size=7, activation="relu", padding="same"
106
- )(inputs)
107
-
108
- for _ in range(5):
109
- x = ResidualBlock(filters=128)(x)
110
-
111
- for _ in range(2):
112
- x = PixelConvLayer(
113
- mask_type="B",
114
- filters=128,
115
- kernel_size=1,
116
- strides=1,
117
- activation="relu",
118
- padding="valid",
119
- )(x)
120
-
121
- out = tensorflow.keras.layers.Conv2D(
122
- filters=3, kernel_size=1, strides=1, activation="sigmoid", padding="valid"
123
- )(x)
124
-
125
- pixel_cnn = tensorflow.keras.Model(inputs, out)
126
- pixel_cnn.summary()
127
-
128
- adam = tensorflow.keras.optimizers.Adam(learning_rate=0.0005)
129
- pixel_cnn.compile(optimizer=adam, loss="binary_crossentropy")
130
-
131
-
132
- # %%
133
- import os
134
- checkpoint_path = "training_1/cp.ckpt"
135
- checkpoint_dir = os.path.dirname(checkpoint_path)
136
-
137
-
138
- pixel_cnn.load_weights(checkpoint_path)
139
-
140
-
141
- # %% [markdown]
142
- # # Display Results 81 images
143
-
144
- # %%
145
- #from IPython.display import Image, display
146
- from tqdm import tqdm_notebook
147
-
148
-
149
- # Create an empty array of pixels.
150
- batch = 81
151
- pixels = np.zeros(shape=(batch,) + (pixel_cnn.input_shape)[1:])
152
- batch, rows, cols, channels = pixels.shape
153
-
154
- print(pixels.shape)
155
-
156
-
157
- import time
158
-
159
- # progress_text = "Operation in progress. Please wait."
160
- # my_bar = st.progress(0, progress_text)
161
- st.caption("Generating..... pls.. wait.. :)")
162
- my_bar = st.progress(0)
163
-
164
-
165
- # Iterate over the pixels because generation has to be done sequentially pixel by pixel.
166
- for row in range(rows):
167
- for col in range(cols):
168
- for channel in range(channels):
169
- time.sleep(0.01)
170
- # Feed the whole array and retrieving the pixel value probabilities for the next
171
- # pixel.
172
- probs = pixel_cnn.predict(pixels)[:, row, col, channel]
173
- # Use the probabilities to pick pixel values and append the values to the image
174
- # frame.
175
- pixels[:, row, col, channel] = tensorflow.math.ceil(
176
- probs - tensorflow.random.uniform(probs.shape)
177
- )
178
- my_bar.progress(row+1)
179
- time.sleep(1)
180
-
181
- counter = 0
182
- for i in range(4):
183
- figout = plt.figure(figsize=(9,6))
184
- for j in range(9):
185
- plt.subplot(990 + 1 + j)
186
- plt.imshow(pixels[counter,:,:,0])#, cmap='gray_r')
187
- counter += 1
188
- plt.axis('off')
189
- plt.show()
190
- st.pyplot(figout)
191
-
192
- # %%
193
-
194
-
195
-