{"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","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"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,"collapsed":true,"_uuid":"811dc14f932c84bccb047f68d26cfc258d77af0d"},"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,"collapsed":true,"_uuid":"6cdbb3e632cc314f4fef7eadcfd6589391550c50"},"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":"3cb1a796db0a47bd4d41745dedde623d6c320baa"},"cell_type":"code","source":"#taking the first 9000 images of dogs and the same for cats\ntrain_images = train_dogs[:9000] + train_cats[:9000]\n#randomly shuffle images\nrandom.shuffle(train_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"967143c6353db19ab23cd83b311e362f024a757a"},"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,"collapsed":true,"_uuid":"95f380508ea77fd303a82c15970e40e7cf92c4a3"},"cell_type":"code","source":"X, y = prep_data(train_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"6deb3f6e5af1ec894f6cb28dd8c1005fbb73cece"},"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,"collapsed":true,"_uuid":"74ebf328fdaa1c0b9dc374f2c6e4ba7a273c9433"},"cell_type":"code","source":"X_test, y_test = prep_data(test_images) #y_test = []","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"2b262586a88425a35fcc4dacf162eb3b002d41ec"},"cell_type":"code","source":"#import VGG19\nfrom keras.applications import VGG19\n#creating an object of vgg19 model and discarding the top layer\nmodel_vgg19 = VGG19(include_top=False, weights='imagenet', input_shape=(rows,cols, channels))\n#model_vgg19.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"9d292cd8776dc073d9d6bbe4eac87633b3519f39"},"cell_type":"code","source":"#copy vgg19 layers into our model\nmodel = Sequential()\nfor layer in model_vgg19.layers:\n    model.add(layer)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"d4719d5f8ef6bbcff3378bb139d1c15a2bacbac2"},"cell_type":"code","source":"#freezing vgg19 layers (saving its original weights)\n#for i in model.layers:\n  #  i.trainable = Fals","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"bb12a0d304a453bfe9fba6e8cf1ec851fa3e8367"},"cell_type":"code","source":"#add top layer\nmodel.add(Flatten())\nmodel.add(Dense(10))\nmodel.add(Activation('relu'))\nmodel.add(Dense(1, activation='sigmoid'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"0d25d629316a9b42fddf9bc915864798c4826764"},"cell_type":"code","source":"#check layers trainability\nnum_layers = len(model.layers)\nfor x in range(0,num_layers):\n    print(model.layers[x])\n    print(model.layers[x].trainable)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"5941539f7d8be30d7d65d546d4945bf308d20b71"},"cell_type":"code","source":"model.compile(optimizer='Adam', metrics=['accuracy'], loss='binary_crossentropy')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"845e22ae7184059cfe045106e2f0dec7ff3c5291"},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"0dd8dac4d880902b640480d002d4664951549f1e"},"cell_type":"code","source":"train_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,"collapsed":true,"_uuid":"b873d6079b10609d9ad6ea4c6cd0e9c38a28e1e0"},"cell_type":"code","source":"#train/validate model\nmodel.fit_generator(train_gen, steps_per_epoch=120, epochs=30, verbose=1, validation_data=valid_gen, validation_steps=60)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"312d73ed03954781788caf5dd40e84661e8c7dcf"},"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":"8a171b5e771612521108e672b6525b855f60ecf1"},"cell_type":"code","source":"test_gen = datagen.flow(np.array(X_test), batch_size = 100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"254ae8dc8de39b4ed24010dc39eca288180ab18e"},"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":"c9e48a9354da3614083e038d8f7b4247e3886ca5"},"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":"cb8069dfef69e1b5395973d01dd0afa1b1dfc190"},"cell_type":"code","source":"id_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}