From 16c1f248d667c600d76cd50479d647a2347bec33 Mon Sep 17 00:00:00 2001 From: Waliens Date: Thu, 12 Dec 2019 16:34:00 +0100 Subject: [PATCH] remove densecrf parameter and code --- Dockerfile | 4 ++-- descriptor.json | 13 +------------ wrapper.py | 37 ++----------------------------------- 3 files changed, 5 insertions(+), 49 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0251d48..85f5b5b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,13 +11,13 @@ RUN git clone https://github.com/cytomine-uliege/Cytomine-python-client.git && \ # Metric for PixCla is pure python so don't need java, nor binaries RUN apt-get update && apt-get install libgeos-dev -y && apt-get clean RUN git clone https://github.com/Neubias-WG5/neubiaswg5-utilities.git && \ - cd /neubiaswg5-utilities/ && git checkout tags/v0.8.0 && pip install . && \ + cd /neubiaswg5-utilities/ && git checkout tags/v0.8.7 && pip install . && \ rm -r /neubiaswg5-utilities # -------------------------------------------------------------------------------------------- # Install pytorch RUN pip install https://download.pytorch.org/whl/cpu/torch-1.0.0-cp36-cp36m-linux_x86_64.whl -RUN pip install torchvision==0.2.1 pydensecrf==1.0rc3 +RUN pip install torchvision==0.2.1 # -------------------------------------------------------------------------------------------- # Install scripts and models diff --git a/descriptor.json b/descriptor.json index 1471972..454f691 100644 --- a/descriptor.json +++ b/descriptor.json @@ -1,5 +1,5 @@ { - "command-line": "python wrapper.py CYTOMINE_HOST CYTOMINE_PUBLIC_KEY CYTOMINE_PRIVATE_KEY CYTOMINE_ID_PROJECT CYTOMINE_ID_SOFTWARE THRESHOLD USE_CRF ", + "command-line": "python wrapper.py CYTOMINE_HOST CYTOMINE_PUBLIC_KEY CYTOMINE_PRIVATE_KEY CYTOMINE_ID_PROJECT CYTOMINE_ID_SOFTWARE THRESHOLD", "inputs": [ { "name": "Cytomine host", @@ -61,17 +61,6 @@ "id": "threshold", "type": "Number", "command-line-flag": "--@id" - }, - { - "default-value": false, - "name": "Use CRF", - "description": "Use conditional random fields as pre-processing", - "set-by-server": false, - "value-key": "@ID", - "optional": true, - "id": "use_crf", - "type": "Boolean", - "command-line-flag": "--@id" } ], "name": "PixCla-UNet-GlaS", diff --git a/wrapper.py b/wrapper.py index 0b5cc77..036d5d5 100644 --- a/wrapper.py +++ b/wrapper.py @@ -5,7 +5,6 @@ import numpy as np import torch from torchvision import transforms -import pydensecrf.densecrf as dcrf from cytomine.models import Job from neubiaswg5 import CLASS_PIXCLA from neubiaswg5.helpers import get_discipline, NeubiasJob, prepare_data, upload_data, upload_metrics @@ -14,39 +13,11 @@ from unet import UNet -def dense_crf(img, output_probs): - h = output_probs.shape[0] - w = output_probs.shape[1] - - output_probs = np.expand_dims(output_probs, 0) - output_probs = np.append(1 - output_probs, output_probs, axis=0) - - d = dcrf.DenseCRF2D(w, h, 2) - U = -np.log(output_probs) - U = U.reshape((2, -1)) - U = np.ascontiguousarray(U) - img = np.ascontiguousarray(img) - - d.setUnaryEnergy(U) - - d.addPairwiseGaussian(sxy=20, compat=3) - d.addPairwiseBilateral(sxy=30, srgb=20, rgbim=img, compat=10) - - Q = d.inference(5) - Q = np.argmax(np.array(Q), axis=0).reshape((h, w)) - - return Q - - def normalize(x): return x / 255 -def predict_img(net, - full_img, - scale_factor=0.5, - out_threshold=0.5, - use_dense_crf=True): +def predict_img(net, full_img, scale_factor=0.5, out_threshold=0.5): net.eval() height, width, channel = full_img.shape img = cv2.resize(full_img, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_CUBIC) @@ -70,9 +41,6 @@ def predict_img(net, proba = tf(proba.cpu()) mask_np = proba.squeeze().cpu().numpy() - if use_dense_crf: - mask_np = dense_crf(np.array(full_img).astype(np.uint8), mask_np) - return mask_np > out_threshold @@ -101,8 +69,7 @@ def main(argv): mask = predict_img( net=net, full_img=img, scale_factor=0.5, # value used at training - out_threshold=nj.parameters.threshold, - use_dense_crf=nj.parameters.use_crf + out_threshold=nj.parameters.threshold ) imwrite(