Project - Ipynb - Colaboratory
Project - Ipynb - Colaboratory
Project - Ipynb - Colaboratory
Mounted at /content/drive
import os
Root = "/content/drive/MyDrive/Dataset"
os.chdir(Root)
import tensorflow as tf
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from tensorflow.keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.metrics import categorical_crossentropy
from keras.models import Sequential, Model
from keras.layers import Conv2D, MaxPooling2D,GlobalAveragePooling2D
from keras.layers import Activation, Dropout, BatchNormalization, Flatten, Dense, AvgPool2D,MaxPool2D
from keras.models import Sequential, Model
from keras.optimizers import Adam
import cv2
data = '/content/drive/MyDrive/Dataset'
No_breast_cancer = '/content/drive/MyDrive/Dataset/train/0'
Yes_breast_cancer = '/content/drive/MyDrive/Dataset/train/1'
dirlist=[No_breast_cancer, Yes_breast_cancer]
classes=['No', 'Yes']
filepaths=[]
labels=[]
for i,j in zip(dirlist, classes):
filelist=os.listdir(i)
for f in filelist:
filepath=os.path.join (i,f)
filepaths.append(filepath)
labels.append(j)
print ('filepaths: ', len(filepaths), ' labels: ', len(labels))
Files=pd.Series(filepaths, name='filepaths')
Label=pd.Series(labels, name='labels')
df=pd.concat([Files,Label], axis=1)
df=pd.DataFrame(np.array(df).reshape(975,2), columns = ['filepaths', 'labels'])
df.head()
filepaths labels
0 /content/drive/MyDrive/Dataset/train/0/18536_1... No
1 /content/drive/MyDrive/Dataset/train/0/21827_9... No
2 /content/drive/MyDrive/Dataset/train/0/22528_1... No
3 /content/drive/MyDrive/Dataset/train/0/20657_1... No
4 /content/drive/MyDrive/Dataset/train/0/23419_1... No
print(df['labels'].value_counts())
labels
Yes 710
No 265
Name: count, dtype: int64
#visualize images
plt.figure(figsize=(12,8))
for i in range(15):
random = np.random.randint(1,len(df))
plt.subplot(3,5,i+1)
plt.imshow(cv2.imread(df.loc[random,"filepaths"]))
plt.title(df.loc[random, "labels"], size = 15, color = "white")
plt.xticks([])
plt.yticks([])
plt.show()
train_gen.class_indices
{'No': 0, 'Yes': 1}
# The base model contains batchnorm layers. We want to keep them in inference mode
# when we unfreeze the base model for fine-tuning, so we make sure that the
# base_model is running in inference mode here.
x = base_model(inputs, training=False)
x = keras.layers.GlobalAveragePooling2D()(x)
x = keras.layers.Dropout(0.2)(x) # Regularize with dropout
outputs = keras.layers.Dense(1, activation="sigmoid")(x)
model = keras.Model(inputs, outputs)
model.summary()
Model: "model_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_4 (InputLayer) [(None, 424, 424, 3)] 0
=================================================================
Total params: 23566849 (89.90 MB)
Trainable params: 2049 (8.00 KB)
Non-trainable params: 23564800 (89.89 MB)
_________________________________________________________________
callbacks = [
tf.keras.callbacks.ModelCheckpoint("Tumor_classifier_model.h5", save_best_only=True, verbose = 0)
]
model.compile(loss='binary_crossentropy', optimizer=Adam(learning_rate= 0.0001), metrics=['accuracy'])
history = model.fit(train_gen, validation_data = val_gen, epochs = 5,
callbacks = [callbacks], verbose = 1)
Epoch 1/5
27/27 [==============================] - ETA: 0s - loss: 0.6680 - accuracy: 0.6651/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py
saving_api.save_model(
27/27 [==============================] - 204s 7s/step - loss: 0.6680 - accuracy: 0.6651 - val_loss: 0.6124 - val_accuracy: 0.7097
Epoch 2/5
27/27 [==============================] - 175s 6s/step - loss: 0.6402 - accuracy: 0.6951 - val_loss: 0.5612 - val_accuracy: 0.7527
Epoch 3/5
27/27 [==============================] - 171s 6s/step - loss: 0.6121 - accuracy: 0.7119 - val_loss: 0.5783 - val_accuracy: 0.7204
Epoch 4/5
27/27 [==============================] - 182s 7s/step - loss: 0.5796 - accuracy: 0.7167 - val_loss: 0.5777 - val_accuracy: 0.7527
Epoch 5/5
27/27 [==============================] - 178s 7s/step - loss: 0.5873 - accuracy: 0.7131 - val_loss: 0.5583 - val_accuracy: 0.7527
model.save("model.h5")
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. T
saving_api.save_model(
WARNING:tensorflow:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or e
pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid(True)
plt.gca().set_ylim(0, 1)
plt.show()
image = cv2.imread("/content/drive/MyDrive/Dataset/train/0/18984_1193779604_ce0f4e42f05f418d84c7c86a827bbf1f.png")
pred = loaded_model.predict(input_data)
if pred >= 0.5:
print("Yes")
else:
print("No")
WARNING:tensorflow:No training configuration found in the save file, so the model was *not* compiled. Compile it manually.
1/1 [==============================] - 2s 2s/step
Yes
train_gen.class_indices
{'No': 0, 'Yes': 1}