{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":63134,"databundleVersionId":6880857,"sourceType":"competition"}],"dockerImageVersionId":30302,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# To ignore warinings\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:07:24.473539Z","iopub.execute_input":"2023-11-12T17:07:24.473922Z","iopub.status.idle":"2023-11-12T17:07:24.497431Z","shell.execute_reply.started":"2023-11-12T17:07:24.473838Z","shell.execute_reply":"2023-11-12T17:07:24.496522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Importing required libraries**","metadata":{}},{"cell_type":"code","source":"!pip install -Uqq fastai","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:07:24.498836Z","iopub.execute_input":"2023-11-12T17:07:24.499098Z","iopub.status.idle":"2023-11-12T17:07:38.639947Z","shell.execute_reply.started":"2023-11-12T17:07:24.499072Z","shell.execute_reply":"2023-11-12T17:07:38.638684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\nfrom fastai.vision.widgets import *","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:07:38.641385Z","iopub.execute_input":"2023-11-12T17:07:38.641718Z","iopub.status.idle":"2023-11-12T17:07:41.205498Z","shell.execute_reply.started":"2023-11-12T17:07:38.641674Z","shell.execute_reply":"2023-11-12T17:07:41.204663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import ImageFile\nImageFile.LOAD_TRUNCATED_IMAGES = True","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:07:41.207695Z","iopub.execute_input":"2023-11-12T17:07:41.208109Z","iopub.status.idle":"2023-11-12T17:07:41.212794Z","shell.execute_reply.started":"2023-11-12T17:07:41.208079Z","shell.execute_reply":"2023-11-12T17:07:41.211741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Loading data and splitting it into training and validation set**","metadata":{}},{"cell_type":"code","source":"path = Path('/kaggle/input/anyas-gojo-revival-aerospace-enigma/train')","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:07:41.214227Z","iopub.execute_input":"2023-11-12T17:07:41.214552Z","iopub.status.idle":"2023-11-12T17:07:41.229967Z","shell.execute_reply.started":"2023-11-12T17:07:41.214523Z","shell.execute_reply":"2023-11-12T17:07:41.229000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os","metadata":{"execution":{"iopub.status.busy":"2023-11-12T18:13:42.753514Z","iopub.execute_input":"2023-11-12T18:13:42.754508Z","iopub.status.idle":"2023-11-12T18:13:42.759615Z","shell.execute_reply.started":"2023-11-12T18:13:42.754460Z","shell.execute_reply":"2023-11-12T18:13:42.758593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2023-11-12T18:12:43.268261Z","iopub.execute_input":"2023-11-12T18:12:43.268929Z","iopub.status.idle":"2023-11-12T18:12:43.273866Z","shell.execute_reply.started":"2023-11-12T18:12:43.268890Z","shell.execute_reply":"2023-11-12T18:12:43.272999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path=\"/kaggle/input/anyas-gojo-revival-aerospace-enigma/train\"\nfor file in os.listdir(path):\n  path1=path+'/'+file\n  for image in os.listdir(path1):\n    filename=path1+'/'+image\n    if filename.endswith(\".jpg\"):\n        prefix = filename.split(\".jpg\")[0]\n        try:\n          im = Image.open(filename)\n          im.save(prefix+'.png')\n        except:\n          print(\"Error\"+filename)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = DataBlock(\nblocks=(ImageBlock, CategoryBlock),\nget_items=get_image_files,\nsplitter=RandomSplitter(valid_pct=0.2, seed=42),\nget_y=parent_label,\nitem_tfms=[Resize(192, method='squish')]\n).dataloaders(path)\n\ndls.show_batch(max_n=6)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:27:11.965226Z","iopub.execute_input":"2023-11-12T17:27:11.966326Z","iopub.status.idle":"2023-11-12T17:27:13.339660Z","shell.execute_reply.started":"2023-11-12T17:27:11.966270Z","shell.execute_reply":"2023-11-12T17:27:13.338421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Defining our learner (model) which uses a pre-trained ResNet-50**","metadata":{}},{"cell_type":"code","source":"learn = vision_learner(dls, resnet50, metrics=accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:51:32.975957Z","iopub.execute_input":"2023-11-12T17:51:32.976343Z","iopub.status.idle":"2023-11-12T17:51:33.647841Z","shell.execute_reply.started":"2023-11-12T17:51:32.976310Z","shell.execute_reply":"2023-11-12T17:51:33.646857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training our learner","metadata":{}},{"cell_type":"code","source":"learn.fine_tune(epochs=20)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:51:35.688789Z","iopub.execute_input":"2023-11-12T17:51:35.689834Z","iopub.status.idle":"2023-11-12T17:54:39.778867Z","shell.execute_reply.started":"2023-11-12T17:51:35.689786Z","shell.execute_reply":"2023-11-12T17:54:39.776783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Looking at a few predictions of our model","metadata":{}},{"cell_type":"code","source":"learn.show_results()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:30:35.361107Z","iopub.status.idle":"2023-11-12T17:30:35.361502Z","shell.execute_reply.started":"2023-11-12T17:30:35.361306Z","shell.execute_reply":"2023-11-12T17:30:35.361325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Looking at the images for which the losses were high","metadata":{}},{"cell_type":"code","source":"interp = Interpretation.from_learner(learn)\ninterp.plot_top_losses(9, figsize=(15,10))","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:30:35.363422Z","iopub.status.idle":"2023-11-12T17:30:35.363831Z","shell.execute_reply.started":"2023-11-12T17:30:35.363612Z","shell.execute_reply":"2023-11-12T17:30:35.363629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plotting a confusion-matrix to look at misclassification","metadata":{}},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:30:35.365384Z","iopub.status.idle":"2023-11-12T17:30:35.365789Z","shell.execute_reply.started":"2023-11-12T17:30:35.365568Z","shell.execute_reply":"2023-11-12T17:30:35.365587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:30:35.367188Z","iopub.status.idle":"2023-11-12T17:30:35.367590Z","shell.execute_reply.started":"2023-11-12T17:30:35.367369Z","shell.execute_reply":"2023-11-12T17:30:35.367393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Creating the final submission file**","metadata":{}},{"cell_type":"code","source":"test_path = Path('/kaggle/input/anyas-gojo-revival-aerospace-enigma/test')\ntest_X = np.array(os.listdir(test_path))","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:30:35.369292Z","iopub.status.idle":"2023-11-12T17:30:35.369687Z","shell.execute_reply.started":"2023-11-12T17:30:35.369501Z","shell.execute_reply":"2023-11-12T17:30:35.369520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_y = []\nfor img in test_X:\n    with learn.no_bar(), learn.no_logging():\n        test_y.append(learn.predict(test_path/img)[0])\ntest_y = np.array(test_y)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:30:35.371647Z","iopub.status.idle":"2023-11-12T17:30:35.372059Z","shell.execute_reply.started":"2023-11-12T17:30:35.371865Z","shell.execute_reply":"2023-11-12T17:30:35.371889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"character_id = np.array([char_id for char_id in test_X])","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:30:35.373337Z","iopub.status.idle":"2023-11-12T17:30:35.373730Z","shell.execute_reply.started":"2023-11-12T17:30:35.373525Z","shell.execute_reply":"2023-11-12T17:30:35.373543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(data=[character_id, test_y]).T","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:30:35.375534Z","iopub.status.idle":"2023-11-12T17:30:35.375923Z","shell.execute_reply.started":"2023-11-12T17:30:35.375733Z","shell.execute_reply":"2023-11-12T17:30:35.375752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.columns = ['image_path', 'class']\nsubmission.set_index('image_path', inplace=True)\nsubmission.head(4)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:30:35.377184Z","iopub.status.idle":"2023-11-12T17:30:35.377547Z","shell.execute_reply.started":"2023-11-12T17:30:35.377365Z","shell.execute_reply":"2023-11-12T17:30:35.377383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:30:35.378504Z","iopub.status.idle":"2023-11-12T17:30:35.378902Z","shell.execute_reply.started":"2023-11-12T17:30:35.378680Z","shell.execute_reply":"2023-11-12T17:30:35.378698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}