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
Layer or Model in invalid state TF 2.12 - bioimageio export - _compute_receptive_field #229
Comments
Hi @esgomezm, sorry for the late reply. I already mentioned a workaround here, but I assume you want something that works with TensorFlow 2? I think the TF model export has to be different in TensorFlow 2, but I don't even know what the format should look like. I need a specific test case, i.e. a consumer for the exported model that can tell me whether the export was correct. If you already know the answers to these questions, you can write/prototype it yourself. It's a bit hacky, but I think you can override def my_new_export_TF(self, fname=None, single_output=True, upsample_grid=True):
// new code that doesn't call csbdeep.utils.tf.export_SavedModel
...
model.export_TF = my_new_export_TF
export_bioimageio(model, ...) |
Hi Uwe, Thanks for the replay. The problem is not the export but running def _my_compute_receptive_field(model, img_size=None):
# TODO: good enough?
from scipy.ndimage import zoom
if img_size is None:
img_size = tuple(g*(128 if model.config.n_dim==2 else 64) for g in model.config.grid)
if np.isscalar(img_size):
img_size = (img_size,) * model.config.n_dim
img_size = tuple(img_size)
# print(img_size)
assert all(_is_power_of_2(s) for s in img_size)
mid = tuple(s//2 for s in img_size)
x = np.zeros((1,)+img_size+(model.config.n_channel_in,), dtype=np.float32)
z = np.zeros_like(x)
x[(0,)+mid+(slice(None),)] = 1
#y = self.keras_model.predict(x)[0][0,...,0]
#y0 = self.keras_model.predict(z)[0][0,...,0]
y = model.predict(np.squeeze(x))[0]
y0 = model.predict(np.squeeze(z))[0]
grid = tuple((np.array(x.shape[1:-1])/np.array(y.shape)).astype(int))
assert grid == model.config.grid
y = zoom(y, grid,order=0)
y0 = zoom(y0,grid,order=0)
ind = np.where(np.abs(y-y0)>0)
return [(m-np.min(i), np.max(i)-m) for (m,i) in zip(mid,ind)]
model._tile_overlap = _my_compute_receptive_field(model)
export_bioimageio(model, modelfile, test_img) |
I'm confused, this is the first time I'm hearing about this. So what's the actual problem again? I still don't understand. |
Hi @uschmidt83 The problem is that with TensorFlow 2.12 |
In the end,
I believe this is just the symptom, not the real cause of the model export problem. Hence, I'm confused why your fix via I feel like we're not making much progress here. Can you make a minimal example for me to reproduce the problem? I.e. a few lines of code that trigger the problem and which version of TensorFlow to use. |
Describe the bug
I'm trying to export a StarDist model using the function
export_bioimageio
but I got an error "Your Layer or Model is in an invalid state". Unfortunately, this breaks the compatibility with the bioimage model zoo. Seems the error comes from the CSBDeep models as it fails when runningmodel.keras_mode.predict
rather than StarDist directly. This error was already reported here. Is there any workaround we could do?To reproduce
I'm using the ZeroCostDL4Mic that installs TensorFlow 2.12 (the latest TF version given by google colab). Everything works well expect the export.
Data and screenshots
Environment (please complete the following information):
The text was updated successfully, but these errors were encountered: