Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codestyle and Good Practices #50

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@

# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# End of https://www.toptal.com/developers/gitignore/api/python
23 changes: 13 additions & 10 deletions cityscapes_gt_converter/cityscapes_panoptic_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os, sys
import json
import os
import glob
import numpy as np
import PIL.Image as Image
Expand All @@ -14,16 +13,18 @@
try:
# set up path for cityscapes scripts
# sys.path.append('./cityscapesScripts/')
from cityscapesscripts.helpers.labels import labels, id2label
from cityscapesscripts.helpers.labels import labels
except Exception:
raise Exception("Please load Cityscapes scripts from https://github.com/mcordts/cityscapesScripts")
raise Exception(
"Please load Cityscapes scripts from https://github.com/mcordts/cityscapesScripts")

original_format_folder = './gtFine/val/'
# folder to store panoptic PNGs
out_folder = './cityscapes_data/cityscapes_panoptic_val/'
# json with segmentations information
out_file = './cityscapes_data/cityscapes_panoptic_val.json'


def panoptic_converter(original_format_folder, out_folder, out_file):

if not os.path.isdir(out_folder):
Expand All @@ -42,7 +43,8 @@ def panoptic_converter(original_format_folder, out_folder, out_file):

categories_dict = {cat['id']: cat for cat in categories}

file_list = sorted(glob.glob(os.path.join(original_format_folder, '*/*_gtFine_instanceIds.png')))
file_list = sorted(glob.glob(os.path.join(
original_format_folder, '*/*_gtFine_instanceIds.png')))

images = []
annotations = []
Expand All @@ -54,17 +56,17 @@ def panoptic_converter(original_format_folder, out_folder, out_file):

file_name = f.split('/')[-1]
image_id = file_name.rsplit('_', 2)[0]
image_filename= '{}_leftImg8bit.png'.format(image_id)
image_filename = '{}_leftImg8bit.png'.format(image_id)
# image entry, id for image is its filename without extension
images.append({"id": image_id,
"width": original_format.shape[1],
"height": original_format.shape[0],
"file_name": image_filename})

pan_format = np.zeros((original_format.shape[0], original_format.shape[1], 3), dtype=np.uint8)
pan_format = np.zeros(
(original_format.shape[0], original_format.shape[1], 3), dtype=np.uint8)
id_generator = IdGenerator(categories_dict)

idx = 0
l = np.unique(original_format)
segm_info = []
for el in l:
Expand All @@ -82,7 +84,7 @@ def panoptic_converter(original_format_folder, out_folder, out_file):
segment_id, color = id_generator.get_id_and_color(semantic_id)
pan_format[mask] = color

area = np.sum(mask) # segment area computation
area = np.sum(mask) # segment area computation

# bbox computation for a segment
hor = np.sum(mask, axis=0)
Expand Down Expand Up @@ -110,9 +112,10 @@ def panoptic_converter(original_format_folder, out_folder, out_file):
d = {'images': images,
'annotations': annotations,
'categories': categories,
}
}

save_json(d, out_file)


if __name__ == "__main__":
panoptic_converter(original_format_folder, out_folder, out_file)
25 changes: 16 additions & 9 deletions converters/2channels2panoptic_coco_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,39 @@
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os, sys
import os
import argparse
import numpy as np
import json
import time
import multiprocessing
import itertools

import PIL.Image as Image

from panopticapi.utils import get_traceback, IdGenerator, save_json

OFFSET = 1000


@get_traceback
def convert_single_core(proc_id, image_set, categories, source_folder, segmentations_folder, VOID=0):
annotations = []
for working_idx, image_info in enumerate(image_set):
if working_idx % 100 == 0:
print('Core: {}, {} from {} images converted'.format(proc_id, working_idx, len(image_set)))
print('Core: {}, {} from {} images converted'.format(
proc_id, working_idx, len(image_set)))

file_name = '{}.png'.format(image_info['file_name'].rsplit('.')[0])
try:
original_format = np.array(Image.open(os.path.join(source_folder, file_name)), dtype=np.uint32)
original_format = np.array(Image.open(
os.path.join(source_folder, file_name)), dtype=np.uint32)
except IOError:
raise KeyError('no prediction png file for id: {}'.format(image_info['id']))
raise KeyError(
'no prediction png file for id: {}'.format(image_info['id']))

pan = OFFSET * original_format[:, :, 0] + original_format[:, :, 1]
pan_format = np.zeros((original_format.shape[0], original_format.shape[1], 3), dtype=np.uint8)
pan_format = np.zeros(
(original_format.shape[0], original_format.shape[1], 3), dtype=np.uint8)

