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