-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contours.py
executable file
·92 lines (74 loc) · 2.79 KB
/
Contours.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
#!/bin/env python
from keras import layers
from keras import models
from keras.layers.advanced_activations import LeakyReLU
from keras.callbacks import EarlyStopping
import keras
act = LeakyReLU(alpha=0.6)
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(200, 200, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
#model.add(layers.Dropout(.35))
#model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
#model.add(layers.Dropout(.35))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
#model.add(layers.Dropout(.35))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(1024))#,activation='relu'))
#model.add(layers.Dropout(.35))
model.add(act) #, activation='relu'))
model.add(layers.Dense(256, activation='relu'))
#model.add(layers.Dropout(.35))
model.add(layers.Dense(1, activation='sigmoid'))
model.summary()
from keras import optimizers
model.compile(loss='binary_crossentropy',
optimizer=keras.optimizers.Adadelta(), #optimizers.RMSprop(lr=1e-4),
metrics=['accuracy'])
train_dir = 'data/train'
validation_dir = 'data/test'
from keras.preprocessing.image import ImageDataGenerator
# All images will be rescaled by 1./255
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
# This is the target directory
train_dir,
# All images will be resized to 156, 126. Size set manually,
#depends on data.
target_size=(175, 175),
color_mode="grayscale",
batch_size=40,
# Since we use binary_crossentropy loss, we need binary labels
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_dir,
target_size=(175, 175),
color_mode="grayscale",
batch_size=20,
class_mode='binary')
for data_batch, labels_batch in train_generator:
print('data batch shape:', data_batch.shape)
print('labels batch shape:', labels_batch.shape)
break
stop_early = EarlyStopping(monitor="val_loss",
min_delta=0,
patience=10,
verbose=0,
mode="auto")
#baseline=None)
history = model.fit_generator(
train_generator,
steps_per_epoch=188,
epochs=20,
validation_data=validation_generator,
validation_steps=32.6,
callbacks=[stop_early])
model.save("contour-opt-7.h5")