<jupyter_start><jupyter_text>Understanding the Amazon Forest from Space<jupyter_code>from IPython.display import Image
from IPython.core.display import HTML 
Image(url= "https://storage.googleapis.com/kaggle-competitions/kaggle/6322/logos/header.png")<jupyter_output><empty_output><jupyter_text>Importing Libraries<jupyter_code>%reload_ext autoreload
%autoreload 2
%matplotlib inline
from fastai.vision import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Loading the Dataset<jupyter_code>path = Path('/kaggle/input/planet-understanding-the-amazon-from-space/')
path.ls()<jupyter_output><empty_output><jupyter_text>Multi-Classification<jupyter_code>df = pd.read_csv(path/'train_v2.csv')
df.head()<jupyter_output><empty_output><jupyter_text>Different Tags<jupyter_code>df.pivot_table(index='tags', aggfunc=len).sort_values('image_name', ascending=False)
# GPU required
torch.cuda.is_available()
torch.backends.cudnn.enabled<jupyter_output><empty_output><jupyter_text>Defining Accuracy and F2-Score<jupyter_code>def p_accuracy(pred, act, **kwargs):
    return accuracy_thresh(pred, act, thresh=0.2, **kwargs)
#This kaggle competition uses f2 score for the final eval. So we should use that as well.
def f2_score(pred, act, **kwargs):
    return fbeta(pred, act, beta=2, thresh=0.2, **kwargs)<jupyter_output><empty_output><jupyter_text>Transforms<jupyter_code>def get_data(sz):
    tfms = get_transforms(do_flip=True, max_lighting=0.1, max_zoom=1.05, max_warp=0.)
    
    data = (
        ImageList
            .from_csv(path, 'train_v2.csv', folder="train-jpg", suffix=".jpg")
            .split_by_rand_pct(0.2)
            .label_from_df(label_delim=' ')
            .transform(tfms, size=sz)
            .add_test_folder('test-jpg-v2')
            .databunch(num_workers=0)
            .normalize(imagenet_stats)
    )
    return data
#Cause learning on 64x64
sz=64
data = get_data(sz)<jupyter_output><empty_output><jupyter_text>Classes<jupyter_code>len(data.classes), data.classes<jupyter_output><empty_output><jupyter_text>Displaying Images<jupyter_code>data.show_batch(rows=3, figsize=(10,12))
!mkdir -p /tmp/.cache/torch/checkpoints
!cp /kaggle/input/fastai-pretrained-models/resnet50-19c8e357.pth /tmp/.cache/torch/checkpoints/resnet50-19c8e357.pth

learn = cnn_learner(data, models.resnet50, metrics=[p_accuracy, f2_score], model_dir = Path('../kaggle/working'),path = Path("."))
learn.lr_find()
learn.recorder.plot()<jupyter_output><empty_output><jupyter_text>Training the Model<jupyter_code>lr = 0.01
learn.fit_one_cycle(5, slice(lr))
learn.unfreeze() 
learn.lr_find()
learn.recorder.plot()
lr = 1e-4
learn.fit_one_cycle(4, slice(lr))
learn.show_results(rows=3, figsize=(12,15))<jupyter_output><empty_output><jupyter_text>Saving Model<jupyter_code>learn.save('stage-2-rn50')
learn.export()<jupyter_output><empty_output><jupyter_text>Predicting on Test Set<jupyter_code>test = ImageList.from_folder(path/'test-jpg-v2').add(ImageImageList.from_folder(path/'test-jpg-additional'))
len(test)
learn = load_learner('../working/', test=test)
preds, _ = learn.get_preds(ds_type=DatasetType.Test)
thresh = 0.2
labelled_preds = [' '.join([learn.data.classes[i] for i,p in enumerate(pred) if p > thresh]) for pred in preds]
submission = pd.read_csv(path/'sample_submission_v2.csv')
submission['tags'] = labelled_preds
submission.to_csv('submission.csv')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup