-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrainGenerator.py
291 lines (240 loc) · 11.5 KB
/
TrainGenerator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from numpy.testing._private.utils import print_assert_equal
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Input, Dense, Activation, Flatten, Reshape
from tensorflow.keras.layers import Conv2D, Conv2DTranspose, UpSampling1D, Convolution2D
from tensorflow.keras.layers import BatchNormalization, MaxPooling1D, Conv1D, UpSampling2D, Conv1DTranspose
from tensorflow.keras.layers import LeakyReLU, Dropout
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
import random
from tensorflow.python.keras.backend import dropout
from tensorflow.python.keras.layers.advanced_activations import Softmax
from Utils import *
from random import shuffle
from tqdm import tqdm
from tensorflow.keras.utils import to_categorical
import tensorflow as tf
n_classes = 6
classNames = ['CoV-2 (B)', 'CoV-2 (B.1.1.7)', 'CoV-2 (B.1.351)', 'CoV-2 (B.1.617.2)', 'CoV-2 (C.37)', 'CoV-2 (P.1)']
all_data_class1 = read_seq_new(r'D:\3. Imam University Research\COVID-19 RNA Analysis\Dataset\CovidVariantsDataset\SARS-CoV-2 (B)\ncbi_dataset\data\genomic.fna',0)
# all_data_class2 = read_seq_new(r'D:\3. Imam University Research\COVID-19 RNA Analysis\Dataset\CovidVariantsDataset\SARS-CoV-2 (B.1.1.7)\ncbi_dataset\data\genomic.fna',1)
# all_data_class3 = read_seq_new(r'D:\3. Imam University Research\COVID-19 RNA Analysis\Dataset\CovidVariantsDataset\SARS-CoV-2 (B.1.351)\ncbi_dataset\data\genomic.fna',2)
# all_data_class4 = read_seq_new(r'D:\3. Imam University Research\COVID-19 RNA Analysis\Dataset\CovidVariantsDataset\SARS-CoV-2 (B.1.617.2)\ncbi_dataset\data\genomic.fna',3)
# all_data_class5 = read_seq_new(r'D:\3. Imam University Research\COVID-19 RNA Analysis\Dataset\CovidVariantsDataset\SARS-CoV-2 (C.37)\ncbi_dataset\data\genomic.fna',4)
# all_data_class6 = read_seq_new(r'D:\3. Imam University Research\COVID-19 RNA Analysis\Dataset\CovidVariantsDataset\SARS-CoV-2 (P.1)\ncbi_dataset\data\genomic.fna',5)
# all_data_class7 = read_seq_new(r'D:\3. Imam University Research\COVID-19 RNA Analysis\Dataset\GRCh38_latest_genomic.fna\GRCh38_latest_genomic.fna',6)
all_data=[]
for itm in all_data_class1:
all_data.append(itm)
# for itm in all_data_class2:
# all_data.append(itm)
# for itm in all_data_class3:
# all_data.append(itm)
# for itm in all_data_class4:
# all_data.append(itm)
# for itm in all_data_class5:
# all_data.append(itm)
# for itm in all_data_class6:
# all_data.append(itm)
# for itm in all_data_class7:
# all_data.append(itm)
shuffle(all_data)
x=[]
y=[]
for itm in all_data:
x.append(itm[0])
y.append(np.array(itm[1]))
x_train,x_test,y_train,y_test= train_test_split(x,y, test_size=0.2)
x_train=np.asarray(x_train,dtype=np.float)
x_test=np.asarray(x_test,dtype=np.float)
y_train=np.asarray(y_train)
y_test=np.asarray(y_test)
encoded = to_categorical([y_train])
y_train = np.squeeze(encoded)
encoded = to_categorical([y_test])
y_test = np.squeeze(encoded)
# Set the dimensions of the noise
z_dim = 100
nch = 500
# # Generator
# adam = Adam(lr=0.0002, beta_1=0.5)
# g = Sequential()
# g.add(Dense(nch*3*4, input_dim=z_dim))
# g.add(Reshape((nch*3, 4)))
# g.add(BatchNormalization())
# g.add(Activation(LeakyReLU(alpha=0.2)))
# g.add(UpSampling1D(size=2))
# g.add(Conv1D(int(nch/8),4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
# g.add(BatchNormalization())
# g.add(Activation(LeakyReLU(alpha=0.2)))
# g.add(Conv1D(4,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
# g.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
# g.summary()
# d = Sequential()
# d.add(Conv1D(int(nch/4),4,padding='same', input_shape=(3000, 4), activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
# d.add(Conv1D(int(nch/8),4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
# d.add(BatchNormalization())
# d.add(Activation(LeakyReLU(alpha=0.2)))
# d.add(Conv1D(int(nch/10),4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
# d.add(Activation(LeakyReLU(alpha=0.2)))
# d.add(Flatten())
# d.add(Dense(112, activation=LeakyReLU(alpha=0.2)))
# d.add(Dense(1, activation='sigmoid'))
# d.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
# d.summary()
# d.trainable = False
# inputs = Input(shape=(z_dim, ))
# hidden = g(inputs)
# output = d(hidden)
# gan = Model(inputs, output)
# gan.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
# gan.summary()
# Set the dimensions of the noise
z_dim = 100
nch = 100
# Generator
adam = Adam(lr=0.0002, beta_1=0.5)
g = Sequential()
g.add(Dense(nch*4, input_dim=z_dim))
g.add(Reshape((nch, 4)))
g.add(BatchNormalization())
g.add(UpSampling1D(2))
g.add(Conv1D(128,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
g.add(Conv1D(256,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
g.add(BatchNormalization())
g.add(UpSampling1D(5))
g.add(Conv1D(256,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
g.add(Conv1D(128,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
g.add(Conv1D(128,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
g.add(BatchNormalization())
g.add(UpSampling1D(10))
g.add(Conv1D(256,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
g.add(Conv1D(128,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
g.add(Conv1D(64,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
g.add(BatchNormalization())
g.add(UpSampling1D(3))
g.add(Dropout(0.5))
g.add(Conv1D(32,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
g.add(Conv1D(16,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
g.add(Conv1D(4,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
g.add(Softmax(axis=2))
g.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
g.summary()
d = Sequential()
d.add(Conv1D(128,4,padding='same', input_shape=(30000, 4), activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
d.add(MaxPooling1D(2))
d.add(Conv1D(64,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
d.add(BatchNormalization())
d.add(MaxPooling1D(2))
d.add(Conv1D(32,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
d.add(MaxPooling1D(2))
d.add(Conv1D(32,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
d.add(MaxPooling1D(2))
d.add(Conv1D(16,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
d.add(MaxPooling1D(2))
d.add(Conv1D(8,4,padding='same',activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform()))
d.add(Flatten())
d.add(Dropout(0.5))
d.add(Dense(112, activation=LeakyReLU(alpha=0.2)))
d.add(Dense(1, activation='sigmoid'))
d.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
d.summary()
d.trainable = False
inputs = Input(shape=(z_dim, ))
hidden = g(inputs)
output = d(hidden)
gan = Model(inputs, output)
gan.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
gan.summary()
def plot_loss(losses):
"""
@losses.keys():
0: loss
1: accuracy
"""
d_loss = [v[0] for v in losses["D"]]
g_loss = [v[0] for v in losses["G"]]
plt.figure(figsize=(10,8))
plt.plot(d_loss, label="Discriminator loss")
plt.plot(g_loss, label="Generator loss")
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
plt.savefig('DisGenLoss.png')
plt.figure(figsize=(10,8))
plt.plot(d_loss, label="Discriminator loss")
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.savefig('DisLoss.png')
plt.figure(figsize=(10,8))
plt.plot(g_loss, label="Generator loss")
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.savefig('GenLoss.png')
# def plot_generated(n_ex=10, dim=(1, 10), figsize=(12, 2)):
# noise = np.random.normal(0, 1, size=(n_ex, z_dim))
# generated_seq = g.predict(noise)
# generated_seq = generated_seq.reshape(generated_seq.shape[0], 3000, 4)
# plt.figure(figsize=figsize)
# for i in range(generated_seq.shape[0]):
# plt.subplot(dim[0], dim[1], i+1)
# plt.imshow(generated_seq[i, :, :], interpolation='nearest', cmap='gray_r')
# plt.axis('off')
# plt.tight_layout()
# plt.show()
# Set up a vector (dict) to store the losses
losses = {"D":[], "G":[]}
samples = []
def train(epochs=100, plt_frq=1, BATCH_SIZE=10):
batchCount = int(x_train.shape[0] / BATCH_SIZE)
print('Epochs:', epochs)
print('Batch size:', BATCH_SIZE)
print('Batches per epoch:', batchCount)
for e in range(1, epochs+2):
if e == 1 or e%plt_frq == 0:
print('-'*15, 'Epoch %d' % e, '-'*15)
for _ in tqdm(range(batchCount)): # tqdm_notebook(range(batchCount), leave=False):
# Create a batch by drawing random index numbers from the training set
Orignal_seq = x_train[np.random.randint(0, x_train.shape[0], size=BATCH_SIZE)]
Orignal_seq = Orignal_seq.reshape(Orignal_seq.shape[0], Orignal_seq.shape[1], Orignal_seq.shape[2])
# Create noise vectors for the generator
noise = np.random.normal(0, 1, size=(BATCH_SIZE, z_dim))
# Generate the images from the noise
generated_seq = g.predict(noise)
# generated_seq = generated_seq[np.where(generated_seq==np.max(generated_seq, axis=2))] = 1
for i in range(generated_seq.shape[0]):
gg = generated_seq[i,:,:]
b = np.zeros_like(gg)
b[np.arange(len(gg)), gg.argmax(1)] = 1
generated_seq[i,:,:] = b
# print(generated_seq.shape, generated_seq[0,0,:])
samples.append(generated_seq)
saveGeneratedSeq(generated_seq[0,:,:])
X = np.concatenate((Orignal_seq, generated_seq))
# Create labels
y = np.zeros(2*BATCH_SIZE)
y[:BATCH_SIZE] = 1 # One-sided label smoothing
# Train discriminator on generated images
d.trainable = True
d_loss = d.train_on_batch(X, y)
# Train generator
noise = np.random.normal(0, 1, size=(BATCH_SIZE, z_dim))
y2 = np.ones(BATCH_SIZE)
d.trainable = False
g_loss = gan.train_on_batch(noise, y2)
# Only store losses from final batch of epoch
print("Epoch", e, "Generator Loss", g_loss)
print("Epoch", e, "Discrminator loss", d_loss)
losses["D"].append(d_loss)
losses["G"].append(g_loss)
# Update the plots
# if e == 1 or e%plt_frq == 0:
# plot_generated()
plot_loss(losses)
train()