{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"},{"sourceId":7025678,"sourceType":"datasetVersion","datasetId":4040466}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\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","_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python --version","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:33:59.375800Z","iopub.execute_input":"2023-12-18T16:33:59.376063Z","iopub.status.idle":"2023-12-18T16:34:00.352354Z","shell.execute_reply.started":"2023-12-18T16:33:59.376039Z","shell.execute_reply":"2023-12-18T16:34:00.351190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -U /kaggle/input/lightautoml-python-3-11/lightautoml-0.3.9b1-py3-none-any.whl[cv]","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ[\"CUDA_DEVICE_ORDER\"]=\"PCI_BUS_ID\"   # see issue #152\nos.environ[\"CUDA_VISIBLE_DEVICES\"]=\"0\"","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:49:17.070765Z","iopub.execute_input":"2023-12-18T16:49:17.071188Z","iopub.status.idle":"2023-12-18T16:49:17.076092Z","shell.execute_reply.started":"2023-12-18T16:49:17.071156Z","shell.execute_reply":"2023-12-18T16:49:17.075153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Standard python libraries\nimport os\nimport time\n\n# Essential DS libraries\nimport numpy as np\nimport pandas as pd\nfrom pathlib import Path\nfrom sklearn.metrics import f1_score, accuracy_score, confusion_matrix\nfrom sklearn.model_selection import train_test_split\nimport plotly.figure_factory as ff\nfrom tqdm import tqdm\nimport cv2\nfrom PIL import Image\nimport torch\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport plotly.graph_objects as go\n\n# LightAutoML presets, task and report generation\nfrom lightautoml.automl.presets.image_presets import TabularCVAutoML\nfrom lightautoml.tasks import Task","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:49:20.086917Z","iopub.execute_input":"2023-12-18T16:49:20.087277Z","iopub.status.idle":"2023-12-18T16:49:56.331568Z","shell.execute_reply.started":"2023-12-18T16:49:20.087248Z","shell.execute_reply":"2023-12-18T16:49:56.330785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(42)\ntorch.set_num_threads(2)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:49:56.333196Z","iopub.execute_input":"2023-12-18T16:49:56.333894Z","iopub.status.idle":"2023-12-18T16:49:56.351062Z","shell.execute_reply.started":"2023-12-18T16:49:56.333857Z","shell.execute_reply":"2023-12-18T16:49:56.349997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! ls /kaggle/input/cassava-leaf-disease-classification","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:49:56.352655Z","iopub.execute_input":"2023-12-18T16:49:56.353000Z","iopub.status.idle":"2023-12-18T16:49:57.348566Z","shell.execute_reply.started":"2023-12-18T16:49:56.352966Z","shell.execute_reply":"2023-12-18T16:49:57.347536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir('/kaggle/input/cassava-leaf-disease-classification/train_images')))","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:50:15.502916Z","iopub.execute_input":"2023-12-18T16:50:15.503759Z","iopub.status.idle":"2023-12-18T16:50:15.785570Z","shell.execute_reply.started":"2023-12-18T16:50:15.503721Z","shell.execute_reply":"2023-12-18T16:50:15.784607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/train.csv')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:50:18.771829Z","iopub.execute_input":"2023-12-18T16:50:18.772672Z","iopub.status.idle":"2023-12-18T16:50:18.807290Z","shell.execute_reply.started":"2023-12-18T16:50:18.772640Z","shell.execute_reply":"2023-12-18T16:50:18.806383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:50:21.880185Z","iopub.execute_input":"2023-12-18T16:50:21.880882Z","iopub.status.idle":"2023-12-18T16:50:21.894103Z","shell.execute_reply.started":"2023-12-18T16:50:21.880844Z","shell.execute_reply":"2023-12-18T16:50:21.893027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_to_disease = pd.read_json(os.path.join('/kaggle/input/cassava-leaf-disease-classification/label_num_to_disease_map.json'), typ='series')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:50:25.088774Z","iopub.execute_input":"2023-12-18T16:50:25.089450Z","iopub.status.idle":"2023-12-18T16:50:25.108818Z","shell.execute_reply.started":"2023-12-18T16:50:25.089410Z","shell.execute_reply":"2023-12-18T16:50:25.108060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(label_to_disease)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:50:28.283374Z","iopub.execute_input":"2023-12-18T16:50:28.283762Z","iopub.status.idle":"2023-12-18T16:50:28.289438Z","shell.execute_reply.started":"2023-12-18T16:50:28.283730Z","shell.execute_reply":"2023-12-18T16:50:28.288455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/sample_submission.csv')\nprint(submission.shape)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:50:33.582170Z","iopub.execute_input":"2023-12-18T16:50:33.582854Z","iopub.status.idle":"2023-12-18T16:50:33.598691Z","shell.execute_reply.started":"2023-12-18T16:50:33.582819Z","shell.execute_reply":"2023-12-18T16:50:33.597786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\ndata['path'] = '/kaggle/input/cassava-leaf-disease-classification/train_images/' + data['image_id']\n\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:50:37.394436Z","iopub.execute_input":"2023-12-18T16:50:37.394817Z","iopub.status.idle":"2023-12-18T16:50:37.411151Z","shell.execute_reply.started":"2023-12-18T16:50:37.394791Z","shell.execute_reply":"2023-12-18T16:50:37.410231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['path'] = '/kaggle/input/cassava-leaf-disease-classification/train_images/' + submission['image_id']\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:50:41.216886Z","iopub.execute_input":"2023-12-18T16:50:41.217639Z","iopub.status.idle":"2023-12-18T16:50:41.227581Z","shell.execute_reply.started":"2023-12-18T16:50:41.217607Z","shell.execute_reply":"2023-12-18T16:50:41.226758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[\"class_name\"] = data[\"label\"].apply(lambda x: label_to_disease.get(x))\ndata","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:50:46.887784Z","iopub.execute_input":"2023-12-18T16:50:46.888503Z","iopub.status.idle":"2023-12-18T16:50:47.023000Z","shell.execute_reply.started":"2023-12-18T16:50:46.888461Z","shell.execute_reply":"2023-12-18T16:50:47.022077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_count = data.groupby('label', as_index=False).count()\nlabel_count.rename(columns={'image_id': 'count', 'label': 'label_count'}, inplace=True)\nlabel_count['label_count'] = label_count['label_count'].apply(lambda x: label_to_disease[x])\n\nfig, ax = plt.subplots(1, 1, figsize=(12, 8))\nfont1 = {'family': 'Times New Roman','weight': 'bold','style':'normal','size': 20}\nax.set_xlabel('Type of disease',font1)\nax.set_ylabel('Count',font1)\nax = sns.barplot(x=label_count['count'], y=label_count['label_count'], palette='viridis')\nax.tick_params(labelsize=16)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:50:54.071428Z","iopub.execute_input":"2023-12-18T16:50:54.072322Z","iopub.status.idle":"2023-12-18T16:50:54.441083Z","shell.execute_reply.started":"2023-12-18T16:50:54.072287Z","shell.execute_reply":"2023-12-18T16:50:54.440213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_examples(df):\n    unique_classes = df['class_name'].unique()\n    for class_name in unique_classes:\n        class_df = df[df['class_name'] == class_name]\n        sample_paths = class_df['path'].sample(n=3, random_state=1)  # Выбираем 3 случайных примера из класса\n        plt.figure(figsize=(10, 5))\n        for i, path in enumerate(sample_paths):\n            plt.subplot(1, 3, i + 1)\n            img = plt.imread(path)\n            plt.imshow(img)\n            plt.title(class_name)\n            plt.axis('off')\n        plt.suptitle(f'Примеры класса: {class_name}')\n        plt.show()\nshow_examples(data)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:50:58.581228Z","iopub.execute_input":"2023-12-18T16:50:58.581629Z","iopub.status.idle":"2023-12-18T16:51:01.300040Z","shell.execute_reply.started":"2023-12-18T16:50:58.581599Z","shell.execute_reply":"2023-12-18T16:51:01.299132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Посмотрим гистограммы изображений\ndef load_img(path):\n    img_bgr = cv2.imread(path)\n    img_rgb = img_bgr[:, :, ::-1]\n    return img_rgb\n# explore the pixels of cassava leaf pictures\nif True:\n    img_names = Path('/kaggle/input/cassava-leaf-disease-classification/train_images').glob('*')\n    plt.figure(figsize=(12,8),dpi=800)\n    pbar = tqdm(img_names, total=len(data))\n    for img_name in pbar:\n        img = load_img(img_name.as_posix())\n        # here use the calchist method in cv2 to show the histogram of photo pixels\n        hist = cv2.calcHist([img],[0],None,[256],[0,256])\n        plt.plot(hist)\n    plt.show()\nelse:\n    print('Can not show the details of images')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:51:04.883100Z","iopub.execute_input":"2023-12-18T16:51:04.883926Z","iopub.status.idle":"2023-12-18T16:58:53.128449Z","shell.execute_reply.started":"2023-12-18T16:51:04.883890Z","shell.execute_reply":"2023-12-18T16:58:53.127447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# посмотрим подробнее основные классы:\nHealthy_Cassava = data[data['class_name'] == 'Healthy']['image_id'].to_list()\n\nCMD_Cassava = data[data['class_name'] == 'Cassava Mosaic Disease (CMD)']['image_id'].to_list()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:58:53.149998Z","iopub.execute_input":"2023-12-18T16:58:53.150315Z","iopub.status.idle":"2023-12-18T16:58:53.171821Z","shell.execute_reply.started":"2023-12-18T16:58:53.150288Z","shell.execute_reply":"2023-12-18T16:58:53.170962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Healthy Cassava Leaves","metadata":{}},{"cell_type":"code","source":"np.random.seed(42)\n\nbase_path = Path('/kaggle/input/cassava-leaf-disease-classification')\ntrain_img_dir =  base_path/'train_images'\n\nrandom_images=[]\nplt.figure(figsize=(16,12))\nfor i in range(9):\n    random_images.append(np.random.choice(Healthy_Cassava))\n\nfor i in range(9):\n    \n    plt.subplot(3, 3, i + 1)\n    img = plt.imread(train_img_dir/random_images[i])\n    plt.imshow(img)\nplt.show()   ","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:58:53.173794Z","iopub.execute_input":"2023-12-18T16:58:53.174091Z","iopub.status.idle":"2023-12-18T16:58:55.647496Z","shell.execute_reply.started":"2023-12-18T16:58:53.174065Z","shell.execute_reply":"2023-12-18T16:58:55.645973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = plt.figure(figsize=(16,8))\nf.add_subplot(1,2, 1)\n\nfont2 = {'family': 'Times New Roman','weight': 'bold','style':'normal','size': 14}\nraw_image = plt.imread(train_img_dir/Healthy_Cassava[2])\nplt.imshow(raw_image, cmap='gray')\nplt.colorbar()\nplt.title('Healthy Image',font2)\nprint(f\"Image dimensions:  {raw_image.shape[0],raw_image.shape[1]}\")\nprint(f\"Maximum pixel value : {raw_image.max():.1f} ; Minimum pixel value:{raw_image.min():.1f}\")\nprint(f\"Mean value of the pixels : {raw_image.mean():.1f} ; Standard deviation : {raw_image.std():.1f}\")\n\nf.add_subplot(1,2, 2)\n\nplt.hist(raw_image[:, :, 0].ravel(), bins = 256, color = 'red', alpha = 0.5)\nplt.hist(raw_image[:, :, 1].ravel(), bins = 256, color = 'Green', alpha = 0.5)\nplt.hist(raw_image[:, :, 2].ravel(), bins = 256, color = 'Blue', alpha = 0.5)\nplt.xlabel('Intensity Value',font2)\nplt.ylabel('Count',font2)\nplt.legend(['Red_Channel', 'Green_Channel', 'Blue_Channel'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:58:55.648939Z","iopub.execute_input":"2023-12-18T16:58:55.649247Z","iopub.status.idle":"2023-12-18T16:58:57.842921Z","shell.execute_reply.started":"2023-12-18T16:58:55.649220Z","shell.execute_reply":"2023-12-18T16:58:57.841818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imageArray=[]\nfor i in tqdm(range(len(Healthy_Cassava))):\n    imageArray.append(cv2.cvtColor(cv2.imread(str(train_img_dir/Healthy_Cassava[i])), cv2.COLOR_BGR2RGB))\n\nred_values = []\ngreen_values = []\nblue_values = []\nvalues = []\n\nfor i in tqdm(range(len(imageArray))):\n    red_values.append(np.mean(imageArray[i][:, :, 0]))\n    green_values.append(np.mean(imageArray[i][:, :, 1]))\n    blue_values.append(np.mean(imageArray[i][:, :, 2]))\n    values.append(np.mean(imageArray[i]))\n\nhist_data = [red_values, green_values, blue_values, values]\ngroup_labels = ['Red', 'Green', 'Blue', 'All']\n\nfig = ff.create_distplot(hist_data, group_labels,colors = ['red', 'green','blue','grey'])\nfig.update_layout(template = 'plotly_white', title_text = 'Channel Distribution - Healthy')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:58:57.844213Z","iopub.execute_input":"2023-12-18T16:58:57.844681Z","iopub.status.idle":"2023-12-18T16:59:27.436103Z","shell.execute_reply.started":"2023-12-18T16:58:57.844639Z","shell.execute_reply":"2023-12-18T16:59:27.435118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figData = []\nfor i, name in zip(range(3), ['Red', 'Green', 'Blue']):\n    trace = go.Box(y = hist_data[i], name = name, boxpoints='all', marker_color  = name)\n    figData.append(trace)\n\nfig = go.Figure(figData)\nfig.update_layout(title_text = 'Pixel Intensity Distribution - health leaf', template = 'plotly_white')\nfig.show() \n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:59:27.437430Z","iopub.execute_input":"2023-12-18T16:59:27.437744Z","iopub.status.idle":"2023-12-18T16:59:27.547719Z","shell.execute_reply.started":"2023-12-18T16:59:27.437717Z","shell.execute_reply":"2023-12-18T16:59:27.546247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CMD Cassava Leaves","metadata":{}},{"cell_type":"code","source":"np.random.seed(42)\n\nrandom_images=[]\nplt.figure(figsize=(16,12))\nfor i in range(9):\n    random_images.append(np.random.choice(CMD_Cassava))\n\nfor i in range(9):\n    \n    plt.subplot(3, 3, i + 1)\n    img = plt.imread(train_img_dir/random_images[i])\n    plt.imshow(img)\nplt.show()   \n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:59:27.549102Z","iopub.execute_input":"2023-12-18T16:59:27.549444Z","iopub.status.idle":"2023-12-18T16:59:29.966795Z","shell.execute_reply.started":"2023-12-18T16:59:27.549397Z","shell.execute_reply":"2023-12-18T16:59:29.965827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = plt.figure(figsize=(16,8))\nf.add_subplot(1,2, 1)\n\nraw_image = plt.imread(train_img_dir/CMD_Cassava[2])\nplt.imshow(raw_image, cmap='gray')\nplt.colorbar()\nplt.title('CMD Image',font2)\nprint(f\"Image dimensions:  {raw_image.shape[0],raw_image.shape[1]}\")\nprint(f\"Maximum pixel value : {raw_image.max():.1f} ; Minimum pixel value:{raw_image.min():.1f}\")\nprint(f\"Mean value of the pixels : {raw_image.mean():.1f} ; Standard deviation : {raw_image.std():.1f}\")\n\nf.add_subplot(1,2, 2)\n\nplt.hist(raw_image[:, :, 0].ravel(), bins = 256, color = 'red', alpha = 0.5)\nplt.hist(raw_image[:, :, 1].ravel(), bins = 256, color = 'Green', alpha = 0.5)\nplt.hist(raw_image[:, :, 2].ravel(), bins = 256, color = 'Blue', alpha = 0.5)\nplt.xlabel('Intensity Value',font2)\nplt.ylabel('Count',font2)\nplt.legend(['Red_Channel', 'Green_Channel', 'Blue_Channel'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:59:29.968406Z","iopub.execute_input":"2023-12-18T16:59:29.968877Z","iopub.status.idle":"2023-12-18T16:59:32.289766Z","shell.execute_reply.started":"2023-12-18T16:59:29.968834Z","shell.execute_reply":"2023-12-18T16:59:32.288629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imageArray=[]\nfor i in range(len(CMD_Cassava[:2000])):\n    imageArray.append(cv2.cvtColor(cv2.imread(str(train_img_dir/CMD_Cassava[i])), cv2.COLOR_BGR2RGB))\n\nred_values = []\ngreen_values = []\nblue_values = []\nvalues = []\n\nfor i in range(len(imageArray)):\n    red_values.append(np.mean(imageArray[i][:, :, 0]))\n    green_values.append(np.mean(imageArray[i][:, :, 1]))\n    blue_values.append(np.mean(imageArray[i][:, :, 2]))\n    values.append(np.mean(imageArray[i]))\n\nhist_data = [red_values, green_values, blue_values, values]\ngroup_labels = ['Red', 'Green', 'Blue', 'All']\n\nfig = ff.create_distplot(hist_data, group_labels,colors = ['red', 'green','blue','grey'])\nfig.update_layout(template = 'plotly_white', title_text = 'Channel Distribution - CMD Images')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:59:32.293657Z","iopub.execute_input":"2023-12-18T16:59:32.294039Z","iopub.status.idle":"2023-12-18T16:59:53.226509Z","shell.execute_reply.started":"2023-12-18T16:59:32.294002Z","shell.execute_reply":"2023-12-18T16:59:53.225620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figData = []\nfor i, name in zip(range(3), ['Red', 'Green', 'Blue']):\n    trace = go.Box(y = hist_data[i], name = name, boxpoints='all', marker_color  = name)\n    figData.append(trace)\n\nfig = go.Figure(figData)\nfig.update_layout(title_text = 'Pixel Intensity Distribution - CMD Images', template = 'plotly_white')\nfig.show() ","metadata":{"execution":{"iopub.status.busy":"2023-12-18T16:59:53.227731Z","iopub.execute_input":"2023-12-18T16:59:53.228091Z","iopub.status.idle":"2023-12-18T16:59:53.310306Z","shell.execute_reply.started":"2023-12-18T16:59:53.228058Z","shell.execute_reply":"2023-12-18T16:59:53.309506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Augmentation","metadata":{}},{"cell_type":"code","source":"!mkdir '/kaggle/working/cassava_train_modified/'","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:00:30.874016Z","iopub.execute_input":"2023-12-18T17:00:30.874795Z","iopub.status.idle":"2023-12-18T17:00:31.930826Z","shell.execute_reply.started":"2023-12-18T17:00:30.874759Z","shell.execute_reply":"2023-12-18T17:00:31.929596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# применяем аугментацию к каждому 10 изображению\nnew_imgs = []\n\nfor i, row in tqdm(data.iterrows()):\n    if i % 10 == 0:    \n        if i % 1000 == 0:\n            print(i)\n\n        img = Image.open(row.path)\n\n        if np.random.rand() > 0.5:\n            new_img = img.rotate(np.random.rand() * 60 - 30, resample=3)\n        else:\n            new_img = img.transpose(Image.FLIP_LEFT_RIGHT)\n\n        n_name = row.path.split('/')[-1][:-4] + '_' + str(i) + '.jpg'\n        new_img_name = '/kaggle/working/cassava_train_modified/' + n_name\n        new_img.save(new_img_name)\n        new_imgs.append([n_name, row.label, new_img_name,  row.class_name])","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:01:17.891903Z","iopub.execute_input":"2023-12-18T17:01:17.892631Z","iopub.status.idle":"2023-12-18T17:02:15.824692Z","shell.execute_reply.started":"2023-12-18T17:01:17.892594Z","shell.execute_reply":"2023-12-18T17:02:15.823843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir('/kaggle/working/cassava_train_modified')))","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:02:21.974495Z","iopub.execute_input":"2023-12-18T17:02:21.974878Z","iopub.status.idle":"2023-12-18T17:02:21.981923Z","shell.execute_reply.started":"2023-12-18T17:02:21.974848Z","shell.execute_reply":"2023-12-18T17:02:21.980920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.concat([data, pd.DataFrame(new_imgs, columns = ['image_id','label','path','class_name'])]).reset_index(drop = True)\ntrain_data","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:02:26.380879Z","iopub.execute_input":"2023-12-18T17:02:26.381271Z","iopub.status.idle":"2023-12-18T17:02:26.406288Z","shell.execute_reply.started":"2023-12-18T17:02:26.381239Z","shell.execute_reply":"2023-12-18T17:02:26.405378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train Val split","metadata":{}},{"cell_type":"code","source":"# Задаем отношение для разделения данных\ntrain_ratio = 0.8  # Пропорция для обучающего набора\n\n# Разделение данных с учетом стратификации\ntr_data, val_data = train_test_split(\n    train_data, \n    train_size=train_ratio, \n    stratify=train_data['label'],  # Используем стратификацию для сохранения баланса классов\n    random_state=42\n)\n\nprint(f\"Размер обучающего набора: {tr_data.shape[0]} изображений\")\nprint(f\"Размер валидационного набора: {val_data.shape[0]} изображений\")\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:02:44.472661Z","iopub.execute_input":"2023-12-18T17:02:44.473054Z","iopub.status.idle":"2023-12-18T17:02:44.498574Z","shell.execute_reply.started":"2023-12-18T17:02:44.473023Z","shell.execute_reply":"2023-12-18T17:02:44.497750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## LightAutoMl modeling","metadata":{}},{"cell_type":"code","source":"task = Task('multiclass')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:03:02.281730Z","iopub.execute_input":"2023-12-18T17:03:02.282116Z","iopub.status.idle":"2023-12-18T17:03:02.296828Z","shell.execute_reply.started":"2023-12-18T17:03:02.282086Z","shell.execute_reply":"2023-12-18T17:03:02.295823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roles = {\n    'target': 'label',\n    'path': ['path'],\n    'drop': ['class_name'],\n    'group': 'image_id'\n}","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:03:05.392428Z","iopub.execute_input":"2023-12-18T17:03:05.393202Z","iopub.status.idle":"2023-12-18T17:03:05.397645Z","shell.execute_reply.started":"2023-12-18T17:03:05.393168Z","shell.execute_reply":"2023-12-18T17:03:05.396719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"automl_0 = TabularCVAutoML(task = task,\n                         timeout=5 * 3600,\n                        cpu_limit = 2,\n                        reader_params = {'cv': 3, 'random_state': 42})","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:03:31.592732Z","iopub.execute_input":"2023-12-18T17:03:31.593067Z","iopub.status.idle":"2023-12-18T17:03:31.637298Z","shell.execute_reply.started":"2023-12-18T17:03:31.593042Z","shell.execute_reply":"2023-12-18T17:03:31.636597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\noof_pred_0 = automl_0.fit_predict(tr_data, roles = roles, verbose = 3)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:03:35.371306Z","iopub.execute_input":"2023-12-18T17:03:35.371709Z","iopub.status.idle":"2023-12-18T17:10:34.495864Z","shell.execute_reply.started":"2023-12-18T17:03:35.371680Z","shell.execute_reply":"2023-12-18T17:10:34.494884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oof_pred_0.features","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:10:56.673097Z","iopub.execute_input":"2023-12-18T17:10:56.673496Z","iopub.status.idle":"2023-12-18T17:10:56.680376Z","shell.execute_reply.started":"2023-12-18T17:10:56.673450Z","shell.execute_reply":"2023-12-18T17:10:56.679527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oof_pred_0.data","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:10:59.372780Z","iopub.execute_input":"2023-12-18T17:10:59.373511Z","iopub.status.idle":"2023-12-18T17:10:59.380245Z","shell.execute_reply.started":"2023-12-18T17:10:59.373477Z","shell.execute_reply":"2023-12-18T17:10:59.379216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_0 = tr_data[['image_id', 'label']]\npreds_0","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:12:28.692422Z","iopub.execute_input":"2023-12-18T17:12:28.692840Z","iopub.status.idle":"2023-12-18T17:12:28.706653Z","shell.execute_reply.started":"2023-12-18T17:12:28.692799Z","shell.execute_reply":"2023-12-18T17:12:28.705636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    preds_0['pred_0_' + str(i)] = oof_pred_0.data[:,i]\n\npreds_0","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:12:32.382087Z","iopub.execute_input":"2023-12-18T17:12:32.382719Z","iopub.status.idle":"2023-12-18T17:12:32.402917Z","shell.execute_reply.started":"2023-12-18T17:12:32.382686Z","shell.execute_reply":"2023-12-18T17:12:32.401981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_0 = preds_0.groupby(['image_id', 'label']).mean().reset_index()\npreds_0","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:12:35.671534Z","iopub.execute_input":"2023-12-18T17:12:35.672399Z","iopub.status.idle":"2023-12-18T17:12:35.726979Z","shell.execute_reply.started":"2023-12-18T17:12:35.672355Z","shell.execute_reply":"2023-12-18T17:12:35.726074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OOFs_0 = np.argmax(preds_0[['pred_0_' + str(i) for i in range(5)]].values, axis = 1)\nOOFs_0","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:12:39.281350Z","iopub.execute_input":"2023-12-18T17:12:39.282289Z","iopub.status.idle":"2023-12-18T17:12:39.292206Z","shell.execute_reply.started":"2023-12-18T17:12:39.282251Z","shell.execute_reply":"2023-12-18T17:12:39.291240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_0 = (OOFs_0 == preds_0['label']).sum() / len(OOFs_0)\nprint(f'Train_Out-of-fold accuracy: {accuracy_0}')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:12:44.282089Z","iopub.execute_input":"2023-12-18T17:12:44.282822Z","iopub.status.idle":"2023-12-18T17:12:44.288620Z","shell.execute_reply.started":"2023-12-18T17:12:44.282789Z","shell.execute_reply":"2023-12-18T17:12:44.287765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracies = []\naccuracies.append([accuracy_0, 'Train accuracy model default'])","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:17:53.171181Z","iopub.execute_input":"2023-12-18T17:17:53.171942Z","iopub.status.idle":"2023-12-18T17:17:53.176596Z","shell.execute_reply.started":"2023-12-18T17:17:53.171904Z","shell.execute_reply":"2023-12-18T17:17:53.175551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracies","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:18:04.370966Z","iopub.execute_input":"2023-12-18T17:18:04.371358Z","iopub.status.idle":"2023-12-18T17:18:04.377870Z","shell.execute_reply.started":"2023-12-18T17:18:04.371325Z","shell.execute_reply":"2023-12-18T17:18:04.376893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cf_matrix = confusion_matrix(preds_0['label'],\n                             OOFs_0)\n\nplt.figure(figsize = (10, 10))\n\nax = sns.heatmap(cf_matrix, annot=True, cmap='Blues', fmt = 'd')\n\nax.set_title('Seaborn Confusion Matrix with labels\\n\\n');\nax.set_xlabel('\\nPredicted Values')\nax.set_ylabel('Actual Values ');\n\nlabels = ['CBB', 'CBSD', 'CGM', 'CMD', 'Healthy']\nax.xaxis.set_ticklabels(labels, rotation = 90)\nax.yaxis.set_ticklabels(labels, rotation = 0)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:12:48.081534Z","iopub.execute_input":"2023-12-18T17:12:48.082402Z","iopub.status.idle":"2023-12-18T17:12:48.496151Z","shell.execute_reply.started":"2023-12-18T17:12:48.082363Z","shell.execute_reply":"2023-12-18T17:12:48.495153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nval_pred_0 = automl_0.predict(val_data)\nprint(f'Prediction for tr_data:\\n{val_pred_0}\\nShape = {val_pred_0.shape}')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:14:50.481582Z","iopub.execute_input":"2023-12-18T17:14:50.482221Z","iopub.status.idle":"2023-12-18T17:16:20.319744Z","shell.execute_reply.started":"2023-12-18T17:14:50.482191Z","shell.execute_reply":"2023-12-18T17:16:20.318533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_v0 = val_data[['image_id', 'label']]\npreds_v0","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:16:45.985741Z","iopub.execute_input":"2023-12-18T17:16:45.986179Z","iopub.status.idle":"2023-12-18T17:16:45.999269Z","shell.execute_reply.started":"2023-12-18T17:16:45.986137Z","shell.execute_reply":"2023-12-18T17:16:45.998233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    preds_v0['pred_v0_' + str(i)] = val_pred_0.data[:,i]\n\npreds_v0","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:16:50.770831Z","iopub.execute_input":"2023-12-18T17:16:50.771218Z","iopub.status.idle":"2023-12-18T17:16:50.791337Z","shell.execute_reply.started":"2023-12-18T17:16:50.771186Z","shell.execute_reply":"2023-12-18T17:16:50.790395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_v0 = preds_v0.groupby(['image_id', 'label']).mean().reset_index()\npreds_v0","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:16:55.271059Z","iopub.execute_input":"2023-12-18T17:16:55.271431Z","iopub.status.idle":"2023-12-18T17:16:55.302215Z","shell.execute_reply.started":"2023-12-18T17:16:55.271401Z","shell.execute_reply":"2023-12-18T17:16:55.301362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OOFs_v0 = np.argmax(preds_v0[['pred_v0_' + str(i) for i in range(5)]].values, axis = 1)\nOOFs_v0","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:16:58.771380Z","iopub.execute_input":"2023-12-18T17:16:58.771765Z","iopub.status.idle":"2023-12-18T17:16:58.780332Z","shell.execute_reply.started":"2023-12-18T17:16:58.771735Z","shell.execute_reply":"2023-12-18T17:16:58.779439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_v0 = (OOFs_v0 == preds_v0['label']).sum() / len(OOFs_v0)\nprint(f'Val_Out-of-fold accuracy: {accuracy_v0}')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:17:11.672175Z","iopub.execute_input":"2023-12-18T17:17:11.673053Z","iopub.status.idle":"2023-12-18T17:17:11.678845Z","shell.execute_reply.started":"2023-12-18T17:17:11.673014Z","shell.execute_reply":"2023-12-18T17:17:11.677775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracies.append([accuracy_v0, 'Val accuracy model default'])","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:18:19.181436Z","iopub.execute_input":"2023-12-18T17:18:19.181831Z","iopub.status.idle":"2023-12-18T17:18:19.186434Z","shell.execute_reply.started":"2023-12-18T17:18:19.181801Z","shell.execute_reply":"2023-12-18T17:18:19.185363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracies","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:18:35.272188Z","iopub.execute_input":"2023-12-18T17:18:35.272567Z","iopub.status.idle":"2023-12-18T17:18:35.278438Z","shell.execute_reply.started":"2023-12-18T17:18:35.272538Z","shell.execute_reply":"2023-12-18T17:18:35.277630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cf_matrix = confusion_matrix(preds_v0['label'],\n                             OOFs_v0)\n\nplt.figure(figsize = (10, 10))\n\nax = sns.heatmap(cf_matrix, annot=True, cmap='Blues', fmt = 'd')\n\nax.set_title('Seaborn Confusion Matrix with labels\\n\\n');\nax.set_xlabel('\\nPredicted Values')\nax.set_ylabel('Actual Values ');\n\nlabels = ['CBB', 'CBSD', 'CGM', 'CMD', 'Healthy']\nax.xaxis.set_ticklabels(labels, rotation = 90)\nax.yaxis.set_ticklabels(labels, rotation = 0)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:18:44.971032Z","iopub.execute_input":"2023-12-18T17:18:44.972059Z","iopub.status.idle":"2023-12-18T17:18:45.379671Z","shell.execute_reply.started":"2023-12-18T17:18:44.972020Z","shell.execute_reply":"2023-12-18T17:18:45.378775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model 2","metadata":{}},{"cell_type":"code","source":"automl_1 = TabularCVAutoML(task = task,\n                         timeout=5 * 3600,\n                         autocv_features={\"embed_model\": 'timm/tf_efficientnetv2_b0.in1k'},\n                        cpu_limit = 2,\n                        reader_params = {'cv': 3, 'random_state': 42})","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:18:52.172115Z","iopub.execute_input":"2023-12-18T17:18:52.172515Z","iopub.status.idle":"2023-12-18T17:18:52.218861Z","shell.execute_reply.started":"2023-12-18T17:18:52.172482Z","shell.execute_reply":"2023-12-18T17:18:52.217735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\noof_pred_1 = automl_1.fit_predict(tr_data, roles = roles, verbose = 3)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:18:56.870409Z","iopub.execute_input":"2023-12-18T17:18:56.870804Z","iopub.status.idle":"2023-12-18T17:24:16.743755Z","shell.execute_reply.started":"2023-12-18T17:18:56.870775Z","shell.execute_reply":"2023-12-18T17:24:16.742659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oof_pred_1.features","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:24:16.746154Z","iopub.execute_input":"2023-12-18T17:24:16.746541Z","iopub.status.idle":"2023-12-18T17:24:16.753911Z","shell.execute_reply.started":"2023-12-18T17:24:16.746500Z","shell.execute_reply":"2023-12-18T17:24:16.752953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oof_pred_1.data","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:24:16.755329Z","iopub.execute_input":"2023-12-18T17:24:16.755692Z","iopub.status.idle":"2023-12-18T17:24:16.767312Z","shell.execute_reply.started":"2023-12-18T17:24:16.755659Z","shell.execute_reply":"2023-12-18T17:24:16.766416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_1 = tr_data[['image_id', 'label']]\npreds_1","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:24:16.769882Z","iopub.execute_input":"2023-12-18T17:24:16.770197Z","iopub.status.idle":"2023-12-18T17:24:16.786818Z","shell.execute_reply.started":"2023-12-18T17:24:16.770168Z","shell.execute_reply":"2023-12-18T17:24:16.785968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    preds_1['pred_1_' + str(i)] = oof_pred_1.data[:,i]\n\npreds_1","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:24:16.787920Z","iopub.execute_input":"2023-12-18T17:24:16.788176Z","iopub.status.idle":"2023-12-18T17:24:16.810489Z","shell.execute_reply.started":"2023-12-18T17:24:16.788154Z","shell.execute_reply":"2023-12-18T17:24:16.809500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_1 = preds_1.groupby(['image_id', 'label']).mean().reset_index()\npreds_1","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:24:16.811785Z","iopub.execute_input":"2023-12-18T17:24:16.812113Z","iopub.status.idle":"2023-12-18T17:24:16.870800Z","shell.execute_reply.started":"2023-12-18T17:24:16.812083Z","shell.execute_reply":"2023-12-18T17:24:16.869864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OOFs_1 = np.argmax(preds_1[['pred_1_' + str(i) for i in range(5)]].values, axis = 1)\nOOFs_1","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:24:16.871940Z","iopub.execute_input":"2023-12-18T17:24:16.872232Z","iopub.status.idle":"2023-12-18T17:24:16.880380Z","shell.execute_reply.started":"2023-12-18T17:24:16.872203Z","shell.execute_reply":"2023-12-18T17:24:16.879379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_1 = (OOFs_1 == preds_1['label']).sum() / len(OOFs_1)\nprint(f'Train Out-of-fold accuracy: {accuracy_1}')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:24:16.881555Z","iopub.execute_input":"2023-12-18T17:24:16.881962Z","iopub.status.idle":"2023-12-18T17:24:16.890749Z","shell.execute_reply.started":"2023-12-18T17:24:16.881937Z","shell.execute_reply":"2023-12-18T17:24:16.889777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracies.append([accuracy_1, 'Train accuracy model timm/tf_efficientnetv2_b0.in1k'])\naccuracies","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:24:16.892090Z","iopub.execute_input":"2023-12-18T17:24:16.892391Z","iopub.status.idle":"2023-12-18T17:24:16.902611Z","shell.execute_reply.started":"2023-12-18T17:24:16.892367Z","shell.execute_reply":"2023-12-18T17:24:16.901384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cf_matrix = confusion_matrix(preds_1['label'],\n                             OOFs_1)\n\nplt.figure(figsize = (10, 10))\n\nax = sns.heatmap(cf_matrix, annot=True, cmap='Blues', fmt = 'd')\n\nax.set_title('Seaborn Confusion Matrix with labels\\n\\n');\nax.set_xlabel('\\nPredicted Values')\nax.set_ylabel('Actual Values ');\n\nlabels = ['CBB', 'CBSD', 'CGM', 'CMD', 'Healthy']\nax.xaxis.set_ticklabels(labels, rotation = 90)\nax.yaxis.set_ticklabels(labels, rotation = 0)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:24:16.905668Z","iopub.execute_input":"2023-12-18T17:24:16.906041Z","iopub.status.idle":"2023-12-18T17:24:17.321885Z","shell.execute_reply.started":"2023-12-18T17:24:16.906011Z","shell.execute_reply":"2023-12-18T17:24:17.320945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nval_pred_1 = automl_1.predict(val_data)\nprint(f'Prediction for te_data:\\n{val_pred_1}\\nShape = {val_pred_1.shape}')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:24:17.323089Z","iopub.execute_input":"2023-12-18T17:24:17.323486Z","iopub.status.idle":"2023-12-18T17:25:19.398389Z","shell.execute_reply.started":"2023-12-18T17:24:17.323433Z","shell.execute_reply":"2023-12-18T17:25:19.397272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_v1 = val_data[['image_id', 'label']]\npreds_v1","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:27:12.371186Z","iopub.execute_input":"2023-12-18T17:27:12.371739Z","iopub.status.idle":"2023-12-18T17:27:12.386949Z","shell.execute_reply.started":"2023-12-18T17:27:12.371702Z","shell.execute_reply":"2023-12-18T17:27:12.385834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    preds_v1['pred_v1_' + str(i)] = val_pred_1.data[:,i]\n\npreds_v1","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:27:16.571416Z","iopub.execute_input":"2023-12-18T17:27:16.572385Z","iopub.status.idle":"2023-12-18T17:27:16.591341Z","shell.execute_reply.started":"2023-12-18T17:27:16.572349Z","shell.execute_reply":"2023-12-18T17:27:16.590494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_v1 = preds_v1.groupby(['image_id', 'label']).mean().reset_index()\npreds_v1","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:27:19.770249Z","iopub.execute_input":"2023-12-18T17:27:19.771145Z","iopub.status.idle":"2023-12-18T17:27:19.799547Z","shell.execute_reply.started":"2023-12-18T17:27:19.771107Z","shell.execute_reply":"2023-12-18T17:27:19.798656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OOFs_v1 = np.argmax(preds_v1[['pred_v1_' + str(i) for i in range(5)]].values, axis = 1)\nOOFs_v1","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:27:22.675974Z","iopub.execute_input":"2023-12-18T17:27:22.676352Z","iopub.status.idle":"2023-12-18T17:27:22.685498Z","shell.execute_reply.started":"2023-12-18T17:27:22.676321Z","shell.execute_reply":"2023-12-18T17:27:22.684431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_v1 = (OOFs_v1 == preds_v1['label']).sum() / len(OOFs_v1)\nprint(f'Val_Out-of-fold accuracy: {accuracy_v1}')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:27:25.395598Z","iopub.execute_input":"2023-12-18T17:27:25.396604Z","iopub.status.idle":"2023-12-18T17:27:25.403525Z","shell.execute_reply.started":"2023-12-18T17:27:25.396564Z","shell.execute_reply":"2023-12-18T17:27:25.402382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracies.append([accuracy_v1, 'Val accuracy model timm/tf_efficientnetv2_b0.in1k'])\naccuracies","metadata":{"execution":{"iopub.status.busy":"2023-12-18T17:27:27.697327Z","iopub.execute_input":"2023-12-18T17:27:27.698242Z","iopub.status.idle":"2023-12-18T17:27:27.704710Z","shell.execute_reply.started":"2023-12-18T17:27:27.698206Z","shell.execute_reply":"2023-12-18T17:27:27.703718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cf_matrix = confusion_matrix(preds_v1['label'],\n                             OOFs_v1)\n\nplt.figure(figsize = (10, 10))\n\nax = sns.heatmap(cf_matrix, annot=True, cmap='Blues', fmt = 'd')\n\nax.set_title('Seaborn Confusion Matrix with labels\\n\\n');\nax.set_xlabel('\\nPredicted Values')\nax.set_ylabel('Actual Values ');\n\nlabels = ['CBB', 'CBSD', 'CGM', 'CMD', 'Healthy']\nax.xaxis.set_ticklabels(labels, rotation = 90)\nax.yaxis.set_ticklabels(labels, rotation = 0)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-17T18:54:07.404796Z","iopub.execute_input":"2023-12-17T18:54:07.405158Z","iopub.status.idle":"2023-12-17T18:54:07.747485Z","shell.execute_reply.started":"2023-12-17T18:54:07.405132Z","shell.execute_reply":"2023-12-17T18:54:07.746564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"В следующем ноутбуке буду пытаться побивать бейзлайн модели, скор на валидационном датасете accuracy = 0.8519541206457094","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}