{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":10418,"databundleVersionId":862236,"sourceType":"competition"}],"dockerImageVersionId":30761,"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nfrom tensorflow.keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, GlobalMaxPooling2D\nfrom tensorflow.keras.models import Model, load_model\nfrom tensorflow.keras.applications.resnet_v2 import ResNet50V2,preprocess_input,decode_predictions","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:21:20.172303Z","iopub.execute_input":"2024-08-22T16:21:20.172798Z","iopub.status.idle":"2024-08-22T16:21:33.451185Z","shell.execute_reply.started":"2024-08-22T16:21:20.172742Z","shell.execute_reply":"2024-08-22T16:21:33.449514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = ResNet50V2()","metadata":{"execution":{"iopub.status.busy":"2024-08-22T14:43:23.167726Z","iopub.execute_input":"2024-08-22T14:43:23.168199Z","iopub.status.idle":"2024-08-22T14:43:29.176343Z","shell.execute_reply.started":"2024-08-22T14:43:23.168156Z","shell.execute_reply":"2024-08-22T14:43:29.174742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.summary()","metadata":{"execution":{"iopub.status.busy":"2024-08-22T14:44:06.086749Z","iopub.execute_input":"2024-08-22T14:44:06.087973Z","iopub.status.idle":"2024-08-22T14:44:06.423461Z","shell.execute_reply.started":"2024-08-22T14:44:06.087886Z","shell.execute_reply":"2024-08-22T14:44:06.422158Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:21:11.391208Z","iopub.execute_input":"2024-08-22T16:21:11.392253Z","iopub.status.idle":"2024-08-22T16:21:12.472212Z","shell.execute_reply.started":"2024-08-22T16:21:11.392205Z","shell.execute_reply":"2024-08-22T16:21:12.470984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataframe = pd.read_csv(\"/kaggle/input/human-protein-atlas-image-classification/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:21:12.474147Z","iopub.execute_input":"2024-08-22T16:21:12.474671Z","iopub.status.idle":"2024-08-22T16:21:12.559678Z","shell.execute_reply.started":"2024-08-22T16:21:12.474629Z","shell.execute_reply":"2024-08-22T16:21:12.558319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataframe.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:21:13.245256Z","iopub.execute_input":"2024-08-22T16:21:13.245696Z","iopub.status.idle":"2024-08-22T16:21:13.273769Z","shell.execute_reply.started":"2024-08-22T16:21:13.245657Z","shell.execute_reply":"2024-08-22T16:21:13.271658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = dataframe[\"Target\"].str.get_dummies(\" \")\nx=  x.reindex(sorted(x.columns,key=int),axis=1)\ndf = pd.concat([dataframe['Id'],x],axis=1)\nclass_tensor = tf.convert_to_tensor(x.values,dtype=tf.float32)\n\npairs = list(zip(df[\"Id\"],class_tensor))\n\nfinal_df = pd.DataFrame({\n    'id': [i for i,_ in pairs],\n    'class_tensor': [t for _,t in pairs]\n}) \nfinal_df.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:21:33.453503Z","iopub.execute_input":"2024-08-22T16:21:33.454384Z","iopub.status.idle":"2024-08-22T16:21:43.379741Z","shell.execute_reply.started":"2024-08-22T16:21:33.454299Z","shell.execute_reply":"2024-08-22T16:21:43.378420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAINING_SET_DIR = \"/kaggle/input/human-protein-atlas-image-classification/train\"","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:21:43.390554Z","iopub.execute_input":"2024-08-22T16:21:43.390989Z","iopub.status.idle":"2024-08-22T16:21:43.420418Z","shell.execute_reply.started":"2024-08-22T16:21:43.390918Z","shell.execute_reply":"2024-08-22T16:21:43.419065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:21:43.382298Z","iopub.execute_input":"2024-08-22T16:21:43.382834Z","iopub.status.idle":"2024-08-22T16:21:43.388993Z","shell.execute_reply.started":"2024-08-22T16:21:43.382776Z","shell.execute_reply":"2024-08-22T16:21:43.387518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image(image_id):\n    image_id = tf.strings.as_string(image_id)\n    image_path = tf.strings.join([TRAINING_SET_DIR,\"/\",image_id,f\"{image_id}_green.png\"])\n    \n    image = tf.io.read_file(image_path)\n    \n    image = tf.image.decode_png(image,channels=3)\n    \n    image = tf.image.resize(image,[512,512])\n    image = image/255.0\n    return image","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:33:44.059864Z","iopub.execute_input":"2024-08-22T16:33:44.060410Z","iopub.status.idle":"2024-08-22T16:33:44.068382Z","shell.execute_reply.started":"2024-08-22T16:33:44.060362Z","shell.execute_reply":"2024-08-22T16:33:44.067070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df['id']","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:26:23.629753Z","iopub.execute_input":"2024-08-22T16:26:23.630285Z","iopub.status.idle":"2024-08-22T16:26:23.642825Z","shell.execute_reply.started":"2024-08-22T16:26:23.630235Z","shell.execute_reply":"2024-08-22T16:26:23.640968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z = list(final_df['id'])","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:26:26.176601Z","iopub.execute_input":"2024-08-22T16:26:26.177125Z","iopub.status.idle":"2024-08-22T16:26:26.189566Z","shell.execute_reply.started":"2024-08-22T16:26:26.177077Z","shell.execute_reply":"2024-08-22T16:26:26.187746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z[:10]","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:26:41.977159Z","iopub.execute_input":"2024-08-22T16:26:41.977794Z","iopub.status.idle":"2024-08-22T16:26:41.986701Z","shell.execute_reply.started":"2024-08-22T16:26:41.977738Z","shell.execute_reply":"2024-08-22T16:26:41.985265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = tf.data.Dataset.from_tensor_slices(z)","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:33:46.736776Z","iopub.execute_input":"2024-08-22T16:33:46.738204Z","iopub.status.idle":"2024-08-22T16:33:46.862242Z","shell.execute_reply.started":"2024-08-22T16:33:46.738148Z","shell.execute_reply":"2024-08-22T16:33:46.860575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = dataset.map(load_image,num_parallel_calls=tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:33:46.949440Z","iopub.execute_input":"2024-08-22T16:33:46.949951Z","iopub.status.idle":"2024-08-22T16:33:47.094972Z","shell.execute_reply.started":"2024-08-22T16:33:46.949889Z","shell.execute_reply":"2024-08-22T16:33:47.093576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image in dataset.take(1):\n    print(f\"shape = {image.shape}\")","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:33:47.238339Z","iopub.execute_input":"2024-08-22T16:33:47.238776Z","iopub.status.idle":"2024-08-22T16:33:47.374016Z","shell.execute_reply.started":"2024-08-22T16:33:47.238735Z","shell.execute_reply":"2024-08-22T16:33:47.371873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:16:12.228379Z","iopub.execute_input":"2024-08-22T16:16:12.228804Z","iopub.status.idle":"2024-08-22T16:16:12.234400Z","shell.execute_reply.started":"2024-08-22T16:16:12.228765Z","shell.execute_reply":"2024-08-22T16:16:12.232930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor i in tqdm(range(len(z))):\n    img_id = z[i]\n    image_path = tf.strings.join([TRAINING_SET_DIR,\"/\",,f\"{img_id}_green.png\"])\n    \n    image = tf.io.read_file(image_path)\n    \n    image = tf.image.decode_png(image,channels=3)\n    \n    image = tf.image.resize(image,(512,512))\n    All_images.append(image)","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:16:15.034675Z","iopub.execute_input":"2024-08-22T16:16:15.035185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = tf.data.Dataset.from_tensor_slices(\n    z\n)\n\ndataset","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:30:37.871413Z","iopub.execute_input":"2024-08-22T16:30:37.871917Z","iopub.status.idle":"2024-08-22T16:30:38.080057Z","shell.execute_reply.started":"2024-08-22T16:30:37.871872Z","shell.execute_reply":"2024-08-22T16:30:38.078061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = dataset.map(load_image)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:05:37.286096Z","iopub.execute_input":"2024-08-22T16:05:37.286569Z","iopub.status.idle":"2024-08-22T16:05:37.313318Z","shell.execute_reply.started":"2024-08-22T16:05:37.286527Z","shell.execute_reply":"2024-08-22T16:05:37.311702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in dataset.take(1):\n    print(f\"shape = {i.shape}\")","metadata":{"execution":{"iopub.status.busy":"2024-08-22T16:05:39.760259Z","iopub.execute_input":"2024-08-22T16:05:39.760698Z","iopub.status.idle":"2024-08-22T16:05:39.882575Z","shell.execute_reply.started":"2024-08-22T16:05:39.760660Z","shell.execute_reply":"2024-08-22T16:05:39.881038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}