{"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\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\nfrom tensorflow.keras import layers\nfrom tensorflow import keras\nimport pandas as pd\nimport numpy as np\nimport tensorflow_io as tfio","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:07:56.037836Z","iopub.execute_input":"2022-09-11T05:07:56.038503Z","iopub.status.idle":"2022-09-11T05:08:01.818075Z","shell.execute_reply.started":"2022-09-11T05:07:56.03841Z","shell.execute_reply":"2022-09-11T05:08:01.817029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gpus = tf.config.list_physical_devices('GPU')\ntf.config.set_visible_devices(gpus[0], 'GPU')","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:08:01.821826Z","iopub.execute_input":"2022-09-11T05:08:01.822818Z","iopub.status.idle":"2022-09-11T05:08:02.023446Z","shell.execute_reply.started":"2022-09-11T05:08:01.822787Z","shell.execute_reply":"2022-09-11T05:08:02.020199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:08:02.025035Z","iopub.execute_input":"2022-09-11T05:08:02.025811Z","iopub.status.idle":"2022-09-11T05:08:02.040002Z","shell.execute_reply.started":"2022-09-11T05:08:02.025734Z","shell.execute_reply":"2022-09-11T05:08:02.038834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/mayo-clinic-strip-ai/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:08:02.04243Z","iopub.execute_input":"2022-09-11T05:08:02.04272Z","iopub.status.idle":"2022-09-11T05:08:02.059515Z","shell.execute_reply.started":"2022-09-11T05:08:02.042692Z","shell.execute_reply":"2022-09-11T05:08:02.058642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label = train_df[\"label\"].values\nfile_paths = train_df[\"image_id\"].values","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:08:02.06081Z","iopub.execute_input":"2022-09-11T05:08:02.061691Z","iopub.status.idle":"2022-09-11T05:08:02.071063Z","shell.execute_reply.started":"2022-09-11T05:08:02.061654Z","shell.execute_reply":"2022-09-11T05:08:02.07004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\ntrain_label = le.fit_transform(train_label)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:08:02.07241Z","iopub.execute_input":"2022-09-11T05:08:02.072914Z","iopub.status.idle":"2022-09-11T05:08:02.767824Z","shell.execute_reply.started":"2022-09-11T05:08:02.07288Z","shell.execute_reply":"2022-09-11T05:08:02.766967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label = train_label.reshape(754,1)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:08:02.769249Z","iopub.execute_input":"2022-09-11T05:08:02.77045Z","iopub.status.idle":"2022-09-11T05:08:02.774976Z","shell.execute_reply.started":"2022-09-11T05:08:02.770414Z","shell.execute_reply":"2022-09-11T05:08:02.773989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_train_itr = tf.data.Dataset.from_tensor_slices((file_paths,train_label.reshape(-1,1)))\ndef read_image(image_path,label):\n    image = tf.io.read_file(\"../input/mayo-clinic-strip-ai/train/\"+image_path+\".tif\")\n    image = tfio.experimental.image.decode_tiff(image, index=0, name=None)\n    #image = tf.io.decode_image(image,channels=3,dtype=tf.dtypes.uint8)\n    image = tf.image.resize(image, [299, 299])\n    \n    return image,label\nds_train = ds_train_itr.map(read_image).batch(1)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:08:41.100199Z","iopub.execute_input":"2022-09-11T05:08:41.101288Z","iopub.status.idle":"2022-09-11T05:08:41.122886Z","shell.execute_reply.started":"2022-09-11T05:08:41.101241Z","shell.execute_reply":"2022-09-11T05:08:41.12202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = tf.io.read_file(\"../input/mayo-clinic-strip-ai/train/006388_0.tif\")\nimg = tfio.experimental.image.decode_tiff(img, index=0, name=None)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#img","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_train","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:08:53.775978Z","iopub.execute_input":"2022-09-11T05:08:53.77636Z","iopub.status.idle":"2022-09-11T05:08:53.783384Z","shell.execute_reply.started":"2022-09-11T05:08:53.776327Z","shell.execute_reply":"2022-09-11T05:08:53.782324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_ir = tf.keras.applications.inception_resnet_v2.InceptionResNetV2(\n    include_top=True,\n    weights='imagenet',\n)\nmodel_ir.trainable = False\nbase_input = model_ir.layers[0].input\nbase_output = model_ir.layers[-1].output\nfinal_output = layers.Dense(1,activation=\"sigmoid\")(base_output)\nfinal_model_ir = keras.Model(inputs = base_input,outputs = final_output)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:09:17.584931Z","iopub.execute_input":"2022-09-11T05:09:17.585958Z","iopub.status.idle":"2022-09-11T05:09:23.860471Z","shell.execute_reply.started":"2022-09-11T05:09:17.585922Z","shell.execute_reply":"2022-09-11T05:09:23.859461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model_ir.summary","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:10:52.64022Z","iopub.execute_input":"2022-09-11T05:10:52.640572Z","iopub.status.idle":"2022-09-11T05:10:52.648959Z","shell.execute_reply.started":"2022-09-11T05:10:52.640542Z","shell.execute_reply":"2022-09-11T05:10:52.647825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model_ir.compile(\noptimizer= keras.optimizers.Adam(learning_rate=0.001),\nloss = keras.losses.binary_crossentropy,\nmetrics=[\"accuracy\"],\n)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:10:55.439458Z","iopub.execute_input":"2022-09-11T05:10:55.439853Z","iopub.status.idle":"2022-09-11T05:10:55.465301Z","shell.execute_reply.started":"2022-09-11T05:10:55.439818Z","shell.execute_reply":"2022-09-11T05:10:55.46434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model_ir.fit(ds_train,batch_size=8, epochs=10,verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential( \n    [\n        keras.Input(shape = (240,240,3)),\n        layers.Conv2D(32,3,padding=\"valid\",activation=\"relu\"),\n        layers.MaxPooling2D(pool_size=(2,2)),\n        layers.Conv2D(64,3,padding=\"valid\",activation=\"relu\"),\n        layers.MaxPooling2D(pool_size=(2,2)),\n        layers.Flatten(),\n        layers.Dense(160,activation=\"relu\"),\n        layers.Dense(3),\n    ]\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\noptimizer= keras.optimizers.Adam(learning_rate=0.001),\nloss = keras.losses.binary_crossentropy,\nmetrics=[\"accuracy\"]\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(ds_train,batch_size=64, epochs=10,verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}