{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","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 os, cv2, re, random\nimport numpy as np\nimport pandas as pd\nfrom keras import backend as K\n\n\nfrom keras.models import *\nfrom keras.layers import *\nfrom keras.applications import *\nfrom keras.optimizers import *\nfrom keras import layers, models, optimizers\nimport h5py\nprint(os.listdir(\"../input\"))\n\n\n# Any results you write to the current directory are saved as output.","execution_count":1,"outputs":[]},{"metadata":{"_cell_guid":"6a149432-57a5-40d8-9cc9-20b73746c172","_uuid":"9c7c158e77ce1dc5ed4fb164ef8b87baa877aaa8","collapsed":true,"trusted":true},"cell_type":"code","source":"TRAIN_DIR = '../input/train/'\nTEST_DIR = '../input/test/'\n\nCHANNELS = 3\nnb_classes=2\nimg_width=299\nimg_height=299\n\ntrain_images = [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR)] # use this for full dataset\ntrain_dogs =   [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'dog' in i]\ntrain_cats =   [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'cat' in i]\n\ntest_images =  [TEST_DIR+i for i in os.listdir(TEST_DIR)]","execution_count":2,"outputs":[]},{"metadata":{"_cell_guid":"b14c74eb-a411-47fb-abf0-4198ba22a66d","_uuid":"ed9c315bcf0486a926c50fbca6010b89fa1e998d","collapsed":true,"trusted":true},"cell_type":"code","source":"#train_images_dogs_cats = train_dogs[0:50] + train_cats[0:50] \n#train_images_dogs_cats = train_images_dogs_cats[0:20]+train_images_dogs_cats[24980:25000]\ntrain_images_dogs_cats=train_images","execution_count":3,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"trusted":true},"cell_type":"code","source":"def read_image(file_path):\n    img = cv2.imread(file_path, cv2.IMREAD_COLOR) #cv2.IMREAD_GRAYSCALE\n    #b,g,r = cv2.split(img)\n    #img2 = cv2.merge([r,g,b])\n    return cv2.resize(img, (img_width, img_height), interpolation=cv2.INTER_CUBIC)","execution_count":4,"outputs":[]},{"metadata":{"_cell_guid":"68b5ad9b-652f-439e-afb5-3086ba901a13","_uuid":"cc05bcac69ac12194d559efa5a343ff8f37272b1","collapsed":true,"trusted":true},"cell_type":"code","source":"from tqdm import tqdm  \ndef prep_data(images):\n \n    x = np.zeros((len(images), img_width, img_height, 3), dtype=np.uint8)\n    y = np.zeros(len(images), dtype=np.uint8)\n    count = len(images)\n        \n    for i, image_file in tqdm(enumerate(images)):\n        x[i] = cv2.resize(cv2.imread(image_file), (img_width,img_height), interpolation=cv2.INTER_CUBIC)\n        if 'dog' in image_file:\n            y[i]=1\n        elif 'cat' in image_file:\n            y[i]=0\n        \n    return x, y","execution_count":5,"outputs":[]},{"metadata":{"_cell_guid":"5c062cfb-e2bf-477c-aecc-dfdc86428dec","_uuid":"46c5ed54cf60f40ab75edee6362dc525c2f247f9","trusted":true},"cell_type":"code","source":"from keras.applications import *\nimport tensorflow as tf\nfrom sklearn.utils import shuffle\nimport keras\nfrom sklearn.model_selection import KFold,StratifiedKFold\nfrom keras.optimizers import *\nfrom keras.callbacks import EarlyStopping\n#print(K.image_data_format())\n#X_train, X_val, Y_train, Y_val = train_test_split(X,Y, test_size=0.2, random_state=1)\n\n#X_train, Y_train = shuffle(X_train, Y_train)\n#X_test, Y_test = prepare_data(test_images_dogs_cats)\nX, Y = prep_data(train_images_dogs_cats)\n#X, Y = shuffle(X, Y)\n#X=np.array(X, dtype='uint8')\n#Y=np.array(Y, dtype='uint8')\nsfolder = StratifiedKFold(n_splits=5,random_state=0,shuffle=False)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"5ce32566-e999-487d-968d-dbc297e2b911","_uuid":"b3d70c0781a887fbe450bdeba189723215d3cb22","collapsed":true,"trusted":true},"cell_type":"code","source":"\nnb_epoch = 10\nbatch_size = 16\n\n## Callback for loss logging per epoch\nclass LossHistory(keras.callbacks.Callback):\n    def on_train_begin(self, logs={}):\n        self.losses = []\n        self.val_losses = []\n        \n    def on_epoch_end(self, batch, logs={}):\n        self.losses.append(logs.get('loss'))\n        self.val_losses.append(logs.get('val_loss'))   ","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"632d5f5f-cfe1-452b-9622-1ae5641b7753","_uuid":"ab1e85aaceb70bd023c2e49a9d01b41169c2324a","trusted":true},"cell_type":"code","source":"from keras.utils import multi_gpu_model\nfrom keras.applications.inception_resnet_v2 import *\ninput_x = Input((img_width, img_height, 3))\ninput_x = Lambda(preprocess_input)(input_x)\nbase_model = InceptionResNetV2(input_tensor=input_x,weights='imagenet', include_top=False)\n# add a global spatial average pooling layer\nx = base_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dropout(0.5)(x)\npredictions = Dense(1, activation='sigmoid')(x)\n\n# this is the model we will train\nmodel = Model(inputs=base_model.input, outputs=predictions)\n\n#for layer in base_model.layers:\n#    layer.trainable = False\nearly_stopping = EarlyStopping(monitor='val_loss', patience=3, verbose=1, mode='auto')   \n\nkeras.optimizers.Adadelta(lr=0.001, rho=0.95, epsilon=1e-06)\n#model = multi_gpu_model(model2, gpus=4)\nmodel.compile(optimizer='adadelta',\n              loss='binary_crossentropy',\n              metrics=['accuracy'])\nhistory = LossHistory()\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"863b0f78-7694-42fc-94ec-6f26daf4f764","_uuid":"c44a53b3ca5600b482d89157d12ff704b3721299","collapsed":true,"trusted":true},"cell_type":"code","source":"#historylist = []\nfor train_index, test_index in sfolder.split(X,Y):\n    #print(\"TRAIN:\", train_index, \"valid:\",test_index)\n    print('----------------------------------------------------')\n    X_train, X_val = X[train_index],X[test_index]\n    Y_train, Y_val = Y[train_index],Y[test_index]\n    \n    #validdata=np.concatenate((np.array(X_val),np.array(Y_val)), axis=0)\n    model.fit(X_train, Y_train, batch_size=batch_size, epochs=nb_epoch,\n              validation_data=(X_val,Y_val), verbose=1, shuffle=True, callbacks=[history, early_stopping])\n    del X_train,Y_train,X_val,Y_val\n    #historylist.append(history)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"c1d972d5-a222-4ccd-af1f-be5cd29e630c","_uuid":"018926a6be32d2b3dd33aeb2817cb21d27b2ddc5","collapsed":true,"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nloss = history.losses\nval_loss = history.val_losses\n\n\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.title('InceptionResNetV2 Loss Trend')\nplt.plot(loss, 'blue', label='Training Loss')\nplt.plot(val_loss, 'green', label='Validation Loss')\nplt.xticks(range(0,nb_epoch)[0::1])\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"23d7a20d-fd84-4d8f-a446-212e0b99f29e","_uuid":"0931402fcb7009a5ef8ee3d2593c8f7dcae75d0d","collapsed":true,"trusted":true},"cell_type":"code","source":"#model.save('model_inception_resnet_v2_keras.h5')\ndef pre_test():\n    x = np.zeros((len(test_images), img_width, img_height, 3), dtype=np.uint8)\n    for i, image_file in enumerate(test_images):\n        x[i] = cv2.resize(cv2.imread(image_file), (img_width,img_height), interpolation=cv2.INTER_CUBIC)\n    return x","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"2b5144d8-419c-4580-b7f5-075a129440af","_uuid":"e45534c7e9ba1473d3aa189c79be176655751d61","collapsed":true,"trusted":true},"cell_type":"code","source":"#test_images_dogs_cats = test_images[0:1000] \n#test_images_dogs_cats = test_images_dogs_cats[0:20] \nfrom tqdm import tqdm \nimport pandas as pd\ndf = pd.read_csv(\"sample_submission.csv\")\n\ntestdata=pre_test()\nmodel_out = model.predict(testdata)\nmodel_out=model_out.clip(min=0.005, max=0.995)\n\nfor i, fname in enumerate(test_images):\n    index = int(fname[fname.rfind('/')+1:fname.rfind('.')])\n    df.set_value(index-1, 'label', model_out[i])\n\ndf.to_csv('pred0507.csv', index=None)\ndf.head(10)\n","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}