{"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":64733,"databundleVersionId":8171035,"sourceType":"competition"},{"sourceId":43782,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":36765}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-06T05:12:36.440334Z","iopub.execute_input":"2024-05-06T05:12:36.440666Z","iopub.status.idle":"2024-05-06T05:12:37.292421Z","shell.execute_reply.started":"2024-05-06T05:12:36.440640Z","shell.execute_reply":"2024-05-06T05:12:37.291637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Paths","metadata":{}},{"cell_type":"markdown","source":"# Enviromental Rasters","metadata":{}},{"cell_type":"markdown","source":"### Enviromental Rasters - Climate - Average 1981-2010","metadata":{}},{"cell_type":"code","source":"glc24_pa_bioclim_avg_test=\"/kaggle/input/geolifeclef-2024/EnvironmentalRasters/EnvironmentalRasters/Climate/Average 1981-2010/GLC24-PA-test-bioclimatic.csv\"\nglc24_pa_bioclim_avg_train=\"/kaggle/input/geolifeclef-2024/EnvironmentalRasters/EnvironmentalRasters/Climate/Average 1981-2010/GLC24-PA-train-bioclimatic.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:18:20.113060Z","iopub.execute_input":"2024-05-06T05:18:20.113860Z","iopub.status.idle":"2024-05-06T05:18:20.118063Z","shell.execute_reply.started":"2024-05-06T05:18:20.113828Z","shell.execute_reply":"2024-05-06T05:18:20.117105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Train","metadata":{}},{"cell_type":"code","source":"glc24_pa_bioclim_avg_train_df = pd.read_csv(glc24_pa_bioclim_avg_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:48:33.014418Z","iopub.execute_input":"2024-05-06T05:48:33.014790Z","iopub.status.idle":"2024-05-06T05:48:33.322839Z","shell.execute_reply.started":"2024-05-06T05:48:33.014764Z","shell.execute_reply":"2024-05-06T05:48:33.322047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"glc24_pa_bioclim_avg_train_df","metadata":{"execution":{"iopub.status.busy":"2024-05-06T07:48:55.085951Z","iopub.execute_input":"2024-05-06T07:48:55.086789Z","iopub.status.idle":"2024-05-06T07:48:55.104780Z","shell.execute_reply.started":"2024-05-06T07:48:55.086755Z","shell.execute_reply":"2024-05-06T07:48:55.103761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Example:\n\nLets take surveyid :212\nbio-1:2883\n\nso we will calculate it like 2883*0.1 + offset(-273.15) Which is nearly === 15 deg celcius\n\nSimilarly you can understand other things....","metadata":{}},{"cell_type":"markdown","source":"##### Test","metadata":{}},{"cell_type":"code","source":"glc24_pa_bioclim_avg_test_df = pd.read_csv(glc24_pa_bioclim_avg_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:50:02.265458Z","iopub.execute_input":"2024-05-06T05:50:02.266296Z","iopub.status.idle":"2024-05-06T05:50:02.299788Z","shell.execute_reply.started":"2024-05-06T05:50:02.266263Z","shell.execute_reply":"2024-05-06T05:50:02.298847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Enviromental Rasters - Climate - Monthly","metadata":{}},{"cell_type":"code","source":"glc24_pa_bioclim_monthly_test = \"/kaggle/input/geolifeclef-2024/EnvironmentalRasters/EnvironmentalRasters/Climate/Monthly/GLC24-PA-test-bioclimatic_monthly.csv\"\nglc24_pa_bioclim_monthly_train = \"/kaggle/input/geolifeclef-2024/EnvironmentalRasters/EnvironmentalRasters/Climate/Monthly/GLC24-PA-train-bioclimatic_monthly.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:19:46.272181Z","iopub.execute_input":"2024-05-06T05:19:46.272879Z","iopub.status.idle":"2024-05-06T05:19:46.276971Z","shell.execute_reply.started":"2024-05-06T05:19:46.272852Z","shell.execute_reply":"2024-05-06T05:19:46.276043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Train","metadata":{}},{"cell_type":"markdown","source":"### Year 2000 - 2018 total 19 years data for europe flora, pr-precipitation, 01-month from jan - dec and 2000 is year here. tas is tempreature monthly. min, max, mean.","metadata":{}},{"cell_type":"code","source":"glc24_pa_bioclim_monthly_train_df = pd.read_csv(glc24_pa_bioclim_monthly_train)\nglc24_pa_bioclim_monthly_train_df","metadata":{"execution":{"iopub.status.busy":"2024-05-06T07:17:01.264804Z","iopub.execute_input":"2024-05-06T07:17:01.265476Z","iopub.status.idle":"2024-05-06T07:17:10.116457Z","shell.execute_reply.started":"2024-05-06T07:17:01.265446Z","shell.execute_reply":"2024-05-06T07:17:10.115539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Test","metadata":{}},{"cell_type":"markdown","source":"# Enviromental Rasters - Elevation","metadata":{}},{"cell_type":"code","source":"glc24_pa_elevation_test = \"/kaggle/input/geolifeclef-2024/EnvironmentalRasters/EnvironmentalRasters/Elevation/GLC24-PA-test-elevation.csv\"\nglc24_pa_elevation_train = \"/kaggle/input/geolifeclef-2024/EnvironmentalRasters/EnvironmentalRasters/Elevation/GLC24-PA-train-elevation.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:22:30.149418Z","iopub.execute_input":"2024-05-06T05:22:30.150328Z","iopub.status.idle":"2024-05-06T05:22:30.154570Z","shell.execute_reply.started":"2024-05-06T05:22:30.150294Z","shell.execute_reply":"2024-05-06T05:22:30.153521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Train","metadata":{}},{"cell_type":"markdown","source":"https://lpdaac.usgs.gov/products/astgtmv003/","metadata":{}},{"cell_type":"code","source":"glc24_pa_elevation_train_df = pd.read_csv(glc24_pa_elevation_train)\nglc24_pa_elevation_train_df","metadata":{"execution":{"iopub.status.busy":"2024-05-06T07:16:07.619135Z","iopub.execute_input":"2024-05-06T07:16:07.619900Z","iopub.status.idle":"2024-05-06T07:16:07.662560Z","shell.execute_reply.started":"2024-05-06T07:16:07.619868Z","shell.execute_reply":"2024-05-06T07:16:07.661588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Test","metadata":{}},{"cell_type":"markdown","source":"# Enviromental Rasters - Human Footprint","metadata":{}},{"cell_type":"code","source":"glc24_pa_humanfootprint_test = \"/kaggle/input/geolifeclef-2024/EnvironmentalRasters/EnvironmentalRasters/Human Footprint/GLC24-PA-test-human_footprint.csv\"\nglc24_pa_humanfootprint_train = \"/kaggle/input/geolifeclef-2024/EnvironmentalRasters/EnvironmentalRasters/Human Footprint/GLC24-PA-train-human_footprint.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:26:03.441992Z","iopub.execute_input":"2024-05-06T05:26:03.442913Z","iopub.status.idle":"2024-05-06T05:26:03.446895Z","shell.execute_reply.started":"2024-05-06T05:26:03.442876Z","shell.execute_reply":"2024-05-06T05:26:03.445997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Train\n","metadata":{}},{"cell_type":"code","source":"glc24_pa_humanfootprint_train_df = pd.read_csv(glc24_pa_humanfootprint_train)\nglc24_pa_humanfootprint_train_df","metadata":{"execution":{"iopub.status.busy":"2024-05-06T08:09:54.652502Z","iopub.execute_input":"2024-05-06T08:09:54.652918Z","iopub.status.idle":"2024-05-06T08:09:54.946984Z","shell.execute_reply.started":"2024-05-06T08:09:54.652887Z","shell.execute_reply":"2024-05-06T08:09:54.945991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Test","metadata":{}},{"cell_type":"markdown","source":"#### Enviromental Rasters - LandCover","metadata":{}},{"cell_type":"code","source":"glc24_pa_landcover_test = \"/kaggle/input/geolifeclef-2024/EnvironmentalRasters/EnvironmentalRasters/LandCover/GLC24-PA-test-landcover.csv\"\nglc24_pa_landcover_train = \"/kaggle/input/geolifeclef-2024/EnvironmentalRasters/EnvironmentalRasters/LandCover/GLC24-PA-train-landcover.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:26:10.483166Z","iopub.execute_input":"2024-05-06T05:26:10.483561Z","iopub.status.idle":"2024-05-06T05:26:10.488021Z","shell.execute_reply.started":"2024-05-06T05:26:10.483533Z","shell.execute_reply":"2024-05-06T05:26:10.487077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Train","metadata":{}},{"cell_type":"code","source":"glc24_pa_landcover_train_df = pd.read_csv(glc24_pa_landcover_train)\nglc24_pa_landcover_train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:57:52.024486Z","iopub.execute_input":"2024-05-06T05:57:52.024840Z","iopub.status.idle":"2024-05-06T05:57:52.057397Z","shell.execute_reply.started":"2024-05-06T05:57:52.024814Z","shell.execute_reply":"2024-05-06T05:57:52.056503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Test","metadata":{}},{"cell_type":"markdown","source":"#### Envirmental Rasters - Soil Grids","metadata":{}},{"cell_type":"code","source":"glc24_pa_soilgrids_test = \"/kaggle/input/geolifeclef-2024/EnvironmentalRasters/EnvironmentalRasters/SoilGrids/GLC24-PA-test-soilgrids.csv\"\nglc24_pa_soilgrids_train = \"/kaggle/input/geolifeclef-2024/EnvironmentalRasters/EnvironmentalRasters/SoilGrids/GLC24-PA-train-soilgrids.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:59:47.082406Z","iopub.execute_input":"2024-05-06T05:59:47.082757Z","iopub.status.idle":"2024-05-06T05:59:47.087183Z","shell.execute_reply.started":"2024-05-06T05:59:47.082730Z","shell.execute_reply":"2024-05-06T05:59:47.086269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Train","metadata":{}},{"cell_type":"code","source":"glc24_pa_soilgrids_train_df = pd.read_csv(glc24_pa_soilgrids_train)\nglc24_pa_soilgrids_train_df","metadata":{"execution":{"iopub.status.busy":"2024-05-06T08:16:24.221962Z","iopub.execute_input":"2024-05-06T08:16:24.222365Z","iopub.status.idle":"2024-05-06T08:16:24.348767Z","shell.execute_reply.started":"2024-05-06T08:16:24.222339Z","shell.execute_reply":"2024-05-06T08:16:24.347746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Test","metadata":{}},{"cell_type":"markdown","source":"# LandSat TimeSeries","metadata":{}},{"cell_type":"code","source":"glc24_pa_ls_ts_test_blue  = \"/kaggle/input/geolifeclef-2024/PA-test-landsat_time_series/GLC24-PA-test-landsat_time_series-blue.csv\"\nglc24_pa_ls_ts_test_green = \"/kaggle/input/geolifeclef-2024/PA-test-landsat_time_series/GLC24-PA-test-landsat_time_series-green.csv\"\nglc24_pa_ls_ts_test_nir= \"/kaggle/input/geolifeclef-2024/PA-test-landsat_time_series/GLC24-PA-test-landsat_time_series-nir.csv\"\nglc24_pa_ls_ts_test_red = \"/kaggle/input/geolifeclef-2024/PA-test-landsat_time_series/GLC24-PA-test-landsat_time_series-red.csv\"\nglc24_pa_ls_ts_test_swir1 = \"/kaggle/input/geolifeclef-2024/PA-test-landsat_time_series/GLC24-PA-test-landsat_time_series-swir1.csv\"\nglc24_pa_ls_ts_test_swir2 = \"/kaggle/input/geolifeclef-2024/PA-test-landsat_time_series/GLC24-PA-test-landsat_time_series-swir2.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:31:01.166058Z","iopub.execute_input":"2024-05-06T05:31:01.166425Z","iopub.status.idle":"2024-05-06T05:31:01.171582Z","shell.execute_reply.started":"2024-05-06T05:31:01.166400Z","shell.execute_reply":"2024-05-06T05:31:01.170492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train","metadata":{}},{"cell_type":"code","source":"glc24_pa_ls_ts_train_blue  = \"/kaggle/input/geolifeclef-2024/PA-train-landsat_time_series/GLC24-PA-train-landsat_time_series-blue.csv\"\nglc24_pa_ls_ts_train_green = \"/kaggle/input/geolifeclef-2024/PA-train-landsat_time_series/GLC24-PA-train-landsat_time_series-green.csv\"\nglc24_pa_ls_ts_train_nir= \"/kaggle/input/geolifeclef-2024/PA-train-landsat_time_series/GLC24-PA-train-landsat_time_series-nir.csv\"\nglc24_pa_ls_ts_train_red = \"/kaggle/input/geolifeclef-2024/PA-train-landsat_time_series/GLC24-PA-train-landsat_time_series-red.csv\"\nglc24_pa_ls_ts_train_swir1 = \"/kaggle/input/geolifeclef-2024/PA-train-landsat_time_series/GLC24-PA-train-landsat_time_series-swir1.csv\"\nglc24_pa_ls_ts_train_swir2 = \"/kaggle/input/geolifeclef-2024/PA-train-landsat_time_series/GLC24-PA-train-landsat_time_series-swir2.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:38:31.139922Z","iopub.execute_input":"2024-05-06T05:38:31.140789Z","iopub.status.idle":"2024-05-06T05:38:31.145689Z","shell.execute_reply.started":"2024-05-06T05:38:31.140758Z","shell.execute_reply":"2024-05-06T05:38:31.144639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Blue","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"glc24_pa_ls_ts_train_blue_df = pd.read_csv(glc24_pa_ls_ts_train_blue)\nglc24_pa_ls_ts_train_blue_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:01:51.953095Z","iopub.execute_input":"2024-05-06T06:01:51.954159Z","iopub.status.idle":"2024-05-06T06:01:52.696671Z","shell.execute_reply.started":"2024-05-06T06:01:51.954125Z","shell.execute_reply":"2024-05-06T06:01:52.695714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Green","metadata":{}},{"cell_type":"code","source":"glc24_pa_ls_ts_train_green_df = pd.read_csv(glc24_pa_ls_ts_train_green)\nglc24_pa_ls_ts_train_green_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:02:54.593544Z","iopub.execute_input":"2024-05-06T06:02:54.593914Z","iopub.status.idle":"2024-05-06T06:02:55.938389Z","shell.execute_reply.started":"2024-05-06T06:02:54.593885Z","shell.execute_reply":"2024-05-06T06:02:55.937500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### NIR","metadata":{}},{"cell_type":"code","source":"glc24_pa_ls_ts_train_nir_df = pd.read_csv(glc24_pa_ls_ts_train_nir)\nglc24_pa_ls_ts_train_nir_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:03:25.994654Z","iopub.execute_input":"2024-05-06T06:03:25.995631Z","iopub.status.idle":"2024-05-06T06:03:27.222601Z","shell.execute_reply.started":"2024-05-06T06:03:25.995586Z","shell.execute_reply":"2024-05-06T06:03:27.221647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### RED","metadata":{}},{"cell_type":"code","source":"glc24_pa_ls_ts_train_red_df = pd.read_csv(glc24_pa_ls_ts_train_red)\nglc24_pa_ls_ts_train_red_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:03:45.704513Z","iopub.execute_input":"2024-05-06T06:03:45.705584Z","iopub.status.idle":"2024-05-06T06:03:47.016901Z","shell.execute_reply.started":"2024-05-06T06:03:45.705545Z","shell.execute_reply":"2024-05-06T06:03:47.015979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### SWIR-1","metadata":{}},{"cell_type":"code","source":"glc24_pa_ls_ts_train_swir1_df = pd.read_csv(glc24_pa_ls_ts_train_swir1)\nglc24_pa_ls_ts_train_swir1_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:04:07.978168Z","iopub.execute_input":"2024-05-06T06:04:07.979056Z","iopub.status.idle":"2024-05-06T06:04:09.178633Z","shell.execute_reply.started":"2024-05-06T06:04:07.979028Z","shell.execute_reply":"2024-05-06T06:04:09.177499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### SWIR-2","metadata":{}},{"cell_type":"code","source":"glc24_pa_ls_ts_train_swir2_df = pd.read_csv(glc24_pa_ls_ts_train_swir2)\nglc24_pa_ls_ts_train_swir2_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:04:29.064088Z","iopub.execute_input":"2024-05-06T06:04:29.065041Z","iopub.status.idle":"2024-05-06T06:04:30.259233Z","shell.execute_reply.started":"2024-05-06T06:04:29.065009Z","shell.execute_reply":"2024-05-06T06:04:30.258150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## PA Satellite NIR - Folder","metadata":{}},{"cell_type":"code","source":"pa_test_patches_nir = \"/kaggle/input/geolifeclef-2024/PA_Test_SatellitePatches_NIR/pa_test_patches_nir\"\npa_train_patches_nir = \"/kaggle/input/geolifeclef-2024/PA_Train_SatellitePatches_NIR/pa_train_patches_nir\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:46:48.734292Z","iopub.execute_input":"2024-05-06T05:46:48.734671Z","iopub.status.idle":"2024-05-06T05:46:48.739220Z","shell.execute_reply.started":"2024-05-06T05:46:48.734643Z","shell.execute_reply":"2024-05-06T05:46:48.738252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pa_train_patches_nir_dirs = os.listdir(pa_train_patches_nir)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:14:25.322298Z","iopub.execute_input":"2024-05-06T06:14:25.323003Z","iopub.status.idle":"2024-05-06T06:14:25.327424Z","shell.execute_reply.started":"2024-05-06T06:14:25.322973Z","shell.execute_reply":"2024-05-06T06:14:25.326395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tpnirsam = pa_train_patches_nir+\"/\"+pa_train_patches_nir_dirs[0]+'/00'","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:14:46.323906Z","iopub.execute_input":"2024-05-06T06:14:46.324294Z","iopub.status.idle":"2024-05-06T06:14:46.329597Z","shell.execute_reply.started":"2024-05-06T06:14:46.324265Z","shell.execute_reply":"2024-05-06T06:14:46.328587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### \"/CD/AB/XXXXABCD.jpeg'. For example, the image location for the surveyId 3018575 is \"./75/85/3018575.jpeg\"  ----> This is how we will check images. ","metadata":{}},{"cell_type":"code","source":"for img_1 in os.listdir(tpnirsam):\n    print(img_1)\n    img = mpimg.imread(tpnirsam+'/'+img_1)\n    plt.imshow(img)\n    plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:22:19.662273Z","iopub.execute_input":"2024-05-06T06:22:19.662980Z","iopub.status.idle":"2024-05-06T06:22:22.065006Z","shell.execute_reply.started":"2024-05-06T06:22:19.662948Z","shell.execute_reply":"2024-05-06T06:22:22.064121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## PA Satellite RGB - Folder","metadata":{}},{"cell_type":"code","source":"pa_test_patches_rgb = \"/kaggle/input/geolifeclef-2024/PA_Test_SatellitePatches_RGB/pa_test_patches_rgb\"\npa_train_patches_rgb = \"/kaggle/input/geolifeclef-2024/PA_Train_SatellitePatches_RGB/pa_train_patches_rgb\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:46:51.006861Z","iopub.execute_input":"2024-05-06T05:46:51.007267Z","iopub.status.idle":"2024-05-06T05:46:51.011996Z","shell.execute_reply.started":"2024-05-06T05:46:51.007235Z","shell.execute_reply":"2024-05-06T05:46:51.011032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pa_train_patches_rgb_dirs = os.listdir(pa_train_patches_rgb)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:25:06.672039Z","iopub.execute_input":"2024-05-06T06:25:06.672944Z","iopub.status.idle":"2024-05-06T06:25:06.678831Z","shell.execute_reply.started":"2024-05-06T06:25:06.672911Z","shell.execute_reply":"2024-05-06T06:25:06.677864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patprgs = pa_train_patches_rgb + '/' + pa_train_patches_rgb_dirs[0]+'/00'","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:26:15.375241Z","iopub.execute_input":"2024-05-06T06:26:15.376015Z","iopub.status.idle":"2024-05-06T06:26:15.380307Z","shell.execute_reply.started":"2024-05-06T06:26:15.375963Z","shell.execute_reply":"2024-05-06T06:26:15.379293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patprgs","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:27:54.110699Z","iopub.execute_input":"2024-05-06T06:27:54.111076Z","iopub.status.idle":"2024-05-06T06:27:54.117326Z","shell.execute_reply.started":"2024-05-06T06:27:54.111046Z","shell.execute_reply":"2024-05-06T06:27:54.116387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(patprgs)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:27:59.986258Z","iopub.execute_input":"2024-05-06T06:27:59.986611Z","iopub.status.idle":"2024-05-06T06:27:59.999400Z","shell.execute_reply.started":"2024-05-06T06:27:59.986585Z","shell.execute_reply":"2024-05-06T06:27:59.998466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img_1 in os.listdir(patprgs)[:3]:\n    print(img_1)\n    img = mpimg.imread(patprgs+'/'+img_1)\n    plt.imshow(img)\n    plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:34:09.885155Z","iopub.execute_input":"2024-05-06T06:34:09.885566Z","iopub.status.idle":"2024-05-06T06:34:10.642212Z","shell.execute_reply.started":"2024-05-06T06:34:09.885539Z","shell.execute_reply":"2024-05-06T06:34:10.641360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Time Series Cubes - Foloders","metadata":{}},{"cell_type":"code","source":"tsc_glc24_bioclim_monthly_test = \"/kaggle/input/geolifeclef-2024/TimeSeries-Cubes/TimeSeries-Cubes/GLC24-PA-test-bioclimatic_monthly\"\ntsc_glc24_bioclim_monthly_train = \"/kaggle/input/geolifeclef-2024/TimeSeries-Cubes/TimeSeries-Cubes/GLC24-PA-train-bioclimatic_monthly\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:46:52.081797Z","iopub.execute_input":"2024-05-06T05:46:52.082488Z","iopub.status.idle":"2024-05-06T05:46:52.087041Z","shell.execute_reply.started":"2024-05-06T05:46:52.082455Z","shell.execute_reply":"2024-05-06T05:46:52.086055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tsc_glc24_bioclim_monthly_train_dir = os.listdir(tsc_glc24_bioclim_monthly_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:35:29.396369Z","iopub.execute_input":"2024-05-06T06:35:29.397033Z","iopub.status.idle":"2024-05-06T06:35:29.453852Z","shell.execute_reply.started":"2024-05-06T06:35:29.397001Z","shell.execute_reply":"2024-05-06T06:35:29.452921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for ptf_1 in tsc_glc24_bioclim_monthly_train_dir[:10]:\n    ptf_1 = tsc_glc24_bioclim_monthly_train + \"/\" + ptf_1\n    print(ptf_1)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:38:43.305369Z","iopub.execute_input":"2024-05-06T06:38:43.306350Z","iopub.status.idle":"2024-05-06T06:38:43.311437Z","shell.execute_reply.started":"2024-05-06T06:38:43.306298Z","shell.execute_reply":"2024-05-06T06:38:43.310534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tsc_glc24_landsat_monthly_train = \"/kaggle/input/geolifeclef-2024/TimeSeries-Cubes/TimeSeries-Cubes/GLC24-PA-train-landsat_time_series\"\ntsc_glc24_landsat_monthly_test = \"/kaggle/input/geolifeclef-2024/TimeSeries-Cubes/TimeSeries-Cubes/GLC24-PA-test-landsat_time_series\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:46:52.301491Z","iopub.execute_input":"2024-05-06T05:46:52.301774Z","iopub.status.idle":"2024-05-06T05:46:52.306337Z","shell.execute_reply.started":"2024-05-06T05:46:52.301751Z","shell.execute_reply":"2024-05-06T05:46:52.305313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Meta Data","metadata":{}},{"cell_type":"code","source":"glc_meta_po_train = \"/kaggle/input/geolifeclef-2024/GLC24_P0_metadata_train.csv\"\nglc_meta_pa_train = \"/kaggle/input/geolifeclef-2024/GLC24_PA_metadata_train.csv\"\nglc_meta_pa_test = \"/kaggle/input/geolifeclef-2024/GLC24_PA_metadata_test.csv\"\n","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:46:53.371871Z","iopub.execute_input":"2024-05-06T05:46:53.372206Z","iopub.status.idle":"2024-05-06T05:46:53.376752Z","shell.execute_reply.started":"2024-05-06T05:46:53.372170Z","shell.execute_reply":"2024-05-06T05:46:53.375801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"glc_meta_po_train_df = pd.read_csv(glc_meta_po_train)\nglc_meta_po_train_df","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:00:40.684117Z","iopub.execute_input":"2024-05-06T09:00:40.685105Z","iopub.status.idle":"2024-05-06T09:00:56.285220Z","shell.execute_reply.started":"2024-05-06T09:00:40.685074Z","shell.execute_reply":"2024-05-06T09:00:56.284353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"glc_meta_pa_train_df = pd.read_csv(glc_meta_pa_train)\nglc_meta_pa_train_df","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:01:46.619632Z","iopub.execute_input":"2024-05-06T09:01:46.619961Z","iopub.status.idle":"2024-05-06T09:01:49.149670Z","shell.execute_reply.started":"2024-05-06T09:01:46.619938Z","shell.execute_reply":"2024-05-06T09:01:49.148738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"glc_ss = \"/kaggle/input/geolifeclef-2024/GLC24_SAMPLE_SUBMISSION.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:46:42.097141Z","iopub.execute_input":"2024-05-06T05:46:42.097979Z","iopub.status.idle":"2024-05-06T05:46:42.101764Z","shell.execute_reply.started":"2024-05-06T05:46:42.097952Z","shell.execute_reply":"2024-05-06T05:46:42.100837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"glc_ss_df = pd.read_csv(glc_ss)\nglc_ss_df","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:03:23.125808Z","iopub.execute_input":"2024-05-06T09:03:23.127013Z","iopub.status.idle":"2024-05-06T09:03:23.153298Z","shell.execute_reply.started":"2024-05-06T09:03:23.126977Z","shell.execute_reply":"2024-05-06T09:03:23.152409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Making","metadata":{}},{"cell_type":"code","source":"!pip install einops","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:29:06.157545Z","iopub.execute_input":"2024-05-06T09:29:06.158491Z","iopub.status.idle":"2024-05-06T09:29:19.743310Z","shell.execute_reply.started":"2024-05-06T09:29:06.158447Z","shell.execute_reply":"2024-05-06T09:29:19.742149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Importing necessary libraries ","metadata":{}},{"cell_type":"code","source":"import torch\nimport tqdm\nimport torchvision.models as models\nimport torchvision.transforms as transforms\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader\nfrom torch.optim.lr_scheduler import CosineAnnealingLR\nfrom sklearn.metrics import precision_recall_fscore_support\nfrom PIL import Image\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom einops.layers.torch import Rearrange\nfrom einops import rearrange, repeat","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:30:07.417022Z","iopub.execute_input":"2024-05-06T09:30:07.417928Z","iopub.status.idle":"2024-05-06T09:30:13.850333Z","shell.execute_reply.started":"2024-05-06T09:30:07.417890Z","shell.execute_reply":"2024-05-06T09:30:13.849475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 11255","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:30:34.436712Z","iopub.execute_input":"2024-05-06T09:30:34.437622Z","iopub.status.idle":"2024-05-06T09:30:34.441533Z","shell.execute_reply.started":"2024-05-06T09:30:34.437593Z","shell.execute_reply":"2024-05-06T09:30:34.440659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestDataset(Dataset):\n    def __init__(self, metadata, subset, transform=None):\n        self.subset = subset\n        self.transform = transform\n        self.metadata = metadata\n        \n        self.metadata = self.metadata.drop_duplicates(subset=\"surveyId\").reset_index(drop=True)\n        self.metadata.fillna(0,inplace=True)\n        self.metadata.replace({float('-inf'): 0}, inplace=True)\n        self.metadata_data = self.Norm(self.metadata.iloc[:,:5])\n        \n        self.merge_key = 'surveyId'\n        self.climate_average = pd.read_csv(glc24_pa_bioclim_avg_test)\n        self.climate_monthly = pd.read_csv(glc24_pa_bioclim_monthly_test)\n        self.climate = pd.merge(self.climate_average, self.climate_monthly, on=self.merge_key)\n        self.climate.fillna(self.climate.mean(),inplace=True)\n        self.climate_data = self.Norm_all(self.climate)\n        \n        self.landsat_b = pd.read_csv(glc24_pa_ls_ts_test_blue)\n        self.landsat_b.fillna(self.landsat_b.mean(),inplace=True)\n        self.landsat_g = pd.read_csv(glc24_pa_ls_ts_test_green)\n        self.landsat_g.fillna(self.landsat_g.mean(),inplace=True)\n        self.landsat_r = pd.read_csv(glc24_pa_ls_ts_test_red)\n        self.landsat_r.fillna(self.landsat_r.mean(),inplace=True)\n        self.landsat_n = pd.read_csv(glc24_pa_ls_ts_test_nir)\n        self.landsat_n.fillna(self.landsat_n.mean(),inplace=True)\n        self.landsat_s1 = pd.read_csv(glc24_pa_ls_ts_test_swir1)\n        self.landsat_s1.fillna(self.landsat_s1.mean(),inplace=True)\n        self.landsat_s2 = pd.read_csv(glc24_pa_ls_ts_test_swir2)\n        self.landsat_s2.fillna(self.landsat_s2.mean(),inplace=True)\n        self.landsat_data = torch.cat([self.Norm_all(self.landsat_b),self.Norm_all(self.landsat_g),self.Norm_all(self.landsat_r),self.Norm_all(self.landsat_n),self.Norm_all(self.landsat_s1),self.Norm_all(self.landsat_s2)],axis=1)\n         \n        self.elevation = pd.read_csv(glc24_pa_elevation_test)\n        self.elevation[self.elevation<0]=0\n        self.elevation.fillna(self.elevation.mean(),inplace=True)\n        self.elevation_data = self.Norm(self.elevation)\n        \n        self.human_footprint = pd.read_csv(glc24_pa_humanfootprint_test)\n        self.human_footprint[self.human_footprint<0]=0\n        self.human_footprint.fillna(self.human_footprint.mean(),inplace=True)  \n        self.human_footprint_data = self.Norm(self.human_footprint)\n        \n        self.landcover = pd.read_csv(glc24_pa_landcover_test)\n        self.landcover[self.landcover<0]=0\n        self.landcover.fillna(self.landcover.mean(),inplace=True)    \n        self.landcover_data = self.Norm(self.landcover)\n        \n        self.soilgrids = pd.read_csv(glc24_pa_soilgrids_test)\n        self.soilgrids[self.soilgrids<0]=0\n        self.soilgrids.fillna(self.soilgrids.mean(),inplace=True)\n        self.soilgrids_data = self.Norm(self.soilgrids)\n        \n        self.metadata_data = torch.cat((self.metadata_data, self.elevation_data, self.human_footprint_data, self.landcover_data, self.soilgrids_data), dim=1)\n        \n    def Norm(self,df):\n        output=torch.from_numpy(df.iloc[:,1:].values).float()\n        return (output-output.mean(dim=0))/output.std(dim=0)\n    \n    def Norm_all(self,df):\n        output=torch.from_numpy(df.iloc[:,1:].values).float()\n        return (output-output.mean())/output.std()\n    \n    def patch_rgb_path(self,survey_id):\n        path = pa_test_patches_rgb\n        for d in (str(survey_id)[-2:], str(survey_id)[-4:-2]):\n            path = os.path.join(path, d)\n        path = os.path.join(path, f\"{survey_id}.jpeg\")\n        return path\n    \n    def patch_nir_path(self,survey_id):\n        path = pa_test_patches_nir\n        for d in (str(survey_id)[-2:], str(survey_id)[-4:-2]):\n            path = os.path.join(path, d)\n        path = os.path.join(path, f\"{survey_id}.jpeg\")\n        return path\n\n    def __len__(self):\n        return len(self.metadata)\n\n    def __getitem__(self, idx):\n        \n        survey_id = self.metadata.surveyId[idx]\n        \n        image_path = self.patch_rgb_path(survey_id)\n        image = Image.open(image_path).convert(\"RGB\")\n        image = self.transform(image)\n        image = image.unsqueeze(0)\n        image_nir_path = self.patch_nir_path(survey_id)\n        nir_image = Image.open(image_nir_path).convert(\"L\")\n        nir_image = self.transform(nir_image)\n        nir_image = nir_image.unsqueeze(0)\n        image_data = torch.cat([image,nir_image],dim=1)\n        image_data = torch.squeeze(image_data)\n        sample=[self.metadata_data[idx,:],image_data,self.landsat_data[idx,:],self.climate_data[idx,:]]\n        return sample, survey_id","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:48:57.136002Z","iopub.execute_input":"2024-05-06T09:48:57.136385Z","iopub.status.idle":"2024-05-06T09:48:57.164049Z","shell.execute_reply.started":"2024-05-06T09:48:57.136358Z","shell.execute_reply":"2024-05-06T09:48:57.163040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 1\ntransform = transforms.Compose([\n    transforms.Resize((128,128)),\n    transforms.ToTensor()\n])\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:48:58.381762Z","iopub.execute_input":"2024-05-06T09:48:58.382494Z","iopub.status.idle":"2024-05-06T09:48:58.387204Z","shell.execute_reply.started":"2024-05-06T09:48:58.382459Z","shell.execute_reply":"2024-05-06T09:48:58.386231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"glc_meta_pa_test_df = pd.read_csv(glc_meta_pa_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:49:04.782003Z","iopub.execute_input":"2024-05-06T09:49:04.782652Z","iopub.status.idle":"2024-05-06T09:49:04.798272Z","shell.execute_reply.started":"2024-05-06T09:49:04.782621Z","shell.execute_reply":"2024-05-06T09:49:04.797190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = TestDataset(glc_meta_pa_test_df, subset=\"test\", transform=transform)\ntest_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=1)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:49:06.141257Z","iopub.execute_input":"2024-05-06T09:49:06.141886Z","iopub.status.idle":"2024-05-06T09:49:07.430909Z","shell.execute_reply.started":"2024-05-06T09:49:06.141856Z","shell.execute_reply":"2024-05-06T09:49:07.430073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Embedding(nn.Module):\n    def __init__(self, dim, out_dim):\n        super().__init__()\n        self.fc1 = nn.Linear(dim, out_dim*5)\n        self.fc2 = nn.Linear(out_dim*5, out_dim)\n        self.norm = nn.LayerNorm(out_dim*5)\n\n    def forward(self, x):\n        x = F.tanh(self.fc1(x))\n        x = self.norm(x)\n        x = self.fc2(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:49:24.464718Z","iopub.execute_input":"2024-05-06T09:49:24.465094Z","iopub.status.idle":"2024-05-06T09:49:24.471432Z","shell.execute_reply.started":"2024-05-06T09:49:24.465067Z","shell.execute_reply":"2024-05-06T09:49:24.470557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Multihead_self_attention(nn.Module):\n    def __init__(self, heads, head_dim, dim):\n        super().__init__()\n        self.head_dim = head_dim\n        self.heads = heads \n        self.inner_dim = self.heads*self.head_dim  \n        self.scale = self.head_dim**-0.5   \n        self.to_qkv = nn.Linear(dim, self.inner_dim*3) \n        self.to_output = nn.Linear(self.inner_dim, dim)\n        self.norm = nn.LayerNorm(dim)\n        self.softmax = nn.Softmax(dim=-1)\n\n    def forward(self, x):\n        x = self.norm(x)    \n        qkv = self.to_qkv(x).chunk(3, dim=-1) \n        Q, K, V = map(lambda t: rearrange(t, 'b l (h dim) -> b h l dim', dim=self.head_dim), qkv)\n        K_T = K.transpose(-1, -2)\n        att_score = Q@K_T*self.scale\n        att = self.softmax(att_score)\n        out = att@V   # (B,H,L,dim)\n        out = rearrange(out, 'b h l dim -> b l (h dim)')  \n        output = self.to_output(out)\n        return output","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:49:56.133085Z","iopub.execute_input":"2024-05-06T09:49:56.133460Z","iopub.status.idle":"2024-05-06T09:49:56.143813Z","shell.execute_reply.started":"2024-05-06T09:49:56.133435Z","shell.execute_reply":"2024-05-06T09:49:56.142908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class FeedForward(nn.Module):\n    def __init__(self, dim, mlp_dim):\n        super().__init__()\n        self.fc1 = nn.Linear(dim, mlp_dim)\n        self.fc2 = nn.Linear(mlp_dim, dim)\n        self.norm = nn.LayerNorm(dim)\n\n    def forward(self, x):\n        x = self.norm(x)\n        x = F.gelu(self.fc1(x))\n        x = self.fc2(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:50:08.451972Z","iopub.execute_input":"2024-05-06T09:50:08.452348Z","iopub.status.idle":"2024-05-06T09:50:08.458555Z","shell.execute_reply.started":"2024-05-06T09:50:08.452320Z","shell.execute_reply":"2024-05-06T09:50:08.457606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Transformer_block(nn.Module):\n    def __init__(self, dim, heads, head_dim, mlp_dim):\n        super().__init__()\n        self.MHA = Multihead_self_attention(heads=heads, head_dim=head_dim, dim=dim)\n        self.FeedForward = FeedForward(dim=dim, mlp_dim=mlp_dim)\n\n    def forward(self, x):\n        x = self.MHA(x)+x\n        x = self.FeedForward(x)+x\n        return x","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:50:25.513171Z","iopub.execute_input":"2024-05-06T09:50:25.513925Z","iopub.status.idle":"2024-05-06T09:50:25.519781Z","shell.execute_reply.started":"2024-05-06T09:50:25.513891Z","shell.execute_reply":"2024-05-06T09:50:25.518836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ViT(nn.Module):\n    def __init__(self, dim, heads, head_dim, mlp_dim, num_class):\n        super().__init__()\n        self.transformer = Transformer_block(dim=dim, heads=heads, head_dim=head_dim, mlp_dim=mlp_dim)\n\n        self.MLP_head = nn.Sequential(\n            nn.LayerNorm(dim),\n            nn.Linear(dim, num_class)\n        )\n        self.sigmoid = nn.Sigmoid()\n\n    def forward(self, x):\n        x = self.transformer(x)\n        CLS_token = x[:, 0, :] \n        out = self.MLP_head(CLS_token)\n        return out","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:50:38.791881Z","iopub.execute_input":"2024-05-06T09:50:38.792250Z","iopub.status.idle":"2024-05-06T09:50:38.798933Z","shell.execute_reply.started":"2024-05-06T09:50:38.792222Z","shell.execute_reply":"2024-05-06T09:50:38.797993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResNet18(nn.Module):\n    def __init__(self, num_classes):\n        super(ResNet18, self).__init__()\n\n        self.resnet18 = models.resnet18(weights=None)\n        self.resnet18.conv1 = nn.Conv2d(4, 64, kernel_size=3, stride=1, padding=1, bias=False)\n        self.resnet18.maxpool = nn.Identity()\n        self.ln = nn.LayerNorm(1000)\n        self.fc1 = nn.Linear(1000, num_classes)\n\n    def forward(self, x):\n        x = self.resnet18(x)\n        x = self.ln(x)\n        x = self.fc1(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:50:56.737109Z","iopub.execute_input":"2024-05-06T09:50:56.737488Z","iopub.status.idle":"2024-05-06T09:50:56.744557Z","shell.execute_reply.started":"2024-05-06T09:50:56.737461Z","shell.execute_reply":"2024-05-06T09:50:56.743614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MutiModal(nn.Module):\n    def __init__(self, num_classes):\n        super(MutiModal, self).__init__()\n        self.cls = nn.Parameter(torch.randn(1, 1, 200))\n        self.meta = Embedding(31,200)\n        self.resnet18 = ResNet18(200)\n        self.landsat = Embedding(504,200)\n        self.position_landsat = nn.Parameter(torch.randn(1, 504))\n        self.climate = Embedding(931,200)\n        self.position_climate = nn.Parameter(torch.randn(1, 931))\n        self.emb = Embedding(800,200)\n        self.position_combine = nn.Parameter(torch.randn(1, 800))\n        self.vit = ViT(200, 2, 200, 400, num_classes)\n        self.position = nn.Parameter(torch.randn(1, 6, 200))\n\n    def forward(self, x):\n        batch = x[0].size(0)\n        CLS = repeat(self.cls, '1 1 d -> b 1 d', b=batch).to(device)\n        META = self.meta(x[0])\n        IMG = self.resnet18(x[1])\n        LANDSAT = self.landsat(x[2]+self.position_landsat)\n        CLIMATE = self.climate(x[3]+self.position_climate)\n        combine = torch.cat((META, IMG, LANDSAT, CLIMATE), dim=1)\n        COMBINE = self.emb(combine+self.position_combine)\n        token = torch.concat((CLS, META.unsqueeze(1)), dim=1)\n        token = torch.concat((token, IMG.unsqueeze(1)), dim=1)\n        token = torch.concat((token, LANDSAT.unsqueeze(1)), dim=1)\n        token = torch.concat((token, CLIMATE.unsqueeze(1)), dim=1)\n        token = torch.concat((token, COMBINE.unsqueeze(1)), dim=1)\n        out = self.vit(token+self.position)\n        return out","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:51:18.711557Z","iopub.execute_input":"2024-05-06T09:51:18.712185Z","iopub.status.idle":"2024-05-06T09:51:18.724716Z","shell.execute_reply.started":"2024-05-06T09:51:18.712154Z","shell.execute_reply":"2024-05-06T09:51:18.723849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check if cuda is available\ndevice = torch.device(\"cpu\")\n\nif torch.cuda.is_available():\n    device = torch.device(\"cuda\")\n    print(\"DEVICE = CUDA\")\n\nmodel = MutiModal(num_classes).to(device)\nmodel.load_state_dict(torch.load(\"/kaggle/input/mutimodal-vit/pytorch/mvit/1/Model.pth\", map_location=device))","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:54:55.144288Z","iopub.execute_input":"2024-05-06T09:54:55.144695Z","iopub.status.idle":"2024-05-06T09:54:56.263800Z","shell.execute_reply.started":"2024-05-06T09:54:55.144668Z","shell.execute_reply":"2024-05-06T09:54:56.262864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    surveys = []\n    top_indices = []\n    for data, surveyID in tqdm.tqdm(test_loader, total=len(test_loader)):\n\n        data = [tensor.to(device) for tensor in data]\n        \n        outputs = model(data)\n        predictions = torch.sigmoid(outputs).cpu().numpy()\n        predictions = np.squeeze(predictions)\n        prediction = np.argwhere(predictions>=0.95).flatten()\n        top_indices.append(prediction)\n        surveys.extend(surveyID.cpu().numpy())","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:55:43.901805Z","iopub.execute_input":"2024-05-06T09:55:43.902189Z","iopub.status.idle":"2024-05-06T09:57:12.852122Z","shell.execute_reply.started":"2024-05-06T09:55:43.902159Z","shell.execute_reply":"2024-05-06T09:57:12.851027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_concatenated = [' '.join(map(str, row)) for row in top_indices]\n\npd.DataFrame(\n    \n    {'surveyId': surveys,\n     'predictions': data_concatenated,\n    }).to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T09:57:45.112941Z","iopub.execute_input":"2024-05-06T09:57:45.113373Z","iopub.status.idle":"2024-05-06T09:57:45.218127Z","shell.execute_reply.started":"2024-05-06T09:57:45.113336Z","shell.execute_reply":"2024-05-06T09:57:45.217409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}