{"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 matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import metrics\n\nimport cv2\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.layers.core import Dense,Activation,Dropout,Flatten\nfrom keras.layers import Conv2D","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a3ffdee068a2bf0f5a41730abf268897b4292ef0"},"cell_type":"code","source":"labels = pd.read_csv(\"../input/dog-breed-identification/labels.csv\")\nlabels.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7b660441c64043e5762703f1ebd9bf5c528199a2"},"cell_type":"code","source":"#x = cv2.imread(\"../input/dog-breed-identification/train/000bec180eb18c7604dcecc8fe0dba07.jpg\")\n#plt.imshow(x,cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"89b74ea8ec8c705c62e4a8d2ecd591c3c6c5405a"},"cell_type":"code","source":"imgs = []\nfor x in labels['id']:\n    x = cv2.imread(\"../input/dog-breed-identification/train/\"+x+\".jpg\")\n    #x = cv2.imread(\"../input/train/\"+x+\".jpg\",0)\n    x = 0.2989 * x[:,:,0] + 0.5870 * x[:,:,1] + 0.1140 * x[:,:,2]\n    x = cv2.resize(x, (64,64))\n    #x = x/255.0\n    x = (x-np.mean(x))/np.std(x)\n    imgs.append(x)\nlen(imgs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"641fe37b6e93f43d11fbe87a31a777ce1c20258f"},"cell_type":"code","source":"plt.imshow(imgs[0],cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"94950940e0b46ca48ee023cc0aee5094efe3a81c"},"cell_type":"code","source":"#from collections import Counter\n#x = dict(Counter([x.shape for x in imgs]).most_common(20))\n#x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6d13970c0a5ff257a571fa3d5a6145625a86d32c"},"cell_type":"code","source":"\"\"\"plt.figure(figsize=(12,8))\nplt.bar(range(20),x.values())\nplt.xticks(range(20),x.keys(), rotation='vertical')\nplt.show()\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a6b7b04c115a7384a8acf5b033a145a61f994f43"},"cell_type":"code","source":"\"\"\"x = Counter([x for x in labels['breed']])\nplt.figure(figsize=(20,8))\nplt.bar(range(len(x)),x.values())\nplt.xticks(range(len(x)),x.keys(), rotation='vertical')\nplt.show()\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6dca2f7223150e8410142e160413c1f308665194"},"cell_type":"code","source":"\"\"\"def preproc(image):\n    x = 0.2989 * image[:,:,0] + 0.5870 * image[:,:,1] + 0.1140 * image[:,:,2]\n    x = cv2.resize(x, (64,64))\n    x = (x-np.mean(x))/np.std(x)\n    return x\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5dbdd4f37f3e7c9c04e92db120d4db970e2d5409"},"cell_type":"code","source":"#prepro_imgs = [preproc(img) for img in imgs]\n#X = np.array(prepro_imgs)\nX = np.array(imgs)\nY = pd.get_dummies(labels['breed'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"08a4a746b5b2b4311e3246260635e85833dbff4a"},"cell_type":"code","source":"data_aug =ImageDataGenerator(width_shift_range = 0.2,\n                             height_shift_range = 0.2,\n                             rotation_range = 40,\n                             zoom_range = 0.2,\n                             horizontal_flip = True,\n                             fill_mode = 'nearest')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a842e9e0063b2fbbf0d4d13e307fa5d60b910319"},"cell_type":"code","source":"\"\"\"image = cv2.imread(\"../input/train/000bec180eb18c7604dcecc8fe0dba07.jpg\")\npro_image = preproc(image)\nplt.imshow(pro_image,cmap='gray')\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"034b9be16c66f26e534c128ed90d221466ef48b6"},"cell_type":"code","source":"x_train,x_test,y_train,y_test = train_test_split(X,Y,test_size=0.2,random_state = 10)\nprint (x_train.shape)\nprint (y_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b93b40af6cc5478439e8cd7748c9bad306095e90"},"cell_type":"code","source":"X_train = x_train[:,:,:,np.newaxis]\nX_test = x_test [:,:,:,np.newaxis]\ndata_aug.fit(X_train)\ntrain_gen = data_aug.flow(np.array(X_train),np.array(y_train),batch_size=32)\ntest_gen = data_aug.flow(np.array(X_test),np.array(y_test),batch_size=32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"24ed5602f1ca5ebf617a6e16d0de4dc4ffdd12e5"},"cell_type":"code","source":"from keras.layers import MaxPooling2D,ZeroPadding2D,BatchNormalization","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6820420625b178dc211c0a1641fcc31b42c65560"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32,input_shape=(64,64,1),kernel_size=(3,3),activation='relu',padding='same'))\nmodel.add(MaxPooling2D((2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(64,kernel_size=(3,3),activation='relu',padding='same'))\nmodel.add(MaxPooling2D((2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(128,kernel_size=(3,3),activation='relu',padding='same'))\nmodel.add(MaxPooling2D((2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(256,kernel_size=(3,3),activation='relu',padding='same'))\nmodel.add(MaxPooling2D((2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(512,kernel_size=(3,3),activation='relu',padding='same'))\nmodel.add(MaxPooling2D((2,2)))\nmodel.add(Flatten())\nmodel.add(Dense(y_train.shape[1],activation='softmax'))\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"99d9fb47e797d6d50119580a990f29f7c6465753"},"cell_type":"code","source":"model.compile(optimizer='Adam',\n          loss='categorical_crossentropy', \n           metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a955ba91026ccd4141c87ddeb50800ed47128eb9","scrolled":false},"cell_type":"code","source":"model.fit_generator(train_gen,steps_per_epoch=len(x_train)/32, epochs=30, validation_data = test_gen,validation_steps = len(X_test)/32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"6105b1a76b35afd867b9ae27a14e5ff899da0198"},"cell_type":"code","source":"tested = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')\nprint (tested.head())\ntest_imgs = []\nfor x in tested['id']:\n    x = cv2.imread(\"../input/dog-breed-identification/test/\"+x+\".jpg\")\n    #x = cv2.imread(\"../input/train/\"+x+\".jpg\",0)\n    x = 0.2989 * x[:,:,0] + 0.5870 * x[:,:,1] + 0.1140 * x[:,:,2]\n    x = cv2.resize(x, (64,64))\n    #x = x/255.0\n    x = (x-np.mean(x))/np.std(x)\n    test_imgs.append(x)\nlen(test_imgs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"82518b9389838a546c71d78747fad518a13701ff"},"cell_type":"code","source":"test = np.array(test_imgs)[:,:,:,np.newaxis]\noutput= model.predict(test)\noutput","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6a14271f4134a898f784a52d42ef4249292f916f"},"cell_type":"code","source":"output[0].argmax()\noutput[0].max()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e05ef931fb09a8bf666cb38880774d6bdc008580"},"cell_type":"code","source":"sub = pd.DataFrame(output,columns=pd.get_dummies(labels['breed']).columns)\nsub.insert(loc=0, column='id', value=tested['id'])\nsub.drop(0)\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0e80e05d6d01d549daf4b4ddfa46565dcc102cfd"},"cell_type":"code","source":"sub.to_csv('ouput.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"51aadd472092a3e5e1f513c1236374c129fd9e9d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}