{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Load Libraries","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport cv2, os\nfrom tqdm import tqdm\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"../input/landmark-retrieval-2020/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Define a function that can read all paths.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_paths(sub):\n    index = [\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"a\",\"b\",\"c\",\"d\",\"e\",\"f\"]\n\n    paths = []\n\n    for a in index:\n        for b in index:\n            for c in index:\n                try:\n                    paths.extend([f\"../input/landmark-retrieval-2020/{sub}/{a}/{b}/{c}/\" + x for x in os.listdir(f\"../input/landmark-retrieval-2020/{sub}/{a}/{b}/{c}\")])\n                except:\n                    pass\n\n    return paths","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Get the training paths","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_path = train\n\nrows = []\nfor i in tqdm(range(len(train))):\n    row = train.iloc[i]\n    path  = list(row[\"id\"])[:3]\n    temp = row[\"id\"]\n    row[\"id\"] = f\"../input/landmark-retrieval-2020/train/{path[0]}/{path[1]}/{path[2]}/{temp}.jpg\"\n    rows.append(row[\"id\"])\n    \nrows = pd.DataFrame(rows)\ntrain_path[\"id\"] = rows","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Save paths without KFold**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_path.to_csv(\"train_paths.csv\",index = False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Apply KFold with 10 Folds","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import KFold\n\nkf = KFold(n_splits=10,shuffle = True,random_state = 15)\nkf.get_n_splits(train_path)\n\nfolds = np.zeros(len(train_path))\n\nfor fold, (train_index, test_index) in enumerate(kf.split(train_path)):\n    folds[list(train_index)] = fold\n    \ntrain_path[\"Fold\"] = folds.astype(int)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Save it.**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_path.to_csv(\"train_paths_KFold.csv\",index = False)\npd.DataFrame(get_paths(\"test\")).to_csv(\"test_paths.csv\",index = False)\npd.DataFrame(get_paths(\"index\")).to_csv(\"index_paths.csv\",index = False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}