-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmelanoma.py
More file actions
180 lines (135 loc) · 3.88 KB
/
Copy pathmelanoma.py
File metadata and controls
180 lines (135 loc) · 3.88 KB
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
# -*- coding: utf-8 -*-
"""Melanoma.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1Wz_engWh9tmPTw05AItL-BUtZD9-2wRc
"""
import numpy as np
import pandas as pd
import seaborn as sb
import matplotlib.pyplot as plt
from glob import glob
from PIL import Image
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow import keras
from keras import layers
from functools import partial
AUTO = tf.data.experimental.AUTOTUNE
import warnings
warnings.filterwarnings('ignore')
"""# Load & Plot the Data"""
images = glob('drive/MyDrive/ML/train_cancer/*/*.jpg')
len(images)
#replace backslash with forward slash to avoid unexpected errors
images = [path.replace('\\', '/') for path in images]
df = pd.DataFrame({'filepath': images})
df['label'] = df['filepath'].str.split('/', expand=True)[4]
df.head()
df['label_bin'] = np.where(df['label'].values == 'malignant', 1, 0)
df.head()
x = df['label'].value_counts()
plt.pie(x.values,
labels=x.index,
autopct='%1.1f%%')
plt.show()
for cat in df['label'].unique():
temp = df[df['label'] == cat]
index_list = temp.index
fig, ax = plt.subplots(1, 4, figsize=(15, 5))
fig.suptitle(f'Images for {cat} category . . . .', fontsize=20)
for i in range(4):
index = np.random.randint(0, len(index_list))
index = index_list[index]
data = df.iloc[index]
image_path = data[0]
img = np.array(Image.open(image_path))
ax[i].imshow(img)
plt.tight_layout()
plt.show()
"""# Prepare Dataset"""
features = df['filepath']
target = df['label_bin']
X_train, X_val,\
Y_train, Y_val = train_test_split(features, target,
test_size=0.15,
random_state=10)
X_train.shape, X_val.shape
def decode_image(filepath, label):
img = tf.io.read_file(filepath)
img = tf.image.decode_jpeg(img)
img = tf.image.resize(img, [224, 224])
img = tf.cast(img, tf.float32) / 255.0
return img, label
train_ds = (
tf.data.Dataset
.from_tensor_slices((X_train, Y_train))
.map(decode_image, num_parallel_calls=AUTO)
.batch(32)
.prefetch(AUTO)
)
val_ds = (
tf.data.Dataset
.from_tensor_slices((X_val, Y_val))
.map(decode_image, num_parallel_calls=AUTO)
.batch(32)
.prefetch(AUTO)
)
"""# Model
### Pretrained Model - TF
"""
from tensorflow.keras.applications.efficientnet import EfficientNetB7
pre_trained_model = EfficientNetB7(
input_shape=(224, 224, 3),
weights='imagenet',
include_top=False
)
for layer in pre_trained_model.layers:
layer.trainable = False
"""### Model - Fully connected (dense) neural network"""
from tensorflow.keras import Model
inputs = layers.Input(shape=(224, 224, 3))
x = layers.Flatten()(inputs)
x = layers.Dense(256, activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.Dense(256, activation='relu')(x)
x = layers.Dropout(0.3)(x)
x = layers.BatchNormalization()(x)
outputs = layers.Dense(1, activation='sigmoid')(x)
model = Model(inputs, outputs)
model.compile(
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
optimizer='adam',
metrics=['AUC']
)
history = model.fit(train_ds,
validation_data=val_ds,
epochs=5,
verbose=1)
model.save('my_model.h5')
"""# Results"""
hist_df = pd.DataFrame(history.history)
hist_df.head()
hist_df['loss'].plot()
hist_df['val_loss'].plot()
plt.title('Loss v/s Validation Loss')
plt.legend()
plt.show()
hist_df['auc'].plot()
hist_df['val_auc'].plot()
plt.title('AUC v/s Validation AUC')
plt.legend()
plt.show()
"""# Model Test with raw JPG"""
def decode_image_wo_label(filepath):
img = tf.io.read_file(filepath)
img = tf.image.decode_jpeg(img, channels=3)
img = tf.image.resize(img, [224,224])
img = tf.cast(img, tf.float32) / 255.0
return img
imageVm = 'drive/MyDrive/ML/train_cancer/malignant/10.jpg'
decoded = decode_image_wo_label(imageVm)
decoded.shape
decoded = tf.expand_dims(decoded, axis=0)
predictions = model.predict(decoded)
print(predictions)