{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\ndf = pd.read_csv(\"../input/dog-breed-identification/labels.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(df[\"id\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from PIL import Image\nimport random\n\nImage.open(\"../input/dog-breed-identification/train/\"+ random.choice(df[\"id\"]) +\".jpg\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Turning images into array\ndef prepareImages(data, m, dataset):\n    \n    print(\"Preparing images\")\n    \n    X_train = np.zeros((m, 100, 100, 3))\n    \n    count = 0\n    \n    for fig in data['id']:\n        \n        #load images into images of size 100x100x3\n        \n        img = image.load_img(\"../input/dog-breed-identification/\"+dataset+\"/\"+fig + \".jpg\", target_size=(100, 100, 3))\n        x = image.img_to_array(img)\n        \n        \n        x = preprocess_input(x)\n        \n        X_train[count] = x\n        \n        if (count%500 == 0):\n            print(\"Processing image: \", count+1, \", \", fig)\n        count += 1\n    \n    count = 0\n    \n    print(\"Finished!\")\n            \n    return X_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder\nfrom sklearn.preprocessing import LabelEncoder","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Turning labels into one hot encoder\ndef prepareY(Y):\n\n    values = array(Y)\n    print(values.shape)\n    label_encoder = LabelEncoder()\n    integer_encoded = label_encoder.fit_transform(values)\n    print(integer_encoded)\n\n    onehot_encoder = OneHotEncoder(sparse=False)\n    integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)\n    onehot_encoded = onehot_encoder.fit_transform(integer_encoded)\n    print(onehot_encoded)\n\n    y = onehot_encoded\n    print(y.shape)\n    return y, label_encoder","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, BatchNormalization, MaxPooling2D\nfrom keras.layers import Dropout, Activation, Dense, AveragePooling2D, Flatten","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32, (7,7), (1,1), input_shape = (100,100,3)))\nmodel.add(BatchNormalization(axis = 3))\nmodel.add(Activation(\"relu\"))\nmodel.add(MaxPooling2D(2,2))\n\nmodel.add(Conv2D(64, (3,3), (1,1)))\nmodel.add(Activation(\"relu\"))\nmodel.add(AveragePooling2D(3,3))\n\nmodel.add(Flatten())\nmodel.add(Dense(5000, activation = \"relu\"))\nmodel.add(Dropout(0.6))\nmodel.add(Dense(10222, activation = \"softmax\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.output_shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(loss = \"categorical_crossentropy\", optimizer = \"adam\", metrics = \"accuracy\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing import image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.applications.imagenet_utils import preprocess_input","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Preparing data for training\n\nX = prepareImages(df, 10222, \"train\")\n\n#put all the values of the training data in the range between 0 and 1\n\nX /= 255\n\nprint(\"Shape X-train: \", X.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Y = df['id']\nprint(\"Shape Y-train: \", Y.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from numpy import array","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y, label_encoder = prepareY(Y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(X, y, epochs=100, batch_size=50, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import gc\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#plot how the accuracy changes as the model was trained\nplt.plot(history.history['accuracy'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\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}