{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n\"\"\"\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\"\"\"\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-15T13:42:42.468853Z","iopub.execute_input":"2021-08-15T13:42:42.469228Z","iopub.status.idle":"2021-08-15T13:42:42.47576Z","shell.execute_reply.started":"2021-08-15T13:42:42.46919Z","shell.execute_reply":"2021-08-15T13:42:42.474581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install pytorch","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:42:42.47722Z","iopub.execute_input":"2021-08-15T13:42:42.477485Z","iopub.status.idle":"2021-08-15T13:42:42.492469Z","shell.execute_reply.started":"2021-08-15T13:42:42.477459Z","shell.execute_reply":"2021-08-15T13:42:42.491535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision.models as models\nimport torchvision\nfrom collections import Counter\nfrom distutils.dir_util import copy_tree\nimport matplotlib.pyplot as plt\nfrom pathlib import Path\n\nfrom torchvision import transforms,datasets\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:42:42.494041Z","iopub.execute_input":"2021-08-15T13:42:42.494358Z","iopub.status.idle":"2021-08-15T13:42:42.504584Z","shell.execute_reply.started":"2021-08-15T13:42:42.494327Z","shell.execute_reply":"2021-08-15T13:42:42.503649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.resnet18(pretrained=False)\nmodel.fc=torch.nn.Linear(512,5)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:42:42.50619Z","iopub.execute_input":"2021-08-15T13:42:42.506807Z","iopub.status.idle":"2021-08-15T13:42:42.734702Z","shell.execute_reply.started":"2021-08-15T13:42:42.50677Z","shell.execute_reply":"2021-08-15T13:42:42.733564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"fromDirectory = \"../input/aptos2019-blindness-detection/train_images/\"\ntoDirectory = \"./train_images/\"\n#without output\n_=copy_tree(fromDirectory, toDirectory)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T15:17:26.835984Z","iopub.execute_input":"2021-08-14T15:17:26.836328Z","iopub.status.idle":"2021-08-14T15:19:14.868255Z","shell.execute_reply.started":"2021-08-14T15:17:26.836283Z","shell.execute_reply":"2021-08-14T15:19:14.866899Z"}}},{"cell_type":"code","source":"transformed_dir=\"./train_images/\"\nPath(transformed_dir).mkdir(parents=True, exist_ok=True)\ntrain_dir=\"../input/aptos2019-blindness-detection/train_images/\"\ntest_dir=\"../input/aptos2019-blindness-detection/test_images/\"\ndf=pd.read_csv(\"../input/aptos2019-blindness-detection/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:42:42.735819Z","iopub.execute_input":"2021-08-15T13:42:42.736095Z","iopub.status.idle":"2021-08-15T13:42:42.747779Z","shell.execute_reply.started":"2021-08-15T13:42:42.736052Z","shell.execute_reply":"2021-08-15T13:42:42.746737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = torch.optim.Adam(model.parameters(),lr = 1e-3)\n#loss = torch.nn.CrossEntropyLoss()\nloss=torch.nn.MSELoss()\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\nmodel=model.to(device)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:42:42.755675Z","iopub.execute_input":"2021-08-15T13:42:42.756164Z","iopub.status.idle":"2021-08-15T13:42:42.769147Z","shell.execute_reply.started":"2021-08-15T13:42:42.756133Z","shell.execute_reply":"2021-08-15T13:42:42.768269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Counter(list(df.diagnosis))","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:42:42.770747Z","iopub.execute_input":"2021-08-15T13:42:42.771259Z","iopub.status.idle":"2021-08-15T13:42:42.781321Z","shell.execute_reply.started":"2021-08-15T13:42:42.771227Z","shell.execute_reply":"2021-08-15T13:42:42.780262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diagnosis_count=Counter(list(df.diagnosis))\nmax_diagnosis_count=max(diagnosis_count.values())\nprint(max_diagnosis_count)\n#what about int(round(...))?\ntransforms_count={i:max_diagnosis_count//diagnosis_count[i]-1 for i in diagnosis_count.keys()}","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:42:42.782518Z","iopub.execute_input":"2021-08-15T13:42:42.782864Z","iopub.status.idle":"2021-08-15T13:42:42.795183Z","shell.execute_reply.started":"2021-08-15T13:42:42.782833Z","shell.execute_reply":"2021-08-15T13:42:42.794305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transforms_count","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:42:42.796461Z","iopub.execute_input":"2021-08-15T13:42:42.796847Z","iopub.status.idle":"2021-08-15T13:42:42.811608Z","shell.execute_reply.started":"2021-08-15T13:42:42.796817Z","shell.execute_reply":"2021-08-15T13:42:42.810121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def return_n_transformed_images(orig_img,n:int):\n    train_transforms = transforms.Compose([\n    transforms.RandomHorizontalFlip(),\n    #transforms.RandomVerticalFlip(),\n    #transforms.RandomAutocontrast(),\n    transforms.ColorJitter(brightness=(0.7,1.0), contrast=(0.7,1.0)),\n    transforms.RandomApply([transforms.GaussianBlur(kernel_size=(5, 9), sigma=(0.1, 5))])\n])\n    return [train_transforms(orig_img) for i in range(n)]","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:42:42.813321Z","iopub.execute_input":"2021-08-15T13:42:42.813656Z","iopub.status.idle":"2021-08-15T13:42:42.820848Z","shell.execute_reply.started":"2021-08-15T13:42:42.813622Z","shell.execute_reply":"2021-08-15T13:42:42.819764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def return_n_transformed_images_stub(orig_img,n:int):\n    train_transforms = transforms.Compose([\n    transforms.RandomHorizontalFlip(p=0.0),\n])\n    return [train_transforms(orig_img) for i in range(n)]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"image_path=os.path.join(\"../input/aptos2019-blindness-detection/train_images/000c1434d8d7.png\")\nimg = cv2.cvtColor(cv2.imread(image_path),cv2.COLOR_BGR2RGB)\nimg=transforms.ToTensor()(img)\ntr_imgs=return_n_transformed_images(img,1)\ntransforms.ToPILImage()(tr_imgs[0])","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:42:42.821948Z","iopub.execute_input":"2021-08-15T13:42:42.822227Z","iopub.status.idle":"2021-08-15T13:42:45.806312Z","shell.execute_reply.started":"2021-08-15T13:42:42.8222Z","shell.execute_reply":"2021-08-15T13:42:45.805136Z"}}},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:42:45.808066Z","iopub.execute_input":"2021-08-15T13:42:45.808614Z","iopub.status.idle":"2021-08-15T13:42:45.821296Z","shell.execute_reply.started":"2021-08-15T13:42:45.808578Z","shell.execute_reply":"2021-08-15T13:42:45.820497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fill df\nc=len(df)-1\nouts=[]\nfor i in df.values:\n    image_id,diagnosis=i\n    image_path=os.path.join(train_dir,image_id+\".png\")\n    img = cv2.cvtColor(cv2.imread(image_path),cv2.COLOR_BGR2RGB)\n    img=transforms.ToTensor()(img)\n    img.to(device)\n    for transformed_image in return_n_transformed_images_stub(img,transforms_count[diagnosis]):\n        transformed_image.to(device)\n        c+=1\n        pil = transforms.ToPILImage()(transformed_image)\n        file_path=os.path.join(transformed_dir,str(c)+\".png\")\n        if c%10==0:\n            print(c,end=\" \")\n        pil.save(file_path)\n        outs.append([file_path,diagnosis])","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:42:45.82271Z","iopub.execute_input":"2021-08-15T13:42:45.823191Z","iopub.status.idle":"2021-08-15T13:44:15.946029Z","shell.execute_reply.started":"2021-08-15T13:42:45.823155Z","shell.execute_reply":"2021-08-15T13:44:15.944253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug_only_df=pd.DataFrame(outs,columns=[\"id_code\",\"diagnosis\"],index=list(map(lambda x:x[0],outs)))","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:44:22.64405Z","iopub.execute_input":"2021-08-15T13:44:22.644562Z","iopub.status.idle":"2021-08-15T13:44:22.650804Z","shell.execute_reply.started":"2021-08-15T13:44:22.644527Z","shell.execute_reply":"2021-08-15T13:44:22.650106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:44:22.829484Z","iopub.execute_input":"2021-08-15T13:44:22.829823Z","iopub.status.idle":"2021-08-15T13:44:22.836253Z","shell.execute_reply.started":"2021-08-15T13:44:22.82979Z","shell.execute_reply":"2021-08-15T13:44:22.83519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug_only_df=pd.DataFrame(outs,columns=[\"id_code\",\"diagnosis\"],index=list(map(lambda x:x[0],outs)))","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:44:23.093756Z","iopub.execute_input":"2021-08-15T13:44:23.094111Z","iopub.status.idle":"2021-08-15T13:44:23.099883Z","shell.execute_reply.started":"2021-08-15T13:44:23.094067Z","shell.execute_reply":"2021-08-15T13:44:23.098794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.id_code=train_dir+df.id_code+\".png\"","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:44:23.373835Z","iopub.execute_input":"2021-08-15T13:44:23.374176Z","iopub.status.idle":"2021-08-15T13:44:23.393567Z","shell.execute_reply.started":"2021-08-15T13:44:23.374145Z","shell.execute_reply":"2021-08-15T13:44:23.392531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:44:23.513703Z","iopub.execute_input":"2021-08-15T13:44:23.514202Z","iopub.status.idle":"2021-08-15T13:44:23.527312Z","shell.execute_reply.started":"2021-08-15T13:44:23.514141Z","shell.execute_reply":"2021-08-15T13:44:23.52657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=df.append(aug_only_df)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:44:23.810403Z","iopub.execute_input":"2021-08-15T13:44:23.810863Z","iopub.status.idle":"2021-08-15T13:44:23.822708Z","shell.execute_reply.started":"2021-08-15T13:44:23.810832Z","shell.execute_reply":"2021-08-15T13:44:23.821937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=df.sample(frac=1)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:44:23.983622Z","iopub.execute_input":"2021-08-15T13:44:23.983953Z","iopub.status.idle":"2021-08-15T13:44:23.989181Z","shell.execute_reply.started":"2021-08-15T13:44:23.983923Z","shell.execute_reply":"2021-08-15T13:44:23.988102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:44:24.235204Z","iopub.execute_input":"2021-08-15T13:44:24.235556Z","iopub.status.idle":"2021-08-15T13:44:24.247664Z","shell.execute_reply.started":"2021-08-15T13:44:24.235526Z","shell.execute_reply":"2021-08-15T13:44:24.246691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.diagnosis.hist(bins=5)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c=0\nfor i in df.values:\n    image_path,diagnosis=i\n    img = cv2.imread(image_path)\n    img=transforms.ToTensor()(img).unsqueeze_(0)/256\n    img=img.to(device)\n    img=return_n_transformed_images(img,1)[0]\n    true_tensor=[0.0,0.0,0.0,0.0,0.0]\n    true_tensor[diagnosis]=1.0\n    true_tensor=torch.Tensor(true_tensor).to(device)\n    optimizer.zero_grad()\n    preds=model.forward(img)\n    loss_value = loss(preds[0], true_tensor)\n    loss_value.backward()\n    optimizer.step()\n    c+=1\n    if c%20==0:\n        print(c, loss_value)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:44:24.504894Z","iopub.execute_input":"2021-08-15T13:44:24.505257Z","iopub.status.idle":"2021-08-15T13:47:52.842833Z","shell.execute_reply.started":"2021-08-15T13:44:24.505221Z","shell.execute_reply":"2021-08-15T13:47:52.84121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(\"../input/aptos2019-blindness-detection/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:47:52.843785Z","iopub.status.idle":"2021-08-15T13:47:52.844167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:47:52.845635Z","iopub.status.idle":"2021-08-15T13:47:52.845996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out=[]\nc=0\nfor i in df.values:\n    image_id=i[0]\n    image_path=os.path.join(test_dir,image_id+\".png\")\n    img = cv2.imread(image_path)\n    img=transforms.ToTensor()(img).unsqueeze_(0)/256\n    img=img.to(device)\n    preds=model.forward(img)\n    max_class=max(preds[0].tolist())\n    max_index=preds[0].tolist().index(max_class)\n    out.append([image_id,max_index])\n    c+=1\n    if c%50==0:\n        print(c)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:47:52.847066Z","iopub.status.idle":"2021-08-15T13:47:52.847433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(out)\ndf.columns=[\"id_code\",\"diagnosis\"]\ndf.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:47:52.848332Z","iopub.status.idle":"2021-08-15T13:47:52.848697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.diagnosis.hist(bins=5)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T13:47:52.849435Z","iopub.status.idle":"2021-08-15T13:47:52.849792Z"},"trusted":true},"execution_count":null,"outputs":[]}]}