0% found this document useful (0 votes)
7 views17 pages

Chest Cancer - 90.8 On Test Data Set Code

Uploaded by

Abdul Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views17 pages

Chest Cancer - 90.8 On Test Data Set Code

Uploaded by

Abdul Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Dataset

Dataset is divided into 4 classes


1. Andenocarcinoma
2. Large Cell Carcinoma
3. Normal
4. Squamous Carcinoma

Department of Electrical & Computer Engineering 2


Dataset
Adenocarcinoma

Department of Electrical & Computer Engineering 3


Dataset
Large Cell Carcinoma

Department of Electrical & Computer Engineering 4


Dataset
Normal

Department of Electrical & Computer Engineering 5


Dataset
Squamous Cell Carcinoma

Department of Electrical & Computer Engineering 6


Code Description
Cell 1-4

Import the libraries and add the path

Department of Electrical & Computer Engineering 7


Cell 05
The image_paths argument is a list of strings that represent the path of
the directories where the images are stored. The function will load the
first n/4 images from each directory in image_paths.
The n argument specifies the total number of images to be loaded. The
function loads n/4 images from each directory in image_paths, so the
Code total number of images will be n.The function uses the cv2 library to
Description read the images from disk, and the os library to access the directories
specified in image_paths. The images are stored in a list of lists, where
each inner list contains the images from one directory. The final result is
a NumPy array that is returned by the function.

Department of Electrical & Computer Engineering 8


Cell 06
In this cell, visualizes a sample of images loaded using the load_images()
function. The code first calls the load_images() function and stores the returned
NumPy array in the sample variable.
Then, a figure is created using the plt.figure() method from the Matplotlib library.
The figure size is set to (20, 5) using the figsize argument.
The code uses a for loop to plot the images on the figure. The outer for loop

Code iterates over the first dimension of the sample array, and the inner for loop
iterates over the second dimension. For each iteration, a subplot is added to the
figure using the fig.add_subplot() method. The x ticks and y ticks arguments are
Description set to ‘[ ]’ to remove the x and y axis labels.
The subplot is then filled with an image using the ax.imshow() method. The
image is obtained from sample[i,m] and is passed through the np.squeeze()
method to remove single-dimensional entries from the shape of the array. The
shapes of the images are also appended to a list called shapes.
Department of Electrical & Computer Engineering 9
Code Description Cell 07
This code defines the creation of three Image Data
Generators for a machine learning task that involves
images. The first two Image Data Generators,
train_datagen and valid_datagen, are used to
preprocess the training and validation data,
respectively. The preprocessing steps include
converting the datatype to float32 and rescaling the
pixel values so that they lie between 0 and 1. The third
Image Data Generator, test_datagen, is used to
preprocess the test data.
The flow_from_directory method is then used to
generate batches of image data from the respective
directories for each Image Data Generator. The
batch_size argument specifies the number of images
in each batch, and the target_size argument specifies
the size that the images should be resized to (305x430
pixels in this case). The class_mode argument is set to
'categorical' since the images belong to multiple
classes.
Department of Electrical & Computer Engineering 10
Code Description
Cell 08
This code defines a neural network using the Keras library.
The network is created using the Sequential model and is
composed of several layers:
• The first layer is a Conv2D layer with 8 filters, a kernel
size of 2, a padding of 'same', and a ReLU activation
function. This layer serves as a feature extraction layer,
where the filters are applied to the input image to detect
various features in the image.
• The second layer is a MaxPooling2D layer with a pool
size of 2. This layer serves as a down-sampling layer,
where the spatial resolution of the feature maps is
reduced to half its original size.
• The third and fourth layers are another Conv2D and
MaxPooling2D layer, respectively, with similar
properties as the first two layers, but with 16 filters
instead of 8.

Department of Electrical & Computer Engineering 11


Cell 08
Code Description • The fifth layer is a Dropout layer with a rate of 0.4. This
layer serves as a regularization layer, where some of the
neurons in the network are randomly dropped during training to
prevent overfitting.

• The sixth layer is a Flatten layer, which flattens the feature


maps from the previous layer into a one-dimensional vector.

• The seventh layer is a Dense layer with 300 neurons and a


ReLU activation function. This layer serves as a fully connected
layer that processes the features extracted from the previous
layers.

• The eighth layer is another Dropout layer with a rate of 0.5.

• The final layer is a Dense layer with 4 neurons and a softmax


activation function. This layer serves as the output layer, where
the network outputs a probability distribution over the 4 classes
in the problem.

• The model has a L2 regularization term with a weight decay of


