diff --git a/Classification/cnns/optimizer_util.py b/Classification/cnns/optimizer_util.py index 43cd977..146bf85 100755 --- a/Classification/cnns/optimizer_util.py +++ b/Classification/cnns/optimizer_util.py @@ -68,7 +68,7 @@ def set_up_optimizer(loss, args): batches_per_epoch = math.ceil(args.num_examples / train_batch_size) warmup_batches = batches_per_epoch * args.warmup_epochs num_train_batches = batches_per_epoch * args.num_epochs - decay_batches = num_train_batches - warmup_batches + decay_batches = num_train_batches# - warmup_batches exponential_decay_batches = batches_per_epoch * args.lr_decay_epochs # set up warmup strategy diff --git a/Classification/cnns/resnet_model.py b/Classification/cnns/resnet_model.py index 7e9c1fc..cbb48a7 100755 --- a/Classification/cnns/resnet_model.py +++ b/Classification/cnns/resnet_model.py @@ -50,7 +50,7 @@ def _conv2d( else: shape = (filters, input.shape[1], kernel_size, kernel_size) weight = flow.get_variable( - name + "-weight", + name + ".weight", shape=shape, dtype=input.dtype, initializer=self.weight_initializer, @@ -113,7 +113,7 @@ def _batch_norm_relu(self, inputs, name=None, last=False): name=name + "_bn_relu", ) else: - return flow.nn.relu(self._batch_norm(inputs, name + "_bn", last=last)) + return flow.nn.relu(self._batch_norm(inputs, name, last=last)) def _batch_norm_add_relu(self, inputs, addend, name=None, last=False): if self.fuse_bn_add_relu: @@ -139,7 +139,7 @@ def _batch_norm_add_relu(self, inputs, addend, name=None, last=False): ) else: return flow.nn.relu( - self._batch_norm(inputs, name + "_bn", last=last) + addend + self._batch_norm(inputs, name, last=last) + addend ) def conv2d_affine(self, input, name, filters, kernel_size, strides): @@ -150,21 +150,21 @@ def conv2d_affine(self, input, name, filters, kernel_size, strides): def bottleneck_transformation( self, input, block_name, filters, filters_inner, strides ): - a = self.conv2d_affine(input, block_name + "_branch2a", filters_inner, 1, 1) - a = self._batch_norm_relu(a, block_name + "_branch2a") + a = self.conv2d_affine(input, block_name + ".conv1", filters_inner, 1, 1) + a = self._batch_norm_relu(a, block_name + ".bn1") - b = self.conv2d_affine(a, block_name + "_branch2b", filters_inner, 3, strides) - b = self._batch_norm_relu(b, block_name + "_branch2b") + b = self.conv2d_affine(a, block_name + ".conv2", filters_inner, 3, strides) + b = self._batch_norm_relu(b, block_name + ".bn2") - c = self.conv2d_affine(b, block_name + "_branch2c", filters, 1, 1) + c = self.conv2d_affine(b, block_name + ".conv3", filters, 1, 1) return c def residual_block(self, input, block_name, filters, filters_inner, strides_init): - if strides_init != 1 or block_name == "res2_0": + if strides_init != 1 or block_name == "layer1.0": shortcut = self.conv2d_affine( - input, block_name + "_branch1", filters, 1, strides_init + input, block_name + ".downsample.0", filters, 1, strides_init ) - shortcut = self._batch_norm(shortcut, block_name + "_branch1_bn") + shortcut = self._batch_norm(shortcut, block_name + ".downsample.1") else: shortcut = input @@ -172,7 +172,7 @@ def residual_block(self, input, block_name, filters, filters_inner, strides_init input, block_name, filters, filters_inner, strides_init, ) return self._batch_norm_add_relu( - bottleneck, shortcut, block_name + "_branch2c", last=True + bottleneck, shortcut, block_name + ".bn3", last=True ) def residual_stage( @@ -180,7 +180,7 @@ def residual_stage( ): output = input for i in range(counts): - block_name = "%s_%d" % (stage_name, i) + block_name = "%s.%d" % (stage_name, i) output = self.residual_block( output, block_name, filters, filters_inner, stride_init if i == 0 else 1 ) @@ -192,7 +192,7 @@ def resnet_conv_x_body(self, input): for i, (counts, filters, filters_inner) in enumerate( zip(BLOCK_COUNTS, BLOCK_FILTERS, BLOCK_FILTERS_INNER) ): - stage_name = "res%d" % (i + 2) + stage_name = "layer%d" % (i + 1) output = self.residual_stage( output, stage_name, counts, filters, filters_inner, 1 if i == 0 else 2 ) @@ -201,7 +201,7 @@ def resnet_conv_x_body(self, input): def resnet_stem(self, input): conv1 = self._conv2d("conv1", input, 64, 7, 2) - conv1_bn = self._batch_norm_relu(conv1, "conv1") + conv1_bn = self._batch_norm_relu(conv1, "bn1") pool1 = flow.nn.max_pool2d( conv1_bn, ksize=3, @@ -232,28 +232,29 @@ def resnet50(images, args, trainable=True, training=True): else: paddings = ((0, 0), (0, 1), (0, 0), (0, 0)) images = flow.pad(images, paddings=paddings) - with flow.scope.namespace("Resnet"): - stem = builder.resnet_stem(images) - body = builder.resnet_conv_x_body(stem) - pool5 = flow.nn.avg_pool2d( - body, - ksize=7, - strides=1, - padding="VALID", - data_format=builder.data_format, - name="pool5", - ) - fc1001 = flow.layers.dense( - flow.reshape(pool5, (pool5.shape[0], -1)), - units=1000, - use_bias=True, - kernel_initializer=flow.variance_scaling_initializer( - 2, "fan_in", "random_normal" - ), - bias_initializer=flow.zeros_initializer(), - kernel_regularizer=weight_regularizer, - bias_regularizer=weight_regularizer, - trainable=trainable, - name="fc1001", - ) + # with flow.scope.namespace("resnet50"): + stem = builder.resnet_stem(images) + body = builder.resnet_conv_x_body(stem) + pool5 = flow.nn.avg_pool2d( + body, + ksize=7, + strides=1, + padding="VALID", + data_format=builder.data_format, + name="avgpool", + ) + fc1001 = flow.layers.dense( + flow.reshape(pool5, (pool5.shape[0], -1)), + units=1000, + use_bias=True, + kernel_initializer=flow.variance_scaling_initializer( + 2, "fan_in", "random_normal" + ), + bias_initializer=flow.zeros_initializer(), + kernel_regularizer=weight_regularizer, + bias_regularizer=weight_regularizer, + trainable=trainable, + name="fc", + ) return fc1001 +