r/computervision • • 3d ago

Help: Theory CNN Generalisability Problems

Post image

Hi everyone!

So, I'm having a bit of difficulty with CNN generalisability.

My problem is that, given a camera image, we need to predict object pose e.g., rotation

A week ago, I was using a synthetic dataset. Using (unforseen) synthetic data, I get very low error. Using real data, however, it is a disaster. I though this might be a domain gap from sim-to-real.

So, I did some more data retrieval and now I have 2 distinct video feeds, so 2 real datasets. However, if I train on one, inference on the other is terrible. So, it is not sim-to-real, but a generalisation problem in general.

I do have a pre-processing, which I won't detail, but if you look at the image above what the CNN sees is quite similar to my scenario (just imagine they sre 64x64x3 images). Imagine really low errors on the left lamp (same training environment) and high on the right lamp (different camera/perspective etc).

Basically, one camera has a different FOV/lens/resolution, and the images might also have varying sizes (e.g. a lamp in one camera might be "chunkier" due to lens characteristics).

In teems of solutions, I have read that it is very healthy to augment your training data, especially for computer vision. This could be blur/brightness/contrast etc., that you can do natively in PyTorch.

Would this be a profitable direction? What are good steps to take to try to debug this? What are the standard practices in these situations? What are the features my CNN is learning that are so specific to one dataset?

I should mention, my CNN is almost certainly not the problem. I have another pipeline that uses a VAE, inspired from a paper that achieves very high accuracy not only for generalisability, but even for sim-to-real. This current architecture fails for my data.

Thanks for any response!

20 Upvotes

17 comments sorted by

8

u/FirstPrinciplesRobot 3d ago

Augmentation is worth doing, but I'd first work out which camera difference is actually hurting you. Since the two cameras differ in FOV, lens and resolution, the object ends up a different apparent size and shape in the crop, and blur or brightness jitter won't fix that. Try random scale, aspect and perspective augmentation, plus a bit of lens-distortion-style warping, so the net can't rely on one camera's geometry. Also check what happens when you resize: if one camera's crops are interpolated very differently, the net can latch onto that texture. For debugging, hold out one camera entirely, look at where the errors are worst (certain angles? certain distances?), and try Grad-CAM or occlusion tests to see whether it's looking at the object or at background cues that happen to differ per camera.

1

u/SnappySkillz 3d ago

Ah brilliant, thanks for your response! So, just to clarify, all of these transformations you recommended doing as pre-processing steps for the training data only? I.e., I should prioritise all pre-processing in one place than, e.g., in my synthetic data environment (Unreal Engine), where I can do similar distortions?

2

u/FirstPrinciplesRobot 2d ago

Yes, do the augmentation on the training data only, and apply it randomly on the fly each epoch instead of baking it into saved images. In Unreal it's even better to randomize the camera itself, so vary the FOV, lens distortion and render resolution per sample, because then the net sees a whole range of cameras instead of two. Keep a validation set of real frames from both cameras that never gets augmented, so you can tell if it's actually helping.

1

u/SnappySkillz 2d ago

Perfect! I actually generated my augmented data today. Out of curiosity, why is is better to do it on the fly? My augmentation was random anyway, so some images are much more affected than others. How would doing it during the training process affect performance?

2

u/FirstPrinciplesRobot 1d ago

Mostly because of variety. If you bake it once, the net sees the exact same distorted copy of each image every epoch and can start memorizing it, while on the fly every epoch gets a fresh random version, so it effectively sees many more distinct images. It also lets you change the strength or ranges later without regenerating a dataset. Your random-per-image set isn't wrong, it's just one fixed draw per image, so I'd keep it and add a lighter on-the-fly pass on top if your training code allows it.

2

u/wingman-jr 23h ago

u/SnappySkillz Maybe another thing I'd add is that augmentation is one of the things that I think tends to get tweaked the most. So if you generate in advance, you'll find you constantly need to regenerate for each experiment. I did more work with this in Tensorflow / Keras, but there the augmentation could be tapped directly into the image loading pipeline and done in a GPU-accelerated fashion too so it was quite feasible to just chain a few augmentations together on the fly and tweak the recipe as needed.

