<jupyter_start><jupyter_text><jupyter_code># Load the Drive helper and mount
from google.colab import drive

# This will prompt for authorization.
drive.mount('/content/drive')
import fastai
from fastai.vision import *
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from zipfile import ZipFile 
def unZip(file_name):
  with ZipFile(file_name, 'r') as zip: 
      zip.extractall() 
      print('Done!') 
!rm -rf chest_xray
!rm -rf dataset1
file_name = '/content/drive/chestXRay.zip'
path="/content/drive/My Drive/"
file_name=path+'chestXRay.zip'
unZip(file_name)
!ls chest_xray
!ls
unZip('chest_xray.zip')
!ls chest_xray
import glob
import cv2
import numpy as np
%matplotlib inline
filesPos= sorted(glob.glob('chest_xray/train/NORMAL/*.jpeg'))
filesNeg=sorted(glob.glob('chest_xray/train/PNEUMONIA/*.jpeg'))
print(len(filesPos),len(filesNeg))
from fastai import *
import shutil
def copyfiles(filesName,dest):
  c=0
  for file in filesName:
    shutil.copy(file, dest)
    c+=1
  print("Copied "+str(c))
!mkdir dataset1
!mkdir dataset1/train
!ls dataset1
!mkdir dataset1/train/PNEUMONIA
!mkdir dataset1/train/NORMAL
!ls dataset1/train/
filesNeg=filesNeg[:1340]

import random 
random.seed(3000)
random.shuffle(filesNeg)
random.shuffle(filesPos)
!ls chest_xray
copyfiles(filesPos,'dataset1/train/NORMAL')
copyfiles(filesNeg,'dataset1/train/PNEUMONIA')
!mkdir dataset1/valid
!mkdir dataset1/test
!cp -r chest_xray/val dataset1/
!cp -r chest_xray/test dataset1/
!rm -rf dataset1/valid
!mv dataset1/val dataset1/valid

np.random.seed(1234)
path='dataset1'
data = ImageDataBunch.from_folder(path,
        ds_tfms=get_transforms(do_flip=True,max_lighting=0.1),
        seed=1234,
        valid_pct=0.2,                          
        size=224,
        num_workers=8,
        bs=32,
        test="test")

data
data.classes
data.show_batch(rows=3, figsize=(10,6), hide_axis=False)
from sklearn.metrics import roc_auc_score,f1_score
def f1_score_a(y_pred,y_true,tens=True):
#     score=roc_auc_score(y_true,torch.sigmoid(y_pred)[:,1])
    f1a_score=f1_score(y_true, np.round(torch.sigmoid(y_pred)[:,1]), average='macro') 
    if tens:
#         score=tensor(score)
        f1a_score=tensor(f1a_score)
    else:
        f1a_score=f1a_score
    return f1a_score
# from sklearn.metrics import f1_score
# def f1_out(y_ture,y_pred,tens=True):
#     f1a_score=f1_score(y_true, torch.sigmoid(y_pred)[:,1], average='macro') 
#     if tens:
#         f1a_score=tensor(f1a_score)
#     else:
#         f1a_score=f1a_score
#     return f1a_score

import torch 
import torchvision
densenet121 = torchvision.models.densenet121(pretrained=True)
densenet121<jupyter_output><empty_output><jupyter_text><jupyter_code>num_ftrs = densenet121.classifier.in_features
densenet121.classifier=nn.Sequential(
    
    nn.Dropout(0.5),
    nn.Linear(num_ftrs, 128),
    nn.Dropout(0.3),
    nn.Linear(128, 2),
    
)
densenet121=densenet121.cuda()

auc=[]
loss=[]
aucc=[]
learn = Learner(data, densenet121, metrics=[error_rate, accuracy,f1_score_a])
learn
from fastai.callbacks import *
learn.fit_one_cycle(10,0.0003,callbacks=[SaveModelCallback(learn, every='imrpovement', monitor='f1_score_a')])
lossArray=[]

lossArray.append(learn.recorder.losses)
auc=[]
auc.append(learn.recorder.metrics)

auc

learn.recorder.plot_losses()
learn.recorder.plot(show_momentum=True)
learn.lr_find()
learn.recorder.plot()
learn.load('bestmodel')
learn.unfreeze()
learn.fit_one_cycle(5,callbacks=[SaveModelCallback(learn, every='imrpovement', monitor='f1_score_a')])

learn.load('bestmodel')
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()
# learn.load('Res')
# learn.lr_find()
# # learn.recorder.plot()
# learn.fit_one_cycle(20,max_lr=(1e-07),wd=0.25)
# interp = ClassificationInterpretation.from_learner(learn)
# interp.plot_confusion_matrix()
# learn.lr_find()
# learn.recorder.plot()
conf=interp.confusion_matrix()
TrueNagitive=conf[0][0]
FalseNegative=conf[0][1]
TruePositive=conf[1][1]
FalsePositive=conf[1][0]
recal=TruePositive/(TruePositive+FalseNegative)
precision=TruePositive/(TruePositive+FalsePositive)
print("Precision of Model =",precision,"Recall of Model ", recal)
f1=2*((precision*recal)/(precision+recal))
print('F1 Score of Model =',f1)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup