{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Citation\nWill Cukierski. (2013). CIFAR-10 - Object Recognition in Images. Kaggle. https://kaggle.com/competitions/cifar-10","metadata":{}},{"cell_type":"markdown","source":"**Add libarchive to the Environment**","metadata":{}},{"cell_type":"code","source":"# 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\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input/cifar-10'):\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\n!pip install libarchive-c\nimport libarchive\nlibarchive.extract_file('/kaggle/input/cifar-10/test.7z')\nlibarchive.extract_file('/kaggle/input/cifar-10/train.7z')\n\n# Listing the contents of the extracted directory\ncontents = os.listdir('/kaggle/working/train')\nprint(\"Contents of the directory:\")\nprint(contents[:5])  # Displaying the first few items in the directory","metadata":{"execution":{"iopub.status.busy":"2023-12-04T13:43:28.959993Z","iopub.execute_input":"2023-12-04T13:43:28.960418Z","iopub.status.idle":"2023-12-04T13:45:16.512073Z","shell.execute_reply.started":"2023-12-04T13:43:28.960386Z","shell.execute_reply":"2023-12-04T13:45:16.511084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Loading Libraries**","metadata":{}},{"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# import backend\nimport tensorflow as  tf\n\n# Model architecture\nfrom PIL import Image\nfrom sklearn.model_selection import train_test_split\nfrom keras.models import Sequential\nfrom tensorflow.keras.models import Sequential, load_model\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, BatchNormalization\nfrom keras.layers import MaxPool2D, Activation, MaxPooling2D\n#from keras.layers.convolutionalimport Convolution2D, MaxPooling2D\n\n# model optimisation and scores\nfrom sklearn import metrics\nfrom sklearn.metrics import accuracy_score, f1_score\nfrom tensorflow.keras.optimizers import Adam, SGD\nfrom sklearn.metrics import confusion_matrix, classification_report\nfrom sklearn.metrics import roc_auc_score, auc, roc_curve\n\n\n# Annealer\nfrom keras.callbacks import LearningRateScheduler\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint\n\n\n# Data processing\n# from keras.preprocessing.image import ImageDataGenerator\nfrom keras.utils import to_categorical\n# from keras.preprocessing import image\n\n#Save the model\nfrom tensorflow.keras.models import load_model\n\n# Visualization\nimport matplotlib.pyplot as plt\nimport h5py","metadata":{"execution":{"iopub.status.busy":"2023-12-04T13:45:16.514189Z","iopub.execute_input":"2023-12-04T13:45:16.514513Z","iopub.status.idle":"2023-12-04T13:45:33.89918Z","shell.execute_reply.started":"2023-12-04T13:45:16.514485Z","shell.execute_reply":"2023-12-04T13:45:33.898352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Defining Loading Functions**","metadata":{}},{"cell_type":"code","source":"def load_images_from_directory(directory):\n    images_list = []\n    for filename in sorted(os.listdir(directory), key=lambda x: int(x.split('.')[0])):  # Sort by ID\n        with Image.open(os.path.join(directory, filename)) as img:\n            images_list.append(np.array(img))\n    images = np.array(images_list)\n    return images","metadata":{"execution":{"iopub.status.busy":"2023-12-04T13:45:33.900305Z","iopub.execute_input":"2023-12-04T13:45:33.900864Z","iopub.status.idle":"2023-12-04T13:45:33.907238Z","shell.execute_reply.started":"2023-12-04T13:45:33.900836Z","shell.execute_reply":"2023-12-04T13:45:33.906048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Read Training Data and Labels**","metadata":{}},{"cell_type":"code","source":"################## Preparing Train Data (x_train) ##################\n# Load Data\nTrain_Data = load_images_from_directory('/kaggle/working/train')\nprint('Shape of Test_Data',Train_Data.shape)\n\n# Normalize the data\nx_train = Train_Data.astype('float32') / 255.0\n\n# Data Augmentation\ndatagen = ImageDataGenerator(\n    rotation_range=15,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    horizontal_flip=True,\n    )\n\ndatagen.fit(x_train)\n\n################## Preparing Train Labels (y_train)##################\nfrom sklearn.preprocessing import LabelBinarizer\n\nTrain_Labels = pd.read_csv('../input/cifar-10/trainLabels.csv')\ny_train_strings = Train_Labels['label']\nprint(\"Number of training sample: \",Train_Labels.shape[0])\n\n# Your list of labels\nlabels = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']\n\n# Initialize LabelBinarizer\nlabel_binarizer = LabelBinarizer()\nlabel_binarizer.fit(labels)\n\n# Convert string labels to one-hot encoded vectors\ny_train = label_binarizer.transform(y_train_strings)\n\n# Display the resulting one-hot encoded vectors\nprint(y_train.shape)\nprint(y_train[0])","metadata":{"execution":{"iopub.status.busy":"2023-12-04T13:45:33.910048Z","iopub.execute_input":"2023-12-04T13:45:33.910431Z","iopub.status.idle":"2023-12-04T13:45:43.956274Z","shell.execute_reply.started":"2023-12-04T13:45:33.910397Z","shell.execute_reply":"2023-12-04T13:45:43.955353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Coding for CNN**","metadata":{}},{"cell_type":"code","source":"#Two GPU Function\nstrategy = tf.distribute.MirroredStrategy()\nprint('Number of devices: {}'.format(strategy.num_replicas_in_sync))\n\n# Open a strategy scope.\nwith strategy.scope():\n    # Everything that creates variables should be under the strategy scope.\n    # In general this is only model's `build`, `compile`.\n    \n    # Build the model\n    model = Sequential([\n        Conv2D(64, (3, 3),padding='same',input_shape=(32, 32, 3),activation='relu', ),\n        BatchNormalization(),\n        MaxPooling2D(2, 2),\n        \n        Conv2D(128, (3, 3),padding='same', activation='relu'),\n        BatchNormalization(),\n        MaxPooling2D(2, 2),\n        \n        Conv2D(256, (3, 3),padding='same', activation='relu'),\n        BatchNormalization(),\n        MaxPooling2D(2, 2),\n        \n        Conv2D(256, (3, 3),padding='same', activation='relu'),\n        BatchNormalization(),\n        MaxPooling2D(2, 2),\n        \n        Conv2D(512, (3, 3),padding='same', activation='relu'),\n        BatchNormalization(),\n        MaxPooling2D(2, 2),\n        \n        Conv2D(256, (3, 3),padding='same', activation='relu'),\n        Flatten(),\n        \n        Dropout(0.3),\n        Dense(128, activation='relu'),\n        Dense(10, activation='softmax')\n    ])\n    \n    # Compile the model\n    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-04T13:45:43.957736Z","iopub.execute_input":"2023-12-04T13:45:43.95815Z","iopub.status.idle":"2023-12-04T13:45:50.161704Z","shell.execute_reply.started":"2023-12-04T13:45:43.958104Z","shell.execute_reply":"2023-12-04T13:45:50.160869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Training, Validating, and Saving the Model**","metadata":{}},{"cell_type":"code","source":"#adjusting learning rate    \ndef lr_schedule(epoch):\n    lrate = 0.001\n    if epoch>15:\n        lrate = 0.0005\n    return lrate\n\n# Train the model\n#model.fit(x_train, y_train, epochs=20, callbacks=[LearningRateScheduler(lr_schedule)], batch_size=100, validation_split=0.2)\nmodel.fit(datagen.flow(x_train, y_train, batch_size=128),\n          epochs=100, \n          callbacks=[LearningRateScheduler(lr_schedule)], \n          validation_data=(x_train[-int(len(x_train)*0.2):], y_train[-int(len(y_train)*0.2):]))\n\n\n# To evaluate on validation data we use:\nvalidation_loss, validation_accuracy = model.evaluate(x_train[int(0.8*len(x_train)):], y_train[int(0.8*len(y_train)):])\nprint(f\"Validation Accuracy: {validation_accuracy * 100:.2f}%\")\n\nAccuracy = str(validation_accuracy*100)[:2]\nprint(\"Accuracy\",Accuracy)\n\n# Save the model (optional)\nmodel.save(f\"cifar10_model_0.{Accuracy}.h5\")\nprint('model saved')","metadata":{"execution":{"iopub.status.busy":"2023-12-04T13:45:50.162915Z","iopub.execute_input":"2023-12-04T13:45:50.163163Z","iopub.status.idle":"2023-12-04T14:38:16.555758Z","shell.execute_reply.started":"2023-12-04T13:45:50.163142Z","shell.execute_reply":"2023-12-04T14:38:16.554748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Preparing Test Data**","metadata":{}},{"cell_type":"code","source":"# Load Data\nTest_Data = load_images_from_directory('/kaggle/working/test')\nprint('Shape of Test_Data',Test_Data.shape)\n\n# Normalize the data\nx_test = Test_Data.astype('float32') / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-12-04T14:38:16.556999Z","iopub.execute_input":"2023-12-04T14:38:16.557338Z","iopub.status.idle":"2023-12-04T14:39:13.127654Z","shell.execute_reply.started":"2023-12-04T14:38:16.557302Z","shell.execute_reply":"2023-12-04T14:39:13.12676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Loading Model and Predicting Test Labels**","metadata":{}},{"cell_type":"code","source":"# Load the model\nloaded_model = load_model(f\"cifar10_model_0.{Accuracy}.h5\")\n\npredictions_vecs = loaded_model.predict(x_test)\npredictions_final = np.argmax(predictions_vecs, axis=1)\npredicted_classes = [labels[i] for i in predictions_final]\n\n# Extract file IDs\ni=1\nids = []\nfor i in range(x_test.shape[0]):\n    ids.append(i+1) \n\nprint('ids shape is', len(ids))\n\n# Generate submission DataFrame\nsubmission = pd.DataFrame({\n    'id': ids,\n    'label': predicted_classes\n})\n\n# Ensure labels are strings and sort by 'id'\nsubmission = submission.sort_values(by='id').reset_index(drop=True)\n\n# Save submission to CSV\nsubmission.to_csv('submission.csv', index=False)\nprint('submission file saved')","metadata":{"execution":{"iopub.status.busy":"2023-12-04T14:39:13.132934Z","iopub.execute_input":"2023-12-04T14:39:13.13326Z","iopub.status.idle":"2023-12-04T14:39:53.300531Z","shell.execute_reply.started":"2023-12-04T14:39:13.133232Z","shell.execute_reply":"2023-12-04T14:39:53.299332Z"},"trusted":true},"execution_count":null,"outputs":[]}]}