分类任务是人工智能中最常见的任务,因为它需要很少的库。我尝试使用在线编译器的资源进行编写,但不了解工作的复杂性。
def rle_decode(mask_rle, shape=(1280, 1918, 1)): ''' mask_rle: run-length as string formated (start length) shape: (height,width) of array to return Returns numpy array, 1 - mask, 0 - background ''' img = np.zeros(shape[0]*shape[1], dtype=np.uint8) s = mask_rle.split() starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])] starts -= 1 ends = starts + lengths for lo, hi in zip(starts, ends): img[lo:hi] = 1 img = img.reshape(shape) return img
例如,使用解码掩码0/1的功能,您可以依赖它们的长度。但要生成神经网络的批量数据包,您仍然需要监控当前结果。
def keras_generator(gen_df, batch_size): while True: x_batch = [] y_batch = [] for i in range(batch_size): img_name, mask_rle = gen_df.sample(1).values[0] img = cv2.imread('data/train/{}'.format(img_name)) mask = rle_decode(mask_rle) img = cv2.resize(img, (256, 256)) mask = cv2.resize(mask, (256, 256)) x_batch += [img] y_batch += [mask] x_batch = np.array(x_batch) / 255. y_batch = np.array(y_batch) yield x_batch, np.expand_dims(y_batch, -1)
im_id = 5 fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(25, 25)) axes[0].imshow(x[im_id]) axes[1].imshow(pred[im_id, ..., 0] > 0.5) plt.show()
结果的输出=保证与编写的代码接触。在这种情况下,不需要异常处理。
以上是高效的错误处理程序的详细内容。更多信息请关注PHP中文网其他相关文章!