{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\n\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":"2024-03-23T19:45:09.776761Z","iopub.execute_input":"2024-03-23T19:45:09.777575Z","iopub.status.idle":"2024-03-23T19:45:09.782192Z","shell.execute_reply.started":"2024-03-23T19:45:09.777539Z","shell.execute_reply":"2024-03-23T19:45:09.781246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\n\nfrom PIL import Image\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import layers, activations,losses\n","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:45:10.844190Z","iopub.execute_input":"2024-03-23T19:45:10.845041Z","iopub.status.idle":"2024-03-23T19:45:10.849737Z","shell.execute_reply.started":"2024-03-23T19:45:10.845006Z","shell.execute_reply":"2024-03-23T19:45:10.848748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def addAugmentedData(dataset):\n    augmentationLayer = Sequential([\n        layers.RandomFlip(seed=1111),\n        layers.RandomRotation(0.2, seed=1111),\n        layers.RandomZoom(0.2, seed=1111)\n    ])\n    augmentedData = dataset.map(lambda x, y: (augmentationLayer(x), y))\n    augmentedData = augmentedData.map(lambda img, label: (tf.image.convert_image_dtype(img, tf.uint8), label))\n\n    completeDataset = dataset.concatenate(augmentedData)\n    return completeDataset\n","metadata":{"execution":{"iopub.status.busy":"2024-03-23T17:36:56.503645Z","iopub.execute_input":"2024-03-23T17:36:56.504179Z","iopub.status.idle":"2024-03-23T17:36:56.510248Z","shell.execute_reply.started":"2024-03-23T17:36:56.504151Z","shell.execute_reply":"2024-03-23T17:36:56.509390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def loadImagesWithClassName(csv_file_path, imagePath):\n    labelsDf = pd.read_csv(csv_file_path)\n    image_class_mapping = dict(zip(labelsDf['id'], labelsDf['breed']))\n\n    file_paths = [imagePath + image_name + '.jpg' for image_name in labelsDf['id']]\n    labels = [class_name for class_name in labelsDf['breed']]\n    \n    num_classes = len(labelsDf['breed'].unique())\n    labels_one_hot = tf.one_hot(pd.Categorical(labels).codes, num_classes) #see\n    \n    dataset = tf.data.Dataset.from_tensor_slices((file_paths, labels_one_hot))\n    dataset = dataset.map(load_image)\n    \n#     dataset = addAugmentedData(dataset)\n    dataset = dataset.shuffle(buffer_size=(dataset.cardinality().numpy())/2)\n    \n    train_dataset = dataset.skip(int(0.2*(dataset.cardinality().numpy()))).batch(64)\n    test_dataset =dataset.take(int(0.2*(dataset.cardinality().numpy()))).batch(64)\n\n    return  train_dataset, test_dataset\n\ndef load_image(file_path, label):\n    image = tf.io.read_file(file_path)\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize_with_crop_or_pad(image,500,500)\n    return image, label\n\ntrain, test = loadImagesWithClassName('/kaggle/input/dog-breed-identification/labels.csv', '/kaggle/input/dog-breed-identification/train/')\n\n\ntrain = train.prefetch(buffer_size=tf.data.AUTOTUNE)\ntest = test.prefetch(buffer_size=tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T17:36:56.512842Z","iopub.execute_input":"2024-03-23T17:36:56.514028Z","iopub.status.idle":"2024-03-23T17:36:57.637894Z","shell.execute_reply.started":"2024-03-23T17:36:56.513944Z","shell.execute_reply":"2024-03-23T17:36:57.636930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"firstTake = train.take(4)\nimages, labels1 = list(firstTake)[0]\n\nfirstImage = images[0]\nnumpyImage = firstImage.numpy().astype(\"uint8\")\nimg = Image.fromarray(numpyImage)\nimg\n","metadata":{"execution":{"iopub.status.busy":"2024-03-23T17:36:57.639074Z","iopub.execute_input":"2024-03-23T17:36:57.639372Z","iopub.status.idle":"2024-03-23T17:37:24.742672Z","shell.execute_reply.started":"2024-03-23T17:36:57.639349Z","shell.execute_reply":"2024-03-23T17:37:24.741770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def initializeModel(networkArray):\n    savePath = '/kaggle/working/model.h5'\n    model = Sequential(networkArray)\n    \n    if os.path.exists(savePath):\n        model.load_weights(savePath)\n        print(\":::Loaded saved weights:::\")\n        \n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3), loss='categorical_crossentropy', metrics=['accuracy'])\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:54:06.438486Z","iopub.execute_input":"2024-03-23T19:54:06.438846Z","iopub.status.idle":"2024-03-23T19:54:06.444804Z","shell.execute_reply.started":"2024-03-23T19:54:06.438816Z","shell.execute_reply":"2024-03-23T19:54:06.443683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def createAndTrainModel(networkArray, train, test):\n        savePath = '/kaggle/working/model.h5'\n        \n        model = initializeModel(networkArray)\n        checkpointCallback = tf.keras.callbacks.ModelCheckpoint(\n            savePath,\n            monitor = 'val_loss',\n            verbose = 1,\n            save_best_only = True,\n            save_weights_only = True,\n        )\n        \n        history = model.fit(train, validation_data=test, epochs = 120, callbacks = [checkpointCallback])\n    \n        return pd.DataFrame(history.history), model","metadata":{"execution":{"iopub.status.busy":"2024-03-23T17:37:24.752849Z","iopub.execute_input":"2024-03-23T17:37:24.753369Z","iopub.status.idle":"2024-03-23T17:37:24.776027Z","shell.execute_reply.started":"2024-03-23T17:37:24.753336Z","shell.execute_reply":"2024-03-23T17:37:24.775109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#AlexNet architecture\nnetwork = [\n        layers.InputLayer(input_shape=( 500, 500, 3)),\n        layers.Rescaling(1/255),\n        layers.Conv2D(96, kernel_size=(11,11), strides= (4, 4),padding= 'valid', activation= 'relu'),\n        layers.MaxPooling2D(pool_size=(3,3), strides= (2,2),padding= 'valid'),\n        layers.Conv2D(256, kernel_size=(5,5), strides= (1,1),padding= 'same', activation= 'relu'),\n        layers.MaxPooling2D(pool_size=(3,3), strides= (2,2), padding= 'valid'),\n        layers.Conv2D(384, kernel_size=(3,3), strides= (1,1),padding= 'same', activation= 'relu'),\n#         layers.Conv2D(384, kernel_size=(3,3), strides= (1,1), padding= 'same', activation= 'relu'),\n#         layers.Conv2D(256, kernel_size=(3,3), strides= (1,1), padding= 'same', activation= 'relu'),\n        layers.MaxPooling2D(pool_size=(3,3), strides= (2,2),padding= 'valid'),\n        layers.Flatten(),\n        layers.Dense(4098, activation= 'relu'),\n        layers.Dense(2048, activation= 'relu'), \n        layers.Dense(1024, activation= 'relu'),\n        layers.Dense(units = 120, activation = 'softmax')\n]\n","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:53:48.034806Z","iopub.execute_input":"2024-03-23T19:53:48.035194Z","iopub.status.idle":"2024-03-23T19:53:48.056693Z","shell.execute_reply.started":"2024-03-23T19:53:48.035162Z","shell.execute_reply":"2024-03-23T19:53:48.055664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nhistory, model = createAndTrainModel(network,  train, test)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T17:37:24.821364Z","iopub.execute_input":"2024-03-23T17:37:24.821636Z","iopub.status.idle":"2024-03-23T18:13:55.393537Z","shell.execute_reply.started":"2024-03-23T17:37:24.821611Z","shell.execute_reply":"2024-03-23T18:13:55.392663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()\nmodel.save_weights('/kaggle/working/model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-23T18:13:55.396430Z","iopub.execute_input":"2024-03-23T18:13:55.396710Z","iopub.status.idle":"2024-03-23T18:13:58.620633Z","shell.execute_reply.started":"2024-03-23T18:13:55.396686Z","shell.execute_reply":"2024-03-23T18:13:58.619475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history[['accuracy','val_accuracy']].plot()","metadata":{"execution":{"iopub.status.busy":"2024-03-23T18:13:58.621841Z","iopub.execute_input":"2024-03-23T18:13:58.622201Z","iopub.status.idle":"2024-03-23T18:13:58.925363Z","shell.execute_reply.started":"2024-03-23T18:13:58.622169Z","shell.execute_reply":"2024-03-23T18:13:58.924429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labelsDf = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\nlabels = [class_name for class_name in labelsDf['breed']]\n\nbreeds = pd.Categorical(labels).categories\nbreeds","metadata":{"execution":{"iopub.status.busy":"2024-03-23T18:13:58.926415Z","iopub.execute_input":"2024-03-23T18:13:58.926680Z","iopub.status.idle":"2024-03-23T18:13:58.953239Z","shell.execute_reply.started":"2024-03-23T18:13:58.926656Z","shell.execute_reply":"2024-03-23T18:13:58.952357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# print(\"Example\"+ \"     |     \"+ \"Label\"+\"     |     \"+\"Prediction\"+ \"\\n\")\n\n# for images, labels in test:\n    \n#     predictions = model.predict(images)\n#     predictions = predictions.argmax(axis=1)\n#     predictions = [breeds[i] for i in predictions]\n#     labels = [breeds[i] for i in np.array(labels).argmax(axis=1)]\n#     for i in range(10):\n#         # Get the image and label\n#         image = images[i]\n#         label = labels[i]\n        \n#         # Convert the image tensor to a NumPy array\n#         image_np = image.numpy().astype(\"uint8\")\n#         im=plt.imshow(image_np) \n#         plt.axis('off')\n        \n#         plt.show()\n#         print(str(i+1) + \"     |     \"+label+\"     |     \"+predictions[i]+ \"\\n\" )","metadata":{"execution":{"iopub.status.busy":"2024-03-23T18:13:58.954429Z","iopub.execute_input":"2024-03-23T18:13:58.954788Z","iopub.status.idle":"2024-03-23T18:13:58.959616Z","shell.execute_reply.started":"2024-03-23T18:13:58.954757Z","shell.execute_reply":"2024-03-23T18:13:58.958705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_prediction_image(file_path):\n    image = tf.io.read_file(file_path)\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize_with_crop_or_pad(image,500,500)\n    return image","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:45:15.827991Z","iopub.execute_input":"2024-03-23T19:45:15.828658Z","iopub.status.idle":"2024-03-23T19:45:15.833860Z","shell.execute_reply.started":"2024-03-23T19:45:15.828628Z","shell.execute_reply":"2024-03-23T19:45:15.832814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenameList = []\nfor root, folders, files in os.walk('/kaggle/input/dog-breed-identification/test/'):\n    for file in files:\n        filenameList.append(file)\n        \npredictionDataset = tf.data.Dataset.from_tensor_slices([os.path.join('/kaggle/input/dog-breed-identification/test/',fileName) for fileName in filenameList])\npredictionDataset = predictionDataset.map(load_prediction_image).batch(64)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:46:59.330921Z","iopub.execute_input":"2024-03-23T19:46:59.331314Z","iopub.status.idle":"2024-03-23T19:46:59.540675Z","shell.execute_reply.started":"2024-03-23T19:46:59.331283Z","shell.execute_reply":"2024-03-23T19:46:59.539908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = initializeModel(network)\npredictionResult = model.predict(predictionDataset)\n\ndataframe1 = pd.DataFrame({\"id\": filenameList})\ndataframe2 = pd.DataFrame(predictionResult, columns = breeds)\n\nfinalPredictionDataFrame = pd.concat([dataframe1,dataframe2],axis=1)\nfinalPredictionDataFrame.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T19:54:12.479079Z","iopub.execute_input":"2024-03-23T19:54:12.479474Z","iopub.status.idle":"2024-03-23T19:54:25.064001Z","shell.execute_reply.started":"2024-03-23T19:54:12.479441Z","shell.execute_reply":"2024-03-23T19:54:25.063183Z"},"trusted":true},"execution_count":null,"outputs":[]}]}