1e-3 applied to the Conv2D layers. This helps to prevent
overfitting by adding a penalty term to the loss function during
training that discourages large weights in the model.
Department of Electrical & Computer Engineering 12
Cell 09
Code Description This code trains a deep learning model using the fit method. The
training data is passed through a train_generator object, which
provides batches of data for training.

The model is trained for a maximum of 100 epochs and the


validation data is passed through the valid_generator object. The
learning process can be controlled using a number of callbacks,
including Model Checkpoint and Early Stopping. Mode lCheckpoint
is used to save the best model during the training process. The
best model is defined as the one with the lowest validation loss.
The model is saved to a file named 'chestmodel.hdf5' with the
'verbose' argument set to 1, which means that information about
the saving process will be printed to the console. Early stopping is
used to stop the training process early if the validation loss does
not improve for a certain number of epochs. In this case, the
training process will be stopped if the validation loss does not
improve for 15 epochs.

The model is optimized using the Adam optimizer, with a learning


rate of 0.00001 and a decay of 1e-5. The loss function used during
training is 'categorical_crossentropy' and the accuracy of the
model is monitored using the 'acc' metric.

Department of Electrical & Computer Engineering 13


Cell 10
Code Description This code trains a deep learning model using the fit method. The
training data is passed through a train_generator object, which
provides batches of data for training.

The model is trained for a maximum of 100 epochs and the


validation data is passed through the valid_generator object. The
learning process can be controlled using a number of callbacks,
including Model Checkpoint and Early Stopping. Mode lCheckpoint
is used to save the best model during the training process. The
best model is defined as the one with the lowest validation loss.
The model is saved to a file named 'chestmodel.hdf5' with the
'verbose' argument set to 1, which means that information about
the saving process will be printed to the console. Early stopping is
used to stop the training process early if the validation loss does
not improve for a certain number of epochs. In this case, the
training process will be stopped if the validation loss does not
improve for 15 epochs.

The model is optimized using the Adam optimizer, with a learning


rate of 0.00001 and a decay of 1e-5. The loss function used during
training is 'categorical_crossentropy' and the accuracy of the
model is monitored using the 'acc' metric.

Department of Electrical & Computer Engineering 14


VGG 16 ResNet50 model is instantiated using the ResNet50
function from the keras.applications module. The
include_top argument is set to False, which means that
the fully connected layers at the top of the ResNet50
model will not be included.

The pooling argument is set to 'avg', which means that


average pooling will be used. The weights argument is set
to 'imagenet', which means that the pre-trained weights on
the ImageNet dataset will be used to initialize the model
weights.

The input_shape argument is set to (image_shape), which


defines the input shape of the model. After the ResNet50
model is instantiated, a loop is used to set the trainable
attribute of each layer to False if the layer's name does not
contain the string 'conv5’.

This means that all layers in the ResNet50 model except


for the layers with names that contain the string 'conv5' will
not be updated during training.
Department of Electrical & Computer Engineering 15
VGG 16
This approach is often used to fine-tune a pre-trained
deep learning model, where the idea is to use the pre-
trained weights as a starting point and then fine-tune the
model on a new task. By making only some of the layers
trainable, the model can continue to benefit from the pre-
trained weights while also learning task-specific features.
In this code, it appears that only the conv5 layers are
made trainable, which is a common strategy for fine-tuning
pre-trained models..

Department of Electrical & Computer Engineering 16


VGG 16 In the code you posted, a Sequential model from the keras module is
instantiated, and the ResNet50 model created earlier is added to this model
as the first layer.

After adding the ResNet50 model, a dropout layer with a dropout rate of 0.6 is
added using the Dropout function. Dropout is a regularization technique used
to prevent overfitting in deep learning models. The dropout layer randomly
sets some fraction of the input units to 0 during training, which helps to
prevent the model from relying too heavily on any single feature.

Next, a Flatten layer is added, which reshapes the output of the previous
layer into a 1D tensor. This is followed by a Batch Normalization layer, which
normalizes the activations of the previous layer by subtracting the batch mean
and dividing by the batch standard deviation.

The Batch Normalization layer helps to improve the stability and speed of
training. Another dropout layer with a dropout rate of 0.6 is added, followed by
a dense (fully connected) layer with N_CLASSES output units and a softmax
activation function. The softmax activation function is used for multi-class
classification problems, as it returns a probability distribution over the
N_CLASSES classes, where the sum of all probabilities is 1.

Finally, the model.summary() function is called, which provides a summary of


the architecture of the model, including the number of parameters in each
layer and the total number of parameters in the model.

Department of Electrical & Computer Engineering 17

You might also like