-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXRayClassification.py
More file actions
154 lines (121 loc) · 4.72 KB
/
Copy pathXRayClassification.py
File metadata and controls
154 lines (121 loc) · 4.72 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
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import mobilenet_v2
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from PIL import Image, UnidentifiedImageError
def create_dataframe(dataset_dir):
image_paths = []
labels = []
bad_files = []
for body_part in os.listdir(dataset_dir):
body_part_path = os.path.join(dataset_dir, body_part)
if os.path.isdir(body_part_path):
for patient in os.listdir(body_part_path):
patient_path = os.path.join(body_part_path, patient)
if os.path.isdir(patient_path):
for study in os.listdir(patient_path):
study_path = os.path.join(patient_path, study)
if os.path.isdir(study_path):
label = 'Abnormal' if 'positive' in study else 'Normal'
for image_file in os.listdir(study_path):
if image_file.startswith('.') or image_file.startswith('._'):
continue
if image_file.lower().endswith('.png'):
p = os.path.join(study_path, image_file)
try:
with Image.open(p) as im:
im.verify()
image_paths.append(p)
labels.append(label)
except (UnidentifiedImageError, OSError):
bad_files.append(p)
if bad_files:
print(f"Skipped {len(bad_files)} unreadable images. Example: {bad_files[:5]}")
return pd.DataFrame({'image_path': image_paths, 'label': labels})
train_dir='I:\\XRay\\MURA-v1.1\\train'
valid_dir='I:\\XRay\\MURA-v1.1\\valid'
train_df = create_dataframe(train_dir)
valid_df = create_dataframe(valid_dir)
print(f'Training samples: {len(train_df)}')
print(f'Validation samples: {len(valid_df)}')
print(train_df.head())
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=20,
zoom_range=0.2,
horizontal_flip=True,
)
val_datagen = ImageDataGenerator(rescale=1./255)
train_loader = train_datagen.flow_from_dataframe(
dataframe=train_df,
x_col='image_path',
y_col='label',
target_size=(224, 224),
batch_size=32,
class_mode='binary',
shuffle=True,
)
valid_loader = val_datagen.flow_from_dataframe(
dataframe=valid_df,
x_col='image_path',
y_col='label',
target_size=(224, 224),
batch_size=32,
class_mode='binary',
shuffle=False,
)
image, label = next(train_loader)
plt.figure(figsize=(12, 12))
for i in range(9):
plt.subplot(3, 3, i + 1)
plt.imshow(image[i])
plt.title(f"Label: {int(label[i])}")
plt.axis('off')
plt.show()
base_model = mobilenet_v2.MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
base_model.trainable = False
x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(128, activation='relu')(x)
x=Dropout(0.2)(x)
predictions=Dense(1, activation='sigmoid')(x)
model=Model(inputs=base_model.input, outputs=predictions)
model.compile(optimizer=Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy'])
model.summary()
history=model.fit(
train_loader,
epochs=5,
validation_data=valid_loader
)
model.save("models/xray_mobilenetv2.h5")
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Accuracy over Epochs')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Loss over Epochs')
plt.legend()
plt.savefig('training_history.png')
plt.show()
test_images, test_labels = next(valid_loader)
predictions = model.predict(test_images)
plt.figure(figsize=(12, 6))
for i in range(5):
plt.subplot(1, 5, i + 1)
plt.imshow(test_images[i])
pred_label = 'Abnormal' if predictions[i] > 0.5 else 'Normal'
true_label = 'Abnormal' if test_labels[i] == 1 else 'Normal'
plt.title(f"Pred: {pred_label}\nTrue: {true_label}")
plt.axis('off')
plt.savefig('sample_predictions.png')
plt.show()