|
|
@ -62,7 +62,7 @@ class SbbBinarizer:
|
|
|
|
n_classes = model.layers[len(model.layers)-1].output_shape[3]
|
|
|
|
n_classes = model.layers[len(model.layers)-1].output_shape[3]
|
|
|
|
return model, model_height, model_width, n_classes
|
|
|
|
return model, model_height, model_width, n_classes
|
|
|
|
|
|
|
|
|
|
|
|
def predict(self, model_in, img, use_patches):
|
|
|
|
def predict(self, model_in, img):
|
|
|
|
tensorflow_backend.set_session(self.session)
|
|
|
|
tensorflow_backend.set_session(self.session)
|
|
|
|
model, model_height, model_width, n_classes = model_in
|
|
|
|
model, model_height, model_width, n_classes = model_in
|
|
|
|
|
|
|
|
|
|
|
@ -101,152 +101,136 @@ class SbbBinarizer:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
img = np.copy(img_padded)
|
|
|
|
img = np.copy(img_padded)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if use_patches:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
margin = int(0.1 * model_width)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
width_mid = model_width - 2 * margin
|
|
|
|
margin = int(0.1 * model_width)
|
|
|
|
height_mid = model_height - 2 * margin
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
width_mid = model_width - 2 * margin
|
|
|
|
|
|
|
|
height_mid = model_height - 2 * margin
|
|
|
|
|
|
|
|
|
|
|
|
img = img / float(255.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
img_h = img.shape[0]
|
|
|
|
img = img / float(255.0)
|
|
|
|
img_w = img.shape[1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prediction_true = np.zeros((img_h, img_w, 3))
|
|
|
|
img_h = img.shape[0]
|
|
|
|
mask_true = np.zeros((img_h, img_w))
|
|
|
|
img_w = img.shape[1]
|
|
|
|
nxf = img_w / float(width_mid)
|
|
|
|
|
|
|
|
nyf = img_h / float(height_mid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if nxf > int(nxf):
|
|
|
|
prediction_true = np.zeros((img_h, img_w, 3))
|
|
|
|
nxf = int(nxf) + 1
|
|
|
|
mask_true = np.zeros((img_h, img_w))
|
|
|
|
else:
|
|
|
|
nxf = img_w / float(width_mid)
|
|
|
|
nxf = int(nxf)
|
|
|
|
nyf = img_h / float(height_mid)
|
|
|
|
|
|
|
|
|
|
|
|
if nyf > int(nyf):
|
|
|
|
if nxf > int(nxf):
|
|
|
|
nyf = int(nyf) + 1
|
|
|
|
nxf = int(nxf) + 1
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
nyf = int(nyf)
|
|
|
|
nxf = int(nxf)
|
|
|
|
|
|
|
|
|
|
|
|
for i in range(nxf):
|
|
|
|
|
|
|
|
for j in range(nyf):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if i == 0:
|
|
|
|
if nyf > int(nyf):
|
|
|
|
index_x_d = i * width_mid
|
|
|
|
nyf = int(nyf) + 1
|
|
|
|
index_x_u = index_x_d + model_width
|
|
|
|
else:
|
|
|
|
elif i > 0:
|
|
|
|
nyf = int(nyf)
|
|
|
|
index_x_d = i * width_mid
|
|
|
|
|
|
|
|
index_x_u = index_x_d + model_width
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if j == 0:
|
|
|
|
for i in range(nxf):
|
|
|
|
index_y_d = j * height_mid
|
|
|
|
for j in range(nyf):
|
|
|
|
index_y_u = index_y_d + model_height
|
|
|
|
|
|
|
|
elif j > 0:
|
|
|
|
|
|
|
|
index_y_d = j * height_mid
|
|
|
|
|
|
|
|
index_y_u = index_y_d + model_height
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if index_x_u > img_w:
|
|
|
|
if i == 0:
|
|
|
|
index_x_u = img_w
|
|
|
|
index_x_d = i * width_mid
|
|
|
|
index_x_d = img_w - model_width
|
|
|
|
index_x_u = index_x_d + model_width
|
|
|
|
if index_y_u > img_h:
|
|
|
|
elif i > 0:
|
|
|
|
index_y_u = img_h
|
|
|
|
index_x_d = i * width_mid
|
|
|
|
index_y_d = img_h - model_height
|
|
|
|
index_x_u = index_x_d + model_width
|
|
|
|
|
|
|
|
|
|
|
|
img_patch = img[index_y_d:index_y_u, index_x_d:index_x_u, :]
|
|
|
|
if j == 0:
|
|
|
|
|
|
|
|
index_y_d = j * height_mid
|
|
|
|
|
|
|
|
index_y_u = index_y_d + model_height
|
|
|
|
|
|
|
|
elif j > 0:
|
|
|
|
|
|
|
|
index_y_d = j * height_mid
|
|
|
|
|
|
|
|
index_y_u = index_y_d + model_height
|
|
|
|
|
|
|
|
|
|
|
|
label_p_pred = model.predict(img_patch.reshape(1, img_patch.shape[0], img_patch.shape[1], img_patch.shape[2]))
|
|
|
|
if index_x_u > img_w:
|
|
|
|
|
|
|
|
index_x_u = img_w
|
|
|
|
|
|
|
|
index_x_d = img_w - model_width
|
|
|
|
|
|
|
|
if index_y_u > img_h:
|
|
|
|
|
|
|
|
index_y_u = img_h
|
|
|
|
|
|
|
|
index_y_d = img_h - model_height
|
|
|
|
|
|
|
|
|
|
|
|
seg = np.argmax(label_p_pred, axis=3)[0]
|
|
|
|
img_patch = img[index_y_d:index_y_u, index_x_d:index_x_u, :]
|
|
|
|
|
|
|
|
|
|
|
|
seg_color = np.repeat(seg[:, :, np.newaxis], 3, axis=2)
|
|
|
|
label_p_pred = model.predict(img_patch.reshape(1, img_patch.shape[0], img_patch.shape[1], img_patch.shape[2]))
|
|
|
|
|
|
|
|
|
|
|
|
if i == 0 and j == 0:
|
|
|
|
seg = np.argmax(label_p_pred, axis=3)[0]
|
|
|
|
seg_color = seg_color[0:seg_color.shape[0] - margin, 0:seg_color.shape[1] - margin, :]
|
|
|
|
|
|
|
|
seg = seg[0:seg.shape[0] - margin, 0:seg.shape[1] - margin]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + 0:index_x_u - margin] = seg
|
|
|
|
seg_color = np.repeat(seg[:, :, np.newaxis], 3, axis=2)
|
|
|
|
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + 0:index_x_u - margin, :] = seg_color
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif i == nxf-1 and j == nyf-1:
|
|
|
|
if i == 0 and j == 0:
|
|
|
|
seg_color = seg_color[margin:seg_color.shape[0] - 0, margin:seg_color.shape[1] - 0, :]
|
|
|
|
seg_color = seg_color[0:seg_color.shape[0] - margin, 0:seg_color.shape[1] - margin, :]
|
|
|
|
seg = seg[margin:seg.shape[0] - 0, margin:seg.shape[1] - 0]
|
|
|
|
seg = seg[0:seg.shape[0] - margin, 0:seg.shape[1] - margin]
|
|
|
|
|
|
|
|
|
|
|
|
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - 0] = seg
|
|
|
|
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + 0:index_x_u - margin] = seg
|
|
|
|
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - 0, :] = seg_color
|
|
|
|
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + 0:index_x_u - margin, :] = seg_color
|
|
|
|
|
|
|
|
|
|
|
|
elif i == 0 and j == nyf-1:
|
|
|
|
elif i == nxf-1 and j == nyf-1:
|
|
|
|
seg_color = seg_color[margin:seg_color.shape[0] - 0, 0:seg_color.shape[1] - margin, :]
|
|
|
|
seg_color = seg_color[margin:seg_color.shape[0] - 0, margin:seg_color.shape[1] - 0, :]
|
|
|
|
seg = seg[margin:seg.shape[0] - 0, 0:seg.shape[1] - margin]
|
|
|
|
seg = seg[margin:seg.shape[0] - 0, margin:seg.shape[1] - 0]
|
|
|
|
|
|
|
|
|
|
|
|
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + 0:index_x_u - margin] = seg
|
|
|
|
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - 0] = seg
|
|
|
|
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + 0:index_x_u - margin, :] = seg_color
|
|
|
|
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - 0, :] = seg_color
|
|
|
|
|
|
|
|
|
|
|
|
elif i == nxf-1 and j == 0:
|
|
|
|
elif i == 0 and j == nyf-1:
|
|
|
|
seg_color = seg_color[0:seg_color.shape[0] - margin, margin:seg_color.shape[1] - 0, :]
|
|
|
|
seg_color = seg_color[margin:seg_color.shape[0] - 0, 0:seg_color.shape[1] - margin, :]
|
|
|
|
seg = seg[0:seg.shape[0] - margin, margin:seg.shape[1] - 0]
|
|
|
|
seg = seg[margin:seg.shape[0] - 0, 0:seg.shape[1] - margin]
|
|
|
|
|
|
|
|
|
|
|
|
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - 0] = seg
|
|
|
|
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + 0:index_x_u - margin] = seg
|
|
|
|
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - 0, :] = seg_color
|
|
|
|
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + 0:index_x_u - margin, :] = seg_color
|
|
|
|
|
|
|
|
|
|
|
|
elif i == 0 and j != 0 and j != nyf-1:
|
|
|
|
elif i == nxf-1 and j == 0:
|
|
|
|
seg_color = seg_color[margin:seg_color.shape[0] - margin, 0:seg_color.shape[1] - margin, :]
|
|
|
|
seg_color = seg_color[0:seg_color.shape[0] - margin, margin:seg_color.shape[1] - 0, :]
|
|
|
|
seg = seg[margin:seg.shape[0] - margin, 0:seg.shape[1] - margin]
|
|
|
|
seg = seg[0:seg.shape[0] - margin, margin:seg.shape[1] - 0]
|
|
|
|
|
|
|
|
|
|
|
|
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + 0:index_x_u - margin] = seg
|
|
|
|
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - 0] = seg
|
|
|
|
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + 0:index_x_u - margin, :] = seg_color
|
|
|
|
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - 0, :] = seg_color
|
|
|
|
|
|
|
|
|
|
|
|
elif i == nxf-1 and j != 0 and j != nyf-1:
|
|
|
|
elif i == 0 and j != 0 and j != nyf-1:
|
|
|
|
seg_color = seg_color[margin:seg_color.shape[0] - margin, margin:seg_color.shape[1] - 0, :]
|
|
|
|
seg_color = seg_color[margin:seg_color.shape[0] - margin, 0:seg_color.shape[1] - margin, :]
|
|
|
|
seg = seg[margin:seg.shape[0] - margin, margin:seg.shape[1] - 0]
|
|
|
|
seg = seg[margin:seg.shape[0] - margin, 0:seg.shape[1] - margin]
|
|
|
|
|
|
|
|
|
|
|
|
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - 0] = seg
|
|
|
|
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + 0:index_x_u - margin] = seg
|
|
|
|
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - 0, :] = seg_color
|
|
|
|
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + 0:index_x_u - margin, :] = seg_color
|
|
|
|
|
|
|
|
|
|
|
|
elif i != 0 and i != nxf-1 and j == 0:
|
|
|
|
elif i == nxf-1 and j != 0 and j != nyf-1:
|
|
|
|
seg_color = seg_color[0:seg_color.shape[0] - margin, margin:seg_color.shape[1] - margin, :]
|
|
|
|
seg_color = seg_color[margin:seg_color.shape[0] - margin, margin:seg_color.shape[1] - 0, :]
|
|
|
|
seg = seg[0:seg.shape[0] - margin, margin:seg.shape[1] - margin]
|
|
|
|
seg = seg[margin:seg.shape[0] - margin, margin:seg.shape[1] - 0]
|
|
|
|
|
|
|
|
|
|
|
|
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - margin] = seg
|
|
|
|
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - 0] = seg
|
|
|
|
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - margin, :] = seg_color
|
|
|
|
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - 0, :] = seg_color
|
|
|
|
|
|
|
|
|
|
|
|
elif i != 0 and i != nxf-1 and j == nyf-1:
|
|
|
|
elif i != 0 and i != nxf-1 and j == 0:
|
|
|
|
seg_color = seg_color[margin:seg_color.shape[0] - 0, margin:seg_color.shape[1] - margin, :]
|
|
|
|
seg_color = seg_color[0:seg_color.shape[0] - margin, margin:seg_color.shape[1] - margin, :]
|
|
|
|
seg = seg[margin:seg.shape[0] - 0, margin:seg.shape[1] - margin]
|
|
|
|
seg = seg[0:seg.shape[0] - margin, margin:seg.shape[1] - margin]
|
|
|
|
|
|
|
|
|
|
|
|
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - margin] = seg
|
|
|
|
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - margin] = seg
|
|
|
|
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - margin, :] = seg_color
|
|
|
|
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - margin, :] = seg_color
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
elif i != 0 and i != nxf-1 and j == nyf-1:
|
|
|
|
seg_color = seg_color[margin:seg_color.shape[0] - margin, margin:seg_color.shape[1] - margin, :]
|
|
|
|
seg_color = seg_color[margin:seg_color.shape[0] - 0, margin:seg_color.shape[1] - margin, :]
|
|
|
|
seg = seg[margin:seg.shape[0] - margin, margin:seg.shape[1] - margin]
|
|
|
|
seg = seg[margin:seg.shape[0] - 0, margin:seg.shape[1] - margin]
|
|
|
|
|
|
|
|
|
|
|
|
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - margin] = seg
|
|
|
|
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - margin] = seg
|
|
|
|
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - margin, :] = seg_color
|
|
|
|
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - margin, :] = seg_color
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prediction_true = prediction_true[index_start_h: index_start_h+img_org_h, index_start_w: index_start_w+img_org_w,:]
|
|
|
|
|
|
|
|
prediction_true = prediction_true.astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
img_h_page = img.shape[0]
|
|
|
|
seg_color = seg_color[margin:seg_color.shape[0] - margin, margin:seg_color.shape[1] - margin, :]
|
|
|
|
img_w_page = img.shape[1]
|
|
|
|
seg = seg[margin:seg.shape[0] - margin, margin:seg.shape[1] - margin]
|
|
|
|
img = img / float(255.0)
|
|
|
|
|
|
|
|
img = resize_image(img, model_height, model_width)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
label_p_pred = model.predict(img.reshape(1, img.shape[0], img.shape[1], img.shape[2]))
|
|
|
|
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - margin] = seg
|
|
|
|
|
|
|
|
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - margin, :] = seg_color
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prediction_true = prediction_true[index_start_h: index_start_h+img_org_h, index_start_w: index_start_w+img_org_w,:]
|
|
|
|
|
|
|
|
prediction_true = prediction_true.astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
|
|
seg = np.argmax(label_p_pred, axis=3)[0]
|
|
|
|
|
|
|
|
seg_color = np.repeat(seg[:, :, np.newaxis], 3, axis=2)
|
|
|
|
|
|
|
|
prediction_true = resize_image(seg_color, img_h_page, img_w_page)
|
|
|
|
|
|
|
|
prediction_true = prediction_true.astype(np.uint8)
|
|
|
|
|
|
|
|
return prediction_true[:,:,0]
|
|
|
|
return prediction_true[:,:,0]
|
|
|
|
|
|
|
|
|
|
|
|
def run(self, image=None, image_path=None, save=None, use_patches=False):
|
|
|
|
def run(self, image=None, image_path=None, save=None):
|
|
|
|
if (image is not None and image_path is not None) or \
|
|
|
|
if (image is not None and image_path is not None) or \
|
|
|
|
(image is None and image_path is None):
|
|
|
|
(image is None and image_path is None):
|
|
|
|
raise ValueError("Must pass either a opencv2 image or an image_path")
|
|
|
|
raise ValueError("Must pass either a opencv2 image or an image_path")
|
|
|
@ -256,7 +240,7 @@ class SbbBinarizer:
|
|
|
|
for n, (model, model_file) in enumerate(zip(self.models, self.model_files)):
|
|
|
|
for n, (model, model_file) in enumerate(zip(self.models, self.model_files)):
|
|
|
|
self.log.info('Predicting with model %s [%s/%s]' % (model_file, n + 1, len(self.model_files)))
|
|
|
|
self.log.info('Predicting with model %s [%s/%s]' % (model_file, n + 1, len(self.model_files)))
|
|
|
|
|
|
|
|
|
|
|
|
res = self.predict(model, image, use_patches)
|
|
|
|
res = self.predict(model, image)
|
|
|
|
|
|
|
|
|
|
|
|
img_fin = np.zeros((res.shape[0], res.shape[1], 3))
|
|
|
|
img_fin = np.zeros((res.shape[0], res.shape[1], 3))
|
|
|
|
res[:, :][res[:, :] == 0] = 2
|
|
|
|
res[:, :][res[:, :] == 0] = 2
|
|
|
|