{"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":"# !7za x /kaggle/input/cifar-10/test.7z -o/kaggle/working/\n# !7za x /kaggle/input/cifar-10/train.7z -o/kaggle/working/\n# # took ages","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nimport os\n\n# Delete the test directory\nif os.path.exists(\"/kaggle/working/test/\"):\n    shutil.rmtree(\"/kaggle/working/test/\")\n\n# Delete the train directory\nif os.path.exists(\"/kaggle/working/train/\"):\n    shutil.rmtree(\"/kaggle/working/train/\")\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T19:13:48.084078Z","iopub.execute_input":"2023-09-18T19:13:48.084424Z","iopub.status.idle":"2023-09-18T19:13:53.154147Z","shell.execute_reply.started":"2023-09-18T19:13:48.084395Z","shell.execute_reply":"2023-09-18T19:13:53.149083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T22:26:05.473880Z","iopub.execute_input":"2023-09-18T22:26:05.474633Z","iopub.status.idle":"2023-09-18T22:26:18.764076Z","shell.execute_reply.started":"2023-09-18T22:26:05.474597Z","shell.execute_reply":"2023-09-18T22:26:18.760786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr\n\nwith py7zr.SevenZipFile('/kaggle/input/cifar-10/test.7z', mode='r') as z:\n    z.extractall(path=\"/kaggle/working/\")\n\nwith py7zr.SevenZipFile('/kaggle/input/cifar-10/train.7z', mode='r') as z:\n    z.extractall(path=\"/kaggle/working/\")\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T19:14:30.188191Z","iopub.execute_input":"2023-09-18T19:14:30.188570Z","iopub.status.idle":"2023-09-18T19:35:39.806394Z","shell.execute_reply.started":"2023-09-18T19:14:30.188536Z","shell.execute_reply":"2023-09-18T19:35:39.805244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install numpy==1.22.0\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T22:26:28.447903Z","iopub.execute_input":"2023-09-18T22:26:28.448917Z","iopub.status.idle":"2023-09-18T22:26:40.616486Z","shell.execute_reply.started":"2023-09-18T22:26:28.448881Z","shell.execute_reply":"2023-09-18T22:26:40.615235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\n# from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\n# why keras only\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom keras.optimizers import Adam\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T22:26:45.630983Z","iopub.execute_input":"2023-09-18T22:26:45.631929Z","iopub.status.idle":"2023-09-18T22:26:56.297540Z","shell.execute_reply.started":"2023-09-18T22:26:45.631894Z","shell.execute_reply":"2023-09-18T22:26:56.296495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"strategy = tf.distribute.MirroredStrategy()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T22:26:59.834434Z","iopub.execute_input":"2023-09-18T22:26:59.835142Z","iopub.status.idle":"2023-09-18T22:27:04.929844Z","shell.execute_reply.started":"2023-09-18T22:26:59.835099Z","shell.execute_reply":"2023-09-18T22:27:04.928802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number of devices: {}\".format(strategy.num_replicas_in_sync))","metadata":{"execution":{"iopub.status.busy":"2023-09-18T22:27:07.435292Z","iopub.execute_input":"2023-09-18T22:27:07.435748Z","iopub.status.idle":"2023-09-18T22:27:07.442155Z","shell.execute_reply.started":"2023-09-18T22:27:07.435712Z","shell.execute_reply":"2023-09-18T22:27:07.441278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = \"/kaggle/working/train/\"\ntest_dir = \"/kaggle/working/test/\"\n# df_train['id'] = df_train['id'].astype(str) + '.png'\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T22:27:11.880415Z","iopub.execute_input":"2023-09-18T22:27:11.880820Z","iopub.status.idle":"2023-09-18T22:27:11.885610Z","shell.execute_reply.started":"2023-09-18T22:27:11.880790Z","shell.execute_reply":"2023-09-18T22:27:11.884445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\n# Data Augmentation\ndatagen = ImageDataGenerator(\n    rescale=1./255,          # Rescale pixel values to [0, 1]\n    validation_split=0.2,    # Reserve some data for validation\n    horizontal_flip=True,    # Random horizontal flip\n    width_shift_range=0.1,   # Horizontal shift\n    height_shift_range=0.1,  # Vertical shift\n    rotation_range=20,       # Random rotation between -20 to 20 degrees\n    zoom_range=0.15,         # Random zoom in the range [0.85, 1.15]\n    shear_range=0.15         # Shear Intensity\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T22:27:15.513258Z","iopub.execute_input":"2023-09-18T22:27:15.514375Z","iopub.status.idle":"2023-09-18T22:27:15.521603Z","shell.execute_reply.started":"2023-09-18T22:27:15.514332Z","shell.execute_reply":"2023-09-18T22:27:15.520433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\n\n# Convert class labels from string to str format\n# df_train['label'] = df_train['label'].astype(str)\n# df_train['id'] = df_train['id'].astype(str) + '.png'\ndf_train['id'] = df_train['id'].astype(str) + '.png'\n\n\n# Load images using Keras's ImageDataGenerator\ndatagen = ImageDataGenerator(\n    rescale=1./255,\n    validation_split=0.2,\n    horizontal_flip=True,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)\n\ntrain_generator = datagen.flow_from_dataframe(\n    df_train,\n    directory=train_dir,\n    x_col='id',\n    y_col='label',\n    target_size=(32, 32),\n    class_mode='categorical',\n    batch_size=64,\n    subset='training'\n)\n\nvalidation_generator = datagen.flow_from_dataframe(\n    df_train,\n    directory=train_dir,\n    x_col='id',\n    y_col='label',\n    target_size=(32, 32),\n    class_mode='categorical',\n    batch_size=64,\n    subset='validation'\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T22:27:19.919540Z","iopub.execute_input":"2023-09-18T22:27:19.920377Z","iopub.status.idle":"2023-09-18T22:27:21.477971Z","shell.execute_reply.started":"2023-09-18T22:27:19.920337Z","shell.execute_reply":"2023-09-18T22:27:21.477012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ndel df_train\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-09-18T22:27:26.576026Z","iopub.execute_input":"2023-09-18T22:27:26.576743Z","iopub.status.idle":"2023-09-18T22:27:26.757021Z","shell.execute_reply.started":"2023-09-18T22:27:26.576710Z","shell.execute_reply":"2023-09-18T22:27:26.755936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = Sequential([\n#     Conv2D(32, (3,3), activation='relu', padding='same', input_shape=(32, 32, 3)),\n#     Conv2D(32, (3,3), activation='relu', padding='same'),\n#     MaxPooling2D(2,2),\n#     Dropout(0.2),\n    \n#     Conv2D(64, (3,3), activation='relu', padding='same'),\n#     Conv2D(64, (3,3), activation='relu', padding='same'),\n#     MaxPooling2D(2,2),\n#     Dropout(0.3),\n    \n#     Conv2D(128, (3,3), activation='relu', padding='same'),\n#     Conv2D(128, (3,3), activation='relu', padding='same'),\n#     MaxPooling2D(2,2),\n#     Dropout(0.4),\n\n#     Flatten(),\n#     Dense(512, activation='relu'),\n#     Dropout(0.5),\n#     Dense(10, activation='softmax')\n# ])\n# # Model Summary\n# model.summary()\n\n\nfrom keras.layers import BatchNormalization\nfrom keras.regularizers import l2\n\nmodel = Sequential([\n    # First block\n    Conv2D(32, (3,3), activation='relu', padding='same', input_shape=(32, 32, 3)),\n    BatchNormalization(),\n    Conv2D(32, (3,3), activation='relu', padding='same'),\n    BatchNormalization(),\n    MaxPooling2D(2,2),\n    Dropout(0.2),\n    \n    # Second block\n    Conv2D(64, (3,3), activation='relu', padding='same'),\n    BatchNormalization(),\n    Conv2D(64, (3,3), activation='relu', padding='same'),\n    BatchNormalization(),\n    MaxPooling2D(2,2),\n    Dropout(0.3),\n    \n    # Third block\n    Conv2D(128, (3,3), activation='relu', padding='same'),\n    BatchNormalization(),\n    Conv2D(128, (3,3), activation='relu', padding='same'),\n    BatchNormalization(),\n    MaxPooling2D(2,2),\n    Dropout(0.4),\n\n    # Fully connected layers\n    Flatten(),\n    Dense(512, activation='relu', kernel_regularizer=l2(0.01)),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])\n\n# Model Summary\nmodel.summary()\n\n#Custom learning rate\n\nINIT_LR = 0.003  # You can change this value\n\noptimizer = Adam(learning_rate=INIT_LR)\nmodel.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T22:27:36.600296Z","iopub.execute_input":"2023-09-18T22:27:36.600685Z","iopub.status.idle":"2023-09-18T22:27:36.948905Z","shell.execute_reply.started":"2023-09-18T22:27:36.600655Z","shell.execute_reply":"2023-09-18T22:27:36.948098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stop = EarlyStopping(patience=10, restore_best_weights=True)\nreduce_lr = ReduceLROnPlateau(factor=0.2, patience=3)\n\nhistory = model.fit(\n    train_generator,\n    validation_data=validation_generator,\n    epochs=50,\n    callbacks=[early_stop, reduce_lr]\n)\nmodel.save(\"/kaggle/working/cifar10_model03.h5\")\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T22:27:44.464082Z","iopub.execute_input":"2023-09-18T22:27:44.464469Z","iopub.status.idle":"2023-09-18T23:13:32.854382Z","shell.execute_reply.started":"2023-09-18T22:27:44.464439Z","shell.execute_reply":"2023-09-18T23:13:32.853287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Clear Memory\ndel train_generator, validation_generator\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-09-18T23:14:56.423701Z","iopub.execute_input":"2023-09-18T23:14:56.424820Z","iopub.status.idle":"2023-09-18T23:14:56.629751Z","shell.execute_reply.started":"2023-09-18T23:14:56.424783Z","shell.execute_reply":"2023-09-18T23:14:56.628584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights(\"/kaggle/working/best_weights.h5\")\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T22:08:52.223780Z","iopub.execute_input":"2023-09-18T22:08:52.224204Z","iopub.status.idle":"2023-09-18T22:08:52.451468Z","shell.execute_reply.started":"2023-09-18T22:08:52.224170Z","shell.execute_reply":"2023-09-18T22:08:52.449266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Predict on Test Data in Batches\n# test_files = os.listdir(test_dir)\n\n# batch_size = 128\n# predicted_classes = []\n\n# for i in range(0, len(test_files), batch_size):\n#     batch_files = test_files[i:i+batch_size]\n#     test_images = [load_img(test_dir + fname, target_size=(32,32)) for fname in batch_files]\n#     test_array = np.array([img_to_array(img) for img in test_images]) / 255.0\n\n#     batch_predictions = model.predict(test_array)\n#     batch_labels = np.argmax(batch_predictions, axis=1)\n#     predicted_classes.extend(batch_labels)\n\n#     # Freeing up memory\n#     del test_images, test_array, batch_predictions\n#     gc.collect()\n\n# predicted_classes = np.array(predicted_classes)\n\n# # Create CSV for Submission\n# submission_df = pd.DataFrame({\n#     'id': [fname.split('.')[0] for fname in test_files],\n#     'label': predicted_classes\n# })\n\n# submission_df.to_csv(\"/kaggle/working/submission.csv\", index=False)\n\n\n\n# # Predict on Test Data in Batches\n# test_files = os.listdir(test_dir)\n\n# batch_size = 512\n# predicted_classes = []\n\n# for i in range(0, len(test_files), batch_size):\n#     batch_files = test_files[i:i+batch_size]\n#     test_images = [load_img(test_dir + fname, target_size=(32,32)) for fname in batch_files]\n#     test_array = np.array([img_to_array(img) for img in test_images]) / 255.0\n\n#     batch_predictions = model.predict(test_array)\n#     batch_labels = np.argmax(batch_predictions, axis=1)\n#     predicted_classes.extend(batch_labels)\n\n#     # Freeing up memory\n#     del test_images, test_array, batch_predictions\n#     gc.collect()\n\n# predicted_classes = np.array(predicted_classes)\n\n# # Assuming these are the class names in the order you trained your model\n# class_names = [\"airplane\", \"automobile\", \"bird\", \"cat\", \"deer\", \"dog\", \"frog\", \"horse\", \"ship\", \"truck\"]\n\n# # Map the numerical labels to class names\n# predicted_class_names = [class_names[label] for label in predicted_classes]\n\n# # Create CSV for Submission\n# submission_df = pd.DataFrame({\n#     'id': [int(fname.split('.')[0]) for fname in test_files],  # Convert to integer to ensure proper format\n#     'label': predicted_class_names\n# })\n\n# # Sort the dataframe by 'id' column for cleaner output\n# submission_df = submission_df.sort_values(by='id')\n\n# submission_df.to_csv(\"/kaggle/working/submission01.csv\", index=False)\n\n\n###############################################################\n\n# Predict on Test Data in Batches\ntest_files = os.listdir(test_dir)\n\nbatch_size = 1024\npredicted_classes = []\n\nprint(f\"Total test files: {len(test_files)}\")\n\nfor i in range(0, len(test_files), batch_size):\n    print(f\"Processing images {i} to {i+batch_size}...\")\n    \n    batch_files = test_files[i:i+batch_size]\n    test_images = [load_img(test_dir + fname, target_size=(32,32)) for fname in batch_files]\n    test_array = np.array([img_to_array(img) for img in test_images]) / 255.0\n\n    batch_predictions = model.predict(test_array)\n    batch_labels = np.argmax(batch_predictions, axis=1)\n    predicted_classes.extend(batch_labels)\n\n    # Freeing up memory\n    del test_images, test_array, batch_predictions\n    gc.collect()\n\npredicted_classes = np.array(predicted_classes)\nprint(f\"Total predicted classes: {len(predicted_classes)}\")\n\n# rest of the code \n\n# Assuming these are the class names in the order you trained your model\nclass_names = [\"airplane\", \"automobile\", \"bird\", \"cat\", \"deer\", \"dog\", \"frog\", \"horse\", \"ship\", \"truck\"]\n\n# Map the numerical labels to class names\npredicted_class_names = [class_names[label] for label in predicted_classes]\n\n# Create CSV for Submission\nsubmission_df = pd.DataFrame({\n    'id': [int(fname.split('.')[0]) for fname in test_files],  # Convert to integer to ensure proper format\n    'label': predicted_class_names\n})\n\n# Sort the dataframe by 'id' column for cleaner output\nsubmission_df = submission_df.sort_values(by='id')\n\nsubmission_df.to_csv(\"/kaggle/working/submission02.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T00:07:27.424475Z","iopub.execute_input":"2023-09-19T00:07:27.424872Z","iopub.status.idle":"2023-09-19T00:10:39.025681Z","shell.execute_reply.started":"2023-09-19T00:07:27.424840Z","shell.execute_reply":"2023-09-19T00:10:39.024388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport random\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\n# from keras.preprocessing.image import load_img\n# from keras.preprocessing import image\n\n# Load submission file\nsubmission_df = pd.read_csv(\"/kaggle/working/submission01.csv\")\n\n# List all images in the test directory\nall_images = os.listdir(\"/kaggle/working/test\")\n# Randomly select an image name\nimage_name = random.choice(all_images)\n\n# Define path\nimage_path = os.path.join(\"/kaggle/working/test\", image_name)\n\n# Display the image\n# img = image.load_img(image_path)\nimg = load_img(image_path)\nplt.imshow(img)\nplt.title(image_name)\nplt.axis('off')\nplt.show()\n\n# Check prediction\n# image_id = os.path.splitext(image_name)[0]\nimage_id = int(os.path.splitext(image_name)[0])\nif image_id in submission_df['id'].values:\n    predicted_class_idx = submission_df[submission_df['id'] == image_id]['label'].values[0]\n    predicted_class_name = class_names[predicted_class_idx]  # assuming you have `class_names` list defined as before\n    print(f\"Predicted class for {image_name} is: {predicted_class_name}\")\nelse:\n    print(f\"Image {image_id} is not in the submission file.\")\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T00:11:19.520010Z","iopub.execute_input":"2023-09-19T00:11:19.521193Z","iopub.status.idle":"2023-09-19T00:11:20.031672Z","shell.execute_reply.started":"2023-09-19T00:11:19.521001Z","shell.execute_reply":"2023-09-19T00:11:20.030107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(test_files))  # This should print 10000\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T23:52:37.705861Z","iopub.execute_input":"2023-09-18T23:52:37.707240Z","iopub.status.idle":"2023-09-18T23:52:37.714827Z","shell.execute_reply.started":"2023-09-18T23:52:37.707182Z","shell.execute_reply":"2023-09-18T23:52:37.713446Z"},"trusted":true},"execution_count":null,"outputs":[]}]}