Skip to content

Commit

Permalink
remove densecrf parameter and code
Browse files Browse the repository at this point in the history
  • Loading branch information
Waliens committed Dec 12, 2019
1 parent d53258a commit 16c1f24
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 49 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 1 addition & 12 deletions descriptor.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
37 changes: 2 additions & 35 deletions wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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


Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 16c1f24

Please sign in to comment.