-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtrain_drive.py
128 lines (83 loc) · 4.16 KB
/
train_drive.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
import numpy as np
from keras.callbacks import TensorBoard, ModelCheckpoint
np.random.seed(42)
import scipy.misc as mc
data_location = ''
import os
from util import *
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
training_images_loc = data_location + 'Drive/train/images/'
training_label_loc = data_location + 'Drive/train/labels/'
validate_images_loc = data_location + 'Drive/validate/images/'
validate_label_loc = data_location + 'Drive/validate/labels/'
train_files = os.listdir(training_images_loc)
train_data = []
train_label = []
validate_files = os.listdir(validate_images_loc)
validate_data = []
validate_label = []
desired_size = 592
for i in train_files:
im = mc.imread(training_images_loc + i)
label = mc.imread(training_label_loc + i.split('_')[0] + '_manual1.png',mode="L")
old_size = im.shape[:2] # old_size is in (height, width) format
delta_w = desired_size - old_size[1]
delta_h = desired_size - old_size[0]
top, bottom = delta_h // 2, delta_h - (delta_h // 2)
left, right = delta_w // 2, delta_w - (delta_w // 2)
color = [0, 0, 0]
color2 = [0]
new_im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT,
value=color)
new_label = cv2.copyMakeBorder(label, top, bottom, left, right, cv2.BORDER_CONSTANT,
value=color2)
train_data.append(cv2.resize(new_im, (desired_size, desired_size)))
temp = cv2.resize(new_label, (desired_size, desired_size))
_, temp = cv2.threshold(temp, 127, 255, cv2.THRESH_BINARY)
train_label.append(temp)
for i in validate_files:
im = mc.imread(validate_images_loc + i)
label = mc.imread(validate_label_loc + i.split('_')[0] + '_manual1.png',mode="L")
old_size = im.shape[:2] # old_size is in (height, width) format
delta_w = desired_size - old_size[1]
delta_h = desired_size - old_size[0]
top, bottom = delta_h // 2, delta_h - (delta_h // 2)
left, right = delta_w // 2, delta_w - (delta_w // 2)
color = [0, 0, 0]
color2 = [0]
new_im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT,
value=color)
new_label = cv2.copyMakeBorder(label, top, bottom, left, right, cv2.BORDER_CONSTANT,
value=color2)
validate_data.append(cv2.resize(new_im, (desired_size, desired_size)))
temp = cv2.resize(new_label, (desired_size, desired_size))
_, temp = cv2.threshold(temp, 127, 255, cv2.THRESH_BINARY)
validate_label.append(temp)
train_data = np.array(train_data)
train_label = np.array(train_label)
validate_data = np.array(validate_data)
validate_label = np.array(validate_label)
x_train = train_data.astype('float32') / 255.
y_train = train_label.astype('float32') / 255.
x_validate = validate_data.astype('float32') / 255.
y_validate = validate_label.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), desired_size, desired_size, 3)) # adapt this if using `channels_first` image data format
y_train = np.reshape(y_train, (len(y_train), desired_size, desired_size, 1)) # adapt this if using `channels_first` im
x_validate = np.reshape(x_validate, (len(x_validate), desired_size, desired_size, 3)) # adapt this if using `channels_first` image data format
y_validate = np.reshape(y_validate, (len(y_validate), desired_size, desired_size, 1)) # adapt this if using `channels_first` im
TensorBoard(log_dir='./autoencoder', histogram_freq=0,
write_graph=True, write_images=True)
from RSAN import *
model=RSANet(input_size=(desired_size,desired_size,3),start_neurons=16,keep_prob=0.85,lr=1e-3)
weight="Drive/Model/RSAN.h5"
restore=False
if restore and os.path.isfile(weight):
model.load_weights(weight)
model_checkpoint = ModelCheckpoint(weight, monitor='val_accuracy', verbose=1, save_best_only=False)
history=model.fit(x_train, y_train,
epochs=100, #first 100 with lr=1e-3,,and last 50 with lr=1e-4
batch_size=2,
# validation_split=0.05,
validation_data=(x_validate, y_validate),
shuffle=True,
callbacks= [TensorBoard(log_dir='./autoencoder'), model_checkpoint])