{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from fastai.vision.all import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('/kaggle/input/cassava-leaf-disease-classification/')\npath.ls()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(path/'train.csv')\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open(path/'label_num_to_disease_map.json') as f:\n    label_dict = json.load(f)\n\nlabel_dict = {int(k):v for k,v in label_dict.items()}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df['label'].value_counts(normalize=True).plot(kind='barh')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import StratifiedKFold\n\ncv = StratifiedKFold(n_splits=5, random_state=42, shuffle=True)\nsplits = {}\n\nfor i,(tr_idx,val_idx) in enumerate(cv.split(train_df, train_df['label'])):\n    splits[i] = [tr_idx, val_idx]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = train_df.set_index('image_id')\nlabels = df.to_dict()['label']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\nlabels['1000723321.jpg']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_label(labels, x):\n    x = Path(x)\n    return labels[x.name]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fn = train_df.iloc[0,0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\nget_label(labels,path/'train_images'/fn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_splits(FOLD, x):\n    return splits[FOLD]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_data(labels,split,bs=64, presize=500, resize=384):\n    tfms = [Rotate(90), Warp(magnitude=0.4, p=1.),Zoom(min_zoom=0.9, max_zoom=1.3), Brightness(max_lighting=0.5), Flip(), Contrast(), Resize(resize)]\n    comp = setup_aug_tfms(tfms)\n    return DataBlock(blocks=(ImageBlock, CategoryBlock),\n        get_items=lambda p: get_image_files(p),\n        get_y=partial(get_label, labels),\n        splitter=partial(get_splits, split),\n        item_tfms=[Resize(presize)],\n        batch_tfms=[*comp, Normalize.from_stats(*imagenet_stats)]).dataloaders(path/'train_images',bs=bs,num_workers=8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"N_FOLDS=5","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.makedirs('/root/.cache/torch/hub/checkpoints/', exist_ok=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp /kaggle/input/fastai-pretrained/resnet50-19c8e357.pth /root/.cache/torch/hub/checkpoints/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for split in range(N_FOLDS):\n    dls = get_data(labels,split,bs=64)\n    learn = cnn_learner(dls, models.resnet50, metrics=[accuracy]).to_fp16()\n    learn.freeze()\n    # learn.lr_find()\n    learn.fit_one_cycle(10, 5e-3, cbs=[MixUp()])\n    learn.save(f'resnet50-frozen-fold_{split}')\n    learn.unfreeze()\n    learn.fit_one_cycle(10, slice(1e-5, 1e-4), cbs=[MixUp()])\n    learn.save(f'resnet50-full-fold_{split}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}