Exploring the World of Medical Imaging: Building an App to Identify Brain Tumors from MRI Scans (About Accuracy)
Tags: datascience
Published: 2024-01-16 21:23:28

The article I wrote earlier primarily focused on learning the process of creating an AI application, so I didn't emphasize much on image recognition accuracy. However, as someone claiming to be a machine learning engineer, pursuing accuracy is essential. In this article, I will discuss the efforts made to improve the accuracy, referencing a popular page from the "Discussion" section of a dataset containing MRI brain images.

The page referred to for improving accuracy can be found here:
[https://www.kaggle.com/code/ruslankl/brain-tumor-detection-v1-0-cnn-vgg-16](https://www.kaggle.com/code/ruslankl/brain-tumor-detection-v1-0-cnn-vgg-16)

### Steps taken to improve accuracy:

#### 1. Removing unnecessary parts of the images

The provided dataset contained images with varying sizes and irregular areas of black space around the brain, as depicted in the following image:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3522867/afbbbdfe-a7f1-28fb-0ea4-39f63b61bc71.png)

To maintain consistency and avoid introducing anomalies during training, unnecessary portions around the brain were removed. The process involved four steps, including converting the images to grayscale, obtaining contours, and extracting the outermost points to create a rectangle that encompasses the brain.

```python
# Code snippet for removing unnecessary parts of the images
# (Refer to the original article for the complete code)
def crop_imgs(set_name, add_pixels_value=0):
    # Implementation details...
    return np.array(set_new)
```

After removing unnecessary portions, the images displayed a more uniform appearance:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3522867/01c109aa-8175-0072-d9e5-df6885f11db1.png)

#### 2. Image augmentation

Due to the limited number of images in the dataset (100-200), data augmentation was performed to increase the dataset size. Augmentation techniques included rotation, shifting, flipping, and adjusting brightness.

```python
# Code snippet for image augmentation
# (Refer to the original article for the complete code)
train_datagen = ImageDataGenerator(
    rotation_range=15,
    width_shift_range=0.1,
    height_shift_range=0.1,
    shear_range=0.1,
    brightness_range=[0.5, 1.5],
    horizontal_flip=True,
    vertical_flip=True,
    preprocessing_function=preprocess_input
)
```

The augmented images introduced subtle variations:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3522867/037bb7a4-923b-5464-4bca-06bfe71b6d4d.png)

### Results of accuracy improvement:

The model's accuracy improved progressively with the increase in epochs, reaching close to 90% in the end:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3522867/6e8422d7-49a4-7327-4a8b-00e64348456a.png)

However, in medical image classification, the emphasis is often on avoiding false negatives, as misdiagnosing a condition could have serious consequences. The confusion matrix is used to evaluate the model's performance in such cases.

```python
# Code snippet for creating and displaying a confusion matrix
# (Refer to the original article for the complete code)
confusion_mtx = confusion_matrix(y_val, predictions) 
cm = plot_confusion_matrix(confusion_mtx, classes=list(labels.items()), normalize=False)
```

The confusion matrix visually represents the model's performance:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3522867/a650096a-251d-4200-aa8a-ef8f69b826c8.png)

### Conclusion:

While the article borrowed code from a provided source, the author expressed a desire to incorporate original ideas in future work to further enhance accuracy.