<jupyter_start><jupyter_text>**INITIALIZATION:**- I use these three lines of code on top of my each notebooks because it will help to prevent any problems while reloading the same project. And the third line of code helps to make visualization within the notebook.<jupyter_code>#@ INITIALIZATION: 
%reload_ext autoreload
%autoreload 2
%matplotlib inline<jupyter_output><empty_output><jupyter_text>**LIBRARIES AND DEPENDENCIES:**- I have downloaded all the libraries and dependencies required for the project in one particular cell.<jupyter_code>#@ INSTALLING DEPENDENCIES: UNCOMMENT BELOW: 
# !pip install -Uqq fastbook
# import fastbook
# fastbook.setup_book()
# !pip install pydicom kornia opencv-python scikit-image nbdev
#@ DOWNLOADING LIBRARIES AND DEPENDENCIES: 
from fastai.basics import *
from fastai.callback.all import *
from fastai.vision.all import *
from fastai.medical.imaging import *

import pydicom 
import pandas as pd

#@ IGNORING WARNINGS: 
import warnings
warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>**GETTING DICOM FILES:**- **DICOM** stands for Digital Imaging and Communications in Medicine. It is the de-facto standard that establishes rules that allow the medical images and associated information to be exchanged between imaging equipment from different vendors, computers and hospitals.<jupyter_code>#@ GETTING DICOM FILES: 
pneumothorax_source = untar_data(URLs.SIIM_SMALL)           # Downloading the Files. 
items = get_dicom_files(pneumothorax_source/f"train/")      # Getting Items. 

#@ SPLITTING THE DATA ITEMS: 
train, val = RandomSplitter()(items)                        # Splitting the Items. 
#@ LOADING THE DICOM FILE: 
patient = 7                                                 # Initializing an Item. 
xray_sample = items[patient].dcmread()                      # Initialization. 
xray_sample                                                 # Inspecting the Data.<jupyter_output><empty_output><jupyter_text>**Note:**- The detailed explanations is in [**Fastai**](https://docs.fast.ai/tutorial.medical_imaging.html).<jupyter_code>#@ INSPECTING PIXEL DATA: 
xray_sample.PixelData[:200]                               # Inspecting the Pixel. 
#@ INSPECTING PIXEL DATA: 
xray_sample.pixel_array, xray_sample.pixel_array.shape    # Inspecting the Pixel. 
#@ INSPECTING THE DICOM FILE: 
xray_sample.show()
#@ CREATING DICOM DATAFRAME: 
dicom_dataframe = pd.DataFrame.from_dicoms(items)                       # Initializing a DataFrame. 
dicom_dataframe[:2]                                                     # Inspecting a DataFrame. 
#@ LOADING LABELS: 
df = pd.read_csv(pneumothorax_source/f"labels.csv")                     # Creating a DataFrame. 
df.head(2)                                                              # Inspection.<jupyter_output><empty_output><jupyter_text>**INITIALIZING DATA BLOCK AND DATA LOADERS:**<jupyter_code>#@ INITIALIZING DATA BLOCK: 
pneumothorax = DataBlock(blocks=(ImageBlock(cls=PILDicom), CategoryBlock),      # Initializing Image Blocks. 
                         get_x=lambda x:pneumothorax_source/f"{x[0]}",          # Getting Data Items. 
                         get_y=lambda x:x[1],                                   # Getting Labels. 
                         batch_tfms=[*aug_transforms(size=224),                 # Initializing Batch Transformations.
                                     Normalize.from_stats(*imagenet_stats)])    # Initializing Data Block. 

#@ INITIALIZING DATA LOADERS: 
dls = pneumothorax.dataloaders(df.values, num_workers=0)

