-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainSegmentation.py
More file actions
156 lines (121 loc) · 4.01 KB
/
Copy pathtrainSegmentation.py
File metadata and controls
156 lines (121 loc) · 4.01 KB
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
# USAGE
# set the matplotlib backend so figures can be saved in the background
import matplotlib
import tensorflow as tf
matplotlib.use("Agg")
# import the necessary packages
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
from sklearn.model_selection import train_test_split
from keras.preprocessing.image import img_to_array
from keras.utils import to_categorical
from sklearn.model_selection import StratifiedKFold
from keras.callbacks import EarlyStopping, ModelCheckpoint
import keras.backend as K
from imutils import paths
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
#Condition to load npy with bug in keras
old = np.load
np.load = lambda *a,**k: old(*a, allow_pickle=True, **k)
from numpy import genfromtxt
import argparse
import random
import csv
import os
from scipy import signal, misc
import cv2
#Import the model
from unetLungSounds import unetLungNet
#Allow one gpu to
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID";
os.environ["CUDA_VISIBLE_DEVICES"]="1";
#This allows for gpu memory growth over all visipble gpus
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True,
help="path to input dataset")
ap.add_argument("-l", "--labels", required=True,
help="path to input label vectors")
ap.add_argument("-m", "--model", required=True,
help="path to output model")
args = vars(ap.parse_args())
# initialize the number of epochs to train for, initial learning rate,
# and batch size
EPOCHS = 500
INIT_LR = 1e-4
BS = 100
# initialize the data and labels
hilbert = []
labels = []
resampledHilberts=[]
resampledLabels = []
print(args["dataset"])
# Envolope Directory loading
print("[INFO] loading raw envolopes in...")
fileCount = 0
for filename in os.listdir(args["dataset"]):
if filename.endswith(".csv"):
print(fileCount)
fileCount += 1
#Importing the raw csv data
rawCSVHilbert = np.loadtxt(args["dataset"]+'/'+filename)
print(rawCSVHilbert.size)
if rawCSVHilbert.size == 8000:
hilbert.append(rawCSVHilbert)
data = np.array(hilbert)
np.save('dataTrain.npy', data)
# Label Directory loading
print("[INFO] loading raw labels in...")
fileCount = 0
for filename in os.listdir(args["labels"]):
if filename.endswith(".csv"):
print(fileCount)
fileCount += 1
#Importing the raw csv data
rawCSVLabels = np.loadtxt(args["labels"]+'/'+filename)
print(rawCSVLabels.size)
#this if statment gets the label to the right size
if rawCSVLabels.size == 8000:
labels.append(rawCSVLabels)
target = np.array(labels)
np.save('targetTrain.npy', target)
X = np.load('dataTrain.npy')
Y = np.load('targetTrain.npy')
print('X Shape:',X.shape)
print('Y Shape:',Y.shape)
#Reshaping the data
X = np.expand_dims(X, axis=2) # reshape (training_size, 88200) to (569, 30, 1)
print(X.shape)
Y = np.expand_dims(Y, axis=2) # reshape (training_size, 88200)
print(Y.shape)
# fix random seed for reproducibility
seed = 7
np.random.seed(seed)
# define 10-fold cross validation test harness
kfold = StratifiedKFold(n_splits=3, shuffle=True, random_state=seed)
kFoldCount = 1
cvscores = []
#intialize the model
print("COMPILING MODEL....")
#Fit the model
model =unetLungNet()
model.summary()
#model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
#Optomizer setting
opt = Adam(lr=INIT_LR, decay=INIT_LR/(EPOCHS))
#model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["acc"])
model.compile(loss="binary_crossentropy",
optimizer =opt, metrics=["accuracy"])
# Set callback functions to early stop traing and save the best model from training
callback = [EarlyStopping(monitor='val_loss', patience=2),
ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True)]
# Fitting the model
model.fit(X,Y,batch_size=BS,epochs=EPOCHS, verbose=1,
callbacks = None, validation_data=None)
# save the model to disk
print("[INFO] serializing network...")
model.save(args["model"]+str(kFoldCount))