{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"model_checkpoint = \"google/vit-base-patch16-224-in21k\" # pre-trained model from which to fine-tune\nbatch_size = 8 # batch size for training and evaluation","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:30:12.794966Z","iopub.execute_input":"2022-10-21T08:30:12.795377Z","iopub.status.idle":"2022-10-21T08:30:12.800792Z","shell.execute_reply.started":"2022-10-21T08:30:12.795334Z","shell.execute_reply":"2022-10-21T08:30:12.799383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q  datasets transformers","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:30:22.438674Z","iopub.execute_input":"2022-10-21T08:30:22.439366Z","iopub.status.idle":"2022-10-21T08:30:36.497556Z","shell.execute_reply.started":"2022-10-21T08:30:22.439330Z","shell.execute_reply":"2022-10-21T08:30:36.496249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport cv2\nfrom datasets import load_dataset\nfrom datasets import load_metric\n","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:30:36.503384Z","iopub.execute_input":"2022-10-21T08:30:36.505670Z","iopub.status.idle":"2022-10-21T08:30:37.679426Z","shell.execute_reply.started":"2022-10-21T08:30:36.505628Z","shell.execute_reply":"2022-10-21T08:30:37.678428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alldf = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:30:37.758456Z","iopub.execute_input":"2022-10-21T08:30:37.765648Z","iopub.status.idle":"2022-10-21T08:30:37.832097Z","shell.execute_reply.started":"2022-10-21T08:30:37.765609Z","shell.execute_reply":"2022-10-21T08:30:37.830908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_dataset():\n    subdir = {0 : 'CBB', 1 : 'CBSD', 2 : 'CGM', 3 : 'CMD', 4 : 'H'}\n    for index, row in alldf.iterrows():\n        label = row['label']\n        image_name = row['image_id']\n        img_path =  '/kaggle/input/cassava-leaf-disease-classification/train_images/' + image_name\n        image = cv2.imread(img_path)\n        write_path = './dataset/' + subdir[label] + '/' + image_name\n        cv2.imwrite(write_path, image)\n# '''\n!mkdir './dataset'\n!mkdir './dataset/CBB'\n!mkdir './dataset/CBSD'\n!mkdir './dataset/CGM'\n!mkdir './dataset/CMD'\n!mkdir './dataset/H'\nprepare_dataset()\n# '''\n# !ls './CBB/'  | wc -l","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:30:37.833370Z","iopub.execute_input":"2022-10-21T08:30:37.833788Z","iopub.status.idle":"2022-10-21T08:39:11.297066Z","shell.execute_reply.started":"2022-10-21T08:30:37.833741Z","shell.execute_reply":"2022-10-21T08:39:11.295820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = load_dataset(\"imagefolder\", data_dir='/kaggle/working/dataset/')\ndataset","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:39:11.299014Z","iopub.execute_input":"2022-10-21T08:39:11.299787Z","iopub.status.idle":"2022-10-21T08:39:47.226196Z","shell.execute_reply.started":"2022-10-21T08:39:11.299739Z","shell.execute_reply":"2022-10-21T08:39:47.225138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metric = load_metric(\"accuracy\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:41:40.245487Z","iopub.execute_input":"2022-10-21T08:41:40.245905Z","iopub.status.idle":"2022-10-21T08:41:41.825686Z","shell.execute_reply.started":"2022-10-21T08:41:40.245874Z","shell.execute_reply":"2022-10-21T08:41:41.824730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['train'].features","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:41:43.350176Z","iopub.execute_input":"2022-10-21T08:41:43.352132Z","iopub.status.idle":"2022-10-21T08:41:43.360480Z","shell.execute_reply.started":"2022-10-21T08:41:43.352086Z","shell.execute_reply":"2022-10-21T08:41:43.359566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['train'][0]['image'].resize((100,100))","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:41:46.031205Z","iopub.execute_input":"2022-10-21T08:41:46.031554Z","iopub.status.idle":"2022-10-21T08:41:46.067987Z","shell.execute_reply.started":"2022-10-21T08:41:46.031524Z","shell.execute_reply":"2022-10-21T08:41:46.067094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[\"train\"].features[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:41:49.432163Z","iopub.execute_input":"2022-10-21T08:41:49.432528Z","iopub.status.idle":"2022-10-21T08:41:49.439041Z","shell.execute_reply.started":"2022-10-21T08:41:49.432499Z","shell.execute_reply":"2022-10-21T08:41:49.438102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = dataset[\"train\"].features[\"label\"].names\nlabel2id, id2label = dict(), dict()\nfor i, label in enumerate(labels):\n    label2id[label] = i\n    id2label[i] = label\n\nid2label[2]","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:41:51.457000Z","iopub.execute_input":"2022-10-21T08:41:51.457659Z","iopub.status.idle":"2022-10-21T08:41:51.465467Z","shell.execute_reply.started":"2022-10-21T08:41:51.457623Z","shell.execute_reply":"2022-10-21T08:41:51.464311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import AutoFeatureExtractor\n\nfeature_extractor = AutoFeatureExtractor.from_pretrained(model_checkpoint)\nfeature_extractor","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:41:54.053083Z","iopub.execute_input":"2022-10-21T08:41:54.053438Z","iopub.status.idle":"2022-10-21T08:41:56.401903Z","shell.execute_reply.started":"2022-10-21T08:41:54.053408Z","shell.execute_reply":"2022-10-21T08:41:56.400956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.transforms import (\n    CenterCrop,\n    Compose,\n    Normalize,\n    RandomHorizontalFlip,\n    RandomResizedCrop,\n    Resize,\n    ToTensor,\n)\n\nnormalize = Normalize(mean=feature_extractor.image_mean, std=feature_extractor.image_std)\ntrain_transforms = Compose(\n        [\n            RandomResizedCrop(feature_extractor.size),\n            RandomHorizontalFlip(),\n            ToTensor(),\n            normalize,\n        ]\n    )\n\nval_transforms = Compose(\n        [\n            Resize(feature_extractor.size),\n            CenterCrop(feature_extractor.size),\n            ToTensor(),\n            normalize,\n        ]\n    )\n\ndef preprocess_train(example_batch):\n    \"\"\"Apply train_transforms across a batch.\"\"\"\n    example_batch[\"pixel_values\"] = [\n        train_transforms(image.convert(\"RGB\")) for image in example_batch[\"image\"]\n    ]\n    return example_batch\n\ndef preprocess_val(example_batch):\n    \"\"\"Apply val_transforms across a batch.\"\"\"\n    example_batch[\"pixel_values\"] = [val_transforms(image.convert(\"RGB\")) for image in example_batch[\"image\"]]\n    return example_batch","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:42:01.371890Z","iopub.execute_input":"2022-10-21T08:42:01.372605Z","iopub.status.idle":"2022-10-21T08:42:03.245821Z","shell.execute_reply.started":"2022-10-21T08:42:01.372568Z","shell.execute_reply":"2022-10-21T08:42:03.244786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split up training into training + validation\nsplits = dataset[\"train\"].train_test_split(test_size=0.2)\ntrain_ds = splits['train']\nval_ds = splits['test']","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:42:05.147498Z","iopub.execute_input":"2022-10-21T08:42:05.149152Z","iopub.status.idle":"2022-10-21T08:42:05.693209Z","shell.execute_reply.started":"2022-10-21T08:42:05.149106Z","shell.execute_reply":"2022-10-21T08:42:05.692222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds.set_transform(preprocess_train)\nval_ds.set_transform(preprocess_val)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:42:09.501809Z","iopub.execute_input":"2022-10-21T08:42:09.502360Z","iopub.status.idle":"2022-10-21T08:42:09.510422Z","shell.execute_reply.started":"2022-10-21T08:42:09.502322Z","shell.execute_reply":"2022-10-21T08:42:09.509548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds[0]","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:42:17.627560Z","iopub.execute_input":"2022-10-21T08:42:17.627961Z","iopub.status.idle":"2022-10-21T08:42:17.719305Z","shell.execute_reply.started":"2022-10-21T08:42:17.627926Z","shell.execute_reply":"2022-10-21T08:42:17.718298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import AutoModelForImageClassification, TrainingArguments, Trainer\n\nmodel = AutoModelForImageClassification.from_pretrained(\n    model_checkpoint, \n    label2id=label2id,\n    id2label=id2label,\n    ignore_mismatched_sizes = True, # provide this in case you're planning to fine-tune an already fine-tuned checkpoint\n)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:42:22.606696Z","iopub.execute_input":"2022-10-21T08:42:22.607765Z","iopub.status.idle":"2022-10-21T08:42:35.725113Z","shell.execute_reply.started":"2022-10-21T08:42:22.607720Z","shell.execute_reply":"2022-10-21T08:42:35.724081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### using huggingface to push to hub","metadata":{}},{"cell_type":"code","source":"from huggingface_hub import notebook_login\n\nnotebook_login()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:42:35.727882Z","iopub.execute_input":"2022-10-21T08:42:35.728687Z","iopub.status.idle":"2022-10-21T08:42:35.787729Z","shell.execute_reply.started":"2022-10-21T08:42:35.728640Z","shell.execute_reply":"2022-10-21T08:42:35.786773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n!sudo apt -qq install git-lfs\n!git config --global credential.helper store","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:44:04.790857Z","iopub.execute_input":"2022-10-21T08:44:04.792116Z","iopub.status.idle":"2022-10-21T08:44:12.419132Z","shell.execute_reply.started":"2022-10-21T08:44:04.792067Z","shell.execute_reply":"2022-10-21T08:44:12.417774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_name = model_checkpoint.split(\"/\")[-1]\n\nargs = TrainingArguments(\n    f\"{model_name}-finetuned-cassava\",\n    remove_unused_columns=False,\n    evaluation_strategy = \"epoch\",\n    save_strategy = \"epoch\",\n    learning_rate=5e-5,\n    per_device_train_batch_size=batch_size,\n    gradient_accumulation_steps=4,\n    per_device_eval_batch_size=batch_size,\n    num_train_epochs=10,\n    warmup_ratio=0.1,\n    logging_steps=10,\n    load_best_model_at_end=True,\n    metric_for_best_model=\"accuracy\",\n    push_to_hub=True,\n)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:44:12.421944Z","iopub.execute_input":"2022-10-21T08:44:12.422357Z","iopub.status.idle":"2022-10-21T08:44:12.496025Z","shell.execute_reply.started":"2022-10-21T08:44:12.422315Z","shell.execute_reply":"2022-10-21T08:44:12.495061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\n# the compute_metrics function takes a Named Tuple as input:\n# predictions, which are the logits of the model as Numpy arrays,\n# and label_ids, which are the ground-truth labels as Numpy arrays.\ndef compute_metrics(eval_pred):\n    \"\"\"Computes accuracy on a batch of predictions\"\"\"\n    predictions = np.argmax(eval_pred.predictions, axis=1)\n    return metric.compute(predictions=predictions, references=eval_pred.label_ids)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:44:17.220314Z","iopub.execute_input":"2022-10-21T08:44:17.220682Z","iopub.status.idle":"2022-10-21T08:44:17.228275Z","shell.execute_reply.started":"2022-10-21T08:44:17.220652Z","shell.execute_reply":"2022-10-21T08:44:17.226012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\ndef collate_fn(examples):\n    pixel_values = torch.stack([example[\"pixel_values\"] for example in examples])\n    labels = torch.tensor([example[\"label\"] for example in examples])\n    return {\"pixel_values\": pixel_values, \"labels\": labels}","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:44:20.033473Z","iopub.execute_input":"2022-10-21T08:44:20.034510Z","iopub.status.idle":"2022-10-21T08:44:20.039949Z","shell.execute_reply.started":"2022-10-21T08:44:20.034469Z","shell.execute_reply":"2022-10-21T08:44:20.038908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer = Trainer(\n    model,\n    args,\n    train_dataset=train_ds,\n    eval_dataset=val_ds,\n    tokenizer=feature_extractor,\n    compute_metrics=compute_metrics,\n    data_collator=collate_fn,\n)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:44:22.053027Z","iopub.execute_input":"2022-10-21T08:44:22.055564Z","iopub.status.idle":"2022-10-21T08:46:16.036552Z","shell.execute_reply.started":"2022-10-21T08:44:22.055527Z","shell.execute_reply":"2022-10-21T08:46:16.035047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_results = trainer.train()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:46:16.038929Z","iopub.execute_input":"2022-10-21T08:46:16.039325Z","iopub.status.idle":"2022-10-21T10:45:23.334077Z","shell.execute_reply.started":"2022-10-21T08:46:16.039280Z","shell.execute_reply":"2022-10-21T10:45:23.332102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rest is optional but nice to have\ntrainer.save_model()\ntrainer.log_metrics(\"train\", train_results.metrics)\ntrainer.save_metrics(\"train\", train_results.metrics)\ntrainer.save_state()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T10:49:20.633802Z","iopub.execute_input":"2022-10-21T10:49:20.634166Z","iopub.status.idle":"2022-10-21T10:49:25.876337Z","shell.execute_reply.started":"2022-10-21T10:49:20.634133Z","shell.execute_reply":"2022-10-21T10:49:25.874138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics = trainer.evaluate()\n# some nice to haves:\ntrainer.log_metrics(\"eval\", metrics)\ntrainer.save_metrics(\"eval\", metrics)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T10:49:25.878113Z","iopub.execute_input":"2022-10-21T10:49:25.878495Z","iopub.status.idle":"2022-10-21T10:50:58.384090Z","shell.execute_reply.started":"2022-10-21T10:49:25.878453Z","shell.execute_reply":"2022-10-21T10:50:58.382907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.push_to_hub()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T10:50:58.385535Z","iopub.execute_input":"2022-10-21T10:50:58.386164Z","iopub.status.idle":"2022-10-21T10:51:21.588380Z","shell.execute_reply.started":"2022-10-21T10:50:58.386127Z","shell.execute_reply":"2022-10-21T10:51:21.587302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import pipeline\n\npipe = pipeline(\"image-classification\", \"siddharth963/\" + model_checkpoint.split(\"/\")[-1] + \"-finetuned-cassava\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T11:11:45.854386Z","iopub.execute_input":"2022-10-21T11:11:45.854863Z","iopub.status.idle":"2022-10-21T11:12:03.119777Z","shell.execute_reply.started":"2022-10-21T11:11:45.854803Z","shell.execute_reply":"2022-10-21T11:12:03.118509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### prediction example","metadata":{}},{"cell_type":"code","source":"example = dataset['train'][0]","metadata":{"execution":{"iopub.status.busy":"2022-10-21T11:12:03.123791Z","iopub.execute_input":"2022-10-21T11:12:03.124154Z","iopub.status.idle":"2022-10-21T11:12:03.146039Z","shell.execute_reply.started":"2022-10-21T11:12:03.124122Z","shell.execute_reply":"2022-10-21T11:12:03.144987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipe(example['image'])","metadata":{"execution":{"iopub.status.busy":"2022-10-21T11:12:03.147850Z","iopub.execute_input":"2022-10-21T11:12:03.148275Z","iopub.status.idle":"2022-10-21T11:12:03.986275Z","shell.execute_reply.started":"2022-10-21T11:12:03.148233Z","shell.execute_reply":"2022-10-21T11:12:03.985201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example['label']","metadata":{"execution":{"iopub.status.busy":"2022-10-21T11:12:06.180261Z","iopub.execute_input":"2022-10-21T11:12:06.182089Z","iopub.status.idle":"2022-10-21T11:12:06.190542Z","shell.execute_reply.started":"2022-10-21T11:12:06.182033Z","shell.execute_reply":"2022-10-21T11:12:06.189434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !ls './vit-base-patch16-224-in21k-finetuned-cassava' | wc -l\n# from IPython.display import FileLinks\n# FileLinks('./vit-base-patch16-224-in21k-finetuned-cassava') # input argument is specified folder","metadata":{"execution":{"iopub.status.busy":"2022-10-12T19:39:25.384843Z","iopub.execute_input":"2022-10-12T19:39:25.385236Z","iopub.status.idle":"2022-10-12T19:39:25.393259Z","shell.execute_reply.started":"2022-10-12T19:39:25.385202Z","shell.execute_reply":"2022-10-12T19:39:25.392149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !ls wandb | wc -l","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}