{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":10200,"databundleVersionId":868375,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport ast\nimport os\nimport random\n\nimport PIL\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-22T08:33:17.218467Z","iopub.execute_input":"2024-03-22T08:33:17.218873Z","iopub.status.idle":"2024-03-22T08:33:33.807211Z","shell.execute_reply.started":"2024-03-22T08:33:17.218829Z","shell.execute_reply":"2024-03-22T08:33:33.806124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Bees from dataset**","metadata":{}},{"cell_type":"code","source":"bee = pd.read_csv('/kaggle/input/quickdraw-doodle-recognition/train_simplified/bee.csv')\nbee = bee[bee.recognized]\nbee['timestamp'] = pd.to_datetime(bee.timestamp)\nbee = bee.sort_values(by='timestamp', ascending=False)\nbee['drawing'] = bee['drawing'].apply(ast.literal_eval)\nbee.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:33:38.347871Z","iopub.execute_input":"2024-03-22T08:33:38.348589Z","iopub.status.idle":"2024-03-22T08:34:27.415228Z","shell.execute_reply.started":"2024-03-22T08:33:38.348555Z","shell.execute_reply":"2024-03-22T08:34:27.414107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bee.shape[0]","metadata":{"execution":{"iopub.status.busy":"2024-03-17T08:00:55.013067Z","iopub.execute_input":"2024-03-17T08:00:55.013367Z","iopub.status.idle":"2024-03-17T08:00:55.018606Z","shell.execute_reply.started":"2024-03-17T08:00:55.013344Z","shell.execute_reply":"2024-03-17T08:00:55.017597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b = bee['drawing'].values[1]\nb","metadata":{"execution":{"iopub.status.busy":"2024-03-17T09:25:28.465162Z","iopub.execute_input":"2024-03-17T09:25:28.465472Z","iopub.status.idle":"2024-03-17T09:25:28.474208Z","shell.execute_reply.started":"2024-03-17T09:25:28.465451Z","shell.execute_reply":"2024-03-17T09:25:28.473448Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x, y in b:\n    print(\"X:\", x)\n    print(\"Y:\", y)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T04:26:11.283584Z","iopub.execute_input":"2024-03-17T04:26:11.283997Z","iopub.status.idle":"2024-03-17T04:26:11.290692Z","shell.execute_reply.started":"2024-03-17T04:26:11.283968Z","shell.execute_reply":"2024-03-17T04:26:11.289234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x, y in b:\n    plt.plot(x, -np.array(y), lw=3)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T04:26:14.671527Z","iopub.execute_input":"2024-03-17T04:26:14.671971Z","iopub.status.idle":"2024-03-17T04:26:14.96547Z","shell.execute_reply.started":"2024-03-17T04:26:14.671941Z","shell.execute_reply":"2024-03-17T04:26:14.964336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n = 10\nfig, axs = plt.subplots(nrows=n, ncols=n, sharex=True, sharey=True, figsize=(16, 10))\nfor i, drawing in enumerate(bee.drawing):\n    ax = axs[i // n, i % n]\n    for x, y in drawing:\n        ax.plot(x, -np.array(y), lw=3)\n    ax.axis('off')\n#fig.savefig('duck.png', dpi=200)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T04:27:11.58523Z","iopub.execute_input":"2024-03-17T04:27:11.585676Z","iopub.status.idle":"2024-03-17T04:27:28.243384Z","shell.execute_reply.started":"2024-03-17T04:27:11.585646Z","shell.execute_reply":"2024-03-17T04:27:28.242449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Save bees to csv**","metadata":{}},{"cell_type":"code","source":"bee.to_csv('bee.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:35:03.690924Z","iopub.execute_input":"2024-03-22T08:35:03.692165Z","iopub.status.idle":"2024-03-22T08:35:10.90154Z","shell.execute_reply.started":"2024-03-22T08:35:03.69212Z","shell.execute_reply":"2024-03-22T08:35:10.900309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Save non bees to csv**","metadata":{}},{"cell_type":"code","source":"def random_rows(filepath):\n    tmp_df = pd.read_csv(filepath)\n    tmp_df = tmp_df[tmp_df.recognized]\n    tmp_df = tmp_df.sample(n=340)\n    tmp_df['timestamp'] = pd.to_datetime(tmp_df.timestamp)\n    tmp_df = tmp_df.sort_values(by='timestamp', ascending=True)\n    tmp_df['drawing'] = tmp_df['drawing'].apply(ast.literal_eval)\n    return tmp_df","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:41:39.200561Z","iopub.execute_input":"2024-03-22T08:41:39.201589Z","iopub.status.idle":"2024-03-22T08:41:39.209145Z","shell.execute_reply.started":"2024-03-22T08:41:39.201551Z","shell.execute_reply":"2024-03-22T08:41:39.207702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Non bees\npath = '/kaggle/input/quickdraw-doodle-recognition/train_simplified'\nnonbee = pd.DataFrame()\n\nfor filename in os.listdir(path):\n    print(filename)\n    if filename.endswith('.csv') & (filename != 'bee.csv'):\n        filepath = os.path.join(path, filename)\n        rows = random_rows(filepath)\n        if not rows.empty:\n            #nonbee = nonbee.append(rows)\n            nonbee = pd.concat([nonbee, rows], ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T08:44:37.837175Z","iopub.execute_input":"2024-03-22T08:44:37.837608Z","iopub.status.idle":"2024-03-22T08:55:10.968773Z","shell.execute_reply.started":"2024-03-22T08:44:37.837577Z","shell.execute_reply":"2024-03-22T08:55:10.967595Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nonbee.shape[0]","metadata":{"execution":{"iopub.status.busy":"2024-03-22T09:00:40.336455Z","iopub.execute_input":"2024-03-22T09:00:40.336892Z","iopub.status.idle":"2024-03-22T09:00:40.346421Z","shell.execute_reply.started":"2024-03-22T09:00:40.336859Z","shell.execute_reply":"2024-03-22T09:00:40.3451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Trim to have the same size as bee 110906\nnonbee = nonbee.iloc[:110906]\nnonbee.shape[0]","metadata":{"execution":{"iopub.status.busy":"2024-03-22T09:04:26.205463Z","iopub.execute_input":"2024-03-22T09:04:26.205862Z","iopub.status.idle":"2024-03-22T09:04:26.213712Z","shell.execute_reply.started":"2024-03-22T09:04:26.20583Z","shell.execute_reply":"2024-03-22T09:04:26.21261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nonbee.to_csv('nonbee.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T09:05:04.93869Z","iopub.execute_input":"2024-03-22T09:05:04.939172Z","iopub.status.idle":"2024-03-22T09:05:09.976333Z","shell.execute_reply.started":"2024-03-22T09:05:04.939135Z","shell.execute_reply":"2024-03-22T09:05:09.975021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Save bees to images**","metadata":{}},{"cell_type":"code","source":"#Save images bees\n!mkdir /kaggle/working/bee","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:36:04.318344Z","iopub.execute_input":"2024-03-17T10:36:04.319142Z","iopub.status.idle":"2024-03-17T10:36:04.667136Z","shell.execute_reply.started":"2024-03-17T10:36:04.319085Z","shell.execute_reply":"2024-03-17T10:36:04.666274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, b in enumerate(bee['drawing'].values):\n    plt.figure(figsize=(2.56, 2.56))\n    for x, y in b:\n        plt.plot(x, -np.array(y), lw=3, color='black')\n        plt.axis('off')\n    plt.savefig(f'/kaggle/working/bee/bee_{i}.jpg', format='jpg', bbox_inches='tight', pad_inches=0, dpi=100)\n    plt.close()\n#in bee 32553 files","metadata":{"execution":{"iopub.status.busy":"2024-03-17T09:56:28.178079Z","iopub.execute_input":"2024-03-17T09:56:28.178424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Find number of files\nfolder_path = '/kaggle/working/bee/'\nmax_idx = 0\n\nfor filename in os.listdir(folder_path):\n        idx = int(filename.split('_')[1].split('.')[0])\n        max_idx = max(max_idx, idx)\n\nprint(max_idx)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T10:08:15.687335Z","iopub.execute_input":"2024-03-22T10:08:15.689204Z","iopub.status.idle":"2024-03-22T10:08:15.764665Z","shell.execute_reply.started":"2024-03-22T10:08:15.68916Z","shell.execute_reply":"2024-03-22T10:08:15.763539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Save images nonbees\n!mkdir /kaggle/working/nonbee","metadata":{"execution":{"iopub.status.busy":"2024-03-22T09:15:17.531071Z","iopub.execute_input":"2024-03-22T09:15:17.531499Z","iopub.status.idle":"2024-03-22T09:15:18.673022Z","shell.execute_reply.started":"2024-03-22T09:15:17.531468Z","shell.execute_reply":"2024-03-22T09:15:18.671608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Nonbee not in kaggle working\n#nonbee = pd.read_csv('/kaggle/working/nonbee.csv')\nnonbee = pd.read_csv('/kaggle/input/nonbee-csv/nonbee.csv')\nnonbee['drawing'] = nonbee['drawing'].apply(ast.literal_eval)\nnonbee.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T20:04:28.669525Z","iopub.execute_input":"2024-03-17T20:04:28.669878Z","iopub.status.idle":"2024-03-17T20:04:53.815219Z","shell.execute_reply.started":"2024-03-17T20:04:28.669851Z","shell.execute_reply":"2024-03-17T20:04:53.814236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, b in enumerate(nonbee['drawing'].values[:32553]):\n    plt.figure(figsize=(2.56, 2.56))\n    for x, y in b:\n        plt.plot(x, -np.array(y), lw=3, color='black')\n        plt.axis('off')\n    plt.savefig(f'/kaggle/working/nonbee/nonbee_{i}.jpg', format='jpg', bbox_inches='tight', pad_inches=0, dpi=100)\n    plt.close()","metadata":{"execution":{"iopub.status.busy":"2024-03-22T09:15:24.896346Z","iopub.execute_input":"2024-03-22T09:15:24.896781Z","iopub.status.idle":"2024-03-22T09:57:07.503609Z","shell.execute_reply.started":"2024-03-22T09:15:24.896745Z","shell.execute_reply":"2024-03-22T09:57:07.501198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Bee vs nonbee\nbee2 = bee.drop(columns=['word'])\nbee2['bee'] = 'bee'\nbee2.to_csv('bee2.csv',index=False)\nbee2.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T09:20:49.010961Z","iopub.execute_input":"2024-03-17T09:20:49.011311Z","iopub.status.idle":"2024-03-17T09:20:52.246648Z","shell.execute_reply.started":"2024-03-17T09:20:49.011285Z","shell.execute_reply":"2024-03-17T09:20:52.245739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nonbee2 = nonbee.drop(columns=['word'])\nnonbee2['bee'] = 'not bee'\nnonbee2.to_csv('nonbee2.csv',index=False)\nnonbee2.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T09:21:54.505853Z","iopub.execute_input":"2024-03-17T09:21:54.506239Z","iopub.status.idle":"2024-03-17T09:21:56.810072Z","shell.execute_reply.started":"2024-03-17T09:21:54.506208Z","shell.execute_reply":"2024-03-17T09:21:56.809224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Zip files\n!zip -r nonbee.zip /kaggle/working/nonbee","metadata":{"execution":{"iopub.status.busy":"2024-03-22T10:08:34.073063Z","iopub.execute_input":"2024-03-22T10:08:34.07348Z","iopub.status.idle":"2024-03-22T10:08:47.006521Z","shell.execute_reply.started":"2024-03-22T10:08:34.073451Z","shell.execute_reply":"2024-03-22T10:08:47.004918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Snippets\n\nImage generator\nhttps://www.kaggle.com/code/gaborfodor/data-reggeli\n\nResNet50\nhttps://www.kaggle.com/code/kotarojp/first-step-for-submission-keras-resnet50#Training\n\nSome basic image classification\nhttps://www.tensorflow.org/tutorials/images/classification\n\nCNN, VGG, RESNET and other pre-trained models\nhttps://www.kaggle.com/code/shivamb/cnn-architectures-vgg-resnet-inception-tl#1.4-Resnets\n\nMNIST\nhttps://github.com/fchollet/deep-learning-with-python-notebooks/blob/master/chapter05_fundamentals-of-ml.ipynb","metadata":{}}]}