|
| 1 | +# Copyright 2021 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# Lint as: python3 |
| 16 | +"""Semantic segmentation configuration definition.""" |
| 17 | +from typing import List, Optional, Union |
| 18 | + |
| 19 | +import dataclasses |
| 20 | + |
| 21 | +from official.core import exp_factory |
| 22 | +from official.modeling import hyperparams |
| 23 | +from official.modeling import optimization |
| 24 | +from official.modeling.hyperparams import config_definitions as cfg |
| 25 | +from official.vision.beta.configs import common |
| 26 | +from official.vision.beta.projects.volumetric_models.configs import backbones |
| 27 | +from official.vision.beta.projects.volumetric_models.configs import decoders |
| 28 | + |
| 29 | + |
| 30 | +@dataclasses.dataclass |
| 31 | +class DataConfig(cfg.DataConfig): |
| 32 | + """Input config for training.""" |
| 33 | + output_size: List[int] = dataclasses.field(default_factory=list) |
| 34 | + input_size: List[int] = dataclasses.field(default_factory=list) |
| 35 | + num_classes: int = 0 |
| 36 | + num_channels: int = 1 |
| 37 | + input_path: str = '' |
| 38 | + global_batch_size: int = 0 |
| 39 | + is_training: bool = True |
| 40 | + dtype: str = 'float32' |
| 41 | + label_dtype: str = 'float32' |
| 42 | + image_field_key: str = 'image/encoded' |
| 43 | + label_field_key: str = 'image/class/label' |
| 44 | + shuffle_buffer_size: int = 1000 |
| 45 | + cycle_length: int = 10 |
| 46 | + drop_remainder: bool = False |
| 47 | + file_type: str = 'tfrecord' |
| 48 | + |
| 49 | + |
| 50 | +@dataclasses.dataclass |
| 51 | +class SegmentationHead3D(hyperparams.Config): |
| 52 | + """Segmentation head config.""" |
| 53 | + num_classes: int = 0 |
| 54 | + level: int = 1 |
| 55 | + num_convs: int = 0 |
| 56 | + num_filters: int = 256 |
| 57 | + upsample_factor: int = 1 |
| 58 | + output_logits: bool = True |
| 59 | + |
| 60 | + |
| 61 | +@dataclasses.dataclass |
| 62 | +class SemanticSegmentationModel3D(hyperparams.Config): |
| 63 | + """Semantic segmentation model config.""" |
| 64 | + num_classes: int = 0 |
| 65 | + num_channels: int = 1 |
| 66 | + input_size: List[int] = dataclasses.field(default_factory=list) |
| 67 | + min_level: int = 3 |
| 68 | + max_level: int = 6 |
| 69 | + head: SegmentationHead3D = SegmentationHead3D() |
| 70 | + backbone: backbones.Backbone = backbones.Backbone( |
| 71 | + type='unet_3d', unet_3d=backbones.UNet3D()) |
| 72 | + decoder: decoders.Decoder = decoders.Decoder( |
| 73 | + type='unet_3d_decoder', unet_3d_decoder=decoders.UNet3DDecoder()) |
| 74 | + norm_activation: common.NormActivation = common.NormActivation() |
| 75 | + |
| 76 | + |
| 77 | +@dataclasses.dataclass |
| 78 | +class Losses(hyperparams.Config): |
| 79 | + # Supported `loss_type` are `adaptive` and `generalized`. |
| 80 | + loss_type: str = 'adaptive' |
| 81 | + l2_weight_decay: float = 0.0 |
| 82 | + |
| 83 | + |
| 84 | +@dataclasses.dataclass |
| 85 | +class Evaluation(hyperparams.Config): |
| 86 | + report_per_class_metric: bool = False # Whether to report per-class metrics. |
| 87 | + |
| 88 | + |
| 89 | +@dataclasses.dataclass |
| 90 | +class SemanticSegmentation3DTask(cfg.TaskConfig): |
| 91 | + """The model config.""" |
| 92 | + model: SemanticSegmentationModel3D = SemanticSegmentationModel3D() |
| 93 | + train_data: DataConfig = DataConfig(is_training=True) |
| 94 | + validation_data: DataConfig = DataConfig(is_training=False) |
| 95 | + losses: Losses = Losses() |
| 96 | + evaluation: Evaluation = Evaluation() |
| 97 | + train_input_partition_dims: List[int] = dataclasses.field( |
| 98 | + default_factory=list) |
| 99 | + eval_input_partition_dims: List[int] = dataclasses.field(default_factory=list) |
| 100 | + init_checkpoint: Optional[str] = None |
| 101 | + init_checkpoint_modules: Union[ |
| 102 | + str, List[str]] = 'all' # all, backbone, and/or decoder |
| 103 | + |
| 104 | + |
| 105 | +@exp_factory.register_config_factory('seg_unet3d_test') |
| 106 | +def seg_unet3d_test() -> cfg.ExperimentConfig: |
| 107 | + """Image segmentation on a dummy dataset with 3D UNet for testing purpose.""" |
| 108 | + train_batch_size = 2 |
| 109 | + eval_batch_size = 2 |
| 110 | + steps_per_epoch = 10 |
| 111 | + config = cfg.ExperimentConfig( |
| 112 | + task=SemanticSegmentation3DTask( |
| 113 | + model=SemanticSegmentationModel3D( |
| 114 | + num_classes=2, |
| 115 | + input_size=[32, 32, 32], |
| 116 | + num_channels=2, |
| 117 | + backbone=backbones.Backbone( |
| 118 | + type='unet_3d', unet_3d=backbones.UNet3D(model_id=2)), |
| 119 | + decoder=decoders.Decoder( |
| 120 | + type='unet_3d_decoder', |
| 121 | + unet_3d_decoder=decoders.UNet3DDecoder(model_id=2)), |
| 122 | + head=SegmentationHead3D(num_convs=0, num_classes=2), |
| 123 | + norm_activation=common.NormActivation( |
| 124 | + activation='relu', use_sync_bn=False)), |
| 125 | + train_data=DataConfig( |
| 126 | + input_path='train.tfrecord', |
| 127 | + num_classes=2, |
| 128 | + input_size=[32, 32, 32], |
| 129 | + num_channels=2, |
| 130 | + is_training=True, |
| 131 | + global_batch_size=train_batch_size), |
| 132 | + validation_data=DataConfig( |
| 133 | + input_path='val.tfrecord', |
| 134 | + num_classes=2, |
| 135 | + input_size=[32, 32, 32], |
| 136 | + num_channels=2, |
| 137 | + is_training=False, |
| 138 | + global_batch_size=eval_batch_size), |
| 139 | + losses=Losses(loss_type='adaptive')), |
| 140 | + trainer=cfg.TrainerConfig( |
| 141 | + steps_per_loop=steps_per_epoch, |
| 142 | + summary_interval=steps_per_epoch, |
| 143 | + checkpoint_interval=steps_per_epoch, |
| 144 | + train_steps=10, |
| 145 | + validation_steps=10, |
| 146 | + validation_interval=steps_per_epoch, |
| 147 | + optimizer_config=optimization.OptimizationConfig({ |
| 148 | + 'optimizer': { |
| 149 | + 'type': 'sgd', |
| 150 | + }, |
| 151 | + 'learning_rate': { |
| 152 | + 'type': 'constant', |
| 153 | + 'constant': { |
| 154 | + 'learning_rate': 0.000001 |
| 155 | + } |
| 156 | + } |
| 157 | + })), |
| 158 | + restrictions=[ |
| 159 | + 'task.train_data.is_training != None', |
| 160 | + 'task.validation_data.is_training != None' |
| 161 | + ]) |
| 162 | + |
| 163 | + return config |
0 commit comments