{"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\nimport time\nfrom PIL import Image\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential, load_model\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-19T07:25:06.979068Z","iopub.execute_input":"2023-09-19T07:25:06.979429Z","iopub.status.idle":"2023-09-19T07:25:36.488545Z","shell.execute_reply.started":"2023-09-19T07:25:06.979399Z","shell.execute_reply":"2023-09-19T07:25:36.487561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\n!pip install py7zr\nimport py7zr\n\n# Define the paths to the input .7z files and the directory where you want to extract them\ninput_dir = '/kaggle/input/cifar-10'\noutput_dir = '/kaggle/temp/cifar-10-extracted'  # You can adjust this path\n\n# Create the output directory if it doesn't exist\nos.makedirs(output_dir, exist_ok=True)\n# Extract the test and train data\n# with py7zr.SevenZipFile(os.path.join(input_dir, 'test.7z'), mode='r') as z:\n#     z.extractall(path=output_dir)\n\n\nprint(\"\\nExtraction of Train_Data started\")\nwith py7zr.SevenZipFile(os.path.join(input_dir, 'train.7z'), mode='r') as z:\n    z.extractall(path=output_dir)\nprint(\"Train_Data Extraction completed successfully.\")\n\nprint(\"Extraction of Test_Data started\")\nwith py7zr.SevenZipFile(os.path.join(input_dir, 'test.7z'), mode='r') as z:\n    z.extractall(path=output_dir)\nprint(\"Test_Data Extraction completed successfully.\")","metadata":{"execution":{"iopub.status.busy":"2023-09-19T07:27:58.599366Z","iopub.execute_input":"2023-09-19T07:27:58.600702Z","iopub.status.idle":"2023-09-19T07:50:45.304549Z","shell.execute_reply.started":"2023-09-19T07:27:58.600657Z","shell.execute_reply":"2023-09-19T07:50:45.303624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\n# Define paths\nTRAIN_DIR = \"/kaggle/temp/cifar-10-extracted/train\"\nTRAIN_LABELS_CSV = \"/kaggle/input/cifar-10/trainLabels.csv\"\n\n\n# Load train labels from CSV\ntrain_labels_df = pd.read_csv(TRAIN_LABELS_CSV)\nlabel_mapping = dict(zip(train_labels_df['id'].values, train_labels_df['label'].values))\nlabel_to_index = {label: index for index, label in enumerate(train_labels_df['label'].unique())}\n\n# Explicitly specify the index for each label\nlabel_to_index = {\n    \"airplane\": 0,\n    \"automobile\": 1,\n    \"bird\": 2,\n    \"cat\": 3,\n    \"deer\": 4,\n    \"dog\": 5,\n    \"frog\": 6,\n    \"horse\": 7,\n    \"ship\": 8,\n    \"truck\": 9\n}\n\n# Print the label categories\nprint(\"Label categories:\", list(label_to_index.keys()))\n\ndef load_images_from_directory(directory, label_provided=True):\n    images_list = []\n    labels_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            if label_provided:\n                image_id = int(filename.split('.')[0])\n                labels_list.append(label_to_index[label_mapping[image_id]])\n    images = np.array(images_list)\n    labels = np.array(labels_list) if label_provided else None\n    return images, labels\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T07:54:04.032791Z","iopub.execute_input":"2023-09-19T07:54:04.033870Z","iopub.status.idle":"2023-09-19T07:54:04.075879Z","shell.execute_reply.started":"2023-09-19T07:54:04.033826Z","shell.execute_reply":"2023-09-19T07:54:04.075055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\n# Load the data\nTrain_Data, Train_Label = load_images_from_directory(TRAIN_DIR)\nprint('Shape of Test_Data',Train_Data.shape)\n\n# Normalize the data\nx_train = Train_Data.astype('float32') / 255.0\ny_train = tf.keras.utils.to_categorical(Train_Label, 10)\n\n#Two GPU Function\n# Create a MirroredStrategy.\nstrategy = tf.distribute.MirroredStrategy()\n\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(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),\n        MaxPooling2D(2, 2),\n        Conv2D(64, (3, 3), activation='relu'),\n        MaxPooling2D(2, 2),\n        Conv2D(128, (3, 3), activation='relu'),\n        Flatten(),\n        Dense(512, activation='relu'),\n        Dense(10, activation='softmax')\n    ])\n    \n    # Compile the model\n    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model\nhistory = model.fit(x_train, y_train, epochs=20, batch_size=128, validation_split=0.2)\n\n# To evaluate on validation data you can 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\")","metadata":{"execution":{"iopub.status.busy":"2023-09-19T07:54:26.977185Z","iopub.execute_input":"2023-09-19T07:54:26.978064Z","iopub.status.idle":"2023-09-19T07:57:13.845547Z","shell.execute_reply.started":"2023-09-19T07:54:26.978029Z","shell.execute_reply":"2023-09-19T07:57:13.844491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\n# Uncomment and adjust the line below if you want to specify a particular accuracy value:\n# Accuracy = 69\n\n# Load the model\nloaded_model = load_model(f\"cifar10_model_0.{Accuracy}.h5\")\n\nTEST_DIR = \"/kaggle/temp/cifar-10-extracted/test\"\n\n# Create a dataframe with filepaths\nfilepaths = [os.path.join(TEST_DIR, fname) for fname in os.listdir(TEST_DIR) if fname.endswith('.png')]\ndf_test = pd.DataFrame({'filename': filepaths})\n\n# Use ImageDataGenerator with flow_from_dataframe\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntest_gen = test_datagen.flow_from_dataframe(dataframe=df_test, \n                                            x_col=\"filename\", \n                                            y_col=None, \n                                            target_size=(32, 32), \n                                            batch_size=64, \n                                            class_mode=None, \n                                            shuffle=False)\n\npredictions_vecs = loaded_model.predict(test_gen)\n\npredictions_final = np.argmax(predictions_vecs, axis=1)\n\nclass_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']\n\npredicted_classes = [class_names[i] for i in predictions_final]\n\n# Extract file IDs\nids = [int(fname.split(sep=\"/\")[-1].split(sep=\".\")[0]) for fname in test_gen.filenames]\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)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T07:58:09.407829Z","iopub.execute_input":"2023-09-19T07:58:09.408293Z","iopub.status.idle":"2023-09-19T08:00:32.918173Z","shell.execute_reply.started":"2023-09-19T07:58:09.408256Z","shell.execute_reply":"2023-09-19T08:00:32.917044Z"},"trusted":true},"execution_count":null,"outputs":[]}]}