id_generator = IdGenerator(categories)

Expand All @@ -70,7 +74,8 @@ def convert_single_core(proc_id, image_set, categories, source_folder, segmentat
'file_name': file_name,
"segments_info": segm_info})

Image.fromarray(pan_format).save(os.path.join(segmentations_folder, file_name))
Image.fromarray(pan_format).save(
os.path.join(segmentations_folder, file_name))
print('Core: {}, all {} images processed'.format(proc_id, len(image_set)))
return annotations

Expand All @@ -93,7 +98,8 @@ def converter(source_folder, images_json_file, categories_json_file,
if segmentations_folder is None:
segmentations_folder = predictions_json_file.rsplit('.', 1)[0]
if not os.path.isdir(segmentations_folder):
print("Creating folder {} for panoptic segmentation PNGs".format(segmentations_folder))
print("Creating folder {} for panoptic segmentation PNGs".format(
segmentations_folder))
os.mkdir(segmentations_folder)

print("CONVERTING...")
Expand All @@ -106,7 +112,8 @@ def converter(source_folder, images_json_file, categories_json_file,
print('\n')
cpu_num = multiprocessing.cpu_count()
images_split = np.array_split(images, cpu_num)
print("Number of cores: {}, images per core: {}".format(cpu_num, len(images_split[0])))
print("Number of cores: {}, images per core: {}".format(
cpu_num, len(images_split[0])))
workers = multiprocessing.Pool(processes=cpu_num)
processes = []
for proc_id, image_set in enumerate(images_split):
Expand Down
24 changes: 15 additions & 9 deletions converters/detection2panoptic_coco_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os, sys
import os
import argparse
import numpy as np
import json
Expand All @@ -22,10 +22,11 @@
try:
# set up path for pycocotools
# sys.path.append('./cocoapi-master/PythonAPI/')
from pycocotools import mask as COCOmask
from pycocotools.coco import COCO as COCO
except Exception:
raise Exception("Please install pycocotools module from https://github.com/cocodataset/cocoapi")
raise Exception(
"Please install pycocotools module from https://github.com/cocodataset/cocoapi")


@get_traceback
def convert_detection_to_panoptic_coco_format_single_core(
Expand Down Expand Up @@ -55,8 +56,9 @@ def convert_detection_to_panoptic_coco_format_single_core(
if ann['category_id'] not in categories:
raise Exception('Panoptic coco categories file does not contain \
category with id: {}'.format(ann['category_id'])
)
segment_id, color = id_generator.get_id_and_color(ann['category_id'])
)
segment_id, color = id_generator.get_id_and_color(
ann['category_id'])
mask = coco_detection.annToMask(ann)
overlaps_map += mask
pan_format[mask == 1] = color
Expand All @@ -66,11 +68,13 @@ def convert_detection_to_panoptic_coco_format_single_core(
segments_info.append(ann)

if np.sum(overlaps_map > 1) != 0:
raise Exception("Segments for image {} overlap each other.".format(img_id))
raise Exception(
"Segments for image {} overlap each other.".format(img_id))
panoptic_record['segments_info'] = segments_info
annotations_panoptic.append(panoptic_record)

Image.fromarray(pan_format).save(os.path.join(segmentations_folder, file_name))
Image.fromarray(pan_format).save(
os.path.join(segmentations_folder, file_name))

print('Core: {}, all {} images processed'.format(proc_id, len(img_ids)))
return annotations_panoptic
Expand All @@ -85,7 +89,8 @@ def convert_detection_to_panoptic_coco_format(input_json_file,
if segmentations_folder is None:
segmentations_folder = output_json_file.rsplit('.', 1)[0]
if not os.path.isdir(segmentations_folder):
print("Creating folder {} for panoptic segmentation PNGs".format(segmentations_folder))
print("Creating folder {} for panoptic segmentation PNGs".format(
segmentations_folder))
os.mkdir(segmentations_folder)

print("CONVERTING...")
Expand All @@ -106,7 +111,8 @@ def convert_detection_to_panoptic_coco_format(input_json_file,

cpu_num = multiprocessing.cpu_count()
img_ids_split = np.array_split(img_ids, cpu_num)
print("Number of cores: {}, images per core: {}".format(cpu_num, len(img_ids_split[0])))
print("Number of cores: {}, images per core: {}".format(
cpu_num, len(img_ids_split[0])))
workers = multiprocessing.Pool(processes=cpu_num)
processes = []
for proc_id, img_ids in enumerate(img_ids_split):
Expand Down
Loading