<jupyter_start><jupyter_text>**Download data from kaggle to gdrive and make it ready for submission to kaggle directly**<jupyter_code>#import necessary libraries
%reload_ext autoreload
%autoreload 2
%matplotlib inline
from fastai.vision import *
import pandas as pd
from fastai.metrics import error_rate
from fastai.callbacks import *
from PIL import ImageFile
from pathlib import Path
ImageFile.LOAD_TRUNCATED_IMAGES = True
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module="torch.nn.functional")
!nvidia-smi
#This will prompt you for authentication from gdrive. Click the link and allow colab to access your drive folders
from google.colab import drive
drive.mount('/content/drive')

#Path to your gdrive data directory
path= '/content/drive/My Drive/vehicle_classification'
path = Path(path)
data_path = path/'vehicle_classification_dataset'
model_path= path/'models'
def get_data(bs, sz):
  tfms = get_transforms()
  data = (ImageList.
                  from_folder(f'{data_path}').
                  filter_by_folder(include=['train']).
                  # use_partial_data(sample_pct=0.3). #take on 10% of available input data for faster evaluation
                  split_by_rand_pct(valid_pct = 0.25, seed=42). #set train/valid split to 80:20
                  label_from_folder().
                  add_test_folder().
                  transform(tfms,size=sz).
                  databunch(bs=bs, num_workers=0)   
               )
  data.normalize(imagenet_stats)
  return data

def do_fit(bs,sz,epochs,lr, freeze=True):
    learn.dbunch = get_data(bs, sz)
    if freeze:
        learn.freeze()
        learn.fit_one_cycle(10, slice(lr))
    learn.unfreeze()
    learn.fit_one_cycle(epochs, slice(lr))

def classification_interp(learn):
  data = learn.data
  interp = ClassificationInterpretation.from_learner(learn)
  losses,idxs = interp.top_losses()
  len(data.valid_ds)==len(losses)==len(idxs)
  interp.plot_top_losses(9, figsize=(15,11))
  interp.plot_confusion_matrix(figsize=(12,12), dpi=60)
  interp.most_confused(min_val=2)

# from google.colab import files
# files.upload()  #this will prompt you to upload the kaggle.json
# !pip install -q kaggle
# !mkdir -p ~/.kaggle
# !cp kaggle.json ~/.kaggle/
# !ls ~/.kaggle
# !chmod 600 /root/.kaggle/kaggle.json  # set permission
# path= '/content/drive/My Drive/vehicle_classification'
# path = Path(path)
# data_path = path/'vehicle_classification_dataset'
# model_path= path/'models'
# # !kaggle competitions download -c understanding_cloud_organization -p /content/drive/My\ Drive/cloud_formation/cloud_formation_dataset
# # !kaggle competitions download vehicle -p /content/drive/My\ Drive/vehicle_classification/vehicle_classification_dataset -o
# !unzip -q -n /content/drive/My\ Drive/vehicle_classification/vehicle_classification_dataset/vehicle.zip -d /content/drive/My\ Drive/vehicle_classification/vehicle_classification_dataset
# !unzip -q -n /content/drive/My\ Drive/vehicle_classification/vehicle_classification_dataset/train.zip -d /content/drive/My\ Drive/vehicle_classification/vehicle_classification_dataset/train
# !unzip -q -n /content/drive/My\ Drive/vehicle_classification/vehicle_classification_dataset/test.zip -d /content/drive/My\ Drive/vehicle_classification/vehicle_classification_dataset/test
data = get_data(92, 92)
data
data.show_batch(rows=3, figsize=(10,10))  
learn = cnn_learner(data, models.resnet18 , pretrained=True, model_dir= model_path , metrics=[error_rate, accuracy], callback_fns=[partial(EarlyStoppingCallback, monitor='accuracy', min_delta=0.01, patience=3)])
learn.freeze()
learn.data
learn.fit_one_cycle(6, 1e-3)

do_fit(128, 128, 10, 1e-3)

learn.save('stage-1-resnet-18-82-perct')<jupyter_output><empty_output><jupyter_text>**Make Prediction and submit**<jupyter_code>DatasetType.Test
preds,y=learn.get_preds(ds_type=DatasetType.Test)
preds = np.argmax(preds, axis = 1)
preds_classes = [data.classes[i] for i in preds]

submission = pd.DataFrame({ 'file': os.listdir('/content/drive/My Drive/plant_seedlings/plant_seedling_extract/test'), 'species': preds_classes })
submission.to_csv('test_classification_results.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11446930033317468, 'fraction_numerical': 0.015706806282722512, 'mean_word_length': 4.829403606102635, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '661393', 'license': 'MIT', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1370, 'n_words': 285, 'path': 'vehicle_classification.ipynb', 'repo_name': 'HiteshAI/Projects', 'size': '4202'}