-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Allowing for user defined transforms #10308
base: main
Are you sure you want to change the base?
Allowing for user defined transforms #10308
Conversation
All Contributors have signed the CLA. ✅ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👋 Hello @edkazcarlson, thank you for submitting an Ultralytics YOLOv8 🚀 PR! To allow your work to be integrated as seamlessly as possible, we advise you to:
- ✅ Verify your PR is up-to-date with
ultralytics/ultralytics
main
branch. If your PR is behind you can update your code by clicking the 'Update branch' button or by runninggit pull
andgit merge main
locally. - ✅ Verify all YOLOv8 Continuous Integration (CI) checks are passing.
- ✅ Update YOLOv8 Docs for any new or updated features.
- ✅ Reduce changes to the absolute minimum required for your bug fix or feature addition. "It is not daily increase but daily decrease, hack away the unessential. The closer to the source, the less wastage there is." — Bruce Lee
See our Contributing Guide for details and let us know if you have any questions!
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #10308 +/- ##
==========================================
- Coverage 70.14% 70.10% -0.04%
==========================================
Files 124 124
Lines 15716 15754 +38
==========================================
+ Hits 11024 11045 +21
- Misses 4692 4709 +17
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
I have read the CLA Document and I sign the CLA |
Unsure if this will help with reviewing, but I tested the changes with the following code def preprocess32(x):
"""
Convert to float32 and normalize to 0-1
Returns: np array
"""
if (type(x) == type(torch.tensor([]))):
x = x.transpose(0,1).transpose(1,2) # go from channel, height, width to height, width, channel
x = x.numpy().astype(np.float32)
if np.max(x) > 1.1:
x /= 255
elif type(x) == type(Image.Image()):
x = np.float32(np.asarray(x))
x /= 255
elif type(x) == type(np.array([])):
x = np.float32(x)
x /= 255
else:
print(f'In preprocess32 found {type(x)} wanted {type(Image.Image())} or {type(np.array())} or {type(torch.tensor([]))}')
exit()
assert np.max(x) <= 1.01, f'np.max(x) {np.max(x)}'
assert np.min(x) >= -.01, f'np.min(x) {np.min(x)}'
x = np.clip(x, 0, 1)
return x
def FourChannelTransformMethod(x):
x = preprocess32(x)
x = torch.tensor(x, dtype= torch.float)
x = x.transpose(2,1).transpose(1,0) # hwc -> chw
x = torch.cat((x, torch.zeros_like(x[0]).unsqueeze(0)), dim=0)
return x #c h w
class FourChannelTransform(object):
"""Changes an image from bgr to lrgb.
Args: normalizeSB: boolean that is true if the saturation and brightness are normalized around 0
"""
def __init__(self):
pass
def __call__(self, labels):
img = FourChannelTransformMethod(labels['img'])
labels['img'] = img.to(torch.float16)
return labels
class ThreeChannelTransform(object):
"""Changes an image from bgr to lrgb.
Args: normalizeSB: boolean that is true if the saturation and brightness are normalized around 0
"""
def __init__(self, dtype):
self.dtype = dtype
pass
def __call__(self, labels):
labels['img'] = labels['img'].to(self.dtype)
return labels
class TwoChannelTransform(object):
def __init__(self):
pass
def __call__(self, labels):
labels['img'] = labels['img'][0:2]
return labels
def firstTest():
print('Default DetectionTrainer')
overrides = {'epochs': 2, 'imgsz': 640, 'data': 'coco.yaml', 'model': f'yolov8n.yaml', 'inputCh': 3, 'batch': 8, 'close_mosaic': 1}
trainer = DetectionTrainer(overrides=overrides)
trainer.train()
def secondTest():
print('3 channel detection trainer with float16')
overrides = {'epochs': 2, 'imgsz': 640, 'data': 'coco.yaml', 'model': f'yolov8n.yaml', 'inputCh': 3, 'batch': 8, 'close_mosaic': 1}
trainer = DetectionTrainer(overrides=overrides, append_label_transforms=ThreeChannelTransform(torch.float16))
trainer.train()
def thirdTest():
print('3 channel detection trainer with float32')
overrides = {'epochs': 2, 'imgsz': 640, 'data': 'coco.yaml', 'model': f'yolov8n.yaml', 'inputCh': 3, 'batch': 8, 'close_mosaic': 1}
trainer = DetectionTrainer(overrides=overrides, append_label_transforms=ThreeChannelTransform(torch.float32))
trainer.train()
def fourthTest():
print('4 channel detection')
overrides = {'epochs': 2, 'imgsz': 640, 'data': 'coco.yaml', 'model': f'yolov8n.yaml', 'inputCh': 4, 'batch': 8, 'close_mosaic': 1}
trainer = DetectionTrainer(overrides=overrides, append_label_transforms=FourChannelTransform())
trainer.train()
def fifthTest():
print('2 channel detection')
overrides = {'epochs': 2, 'imgsz': 640, 'data': 'coco.yaml', 'model': f'yolov8n.yaml', 'inputCh': 2, 'batch': 8, 'close_mosaic': 1}
trainer = DetectionTrainer(overrides=overrides, append_label_transforms=TwoChannelTransform())
trainer.train()
|
Thanks for sharing your testing code! It looks comprehensive and covers a variety of scenarios with different channel configurations and data types. This will definitely help in understanding how the changes perform across different setups. If you encounter any issues or have further suggestions, feel free to share! 🚀 |
@edkazcarlson hey, saw your message in the Discord server. This is an interesting PR to be sure, but I wouldn't be the best person to evaluate it. I did have a couple of questions for you to consider:
These are all things for you to consider, but I don't need you or expect you to reply. My suggestions are only that, suggestions. I shared them as I believe they are important for the PR to be accepted, but doesn't ensure it will or not. Please be patient, we have so much going on and only a handful of people working on PR reviews and a single person to approve/merge changes. Thanks for the PR 🚀 |
🛠️ PR Summary
Made with ❤️ by Ultralytics Actions
🌟 Summary
Enhancements and Flexibility in Data Handling and Model Configuration
📊 Key Changes
inputCh
configuration to specify the number of input channels for images.override_label_transforms
andappend_label_transforms
options.🎯 Purpose & Impact
These changes enhance the library's flexibility, making it more adaptable to various types of data and specific project requirements.