↓ 跳过正文
  1. Posts/

how to copy & modify nets model on tensorflow slim

·662 字·2 分钟

想要修改tensorflow-slim 中 nets中的某个model,例如命名为kk_v2.py

观察到train_image_classifier.py中调用模型的部分

1network_fn = nets_factory.get_network_fn(
2        FLAGS.model_name,
3        num_classes=(dataset.num_classes - FLAGS.labels_offset),
4        weight_decay=FLAGS.weight_decay,
5        is_training=True)

调用了nets_factory.get_network_fn,get_network_fn如下:

 1def get_network_fn(name, num_classes, weight_decay=0.0, is_training=False):
 2  """Returns a network_fn such as `logits, end_points = network_fn(images)`.
 3
 4  Args:
 5    name: The name of the network.
 6    num_classes: The number of classes to use for classification.
 7    weight_decay: The l2 coefficient for the model weights.
 8    is_training: `True` if the model is being used for training and `False`
 9      otherwise.
10
11  Returns:
12    network_fn: A function that applies the model to a batch of images. It has
13      the following signature:
14        logits, end_points = network_fn(images)
15  Raises:
16    ValueError: If network `name` is not recognized.
17  """
18  if name not in networks_map:
19    raise ValueError('Name of network unknown %s' % name)
20  func = networks_map[name]
21  @functools.wraps(func)
22  def network_fn(images):
23    arg_scope = arg_scopes_map[name](weight_decay=weight_decay)
24    with slim.arg_scope(arg_scope):
25      return func(images, num_classes, is_training=is_training)
26  if hasattr(func, 'default_image_size'):
27    network_fn.default_image_size = func.default_image_size
28
29  return network_fn

我们看到model name 是通过 networks_map映射到func的

因此需要添加新model kk_v2的映射

 1networks_map = {'alexnet_v2': alexnet.alexnet_v2,
 2                'cifarnet': cifarnet.cifarnet,
 3                'overfeat': overfeat.overfeat,
 4                'vgg_a': vgg.vgg_a,
 5                'vgg_16': vgg.vgg_16,
 6                'vgg_19': vgg.vgg_19,
 7                'inception_v1': inception.inception_v1,
 8                'inception_v2': inception.inception_v2,
 9                'inception_v3': inception.inception_v3,
10                'inception_v4': inception.inception_v4,
11                'inception_resnet_v2': inception.inception_resnet_v2,
12                'kk_v2':inception.inception_resnet_v2,
13                'lenet': lenet.lenet,
14                'resnet_v1_50': resnet_v1.resnet_v1_50,
15                'resnet_v1_101': resnet_v1.resnet_v1_101,
16                'resnet_v1_152': resnet_v1.resnet_v1_152,
17                'resnet_v1_200': resnet_v1.resnet_v1_200,
18                'resnet_v2_50': resnet_v2.resnet_v2_50,
19                'resnet_v2_101': resnet_v2.resnet_v2_101,
20                'resnet_v2_152': resnet_v2.resnet_v2_152,
21                'resnet_v2_200': resnet_v2.resnet_v2_200,
22                'mobilenet_v1': mobilenet_v1.mobilenet_v1,
23               }

由于train_image_classifier.py中有如下参数,