#@ INSPECTING DATA LOADERS: 
dls.show_batch(max_n=16)<jupyter_output><empty_output><jupyter_text>**INITIALIZING TRAINING:**<jupyter_code>#@ TRAINING THE PRETRAINED MODEL: 
learn = cnn_learner(dls, resnet34, metrics=accuracy)                 # Initializing the Learner. 
#@ INITIALIZING LR FINDER: 
learn.lr_find()                                                      # Inspecting Learning Rates. 
#@ TRAINING THE PRETRAINED MODEL: 
learn.fit_one_cycle(1)
#@ PREDICTING THE IMAGE: 
learn.predict(pneumothorax_source/f"train/Pneumothorax/000004.dcm")
#@ INSPECTING THE RESULTS: 
tta = learn.tta(use_max=True)
learn.show_results(max_n=16)<jupyter_output><empty_output><jupyter_text>**MODEL INTERPRETATION:**<jupyter_code>#@ INITIALIZING MODEL INTERPRETATION: 
interp = Interpretation.from_learner(learn)                  # Initializing Interpretation. 
interp.plot_top_losses(2)                                    # Inspection.<jupyter_output><empty_output><jupyter_text>**CONFUSION MATRIX:**- The **Confusion Matrix** is plotted against the valid dataset.<jupyter_code>#@ PLOTTING CONFUSION MATRIX: 
interp = ClassificationInterpretation.from_learner(learn)          # Initializing Interpretation. 
losses, idxs = interp.top_losses()                                 # Initializing Top Losses. 
len(dls.valid_ds)==len(losses)==len(idxs)
interp.plot_confusion_matrix(figsize=(7, 7))                       # Initializing Confusion Matrix. 
#@ INTERPRETING CONFUSION MATRIX: 
upp, low = interp.confusion_matrix()                        # Initializing Confusion Matrix. 
tn, fp = upp[0], upp[1]                                     # Initialization. 
fn, tp = low[0], low[1]                                     # Initialization.
print(tn, fp, fn, tp)                                       # Inspection.<jupyter_output>33 2 13 2<jupyter_text>**SENSITIVITY:**- **Sensitivity = True Positive / (True Positive + False Negative)**- It is also known as a **Type II Error**.<jupyter_code>#@ INSPECTING SENSITIVITY: 
sensitivity = tp/(tp + fn)                                  # Initializing Sensitivity. 
sensitivity                                                 # Inspection.<jupyter_output><empty_output><jupyter_text>**SPECIFICITY:**- **Specificity = True Negative / (False Positive + True Negative)**- It is also known as a **Type I Error**.<jupyter_code>#@ INSPECTING SPECIFICITY: 
specificity = tn/(fp + tn)                                  # Initializing Specificity. 
specificity                                                 # Inspection. 
#@ INITIALIZING POSITIVE PREDICTIVE VALUE (PPV)
ppv = tp/(tp+fp)                                            # Initializing PPV. 
ppv                                                         # Inspection. 
#@ INITIALIZING NEGATIVE PREDICTIVE VALUE (NPV)
npv = tn/(tn+fn)                                            # Initializing NPV. 
npv                                                         # Inspection.<jupyter_output><empty_output><jupyter_text>**ACCURACY:**- **Accuracy = Sensitivity X Prevalence + Specificity(1 - Prevalence)**.<jupyter_code>#@ INITIALIZING PREVALENCE: 
val = dls.valid_ds.cat                                      # Initialization. 
prevalence = 15 / 50                                        # Initializing Prevalence. 
prevalence
#@ GETTING ACCURACY: 
accuracy = (sensitivity*prevalence)+(specificity*(1-prevalence))    # Initializing Accuracy. 
accuracy                                                            # Inspection.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09198645598194131, 'fraction_numerical': 0.006348758465011287, 'mean_word_length': 2.372502378686965, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '293213', 'license': 'MIT', 'n_tokens_mistral': 2226, 'n_tokens_neox': 2082, 'n_words': 472, 'path': '19. Chest XRays Classification/XRays Classification.ipynb', 'repo_name': 'ThinamXx/Fastai', 'size': '7088'}