Skip to content

CURSOR: potential bug between write and read crop model for bgr rgb swap channel order. #1308

@bw4sz

Description

@bw4sz

This issue was written by an AI model. I don't have time to investigate and i'm unsure how important it may or may not be. It was found incidentally during development. I'd like someone to follow up, confirm and implement the fix. This is one of those times that because it was found by a model, we want human eyes on it to get it some veracity.

CropModel.write_crops saves images with swapped R/B channels

Summary

CropModel.write_crops() reads image data via rasterio (RGB order) then saves with cv2.imwrite(), which interprets the array as BGR. This silently swaps the red and blue channels in all saved crop PNGs. Since training loads these PNGs via ImageFolder/PIL (which reads them back as-is), the model trains on BGR data. At inference time, BoundingBoxDataset reads directly from rasterio in correct RGB order, creating a channel mismatch that degrades classification performance.

Location

https://github.com/weecology/DeepForest/blob/main/src/deepforest/model.py#L303-L308

img = src.read(window=((int(ymin), int(ymax)), (int(xmin), int(xmax))))
img = np.rollaxis(img, 0, 3)  # RGB from rasterio
cv2.imwrite(img_path, img)    # cv2 interprets this as BGR

Suggested fix

Replace cv2.imwrite with PIL, which preserves channel order:

from PIL import Image

Image.fromarray(img).save(img_path)

Or convert before writing:

img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imwrite(img_path, img)

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions