{"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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfrom shutil import copyfile\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import seaborn as sns\nimport matplotlib\nimport os\nfrom shutil import copyfile\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels_dict = {i:j for i,j in zip(labels['id'],labels['breed'])}\nclasses = set(labels_dict.values())\nclasses","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images = [f for f in os.listdir('/kaggle/input/dog-breed-identification/train/')]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"split = int(len(images) * 0.85)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_images = images[:split]\nvalidation_images  = images[split:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if  not os.path.exists('training_images'):\n        os.makedirs('training_images')\n\nif  not os.path.exists('validation_images'):\n    os.makedirs('validation_images')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for curClass in classes:    \n    if  not os.path.exists(os.path.join('training_images', curClass)):\n        os.makedirs(os.path.join('training_images', curClass))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for curClass in classes:    \n    if  not os.path.exists(os.path.join('validation_images', curClass)):\n        os.makedirs(os.path.join('validation_images', curClass))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"count = 0 \ndestination_directory = 'training_images/'\nfor item in images:\n    if count >7999:\n        destination_directory = 'validation_images/'\n    filekey = os.path.splitext(item)[0]\n    des = destination_directory + labels_dict[filekey]+'/'+item\n    print(des)\n    if  not os.path.exists(des):\n        src = '/kaggle/input/dog-breed-identification/train/' + item\n        copyfile(src, des)\n    print(labels_dict[filekey])\n    count +=1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = load_img('training_images/cocker_spaniel/dd126e42b474c3831f8fda33052428c1.jpg') \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen = ImageDataGenerator(\n        rotation_range=50,\n        width_shift_range=0.3,\n        height_shift_range=0.2,\n        shear_range=0.3,\n        zoom_range=0.3,\n        horizontal_flip=True,\n        fill_mode='nearest')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = load_img('/kaggle/input/dog-breed-identification/train/09839ef1c5a5a5b3acb61c4093cab07f.jpg') \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x = img_to_array(img)\nx = x.reshape((1,) + x.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.layers import Convolution2D\nfrom keras.layers import MaxPooling2D\nfrom keras.layers import Flatten\nfrom keras.layers import Dense\nfrom keras.layers import Conv2D,Dropout\nfrom keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n        rescale=1./255,\n        shear_range=0.2,\n        zoom_range=0.2,\n        horizontal_flip=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_set = train_datagen.flow_from_directory(\n        'training_images',\n        target_size=(128, 128),\n        batch_size=20,\n        class_mode='categorical')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_set = train_datagen.flow_from_directory(\n        'training_images',\n        target_size=(128, 128),\n        batch_size=20,\n        class_mode='categorical')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_set = test_datagen.flow_from_directory(\n        'validation_images',\n        target_size=(128, 128),\n        batch_size=20,\n        class_mode='categorical')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.layers import Dropout\nclf = Sequential()\n#Convolution\n#32 is number of kernals of 3x3, we can use 64 128 256 etc in next layers\n#input shape can be 128, 256 later\nclf.add(Conv2D(32,(3,3),input_shape=(128,128,3),activation='relu'))\n#Max Pooling size reduces divided by 2\nclf.add(MaxPooling2D(pool_size=(2,2)))      \n\n\n#clf.add(Dropout(0.5))\n\nclf.add(Conv2D(32,(3,3), activation='relu'))\nclf.add(MaxPooling2D(pool_size=(2,2)))\n#clf.add(Dropout(0.25))\n\nclf.add(Conv2D(64, (3, 3), activation='relu'))\nclf.add(MaxPooling2D(pool_size=(2, 2)))\n#clf.add(Dropout(0.10))\n#Flattening\nclf.add(Flatten())\n        \n#Adding An ANN\n#lets take 128 hidden nodes in hidden layer\n#clf.add(Dense(units=128,activation='relu'))\nclf.add(Dense(units=64, activation='relu'))\nclf.add(Dropout(0.5))\nclf.add(Dense(units=120,activation='softmax'))\n#stochastic gradient descent -Adam -optimizer\n#loss func categorical cross entropy\n#metrics = accuracy\nclf.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.callbacks import EarlyStopping\nearly_stopping_monitor=EarlyStopping(patience=6)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"hist=clf.fit_generator(\n        training_set,\n        steps_per_epoch=400,\n        epochs=50,\n        validation_data=test_set,\n        validation_steps=2222,\ncallbacks=[early_stopping_monitor])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport cv2\nimport pandas as pd\ntest_set = []\ntest_set_ids = []\n\nfor curImage in os.listdir('/kaggle/input/dog-breed-identification/test'):\n    test_set_ids.append(os.path.splitext(curImage)[0])\n    #print(os.path.splitext(curImage)[0])\n    curImage = cv2.imread('/kaggle/input/dog-breed-identification/test/'+curImage)\n    test_set.append(cv2.resize(curImage,(128, 128)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_set = np.array(test_set, np.float32)/255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions= clf.predict(test_set)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions[0].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_set.class_indices","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classes= {index:breed for breed,index in training_set.class_indices.items()}\ncolumn_names = [classes[i] for i in range(120)]\ncolumn_names","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions_df = pd.DataFrame(predictions)\npredictions_df.columns = column_names\npredictions_df.insert(0,'id', test_set_ids)\n\npredictions_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions_df.to_csv('submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(hist.history['val_loss'])\nplt.xlabel('epochs')\nplt.ylabel('validation loss')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(hist.history['loss'],label=\"traing loss\")\nplt.plot(hist.history['val_loss'], label=\"Validation loss\")\nplt.legend()\nplt.xlabel('epochs')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}