class DilatedBlockLayerParams(LayerParams): @classmethod def name_prefix(cls) -> str: return "dilated_block" @classmethod def cls(cls) -> Type["Layer"]: return DilatedBlockLayer def downscale(self, size: IntVec2D) -> IntVec2D: return IntVec2D( (size.x + self.strides.x - 1) // self.strides.x, (size.y + self.strides.y - 1) // self.strides.y, ) def downscale_factor(self, factor: IntVec2D) -> IntVec2D: return IntVec2D(factor.x * self.strides.x, factor.y * self.strides.y) filters: int = 40 kernel_size: IntVec2D = field(default_factory=lambda: IntVec2D(3, 3)) strides: IntVec2D = field(default_factory=lambda: IntVec2D(1, 1)) padding: str = "same" activation: str = "relu" dilated_depth: int = 2
class MaxPool2DLayerParams(LayerParams): @classmethod def name_prefix(cls) -> str: return "maxpool2d" @classmethod def cls(cls) -> Type["Layer"]: return MaxPool2DLayer def downscale(self, size: IntVec2D) -> IntVec2D: strides = self.real_strides() return IntVec2D((size.x + strides.x - 1) // strides.x, (size.y + strides.y - 1) // strides.y) def downscale_factor(self, factor: IntVec2D) -> IntVec2D: strides = self.real_strides() return IntVec2D(factor.x * strides.x, factor.y * strides.y) def real_strides(self) -> IntVec2D: if self.strides is None: return self.pool_size return IntVec2D( self.strides.x if self.strides.x >= 0 else self.pool_size.x, self.strides.y if self.strides.y >= 0 else self.pool_size.y, ) pool_size: IntVec2D = field(default_factory=lambda: IntVec2D(2, 2), metadata=pai_meta(tuple_like=True)) strides: IntVec2D = field(default_factory=lambda: IntVec2D(-1, -1), metadata=pai_meta(tuple_like=True)) padding: str = "same"
class TransposedConv2DLayerParams(LayerParams): @classmethod def name_prefix(cls) -> str: return "tconv2d" @classmethod def cls(cls) -> Type["Layer"]: return TransposedConv2DLayer def downscale(self, size: IntVec2D) -> IntVec2D: return IntVec2D(size.x * self.strides.x, size.y * self.strides.y) def downscale_factor(self, size: IntVec2D) -> IntVec2D: return IntVec2D( (size.x + self.strides.x - 1) // self.strides.x, (size.y + self.strides.y - 1) // self.strides.y, ) filters: int = 40 kernel_size: IntVec2D = field(default_factory=lambda: IntVec2D(3, 3), metadata=pai_meta(tuple_like=True)) strides: IntVec2D = field(default_factory=lambda: IntVec2D(2, 2), metadata=pai_meta(tuple_like=True)) padding: str = "same" activation: str = "relu"
def compute_max_downscale_factor(self) -> IntVec2D: factor = IntVec2D(1, 1) max_factor = IntVec2D(1, 1) for layer in self.layers: factor = layer.downscale_factor(factor) max_factor.x = max(max_factor.x, factor.x) max_factor.y = max(max_factor.y, factor.y) return max_factor
def test_pure_cnn_architecture(self): trainer_params = uw3_trainer_params() trainer_params.scenario.model.layers = [ Conv2DLayerParams(filters=10), MaxPool2DLayerParams(), Conv2DLayerParams(filters=20, strides=IntVec2D(2, 2), kernel_size=IntVec2D(4, 4)), Conv2DLayerParams(filters=30), ] with tempfile.TemporaryDirectory() as d: trainer_params.output_dir = d main(trainer_params)
def compute_downscaled(self, size: Union[int, IntVec2D, Tuple[Any, Any]]): if isinstance(size, int): for layer in self.layers: size = layer.downscale(IntVec2D(size, 1)).x elif isinstance(size, IntVec2D): for layer in self.layers: size = layer.downscale(size) elif isinstance(size, tuple): for layer in self.layers: size = layer.downscale(IntVec2D(size[0], size[1])) size = size.x, size.y else: raise NotImplementedError return size
def real_strides(self) -> IntVec2D: if self.strides is None: return self.pool_size return IntVec2D( self.strides.x if self.strides.x >= 0 else self.pool_size.x, self.strides.y if self.strides.y >= 0 else self.pool_size.y, )
def test_pure_cnn_architecture(self): trainer_params = uw3_trainer_params() trainer_params.scenario.model.layers = [ Conv2DLayerParams(filters=10), MaxPool2DLayerParams(), Conv2DLayerParams(filters=20, strides=IntVec2D(2, 2), kernel_size=IntVec2D(4, 4)), Conv2DLayerParams(filters=30), ] post_init(trainer_params) cmd_line_trainer_params = parse_args( ["--network", "conv=10,pool=2x2,conv=20:4x4:2x2,conv=30"]) self.assertDictEqual(trainer_params.scenario.model.to_dict(), cmd_line_trainer_params.scenario.model.to_dict()) cmd_line_trainer_params = parse_args([ "--model.layers", "Conv", "Pool", "Conv", "Conv", "--model.layers.0.filters", "10", "--model.layers.2.filters", "20", "--model.layers.2.kernel_size.x", "4", "--model.layers.2.kernel_size.y", "4", "--model.layers.2.strides.x", "2", "--model.layers.2.strides.y", "2", "--model.layers.3.filters", "30", ]) self.assertDictEqual(trainer_params.scenario.model.to_dict(), cmd_line_trainer_params.scenario.model.to_dict()) with tempfile.TemporaryDirectory() as d: trainer_params.output_dir = d main(trainer_params)
class Conv2DLayerParams(LayerParams): @classmethod def name_prefix(cls) -> str: return 'conv2d' @classmethod def cls(cls) -> Type['Layer']: return Conv2DLayer def downscale(self, size: IntVec2D) -> IntVec2D: return IntVec2D((size.x + self.strides.x - 1) // self.strides.x, (size.y + self.strides.y - 1) // self.strides.y) def downscale_factor(self, factor: IntVec2D) -> IntVec2D: return IntVec2D(factor.x * self.strides.x, factor.y * self.strides.y) filters: int = 40 kernel_size: IntVec2D = field(default_factory=lambda: IntVec2D(3, 3)) strides: IntVec2D = field(default_factory=lambda: IntVec2D(1, 1)) padding: str = "same" activation: str = 'relu'
def default_trainer_params(cls): p = super().default_trainer_params() p.scenario.model.layers = [ Conv2DLayerParams(filters=2), MaxPool2DLayerParams(pool_size=IntVec2D(4, 4)), BiLSTMLayerParams(hidden_nodes=2), DropoutLayerParams(rate=0.5), ] p.gen.setup.val.batch_size = 1 p.gen.setup.val.num_processes = 1 p.gen.setup.train.batch_size = 1 p.gen.setup.train.num_processes = 1 p.epochs = 1 p.samples_per_epoch = 2 p.scenario.data.pre_proc.run_parallel = False post_init(p) return p
class MaxPool2DLayerParams(LayerParams): @classmethod def name_prefix(cls) -> str: return "maxpool2d" @classmethod def cls(cls) -> Type["Layer"]: return MaxPool2DLayer def downscale(self, size: IntVec2D) -> IntVec2D: strides = self.strides if self.strides is not None else self.pool_size return IntVec2D((size.x + strides.x - 1) // strides.x, (size.y + strides.y - 1) // strides.y) def downscale_factor(self, factor: IntVec2D) -> IntVec2D: strides = self.strides if self.strides is not None else self.pool_size return IntVec2D(factor.x * strides.x, factor.y * strides.y) pool_size: IntVec2D = field(default_factory=lambda: IntVec2D(2, 2)) strides: Optional[IntVec2D] = field(default=None) padding: str = "same"
def test_dilated_block_architecture(self): trainer_params = uw3_trainer_params() trainer_params.scenario.model.layers = [ Conv2DLayerParams(filters=10), MaxPool2DLayerParams(strides=IntVec2D(2, 2)), DilatedBlockLayerParams(filters=10), DilatedBlockLayerParams(filters=10), Conv2DLayerParams(filters=10), ] post_init(trainer_params) cmd_line_trainer_params = parse_args( ["--network", "conv=10,pool=2x2:2x2,db=10:2,db=10:2,conv=10"]) self.assertDictEqual(trainer_params.scenario.model.to_dict(), cmd_line_trainer_params.scenario.model.to_dict()) cmd_line_trainer_params = parse_args([ "--model.layers", "Conv", "Pool", "DilatedBlock", "DilatedBlock", "Conv", "--model.layers.0.filters", "10", "--model.layers.1.strides", "2", "2", "--model.layers.2.filters", "10", "--model.layers.3.filters", "10", "--model.layers.4.filters", "10", ]) self.assertDictEqual(trainer_params.scenario.model.to_dict(), cmd_line_trainer_params.scenario.model.to_dict()) with tempfile.TemporaryDirectory() as d: trainer_params.output_dir = d main(trainer_params)
def downscale_factor(self, factor: IntVec2D) -> IntVec2D: strides = self.strides if self.strides is not None else self.pool_size return IntVec2D(factor.x * strides.x, factor.y * strides.y)
def downscale(self, size: IntVec2D) -> IntVec2D: strides = self.real_strides() return IntVec2D((size.x + strides.x - 1) // strides.x, (size.y + strides.y - 1) // strides.y)
def downscale_factor(self, factor: IntVec2D) -> IntVec2D: strides = self.real_strides() return IntVec2D(factor.x * strides.x, factor.y * strides.y)
def downscale(self, size: IntVec2D) -> IntVec2D: strides = self.strides if self.strides is not None else self.pool_size return IntVec2D((size.x + strides.x - 1) // strides.x, (size.y + strides.y - 1) // strides.y)
def compute_downscale_factor(self) -> IntVec2D: factor = IntVec2D(1, 1) for layer in self.layers: factor = layer.downscale_factor(factor) return factor
def downscale(self, size: IntVec2D) -> IntVec2D: return IntVec2D((size.x + self.strides.x - 1) // self.strides.x, (size.y + self.strides.y - 1) // self.strides.y)
def downscale_factor(self, factor: IntVec2D) -> IntVec2D: return IntVec2D(factor.x * self.strides.x, factor.y * self.strides.y)
def downscale(self, size: IntVec2D) -> IntVec2D: return IntVec2D(size.x * self.strides.x, size.y * self.strides.y)