{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from pathlib import Path\nimport numpy as np\nfrom PIL import Image\nimport random\nrandom.seed(42)\n\nSSDDIR = Path('./ssddir-traffic-signs')\nSSDDIR.mkdir(exist_ok=True, parents=True)\n\nN_TRAIN = 30\nN_VALID = 10\n\nIMAGES = Path('../input/acc-german-traffic-sign-classification/GTSRB_Challenge/train')\nlabel_folders = sorted(list(IMAGES.glob(\"*\")))\nfor ldir in label_folders:\n    images = sorted(list(ldir.glob('*.ppm')))\n    random.shuffle(images)\n    print('writing', ldir.name, 'with', len(images), 'images...')\n    \n    train = SSDDIR/'train'/ldir.name\n    valid = SSDDIR/'valid'/ldir.name\n    test = SSDDIR/'test'/ldir.name\n    \n    train.mkdir(exist_ok=True, parents=True)    \n    valid.mkdir(exist_ok=True, parents=True)    \n    test.mkdir(exist_ok=True, parents=True)    \n    \n    for i, impth in enumerate(images):\n        split = test\n        if (i>=0) and (i<N_TRAIN):\n            split=train\n        elif (i>=N_TRAIN) and (i<(N_TRAIN+N_VALID)):\n            split=valid\n            \n        im = Image.open(str(impth))\n        im.save(str(split / f\"{impth.stem}.jpg\"))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-07T10:38:33.712893Z","iopub.execute_input":"2022-04-07T10:38:33.713267Z","iopub.status.idle":"2022-04-07T10:41:37.624494Z","shell.execute_reply.started":"2022-04-07T10:38:33.713173Z","shell.execute_reply":"2022-04-07T10:41:37.623687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = { 0:'Speed limit (20km/h)',\n            1:'Speed limit (30km/h)', \n            2:'Speed limit (50km/h)', \n            3:'Speed limit (60km/h)', \n            4:'Speed limit (70km/h)', \n            5:'Speed limit (80km/h)', \n            6:'End of speed limit (80km/h)', \n            7:'Speed limit (100km/h)', \n            8:'Speed limit (120km/h)', \n            9:'No passing', \n            10:'No passing veh over 3.5 tons', \n            11:'Right-of-way at intersection', \n            12:'Priority road', \n            13:'Yield', \n            14:'Stop', \n            15:'No vehicles', \n            16:'Veh > 3.5 tons prohibited', \n            17:'No entry', \n            18:'General caution', \n            19:'Dangerous curve left', \n            20:'Dangerous curve right', \n            21:'Double curve', \n            22:'Bumpy road', \n            23:'Slippery road', \n            24:'Road narrows on the right', \n            25:'Road work', \n            26:'Traffic signals', \n            27:'Pedestrians', \n            28:'Children crossing', \n            29:'Bicycles crossing', \n            30:'Beware of ice/snow',\n            31:'Wild animals crossing', \n            32:'End speed + passing limits', \n            33:'Turn right ahead', \n            34:'Turn left ahead', \n            35:'Ahead only', \n            36:'Go straight or right', \n            37:'Go straight or left', \n            38:'Keep right', \n            39:'Keep left', \n            40:'Roundabout mandatory', \n            41:'End of no passing', \n            42:'End no passing veh > 3.5 tons' }\n\nimport json\nwith open(SSDDIR/'label_map.json', 'w') as fp:\n    json.dump(classes, fp, indent=4)","metadata":{"execution":{"iopub.status.busy":"2022-04-07T11:08:26.337521Z","iopub.execute_input":"2022-04-07T11:08:26.337877Z","iopub.status.idle":"2022-04-07T11:08:26.350719Z","shell.execute_reply.started":"2022-04-07T11:08:26.337842Z","shell.execute_reply":"2022-04-07T11:08:26.349552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !rm -rf {SSDDIR}\n!tree {SSDDIR} --filelimit 4","metadata":{"execution":{"iopub.status.busy":"2022-04-07T10:43:35.769978Z","iopub.execute_input":"2022-04-07T10:43:35.770847Z","iopub.status.idle":"2022-04-07T10:43:36.529435Z","shell.execute_reply.started":"2022-04-07T10:43:35.770803Z","shell.execute_reply":"2022-04-07T10:43:36.528414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport random\nrandom.seed(42)\nplt.style.use('fivethirtyeight')\n\nSPLIT_NAME='train'\ncounts = dict()\nfig, axs = plt.subplots(43, 10, figsize=(20, 100)) \n# axs = axs.flatten()\nfor split in SSDDIR.glob('*'):\n    if split.name not in counts:\n        if \".\" in split.name:\n            continue\n        counts[split.name] = dict(label_names=[], counts=[])\n        \n    for ldir in split.glob('*'):\n        images = sorted(list(ldir.glob(\"*.jpg\")))           \n        if split.name not in counts:\n            continue\n        \n        counts[split.name]['label_names'].append(ldir.name)\n        counts[split.name]['counts'].append(len(images))\n        \n        if split.name!=SPLIT_NAME:\n            continue\n        lid = int(ldir.stem)\n        for j in range(10):\n            rid = random.randint(0, len(images)-1)\n            im = Image.open(images[rid])\n            axs[lid,j].imshow(im)\n            axs[lid,j].set_title(f\"[{rid}]{ldir.name}\")\n            axs[lid, j].axis('off')\n\nplt.title(f\"TRAIN_DATA ({N_TRAIN} samples)\")\nplt.savefig(str(SSDDIR/'preview.jpg'))\nplt.show()\n\n#### PLOT DISTRIBUTION\nfor split_name, distr in counts.items():\n    xs = counts[split_name]['label_names']\n    ys = counts[split_name]['counts']\n    xs, ys = np.array(xs), np.array(ys)\n    idxs = np.argsort(ys)[::-1]\n    xs = xs[idxs]\n    ys = ys[idxs]\n\n    plt.figure(figsize=(15, 5))\n    plt.bar(xs, ys)\n    plt.title(split_name)\n    plt.xticks(rotation=90)\n    plt.savefig(str(SSDDIR/f'distr_{split_name}.jpg'))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T11:27:03.485261Z","iopub.execute_input":"2022-04-07T11:27:03.485565Z","iopub.status.idle":"2022-04-07T11:27:27.686351Z","shell.execute_reply.started":"2022-04-07T11:27:03.485532Z","shell.execute_reply":"2022-04-07T11:27:27.685366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport random\nrandom.seed(42)\nplt.style.use('fivethirtyeight')\n\nSPLIT_NAME='valid'\ncounts = dict()\nfig, axs = plt.subplots(43, 10, figsize=(20, 100)) \n# axs = axs.flatten()\nfor split in SSDDIR.glob('*'):\n    if split.name not in counts:\n        if \".\" in split.name:\n            continue\n        counts[split.name] = dict(label_names=[], counts=[])\n        \n    for ldir in split.glob('*'):\n        images = sorted(list(ldir.glob(\"*.jpg\")))           \n        if split.name not in counts:\n            continue\n        \n        counts[split.name]['label_names'].append(ldir.name)\n        counts[split.name]['counts'].append(len(images))\n        \n        if split.name!=SPLIT_NAME:\n            continue\n        lid = int(ldir.stem)\n        for j in range(10):\n            rid = j # random.randint(0, len(images)-1)\n            im = Image.open(images[rid])\n            axs[lid,j].imshow(im)\n            axs[lid,j].set_title(f\"[{rid}]{ldir.name}\")\n            axs[lid, j].axis('off')\n\nplt.savefig(str(SSDDIR/f'preview_{SPLIT_NAME}.jpg'))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T11:25:56.258315Z","iopub.execute_input":"2022-04-07T11:25:56.258596Z","iopub.status.idle":"2022-04-07T11:26:18.583696Z","shell.execute_reply.started":"2022-04-07T11:25:56.258569Z","shell.execute_reply":"2022-04-07T11:26:18.581763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport random\nrandom.seed(42)\nplt.style.use('fivethirtyeight')\n\nSPLIT_NAME='test'\ncounts = dict()\nfig, axs = plt.subplots(43, 10, figsize=(20, 100)) \n# axs = axs.flatten()\nfor split in SSDDIR.glob('*'):\n    if split.name not in counts:\n        if \".\" in split.name:\n            continue\n        counts[split.name] = dict(label_names=[], counts=[])\n        \n    for ldir in split.glob('*'):\n        images = sorted(list(ldir.glob(\"*.jpg\")))           \n        if split.name not in counts:\n            continue\n        \n        counts[split.name]['label_names'].append(ldir.name)\n        counts[split.name]['counts'].append(len(images))\n        \n        if split.name!=SPLIT_NAME:\n            continue\n        lid = int(ldir.stem)\n        for j in range(10):\n            rid = random.randint(0, len(images)-1)\n            im = Image.open(images[rid])\n            axs[lid,j].imshow(im)\n            axs[lid,j].set_title(f\"[{rid}]{ldir.name}\")\n            axs[lid,j].axis('off')\n\nplt.savefig(str(SSDDIR/f'preview_{SPLIT_NAME}.jpg'))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T11:25:16.180685Z","iopub.execute_input":"2022-04-07T11:25:16.181007Z","iopub.status.idle":"2022-04-07T11:25:39.275646Z","shell.execute_reply.started":"2022-04-07T11:25:16.180972Z","shell.execute_reply":"2022-04-07T11:25:39.271619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r ssddir-traffic-sign.zip {SSDDIR}","metadata":{"execution":{"iopub.status.idle":"2022-04-07T11:29:02.086940Z","shell.execute_reply.started":"2022-04-07T11:28:53.946901Z","shell.execute_reply":"2022-04-07T11:29:02.085893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}