import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
class PositionalEncoding(nn.Module):
def __init__(self, dim, max_len=5000):
super(PositionalEncoding, self).__init__()
# 创建一个max_len x dim的位置编码矩阵
pe = torch.zeros(max_len, dim)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, dim, 2).float() * (-np.log(10000.0) / dim))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
self.register_buffer('pe', pe)
def forward(self, x):
# x: [batch_size, seq_len, dim]
return x + self.pe[:x.size(1), :]
class PixelValuePredictor(nn.Module):
def __init__(self, dim):
super(PixelValuePredictor, self).__init__()
self.fc1 = nn.Linear(dim, 128)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(128, 1) # 输出一个像素值
self.pos_encoder = PositionalEncoding(dim)
def forward(self, coords):
# coords: [batch_size, 2] 代表 (x, y) 坐标的坐标
x = self.pos_encoder(coords.unsqueeze(0)) # 加上batch维度
x = x.squeeze(0) # 移除batch维度
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x
User
获取图片尺寸
_, height, width = image_tensor.shape
生成坐标网格并归一化
coords = torch.tensor([[x, y] for y in range(height) for x in range(width)], dtype=torch.float32)
coords /= torch.tensor([width - 1, height - 1]).float()
CUDA error: initialization error
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.
为什么可以训练,但报错是这样的呢?File "fitting_curves/fitting_ori.py", line 110, in
predicted_pixels = model(encoded_coords)
File "/home/jianruobing/anaconda3/envs/nerf/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "fitting_curves/fitting_ori.py", line 67, in forward
return self.fc(x)
File "/home/jianruobing/anaconda3/envs/nerf/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "/home/jianruobing/anaconda3/envs/nerf/lib/python3.7/site-packages/torch/nn/modules/container.py", line 141, in forward
input = module(input)
File "/home/jianruobing/anaconda3/envs/nerf/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "/home/jianruobing/anaconda3/envs/nerf/lib/python3.7/site-packages/torch/nn/modules/linear.py", line 103, in forward
return F.linear(input, self.weight, self.bias)
File "/home/jianruobing/anaconda3/envs/nerf/lib/python3.7/site-packages/torch/nn/functional.py", line 1848, in linear
return torch._C._nn.linear(input, weight, bias)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument mat1 in method wrapper_addmm)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument mat1 in method wrapper_addmm)