-
Notifications
You must be signed in to change notification settings - Fork 1
/
first_model(unet).py
223 lines (171 loc) · 8.04 KB
/
first_model(unet).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
import os
import numpy as np
import json
from PIL import Image
import matplotlib
import pandas as pd
from matplotlib.collections import PatchCollection
import cv2 as cv
from skimage import io
from skimage.segmentation import quickshift
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization, Input, merge, UpSampling2D, Cropping2D, ZeroPadding2D, Reshape, core, Convolution2D
from tqdm import tqdm
import tensorflow as tf
from keras.models import Sequential, Model
from keras.callbacks import EarlyStopping, ModelCheckpoint, LearningRateScheduler
from keras import optimizers
from keras import backend as K
from keras.optimizers import SGD
from keras.layers.merge import concatenate
from sklearn.metrics import fbeta_score
import glob
from skimage.transform import rescale, resize, downscale_local_mean
from sklearn.model_selection import train_test_split
shape=(622,782)
print('sss')
# sat image
imgarr = io.imread('The-Eye-in-the-Sky-dataset/sat/13.tif')
#takes first 3 band of image(rgb)
a_rgb=(imgarr[...,0:3])
print (a_rgb.shape)
print(a_rgb.dtype)
#print (imgarr)
#print (imgarr.shape)
#print (imgarr.dtype)
def scale_percentile(matrix):
"""Fixes the pixel value range to 2%-98% original distribution of values"""
orig_shape = matrix.shape
matrix = np.reshape(matrix, [matrix.shape[0]*matrix.shape[1], 3]).astype(float)
# Get 2nd and 98th percentile
mins = np.percentile(matrix, 1, axis=0)
maxs = np.percentile(matrix, 99, axis=0) - mins
matrix = (matrix - mins[None,:])/maxs[None,:]
matrix = np.reshape(matrix, orig_shape)
matrix = matrix.clip(0,1)
return matrix
fixed_im = scale_percentile(a_rgb)
def mean_iou(y_true, y_pred):
prec = []
for t in np.arange(0.5, 1.0, 0.05):
y_pred_ = tf.to_int32(y_pred > t)
score, up_opt = tf.metrics.mean_iou(y_true, y_pred_, 2)
K.get_session().run(tf.local_variables_initializer())
with tf.control_dependencies([up_opt]):
score = tf.identity(score)
prec.append(score)
return K.mean(K.stack(prec), axis=0)
# Custom loss function
def dice_coef(y_true, y_pred):
smooth = 1.
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
def bce_dice_loss(y_true, y_pred):
return 0.5 * keras.losses.binary_crossentropy(y_true, y_pred) - dice_coef(y_true, y_pred)
def get_crop_shape(target, refer):
# width, the 3rd dimension
cw = (target.get_shape()[2] - refer.get_shape()[2]).value
assert (cw >= 0)
if cw % 2 != 0:
cw1, cw2 = int(cw/2), int(cw/2) + 1
else:
cw1, cw2 = int(cw/2), int(cw/2)
# height, the 2nd dimension
ch = (target.get_shape()[1] - refer.get_shape()[1]).value
assert (ch >= 0)
if ch % 2 != 0:
ch1, ch2 = int(ch/2), int(ch/2) + 1
else:
ch1, ch2 = int(ch/2), int(ch/2)
return (ch1, ch2), (cw1, cw2)
def get_unet(n_ch,patch_height,patch_width):
concat_axis = 3
inputs = Input((patch_height, patch_width, n_ch))
conv1 = Conv2D(32, (3, 3), padding="same", name="conv1_1", activation="relu", data_format="channels_last")(inputs)
conv1 = Conv2D(32, (3, 3), padding="same", activation="relu", data_format="channels_last")(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2), data_format="channels_last")(conv1)
conv2 = Conv2D(64, (3, 3), padding="same", activation="relu", data_format="channels_last")(pool1)
conv2 = Conv2D(64, (3, 3), padding="same", activation="relu", data_format="channels_last")(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2), data_format="channels_last")(conv2)
conv3 = Conv2D(128, (3, 3), padding="same", activation="relu", data_format="channels_last")(pool2)
conv3 = Conv2D(128, (3, 3), padding="same", activation="relu", data_format="channels_last")(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2), data_format="channels_last")(conv3)
conv4 = Conv2D(256, (3, 3), padding="same", activation="relu", data_format="channels_last")(pool3)
conv4 = Conv2D(256, (3, 3), padding="same", activation="relu", data_format="channels_last")(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2), data_format="channels_last")(conv4)
conv5 = Conv2D(512, (3, 3), padding="same", activation="relu", data_format="channels_last")(pool4)
conv5 = Conv2D(512, (3, 3), padding="same", activation="relu", data_format="channels_last")(conv5)
up_conv5 = UpSampling2D(size=(2, 2), data_format="channels_last")(conv5)
ch, cw = get_crop_shape(conv4, up_conv5)
crop_conv4 = Cropping2D(cropping=(ch,cw), data_format="channels_last")(conv4)
up6 = concatenate([up_conv5, crop_conv4], axis=concat_axis)
conv6 = Conv2D(256, (3, 3), padding="same", activation="relu", data_format="channels_last")(up6)
conv6 = Conv2D(256, (3, 3), padding="same", activation="relu", data_format="channels_last")(conv6)
up_conv6 = UpSampling2D(size=(2, 2), data_format="channels_last")(conv6)
ch, cw = get_crop_shape(conv3, up_conv6)
crop_conv3 = Cropping2D(cropping=(ch,cw), data_format="channels_last")(conv3)
up7 = concatenate([up_conv6, crop_conv3], axis=concat_axis)
conv7 = Conv2D(128, (3, 3), padding="same", activation="relu", data_format="channels_last")(up7)
conv7 = Conv2D(128, (3, 3), padding="same", activation="relu", data_format="channels_last")(conv7)
up_conv7 = UpSampling2D(size=(2, 2), data_format="channels_last")(conv7)
ch, cw = get_crop_shape(conv2, up_conv7)
crop_conv2 = Cropping2D(cropping=(ch,cw), data_format="channels_last")(conv2)
up8 = concatenate([up_conv7, crop_conv2], axis=concat_axis)
conv8 = Conv2D(64, (3, 3), padding="same", activation="relu", data_format="channels_last")(up8)
conv8 = Conv2D(64, (3, 3), padding="same", activation="relu", data_format="channels_last")(conv8)
up_conv8 = UpSampling2D(size=(2, 2), data_format="channels_last")(conv8)
ch, cw = get_crop_shape(conv1, up_conv8)
crop_conv1 = Cropping2D(cropping=(ch,cw), data_format="channels_last")(conv1)
up9 = concatenate([up_conv8, crop_conv1], axis=concat_axis)
conv9 = Conv2D(32, (3, 3), padding="same", activation="relu", data_format="channels_last")(up9)
conv9 = Conv2D(32, (3, 3), padding="same", activation="relu", data_format="channels_last")(conv9)
#ch, cw = get_crop_shape(inputs, conv9)
#conv9 = ZeroPadding2D(padding=(ch[0],cw[0]), data_format="channels_last")(conv9)
#conv10 = Conv2D(1, (1, 1), data_format="channels_last", activation="sigmoid")(conv9)
flatten = Flatten()(conv9)
Dense1 = Dense(512, activation='relu')(flatten)
BN =BatchNormalization() (Dense1)
Dense2 = Dense(17, activation='sigmoid')(BN)
model = Model(input=inputs, output=Dense2)
return model
model = get_unet(3, 622, 782)
model.compile(optimizer=tf.train.AdamOptimizer(),
loss=bce_dice_loss,
metrics=['accuracy'])
#making train data
mypath = '/The-Eye-in-the-Sky-dataset/gt'
onlyfiles = [f for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath, f))]
k = 0
images = []
for n in range(0, len(onlyfiles)):
im = io.imread(os.path.join(mypath, onlyfiles[n]))
im=resize(im, shape)
images.append(im)
print(im.shape)
#df2 = pd.DataFrame(im.reshape(-1))
#df = pd.concat([df, df2], axis=1, ignore_index=True)
k = k + 1
print(k)
#print(df)
images_gt = np.asarray(images)
print(images_gt.shape)
mypath = '/The-Eye-in-the-Sky-dataset/sat'
onlyfiles = [f for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath, f))]
k = 0
images_sat = []
for n in range(0, len(onlyfiles)):
im = io.imread(os.path.join(mypath, onlyfiles[n]))
im_rgb=im[...,0:3]
fixed_im = scale_percentile(im_rgb)
im=fixed_im
im=resize(im, shape)
images_sat.append(im)
print(im.shape)
#df2 = pd.DataFrame(im.reshape(-1))
#df = pd.concat([df, df2], axis=1, ignore_index=True)
k = k + 1
print(k)
#print(df)
images_sat = np.asarray(images_sat)
print(images_sat.shape)