Diff of /Lung Cancer Prediction.py [000000] .. [8eeb41]

Switch to unified view

a b/Lung Cancer Prediction.py
1
# Mount Google Drive to access dataset files
2
from google.colab import drive
3
drive.mount('/content/drive', force_remount=True)
4
5
# Define paths to the training, validation, and test datasets
6
train_folder = '/content/drive/MyDrive/dataset/train'
7
test_folder = '/content/drive/MyDrive/dataset/test'
8
validate_folder = '/content/drive/MyDrive/datasetvalid'
9
10
# Define paths to the specific classes within the dataset
11
normal_folder = '/normal'
12
adenocarcinoma_folder = '/adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib'
13
large_cell_carcinoma_folder = '/large.cell.carcinoma_left.hilum_T2_N2_M0_IIIa'
14
squamous_cell_carcinoma_folder = '/squamous.cell.carcinoma_left.hilum_T1_N2_M0_IIIa'
15
16
# Import necessary libraries
17
import warnings
18
warnings.filterwarnings('ignore')
19
20
import pandas as pd
21
import numpy as np
22
import seaborn as sns
23
import matplotlib.pyplot as plt
24
from sklearn.preprocessing import MinMaxScaler, StandardScaler
25
from sklearn import datasets
26
from sklearn.model_selection import train_test_split
27
from sklearn.neighbors import KNeighborsClassifier
28
from sklearn.svm import SVC
29
from sklearn.decomposition import PCA
30
from sklearn.preprocessing import LabelEncoder
31
32
import tensorflow as tf
33
import tensorflow.keras
34
from tensorflow.keras.preprocessing.image import ImageDataGenerator
35
from tensorflow.keras.models import Sequential
36
from tensorflow.keras.layers import Dense, Dropout, SpatialDropout2D, Activation, Lambda, Flatten, LSTM
37
from tensorflow.keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D
38
from tensorflow.keras.optimizers import Adam, RMSprop
39
from tensorflow.keras import utils
40
41
print("Libraries Imported")
42
43
# Set the image size for resizing
44
IMAGE_SIZE = (350, 350)
45
46
# Initialize the image data generators for training and testing
47
train_datagen = ImageDataGenerator(rescale=1./255, horizontal_flip=True)
48
test_datagen = ImageDataGenerator(rescale=1./255)
49
50
# Define the batch size for training
51
batch_size = 8
52
53
# Create the training data generator
54
train_generator = train_datagen.flow_from_directory(
55
    train_folder,
56
    target_size=IMAGE_SIZE,
57
    batch_size=batch_size,
58
    color_mode="rgb",
59
    class_mode='categorical'
60
)
61
62
# Create the validation data generator
63
validation_generator = test_datagen.flow_from_directory(
64
    test_folder,
65
    target_size=IMAGE_SIZE,
66
    batch_size=batch_size,
67
    color_mode="rgb",
68
    class_mode='categorical'
69
)
70
71
# Set up callbacks for learning rate reduction, early stopping, and model checkpointing
72
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint
73
74
learning_rate_reduction = ReduceLROnPlateau(monitor='loss', patience=5, verbose=2, factor=0.5, min_lr=0.000001)
75
early_stops = EarlyStopping(monitor='loss', min_delta=0, patience=6, verbose=2, mode='auto')
76
checkpointer = ModelCheckpoint(filepath='best_model.hdf5', verbose=2, save_best_only=True, save_weights_only=True)
77
78
# Define the number of output classes
79
OUTPUT_SIZE = 4
80
81
# Load a pre-trained model (Xception) without the top layers and freeze its weights
82
pretrained_model = tf.keras.applications.Xception(weights='imagenet', include_top=False, input_shape=[*IMAGE_SIZE, 3])
83
pretrained_model.trainable = False
84
85
# Create a new model with the pre-trained base and additional layers for classification
86
model = Sequential()
87
model.add(pretrained_model)
88
model.add(GlobalAveragePooling2D())
89
model.add(Dense(OUTPUT_SIZE, activation='softmax'))
90
91
print("Pretrained model used:")
92
pretrained_model.summary()
93
94
print("Final model created:")
95
model.summary()
96
97
# Compile the model with an optimizer, loss function, and evaluation metric
98
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
99
100
# Train the model with the training and validation data generators
101
history = model.fit(
102
    train_generator,
103
    steps_per_epoch=25,
104
    epochs=50,
105
    callbacks=[learning_rate_reduction, early_stops, checkpointer],
106
    validation_data=validation_generator,
107
    validation_steps=20
108
)
109
110
print("Final training accuracy =", history.history['accuracy'][-1])
111
print("Final testing accuracy =", history.history['val_accuracy'][-1])
112
113
# Function to display training curves for loss and accuracy
114
def display_training_curves(training, validation, title, subplot):
115
    if subplot % 10 == 1:
