{"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\n\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom tensorflow import keras\nimport cv2\nimport PIL\n\nfrom os.path import join\nimport PIL.Image\nfrom PIL import ImageOps\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-13T03:20:11.267991Z","iopub.execute_input":"2021-10-13T03:20:11.268843Z","iopub.status.idle":"2021-10-13T03:20:43.633566Z","shell.execute_reply.started":"2021-10-13T03:20:11.268708Z","shell.execute_reply":"2021-10-13T03:20:43.632478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.models import Sequential, load_model, Model\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_src = '../input/dog-breed-identification/train'\ntest_src = '../input/dog-breed-identification/test'\ntrain = pd.read_csv('../input/dog-breed-identification/labels.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:06:07.819627Z","iopub.execute_input":"2021-10-13T03:06:07.820345Z","iopub.status.idle":"2021-10-13T03:06:07.83878Z","shell.execute_reply.started":"2021-10-13T03:06:07.820302Z","shell.execute_reply":"2021-10-13T03:06:07.837859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_picture_destination = [join(train_directory, f) for f in os.listdir(train_directory)]\ntesting_picture_destination = [join(testing_directory, f) for f in os.listdir(testing_directory)]\n\ntrain_size = len(train_picture_destination)\ntesting_size = len(testing_picture_destination)\n\ntrain_size,testing_size","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:06:07.840184Z","iopub.execute_input":"2021-10-13T03:06:07.840603Z","iopub.status.idle":"2021-10-13T03:06:07.900582Z","shell.execute_reply.started":"2021-10-13T03:06:07.840572Z","shell.execute_reply":"2021-10-13T03:06:07.899937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:06:07.902245Z","iopub.execute_input":"2021-10-13T03:06:07.902579Z","iopub.status.idle":"2021-10-13T03:06:07.923988Z","shell.execute_reply.started":"2021-10-13T03:06:07.902553Z","shell.execute_reply":"2021-10-13T03:06:07.9234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:06:07.924999Z","iopub.execute_input":"2021-10-13T03:06:07.925309Z","iopub.status.idle":"2021-10-13T03:06:07.9323Z","shell.execute_reply.started":"2021-10-13T03:06:07.925284Z","shell.execute_reply":"2021-10-13T03:06:07.931375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.rename(columns = {\"breed\": \"label\"}, inplace = True)\ntrain[\"id\"] = train[\"id\"].apply(lambda x: x+\".\"+\"jpg\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:06:07.933881Z","iopub.execute_input":"2021-10-13T03:06:07.934876Z","iopub.status.idle":"2021-10-13T03:06:07.957574Z","shell.execute_reply.started":"2021-10-13T03:06:07.934805Z","shell.execute_reply":"2021-10-13T03:06:07.956358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testing_files = os.listdir(testing_directory)\ntest = pd.DataFrame({\"id\": testing_files, \"label\": \"boston_bull\"})\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:06:07.959091Z","iopub.execute_input":"2021-10-13T03:06:07.960097Z","iopub.status.idle":"2021-10-13T03:06:07.982505Z","shell.execute_reply.started":"2021-10-13T03:06:07.960053Z","shell.execute_reply":"2021-10-13T03:06:07.981567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = len(train)\ntesting_size = len(test)\n\ntrain_size,testing_size","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:06:07.984035Z","iopub.execute_input":"2021-10-13T03:06:07.984257Z","iopub.status.idle":"2021-10-13T03:06:07.990058Z","shell.execute_reply.started":"2021-10-13T03:06:07.984232Z","shell.execute_reply":"2021-10-13T03:06:07.989422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_generator = ImageDataGenerator(rescale = 1./255, validation_split = 0.2)\ntrain_generator = train_data_generator.flow_from_dataframe(train, train_directory, x_col = 'id', y_col = 'label', subset = 'train',\n                                                     color_mode = 'rgb', class_mode = 'categorical', target_size = (299, 299),\n                                                     batch_size = 32, shuffle = True, seed = 123)","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:06:07.992395Z","iopub.execute_input":"2021-10-13T03:06:07.993071Z","iopub.status.idle":"2021-10-13T03:06:12.172286Z","shell.execute_reply.started":"2021-10-13T03:06:07.993038Z","shell.execute_reply":"2021-10-13T03:06:12.17161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validating_data_generator = ImageDataGenerator(rescale = 1./255, validation_split = 0.2)\nvalidation_generator = validating_data_generator.flow_from_dataframe(train, train_directory, x_col = 'id', y_col = 'label',\n                                                              subset = 'validation', color_mode = 'rgb',\n                                                              class_mode = 'categorical', target_size = (299, 299),\n                                                              batch_size = 32, shuffle = True, seed = 123)","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:06:12.173365Z","iopub.execute_input":"2021-10-13T03:06:12.174061Z","iopub.status.idle":"2021-10-13T03:06:12.292295Z","shell.execute_reply.started":"2021-10-13T03:06:12.174027Z","shell.execute_reply":"2021-10-13T03:06:12.291069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def picture_processing(path):\n    picture = PIL.Image.open(path)\n    size = (299, 299)\n    picture = ImageOps.fit(picture, size, PIL.Image.ANTIALIAS) \n    picture_array = np.asarray(picture)\n    \n    return picture_array","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:06:12.294059Z","iopub.execute_input":"2021-10-13T03:06:12.294399Z","iopub.status.idle":"2021-10-13T03:06:12.301232Z","shell.execute_reply.started":"2021-10-13T03:06:12.294358Z","shell.execute_reply":"2021-10-13T03:06:12.300141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_auxillary = train.copy()\ntrain_auxillary['img_path'] = train\n_directory + '/' + train_auxillary['id']\ntrain_auxillary","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:06:12.302462Z","iopub.execute_input":"2021-10-13T03:06:12.302682Z","iopub.status.idle":"2021-10-13T03:06:12.326701Z","shell.execute_reply.started":"2021-10-13T03:06:12.302655Z","shell.execute_reply":"2021-10-13T03:06:12.325538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dogs = []\nlabel_names=[]\n\nfor i in range(0, len(train_auxillary)):\n    label = train_auxillary.iloc[i,1]\n    filename = train_auxillary.iloc[i,2]\n    picture = picture_processing(filename)\n    dogs.append(picture)\n    label_names.append(label)\n    \nprint(np.shape(label_names))\nprint(np.shape(dogs))","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:06:12.328639Z","iopub.execute_input":"2021-10-13T03:06:12.328914Z","iopub.status.idle":"2021-10-13T03:08:34.454293Z","shell.execute_reply.started":"2021-10-13T03:06:12.328885Z","shell.execute_reply":"2021-10-13T03:08:34.453254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (15,15))\nfor i in range(9):\n    plt.subplot(330 + 1 + i)\n    picture = dogs\n    plt.imshow(picture[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:08:34.455735Z","iopub.execute_input":"2021-10-13T03:08:34.456004Z","iopub.status.idle":"2021-10-13T03:08:36.0012Z","shell.execute_reply.started":"2021-10-13T03:08:34.455977Z","shell.execute_reply":"2021-10-13T03:08:35.999877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32, kernel_size = (3, 3), activation = 'selu', input_shape=(299, 299, 3)))\nmodel.add(MaxPooling2D(pool_size = (2, 2)))\nmodel.add(Conv2D(64, kernel_size = (3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2, 2)))\nmodel.add(Conv2D(128, kernel_size = (3,3), activation = 'selu'))\nmodel.add(Flatten())\nmodel.add(Dense(40, activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(20, activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(len(list(train_generator.class_indices.keys())), activation = 'softmax'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:08:36.002938Z","iopub.execute_input":"2021-10-13T03:08:36.004169Z","iopub.status.idle":"2021-10-13T03:08:36.313578Z","shell.execute_reply.started":"2021-10-13T03:08:36.004102Z","shell.execute_reply":"2021-10-13T03:08:36.312467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss = 'categorical_crossentropy', optimizer = Adam(), metrics = ['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:08:36.315446Z","iopub.execute_input":"2021-10-13T03:08:36.315766Z","iopub.status.idle":"2021-10-13T03:08:36.329481Z","shell.execute_reply.started":"2021-10-13T03:08:36.315727Z","shell.execute_reply":"2021-10-13T03:08:36.328234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks_list = [\n    keras.callbacks.ModelCheckpoint(filepath = 'my_model.h',monitor = 'val_loss', save_best_only = True, verbose = 1),\n    keras.callbacks.EarlyStopping(monitor = 'val_loss', patience = 10,verbose = 1)\n]","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:08:36.331029Z","iopub.execute_input":"2021-10-13T03:08:36.331328Z","iopub.status.idle":"2021-10-13T03:08:36.339198Z","shell.execute_reply.started":"2021-10-13T03:08:36.33129Z","shell.execute_reply":"2021-10-13T03:08:36.338296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator,\n                    steps_per_epoch = train_generator.samples // 32,\n                    epochs = 30,\n                    callbacks = callbacks_list,\n                    validation_data = validation_generator,\n                    verbose = 1,\n                    validation_steps = validation_generator.samples // 32)","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:08:36.34069Z","iopub.execute_input":"2021-10-13T03:08:36.340965Z","iopub.status.idle":"2021-10-13T03:14:40.905227Z","shell.execute_reply.started":"2021-10-13T03:08:36.340937Z","shell.execute_reply":"2021-10-13T03:14:40.903534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_dict = history.history\nloss_values = history_dict['loss']\nval_loss_values = history_dict['val_loss']\n\nepochs_x = range(1, len(loss_values) + 1)\nplt.figure(figsize=(10,10))\nplt.subplot(2,1,1)\nplt.plot(epochs_x, loss_values, 'bo', label='Training loss')\nplt.plot(epochs_x, val_loss_values, 'b', label='Validation loss')\nplt.title('Training and validation Loss and Acc')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.subplot(2,1,2)\nacc_values = history_dict['accuracy']\nval_acc_values = history_dict['val_accuracy']\nplt.plot(epochs_x, acc_values, 'bo', label='Training acc')\nplt.plot(epochs_x, val_acc_values, 'b', label='Validation acc')\n\nplt.xlabel('Epochs')\nplt.ylabel('Acc')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:14:40.906641Z","iopub.status.idle":"2021-10-13T03:14:40.907094Z","shell.execute_reply.started":"2021-10-13T03:14:40.906844Z","shell.execute_reply":"2021-10-13T03:14:40.906864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model('my_model.h')","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:14:40.908863Z","iopub.status.idle":"2021-10-13T03:14:40.909222Z","shell.execute_reply.started":"2021-10-13T03:14:40.909036Z","shell.execute_reply":"2021-10-13T03:14:40.909053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate_generator(validation_generator)\nprint('Val loss:', score[0])\nprint('Val accuracy:', score[1])","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:14:40.910883Z","iopub.status.idle":"2021-10-13T03:14:40.911248Z","shell.execute_reply.started":"2021-10-13T03:14:40.911067Z","shell.execute_reply":"2021-10-13T03:14:40.911085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testing_data_generator = ImageDataGenerator(rescale = 1./255)\ntest_generator = testing_data_generator.flow_from_dataframe(test, testing_directory, x_col = 'id', y_col = 'label', color_mode = 'rgb',class_mode = 'categorical', target_size = (299, 299), batch_size = 32, shuffle = False,seed = 123)","metadata":{"execution":{"iopub.status.busy":"2021-10-13T03:14:40.912743Z","iopub.status.idle":"2021-10-13T03:14:40.913721Z","shell.execute_reply.started":"2021-10-13T03:14:40.913476Z","shell.execute_reply":"2021-10-13T03:14:40.913499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}