Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robodummy #57

Draft
wants to merge 45 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
5b8c573
robo user first commit
Pranjal-sopho Jun 11, 2019
04e3fd9
Merge pull request #1 from ilastik/master
Pranjal-sopho Jun 11, 2019
73e4c47
some changes
Pranjal-sopho Jun 11, 2019
2dec996
Merge remote-tracking branch 'origin/master' into robodummy
Pranjal-sopho Jun 11, 2019
3d66cbc
structural changes
Pranjal-sopho Jun 12, 2019
7bf8f81
some structural changes
Pranjal-sopho Jun 13, 2019
d157d9d
deleted unnecessary files
Pranjal-sopho Jun 13, 2019
39fb936
changed tiling to generic+ apply black
Pranjal-sopho Jun 13, 2019
52d0114
switched to n5 for better mem mang.
Pranjal-sopho Jun 17, 2019
83316c2
incorporated suggestions
Pranjal-sopho Jun 18, 2019
8814e98
added test folder
Pranjal-sopho Jun 18, 2019
eb19bb3
indexing now generic and loss configurable
Pranjal-sopho Jun 21, 2019
8220b95
added basic tensorboard logger
Pranjal-sopho Jun 21, 2019
35af962
made changes acc to previous review
Pranjal-sopho Jun 25, 2019
ae97dbd
tiling now completely generic+other modifications
Pranjal-sopho Jun 27, 2019
3b8b7d7
bug fixed in tile_image + other changes
Pranjal-sopho Jul 2, 2019
84d61cb
tests now running
Pranjal-sopho Jul 5, 2019
28640d6
Merge pull request #2 from ilastik/master
Pranjal-sopho Jul 5, 2019
997c4eb
more tensorboard logging
Pranjal-sopho Jul 5, 2019
d355deb
Merge branch 'master' into obodummy
Pranjal-sopho Jul 5, 2019
a9cfecd
tensorboard errors fixed
Pranjal-sopho Jul 8, 2019
fd1eb97
new strategy added
Pranjal-sopho Jul 10, 2019
cba7191
sparse annotation strategies added
Pranjal-sopho Jul 11, 2019
f265faf
video labelling strategies added
Pranjal-sopho Jul 17, 2019
fcce3f2
class annotator added
Pranjal-sopho Jul 19, 2019
4daa134
all bugs fixed
Pranjal-sopho Jul 22, 2019
6be7a82
apply black
Pranjal-sopho Jul 22, 2019
f38692a
Merge pull request #3 from ilastik/master
Pranjal-sopho Jul 22, 2019
27c4f43
new commit
Pranjal-sopho Jul 22, 2019
d1a1cdc
added gitignore
Pranjal-sopho Jul 22, 2019
064c27c
Merge branch 'master' into robodummy
Pranjal-sopho Jul 22, 2019
4c6d9cb
testing..
Pranjal-sopho Jul 23, 2019
3940c55
Update environemnt file to include MrRobot deps
m-novikov Jul 23, 2019
5da186d
Add __init__ and __main__
m-novikov Jul 23, 2019
f45d619
Make __main__ work
m-novikov Jul 23, 2019
3db31e9
Fix confusion_matrix nans
m-novikov Jul 23, 2019
79a77da
Fixes to strategies
m-novikov Jul 23, 2019
6bd9d7e
Fix unstopabble predictions of inferno trainer
m-novikov Jul 23, 2019
055d08f
Add train_for method to tiktorch server
m-novikov Jul 23, 2019
3eec9c8
Use train_for in mr_robot
m-novikov Jul 23, 2019
8e907e3
training problems fixed (temporarily)
Pranjal-sopho Jul 24, 2019
c6ddc1d
training problems fixed (temporarily)
Pranjal-sopho Jul 24, 2019
617a4df
training problems fixed (temporarily)
Pranjal-sopho Jul 25, 2019
39a674d
updating with code used for result prep
Pranjal-sopho Oct 2, 2019
9050f93
strategy params passed generalized and hardcodings for done for resul…
Pranjal-sopho Nov 11, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ tiktorch/.idea
tiktorch/__pycache/
/#wrapper.py#
/.#wrapper.py#
.py~
.py~
*.nn
*.hdf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no need to ignore .nn and .hdf files (as there are none in the repo). Pls remove

