{"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)\nfrom tensorflow import keras\nfrom matplotlib.image import imread\nfrom matplotlib import pyplot as plt\nfrom progressbar import ProgressBar\nfrom collections import Counter\nfrom sklearn import preprocessing\nimport gc\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 all files under the input directory\n\nimport os\nimagenames = []\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    if dirname.endswith(r'train/train'):\n        print(dirname)\n        train_dir = dirname\n    for filename in filenames:\n        if filename.endswith('.jpg') and dirname.endswith(r'train/train'):\n            imagenames.append(filename)\nprint(len(imagenames))\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Preprocessing the images"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Checking images sizes..\npbar = ProgressBar()\nimg_sizes = []\nfor imagename in pbar(imagenames):\n        img_sizes.append(imread(os.path.join(train_dir, imagename)).shape)\nCounter(img_sizes).most_common(6)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Based on the most common resolutions, and without losing as much data as possible, I will rescale to (224, 224, 3).\n> As kaggle's RAM is limited to 13 GB on GPU Kernels, array dtype np.float16 is enough as all values are between 0 and 1 after normalization.\n\n> If you want to understand in depth, please visit https://en.wikipedia.org/wiki/Half-precision_floating-point_format#Precision_limitations_on_decimal_values_in_[0,_1]"},{"metadata":{"trusted":true},"cell_type":"code","source":"doggo_images = np.empty((len(imagenames), 224, 224, 3), dtype=np.float16) # Large Array, A MemoryError could be raised\ndoggo_targets = np.empty((len(imagenames)), dtype=np.uint8)\n\nlabels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\nle = preprocessing.LabelEncoder()\nlabels['target'] = le.fit_transform(labels.breed)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pbar = ProgressBar()\nfor i in pbar(range(len(imagenames))): \n    doggo_images[i] = np.expand_dims(\n                        keras.preprocessing.image.img_to_array(\n                            keras.preprocessing.image.load_img(\n                                os.path.join(train_dir, imagenames[i]),\n                                    target_size=(224, 224))), axis=0) / 255.0\n    doggo_targets[i] = labels[labels.id == imagenames[i].rstrip('.jpg')].target.values[0]\nprint(doggo_images.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_dogs = []\nbreed = []\nfor i in np.sort(np.unique(doggo_targets)):\n    class_dogs.append(doggo_images[doggo_targets == i][np.random.randint(50)])\n    breed.append(i)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Some dog breeds look pretty similar\nplt.figure(figsize=(11,11))\nfor i in range(len(np.unique(doggo_targets))):\n    plt.subplot(11, 11, i+1) # plot index can not be 0\n    plt.imshow(class_dogs[i].astype(np.float32))\n    plt.axis('off')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"keras.backend.clear_session()\n\nmodel_base = keras.applications.inception_resnet_v2.InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(224,224,3))\n\nmodel = keras.models.Sequential()\nfor layer in model_base.layers:\n    layer.trainable = False\n\nmodel.add(model_base)\nmodel.add(keras.layers.GlobalAveragePooling2D())\nmodel.add(keras.layers.Dense(120, activation='softmax'))\n    \nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\nhistory = model.fit(doggo_images, doggo_targets, validation_split=0.05, epochs=20, batch_size=1024)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pbar = ProgressBar()\ntestnames = []\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    if dirname.endswith(r'test/test'):\n        test_dir = dirname\n    for filename in filenames:\n        if filename.endswith('.jpg') and dirname.endswith(r'test/test'):\n            testnames.append(filename)\n\ndoggo_test_images = np.empty((len(testnames), 224, 224, 3), dtype=np.float16)\n\nfor i in pbar(range(len(testnames))): \n    doggo_test_images[i] = np.expand_dims(\n                        keras.preprocessing.image.img_to_array(\n                            keras.preprocessing.image.load_img(\n                                os.path.join(test_dir, testnames[i]),\n                                    target_size=(224, 224))), axis=0) / 255.0\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = pd.DataFrame(model.predict(doggo_test_images))\npredictions.columns = list(le.inverse_transform([int(col) for col in predictions.columns]))\npredictions['id'] = [testname.rstrip('.jpg') for testname in testnames]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions.to_csv('submission.csv', index=False)","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":1}