Spaces:
Sleeping
Sleeping
Commit
·
0e65489
1
Parent(s):
6063721
Created dataloader class
Browse files
data/download.py → datasets/downloader.py
RENAMED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# Script to automatically download and cache dataset
|
| 2 |
-
# Usage: python
|
| 3 |
#
|
| 4 |
# To learn more about the dataset, access:
|
| 5 |
# https://www.cityscapes-dataset.com/
|
|
@@ -13,7 +13,7 @@ def main():
|
|
| 13 |
pass
|
| 14 |
|
| 15 |
|
| 16 |
-
def download(name='cityscapes', path='
|
| 17 |
"""Select one of the available and implemented datasets to download:
|
| 18 |
name=any(['cityscapes', 'camvid', 'labelme'])
|
| 19 |
"""
|
|
@@ -23,11 +23,19 @@ def download(name='cityscapes', path='data/downloads'):
|
|
| 23 |
raise NotImplementedError
|
| 24 |
|
| 25 |
|
| 26 |
-
def download_cityscapes(path='
|
| 27 |
if hasattr(pip, 'main'):
|
| 28 |
-
pip.main(['install', '
|
| 29 |
else:
|
| 30 |
raise EnvironmentError("pip is not installed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
|
|
|
| 1 |
# Script to automatically download and cache dataset
|
| 2 |
+
# Usage: python downloader.py
|
| 3 |
#
|
| 4 |
# To learn more about the dataset, access:
|
| 5 |
# https://www.cityscapes-dataset.com/
|
|
|
|
| 13 |
pass
|
| 14 |
|
| 15 |
|
| 16 |
+
def download(name='cityscapes', path='datasets/downloads'):
|
| 17 |
"""Select one of the available and implemented datasets to download:
|
| 18 |
name=any(['cityscapes', 'camvid', 'labelme'])
|
| 19 |
"""
|
|
|
|
| 23 |
raise NotImplementedError
|
| 24 |
|
| 25 |
|
| 26 |
+
def download_cityscapes(path='datasets/downloads'):
|
| 27 |
if hasattr(pip, 'main'):
|
| 28 |
+
pip.main(['install', 'cityscapesscripts'])
|
| 29 |
else:
|
| 30 |
raise EnvironmentError("pip is not installed")
|
| 31 |
+
print("Which dataset do you want to download?")
|
| 32 |
+
os.system("csDownload -l")
|
| 33 |
+
ds_name = input()
|
| 34 |
+
while ds_name not in ['gtFine_trainvaltest', 'gtFine_trainval', 'gtFine_test',
|
| 35 |
+
'leftImg8bit_trainvaltest', 'leftImg8bit_trainval', 'leftImg8bit_test']:
|
| 36 |
+
print("Invalid dataset name. Please try again.")
|
| 37 |
+
ds_name = input()
|
| 38 |
+
os.system(f"csDownload {ds_name} -d {path}/{ds_name}")
|
| 39 |
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
notebooks/dataloader.ipynb
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"metadata": {
|
| 7 |
+
"collapsed": true,
|
| 8 |
+
"pycharm": {
|
| 9 |
+
"name": "#%%\n"
|
| 10 |
+
}
|
| 11 |
+
},
|
| 12 |
+
"outputs": [
|
| 13 |
+
{
|
| 14 |
+
"name": "stdout",
|
| 15 |
+
"output_type": "stream",
|
| 16 |
+
"text": [
|
| 17 |
+
"/mnt/c/Users/rzimm/Workspace/data/zero-to-hero\n"
|
| 18 |
+
]
|
| 19 |
+
}
|
| 20 |
+
],
|
| 21 |
+
"source": [
|
| 22 |
+
"%cd ..\n",
|
| 23 |
+
"%load_ext autoreload\n",
|
| 24 |
+
"%autoreload 2\n",
|
| 25 |
+
"from datasets import downloader"
|
| 26 |
+
]
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"cell_type": "code",
|
| 30 |
+
"execution_count": 56,
|
| 31 |
+
"outputs": [],
|
| 32 |
+
"source": [
|
| 33 |
+
"import pandas as pd\n",
|
| 34 |
+
"import numpy as np\n",
|
| 35 |
+
"import random\n",
|
| 36 |
+
"from glob import glob, escape\n",
|
| 37 |
+
"import imageio.v2 as imageio"
|
| 38 |
+
],
|
| 39 |
+
"metadata": {
|
| 40 |
+
"collapsed": false,
|
| 41 |
+
"pycharm": {
|
| 42 |
+
"name": "#%%\n"
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
"cell_type": "code",
|
| 48 |
+
"execution_count": null,
|
| 49 |
+
"outputs": [],
|
| 50 |
+
"source": [
|
| 51 |
+
"# download.download(\"cityscapes\", \"datasets/downloaded\")"
|
| 52 |
+
],
|
| 53 |
+
"metadata": {
|
| 54 |
+
"collapsed": false,
|
| 55 |
+
"pycharm": {
|
| 56 |
+
"name": "#%%\n",
|
| 57 |
+
"is_executing": true
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
"cell_type": "code",
|
| 63 |
+
"execution_count": 41,
|
| 64 |
+
"outputs": [],
|
| 65 |
+
"source": [
|
| 66 |
+
"def load_dataset(name=\"gtFine\", path=\"datasets/downloads/\"):\n",
|
| 67 |
+
" src = path+name\n",
|
| 68 |
+
" test, train, val = [f\"{src}/{subpath}\" for subpath in [\"test\", \"train\", \"val\"]]\n",
|
| 69 |
+
"\n",
|
| 70 |
+
" dataset = {\"test\": glob(test + \"/*/*\"), \"train\": glob(train + \"/*/*\"), \"val\": glob(val + \"/*/*\")}\n",
|
| 71 |
+
"\n",
|
| 72 |
+
" return dataset"
|
| 73 |
+
],
|
| 74 |
+
"metadata": {
|
| 75 |
+
"collapsed": false,
|
| 76 |
+
"pycharm": {
|
| 77 |
+
"name": "#%%\n"
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"cell_type": "code",
|
| 83 |
+
"execution_count": 44,
|
| 84 |
+
"outputs": [
|
| 85 |
+
{
|
| 86 |
+
"data": {
|
| 87 |
+
"text/plain": "list"
|
| 88 |
+
},
|
| 89 |
+
"execution_count": 44,
|
| 90 |
+
"metadata": {},
|
| 91 |
+
"output_type": "execute_result"
|
| 92 |
+
}
|
| 93 |
+
],
|
| 94 |
+
"source": [
|
| 95 |
+
"type(load_dataset()[\"train\"])"
|
| 96 |
+
],
|
| 97 |
+
"metadata": {
|
| 98 |
+
"collapsed": false,
|
| 99 |
+
"pycharm": {
|
| 100 |
+
"name": "#%%\n"
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
},
|
| 104 |
+
{
|
| 105 |
+
"cell_type": "code",
|
| 106 |
+
"execution_count": 45,
|
| 107 |
+
"outputs": [],
|
| 108 |
+
"source": [
|
| 109 |
+
"a = [1, 2, 3]"
|
| 110 |
+
],
|
| 111 |
+
"metadata": {
|
| 112 |
+
"collapsed": false,
|
| 113 |
+
"pycharm": {
|
| 114 |
+
"name": "#%%\n"
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
+
},
|
| 118 |
+
{
|
| 119 |
+
"cell_type": "code",
|
| 120 |
+
"execution_count": 143,
|
| 121 |
+
"outputs": [],
|
| 122 |
+
"source": [
|
| 123 |
+
"class DataLoader:\n",
|
| 124 |
+
" def __init__(self, data):\n",
|
| 125 |
+
" self.data = np.array(data)\n",
|
| 126 |
+
" self.total = len(self.data)\n",
|
| 127 |
+
" self.__items = self.data\n",
|
| 128 |
+
" self.__remaining = len(self.data)\n",
|
| 129 |
+
" def __next__(self, n=1):\n",
|
| 130 |
+
" if n > self.total:\n",
|
| 131 |
+
" raise ValueError(f\"Dataset doesn't have enough elements to suffice request of {n} elements.\")\n",
|
| 132 |
+
" if self.__remaining > 0:\n",
|
| 133 |
+
" indices = random.sample(range(self.__remaining), n)\n",
|
| 134 |
+
" sampled = self.__items[indices]\n",
|
| 135 |
+
" self.__items = np.delete(self.__items, indices)\n",
|
| 136 |
+
" self.__remaining -= n\n",
|
| 137 |
+
" return sampled\n",
|
| 138 |
+
" else:\n",
|
| 139 |
+
" self.__items = self.data\n",
|
| 140 |
+
" self.__remaining = len(self.data)\n",
|
| 141 |
+
" return self.__next__(n)"
|
| 142 |
+
],
|
| 143 |
+
"metadata": {
|
| 144 |
+
"collapsed": false,
|
| 145 |
+
"pycharm": {
|
| 146 |
+
"name": "#%%\n"
|
| 147 |
+
}
|
| 148 |
+
}
|
| 149 |
+
},
|
| 150 |
+
{
|
| 151 |
+
"cell_type": "code",
|
| 152 |
+
"execution_count": 144,
|
| 153 |
+
"outputs": [],
|
| 154 |
+
"source": [
|
| 155 |
+
"loader = DataLoader(a)"
|
| 156 |
+
],
|
| 157 |
+
"metadata": {
|
| 158 |
+
"collapsed": false,
|
| 159 |
+
"pycharm": {
|
| 160 |
+
"name": "#%%\n"
|
| 161 |
+
}
|
| 162 |
+
}
|
| 163 |
+
},
|
| 164 |
+
{
|
| 165 |
+
"cell_type": "code",
|
| 166 |
+
"execution_count": null,
|
| 167 |
+
"outputs": [],
|
| 168 |
+
"source": [],
|
| 169 |
+
"metadata": {
|
| 170 |
+
"collapsed": false,
|
| 171 |
+
"pycharm": {
|
| 172 |
+
"name": "#%%\n"
|
| 173 |
+
}
|
| 174 |
+
}
|
| 175 |
+
}
|
| 176 |
+
],
|
| 177 |
+
"metadata": {
|
| 178 |
+
"kernelspec": {
|
| 179 |
+
"display_name": "Python 3",
|
| 180 |
+
"language": "python",
|
| 181 |
+
"name": "python3"
|
| 182 |
+
},
|
| 183 |
+
"language_info": {
|
| 184 |
+
"codemirror_mode": {
|
| 185 |
+
"name": "ipython",
|
| 186 |
+
"version": 2
|
| 187 |
+
},
|
| 188 |
+
"file_extension": ".py",
|
| 189 |
+
"mimetype": "text/x-python",
|
| 190 |
+
"name": "python",
|
| 191 |
+
"nbconvert_exporter": "python",
|
| 192 |
+
"pygments_lexer": "ipython2",
|
| 193 |
+
"version": "2.7.6"
|
| 194 |
+
}
|
| 195 |
+
},
|
| 196 |
+
"nbformat": 4,
|
| 197 |
+
"nbformat_minor": 0
|
| 198 |
+
}
|