-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRNN.py
171 lines (143 loc) · 6.27 KB
/
CRNN.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
# -*- coding: utf-8 -*-
"""
Created on Thu April 13 16:25:19 2017
@author: lingyu
"""
import numpy as np
import csv
import sys
import os
import h5py
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Reshape
from keras.layers import LSTM
from keras.layers.convolutional import Convolution2D
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
seed =7
np.random.seed(seed)
NB_EPOCHS=500
BATCH_SIZE=200
VERBOSE=0
Nb_fm=32# number of feature maps
k=3 # size of kernel
m=3 # stride
DROP_OUT=0.5
num_classes=1
def main():
def CRNN_model(Nb_fm,k,m,DROP_OUT,num_classes):
# create model
model=Sequential()
model.add(Convolution2D(Nb_fm,k,m,input_shape=(1,100,5),border_mode='same', activation='relu'))
model.add(Dropout(DROP_OUT))
model.add(Convolution2D(Nb_fm,k,m,border_mode='same', activation='relu'))
model.add(Dropout(DROP_OUT))
model.add(Reshape((100,5*Nb_fm)))
model.add(LSTM(32,input_shape=(100,5*Nb_fm),dropout_W=DROP_OUT,dropout_U=DROP_OUT))
model.add(Dropout(DROP_OUT))
model.add(Dense(100,init='uniform',activation='relu'))
model.add(Dense(20,init='uniform',activation='relu'))
model.add(Dense(num_classes,init='uniform',activation='sigmoid'))
# compile model
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
return model
# setting path
first_path='.../Data'
base_filename_3_1='_rc_x'
base_filename_3_2='_rc_y'
base_filename_4_1='_train'
base_filename_4_2='_test'
filename_suffix_out='csv'
# load cell types
print('load cell types...')
cellType=np.loadtxt('.../cellType.txt',delimiter='\t',dtype=str)
nb_split=len(cellType) # the number of classes
auc=[]
for i in range(0,nb_split):
dir_name=cellType[i]
# get the path of data file
save_path_x_train=os.path.join(first_path, dir_name + base_filename_3_1 + base_filename_4_1 + '.' + filename_suffix_out)
save_path_x_test=os.path.join(first_path, dir_name + base_filename_3_1 + base_filename_4_2 + '.' + filename_suffix_out)
save_path_y_train=os.path.join(first_path, dir_name + base_filename_3_2 + base_filename_4_1 + '.' + filename_suffix_out)
save_path_y_test=os.path.join(first_path, dir_name + base_filename_3_2 + base_filename_4_2 + '.' + filename_suffix_out)
# load data
print("Loading data...",dir_name)
X_train = np.loadtxt(save_path_x_train,
delimiter = ",", skiprows = 1)
X_test = np.loadtxt(save_path_x_test,
delimiter = ",", skiprows = 1)
y_train = np.loadtxt(save_path_y_train,
delimiter = ",", skiprows = 1)
y_test = np.loadtxt(save_path_y_test,
delimiter = ",", skiprows = 1)
print "All files loaded. Preprocessing..."
# remove the first column(Gene Id)
X_train = X_train[:,1:]
X_test = X_test[:,1:]
y_train = y_train[:,1:]
y_test = y_test[:,1:]
# Every 100 rows correspond to one gene.
# Extract all 100-row-blocks into a list using np.split.
num_genes_train = X_train.shape[0] / 100
num_genes_test = X_test.shape[0] / 100
print("Train / test data has %d / %d genes." % \
(num_genes_train, num_genes_test))
X_train = np.split(X_train, num_genes_train)
X_test = np.split(X_test, num_genes_test)
# convert data from list to array
X_train = np.array(X_train)
y_train = np.array(y_train)
X_test = np.array(X_test)
y_train = np.ravel(y_train)
y_test = np.array(y_test)
# reshape--(samples,channels,rows,cols)
X_train = np.reshape(X_train,(X_train.shape[0],1,X_train.shape[1],X_train.shape[2]))
X_test = np.reshape(X_test,(X_test.shape[0],1,X_test.shape[1],X_test.shape[2]))
# convert data from list to array
X_train = np.array(X_train)
y_train = np.array(y_train)
X_test = np.array(X_test)
y_train = np.ravel(y_train)
y_test = np.array(y_test)
y_train = y_train[:].astype('int')
y_test = y_test[:].astype('int')
print("X_train shape is %s" % str(X_train.shape))
print("y_train shape is %s" % str(y_train.shape))
print("X_test shape is %s" % str(X_test.shape))
print('Data preprocessing done...')
#split train set into train-val
x_tr, x_va, y_tr, y_va = train_test_split(X_train, y_train, test_size=0.2, random_state=0)
print('training CRNN model..\n')
model=CRNN_model(Nb_fm,k,m,DROP_OUT,num_classes)
# Create loss and accuracy variables for training and validation process of all cell types
hist_loss_tr=[]
hist_loss_va=[]
hist_acc_tr=[]
hist_acc_va=[]
# fit the model
for iteration in range(NB_EPOCHS):
hist=model.fit(x_tr, y_tr, validation_data=(x_va, y_va), nb_epoch=1, batch_size=BATCH_SIZE, verbose=VERBOSE)
val_error =hist.history['val_loss']
# if validation loss become nan, stop training process
if np.isnan(val_error):
break
hist_loss_tr.append(hist.history['loss'])
hist_loss_va.append(hist.history['val_loss'])
hist_acc_tr.append(hist.history['acc'])
hist_acc_va.append(hist.history['val_acc'])
# summarize performance of the model
scores = model.evaluate(x_va,y_va, verbose=VERBOSE)
print("Model Accuracy: %.2f%%" % (scores[1]*100))
# save model
model.save('my_model.h5')
# prediction
y_pred = model.predict_classes(X_test, verbose=VERBOSE)
y_score = model.predict_proba(X_test, verbose=VERBOSE)
# result saving
score = accuracy_score(y_test,y_pred)
auc.append(roc_auc_score(y_test,y_score))
np.savez(os.path.join(dir_name + '.npz'), AUC=auc,hist_loss_tr=hist_loss_tr,hist_loss_va=hist_loss_va,hist_acc_tr=hist_acc_tr,hist_acc_va=hist_acc_va,y_te=y_test,y_pre=y_pred,y_sco=y_score)
np.savez(os.path.join('AUC' + '.npz'), AUC=auc)
if __name__== '__main__':
main()