{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":21154,"databundleVersionId":1243559,"sourceType":"competition"}],"dockerImageVersionId":30733,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-27T17:17:00.895952Z","iopub.execute_input":"2024-06-27T17:17:00.89631Z","iopub.status.idle":"2024-06-27T17:17:01.04446Z","shell.execute_reply.started":"2024-06-27T17:17:00.89628Z","shell.execute_reply":"2024-06-27T17:17:01.043573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\ndef inspect_tfrecord(filenames, num_examples=5):\n    raw_dataset = tf.data.TFRecordDataset(filenames)\n    for raw_record in raw_dataset.take(num_examples):\n        example = tf.train.Example()\n        example.ParseFromString(raw_record.numpy())\n        print(example)\n\n# Inspect a few examples from the train dataset\ntrain_filenames = tf.io.gfile.glob('/kaggle/input/tpu-getting-started/tfrecords-jpeg-224x224train/*.tfrec')\ninspect_tfrecord(train_filenames, num_examples=5)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-27T17:39:06.548319Z","iopub.execute_input":"2024-06-27T17:39:06.549074Z","iopub.status.idle":"2024-06-27T17:39:06.587418Z","shell.execute_reply.started":"2024-06-27T17:39:06.549041Z","shell.execute_reply":"2024-06-27T17:39:06.5867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _parse_function(proto):\n    keys_to_features = {\n        'id': tf.io.FixedLenFeature([], tf.string),\n        'label': tf.io.FixedLenFeature([], tf.int64),\n        'img': tf.io.FixedLenFeature([], tf.string)  # Adjust if the feature name is different\n    }\n    parsed_features = tf.io.parse_single_example(proto, keys_to_features)\n    \n    if 'img' not in parsed_features:\n        raise ValueError(\"The feature 'img' is not present in the TFRecord file.\")\n    \n    image = tf.image.decode_jpeg(parsed_features['img'])\n    image = tf.image.resize(image, [224, 224])\n    image /= 255.0\n    return image, parsed_features['label']\n","metadata":{"execution":{"iopub.status.busy":"2024-06-27T17:39:22.043768Z","iopub.execute_input":"2024-06-27T17:39:22.044445Z","iopub.status.idle":"2024-06-27T17:39:22.051306Z","shell.execute_reply.started":"2024-06-27T17:39:22.044403Z","shell.execute_reply":"2024-06-27T17:39:22.050359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load datasets\ntrain_dataset = load_dataset(train_filenames)\nval_dataset = load_dataset(val_filenames)\ntest_dataset = load_dataset(test_filenames)\n\n# Data batching and prefetching\ntrain_dataset = train_dataset.shuffle(1000).batch(32).prefetch(tf.data.AUTOTUNE)\nval_dataset = val_dataset.batch(32).prefetch(tf.data.AUTOTUNE)\ntest_dataset = test_dataset.batch(32).prefetch(tf.data.AUTOTUNE)\n\n# Customized CNN architecture\ndef build_model(input_shape, num_classes):\n    model = Sequential([\n        Conv2D(32, (3, 3), activation='relu', input_shape=input_shape),\n        MaxPooling2D((2, 2)),\n        Conv2D(64, (3, 3), activation='relu'),\n        MaxPooling2D((2, 2)),\n        Conv2D(128, (3, 3), activation='relu'),\n        MaxPooling2D((2, 2)),\n        GlobalAveragePooling2D(),\n        Dense(128, activation='relu'),\n        Dropout(0.5),\n        Dense(num_classes, activation='softmax')\n    ])\n    model.summary()\n    return model\n\n# Build and compile the model\ninput_shape = (224, 224, 3)\nnum_classes = 104\nmodel = build_model(input_shape, num_classes)\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model\nhistory = model.fit(train_dataset, validation_data=val_dataset, epochs=10)\n\n# Evaluate the model\nval_loss, val_accuracy = model.evaluate(val_dataset)\nprint(f'Validation Accuracy: {val_accuracy:.4f}')\n\n# Confusion matrix\ndef plot_confusion_matrix(true_labels, predictions, class_names):\n    cm = confusion_matrix(true_labels, predictions)\n    plt.figure(figsize=(10, 8))\n    sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=class_names, yticklabels=class_names)\n    plt.xlabel('Predicted')\n    plt.ylabel('True')\n    plt.show()\n\n# Get validation labels and predictions\nval_labels = []\nval_predictions = []\n\nfor images, labels in val_dataset:\n    preds = model.predict(images)\n    val_labels.extend(labels.numpy())\n    val_predictions.extend(np.argmax(preds, axis=1))\n\nclass_names = [str(i) for i in range(num_classes)]\nplot_confusion_matrix(val_labels, val_predictions, class_names)\n\n# Generate predictions for test data\ntest_ids = []\ntest_predictions = []\n\nfor batch in test_dataset:\n    images = batch[0]\n    preds = model.predict(images)\n    test_ids.extend(batch[1].numpy())\n    test_predictions.extend(np.argmax(preds, axis=1))\n\n# Prepare submission file\nsubmission = pd.DataFrame({\n    'id': test_ids,\n    'label': test_predictions\n})\nsubmission.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-27T17:39:59.510325Z","iopub.execute_input":"2024-06-27T17:39:59.51121Z","iopub.status.idle":"2024-06-27T17:40:01.858658Z","shell.execute_reply.started":"2024-06-27T17:39:59.511178Z","shell.execute_reply":"2024-06-27T17:40:01.857323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}