{"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 os\nimport shutil\n\nimport numpy as np\nimport pandas as pd\nimport torch\n\nfrom datasets import load_dataset\nfrom torchvision.transforms import Compose, RandomResizedCrop, GaussianBlur, RandomAdjustSharpness, RandomEqualize, ToTensor\n\nfrom transformers import TrainingArguments, Trainer\nfrom transformers import ConvNextV2ForImageClassification\nfrom transformers import AutoImageProcessor, AutoModelForImageClassification\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-25T08:20:35.033703Z","iopub.execute_input":"2023-10-25T08:20:35.033966Z","iopub.status.idle":"2023-10-25T08:20:48.667993Z","shell.execute_reply.started":"2023-10-25T08:20:35.033941Z","shell.execute_reply":"2023-10-25T08:20:48.667248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntest = pd.read_csv(\"/kaggle/input/UBC-OCEAN/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-10-25T08:20:48.669518Z","iopub.execute_input":"2023-10-25T08:20:48.669805Z","iopub.status.idle":"2023-10-25T08:20:48.684285Z","shell.execute_reply.started":"2023-10-25T08:20:48.66978Z","shell.execute_reply":"2023-10-25T08:20:48.683583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nid2label = {\n    0:\"CC\",\n    1:\"EC\",\n    2:\"HGSC\",\n    3:\"LGSC\",\n    4:\"MC\",\n    #5:\"Other\"\n}\nlabel2id = dict()\n\nfor i in id2label.keys():\n    label2id[id2label[i]] = i","metadata":{"execution":{"iopub.status.busy":"2023-10-25T08:20:48.685405Z","iopub.execute_input":"2023-10-25T08:20:48.685921Z","iopub.status.idle":"2023-10-25T08:20:48.690676Z","shell.execute_reply.started":"2023-10-25T08:20:48.685896Z","shell.execute_reply":"2023-10-25T08:20:48.68983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrained = \"/kaggle/input/ubc-convnextv2-tiny2/convnextv2-tiny\"\nimage_processor = AutoImageProcessor.from_pretrained(pretrained, local_files_only=True)\nimage_processor","metadata":{"execution":{"iopub.status.busy":"2023-10-25T08:20:48.69188Z","iopub.execute_input":"2023-10-25T08:20:48.692139Z","iopub.status.idle":"2023-10-25T08:20:48.712368Z","shell.execute_reply.started":"2023-10-25T08:20:48.692117Z","shell.execute_reply":"2023-10-25T08:20:48.711588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Creating model\")\nmodel = ConvNextV2ForImageClassification.from_pretrained(\n    pretrained,\n    num_labels=5,\n    id2label=id2label,\n    label2id=label2id,\n    ignore_mismatched_sizes=True,\n)\n\nmodel.to(\"cuda\")\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-25T08:20:48.714595Z","iopub.execute_input":"2023-10-25T08:20:48.715161Z","iopub.status.idle":"2023-10-25T08:20:55.707392Z","shell.execute_reply.started":"2023-10-25T08:20:48.715129Z","shell.execute_reply":"2023-10-25T08:20:55.706427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nsubmission = []\nfor i in test.iterrows():\n    id = i[1][\"image_id\"]\n    try:\n        image = Image.open(f\"/kaggle/input/UBC-OCEAN/test_thumbnails/{id}_thumbnail.png\")\n        print(image)\n\n        DEVICE = \"cuda\"\n        #image_processor = AutoImageProcessor.from_pretrained(CHECKPOINT_DIR)\n\n        inputs = image_processor(image, return_tensors=\"pt\")\n        inputs = inputs.to(DEVICE)\n\n        with torch.no_grad():\n            logits = model(**inputs).logits\n\n        predicted_label = logits.argmax(-1).item()\n        predicted_label_name = model.config.id2label[predicted_label]\n    except Exception as e:\n        print(\"fuck\", e)\n        predicted_label_name = \"CC\"\n        \n    submission.append({'image_id':id, 'label':predicted_label_name})\n    \nsubmission = pd.DataFrame(submission)\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(submission)","metadata":{"execution":{"iopub.status.busy":"2023-10-25T08:20:55.708753Z","iopub.execute_input":"2023-10-25T08:20:55.709415Z","iopub.status.idle":"2023-10-25T08:21:00.448111Z","shell.execute_reply.started":"2023-10-25T08:20:55.709381Z","shell.execute_reply":"2023-10-25T08:21:00.4472Z"},"trusted":true},"execution_count":null,"outputs":[]}]}