# PyTorch实现自由的数据读取

01

Pytorch数据读入函数介绍

Paste_Image.png

## 问题来源

02

class custom_dset(Dataset): def __init__(self, img_path, txt_path, img_transform=None, loader=default_loader): with open(txt_path, 'r') as f: lines = f.readlines() self.img_list = [ os.path.join(img_path, i.split()[0]) for i in lines ] self.label_list = [i.split()[1] for i in lines] self.img_transform = img_transform self.loader = loader def __getitem__(self, index): img_path = self.img_list[index] label = self.label_list[index] # img = self.loader(img_path) img = img_path if self.img_transform is not None: img = self.img_transform(img) return img, label def __len__(self): return len(self.label_list)

def collate_fn(batch): batch.sort(key=lambda x: len(x[1]), reverse=True) img, label = zip(*batch) pad_label = [] lens = [] max_len = len(label[0]) for i in range(len(label)): temp_label = [0] * max_len temp_label[:len(label[i])] = label[i] pad_label.append(temp_label) lens.append(len(label[i])) return img, pad_label, lens

436 篇文章91 人订阅

0 条评论

## 相关文章

### <算法>蛇形矩阵求解

? 蛇形矩阵 右下，下左，左上，上右，循环往复～ 解题思路: 在单步前进过程中, x与y, 只能有一个发生变化 每次转向, x与y会发生...

3076

36210

2748

4088

### JAVA拾遗 — JMH与8个代码陷阱

JMH (http://openjdk.java.net/projects/code-tools/jmh/) 是 Java Microbenchmark Har...

1934

1941

### 1010 过河卒

1010 过河卒 2002年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果...

2855

1982

2242