{"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":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport matplotlib.image as mpimg\n\nfrom sklearn.model_selection import train_test_split\n\nimport pickle\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-07T21:17:32.909011Z","iopub.execute_input":"2021-11-07T21:17:32.909279Z","iopub.status.idle":"2021-11-07T21:17:32.916157Z","shell.execute_reply.started":"2021-11-07T21:17:32.909222Z","shell.execute_reply":"2021-11-07T21:17:32.915192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(f'../input/histopathologic-cancer-detection/train_labels.csv', dtype=str)\n\nprint('Training Set Size:', train.shape)\n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-07T21:20:36.582412Z","iopub.execute_input":"2021-11-07T21:20:36.583255Z","iopub.status.idle":"2021-11-07T21:20:36.834125Z","shell.execute_reply.started":"2021-11-07T21:20:36.583204Z","shell.execute_reply":"2021-11-07T21:20:36.833078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lets play with 1% data to check if all code works\n# Comment this when running the entire code\nignore, train = train_test_split(train, test_size=0.005, random_state=1, stratify=train.label)\nprint('Training Set Size:', train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-07T21:20:37.237350Z","iopub.execute_input":"2021-11-07T21:20:37.237666Z","iopub.status.idle":"2021-11-07T21:20:37.587065Z","shell.execute_reply.started":"2021-11-07T21:20:37.237631Z","shell.execute_reply":"2021-11-07T21:20:37.586034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['id'] = train['id'].apply(lambda x: f'{x}.tif')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-07T21:20:47.589135Z","iopub.execute_input":"2021-11-07T21:20:47.589471Z","iopub.status.idle":"2021-11-07T21:20:47.602127Z","shell.execute_reply.started":"2021-11-07T21:20:47.589438Z","shell.execute_reply":"2021-11-07T21:20:47.601341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"../input/histopathologic-cancer-detection/train\"\nprint('Training Images:', len(os.listdir(train_path)))\n\nsample = train.iloc[0:1,].reset_index()","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:22:38.847230Z","iopub.execute_input":"2021-11-07T22:22:38.847595Z","iopub.status.idle":"2021-11-07T22:22:39.108191Z","shell.execute_reply.started":"2021-11-07T22:22:38.847561Z","shell.execute_reply":"2021-11-07T22:22:39.107298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_image_shape = (96, 96, 3)\nmodel = Sequential()\nmodel.add(Cropping2D(cropping=((0, 0), (0, 0)), input_shape=input_image_shape))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:45:40.745631Z","iopub.execute_input":"2021-11-07T22:45:40.745948Z","iopub.status.idle":"2021-11-07T22:45:40.766435Z","shell.execute_reply.started":"2021-11-07T22:45:40.745916Z","shell.execute_reply":"2021-11-07T22:45:40.765318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator() #rescale=1/255\nBATCH_SIZE = 64\ntrain_loader = train_datagen.flow_from_dataframe(\n    dataframe = sample,\n    directory = train_path,\n    x_col = 'id',\n    y_col = 'label',\n    batch_size = BATCH_SIZE,\n    seed = 1,\n    shuffle = True,\n    class_mode = 'categorical',\n    target_size = (96,96)\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-07T23:00:38.624974Z","iopub.execute_input":"2021-11-07T23:00:38.625450Z","iopub.status.idle":"2021-11-07T23:00:38.637127Z","shell.execute_reply.started":"2021-11-07T23:00:38.625417Z","shell.execute_reply":"2021-11-07T23:00:38.636387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model_inputs = np.array(train)\nsample_cropped = model.predict(x = train_loader)","metadata":{"execution":{"iopub.status.busy":"2021-11-07T23:00:39.437840Z","iopub.execute_input":"2021-11-07T23:00:39.438617Z","iopub.status.idle":"2021-11-07T23:00:39.550830Z","shell.execute_reply.started":"2021-11-07T23:00:39.438584Z","shell.execute_reply":"2021-11-07T23:00:39.550025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_output = sample_cropped[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-07T23:00:40.119541Z","iopub.execute_input":"2021-11-07T23:00:40.119846Z","iopub.status.idle":"2021-11-07T23:00:40.124430Z","shell.execute_reply.started":"2021-11-07T23:00:40.119812Z","shell.execute_reply":"2021-11-07T23:00:40.123406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,8))\n\nfor i, row in sample.iterrows():\n    print(f'{i} {row.id}')\n    img = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{row.id}')    \n    label = row.label\n\n    plt.subplot(1,2,i+1)\n    plt.imshow(img)\n    plt.text(0, -5, f'Class {label}', color='k')\n\n    plt.subplot(1,2,i+2)\n    plt.imshow(sample_output[:, :, 0])\n    plt.text(0, -5, f'Class {label}', color='k')\n        \n    plt.axis('off')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-11-07T23:04:48.555680Z","iopub.execute_input":"2021-11-07T23:04:48.555995Z","iopub.status.idle":"2021-11-07T23:04:48.986757Z","shell.execute_reply.started":"2021-11-07T23:04:48.555956Z","shell.execute_reply":"2021-11-07T23:04:48.985994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_loader[0][0][0].shape)\nprint(sample_output.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-07T23:01:09.609185Z","iopub.execute_input":"2021-11-07T23:01:09.609515Z","iopub.status.idle":"2021-11-07T23:01:09.619169Z","shell.execute_reply.started":"2021-11-07T23:01:09.609482Z","shell.execute_reply":"2021-11-07T23:01:09.617995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_output[0][0]","metadata":{"execution":{"iopub.status.busy":"2021-11-07T23:00:42.254126Z","iopub.execute_input":"2021-11-07T23:00:42.254456Z","iopub.status.idle":"2021-11-07T23:00:42.261673Z","shell.execute_reply.started":"2021-11-07T23:00:42.254416Z","shell.execute_reply":"2021-11-07T23:00:42.260793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader[0][0][0][0][0]","metadata":{"execution":{"iopub.status.busy":"2021-11-07T23:00:47.581832Z","iopub.execute_input":"2021-11-07T23:00:47.582294Z","iopub.status.idle":"2021-11-07T23:00:47.592973Z","shell.execute_reply.started":"2021-11-07T23:00:47.582220Z","shell.execute_reply":"2021-11-07T23:00:47.591898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}