6 changes: 6 additions & 0 deletions lala.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import yaml
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete this file

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also neuronid.txt seems unused so it is also a candidate for deletion from this pull request.


with open("tests/data/CREMI_DUNet_pretrained_new/robot_config.yml") as f:
config_dict = yaml.load(f)


2 changes: 2 additions & 0 deletions mr_robot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*nn
*hdf
FynnBe marked this conversation as resolved.
Show resolved Hide resolved
141 changes: 141 additions & 0 deletions mr_robot/mr_robot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# import sys
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should avoid uncommented import statements (just remove this line)

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as f
from sklearn.metrics import mean_squared_error
from model import DUNet2D
import h5py
from scipy.ndimage import convolve
from torch.autograd import Variable
from collections import OrderedDict
import yaml
from tiktorch.server import TikTorchServer
from tiktorch.rpc import Client, Server, InprocConnConf
from tiktorch.rpc_interface import INeuralNetworkAPI
from tiktorch.types import NDArray, NDArrayBatch
from utils import *

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe sort the import statements a little don't mix import... and from... too much

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patch_size = 16


class MrRobot:

def __init__(self):
# start the server
self.new_server = TikTorchServer()

def load_data(self):
with h5py.File("train.hdf", "r") as f:
x = np.array(f.get("volumes/labels/neuron_ids"))
y = np.array(f.get("volumes/raw"))

self.labels = []
self.ip = []

for i in range(0, 1):
self.labels.append(make_edges3d(np.expand_dims(x[i], axis=0)))
self.ip.append(make_edges3d(np.expand_dims(y[i], axis=0)))

self.labels = np.asarray(self.labels)[:, :, 0:patch_size, 0:patch_size]
self.ip = NDArray(np.asarray(self.ip)[:, :, 0:patch_size, 0:patch_size])
print("data loaded")
return (ip, labels)

def load_model(self):
# load the model
with open("state.nn", mode="rb") as f:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed these paths should be moved to robot config.

binary_state = f.read()
with open("model.py", mode="rb") as f:
model_file = f.read()

with open("robo_config.yml", mode = "r") as f:
base_config = yaml.load(f)

fut = self.new_server.load_model(base_config, model_file, binary_state, b"", ["cpu"])
print("model loaded")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[optional] use a logger, instead of print:

import logging
logger = logging.getLogger(__name__)
...
logger.info("model loaded")

more at https://docs.python.org/3/howto/logging.html

#print(fut.result())

def resume(self):
self.new_server.resume_training()
print("training resumed")

def predict(self):
self.op = new_server.forward(self.ip)
self.op = op.result().as_numpy()
print("prediction run")
return (self.op, self.labels)

def add(self, row, column):
self.ip = self.ip.as_numpy()[
0, :, patch_size * row : patch_size * (row + 1), patch_size * column : patch_size * (column + 1)
].astype(float)
self.label = self.labels[
0, :, patch_size * row : patch_size * (row + 1), patch_size * column : patch_size * (column + 1)
].astype(float)
#print(ip.dtype, label.dtype)
self.new_server.update_training_data(NDArrayBatch([NDArray(self.ip)]), NDArrayBatch([self.label]))

# annotate worst patch
def dense_annotate(self, x, y, label, image):
raise NotImplementedError

def terminate():
new_server.shutdown()

class BaseStrategy:

def __init__():
raise NotImplementedError
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__init__ should not raise NotImplementedError. In fact, it is a good style to call super().__init__() in your derived class...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, this being work in progress, if you want to indicate that your BaseStrategy is not fully implemented yet, this is fine. (Calling super().__init__() in your derived class would still make sense)


# compute loss for a given patch
def base_loss(self, patch, label):
result = mean_squared_error(label, patch) # CHECK THIS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the criterion should be configurable

return result


class Strategy1(BaseStrategy):

def __init__(self,op,labels):
pred_idx = tile_image(op[0, 0].shape, 16)
actual_idx = tile_image(labels[0, 0].shape, 16)
w, h, self.row, self.column = 32, 32, -1, -1
error = 1e7
for i in range(len(pred_patches)):
# print(pred_patches[i].shape, actual_patches[i].shape)
curr_loss = self.loss(op[0,0,pred_idx[i][0]: pred_idx[i][1], pred_idx[i][2]: pred_idx[i][3] ],
labels[0,0,actual_idx[i][0]: actual_idx[i][1], actual_idx[i][2]: actual_idx[i][3] ])
print(curr_loss)
if error > curr_loss:
error = curr_loss
self.row, self.column = int(i / (w / patch_size)), int(i % (w / patch_size))

