{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30733,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install py7zr\n\nimport py7zr\nimport os","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define file paths and directories\nfile1_path = '/kaggle/input/cifar-10/train.7z'\nfile2_path = '/kaggle/input/cifar-10/test.7z'\noutput_dir1 = '/kaggle/working/'\noutput_dir2 = '/kaggle/working/'\n\n# Create directories if they don't exist\nos.makedirs(output_dir1, exist_ok=True)\nos.makedirs(output_dir2, exist_ok=True)\n\n# Function to extract .7z file to a specified directory\ndef extract_7z(file_path, output_dir):\n    with py7zr.SevenZipFile(file_path, mode='r') as z:\n        z.extractall(path=output_dir)\n\n# Extract files\nextract_7z(file1_path, output_dir1)\nextract_7z(file2_path, output_dir2)\n\nprint(f'Files extracted to {output_dir1} and {output_dir2}')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.layers import Flatten, Dense, Dropout, Conv2D, MaxPooling2D, BatchNormalization\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard, ReduceLROnPlateau\nfrom PIL import Image\nimport datetime\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.image import resize\nfrom tensorflow.keras.applications import Xception\nfrom sklearn.metrics import f1_score, accuracy_score, precision_score, recall_score, confusion_matrix\nfrom tensorflow.keras import layers, models","metadata":{"execution":{"iopub.status.busy":"2024-06-30T13:02:28.267860Z","iopub.execute_input":"2024-06-30T13:02:28.268321Z","iopub.status.idle":"2024-06-30T13:02:40.522403Z","shell.execute_reply.started":"2024-06-30T13:02:28.268275Z","shell.execute_reply":"2024-06-30T13:02:40.521282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model1","metadata":{}},{"cell_type":"code","source":"input_shape = (32, 32, 3)\ntarget_size = (32, 32)\n\n# Function to load images from a folder and resize them\ndef load_images_from_folder(folder, labels_file=None, target_size=target_size):\n    images = []\n    labels = []\n    if labels_file:\n        labels_df = pd.read_csv(labels_file)\n        label_dict = dict(zip(labels_df['id'], labels_df['label']))\n    for filename in os.listdir(folder):\n        img = Image.open(os.path.join(folder, filename)).resize(target_size)\n        if img is not None:\n            images.append(np.array(img))\n            if labels_file:\n                img_id = int(filename.split('.')[0])\n                labels.append(label_dict[img_id])\n    return np.array(images), np.array(labels)\n\n# Load training data\ntrain_folder = '/kaggle/working/train'\ntrain_labels_file = '/kaggle/input/cifar-10/trainLabels.csv'\nX_train, y_train = load_images_from_folder(train_folder, train_labels_file, target_size=target_size)\n\n# Load test data\ntest_folder = '/kaggle/working/test'\nX_test, _ = load_images_from_folder(test_folder, target_size=target_size)\n\n# Normalize pixel values to be between 0 and 1\nX_train = X_train.astype('float32') / 255.0\nX_test = X_test.astype('float32') / 255.0\n\nlabel_encoder = LabelEncoder()\ny_train_encoded = label_encoder.fit_transform(y_train)\n\n# Encode labels to categorical (one-hot encoding)\nnum_of_classes = len(np.unique(y_train_encoded))\ny_train = to_categorical(y_train_encoded, num_of_classes)\n\n# Split a part of training data as validation data\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)\n\n# Define 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\nmodel1 = Sequential()\n\n# Convolutional layers\nmodel1.add(Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=X_train.shape[1:]))\nmodel1.add(BatchNormalization())\nmodel1.add(Conv2D(64, (3, 3), activation='relu', padding='same'))\nmodel1.add(BatchNormalization())\nmodel1.add(MaxPooling2D(pool_size=(2, 2)))\nmodel1.add(Dropout(0.25))\n\nmodel1.add(Conv2D(128, (3, 3), activation='relu', padding='same'))\nmodel1.add(BatchNormalization())\nmodel1.add(Conv2D(128, (3, 3), activation='relu', padding='same'))\nmodel1.add(BatchNormalization())\nmodel1.add(MaxPooling2D(pool_size=(2, 2)))\nmodel1.add(Dropout(0.25))\n\nmodel1.add(Conv2D(256, (3, 3), activation='relu', padding='same'))\nmodel1.add(BatchNormalization())\nmodel1.add(Conv2D(256, (3, 3), activation='relu', padding='same'))\nmodel1.add(BatchNormalization())\nmodel1.add(MaxPooling2D(pool_size=(2, 2)))\nmodel1.add(Dropout(0.25))\n\nmodel1.add(Flatten())\nmodel1.add(Dense(1024, activation='relu'))\nmodel1.add(Dense(num_of_classes, activation='softmax'))\nmodel1.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Print model summary\nmodel1.summary()\n\n# Fit data augmentation on training data\ndatagen.fit(X_train)\n\ncheckpoint_path = \"/kaggle/working/chkpt/chkpt1.keras\"\ncheckpoint_callback = ModelCheckpoint(filepath=checkpoint_path, save_best_only=True, verbose=1)\nearly_stopping_callback = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True, verbose=1)\nreduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, min_lr=0.0001)\nlog_dir = \"/kaggle/working/chkpt/logs/\" + datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\")\ntensorboard_callback = TensorBoard(log_dir=log_dir, histogram_freq=1)\n\ncallbacks = [checkpoint_callback, early_stopping_callback, tensorboard_callback, reduce_lr]\n\n# Train the model with data augmentation\nbatch_size = 128\nepochs = 30","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model1.fit(datagen.flow(X_train, y_train, batch_size=batch_size),\n                    epochs=epochs,\n                    validation_data=(X_val, y_val),\n                    shuffle=True,\n                    callbacks=callbacks)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the training and validation accuracy and loss\nplt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='train_accuracy')\nplt.plot(history.history['val_accuracy'], label='val_accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(history.history['loss'], label='train_loss')\nplt.plot(history.history['val_loss'], label='val_loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.title('Training and Validation Loss')\n\nplt.show()\n\n# Load the best model\nbest_model = tf.keras.models.load_model('/kaggle/working/chkpt/chkpt1.keras')\n\n# Load the label names from the original dataset\nlabels_df = pd.read_csv(train_labels_file)\nclass_names = labels_df['label'].unique()\n\n# Make predictions on the test data\npredictions = best_model.predict(X_test)\n\n# Convert predictions to class labels\npredicted_classes = np.argmax(predictions, axis=1)\npredicted_labels = [class_names[class_index] for class_index in predicted_classes]\n\n# Prepare submission file\nsubmission_df = pd.DataFrame({\n    'id': range(1, len(predicted_labels) + 1),\n    'label': predicted_labels\n})\n\n# Save to CSV\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model2","metadata":{}},{"cell_type":"code","source":"# Load CIFAR-10 data\ntrain_labels = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\nnum_classes = 10\n\n# Prepare labels\nlabel_map = {'airplane': 0, 'automobile': 1, 'bird': 2, 'cat': 3, 'deer': 4,\n             'dog': 5, 'frog': 6, 'horse': 7, 'ship': 8, 'truck': 9}\ntrain_labels['label'] = train_labels['label'].map(label_map)\nlabels = np.array(train_labels['label'])\n\n# Define paths to train and test directories\ntrain_dir = '/kaggle/working/train'\ntest_dir = '/kaggle/working/test'\n\n# Function to load images from directory\ndef load_images_from_directory(directory):\n    from tensorflow.keras.preprocessing.image import load_img, img_to_array\n    images = []\n    file_names = sorted(os.listdir(directory))\n    for file_name in file_names:\n        if file_name.endswith('.png'):\n            img_path = os.path.join(directory, file_name)\n            img = load_img(img_path, target_size=(299, 299))\n            img_array = img_to_array(img)\n            images.append(img_array)\n    return np.array(images)\n\n# Load images and labels for training and validation\nx_train = load_images_from_directory(train_dir)\nx_test = load_images_from_directory(test_dir)\n\n# Split data into training and validation sets\nx_train, x_val, y_train, y_val = train_test_split(\n    x_train, labels, test_size=0.2, random_state=42)\n\n# Normalize pixel values to between 0 and 1\nx_train = x_train.astype('float32') / 255.0\nx_val = x_val.astype('float32') / 255.0\nx_test = x_test.astype('float32') / 255.0","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Build Xception model\nbase_model = Xception(weights='imagenet', include_top=False, input_shape=(299, 299, 3))\nfor layer in base_model.layers:\n    layer.trainable = False\n\nx = base_model.output\nx = layers.GlobalAveragePooling2D()(x)\nx = layers.Dense(1024, activation='relu')(x)\npredictions = layers.Dense(num_classes, activation='softmax')(x)\n\nmodel2 = models.Model(inputs=base_model.input, outputs=predictions)\n\n# Compile the model\nmodel2.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\nmodel2.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Callbacks\ncheckpoint_path = \"/kaggle/working/chkpt/chkpt.keras\"\ncheckpoint_callback = ModelCheckpoint(filepath=checkpoint_path, save_best_only=True, verbose=1)\nearly_stopping_callback = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True, verbose=1)\nreduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, min_lr=0.0001)\nlog_dir = \"/kaggle/working/chkpt/logs/\" + datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\")\ntensorboard_callback = TensorBoard(log_dir=log_dir, histogram_freq=1)\n\ncallbacks = [checkpoint_callback, early_stopping_callback, tensorboard_callback, reduce_lr]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhistory = model2.fit(x_train, y_train, epochs=30, batch_size=128,\n                    validation_data=(x_val, y_val), callbacks=callbacks, shuffle=True)\n\n# Evaluate the model\nscore = model2.evaluate(x_val, y_val, verbose=0)\nprint('Validation accuracy:', score[1])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the training and validation accuracy and loss\nplt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='train_accuracy')\nplt.plot(history.history['val_accuracy'], label='val_accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(history.history['loss'], label='train_loss')\nplt.plot(history.history['val_loss'], label='val_loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.title('Training and Validation Loss')\n\nplt.tight_layout()\nplt.show()\n\n# Make predictions on the test data\npredictions = model2.predict(x_test)\n\n# Convert predictions to class labels\npredicted_classes = np.argmax(predictions, axis=1)\nclass_names = train_labels['label'].unique()\npredicted_labels = [class_names[class_index] for class_index in predicted_classes]\n\n# Prepare submission file\nsubmission_df = pd.DataFrame({\n    'id': range(1, len(predicted_labels) + 1),\n    'label': predicted_labels\n})\n\n# Save to CSV\nsubmission_df.to_csv('submission.csv', index=False)\nprint('File saved successfully.')","metadata":{},"execution_count":null,"outputs":[]}]}