Open hungyiwu opened 4 years ago
segmentify should work for non-square images (width != length, ex. 512x512 is square, 511x512 is not square)
If you wanted to work on a fix for this @hungyiwu I'd be happy to review and merge! It likely involves some of the padding code
I looked into the models and the issue is not strictly square vs. non-square images, but images of shape 512x512 vs. otherwise, as this following didn't work either
example_image = example_image[1:, 1:]
example_labels = example_labels[1:, 1:]
I realized the original model was trained using 512x512 images and likely only support predictions of this input shape. So perhaps not really a bug but we can add documentation to remind users this pre-trained model supports this shape, and if they want something else they can train their own model for that.
Any thoughts? @sofroniewn
I realized the original model was trained using 512x512 images and likely only support predictions of this input shape. So perhaps not really a bug but we can add documentation to remind users this pre-trained model supports this shape, and if they want something else they can train their own model for that.
Ah, makes sense! That sounds like a good addition to the docs. Thanks!!
Expected behavior
segmentify
should work for non-square images (width != length, ex. 512x512 is square, 511x512 is not square)Potentially relevant issues/PR
11 #13
How to reproduce the bug
Below is a slightly modified version of https://github.com/transformify-plugins/segmentify/blob/master/examples/launch.py
execute
button, confirm workingnapari.gui_qt
; run the code, popping the napari window, clickexecute
button, and confirm it crashesparse input file
example_image = os.path.join(os.path.abspath(os.path.dirname(file)), "hpa.png") example_labels = os.path.join(os.path.abspath(os.path.dirname(file)), "hpa_labels.tif")
example_image = io.imread(example_image) example_labels = io.imread(example_labels)
WARNING: uncomment these two lines below to reproduce bug
example_image = example_image[:, 1:]
example_labels = example_labels[:, 1:]
##########################################################
with napari.gui_qt(): viewer = napari.Viewer()
Segmenting... WARNING: Traceback (most recent call last): File "/home/hungyiwu/.venv/segmentify/lib/python3.8/site-packages/magicgui-0.1.5-py3.8.egg/magicgui/core.py", line 199, in
self.call_button.clicked.connect(lambda checked: self.call())
File "/home/hungyiwu/.venv/segmentify/lib/python3.8/site-packages/magicgui-0.1.5-py3.8.egg/magicgui/core.py", line 526, in call
value = self.func(_kwargs)
File "/home/hungyiwu/.venv/segmentify/lib/python3.8/site-packages/segmentify-0.1.1-py3.8.egg/segmentify/gui.py", line 29, in segmentation
clf, features = fit(data, initial_labels.data, featurizer=featurizer.value)
File "/home/hungyiwu/.venv/segmentify/lib/python3.8/site-packages/segmentify-0.1.1-py3.8.egg/segmentify/semantic/main.py", line 129, in fit
padded_features = unet_featurize(padded_image, featurizer)
File "/home/hungyiwu/.venv/segmentify/lib/python3.8/site-packages/segmentify-0.1.1-py3.8.egg/segmentify/semantic/main.py", line 59, in unet_featurize
features = model(image)
File "/home/hungyiwu/.venv/segmentify/lib/python3.8/site-packages/torch-1.5.1-py3.8-linux-x86_64.egg/torch/nn/modules/module.py", line 550, in call
result = self.forward(*input, *kwargs)
File "/home/hungyiwu/.venv/segmentify/lib/python3.8/site-packages/segmentify-0.1.1-py3.8.egg/segmentify/model/unet.py", line 57, in forward
x = self.decode_4(x, skip_4)
File "/home/hungyiwu/.venv/segmentify/lib/python3.8/site-packages/torch-1.5.1-py3.8-linux-x86_64.egg/torch/nn/modules/module.py", line 550, in call
result = self.forward(input, kwargs)
File "/home/hungyiwu/.venv/segmentify/lib/python3.8/site-packages/segmentify-0.1.1-py3.8.egg/segmentify/model/layers/unet_layers.py", line 63, in forward
output += skip
RuntimeError: The size of tensor a (62) must match the size of tensor b (63) at non-singleton dimension 3
Aborted (core dumped)