BiaPyX / BiaPy

Open source Python library for building bioimage analysis pipelines
https://BiaPyX.github.io
MIT License
127 stars 28 forks source link

`data.generators.simple_data_generator` does not respect empty mask #21

Closed sbinnee closed 2 years ago

sbinnee commented 2 years ago

Desc

Hi. I ran into an error while I tried to load a model and to run a test inference. I modified DATA.TEST.PATH, DATA.TEST.MASK_PATH, and PATHS.CHECKPOINT_FILE. As you can see, I don't have any masks, so I put an empty string.

The error points to simple_data_generator and I saw that it computes max on mask variable, which is actually None. When I put if statement to avoid executing that line, I don't have the error anymore.

Config file

PROBLEM:
    TYPE: DETECTION
    NDIM: 3D

DATA:
    PATCH_SIZE: (256, 256, 20, 3)
    REFLECT_TO_COMPLETE_SHAPE: True
    CHECK_GENERATORS: False
    EXTRACT_RANDOM_PATCH: False
    PROBABILITY_MAP: False
    TRAIN:
        PATH: 'path_to_train_data'
        MASK_PATH: 'path_to_train_data_gt'
        IN_MEMORY: True
        PADDING: (0,0,0)
        OVERLAP: (0,0,0)
    VAL:
        FROM_TRAIN: True
        SPLIT_TRAIN: 0.1
        IN_MEMORY: True
        PADDING: (0,0,0)
        OVERLAP: (0,0,0)
    TEST:
        PATH: './ChroMS/2021-03-24 P14 6520-2 Mcol cortex/test/x'
        MASK_PATH: ''
        IN_MEMORY: True
        LOAD_GT: False
        PADDING: (16,16,0)
        OVERLAP: (0,0,0)

AUGMENTOR:
    ENABLE: True
    AUG_SAMPLES: True
    DRAW_GRID: True
    DA_PROB: 1.0
    CHANNEL_SHUFFLE: False
    MISALIGNMENT: False
    CUTOUT: False
    GRIDMASK: False
    CUTNOISE: False
    CNOISE_SCALE: (0.05, 0.1)
    CUTBLUR: False
    RANDOM_ROT: False
    ROT90: False
    VFLIP: False
    HFLIP: False
    CONTRAST: False
    CONTRAST_FACTOR: (-0.3, 0.3)
    BRIGHTNESS: False
    BRIGHTNESS_FACTOR: (0.1, 0.3)
    GAMMA_CONTRAST: False
    GC_GAMMA: (0.5, 1.5)
    ELASTIC: False
    GRAYSCALE: True
    AFFINE_MODE: 'reflect'

MODEL:
    ARCHITECTURE: unet
    FEATURE_MAPS: [36, 48, 64]
    DROPOUT_VALUES: [0.1, 0.2, 0.3]
    Z_DOWN: 1
    LOAD_CHECKPOINT: False

LOSS:
  TYPE: CE

TRAIN:
    ENABLE: False
    OPTIMIZER: ADAM
    LR: 1.E-4
    BATCH_SIZE: 1
    EPOCHS: 500
    PATIENCE: 20

TEST:
    ENABLE: True
    AUGMENTATION: False

    DET_LOCAL_MAX_COORDS: True
    DET_MIN_TH_TO_BE_PEAK: [0.1]
    DET_VOXEL_SIZE: (0.4,0.4,2)
    DET_TOLERANCE: [10]
    DET_MIN_DISTANCE: [10]

    STATS:
        PER_PATCH: True
        MERGE_PATCHES: True
        FULL_IMG: False

PATHS:
    CHECKPOINT_FILE: './brainbow_3d_detection/model_weights_unet_3d_detection_P14_big_1.h5'

What I did

from config.config import Config
from engine.engine import Engine

job_identifier = '1'
dataroot = './'
jobdir = './'

cfg = Config(jobdir,
             job_identifier=job_identifier,
             dataroot=dataroot)

cfg._C.merge_from_file('./brainbow_3d_detection/brainbow_3d_detection.yaml')
cfg.update_dependencies()
cfg = cfg.get_cfg_defaults()

engine = Engine(cfg, job_identifier)

Error msg

###################
#  SANITY CHECKS  #
###################

#################
#   LOAD DATA   #
#################

2) Loading test images . . .
Loading data from ./ChroMS/2021-03-24 P14 6520-2 Mcol cortex/test/x

100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00,  2.60it/s]

*** Loaded data shape is (1, 51, 512, 512, 3)
########################
#  PREPARE GENERATORS  #
########################

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [8], in <cell line: 1>()
----> 1 engine = Engine(cfg, job_identifier)

File ~/workspace/BiaPy/engine/engine.py:179, in Engine.__init__(self, cfg, job_identifier)
    176         check_generator_consistence(
    177             self.val_generator, cfg.PATHS.GEN_CHECKS+"_val", cfg.PATHS.GEN_MASK_CHECKS+"_val")
    178 if cfg.TEST.ENABLE:
--> 179     self.test_generator = create_test_augmentor(cfg, X_test, Y_test)
    182 print("#################\n"
    183       "#  BUILD MODEL  #\n"
    184       "#################\n")
    185 self.model = build_model(cfg, self.job_identifier)

File ~/workspace/BiaPy/data/generators/__init__.py:192, in create_test_augmentor(cfg, X_test, Y_test)
    190     if cfg.PROBLEM.TYPE == 'SUPER_RESOLUTION':
    191         dic['do_normalization']=False
--> 192     test_generator = simple_data_generator(**dic)
    193 return test_generator

File ~/workspace/BiaPy/data/generators/simple_data_generators.py:82, in simple_data_generator.__init__(self, X, d_path, provide_Y, Y, dm_path, dims, batch_size, seed, shuffle_each_epoch, instance_problem, do_normalization)
     80 self.o_indexes = np.arange(self.len)
     81 self.div_X_on_load = True if (np.max(img) > 100 and do_normalization) else False
---> 82 self.div_Y_on_load = True if (np.max(mask) > 100 and do_normalization and not instance_problem) else False
     83 self.on_epoch_end()

TypeError: '>' not supported between instances of 'NoneType' and 'int'
danifranco commented 2 years ago

Hi, I was aware about the issue but did not push the changes because we are making lots of new things these days to publish the library. Thanks for highlighting the error!

sbinnee commented 2 years ago

Thanks for commenting