{"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":3362,"databundleVersionId":31148,"sourceType":"competition"},{"sourceId":179944495,"sourceType":"kernelVersion"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# ADL - Homework 5.3 (Analysis)","metadata":{}},{"cell_type":"code","source":"!pip install patchify","metadata":{"execution":{"iopub.status.busy":"2024-05-27T21:42:28.316193Z","iopub.execute_input":"2024-05-27T21:42:28.317114Z","iopub.status.idle":"2024-05-27T21:42:41.534369Z","shell.execute_reply.started":"2024-05-27T21:42:28.317077Z","shell.execute_reply":"2024-05-27T21:42:41.533111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-05-27T21:42:41.536294Z","iopub.execute_input":"2024-05-27T21:42:41.536585Z","iopub.status.idle":"2024-05-27T21:42:42.110198Z","shell.execute_reply.started":"2024-05-27T21:42:41.536558Z","shell.execute_reply":"2024-05-27T21:42:42.109416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analysis of best topology","metadata":{}},{"cell_type":"code","source":"def getvalues(txtfile):\n    v = []\n    with open(txtfile, 'r') as file:\n        for line in file:\n            try:\n                n = float(line.strip())\n                v.append(n)\n            except ValueError:\n                pass\n    return np.array(v)","metadata":{"execution":{"iopub.status.busy":"2024-05-27T21:42:42.111416Z","iopub.execute_input":"2024-05-27T21:42:42.112099Z","iopub.status.idle":"2024-05-27T21:42:42.117659Z","shell.execute_reply.started":"2024-05-27T21:42:42.112064Z","shell.execute_reply":"2024-05-27T21:42:42.116766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ta_6_6 = getvalues('/kaggle/input/adl-homework-5-3/Flower-Image-Classification-using-Vision-Transformer/TrainAcc_Model6layers6heads.txt')\nta_6_12 = getvalues('/kaggle/input/adl-homework-5-3/Flower-Image-Classification-using-Vision-Transformer/TrainAcc_Model6layers12heads.txt')\nta_12_6 = getvalues('/kaggle/input/adl-homework-5-3/Flower-Image-Classification-using-Vision-Transformer/TrainAcc_Model12layers6heads.txt')\nta_12_12 = getvalues('/kaggle/input/adl-homework-5-3/Flower-Image-Classification-using-Vision-Transformer/TrainAcc_Model12layers12heads.txt')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T21:42:42.120122Z","iopub.execute_input":"2024-05-27T21:42:42.120865Z","iopub.status.idle":"2024-05-27T21:42:42.148054Z","shell.execute_reply.started":"2024-05-27T21:42:42.120833Z","shell.execute_reply":"2024-05-27T21:42:42.147383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(ta_6_6, label='layers=6, heads=6')\nplt.plot(ta_6_12, label='layers=6, heads=12')\nplt.plot(ta_12_6, label='layers=12, heads=6')\nplt.plot(ta_12_12, label='layers=12, heads=12')\nplt.title('Training Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T21:42:42.149143Z","iopub.execute_input":"2024-05-27T21:42:42.149920Z","iopub.status.idle":"2024-05-27T21:42:42.501842Z","shell.execute_reply.started":"2024-05-27T21:42:42.149887Z","shell.execute_reply":"2024-05-27T21:42:42.500927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"va_6_6 = getvalues('/kaggle/input/adl-homework-5-3/Flower-Image-Classification-using-Vision-Transformer/ValAcc_Model6layers6heads.txt')\nva_6_12 = getvalues('/kaggle/input/adl-homework-5-3/Flower-Image-Classification-using-Vision-Transformer/ValAcc_Model6layers12heads.txt')\nva_12_6 = getvalues('/kaggle/input/adl-homework-5-3/Flower-Image-Classification-using-Vision-Transformer/ValAcc_Model12layers6heads.txt')\nva_12_12 = getvalues('/kaggle/input/adl-homework-5-3/Flower-Image-Classification-using-Vision-Transformer/ValAcc_Model12layers12heads.txt')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T21:42:42.502923Z","iopub.execute_input":"2024-05-27T21:42:42.503186Z","iopub.status.idle":"2024-05-27T21:42:42.524114Z","shell.execute_reply.started":"2024-05-27T21:42:42.503163Z","shell.execute_reply":"2024-05-27T21:42:42.523447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(va_6_6, label='layers=6, heads=6')\nplt.plot(va_6_12, label='layers=6, heads=12')\nplt.plot(va_12_6, label='layers=12, heads=6')\nplt.plot(va_12_12, label='layers=12, heads=12')\nplt.title('Validation Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T21:42:42.525095Z","iopub.execute_input":"2024-05-27T21:42:42.525372Z","iopub.status.idle":"2024-05-27T21:42:42.837564Z","shell.execute_reply.started":"2024-05-27T21:42:42.525349Z","shell.execute_reply":"2024-05-27T21:42:42.836696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing the Dogs vs Cats Dataset","metadata":{}},{"cell_type":"code","source":"import zipfile\nimport os\n\n#Define paths to the zip files\ntrain_zip_path = '/kaggle/input/dogs-vs-cats/train.zip'\ntest_zip_path = '/kaggle/input/dogs-vs-cats/test1.zip'\n\n#Define directories to extract the data\ntrain_extract_dir = '/kaggle/working/train'\ntest_extract_dir = '/kaggle/working/test'\n\n#Create directories if they don't exist\nos.makedirs(train_extract_dir, exist_ok=True)\nos.makedirs(test_extract_dir, exist_ok=True)\n\n#Unzip train data\nwith zipfile.ZipFile(train_zip_path, 'r') as zip_ref:\n    zip_ref.extractall(train_extract_dir)\n\n#Unzip test data\nwith zipfile.ZipFile(test_zip_path, 'r') as zip_ref:\n    zip_ref.extractall(test_extract_dir)\n\n#List extracted files\nprint(\"Train data extracted to:\", os.listdir(train_extract_dir))\nprint(\"Test data extracted to:\", os.listdir(test_extract_dir))","metadata":{"execution":{"iopub.status.busy":"2024-05-27T21:42:42.838907Z","iopub.execute_input":"2024-05-27T21:42:42.839219Z","iopub.status.idle":"2024-05-27T21:42:55.931136Z","shell.execute_reply.started":"2024-05-27T21:42:42.839192Z","shell.execute_reply":"2024-05-27T21:42:55.930160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create directories for training and validation splits\nbase_dir = '/kaggle/working/cats_and_dogs_small'\ntrain_dir = os.path.join(base_dir, 'train')\nvalidation_dir = os.path.join(base_dir, 'validation')\nos.makedirs(train_dir, exist_ok=True)\nos.makedirs(validation_dir, exist_ok=True)\n\n#Create subdirectories for train and validation\ntrain_cats_dir = os.path.join(train_dir, 'cats')\ntrain_dogs_dir = os.path.join(train_dir, 'dogs')\nvalidation_cats_dir = os.path.join(validation_dir, 'cats')\nvalidation_dogs_dir = os.path.join(validation_dir, 'dogs')\nos.makedirs(train_cats_dir, exist_ok=True)\nos.makedirs(train_dogs_dir, exist_ok=True)\nos.makedirs(validation_cats_dir, exist_ok=True)\nos.makedirs(validation_dogs_dir, exist_ok=True)\n\n#Define the paths to the original training data\noriginal_train_dir = '/kaggle/working/train/train'\n#print(os.listdir('/kaggle/working/train'))\n#Split the dataset into training and validation sets\nall_cats = [os.path.join(original_train_dir, f) for f in os.listdir(original_train_dir) if 'cat' in f]\nall_dogs = [os.path.join(original_train_dir, f) for f in os.listdir(original_train_dir) if 'dog' in f]\n\n#print (len(all_cats))\ntrain_cats, val_cats = train_test_split(all_cats, test_size=0.2, random_state=42)\ntrain_dogs, val_dogs = train_test_split(all_dogs, test_size=0.2, random_state=42)\n\n##Copy images to corresponding directories\nfor file in train_cats:\n    shutil.copy(file, train_cats_dir)\nfor file in val_cats:\n    shutil.copy(file, validation_cats_dir)\nfor file in train_dogs:\n    shutil.copy(file, train_dogs_dir)\nfor file in val_dogs:\n    shutil.copy(file, validation_dogs_dir)","metadata":{"execution":{"iopub.status.busy":"2024-05-27T21:42:55.932221Z","iopub.execute_input":"2024-05-27T21:42:55.932507Z","iopub.status.idle":"2024-05-27T21:42:59.058981Z","shell.execute_reply.started":"2024-05-27T21:42:55.932484Z","shell.execute_reply":"2024-05-27T21:42:59.058153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_dir)\nprint(validation_dir)","metadata":{"execution":{"iopub.status.busy":"2024-05-27T21:42:59.062090Z","iopub.execute_input":"2024-05-27T21:42:59.062898Z","iopub.status.idle":"2024-05-27T21:42:59.067401Z","shell.execute_reply.started":"2024-05-27T21:42:59.062867Z","shell.execute_reply":"2024-05-27T21:42:59.066493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Using the classification","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/WCosmo/Flower-Image-Classification-using-Vision-Transformer","metadata":{"execution":{"iopub.status.busy":"2024-05-27T21:42:59.068655Z","iopub.execute_input":"2024-05-27T21:42:59.069275Z","iopub.status.idle":"2024-05-27T21:43:01.805629Z","shell.execute_reply.started":"2024-05-27T21:42:59.069225Z","shell.execute_reply":"2024-05-27T21:43:01.804589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir('Flower-Image-Classification-using-Vision-Transformer/')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T21:43:01.807047Z","iopub.execute_input":"2024-05-27T21:43:01.807382Z","iopub.status.idle":"2024-05-27T21:43:01.812382Z","shell.execute_reply.started":"2024-05-27T21:43:01.807354Z","shell.execute_reply":"2024-05-27T21:43:01.811315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python train2.py --datapath \"/kaggle/working/cats_and_dogs_small/train\" --heads 6 --layers 6 ","metadata":{"execution":{"iopub.status.busy":"2024-05-27T21:43:01.813612Z","iopub.execute_input":"2024-05-27T21:43:01.813891Z","iopub.status.idle":"2024-05-27T21:48:19.817580Z","shell.execute_reply.started":"2024-05-27T21:43:01.813868Z","shell.execute_reply":"2024-05-27T21:48:19.816334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python test2.py --datapath \"/kaggle/working/cats_and_dogs_small/validation\" --heads 6 --layers 6 ","metadata":{"execution":{"iopub.status.busy":"2024-05-27T21:48:19.819389Z","iopub.execute_input":"2024-05-27T21:48:19.819773Z","iopub.status.idle":"2024-05-27T21:49:02.710810Z","shell.execute_reply.started":"2024-05-27T21:48:19.819732Z","shell.execute_reply":"2024-05-27T21:49:02.709842Z"},"trusted":true},"execution_count":null,"outputs":[]}]}