{"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-19T13:07:25.328536Z","iopub.execute_input":"2023-09-19T13:07:25.329118Z","iopub.status.idle":"2023-09-19T13:07:25.339865Z","shell.execute_reply.started":"2023-09-19T13:07:25.329077Z","shell.execute_reply":"2023-09-19T13:07:25.338830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision.transforms as transforms\nfrom torch.utils.data import DataLoader\nfrom torchvision.datasets import ImageFolder\nfrom sklearn.preprocessing import LabelEncoder\nfrom torchvision import datasets\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:07:25.343655Z","iopub.execute_input":"2023-09-19T13:07:25.345120Z","iopub.status.idle":"2023-09-19T13:07:31.705041Z","shell.execute_reply.started":"2023-09-19T13:07:25.345077Z","shell.execute_reply":"2023-09-19T13:07:31.704011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize Device\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\nprint(\"Device:\", device)\nprint(\"Num GPUs Available: \", torch.cuda.device_count())","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:07:31.706348Z","iopub.execute_input":"2023-09-19T13:07:31.707979Z","iopub.status.idle":"2023-09-19T13:07:31.794849Z","shell.execute_reply.started":"2023-09-19T13:07:31.707901Z","shell.execute_reply":"2023-09-19T13:07:31.793720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Path Configurations\ninput_dir = \"/kaggle/input/cifar-10/\"\noutput_dir = \"/kaggle/working/cifar-10/\"\n\n# Extract labels from CSV file\ntrain_labels = pd.read_csv(os.path.join(input_dir, 'trainLabels.csv'))\nlabel_encoder = LabelEncoder()\ntrain_labels['encoded_labels'] = label_encoder.fit_transform(train_labels['label'])\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:07:31.797891Z","iopub.execute_input":"2023-09-19T13:07:31.798316Z","iopub.status.idle":"2023-09-19T13:07:31.874633Z","shell.execute_reply.started":"2023-09-19T13:07:31.798279Z","shell.execute_reply":"2023-09-19T13:07:31.873739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\nimport py7zr\nimport os\nfrom os import listdir, path\n\n# List all files in /kaggle/input\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        \n# Set directories\ninputdir = \"/kaggle/input/cifar-10/\"\noutputdir = \"/kaggle/working/cifar-10/\"        \n\n# Create the output directory if it doesn't exist\nos.makedirs(outputdir, exist_ok=True)\n\n# Get a list of 7z files\narchive_files = [f for f in os.listdir(inputdir) if f.endswith(\".7z\")]\n\n# Extract each archive\nfrom py7zr import unpack_7zarchive\nimport shutil\n\nshutil.register_unpack_format('7zip',['.7z'],unpack_7zarchive)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:07:31.876200Z","iopub.execute_input":"2023-09-19T13:07:31.876528Z","iopub.status.idle":"2023-09-19T13:07:50.942421Z","shell.execute_reply.started":"2023-09-19T13:07:31.876497Z","shell.execute_reply":"2023-09-19T13:07:50.941396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.unpack_archive('../input/cifar-10/train.7z', '/kaggle/temp/')","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:07:50.944265Z","iopub.execute_input":"2023-09-19T13:07:50.944665Z","iopub.status.idle":"2023-09-19T13:08:51.066238Z","shell.execute_reply.started":"2023-09-19T13:07:50.944630Z","shell.execute_reply":"2023-09-19T13:08:51.065255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\n# input directory path\ninput_dir = inputdir\n\n\n# Read the labels\ntrain_labels = pd.read_csv(\"../input/cifar-10/trainLabels.csv\", header=\"infer\")\n\n\n# Initialize and fit the label encoder\nclasses = train_labels['label'].unique()\n\nprint('Classes:')\nprint(classes)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:08:51.067908Z","iopub.execute_input":"2023-09-19T13:08:51.068272Z","iopub.status.idle":"2023-09-19T13:08:51.127884Z","shell.execute_reply.started":"2023-09-19T13:08:51.068239Z","shell.execute_reply":"2023-09-19T13:08:51.126874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.exists(\"/kaggle/temp/valid\"):\n    os.mkdir(\"/kaggle/temp/valid\")\n    \nparent_path_train = \"/kaggle/temp/train\"\nparent_path_valid = \"/kaggle/temp/valid\"\nparent_path_test = \"/kaggle/temp/test\"\n\nfor class1 in classes:\n    path_train = os.path.join(parent_path_train,class1)\n    if not os.path.exists(path_train):\n        os.mkdir(path_train)\n    path_valid = os.path.join(parent_path_valid,class1)\n    if not os.path.exists(path_valid):\n        os.mkdir(path_valid)\n        \nfor (int_ind,row) in train_labels.iterrows():\n    id = str(row[\"id\"])+\".png\"\n    source_path = os.path.join(parent_path_train,id)\n    \n    p=np.random.random()\n    if p<=0.8:\n        target_path = os.path.join(parent_path_train,row[\"label\"],id)\n        os.replace(source_path, target_path)\n    else:\n        target_path = os.path.join(parent_path_valid,row[\"label\"],id)\n        os.replace(source_path, target_path)","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:08:51.132861Z","iopub.execute_input":"2023-09-19T13:08:51.136856Z","iopub.status.idle":"2023-09-19T13:08:56.978922Z","shell.execute_reply.started":"2023-09-19T13:08:51.136814Z","shell.execute_reply":"2023-09-19T13:08:56.977768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, Activation, BatchNormalization, MaxPooling2D, Dropout, Flatten, Dense\n\ndef build_cnn_model(input_shape=(32, 32, 3)):\n    \"\"\"\n    Builds a CNN model for image classification.\n    \n    Parameters:\n        input_shape (tuple): Shape of the input images. Default is (32, 32, 3).\n    \n    Returns:\n        model: A Keras Sequential model.\n    \"\"\"\n    model = Sequential()\n    \n    # First Convolutional Block\n    model.add(Conv2D(64, (3, 3), padding='same', input_shape=input_shape))\n    model.add(Activation('relu'))\n    model.add(BatchNormalization())\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.25))\n    \n    # Second Convolutional Block\n    model.add(Conv2D(128, (3, 3), padding='same'))\n    model.add(Activation('relu'))\n    model.add(BatchNormalization())\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.25))\n    \n    # Third Convolutional Block\n    model.add(Conv2D(256, (3, 3), padding='same'))\n    model.add(Activation('relu'))\n    model.add(BatchNormalization())\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.3))\n    \n    # Flatten and Fully Connected Layer\n    model.add(Flatten())\n    model.add(Dense(10))\n    model.add(Activation('softmax'))\n    \n    return model\n\n\nmodel = build_cnn_model()\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:08:56.980598Z","iopub.execute_input":"2023-09-19T13:08:56.980927Z","iopub.status.idle":"2023-09-19T13:09:22.241423Z","shell.execute_reply.started":"2023-09-19T13:08:56.980896Z","shell.execute_reply":"2023-09-19T13:09:22.240594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import Adam\n\nopt = Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999)\nmodel.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:09:22.245513Z","iopub.execute_input":"2023-09-19T13:09:22.245856Z","iopub.status.idle":"2023-09-19T13:09:22.282082Z","shell.execute_reply.started":"2023-09-19T13:09:22.245823Z","shell.execute_reply":"2023-09-19T13:09:22.281118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Data augmenter for the training set\ntraining_data_augmenter = ImageDataGenerator(\n    featurewise_center=True,\n    samplewise_center=True,\n    rotation_range=15,\n    zoom_range=0.15,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    horizontal_flip=True,\n    vertical_flip=False,\n    rescale=1./255\n)\n\n# Data augmenter for the validation set\nvalidation_data_augmenter = ImageDataGenerator(\n    rescale=1./255,\n)\n\n# Load and augment training data\ntraining_data_loader = training_data_augmenter.flow_from_directory(\n    directory='/kaggle/temp/train/',\n    shuffle=True,\n    target_size=(48, 48),\n    batch_size=100\n)\n\n# Load and preprocess validation data\nvalidation_data_loader = validation_data_augmenter.flow_from_directory(\n    directory='/kaggle/temp/valid/',\n    shuffle=True,\n    target_size=(48, 48),\n    batch_size=100\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:09:22.283130Z","iopub.execute_input":"2023-09-19T13:09:22.283460Z","iopub.status.idle":"2023-09-19T13:09:24.087723Z","shell.execute_reply.started":"2023-09-19T13:09:22.283428Z","shell.execute_reply":"2023-09-19T13:09:24.086819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nstrategy = tf.distribute.MirroredStrategy(devices=[\"/gpu:0\", \"/gpu:1\"])  # Use GPU 0 and GPU 1\n\nwith strategy.scope():\n    model = build_cnn_model(input_shape=(48, 48, 3)) \n    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n\n# Train \ntraining_history = model.fit(\n    training_data_loader,\n    epochs=5,\n    validation_data=validation_data_loader,\n    steps_per_epoch=training_data_loader.n // training_data_loader.batch_size,\n    validation_steps=validation_data_loader.n // validation_data_loader.batch_size,\n    workers=8,\n    use_multiprocessing=True\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:09:24.088959Z","iopub.execute_input":"2023-09-19T13:09:24.089316Z","iopub.status.idle":"2023-09-19T13:17:06.046745Z","shell.execute_reply.started":"2023-09-19T13:09:24.089283Z","shell.execute_reply":"2023-09-19T13:17:06.045559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('my_model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:17:06.049153Z","iopub.execute_input":"2023-09-19T13:17:06.049522Z","iopub.status.idle":"2023-09-19T13:17:06.157616Z","shell.execute_reply.started":"2023-09-19T13:17:06.049493Z","shell.execute_reply":"2023-09-19T13:17:06.156601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.unpack_archive('/kaggle/input/cifar-10/test.7z','/kaggle/temp/test')\n\n#unregistering '7zip' format\nshutil.unregister_unpack_format('7zip')","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:17:06.159158Z","iopub.execute_input":"2023-09-19T13:17:06.159515Z","iopub.status.idle":"2023-09-19T13:36:40.282783Z","shell.execute_reply.started":"2023-09-19T13:17:06.159480Z","shell.execute_reply":"2023-09-19T13:36:40.281770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data augmenter for the test set\ntest_data_augmenter = ImageDataGenerator(\n    rescale=1./255,\n    featurewise_center=True,\n    featurewise_std_normalization=True\n)\n\n# Load and preprocess test data\ntest_data_loader = test_data_augmenter.flow_from_directory(\n    directory='/kaggle/temp/test/',\n    shuffle=False,  # Don't shuffle for test data to keep track of file names\n    target_size=(48, 48),\n    batch_size=100\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:36:40.284310Z","iopub.execute_input":"2023-09-19T13:36:40.284666Z","iopub.status.idle":"2023-09-19T13:36:47.782379Z","shell.execute_reply.started":"2023-09-19T13:36:40.284631Z","shell.execute_reply":"2023-09-19T13:36:47.781400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_loader.reset()\n\n# Make predictions\n\npredictions = model.predict(test_data_loader)\n\npredicted = np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-09-19T13:36:47.783778Z","iopub.execute_input":"2023-09-19T13:36:47.784279Z","iopub.status.idle":"2023-09-19T13:39:12.124078Z","shell.execute_reply.started":"2023-09-19T13:36:47.784244Z","shell.execute_reply":"2023-09-19T13:39:12.122972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\n# Run predictions and get class indices\nmodel_predictions = model.predict(test_data_loader)\nclass_indices = np.argmax(model_predictions, axis=1)\n\n# Display class mappings\nprint(type(training_data_loader.class_indices))\nprint(training_data_loader.class_indices)\nlabel_mappings = {value: key for (key, value) in training_data_loader.class_indices.items()}\nprint(label_mappings)\n\n# Initialize array for storing predicted labels\npredicted_labels_array = np.empty(shape=len(test_data_loader.filenames), dtype=np.dtype('U20'))\n\n# Fill the array with predicted labels\nfor idx, val in enumerate(class_indices):\n    predicted_labels_array[idx] = label_mappings[val]\n\n# Extract IDs from filenames\nfilename_ids = []\nfor fname in test_data_loader.filenames:\n    filename_ids.append(int(fname.split(sep=\"/\")[1].split(sep=\".\")[0]) - 1)\n\n# Create a final sorted array of predicted labels\nfinal_predicted_labels = np.empty(shape=len(test_data_loader.filenames), dtype=np.dtype('U20'))\nfinal_predicted_labels[filename_ids] = predicted_labels_array\n\n# Load a sample submission file (adjust path as needed)\nsample_submission = pd.read_csv('../input/cifar-10/sampleSubmission.csv', header='infer')\nsample_submission.info()\n\n# Add the final predicted labels to the submission DataFrame\nsample_submission['label'] = final_predicted_labels\n\n# Save the DataFrame to CSV\nsample_submission.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T14:46:41.762197Z","iopub.execute_input":"2023-09-19T14:46:41.762588Z","iopub.status.idle":"2023-09-19T14:49:01.916027Z","shell.execute_reply.started":"2023-09-19T14:46:41.762557Z","shell.execute_reply":"2023-09-19T14:49:01.914874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-09-19T14:13:56.662893Z","iopub.execute_input":"2023-09-19T14:13:56.663617Z","iopub.status.idle":"2023-09-19T14:13:56.683532Z","shell.execute_reply.started":"2023-09-19T14:13:56.663584Z","shell.execute_reply":"2023-09-19T14:13:56.682348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}