Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resnet50 var rename #214

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Classification/cnns/optimizer_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
79 changes: 40 additions & 39 deletions Classification/cnns/resnet_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -150,37 +150,37 @@ 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

bottleneck = self.bottleneck_transformation(
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(
self, input, stage_name, counts, filters, filters_inner, stride_init=2
):
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
)
Expand All @@ -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
)
Expand All @@ -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,
Expand Down Expand Up @@ -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