遇到过的问题以及是如何解决的:
1.迭代无序
这段代码是cycleGAN中用来读取图片数据的代码。现在用来训练pix2pix,需要图片按照名称一一对应。
def __iter__(self):
"""Return a batch of data"""
for i, data in enumerate(self.dataloader):
if i * self.opt.batch_size >= self.opt.max_dataset_size:
break
yield data
这段代码中,self.dataloader里的A_paths和B_paths是存储图片地址有序列表。但当enumerate后,data并不是按顺序遍历的。此时的A_paths和B_paths里取出的图片名称列表就不能按名称对应。
解决办法:self.dataloader是一个自定义类,类中有__getitem__迭代函数,函数中有控制是否随机的变量。只要修改参数就可以不随机返回了。顺带加上一句判断图片名是否相同的语句就可以了。代码如下:
原来的迭代函数:
A_path = self.A_paths[index % self.A_size] # make sure index is within then range
if self.opt.serial_batches: # make sure index is within then range
index_B = index % self.B_size
else: # randomize the index for domain B to avoid fixed pairs.
index_B = random.randint(0, self.B_size - 1)
B_path = self.B_paths[index_B]
修改后:
A_path = self.A_paths[index % self.A_size] # make sure index is within then range
B_path = self.B_paths[index % self.B_size]
if A_path.split('/')[-1] != B_path.split('/')[-1]:
print("A与B不匹配")
其中,A_path='/home/xq/文档/datasets/OK_20190611_dataset/mask/2019-03-28_10_22_25_876.png'
B_path='/home/xq/文档/datasets/OK_20190611_dataset/img/2019-03-28_10_22_25_876.png'