Cutie icon indicating copy to clipboard operation
Cutie copied to clipboard

When test Cuda out of Mem

Open Air1000thsummer opened this issue 9 months ago • 4 comments

Code : ———————————————————————————— from cutie.utils.get_default_model import get_default_model from cutie.inference.inference_core import InferenceCore import torch import os os.environ['CUDA_VISIBLE_DEVICES']='0' from PIL import Image import numpy as np from torchvision import transforms

def main():

cutie = get_default_model()
processor = InferenceCore(cutie, cfg=cutie.cfg)

image_path = r'TestDataset\JPEGImages\20231214113655970'
images = sorted(os.listdir(image_path))  # ordering is important
mask = Image.open(r'TestDataset\Annotations\20231214113655970\frame_000000.png')
palette = mask.getpalette()
objects = np.unique(np.array(mask))
objects = objects[objects != 0].tolist()  # background "0" does not count as an object
mask = torch.from_numpy(np.array(mask)).cuda()

for ti, image_name in enumerate(images):
    image = Image.open(os.path.join(image_path, image_name))
    image = transforms.ToTensor()(image).cuda().float()

    if ti == 0:
        output_prob = processor.step(image, mask, objects=objects)
    else:
        output_prob = processor.step(image)

    # convert output probabilities to an object mask
    mask = processor.output_prob_to_mask(output_prob)

    # visualize prediction
    output_dir = image_path.replace('JPEGImages','output')
    output_path = os.path.join(image_path, image_name).replace('JPEGImages','output').replace('.jpg','.png')
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    
    mask = Image.fromarray(mask.cpu().numpy().astype(np.uint8))
    mask.putpalette(palette)
    mask.save(output_path)  # or use mask.save(...) to save it somewhere

main() ———————————Code end—————————————

We get : ———————————————————————————— Traceback (most recent call last): File "d:/WorkShopU/WorkShop/DetactionDsitance/main-Cutie/Test.py", line 47, in main() File "d:/WorkShopU/WorkShop/DetactionDsitance/main-Cutie/Test.py", line 31, in main output_prob = processor.step(image) File "d:\WorkShopU\WorkShop\DetactionDsitance\main-Cutie\cutie\inference\inference_core.py", line 252, in step pred_prob_with_bg = self._segment(key, File "d:\WorkShopU\WorkShop\DetactionDsitance\main-Cutie\cutie\inference\inference_core.py", line 153, in _segment memory_readout = self.memory.read(pix_feat, key, selection, self.last_mask, self.network) File "d:\WorkShopU\WorkShop\DetactionDsitance\main-Cutie\cutie\inference\memory_manager.py", line 162, in read similarity = get_similarity(memory_key, shrinkage, query_key, selection) File "d:\WorkShopU\WorkShop\DetactionDsitance\main-Cutie\cutie\model\utils\memory_utils.py", line 31, in get_similarity a_sq = (mk.pow(2) @ qe) torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 72.00 MiB. GPU 0 has a total capacty of 24.00 GiB of which 28.70 MiB is free. Of the allocated memory 21.18 GiB is allocated by PyTorch, and 454.79 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF d:\WorkShopU\WorkShop\DetactionDsitance\main-Cutie\cutie\inference\image_feature_store.py:49: UserWarning: Leaking dict_keys([10]) in the image feature store warnings.warn(f'Leaking {self._store.keys()} in the image feature store')

————————————————————————————

Q:

dear rex is any mistakes in my code or my devices ? i just can't run this inference demo.

Air1000thsummer avatar May 20 '24 07:05 Air1000thsummer

Resize input mitigates this problem, but it still doesn't complete the reasoning

Air1000thsummer avatar May 20 '24 08:05 Air1000thsummer

What's the input? Number of objects/resolution?

hkchengrex avatar May 20 '24 09:05 hkchengrex

JPG 2 objects. resolution:1024*648. i only got 8 mask png when OutOfMemoryError happen. sincely waitting for solution.

Air1000thsummer avatar May 21 '24 04:05 Air1000thsummer

That shouldn't happen. I am speculating but are you sure that there are only 8 objects? Sometimes people interpolate the masks and it creates a lot of new unique values (i.e., objects).

Another note is that you are missing

@torch.inference_mode()
@torch.cuda.amp.autocast()

which are present in our demo.

hkchengrex avatar May 23 '24 16:05 hkchengrex