{"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":"import os\nos.environ[\"WANDB_DISABLED\"] = \"true\"","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:59:39.192858Z","iopub.execute_input":"2022-07-13T12:59:39.193569Z","iopub.status.idle":"2022-07-13T12:59:39.206534Z","shell.execute_reply.started":"2022-07-13T12:59:39.193482Z","shell.execute_reply":"2022-07-13T12:59:39.204549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install datasets -q\n!pip install transformers -q","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-13T12:59:39.209791Z","iopub.execute_input":"2022-07-13T12:59:39.210112Z","iopub.status.idle":"2022-07-13T13:00:00.105863Z","shell.execute_reply.started":"2022-07-13T12:59:39.210085Z","shell.execute_reply":"2022-07-13T13:00:00.104708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datasets import load_dataset\ndataset = load_dataset(\"imagefolder\", data_dir=\"../input/paddy-disease-classification/train_images/\")\ndataset[\"train\"][0][\"image\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:00.109635Z","iopub.execute_input":"2022-07-13T13:00:00.109934Z","iopub.status.idle":"2022-07-13T13:00:14.580399Z","shell.execute_reply.started":"2022-07-13T13:00:00.109907Z","shell.execute_reply":"2022-07-13T13:00:14.579377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = dataset['train']\n\ndataset = dataset.train_test_split(test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:14.582993Z","iopub.execute_input":"2022-07-13T13:00:14.583684Z","iopub.status.idle":"2022-07-13T13:00:14.872515Z","shell.execute_reply.started":"2022-07-13T13:00:14.583644Z","shell.execute_reply":"2022-07-13T13:00:14.871519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:14.874057Z","iopub.execute_input":"2022-07-13T13:00:14.874417Z","iopub.status.idle":"2022-07-13T13:00:14.882271Z","shell.execute_reply.started":"2022-07-13T13:00:14.874381Z","shell.execute_reply":"2022-07-13T13:00:14.881127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nimg_size=224\naug= A.Compose([\n            A.Resize(img_size+32,img_size+32),\n            A.RandomCrop(img_size,img_size),\n            A.HorizontalFlip(0.5),\n            A.VerticalFlip(0.5),\n            A.ShiftScaleRotate(rotate_limit=30),\n            A.Blur(),A.RandomGamma(),\n            A.Sharpen(), A.GaussNoise(),\n            A.CoarseDropout(8,16,16),\n            A.CLAHE(0.5),\n            A.Normalize(),\n            ToTensorV2(p=1.0),\n        ], p=1.0)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:14.883837Z","iopub.execute_input":"2022-07-13T13:00:14.884386Z","iopub.status.idle":"2022-07-13T13:00:16.371721Z","shell.execute_reply.started":"2022-07-13T13:00:14.884348Z","shell.execute_reply":"2022-07-13T13:00:16.370754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def transforms(examples):\n    examples[\"pixel_values\"] = [aug(image=np.array(image.convert(\"RGB\")))['image'] for image in examples[\"image\"]]\n    return examples","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:16.376746Z","iopub.execute_input":"2022-07-13T13:00:16.379239Z","iopub.status.idle":"2022-07-13T13:00:16.387137Z","shell.execute_reply.started":"2022-07-13T13:00:16.379200Z","shell.execute_reply":"2022-07-13T13:00:16.386017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset=dataset.with_transform(transforms)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:16.392461Z","iopub.execute_input":"2022-07-13T13:00:16.394774Z","iopub.status.idle":"2022-07-13T13:00:16.632592Z","shell.execute_reply.started":"2022-07-13T13:00:16.394737Z","shell.execute_reply":"2022-07-13T13:00:16.631671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\n\nimg = dataset['train'][0][\"pixel_values\"]\nplt.imshow(img.permute(1, 2, 0))","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:16.637190Z","iopub.execute_input":"2022-07-13T13:00:16.639480Z","iopub.status.idle":"2022-07-13T13:00:16.893213Z","shell.execute_reply.started":"2022-07-13T13:00:16.639441Z","shell.execute_reply":"2022-07-13T13:00:16.892273Z"},"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] = str(i)\n    id2label[str(i)] = label","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:16.894485Z","iopub.execute_input":"2022-07-13T13:00:16.895320Z","iopub.status.idle":"2022-07-13T13:00:16.902301Z","shell.execute_reply.started":"2022-07-13T13:00:16.895263Z","shell.execute_reply":"2022-07-13T13:00:16.900951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(labels)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:16.904241Z","iopub.execute_input":"2022-07-13T13:00:16.904697Z","iopub.status.idle":"2022-07-13T13:00:16.914157Z","shell.execute_reply.started":"2022-07-13T13:00:16.904639Z","shell.execute_reply":"2022-07-13T13:00:16.913253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import AutoFeatureExtractor\n\nfeature_extractor = AutoFeatureExtractor.from_pretrained(\"google/vit-base-patch16-224-in21k\")","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:16.915779Z","iopub.execute_input":"2022-07-13T13:00:16.916517Z","iopub.status.idle":"2022-07-13T13:00:18.045430Z","shell.execute_reply.started":"2022-07-13T13:00:16.916471Z","shell.execute_reply":"2022-07-13T13:00:18.044472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import AutoModelForImageClassification, TrainingArguments, Trainer\n\nmodel = AutoModelForImageClassification.from_pretrained(\n    \"google/vit-base-patch16-224-in21k\",\n    num_labels=len(labels),\n    id2label=id2label,\n    label2id=label2id,\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:18.050958Z","iopub.execute_input":"2022-07-13T13:00:18.051273Z","iopub.status.idle":"2022-07-13T13:00:21.440578Z","shell.execute_reply.started":"2022-07-13T13:00:18.051248Z","shell.execute_reply":"2022-07-13T13:00:21.439675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datasets import load_metric\n\nmetric = load_metric(\"f1\")\ndef compute_metrics(eval_pred):\n    logits, labels = eval_pred\n    predictions = np.argmax(logits, axis=-1)\n    return metric.compute(predictions=predictions, references=labels,average ='macro')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:21.442066Z","iopub.execute_input":"2022-07-13T13:00:21.442901Z","iopub.status.idle":"2022-07-13T13:00:22.082217Z","shell.execute_reply.started":"2022-07-13T13:00:21.442858Z","shell.execute_reply":"2022-07-13T13:00:22.081276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport transformers\nbatch_size=96\ntraining_args = TrainingArguments(\n    output_dir=\"./results\",\n    per_device_train_batch_size=batch_size,\n    gradient_accumulation_steps=4,\n    per_device_eval_batch_size=batch_size,\n    evaluation_strategy=\"steps\",\n    num_train_epochs=10,\n    fp16=True,\n    save_steps=100,\n    eval_steps=100,\n    logging_steps=10,\n    learning_rate=2e-4,\n    save_total_limit=1,\n    remove_unused_columns=False,\n)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:22.084367Z","iopub.execute_input":"2022-07-13T13:00:22.085047Z","iopub.status.idle":"2022-07-13T13:00:22.129589Z","shell.execute_reply.started":"2022-07-13T13:00:22.085008Z","shell.execute_reply":"2022-07-13T13:00:22.128554Z"},"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-07-13T13:00:22.131022Z","iopub.execute_input":"2022-07-13T13:00:22.132075Z","iopub.status.idle":"2022-07-13T13:00:22.140480Z","shell.execute_reply.started":"2022-07-13T13:00:22.132036Z","shell.execute_reply":"2022-07-13T13:00:22.139458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:22.141744Z","iopub.execute_input":"2022-07-13T13:00:22.143332Z","iopub.status.idle":"2022-07-13T13:00:22.152459Z","shell.execute_reply.started":"2022-07-13T13:00:22.143296Z","shell.execute_reply":"2022-07-13T13:00:22.151241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer = Trainer(\n    model=model,\n    args=training_args,\n    data_collator=collate_fn,\n    train_dataset=dataset[\"train\"],\n    eval_dataset=dataset[\"test\"],\n    tokenizer=feature_extractor,\n    compute_metrics=compute_metrics,\n    \n\n)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:22.154046Z","iopub.execute_input":"2022-07-13T13:00:22.154838Z","iopub.status.idle":"2022-07-13T13:00:24.080746Z","shell.execute_reply.started":"2022-07-13T13:00:22.154801Z","shell.execute_reply":"2022-07-13T13:00:24.079804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.train()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:00:24.083682Z","iopub.execute_input":"2022-07-13T13:00:24.084351Z","iopub.status.idle":"2022-07-13T13:11:44.424640Z","shell.execute_reply.started":"2022-07-13T13:00:24.084314Z","shell.execute_reply":"2022-07-13T13:11:44.420214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[\"test\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:11:49.915169Z","iopub.execute_input":"2022-07-13T13:11:49.915851Z","iopub.status.idle":"2022-07-13T13:11:49.922477Z","shell.execute_reply.started":"2022-07-13T13:11:49.915812Z","shell.execute_reply":"2022-07-13T13:11:49.921426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.predict(dataset[\"test\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:12:00.206134Z","iopub.execute_input":"2022-07-13T13:12:00.206807Z","iopub.status.idle":"2022-07-13T13:12:40.351612Z","shell.execute_reply.started":"2022-07-13T13:12:00.206767Z","shell.execute_reply":"2022-07-13T13:12:40.350714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from glob import glob\nfrom datasets import load_dataset, Image,Dataset,DatasetDict\n\n# test_images=sorted(glob('../input/paddy-disease-classification/test_images/*.jpg'))\n\n# test_dataset = Dataset.from_dict({\"image\": test_images,\"label\":[0]*len(test_images)}).cast_column(\"image\", Image())\n# test_dataset","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:12:40.353451Z","iopub.execute_input":"2022-07-13T13:12:40.354080Z","iopub.status.idle":"2022-07-13T13:12:40.358997Z","shell.execute_reply.started":"2022-07-13T13:12:40.354040Z","shell.execute_reply":"2022-07-13T13:12:40.358095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf=pd.read_csv('../input/paddy-disease-classification/sample_submission.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:12:40.360592Z","iopub.execute_input":"2022-07-13T13:12:40.361297Z","iopub.status.idle":"2022-07-13T13:12:40.382522Z","shell.execute_reply.started":"2022-07-13T13:12:40.361247Z","shell.execute_reply":"2022-07-13T13:12:40.381708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df=df.copy()\ntest_df.image_id='../input/paddy-disease-classification/test_images/'+test_df.image_id\ntest_df.label=0#just for dummy purpose\ntest_df.rename({'image_id':'image'},axis=1,inplace=True)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:12:40.385041Z","iopub.execute_input":"2022-07-13T13:12:40.385394Z","iopub.status.idle":"2022-07-13T13:12:40.400142Z","shell.execute_reply.started":"2022-07-13T13:12:40.385359Z","shell.execute_reply":"2022-07-13T13:12:40.399335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.image[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:12:40.401514Z","iopub.execute_input":"2022-07-13T13:12:40.401837Z","iopub.status.idle":"2022-07-13T13:12:40.408978Z","shell.execute_reply.started":"2022-07-13T13:12:40.401803Z","shell.execute_reply":"2022-07-13T13:12:40.407922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = Dataset.from_pandas(test_df).cast_column(\"image\", Image())","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:12:40.410701Z","iopub.execute_input":"2022-07-13T13:12:40.411437Z","iopub.status.idle":"2022-07-13T13:12:40.422671Z","shell.execute_reply.started":"2022-07-13T13:12:40.411400Z","shell.execute_reply":"2022-07-13T13:12:40.421695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.set_transform(transforms)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:12:40.425359Z","iopub.execute_input":"2022-07-13T13:12:40.425652Z","iopub.status.idle":"2022-07-13T13:12:40.430836Z","shell.execute_reply.started":"2022-07-13T13:12:40.425628Z","shell.execute_reply":"2022-07-13T13:12:40.429750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds=trainer.predict(test_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:12:40.432268Z","iopub.execute_input":"2022-07-13T13:12:40.432938Z","iopub.status.idle":"2022-07-13T13:13:46.026380Z","shell.execute_reply.started":"2022-07-13T13:12:40.432898Z","shell.execute_reply":"2022-07-13T13:13:46.025496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=np.argmax(preds.predictions,1)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:13:46.027725Z","iopub.execute_input":"2022-07-13T13:13:46.028390Z","iopub.status.idle":"2022-07-13T13:13:46.033722Z","shell.execute_reply.started":"2022-07-13T13:13:46.028352Z","shell.execute_reply":"2022-07-13T13:13:46.032677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=[id2label[str(i)] for i in pred]","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:13:46.038059Z","iopub.execute_input":"2022-07-13T13:13:46.038441Z","iopub.status.idle":"2022-07-13T13:13:46.047150Z","shell.execute_reply.started":"2022-07-13T13:13:46.038407Z","shell.execute_reply":"2022-07-13T13:13:46.046173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.label=pred","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:13:46.048630Z","iopub.execute_input":"2022-07-13T13:13:46.049075Z","iopub.status.idle":"2022-07-13T13:13:46.057370Z","shell.execute_reply.started":"2022-07-13T13:13:46.049032Z","shell.execute_reply":"2022-07-13T13:13:46.056241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:13:46.058688Z","iopub.execute_input":"2022-07-13T13:13:46.059146Z","iopub.status.idle":"2022-07-13T13:13:46.071648Z","shell.execute_reply.started":"2022-07-13T13:13:46.059091Z","shell.execute_reply":"2022-07-13T13:13:46.070239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T13:13:46.073236Z","iopub.execute_input":"2022-07-13T13:13:46.073834Z","iopub.status.idle":"2022-07-13T13:13:46.088635Z","shell.execute_reply.started":"2022-07-13T13:13:46.073799Z","shell.execute_reply":"2022-07-13T13:13:46.087833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}