8个计算机视觉深度学习中常见的Bug
%12 : Float(10) = aten::dropout(%input, %10, %11), scope: Sequential/Dropout[1] # /Users/Arseny/.pyenv/versions/3.6.6/lib/python3.6/site-packages/torch/nn/functional.py:806:0 This may cause errors in trace checking. To disable trace checking, pass check_trace=False to torch.jit.trace() check_tolerance, _force_outplace, True, _module_class) /Users/Arseny/.pyenv/versions/3.6.6/lib/python3.6/site-packages/torch/jit/__init__.py:914: TracerWarning: Output nr 1. of the traced function does not match the corresponding output of the Python function. Detailed error: Not within tolerance rtol=1e-05 atol=1e-05 at input[5] (0.0 vs. 0.5454154014587402) and 5 other locations (60.00%) check_tolerance, _force_outplace, True, _module_class) 简单的修复一下: In [4]: model = nn.Sequential( ...: nn.Linear(10, 10), ...: nn.Dropout(.5) ...: ) ...: ...: traced_model = torch.jit.trace(model.eval(), torch.rand(10)) # No more warnings! 在这种情况下, torch.jit.trace将模型运行几次并比较结果。这里的差别是可疑的。 然而 torch.jit.trace在这里不是万能药。这是一种应该知道和记住的细微差别。 5. 复制粘贴的问题 很多东西都是成对存在的:训练和验证、宽度和高度、纬度和经度…… def make_dataloaders(train_cfg, val_cfg, batch_size): train = Dataset.from_config(train_cfg) val = Dataset.from_config(val_cfg) shared_params = {'batch_size': batch_size, 'shuffle': True, 'num_workers': cpu_count()} train = DataLoader(train, **shared_params) val = DataLoader(train, **shared_params) return train, val 不仅仅是我犯了愚蠢的错误。例如,在非常流行的albumentations库也有一个类似的版本。 # https://github.com/albu/albumentations/blob/0.3.0/albumentations/augmentations/transforms.py def apply_to_keypoint(self, keypoint, crop_height=0, crop_width=0, h_start=0, w_start=0, rows=0, cols=0, **params): keypoint = F.keypoint_random_crop(keypoint, crop_height, crop_width, h_start, w_start, rows, cols) scale_x = self.width / crop_height scale_y = self.height / crop_height keypoint = F.keypoint_scale(keypoint, scale_x, scale_y) return keypoint 别担心,已经修改好了。 如何避免?不要复制和粘贴代码,尽量以不需要复制和粘贴的方式编写代码。 datasets = [] data_a = get_dataset(MyDataset(config['dataset_a']), config['shared_param'], param_a) datasets.append(data_a) data_b = get_dataset(MyDataset(config['dataset_b']), config['shared_param'], param_b) datasets.append(data_b) datasets = [] for name, param in zip(('dataset_a', 'dataset_b'), (param_a, param_b), ): datasets.append(get_dataset(MyDataset(config[name]), config['shared_param'], param)) 6. 合适的数据类型 让我们编写一个新的增强: def add_noise(img: np.ndarray) -> np.ndarray: mask = np.random.rand(*img.shape) + .5 img = img.astype('float32') * mask return img.astype('uint8') 图像已被更改。这是我们所期望的吗?嗯,也许它改变得太多了。 这里有一个危险的操作:将 float32 转换为 uint8。它可能会导致溢出: def add_noise(img: np.ndarray) -> np.ndarray: mask = np.random.rand(*img.shape) + .5 img = img.astype('float32') * mask return np.clip(img, 0, 255).astype('uint8') img = add_noise(cv2.imread('two_hands.jpg')[:, :, ::-1]) _ = plt.imshow(img) 看起来好多了,是吧? 顺便说一句,还有一种方法可以避免这个问题:不要重新发明轮子,不要从头开始编写增强代码并使用现有的扩展: albumentations.augmentations.transforms.GaussNoise。 我曾经做过另一个同样起源的bug。 raw_mask = cv2.imread('mask_small.png') mask = raw_mask.astype('float32') / 255 mask = cv2.resize(mask, (64, 64), interpolation=cv2.INTER_LINEAR) mask = cv2.resize(mask, (128, 128), interpolation=cv2.INTER_CUBIC) mask = (mask * 255).astype('uint8') _ = plt.imshow(np.hstack((raw_mask, mask))) (编辑:PHP编程网 - 黄冈站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |