Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeShewill-CV committed Sep 23, 2020
1 parent 81288bf commit 069b355
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 96 deletions.
8 changes: 3 additions & 5 deletions lanenet_model/lanenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@
"""
import tensorflow as tf

from local_utils.config_utils import parse_config_utils
from lanenet_model import lanenet_back_end
from lanenet_model import lanenet_front_end
from semantic_segmentation_zoo import cnn_basenet

CFG = parse_config_utils.lanenet_cfg


class LaneNet(cnn_basenet.CNNBaseModel):
"""
"""
def __init__(self, phase):
def __init__(self, phase, cfg):
"""
"""
super(LaneNet, self).__init__()
self._net_flag = CFG.MODEL.FRONT_END
self._cfg = cfg
self._net_flag = self._cfg.MODEL.FRONT_END

self._frontend = lanenet_front_end.LaneNetFrondEnd(
phase=phase, net_flag=self._net_flag
Expand Down
7 changes: 4 additions & 3 deletions lanenet_model/lanenet_front_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ class LaneNetFrondEnd(cnn_basenet.CNNBaseModel):
"""
LaneNet frontend which is used to extract image features for following process
"""
def __init__(self, phase, net_flag):
def __init__(self, phase, net_flag, cfg):
"""
"""
super(LaneNetFrondEnd, self).__init__()
self._cfg = cfg

self._frontend_net_map = {
'vgg': vgg16_based_fcn.VGG16FCN(phase=phase),
'bisenetv2': bisenet_v2.BiseNetV2(phase=phase),
'vgg': vgg16_based_fcn.VGG16FCN(phase=phase, cfg=self._cfg),
'bisenetv2': bisenet_v2.BiseNetV2(phase=phase, cfg=self._cfg),
}

self._net = self._frontend_net_map[net_flag]
Expand Down
25 changes: 12 additions & 13 deletions semantic_segmentation_zoo/bisenet_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
from semantic_segmentation_zoo import cnn_basenet
from local_utils.config_utils import parse_config_utils

CFG = parse_config_utils.lanenet_cfg


class _StemBlock(cnn_basenet.CNNBaseModel):
"""
Expand Down Expand Up @@ -723,25 +721,26 @@ class BiseNetV2(cnn_basenet.CNNBaseModel):
"""
implementation of bisenet v2
"""
def __init__(self, phase):
def __init__(self, phase, cfg):
"""
"""
super(BiseNetV2, self).__init__()
self._cfg = cfg
self._phase = phase
self._is_training = self._is_net_for_training()

# set model hyper params
self._class_nums = CFG.DATASET.NUM_CLASSES
self._weights_decay = CFG.SOLVER.WEIGHT_DECAY
self._loss_type = CFG.SOLVER.LOSS_TYPE
self._enable_ohem = CFG.SOLVER.OHEM.ENABLE
self._class_nums = self._cfg.DATASET.NUM_CLASSES
self._weights_decay = self._cfg.SOLVER.WEIGHT_DECAY
self._loss_type = self._cfg.SOLVER.LOSS_TYPE
self._enable_ohem = self._cfg.SOLVER.OHEM.ENABLE
if self._enable_ohem:
self._ohem_score_thresh = CFG.SOLVER.OHEM.SCORE_THRESH
self._ohem_min_sample_nums = CFG.SOLVER.OHEM.MIN_SAMPLE_NUMS
self._ge_expand_ratio = CFG.MODEL.BISENETV2.GE_EXPAND_RATIO
self._semantic_channel_ratio = CFG.MODEL.BISENETV2.SEMANTIC_CHANNEL_LAMBDA
self._seg_head_ratio = CFG.MODEL.BISENETV2.SEGHEAD_CHANNEL_EXPAND_RATIO
self._ohem_score_thresh = self._cfg.SOLVER.OHEM.SCORE_THRESH
self._ohem_min_sample_nums = self._cfg.SOLVER.OHEM.MIN_SAMPLE_NUMS
self._ge_expand_ratio = self._cfg.MODEL.BISENETV2.GE_EXPAND_RATIO
self._semantic_channel_ratio = self._cfg.MODEL.BISENETV2.SEMANTIC_CHANNEL_LAMBDA
self._seg_head_ratio = self._cfg.MODEL.BISENETV2.SEGHEAD_CHANNEL_EXPAND_RATIO

# set module used in bisenetv2
self._se_block = _StemBlock(phase=phase)
Expand Down Expand Up @@ -1093,7 +1092,7 @@ def build_model(self, input_tensor, name, reuse=False):
test code
"""
test_in_tensor = tf.placeholder(dtype=tf.float32, shape=[1, 256, 512, 3], name='input')
model = BiseNetV2(phase='train')
model = BiseNetV2(phase='train', cfg=parse_config_utils.lanenet_cfg)
ret = model.build_model(test_in_tensor, name='bisenetv2')
for layer_name, layer_info in ret.items():
print('layer name: {:s} shape: {}'.format(layer_name, layer_info['shape']))
Expand Down
9 changes: 4 additions & 5 deletions semantic_segmentation_zoo/vgg16_based_fcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,21 @@
from semantic_segmentation_zoo import cnn_basenet
from local_utils.config_utils import parse_config_utils

CFG = parse_config_utils.lanenet_cfg


class VGG16FCN(cnn_basenet.CNNBaseModel):
"""
VGG 16 based fcn net for semantic segmentation
"""
def __init__(self, phase):
def __init__(self, phase, cfg):
"""
"""
super(VGG16FCN, self).__init__()
self._cfg = cfg
self._phase = phase
self._is_training = self._is_net_for_training()
self._net_intermediate_results = collections.OrderedDict()
self._class_nums = CFG.DATASET.NUM_CLASSES
self._class_nums = self._cfg.DATASET.NUM_CLASSES

def _is_net_for_training(self):
"""
Expand Down Expand Up @@ -369,7 +368,7 @@ def build_model(self, input_tensor, name, reuse=False):
test code
"""
test_in_tensor = tf.placeholder(dtype=tf.float32, shape=[1, 256, 512, 3], name='input')
model = VGG16FCN(phase='train')
model = VGG16FCN(phase='train', cfg=parse_config_utils.lanenet_cfg)
ret = model.build_model(test_in_tensor, name='vgg16fcn')
for layer_name, layer_info in ret.items():
print('layer name: {:s} shape: {}'.format(layer_name, layer_info['shape']))
4 changes: 2 additions & 2 deletions tools/train_lanenet_tusimple.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def train_model():
"""
if CFG.TRAIN.MULTI_GPU.ENABLE:
LOG.info('Using multi gpu trainner ...')
worker = multi_gpu_trainner.LaneNetTusimpleMultiTrainer()
worker = multi_gpu_trainner.LaneNetTusimpleMultiTrainer(cfg=CFG)
else:
LOG.info('Using single gpu trainner ...')
worker = single_gpu_trainner.LaneNetTusimpleTrainer()
worker = single_gpu_trainner.LaneNetTusimpleTrainer(cfg=CFG)

worker.train()
return
Expand Down
74 changes: 37 additions & 37 deletions trainner/tusimple_lanenet_multi_gpu_trainner.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,61 +23,61 @@
from local_utils.config_utils import parse_config_utils
from lanenet_model import lanenet

CFG = parse_config_utils.lanenet_cfg
LOG = loguru.logger


class LaneNetTusimpleMultiTrainer(object):
"""
init lanenet multi gpu trainner
"""
def __init__(self):
def __init__(self, cfg):
"""
initialize lanenet multi gpu trainner
"""
self._cfg = cfg
# define solver params and dataset
self._train_dataset = lanenet_data_feed_pipline.LaneNetDataFeeder(flags='train')
self._val_dataset = lanenet_data_feed_pipline.LaneNetDataFeeder(flags='val')
self._steps_per_epoch = len(self._train_dataset)
self._val_steps_per_epoch = len(self._val_dataset)

self._model_name = '{:s}_{:s}'.format(CFG.MODEL.FRONT_END, CFG.MODEL.MODEL_NAME)
self._model_name = '{:s}_{:s}'.format(self._cfg.MODEL.FRONT_END, self._cfg.MODEL.MODEL_NAME)

self._train_epoch_nums = CFG.TRAIN.EPOCH_NUMS
self._batch_size = CFG.TRAIN.BATCH_SIZE
self._val_batch_size = CFG.TRAIN.VAL_BATCH_SIZE
self._snapshot_epoch = CFG.TRAIN.SNAPSHOT_EPOCH
self._model_save_dir = ops.join(CFG.TRAIN.MODEL_SAVE_DIR, self._model_name)
self._tboard_save_dir = ops.join(CFG.TRAIN.TBOARD_SAVE_DIR, self._model_name)
self._enable_miou = CFG.TRAIN.COMPUTE_MIOU.ENABLE
self._train_epoch_nums = self._cfg.TRAIN.EPOCH_NUMS
self._batch_size = self._cfg.TRAIN.BATCH_SIZE
self._val_batch_size = self._cfg.TRAIN.VAL_BATCH_SIZE
self._snapshot_epoch = self._cfg.TRAIN.SNAPSHOT_EPOCH
self._model_save_dir = ops.join(self._cfg.TRAIN.MODEL_SAVE_DIR, self._model_name)
self._tboard_save_dir = ops.join(self._cfg.TRAIN.TBOARD_SAVE_DIR, self._model_name)
self._enable_miou = self._cfg.TRAIN.COMPUTE_MIOU.ENABLE
if self._enable_miou:
self._record_miou_epoch = CFG.TRAIN.COMPUTE_MIOU.EPOCH
self._input_tensor_size = [int(tmp) for tmp in CFG.AUG.TRAIN_CROP_SIZE]
self._gpu_devices = CFG.TRAIN.MULTI_GPU.GPU_DEVICES
self._record_miou_epoch = self._cfg.TRAIN.COMPUTE_MIOU.EPOCH
self._input_tensor_size = [int(tmp) for tmp in self._cfg.AUG.TRAIN_CROP_SIZE]
self._gpu_devices = self._cfg.TRAIN.MULTI_GPU.GPU_DEVICES
self._gpu_nums = len(self._gpu_devices)
self._chief_gpu_index = CFG.TRAIN.MULTI_GPU.CHIEF_DEVICE_INDEX
self._chief_gpu_index = self._cfg.TRAIN.MULTI_GPU.CHIEF_DEVICE_INDEX
self._batch_size_per_gpu = int(self._batch_size / self._gpu_nums)

self._init_learning_rate = CFG.SOLVER.LR
self._moving_ave_decay = CFG.SOLVER.MOVING_AVE_DECAY
self._momentum = CFG.SOLVER.MOMENTUM
self._lr_polynimal_decay_power = CFG.SOLVER.LR_POLYNOMIAL_POWER
self._optimizer_mode = CFG.SOLVER.OPTIMIZER.lower()
self._init_learning_rate = self._cfg.SOLVER.LR
self._moving_ave_decay = self._cfg.SOLVER.MOVING_AVE_DECAY
self._momentum = self._cfg.SOLVER.MOMENTUM
self._lr_polynimal_decay_power = self._cfg.SOLVER.LR_POLYNOMIAL_POWER
self._optimizer_mode = self._cfg.SOLVER.OPTIMIZER.lower()

if CFG.TRAIN.RESTORE_FROM_SNAPSHOT.ENABLE:
self._initial_weight = CFG.TRAIN.RESTORE_FROM_SNAPSHOT.SNAPSHOT_PATH
if self._cfg.TRAIN.RESTORE_FROM_SNAPSHOT.ENABLE:
self._initial_weight = self._cfg.TRAIN.RESTORE_FROM_SNAPSHOT.SNAPSHOT_PATH
else:
self._initial_weight = None
if CFG.TRAIN.WARM_UP.ENABLE:
self._warmup_epoches = CFG.TRAIN.WARM_UP.EPOCH_NUMS
if self._cfg.TRAIN.WARM_UP.ENABLE:
self._warmup_epoches = self._cfg.TRAIN.WARM_UP.EPOCH_NUMS
self._warmup_init_learning_rate = self._init_learning_rate / 1000.0
else:
self._warmup_epoches = 0

# define tensorflow session
sess_config = tf.ConfigProto(allow_soft_placement=True)
sess_config.gpu_options.per_process_gpu_memory_fraction = CFG.GPU.GPU_MEMORY_FRACTION
sess_config.gpu_options.allow_growth = CFG.GPU.TF_ALLOW_GROWTH
sess_config.gpu_options.per_process_gpu_memory_fraction = self._cfg.GPU.GPU_MEMORY_FRACTION
sess_config.gpu_options.allow_growth = self._cfg.GPU.TF_ALLOW_GROWTH
sess_config.gpu_options.allocator_type = 'BFC'
self._sess = tf.Session(config=sess_config)

Expand All @@ -97,8 +97,8 @@ def __init__(self):
self._val_dataset.next_batch(batch_size=self._val_batch_size)

# define model
self._model = lanenet.LaneNet(phase='train')
self._val_model = lanenet.LaneNet(phase='test')
self._model = lanenet.LaneNet(phase='train', cfg=self._cfg)
self._val_model = lanenet.LaneNet(phase='test', cfg=self._cfg)

