未验证 提交 de8c0ba5 编写于 作者: N Nyakku Shigure 提交者: GitHub

[CodeStyle][W291] trim trailing whitespace in python file (#45937)

* trim trailing whitespace

* fix `.cmake-format.py`

* revert npu ut changes, avoid npu ci error
上级 cbe64cc1
......@@ -106,8 +106,8 @@ def convert_pascalvoc_local2bin(args):
for object in objects:
bbox_sample = []
# start from 1
bbox_sample.append(
float(label_list.index(object.find('name').text)))
bbox_sample.append(float(label_list.index(
object.find('name').text)))
bbox = object.find('bndbox')
difficult = float(object.find('difficult').text)
bbox_sample.append(float(bbox.find('xmin').text) / im_width)
......@@ -210,8 +210,8 @@ def convert_pascalvoc_tar2bin(tar_path, data_out_path):
for object in objects:
bbox_sample = []
bbox_sample.append(
float(label_list.index(object.find('name').text)))
bbox_sample.append(float(label_list.index(
object.find('name').text)))
bbox = object.find('bndbox')
difficult = float(object.find('difficult').text)
bbox_sample.append(float(bbox.find('xmin').text) / im_width)
......@@ -258,9 +258,9 @@ def download_pascalvoc(data_url, data_dir, tar_targethash, tar_path):
def run_convert():
try_limit = 2
retry = 0
while not (os.path.exists(DATA_OUT_PATH) and
os.path.getsize(DATA_OUT_PATH) == BIN_FULLSIZE and BIN_TARGETHASH
== hashlib.md5(open(DATA_OUT_PATH, 'rb').read()).hexdigest()):
while not (os.path.exists(DATA_OUT_PATH) and os.path.getsize(DATA_OUT_PATH)
== BIN_FULLSIZE and BIN_TARGETHASH == hashlib.md5(
open(DATA_OUT_PATH, 'rb').read()).hexdigest()):
if os.path.exists(DATA_OUT_PATH):
sys.stderr.write(
"The existing binary file is broken. It is being removed...\n")
......@@ -275,49 +275,49 @@ def run_convert():
def main_pascalvoc_preprocess(args):
parser = argparse.ArgumentParser(
description="Convert the full pascalvoc val set or local data to binary file.",
description=
"Convert the full pascalvoc val set or local data to binary file.",
usage=None,
add_help=True)
parser.add_argument(
'--local',
action="store_true",
help="If used, user need to set --data_dir and then convert file")
parser.add_argument(
"--data_dir", default="", type=str, help="Dataset root directory")
parser.add_argument("--data_dir",
default="",
type=str,
help="Dataset root directory")
parser.add_argument(
"--img_annotation_list",
type=str,
default="test_100.txt",
help="A file containing the image file path and corresponding annotation file path"
help=
"A file containing the image file path and corresponding annotation file path"
)
parser.add_argument(
"--label_file",
type=str,
default="label_list",
help="List of object labels with same sequence as denoted in the annotation file"
help=
"List of object labels with same sequence as denoted in the annotation file"
)
parser.add_argument(
"--output_file",
parser.add_argument("--output_file",
type=str,
default="pascalvoc_small.bin",
help="File path of the output binary file")
parser.add_argument(
"--resize_h",
parser.add_argument("--resize_h",
type=int,
default=RESIZE_H,
help="Image preprocess with resize_h")
parser.add_argument(
"--resize_w",
parser.add_argument("--resize_w",
type=int,
default=RESIZE_W,
help="Image prerocess with resize_w")
parser.add_argument(
"--mean_value",
parser.add_argument("--mean_value",
type=str,
default=MEAN_VALUE,
help="Image preprocess with mean_value")
parser.add_argument(
"--ap_version",
parser.add_argument("--ap_version",
type=str,
default=AP_VERSION,
help="Image preprocess with ap_version")
......
......@@ -20,6 +20,7 @@ import sys
class AbsNet(paddle.nn.Layer):
def __init__(self):
super(AbsNet, self).__init__()
......@@ -32,7 +33,6 @@ if __name__ == '__main__':
# build network
model = AbsNet()
# save inferencing format model
net = to_static(
model, input_spec=[InputSpec(
shape=[None, 1, 28, 28], name='x')])
net = to_static(model,
input_spec=[InputSpec(shape=[None, 1, 28, 28], name='x')])
paddle.jit.save(net, sys.argv[1])
......@@ -20,7 +20,6 @@ import paddle
import sys
model = EfficientNet.from_name('efficientnet-b4')
net = to_static(
model, input_spec=[InputSpec(
shape=[None, 3, 256, 256], name='x')])
net = to_static(model,
input_spec=[InputSpec(shape=[None, 3, 256, 256], name='x')])
paddle.jit.save(net, sys.argv[1])
......@@ -38,8 +38,8 @@ class MBConvBlock(nn.Layer):
self._block_args = block_args
self._bn_mom = global_params.batch_norm_momentum
self._bn_eps = global_params.batch_norm_epsilon
self.has_se = (self._block_args.se_ratio is not None) and (
0 < self._block_args.se_ratio <= 1)
self.has_se = (self._block_args.se_ratio
is not None) and (0 < self._block_args.se_ratio <= 1)
self.id_skip = block_args.id_skip # skip connection and drop connect
# Get static or dynamic convolution depending on image size
......@@ -49,13 +49,13 @@ class MBConvBlock(nn.Layer):
inp = self._block_args.input_filters # number of input channels
oup = self._block_args.input_filters * self._block_args.expand_ratio # number of output channels
if self._block_args.expand_ratio != 1:
self._expand_conv = Conv2d(
in_channels=inp,
self._expand_conv = Conv2d(in_channels=inp,
out_channels=oup,
kernel_size=1,
bias_attr=False)
self._bn0 = nn.BatchNorm2D(
num_features=oup, momentum=self._bn_mom, epsilon=self._bn_eps)
self._bn0 = nn.BatchNorm2D(num_features=oup,
momentum=self._bn_mom,
epsilon=self._bn_eps)
# Depthwise convolution phase
k = self._block_args.kernel_size
......@@ -67,32 +67,31 @@ class MBConvBlock(nn.Layer):
kernel_size=k,
stride=s,
bias_attr=False)
self._bn1 = nn.BatchNorm2D(
num_features=oup, momentum=self._bn_mom, epsilon=self._bn_eps)
self._bn1 = nn.BatchNorm2D(num_features=oup,
momentum=self._bn_mom,
epsilon=self._bn_eps)
# Squeeze and Excitation layer, if desired
if self.has_se:
num_squeezed_channels = max(1,
int(self._block_args.input_filters *
self._block_args.se_ratio))
self._se_reduce = Conv2d(
in_channels=oup,
num_squeezed_channels = max(
1,
int(self._block_args.input_filters * self._block_args.se_ratio))
self._se_reduce = Conv2d(in_channels=oup,
out_channels=num_squeezed_channels,
kernel_size=1)
self._se_expand = Conv2d(
in_channels=num_squeezed_channels,
self._se_expand = Conv2d(in_channels=num_squeezed_channels,
out_channels=oup,
kernel_size=1)
# Output phase
final_oup = self._block_args.output_filters
self._project_conv = Conv2d(
in_channels=oup,
self._project_conv = Conv2d(in_channels=oup,
out_channels=final_oup,
kernel_size=1,
bias_attr=False)
self._bn2 = nn.BatchNorm2D(
num_features=final_oup, momentum=self._bn_mom, epsilon=self._bn_eps)
self._bn2 = nn.BatchNorm2D(num_features=final_oup,
momentum=self._bn_mom,
epsilon=self._bn_eps)
self._swish = nn.Hardswish()
def forward(self, inputs, drop_connect_rate=None):
......@@ -121,8 +120,9 @@ class MBConvBlock(nn.Layer):
input_filters, output_filters = self._block_args.input_filters, self._block_args.output_filters
if self.id_skip and self._block_args.stride == 1 and input_filters == output_filters:
if drop_connect_rate:
x = drop_connect(
x, prob=drop_connect_rate, training=self.training)
x = drop_connect(x,
prob=drop_connect_rate,
training=self.training)
x = x + inputs # skip connection
return x
......@@ -162,10 +162,14 @@ class EfficientNet(nn.Layer):
in_channels = 3 # rgb
out_channels = round_filters(
32, self._global_params) # number of output channels
self._conv_stem = Conv2d(
in_channels, out_channels, kernel_size=3, stride=2, bias_attr=False)
self._bn0 = nn.BatchNorm2D(
num_features=out_channels, momentum=bn_mom, epsilon=bn_eps)
self._conv_stem = Conv2d(in_channels,
out_channels,
kernel_size=3,
stride=2,
bias_attr=False)
self._bn0 = nn.BatchNorm2D(num_features=out_channels,
momentum=bn_mom,
epsilon=bn_eps)
# Build blocks
self._blocks = nn.LayerList([])
......@@ -186,16 +190,19 @@ class EfficientNet(nn.Layer):
block_args = block_args._replace(
input_filters=block_args.output_filters, stride=1)
for _ in range(block_args.num_repeat - 1):
self._blocks.append(
MBConvBlock(block_args, self._global_params))
self._blocks.append(MBConvBlock(block_args,
self._global_params))
# Head
in_channels = block_args.output_filters # output of final block
out_channels = round_filters(1280, self._global_params)
self._conv_head = Conv2d(
in_channels, out_channels, kernel_size=1, bias_attr=False)
self._bn1 = nn.BatchNorm2D(
num_features=out_channels, momentum=bn_mom, epsilon=bn_eps)
self._conv_head = Conv2d(in_channels,
out_channels,
kernel_size=1,
bias_attr=False)
self._bn1 = nn.BatchNorm2D(num_features=out_channels,
momentum=bn_mom,
epsilon=bn_eps)
# Final linear layer
self._avg_pooling = nn.AdaptiveAvgPool2D(1)
......@@ -253,16 +260,17 @@ class EfficientNet(nn.Layer):
advprop=False,
num_classes=1000,
in_channels=3):
model = cls.from_name(
model_name, override_params={'num_classes': num_classes})
load_pretrained_weights(
model, model_name, load_fc=(num_classes == 1000), advprop=advprop)
model = cls.from_name(model_name,
override_params={'num_classes': num_classes})
load_pretrained_weights(model,
model_name,
load_fc=(num_classes == 1000),
advprop=advprop)
if in_channels != 3:
Conv2d = get_same_padding_conv2d(
image_size=model._global_params.image_size)
out_channels = round_filters(32, model._global_params)
model._conv_stem = Conv2d(
in_channels,
model._conv_stem = Conv2d(in_channels,
out_channels,
kernel_size=3,
stride=2,
......@@ -280,5 +288,5 @@ class EfficientNet(nn.Layer):
""" Validates model name. """
valid_models = ['efficientnet-b' + str(i) for i in range(9)]
if model_name not in valid_models:
raise ValueError('model_name should be one of: ' + ', '.join(
valid_models))
raise ValueError('model_name should be one of: ' +
', '.join(valid_models))
......@@ -96,8 +96,7 @@ class Conv2dDynamicSamePadding(nn.Conv2D):
dilation=1,
groups=1,
bias_attr=None):
super().__init__(
in_channels,
super().__init__(in_channels,
out_channels,
kernel_size,
stride,
......@@ -113,10 +112,12 @@ class Conv2dDynamicSamePadding(nn.Conv2D):
kh, kw = self.weight.shape[-2:]
sh, sw = self.stride
oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
pad_h = max((oh - 1) * self.stride[0] +
(kh - 1) * self._dilation[0] + 1 - ih, 0)
pad_w = max((ow - 1) * self.stride[1] +
(kw - 1) * self._dilation[1] + 1 - iw, 0)
pad_h = max(
(oh - 1) * self.stride[0] + (kh - 1) * self._dilation[0] + 1 - ih,
0)
pad_w = max(
(ow - 1) * self.stride[1] + (kw - 1) * self._dilation[1] + 1 - iw,
0)
if pad_h > 0 or pad_w > 0:
x = F.pad(x, [
pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2
......@@ -142,15 +143,18 @@ class Conv2dStaticSamePadding(nn.Conv2D):
# Calculate padding based on image size and save it
assert image_size is not None
ih, iw = image_size if type(
image_size) == list else [image_size, image_size]
ih, iw = image_size if type(image_size) == list else [
image_size, image_size
]
kh, kw = self.weight.shape[-2:]
sh, sw = self.stride
oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
pad_h = max((oh - 1) * self.stride[0] +
(kh - 1) * self._dilation[0] + 1 - ih, 0)
pad_w = max((ow - 1) * self.stride[1] +
(kw - 1) * self._dilation[1] + 1 - iw, 0)
pad_h = max(
(oh - 1) * self.stride[0] + (kh - 1) * self._dilation[0] + 1 - ih,
0)
pad_w = max(
(ow - 1) * self.stride[1] + (kw - 1) * self._dilation[1] + 1 - iw,
0)
if pad_h > 0 or pad_w > 0:
self.static_padding = nn.Pad2D([
pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2
......@@ -166,6 +170,7 @@ class Conv2dStaticSamePadding(nn.Conv2D):
class Identity(nn.Layer):
def __init__(self, ):
super().__init__()
......@@ -225,9 +230,12 @@ class BlockDecoder(object):
def _encode_block_string(block):
"""Encodes a block to a string."""
args = [
'r%d' % block.num_repeat, 'k%d' % block.kernel_size, 's%d%d' %
(block.strides[0], block.strides[1]), 'e%s' % block.expand_ratio,
'i%d' % block.input_filters, 'o%d' % block.output_filters
'r%d' % block.num_repeat,
'k%d' % block.kernel_size,
's%d%d' % (block.strides[0], block.strides[1]),
'e%s' % block.expand_ratio,
'i%d' % block.input_filters,
'o%d' % block.output_filters
]
if 0 < block.se_ratio <= 1:
args.append('se%s' % block.se_ratio)
......@@ -291,7 +299,8 @@ def efficientnet(width_coefficient=None,
depth_coefficient=depth_coefficient,
depth_divisor=8,
min_depth=None,
image_size=image_size, )
image_size=image_size,
)
return blocks_args, global_params
......@@ -300,8 +309,7 @@ def get_model_params(model_name, override_params):
""" Get the block args and global params for a given model """
if model_name.startswith('efficientnet'):
w, d, s, p = efficientnet_params(model_name)
blocks_args, global_params = efficientnet(
width_coefficient=w,
blocks_args, global_params = efficientnet(width_coefficient=w,
depth_coefficient=d,
dropout_rate=p,
image_size=s)
......
......@@ -28,6 +28,7 @@ CLASS_NUM = 10
# define a random dataset
class RandomDataset(paddle.io.Dataset):
def __init__(self, num_samples):
self.num_samples = num_samples
......@@ -41,6 +42,7 @@ class RandomDataset(paddle.io.Dataset):
class LinearNet(nn.Layer):
def __init__(self):
super(LinearNet, self).__init__()
self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM)
......@@ -69,8 +71,11 @@ adam = opt.Adam(learning_rate=0.001, parameters=layer.parameters())
# create data loader
dataset = RandomDataset(BATCH_NUM * BATCH_SIZE)
loader = paddle.io.DataLoader(
dataset, batch_size=BATCH_SIZE, shuffle=True, drop_last=True, num_workers=2)
loader = paddle.io.DataLoader(dataset,
batch_size=BATCH_SIZE,
shuffle=True,
drop_last=True,
num_workers=2)
# train
train(layer, loader, loss_fn, adam)
......
......@@ -19,7 +19,6 @@ from paddle.static import InputSpec
import sys
model = resnet50(True)
net = to_static(
model, input_spec=[InputSpec(
shape=[None, 3, 256, 256], name='x')])
net = to_static(model,
input_spec=[InputSpec(shape=[None, 3, 256, 256], name='x')])
paddle.jit.save(net, sys.argv[1])
......@@ -24,8 +24,7 @@ import time
def parse_args():
parser = argparse.ArgumentParser("conda build for paddlepaddle version")
parser.add_argument(
"--paddle_version",
parser.add_argument("--paddle_version",
type=str,
required=True,
help="paddle version for conda build.")
......@@ -35,6 +34,7 @@ def parse_args():
class ConstantVar:
def __init__(self):
self.build = r"""
build:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册