{"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 the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import keras.backend as k\nfrom keras import layers\nfrom keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D\nfrom keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D\nfrom keras.models import Model\nimport tensorflow as tf\nimport os\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelBinarizer\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom glob import glob\nfrom skimage.io import imread","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.DataFrame({'path': glob(os.path.join('../input/train', '*.tif'))})\ndf['id'] = df.path.map(lambda x: x.split('/')[3].split(\".\")[0])\nlabels = pd.read_csv('../input/train_labels.csv')\ndf = df.merge(labels, on=\"id\")\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df0 = df[df.label == 0].sample(500)\ndf1 = df[df.label == 1].sample(500)\ndf = pd.concat([df0, df1])\ndf = df[[\"path\", \"id\", \"label\"]]\ndf.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['image'] = df['path'].map(imread)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image = (df['image'][450], df['label'][450])\nimg = plt.imshow(image[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"input_images = np.stack(list(df.image), axis=0)\ninput_images.shape\nY = LabelBinarizer().fit_transform(df.label)\nX = input_images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_X, test_X, train_Y, test_Y = train_test_split(X, Y, test_size=0.2, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def model(input_shape):\n    # Defining the input placeholder\n    X_input = Input(input_shape)\n    \n    # Padding the borders\n    X = ZeroPadding2D((3, 3))(X_input)\n    # Applying the first layer\n    X = Conv2D(32, (7, 7), strides= (1, 1), name='conv0')(X)\n    X = BatchNormalization(axis=3, name='bn0')(X)\n    X = Activation('relu')(X)\n     # MaxPool\n    X = MaxPooling2D((2, 2), name='max_pool1')(X)\n    \n    # Applying the second layer\n    X = Conv2D(64, (7, 7), strides= (1, 1), name='conv1')(X)\n    X = BatchNormalization(axis=3, name='bn1')(X)\n    X = Activation('relu')(X)\n    # MaxPool\n    X = MaxPooling2D((2, 2), name='max_pool2')(X)\n    \n    #Applying third layer\n    X = Conv2D(128, (7, 7), strides= (1, 1), name='conv2')(X)\n    X = BatchNormalization(axis=3, name='bn2')(X)\n    X = Activation('relu')(X)\n     # MaxPool\n    X = MaxPooling2D((2, 2), name='max_pool3')(X)  \n    # Flatten and FullyConnected Layer\n    X = Flatten()(X)\n    X = Dense(1, activation='sigmoid', name='fc')(X)\n    \n    model = Model(inputs=X_input, outputs=X, name='Model')\n    \n    return model\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_final = model(train_X.shape[1:])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_final.compile('adam', 'binary_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_final.fit(train_X, train_Y, epochs=10, batch_size=50)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"evals = model_final.evaluate(test_X, test_Y, batch_size=32, verbose=1)\nprint('Test accuracy: '+str(evals[1]*100)+'%')","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}