tbepler / topaz

Pipeline for particle picking in cryo-electron microscopy images using convolutional neural networks trained from positive and unlabeled examples. Also featuring micrograph and tomogram denoising with DNNs.
GNU General Public License v3.0
169 stars 63 forks source link

Support MRC mode 12 #97

Closed biochem-fan closed 3 years ago

biochem-fan commented 3 years ago

The upcoming RELION 4.0 will introduce MRC mode 12 (half precision float, i.e., float16) to save disk space.

Although RELION's Topaz wrapper converts float16 into float32 before calling Topaz, it would be nice if Topaz itself supports it in case people use it outside RELION.

A sample image is in ftp://ftp.mrc-lmb.cam.ac.uk/pub/tnakane/20170629_00024_frameImage_float16.mrc.

The following patch fixes topaz preprocess. Other sub-commands (denoise, particle_stack) probably need more changes but I don't have time to work on them at the moment.

diff --git a/topaz/mrc.py b/topaz/mrc.py
index 679748d..646ce09 100644
--- a/topaz/mrc.py
+++ b/topaz/mrc.py
@@ -130,6 +130,8 @@ def parse(content):
         dtype = np.uint16
     elif header.mode == 16:
         dtype = '3B' # RGB values
+    elif header.mode == 12:
+        dtype = np.float16
     else:
         raise Exception('Unknown dtype mode:' + str(header.model))

diff --git a/topaz/utils/data/loader.py b/topaz/utils/data/loader.py
index c9a88ed..2cbc82e 100644
--- a/topaz/utils/data/loader.py
+++ b/topaz/utils/data/loader.py
@@ -49,6 +49,8 @@ def load_mrc(path, standardize=False):
     with open(path, 'rb') as f:
         content = f.read()
     image, header, extended_header = mrc.parse(content)
+    if image.dtype == np.float16:
+        image = image.astype(np.float32)
     if standardize:
         image = image - header.amean
         image /= header.rms
tbepler commented 3 years ago

Sure, happy to include this. Can you submit it as a pull request?

biochem-fan commented 3 years ago

OK, I made a pull request.

I tested this on two commands:

python -m topaz.main preprocess -s 8
python -m topaz.main denoise --patch-size 1024

P.S.

I think the default value for --patch-size should be changed to something reasonable for typical GPUs (768 or 1024).

tbepler commented 3 years ago

Thanks, pull request merged.

I think the default value for --patch-size should be changed to something reasonable for typical GPUs (768 or 1024).

Seems reasonable. I changed it to 1024.