{"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\n#for 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","execution":{"iopub.status.busy":"2023-06-01T06:28:20.706890Z","iopub.execute_input":"2023-06-01T06:28:20.707294Z","iopub.status.idle":"2023-06-01T06:28:21.071587Z","shell.execute_reply.started":"2023-06-01T06:28:20.707264Z","shell.execute_reply":"2023-06-01T06:28:21.070598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:28:21.073363Z","iopub.execute_input":"2023-06-01T06:28:21.073732Z","iopub.status.idle":"2023-06-01T06:36:21.552096Z","shell.execute_reply.started":"2023-06-01T06:28:21.073707Z","shell.execute_reply":"2023-06-01T06:36:21.551022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\n\n\nimg = Image.open(r'/kaggle/input/histopathologic-cancer-detection/train/000020de2aa6193f4c160e398a8edea95b1da598.tif')\nimg = np.array(img); print(img.shape)\nplt.imshow(img[:,:,1])\n","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:36:21.553384Z","iopub.execute_input":"2023-06-01T06:36:21.553893Z","iopub.status.idle":"2023-06-01T06:36:23.197497Z","shell.execute_reply.started":"2023-06-01T06:36:21.553860Z","shell.execute_reply":"2023-06-01T06:36:23.196425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fil_lst= os.listdir(\"/kaggle/input/histopathologic-cancer-detection/train\")\ntrain_files = fil_lst[0:150000]; \ntrain_size = 500\nx_train = np.empty((train_size,96,96,3))\nfor i in range(train_size):\n    img = Image.open(r'/kaggle/input/histopathologic-cancer-detection/train/'+train_files[i])\n    x_train[i,:,:,:] = np.array(img);\n\ndf = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/train_labels.csv')\nprint(df.head())\ny_data = np.array(df['label']); \ny_train = y_data[:train_size]","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:36:23.200074Z","iopub.execute_input":"2023-06-01T06:36:23.200362Z","iopub.status.idle":"2023-06-01T06:36:29.308321Z","shell.execute_reply.started":"2023-06-01T06:36:23.200335Z","shell.execute_reply":"2023-06-01T06:36:29.307156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fil_lst_tst= os.listdir(\"/kaggle/input/histopathologic-cancer-detection/test\")\ntrain_files_tst = fil_lst_tst[:]; \ntrain_size_tst = len(train_files_tst)\nx_test = np.empty((train_size_tst,96,96,3))\nfor i in range(train_size_tst):\n    img = Image.open(r'/kaggle/input/histopathologic-cancer-detection/test/'+train_files_tst[i])\n    x_test[i,:,:,:] = np.array(img);\n\ntest = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/sample_submission.csv')\nprint(test.head())","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:36:29.309688Z","iopub.execute_input":"2023-06-01T06:36:29.309999Z","iopub.status.idle":"2023-06-01T06:43:58.617648Z","shell.execute_reply.started":"2023-06-01T06:36:29.309974Z","shell.execute_reply":"2023-06-01T06:43:58.616674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l1_reg= tf.keras.regularizers.L2(0.0007)\nimg_inputs = keras.Input(shape=(96, 96, 3))\nx1= keras.layers.Conv2D(50,(3,3),padding=\"same\",activation='relu',strides=1,kernel_initializer=\"glorot_uniform\",kernel_regularizer=l1_reg)(img_inputs)\nx1= keras.layers.MaxPool2D(2)(x1)\nx1= keras.layers.Conv2D(50,(3,3),padding=\"same\",activation='relu',strides=1,kernel_initializer=\"glorot_uniform\",kernel_regularizer=l1_reg)(x1)\nx2= keras.layers.Conv2D(50,(5,5),padding=\"same\",activation='relu',strides=1,kernel_initializer=\"glorot_uniform\",kernel_regularizer=l1_reg)(img_inputs)\nx2= keras.layers.MaxPool2D(2)(x2)\nx2= keras.layers.Conv2D(50,(5,5),padding=\"same\",activation='relu',strides=1,kernel_initializer=\"glorot_uniform\",kernel_regularizer=l1_reg)(x2)\nx = keras.layers.Concatenate()([x1,x2])\nx=  keras.layers.Conv2D(100,(3,3),padding=\"same\",activation='relu',strides=1,kernel_initializer=\"glorot_uniform\",kernel_regularizer=l1_reg)(x)\nx= keras.layers.MaxPool2D(2)(x)\nx= keras.layers.Conv2D(100,(3,3),padding=\"same\",strides=1,activation='relu',kernel_initializer=\"glorot_uniform\",kernel_regularizer=l1_reg)(x)\nx= keras.layers.Flatten()(x)\nx= keras.layers.Dense(1000,activation='relu',kernel_initializer=\"glorot_uniform\",kernel_regularizer=l1_reg)(x)\nx= keras.layers.Dense(200,activation='relu',kernel_initializer=\"glorot_uniform\",kernel_regularizer=l1_reg)(x)\noutputs= keras.layers.Dense(1,activation='sigmoid')(x)\n\nmodel = keras.Model(inputs=img_inputs, outputs=outputs)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:58.618904Z","iopub.execute_input":"2023-06-01T06:43:58.619188Z","iopub.status.idle":"2023-06-01T06:44:02.663281Z","shell.execute_reply.started":"2023-06-01T06:43:58.619165Z","shell.execute_reply":"2023-06-01T06:44:02.662386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = tf.keras.optimizers.SGD(learning_rate=0.0001)\nlos= tf.keras.losses.BinaryCrossentropy()\nmodel.compile(optimizer=opt,loss=los, metrics=['Accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:44:02.664525Z","iopub.execute_input":"2023-06-01T06:44:02.664898Z","iopub.status.idle":"2023-06-01T06:44:02.684951Z","shell.execute_reply.started":"2023-06-01T06:44:02.664862Z","shell.execute_reply":"2023-06-01T06:44:02.684043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train= tf.convert_to_tensor(x_train); y_train= tf.convert_to_tensor(y_train)\nmodel.fit(x_train,y_train,batch_size=50,epochs=50,validation_split=0.1)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:44:02.686095Z","iopub.execute_input":"2023-06-01T06:44:02.686482Z","iopub.status.idle":"2023-06-01T06:48:46.101842Z","shell.execute_reply.started":"2023-06-01T06:44:02.686456Z","shell.execute_reply":"2023-06-01T06:48:46.100591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ny_pred = model.predict(x_test, batch_size=20)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:48:46.103334Z","iopub.execute_input":"2023-06-01T06:48:46.103952Z","iopub.status.idle":"2023-06-01T06:50:17.073115Z","shell.execute_reply.started":"2023-06-01T06:48:46.103921Z","shell.execute_reply":"2023-06-01T06:50:17.071984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"label\"] = y_pred","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:50:17.076197Z","iopub.execute_input":"2023-06-01T06:50:17.076536Z","iopub.status.idle":"2023-06-01T06:50:17.081363Z","shell.execute_reply.started":"2023-06-01T06:50:17.076510Z","shell.execute_reply":"2023-06-01T06:50:17.080547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test.loc[25:50])","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:50:17.082304Z","iopub.execute_input":"2023-06-01T06:50:17.082556Z","iopub.status.idle":"2023-06-01T06:50:17.097426Z","shell.execute_reply.started":"2023-06-01T06:50:17.082535Z","shell.execute_reply":"2023-06-01T06:50:17.096389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ny_prd = np.rint(y_pred);\nprint(y_prd[:10])","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:50:17.098565Z","iopub.execute_input":"2023-06-01T06:50:17.099009Z","iopub.status.idle":"2023-06-01T06:50:17.106232Z","shell.execute_reply.started":"2023-06-01T06:50:17.098984Z","shell.execute_reply":"2023-06-01T06:50:17.105360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"label\"] = y_prd","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:50:17.107359Z","iopub.execute_input":"2023-06-01T06:50:17.107756Z","iopub.status.idle":"2023-06-01T06:50:17.114174Z","shell.execute_reply.started":"2023-06-01T06:50:17.107728Z","shell.execute_reply":"2023-06-01T06:50:17.113412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:50:17.115096Z","iopub.execute_input":"2023-06-01T06:50:17.115424Z","iopub.status.idle":"2023-06-01T06:50:17.298148Z","shell.execute_reply.started":"2023-06-01T06:50:17.115402Z","shell.execute_reply":"2023-06-01T06:50:17.296987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}