{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"print(len(os.listdir('/kaggle/input/histopathologic-cancer-detection/train')))\nprint(len(os.listdir('/kaggle/input/histopathologic-cancer-detection/test')))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/train_labels.csv',dtype=str)\ntrain[train['id'] != 'dd6dfed324f9fcb6f93f46f32fc800f2ec196be2']\ntrain[train['id'] != '9369c7278ec8bcc6c880d99194de09fc2bd4efbe']\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"t0 = train[train['label']==0].sample(80000)\nt1 = train[train['label']==1].sample(80000)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.utils import shuffle\ntrain = shuffle(pd.concat([t0,t1],axis=0)).reset_index(drop = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ny = train['label']\ndf_train, df_val = train_test_split(train, test_size=0.10, random_state=101, stratify=y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(preprocessing_function=lambda x:(x - x.mean()) / x.std() if x.std() > 0 else x,\n                                   horizontal_flip=True,\n                                   vertical_flip=True)\n\ntest_datagen = ImageDataGenerator(preprocessing_function=lambda x:(x - x.mean()) / x.std() if x.std() > 0 else x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory(\n        dataframe = df_train,\n        directory='/kaggle/input/histopathologic-cancer-detection/test',\n        x_col='id',\n        y_col='label',\n        target_size=(96, 96),\n        batch_size=32,\n        class_mode='binary')\n\nvalidation_generator = test_datagen.flow_from_directory(\n        dataframe = df_val,\n        directory = '/kaggle/input/histopathologic-cancer-detection/test',\n        x_col='id',\n        y_col='label',\n        target_size=(96, 96),\n        batch_size=32,\n        class_mode='binary')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe(\n        dataframe = df_train,\n        directory='/kaggle/input/histopathologic-cancer-detection/train',\n    x_col='id',\n    y_col='label',\n#     subset='training',\n    batch_size=32,\n    seed=2018,\n    shuffle=True,\n    class_mode='binary',\n    target_size=(96,96))\n\nvalid_generator = test_datagen.flow_from_dataframe(\n        dataframe = df_val,\n        directory='/kaggle/input/histopathologic-cancer-detection/train',\n    x_col='id',\n    y_col='label',\n#     subset='validation',\n    batch_size=32,\n    seed=2018,\n    shuffle=False,\n    class_mode='binary',\n    target_size=(96,96)\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"main = \"Data\"\nos.mkdir(main)\ntrain_dir =os.path.join(main,\"train_dir\")\nos.mkdir(train_dir)\ntest_dir = os.path.join(main,\"test_dir\")\nos.mkdir(test_dir)\nhas_tumor = os.path.join(train_dir,\"has_tumor\")\nos.mkdir(has_tumor)\nhas_tumor = os.path.join(test_dir,\"has_tumor\")\nos.mkdir(has_tumor)\ndoes_not_have_tumor = os.path.join(train_dir,\"does_not_have_tumor\")\nos.mkdir(does_not_have_tumor)\ndoes_not_have_tumor = os.path.join(test_dir,\"does_not_have_tumor\")\nos.mkdir(does_not_have_tumor)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir('Data/train_dir')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train[df_train['label']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import shutil\nfor lab in df_train['id']:\n    label = df_train.loc[lab,'label']\n    if label == 0:\n        src = \"/kaggle/input/histopathologic-cancer-detection/train/\" + lab + \".tif\"\n        det = 'Data/train_dir/does_not_have_tumor'\n        shutil.copyfile(src,det)\n    else:\n        src = \"/kaggle/input/histopathologic-cancer-detection/train/\" + lab + \".tif\"\n        det = 'Data/train_dir/has_tumor'\n        shutil.copyfile(src,det)    \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for lab in df_test['id']:\n    label = df_test.loc[lab,'label']\n    if label == 0:\n        src = \"/kaggle/input/histopathologic-cancer-detection/train/\" + lab + \".tif\"\n        det = 'Data/train_dir/does_not_have_tumor'\n        shutil.copyfile(src,det)\n    else:\n        src = \"/kaggle/input/histopathologic-cancer-detection/train/\" + lab + \".tif\"\n        det = 'Data/train_dir/has_tumor'\n        shutil.copyfile(src,det)      ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}