-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
155 lines (124 loc) · 3.7 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
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
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import roc_curve
from tensorflow.keras import layers
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
import tensorflow_hub as hub
import math
import tensorflow as tf
import os
import shutil
import random
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
train_examples = 20225
test_examples = 2551
validation_examples = 2555
img_height = img_width = 224
batch_size = 32
epochs = 10
'''
#Nasnet
model = keras.Sequential([
hub.KerasLayer("https://tfhub.dev/google/imagenet/nasnet_mobile/feature_vector/4",
trainable=True),
layers.Dense(1,activation="sigmoid") # sigmoid because we have only 2 classes
])
model.build([None,img_height,img_width,3])
print(model.summary())
'''
# EfficientNet
model_url = "https://tfhub.dev/tensorflow/efficientnet/b0/classification/1"
model = keras.Sequential([
hub.KerasLayer(model_url,trainable=True),
layers.Dense(1,activation="sigmoid") # sigmoid because we have only 2 classes
])
model.build([None, img_height, img_width, 3])
print(model.summary())
# model = keras.models.load_model("isic_model/") # use this line of code if you want to resume training
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,
)
# since our dataset is skewed we have to introduce a few metrics which are inbuilt in Keras to handle data
METRICS = [
keras.metrics.BinaryAccuracy(name="accuracy"),
keras.metrics.Precision(name="precision"),
keras.metrics.Recall(name="recall"),
keras.metrics.AUC(name="auc"),
]
# compile model
model.compile(
optimizer=keras.optimizers.Adam(lr=3e-4),
loss=[keras.losses.BinaryCrossentropy(from_logits=False)],
metrics=METRICS,
)
model.fit(
train_gen,
epochs=epochs,
verbose=1,
steps_per_epoch=train_examples // batch_size,
validation_data=validation_gen,
validation_steps=validation_examples // batch_size,
callbacks=[keras.callbacks.ModelCheckpoint("isic_model")],
)
# plotting a ROC curve (it predicts the true positive rate vs false positive rate)
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()
# the following lines of code are just to get all the labels from the test set to a single np array
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)
# evaluate the model
model.evaluate(validation_gen, verbose=1)
model.evaluate(test_gen, verbose=1)