from fastai.vision.all import *
from fastai import *
from fastai.vision import *
from fastai.vision.widgets import *
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    counter = 0
    for filename in filenames:
        counter = counter + 1
        #print(os.path.join(dirname, filename))

print(f"Loaded all files from the directory: {dirname}")
print(f"Number of files loaded:{counter}")
    

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
Loaded all files from the directory: /kaggle/input/proton-collision-image-set/Proton Collision 13TeV/Train/WJets
Number of files loaded:8444
dir_name = '../input/proton-collision-image-set/Proton Collision 13TeV/Train'
path = Path(dir_name)
path.ls()
(#3) [Path('../input/proton-collision-image-set/Proton Collision 13TeV/Train/TTbar'),Path('../input/proton-collision-image-set/Proton Collision 13TeV/Train/QCD'),Path('../input/proton-collision-image-set/Proton Collision 13TeV/Train/WJets')]
dls = DataBlock(
    blocks=(ImageBlock, CategoryBlock), 
    get_items=get_image_files, 
    splitter=RandomSplitter(valid_pct=0.2, seed=42),
    get_y=parent_label,
    item_tfms=[Resize(128, method='squish')]
).dataloaders(path, bs=32)
dls.show_batch(max_n=9)
#learn.fine_tune(3)

learn = vision_learner(dls, resnet18, metrics = error_rate)
learn.lr_find()
Downloading: "https://download.pytorch.org/models/resnet18-f37072fd.pth" to /root/.cache/torch/hub/checkpoints/resnet18-f37072fd.pth
SuggestedLRs(valley=0.0010000000474974513)
learn.fine_tune(4, 4.7e-3)
epoch train_loss valid_loss error_rate time
0 1.076869 0.994646 0.488542 04:20
epoch train_loss valid_loss error_rate time
0 0.823417 0.794527 0.380680 04:06
1 0.761839 0.736746 0.344923 04:06
2 0.621185 0.706744 0.317859 04:05
3 0.416418 0.804867 0.329514 04:03
img = PILImage.create('../input/proton-collision-image-set/Proton Collision 13TeV/Test/TTbar/ttbar_lepFilter_13TeV_1087_1934.png')
what_proton,_,probs = learn.predict(img)
idx = 0
if what_proton == "TTbar":
    idx = 1
elif what_proton == "WJets":
    idx = 2
print(f"This collision belongs to category of: {what_proton}")
print(f"Probability it belongs to that category: {probs[idx]:.4f}")
img
This collision belongs to category of: TTbar
Probability it belongs to that category: 0.9989
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()
learn.show_results()
interp.plot_top_losses(5, nrows=1)
cleaner = ImageClassifierCleaner(learn)
cleaner

End of notebook (for now)