{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator, load_img\nfrom sklearn.model_selection import train_test_split\nfrom keras.applications import VGG16\nimport matplotlib.pyplot as plt\nfrom keras.optimizers import SGD\n\nfrom keras import optimizers\nfrom keras import models\nfrom keras import layers\n\n\nimport pandas as pd\nimport numpy as np\nimport zipfile, os\n\n\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip', 'r') as zip:\n    zip.extractall()    \n    zip.close()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"filenames = os.listdir(\"/kaggle/working/train\")\n\nlabels = []\n\nfor filename in filenames:\n    label = filename.split(\".\")[0] \n    if label == \"cat\":\n        labels.append(\"0\")\n    else:\n        labels.append(\"1\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.DataFrame({\"id\": filenames, \"label\" : labels})\n\nprint(df.shape)\nprint(df.head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# define the total number of epochs to train for along with the\n# initial learning rate\n\nNUM_EPOCHS = 50\nINIT_LR = 1e-1\n\ndef poly_decay(epoch):\n    # initialize the maximum number of epochs, base learning rate,\n    # and power of the polynomial\n    maxEpochs = NUM_EPOCHS\n    baseLR = INIT_LR\n    power = 1.0\n\n    # compute the new learning rate based on polynomial decay\n    alpha = baseLR * (1 - (epoch / float(maxEpochs))) ** power\n\n    # return the new learning rate\n    return alpha\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nfrom keras.layers.normalization import BatchNormalization\nfrom keras.layers.convolutional import Conv2D\nfrom keras.layers.convolutional import AveragePooling2D\nfrom keras.layers.convolutional import MaxPooling2D\nfrom keras.layers.convolutional import ZeroPadding2D\nfrom keras.layers.core import Activation\nfrom keras.layers.core import Dense\nfrom keras.layers import Flatten\nfrom keras.layers import Input\nfrom keras.models import Model\nfrom keras.layers import add\nfrom keras.regularizers import l2\n\ndef residual_module(data, K, stride, chanDim, red=False,\n    reg=0.0001, bnEps=2e-5, bnMom=0.9):\n\n    # the shortcut branch of the ResNet module should be\n    # initialize as the input (identity) data\n    shortcut = data\n\n    # the first block of the ResNet module are the 1x1 CONVs\n    bn1 = BatchNormalization(axis=chanDim, epsilon=bnEps,\n        momentum=bnMom)(data)\n    act1 = Activation(\"relu\")(bn1)\n\n    conv1 = Conv2D(int(K * 0.25), (1, 1), use_bias=False,\n        kernel_regularizer=l2(reg))(act1)\n\n    # the second block of the ResNet module are the 3x3 CONVs\n    bn2 = BatchNormalization(axis=chanDim, epsilon=bnEps,\n        momentum=bnMom)(conv1)\n    act2 = Activation(\"relu\")(bn2)\n    conv2 = Conv2D(int(K * 0.25), (3, 3), strides=stride,\n        padding=\"same\", use_bias=False,\n        kernel_regularizer=l2(reg))(act2)\n\n    #the third block of the ResNet module is another set of 1x1 CONVs\n    bn3 = BatchNormalization(axis=chanDim, epsilon=bnEps,\n        momentum=bnMom)(conv2)\n    act3 = Activation(\"relu\")(bn3)\n    conv3 = Conv2D(K, (1, 1), use_bias=False,\n        kernel_regularizer=l2(reg))(act3)\n\n    # if we are to reduce the spatial size, apply a CONV layer to\n    # the shortcut\n    if red:\n        shortcut = Conv2D(K, (1, 1), strides=stride,\n            use_bias=False, kernel_regularizer=l2(reg))(act1)\n    \n    # add together the shortcut and the final CONV\n    x = add([conv3, shortcut])\n\n    # return the addition as the output of the ResNet module\n    return x\n\ndef build_model(width, height, depth, classes, stages, filters,\n        reg=0.0001, bnEps=2e-5, bnMom=0.9):\n\n        # initialize the input shape to be \"channels last\" and the\n        # channels dimension itself\n        inputShape = (height, width, depth)\n        chanDim = -1\n\n        # set the input and apply BN\n        inputs = Input(shape=inputShape)\n        x = BatchNormalization(axis=chanDim, epsilon=bnEps,\n            momentum=bnMom)(inputs)\n\n        # apply CONV => BN => ACT => POOL to reduce spatial size       \n        x = Conv2D(filters[0], (5, 5), use_bias=False,\n        padding=\"same\", kernel_regularizer=l2(reg))(x)\n        x = BatchNormalization(axis=chanDim, epsilon=bnEps,\n        momentum=bnMom)(x)\n        x = Activation(\"relu\")(x)\n        x = ZeroPadding2D((1, 1))(x)\n        x = MaxPooling2D((3, 3), strides=(2, 2))(x)\n\n        # loop over the number of stages\n        for i in range(0, len(stages)):\n            # initialize the stride, then apply a residual module\n            # used to reduce the spatial size of the input volume\n            stride = (1, 1) if i == 0 else (2, 2)\n            x = residual_module(x, filters[i + 1], stride,\n                chanDim, red=True, bnEps=bnEps, bnMom=bnMom)\n\n            # loop over the number of layers in the stage\n            for j in range(0, stages[i] - 1):\n                # apply a ResNet module\n                x = residual_module(x, filters[i + 1],\n                    (1, 1), chanDim, bnEps=bnEps, bnMom=bnMom)\n\n        # apply BN => ACT => POOL\n        x = BatchNormalization(axis=chanDim, epsilon=bnEps,\n        momentum=bnMom)(x)\n        x = Activation(\"relu\")(x)\n        x = AveragePooling2D((8, 8))(x)\n\n\n        # softmax classifier\n        x = Flatten()(x)\n        x = Dense(classes, kernel_regularizer=l2(reg))(x)\n        x = Activation(\"softmax\")(x)\n\n        # create the model\n        model = Model(inputs, x, name=\"resnet\")\n\n        # return the constructed network architecture\n        return model\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntrainGen = ImageDataGenerator(rescale=1./255,\n                                  rotation_range=40,\n                                  width_shift_range=0.2,\n                                  height_shift_range=0.2,\n                                  shear_range=0.2,\n                                  zoom_range=0.2,\n                                  horizontal_flip=True,\n                                  fill_mode='nearest')\n\ntestGen = ImageDataGenerator(rescale=1. / 255)\n\n\ntrain_df, validation_df = train_test_split(df, test_size=0.25)\n\ntrain_size = train_df.shape[0]\n\nvalidation_size = validation_df.shape[0]\n\nbatch_size = 16\n\nprint(\"train size: \" + str(train_size)+\"\\nvalidation_size:\" + str(validation_size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntrain_it = trainGen.flow_from_dataframe(train_df,\n    \"train/\",\n    x_col=\"id\",\n    y_col=\"label\",\n    #class_mode=\"binary\",\n    target_size=(200,200),\n    batch_size = batch_size)\n\n\nvalidation_it = trainGen.flow_from_dataframe(validation_df,\n    \"train/\",\n    x_col=\"id\",\n    y_col=\"label\",\n    #class_mode=\"binary\",\n    target_size=(200,200),\n    batch_size = batch_size)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#compiling model...\nopt = SGD(lr=INIT_LR, momentum=0.9)\nopt=\"adam\"\nmodel = build_model(200, 200, 3, 2, (9, 9, 9),\n    (64, 64, 128, 256), reg=0.0005)\nmodel.compile(loss=\"binary_crossentropy\", optimizer=opt,\n    metrics=[\"accuracy\"])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"H = model.fit_generator(train_it,\n    steps_per_epoch=train_size//batch_size,\n    epochs=50,\n    validation_data=validation_it,\n    validation_steps=validation_size//batch_size)\n\nmodel.save(\"catsvsdogs_resnet.hdf5\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nplt.style.use('ggplot')\n\nacc = H.history['accuracy']\nval_acc = H.history['val_accuracy']\nloss = H.history['loss']\nval_loss = H.history['val_loss']\n\nepochs = range(1, len(acc) + 1)\n\nplt.plot(epochs, acc, 'b', label='training acc')\nplt.plot(epochs, val_acc, 'r', label='validation acc')\nplt.title('accuracy')\nplt.legend()\n\nplt.figure()\nplt.plot(epochs, loss, 'b', label='training loss')\nplt.plot(epochs, val_loss, 'r', label='validation loss')\nplt.title('loss')\nplt.legend()\n\nplt.show()","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}