1    tf.app.flags.DEFINE_string(
2        'preprocessing_name', None, 'The name of the preprocessing to use. If left '
3        'as `None`, then the model_name flag is used.'
4
5    preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name

因此还需要修改预处理的映射表

 1preprocessing_fn_map = {
 2      'cifarnet': cifarnet_preprocessing,
 3      'inception': inception_preprocessing,
 4      'inception_v1': inception_preprocessing,
 5      'inception_v2': inception_preprocessing,
 6      'inception_v3': inception_preprocessing,
 7      'inception_v4': inception_preprocessing,
 8      'inception_resnet_v2': inception_preprocessing,
 9      'kk_v2': inception_preprocessing,
10      'lenet': lenet_preprocessing,
11      'mobilenet_v1': inception_preprocessing,
12      'resnet_v1_50': vgg_preprocessing,
13      'resnet_v1_101': vgg_preprocessing,
14      'resnet_v1_152': vgg_preprocessing,
15      'resnet_v1_200': vgg_preprocessing,
16      'resnet_v2_50': vgg_preprocessing,
17      'resnet_v2_101': vgg_preprocessing,
18      'resnet_v2_152': vgg_preprocessing,
19      'resnet_v2_200': vgg_preprocessing,
20      'vgg': vgg_preprocessing,
21      'vgg_a': vgg_preprocessing,
22      'vgg_16': vgg_preprocessing,
23      'vgg_19': vgg_preprocessing,
24  }

还要修改arg_scopes_map,添加kk_v2的key

 1arg_scopes_map = {'alexnet_v2': alexnet.alexnet_v2_arg_scope,
 2                  'cifarnet': cifarnet.cifarnet_arg_scope,
 3                  'overfeat': overfeat.overfeat_arg_scope,
 4                  'vgg_a': vgg.vgg_arg_scope,
 5                  'vgg_16': vgg.vgg_arg_scope,
 6                  'vgg_19': vgg.vgg_arg_scope,
 7                  'inception_v1': inception.inception_v3_arg_scope,
 8                  'inception_v2': inception.inception_v3_arg_scope,
 9                  'inception_v3': inception.inception_v3_arg_scope,
10                  'inception_v4': inception.inception_v4_arg_scope,
11                  'inception_resnet_v2':
12                  inception.inception_resnet_v2_arg_scope,
13                  'kk_v2':
14                  inception.inception_resnet_v2_arg_scope,
15                  'lenet': lenet.lenet_arg_scope,
16                  'resnet_v1_50': resnet_v1.resnet_arg_scope,
17                  'resnet_v1_101': resnet_v1.resnet_arg_scope,
18                  'resnet_v1_152': resnet_v1.resnet_arg_scope,
19                  'resnet_v1_200': resnet_v1.resnet_arg_scope,
20                  'resnet_v2_50': resnet_v2.resnet_arg_scope,
21                  'resnet_v2_101': resnet_v2.resnet_arg_scope,
22                  'resnet_v2_152': resnet_v2.resnet_arg_scope,
23                  'resnet_v2_200': resnet_v2.resnet_arg_scope,
24                  'mobilenet_v1': mobilenet_v1.mobilenet_v1_arg_scope,
25                 }

相关文章

Inception-v4,Inception-ResNet 和残差连接对学习的影响

·6449 字·13 分钟
原始论文 翻译链接 **——前言:**作者认为残差连接在训练深度卷积模型时是很有必要的。至少在图像识别上,我们的研究似乎并不支持这一观点。 摘要: 近年来,深度卷积神经网络对图像识别性能的巨大提升发挥着关键作用。以Inception网络为例,其以相对较低的计算代价取得出色的表现。最近,与传统结构相结合的残差连接网络在2015ILSVRC挑战赛上取得非常优异的成绩;它的性能跟最新的Inception-v3 网络非常接近。因此也就引出了结合残差连接的Inception结构能否对性能进行提高的问题。本文给出实验证明,残差连接可以明显加速Inception网络的训练。同时实验也证明,相比没有残差连接的消耗相似的Inception网络,残差Inception网络在性能上具有微弱的优势。针对是否包含残差连接的Inception网络,本文同时提出了一些新的简化网络。这些网络的变体在ILSVRC2012分类任务上很明显的改善了单一框架的识别性能。本文进一步展示了适当的激活缩放如何使得很宽的残差Inception网络的训练更加稳定。本文通过对三个残差和一个Inception-v4进行组合,在top-5错误率上达到了 3.08%。

几种梯度下降(GD)法的比较(转载)

·1089 字·3 分钟
参考资料 机器学习中梯度下降(Gradient Descent, GD)算法只需要计算损失函数的一阶导数,计算代价小,非常适合训练数据非常大的应用。 梯度下降法的物理意义很好理解,就是沿着当前点的梯度方向进行线搜索,找到下一个迭代点。但是,为什么会派生出 batch、mini-batch、online 这些 GD 算法呢?