# define average container
tower_grads = []
Expand Down Expand Up @@ -183,7 +183,7 @@ def __init__(self):

# define moving average op
with tf.variable_scope(name_or_scope='moving_avg'):
if CFG.TRAIN.FREEZE_BN.ENABLE:
if self._cfg.TRAIN.FREEZE_BN.ENABLE:
train_var_list = [
v for v in tf.trainable_variables() if 'beta' not in v.name and 'gamma' not in v.name
]
Expand Down Expand Up @@ -219,24 +219,24 @@ def __init__(self):
with tf.variable_scope('miou'):
pred = tf.reshape(self._binary_prediciton, [-1, ])
gt = tf.reshape(self._input_binary_label_image_list[self._chief_gpu_index], [-1, ])
indices = tf.squeeze(tf.where(tf.less_equal(gt, CFG.DATASET.NUM_CLASSES - 1)), 1)
indices = tf.squeeze(tf.where(tf.less_equal(gt, self._cfg.DATASET.NUM_CLASSES - 1)), 1)
gt = tf.gather(gt, indices)
pred = tf.gather(pred, indices)
self._miou, self._miou_update_op = tf.metrics.mean_iou(
labels=gt,
predictions=pred,
num_classes=CFG.DATASET.NUM_CLASSES
num_classes=self._cfg.DATASET.NUM_CLASSES
)

val_pred = tf.reshape(self._val_binary_prediction, [-1, ])
val_gt = tf.reshape(self._val_input_binary_label_image, [-1, ])
indices = tf.squeeze(tf.where(tf.less_equal(val_gt, CFG.DATASET.NUM_CLASSES - 1)), 1)
indices = tf.squeeze(tf.where(tf.less_equal(val_gt, self._cfg.DATASET.NUM_CLASSES - 1)), 1)
val_gt = tf.gather(val_gt, indices)
val_pred = tf.gather(val_pred, indices)
self._val_miou, self._val_miou_update_op = tf.metrics.mean_iou(
labels=val_gt,
predictions=val_pred,
num_classes=CFG.DATASET.NUM_CLASSES
num_classes=self._cfg.DATASET.NUM_CLASSES
)

# define saver and loader
Expand Down Expand Up @@ -278,9 +278,9 @@ def __init__(self):
if ops.exists(self._tboard_save_dir):
shutil.rmtree(self._tboard_save_dir)
os.makedirs(self._tboard_save_dir, exist_ok=True)
model_params_file_save_path = ops.join(self._tboard_save_dir, CFG.TRAIN.MODEL_PARAMS_CONFIG_FILE_NAME)
model_params_file_save_path = ops.join(self._tboard_save_dir, self._cfg.TRAIN.MODEL_PARAMS_CONFIG_FILE_NAME)
with open(model_params_file_save_path, 'w', encoding='utf-8') as f_obj:
CFG.dump_to_json_file(f_obj)
self._cfg.dump_to_json_file(f_obj)
self._write_summary_op = tf.summary.merge(summary_merge_list)
self._val_write_summary_op = tf.summary.merge(val_summary_merge_list)
self._summary_writer = tf.summary.FileWriter(self._tboard_save_dir, graph=self._sess.graph)
Expand Down Expand Up @@ -355,7 +355,7 @@ def _compute_net_gradients(self, images, binary_labels, instance_labels, optimiz
reuse=is_net_first_initialized
)

if CFG.TRAIN.FREEZE_BN.ENABLE:
if self._cfg.TRAIN.FREEZE_BN.ENABLE:
train_var_list = [
v for v in tf.trainable_variables() if 'beta' not in v.name and 'gamma' not in v.name
]
Expand All @@ -376,7 +376,7 @@ def train(self):
"""
self._sess.run(tf.global_variables_initializer())
self._sess.run(tf.local_variables_initializer())
if CFG.TRAIN.RESTORE_FROM_SNAPSHOT.ENABLE:
if self._cfg.TRAIN.RESTORE_FROM_SNAPSHOT.ENABLE:
try:
LOG.info('=> Restoring weights from: {:s} ... '.format(self._initial_weight))
self._loader.restore(self._sess, self._initial_weight)
Expand Down Expand Up @@ -550,5 +550,5 @@ def train(self):
"""
test code
"""
worker = LaneNetTusimpleMultiTrainer()
worker = LaneNetTusimpleMultiTrainer(cfg=parse_config_utils.lanenet_cfg)
print('Init complete')
Loading

0 comments on commit 069b355

Please sign in to comment.