-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
130 lines (108 loc) · 3.15 KB
/
train.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
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import tensorflow as tf
import math
import tensorflow_hub as hub
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers
from sklearn.metrics import roc_curve
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_examples = 20225
test_examples = 2551
validation_examples = 2555
img_height = img_width = 224
batch_size = 32
# NasNet
model = keras.Sequential([
hub.KerasLayer("https://tfhub.dev/google/imagenet/inception_v1/classification/5",
trainable=True),
layers.Dense(1, activation="sigmoid"),
])
# model = keras.models.load_model("isic_model/")
train_datagen = ImageDataGenerator(
rescale=1.0 / 255,
rotation_range=15,
zoom_range=(0.95, 0.95),
horizontal_flip=True,
vertical_flip=True,
data_format="channels_last",
dtype=tf.float32,
)
validation_datagen = ImageDataGenerator(rescale=1.0 / 255, dtype=tf.float32)
test_datagen = ImageDataGenerator(rescale=1.0 / 255, dtype=tf.float32)
train_gen = train_datagen.flow_from_directory(
"data/train/",
target_size=(img_height, img_width),
batch_size=batch_size,
color_mode="rgb",
class_mode="binary",
shuffle=True,
seed=123,
)
validation_gen = validation_datagen.flow_from_directory(
"data/validation/",
target_size=(img_height, img_width),
batch_size=batch_size,
color_mode="rgb",
class_mode="binary",
shuffle=True,
seed=123,
)
test_gen = test_datagen.flow_from_directory(
"data/test/",
target_size=(img_height, img_width),
batch_size=batch_size,
color_mode="rgb",
class_mode="binary",
shuffle=True,
seed=123,
)
METRICS = [
keras.metrics.BinaryAccuracy(name="accuracy"),
keras.metrics.Precision(name="precision"),
keras.metrics.Recall(name="recall"),
keras.metrics.AUC(name="auc"),
]
model.compile(
optimizer=keras.optimizers.Adam(lr=3e-4),
loss=[keras.losses.BinaryCrossentropy(from_logits=False)],
metrics=METRICS,
)
model.fit(
train_gen,
epochs=10,
verbose=2,
steps_per_epoch=train_examples // batch_size,
validation_data=validation_gen,
validation_steps=validation_examples // batch_size,
callbacks=[keras.callbacks.ModelCheckpoint("isic_model")],
)
def plot_roc(labels, data):
predictions = model.predict(data)
fp, tp, _ = roc_curve(labels, predictions)
plt.plot(100 * fp, 100 * tp)
plt.xlabel("False positives [%]")
plt.ylabel("True positives [%]")
plt.show()
plt.savefig('plot_roc.jpg')
def plot_acc(model):
plt.plot(model.history['accuracy'], label='accuracy')
plt.plot(model.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.5, 1])
plt.legend(loc='lower right')
plt.show()
plt.savefig('accuracy.jpg')
test_labels = np.array([])
num_batches = 0
for _, y in test_gen:
test_labels = np.append(test_labels, y)
num_batches += 1
if num_batches == math.ceil(test_examples / batch_size):
break
plot_roc(test_labels, test_gen)
model.evaluate(validation_gen, verbose=2)
model.evaluate(test_gen, verbose=2)