{"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":"import os, cv2, random, time, shutil, csv\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport tqdm as tqdm\nimport json","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-20T13:34:51.533356Z","iopub.execute_input":"2023-02-20T13:34:51.533879Z","iopub.status.idle":"2023-02-20T13:34:52.415211Z","shell.execute_reply.started":"2023-02-20T13:34:51.533843Z","shell.execute_reply":"2023-02-20T13:34:52.413651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files_type = {}\nfiles_extension = {}\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        if '.' in filename:\n            extension = filename[filename.find('.')+1:]\n            files_extension[extension] = files_extension.get(extension, 0) + 1\n            if extension not in files_type: files_type[extension] = []\n            files_type[extension].append(os.path.join(dirname, filename))\nprint(files_extension)\nprint(files_type['csv'])","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:34:52.417260Z","iopub.execute_input":"2023-02-20T13:34:52.417643Z","iopub.status.idle":"2023-02-20T13:35:09.671574Z","shell.execute_reply.started":"2023-02-20T13:34:52.417608Z","shell.execute_reply":"2023-02-20T13:35:09.669670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\nlabels_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:35:09.674398Z","iopub.execute_input":"2023-02-20T13:35:09.675362Z","iopub.status.idle":"2023-02-20T13:35:09.735553Z","shell.execute_reply.started":"2023-02-20T13:35:09.675303Z","shell.execute_reply":"2023-02-20T13:35:09.734058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:35:09.739063Z","iopub.execute_input":"2023-02-20T13:35:09.739994Z","iopub.status.idle":"2023-02-20T13:35:09.777988Z","shell.execute_reply.started":"2023-02-20T13:35:09.739903Z","shell.execute_reply":"2023-02-20T13:35:09.776356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:35:09.779893Z","iopub.execute_input":"2023-02-20T13:35:09.780321Z","iopub.status.idle":"2023-02-20T13:35:10.442537Z","shell.execute_reply.started":"2023-02-20T13:35:09.780286Z","shell.execute_reply":"2023-02-20T13:35:10.441051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.describe()","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:35:10.444429Z","iopub.execute_input":"2023-02-20T13:35:10.445295Z","iopub.status.idle":"2023-02-20T13:35:10.785192Z","shell.execute_reply.started":"2023-02-20T13:35:10.445241Z","shell.execute_reply":"2023-02-20T13:35:10.783480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_breeds = sorted(list(set(labels_df['breed'])))\nn_classes = len(dog_breeds)\nclass_to_num = dict(zip(dog_breeds, range(n_classes)))\ntrain_dir = '/kaggle/input/dog-breed-identification/train/'\ntest_dir = '/kaggle/input/dog-breed-identification/test/'","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:35:10.787272Z","iopub.execute_input":"2023-02-20T13:35:10.787697Z","iopub.status.idle":"2023-02-20T13:35:10.796498Z","shell.execute_reply.started":"2023-02-20T13:35:10.787657Z","shell.execute_reply":"2023-02-20T13:35:10.794382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.applications.resnet50 import preprocess_input\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import BatchNormalization, Dense, GlobalAveragePooling2D, Lambda, Dropout, InputLayer, Input\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import load_img\nfrom tensorflow.keras.preprocessing import image\nfrom sklearn.utils import shuffle\nimport gc","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:35:10.798992Z","iopub.execute_input":"2023-02-20T13:35:10.799569Z","iopub.status.idle":"2023-02-20T13:35:20.773475Z","shell.execute_reply.started":"2023-02-20T13:35:10.799519Z","shell.execute_reply":"2023-02-20T13:35:20.771822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(128, 128, 3))","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:35:20.775091Z","iopub.execute_input":"2023-02-20T13:35:20.775900Z","iopub.status.idle":"2023-02-20T13:35:24.184300Z","shell.execute_reply.started":"2023-02-20T13:35:20.775855Z","shell.execute_reply":"2023-02-20T13:35:24.182398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_model.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:35:24.191120Z","iopub.execute_input":"2023-02-20T13:35:24.191606Z","iopub.status.idle":"2023-02-20T13:35:24.204236Z","shell.execute_reply.started":"2023-02-20T13:35:24.191565Z","shell.execute_reply":"2023-02-20T13:35:24.203021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = base_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(1024, activation='relu')(x)\nx = Dropout(0.5)(x)\npredictions = Dense(n_classes, activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:35:24.205440Z","iopub.execute_input":"2023-02-20T13:35:24.205904Z","iopub.status.idle":"2023-02-20T13:35:24.270195Z","shell.execute_reply.started":"2023-02-20T13:35:24.205866Z","shell.execute_reply":"2023-02-20T13:35:24.268633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Model(inputs=base_model.input, outputs=predictions)\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:35:24.271726Z","iopub.execute_input":"2023-02-20T13:35:24.272167Z","iopub.status.idle":"2023-02-20T13:35:24.313430Z","shell.execute_reply.started":"2023-02-20T13:35:24.272130Z","shell.execute_reply":"2023-02-20T13:35:24.312073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_train_images(dir_path):\n    x_train, y_train = [], []\n    for img_path in os.listdir(dir_path):\n        img = load_img(os.path.join(dir_path, img_path), target_size=(128, 128, 1))\n        x_train.append(preprocess_input(np.array(img).astype(\"float32\")))\n        img_name = img_path.split('/')[-1].split('.')[0]\n        class_id = class_to_num[labels_df[labels_df['id'] == img_name]['breed'].values[0]]\n        y_train.append(class_id)\n    x_train = np.array(x_train)\n    y_train = to_categorical(y_train, num_classes=n_classes)\n    x_train, y_train = shuffle(x_train, y_train)\n    return x_train, y_train","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:35:24.315222Z","iopub.execute_input":"2023-02-20T13:35:24.315675Z","iopub.status.idle":"2023-02-20T13:35:24.325776Z","shell.execute_reply.started":"2023-02-20T13:35:24.315635Z","shell.execute_reply":"2023-02-20T13:35:24.324412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, y_train = load_train_images(train_dir)\nprint(x_train.shape)\nprint(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:35:24.327856Z","iopub.execute_input":"2023-02-20T13:35:24.328473Z","iopub.status.idle":"2023-02-20T13:37:27.570597Z","shell.execute_reply.started":"2023-02-20T13:35:24.328413Z","shell.execute_reply":"2023-02-20T13:37:27.569044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_test_images(dir_path):\n    x_test = []\n    for img_path in os.listdir(dir_path):\n        img = load_img(os.path.join(dir_path, img_path), target_size=(128, 128, 1))\n        x_test.append(preprocess_input(np.array(img).astype(\"float32\")))\n    x_test = np.array(x_test)\n    return x_test","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:37:27.572279Z","iopub.execute_input":"2023-02-20T13:37:27.572680Z","iopub.status.idle":"2023-02-20T13:37:27.581583Z","shell.execute_reply.started":"2023-02-20T13:37:27.572644Z","shell.execute_reply":"2023-02-20T13:37:27.580055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = load_test_images(test_dir)\nprint(x_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:37:27.583994Z","iopub.execute_input":"2023-02-20T13:37:27.584990Z","iopub.status.idle":"2023-02-20T13:39:10.032486Z","shell.execute_reply.started":"2023-02-20T13:37:27.584903Z","shell.execute_reply":"2023-02-20T13:39:10.031024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train, y_train, epochs=50, batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2023-02-20T13:39:10.034769Z","iopub.execute_input":"2023-02-20T13:39:10.035218Z","iopub.status.idle":"2023-02-20T18:16:21.005781Z","shell.execute_reply.started":"2023-02-20T13:39:10.035176Z","shell.execute_reply":"2023-02-20T18:16:21.004413Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_model_history(history, fig_title, shape=(24,6)):\n    fig, ax = plt.subplots(1,2, figsize=shape)\n    fig.suptitle(fig_title, fontsize=16)\n    ax[0].plot(history.history['loss'],     color='b', label=\"Training loss\")\n    #ax[0].plot(history.history['val_loss'], color='r', label=\"validation loss\",axes =ax[0])\n    legend = ax[0].legend(loc='best', shadow=True)\n    ax[0].set_xlabel('Epochs')\n    ax[0].set_ylabel('Loss')\n    ax[1].plot(history.history['accuracy'],     color='b', label=\"Training accuracy\")\n    #ax[1].plot(history.history['val_accuracy'], color='r', label=\"Validation accuracy\")\n    legend = ax[1].legend(loc='best', shadow=True)\n    ax[1].set_xlabel('Epochs')\n    ax[1].set_ylabel('Accuracy')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-20T18:16:21.007817Z","iopub.execute_input":"2023-02-20T18:16:21.008244Z","iopub.status.idle":"2023-02-20T18:16:21.019244Z","shell.execute_reply.started":"2023-02-20T18:16:21.008208Z","shell.execute_reply":"2023-02-20T18:16:21.017400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model_history(history, 'History of the Model')","metadata":{"execution":{"iopub.status.busy":"2023-02-20T18:16:21.020926Z","iopub.execute_input":"2023-02-20T18:16:21.021384Z","iopub.status.idle":"2023-02-20T18:16:21.532493Z","shell.execute_reply.started":"2023-02-20T18:16:21.021346Z","shell.execute_reply":"2023-02-20T18:16:21.531049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_image(model, x_test):\n    num_to_class = {v: k for k, v in class_to_num.items()}\n    predictions = model.predict(x_test)\n    predicted_labels = []\n    for pred_i in predictions:\n        label_index = np.argmax(pred_i)\n        predicted_labels.append(num_to_class[label_index])\n    return predicted_labels","metadata":{"execution":{"iopub.status.busy":"2023-02-20T18:16:21.534361Z","iopub.execute_input":"2023-02-20T18:16:21.534880Z","iopub.status.idle":"2023-02-20T18:16:21.542874Z","shell.execute_reply.started":"2023-02-20T18:16:21.534842Z","shell.execute_reply":"2023-02-20T18:16:21.541208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_labels = predict_image(model, x_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-20T18:16:21.544537Z","iopub.execute_input":"2023-02-20T18:16:21.544987Z","iopub.status.idle":"2023-02-20T18:21:51.065736Z","shell.execute_reply.started":"2023-02-20T18:16:21.544948Z","shell.execute_reply":"2023-02-20T18:21:51.064265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(predicted_labels))\nprint(predicted_labels[:10])","metadata":{"execution":{"iopub.status.busy":"2023-02-20T18:21:51.067434Z","iopub.execute_input":"2023-02-20T18:21:51.068632Z","iopub.status.idle":"2023-02-20T18:21:51.075139Z","shell.execute_reply.started":"2023-02-20T18:21:51.068579Z","shell.execute_reply":"2023-02-20T18:21:51.073758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Print the predictions and display the image\nprint(\"Predicted label: \", predicted_labels[25])\nplt.imshow(x_test[25])\nplt.title(predicted_labels[25])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-20T18:44:49.314623Z","iopub.execute_input":"2023-02-20T18:44:49.315186Z","iopub.status.idle":"2023-02-20T18:44:49.584863Z","shell.execute_reply.started":"2023-02-20T18:44:49.315142Z","shell.execute_reply":"2023-02-20T18:44:49.583604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from keras.applications.inception_v3 import InceptionV3, preprocess_input\n#inception_preprocessor = preprocess_input\n#from keras.applications.xception import Xception, preprocess_input\n#xception_preprocessor = preprocess_input\n#from keras.applications.nasnet import NASNetLarge, preprocess_input\n#nasnet_preprocessor = preprocess_input\n#from keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input\n#inc_resnet_preprocessor = preprocess_input\n\n#models = [InceptionV3,  InceptionResNetV2, Xception, ]\n#preprocs = [inception_preprocessor,  inc_resnet_preprocessor, xception_preprocessor]","metadata":{"execution":{"iopub.status.busy":"2023-02-20T18:21:51.388537Z","iopub.execute_input":"2023-02-20T18:21:51.389612Z","iopub.status.idle":"2023-02-20T18:21:51.395196Z","shell.execute_reply.started":"2023-02-20T18:21:51.389573Z","shell.execute_reply":"2023-02-20T18:21:51.393858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-20T19:03:04.821161Z","iopub.execute_input":"2023-02-20T19:03:04.821709Z","iopub.status.idle":"2023-02-20T19:08:35.469715Z","shell.execute_reply.started":"2023-02-20T19:03:04.821659Z","shell.execute_reply":"2023-02-20T19:08:35.468441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target, dog_breeds = pd.factorize(labels_df['breed'], sort = True)","metadata":{"execution":{"iopub.status.busy":"2023-02-20T19:10:57.715978Z","iopub.execute_input":"2023-02-20T19:10:57.716516Z","iopub.status.idle":"2023-02-20T19:10:57.725014Z","shell.execute_reply.started":"2023-02-20T19:10:57.716477Z","shell.execute_reply":"2023-02-20T19:10:57.723571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns = list(submission.columns)\ncolumns.remove('id')\nsubmission.loc[:, dog_breeds] = predictions\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-20T19:13:46.391439Z","iopub.execute_input":"2023-02-20T19:13:46.392815Z","iopub.status.idle":"2023-02-20T19:13:47.099558Z","shell.execute_reply.started":"2023-02-20T19:13:46.392754Z","shell.execute_reply":"2023-02-20T19:13:47.097988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-20T19:13:52.303379Z","iopub.execute_input":"2023-02-20T19:13:52.303895Z","iopub.status.idle":"2023-02-20T19:13:54.599860Z","shell.execute_reply.started":"2023-02-20T19:13:52.303857Z","shell.execute_reply":"2023-02-20T19:13:54.598516Z"},"trusted":true},"execution_count":null,"outputs":[]}]}