-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInceptionResNetV2 (proxy for InceptionV4)
95 lines (73 loc) · 3.26 KB
/
InceptionResNetV2 (proxy for InceptionV4)
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
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Dropout, GlobalAveragePooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import InceptionResNetV2
from sklearn.svm import SVC
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
import matplotlib.pyplot as plt
training_data_generator = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
validation_data_generator = ImageDataGenerator(rescale=1./255)
training_data_flow = training_data_generator.flow_from_directory(
'lung-data/Data/train/',
target_size=(224, 224),
batch_size=32,
class_mode='categorical'
)
validation_data_flow = validation_data_generator.flow_from_directory(
'lung-data/Data/valid/',
target_size=(224, 224),
batch_size=32,
class_mode='categorical'
)
inception_resnet_base = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
layer = inception_resnet_base.output
layer = GlobalAveragePooling2D()(layer)
layer = Dense(4096, activation='relu')(layer)
layer = Dropout(0.5)(layer)
layer = Dense(2048, activation='relu')(layer)
layer = Dropout(0.5)(layer)
output_layer = Dense(4, activation='softmax')(layer)
final_model = Model(inputs=inception_resnet_base.input, outputs=output_layer)
for inception_resnet_layer in inception_resnet_base.layers:
inception_resnet_layer.trainable = False
final_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
final_model.summary()
training_history = final_model.fit(
training_data_flow,
steps_per_epoch=training_data_flow.samples // training_data_flow.batch_size,
validation_data=validation_data_flow,
validation_steps=validation_data_flow.samples // validation_data_flow.batch_size,
epochs=25
)
validation_loss, validation_accuracy = final_model.evaluate(validation_data_flow)
print(f"Validation Accuracy: {validation_accuracy}")
training_features = final_model.predict(training_data_flow)
validation_features = final_model.predict(validation_data_flow)
svm_classifier_pipeline = make_pipeline(StandardScaler(), SVC(kernel='linear', decision_function_shape='ovr'))
svm_classifier_pipeline.fit(training_features, training_data_flow.classes)
validation_predictions = svm_classifier_pipeline.predict(validation_features)
print('Confusion Matrix')
print(confusion_matrix(validation_data_flow.classes, validation_predictions))
print('Classification Report')
target_labels = ['Adenocarcinoma', 'Large Cell Carcinoma', 'Squamous Cell Carcinoma', 'Normal']
print(classification_report(validation_data_flow.classes, validation_predictions, target_names=target_labels))
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(training_history.history['accuracy'], label='Train Accuracy')
plt.plot(training_history.history['val_accuracy'], label='Validation Accuracy')
plt.legend()
plt.title('Model Accuracy')
plt.subplot(1, 2, 2)
plt.plot(training_history.history['loss'], label='Train Loss')
plt.plot(training_history.history['val_loss'], label='Validation Loss')
plt.legend()
plt.title('Model Loss')
plt.show()