{"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":"gpu","dataSources":[{"sourceId":74631,"databundleVersionId":8138903,"sourceType":"competition"}],"dockerImageVersionId":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"_kg_hide-input":false,"execution":{"iopub.status.busy":"2024-04-08T13:06:52.760194Z","iopub.execute_input":"2024-04-08T13:06:52.760610Z","iopub.status.idle":"2024-04-08T13:06:54.041276Z","shell.execute_reply.started":"2024-04-08T13:06:52.760584Z","shell.execute_reply":"2024-04-08T13:06:54.040364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=pd.read_csv('/kaggle/input/copy-of-simpsons-faces-but-3-app/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:06:54.042514Z","iopub.execute_input":"2024-04-08T13:06:54.042875Z","iopub.status.idle":"2024-04-08T13:06:54.075959Z","shell.execute_reply.started":"2024-04-08T13:06:54.042843Z","shell.execute_reply":"2024-04-08T13:06:54.075216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:06:54.077818Z","iopub.execute_input":"2024-04-08T13:06:54.078086Z","iopub.status.idle":"2024-04-08T13:06:54.093621Z","shell.execute_reply.started":"2024-04-08T13:06:54.078063Z","shell.execute_reply":"2024-04-08T13:06:54.092897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe()","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:06:54.094556Z","iopub.execute_input":"2024-04-08T13:06:54.094786Z","iopub.status.idle":"2024-04-08T13:06:54.119993Z","shell.execute_reply.started":"2024-04-08T13:06:54.094765Z","shell.execute_reply":"2024-04-08T13:06:54.119053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:06:54.121526Z","iopub.execute_input":"2024-04-08T13:06:54.121836Z","iopub.status.idle":"2024-04-08T13:06:54.147075Z","shell.execute_reply.started":"2024-04-08T13:06:54.121807Z","shell.execute_reply":"2024-04-08T13:06:54.144012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.classId.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:06:54.149093Z","iopub.execute_input":"2024-04-08T13:06:54.149470Z","iopub.status.idle":"2024-04-08T13:06:54.159276Z","shell.execute_reply.started":"2024-04-08T13:06:54.149437Z","shell.execute_reply":"2024-04-08T13:06:54.158370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.className.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:06:54.160508Z","iopub.execute_input":"2024-04-08T13:06:54.160756Z","iopub.status.idle":"2024-04-08T13:06:54.173897Z","shell.execute_reply.started":"2024-04-08T13:06:54.160734Z","shell.execute_reply":"2024-04-08T13:06:54.171699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nfrom matplotlib import pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:06:54.175007Z","iopub.execute_input":"2024-04-08T13:06:54.175279Z","iopub.status.idle":"2024-04-08T13:06:54.376289Z","shell.execute_reply.started":"2024-04-08T13:06:54.175254Z","shell.execute_reply":"2024-04-08T13:06:54.375594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Replace 'image_path.jpg' with the path to your image file\nimage_path = '/kaggle/input/copy-of-simpsons-faces-but-3-app/simpsons/train/dataset_000001.png'\nimage = cv2.imread(image_path)\n\n# OpenCV reads images in BGR format by default. Convert it to RGB to display correctly.\nimage_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n# Display the image\nplt.imshow(image_rgb)\nplt.axis('off')  # Turn off the axis\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:06:54.379773Z","iopub.execute_input":"2024-04-08T13:06:54.380102Z","iopub.status.idle":"2024-04-08T13:06:54.560604Z","shell.execute_reply.started":"2024-04-08T13:06:54.380079Z","shell.execute_reply":"2024-04-08T13:06:54.559460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[data['className']=='bart_simpson'].sample(frac=1)[:3]['path'].tolist()","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:07:55.253922Z","iopub.execute_input":"2024-04-08T13:07:55.254606Z","iopub.status.idle":"2024-04-08T13:07:55.266432Z","shell.execute_reply.started":"2024-04-08T13:07:55.254573Z","shell.execute_reply":"2024-04-08T13:07:55.265605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers.utils.dummy_vision_objects import ImageGPTFeatureExtractor\nimport random\nfrom PIL import ImageDraw, ImageFont, Image\nimport os,random\n\n# Path to the directory containing images\nimage_dir = '/kaggle/input/copy-of-simpsons-faces-but-3-app/simpsons/train'\n\ndef show_examples(ds, seed: int = 1234, examples_per_class: int = 3, size=(350,350)):\n\n    w, h = size\n    labels = ds['className'].unique().tolist()\n    random.shuffle(labels)\n    labels=labels[0:5]\n    grid = Image.new('RGB', size=(examples_per_class * w, len(labels)*h))\n    draw = ImageDraw.Draw(grid)\n    font = ImageFont.truetype(\"/usr/share/fonts/truetype/liberation/LiberationMono-Bold.ttf\", 24)\n\n    for label_id, label in enumerate(labels):\n\n        # Filter the dataset by a single label, shuffle it, and grab a few samples\n        ds_slice = data[data['className']==label].sample(frac=1)[:examples_per_class]['path'].tolist()\n        \n        \n        # Plot this label's examples along a row\n        for i, example in enumerate(ds_slice):\n            image_path = os.path.join(image_dir, example)\n            image = Image.open(image_path)\n            idx = examples_per_class * label_id + i\n            box = (idx % examples_per_class * w, idx // examples_per_class * h)\n            grid.paste(image.resize(size), box=box)\n            draw.text(box, label, (255, 255, 255), font=font)\n\n    return grid\n\nshow_examples(data, seed=random.randint(0, 1337), examples_per_class=3)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:07:56.586303Z","iopub.execute_input":"2024-04-08T13:07:56.587153Z","iopub.status.idle":"2024-04-08T13:07:57.367806Z","shell.execute_reply.started":"2024-04-08T13:07:56.587120Z","shell.execute_reply":"2024-04-08T13:07:57.366539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import ViTFeatureExtractor\n\nmodel_name_or_path = 'google/vit-base-patch16-224'\nfeature_extractor = ViTFeatureExtractor.from_pretrained(model_name_or_path)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:07:59.695039Z","iopub.execute_input":"2024-04-08T13:07:59.695386Z","iopub.status.idle":"2024-04-08T13:07:59.885732Z","shell.execute_reply.started":"2024-04-08T13:07:59.695359Z","shell.execute_reply":"2024-04-08T13:07:59.884832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_extractor","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:08:02.685028Z","iopub.execute_input":"2024-04-08T13:08:02.685682Z","iopub.status.idle":"2024-04-08T13:08:02.691469Z","shell.execute_reply.started":"2024-04-08T13:08:02.685648Z","shell.execute_reply":"2024-04-08T13:08:02.690592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_extractor(image, return_tensors='pt')","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:08:32.632971Z","iopub.execute_input":"2024-04-08T13:08:32.633635Z","iopub.status.idle":"2024-04-08T13:08:32.651647Z","shell.execute_reply.started":"2024-04-08T13:08:32.633605Z","shell.execute_reply":"2024-04-08T13:08:32.650821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:08:33.404470Z","iopub.execute_input":"2024-04-08T13:08:33.404798Z","iopub.status.idle":"2024-04-08T13:08:33.410433Z","shell.execute_reply.started":"2024-04-08T13:08:33.404773Z","shell.execute_reply":"2024-04-08T13:08:33.409560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# Split the data into training and validation sets (80% training, 20% validation)\ntrain_data, val_data = train_test_split(data, test_size=0.2, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:08:34.007224Z","iopub.execute_input":"2024-04-08T13:08:34.007921Z","iopub.status.idle":"2024-04-08T13:08:34.744499Z","shell.execute_reply.started":"2024-04-08T13:08:34.007891Z","shell.execute_reply":"2024-04-08T13:08:34.743696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset\nclass SimpsonsDataset(Dataset):\n    def __init__(self, dataframe, root_dir, feature_extractor, transform=None):\n        self.dataframe = dataframe\n        self.root_dir = root_dir\n        self.feature_extractor = feature_extractor\n        self.transform = transform  \n        \n    def __len__(self):\n        return len(self.dataframe)\n\n    def __getitem__(self, idx):\n        img_path = self.dataframe.iloc[idx]['path']\n        img = Image.open(f'{self.root_dir}/{img_path}').convert('RGB')\n\n        if self.transform:\n            img = self.transform(img)\n\n        # Apply feature extractor\n        features = self.feature_extractor(images=img, return_tensors=\"pt\")\n\n        class_id = self.dataframe.iloc[idx]['classId']\n\n        return features['pixel_values'].squeeze(0), class_id\n","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:08:34.746055Z","iopub.execute_input":"2024-04-08T13:08:34.746673Z","iopub.status.idle":"2024-04-08T13:08:34.754031Z","shell.execute_reply.started":"2024-04-08T13:08:34.746646Z","shell.execute_reply":"2024-04-08T13:08:34.753126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision import transforms\nfrom torch.utils.data import DataLoader\n# Define training data augmentation (if necessary, adjust based on your requirements)\ntrain_transforms = transforms.Compose([\n    transforms.RandomHorizontalFlip(),\n    transforms.RandomRotation(degrees=15),\n    # Ensure no resizing or normalization here as it's handled by ViTFeatureExtractor\n])\n\n\n# Initialize training and validation datasets\ntrain_dataset = SimpsonsDataset(\n    dataframe=train_data,\n    root_dir='/kaggle/input/copy-of-simpsons-faces-but-3-app/simpsons/train',\n    feature_extractor=feature_extractor,\n    transform=train_transforms\n)\n\nval_dataset = SimpsonsDataset(\n    dataframe=val_data,\n    root_dir='/kaggle/input/copy-of-simpsons-faces-but-3-app/simpsons/train',\n    feature_extractor=feature_extractor,\n)\n\n# Initialize DataLoaders\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)\nval_loader = DataLoader(val_dataset, batch_size=32, shuffle=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:08:35.169050Z","iopub.execute_input":"2024-04-08T13:08:35.169873Z","iopub.status.idle":"2024-04-08T13:08:35.400246Z","shell.execute_reply.started":"2024-04-08T13:08:35.169840Z","shell.execute_reply":"2024-04-08T13:08:35.399511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import ViTForImageClassification, ViTFeatureExtractor\nimport torch\n\nmodel = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224')\n","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:08:36.248471Z","iopub.execute_input":"2024-04-08T13:08:36.249136Z","iopub.status.idle":"2024-04-08T13:08:36.637548Z","shell.execute_reply.started":"2024-04-08T13:08:36.249105Z","shell.execute_reply":"2024-04-08T13:08:36.636749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.optim import AdamW\nfrom torch.nn.functional import cross_entropy\n\n# Define the device\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n# Prepare model and optimizer\nmodel.to(device)\noptimizer = AdamW(model.parameters(), lr=5e-5)\n\n# Training loop\nmodel.train()\nnum_epochs = 3  # Set the number of epochs based on your dataset and training needs\nfor epoch in range(num_epochs):\n    for batch in train_loader:\n        # Move batch to device\n        inputs, labels = batch\n        inputs = inputs.to(device)\n        labels = labels.to(device)\n\n        # Zero the parameter gradients\n        optimizer.zero_grad()\n\n        # Forward pass\n        outputs = model(inputs).logits\n\n        # Compute loss\n        loss = cross_entropy(outputs, labels)\n\n        # Backward pass and optimize\n        loss.backward()\n        optimizer.step()\n\n        print(f\"Loss: {loss.item()}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:08:38.526888Z","iopub.execute_input":"2024-04-08T13:08:38.527244Z","iopub.status.idle":"2024-04-08T13:15:31.783868Z","shell.execute_reply.started":"2024-04-08T13:08:38.527207Z","shell.execute_reply":"2024-04-08T13:15:31.782948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\ntotal = 0\ncorrect = 0\nwith torch.no_grad():\n    for batch in val_loader:\n        inputs, labels = batch\n        inputs = inputs.to(device)\n        labels = labels.to(device)\n\n        outputs = model(inputs).logits\n        _, predicted = torch.max(outputs.data, 1)\n        total += labels.size(0)\n        correct += (predicted == labels).sum().item()\n\nprint(f'Accuracy: {100 * correct / total}%')\n","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:15:31.785911Z","iopub.execute_input":"2024-04-08T13:15:31.786273Z","iopub.status.idle":"2024-04-08T13:16:00.030217Z","shell.execute_reply.started":"2024-04-08T13:15:31.786239Z","shell.execute_reply":"2024-04-08T13:16:00.029292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test=pd.read_csv('/kaggle/input/copy-of-simpsons-faces-but-3-app/test.csv')\ndata_sample=pd.read_csv('/kaggle/input/copy-of-simpsons-faces-but-3-app/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:19:39.165463Z","iopub.execute_input":"2024-04-08T13:19:39.166152Z","iopub.status.idle":"2024-04-08T13:19:39.183870Z","shell.execute_reply.started":"2024-04-08T13:19:39.166122Z","shell.execute_reply":"2024-04-08T13:19:39.182823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:19:03.119253Z","iopub.execute_input":"2024-04-08T13:19:03.119620Z","iopub.status.idle":"2024-04-08T13:19:03.129380Z","shell.execute_reply.started":"2024-04-08T13:19:03.119590Z","shell.execute_reply":"2024-04-08T13:19:03.128466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_sample.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:19:49.917435Z","iopub.execute_input":"2024-04-08T13:19:49.918036Z","iopub.status.idle":"2024-04-08T13:19:49.927289Z","shell.execute_reply.started":"2024-04-08T13:19:49.918004Z","shell.execute_reply":"2024-04-08T13:19:49.926112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_path = '/kaggle/working/vit_model.pth'\ntorch.save(model.state_dict(), model_path)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:24:10.728739Z","iopub.execute_input":"2024-04-08T13:24:10.729091Z","iopub.status.idle":"2024-04-08T13:24:11.420913Z","shell.execute_reply.started":"2024-04-08T13:24:10.729063Z","shell.execute_reply":"2024-04-08T13:24:11.419902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SimpsonsTestDataset(Dataset):\n    def __init__(self, dataframe, root_dir, feature_extractor):\n        self.dataframe = dataframe\n        self.root_dir = root_dir\n        self.feature_extractor = feature_extractor\n\n    def __len__(self):\n        return len(self.dataframe)\n\n    def __getitem__(self, idx):\n        img_path = self.dataframe.iloc[idx]['path']\n        img = Image.open(f'{self.root_dir}/{img_path}').convert('RGB')\n        features = self.feature_extractor(images=img, return_tensors=\"pt\")\n        return features['pixel_values'].squeeze(0), img_path\n","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:24:44.341078Z","iopub.execute_input":"2024-04-08T13:24:44.341447Z","iopub.status.idle":"2024-04-08T13:24:44.348367Z","shell.execute_reply.started":"2024-04-08T13:24:44.341394Z","shell.execute_reply":"2024-04-08T13:24:44.347460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = SimpsonsTestDataset(data_test, '/kaggle/input/copy-of-simpsons-faces-but-3-app/simpsons/test', feature_extractor)\ntest_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:25:31.356186Z","iopub.execute_input":"2024-04-08T13:25:31.357139Z","iopub.status.idle":"2024-04-08T13:25:31.363535Z","shell.execute_reply.started":"2024-04-08T13:25:31.357090Z","shell.execute_reply":"2024-04-08T13:25:31.362459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()  # Set model to evaluation mode\npredictions = []\npaths = []\n\nwith torch.no_grad():\n    for inputs, img_paths in test_loader:\n        inputs = inputs.to(device)\n\n        outputs = model(inputs).logits\n        _, predicted = torch.max(outputs, 1)\n\n        predictions.extend(predicted.cpu().numpy())\n        paths.extend(img_paths)\n\n# Now, you have your predictions and the corresponding image paths\n","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:25:42.070476Z","iopub.execute_input":"2024-04-08T13:25:42.070807Z","iopub.status.idle":"2024-04-08T13:25:57.484939Z","shell.execute_reply.started":"2024-04-08T13:25:42.070783Z","shell.execute_reply":"2024-04-08T13:25:57.484149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    'path': paths,\n    'classId': predictions\n})\nsubmission_file = '/kaggle/working/submission.csv'\nsubmission_df.to_csv(submission_file, index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:26:06.585066Z","iopub.execute_input":"2024-04-08T13:26:06.585391Z","iopub.status.idle":"2024-04-08T13:26:06.596835Z","shell.execute_reply.started":"2024-04-08T13:26:06.585367Z","shell.execute_reply":"2024-04-08T13:26:06.596071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions=pd.read_csv('/kaggle/working/submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:26:35.024591Z","iopub.execute_input":"2024-04-08T13:26:35.024945Z","iopub.status.idle":"2024-04-08T13:26:35.032297Z","shell.execute_reply.started":"2024-04-08T13:26:35.024915Z","shell.execute_reply":"2024-04-08T13:26:35.031388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-08T13:26:43.152542Z","iopub.execute_input":"2024-04-08T13:26:43.153257Z","iopub.status.idle":"2024-04-08T13:26:43.161880Z","shell.execute_reply.started":"2024-04-08T13:26:43.153224Z","shell.execute_reply":"2024-04-08T13:26:43.160976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}