|
| 1 | +""" |
| 2 | +Convolutional Neural Network |
| 3 | +
|
| 4 | +Objective : To train a CNN model detect if TB is present in Lung X-ray or not. |
| 5 | +
|
| 6 | +Resources CNN Theory : |
| 7 | + https://en.wikipedia.org/wiki/Convolutional_neural_network |
| 8 | +Resources Tensorflow : https://www.tensorflow.org/tutorials/images/cnn |
| 9 | +
|
| 10 | +Download dataset from : |
| 11 | +https://lhncbc.nlm.nih.gov/LHC-publications/pubs/TuberculosisChestXrayImageDataSets.html |
| 12 | +
|
| 13 | +1. Download the dataset folder and create two folder training set and test set |
| 14 | +in the parent dataste folder |
| 15 | +2. Move 30-40 image from both TB positive and TB Negative folder |
| 16 | +in the test set folder |
| 17 | +3. The labels of the iamges will be extracted from the folder name |
| 18 | +the image is present in. |
| 19 | +
|
| 20 | +""" |
| 21 | + |
| 22 | +# Part 1 - Building the CNN |
| 23 | + |
| 24 | +import numpy as np |
| 25 | + |
| 26 | +# Importing the Keras libraries and packages |
| 27 | +import tensorflow as tf |
| 28 | +from tensorflow.keras import layers, models |
| 29 | + |
| 30 | +if __name__ == "__main__": |
| 31 | + |
| 32 | + # Initialising the CNN |
| 33 | + classifier = models.Sequential() |
| 34 | + |
| 35 | + # Step 1 - Convolution |
| 36 | + classifier.add( |
| 37 | + layers.Conv2D(32, (3, 3), input_shape=(64, 64, 3), activation="relu") |
| 38 | + ) |
| 39 | + |
| 40 | + # Step 2 - Pooling |
| 41 | + classifier.add(layers.MaxPooling2D(pool_size=(2, 2))) |
| 42 | + |
| 43 | + # Adding a second convolutional layer |
| 44 | + classifier.add(layers.Conv2D(32, (3, 3), activation="relu")) |
| 45 | + classifier.add(layers.MaxPooling2D(pool_size=(2, 2))) |
| 46 | + |
| 47 | + # Step 3 - Flattening |
| 48 | + classifier.add(layers.Flatten()) |
| 49 | + |
| 50 | + # Step 4 - Full connection |
| 51 | + classifier.add(layers.Dense(units=128, activation="relu")) |
| 52 | + classifier.add(layers.Dense(units=1, activation="sigmoid")) |
| 53 | + |
| 54 | + # Compiling the CNN |
| 55 | + classifier.compile( |
| 56 | + optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"] |
| 57 | + ) |
| 58 | + |
| 59 | + # Part 2 - Fitting the CNN to the images |
| 60 | + |
| 61 | + # Load Trained model weights |
| 62 | + |
| 63 | + # from keras.models import load_model |
| 64 | + # regressor=load_model('cnn.h5') |
| 65 | + |
| 66 | + train_datagen = tf.keras.preprocessing.image.ImageDataGenerator( |
| 67 | + rescale=1.0 / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True |
| 68 | + ) |
| 69 | + |
| 70 | + test_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1.0 / 255) |
| 71 | + |
| 72 | + training_set = train_datagen.flow_from_directory( |
| 73 | + "dataset/training_set", target_size=(64, 64), batch_size=32, class_mode="binary" |
| 74 | + ) |
| 75 | + |
| 76 | + test_set = test_datagen.flow_from_directory( |
| 77 | + "dataset/test_set", target_size=(64, 64), batch_size=32, class_mode="binary" |
| 78 | + ) |
| 79 | + |
| 80 | + classifier.fit_generator( |
| 81 | + training_set, steps_per_epoch=5, epochs=30, validation_data=test_set |
| 82 | + ) |
| 83 | + |
| 84 | + classifier.save("cnn.h5") |
| 85 | + |
| 86 | + # Part 3 - Making new predictions |
| 87 | + |
| 88 | + test_image = tf.keras.preprocessing.image.load_img( |
| 89 | + "dataset/single_prediction/image.png", target_size=(64, 64) |
| 90 | + ) |
| 91 | + test_image = tf.keras.preprocessing.image.img_to_array(test_image) |
| 92 | + test_image = np.expand_dims(test_image, axis=0) |
| 93 | + result = classifier.predict(test_image) |
| 94 | + training_set.class_indices |
| 95 | + if result[0][0] == 0: |
| 96 | + prediction = "Normal" |
| 97 | + if result[0][0] == 1: |
| 98 | + prediction = "Abnormality detected" |
0 commit comments