{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":6799,"databundleVersionId":4225553,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport shutil\nfrom tqdm import tqdm\nimport random\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-22T03:02:15.356843Z","iopub.execute_input":"2024-02-22T03:02:15.357301Z","iopub.status.idle":"2024-02-22T03:02:15.371647Z","shell.execute_reply.started":"2024-02-22T03:02:15.357266Z","shell.execute_reply":"2024-02-22T03:02:15.370826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train\"\ntest_path = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train\"\nvalidation_path = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/val\"","metadata":{"execution":{"iopub.status.busy":"2024-02-24T19:13:54.220354Z","iopub.execute_input":"2024-02-24T19:13:54.220687Z","iopub.status.idle":"2024-02-24T19:13:54.225385Z","shell.execute_reply.started":"2024-02-24T19:13:54.220662Z","shell.execute_reply":"2024-02-24T19:13:54.224261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# import os\n# import shutil\n# import numpy as np\n# from tqdm import tqdm\n# from sklearn.model_selection import StratifiedKFold\n\n# ex_per_class = 50  # Increase the number of selected images per class\n# output_folder = \"/kaggle/working/imagenet_1pct\"\n\n# # Create output folder if it doesn't exist\n# os.makedirs(output_folder, exist_ok=True)\n\n# # Lists to store file paths and corresponding labels\n# all_files = []\n# all_labels = []\n\n# # Populate the lists with file paths and labels\n# for label, folder in enumerate(tqdm(os.listdir(train_path))):\n#     folder_path = os.path.join(train_path, folder)\n#     folder_files = os.listdir(folder_path)\n    \n#     all_files.extend([os.path.join(folder_path, filename) for filename in folder_files])\n#     all_labels.extend([label] * len(folder_files))\n\n# # Use StratifiedKFold to get stratified samples\n# skf = StratifiedKFold(n_splits=ex_per_class, shuffle=True, random_state=42)\n\n# for _, test_index in skf.split(all_files, all_labels):\n#     selected_files = np.array(all_files)[test_index]\n\n#     # Copy selected files to output folder\n#     for file_path in selected_files:\n#         shutil.copy2(file_path, output_folder)\n\n# # Create a zip archive\n# shutil.make_archive(\"imagenet_subset\", 'zip', output_folder)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-30T00:01:12.188917Z","iopub.execute_input":"2024-01-30T00:01:12.189474Z","iopub.status.idle":"2024-01-30T00:02:57.679659Z","shell.execute_reply.started":"2024-01-30T00:01:12.189433Z","shell.execute_reply":"2024-01-30T00:02:57.677979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import shutil\n# import numpy as np\n# from tqdm import tqdm\n\n# ex_per_class = 10\n# val_path = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/val\"\n# output_folder = \"/kaggle/working/imagenet_1pct_val\"\n\n# for folder in tqdm(os.listdir(val_path)):\n#     folder_path = os.path.join(val_path, folder)\n\n#     # Check if it's a directory\n#     if os.path.isdir(folder_path):\n#         os.makedirs(os.path.join(output_folder, folder))\n#         folder_files = os.listdir(folder_path)\n        \n#         # Select random images\n#         selected_files = np.random.choice(folder_files, ex_per_class, replace=False)\n        \n#         # Copy selected files to output folder\n#         for filename in selected_files:\n#             input_filepath = os.path.join(folder_path, filename)\n#             output_filepath = os.path.join(output_folder, folder, filename)\n#             shutil.copy2(input_filepath, output_filepath)\n\n# # Create a zip archive of the subset\n# shutil.make_archive(output_folder, 'zip', output_folder)\n\n# data augmentation \n\n#ttruth table inferance ","metadata":{"execution":{"iopub.status.busy":"2024-01-02T01:56:52.325609Z","iopub.execute_input":"2024-01-02T01:56:52.326009Z","iopub.status.idle":"2024-01-02T01:58:46.644447Z","shell.execute_reply.started":"2024-01-02T01:56:52.325977Z","shell.execute_reply":"2024-01-02T01:58:46.643155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\n# Check if GPU is available\nprint(\"Num GPUs Available: \", len(tf.config.experimental.list_physical_devices('GPU')))\n\n# If GPU is available, set TensorFlow to use GPU\nif tf.test.gpu_device_name():\n    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))\nelse:\n    print(\"GPU not found\")","metadata":{"execution":{"iopub.status.busy":"2024-02-22T03:03:01.529814Z","iopub.execute_input":"2024-02-22T03:03:01.530474Z","iopub.status.idle":"2024-02-22T03:03:01.541687Z","shell.execute_reply.started":"2024-02-22T03:03:01.530424Z","shell.execute_reply":"2024-02-22T03:03:01.540815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import models, transforms, datasets\nfrom tqdm import tqdm\nimport os\nfrom sklearn.metrics import precision_score, recall_score, f1_score\ntrain_path = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train\"\n\ntransform = transforms.Compose([\n#     transforms.RandomResizedCrop(224, scale=(0.9, 1.0)),  # Adjusted scale\n#     transforms.RandomHorizontalFlip(),\n#     transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.2),  # Color Jitter\n#     transforms.RandomRotation(degrees=15),  # Random Rotation\n#     transforms.RandomAffine(degrees=0, translate=(0.1, 0.1), shear=0.1),  # Random Affine Transformation\n#     transforms.RandomVerticalFlip(),  # Random Vertical Flip\n    transforms.ToTensor(),\n    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),\n])\n\n# Create a dataset and dataloader for the ImageNet subset\ndataset = datasets.ImageFolder(train_path, transform=transform)\ndataloader = DataLoader(dataset, batch_size= 64, shuffle=True, num_workers=2)\n\n# Load pre-trained ResNet-18 model\nresnet18 = models.resnet18(pretrained=True)\n\n# # Modify the final fully connected layer for your specific classification task\n# num_classes = len(os.listdir(train_path))\n# resnet18.fc = nn.Linear(resnet18.fc.in_features, num_classes)\n\n\n# Define loss function and optimizer\ncriterion = nn.CrossEntropyLoss()\n#optimizer = optim.Adam(model.parameters(), lr=0.001)\n\noptimizer = optim.SGD(resnet18.parameters(), lr=0.085, momentum=0.95, weight_decay= 1e-4)\n\n# Move the model to GPU if available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nresnet18.to(device)\n\n\n# # Train the model\n# num_epochs = 25\n# for epoch in range(num_epochs):\n#     running_loss = 0.0\n#     correct_predictions = 0\n#     total_samples = 0\n#     all_predictions = []\n#     all_labels = []\n    \n    resnet18.eval()\n    \n    for inputs, labels in tqdm(dataloader, desc=f\"Epoch {epoch+1}/{num_epochs}\", position=0, leave=True):\n        inputs, labels = inputs.to(device), labels.to(device)\n\n        optimizer = optim.AdamW(resnet18.parameters())\n\n        outputs = resnet18(inputs)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        running_loss += loss.item()\n\n        _, predicted = torch.max(outputs, 1)\n        correct_predictions += (predicted == labels).sum().item()\n        total_samples += labels.size(0)\n\n        all_predictions.extend(predicted.cpu().numpy())\n        all_labels.extend(labels.cpu().numpy())\n\n    accuracy = correct_predictions / total_samples\n    epoch_loss = running_loss / len(dataloader)\n    \n#resnet18.eval()\n    \n    # Calculate precision, recall, and F1 score\n    precision = precision_score(all_labels, all_predictions, average='weighted')\n    recall = recall_score(all_labels, all_predictions, average='weighted')\n    f1 = f1_score(all_labels, all_predictions, average='weighted')\n\n    print(f\"Epoch {epoch+1}/{num_epochs}, Loss: {epoch_loss}, Accuracy: {accuracy * 100:.2f}%\")\n    print(f\"Precision: {precision:.4f}, Recall: {recall:.4f}, F1 Score: {f1:.4f}\")\n\n# Save the trained model\ntorch.save(resnet18.state_dict(), \"/kaggle/working/resnet18_imagenet_subset.pth\")\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import models, transforms, datasets\nfrom tqdm import tqdm\nimport os\nfrom sklearn.metrics import precision_score, recall_score, f1_score\ntrain_path = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train\"\n\ntransform = transforms.Compose([\n    transforms.Resize((224, 224)),  \n    transforms.ToTensor(),\n    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),\n])\n\n\n# Create a dataset and dataloader for the ImageNet subset\ndataset = datasets.ImageFolder(train_path, transform=transform)\ndataloader = DataLoader(dataset, batch_size= 64, shuffle=True, num_workers=2)\n\n# Load pre-trained ResNet-18 model\nresnet18 = models.resnet18(pretrained=True)\n\n# Move the model to GPU if available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nresnet18.to(device)\n\nresnet18.eval()\n    \ncorrect_predictions = 0\ntotal_samples = 0\n\nfor inputs, labels in tqdm(dataloader, desc=\"Evaluation\", position=0, leave=True):\n    inputs, labels = inputs.to(device), labels.to(device)\n\n    outputs = resnet18(inputs)\n\n    _, predicted = torch.max(outputs, 1)\n\n    correct_predictions += (predicted == labels).sum().item()\n    total_samples += labels.size(0)\n\n    all_predictions.extend(predicted.cpu().numpy())\n    all_labels.extend(labels.cpu().numpy())\n\n# Calculate accuracy\naccuracy = correct_predictions / total_samples\n\n# Calculate metrics\nprecision = precision_score(all_labels, all_predictions, average='weighted')\nrecall = recall_score(all_labels, all_predictions, average='weighted')\nf1 = f1_score(all_labels, all_predictions, average='weighted')\n\nprint(f\"Accuracy: {accuracy * 100:.2f}%\")\nprint(f\"Precision: {precision:.4f}, Recall: {recall:.4f}, F1 Score: {f1:.4f}\")\n\ntorch.save(resnet18.state_dict(), \"/kaggle/working/resnet18_imagenet_subset.pth\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-22T03:04:31.142107Z","iopub.execute_input":"2024-02-22T03:04:31.142873Z","iopub.status.idle":"2024-02-22T03:32:36.630986Z","shell.execute_reply.started":"2024-02-22T03:04:31.142838Z","shell.execute_reply":"2024-02-22T03:32:36.629697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import models, transforms, datasets\nfrom tqdm import tqdm\nimport os\nfrom sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score, confusion_matrix\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Set the seed for reproducibility\ntorch.manual_seed(42)\n\n# Define the path to the training data\ntrain_path = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train\"\n\n# Define data transformations\n# Define data transformations\n# transform = transforms.Compose([\n#     transforms.RandomResizedCrop(224),\n#     transforms.RandomHorizontalFlip(),\n#     transforms.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4, hue=0.1),\n#     transforms.RandomRotation(15),\n#     transforms.ToTensor(),\n#     transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),  # ImageNet mean and std\n# ])\ntransform = transforms.Compose([\n    transforms.Resize((224, 224)),  \n    transforms.ToTensor(),\n    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),\n])\n\n\n\n# Create a dataset and dataloader for the ImageNet subset\ndataset = datasets.ImageFolder(train_path, transform=transform)\ndataloader = DataLoader(dataset, batch_size=64, shuffle=True, num_workers=2)\n\n# Load pre-trained ResNet-18 model\nresnet18 = models.resnet18(pretrained=True)\n\n# Move the model to GPU if available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nresnet18.to(device)\n\n# Set the model to evaluation mode\nresnet18.eval()\n\n# Initialize lists to store predictions and labels for evaluation\nall_predictions = []\nall_labels = []\n\n# Iterate over the dataset for evaluation\nfor inputs, labels in tqdm(dataloader, desc=\"Evaluation\", position=0, leave=True):\n    inputs, labels = inputs.to(device), labels.to(device)\n\n    # Perform forward pass\n    outputs = resnet18(inputs)\n\n    # Get the predicted labels\n    _, predicted = torch.max(outputs, 1)\n\n    # Store predictions and labels\n    all_predictions.extend(predicted.cpu().numpy())\n    all_labels.extend(labels.cpu().numpy())\n\n# Calculate accuracy\naccuracy = accuracy_score(all_labels, all_predictions)\n\n# Calculate precision, recall, and F1 score\nprecision = precision_score(all_labels, all_predictions, average='weighted')\nrecall = recall_score(all_labels, all_predictions, average='weighted')\nf1 = f1_score(all_labels, all_predictions, average='weighted')\n\n# Calculate confusion matrix (truth table)\nconf_matrix = confusion_matrix(all_labels, all_predictions)\n\n# Print evaluation metrics\nprint(f\"Accuracy: {accuracy * 100:.2f}%\")\nprint(f\"Precision: {precision:.4f}, Recall: {recall:.4f}, F1 Score: {f1:.4f}\")\n\n# Plot the confusion matrix\nplt.figure(figsize=(10, 8))\nsns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False)\nplt.xlabel('Predicted Label')\nplt.ylabel('True Label')\nplt.title('Confusion Matrix')\nplt.show()\n\n# Save the model\ntorch.save(resnet18.state_dict(), \"/kaggle/working/resnet18_imagenet_subset.pth\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-22T07:54:22.320211Z","iopub.status.idle":"2024-02-22T07:54:22.320753Z","shell.execute_reply.started":"2024-02-22T07:54:22.320484Z","shell.execute_reply":"2024-02-22T07:54:22.320516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import models, transforms, datasets\nfrom tqdm import tqdm\nimport os\nfrom sklearn.metrics import precision_score, recall_score, f1_score\n \ntrain_path = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train\"\n \ntransform = transforms.Compose([\n    transforms.Resize(256),\n    transforms.CenterCrop(224),\n    transforms.ToTensor(),\n    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),\n])\n \n# Create a dataset and dataloader for the ImageNet subset\ndataset = datasets.ImageFolder(train_path, transform=transform)\ndataloader = DataLoader(dataset, batch_size=64, shuffle=True, num_workers=2)\n \n# Load pre-trained ResNet-18 model\nresnet18 = models.resnet18(pretrained=True)\n \n# Move the model to GPU if available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nresnet18.to(device)\n \n# Evaluate the model\nresnet18.eval()\n \nrunning_loss = 0.0\ncorrect_predictions = 0\ntotal_samples = 0\nall_predictions = []\nall_labels = []\n \ncriterion = nn.CrossEntropyLoss()\n \nwith torch.no_grad():  # Disable gradient calculation\n    for inputs, labels in tqdm(dataloader, desc=\"Evaluation\", position=0, leave=True):\n        inputs, labels = inputs.to(device), labels.to(device)\n \n        outputs = resnet18(inputs)\n        loss = criterion(outputs, labels)\n \n        running_loss += loss.item()\n \n        _, predicted = torch.max(outputs, 1)\n        correct_predictions += (predicted == labels).sum().item()\n        total_samples += labels.size(0)\n \n        all_predictions.extend(predicted.cpu().numpy())\n        all_labels.extend(labels.cpu().numpy())\naccuracy = correct_predictions / total_samples\nepoch_loss = running_loss / len(dataloader)\n \n# Calculate precision, recall, and F1 score\nprecision = precision_score(all_labels, all_predictions, average='weighted')\nrecall = recall_score(all_labels, all_predictions, average='weighted')\nf1 = f1_score(all_labels, all_predictions, average='weighted')\n \nprint(f\"Loss: {epoch_loss}, Accuracy: {accuracy * 100:.2f}%\")\nprint(f\"Precision: {precision:.4f}, Recall: {recall:.4f}, F1 Score: {f1:.4f}\")\n \n# Save the trained model\ntorch.save(resnet18.state_dict(), \"/kaggle/working/resnet18_imagenet_subset.pth\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-22T07:54:22.322564Z","iopub.status.idle":"2024-02-22T07:54:22.323024Z","shell.execute_reply.started":"2024-02-22T07:54:22.322788Z","shell.execute_reply":"2024-02-22T07:54:22.32281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import models, transforms, datasets\nfrom tqdm import tqdm\nimport os\nfrom sklearn.metrics import precision_score, recall_score, f1_score\n\n# Set random seed for reproducibility\ntorch.manual_seed(42)\n\ntrain_path = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train\"\n\ntransform = transforms.Compose([\n    transforms.Resize(256),\n    transforms.CenterCrop(224),\n    transforms.ToTensor(),\n    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),\n])\n\n# Create a dataset and dataloader for the ImageNet subset\ndataset = datasets.ImageFolder(train_path, transform=transform)\ndataloader = DataLoader(dataset, batch_size=64, shuffle=True, num_workers=2)\n\n# Load pre-trained ResNet-18 model\nresnet18 = models.resnet18(pretrained=True)\n\n# Move the model to GPU if available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nresnet18.to(device)\n\n# Enable quantization-aware training\nresnet18.qconfig = torch.quantization.get_default_qat_qconfig('fbgemm')\ntorch.quantization.prepare_qat(resnet18, inplace=True)\n\n# Define the optimizer for quantization-aware training\noptimizer = optim.SGD(resnet18.parameters(), lr=0.001, momentum=0.9)\n\n# Define the loss function\ncriterion = nn.CrossEntropyLoss()\n\n# Train the model\n# resnet18.train()\n\n# for epoch in range(5):  # Train for 5 epochs as an example\n#     running_loss = 0.0\n#     correct_predictions = 0\n#     total_samples = 0\n\n#     for inputs, labels in tqdm(dataloader, desc=f\"Epoch {epoch+1}\", position=0, leave=True):\n#         inputs, labels = inputs.to(device), labels.to(device)\n\n#         # Forward pass\n#         optimizer.zero_grad()\n#         outputs = resnet18(inputs)\n#         loss = criterion(outputs, labels)\n\n#         # Backward pass and optimization\n#         loss.backward()\n#         optimizer.step()\n\n#         running_loss += loss.item()\n\n#         _, predicted = torch.max(outputs, 1)\n#         correct_predictions += (predicted == labels).sum().item()\n#         total_samples += labels.size(0)\n\n#     epoch_loss = running_loss / len(dataloader)\n#     accuracy = correct_predictions / total_samples\n\n#     print(f\"Epoch {epoch+1}, Loss: {epoch_loss}, Accuracy: {accuracy * 100:.2f}%\")\n\n# Evaluate the quantized model\nresnet18.eval()\n\nall_predictions = []\nall_labels = []\n\nwith torch.no_grad():\n    for inputs, labels in tqdm(dataloader, desc=\"Evaluation\", position=0, leave=True):\n        inputs, labels = inputs.to(device), labels.to(device)\n\n        outputs = resnet18(inputs)\n\n        _, predicted = torch.max(outputs, 1)\n\n        all_predictions.extend(predicted.cpu().numpy())\n        all_labels.extend(labels.cpu().numpy())\n\n# Calculate metrics\nprecision = precision_score(all_labels, all_predictions, average='weighted')\nrecall = recall_score(all_labels, all_predictions, average='weighted')\nf1 = f1_score(all_labels, all_predictions, average='weighted')\n\nprint(f\"Precision: {precision:.4f}, Recall: {recall:.4f}, F1 Score: {f1:.4f}\")\n\n# Convert the model to a quantized model\ntorch.quantization.convert(resnet18, inplace=True)\n\n# Save the quantized model\ntorch.save(resnet18.state_dict(), \"/kaggle/working/quantized_resnet18_imagenet_subset.pth\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:41:55.140293Z","iopub.execute_input":"2024-02-09T09:41:55.140954Z","iopub.status.idle":"2024-02-09T13:15:00.086761Z","shell.execute_reply.started":"2024-02-09T09:41:55.140924Z","shell.execute_reply":"2024-02-09T13:15:00.085217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import models, transforms, datasets\nfrom tqdm import tqdm\nimport os\nfrom sklearn.metrics import precision_score, recall_score, f1_score\n# torch.manual_seed(42)\n\ntrain_path = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train\"\n\ntransform = transforms.Compose([\n    transforms.Resize(256),\n    transforms.CenterCrop(224),\n    transforms.ToTensor(),\n    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),\n])\n\n# Create a dataset and dataloader for the ImageNet subset\ndataset = datasets.ImageFolder(train_path, transform=transform)\ndataloader = DataLoader(dataset, batch_size=64, shuffle=True, num_workers=2)\n\nresnet18 = models.resnet18(pretrained=True)\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nresnet18.to(device)\n\n# Enable quantization-aware training\nresnet18.qconfig = torch.quantization.get_default_qat_qconfig('fbgemm')\ntorch.quantization.prepare_qat(resnet18, inplace=True)\n\n# Define the optimizer for quantization-aware training\noptimizer = optim.SGD(resnet18.parameters(), lr=0.001, momentum=0.9) # decay will add lst \n\n# Define the loss function\ncriterion = nn.CrossEntropyLoss()\n\n# Train the model\nresnet18.train()\n\nfor epoch in range(1):  # wil increase epochs\n    running_loss = 0.0\n    correct_predictions = 0\n    total_samples = 0\n\n    for inputs, labels in tqdm(dataloader, desc=f\"Epoch {epoch+1}\", position=0, leave=True):\n        inputs, labels = inputs.to(device), labels.to(device)\n\n        # Forward pass\n        optimizer.zero_grad()\n        outputs = resnet18(inputs)\n        loss = criterion(outputs, labels)\n\n        # Backward pass and optimization\n        loss.backward()\n        optimizer.step()\n\n        running_loss += loss.item()\n\n        _, predicted = torch.max(outputs, 1)\n        correct_predictions += (predicted == labels).sum().item()\n        total_samples += labels.size(0)\n\n    epoch_loss = running_loss / len(dataloader)\n    accuracy = correct_predictions / total_samples\n\n    print(f\"Epoch {epoch+1}, Loss: {epoch_loss}, Accuracy: {accuracy * 100:.2f}%\")\n\n# Evaluate the quantized model\nresnet18.eval()\n\nall_predictions = []\nall_labels = []\n\nwith torch.no_grad():\n    for inputs, labels in tqdm(dataloader, desc=\"Evaluation\", position=0, leave=True):\n        inputs, labels = inputs.to(device), labels.to(device)\n\n        outputs = resnet18(inputs)\n\n        _, predicted = torch.max(outputs, 1)\n\n        all_predictions.extend(predicted.cpu().numpy())\n        all_labels.extend(labels.cpu().numpy())\n\n# Calculate metrics\nprecision = precision_score(all_labels, all_predictions, average='weighted')\nrecall = recall_score(all_labels, all_predictions, average='weighted')\nf1 = f1_score(all_labels, all_predictions, average='weighted')\n\nprint(f\"Precision: {precision:.4f}, Recall: {recall:.4f}, F1 Score: {f1:.4f}, Accuracy: {accuracy * 100:.2f}%\")\n\ntorch.quantization.convert(resnet18, inplace=True)\n\n# Save the quantized model\ntorch.save(resnet18.state_dict(), \"/kaggle/working/quantized_resnet18_imagenet_subset.pth\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-22T07:57:06.905503Z","iopub.execute_input":"2024-02-22T07:57:06.906116Z","iopub.status.idle":"2024-02-22T13:15:14.055345Z","shell.execute_reply.started":"2024-02-22T07:57:06.906076Z","shell.execute_reply":"2024-02-22T13:15:14.048766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Before quantization\nprint(\"Model size before quantization:\")\nprint(sum(tensor.numel() for tensor in resnet18.state_dict().values()))\n\n# After quantization\ntorch.quantization.convert(resnet18, inplace=True)\nprint(\"Model size after quantization:\")\nprint(sum(tensor.numel() for tensor in resnet18.state_dict().values()))\n","metadata":{"execution":{"iopub.status.busy":"2024-02-24T20:47:08.457578Z","iopub.execute_input":"2024-02-24T20:47:08.458393Z","iopub.status.idle":"2024-02-24T20:47:08.471872Z","shell.execute_reply.started":"2024-02-24T20:47:08.458351Z","shell.execute_reply":"2024-02-24T20:47:08.47088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for name, module in resnet18.named_modules():\n    if hasattr(module, 'scale') and hasattr(module, 'zero_point'):\n        print(f\"{name} is quantized with scale {module.scale} and zero_point {module.zero_point}\")\n    else:\n        print(f\"{name} is not quantized\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-24T20:47:29.387487Z","iopub.execute_input":"2024-02-24T20:47:29.388343Z","iopub.status.idle":"2024-02-24T20:47:29.394313Z","shell.execute_reply.started":"2024-02-24T20:47:29.388308Z","shell.execute_reply":"2024-02-24T20:47:29.393337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if hasattr(resnet18, 'qconfig'):\n    print(\"The model has been quantized.\")\nelse:\n    print(\"The model has not been quantized.\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-22T13:52:57.423799Z","iopub.execute_input":"2024-02-22T13:52:57.424237Z","iopub.status.idle":"2024-02-22T13:52:57.42983Z","shell.execute_reply.started":"2024-02-22T13:52:57.424202Z","shell.execute_reply":"2024-02-22T13:52:57.428928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision.models as models\n\n# Load the model\nresnet18 = models.resnet18(pretrained=False)\n\n# Enable quantization-aware training\nresnet18.qconfig = torch.quantization.get_default_qat_qconfig('fbgemm')\ntorch.quantization.prepare_qat(resnet18, inplace=True)\n\n# Train the model (omitted for brevity)\n\n# Check if the model has a QConfig attached\nif hasattr(resnet18, 'qconfig'):\n    print(\"The model is quantized using quantization-aware training (QAT).\")\nelse:\n    print(\"The model is not quantized using quantization-aware training (QAT).\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-22T13:53:00.612509Z","iopub.execute_input":"2024-02-22T13:53:00.613118Z","iopub.status.idle":"2024-02-22T13:53:01.022609Z","shell.execute_reply.started":"2024-02-22T13:53:00.613087Z","shell.execute_reply":"2024-02-22T13:53:01.021631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision.models as models\nimport torch.nn as nn\n\n# Load the ResNet-18 model\nresnet18 = models.resnet18(pretrained=False)\n\n# Check if the model is quantized\ntry:\n    quantized_model = torch.quantization.quantize_dynamic(resnet18, {nn.Conv2d}, dtype=torch.qint8)\n    print(\"Model is quantized\")\nexcept Exception as e:\n    print(\"Model is not quantized\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-24T20:43:45.107778Z","iopub.execute_input":"2024-02-24T20:43:45.10867Z","iopub.status.idle":"2024-02-24T20:43:45.346973Z","shell.execute_reply.started":"2024-02-24T20:43:45.108636Z","shell.execute_reply":"2024-02-24T20:43:45.345974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import models, transforms, datasets\nfrom tqdm import tqdm\nimport os\nfrom sklearn.metrics import precision_score, recall_score, f1_score\nfrom torch.optim.lr_scheduler import StepLR\n\ntrain_path = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train\"\n\n\n# Create a dataset and dataloader for the ImageNet subset\ndataset = datasets.ImageFolder(train_path, transform=transform)\ndataloader = DataLoader(dataset, batch_size=64, shuffle=True, num_workers=2)\n\n# Load pre-trained ResNet-18 model\nresnet18 = models.resnet18(pretrained=True)\n\n# Define the quantization configuration\nquantization_config = torch.quantization.get_default_qconfig('qnnpack')\n\n# Prepare the model for quantization-aware training\nresnet18.qconfig = quantization_config\ntorch.quantization.prepare(resnet18, inplace=True)\n\n# resnet18.qconfig = torch.quantization.get_default_qat_qconfig('fbgemm')\n#         resnet18 = torch.quantization.prepare_qat(resnet18)\n#         outputs = resnet18(inputs)\n\n# Move the model to GPU if available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n# resnet18.to(device)\n\nresnet18.to('cpu')\n# inputs = inputs.to('cpu')\n\n# Define the loss function\ncriterion = nn.CrossEntropyLoss()\n\n# Define the optimizer\noptimizer = optim.SGD(resnet18.parameters(), lr=0.001, momentum=0.9)\n\n# Convert the model to a quantized version\ntorch.quantization.convert(resnet18, inplace=True)\n\n# Evaluate the quantized model\nresnet18.eval()\nrunning_loss = 0.0\ncorrect_predictions = 0\ntotal_samples = 0\nall_predictions = []\nall_labels = []\n\nwith torch.no_grad():\n    for inputs, labels in tqdm(dataloader, desc=\"Evaluation\", position=0, leave=True):\n        inputs, labels = inputs.to(cpu), labels.to(cpu)\n\n        outputs = resnet18(inputs)\n        loss = criterion(outputs, labels)\n\n        running_loss += loss.item()\n\n        _, predicted = torch.max(outputs, 1)\n        correct_predictions += (predicted == labels).sum().item()\n        total_samples += labels.size(0)\n\n        all_predictions.extend(predicted.cpu().numpy())\n        all_labels.extend(labels.cpu().numpy())\n\naccuracy = correct_predictions / total_samples\nepoch_loss = running_loss / len(dataloader)\n\n# Calculate precision, recall, and F1 score\nprecision = precision_score(all_labels, all_predictions, average='weighted')\nrecall = recall_score(all_labels, all_predictions, average='weighted')\nf1 = f1_score(all_labels, all_predictions, average='weighted')\n\nprint(f\"Loss: {epoch_loss}, Accuracy: {accuracy * 100:.2f}%\")\nprint(f\"Precision: {precision:.4f}, Recall: {recall:.4f}, F1 Score: {f1:.4f}\")\n\n# Save the trained and quantized model\ntorch.save(resnet18.state_dict(), \"/kaggle/working/resnet18_imagenet_subset_quantized.pth\")\n\n \n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-09T00:06:35.095194Z","iopub.execute_input":"2024-02-09T00:06:35.095615Z","iopub.status.idle":"2024-02-09T00:15:00.759367Z","shell.execute_reply.started":"2024-02-09T00:06:35.095585Z","shell.execute_reply":"2024-02-09T00:15:00.75816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import models, datasets, transforms\nfrom tqdm import tqdm\nimport os\nfrom sklearn.metrics import precision_score, recall_score, f1_score\n\ntrain_path = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train\"\n\ntransform = transforms.Compose([\n    transforms.Resize(256),\n    transforms.CenterCrop(224),\n    transforms.ToTensor(),\n    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),\n])\n\n# Create a dataset and dataloader for the ImageNet subset\ndataset = datasets.ImageFolder(train_path, transform=transform)\ndataloader = DataLoader(dataset, batch_size=64, shuffle=True, num_workers=2)\n\n# Load pre-trained ResNet-18 model\nresnet18 = models.resnet18(pretrained=True)\n\nquantization_config = torch.quantization.get_default_qconfig('qnnpack')\nresnet18.qconfig = quantization_config\ntorch.quantization.prepare(resnet18, inplace=True)\n\n# Move the model to CPU before quantization\nresnet18.to('cpu')\n\n# Define the loss function\ncriterion = nn.CrossEntropyLoss()\n\n# Define the optimizer\noptimizer = optim.SGD(resnet18.parameters(), lr=0.001, momentum=0.9)\n\n# # Train the model\n# resnet18.train()\n# for epoch in range(5):  # Train for 5 epochs as an example\n#     running_loss = 0.0\n#     correct_predictions = 0\n#     total_samples = 0\n\n#     for inputs, labels in tqdm(dataloader, desc=f\"Epoch {epoch+1} Training\", position=0, leave=True):\n#         inputs, labels = inputs.to('cpu'), labels.to('cpu')  # Move data to CPU\n\n#         optimizer.zero_grad()\n\n#         outputs = resnet18(inputs)\n#         loss = criterion(outputs, labels)\n#         loss.backward()\n#         optimizer.step()\n\n#         running_loss += loss.item()\n\n#         _, predicted = torch.max(outputs, 1)\n#         correct_predictions += (predicted == labels).sum().item()\n#         total_samples += labels.size(0)\n\n#     epoch_loss = running_loss / len(dataloader)\n#     accuracy = correct_predictions / total_samples\n\n    print(f\"Epoch {epoch+1} Loss: {epoch_loss}, Accuracy: {accuracy * 100:.2f}%\")\n\n# Convert the model to a quantized version\ntorch.quantization.convert(resnet18, inplace=True)\n\n# Evaluate the quantized model\nresnet18.eval()\nrunning_loss = 0.0\ncorrect_predictions = 0\ntotal_samples = 0\nall_predictions = []\nall_labels = []\n\nwith torch.no_grad():\n    for inputs, labels in tqdm(dataloader, desc=\"Evaluation\", position=0, leave=True):\n        inputs, labels = inputs.to('cpu'), labels.to('cpu')  # Move data to CPU\n\n        outputs = resnet18(inputs)\n        loss = criterion(outputs, labels)\n\n        running_loss += loss.item()\n\n        _, predicted = torch.max(outputs, 1)\n        correct_predictions += (predicted == labels).sum().item()\n        total_samples += labels.size(0)\n\n        all_predictions.extend(predicted.cpu().numpy())\n        all_labels.extend(labels.cpu().numpy())\n\naccuracy = correct_predictions / total_samples\nepoch_loss = running_loss / len(dataloader)\n\n# Calculate precision, recall, and F1 score\nprecision = precision_score(all_labels, all_predictions, average='weighted')\nrecall = recall_score(all_labels, all_predictions, average='weighted')\nf1 = f1_score(all_labels, all_predictions, average='weighted')\n\nprint(f\"Loss: {epoch_loss}, Accuracy: {accuracy * 100:.2f}%\")\nprint(f\"Precision: {precision:.4f}, Recall: {recall:.4f}, F1 Score: {f1:.4f}\")\n\n# Save the trained and quantized model\ntorch.save(resnet18.state_dict(), \"/kaggle/working/resnet18_imagenet_subset_quantized.pth\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-09T05:04:26.021055Z","iopub.execute_input":"2024-02-09T05:04:26.021615Z","iopub.status.idle":"2024-02-09T05:04:26.036688Z","shell.execute_reply.started":"2024-02-09T05:04:26.021586Z","shell.execute_reply":"2024-02-09T05:04:26.035329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import models, datasets, transforms\nfrom tqdm import tqdm\nimport os\nfrom sklearn.metrics import precision_score, recall_score, f1_score\n\ntrain_path = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train\"\n\ntransform = transforms.Compose([\n    transforms.Resize(256),\n    transforms.CenterCrop(224),\n    transforms.ToTensor(),\n    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),\n])\n\n# Create a dataset and dataloader for the ImageNet subset\ndataset = datasets.ImageFolder(train_path, transform=transform)\ndataloader = DataLoader(dataset, batch_size=64, shuffle=True, num_workers=2)\n\n# Load pre-trained ResNet-18 model\nresnet18 = models.resnet18(pretrained=True)\n\nquantization_config = torch.quantization.get_default_qconfig('qnnpack')\nresnet18.qconfig = quantization_config\ntorch.quantization.prepare(resnet18, inplace=True)\n\n# Move the model to GPU before quantization\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nresnet18.to(device)\n\n# Define the loss function\ncriterion = nn.CrossEntropyLoss()\n\n# Define the optimizer\noptimizer = optim.SGD(resnet18.parameters(), lr=0.001, momentum=0.9)\n\n# Train the model\n# resnet18.train()\n# for epoch in range(5):  # Train for 5 epochs as an example\n#     running_loss = 0.0\n#     correct_predictions = 0\n#     total_samples = 0\n\n#     for inputs, labels in tqdm(dataloader, desc=f\"Epoch {epoch+1} Training\", position=0, leave=True):\n#         inputs, labels = inputs.to(device), labels.to(device)  # Move data to GPU\n\n#         optimizer.zero_grad()\n\n#         outputs = resnet18(inputs)\n#         loss = criterion(outputs, labels)\n#         loss.backward()\n#         optimizer.step()\n\n#         running_loss += loss.item()\n\n#         _, predicted = torch.max(outputs, 1)\n#         correct_predictions += (predicted == labels).sum().item()\n#         total_samples += labels.size(0)\n\n#     epoch_loss = running_loss / len(dataloader)\n#     accuracy = correct_predictions / total_samples\n\n#     print(f\"Epoch {epoch+1} Loss: {epoch_loss}, Accuracy: {accuracy * 100:.2f}%\")\n\n# Convert the model to a quantized version\ntorch.quantization.convert(resnet18, inplace=True)\n\n# Evaluate the quantized model\nresnet18.eval()\nrunning_loss = 0.0\ncorrect_predictions = 0\ntotal_samples = 0\nall_predictions = []\nall_labels = []\n\nwith torch.no_grad():\n    for inputs, labels in tqdm(dataloader, desc=\"Evaluation\", position=0, leave=True):\n        inputs, labels = inputs.to(device), labels.to(device)  # Move data to GPU\n\n        outputs = resnet18(inputs)\n        loss = criterion(outputs, labels)\n\n        running_loss += loss.item()\n\n        _, predicted = torch.max(outputs, 1)\n        correct_predictions += (predicted == labels).sum().item()\n        total_samples += labels.size(0)\n\n        all_predictions.extend(predicted.cpu().numpy())\n        all_labels.extend(labels.cpu().numpy())\n\naccuracy = correct_predictions / total_samples\nepoch_loss = running_loss / len(dataloader)\n\n# Calculate precision, recall, and F1 score\nprecision = precision_score(all_labels, all_predictions, average='weighted')\nrecall = recall_score(all_labels, all_predictions, average='weighted')\nf1 = f1_score(all_labels, all_predictions, average='weighted')\n\nprint(f\"Loss: {epoch_loss}, Accuracy: {accuracy * 100:.2f}%\")\nprint(f\"Precision: {precision:.4f}, Recall: {recall:.4f}, F1 Score: {f1:.4f}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-22T07:56:43.778085Z","iopub.status.idle":"2024-02-22T07:56:43.778424Z","shell.execute_reply.started":"2024-02-22T07:56:43.77826Z","shell.execute_reply":"2024-02-22T07:56:43.778276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import models, transforms, datasets\nfrom tqdm import tqdm\nimport os\nfrom sklearn.metrics import precision_score, recall_score, f1_score\nfrom torch.optim.lr_scheduler import StepLR\n\n# Set the path to your ImageNet subset\nsubset_path = \"/kaggle/working/imagenet_1pct\" \n\n# Define data transformations\ntransform = transforms.Compose([\n    transforms.RandomCrop(32, padding=4),\n        transforms.RandomHorizontalFlip(),\n        transforms.ToTensor(),\n    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),\n    ])\n\n# Create a dataset and dataloader for the ImageNet subset\ndataset = datasets.ImageFolder(subset_path, transform=transform)\ndataloader = DataLoader(dataset, batch_size= 32, shuffle=True, num_workers= 2)\n\n# Load pre-trained ResNet-18 model\nresnet18 = models.resnet18(weights= None)\n\n# Modify the final fully connected layer for your specific classification task\nnum_classes = len(os.listdir(subset_path))\nresnet18.fc = nn.Sequential(\n    nn.Dropout(0.7),\n    nn.Linear(resnet18.fc.in_features, num_classes)\n)\n\n# Apply quantization-aware training to the model\nresnet18.qconfig = torch.quantization.get_default_qat_qconfig('fbgemm')\ntorch.quantization.prepare_qat(resnet18, inplace=True)\n\n# Define loss function and optimizer\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.SGD(resnet18.parameters(), lr= 0.85, momentum=0.9, weight_decay=1e-5)\n\n\nscheduler = StepLR(optimizer, step_size=5, gamma=0.1)\n\n# Move the model to GPU if available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nresnet18.to(device)\n\n# Train the quantized model\nnum_epochs = 2\nfor epoch in range(num_epochs):\n    running_loss = 0.0\n    correct_predictions = 0\n    total_samples = 0\n    all_predictions = []\n    all_labels = []\n\n    for inputs, labels in tqdm(dataloader, desc=f\"Epoch {epoch+1}/{num_epochs}\", position=0, leave=True):\n        inputs, labels = inputs.to(device), labels.to(device)\n\n        optimizer.zero_grad()\n\n        # Enable quantization during training\n        resnet18.qconfig = torch.quantization.get_default_qat_qconfig('fbgemm')\n        resnet18 = torch.quantization.prepare_qat(resnet18)\n        outputs = resnet18(inputs)\n        loss = criterion(outputs, labels)\n\n        loss.backward()\n        optimizer.step()\n        scheduler.step()\n\n        running_loss += loss.item()\n\n        _, predicted = torch.max(outputs, 1)\n        correct_predictions += (predicted == labels).sum().item()\n        total_samples += labels.size(0)\n\n        all_predictions.extend(predicted.cpu().numpy())\n        all_labels.extend(labels.cpu().numpy())\n\n    accuracy = correct_predictions / total_samples\n    epoch_loss = running_loss / len(dataloader)\n    precision = precision_score(all_labels, all_predictions, average='weighted')\n    recall = recall_score(all_labels, all_predictions, average='weighted')\n    f1 = f1_score(all_labels, all_predictions, average='weighted')\n    print(f\"Epoch {epoch+1}/{num_epochs}, Loss: {epoch_loss}, Accuracy: {accuracy * 100:.2f}%\")\n    print(f\"Precision: {precision:.4f}, Recall: {recall:.4f}, F1 Score: {f1:.4f}\")\n\n# Convert the model to a regular version\ntorch.quantization.convert(resnet18, inplace=True)\n\n# Save the quantized model\ntorch.save(resnet18.state_dict(), \"/kaggle/working/quantized_resnet18_imagenet_subset.pth\")\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.models import resnet18\nfrom torchvision.models.resnet import ResNet18_Weights\n\n# Load the model with pre-trained weights\nweights = ResNet18_Weights.IMAGENET1K_V1  # Choose the specific pre-trained weights\nmodel = resnet18(weights=weights)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import models, transforms, datasets\nfrom tqdm import tqdm\nfrom sklearn.metrics import precision_score, recall_score, f1_score\n\n# Assuming the train_path is set correctly\ntrain_path = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train\"\n\ntransform = transforms.Compose([\n    transforms.Resize(256),\n    transforms.CenterCrop(224),\n    transforms.ToTensor(),\n    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),\n])\n\ndataset = datasets.ImageFolder(train_path, transform=transform)\ndataloader = DataLoader(dataset, batch_size=64, shuffle=True, num_workers=2)\n\nresnet18 = models.resnet18(pretrained=True)\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nresnet18.to(device)\n\n# Correct placement for quantization-aware training setup\nresnet18.qconfig = torch.quantization.get_default_qat_qconfig('fbgemm')\nresnet18 = torch.quantization.prepare_qat(resnet18, inplace=True)\n\noptimizer = optim.SGD(resnet18.parameters(), lr=0.001, momentum=0.9)\ncriterion = nn.CrossEntropyLoss()\n\n# Training loop\nresnet18.train()\nfor epoch in range(5):  # Adjust number of epochs as needed\n    running_loss = 0.0\n    correct_predictions = 0\n    total_samples = 0\n\n    for inputs, labels in tqdm(dataloader, desc=f\"Epoch {epoch+1}\", position=0, leave=True):\n        inputs, labels = inputs.to(device), labels.to(device)\n\n        optimizer.zero_grad()\n        outputs = resnet18(inputs)\n        loss = criterion(outputs, labels)\n\n        loss.backward()\n        optimizer.step()\n\n        running_loss += loss.item()\n        _, predicted = torch.max(outputs, 1)\n        correct_predictions += (predicted == labels).sum().item()\n        total_samples += labels.size(0)\n\n    epoch_loss = running_loss / len(dataloader)\n    accuracy = correct_predictions / total_samples\n    print(f\"Epoch {epoch+1}, Loss: {epoch_loss}, Accuracy: {accuracy * 100:.2f}%\")\n\n# Convert to quantized model for evaluation and deployment\nresnet18.eval()\nresnet18 = torch.quantization.convert(resnet18, inplace=True)\n\n# Evaluation loop (now with the quantized model)\nall_predictions = []\nall_labels = []\nwith torch.no_grad():\n    for inputs, labels in tqdm(dataloader, desc=\"Evaluation\", position=0, leave=True):\n        inputs, labels = inputs.to(device), labels.to(device)\n        outputs = resnet18(inputs)\n        _, predicted = torch.max(outputs, 1)\n        all_predictions.extend(predicted.cpu().numpy())\n        all_labels.extend(labels.cpu().numpy())\n\nprecision = precision_score(all_labels, all_predictions, average='weighted')\nrecall = recall_score(all_labels, all_predictions, average='weighted')\nf1 = f1_score(all_labels, all_predictions, average='weighted')\n\nprint(f\"Precision: {precision:.4f}, Recall: {recall:.4f}, F1 Score: {f1:.4f}, Accuracy: {accuracy * 100:.2f}%\")\n\n# Save the quantized model\ntorch.save(resnet18.state_dict(), \"/kaggle/working/quantized_resnet18_imagenet_subset.pth\")\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}