Skip to content

Non color images #1976

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

Merged
merged 4 commits into from
Jan 21, 2020
Merged

Non color images #1976

merged 4 commits into from
Jan 21, 2020

Conversation

melikovk
Copy link
Contributor

Hi,
Thank you for great project. This pull request adds ability to load images that are not 3 channel color images. Since opencv treats 'grayscale' images as 2D matrices an empty third dimension have to be added before converting them to tensors.

On branch non-color-images
Changes to be committed:
modified:   mmdet/datasets/pipelines/loading.py
Add 'color_type' parameter to LoadImageFromFile class
Change __repr__ method accordingly
Since non-color images maybe two dimensional expand image
dimensions if necessary in DefaultFormatBundle and
ImageToTensor classes

Changes to be committed:
    modified:   mmdet/datasets/pipelines/formating.py
    modified:   mmdet/datasets/pipelines/loading.py
@melikovk melikovk requested a review from hellock January 14, 2020 19:00

def __call__(self, results):
if results['img_prefix'] is not None:
filename = osp.join(results['img_prefix'],
results['img_info']['filename'])
else:
filename = results['img_info']['filename']
img = mmcv.imread(filename)
img = mmcv.imread(filename, self.color_type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest expanding dims here to simplify the formatting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this will not work, because opencv will drop a dummy dimension. For example this code
img = np.random.rand(320,320,1)
cv2.resize(img, (300,300)).shape
will output (300,300)
Now I realize that more changes are necessary to accommodate grayscale (single-channel) images. In particular changes should be introduced into at least some of the transforms (I added proposed changes to RandomCrop). In addition the way I handle it, 'image_shape' and 'pad_shape' will be 2-tuples and corresponding changes might be necessary (I made some for 'pad_shape'). Clearly one can convert grayscale images to 3 channel images by simply replicating the image, but I think it would be useful to support single channel images directly. I will be happy to do it if there is an interest.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Did you tried training any models with grayscale images?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I trained small RetinaNet (retinanet_r50_fpn_1x). It works fine, but I used only Resize, RandomCrop, RandomFlip, Pad and Normalize transforms. I will need to check how other transforms behave with grayscale images.

Changes to be committed:
    modified:   mmdet/datasets/pipelines/transforms.py
This addreses problems with single channel images for which the
shape is  tuple with 2 values

Changes to be committed:
    modified:   mmdet/models/anchor_heads/anchor_head.py
    modified:   mmdet/models/anchor_heads/guided_anchor_head.py
    modified:   mmdet/models/anchor_heads/reppoints_head.py
@hellock hellock merged commit 8c86f74 into open-mmlab:master Jan 21, 2020
@melikovk melikovk deleted the non-color-images branch February 21, 2020 14:14
mattdawkins added a commit to VIAME/mmdetection that referenced this pull request Mar 13, 2020
* origin/viame/master: (28 commits)
  Fix FPN upscale
  Extra compiler args
  VIAME-specific build parameters
  Bump version to 1.0.0 (open-mmlab#2029)
  Fix the incompatibility of the latest numpy and pycocotools (open-mmlab#2024)
  format configs with yapf (open-mmlab#2023)
  options for FCNMaskHead during testtime (open-mmlab#2013)
  Enhance AssignResult and SamplingResult (open-mmlab#1995)
  Fix typo activatation -> activation (open-mmlab#2007)
  Reorganize requirements, make albumentations optional (open-mmlab#1969)
  Encapsulate DCN into a ConvModule & Conv_layers (open-mmlab#1894)
  Code for Paper "Bridging the Gap Between Anchor-based and Anchor-free… (open-mmlab#1872)
  Non color images (open-mmlab#1976)
  Fix albu mask format bug (open-mmlab#1818)
  Fix CI by limiting the version of torchvision (open-mmlab#2005)
  Add ability to overwite existing module in Registry (open-mmlab#1982)
  bug for distributed training (open-mmlab#1985)
  Update Libra RetinaNet config with the latest code (open-mmlab#1975)
  Fix issue in refine_bboxes and add doctest (open-mmlab#1962)
  add link to official repo (open-mmlab#1971)
  ...
ioir123ju pushed a commit to ioir123ju/mmdetection that referenced this pull request Mar 30, 2020
* First Draft

On branch non-color-images
Changes to be committed:
modified:   mmdet/datasets/pipelines/loading.py

* Add option to load non color images

Add 'color_type' parameter to LoadImageFromFile class
Change __repr__ method accordingly
Since non-color images maybe two dimensional expand image
dimensions if necessary in DefaultFormatBundle and
ImageToTensor classes

Changes to be committed:
    modified:   mmdet/datasets/pipelines/formating.py
    modified:   mmdet/datasets/pipelines/loading.py

* Fix RandomCrop to work with grayscale images

Changes to be committed:
    modified:   mmdet/datasets/pipelines/transforms.py

* Modify retrieving w, h of padded image in anchor heads

This addreses problems with single channel images for which the
shape is  tuple with 2 values

Changes to be committed:
    modified:   mmdet/models/anchor_heads/anchor_head.py
    modified:   mmdet/models/anchor_heads/guided_anchor_head.py
    modified:   mmdet/models/anchor_heads/reppoints_head.py
mike112223 pushed a commit to mike112223/mmdetection that referenced this pull request Aug 25, 2020
* First Draft

On branch non-color-images
Changes to be committed:
modified:   mmdet/datasets/pipelines/loading.py

* Add option to load non color images

Add 'color_type' parameter to LoadImageFromFile class
Change __repr__ method accordingly
Since non-color images maybe two dimensional expand image
dimensions if necessary in DefaultFormatBundle and
ImageToTensor classes

Changes to be committed:
    modified:   mmdet/datasets/pipelines/formating.py
    modified:   mmdet/datasets/pipelines/loading.py

* Fix RandomCrop to work with grayscale images

Changes to be committed:
    modified:   mmdet/datasets/pipelines/transforms.py

* Modify retrieving w, h of padded image in anchor heads

This addreses problems with single channel images for which the
shape is  tuple with 2 values

Changes to be committed:
    modified:   mmdet/models/anchor_heads/anchor_head.py
    modified:   mmdet/models/anchor_heads/guided_anchor_head.py
    modified:   mmdet/models/anchor_heads/reppoints_head.py
jben-hun pushed a commit to jben-hun/mmdetection that referenced this pull request Jan 10, 2025
* First Draft

On branch non-color-images
Changes to be committed:
modified:   mmdet/datasets/pipelines/loading.py

* Add option to load non color images

Add 'color_type' parameter to LoadImageFromFile class
Change __repr__ method accordingly
Since non-color images maybe two dimensional expand image
dimensions if necessary in DefaultFormatBundle and
ImageToTensor classes

Changes to be committed:
    modified:   mmdet/datasets/pipelines/formating.py
    modified:   mmdet/datasets/pipelines/loading.py

* Fix RandomCrop to work with grayscale images

Changes to be committed:
    modified:   mmdet/datasets/pipelines/transforms.py

* Modify retrieving w, h of padded image in anchor heads

This addreses problems with single channel images for which the
shape is  tuple with 2 values

Changes to be committed:
    modified:   mmdet/models/anchor_heads/anchor_head.py
    modified:   mmdet/models/anchor_heads/guided_anchor_head.py
    modified:   mmdet/models/anchor_heads/reppoints_head.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants