{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":true},"cell_type":"code","source":"#import libraries\nimport pandas as pd\nimport numpy as np\nimport os, random ,cv2\nimport keras\nfrom sklearn.model_selection import train_test_split\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, Dense, Flatten, Dropout, Activation, MaxPool2D\nfrom keras.optimizers import Adam, RMSprop\nfrom keras.losses import binary_crossentropy\nfrom keras.preprocessing.image import ImageDataGenerator\nimport matplotlib.pyplot as plt\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"#specify train and test datasets paths\ntrain_path = '../input/train/'\ntest_path = '../input/test/'\n#define image dimensions \nrows = 150\ncols = 150\nchannels = 3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"908f2b8bfa8e11e735a0cf52be086f4dcabbd70c","collapsed":true},"cell_type":"code","source":"#create a list of train image paths \"including image name\"\ntrain_images = [train_path+i for i in os.listdir(train_path)]\ntrain_dogs = [train_path+i for i in os.listdir(train_path) if 'dog' in i]\ntrain_cats = [train_path+i for i in os.listdir(train_path) if 'cat' in i]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d37c3d258810ce8de3c113b0fa8f73c9bec9bacc","collapsed":true},"cell_type":"code","source":"#create a list of test image paths \"including image name\"\ntest_images = [test_path+i for i in os.listdir(test_path)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"142208f4d0c41a39f5ea8cda60f03317c690680e"},"cell_type":"code","source":"train_images = train_dogs[:3000] + train_cats[:3000]\n#randomly shuffle train images\nrandom.shuffle(train_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"894d8a86e93f4c3d3f9f9d6be5b047c4bd457a00","collapsed":true},"cell_type":"code","source":"def prep_data(image_path_list):\n    x=[]\n    y=[]\n    for i in image_path_list:\n        x.append(cv2.resize(plt.imread(i), #read then resize image \n        (rows,cols), interpolation=cv2.INTER_CUBIC))  #appened new image to x\n    for j in image_path_list: #create a label and append it to y \n        if 'dog' in j:\n            y.append(1)\n        elif 'cat' in j:\n            y.append(0)\n    return x,y        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"83d246796b7b2ffe20c6023a21f72347b4d18f9e","collapsed":true},"cell_type":"code","source":"X, y = prep_data(train_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb7f871719835b4b010a979fe0f7b59491b3bebf","collapsed":true},"cell_type":"code","source":"#split X,y into a train and validation data sets\nX_train, X_val, y_train, y_val = train_test_split(X,y, test_size=(1/3), random_state=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c2ee16df179a1b57321762b200906b92215b703a","collapsed":true},"cell_type":"code","source":"X_test, y_test = prep_data(test_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"979973b0db42929066ce847ffa0612c3ee89fef0","collapsed":true},"cell_type":"code","source":"#create a keras CNN model from sctarch\nmodel = Sequential()\n\nmodel.add(Conv2D(32,(3,3), input_shape=(rows, cols, 3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(64,(3,3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(128,(3,3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(256,(3,3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(256))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(256))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(1))\nmodel.add(Activation('sigmoid'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fc3a4eb7d735ce83ef78a5aafd1c628166adeed4","collapsed":true},"cell_type":"code","source":"model.compile(optimizer='RMSprop', metrics=['accuracy'], loss='binary_crossentropy')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fba0da2c40b456c757582c97c3e3e9106dd80422","collapsed":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a1ae55bcfca55e330e2931c070efd453e2258171","collapsed":true},"cell_type":"code","source":"#create a data generator object with some image augmentation specs\ndatagen = ImageDataGenerator(\n    rescale=1./ 255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True\n)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"99ec2dc9c9f132aab443188759e655f3b9e90ac5","collapsed":true},"cell_type":"code","source":"#create an iterator for both train and valid sets\ntrain_gen = datagen.flow(x=np.array(X_train), y=y_train, batch_size=50)\nvalid_gen = datagen.flow(x=np.array(X_val), y=y_val, batch_size=50)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5507f3bda5dc5ed5bdad82295b28f0024f1ad9aa","collapsed":true},"cell_type":"code","source":"#train/validate model\nmodel.fit_generator(train_gen, steps_per_epoch=40, epochs=50, verbose=1, validation_data=valid_gen, validation_steps=20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"13f5d4f8c2f14aa5fed7e6804fbdd6452c7b88a9"},"cell_type":"code","source":"#create a data generator object for testing\ndatagen = ImageDataGenerator(rescale = 1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"50e628a2ce58669b4b09666c132da0e99be16f7a"},"cell_type":"code","source":"test_gen = datagen.flow(np.array(X_test), batch_size = 100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"146b107f2edeb173e353fd2584ed6661527ec1f7","collapsed":true},"cell_type":"code","source":"#predict\npredictions = model.predict_generator(test_gen, steps=125, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"97c17a1e0513657f0664da14e3c32f88198e6b30"},"cell_type":"code","source":"predictions_dogs=[]\nfor i in predictions:\n    predictions_dogs.append(i[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"495bdc2f62732244f3ddf03bac3fd28e712b040b"},"cell_type":"code","source":"#submit\nid_num = range(1, len(predictions_dogs) + 1)\nsubmission = pd.DataFrame({\"id\": id_num, \"label\":predictions_dogs})\nsubmission.to_csv(\"submission.csv\", index = False)","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}