{"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\n# import numpy as np # linear algebra\n# import 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\n# import os\n# for 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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom skimage import io, transform\nfrom skimage.io import imread\nimport tensorflow as tf\nimport os\nimport random as rnd\nfrom sklearn.model_selection import train_test_split\nimport numpy as np\nfrom sklearn.preprocessing import LabelEncoder\n\n# tensorflow keras\nfrom tensorflow.keras import datasets, layers, models\n\nimport tensorflow as tf\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.models import Sequential\n\n# libraries for plotting\nimport matplotlib.pyplot as plt\n\n\n# reading dataframe\ntrain_df = pd.read_csv('../input/dog-breed-identification/labels.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-23T11:32:08.327305Z","iopub.execute_input":"2021-08-23T11:32:08.327681Z","iopub.status.idle":"2021-08-23T11:32:08.36225Z","shell.execute_reply.started":"2021-08-23T11:32:08.327647Z","shell.execute_reply":"2021-08-23T11:32:08.361214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_breeds = train_df.breed.unique()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T09:58:34.778483Z","iopub.execute_input":"2021-08-23T09:58:34.779054Z","iopub.status.idle":"2021-08-23T09:58:34.791792Z","shell.execute_reply.started":"2021-08-23T09:58:34.779003Z","shell.execute_reply":"2021-08-23T09:58:34.790777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_df = pd.concat([pd.DataFrame(columns = ['id']), pd.DataFrame(columns = unique_breeds)])","metadata":{"execution":{"iopub.status.busy":"2021-08-23T09:58:36.396156Z","iopub.execute_input":"2021-08-23T09:58:36.396698Z","iopub.status.idle":"2021-08-23T09:58:36.418004Z","shell.execute_reply.started":"2021-08-23T09:58:36.39665Z","shell.execute_reply":"2021-08-23T09:58:36.416996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T09:58:42.262488Z","iopub.execute_input":"2021-08-23T09:58:42.263047Z","iopub.status.idle":"2021-08-23T09:58:42.292835Z","shell.execute_reply.started":"2021-08-23T09:58:42.262996Z","shell.execute_reply":"2021-08-23T09:58:42.291741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = []\ny = []\ntrain_dir = '../input/dog-breed-identification/train'\nfor index, row in train_df.iterrows():\n    img = imread(os.path.join(train_dir, str(row['id'])+'.jpg'))\n    img = transform.resize(img, (100,100,3))\n    X.append(img)\n    y.append(row['breed'])\n# X = pd.DataFrame(X, columns =['img'])\n# y = pd.DataFrame(y, columns=['label'])","metadata":{"execution":{"iopub.status.busy":"2021-08-23T11:03:16.143841Z","iopub.execute_input":"2021-08-23T11:03:16.144284Z","iopub.status.idle":"2021-08-23T11:10:10.270493Z","shell.execute_reply.started":"2021-08-23T11:03:16.144248Z","shell.execute_reply":"2021-08-23T11:10:10.269189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T11:10:10.272906Z","iopub.execute_input":"2021-08-23T11:10:10.273741Z","iopub.status.idle":"2021-08-23T11:10:10.279302Z","shell.execute_reply.started":"2021-08-23T11:10:10.273669Z","shell.execute_reply":"2021-08-23T11:10:10.277858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X.label.unique()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T11:10:10.281939Z","iopub.execute_input":"2021-08-23T11:10:10.282812Z","iopub.status.idle":"2021-08-23T11:10:10.300347Z","shell.execute_reply.started":"2021-08-23T11:10:10.282756Z","shell.execute_reply":"2021-08-23T11:10:10.299169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# img, label = X.loc[rnd.choice(range(len(X)))]\n# img.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-23T11:24:51.587964Z","iopub.execute_input":"2021-08-23T11:24:51.588514Z","iopub.status.idle":"2021-08-23T11:24:51.593411Z","shell.execute_reply.started":"2021-08-23T11:24:51.588468Z","shell.execute_reply":"2021-08-23T11:24:51.592205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_img = X.img\n# y = X.label\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.2, shuffle = True, stratify = y)\n# X_train = X_train.to_numpy()\n# X_val = X_val.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T11:25:07.468605Z","iopub.execute_input":"2021-08-23T11:25:07.469233Z","iopub.status.idle":"2021-08-23T11:25:07.501116Z","shell.execute_reply.started":"2021-08-23T11:25:07.469196Z","shell.execute_reply":"2021-08-23T11:25:07.500283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(X_train))\nprint(len(X_val))\nprint(len(y_train))\nprint(len(y_val))","metadata":{"execution":{"iopub.status.busy":"2021-08-23T11:25:11.386268Z","iopub.execute_input":"2021-08-23T11:25:11.386837Z","iopub.status.idle":"2021-08-23T11:25:11.393084Z","shell.execute_reply.started":"2021-08-23T11:25:11.386802Z","shell.execute_reply":"2021-08-23T11:25:11.392117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(X_train)\nX_val = np.array(X_val)\ny_train = np.array(y_train)\ny_val = np.array(y_val)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T11:42:51.410833Z","iopub.execute_input":"2021-08-23T11:42:51.411301Z","iopub.status.idle":"2021-08-23T11:42:53.013827Z","shell.execute_reply.started":"2021-08-23T11:42:51.411265Z","shell.execute_reply":"2021-08-23T11:42:53.012216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder()\ny_train = le.fit_transform(y_train)\ny_val = le.transform(y_val)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-23T11:42:53.545334Z","iopub.execute_input":"2021-08-23T11:42:53.545806Z","iopub.status.idle":"2021-08-23T11:42:53.55346Z","shell.execute_reply.started":"2021-08-23T11:42:53.545765Z","shell.execute_reply":"2021-08-23T11:42:53.55196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-23T11:42:56.234334Z","iopub.execute_input":"2021-08-23T11:42:56.234773Z","iopub.status.idle":"2021-08-23T11:42:56.241952Z","shell.execute_reply.started":"2021-08-23T11:42:56.234733Z","shell.execute_reply":"2021-08-23T11:42:56.240785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_val.shape)\nprint(y_train.shape)\nprint(y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T11:42:59.133614Z","iopub.execute_input":"2021-08-23T11:42:59.134064Z","iopub.status.idle":"2021-08-23T11:42:59.140504Z","shell.execute_reply.started":"2021-08-23T11:42:59.134009Z","shell.execute_reply":"2021-08-23T11:42:59.13956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = len(train_df.breed.unique())\nimg_height = 100\nimg_width = 100\nnum_channels = 3\ninput_shape = (100, 100, 100, 3)\nmodel = Sequential([\n  layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, num_channels)),\n  layers.Conv2D(10, 5,activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(20, 11, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(20, 14, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Flatten(),\n  layers.Dense(50, activation='relu'),\n  layers.Dense(num_classes)\n])","metadata":{"execution":{"iopub.status.busy":"2021-08-23T12:24:55.732681Z","iopub.execute_input":"2021-08-23T12:24:55.73325Z","iopub.status.idle":"2021-08-23T12:24:55.827633Z","shell.execute_reply.started":"2021-08-23T12:24:55.733179Z","shell.execute_reply":"2021-08-23T12:24:55.826388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n               metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-23T12:24:58.489908Z","iopub.execute_input":"2021-08-23T12:24:58.490345Z","iopub.status.idle":"2021-08-23T12:24:58.507292Z","shell.execute_reply.started":"2021-08-23T12:24:58.49031Z","shell.execute_reply":"2021-08-23T12:24:58.506093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T12:25:00.342415Z","iopub.execute_input":"2021-08-23T12:25:00.342952Z","iopub.status.idle":"2021-08-23T12:25:00.353811Z","shell.execute_reply.started":"2021-08-23T12:25:00.342914Z","shell.execute_reply":"2021-08-23T12:25:00.351509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=10\nhistory = model.fit(\n  X_train,\n    y_train,\n    batch_size=100,\n    # We pass some validation for\n    # monitoring validation loss and metrics\n    # at the end of each epoch\n    validation_data=(X_val, y_val),\n  epochs=epochs\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T12:25:04.624852Z","iopub.execute_input":"2021-08-23T12:25:04.625462Z","iopub.status.idle":"2021-08-23T12:25:27.980218Z","shell.execute_reply.started":"2021-08-23T12:25:04.625409Z","shell.execute_reply":"2021-08-23T12:25:27.977678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-23T10:53:32.041268Z","iopub.execute_input":"2021-08-23T10:53:32.041647Z","iopub.status.idle":"2021-08-23T10:53:32.047923Z","shell.execute_reply.started":"2021-08-23T10:53:32.041611Z","shell.execute_reply":"2021-08-23T10:53:32.046979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}