Also - looking at the images, if you don't have a random blur of at least sigma=1.5 or something I think you're definitely not capturing the differences between the two images. Also, if you are using generated stills and then taking video, my experience with video is that it tends to be blurrier than you think on paused frames etc.

If the pose estimation is truly something like e.g. angle of the lamp, that's a pretty small amount of data from the images, and you may actually find that going smaller on input size and reducing to grayscale so your input data space is smaller may help reveal any structural problems that matter, as u/FirstPrinciplesRobot was attacking from the other side by suggesting stronger augmentations and analysis tools.

Good luck!

1

u/SnappySkillz 14h ago

Awesome, makes sense! Will couple everything on Monday, already have GPU acceleration working nicely, shouldn't be too hard to hook in both pre-processing and training together. Thank you!

1

u/SnappySkillz 14h ago

Ah, that makes perfect sense, thank you! I think in that case I'm gonna have to re-couple pre-processing to training, so that each epoch sees a different version of each image. Training will be much slower, but I guess if I am constantly tweaking I can monitor progress on the fly rather than generate -> test. Thanks for much for this advice, I think this might help me reduce losses a lot, yesterday I managed to half errors from 90 degrees to ~45 using segmentation and greyscale, I think augmentation might significantly help the rest!

5

u/impatiens-capensis 3d ago

I'm a bit confused about what your training dataset is and what your test environment is.

Are you training on many (image,pose) pairs and hoping to generalize to any scene or is it... specifically for this lamp? You need to be more clear.

So, it is not sim-to-real, but a generalisation problem in general.

There's no reason to think a model trained on a single distribution will generalize to all others. Models learn exactly what they are told to learn, and often very little more than that. You need to find ways to make your training data look more diverse. You could even do augmentation with image-to-image generators to make your real images look synthetic or like other domains (i.e. make this image look like it was taken by a security camera, etc.) to add semantically structured noise.

1

u/SnappySkillz 3d ago

Hi! Ah, to clarify, I am training using image/pose pairs. I have a choice of datasets, either synthetic generators in both Blender and Unreal Engine (I have 100k samples) or from real videos (some thousands of frames of image/ground truth pairs).

I'm training in one domain and trying to bridge the gap with another. Ideally, I want the synthetic data to generalise, as then I have unlimited data which I can optimise and vary scenarios.

I agree that I could user gen AI to get other samples, but I've got so many images now, I suppose it makes sense to perform the transformations you mentioned on those?

Thanks for the response :)!

1

u/impatiens-capensis 3d ago

Are you training from scratch or fine-tuning/tuning a LoRA/etc. into an existing object pose detector?

3

u/Necessary-Meeting-28 3d ago edited 2d ago

You can use camera intrinsics as a dense input to the CNN, using techniques such as Plücker coordinates. Training by augmenting camera intrinsics for your task also makes sense.

For pose cameras matter, especially for size and translation. If you train with one camera, the model will memorize that camera and will not generalize to others.

2

u/Dry-Snow5154 3d ago

I have 2 distinct video feeds

Please, tell me you don't just have 2 videos, train on one and expect it to generalize to another.

In my experience augmentations can deliver 92 -> 95% improvement, but never 60 -> 95%.

Synthetic data usually works when it looks like real data. If your doesn't it likely won't work.

The article you linked last time has a very simplistic use case. It is totally possible VAE learnt to differentiate the object by color alone, which won't work in any real scenario.

1

u/Aggressive_Hand_9280 3d ago

What is your real dataset accuracy? Simulated rotations are perfect and real not always, maybe you overfit to one video?

1

u/Beautiful_Egg_371 1d ago

As noted by others, data augmentation is one. What is the CNnN you are using ? A lot depends on that. I would suggest you try a strong image backbone. If you are already using a strong backbone, then this is interesting

1

u/bfyvfftujijg 10h ago

What pretrained model are you starting from?

Ones like DINO tend to already generalize fairly well out of the box. You just need to train your own head on top of the DINO backbone.