Pytorch 即使使用GPU,培训也不会加速
批量大小为4Pytorch 即使使用GPU,培训也不会加速,pytorch,gpu,kaggle,Pytorch,Gpu,Kaggle,批量大小为4 深度学习lib库=“数据”和数据[地标]是张量设备=火炬。(CUDA:0)和我使用的深度学习库,但GPU仍然不适合我。其使用率为5%,总时间为1.5-4小时 如果有人指出我的错误,我会很有帮助的 附加资源使用和GPU配置的映像。 附加显示GPU已打开的图像 这是我笔记本的链接 如果运行此语句,结果是什么device=torch.device('cuda'如果torch.cuda.is_可用(),则为'cpu')给出在计算机上运行的答案GPU@stackoverflowuser2
深度学习lib库=“数据”和数据[地标]是张量设备=火炬。(CUDA:0)和我使用的深度学习库,但GPU仍然不适合我。其使用率为5%,总时间为1.5-4小时
如果有人指出我的错误,我会很有帮助的 附加资源使用和GPU配置的映像。 附加显示GPU已打开的图像 这是我笔记本的链接如果运行此语句,结果是什么
device=torch.device('cuda'如果torch.cuda.is_可用(),则为'cpu')
给出在计算机上运行的答案GPU@stackoverflowuser2010,我刚刚在我的问题中添加了另一个图像,显示GPU正在运行我不知道您的图像和裁剪图像的大小,但我认为您的模型太小了(数千个参数)GPU在CPU方面没有什么可以改进的。您可能主要受I/O限制。试着使用更大的线性层,看看这是否会提高GPU速度/CPU速度比——确实,训练时间会增加。但我想表明的是,你的GPU可能对要执行的操作的数量不感兴趣。您很可能可以执行更复杂的操作(比如更大的fc层或深度卷积,毕竟您正在处理图像)。JPG已经是加载/解压缩速度最快的文件格式之一,但您可以尝试使用pin_memory()来减少数据传输瓶颈
transform = transforms.Compose([transforms.Resize(IMG_SIZE),
transforms.CenterCrop(CROP_SIZE),
transforms.ToTensor()])
class LandmarksDatasetTrain(Dataset):
"""Landmarks dataset."""
def __init__(self, landmarks_frame, root_dir, transform=None):
"""
Args:
csv_file (string): Path to the csv file with annotations.
root_dir (string): Directory with all the images.
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.landmarks_frame = landmarks_frame
self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.landmarks_frame)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img_name = os.path.join(self.root_dir,self.landmarks_frame.loc[idx, 'id'][0],self.landmarks_frame.loc[idx, 'id'][1], self.landmarks_frame.loc[idx, 'id'][2], self.landmarks_frame.loc[idx, 'id'])
img_name += ".jpg"
image = Image.open(img_name)
landmarks = self.landmarks_frame.loc[idx, 'landmark_id']
sample = {'image': image, 'landmarks': landmarks}
if self.transform:
sample['image'] = self.transform(sample['image'])
sample['landmarks'] = torch.tensor(sample['landmarks'])
return sample
dataset_train = LandmarksDatasetTrain(landmarks_frame = frame,
root_dir='/kaggle/input/landmark-recognition-2020/train',
transform=transform)
train_loader = DataLoader(dataset_train, batch_size=4, shuffle=True, num_workers=4, drop_last=False)
class Net(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(CROP_SIZE*CROP_SIZE*3, 64)
self.fc2 = nn.Linear(64, 64)
self.fc3 = nn.Linear(64, 64)
self.fc4 = nn.Linear(64, frame['landmark_id'].nunique())
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = self.fc4(x)
return F.log_softmax(x, dim=1)
net = Net()
net.to(device)
for epoch in range(3):
optimizer = optim.Adam(net.parameters(), lr=0.001)
for data in tqdm(train_loader):
X = data['image'].to(device)
y = data['landmarks'].to(device)
net.zero_grad()
output = net(X.view(-1,CROP_SIZE*CROP_SIZE*3))
loss = F.nll_loss(output, y)
loss.backward()
optimizer.step()
print(loss)