Skip to content
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

RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu) #18

Open
tankailong opened this issue Jul 17, 2023 · 2 comments

Comments

@tankailong
Copy link

After a long time, I finally installed the program, but when I ran the test data, I found that this error returned me, can someone help me check the problem? I'm using a 4070 graphics card

`(word) F:\python\Word-As-Image>python code/main.py  --semantic_concept "LEAVES" --word "NATURE" --optimized_letter "T" --font "HobeauxRococeaux-Sherman" --seed 0
D:\ProgramData\anaconda3\envs\word\lib\site-packages\torchvision\io\image.py:13: UserWarning: Failed to load image Python extension: '[WinError 127] 找不到指定的程序。'If you don't plan on using image functionality from `torchvision.io`, you can ignore this warning. Otherwise, there might be something wrong with your environment. Did you have `libjpeg` or `libpng` installed before building `torchvision` from source?
  warn(
Config: {'parent_config': 'baseline', 'save': {'init': True, 'image': True, 'video': True, 'video_frame_freq': 1}, 'trainable': {'point': True}, 'lr_base': {'point': 1}, 'lr': {'lr_init': 0.002, 'lr_final': 0.0008, 'lr_delay_mult': 0.1, 'lr_delay_steps': 100}, 'num_iter': 500, 'render_size': 600, 'cut_size': 512, 'level_of_cc': 1, 'seed': 0, 'diffusion': {'model': 'runwayml/stable-diffusion-v1-5', 'timesteps': 1000, 'guidance_scale': 100}, 'loss': {'use_sds_loss': True, 'tone': {'use_tone_loss': True, 'dist_loss_weight': 100, 'pixel_dist_kernel_blur': 201, 'pixel_dist_sigma': 30}, 'conformal': {'use_conformal_loss': True, 'angeles_w': 0.5}}, 'config': 'code/config/base.yaml', 'experiment': 'conformal_0.5_dist_pixel_100_kernel201', 'font': 'HobeauxRococeaux-Sherman', 'semantic_concept': 'LEAVES', 'word': 'NATURE', 'caption': 'a LEAVES. minimal flat 2d vector. lineal color. trending on artstation', 'log_dir': 'output/conformal_0.5_dist_pixel_100_kernel201_NATURE', 'optimized_letter': 'T', 'batch_size': 1, 'token': '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', 'use_wandb': 0, 'wandb_user': 'none', 'letter': 'HobeauxRococeaux-Sherman_T_scaled', 'target': 'code/data/init/HobeauxRococeaux-Sherman_T_scaled', 'experiment_dir': 'output/conformal_0.5_dist_pixel_100_kernel201_NATURE\\HobeauxRococeaux-Sherman\\HobeauxRococeaux-Sherman_T_scaled_concept_LEAVES_seed_0'}
preprocessing
======= HobeauxRococeaux-Sherman =======
94
196
155
124
121
152
==== N ====
TOTAL CP:   [195]
195
code/data/fonts/HobeauxRococeaux-Sherman.ttf N
==== A ====
TOTAL CP:   [153]
153
code/data/fonts/HobeauxRococeaux-Sherman.ttf A
==== T ====
TOTAL CP:   [123]
123
code/data/fonts/HobeauxRococeaux-Sherman.ttf T
==== U ====
TOTAL CP:   [120]
120
code/data/fonts/HobeauxRococeaux-Sherman.ttf U
==== R ====
TOTAL CP:   [150]
150
code/data/fonts/HobeauxRococeaux-Sherman.ttf R
==== E ====
TOTAL CP:   [129]
129
code/data/fonts/HobeauxRococeaux-Sherman.ttf E
Done preprocess
Fetching 27 files: 100%|███████████████████████████████████████████████████████████████████████| 27/27 [00:00<?, ?it/s]
`text_config_dict` is provided which will be used to initialize `CLIPTextConfig`. The value `text_config["id2label"]` will be overriden.
initializing shape
saving init
T tensor(0) tensor(0)
Traceback (most recent call last):
  File "code/main.py", line 95, in <module>
    conformal_loss = ConformalLoss(parameters, device, cfg.optimized_letter, shape_groups)
  File "F:\python\Word-As-Image\code\losses.py", line 124, in __init__
    self.reset()
  File "F:\python\Word-As-Image\code\losses.py", line 149, in reset
    self.angles = self.get_angles(points)
  File "F:\python\Word-As-Image\code\losses.py", line 130, in get_angles
    triangles = points[self.faces[i]]
RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu)`
@tankailong
Copy link
Author

I added the following code to code/losses.py line 128 and it worked

    def get_angles(self, points: torch.Tensor) -> torch.Tensor:
        points = points.to('cuda')

But the connection is disconnected directly during program training, and no error message appears, what should I do?

code/data/fonts/HobeauxRococeaux-Sherman.ttf E
Done preprocess
Fetching 27 files: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 27/27 [00:00<00:00, 26995.52it/s]
`text_config_dict` is provided which will be used to initialize `CLIPTextConfig`. The value `text_config["id2label"]` will be overriden.
initializing shape
saving init
T tensor(0) tensor(0)
start training
  0%|                                                                                                                                                                                                                                                                                               | 0/500 [00:00<?, ?it/s]
(word) F:\python\Word-As-Image>

@gengchaogit
Copy link

gengchaogit commented Dec 14, 2024

Hello,

I meet this bug to.

The points tensor is probably on the GPU, while self.faces[i] is on the CPU, so indexing won't work. To fix this, you need to make sure the self.faces[i] tensor is on the same device as the points tensor (either the CPU or GPU).

You can replace the following in the losses.py method def get_angles(self, points: torch.Tensor) -> torch.Tensor: with the new three lines of code which will fix the problem.

triangles = points[self.faces[i]]
triangles_roll_a = points[self.faces_roll_a[i]]

device = points.device # Get the device of the points tensor
triangles = points[self.faces[i].to(device)] # Move faces[i] to the same device as points
triangles_roll_a = points[self.faces_roll_a[i].to(device)] # Similarly move faces_roll_a[i]

Best Regards,
cg

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

No branches or pull requests

2 participants