{"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":"import tensorflow as tf\n\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()  # TPU detection\n    print('Running on TPU')\nexcept ValueError:\n    tpu = None\n    print('Running on GPU or CPU')\n\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nelse:\n    strategy = tf.distribute.get_strategy() \n\nprint(\"REPLICAS: \", strategy.num_replicas_in_sync)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\n","metadata":{"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-19T04:47:11.943125Z","iopub.execute_input":"2023-09-19T04:47:11.944215Z","iopub.status.idle":"2023-09-19T05:09:23.719914Z","shell.execute_reply.started":"2023-09-19T04:47:11.944167Z","shell.execute_reply":"2023-09-19T05:09:23.718678Z"},"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-19T05:10:26.387239Z","iopub.execute_input":"2023-09-19T05:10:26.388170Z","iopub.status.idle":"2023-09-19T05:10:26.394595Z","shell.execute_reply.started":"2023-09-19T05:10:26.388134Z","shell.execute_reply":"2023-09-19T05:10:26.393608Z"},"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-19T05:31:38.936820Z","iopub.execute_input":"2023-09-19T05:31:38.937291Z","iopub.status.idle":"2023-09-19T05:31:38.942723Z","shell.execute_reply.started":"2023-09-19T05:31:38.937253Z","shell.execute_reply":"2023-09-19T05:31:38.941620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # 1. DATA AUGMENTATION FUNCTION\n# def data_augmentation(image, label):\n#     image = tf.image.random_flip_left_right(image)  # Random horizontal flip\n#     image = tf.image.random_flip_up_down(image)  # Random vertical flip\n#     image = tf.image.random_brightness(image, max_delta=0.1)  # Random brightness\n#     image = tf.image.random_saturation(image, lower=0.9, upper=1.1)  # Random saturation\n#     image = tf.image.random_hue(image, max_delta=0.1)  # Random hue\n#     image = tf.image.random_contrast(image, lower=0.9, upper=1.1)  # Random contrast\n#     image = tf.image.rot90(image, k=tf.random.uniform(shape=[], minval=0, maxval=4, dtype=tf.int32))  # Random rotation\n#     return image, label\n\nfrom 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-19T05:31:42.194927Z","iopub.execute_input":"2023-09-19T05:31:42.195415Z","iopub.status.idle":"2023-09-19T05:31:42.202599Z","shell.execute_reply.started":"2023-09-19T05:31:42.195376Z","shell.execute_reply":"2023-09-19T05:31:42.201588Z"},"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\n# datagen = 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=256,\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=256,\n    subset='validation'\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T05:31:45.893734Z","iopub.execute_input":"2023-09-19T05:31:45.894601Z","iopub.status.idle":"2023-09-19T05:31:47.230594Z","shell.execute_reply.started":"2023-09-19T05:31:45.894553Z","shell.execute_reply":"2023-09-19T05:31:47.229151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ndel df_train\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-09-19T05:31:51.218864Z","iopub.execute_input":"2023-09-19T05:31:51.219329Z","iopub.status.idle":"2023-09-19T05:31:51.493072Z","shell.execute_reply.started":"2023-09-19T05:31:51.219280Z","shell.execute_reply":"2023-09-19T05:31:51.491890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\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.001  # 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-19T05:32:20.672731Z","iopub.execute_input":"2023-09-19T05:32:20.673180Z","iopub.status.idle":"2023-09-19T05:32:20.933361Z","shell.execute_reply.started":"2023-09-19T05:32:20.673146Z","shell.execute_reply":"2023-09-19T05:32:20.932217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])\n# tf.config.experimental_connect_to_cluster(resolver)\n# tf.tpu.experimental.initialize_tpu_system(resolver)\n# strategy = tf.distribute.experimental.TPUStrategy(resolver)\nimport os\nimport tensorflow as tf\n\ntry:\n    # TPU detection. No parameters necessary if TPU_NAME environment variable is set. On Kaggle this is always the case.\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print('Running on TPU ', tpu.master())\nexcept ValueError:\n    tpu = None\n\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n#     strategy = tf.distribute.experimental.TPUStrategy(tpu)\n    strategy = tf.distribute.TPUStrategy(tpu)\n\nelse:\n    # Default distribution strategy in Tensorflow. Works on CPU and single GPU.\n    strategy = tf.distribute.get_strategy()\n\nprint(\"REPLICAS: \", strategy.num_replicas_in_sync)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T05:34:44.573283Z","iopub.execute_input":"2023-09-19T05:34:44.573786Z","iopub.status.idle":"2023-09-19T05:34:48.686777Z","shell.execute_reply.started":"2023-09-19T05:34:44.573750Z","shell.execute_reply":"2023-09-19T05:34:48.685674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"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_model0tpu.h5\")\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T05:35:00.958608Z","iopub.execute_input":"2023-09-19T05:35:00.959059Z","iopub.status.idle":"2023-09-19T06:27:56.831852Z","shell.execute_reply.started":"2023-09-19T05:35:00.959026Z","shell.execute_reply":"2023-09-19T06:27:56.830589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 7. CLEAR MEMORY (OPTIONAL)\n# tf.keras.backend.clear_session()\n\n# Clear Memory\ndel train_generator, validation_generator\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-09-19T06:30:38.091672Z","iopub.execute_input":"2023-09-19T06:30:38.092117Z","iopub.status.idle":"2023-09-19T06:30:38.333125Z","shell.execute_reply.started":"2023-09-19T06:30:38.092067Z","shell.execute_reply":"2023-09-19T06:30:38.332132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict on Test Data in Batches\ntest_files = os.listdir(test_dir)\n\n# batch_size = 1024\nbatch_size = 4096\n\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/submissiontpu1.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}