forked from neokarn/computer_vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example9_3_segmentation_iou
85 lines (70 loc) · 3.18 KB
/
example9_3_segmentation_iou
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
from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, UpSampling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint
from keras import backend as K
import matplotlib.pyplot as plt
BATCH_SIZE = 5
MAX_EPOCH = 20
IMAGE_SIZE = (256,256)
TRAIN_IM = 160
VALIDATE_IM = 15
model = Sequential()
model.add(Conv2D(16, 3, activation='relu', padding='same', kernel_initializer='he_normal',
input_shape=(IMAGE_SIZE[0],IMAGE_SIZE[1],3)))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Conv2D(32, 3, activation='relu', padding='same', kernel_initializer='he_normal'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal'))
model.add(Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal'))
model.add(UpSampling2D(size=(2, 2)))
model.add(Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal'))
model.add(UpSampling2D(size=(2, 2)))
model.add(Conv2D(32, 3, activation='relu', padding='same', kernel_initializer='he_normal'))
model.add(UpSampling2D(size=(2, 2)))
model.add(Conv2D(16, 3, activation='relu', padding='same', kernel_initializer='he_normal'))
model.add(Conv2D(1, 3, activation='sigmoid', padding='same', kernel_initializer='he_normal'))
print(model.summary())
def iou(y_true, y_pred):
y_true = K.cast(K.greater(y_true, 0.5), dtype='float32')
y_pred = K.cast(K.greater(y_pred, 0.5), dtype='float32')
inter = K.sum(K.sum(K.squeeze(y_true * y_pred, axis=3), axis=2), axis=1)
union = K.sum(K.sum(K.squeeze(K.clip(y_true + y_pred, 0, 1), axis=3), axis=2), axis=1)
return K.mean((inter + K.epsilon()) / (union + K.epsilon()))
model.compile(optimizer= 'adam', loss='binary_crossentropy', metrics=['accuracy',iou])
def myGenerator(type):
datagen = ImageDataGenerator(rescale=1./255)
input_generator = datagen.flow_from_directory(
'textlocalize/'+type,
classes = ['Input'],
class_mode=None,
color_mode='rgb',
target_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
shuffle=True,
seed = 1)
expected_output_generator = datagen.flow_from_directory(
'textlocalize/'+type,
classes = ['Output'],
class_mode=None,
color_mode='grayscale',
target_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
shuffle=True,
seed = 1)
while True:
in_batch = input_generator.next()
out_batch = expected_output_generator.next()
yield in_batch, out_batch
checkpoint = ModelCheckpoint('my_model.h5', verbose=1, monitor='val_iou',save_best_only=True, mode='max')
h = model.fit_generator(myGenerator('train'),
steps_per_epoch=TRAIN_IM/BATCH_SIZE,
epochs=MAX_EPOCH,
validation_data=myGenerator('validation'),
validation_steps=VALIDATE_IM/BATCH_SIZE,
callbacks=[checkpoint])
plt.plot(h.history['iou'])
plt.plot(h.history['val_iou'])
plt.show()