{"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)\nimport cv2\nfrom tqdm import tqdm\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\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 20GB 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":"labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\nprint(labels.head())\nprint(labels.shape)\nprint(labels['breed'].value_counts())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#one hot encode labels as needed in the submission format\nencoded_labels = pd.get_dummies(labels['breed'])\nlabels_array = np.asarray(encoded_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#img parameters\nimg_rows = 128\nimg_cols = 128\nchannels = 3 #coloured image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_feature = []\ny_feature = []\n\ni=0\nfor f,img in tqdm(labels.values):\n    train_img = cv2.imread('../input/dog-breed-identification/train/{}.jpg'.format(f))\n    label = labels_array[i]\n    train_img_resize = cv2.resize(train_img,(img_rows,img_cols))\n    x_feature.append(train_img_resize)\n    y_feature.append(label)\n    i+=1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train_data = np.array(x_feature, np.float32) / 255.\nx_train_data = x_train_data.reshape(-1,img_rows,img_cols,3)\nprint(x_train_data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train_data = np.array(y_feature)\nprint (y_train_data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train ,x_val ,y_train ,y_val = train_test_split(x_train_data,y_train_data,test_size=0.25,random_state=2)\nprint(x_train.shape)\nprint(x_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\ntest_img = submission['id']\nprint(test_img.head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_test_feature=[]\nfor f in test_img.values:\n    test_img = cv2.imread('/kaggle/input/dog-breed-identification/test/{}.jpg'.format(f))\n    test_img = cv2.resize(test_img,(img_rows,img_cols))\n    x_test_feature.append(test_img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_test_data = np.array(x_test_feature, np.float32) / 255. \nx_test_data = x_test_data.reshape(-1,img_rows,img_cols,3)\nprint(x_test_data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential \nfrom keras.layers import Dense,Dropout\nfrom keras.layers import Convolution2D \nfrom keras.layers import MaxPooling2D \nfrom keras.layers import Flatten","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classifier = Sequential()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classifier.add(Convolution2D(32,(3,3),input_shape = (img_cols,img_rows,3),activation='relu'))\nclassifier.add(MaxPooling2D(pool_size = (2,2)))\nclassifier.add(Convolution2D(64,(3,3),activation='relu'))\nclassifier.add(MaxPooling2D(pool_size = (2,2)))\n\n\nclassifier.add(Flatten())\nclassifier.add(Dropout(0.2))\n#classifier.add(Dense(units=32,activation='relu'))\n#classifier.add(Dense(units=64,activation='relu'))\n#classifier.add(Dense(units=120,activation='relu'))\nclassifier.add(Dense(units = 120, activation = 'softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classifier.compile(optimizer = 'adam' , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"]) \nclassifier.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = classifier.fit(x_train,y_train,\n         batch_size = 128,\n         epochs = 20,\n         verbose = 2,\n         validation_data = (x_val,y_val) \n         )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results = classifier.predict(x_test_data)\nprediction = pd.DataFrame(results)\nprediction.columns = encoded_labels.columns.values\nprediction.insert(0, 'id', submission['id'])\nsubmission = prediction\nsubmission.to_csv('new_submission.csv', index=False)","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}