提交 bb622b24 编写于 作者: S shippingwang

fix bugs

上级 04e9720b
......@@ -33,6 +33,3 @@
- id: trailing-whitespace
files: \.(md|yml)$
- id: check-case-conflict
- id: flake8
args: ['--ignore=E265']
......@@ -46,10 +46,10 @@ TRAIN:
channel_first: False
- RandCropImage:
size: 224
interpolation: 2
#interpolation: 2
- RandFlipImage:
flip_code: 1
- AutoArgument:
- AutoAugment:
- NormalizeImage:
scale: 1./255.
mean: [0.485, 0.456, 0.406]
......
......@@ -25,7 +25,8 @@ import random
import cv2
import numpy as np
from autoargument import ImageNetPolicy
from .autoaugment import ImageNetPolicy
class OperatorParamError(ValueError):
""" OperatorParamError
......@@ -172,12 +173,12 @@ class RandFlipImage(object):
else:
return img
class AutoArgument(object):
class AutoAugment(object):
def __init__(self):
self.policy = ImageNetPolicy()
def __call__(self,img):
def __call__(self, img):
from PIL import Image
img = np.ascontiguousarray(img)
img = Image.fromarray(img)
......
......@@ -383,7 +383,9 @@ class EfficientNet():
use_bias=True,
padding_type=self.padding_type,
name=name + '_se_expand')
se_out = inputs * fluid.layers.sigmoid(x_squeezed)
#se_out = inputs * fluid.layers.sigmoid(x_squeezed)
se_out = fluid.layers.elementwise_mul(
inputs, fluid.layers.sigmoid(x_squeezed), axis=-1)
return se_out
def extract_features(self, inputs, is_test):
......@@ -467,8 +469,8 @@ class BlockDecoder(object):
# Check stride
cond_1 = ('s' in options and len(options['s']) == 1)
cond_2 = ((len(options['s']) == 2)
and (options['s'][0] == options['s'][1]))
cond_2 = ((len(options['s']) == 2) and
(options['s'][0] == options['s'][1]))
assert (cond_1 or cond_2)
return BlockArgs(
......
#copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import division
......@@ -146,7 +146,6 @@ class CosineWarmup(object):
class ExponentialWarmup(object):
"""
Exponential learning rate decay with warmup
[0, warmup_epoch): linear warmup
......@@ -160,8 +159,14 @@ class ExponentialWarmup(object):
warmup_epoch(int): epoch num of warmup
"""
def __init__(self, lr, step_each_epoch, decay_epochs=2.4, decay_rate=0.97, warmup_epoch=5, **kwargs):
super(CosineWarmup, self).__init__()
def __init__(self,
lr,
step_each_epoch,
decay_epochs=2.4,
decay_rate=0.97,
warmup_epoch=5,
**kwargs):
super(ExponentialWarmup, self).__init__()
self.lr = lr
self.step_each_epoch = step_each_epoch
self.decay_epochs = decay_epochs * self.step_each_epoch
......@@ -192,12 +197,13 @@ class ExponentialWarmup(object):
rest_step = global_step - self.warmup_epoch * self.step_each_epoch
div_res = ops.floor(rest_step / self.decay_epochs)
decayed_lr = self.lr*(self.decay_rate**div_res)
decayed_lr = self.lr * (self.decay_rate**div_res)
fluid.layers.tensor.assign(
input=decayed_lr, output=learning_rate)
return learning_rate
class LearningRateBuilder():
"""
Build learning rate variable
......
......@@ -36,7 +36,7 @@ from ppcls.utils import logger
from paddle.fluid.incubate.fleet.collective import fleet
from paddle.fluid.incubate.fleet.collective import DistributedStrategy
import ema
from ema import ExponentialMovingAverage
def create_feeds(image_shape, use_mix=None):
......@@ -359,10 +359,12 @@ def build(config, main_prog, startup_prog, is_train=True):
optimizer.minimize(fetchs['loss'][0])
if config.get('use_ema'):
global_steps = fluid.layers.learning_rate_scheduler._decay_step_counter()
ema = ExponentialMovingAverage(config.get('ema_decay'), thres_steps=global_steps)
global_steps = fluid.layers.learning_rate_scheduler._decay_step_counter(
)
ema = ExponentialMovingAverage(
config.get('ema_decay'), thres_steps=global_steps)
ema.update()
fetchs['ema'] = ema
return dataloader, fetchs, ema
return dataloader, fetchs
......@@ -396,7 +398,13 @@ def compile(config, program, loss_name=None):
total_step = 0
def run(dataloader, exe, program, fetchs, epoch=0, mode='train', vdl_writer=None):
def run(dataloader,
exe,
program,
fetchs,
epoch=0,
mode='train',
vdl_writer=None):
"""
Feed data to the model and fetch the measures and loss
......@@ -410,6 +418,7 @@ def run(dataloader, exe, program, fetchs, epoch=0, mode='train', vdl_writer=None
Returns:
"""
print(fetchs)
fetch_list = [f[0] for f in fetchs.values()]
metric_list = [f[1] for f in fetchs.values()]
for m in metric_list:
......
......@@ -70,8 +70,12 @@ def main(args):
best_top1_acc = 0.0 # best top1 acc record
if not config.get('use_ema'):
train_dataloader, train_fetchs = program.build(
config, train_prog, startup_prog, is_train=True)
else:
train_dataloader, train_fetchs, ema = program.build(
config, train_prog, startup_prog, is_train=True)
if config.validate:
valid_prog = fluid.Program()
......@@ -81,11 +85,11 @@ def main(args):
valid_prog = valid_prog.clone(for_test=True)
# create the "Executor" with the statement of which place
exe = fluid.Executor(place=place)
# only run startup_prog once to init
exe = fluid.Executor(place)
# Parameter initialization
exe.run(startup_prog)
# load model from checkpoint or pretrained model
# load model from 1. checkpoint to resume training, 2. pretrained model to finetune
init_model(config, train_prog, exe)
train_reader = Reader(config, 'train')()
......@@ -110,8 +114,8 @@ def main(args):
logger.info(logger.coloring("EMA validate start..."))
with train_fetchs('ema').apply(exe):
top1_acc = program.run(valid_dataloader, exe,
compiled_valid_prog, valid_fetchs,
epoch_id, 'valid')
compiled_valid_prog,
valid_fetchs, epoch_id, 'valid')
logger.info(logger.coloring("EMA validate over!"))
top1_acc = program.run(valid_dataloader, exe,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册