116
        plt.subplots(figsize=(10, 10), facecolor='#F0F0F0')
117
        plt.tight_layout()
118
    ax = plt.subplot(subplot)
119
    ax.set_facecolor('#F8F8F8')
120
    ax.plot(training)
121
    ax.plot(validation)
122
    ax.set_title('model ' + title)
123
    ax.set_ylabel(title)
124
    ax.set_xlabel('epoch')
125
    ax.legend(['train', 'valid.'])
126
127
# Display training curves for loss and accuracy
128
display_training_curves(history.history['loss'], history.history['val_loss'], 'loss', 211)
129
display_training_curves(history.history['accuracy'], history.history['val_accuracy'], 'accuracy', 212)
130
131
# Save the trained model
132
model.save('/content/drive/MyDrive/dataset/trained_lung_cancer_model.h5')
133
134
# Function to load and preprocess an image for prediction
135
from tensorflow.keras.preprocessing import image
136
import numpy as np
137
138
def load_and_preprocess_image(img_path, target_size):
139
    img = image.load_img(img_path, target_size=target_size)
140
    img_array = image.img_to_array(img)
141
    img_array = np.expand_dims(img_array, axis=0)
142
    img_array /= 255.0  # Rescale the image like the training images
143
    return img_array
144
145
# Load, preprocess, and predict the class of an image
146
img_path = '/content/sq.png'
147
img = load_and_preprocess_image(img_path, IMAGE_SIZE)
148
predictions = model.predict(img)
149
predicted_class = np.argmax(predictions[0])
150
class_labels = list(train_generator.class_indices.keys())
151
predicted_label = class_labels[predicted_class]
152
153
print(f"The image belongs to class: {predicted_label}")
154
155
# Display the image with the predicted class
156
plt.imshow(image.load_img(img_path, target_size=IMAGE_SIZE))
157
plt.title(f"Predicted: {predicted_label}")
158
plt.axis('off')
159
plt.show()
160
161
# Repeat the process for additional images
162
img_path = '/content/ad3.png'
163
img = load_and_preprocess_image(img_path, IMAGE_SIZE)
164
predictions = model.predict(img)
165
predicted_class = np.argmax(predictions[0])
166
predicted_label = class_labels[predicted_class]
167
print(f"The image belongs to class: {predicted_label}")
168
plt.imshow(image.load_img(img_path, target_size=IMAGE_SIZE))
169
plt.title(f"Predicted: {predicted_label}")
170
plt.axis('off')
171
plt.show()
172
173
img_path = '/content/l3.png'
174
img = load_and_preprocess_image(img_path, IMAGE_SIZE)
175
predictions = model.predict(img)
176
predicted_class = np.argmax(predictions[0])
177
predicted_label = class_labels[predicted_class]
178
print(f"The image belongs to class: {predicted_label}")
179
plt.imshow(image.load_img(img_path, target_size=IMAGE_SIZE))
180
plt.title(f"Predicted: {predicted_label}")
181
plt.axis('off')
182
plt.show()
183
184
img_path = '/content/n8.jpg'
185
img = load_and_preprocess_image(img_path, IMAGE_SIZE)
186
predictions = model.predict(img)
187
predicted_class = np.argmax(predictions[0])
188
predicted_label = class_labels[predicted_class]
189
print(f"The image belongs to class: {predicted_label}")
190
plt.imshow(image.load_img(img_path, target_size=IMAGE_SIZE))
191
plt.title(f"Predicted: {predicted_label}")
192
plt.axis('off')
193
plt.show()