{"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":"markdown","source":"# ***HAPPYWHALE-WHALE & DOLPHIN***\n\nIdentify whales 🐳 and dolphins 🐬 by unique characteristic\n\n### **Today! Baseline_PytorchLighting_EfficientNet**","metadata":{}},{"cell_type":"markdown","source":"## **1. Explore data**","metadata":{}},{"cell_type":"code","source":"!ls -l ../input/happy-whale-and-dolphin","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:56:19.330873Z","iopub.execute_input":"2022-02-27T12:56:19.331462Z","iopub.status.idle":"2022-02-27T12:56:20.101240Z","shell.execute_reply.started":"2022-02-27T12:56:19.331410Z","shell.execute_reply":"2022-02-27T12:56:20.100043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Import necessary library\nimport os\nimport json\nimport pandas as pd\nimport seaborn as sn\nimport matplotlib.pyplot as plt\nfrom pprint import pprint\n\nsn.set()","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:57:39.395087Z","iopub.execute_input":"2022-02-27T12:57:39.395411Z","iopub.status.idle":"2022-02-27T12:57:39.401909Z","shell.execute_reply.started":"2022-02-27T12:57:39.395380Z","shell.execute_reply":"2022-02-27T12:57:39.400904Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Download the PytorchLighting Library**","metadata":{}},{"cell_type":"code","source":"!pip install -q effdet \"icevision[all]\" 'lightning-flash[image]'\n# !pip install -q \"pytorch-lightning==1.4.*\"\n!pip uninstall -y wandb","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:57:47.329312Z","iopub.execute_input":"2022-02-27T12:57:47.330341Z","iopub.status.idle":"2022-02-27T12:59:21.683440Z","shell.execute_reply.started":"2022-02-27T12:57:47.330290Z","shell.execute_reply":"2022-02-27T12:59:21.682229Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip download -q effdet \"icevision[all]\" 'lightning-flash[image]' --dest frozen_packages --prefer-binary\n!rm frozen_packages/torch-*\n!ls -l frozen_packages","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:59:21.685824Z","iopub.execute_input":"2022-02-27T12:59:21.686538Z","iopub.status.idle":"2022-02-27T13:01:42.174604Z","shell.execute_reply.started":"2022-02-27T12:59:21.686487Z","shell.execute_reply":"2022-02-27T13:01:42.173345Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Training with Lightning⚡Flash**\n\nFollow the example: https://lightning-flash.readthedocs.io/en/stable/reference/image_classification.html\n\nReference-FullCredit-[https://www.kaggle.com/jirkaborovec/herbarium-eda-baseline-flash-efficientnet](http://)\n\n","metadata":{}},{"cell_type":"code","source":"import torch\n\nimport flash\nfrom flash.core.data.utils import download_data\nfrom flash.image import ImageClassificationData, ImageClassifier","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:01:42.181784Z","iopub.execute_input":"2022-02-27T13:01:42.182430Z","iopub.status.idle":"2022-02-27T13:01:55.318214Z","shell.execute_reply.started":"2022-02-27T13:01:42.182383Z","shell.execute_reply":"2022-02-27T13:01:55.317194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Load Data's**","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv('../input/happy-whale-and-dolphin/train.csv')\ndf_train['path'] = '../input/happy-whale-and-dolphin/train_images/' + train_df['image']\n\npred_df = pd.read_csv('../input/happy-whale-and-dolphin/sample_submission.csv')\npred_df['path'] = '../input/happy-whale-and-dolphin/test_images/' + pred_df['image']","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:12:42.674580Z","iopub.execute_input":"2022-02-27T13:12:42.674904Z","iopub.status.idle":"2022-02-27T13:12:42.786461Z","shell.execute_reply.started":"2022-02-27T13:12:42.674872Z","shell.execute_reply":"2022-02-27T13:12:42.785486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.sample(3)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:12:55.021695Z","iopub.execute_input":"2022-02-27T13:12:55.022011Z","iopub.status.idle":"2022-02-27T13:12:55.047031Z","shell.execute_reply.started":"2022-02-27T13:12:55.021955Z","shell.execute_reply":"2022-02-27T13:12:55.046005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv('../input/happy-whale-and-dolphin/sample_submission.csv')\nsample.sample(3)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:12:55.557833Z","iopub.execute_input":"2022-02-27T13:12:55.558544Z","iopub.status.idle":"2022-02-27T13:12:55.603932Z","shell.execute_reply.started":"2022-02-27T13:12:55.558512Z","shell.execute_reply":"2022-02-27T13:12:55.603033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Build the model and feature selection**","metadata":{}},{"cell_type":"code","source":"datamodule = ImageClassificationData.from_data_frame(\n    input_field=\"image\",\n    target_fields=\"individual_id\",\n    # for simplicity take just half of the data\n    train_data_frame=df_train[:len(df_train) // 2],\n    train_images_root=os.path.join(PATH_DATASET, \"train_images\"),\n    batch_size=128,\n    transform_kwargs={\"image_size\": (224, 224)},\n    num_workers=3,\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:13:34.742648Z","iopub.execute_input":"2022-02-27T13:13:34.743004Z","iopub.status.idle":"2022-02-27T13:13:53.942420Z","shell.execute_reply.started":"2022-02-27T13:13:34.742960Z","shell.execute_reply":"2022-02-27T13:13:53.940066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = ImageClassifier(\n    backbone=\"efficientnet_b0\",\n    num_classes=datamodule.num_classes,\n    pretrained=True,\n    optimizer=\"AdamW\",\n    learning_rate=0.001,\n)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:13:56.540683Z","iopub.execute_input":"2022-02-27T13:13:56.541268Z","iopub.status.idle":"2022-02-27T13:13:58.595801Z","shell.execute_reply.started":"2022-02-27T13:13:56.541221Z","shell.execute_reply":"2022-02-27T13:13:58.594748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Finetune the model**","metadata":{}},{"cell_type":"code","source":"from pytorch_lightning.loggers import CSVLogger\n# from pytorch_lightning.callbacks import StochasticWeightAveraging\n\n# Trainer Args\nGPUS = int(torch.cuda.is_available())  # Set to 1 if GPU is enabled for notebook\n\n# swa = StochasticWeightAveraging(swa_epoch_start=0.6)\nlogger = CSVLogger(save_dir='logs/')\n\ntrainer = flash.Trainer(\n    max_epochs=3,\n    # gradient_clip_val=0.01,\n    gpus=GPUS,\n    precision=16 if GPUS else 32,\n    logger=logger,\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:14:19.807345Z","iopub.execute_input":"2022-02-27T13:14:19.807664Z","iopub.status.idle":"2022-02-27T13:14:19.824398Z","shell.execute_reply.started":"2022-02-27T13:14:19.807633Z","shell.execute_reply":"2022-02-27T13:14:19.823456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.finetune(model, datamodule=datamodule, strategy=\"freeze\")\n\ntrainer.save_checkpoint(\"image_classification_model.pt\")","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:14:27.597038Z","iopub.execute_input":"2022-02-27T13:14:27.597654Z","iopub.status.idle":"2022-02-27T16:04:03.065538Z","shell.execute_reply.started":"2022-02-27T13:14:27.597605Z","shell.execute_reply":"2022-02-27T16:04:03.064386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics = pd.read_csv(f'{trainer.logger.log_dir}/metrics.csv')\ndel metrics[\"step\"]\nmetrics.set_index(\"epoch\", inplace=True)\ndisplay(metrics.dropna(axis=1, how=\"all\").head())\ng = sn.relplot(data=metrics, kind=\"line\")\nplt.gcf().set_size_inches(15, 5)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T16:09:46.329441Z","iopub.execute_input":"2022-02-27T16:09:46.331213Z","iopub.status.idle":"2022-02-27T16:09:47.439308Z","shell.execute_reply.started":"2022-02-27T16:09:46.331159Z","shell.execute_reply":"2022-02-27T16:09:47.438294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = pd.DataFrame(pred_df).set_index(\"path\")\ndisplay(test_images.head())\nprint(f\"inference for {len(test_images)} images\")","metadata":{"execution":{"iopub.status.busy":"2022-02-27T16:17:10.305227Z","iopub.execute_input":"2022-02-27T16:17:10.305575Z","iopub.status.idle":"2022-02-27T16:17:10.325755Z","shell.execute_reply.started":"2022-02-27T16:17:10.305542Z","shell.execute_reply":"2022-02-27T16:17:10.324763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datamodule = ImageClassificationData.from_data_frame(\n    input_field=\"image\",\n    # target_fields=\"category_id\",\n    predict_data_frame=test_images,\n    # for simplicity take just fraction of the data\n    # predict_data_frame=test_images[:len(test_images) // 100],\n    predict_images_root=os.path.join(PATH_DATASET, 'test_images'),\n    batch_size=16,\n    transform_kwargs={\"image_size\": (224, 224)},\n    num_workers=2,\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T16:17:42.434352Z","iopub.execute_input":"2022-02-27T16:17:42.434695Z","iopub.status.idle":"2022-02-27T16:18:04.468219Z","shell.execute_reply.started":"2022-02-27T16:17:42.434656Z","shell.execute_reply":"2022-02-27T16:18:04.467119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\nfor lbs in trainer.predict(model, datamodule=datamodule, output=\"labels\"):\n    # lbs = [torch.argmax(p[\"preds\"].float()).item() for p in preds]\n    predictions += lbs","metadata":{"execution":{"iopub.status.busy":"2022-02-27T16:18:09.214984Z","iopub.execute_input":"2022-02-27T16:18:09.215674Z","iopub.status.idle":"2022-02-27T17:30:39.843491Z","shell.execute_reply.started":"2022-02-27T16:18:09.215636Z","shell.execute_reply":"2022-02-27T17:30:39.842231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Thankyou for visiting guys_ComingSoonNextpart**","metadata":{}}]}