如何解决即使使用GPU,训练也无法加快速度
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],'id'][1],'id'][2],'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)
批量为4
data ['image']和data ['landmarks']是张量device = torch.device(“ cuda:0”),而我正在使用的深度学习库是Pytorch,但GPU仍然对我不起作用。其用法显示为5%,总时间为1到3.5到4个小时
如果有人指出我的错误,将会非常有帮助。
这是我笔记本的链接 https://www.kaggle.com/hiteshsom/google-landmark-recognition
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。