def get_patch(self):
return (self.row,self.column)

class Strategy2(BaseStrategy):
def __init__():
raise NotImplementedError


class Strategy3(BaseStrategy):
def __init__():
raise NotImplementedError


if __name__ == "__main__":

robo = MrRobot()
robo.load_data()
robo.load_model()
robo.resume() #resume training

#run prediction
op, label = robo.predict()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I think algorithm should be read as follows:

# Step 1. Intialization
robo = MrRobot('/home/user/config.yaml')  # Here robot loads all required data
robo.use_strategy(StrategyRandom())
# or even
robo = MrRobot('/home/user/config.yaml', StrategyRandom)

# Step 2. Start
robo.start()  # Start tiktorch server

# Step 3. Prediction loop
while robo.should_stop():
      robo.predict()

# def robo.predict
# 1. labels? = self.strategy.get_next_patch(<relevant data>)
# 2. self.update_training(labels, ...)

# Step 4. Termination
robo.terminate()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I'd vote for

robo = MrRobot('/home/user/config.yaml', StrategyRandom)

metric = Strategy1(op,label)
row,column = metric.get_patch()
robo.add(row, column)

# shut down server
robo.terminate()
16 changes: 16 additions & 0 deletions mr_robot/robot_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#base config for robot

model_class_name: DUNet2D
model_init_kwargs: {in_channels: 1, out_channels: 1}
training: {
training_shape: [1, 32, 32],
batch_size: 1,
loss_criterion_config: {"method": "MSELoss"},
optimizer_config: {"method": "Adam"},
num_iterations_done: 1
}
validation: {}
dry_run: {
"skip": True,
"shrinkage": [0, 0, 0]
}
155 changes: 155 additions & 0 deletions mr_robot/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
## utility functions for the robot ##
#
def summary(model, input_size, batch_size=-1, device="cuda"):
def register_hook(module):
def hook(module, input, output):
class_name = str(module.__class__).split(".")[-1].split("'")[0]
module_idx = len(summary)

m_key = "%s-%i" % (class_name, module_idx + 1)
summary[m_key] = OrderedDict()
summary[m_key]["input_shape"] = list(input[0].size())
summary[m_key]["input_shape"][0] = batch_size
if isinstance(output, (list, tuple)):
summary[m_key]["output_shape"] = [[-1] + list(o.size())[1:] for o in output]
else:
summary[m_key]["output_shape"] = list(output.size())
summary[m_key]["output_shape"][0] = batch_size

params = 0
if hasattr(module, "weight") and hasattr(module.weight, "size"):
params += torch.prod(torch.LongTensor(list(module.weight.size())))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function seems strange, it uses torch but torch is not imported.

summary[m_key]["trainable"] = module.weight.requires_grad
if hasattr(module, "bias") and hasattr(module.bias, "size"):
params += torch.prod(torch.LongTensor(list(module.bias.size())))
summary[m_key]["nb_params"] = params

if not isinstance(module, nn.Sequential) and not isinstance(module, nn.ModuleList) and not (module == model):
hooks.append(module.register_forward_hook(hook))

device = device.lower()
assert device in ["cuda", "cpu"], "Input device is not valid, please specify 'cuda' or 'cpu'"

if device == "cuda" and torch.cuda.is_available():
dtype = torch.cuda.FloatTensor
else:
dtype = torch.FloatTensor

# multiple inputs to the network
if isinstance(input_size, tuple):
input_size = [input_size]

# batch_size of 2 for batchnorm
x = [torch.rand(2, *in_size).type(dtype) for in_size in input_size]
# print(type(x[0]))

# create properties
summary = OrderedDict()
hooks = []

# register hook
model.apply(register_hook)

# make a forward pass
# print(x.shape)
model(*x)

# remove these hooks
for h in hooks:
h.remove()

