{"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_minor":4,"nbformat":4,"cells":[{"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\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-21T12:30:20.554697Z","iopub.execute_input":"2022-01-21T12:30:20.555469Z","iopub.status.idle":"2022-01-21T12:30:24.039146Z","shell.execute_reply.started":"2022-01-21T12:30:20.555432Z","shell.execute_reply":"2022-01-21T12:30:24.037255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport cv2\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.039879Z","iopub.status.idle":"2022-01-21T12:30:24.040163Z","shell.execute_reply.started":"2022-01-21T12:30:24.040008Z","shell.execute_reply":"2022-01-21T12:30:24.040028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/dog-breed-identification/labels.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.041603Z","iopub.status.idle":"2022-01-21T12:30:24.042134Z","shell.execute_reply.started":"2022-01-21T12:30:24.04189Z","shell.execute_reply":"2022-01-21T12:30:24.041915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"../input/dog-breed-identification/train/\"\n\nX = []\nfor i in range(len(train_df)):\n    \n    pth = path+train_df.id[i]+\".jpg\"\n    img = np.stack(cv2.resize(cv2.imread(pth), (331, 331)))\n    X.append(img) \n    ","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.043726Z","iopub.status.idle":"2022-01-21T12:30:24.044548Z","shell.execute_reply.started":"2022-01-21T12:30:24.044307Z","shell.execute_reply":"2022-01-21T12:30:24.044336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.shape(X)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.045563Z","iopub.status.idle":"2022-01-21T12:30:24.046359Z","shell.execute_reply.started":"2022-01-21T12:30:24.046118Z","shell.execute_reply":"2022-01-21T12:30:24.046143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.asarray(X)\ny = np.asarray(train_df['breed'])","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.04749Z","iopub.status.idle":"2022-01-21T12:30:24.048292Z","shell.execute_reply.started":"2022-01-21T12:30:24.048047Z","shell.execute_reply":"2022-01-21T12:30:24.048074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = pd.get_dummies(y)\noutput_len = len(y.columns)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.04947Z","iopub.status.idle":"2022-01-21T12:30:24.050275Z","shell.execute_reply.started":"2022-01-21T12:30:24.05003Z","shell.execute_reply":"2022-01-21T12:30:24.050055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Dense, Flatten, Dropout, Conv2D, GlobalAveragePooling2D, BatchNormalization","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.05147Z","iopub.status.idle":"2022-01-21T12:30:24.05227Z","shell.execute_reply.started":"2022-01-21T12:30:24.052028Z","shell.execute_reply":"2022-01-21T12:30:24.052054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import InceptionV3\nfrom tensorflow.keras.applications.inception_v3 import preprocess_input\n\ninputs = tf.keras.Input(shape=(331, 331, 3))\nbase_model = InceptionV3(weights='imagenet', include_top=False, input_tensor=inputs)\nbase_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.053383Z","iopub.status.idle":"2022-01-21T12:30:24.054322Z","shell.execute_reply.started":"2022-01-21T12:30:24.054072Z","shell.execute_reply":"2022-01-21T12:30:24.054097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = tf.keras.Input(shape=(331,331,3))\nx = inputs\n \nx = preprocess_input(x)\n\nx = base_model(x, training=False)\nx = GlobalAveragePooling2D()(x)\nx = Flatten()(x)\nx = Dense(256, activation='relu')(x)\nx = Dropout(0.2)(x)\nx = BatchNormalization()(x)\n\noutput = Dense(output_len, activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.0557Z","iopub.status.idle":"2022-01-21T12:30:24.056117Z","shell.execute_reply.started":"2022-01-21T12:30:24.05589Z","shell.execute_reply":"2022-01-21T12:30:24.055913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Model(inputs, output)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.057463Z","iopub.status.idle":"2022-01-21T12:30:24.057898Z","shell.execute_reply.started":"2022-01-21T12:30:24.05767Z","shell.execute_reply":"2022-01-21T12:30:24.057693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='Adam', loss=\"binary_crossentropy\", metrics=['Accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.059214Z","iopub.status.idle":"2022-01-21T12:30:24.059641Z","shell.execute_reply.started":"2022-01-21T12:30:24.059401Z","shell.execute_reply":"2022-01-21T12:30:24.059422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r = model.fit(X, y, batch_size=32, validation_split=0.2, epochs=15)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.060929Z","iopub.status.idle":"2022-01-21T12:30:24.061609Z","shell.execute_reply.started":"2022-01-21T12:30:24.061349Z","shell.execute_reply":"2022-01-21T12:30:24.061373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loss = r.history['loss']\nval_loss = r.history['val_loss']\n\ntrain_acc = r.history['Accuracy']\nval_acc = r.history['val_Accuracy']","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.062844Z","iopub.status.idle":"2022-01-21T12:30:24.063496Z","shell.execute_reply.started":"2022-01-21T12:30:24.063255Z","shell.execute_reply":"2022-01-21T12:30:24.06328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.064743Z","iopub.status.idle":"2022-01-21T12:30:24.065402Z","shell.execute_reply.started":"2022-01-21T12:30:24.065157Z","shell.execute_reply":"2022-01-21T12:30:24.065184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(18,6))\nplt.plot(train_loss, label='train_los')\nplt.plot(val_loss, label='val_loss')\nplt.plot(train_acc, label='train_acc')\nplt.plot(val_acc, label='val_acc')\nplt.xlabel('epochs')\nplt.ylabel('value')\nplt.legend(loc='upper right')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-21T12:30:24.066684Z","iopub.status.idle":"2022-01-21T12:30:24.067331Z","shell.execute_reply.started":"2022-01-21T12:30:24.067079Z","shell.execute_reply":"2022-01-21T12:30:24.067105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}