{"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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        #print(os.path.join(dirname, filename))\n        pass\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":"2022-07-04T16:36:41.135788Z","iopub.execute_input":"2022-07-04T16:36:41.136236Z","iopub.status.idle":"2022-07-04T16:36:41.375586Z","shell.execute_reply.started":"2022-07-04T16:36:41.136137Z","shell.execute_reply":"2022-07-04T16:36:41.374608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# procedure:\n* make dats sets\n* make model\n* evaluate model\n* create submission","metadata":{}},{"cell_type":"markdown","source":"# IMPORTS","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport PIL\nfrom fastai.vision.all import *\nimport torch\nimport os\nimport ast\nimport cv2\nimport tqdm\nfrom sklearn.model_selection import train_test_split as tts \nfrom torchvision import transforms, utils\nimport gc\n","metadata":{"execution":{"iopub.status.busy":"2022-07-04T16:36:44.737978Z","iopub.execute_input":"2022-07-04T16:36:44.738667Z","iopub.status.idle":"2022-07-04T16:36:47.772097Z","shell.execute_reply.started":"2022-07-04T16:36:44.738627Z","shell.execute_reply":"2022-07-04T16:36:47.771143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T16:36:47.773792Z","iopub.execute_input":"2022-07-04T16:36:47.774552Z","iopub.status.idle":"2022-07-04T16:36:47.901313Z","shell.execute_reply.started":"2022-07-04T16:36:47.77451Z","shell.execute_reply":"2022-07-04T16:36:47.90044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# visualizing some data","metadata":{}},{"cell_type":"code","source":"class CSV:\n    base=\"../input/quickdraw-doodle-recognition/train_simplified\"\n    total=len(os.listdir(base))\n    rows=510\n    def __init__(self,index):\n        base=self.base\n        self.loc = base+\"/\"+os.listdir(base)[index]\n        self.name = os.listdir(base)[index][:-4]\n        self.csv = pd.read_csv(self.loc).iloc[:self.rows]\n        \n    def __len__(self):\n        return self.csv.shape[0]\n    \n    def loc(self):\n        return self.loc\n    \n    def name(self):\n        return self.name\n    \n    def csv(self):\n        return self.csv","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:24:51.412261Z","iopub.execute_input":"2022-07-04T17:24:51.412696Z","iopub.status.idle":"2022-07-04T17:24:51.433877Z","shell.execute_reply.started":"2022-07-04T17:24:51.412659Z","shell.execute_reply":"2022-07-04T17:24:51.43272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index=11\n\na=CSV(index)\n\nlen(a),\n#a.total\n\na.name\n\n#a.loc\n#a.name\n#a.csv\n\nvis=a.csv","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:24:51.485395Z","iopub.execute_input":"2022-07-04T17:24:51.485841Z","iopub.status.idle":"2022-07-04T17:24:52.18924Z","shell.execute_reply.started":"2022-07-04T17:24:51.485796Z","shell.execute_reply":"2022-07-04T17:24:52.188154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X,y=vis[\"drawing\"].values,vis[\"word\"].values\n#X=X[0].split(']]')\ndel vis,a,index\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:24:52.194674Z","iopub.execute_input":"2022-07-04T17:24:52.197323Z","iopub.status.idle":"2022-07-04T17:24:52.397876Z","shell.execute_reply.started":"2022-07-04T17:24:52.197282Z","shell.execute_reply":"2022-07-04T17:24:52.396961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    print(\"\\n\\n## IMAGE {} ##\\n\\n\".format(y[i]))\n    new = ast.literal_eval(X[i])\n    img= np.zeros((256,256), np.uint8)\n    for t, stroke in enumerate(new):\n        for i in range(len(stroke[0]) - 1):\n            color = 255 - min(t,10) * 13 if True else 255\n            _ = cv2.line(img, (stroke[0][i], stroke[1][i]), (stroke[0][i+1], stroke[1][i+1]), color, 6)\n    plt.imshow(img)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:24:52.402389Z","iopub.execute_input":"2022-07-04T17:24:52.404671Z","iopub.status.idle":"2022-07-04T17:24:53.433949Z","shell.execute_reply.started":"2022-07-04T17:24:52.404632Z","shell.execute_reply":"2022-07-04T17:24:53.433009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Images are overall similar to eachother yet contrastly different in meaning,\nfor example traffic lights are rectangular in shape and are related to rectangles but are labelled as traffic lights","metadata":{}},{"cell_type":"markdown","source":"# Utility function","metadata":{}},{"cell_type":"code","source":"def get_ims(X):\n    ims=[]\n    \n    for i in range(len(X)):\n        new = ast.literal_eval(X[i])\n        img= np.zeros((256,256), np.uint8)\n        \n        for t, stroke in enumerate(new):\n            for i in range(len(stroke[0]) - 1):\n                color = 255 - min(t,10) * 13 if False else 255\n                _ = cv2.line(img, (stroke[0][i], stroke[1][i]), (stroke[0][i+1], stroke[1][i+1]), color, 6)\n        ims.append(img)\n        del _,color,i,t,stroke,img\n        gc.collect()\n    return ims#return imges ","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:24:53.439163Z","iopub.execute_input":"2022-07-04T17:24:53.441677Z","iopub.status.idle":"2022-07-04T17:24:53.453139Z","shell.execute_reply.started":"2022-07-04T17:24:53.441636Z","shell.execute_reply":"2022-07-04T17:24:53.452119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# training data{\"image\":list,\"label\":label}","metadata":{}},{"cell_type":"code","source":"def data(df,index):\n    a=CSV(index)\n    lenn=len(a)\n    label=a.name\n    a=a.csv[\"drawing\"]\n    a=get_ims(a)\n    \n    a=pd.DataFrame({\"image\":a,\"label\":[label for i in range(lenn)]})\n    df = pd.concat([df , a], axis=0,ignore_index=True)\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:24:53.459434Z","iopub.execute_input":"2022-07-04T17:24:53.462191Z","iopub.status.idle":"2022-07-04T17:24:53.472223Z","shell.execute_reply.started":"2022-07-04T17:24:53.462153Z","shell.execute_reply":"2022-07-04T17:24:53.471103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.DataFrame({\"image\":[],\"label\":[]})\nfor i,j in enumerate(tqdm.tqdm(range(340))):\n    df=data(df,j)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:24:53.477365Z","iopub.execute_input":"2022-07-04T17:24:53.480044Z","iopub.status.idle":"2022-07-04T17:25:03.615391Z","shell.execute_reply.started":"2022-07-04T17:24:53.480008Z","shell.execute_reply":"2022-07-04T17:25:03.612904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:44:32.831827Z","iopub.execute_input":"2022-07-03T19:44:32.832309Z","iopub.status.idle":"2022-07-03T19:44:32.838254Z","shell.execute_reply.started":"2022-07-03T19:44:32.832265Z","shell.execute_reply":"2022-07-03T19:44:32.837176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = tts(df[\"image\"], df[\"label\"], test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T16:39:31.384682Z","iopub.status.idle":"2022-07-04T16:39:31.387066Z","shell.execute_reply.started":"2022-07-04T16:39:31.386786Z","shell.execute_reply":"2022-07-04T16:39:31.386815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del df,i,j\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:44:40.724699Z","iopub.execute_input":"2022-07-03T19:44:40.725202Z","iopub.status.idle":"2022-07-03T19:44:40.917627Z","shell.execute_reply.started":"2022-07-03T19:44:40.725158Z","shell.execute_reply":"2022-07-03T19:44:40.915531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_sizes = {\"train\":len(X_train),\"valid\":len(y_train)}","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:44:46.131255Z","iopub.execute_input":"2022-07-03T19:44:46.134849Z","iopub.status.idle":"2022-07-03T19:44:46.143997Z","shell.execute_reply.started":"2022-07-03T19:44:46.134792Z","shell.execute_reply":"2022-07-03T19:44:46.14268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftrain=pd.DataFrame({\"image\":X_train,\"label\":y_train})\ndftest=pd.DataFrame({\"image\":X_test,\"label\":y_test})","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:44:50.854552Z","iopub.execute_input":"2022-07-03T19:44:50.855126Z","iopub.status.idle":"2022-07-03T19:44:50.868853Z","shell.execute_reply.started":"2022-07-03T19:44:50.85508Z","shell.execute_reply":"2022-07-03T19:44:50.86783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dftrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:44:53.720771Z","iopub.execute_input":"2022-07-03T19:44:53.721332Z","iopub.status.idle":"2022-07-03T19:44:53.727223Z","shell.execute_reply.started":"2022-07-03T19:44:53.721283Z","shell.execute_reply":"2022-07-03T19:44:53.726223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dftest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:44:55.438Z","iopub.execute_input":"2022-07-03T19:44:55.438465Z","iopub.status.idle":"2022-07-03T19:44:55.448308Z","shell.execute_reply.started":"2022-07-03T19:44:55.438425Z","shell.execute_reply":"2022-07-03T19:44:55.446256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X_train, X_test, y_train, y_test \ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:00.687207Z","iopub.execute_input":"2022-07-03T19:45:00.687741Z","iopub.status.idle":"2022-07-03T19:45:00.890313Z","shell.execute_reply.started":"2022-07-03T19:45:00.687693Z","shell.execute_reply":"2022-07-03T19:45:00.889072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftrain.image.iloc[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:04.06957Z","iopub.execute_input":"2022-07-03T19:45:04.070108Z","iopub.status.idle":"2022-07-03T19:45:04.081191Z","shell.execute_reply.started":"2022-07-03T19:45:04.070064Z","shell.execute_reply":"2022-07-03T19:45:04.080005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(dftrain.image.iloc[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:05.819224Z","iopub.execute_input":"2022-07-03T19:45:05.819781Z","iopub.status.idle":"2022-07-03T19:45:06.087799Z","shell.execute_reply.started":"2022-07-03T19:45:05.819734Z","shell.execute_reply":"2022-07-03T19:45:06.086805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique=dftrain.label.unique()\nunique1=unique\nunique=enumerate(unique)\nlegend={}\nfor i, j in unique:\n    legend[j]=i\ndel unique\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:08.589201Z","iopub.execute_input":"2022-07-03T19:45:08.589759Z","iopub.status.idle":"2022-07-03T19:45:08.837385Z","shell.execute_reply.started":"2022-07-03T19:45:08.589709Z","shell.execute_reply":"2022-07-03T19:45:08.836183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class trainingdf(torch.utils.data.Dataset):\n    def __init__(self,df):\n        self.DF=df\n        self.class2index=legend\n    def __len__(self):\n        return self.DF.shape[0]\n    def __getitem__(self,index):\n        image=self.DF.image.iloc[index]\n        image=np.array([image])\n        label=self.DF.label.iloc[index]\n        label=self.class2index[label]\n        return image, label","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:12.536171Z","iopub.execute_input":"2022-07-03T19:45:12.536668Z","iopub.status.idle":"2022-07-03T19:45:12.548839Z","shell.execute_reply.started":"2022-07-03T19:45:12.536623Z","shell.execute_reply":"2022-07-03T19:45:12.547697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftrain=trainingdf(dftrain)\ndftest=trainingdf(dftest)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:16.353998Z","iopub.execute_input":"2022-07-03T19:45:16.354474Z","iopub.status.idle":"2022-07-03T19:45:16.360138Z","shell.execute_reply.started":"2022-07-03T19:45:16.354432Z","shell.execute_reply":"2022-07-03T19:45:16.359015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:17.857531Z","iopub.execute_input":"2022-07-03T19:45:17.858141Z","iopub.status.idle":"2022-07-03T19:45:18.074416Z","shell.execute_reply.started":"2022-07-03T19:45:17.858094Z","shell.execute_reply":"2022-07-03T19:45:18.073388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# training model","metadata":{}},{"cell_type":"code","source":"\nfrom __future__ import print_function, division\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\nimport torch.backends.cudnn as cudnn\nimport torchvision\nfrom torchvision import datasets, models, transforms\nimport matplotlib.pyplot as plt\nimport time\nfrom torch.utils.data import Dataset, DataLoader, ConcatDataset\nimport copy\n","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:21.142556Z","iopub.execute_input":"2022-07-03T19:45:21.143096Z","iopub.status.idle":"2022-07-03T19:45:21.153499Z","shell.execute_reply.started":"2022-07-03T19:45:21.143048Z","shell.execute_reply":"2022-07-03T19:45:21.152354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls_tr=DataLoader(dftrain,batch_size=128*2,shuffle=True,num_workers=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:23.230927Z","iopub.execute_input":"2022-07-03T19:45:23.231428Z","iopub.status.idle":"2022-07-03T19:45:23.240837Z","shell.execute_reply.started":"2022-07-03T19:45:23.231385Z","shell.execute_reply":"2022-07-03T19:45:23.239745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls_te=DataLoader(dftest,batch_size=128*2,shuffle=True,num_workers=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:25.147555Z","iopub.execute_input":"2022-07-03T19:45:25.148186Z","iopub.status.idle":"2022-07-03T19:45:25.159617Z","shell.execute_reply.started":"2022-07-03T19:45:25.148141Z","shell.execute_reply":"2022-07-03T19:45:25.158335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del dftest,dftrain\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:26.837554Z","iopub.execute_input":"2022-07-03T19:45:26.838133Z","iopub.status.idle":"2022-07-03T19:45:27.045166Z","shell.execute_reply.started":"2022-07-03T19:45:26.838083Z","shell.execute_reply":"2022-07-03T19:45:27.043687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:32.669729Z","iopub.execute_input":"2022-07-03T19:45:32.670374Z","iopub.status.idle":"2022-07-03T19:45:32.867373Z","shell.execute_reply.started":"2022-07-03T19:45:32.670329Z","shell.execute_reply":"2022-07-03T19:45:32.865943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls_te.batch_size","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:35.268238Z","iopub.execute_input":"2022-07-03T19:45:35.268783Z","iopub.status.idle":"2022-07-03T19:45:35.282421Z","shell.execute_reply.started":"2022-07-03T19:45:35.268734Z","shell.execute_reply":"2022-07-03T19:45:35.2812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,j in dls_tr:\n    break","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:37.72499Z","iopub.execute_input":"2022-07-03T19:45:37.725557Z","iopub.status.idle":"2022-07-03T19:45:37.776357Z","shell.execute_reply.started":"2022-07-03T19:45:37.72551Z","shell.execute_reply":"2022-07-03T19:45:37.775136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:39.279419Z","iopub.execute_input":"2022-07-03T19:45:39.279939Z","iopub.status.idle":"2022-07-03T19:45:39.288454Z","shell.execute_reply.started":"2022-07-03T19:45:39.279888Z","shell.execute_reply":"2022-07-03T19:45:39.287386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(i[0][0])\nprint(j[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:40.646173Z","iopub.execute_input":"2022-07-03T19:45:40.646752Z","iopub.status.idle":"2022-07-03T19:45:40.858053Z","shell.execute_reply.started":"2022-07-03T19:45:40.646698Z","shell.execute_reply":"2022-07-03T19:45:40.857032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del i,j\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:42.718268Z","iopub.execute_input":"2022-07-03T19:45:42.71878Z","iopub.status.idle":"2022-07-03T19:45:42.917391Z","shell.execute_reply.started":"2022-07-03T19:45:42.718732Z","shell.execute_reply":"2022-07-03T19:45:42.915922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torchvision.models.resnet18(pretrained=True)\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:48.076349Z","iopub.execute_input":"2022-07-03T19:45:48.07688Z","iopub.status.idle":"2022-07-03T19:45:48.547158Z","shell.execute_reply.started":"2022-07-03T19:45:48.076832Z","shell.execute_reply":"2022-07-03T19:45:48.545914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:50.352664Z","iopub.execute_input":"2022-07-03T19:45:50.353165Z","iopub.status.idle":"2022-07-03T19:45:50.365847Z","shell.execute_reply.started":"2022-07-03T19:45:50.353121Z","shell.execute_reply":"2022-07-03T19:45:50.364658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(model.parameters(), lr=0.002, amsgrad = True)\nscheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[5000, 12000, 18000], gamma=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:54.274044Z","iopub.execute_input":"2022-07-03T19:45:54.274604Z","iopub.status.idle":"2022-07-03T19:45:54.283551Z","shell.execute_reply.started":"2022-07-03T19:45:54.274543Z","shell.execute_reply":"2022-07-03T19:45:54.282447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_ftrs = model.fc.in_features\nmodel.fc = nn.Linear(num_ftrs, 340)\nmodel = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:45:55.959207Z","iopub.execute_input":"2022-07-03T19:45:55.95977Z","iopub.status.idle":"2022-07-03T19:45:59.688685Z","shell.execute_reply.started":"2022-07-03T19:45:55.959718Z","shell.execute_reply":"2022-07-03T19:45:59.686532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataloaders={\"train\":dls_tr,\"valid\":dls_te}","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:46:01.719694Z","iopub.execute_input":"2022-07-03T19:46:01.720191Z","iopub.status.idle":"2022-07-03T19:46:01.728345Z","shell.execute_reply.started":"2022-07-03T19:46:01.720149Z","shell.execute_reply":"2022-07-03T19:46:01.727166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del dls_tr,dls_te\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:46:09.352434Z","iopub.execute_input":"2022-07-03T19:46:09.352999Z","iopub.status.idle":"2022-07-03T19:46:09.54911Z","shell.execute_reply.started":"2022-07-03T19:46:09.352948Z","shell.execute_reply":"2022-07-03T19:46:09.547907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_model(model, criterion, optimizer, scheduler, num_epochs=25):\n    since = time.time()\n\n    best_model_wts = copy.deepcopy(model.state_dict())\n    best_acc = 0.0\n\n    for epoch in range(num_epochs):\n        print(f'Epoch {epoch}/{num_epochs - 1}')\n        print('-' * 10)\n\n        # Each epoch has a training and validation phase\n        for phase in ['train', 'valid']:\n            if phase == 'train':\n                model.train()  # Set model to training mode\n            else:\n                model.eval()   # Set model to evaluate mode\n\n            running_loss = 0.0\n            running_corrects = 0\n            #global inputs\n            # Iterate over data.\n            for inputs, labels in dataloaders[phase]:\n                inputs = inputs.to(device)\n                labels = labels.to(device)\n\n                # zero the parameter gradients\n                optimizer.zero_grad()\n\n                # forward\n                # track history if only in train\n                with torch.set_grad_enabled(phase == 'train'):\n                    \n                    outputs = model(inputs/255)\n                    _, preds = torch.max(outputs, 1)\n                    loss = criterion(outputs, labels)\n\n                    # backward + optimize only if in training phase\n                    if phase == 'train':\n                        loss.backward()\n                        optimizer.step()\n\n                # statistics\n                running_loss += loss.item() * inputs.size(0)\n                running_corrects += torch.sum(preds == labels.data)\n            if phase == 'train':\n                scheduler.step()\n\n            epoch_loss = running_loss / dataset_sizes[phase]\n            epoch_acc = running_corrects.double() / dataset_sizes[phase]\n\n            print(f'{phase} Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')\n\n            # deep copy the model\n            if phase == 'valid' and epoch_acc > best_acc:\n                best_acc = epoch_acc\n                best_model_wts = copy.deepcopy(model.state_dict())\n\n        print()\n\n    time_elapsed = time.time() - since\n    print(f'Training complete in {time_elapsed // 60:.0f}m {time_elapsed % 60:.0f}s')\n    print(f'Best val Acc: {best_acc:4f}')\n    del best_acc,epoch_loss,epoch_acc\n    gc.collect()\n    # load best model weights\n    model.load_state_dict(best_model_wts)\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:46:21.16531Z","iopub.execute_input":"2022-07-03T19:46:21.165858Z","iopub.status.idle":"2022-07-03T19:46:21.185197Z","shell.execute_reply.started":"2022-07-03T19:46:21.16581Z","shell.execute_reply":"2022-07-03T19:46:21.183633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_model(model, num_images=6):\n    was_training = model.training\n    model.eval()\n    images_so_far = 0\n    fig = plt.figure()\n\n    with torch.no_grad():\n        for i, (inputs, labels) in enumerate(dataloaders['valid']):\n            \n            inputs = inputs.to(device)\n            labels = labels.to(device)\n\n            outputs = model(inputs/255)\n            _, preds = torch.max(outputs, 1)\n\n            for j in range(inputs.size()[0]):\n                images_so_far += 1\n                ax = plt.subplot(num_images//2, 2, images_so_far)\n                ax.axis('off')\n                \n                ax.set_title(f'predicted: {decode_labels(j)}')\n                plt.imshow(inputs.cpu().data[j][0])\n\n                if images_so_far == num_images:\n                    model.train(mode=was_training)\n                    return\n        model.train(mode=was_training)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:46:28.570546Z","iopub.execute_input":"2022-07-03T19:46:28.571087Z","iopub.status.idle":"2022-07-03T19:46:28.584683Z","shell.execute_reply.started":"2022-07-03T19:46:28.571043Z","shell.execute_reply":"2022-07-03T19:46:28.582405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def squeeze_weights(m):\n    m.weight.data = m.weight.data.sum(dim=1)[:,None]\n    m.in_channels = 1\n\nmodel.conv1.apply(squeeze_weights)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:46:32.090093Z","iopub.execute_input":"2022-07-03T19:46:32.090579Z","iopub.status.idle":"2022-07-03T19:46:32.143711Z","shell.execute_reply.started":"2022-07-03T19:46:32.090536Z","shell.execute_reply":"2022-07-03T19:46:32.142665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#inputs/255","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:46:38.475854Z","iopub.execute_input":"2022-07-03T19:46:38.476363Z","iopub.status.idle":"2022-07-03T19:46:38.481652Z","shell.execute_reply.started":"2022-07-03T19:46:38.476317Z","shell.execute_reply":"2022-07-03T19:46:38.480555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_ft = train_model(model, criterion, optimizer, scheduler, num_epochs=25)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:46:48.97883Z","iopub.execute_input":"2022-07-03T19:46:48.979333Z","iopub.status.idle":"2022-07-03T19:49:49.632157Z","shell.execute_reply.started":"2022-07-03T19:46:48.979292Z","shell.execute_reply":"2022-07-03T19:49:49.631022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del model","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:53:55.386354Z","iopub.execute_input":"2022-07-03T19:53:55.386922Z","iopub.status.idle":"2022-07-03T19:53:55.396458Z","shell.execute_reply.started":"2022-07-03T19:53:55.386875Z","shell.execute_reply":"2022-07-03T19:53:55.395207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:53:56.545374Z","iopub.execute_input":"2022-07-03T19:53:56.54594Z","iopub.status.idle":"2022-07-03T19:53:56.739862Z","shell.execute_reply.started":"2022-07-03T19:53:56.545891Z","shell.execute_reply":"2022-07-03T19:53:56.738326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# visualizing results","metadata":{}},{"cell_type":"code","source":"\ndec_dict = {v: k for k , v in legend.items()}\n\ndef decode_labels(label):\n    return dec_dict[label]","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:58:21.058029Z","iopub.execute_input":"2022-07-03T19:58:21.058498Z","iopub.status.idle":"2022-07-03T19:58:21.067811Z","shell.execute_reply.started":"2022-07-03T19:58:21.058454Z","shell.execute_reply":"2022-07-03T19:58:21.066631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_model(model_ft)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T17:21:23.227677Z","iopub.status.idle":"2022-07-03T17:21:23.228639Z","shell.execute_reply.started":"2022-07-03T17:21:23.228296Z","shell.execute_reply":"2022-07-03T17:21:23.228329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del dataloaders,legend\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:58:37.113529Z","iopub.execute_input":"2022-07-03T19:58:37.114059Z","iopub.status.idle":"2022-07-03T19:58:37.467361Z","shell.execute_reply.started":"2022-07-03T19:58:37.114013Z","shell.execute_reply":"2022-07-03T19:58:37.466332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.empty_cache() ","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:58:50.056331Z","iopub.execute_input":"2022-07-03T19:58:50.056953Z","iopub.status.idle":"2022-07-03T19:58:50.075409Z","shell.execute_reply.started":"2022-07-03T19:58:50.056899Z","shell.execute_reply":"2022-07-03T19:58:50.07444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:58:54.46454Z","iopub.execute_input":"2022-07-03T19:58:54.465029Z","iopub.status.idle":"2022-07-03T19:58:54.65417Z","shell.execute_reply.started":"2022-07-03T19:58:54.464988Z","shell.execute_reply":"2022-07-03T19:58:54.652912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# preping testset and submission file","metadata":{}},{"cell_type":"code","source":"class testdf(torch.utils.data.Dataset):\n    def __init__(self,df):\n        self.DF=df\n\n    def __len__(self):\n        return self.DF.shape[0]\n    \n    def __getitem__(self,index):\n        image=self.DF.image.iloc[index]\n        image=np.array([image])\n        return image","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:59:05.236482Z","iopub.execute_input":"2022-07-03T19:59:05.237069Z","iopub.status.idle":"2022-07-03T19:59:05.249717Z","shell.execute_reply.started":"2022-07-03T19:59:05.237022Z","shell.execute_reply":"2022-07-03T19:59:05.248787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv(\"../input/quickdraw-doodle-recognition/test_simplified.csv\").drawing\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:59:22.553038Z","iopub.execute_input":"2022-07-03T19:59:22.553521Z","iopub.status.idle":"2022-07-03T19:59:24.091538Z","shell.execute_reply.started":"2022-07-03T19:59:22.55348Z","shell.execute_reply":"2022-07-03T19:59:24.09052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1=pd.DataFrame({\"image\":get_ims(test)})","metadata":{"execution":{"iopub.status.busy":"2022-07-03T19:59:45.277243Z","iopub.execute_input":"2022-07-03T19:59:45.277768Z","iopub.status.idle":"2022-07-03T20:01:04.789881Z","shell.execute_reply.started":"2022-07-03T19:59:45.277717Z","shell.execute_reply":"2022-07-03T20:01:04.78869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T20:01:09.789521Z","iopub.execute_input":"2022-07-03T20:01:09.790015Z","iopub.status.idle":"2022-07-03T20:01:10.003252Z","shell.execute_reply.started":"2022-07-03T20:01:09.789965Z","shell.execute_reply":"2022-07-03T20:01:10.002278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.garbage","metadata":{"execution":{"iopub.status.busy":"2022-07-03T20:01:14.055694Z","iopub.execute_input":"2022-07-03T20:01:14.056326Z","iopub.status.idle":"2022-07-03T20:01:14.065717Z","shell.execute_reply.started":"2022-07-03T20:01:14.056279Z","shell.execute_reply":"2022-07-03T20:01:14.064457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=testdf(test1)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T20:01:21.405066Z","iopub.execute_input":"2022-07-03T20:01:21.405534Z","iopub.status.idle":"2022-07-03T20:01:21.411047Z","shell.execute_reply.started":"2022-07-03T20:01:21.405492Z","shell.execute_reply":"2022-07-03T20:01:21.409985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testloader = DataLoader(test, batch_size=128*2, shuffle=False, num_workers=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T20:01:28.679832Z","iopub.execute_input":"2022-07-03T20:01:28.680381Z","iopub.status.idle":"2022-07-03T20:01:28.687754Z","shell.execute_reply.started":"2022-07-03T20:01:28.680334Z","shell.execute_reply":"2022-07-03T20:01:28.686575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-03T20:01:29.861558Z","iopub.execute_input":"2022-07-03T20:01:29.862179Z","iopub.status.idle":"2022-07-03T20:01:30.053391Z","shell.execute_reply.started":"2022-07-03T20:01:29.862129Z","shell.execute_reply":"2022-07-03T20:01:30.052312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PREDICTION","metadata":{}},{"cell_type":"code","source":"\nmodel_ft.eval()\nmodel_ft = model_ft.to(device)\nlabels = np.empty((0,3))\n#labels = labels.to(device)\nfor x in tqdm.tqdm(testloader):\n    x = x.to(device)\n    output = model_ft(x/255)\n    _, pred = output.topk(3, 1, True, True)\n    labels = np.concatenate([labels, pred.cpu()], axis = 0)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T20:01:45.450951Z","iopub.execute_input":"2022-07-03T20:01:45.451555Z","iopub.status.idle":"2022-07-03T20:03:00.472515Z","shell.execute_reply.started":"2022-07-03T20:01:45.451505Z","shell.execute_reply":"2022-07-03T20:03:00.471362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/quickdraw-doodle-recognition/test_simplified.csv', index_col='key_id')\nsubmission.drop(['countrycode', 'drawing'], axis=1, inplace=True)\nsubmission['word'] = ''\nfor i, label in enumerate(labels):\n    submission.word.iloc[i] = \" \".join([decode_labels(l) for l in label])","metadata":{"execution":{"iopub.status.busy":"2022-07-03T20:03:22.975835Z","iopub.execute_input":"2022-07-03T20:03:22.97648Z","iopub.status.idle":"2022-07-03T20:04:41.322501Z","shell.execute_reply.started":"2022-07-03T20:03:22.97642Z","shell.execute_reply":"2022-07-03T20:04:41.321251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-03T20:34:05.603699Z","iopub.execute_input":"2022-07-03T20:34:05.60423Z","iopub.status.idle":"2022-07-03T20:34:06.140947Z","shell.execute_reply.started":"2022-07-03T20:34:05.604183Z","shell.execute_reply":"2022-07-03T20:34:06.139813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-03T20:34:09.138455Z","iopub.execute_input":"2022-07-03T20:34:09.139002Z","iopub.status.idle":"2022-07-03T20:34:09.161866Z","shell.execute_reply.started":"2022-07-03T20:34:09.138955Z","shell.execute_reply":"2022-07-03T20:34:09.160931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}