print("----------------------------------------------------------------")
line_new = "{:>20} {:>25} {:>15}".format("Layer (type)", "Output Shape", "Param #")
print(line_new)
print("================================================================")
total_params = 0
total_output = 0
trainable_params = 0
for layer in summary:
# input_shape, output_shape, trainable, nb_params
line_new = "{:>20} {:>25} {:>15}".format(
layer, str(summary[layer]["output_shape"]), "{0:,}".format(summary[layer]["nb_params"])
)
total_params += summary[layer]["nb_params"]
total_output += np.prod(summary[layer]["output_shape"])
if "trainable" in summary[layer]:
if summary[layer]["trainable"] == True:
trainable_params += summary[layer]["nb_params"]
print(line_new)

# assume 4 bytes/number (float on cuda).
total_input_size = abs(np.prod(input_size) * batch_size * 4.0 / (1024 ** 2.0))
total_output_size = abs(2.0 * total_output * 4.0 / (1024 ** 2.0)) # x2 for gradients
total_params_size = abs(total_params.numpy() * 4.0 / (1024 ** 2.0))
total_size = total_params_size + total_output_size + total_input_size

print("================================================================")
print("Total params: {0:,}".format(total_params))
print("Trainable params: {0:,}".format(trainable_params))
print("Non-trainable params: {0:,}".format(total_params - trainable_params))
print("----------------------------------------------------------------")
print("Input size (MB): %0.2f" % total_input_size)
print("Forward/backward pass size (MB): %0.2f" % total_output_size)
print("Params size (MB): %0.2f" % total_params_size)
print("Estimated Total Size (MB): %0.2f" % total_size)
print("----------------------------------------------------------------")


def make_edges3d(segmentation):
FynnBe marked this conversation as resolved.
Show resolved Hide resolved
""" Make 3d edge volume from 3d segmentation
"""
# NOTE we add one here to make sure that we don't have zero in the segmentation
gz = convolve(segmentation + 1, np.array([-1.0, 0.0, 1.0]).reshape(3, 1, 1))
gy = convolve(segmentation + 1, np.array([-1.0, 0.0, 1.0]).reshape(1, 3, 1))
gx = convolve(segmentation + 1, np.array([-1.0, 0.0, 1.0]).reshape(1, 1, 3))
return (gx ** 2 + gy ** 2 + gz ** 2) > 0

# create patches
def tile_image2D(image_shape, tile_size):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems to me that image tiling could nicely be implemented for n dimensions. Maybe have a look at https://github.com/ilastik/lazyflow/blob/dfbb450989d4f790f5b19170383b777fb88be0e8/lazyflow/roi.py#L473 for some inspiration

tiles = []
(w, h) = image_shape
for wsi in range(0, w - tile_size + 1, int(tile_size)):
for hsi in range(0, h - tile_size + 1, int(tile_size)):
img = (wsi,wsi + tile_size, hsi, hsi + tile_size)
tiles.append(img)

if h % tile_size != 0:
for wsi in range(0, w - tile_size + 1, int(tile_size)):
img = (wsi, wsi + tile_size, h - tile_size, h)
tiles.append(img)

if w % tile_size != 0:
for hsi in range(0, h - tile_size + 1, int(tile_size)):
img = (w - tile_size, w, hsi, hsi + tile_size)
tiles.append(img)

if w % tile_size != 0 and h % tile_size != 0:
img = (w - tile_size, w, h - tile_size, h)
tiles.append(img)

return tiles

def tile_image3D(image_shape,tile_size):
tiles = []
(z, w, h) = image_shape
for wsi in range(0, w - tile_size + 1, int(tile_size)):
for hsi in range(0, h - tile_size + 1, int(tile_size)):
img = (:,wsi : wsi + tile_size, hsi : hsi + tile_size)
tiles.append(img)

if h % tile_size != 0:
for wsi in range(0, w - tile_size + 1, int(tile_size)):
img = (wsi : wsi + tile_size, h - tile_size :)
tiles.append(img)

if w % tile_size != 0:
for hsi in range(0, h - tile_size + 1, int(tile_size)):
img = (w - tile_size :, hsi : hsi + tile_size)
tiles.append(img)

if w % tile_size != 0 and h % tile_size != 0:
img = (w - tile_size :, h - tile_size :)
tiles.append(img)

return tiles
Loading