Unverified Commit 451906e4 authored by pkulzc's avatar pkulzc Committed by GitHub
Browse files

Release MobileDet code and model, and require tf_slim installation for OD API. (#8562)



* Merged commit includes the following changes:
311933687  by Sergio Guadarrama:

    Removes spurios use of tf.compat.v2, which results in spurious tf.compat.v1.compat.v2. Adds basic test to nasnet_utils.
    Replaces all remaining import tensorflow as tf with import tensorflow.compat.v1 as tf

--
311766063  by Sergio Guadarrama:

    Removes explicit tf.compat.v1 in all call sites (we already import tf.compat.v1, so this code was  doing tf.compat.v1.compat.v1). The existing code worked in latest version of tensorflow, 2.2, (and 1.15) but not in 1.14 or in 2.0.0a, this CL fixes it.

--
311624958  by Sergio Guadarrama:

    Updates README that doesn't render properly in github documentation

--
310980959  by Sergio Guadarrama:

    Moves research_models/slim off tf.contrib.slim/layers/framework to tf_slim

--
310263156  by Sergio Guadarrama:

    Adds model breakdown for MobilenetV3

--
308640516  by Sergio Guadarrama:

    Internal change

308244396  by Sergio Guadarrama:

    GroupNormalization support for MobilenetV3.

--
307475800  by Sergio Guadarrama:

    Internal change

--
302077708  by Sergio Guadarrama:

    Remove `disable_tf2` behavior from slim py_library targets

--
301208453  by Sergio Guadarrama:

    Automated refactoring to make code Python 3 compatible.

--
300816672  by Sergio Guadarrama:

    Internal change

299433840  by Sergio Guadarrama:

    Internal change

299221609  by Sergio Guadarrama:

    Explicitly disable Tensorflow v2 behaviors for all TF1.x binaries and tests

--
299179617  by Sergio Guadarrama:

    Internal change

299040784  by Sergio Guadarrama:

    Internal change

299036699  by Sergio Guadarrama:

    Internal change

298736510  by Sergio Guadarrama:

    Internal change

298732599  by Sergio Guadarrama:

    Internal change

298729507  by Sergio Guadarrama:

    Internal change

298253328  by Sergio Guadarrama:

    Internal change

297788346  by Sergio Guadarrama:

    Internal change

297785278  by Sergio Guadarrama:

    Internal change

297783127  by Sergio Guadarrama:

    Internal change

297725870  by Sergio Guadarrama:

    Internal change

297721811  by Sergio Guadarrama:

    Internal change

297711347  by Sergio Guadarrama:

    Internal change

297708059  by Sergio Guadarrama:

    Internal change

297701831  by Sergio Guadarrama:

    Internal change

297700038  by Sergio Guadarrama:

    Internal change

297670468  by Sergio Guadarrama:

    Internal change.

--
297350326  by Sergio Guadarrama:

    Explicitly replace "import tensorflow" with "tensorflow.compat.v1" for TF2.x migration

--
297201668  by Sergio Guadarrama:

    Explicitly replace "import tensorflow" with "tensorflow.compat.v1" for TF2.x migration

--
294483372  by Sergio Guadarrama:

    Internal change

PiperOrigin-RevId: 311933687

* Merged commit includes the following changes:
312578615  by Menglong Zhu:

    Modify the LSTM feature extractors to be python 3 compatible.

--
311264357  by Menglong Zhu:

    Removes contrib.slim

--
308957207  by Menglong Zhu:

    Automated refactoring to make code Python 3 compatible.

--
306976470  by yongzhe:

    Internal change

306777559  by Menglong Zhu:

    Internal change

--
299232507  by lzyuan:

    Internal update.

--
299221735  by lzyuan:

    Add small epsilon on max_range for quantize_op to prevent range collapse.

--

PiperOrigin-RevId: 312578615

* Merged commit includes the following changes:
310447280  by lzc:

    Internal changes.

--

PiperOrigin-RevId: 310447280
Co-authored-by: default avatarSergio Guadarrama <sguada@google.com>
Co-authored-by: default avatarMenglong Zhu <menglong@google.com>
parent 73b5be67
......@@ -18,15 +18,13 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
from tensorflow.contrib import slim as contrib_slim
import tensorflow.compat.v1 as tf
import tf_slim as slim
from nets import inception_utils
slim = contrib_slim
# pylint: disable=g-long-lambda
trunc_normal = lambda stddev: tf.compat.v1.truncated_normal_initializer(
trunc_normal = lambda stddev: tf.truncated_normal_initializer(
0.0, stddev)
......@@ -100,7 +98,7 @@ def inception_v3_base(inputs,
raise ValueError('depth_multiplier is not greater than zero.')
depth = lambda d: max(int(d * depth_multiplier), min_depth)
with tf.compat.v1.variable_scope(scope, 'InceptionV3', [inputs]):
with tf.variable_scope(scope, 'InceptionV3', [inputs]):
with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
stride=1, padding='VALID'):
# 299 x 299 x 3
......@@ -145,20 +143,20 @@ def inception_v3_base(inputs,
stride=1, padding='SAME'):
# mixed: 35 x 35 x 256.
end_point = 'Mixed_5b'
with tf.compat.v1.variable_scope(end_point):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(net, depth(48), [1, 1], scope='Conv2d_0a_1x1')
branch_1 = slim.conv2d(branch_1, depth(64), [5, 5],
scope='Conv2d_0b_5x5')
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
branch_2 = slim.conv2d(branch_2, depth(96), [3, 3],
scope='Conv2d_0b_3x3')
branch_2 = slim.conv2d(branch_2, depth(96), [3, 3],
scope='Conv2d_0c_3x3')
with tf.compat.v1.variable_scope('Branch_3'):
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(branch_3, depth(32), [1, 1],
scope='Conv2d_0b_1x1')
......@@ -168,21 +166,21 @@ def inception_v3_base(inputs,
# mixed_1: 35 x 35 x 288.
end_point = 'Mixed_5c'
with tf.compat.v1.variable_scope(end_point):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(net, depth(48), [1, 1], scope='Conv2d_0b_1x1')
branch_1 = slim.conv2d(branch_1, depth(64), [5, 5],
scope='Conv_1_0c_5x5')
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.conv2d(net, depth(64), [1, 1],
scope='Conv2d_0a_1x1')
branch_2 = slim.conv2d(branch_2, depth(96), [3, 3],
scope='Conv2d_0b_3x3')
branch_2 = slim.conv2d(branch_2, depth(96), [3, 3],
scope='Conv2d_0c_3x3')
with tf.compat.v1.variable_scope('Branch_3'):
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(branch_3, depth(64), [1, 1],
scope='Conv2d_0b_1x1')
......@@ -192,20 +190,20 @@ def inception_v3_base(inputs,
# mixed_2: 35 x 35 x 288.
end_point = 'Mixed_5d'
with tf.compat.v1.variable_scope(end_point):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(net, depth(48), [1, 1], scope='Conv2d_0a_1x1')
branch_1 = slim.conv2d(branch_1, depth(64), [5, 5],
scope='Conv2d_0b_5x5')
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
branch_2 = slim.conv2d(branch_2, depth(96), [3, 3],
scope='Conv2d_0b_3x3')
branch_2 = slim.conv2d(branch_2, depth(96), [3, 3],
scope='Conv2d_0c_3x3')
with tf.compat.v1.variable_scope('Branch_3'):
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(branch_3, depth(64), [1, 1],
scope='Conv2d_0b_1x1')
......@@ -215,17 +213,17 @@ def inception_v3_base(inputs,
# mixed_3: 17 x 17 x 768.
end_point = 'Mixed_6a'
with tf.compat.v1.variable_scope(end_point):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(net, depth(384), [3, 3], stride=2,
padding='VALID', scope='Conv2d_1a_1x1')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
branch_1 = slim.conv2d(branch_1, depth(96), [3, 3],
scope='Conv2d_0b_3x3')
branch_1 = slim.conv2d(branch_1, depth(96), [3, 3], stride=2,
padding='VALID', scope='Conv2d_1a_1x1')
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID',
scope='MaxPool_1a_3x3')
net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2])
......@@ -234,16 +232,16 @@ def inception_v3_base(inputs,
# mixed4: 17 x 17 x 768.
end_point = 'Mixed_6b'
with tf.compat.v1.variable_scope(end_point):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(net, depth(128), [1, 1], scope='Conv2d_0a_1x1')
branch_1 = slim.conv2d(branch_1, depth(128), [1, 7],
scope='Conv2d_0b_1x7')
branch_1 = slim.conv2d(branch_1, depth(192), [7, 1],
scope='Conv2d_0c_7x1')
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.conv2d(net, depth(128), [1, 1], scope='Conv2d_0a_1x1')
branch_2 = slim.conv2d(branch_2, depth(128), [7, 1],
scope='Conv2d_0b_7x1')
......@@ -253,7 +251,7 @@ def inception_v3_base(inputs,
scope='Conv2d_0d_7x1')
branch_2 = slim.conv2d(branch_2, depth(192), [1, 7],
scope='Conv2d_0e_1x7')
with tf.compat.v1.variable_scope('Branch_3'):
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(branch_3, depth(192), [1, 1],
scope='Conv2d_0b_1x1')
......@@ -263,16 +261,16 @@ def inception_v3_base(inputs,
# mixed_5: 17 x 17 x 768.
end_point = 'Mixed_6c'
with tf.compat.v1.variable_scope(end_point):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
branch_1 = slim.conv2d(branch_1, depth(160), [1, 7],
scope='Conv2d_0b_1x7')
branch_1 = slim.conv2d(branch_1, depth(192), [7, 1],
scope='Conv2d_0c_7x1')
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.conv2d(net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
branch_2 = slim.conv2d(branch_2, depth(160), [7, 1],
scope='Conv2d_0b_7x1')
......@@ -282,7 +280,7 @@ def inception_v3_base(inputs,
scope='Conv2d_0d_7x1')
branch_2 = slim.conv2d(branch_2, depth(192), [1, 7],
scope='Conv2d_0e_1x7')
with tf.compat.v1.variable_scope('Branch_3'):
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(branch_3, depth(192), [1, 1],
scope='Conv2d_0b_1x1')
......@@ -291,16 +289,16 @@ def inception_v3_base(inputs,
if end_point == final_endpoint: return net, end_points
# mixed_6: 17 x 17 x 768.
end_point = 'Mixed_6d'
with tf.compat.v1.variable_scope(end_point):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
branch_1 = slim.conv2d(branch_1, depth(160), [1, 7],
scope='Conv2d_0b_1x7')
branch_1 = slim.conv2d(branch_1, depth(192), [7, 1],
scope='Conv2d_0c_7x1')
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.conv2d(net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
branch_2 = slim.conv2d(branch_2, depth(160), [7, 1],
scope='Conv2d_0b_7x1')
......@@ -310,7 +308,7 @@ def inception_v3_base(inputs,
scope='Conv2d_0d_7x1')
branch_2 = slim.conv2d(branch_2, depth(192), [1, 7],
scope='Conv2d_0e_1x7')
with tf.compat.v1.variable_scope('Branch_3'):
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(branch_3, depth(192), [1, 1],
scope='Conv2d_0b_1x1')
......@@ -320,16 +318,16 @@ def inception_v3_base(inputs,
# mixed_7: 17 x 17 x 768.
end_point = 'Mixed_6e'
with tf.compat.v1.variable_scope(end_point):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
branch_1 = slim.conv2d(branch_1, depth(192), [1, 7],
scope='Conv2d_0b_1x7')
branch_1 = slim.conv2d(branch_1, depth(192), [7, 1],
scope='Conv2d_0c_7x1')
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
branch_2 = slim.conv2d(branch_2, depth(192), [7, 1],
scope='Conv2d_0b_7x1')
......@@ -339,7 +337,7 @@ def inception_v3_base(inputs,
scope='Conv2d_0d_7x1')
branch_2 = slim.conv2d(branch_2, depth(192), [1, 7],
scope='Conv2d_0e_1x7')
with tf.compat.v1.variable_scope('Branch_3'):
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(branch_3, depth(192), [1, 1],
scope='Conv2d_0b_1x1')
......@@ -349,12 +347,12 @@ def inception_v3_base(inputs,
# mixed_8: 8 x 8 x 1280.
end_point = 'Mixed_7a'
with tf.compat.v1.variable_scope(end_point):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
branch_0 = slim.conv2d(branch_0, depth(320), [3, 3], stride=2,
padding='VALID', scope='Conv2d_1a_3x3')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
branch_1 = slim.conv2d(branch_1, depth(192), [1, 7],
scope='Conv2d_0b_1x7')
......@@ -362,7 +360,7 @@ def inception_v3_base(inputs,
scope='Conv2d_0c_7x1')
branch_1 = slim.conv2d(branch_1, depth(192), [3, 3], stride=2,
padding='VALID', scope='Conv2d_1a_3x3')
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID',
scope='MaxPool_1a_3x3')
net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2])
......@@ -370,22 +368,22 @@ def inception_v3_base(inputs,
if end_point == final_endpoint: return net, end_points
# mixed_9: 8 x 8 x 2048.
end_point = 'Mixed_7b'
with tf.compat.v1.variable_scope(end_point):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(net, depth(320), [1, 1], scope='Conv2d_0a_1x1')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(net, depth(384), [1, 1], scope='Conv2d_0a_1x1')
branch_1 = tf.concat(axis=3, values=[
slim.conv2d(branch_1, depth(384), [1, 3], scope='Conv2d_0b_1x3'),
slim.conv2d(branch_1, depth(384), [3, 1], scope='Conv2d_0b_3x1')])
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.conv2d(net, depth(448), [1, 1], scope='Conv2d_0a_1x1')
branch_2 = slim.conv2d(
branch_2, depth(384), [3, 3], scope='Conv2d_0b_3x3')
branch_2 = tf.concat(axis=3, values=[
slim.conv2d(branch_2, depth(384), [1, 3], scope='Conv2d_0c_1x3'),
slim.conv2d(branch_2, depth(384), [3, 1], scope='Conv2d_0d_3x1')])
with tf.compat.v1.variable_scope('Branch_3'):
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(
branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
......@@ -395,22 +393,22 @@ def inception_v3_base(inputs,
# mixed_10: 8 x 8 x 2048.
end_point = 'Mixed_7c'
with tf.compat.v1.variable_scope(end_point):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(net, depth(320), [1, 1], scope='Conv2d_0a_1x1')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(net, depth(384), [1, 1], scope='Conv2d_0a_1x1')
branch_1 = tf.concat(axis=3, values=[
slim.conv2d(branch_1, depth(384), [1, 3], scope='Conv2d_0b_1x3'),
slim.conv2d(branch_1, depth(384), [3, 1], scope='Conv2d_0c_3x1')])
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.conv2d(net, depth(448), [1, 1], scope='Conv2d_0a_1x1')
branch_2 = slim.conv2d(
branch_2, depth(384), [3, 3], scope='Conv2d_0b_3x3')
branch_2 = tf.concat(axis=3, values=[
slim.conv2d(branch_2, depth(384), [1, 3], scope='Conv2d_0c_1x3'),
slim.conv2d(branch_2, depth(384), [3, 1], scope='Conv2d_0d_3x1')])
with tf.compat.v1.variable_scope('Branch_3'):
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(
branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
......@@ -486,7 +484,7 @@ def inception_v3(inputs,
raise ValueError('depth_multiplier is not greater than zero.')
depth = lambda d: max(int(d * depth_multiplier), min_depth)
with tf.compat.v1.variable_scope(
with tf.variable_scope(
scope, 'InceptionV3', [inputs], reuse=reuse) as scope:
with slim.arg_scope([slim.batch_norm, slim.dropout],
is_training=is_training):
......@@ -499,7 +497,7 @@ def inception_v3(inputs,
with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
stride=1, padding='SAME'):
aux_logits = end_points['Mixed_6e']
with tf.compat.v1.variable_scope('AuxLogits'):
with tf.variable_scope('AuxLogits'):
aux_logits = slim.avg_pool2d(
aux_logits, [5, 5], stride=3, padding='VALID',
scope='AvgPool_1a_5x5')
......@@ -522,7 +520,7 @@ def inception_v3(inputs,
end_points['AuxLogits'] = aux_logits
# Final pooling and prediction
with tf.compat.v1.variable_scope('Logits'):
with tf.variable_scope('Logits'):
if global_pool:
# Global average pooling.
net = tf.reduce_mean(
......
......@@ -19,13 +19,11 @@ from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
from tensorflow.contrib import slim as contrib_slim
import tensorflow.compat.v1 as tf
import tf_slim as slim
from nets import inception
slim = contrib_slim
class InceptionV3Test(tf.test.TestCase):
......@@ -89,7 +87,7 @@ class InceptionV3Test(tf.test.TestCase):
inputs, final_endpoint=endpoint)
self.assertTrue(out_tensor.op.name.startswith(
'InceptionV3/' + endpoint))
self.assertItemsEqual(endpoints[:index+1], end_points.keys())
self.assertItemsEqual(endpoints[:index + 1], end_points.keys())
def testBuildAndCheckAllEndPointsUptoMixed7c(self):
batch_size = 5
......@@ -223,31 +221,31 @@ class InceptionV3Test(tf.test.TestCase):
[batch_size, 3, 3, 2048])
def testUnknownImageShape(self):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
batch_size = 2
height, width = 299, 299
num_classes = 1000
input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
with self.test_session() as sess:
inputs = tf.compat.v1.placeholder(
inputs = tf.placeholder(
tf.float32, shape=(batch_size, None, None, 3))
logits, end_points = inception.inception_v3(inputs, num_classes)
self.assertListEqual(logits.get_shape().as_list(),
[batch_size, num_classes])
pre_pool = end_points['Mixed_7c']
feed_dict = {inputs: input_np}
tf.compat.v1.global_variables_initializer().run()
tf.global_variables_initializer().run()
pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
self.assertListEqual(list(pre_pool_out.shape), [batch_size, 8, 8, 2048])
def testGlobalPoolUnknownImageShape(self):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
batch_size = 1
height, width = 330, 400
num_classes = 1000
input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
with self.test_session() as sess:
inputs = tf.compat.v1.placeholder(
inputs = tf.placeholder(
tf.float32, shape=(batch_size, None, None, 3))
logits, end_points = inception.inception_v3(inputs, num_classes,
global_pool=True)
......@@ -255,7 +253,7 @@ class InceptionV3Test(tf.test.TestCase):
[batch_size, num_classes])
pre_pool = end_points['Mixed_7c']
feed_dict = {inputs: input_np}
tf.compat.v1.global_variables_initializer().run()
tf.global_variables_initializer().run()
pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
self.assertListEqual(list(pre_pool_out.shape), [batch_size, 8, 11, 2048])
......@@ -264,7 +262,7 @@ class InceptionV3Test(tf.test.TestCase):
height, width = 299, 299
num_classes = 1000
inputs = tf.compat.v1.placeholder(tf.float32, (None, height, width, 3))
inputs = tf.placeholder(tf.float32, (None, height, width, 3))
logits, _ = inception.inception_v3(inputs, num_classes)
self.assertTrue(logits.op.name.startswith('InceptionV3/Logits'))
self.assertListEqual(logits.get_shape().as_list(),
......@@ -272,7 +270,7 @@ class InceptionV3Test(tf.test.TestCase):
images = tf.random.uniform((batch_size, height, width, 3))
with self.test_session() as sess:
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(tf.global_variables_initializer())
output = sess.run(logits, {inputs: images.eval()})
self.assertEquals(output.shape, (batch_size, num_classes))
......@@ -287,7 +285,7 @@ class InceptionV3Test(tf.test.TestCase):
predictions = tf.argmax(input=logits, axis=1)
with self.test_session() as sess:
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(tf.global_variables_initializer())
output = sess.run(predictions)
self.assertEquals(output.shape, (batch_size,))
......@@ -305,7 +303,7 @@ class InceptionV3Test(tf.test.TestCase):
predictions = tf.argmax(input=logits, axis=1)
with self.test_session() as sess:
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(tf.global_variables_initializer())
output = sess.run(predictions)
self.assertEquals(output.shape, (eval_batch_size,))
......@@ -317,32 +315,32 @@ class InceptionV3Test(tf.test.TestCase):
spatial_squeeze=False)
with self.test_session() as sess:
tf.compat.v1.global_variables_initializer().run()
tf.global_variables_initializer().run()
logits_out = sess.run(logits)
self.assertListEqual(list(logits_out.shape), [1, 1, 1, num_classes])
def testNoBatchNormScaleByDefault(self):
height, width = 299, 299
num_classes = 1000
inputs = tf.compat.v1.placeholder(tf.float32, (1, height, width, 3))
inputs = tf.placeholder(tf.float32, (1, height, width, 3))
with slim.arg_scope(inception.inception_v3_arg_scope()):
inception.inception_v3(inputs, num_classes, is_training=False)
self.assertEqual(tf.compat.v1.global_variables('.*/BatchNorm/gamma:0$'), [])
self.assertEqual(tf.global_variables('.*/BatchNorm/gamma:0$'), [])
def testBatchNormScale(self):
height, width = 299, 299
num_classes = 1000
inputs = tf.compat.v1.placeholder(tf.float32, (1, height, width, 3))
inputs = tf.placeholder(tf.float32, (1, height, width, 3))
with slim.arg_scope(
inception.inception_v3_arg_scope(batch_norm_scale=True)):
inception.inception_v3(inputs, num_classes, is_training=False)
gamma_names = set(
v.op.name
for v in tf.compat.v1.global_variables('.*/BatchNorm/gamma:0$'))
for v in tf.global_variables('.*/BatchNorm/gamma:0$'))
self.assertGreater(len(gamma_names), 0)
for v in tf.compat.v1.global_variables('.*/BatchNorm/moving_mean:0$'):
for v in tf.global_variables('.*/BatchNorm/moving_mean:0$'):
self.assertIn(v.op.name[:-len('moving_mean')] + 'gamma', gamma_names)
......
......@@ -24,31 +24,29 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
from tensorflow.contrib import slim as contrib_slim
import tensorflow.compat.v1 as tf
import tf_slim as slim
from nets import inception_utils
slim = contrib_slim
def block_inception_a(inputs, scope=None, reuse=None):
"""Builds Inception-A block for Inception v4 network."""
# By default use stride=1 and SAME padding
with slim.arg_scope([slim.conv2d, slim.avg_pool2d, slim.max_pool2d],
stride=1, padding='SAME'):
with tf.compat.v1.variable_scope(
with tf.variable_scope(
scope, 'BlockInceptionA', [inputs], reuse=reuse):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(inputs, 96, [1, 1], scope='Conv2d_0a_1x1')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(inputs, 64, [1, 1], scope='Conv2d_0a_1x1')
branch_1 = slim.conv2d(branch_1, 96, [3, 3], scope='Conv2d_0b_3x3')
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.conv2d(inputs, 64, [1, 1], scope='Conv2d_0a_1x1')
branch_2 = slim.conv2d(branch_2, 96, [3, 3], scope='Conv2d_0b_3x3')
branch_2 = slim.conv2d(branch_2, 96, [3, 3], scope='Conv2d_0c_3x3')
with tf.compat.v1.variable_scope('Branch_3'):
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(inputs, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(branch_3, 96, [1, 1], scope='Conv2d_0b_1x1')
return tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
......@@ -59,17 +57,17 @@ def block_reduction_a(inputs, scope=None, reuse=None):
# By default use stride=1 and SAME padding
with slim.arg_scope([slim.conv2d, slim.avg_pool2d, slim.max_pool2d],
stride=1, padding='SAME'):
with tf.compat.v1.variable_scope(
with tf.variable_scope(
scope, 'BlockReductionA', [inputs], reuse=reuse):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(inputs, 384, [3, 3], stride=2, padding='VALID',
scope='Conv2d_1a_3x3')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(inputs, 192, [1, 1], scope='Conv2d_0a_1x1')
branch_1 = slim.conv2d(branch_1, 224, [3, 3], scope='Conv2d_0b_3x3')
branch_1 = slim.conv2d(branch_1, 256, [3, 3], stride=2,
padding='VALID', scope='Conv2d_1a_3x3')
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.max_pool2d(inputs, [3, 3], stride=2, padding='VALID',
scope='MaxPool_1a_3x3')
return tf.concat(axis=3, values=[branch_0, branch_1, branch_2])
......@@ -80,21 +78,21 @@ def block_inception_b(inputs, scope=None, reuse=None):
# By default use stride=1 and SAME padding
with slim.arg_scope([slim.conv2d, slim.avg_pool2d, slim.max_pool2d],
stride=1, padding='SAME'):
with tf.compat.v1.variable_scope(
with tf.variable_scope(
scope, 'BlockInceptionB', [inputs], reuse=reuse):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(inputs, 384, [1, 1], scope='Conv2d_0a_1x1')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(inputs, 192, [1, 1], scope='Conv2d_0a_1x1')
branch_1 = slim.conv2d(branch_1, 224, [1, 7], scope='Conv2d_0b_1x7')
branch_1 = slim.conv2d(branch_1, 256, [7, 1], scope='Conv2d_0c_7x1')
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.conv2d(inputs, 192, [1, 1], scope='Conv2d_0a_1x1')
branch_2 = slim.conv2d(branch_2, 192, [7, 1], scope='Conv2d_0b_7x1')
branch_2 = slim.conv2d(branch_2, 224, [1, 7], scope='Conv2d_0c_1x7')
branch_2 = slim.conv2d(branch_2, 224, [7, 1], scope='Conv2d_0d_7x1')
branch_2 = slim.conv2d(branch_2, 256, [1, 7], scope='Conv2d_0e_1x7')
with tf.compat.v1.variable_scope('Branch_3'):
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(inputs, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(branch_3, 128, [1, 1], scope='Conv2d_0b_1x1')
return tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
......@@ -105,19 +103,19 @@ def block_reduction_b(inputs, scope=None, reuse=None):
# By default use stride=1 and SAME padding
with slim.arg_scope([slim.conv2d, slim.avg_pool2d, slim.max_pool2d],
stride=1, padding='SAME'):
with tf.compat.v1.variable_scope(
with tf.variable_scope(
scope, 'BlockReductionB', [inputs], reuse=reuse):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(inputs, 192, [1, 1], scope='Conv2d_0a_1x1')
branch_0 = slim.conv2d(branch_0, 192, [3, 3], stride=2,
padding='VALID', scope='Conv2d_1a_3x3')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(inputs, 256, [1, 1], scope='Conv2d_0a_1x1')
branch_1 = slim.conv2d(branch_1, 256, [1, 7], scope='Conv2d_0b_1x7')
branch_1 = slim.conv2d(branch_1, 320, [7, 1], scope='Conv2d_0c_7x1')
branch_1 = slim.conv2d(branch_1, 320, [3, 3], stride=2,
padding='VALID', scope='Conv2d_1a_3x3')
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.max_pool2d(inputs, [3, 3], stride=2, padding='VALID',
scope='MaxPool_1a_3x3')
return tf.concat(axis=3, values=[branch_0, branch_1, branch_2])
......@@ -128,23 +126,23 @@ def block_inception_c(inputs, scope=None, reuse=None):
# By default use stride=1 and SAME padding
with slim.arg_scope([slim.conv2d, slim.avg_pool2d, slim.max_pool2d],
stride=1, padding='SAME'):
with tf.compat.v1.variable_scope(
with tf.variable_scope(
scope, 'BlockInceptionC', [inputs], reuse=reuse):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(inputs, 256, [1, 1], scope='Conv2d_0a_1x1')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(inputs, 384, [1, 1], scope='Conv2d_0a_1x1')
branch_1 = tf.concat(axis=3, values=[
slim.conv2d(branch_1, 256, [1, 3], scope='Conv2d_0b_1x3'),
slim.conv2d(branch_1, 256, [3, 1], scope='Conv2d_0c_3x1')])
with tf.compat.v1.variable_scope('Branch_2'):
with tf.variable_scope('Branch_2'):
branch_2 = slim.conv2d(inputs, 384, [1, 1], scope='Conv2d_0a_1x1')
branch_2 = slim.conv2d(branch_2, 448, [3, 1], scope='Conv2d_0b_3x1')
branch_2 = slim.conv2d(branch_2, 512, [1, 3], scope='Conv2d_0c_1x3')
branch_2 = tf.concat(axis=3, values=[
slim.conv2d(branch_2, 256, [1, 3], scope='Conv2d_0d_1x3'),
slim.conv2d(branch_2, 256, [3, 1], scope='Conv2d_0e_3x1')])
with tf.compat.v1.variable_scope('Branch_3'):
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(inputs, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(branch_3, 256, [1, 1], scope='Conv2d_0b_1x1')
return tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
......@@ -176,7 +174,7 @@ def inception_v4_base(inputs, final_endpoint='Mixed_7d', scope=None):
end_points[name] = net
return name == final_endpoint
with tf.compat.v1.variable_scope(scope, 'InceptionV4', [inputs]):
with tf.variable_scope(scope, 'InceptionV4', [inputs]):
with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
stride=1, padding='SAME'):
# 299 x 299 x 3
......@@ -191,23 +189,23 @@ def inception_v4_base(inputs, final_endpoint='Mixed_7d', scope=None):
net = slim.conv2d(net, 64, [3, 3], scope='Conv2d_2b_3x3')
if add_and_check_final('Conv2d_2b_3x3', net): return net, end_points
# 147 x 147 x 64
with tf.compat.v1.variable_scope('Mixed_3a'):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope('Mixed_3a'):
with tf.variable_scope('Branch_0'):
branch_0 = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID',
scope='MaxPool_0a_3x3')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(net, 96, [3, 3], stride=2, padding='VALID',
scope='Conv2d_0a_3x3')
net = tf.concat(axis=3, values=[branch_0, branch_1])
if add_and_check_final('Mixed_3a', net): return net, end_points
# 73 x 73 x 160
with tf.compat.v1.variable_scope('Mixed_4a'):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope('Mixed_4a'):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(net, 64, [1, 1], scope='Conv2d_0a_1x1')
branch_0 = slim.conv2d(branch_0, 96, [3, 3], padding='VALID',
scope='Conv2d_1a_3x3')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(net, 64, [1, 1], scope='Conv2d_0a_1x1')
branch_1 = slim.conv2d(branch_1, 64, [1, 7], scope='Conv2d_0b_1x7')
branch_1 = slim.conv2d(branch_1, 64, [7, 1], scope='Conv2d_0c_7x1')
......@@ -217,11 +215,11 @@ def inception_v4_base(inputs, final_endpoint='Mixed_7d', scope=None):
if add_and_check_final('Mixed_4a', net): return net, end_points
# 71 x 71 x 192
with tf.compat.v1.variable_scope('Mixed_5a'):
with tf.compat.v1.variable_scope('Branch_0'):
with tf.variable_scope('Mixed_5a'):
with tf.variable_scope('Branch_0'):
branch_0 = slim.conv2d(net, 192, [3, 3], stride=2, padding='VALID',
scope='Conv2d_1a_3x3')
with tf.compat.v1.variable_scope('Branch_1'):
with tf.variable_scope('Branch_1'):
branch_1 = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID',
scope='MaxPool_1a_3x3')
net = tf.concat(axis=3, values=[branch_0, branch_1])
......@@ -286,7 +284,7 @@ def inception_v4(inputs, num_classes=1001, is_training=True,
end_points: the set of end_points from the inception model.
"""
end_points = {}
with tf.compat.v1.variable_scope(
with tf.variable_scope(
scope, 'InceptionV4', [inputs], reuse=reuse) as scope:
with slim.arg_scope([slim.batch_norm, slim.dropout],
is_training=is_training):
......@@ -296,7 +294,7 @@ def inception_v4(inputs, num_classes=1001, is_training=True,
stride=1, padding='SAME'):
# Auxiliary Head logits
if create_aux_logits and num_classes:
with tf.compat.v1.variable_scope('AuxLogits'):
with tf.variable_scope('AuxLogits'):
# 17 x 17 x 1024
aux_logits = end_points['Mixed_6h']
aux_logits = slim.avg_pool2d(aux_logits, [5, 5], stride=3,
......@@ -316,7 +314,7 @@ def inception_v4(inputs, num_classes=1001, is_training=True,
# Final pooling and prediction
# TODO(sguada,arnoegw): Consider adding a parameter global_pool which
# can be set to False to disable pooling here (as in resnet_*()).
with tf.compat.v1.variable_scope('Logits'):
with tf.variable_scope('Logits'):
# 8 x 8 x 1536
kernel_size = net.get_shape()[1:3]
if kernel_size.is_fully_defined():
......
......@@ -17,8 +17,8 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
from tensorflow.contrib import slim as contrib_slim
import tensorflow.compat.v1 as tf
import tf_slim as slim
from nets import inception
......@@ -147,7 +147,7 @@ class InceptionTest(tf.test.TestCase):
inputs, final_endpoint=endpoint)
self.assertTrue(out_tensor.op.name.startswith(
'InceptionV4/' + endpoint))
self.assertItemsEqual(all_endpoints[:index+1], end_points.keys())
self.assertItemsEqual(all_endpoints[:index + 1], end_points.keys())
def testVariablesSetDevice(self):
batch_size = 5
......@@ -155,15 +155,15 @@ class InceptionTest(tf.test.TestCase):
num_classes = 1000
inputs = tf.random.uniform((batch_size, height, width, 3))
# Force all Variables to reside on the device.
with tf.compat.v1.variable_scope('on_cpu'), tf.device('/cpu:0'):
with tf.variable_scope('on_cpu'), tf.device('/cpu:0'):
inception.inception_v4(inputs, num_classes)
with tf.compat.v1.variable_scope('on_gpu'), tf.device('/gpu:0'):
with tf.variable_scope('on_gpu'), tf.device('/gpu:0'):
inception.inception_v4(inputs, num_classes)
for v in tf.compat.v1.get_collection(
tf.compat.v1.GraphKeys.GLOBAL_VARIABLES, scope='on_cpu'):
for v in tf.get_collection(
tf.GraphKeys.GLOBAL_VARIABLES, scope='on_cpu'):
self.assertDeviceEqual(v.device, '/cpu:0')
for v in tf.compat.v1.get_collection(
tf.compat.v1.GraphKeys.GLOBAL_VARIABLES, scope='on_gpu'):
for v in tf.get_collection(
tf.GraphKeys.GLOBAL_VARIABLES, scope='on_gpu'):
self.assertDeviceEqual(v.device, '/gpu:0')
def testHalfSizeImages(self):
......@@ -197,7 +197,7 @@ class InceptionTest(tf.test.TestCase):
height, width = 350, 400
num_classes = 1000
with self.test_session() as sess:
inputs = tf.compat.v1.placeholder(tf.float32, (batch_size, None, None, 3))
inputs = tf.placeholder(tf.float32, (batch_size, None, None, 3))
logits, end_points = inception.inception_v4(
inputs, num_classes, create_aux_logits=False)
self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
......@@ -205,7 +205,7 @@ class InceptionTest(tf.test.TestCase):
[batch_size, num_classes])
pre_pool = end_points['Mixed_7d']
images = tf.random.uniform((batch_size, height, width, 3))
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(tf.global_variables_initializer())
logits_out, pre_pool_out = sess.run([logits, pre_pool],
{inputs: images.eval()})
self.assertTupleEqual(logits_out.shape, (batch_size, num_classes))
......@@ -216,13 +216,13 @@ class InceptionTest(tf.test.TestCase):
height, width = 299, 299
num_classes = 1000
with self.test_session() as sess:
inputs = tf.compat.v1.placeholder(tf.float32, (None, height, width, 3))
inputs = tf.placeholder(tf.float32, (None, height, width, 3))
logits, _ = inception.inception_v4(inputs, num_classes)
self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
self.assertListEqual(logits.get_shape().as_list(),
[None, num_classes])
images = tf.random.uniform((batch_size, height, width, 3))
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(tf.global_variables_initializer())
output = sess.run(logits, {inputs: images.eval()})
self.assertEquals(output.shape, (batch_size, num_classes))
......@@ -236,7 +236,7 @@ class InceptionTest(tf.test.TestCase):
num_classes,
is_training=False)
predictions = tf.argmax(input=logits, axis=1)
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(tf.global_variables_initializer())
output = sess.run(predictions)
self.assertEquals(output.shape, (batch_size,))
......@@ -254,32 +254,32 @@ class InceptionTest(tf.test.TestCase):
is_training=False,
reuse=True)
predictions = tf.argmax(input=logits, axis=1)
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(tf.global_variables_initializer())
output = sess.run(predictions)
self.assertEquals(output.shape, (eval_batch_size,))
def testNoBatchNormScaleByDefault(self):
height, width = 299, 299
num_classes = 1000
inputs = tf.compat.v1.placeholder(tf.float32, (1, height, width, 3))
with contrib_slim.arg_scope(inception.inception_v4_arg_scope()):
inputs = tf.placeholder(tf.float32, (1, height, width, 3))
with slim.arg_scope(inception.inception_v4_arg_scope()):
inception.inception_v4(inputs, num_classes, is_training=False)
self.assertEqual(tf.compat.v1.global_variables('.*/BatchNorm/gamma:0$'), [])
self.assertEqual(tf.global_variables('.*/BatchNorm/gamma:0$'), [])
def testBatchNormScale(self):
height, width = 299, 299
num_classes = 1000
inputs = tf.compat.v1.placeholder(tf.float32, (1, height, width, 3))
with contrib_slim.arg_scope(
inputs = tf.placeholder(tf.float32, (1, height, width, 3))
with slim.arg_scope(
inception.inception_v4_arg_scope(batch_norm_scale=True)):
inception.inception_v4(inputs, num_classes, is_training=False)
gamma_names = set(
v.op.name
for v in tf.compat.v1.global_variables('.*/BatchNorm/gamma:0$'))
for v in tf.global_variables('.*/BatchNorm/gamma:0$'))
self.assertGreater(len(gamma_names), 0)
for v in tf.compat.v1.global_variables('.*/BatchNorm/moving_mean:0$'):
for v in tf.global_variables('.*/BatchNorm/moving_mean:0$'):
self.assertIn(v.op.name[:-len('moving_mean')] + 'gamma', gamma_names)
......
......@@ -18,10 +18,8 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
from tensorflow.contrib import slim as contrib_slim
slim = contrib_slim
import tensorflow.compat.v1 as tf
import tf_slim as slim
def lenet(images, num_classes=10, is_training=False,
......@@ -59,7 +57,7 @@ def lenet(images, num_classes=10, is_training=False,
"""
end_points = {}
with tf.compat.v1.variable_scope(scope, 'LeNet', [images]):
with tf.variable_scope(scope, 'LeNet', [images]):
net = end_points['conv1'] = slim.conv2d(images, 32, [5, 5], scope='conv1')
net = end_points['pool1'] = slim.max_pool2d(net, [2, 2], 2, scope='pool1')
net = end_points['conv2'] = slim.conv2d(net, 64, [5, 5], scope='conv2')
......@@ -93,6 +91,6 @@ def lenet_arg_scope(weight_decay=0.0):
with slim.arg_scope(
[slim.conv2d, slim.fully_connected],
weights_regularizer=slim.l2_regularizer(weight_decay),
weights_initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.1),
weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
activation_fn=tf.nn.relu) as sc:
return sc
......@@ -6,7 +6,7 @@ This folder contains building code for
definition for each model is located in [mobilenet_v2.py](mobilenet_v2.py) and
[mobilenet_v3.py](mobilenet_v3.py) respectively.
For MobilenetV1 please refer to this [page](../mobilenet_v1.md)
For MobilenetV1 please refer to [this page](../mobilenet_v1.md)
We have also introduced a family of MobileNets customized for the Edge TPU
accelerator found in
......@@ -61,20 +61,20 @@ on CPU, we find that they are much more performant on GPU/DSP.
| Imagenet Checkpoint | MACs (M) | Params (M) | Top1 | Pixel 1 | Pixel 2 | Pixel 3 |
| ------------------ | -------- | ---------- | ---- | ------- | ------- | ------- |
| [Large dm=1 (float)] | 217 | 5.4 | 75.2 | 51.2 | 61 | 44 |
| [Large dm=1 (8-bit)] | 217 | 5.4 | 73.9 | 44 | 42.5 | 32 |
| [Large dm=1 (float)] | 217 | 5.4 | 75.2 | 51.2 | 61 | 44 |
| [Large dm=1 (8-bit)] | 217 | 5.4 | 73.9 | 44 | 42.5 | 32 |
| [Large dm=0.75 (float)] | 155 | 4.0 | 73.3 | 39.8 | 48 | 34 |
| [Small dm=1 (float)] | 66 | 2.9 | 67.5 | 15.8 | 19.4 | 14.4 |
| [Small dm=1 (8-bit)] | 66 | 2.9 | 64.9 | 15.5 | 15 | 10.7 |
| [Small dm=1 (float)] | 66 | 2.9 | 67.5 | 15.8 | 19.4 | 14.4 |
| [Small dm=1 (8-bit)] | 66 | 2.9 | 64.9 | 15.5 | 15 | 10.7 |
| [Small dm=0.75 (float)] | 44 | 2.4 | 65.4 | 12.8 | 15.9 | 11.6 |
#### Minimalistic checkpoints:
| Imagenet Checkpoint | MACs (M) | Params (M) | Top1 | Pixel 1 | Pixel 2 | Pixel 3 |
| -------------- | -------- | ---------- | ---- | ------- | ------- | ------- |
| [Large minimalistic (float)] | 209 | 3.9 | 72.3 | 44.1 | 51 | 35 |
| [Large minimalistic (float)] | 209 | 3.9 | 72.3 | 44.1 | 51 | 35 |
| [Large minimalistic (8-bit)][lm8] | 209 | 3.9 | 71.3 | 37 | 35 | 27 |
| [Small minimalistic (float)] | 65 | 2.0 | 61.9 | 12.2 | 15.1 | 11 |
| [Small minimalistic (float)] | 65 | 2.0 | 61.9 | 12.2 | 15.1 | 11 |
#### Edge TPU checkpoints:
......@@ -103,36 +103,87 @@ tool.
### Mobilenet V2 Imagenet Checkpoints
Classification Checkpoint | MACs (M) | Parameters (M) | Top 1 Accuracy | Top 5 Accuracy | Mobile CPU (ms) Pixel 1
---------------------------------------------------------------------------------------------------------- | -------- | -------------- | -------------- | -------------- | -----------------------
[mobilenet_v2_1.4_224](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.4_224.tgz) | 582 | 6.06 | 75.0 | 92.5 | 138.0
[mobilenet_v2_1.3_224](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.3_224.tgz) | 509 | 5.34 | 74.4 | 92.1 | 123.0
[mobilenet_v2_1.0_224](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_224.tgz) | 300 | 3.47 | 71.8 | 91.0 | 73.8
[mobilenet_v2_1.0_192](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_192.tgz) | 221 | 3.47 | 70.7 | 90.1 | 55.1
[mobilenet_v2_1.0_160](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_160.tgz) | 154 | 3.47 | 68.8 | 89.0 | 40.2
[mobilenet_v2_1.0_128](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_128.tgz) | 99 | 3.47 | 65.3 | 86.9 | 27.6
[mobilenet_v2_1.0_96](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_96.tgz) | 56 | 3.47 | 60.3 | 83.2 | 17.6
[mobilenet_v2_0.75_224](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.75_224.tgz) | 209 | 2.61 | 69.8 | 89.6 | 55.8
[mobilenet_v2_0.75_192](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.75_192.tgz) | 153 | 2.61 | 68.7 | 88.9 | 41.6
[mobilenet_v2_0.75_160](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.75_160.tgz) | 107 | 2.61 | 66.4 | 87.3 | 30.4
[mobilenet_v2_0.75_128](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.75_128.tgz) | 69 | 2.61 | 63.2 | 85.3 | 21.9
[mobilenet_v2_0.75_96](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.75_96.tgz) | 39 | 2.61 | 58.8 | 81.6 | 14.2
[mobilenet_v2_0.5_224](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.5_224.tgz) | 97 | 1.95 | 65.4 | 86.4 | 28.7
[mobilenet_v2_0.5_192](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.5_192.tgz) | 71 | 1.95 | 63.9 | 85.4 | 21.1
[mobilenet_v2_0.5_160](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.5_160.tgz) | 50 | 1.95 | 61.0 | 83.2 | 14.9
[mobilenet_v2_0.5_128](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.5_128.tgz) | 32 | 1.95 | 57.7 | 80.8 | 9.9
[mobilenet_v2_0.5_96](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.5_96.tgz) | 18 | 1.95 | 51.2 | 75.8 | 6.4
[mobilenet_v2_0.35_224](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.35_224.tgz) | 59 | 1.66 | 60.3 | 82.9 | 19.7
[mobilenet_v2_0.35_192](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.35_192.tgz) | 43 | 1.66 | 58.2 | 81.2 | 14.6
[mobilenet_v2_0.35_160](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.35_160.tgz) | 30 | 1.66 | 55.7 | 79.1 | 10.5
[mobilenet_v2_0.35_128](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.35_128.tgz) | 20 | 1.66 | 50.8 | 75.0 | 6.9
[mobilenet_v2_0.35_96](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.35_96.tgz) | 11 | 1.66 | 45.5 | 70.4 | 4.5
Classification Checkpoint | Quantized | MACs (M) | Parameters (M) | Top 1 Accuracy | Top 5 Accuracy | Mobile CPU (ms) Pixel 1
------------------------------------------------------------------------------------------------------|-------------- | -------- | -------------- | -------------- | -------------- | -----------------------
[float_v2_1.4_224](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.4_224.tgz) | [uint8][quantized_v2_1.4_224] | 582 | 6.06 | 75.0 | 92.5 | 138.0
[float_v2_1.3_224](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.3_224.tgz) |[uint8][quantized_v2_1.3_224] | 509 | 5.34 | 74.4 | 92.1 | 123.0
[float_v2_1.0_224](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_224.tgz) |[uint8][quantized_v2_1.0_224] | 300 | 3.47 | 71.8 | 91.0 | 73.8
[float_v2_1.0_192](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_192.tgz) |[uint8][quantized_v2_1.0_192] | 221 | 3.47 | 70.7 | 90.1 | 55.1
[float_v2_1.0_160](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_160.tgz) | [uint8][quantized_v2_1.0_160] | 154 | 3.47 | 68.8 | 89.0 | 40.2
[float_v2_1.0_128](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_128.tgz) | [uint8][quantized_v2_1.0_128] | 99 | 3.47 | 65.3 | 86.9 | 27.6
[float_v2_1.0_96](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_96.tgz) | [uint8][quantized_v2_1.0_96] | 56 | 3.47 | 60.3 | 83.2 | 17.6
[float_v2_0.75_224](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.75_224.tgz)|[uint8][quantized_v2_0.75_224] | 209 | 2.61 | 69.8 | 89.6 | 55.8
[float_v2_0.75_192](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.75_192.tgz)|[uint8][quantized_v2_0.75_192] | 153 | 2.61 | 68.7 | 88.9 | 41.6
[float_v2_0.75_160](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.75_160.tgz)| [uint8][quantized_v2_0.75_160] | 107 | 2.61 | 66.4 | 87.3 | 30.4
[float_v2_0.75_128](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.75_128.tgz)| [uint8][quantized_v2_0.75_128] | 69 | 2.61 | 63.2 | 85.3 | 21.9
[float_v2_0.75_96](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.75_96.tgz) |[uint8][quantized_v2_0.75_96] | 39 | 2.61 | 58.8 | 81.6 | 14.2
[float_v2_0.5_224](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.5_224.tgz) |[uint8][quantized_v2_0.5_224] | 97 | 1.95 | 65.4 | 86.4 | 28.7
[float_v2_0.5_192](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.5_192.tgz) |[uint8][quantized_v2_0.5_192] | 71 | 1.95 | 63.9 | 85.4 | 21.1
[float_v2_0.5_160](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.5_160.tgz) |[uint8][quantized_v2_0.5_160] | 50 | 1.95 | 61.0 | 83.2 | 14.9
[float_v2_0.5_128](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.5_128.tgz) |[uint8][quantized_v2_0.5_128] | 32 | 1.95 | 57.7 | 80.8 | 9.9
[float_v2_0.5_96](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.5_96.tgz) |[uint8][quantized_v2_0.5_96] | 18 | 1.95 | 51.2 | 75.8 | 6.4
[float_v2_0.35_224](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.35_224.tgz)|[uint8][quantized_v2_0.35_224] | 59 | 1.66 | 60.3 | 82.9 | 19.7
[float_v2_0.35_192](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.35_192.tgz)| [uint8][quantized_v2_0.35_192] | 43 | 1.66 | 58.2 | 81.2 | 14.6
[float_v2_0.35_160](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.35_160.tgz)|[uint8][quantized_v2_0.35_160] | 30 | 1.66 | 55.7 | 79.1 | 10.5
[float_v2_0.35_128](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.35_128.tgz)|[uint8][quantized_v2_0.35_128] | 20 | 1.66 | 50.8 | 75.0 | 6.9
[float_v2_0.35_96](https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_0.35_96.tgz) |[uint8][quantized_v2_0.35_96] | 11 | 1.66 | 45.5 | 70.4 | 4.5
[quantized_v2_1.4_224]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_224_140.tgz
[quantized_v2_1.3_224]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_224_130.tgz
[quantized_v2_1.0_224]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_224_100.tgz
[quantized_v2_1.0_192]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_192_100.tgz
[quantized_v2_1.0_160]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_160_100.tgz
[quantized_v2_1.0_128]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_128_100.tgz
[quantized_v2_1.0_96]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_96_100.tgz
[quantized_v2_0.75_224]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_224_75.tgz
[quantized_v2_0.75_192]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_192_75.tgz
[quantized_v2_0.75_160]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_160_75.tgz
[quantized_v2_0.75_128]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_128_75.tgz
[quantized_v2_0.75_96]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_96_75.tgz
[quantized_v2_0.5_224]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_224_50.tgz
[quantized_v2_0.5_192]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_192_50.tgz
[quantized_v2_0.5_160]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_160_50.tgz
[quantized_v2_0.5_128]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_128_50.tgz
[quantized_v2_0.5_96]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_96_50.tgz
[quantized_v2_0.35_224]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_224_35.tgz
[quantized_v2_0.35_192]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_192_35.tgz
[quantized_v2_0.35_160]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_160_35.tgz
[quantized_v2_0.35_128]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_128_35.tgz
[quantized_v2_0.35_96]: https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_96_35.tgz
## Training
### V3
The following configuration, achieves 74.6% using 8 GPU setup and 75.2% using
2x2 TPU setup.
Final Top 1 Accuracy | 74.6 | |
----------------------------|------------------|-----------|
learning_rate | 0.16 |Total learning rate. (Per clone learning rate is 0.02) |
rmsprop_momentum | 0.9 | |
rmsprop_decay | 0.9 | |
rmsprop_epsilon | 0.002 | |
learning_rate_decay_factor | 0.99 | |
optimizer | RMSProp | |
warmup_epochs | 5 | Slim uses per clone epoch, so the the flag value is 0.6 |
num_epochs_per_decay | 3 | Slim uses per clone epoch, so the flag value is 0.375 |
batch_size (per chip) | 192 | |
moving_average_decay | 0.9999 | |
weight_decay | 1e-5 | |
init_stddev | 0.008 | |
dropout_keep_prob | 0.8 | |
bn_moving_average_decay | 0.997 | |
bn_epsilon | 0.001 | |
label_smoothing | 0.1 | |
TODO: Add V3 hyperparameters
### V2
......
......@@ -16,10 +16,8 @@
import contextlib
import functools
import tensorflow as tf
from tensorflow.contrib import slim as contrib_slim
slim = contrib_slim
import tensorflow.compat.v1 as tf
import tf_slim as slim
def _fixed_padding(inputs, kernel_size, rate=1):
......@@ -80,8 +78,8 @@ def _split_divisible(num, num_ways, divisible_by=8):
def _v1_compatible_scope_naming(scope):
"""v1 compatible scope naming."""
if scope is None: # Create uniqified separable blocks.
with tf.compat.v1.variable_scope(None, default_name='separable') as s, \
tf.compat.v1.name_scope(s.original_name_scope):
with tf.variable_scope(None, default_name='separable') as s, \
tf.name_scope(s.original_name_scope):
yield ''
else:
# We use scope_depthwise, scope_pointwise for compatibility with V1 ckpts.
......@@ -300,8 +298,8 @@ def expanded_conv(input_tensor,
if depthwise_activation_fn is not None:
dw_defaults['activation_fn'] = depthwise_activation_fn
# pylint: disable=g-backslash-continuation
with tf.compat.v1.variable_scope(scope, default_name='expanded_conv') as s, \
tf.compat.v1.name_scope(s.original_name_scope), \
with tf.variable_scope(scope, default_name='expanded_conv') as s, \
tf.name_scope(s.original_name_scope), \
slim.arg_scope((slim.conv2d,), **conv_defaults), \
slim.arg_scope((slim.separable_conv2d,), **dw_defaults):
prev_depth = input_tensor.get_shape().as_list()[3]
......@@ -434,7 +432,7 @@ def squeeze_excite(input_tensor,
Returns:
Gated input_tensor. (e.g. X * SE(X))
"""
with tf.compat.v1.variable_scope('squeeze_excite'):
with tf.variable_scope('squeeze_excite'):
if squeeze_input_tensor is None:
squeeze_input_tensor = input_tensor
input_size = input_tensor.shape.as_list()[1:3]
......
......@@ -22,10 +22,8 @@ import contextlib
import copy
import os
import tensorflow as tf
from tensorflow.contrib import slim as contrib_slim
slim = contrib_slim
import tensorflow.compat.v1 as tf
import tf_slim as slim
@slim.add_arg_scope
......@@ -306,8 +304,8 @@ def mobilenet_base( # pylint: disable=invalid-name
@contextlib.contextmanager
def _scope_all(scope, default_scope=None):
with tf.compat.v1.variable_scope(scope, default_name=default_scope) as s,\
tf.compat.v1.name_scope(s.original_name_scope):
with tf.variable_scope(scope, default_name=default_scope) as s,\
tf.name_scope(s.original_name_scope):
yield s
......@@ -318,6 +316,7 @@ def mobilenet(inputs,
reuse=None,
scope='Mobilenet',
base_only=False,
use_reduce_mean_for_pooling=False,
**mobilenet_args):
"""Mobilenet model for classification, supports both V1 and V2.
......@@ -337,6 +336,8 @@ def mobilenet(inputs,
scope: Optional variable_scope.
base_only: if True will only create the base of the network (no pooling
and no logits).
use_reduce_mean_for_pooling: if True use the reduce_mean for pooling. If
True use the global_pool function that provides some optimization.
**mobilenet_args: passed to mobilenet_base verbatim.
- conv_defs: list of conv defs
- multiplier: Float multiplier for the depth (number of channels)
......@@ -363,7 +364,7 @@ def mobilenet(inputs,
if len(input_shape) != 4:
raise ValueError('Expected rank 4 input, was: %d' % len(input_shape))
with tf.compat.v1.variable_scope(scope, 'Mobilenet', reuse=reuse) as scope:
with tf.variable_scope(scope, 'Mobilenet', reuse=reuse) as scope:
inputs = tf.identity(inputs, 'input')
net, end_points = mobilenet_base(inputs, scope=scope, **mobilenet_args)
if base_only:
......@@ -371,8 +372,8 @@ def mobilenet(inputs,
net = tf.identity(net, name='embedding')
with tf.compat.v1.variable_scope('Logits'):
net = global_pool(net)
with tf.variable_scope('Logits'):
net = global_pool(net, use_reduce_mean_for_pooling)
end_points['global_pool'] = net
if not num_classes:
return net, end_points
......@@ -384,7 +385,7 @@ def mobilenet(inputs,
num_classes, [1, 1],
activation_fn=None,
normalizer_fn=None,
biases_initializer=tf.compat.v1.zeros_initializer(),
biases_initializer=tf.zeros_initializer(),
scope='Conv2d_1c_1x1')
logits = tf.squeeze(logits, [1, 2])
......@@ -396,7 +397,9 @@ def mobilenet(inputs,
return logits, end_points
def global_pool(input_tensor, pool_op=tf.compat.v2.nn.avg_pool2d):
def global_pool(input_tensor,
use_reduce_mean_for_pooling=False,
pool_op=tf.nn.avg_pool2d):
"""Applies avg pool to produce 1x1 output.
NOTE: This function is funcitonally equivalenet to reduce_mean, but it has
......@@ -404,24 +407,29 @@ def global_pool(input_tensor, pool_op=tf.compat.v2.nn.avg_pool2d):
Args:
input_tensor: input tensor
use_reduce_mean_for_pooling: if True use reduce_mean for pooling
pool_op: pooling op (avg pool is default)
Returns:
a tensor batch_size x 1 x 1 x depth.
"""
shape = input_tensor.get_shape().as_list()
if shape[1] is None or shape[2] is None:
kernel_size = tf.convert_to_tensor(value=[
1,
tf.shape(input=input_tensor)[1],
tf.shape(input=input_tensor)[2], 1
])
if use_reduce_mean_for_pooling:
return tf.reduce_mean(
input_tensor, [1, 2], keepdims=True, name='ReduceMean')
else:
kernel_size = [1, shape[1], shape[2], 1]
output = pool_op(
input_tensor, ksize=kernel_size, strides=[1, 1, 1, 1], padding='VALID')
# Recover output shape, for unknown shape.
output.set_shape([None, 1, 1, None])
return output
shape = input_tensor.get_shape().as_list()
if shape[1] is None or shape[2] is None:
kernel_size = tf.convert_to_tensor(value=[
1,
tf.shape(input=input_tensor)[1],
tf.shape(input=input_tensor)[2], 1
])
else:
kernel_size = [1, shape[1], shape[2], 1]
output = pool_op(
input_tensor, ksize=kernel_size, strides=[1, 1, 1, 1], padding='VALID')
# Recover output shape, for unknown shape.
output.set_shape([None, 1, 1, None])
return output
def training_scope(is_training=True,
......@@ -432,7 +440,7 @@ def training_scope(is_training=True,
"""Defines Mobilenet training scope.
Usage:
with tf.contrib.slim.arg_scope(mobilenet.training_scope()):
with slim.arg_scope(mobilenet.training_scope()):
logits, endpoints = mobilenet_v2.mobilenet(input_tensor)
# the network created will be trainble with dropout/batch norm
......@@ -462,7 +470,7 @@ def training_scope(is_training=True,
if stddev < 0:
weight_intitializer = slim.initializers.xavier_initializer()
else:
weight_intitializer = tf.compat.v1.truncated_normal_initializer(
weight_intitializer = tf.truncated_normal_initializer(
stddev=stddev)
# Set weight_decay for weights in Conv and FC layers.
......
......@@ -227,7 +227,7 @@
},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import tensorflow.compat.v1 as tf\n",
"from nets.mobilenet import mobilenet_v2\n",
"\n",
"tf.reset_default_graph()\n",
......
......@@ -27,14 +27,12 @@ from __future__ import print_function
import copy
import functools
import tensorflow as tf
from tensorflow.contrib import layers as contrib_layers
from tensorflow.contrib import slim as contrib_slim
import tensorflow.compat.v1 as tf
import tf_slim as slim
from nets.mobilenet import conv_blocks as ops
from nets.mobilenet import mobilenet as lib
slim = contrib_slim
op = lib.op
expand_input = ops.expand_input_by_factor
......@@ -86,18 +84,17 @@ V2_DEF = dict(
# Mobilenet v2 Definition with group normalization.
V2_DEF_GROUP_NORM = copy.deepcopy(V2_DEF)
V2_DEF_GROUP_NORM['defaults'] = {
(contrib_slim.conv2d, contrib_slim.fully_connected,
contrib_slim.separable_conv2d): {
'normalizer_fn': contrib_layers.group_norm, # pylint: disable=C0330
(slim.conv2d, slim.fully_connected, slim.separable_conv2d): {
'normalizer_fn': slim.group_norm, # pylint: disable=C0330
'activation_fn': tf.nn.relu6, # pylint: disable=C0330
}, # pylint: disable=C0330
(ops.expanded_conv,): {
'expansion_size': ops.expand_input_by_factor(6),
'split_expansion': 1,
'normalizer_fn': contrib_layers.group_norm,
'normalizer_fn': slim.group_norm,
'residual': True
},
(contrib_slim.conv2d, contrib_slim.separable_conv2d): {
(slim.conv2d, slim.separable_conv2d): {
'padding': 'SAME'
}
}
......@@ -119,7 +116,7 @@ def mobilenet(input_tensor,
Inference mode is created by default. To create training use training_scope
below.
with tf.contrib.slim.arg_scope(mobilenet_v2.training_scope()):
with slim.arg_scope(mobilenet_v2.training_scope()):
logits, endpoints = mobilenet_v2.mobilenet(input_tensor)
Args:
......@@ -215,7 +212,7 @@ def mobilenet_base_group_norm(input_tensor, depth_multiplier=1.0, **kwargs):
"""Creates base of the mobilenet (no pooling and no logits) ."""
kwargs['conv_defs'] = V2_DEF_GROUP_NORM
kwargs['conv_defs']['defaults'].update({
(contrib_layers.group_norm,): {
(slim.group_norm,): {
'groups': kwargs.pop('groups', 8)
}
})
......@@ -227,11 +224,9 @@ def training_scope(**kwargs):
"""Defines MobilenetV2 training scope.
Usage:
with tf.contrib.slim.arg_scope(mobilenet_v2.training_scope()):
with slim.arg_scope(mobilenet_v2.training_scope()):
logits, endpoints = mobilenet_v2.mobilenet(input_tensor)
with slim.
Args:
**kwargs: Passed to mobilenet.training_scope. The following parameters
are supported:
......
......@@ -19,16 +19,13 @@ from __future__ import division
from __future__ import print_function
import copy
from six.moves import range
import tensorflow as tf
from tensorflow.contrib import slim as contrib_slim
import tensorflow.compat.v1 as tf
import tf_slim as slim
from nets.mobilenet import conv_blocks as ops
from nets.mobilenet import mobilenet
from nets.mobilenet import mobilenet_v2
slim = contrib_slim
def find_ops(optype):
"""Find ops of a given type in graphdef or a graph.
......@@ -37,19 +34,16 @@ def find_ops(optype):
Returns:
List of operations.
"""
gd = tf.compat.v1.get_default_graph()
gd = tf.get_default_graph()
return [var for var in gd.get_operations() if var.type == optype]
class MobilenetV2Test(tf.test.TestCase):
def setUp(self):
tf.compat.v1.reset_default_graph()
def testCreation(self):
spec = dict(mobilenet_v2.V2_DEF)
_, ep = mobilenet.mobilenet(
tf.compat.v1.placeholder(tf.float32, (10, 224, 224, 16)),
tf.placeholder(tf.float32, (10, 224, 224, 16)),
conv_defs=spec)
num_convs = len(find_ops('Conv2D'))
......@@ -66,7 +60,7 @@ class MobilenetV2Test(tf.test.TestCase):
def testCreationNoClasses(self):
spec = copy.deepcopy(mobilenet_v2.V2_DEF)
net, ep = mobilenet.mobilenet(
tf.compat.v1.placeholder(tf.float32, (10, 224, 224, 16)),
tf.placeholder(tf.float32, (10, 224, 224, 16)),
conv_defs=spec,
num_classes=None)
self.assertIs(net, ep['global_pool'])
......@@ -74,9 +68,9 @@ class MobilenetV2Test(tf.test.TestCase):
def testImageSizes(self):
for input_size, output_size in [(224, 7), (192, 6), (160, 5),
(128, 4), (96, 3)]:
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
_, ep = mobilenet_v2.mobilenet(
tf.compat.v1.placeholder(tf.float32, (10, input_size, input_size, 3)))
tf.placeholder(tf.float32, (10, input_size, input_size, 3)))
self.assertEqual(ep['layer_18/output'].get_shape().as_list()[1:3],
[output_size] * 2)
......@@ -87,7 +81,7 @@ class MobilenetV2Test(tf.test.TestCase):
(ops.expanded_conv,): dict(split_expansion=2),
}
_, _ = mobilenet.mobilenet(
tf.compat.v1.placeholder(tf.float32, (10, 224, 224, 16)),
tf.placeholder(tf.float32, (10, 224, 224, 16)),
conv_defs=spec)
num_convs = len(find_ops('Conv2D'))
# All but 3 op has 3 conv operatore, the remainign 3 have one
......@@ -96,16 +90,16 @@ class MobilenetV2Test(tf.test.TestCase):
def testWithOutputStride8(self):
out, _ = mobilenet.mobilenet_base(
tf.compat.v1.placeholder(tf.float32, (10, 224, 224, 16)),
tf.placeholder(tf.float32, (10, 224, 224, 16)),
conv_defs=mobilenet_v2.V2_DEF,
output_stride=8,
scope='MobilenetV2')
self.assertEqual(out.get_shape().as_list()[1:3], [28, 28])
def testDivisibleBy(self):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
mobilenet_v2.mobilenet(
tf.compat.v1.placeholder(tf.float32, (10, 224, 224, 16)),
tf.placeholder(tf.float32, (10, 224, 224, 16)),
conv_defs=mobilenet_v2.V2_DEF,
divisible_by=16,
min_depth=32)
......@@ -115,12 +109,12 @@ class MobilenetV2Test(tf.test.TestCase):
1001], s)
def testDivisibleByWithArgScope(self):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
# Verifies that depth_multiplier arg scope actually works
# if no default min_depth is provided.
with slim.arg_scope((mobilenet.depth_multiplier,), min_depth=32):
mobilenet_v2.mobilenet(
tf.compat.v1.placeholder(tf.float32, (10, 224, 224, 2)),
tf.placeholder(tf.float32, (10, 224, 224, 2)),
conv_defs=mobilenet_v2.V2_DEF,
depth_multiplier=0.1)
s = [op.outputs[0].get_shape().as_list()[-1] for op in find_ops('Conv2D')]
......@@ -128,12 +122,12 @@ class MobilenetV2Test(tf.test.TestCase):
self.assertSameElements(s, [32, 192, 128, 1001])
def testFineGrained(self):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
# Verifies that depth_multiplier arg scope actually works
# if no default min_depth is provided.
mobilenet_v2.mobilenet(
tf.compat.v1.placeholder(tf.float32, (10, 224, 224, 2)),
tf.placeholder(tf.float32, (10, 224, 224, 2)),
conv_defs=mobilenet_v2.V2_DEF,
depth_multiplier=0.01,
finegrain_classification_mode=True)
......@@ -143,19 +137,19 @@ class MobilenetV2Test(tf.test.TestCase):
self.assertSameElements(s, [8, 48, 1001, 1280])
def testMobilenetBase(self):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
# Verifies that mobilenet_base returns pre-pooling layer.
with slim.arg_scope((mobilenet.depth_multiplier,), min_depth=32):
net, _ = mobilenet_v2.mobilenet_base(
tf.compat.v1.placeholder(tf.float32, (10, 224, 224, 16)),
tf.placeholder(tf.float32, (10, 224, 224, 16)),
conv_defs=mobilenet_v2.V2_DEF,
depth_multiplier=0.1)
self.assertEqual(net.get_shape().as_list(), [10, 7, 7, 128])
def testWithOutputStride16(self):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
out, _ = mobilenet.mobilenet_base(
tf.compat.v1.placeholder(tf.float32, (10, 224, 224, 16)),
tf.placeholder(tf.float32, (10, 224, 224, 16)),
conv_defs=mobilenet_v2.V2_DEF,
output_stride=16)
self.assertEqual(out.get_shape().as_list()[1:3], [14, 14])
......@@ -174,7 +168,7 @@ class MobilenetV2Test(tf.test.TestCase):
multiplier_func=inverse_multiplier,
num_outputs=16)
_ = mobilenet_v2.mobilenet_base(
tf.compat.v1.placeholder(tf.float32, (10, 224, 224, 16)),
tf.placeholder(tf.float32, (10, 224, 224, 16)),
conv_defs=new_def,
depth_multiplier=0.1)
s = [op.outputs[0].get_shape().as_list()[-1] for op in find_ops('Conv2D')]
......@@ -183,9 +177,9 @@ class MobilenetV2Test(tf.test.TestCase):
self.assertEqual([160, 8, 48, 8, 48], s[:5])
def testWithOutputStride8AndExplicitPadding(self):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
out, _ = mobilenet.mobilenet_base(
tf.compat.v1.placeholder(tf.float32, (10, 224, 224, 16)),
tf.placeholder(tf.float32, (10, 224, 224, 16)),
conv_defs=mobilenet_v2.V2_DEF,
output_stride=8,
use_explicit_padding=True,
......@@ -193,9 +187,9 @@ class MobilenetV2Test(tf.test.TestCase):
self.assertEqual(out.get_shape().as_list()[1:3], [28, 28])
def testWithOutputStride16AndExplicitPadding(self):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
out, _ = mobilenet.mobilenet_base(
tf.compat.v1.placeholder(tf.float32, (10, 224, 224, 16)),
tf.placeholder(tf.float32, (10, 224, 224, 16)),
conv_defs=mobilenet_v2.V2_DEF,
output_stride=16,
use_explicit_padding=True)
......
......@@ -12,7 +12,341 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Mobilenet V3 conv defs and helper functions."""
"""Mobilenet V3 conv defs and helper functions.
# pylint: disable=line-too-long
Model definitions and layer breakdowns:
==================
==== V3 LARGE ====
==================
Conv2D MobilenetV3/Conv/Conv2D 351.2 k 1x224x224x3 432.0 5.42 M 1x112x112x16
Relu6 MobilenetV3/Conv/hard_swish/Relu6 ? - ? ? 1x112x112x16
DepthConv MobilenetV3/expanded_conv/depthwise/depthwise 401.4 k - 144.0 1.81 M 1x112x112x16
Relu MobilenetV3/expanded_conv/depthwise/Relu ? - ? ? 1x112x112x16
Conv2D MobilenetV3/expanded_conv/project/Conv2D 401.4 k 1x112x112x16 256.0 3.21 M 1x112x112x16
Conv2D MobilenetV3/expanded_conv_1/expand/Conv2D 1.00 M 1x112x112x16 1.02 k 12.8 M 1x112x112x64
Relu MobilenetV3/expanded_conv_1/expand/Relu ? - ? ? 1x112x112x64
DepthConv MobilenetV3/expanded_conv_1/depthwise/depthwise 1.00 M - 576.0 1.81 M 1x56x56x64
Relu MobilenetV3/expanded_conv_1/depthwise/Relu ? - ? ? 1x56x56x64
Conv2D MobilenetV3/expanded_conv_1/project/Conv2D 276.0 k 1x56x56x64 1.54 k 4.82 M 1x56x56x24
Conv2D MobilenetV3/expanded_conv_2/expand/Conv2D 301.1 k 1x56x56x24 1.73 k 5.42 M 1x56x56x72
Relu MobilenetV3/expanded_conv_2/expand/Relu ? - ? ? 1x56x56x72
DepthConv MobilenetV3/expanded_conv_2/depthwise/depthwise 451.6 k - 648.0 2.03 M 1x56x56x72
Relu MobilenetV3/expanded_conv_2/depthwise/Relu ? - ? ? 1x56x56x72
Conv2D MobilenetV3/expanded_conv_2/project/Conv2D 301.1 k 1x56x56x72 1.73 k 5.42 M 1x56x56x24
Conv2D MobilenetV3/expanded_conv_3/expand/Conv2D 301.1 k 1x56x56x24 1.73 k 5.42 M 1x56x56x72
Relu MobilenetV3/expanded_conv_3/expand/Relu ? - ? ? 1x56x56x72
DepthConv MobilenetV3/expanded_conv_3/depthwise/depthwise 282.2 k - 1.80 k 1.41 M 1x28x28x72
Relu MobilenetV3/expanded_conv_3/depthwise/Relu ? - ? ? 1x28x28x72
Conv2D MobilenetV3/expanded_conv_3/squeeze_excite/Conv/Conv2D 96.0 1x1x1x72 1.73 k 1.73 k 1x1x1x24
Relu MobilenetV3/expanded_conv_3/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x24
Conv2D MobilenetV3/expanded_conv_3/squeeze_excite/Conv_1/Conv2D 96.0 1x1x1x24 1.73 k 1.73 k 1x1x1x72
Relu6 MobilenetV3/expanded_conv_3/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x72
Conv2D MobilenetV3/expanded_conv_3/project/Conv2D 87.8 k 1x28x28x72 2.88 k 2.26 M 1x28x28x40
Conv2D MobilenetV3/expanded_conv_4/expand/Conv2D 125.4 k 1x28x28x40 4.80 k 3.76 M 1x28x28x120
Relu MobilenetV3/expanded_conv_4/expand/Relu ? - ? ? 1x28x28x120
DepthConv MobilenetV3/expanded_conv_4/depthwise/depthwise 188.2 k - 3.00 k 2.35 M 1x28x28x120
Relu MobilenetV3/expanded_conv_4/depthwise/Relu ? - ? ? 1x28x28x120
Conv2D MobilenetV3/expanded_conv_4/squeeze_excite/Conv/Conv2D 152.0 1x1x1x120 3.84 k 3.84 k 1x1x1x32
Relu MobilenetV3/expanded_conv_4/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x32
Conv2D MobilenetV3/expanded_conv_4/squeeze_excite/Conv_1/Conv2D 152.0 1x1x1x32 3.84 k 3.84 k 1x1x1x120
Relu6 MobilenetV3/expanded_conv_4/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x120
Conv2D MobilenetV3/expanded_conv_4/project/Conv2D 125.4 k 1x28x28x120 4.80 k 3.76 M 1x28x28x40
Conv2D MobilenetV3/expanded_conv_5/expand/Conv2D 125.4 k 1x28x28x40 4.80 k 3.76 M 1x28x28x120
Relu MobilenetV3/expanded_conv_5/expand/Relu ? - ? ? 1x28x28x120
DepthConv MobilenetV3/expanded_conv_5/depthwise/depthwise 188.2 k - 3.00 k 2.35 M 1x28x28x120
Relu MobilenetV3/expanded_conv_5/depthwise/Relu ? - ? ? 1x28x28x120
Conv2D MobilenetV3/expanded_conv_5/squeeze_excite/Conv/Conv2D 152.0 1x1x1x120 3.84 k 3.84 k 1x1x1x32
Relu MobilenetV3/expanded_conv_5/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x32
Conv2D MobilenetV3/expanded_conv_5/squeeze_excite/Conv_1/Conv2D 152.0 1x1x1x32 3.84 k 3.84 k 1x1x1x120
Relu6 MobilenetV3/expanded_conv_5/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x120
Conv2D MobilenetV3/expanded_conv_5/project/Conv2D 125.4 k 1x28x28x120 4.80 k 3.76 M 1x28x28x40
Conv2D MobilenetV3/expanded_conv_6/expand/Conv2D 219.5 k 1x28x28x40 9.60 k 7.53 M 1x28x28x240
Relu6 MobilenetV3/expanded_conv_6/expand/hard_swish/Relu6 ? - ? ? 1x28x28x240
DepthConv MobilenetV3/expanded_conv_6/depthwise/depthwise 235.2 k - 2.16 k 423.4 k 1x14x14x240
Relu6 MobilenetV3/expanded_conv_6/depthwise/hard_swish/Relu6 ? - ? ? 1x14x14x240
Conv2D MobilenetV3/expanded_conv_6/project/Conv2D 62.7 k 1x14x14x240 19.2 k 3.76 M 1x14x14x80
Conv2D MobilenetV3/expanded_conv_7/expand/Conv2D 54.9 k 1x14x14x80 16.0 k 3.14 M 1x14x14x200
Relu6 MobilenetV3/expanded_conv_7/expand/hard_swish/Relu6 ? - ? ? 1x14x14x200
DepthConv MobilenetV3/expanded_conv_7/depthwise/depthwise 78.4 k - 1.80 k 352.8 k 1x14x14x200
Relu6 MobilenetV3/expanded_conv_7/depthwise/hard_swish/Relu6 ? - ? ? 1x14x14x200
Conv2D MobilenetV3/expanded_conv_7/project/Conv2D 54.9 k 1x14x14x200 16.0 k 3.14 M 1x14x14x80
Conv2D MobilenetV3/expanded_conv_8/expand/Conv2D 51.7 k 1x14x14x80 14.7 k 2.89 M 1x14x14x184
Relu6 MobilenetV3/expanded_conv_8/expand/hard_swish/Relu6 ? - ? ? 1x14x14x184
DepthConv MobilenetV3/expanded_conv_8/depthwise/depthwise 72.1 k - 1.66 k 324.6 k 1x14x14x184
Relu6 MobilenetV3/expanded_conv_8/depthwise/hard_swish/Relu6 ? - ? ? 1x14x14x184
Conv2D MobilenetV3/expanded_conv_8/project/Conv2D 51.7 k 1x14x14x184 14.7 k 2.89 M 1x14x14x80
Conv2D MobilenetV3/expanded_conv_9/expand/Conv2D 51.7 k 1x14x14x80 14.7 k 2.89 M 1x14x14x184
Relu6 MobilenetV3/expanded_conv_9/expand/hard_swish/Relu6 ? - ? ? 1x14x14x184
DepthConv MobilenetV3/expanded_conv_9/depthwise/depthwise 72.1 k - 1.66 k 324.6 k 1x14x14x184
Relu6 MobilenetV3/expanded_conv_9/depthwise/hard_swish/Relu6 ? - ? ? 1x14x14x184
Conv2D MobilenetV3/expanded_conv_9/project/Conv2D 51.7 k 1x14x14x184 14.7 k 2.89 M 1x14x14x80
Conv2D MobilenetV3/expanded_conv_10/expand/Conv2D 109.8 k 1x14x14x80 38.4 k 7.53 M 1x14x14x480
Relu6 MobilenetV3/expanded_conv_10/expand/hard_swish/Relu6 ? - ? ? 1x14x14x480
DepthConv MobilenetV3/expanded_conv_10/depthwise/depthwise 188.2 k - 4.32 k 846.7 k 1x14x14x480
Relu6 MobilenetV3/expanded_conv_10/depthwise/hard_swish/Relu6 ? - ? ? 1x14x14x480
Conv2D MobilenetV3/expanded_conv_10/squeeze_excite/Conv/Conv2D 600.0 1x1x1x480 57.6 k 57.6 k 1x1x1x120
Relu MobilenetV3/expanded_conv_10/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x120
Conv2D MobilenetV3/expanded_conv_10/squeeze_excite/Conv_1/Conv2D 600.0 1x1x1x120 57.6 k 57.6 k 1x1x1x480
Relu6 MobilenetV3/expanded_conv_10/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x480
Conv2D MobilenetV3/expanded_conv_10/project/Conv2D 116.0 k 1x14x14x480 53.8 k 10.5 M 1x14x14x112
Conv2D MobilenetV3/expanded_conv_11/expand/Conv2D 153.7 k 1x14x14x112 75.3 k 14.8 M 1x14x14x672
Relu6 MobilenetV3/expanded_conv_11/expand/hard_swish/Relu6 ? - ? ? 1x14x14x672
DepthConv MobilenetV3/expanded_conv_11/depthwise/depthwise 263.4 k - 6.05 k 1.19 M 1x14x14x672
Relu6 MobilenetV3/expanded_conv_11/depthwise/hard_swish/Relu6 ? - ? ? 1x14x14x672
Conv2D MobilenetV3/expanded_conv_11/squeeze_excite/Conv/Conv2D 840.0 1x1x1x672 112.9 k 112.9 k 1x1x1x168
Relu MobilenetV3/expanded_conv_11/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x168
Conv2D MobilenetV3/expanded_conv_11/squeeze_excite/Conv_1/Conv2D 840.0 1x1x1x168 112.9 k 112.9 k 1x1x1x672
Relu6 MobilenetV3/expanded_conv_11/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x672
Conv2D MobilenetV3/expanded_conv_11/project/Conv2D 153.7 k 1x14x14x672 75.3 k 14.8 M 1x14x14x112
Conv2D MobilenetV3/expanded_conv_12/expand/Conv2D 153.7 k 1x14x14x112 75.3 k 14.8 M 1x14x14x672
Relu6 MobilenetV3/expanded_conv_12/expand/hard_swish/Relu6 ? - ? ? 1x14x14x672
DepthConv MobilenetV3/expanded_conv_12/depthwise/depthwise 164.6 k - 16.8 k 823.2 k 1x7x7x672
Relu6 MobilenetV3/expanded_conv_12/depthwise/hard_swish/Relu6 ? - ? ? 1x7x7x672
Conv2D MobilenetV3/expanded_conv_12/squeeze_excite/Conv/Conv2D 840.0 1x1x1x672 112.9 k 112.9 k 1x1x1x168
Relu MobilenetV3/expanded_conv_12/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x168
Conv2D MobilenetV3/expanded_conv_12/squeeze_excite/Conv_1/Conv2D 840.0 1x1x1x168 112.9 k 112.9 k 1x1x1x672
Relu6 MobilenetV3/expanded_conv_12/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x672
Conv2D MobilenetV3/expanded_conv_12/project/Conv2D 40.8 k 1x7x7x672 107.5 k 5.27 M 1x7x7x160
Conv2D MobilenetV3/expanded_conv_13/expand/Conv2D 54.9 k 1x7x7x160 153.6 k 7.53 M 1x7x7x960
Relu6 MobilenetV3/expanded_conv_13/expand/hard_swish/Relu6 ? - ? ? 1x7x7x960
DepthConv MobilenetV3/expanded_conv_13/depthwise/depthwise 94.1 k - 24.0 k 1.18 M 1x7x7x960
Relu6 MobilenetV3/expanded_conv_13/depthwise/hard_swish/Relu6 ? - ? ? 1x7x7x960
Conv2D MobilenetV3/expanded_conv_13/squeeze_excite/Conv/Conv2D 1.20 k 1x1x1x960 230.4 k 230.4 k 1x1x1x240
Relu MobilenetV3/expanded_conv_13/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x240
Conv2D MobilenetV3/expanded_conv_13/squeeze_excite/Conv_1/Conv2D 1.20 k 1x1x1x240 230.4 k 230.4 k 1x1x1x960
Relu6 MobilenetV3/expanded_conv_13/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x960
Conv2D MobilenetV3/expanded_conv_13/project/Conv2D 54.9 k 1x7x7x960 153.6 k 7.53 M 1x7x7x160
Conv2D MobilenetV3/expanded_conv_14/expand/Conv2D 54.9 k 1x7x7x160 153.6 k 7.53 M 1x7x7x960
Relu6 MobilenetV3/expanded_conv_14/expand/hard_swish/Relu6 ? - ? ? 1x7x7x960
DepthConv MobilenetV3/expanded_conv_14/depthwise/depthwise 94.1 k - 24.0 k 1.18 M 1x7x7x960
Relu6 MobilenetV3/expanded_conv_14/depthwise/hard_swish/Relu6 ? - ? ? 1x7x7x960
Conv2D MobilenetV3/expanded_conv_14/squeeze_excite/Conv/Conv2D 1.20 k 1x1x1x960 230.4 k 230.4 k 1x1x1x240
Relu MobilenetV3/expanded_conv_14/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x240
Conv2D MobilenetV3/expanded_conv_14/squeeze_excite/Conv_1/Conv2D 1.20 k 1x1x1x240 230.4 k 230.4 k 1x1x1x960
Relu6 MobilenetV3/expanded_conv_14/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x960
Conv2D MobilenetV3/expanded_conv_14/project/Conv2D 54.9 k 1x7x7x960 153.6 k 7.53 M 1x7x7x160
Conv2D MobilenetV3/Conv_1/Conv2D 54.9 k 1x7x7x160 153.6 k 7.53 M 1x7x7x960
Relu6 MobilenetV3/Conv_1/hard_swish/Relu6 ? - ? ? 1x7x7x960
AvgPool MobilenetV3/AvgPool2D/AvgPool ? 1x7x7x960 ? 47.0 k 1x1x1x960
Conv2D MobilenetV3/Conv_2/Conv2D 2.24 k 1x1x1x960 1.23 M 1.23 M 1x1x1x1280
Relu6 MobilenetV3/Conv_2/hard_swish/Relu6 ? - ? ? 1x1x1x1280
Conv2D MobilenetV3/Logits/Conv2d_1c_1x1/Conv2D 2.28 k 1x1x1x1280 1.28 M 1.28 M 1x1x1x1001
-----
==================
==== V3 SMALL ====
==================
op name ActMem ConvInput ConvParameters Madds OutputTensor
Conv2D MobilenetV3/Conv/Conv2D 351.2 k 1x224x224x3 432.0 5.42 M 1x112x112x16
Relu6 MobilenetV3/Conv/hard_swish/Relu6 ? - ? ? 1x112x112x16
DepthConv MobilenetV3/expanded_conv/depthwise/depthwise 250.9 k - 144.0 451.6 k 1x56x56x16
Relu MobilenetV3/expanded_conv/depthwise/Relu ? - ? ? 1x56x56x16
Conv2D MobilenetV3/expanded_conv/squeeze_excite/Conv/Conv2D 24.0 1x1x1x16 128.0 128.0 1x1x1x8
Relu MobilenetV3/expanded_conv/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x8
Conv2D MobilenetV3/expanded_conv/squeeze_excite/Conv_1/Conv2D 24.0 1x1x1x8 128.0 128.0 1x1x1x16
Relu6 MobilenetV3/expanded_conv/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x16
Conv2D MobilenetV3/expanded_conv/project/Conv2D 100.4 k 1x56x56x16 256.0 802.8 k 1x56x56x16
Conv2D MobilenetV3/expanded_conv_1/expand/Conv2D 276.0 k 1x56x56x16 1.15 k 3.61 M 1x56x56x72
Relu MobilenetV3/expanded_conv_1/expand/Relu ? - ? ? 1x56x56x72
DepthConv MobilenetV3/expanded_conv_1/depthwise/depthwise 282.2 k - 648.0 508.0 k 1x28x28x72
Relu MobilenetV3/expanded_conv_1/depthwise/Relu ? - ? ? 1x28x28x72
Conv2D MobilenetV3/expanded_conv_1/project/Conv2D 75.3 k 1x28x28x72 1.73 k 1.35 M 1x28x28x24
Conv2D MobilenetV3/expanded_conv_2/expand/Conv2D 87.8 k 1x28x28x24 2.11 k 1.66 M 1x28x28x88
Relu MobilenetV3/expanded_conv_2/expand/Relu ? - ? ? 1x28x28x88
DepthConv MobilenetV3/expanded_conv_2/depthwise/depthwise 138.0 k - 792.0 620.9 k 1x28x28x88
Relu MobilenetV3/expanded_conv_2/depthwise/Relu ? - ? ? 1x28x28x88
Conv2D MobilenetV3/expanded_conv_2/project/Conv2D 87.8 k 1x28x28x88 2.11 k 1.66 M 1x28x28x24
Conv2D MobilenetV3/expanded_conv_3/expand/Conv2D 94.1 k 1x28x28x24 2.30 k 1.81 M 1x28x28x96
Relu6 MobilenetV3/expanded_conv_3/expand/hard_swish/Relu6 ? - ? ? 1x28x28x96
DepthConv MobilenetV3/expanded_conv_3/depthwise/depthwise 94.1 k - 2.40 k 470.4 k 1x14x14x96
Relu6 MobilenetV3/expanded_conv_3/depthwise/hard_swish/Relu6 ? - ? ? 1x14x14x96
Conv2D MobilenetV3/expanded_conv_3/squeeze_excite/Conv/Conv2D 120.0 1x1x1x96 2.30 k 2.30 k 1x1x1x24
Relu MobilenetV3/expanded_conv_3/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x24
Conv2D MobilenetV3/expanded_conv_3/squeeze_excite/Conv_1/Conv2D 120.0 1x1x1x24 2.30 k 2.30 k 1x1x1x96
Relu6 MobilenetV3/expanded_conv_3/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x96
Conv2D MobilenetV3/expanded_conv_3/project/Conv2D 26.7 k 1x14x14x96 3.84 k 752.6 k 1x14x14x40
Conv2D MobilenetV3/expanded_conv_4/expand/Conv2D 54.9 k 1x14x14x40 9.60 k 1.88 M 1x14x14x240
Relu6 MobilenetV3/expanded_conv_4/expand/hard_swish/Relu6 ? - ? ? 1x14x14x240
DepthConv MobilenetV3/expanded_conv_4/depthwise/depthwise 94.1 k - 6.00 k 1.18 M 1x14x14x240
Relu6 MobilenetV3/expanded_conv_4/depthwise/hard_swish/Relu6 ? - ? ? 1x14x14x240
Conv2D MobilenetV3/expanded_conv_4/squeeze_excite/Conv/Conv2D 304.0 1x1x1x240 15.4 k 15.4 k 1x1x1x64
Relu MobilenetV3/expanded_conv_4/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x64
Conv2D MobilenetV3/expanded_conv_4/squeeze_excite/Conv_1/Conv2D 304.0 1x1x1x64 15.4 k 15.4 k 1x1x1x240
Relu6 MobilenetV3/expanded_conv_4/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x240
Conv2D MobilenetV3/expanded_conv_4/project/Conv2D 54.9 k 1x14x14x240 9.60 k 1.88 M 1x14x14x40
Conv2D MobilenetV3/expanded_conv_5/expand/Conv2D 54.9 k 1x14x14x40 9.60 k 1.88 M 1x14x14x240
Relu6 MobilenetV3/expanded_conv_5/expand/hard_swish/Relu6 ? - ? ? 1x14x14x240
DepthConv MobilenetV3/expanded_conv_5/depthwise/depthwise 94.1 k - 6.00 k 1.18 M 1x14x14x240
Relu6 MobilenetV3/expanded_conv_5/depthwise/hard_swish/Relu6 ? - ? ? 1x14x14x240
Conv2D MobilenetV3/expanded_conv_5/squeeze_excite/Conv/Conv2D 304.0 1x1x1x240 15.4 k 15.4 k 1x1x1x64
Relu MobilenetV3/expanded_conv_5/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x64
Conv2D MobilenetV3/expanded_conv_5/squeeze_excite/Conv_1/Conv2D 304.0 1x1x1x64 15.4 k 15.4 k 1x1x1x240
Relu6 MobilenetV3/expanded_conv_5/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x240
Conv2D MobilenetV3/expanded_conv_5/project/Conv2D 54.9 k 1x14x14x240 9.60 k 1.88 M 1x14x14x40
Conv2D MobilenetV3/expanded_conv_6/expand/Conv2D 31.4 k 1x14x14x40 4.80 k 940.8 k 1x14x14x120
Relu6 MobilenetV3/expanded_conv_6/expand/hard_swish/Relu6 ? - ? ? 1x14x14x120
DepthConv MobilenetV3/expanded_conv_6/depthwise/depthwise 47.0 k - 3.00 k 588.0 k 1x14x14x120
Relu6 MobilenetV3/expanded_conv_6/depthwise/hard_swish/Relu6 ? - ? ? 1x14x14x120
Conv2D MobilenetV3/expanded_conv_6/squeeze_excite/Conv/Conv2D 152.0 1x1x1x120 3.84 k 3.84 k 1x1x1x32
Relu MobilenetV3/expanded_conv_6/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x32
Conv2D MobilenetV3/expanded_conv_6/squeeze_excite/Conv_1/Conv2D 152.0 1x1x1x32 3.84 k 3.84 k 1x1x1x120
Relu6 MobilenetV3/expanded_conv_6/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x120
Conv2D MobilenetV3/expanded_conv_6/project/Conv2D 32.9 k 1x14x14x120 5.76 k 1.13 M 1x14x14x48
Conv2D MobilenetV3/expanded_conv_7/expand/Conv2D 37.6 k 1x14x14x48 6.91 k 1.35 M 1x14x14x144
Relu6 MobilenetV3/expanded_conv_7/expand/hard_swish/Relu6 ? - ? ? 1x14x14x144
DepthConv MobilenetV3/expanded_conv_7/depthwise/depthwise 56.4 k - 3.60 k 705.6 k 1x14x14x144
Relu6 MobilenetV3/expanded_conv_7/depthwise/hard_swish/Relu6 ? - ? ? 1x14x14x144
Conv2D MobilenetV3/expanded_conv_7/squeeze_excite/Conv/Conv2D 184.0 1x1x1x144 5.76 k 5.76 k 1x1x1x40
Relu MobilenetV3/expanded_conv_7/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x40
Conv2D MobilenetV3/expanded_conv_7/squeeze_excite/Conv_1/Conv2D 184.0 1x1x1x40 5.76 k 5.76 k 1x1x1x144
Relu6 MobilenetV3/expanded_conv_7/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x144
Conv2D MobilenetV3/expanded_conv_7/project/Conv2D 37.6 k 1x14x14x144 6.91 k 1.35 M 1x14x14x48
Conv2D MobilenetV3/expanded_conv_8/expand/Conv2D 65.9 k 1x14x14x48 13.8 k 2.71 M 1x14x14x288
Relu6 MobilenetV3/expanded_conv_8/expand/hard_swish/Relu6 ? - ? ? 1x14x14x288
DepthConv MobilenetV3/expanded_conv_8/depthwise/depthwise 70.6 k - 7.20 k 352.8 k 1x7x7x288
Relu6 MobilenetV3/expanded_conv_8/depthwise/hard_swish/Relu6 ? - ? ? 1x7x7x288
Conv2D MobilenetV3/expanded_conv_8/squeeze_excite/Conv/Conv2D 360.0 1x1x1x288 20.7 k 20.7 k 1x1x1x72
Relu MobilenetV3/expanded_conv_8/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x72
Conv2D MobilenetV3/expanded_conv_8/squeeze_excite/Conv_1/Conv2D 360.0 1x1x1x72 20.7 k 20.7 k 1x1x1x288
Relu6 MobilenetV3/expanded_conv_8/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x288
Conv2D MobilenetV3/expanded_conv_8/project/Conv2D 18.8 k 1x7x7x288 27.6 k 1.35 M 1x7x7x96
Conv2D MobilenetV3/expanded_conv_9/expand/Conv2D 32.9 k 1x7x7x96 55.3 k 2.71 M 1x7x7x576
Relu6 MobilenetV3/expanded_conv_9/expand/hard_swish/Relu6 ? - ? ? 1x7x7x576
DepthConv MobilenetV3/expanded_conv_9/depthwise/depthwise 56.4 k - 14.4 k 705.6 k 1x7x7x576
Relu6 MobilenetV3/expanded_conv_9/depthwise/hard_swish/Relu6 ? - ? ? 1x7x7x576
Conv2D MobilenetV3/expanded_conv_9/squeeze_excite/Conv/Conv2D 720.0 1x1x1x576 82.9 k 82.9 k 1x1x1x144
Relu MobilenetV3/expanded_conv_9/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x144
Conv2D MobilenetV3/expanded_conv_9/squeeze_excite/Conv_1/Conv2D 720.0 1x1x1x144 82.9 k 82.9 k 1x1x1x576
Relu6 MobilenetV3/expanded_conv_9/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x576
Conv2D MobilenetV3/expanded_conv_9/project/Conv2D 32.9 k 1x7x7x576 55.3 k 2.71 M 1x7x7x96
Conv2D MobilenetV3/expanded_conv_10/expand/Conv2D 32.9 k 1x7x7x96 55.3 k 2.71 M 1x7x7x576
Relu6 MobilenetV3/expanded_conv_10/expand/hard_swish/Relu6 ? - ? ? 1x7x7x576
DepthConv MobilenetV3/expanded_conv_10/depthwise/depthwise 56.4 k - 14.4 k 705.6 k 1x7x7x576
Relu6 MobilenetV3/expanded_conv_10/depthwise/hard_swish/Relu6 ? - ? ? 1x7x7x576
Conv2D MobilenetV3/expanded_conv_10/squeeze_excite/Conv/Conv2D 720.0 1x1x1x576 82.9 k 82.9 k 1x1x1x144
Relu MobilenetV3/expanded_conv_10/squeeze_excite/Conv/Relu ? - ? ? 1x1x1x144
Conv2D MobilenetV3/expanded_conv_10/squeeze_excite/Conv_1/Conv2D 720.0 1x1x1x144 82.9 k 82.9 k 1x1x1x576
Relu6 MobilenetV3/expanded_conv_10/squeeze_excite/Conv_1/Relu6 ? - ? ? 1x1x1x576
Conv2D MobilenetV3/expanded_conv_10/project/Conv2D 32.9 k 1x7x7x576 55.3 k 2.71 M 1x7x7x96
Conv2D MobilenetV3/Conv_1/Conv2D 32.9 k 1x7x7x96 55.3 k 2.71 M 1x7x7x576
Relu6 MobilenetV3/Conv_1/hard_swish/Relu6 ? - ? ? 1x7x7x576
AvgPool MobilenetV3/AvgPool2D/AvgPool ? 1x7x7x576 ? 28.2 k 1x1x1x576
Conv2D MobilenetV3/Conv_2/Conv2D 1.60 k 1x1x1x576 589.8 k 589.8 k 1x1x1x1024
Relu6 MobilenetV3/Conv_2/hard_swish/Relu6 ? - ? ? 1x1x1x1024
Conv2D MobilenetV3/Logits/Conv2d_1c_1x1/Conv2D 2.02 k 1x1x1x1024 1.03 M 1.03 M 1x1x1x1001
-----
Total Total 2.96 M - 2.53 M 56.5 M -
====================
==== V3 EDGETPU ====
====================
op name ActMem ConvInput ConvParameters Madds OutputTensor
Conv2D MobilenetEdgeTPU/Conv/Conv2D 551.9 k 1x224x224x3 864.0 10.8 M 1x112x112x32
Relu MobilenetEdgeTPU/Conv/Relu ? - ? ? 1x112x112x32
Conv2D MobilenetEdgeTPU/expanded_conv/project/Conv2D 602.1 k 1x112x112x32 512.0 6.42 M 1x112x112x16
Conv2D MobilenetEdgeTPU/expanded_conv_1/expand/Conv2D 602.1 k 1x112x112x16 18.4 k 57.8 M 1x56x56x128
Relu MobilenetEdgeTPU/expanded_conv_1/expand/Relu ? - ? ? 1x56x56x128
Conv2D MobilenetEdgeTPU/expanded_conv_1/project/Conv2D 501.8 k 1x56x56x128 4.10 k 12.8 M 1x56x56x32
Conv2D MobilenetEdgeTPU/expanded_conv_2/expand/Conv2D 501.8 k 1x56x56x32 36.9 k 115.6 M 1x56x56x128
Relu MobilenetEdgeTPU/expanded_conv_2/expand/Relu ? - ? ? 1x56x56x128
Conv2D MobilenetEdgeTPU/expanded_conv_2/project/Conv2D 501.8 k 1x56x56x128 4.10 k 12.8 M 1x56x56x32
Conv2D MobilenetEdgeTPU/expanded_conv_3/expand/Conv2D 501.8 k 1x56x56x32 36.9 k 115.6 M 1x56x56x128
Relu MobilenetEdgeTPU/expanded_conv_3/expand/Relu ? - ? ? 1x56x56x128
Conv2D MobilenetEdgeTPU/expanded_conv_3/project/Conv2D 501.8 k 1x56x56x128 4.10 k 12.8 M 1x56x56x32
Conv2D MobilenetEdgeTPU/expanded_conv_4/expand/Conv2D 501.8 k 1x56x56x32 36.9 k 115.6 M 1x56x56x128
Relu MobilenetEdgeTPU/expanded_conv_4/expand/Relu ? - ? ? 1x56x56x128
Conv2D MobilenetEdgeTPU/expanded_conv_4/project/Conv2D 501.8 k 1x56x56x128 4.10 k 12.8 M 1x56x56x32
Conv2D MobilenetEdgeTPU/expanded_conv_5/expand/Conv2D 301.1 k 1x56x56x32 73.7 k 57.8 M 1x28x28x256
Relu MobilenetEdgeTPU/expanded_conv_5/expand/Relu ? - ? ? 1x28x28x256
Conv2D MobilenetEdgeTPU/expanded_conv_5/project/Conv2D 238.3 k 1x28x28x256 12.3 k 9.63 M 1x28x28x48
Conv2D MobilenetEdgeTPU/expanded_conv_6/expand/Conv2D 188.2 k 1x28x28x48 82.9 k 65.0 M 1x28x28x192
Relu MobilenetEdgeTPU/expanded_conv_6/expand/Relu ? - ? ? 1x28x28x192
Conv2D MobilenetEdgeTPU/expanded_conv_6/project/Conv2D 188.2 k 1x28x28x192 9.22 k 7.23 M 1x28x28x48
Conv2D MobilenetEdgeTPU/expanded_conv_7/expand/Conv2D 188.2 k 1x28x28x48 82.9 k 65.0 M 1x28x28x192
Relu MobilenetEdgeTPU/expanded_conv_7/expand/Relu ? - ? ? 1x28x28x192
Conv2D MobilenetEdgeTPU/expanded_conv_7/project/Conv2D 188.2 k 1x28x28x192 9.22 k 7.23 M 1x28x28x48
Conv2D MobilenetEdgeTPU/expanded_conv_8/expand/Conv2D 188.2 k 1x28x28x48 82.9 k 65.0 M 1x28x28x192
Relu MobilenetEdgeTPU/expanded_conv_8/expand/Relu ? - ? ? 1x28x28x192
Conv2D MobilenetEdgeTPU/expanded_conv_8/project/Conv2D 188.2 k 1x28x28x192 9.22 k 7.23 M 1x28x28x48
Conv2D MobilenetEdgeTPU/expanded_conv_9/expand/Conv2D 338.7 k 1x28x28x48 18.4 k 14.5 M 1x28x28x384
Relu MobilenetEdgeTPU/expanded_conv_9/expand/Relu ? - ? ? 1x28x28x384
DepthConv MobilenetEdgeTPU/expanded_conv_9/depthwise/depthwise 376.3 k - 3.46 k 677.4 k 1x14x14x384
Relu MobilenetEdgeTPU/expanded_conv_9/depthwise/Relu ? - ? ? 1x14x14x384
Conv2D MobilenetEdgeTPU/expanded_conv_9/project/Conv2D 94.1 k 1x14x14x384 36.9 k 7.23 M 1x14x14x96
Conv2D MobilenetEdgeTPU/expanded_conv_10/expand/Conv2D 94.1 k 1x14x14x96 36.9 k 7.23 M 1x14x14x384
Relu MobilenetEdgeTPU/expanded_conv_10/expand/Relu ? - ? ? 1x14x14x384
DepthConv MobilenetEdgeTPU/expanded_conv_10/depthwise/depthwise 150.5 k - 3.46 k 677.4 k 1x14x14x384
Relu MobilenetEdgeTPU/expanded_conv_10/depthwise/Relu ? - ? ? 1x14x14x384
Conv2D MobilenetEdgeTPU/expanded_conv_10/project/Conv2D 94.1 k 1x14x14x384 36.9 k 7.23 M 1x14x14x96
Conv2D MobilenetEdgeTPU/expanded_conv_11/expand/Conv2D 94.1 k 1x14x14x96 36.9 k 7.23 M 1x14x14x384
Relu MobilenetEdgeTPU/expanded_conv_11/expand/Relu ? - ? ? 1x14x14x384
DepthConv MobilenetEdgeTPU/expanded_conv_11/depthwise/depthwise 150.5 k - 3.46 k 677.4 k 1x14x14x384
Relu MobilenetEdgeTPU/expanded_conv_11/depthwise/Relu ? - ? ? 1x14x14x384
Conv2D MobilenetEdgeTPU/expanded_conv_11/project/Conv2D 94.1 k 1x14x14x384 36.9 k 7.23 M 1x14x14x96
Conv2D MobilenetEdgeTPU/expanded_conv_12/expand/Conv2D 94.1 k 1x14x14x96 36.9 k 7.23 M 1x14x14x384
Relu MobilenetEdgeTPU/expanded_conv_12/expand/Relu ? - ? ? 1x14x14x384
DepthConv MobilenetEdgeTPU/expanded_conv_12/depthwise/depthwise 150.5 k - 3.46 k 677.4 k 1x14x14x384
Relu MobilenetEdgeTPU/expanded_conv_12/depthwise/Relu ? - ? ? 1x14x14x384
Conv2D MobilenetEdgeTPU/expanded_conv_12/project/Conv2D 94.1 k 1x14x14x384 36.9 k 7.23 M 1x14x14x96
Conv2D MobilenetEdgeTPU/expanded_conv_13/expand/Conv2D 169.3 k 1x14x14x96 73.7 k 14.5 M 1x14x14x768
Relu MobilenetEdgeTPU/expanded_conv_13/expand/Relu ? - ? ? 1x14x14x768
DepthConv MobilenetEdgeTPU/expanded_conv_13/depthwise/depthwise 301.1 k - 6.91 k 1.35 M 1x14x14x768
Relu MobilenetEdgeTPU/expanded_conv_13/depthwise/Relu ? - ? ? 1x14x14x768
Conv2D MobilenetEdgeTPU/expanded_conv_13/project/Conv2D 169.3 k 1x14x14x768 73.7 k 14.5 M 1x14x14x96
Conv2D MobilenetEdgeTPU/expanded_conv_14/expand/Conv2D 94.1 k 1x14x14x96 36.9 k 7.23 M 1x14x14x384
Relu MobilenetEdgeTPU/expanded_conv_14/expand/Relu ? - ? ? 1x14x14x384
DepthConv MobilenetEdgeTPU/expanded_conv_14/depthwise/depthwise 150.5 k - 3.46 k 677.4 k 1x14x14x384
Relu MobilenetEdgeTPU/expanded_conv_14/depthwise/Relu ? - ? ? 1x14x14x384
Conv2D MobilenetEdgeTPU/expanded_conv_14/project/Conv2D 94.1 k 1x14x14x384 36.9 k 7.23 M 1x14x14x96
Conv2D MobilenetEdgeTPU/expanded_conv_15/expand/Conv2D 94.1 k 1x14x14x96 36.9 k 7.23 M 1x14x14x384
Relu MobilenetEdgeTPU/expanded_conv_15/expand/Relu ? - ? ? 1x14x14x384
DepthConv MobilenetEdgeTPU/expanded_conv_15/depthwise/depthwise 150.5 k - 3.46 k 677.4 k 1x14x14x384
Relu MobilenetEdgeTPU/expanded_conv_15/depthwise/Relu ? - ? ? 1x14x14x384
Conv2D MobilenetEdgeTPU/expanded_conv_15/project/Conv2D 94.1 k 1x14x14x384 36.9 k 7.23 M 1x14x14x96
Conv2D MobilenetEdgeTPU/expanded_conv_16/expand/Conv2D 94.1 k 1x14x14x96 36.9 k 7.23 M 1x14x14x384
Relu MobilenetEdgeTPU/expanded_conv_16/expand/Relu ? - ? ? 1x14x14x384
DepthConv MobilenetEdgeTPU/expanded_conv_16/depthwise/depthwise 150.5 k - 3.46 k 677.4 k 1x14x14x384
Relu MobilenetEdgeTPU/expanded_conv_16/depthwise/Relu ? - ? ? 1x14x14x384
Conv2D MobilenetEdgeTPU/expanded_conv_16/project/Conv2D 94.1 k 1x14x14x384 36.9 k 7.23 M 1x14x14x96
Conv2D MobilenetEdgeTPU/expanded_conv_17/expand/Conv2D 169.3 k 1x14x14x96 73.7 k 14.5 M 1x14x14x768
Relu MobilenetEdgeTPU/expanded_conv_17/expand/Relu ? - ? ? 1x14x14x768
DepthConv MobilenetEdgeTPU/expanded_conv_17/depthwise/depthwise 188.2 k - 19.2 k 940.8 k 1x7x7x768
Relu MobilenetEdgeTPU/expanded_conv_17/depthwise/Relu ? - ? ? 1x7x7x768
Conv2D MobilenetEdgeTPU/expanded_conv_17/project/Conv2D 45.5 k 1x7x7x768 122.9 k 6.02 M 1x7x7x160
Conv2D MobilenetEdgeTPU/expanded_conv_18/expand/Conv2D 39.2 k 1x7x7x160 102.4 k 5.02 M 1x7x7x640
Relu MobilenetEdgeTPU/expanded_conv_18/expand/Relu ? - ? ? 1x7x7x640
DepthConv MobilenetEdgeTPU/expanded_conv_18/depthwise/depthwise 62.7 k - 16.0 k 784.0 k 1x7x7x640
Relu MobilenetEdgeTPU/expanded_conv_18/depthwise/Relu ? - ? ? 1x7x7x640
Conv2D MobilenetEdgeTPU/expanded_conv_18/project/Conv2D 39.2 k 1x7x7x640 102.4 k 5.02 M 1x7x7x160
Conv2D MobilenetEdgeTPU/expanded_conv_19/expand/Conv2D 39.2 k 1x7x7x160 102.4 k 5.02 M 1x7x7x640
Relu MobilenetEdgeTPU/expanded_conv_19/expand/Relu ? - ? ? 1x7x7x640
DepthConv MobilenetEdgeTPU/expanded_conv_19/depthwise/depthwise 62.7 k - 16.0 k 784.0 k 1x7x7x640
Relu MobilenetEdgeTPU/expanded_conv_19/depthwise/Relu ? - ? ? 1x7x7x640
Conv2D MobilenetEdgeTPU/expanded_conv_19/project/Conv2D 39.2 k 1x7x7x640 102.4 k 5.02 M 1x7x7x160
Conv2D MobilenetEdgeTPU/expanded_conv_20/expand/Conv2D 39.2 k 1x7x7x160 102.4 k 5.02 M 1x7x7x640
Relu MobilenetEdgeTPU/expanded_conv_20/expand/Relu ? - ? ? 1x7x7x640
DepthConv MobilenetEdgeTPU/expanded_conv_20/depthwise/depthwise 62.7 k - 16.0 k 784.0 k 1x7x7x640
Relu MobilenetEdgeTPU/expanded_conv_20/depthwise/Relu ? - ? ? 1x7x7x640
Conv2D MobilenetEdgeTPU/expanded_conv_20/project/Conv2D 39.2 k 1x7x7x640 102.4 k 5.02 M 1x7x7x160
Conv2D MobilenetEdgeTPU/expanded_conv_21/expand/Conv2D 70.6 k 1x7x7x160 204.8 k 10.0 M 1x7x7x1280
Relu MobilenetEdgeTPU/expanded_conv_21/expand/Relu ? - ? ? 1x7x7x1280
DepthConv MobilenetEdgeTPU/expanded_conv_21/depthwise/depthwise 125.4 k - 11.5 k 564.5 k 1x7x7x1280
Relu MobilenetEdgeTPU/expanded_conv_21/depthwise/Relu ? - ? ? 1x7x7x1280
Conv2D MobilenetEdgeTPU/expanded_conv_21/project/Conv2D 72.1 k 1x7x7x1280 245.8 k 12.0 M 1x7x7x192
Conv2D MobilenetEdgeTPU/Conv_1/Conv2D 72.1 k 1x7x7x192 245.8 k 12.0 M 1x7x7x1280
Relu MobilenetEdgeTPU/Conv_1/Relu ? - ? ? 1x7x7x1280
AvgPool MobilenetEdgeTPU/Logits/AvgPool2D ? 1x7x7x1280 ? 62.7 k 1x1x1x1280
Conv2D MobilenetEdgeTPU/Logits/Conv2d_1c_1x1/Conv2D 2.28 k 1x1x1x1280 1.28 M 1.28 M 1x1x1x1001
-----
Total Total 11.6 M - 4.05 M 990.7 M -
# pylint: enable=line-too-long
"""
from __future__ import absolute_import
from __future__ import division
......@@ -22,13 +356,12 @@ import copy
import functools
import numpy as np
import tensorflow as tf
from tensorflow.contrib import slim as contrib_slim
import tensorflow.compat.v1 as tf
import tf_slim as slim
from nets.mobilenet import conv_blocks as ops
from nets.mobilenet import mobilenet as lib
slim = contrib_slim
op = lib.op
expand_input = ops.expand_input_by_factor
......@@ -45,7 +378,7 @@ _se4 = lambda expansion_tensor, input_tensor: squeeze_excite(expansion_tensor)
def hard_swish(x):
with tf.compat.v1.name_scope('hard_swish'):
with tf.name_scope('hard_swish'):
return x * tf.nn.relu6(x + np.float32(3)) * np.float32(1. / 6.)
......@@ -126,6 +459,16 @@ DEFAULTS = {
},
}
DEFAULTS_GROUP_NORM = {
(ops.expanded_conv,): dict(normalizer_fn=slim.group_norm, residual=True),
(slim.conv2d, slim.fully_connected, slim.separable_conv2d): {
'normalizer_fn': slim.group_norm,
'activation_fn': tf.nn.relu,
},
(slim.group_norm,): {
'groups': 8
},
}
# Compatible checkpoint: http://mldash/5511169891790690458#scalars
V3_LARGE = dict(
defaults=dict(DEFAULTS),
......@@ -276,13 +619,14 @@ def mobilenet(input_tensor,
scope='MobilenetV3',
conv_defs=None,
finegrain_classification_mode=False,
use_groupnorm=False,
**kwargs):
"""Creates mobilenet V3 network.
Inference mode is created by default. To create training use training_scope
below.
with tf.contrib.slim.arg_scope(mobilenet_v3.training_scope()):
with slim.arg_scope(mobilenet_v3.training_scope()):
logits, endpoints = mobilenet_v3.mobilenet(input_tensor)
Args:
......@@ -298,6 +642,8 @@ def mobilenet(input_tensor,
https://arxiv.org/abs/1801.04381
it improves performance for ImageNet-type of problems.
*Note* ignored if final_endpoint makes the builder exit earlier.
use_groupnorm: When set to True, use group_norm as normalizer_fn.
**kwargs: passed directly to mobilenet.mobilenet:
prediction_fn- what prediction function to use.
reuse-: whether to reuse variables (if reuse set to true, scope
......@@ -313,6 +659,16 @@ def mobilenet(input_tensor,
if 'multiplier' in kwargs:
raise ValueError('mobilenetv2 doesn\'t support generic '
'multiplier parameter use "depth_multiplier" instead.')
if use_groupnorm:
conv_defs = copy.deepcopy(conv_defs)
conv_defs['defaults'] = dict(DEFAULTS_GROUP_NORM)
conv_defs['defaults'].update({
(slim.group_norm,): {
'groups': kwargs.pop('groups', 8)
}
})
if finegrain_classification_mode:
conv_defs = copy.deepcopy(conv_defs)
conv_defs['spec'][-1] = conv_defs['spec'][-1]._replace(
......
......@@ -18,54 +18,83 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from absl.testing import absltest
import tensorflow as tf
import tensorflow.compat.v1 as tf
from nets.mobilenet import mobilenet_v3
from google3.testing.pybase import parameterized
class MobilenetV3Test(absltest.TestCase):
class MobilenetV3Test(tf.test.TestCase, parameterized.TestCase):
def setUp(self):
super(MobilenetV3Test, self).setUp()
tf.compat.v1.reset_default_graph()
# pylint: disable = g-unreachable-test-method
def assertVariablesHaveNormalizerFn(self, use_groupnorm):
global_variables = [v.name for v in tf.global_variables()]
has_batch_norm = False
has_group_norm = False
for global_variable in global_variables:
if 'BatchNorm' in global_variable:
has_batch_norm = True
if 'GroupNorm' in global_variable:
has_group_norm = True
if use_groupnorm:
self.assertFalse(has_batch_norm)
self.assertTrue(has_group_norm)
else:
self.assertTrue(has_batch_norm)
self.assertFalse(has_group_norm)
def testMobilenetV3Large(self):
@parameterized.named_parameters(('without_groupnorm', False),
('with_groupnorm', True))
def testMobilenetV3Large(self, use_groupnorm):
logits, endpoints = mobilenet_v3.mobilenet(
tf.compat.v1.placeholder(tf.float32, (1, 224, 224, 3)))
tf.placeholder(tf.float32, (1, 224, 224, 3)),
use_groupnorm=use_groupnorm)
self.assertEqual(endpoints['layer_19'].shape, [1, 1, 1, 1280])
self.assertEqual(logits.shape, [1, 1001])
self.assertVariablesHaveNormalizerFn(use_groupnorm)
def testMobilenetV3Small(self):
@parameterized.named_parameters(('without_groupnorm', False),
('with_groupnorm', True))
def testMobilenetV3Small(self, use_groupnorm):
_, endpoints = mobilenet_v3.mobilenet(
tf.compat.v1.placeholder(tf.float32, (1, 224, 224, 3)),
conv_defs=mobilenet_v3.V3_SMALL)
tf.placeholder(tf.float32, (1, 224, 224, 3)),
conv_defs=mobilenet_v3.V3_SMALL,
use_groupnorm=use_groupnorm)
self.assertEqual(endpoints['layer_15'].shape, [1, 1, 1, 1024])
self.assertVariablesHaveNormalizerFn(use_groupnorm)
def testMobilenetEdgeTpu(self):
@parameterized.named_parameters(('without_groupnorm', False),
('with_groupnorm', True))
def testMobilenetEdgeTpu(self, use_groupnorm):
_, endpoints = mobilenet_v3.edge_tpu(
tf.compat.v1.placeholder(tf.float32, (1, 224, 224, 3)))
tf.placeholder(tf.float32, (1, 224, 224, 3)),
use_groupnorm=use_groupnorm)
self.assertIn('Inference mode is created by default',
mobilenet_v3.edge_tpu.__doc__)
self.assertEqual(endpoints['layer_24'].shape, [1, 7, 7, 1280])
self.assertStartsWith(
endpoints['layer_24'].name, 'MobilenetEdgeTPU')
self.assertVariablesHaveNormalizerFn(use_groupnorm)
def testMobilenetEdgeTpuChangeScope(self):
_, endpoints = mobilenet_v3.edge_tpu(
tf.compat.v1.placeholder(tf.float32, (1, 224, 224, 3)), scope='Scope')
tf.placeholder(tf.float32, (1, 224, 224, 3)), scope='Scope')
self.assertStartsWith(
endpoints['layer_24'].name, 'Scope')
def testMobilenetV3BaseOnly(self):
@parameterized.named_parameters(('without_groupnorm', False),
('with_groupnorm', True))
def testMobilenetV3BaseOnly(self, use_groupnorm):
result, endpoints = mobilenet_v3.mobilenet(
tf.compat.v1.placeholder(tf.float32, (1, 224, 224, 3)),
tf.placeholder(tf.float32, (1, 224, 224, 3)),
conv_defs=mobilenet_v3.V3_LARGE,
use_groupnorm=use_groupnorm,
base_only=True,
final_endpoint='layer_17')
# Get the latest layer before average pool.
self.assertEqual(endpoints['layer_17'].shape, [1, 7, 7, 960])
self.assertEqual(result, endpoints['layer_17'])
self.assertVariablesHaveNormalizerFn(use_groupnorm)
def testMobilenetV3BaseOnly_VariableInput(self):
result, endpoints = mobilenet_v3.mobilenet(
......@@ -78,5 +107,34 @@ class MobilenetV3Test(absltest.TestCase):
[None, None, None, 960])
self.assertEqual(result, endpoints['layer_17'])
# Use reduce mean for pooling and check for operation 'ReduceMean' in graph
@parameterized.named_parameters(('without_groupnorm', False),
('with_groupnorm', True))
def testMobilenetV3WithReduceMean(self, use_groupnorm):
_, _ = mobilenet_v3.mobilenet(
tf.placeholder(tf.float32, (1, 224, 224, 3)),
conv_defs=mobilenet_v3.V3_SMALL,
use_groupnorm=use_groupnorm,
use_reduce_mean_for_pooling=True)
g = tf.get_default_graph()
reduce_mean = [v for v in g.get_operations() if 'ReduceMean' in v.name]
self.assertNotEmpty(reduce_mean)
self.assertVariablesHaveNormalizerFn(use_groupnorm)
@parameterized.named_parameters(('without_groupnorm', False),
('with_groupnorm', True))
def testMobilenetV3WithOutReduceMean(self, use_groupnorm):
_, _ = mobilenet_v3.mobilenet(
tf.placeholder(tf.float32, (1, 224, 224, 3)),
conv_defs=mobilenet_v3.V3_SMALL,
use_groupnorm=use_groupnorm,
use_reduce_mean_for_pooling=False)
g = tf.get_default_graph()
reduce_mean = [v for v in g.get_operations() if 'ReduceMean' in v.name]
self.assertEmpty(reduce_mean)
self.assertVariablesHaveNormalizerFn(use_groupnorm)
if __name__ == '__main__':
absltest.main()
# absltest.main()
tf.test.main()
......@@ -108,11 +108,8 @@ from __future__ import print_function
from collections import namedtuple
import functools
import tensorflow as tf
from tensorflow.contrib import layers as contrib_layers
from tensorflow.contrib import slim as contrib_slim
slim = contrib_slim
import tensorflow.compat.v1 as tf
import tf_slim as slim
# Conv and DepthSepConv namedtuple define layers of the MobileNet architecture
# Conv defines 3x3 convolution layers
......@@ -233,7 +230,7 @@ def mobilenet_v1_base(inputs,
padding = 'SAME'
if use_explicit_padding:
padding = 'VALID'
with tf.compat.v1.variable_scope(scope, 'MobilenetV1', [inputs]):
with tf.variable_scope(scope, 'MobilenetV1', [inputs]):
with slim.arg_scope([slim.conv2d, slim.separable_conv2d], padding=padding):
# The current_stride variable keeps track of the output stride of the
# activations, i.e., the running product of convolution strides up to the
......@@ -311,7 +308,7 @@ def mobilenet_v1(inputs,
min_depth=8,
depth_multiplier=1.0,
conv_defs=None,
prediction_fn=contrib_layers.softmax,
prediction_fn=slim.softmax,
spatial_squeeze=True,
reuse=None,
scope='MobilenetV1',
......@@ -359,7 +356,7 @@ def mobilenet_v1(inputs,
raise ValueError('Invalid input tensor rank, expected 4, was: %d' %
len(input_shape))
with tf.compat.v1.variable_scope(
with tf.variable_scope(
scope, 'MobilenetV1', [inputs], reuse=reuse) as scope:
with slim.arg_scope([slim.batch_norm, slim.dropout],
is_training=is_training):
......@@ -367,7 +364,7 @@ def mobilenet_v1(inputs,
min_depth=min_depth,
depth_multiplier=depth_multiplier,
conv_defs=conv_defs)
with tf.compat.v1.variable_scope('Logits'):
with tf.variable_scope('Logits'):
if global_pool:
# Global average pooling.
net = tf.reduce_mean(
......@@ -435,7 +432,7 @@ def mobilenet_v1_arg_scope(
regularize_depthwise=False,
batch_norm_decay=0.9997,
batch_norm_epsilon=0.001,
batch_norm_updates_collections=tf.compat.v1.GraphKeys.UPDATE_OPS,
batch_norm_updates_collections=tf.GraphKeys.UPDATE_OPS,
normalizer_fn=slim.batch_norm):
"""Defines the default MobilenetV1 arg scope.
......@@ -466,8 +463,8 @@ def mobilenet_v1_arg_scope(
batch_norm_params['is_training'] = is_training
# Set weight_decay for weights in Conv and DepthSepConv layers.
weights_init = tf.compat.v1.truncated_normal_initializer(stddev=stddev)
regularizer = contrib_layers.l2_regularizer(weight_decay)
weights_init = tf.truncated_normal_initializer(stddev=stddev)
regularizer = slim.l2_regularizer(weight_decay)
if regularize_depthwise:
depthwise_regularizer = regularizer
else:
......
......@@ -19,17 +19,16 @@ from __future__ import division
from __future__ import print_function
import math
import tensorflow as tf
import tensorflow.compat.v1 as tf
import tf_slim as slim
from tensorflow.contrib import quantize as contrib_quantize
from tensorflow.contrib import slim as contrib_slim
from datasets import dataset_factory
from nets import mobilenet_v1
from preprocessing import preprocessing_factory
slim = contrib_slim
flags = tf.compat.v1.app.flags
flags = tf.app.flags
flags.DEFINE_string('master', '', 'Session master')
flags.DEFINE_integer('batch_size', 250, 'Batch size')
......@@ -74,7 +73,7 @@ def imagenet_input(is_training):
image = image_preprocessing_fn(image, FLAGS.image_size, FLAGS.image_size)
images, labels = tf.compat.v1.train.batch(
images, labels = tf.train.batch(
tensors=[image, label],
batch_size=FLAGS.batch_size,
num_threads=4,
......@@ -95,10 +94,10 @@ def metrics(logits, labels):
labels = tf.squeeze(labels)
names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
'Accuracy':
tf.compat.v1.metrics.accuracy(
tf.metrics.accuracy(
tf.argmax(input=logits, axis=1), labels),
'Recall_5':
tf.compat.v1.metrics.recall_at_k(labels, logits, 5),
tf.metrics.recall_at_k(labels, logits, 5),
})
for name, value in names_to_values.iteritems():
slim.summaries.add_scalar_summary(
......@@ -154,4 +153,4 @@ def main(unused_arg):
if __name__ == '__main__':
tf.compat.v1.app.run(main)
tf.app.run(main)
......@@ -19,13 +19,11 @@ from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
from tensorflow.contrib import slim as contrib_slim
import tensorflow.compat.v1 as tf
import tf_slim as slim
from nets import mobilenet_v1
slim = contrib_slim
class MobilenetV1Test(tf.test.TestCase):
......@@ -105,7 +103,7 @@ class MobilenetV1Test(tf.test.TestCase):
inputs, final_endpoint=endpoint)
self.assertTrue(out_tensor.op.name.startswith(
'MobilenetV1/' + endpoint))
self.assertItemsEqual(endpoints[:index+1], end_points.keys())
self.assertItemsEqual(endpoints[:index + 1], end_points.keys())
def testBuildCustomNetworkUsingConvDefs(self):
batch_size = 5
......@@ -420,13 +418,13 @@ class MobilenetV1Test(tf.test.TestCase):
[batch_size, 4, 4, 1024])
def testUnknownImageShape(self):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
batch_size = 2
height, width = 224, 224
num_classes = 1000
input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
with self.test_session() as sess:
inputs = tf.compat.v1.placeholder(
inputs = tf.placeholder(
tf.float32, shape=(batch_size, None, None, 3))
logits, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
self.assertTrue(logits.op.name.startswith('MobilenetV1/Logits'))
......@@ -434,18 +432,18 @@ class MobilenetV1Test(tf.test.TestCase):
[batch_size, num_classes])
pre_pool = end_points['Conv2d_13_pointwise']
feed_dict = {inputs: input_np}
tf.compat.v1.global_variables_initializer().run()
tf.global_variables_initializer().run()
pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
self.assertListEqual(list(pre_pool_out.shape), [batch_size, 7, 7, 1024])
def testGlobalPoolUnknownImageShape(self):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
batch_size = 1
height, width = 250, 300
num_classes = 1000
input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
with self.test_session() as sess:
inputs = tf.compat.v1.placeholder(
inputs = tf.placeholder(
tf.float32, shape=(batch_size, None, None, 3))
logits, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes,
global_pool=True)
......@@ -454,7 +452,7 @@ class MobilenetV1Test(tf.test.TestCase):
[batch_size, num_classes])
pre_pool = end_points['Conv2d_13_pointwise']
feed_dict = {inputs: input_np}
tf.compat.v1.global_variables_initializer().run()
tf.global_variables_initializer().run()
pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
self.assertListEqual(list(pre_pool_out.shape), [batch_size, 8, 10, 1024])
......@@ -463,7 +461,7 @@ class MobilenetV1Test(tf.test.TestCase):
height, width = 224, 224
num_classes = 1000
inputs = tf.compat.v1.placeholder(tf.float32, (None, height, width, 3))
inputs = tf.placeholder(tf.float32, (None, height, width, 3))
logits, _ = mobilenet_v1.mobilenet_v1(inputs, num_classes)
self.assertTrue(logits.op.name.startswith('MobilenetV1/Logits'))
self.assertListEqual(logits.get_shape().as_list(),
......@@ -471,7 +469,7 @@ class MobilenetV1Test(tf.test.TestCase):
images = tf.random.uniform((batch_size, height, width, 3))
with self.test_session() as sess:
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(tf.global_variables_initializer())
output = sess.run(logits, {inputs: images.eval()})
self.assertEquals(output.shape, (batch_size, num_classes))
......@@ -486,7 +484,7 @@ class MobilenetV1Test(tf.test.TestCase):
predictions = tf.argmax(input=logits, axis=1)
with self.test_session() as sess:
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(tf.global_variables_initializer())
output = sess.run(predictions)
self.assertEquals(output.shape, (batch_size,))
......@@ -504,7 +502,7 @@ class MobilenetV1Test(tf.test.TestCase):
predictions = tf.argmax(input=logits, axis=1)
with self.test_session() as sess:
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(tf.global_variables_initializer())
output = sess.run(predictions)
self.assertEquals(output.shape, (eval_batch_size,))
......@@ -516,7 +514,7 @@ class MobilenetV1Test(tf.test.TestCase):
spatial_squeeze=False)
with self.test_session() as sess:
tf.compat.v1.global_variables_initializer().run()
tf.global_variables_initializer().run()
logits_out = sess.run(logits)
self.assertListEqual(list(logits_out.shape), [1, 1, 1, num_classes])
......
......@@ -18,17 +18,16 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import tensorflow.compat.v1 as tf
import tf_slim as slim
from tensorflow.contrib import quantize as contrib_quantize
from tensorflow.contrib import slim as contrib_slim
from datasets import dataset_factory
from nets import mobilenet_v1
from preprocessing import preprocessing_factory
slim = contrib_slim
flags = tf.compat.v1.app.flags
flags = tf.app.flags
flags.DEFINE_string('master', '', 'Session master')
flags.DEFINE_integer('task', 0, 'Task')
......@@ -104,10 +103,10 @@ def imagenet_input(is_training):
image = image_preprocessing_fn(image, FLAGS.image_size, FLAGS.image_size)
images, labels = tf.compat.v1.train.batch([image, label],
batch_size=FLAGS.batch_size,
num_threads=4,
capacity=5 * FLAGS.batch_size)
images, labels = tf.train.batch([image, label],
batch_size=FLAGS.batch_size,
num_threads=4,
capacity=5 * FLAGS.batch_size)
labels = slim.one_hot_encoding(labels, FLAGS.num_classes)
return images, labels
......@@ -122,7 +121,7 @@ def build_model():
"""
g = tf.Graph()
with g.as_default(), tf.device(
tf.compat.v1.train.replica_device_setter(FLAGS.ps_tasks)):
tf.train.replica_device_setter(FLAGS.ps_tasks)):
inputs, labels = imagenet_input(is_training=True)
with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(is_training=True)):
logits, _ = mobilenet_v1.mobilenet_v1(
......@@ -131,7 +130,7 @@ def build_model():
depth_multiplier=FLAGS.depth_multiplier,
num_classes=FLAGS.num_classes)
tf.compat.v1.losses.softmax_cross_entropy(labels, logits)
tf.losses.softmax_cross_entropy(labels, logits)
# Call rewriter to produce graph with fake quant ops and folded batch norms
# quant_delay delays start of quantization till quant_delay steps, allowing
......@@ -139,19 +138,19 @@ def build_model():
if FLAGS.quantize:
contrib_quantize.create_training_graph(quant_delay=get_quant_delay())
total_loss = tf.compat.v1.losses.get_total_loss(name='total_loss')
total_loss = tf.losses.get_total_loss(name='total_loss')
# Configure the learning rate using an exponential decay.
num_epochs_per_decay = 2.5
imagenet_size = 1271167
decay_steps = int(imagenet_size / FLAGS.batch_size * num_epochs_per_decay)
learning_rate = tf.compat.v1.train.exponential_decay(
learning_rate = tf.train.exponential_decay(
get_learning_rate(),
tf.compat.v1.train.get_or_create_global_step(),
tf.train.get_or_create_global_step(),
decay_steps,
_LEARNING_RATE_DECAY_FACTOR,
staircase=True)
opt = tf.compat.v1.train.GradientDescentOptimizer(learning_rate)
opt = tf.train.GradientDescentOptimizer(learning_rate)
train_tensor = slim.learning.create_train_op(
total_loss,
......@@ -166,8 +165,8 @@ def get_checkpoint_init_fn():
"""Returns the checkpoint init_fn if the checkpoint is provided."""
if FLAGS.fine_tune_checkpoint:
variables_to_restore = slim.get_variables_to_restore()
global_step_reset = tf.compat.v1.assign(
tf.compat.v1.train.get_or_create_global_step(), 0)
global_step_reset = tf.assign(
tf.train.get_or_create_global_step(), 0)
# When restoring from a floating point model, the min/max values for
# quantized weights and activations are not present.
# We instruct slim to ignore variables that are missing during restoration
......@@ -203,7 +202,7 @@ def train_model():
save_summaries_secs=FLAGS.save_summaries_secs,
save_interval_secs=FLAGS.save_interval_secs,
init_fn=get_checkpoint_init_fn(),
global_step=tf.compat.v1.train.get_global_step())
global_step=tf.train.get_global_step())
def main(unused_arg):
......@@ -211,4 +210,4 @@ def main(unused_arg):
if __name__ == '__main__':
tf.compat.v1.app.run(main)
tf.app.run(main)
......@@ -21,16 +21,13 @@ from __future__ import division
from __future__ import print_function
import copy
import tensorflow as tf
from tensorflow.contrib import framework as contrib_framework
from tensorflow.contrib import layers as contrib_layers
from tensorflow.contrib import slim as contrib_slim
from tensorflow.contrib import training as contrib_training
import tensorflow.compat.v1 as tf
import tf_slim as slim
from tensorflow.contrib import training as contrib_training
from nets.nasnet import nasnet_utils
arg_scope = contrib_framework.arg_scope
slim = contrib_slim
arg_scope = slim.arg_scope
# Notes for training NASNet Cifar Model
......@@ -142,9 +139,8 @@ def nasnet_cifar_arg_scope(weight_decay=5e-4,
'scale': True,
'fused': True,
}
weights_regularizer = contrib_layers.l2_regularizer(weight_decay)
weights_initializer = contrib_layers.variance_scaling_initializer(
mode='FAN_OUT')
weights_regularizer = slim.l2_regularizer(weight_decay)
weights_initializer = slim.variance_scaling_initializer(mode='FAN_OUT')
with arg_scope([slim.fully_connected, slim.conv2d, slim.separable_conv2d],
weights_regularizer=weights_regularizer,
weights_initializer=weights_initializer):
......@@ -178,9 +174,8 @@ def nasnet_mobile_arg_scope(weight_decay=4e-5,
'scale': True,
'fused': True,
}
weights_regularizer = contrib_layers.l2_regularizer(weight_decay)
weights_initializer = contrib_layers.variance_scaling_initializer(
mode='FAN_OUT')
weights_regularizer = slim.l2_regularizer(weight_decay)
weights_initializer = slim.variance_scaling_initializer(mode='FAN_OUT')
with arg_scope([slim.fully_connected, slim.conv2d, slim.separable_conv2d],
weights_regularizer=weights_regularizer,
weights_initializer=weights_initializer):
......@@ -214,9 +209,8 @@ def nasnet_large_arg_scope(weight_decay=5e-5,
'scale': True,
'fused': True,
}
weights_regularizer = contrib_layers.l2_regularizer(weight_decay)
weights_initializer = contrib_layers.variance_scaling_initializer(
mode='FAN_OUT')
weights_regularizer = slim.l2_regularizer(weight_decay)
weights_initializer = slim.variance_scaling_initializer(mode='FAN_OUT')
with arg_scope([slim.fully_connected, slim.conv2d, slim.separable_conv2d],
weights_regularizer=weights_regularizer,
weights_initializer=weights_initializer):
......@@ -231,9 +225,9 @@ def nasnet_large_arg_scope(weight_decay=5e-5,
def _build_aux_head(net, end_points, num_classes, hparams, scope):
"""Auxiliary head used for all models across all datasets."""
activation_fn = tf.nn.relu6 if hparams.use_bounded_activation else tf.nn.relu
with tf.compat.v1.variable_scope(scope):
with tf.variable_scope(scope):
aux_logits = tf.identity(net)
with tf.compat.v1.variable_scope('aux_logits'):
with tf.variable_scope('aux_logits'):
aux_logits = slim.avg_pool2d(
aux_logits, [5, 5], stride=3, padding='VALID')
aux_logits = slim.conv2d(aux_logits, 128, [1, 1], scope='proj')
......@@ -248,7 +242,7 @@ def _build_aux_head(net, end_points, num_classes, hparams, scope):
aux_logits = slim.conv2d(aux_logits, 768, shape, padding='VALID')
aux_logits = slim.batch_norm(aux_logits, scope='aux_bn1')
aux_logits = activation_fn(aux_logits)
aux_logits = contrib_layers.flatten(aux_logits)
aux_logits = slim.flatten(aux_logits)
aux_logits = slim.fully_connected(aux_logits, num_classes)
end_points['AuxLogits'] = aux_logits
......@@ -302,7 +296,7 @@ def build_nasnet_cifar(images, num_classes,
_update_hparams(hparams, is_training)
if tf.test.is_gpu_available() and hparams.data_format == 'NHWC':
tf.compat.v1.logging.info(
tf.logging.info(
'A GPU is available on the machine, consider using NCHW '
'data format for increased speed on GPU.')
......@@ -355,7 +349,7 @@ def build_nasnet_mobile(images, num_classes,
_update_hparams(hparams, is_training)
if tf.test.is_gpu_available() and hparams.data_format == 'NHWC':
tf.compat.v1.logging.info(
tf.logging.info(
'A GPU is available on the machine, consider using NCHW '
'data format for increased speed on GPU.')
......@@ -411,7 +405,7 @@ def build_nasnet_large(images, num_classes,
_update_hparams(hparams, is_training)
if tf.test.is_gpu_available() and hparams.data_format == 'NHWC':
tf.compat.v1.logging.info(
tf.logging.info(
'A GPU is available on the machine, consider using NCHW '
'data format for increased speed on GPU.')
......@@ -537,7 +531,7 @@ def _build_nasnet_base(images,
cell_outputs.append(net)
# Final softmax layer
with tf.compat.v1.variable_scope('final_layer'):
with tf.variable_scope('final_layer'):
net = activation_fn(net)
net = nasnet_utils.global_avg_pool(net)
if add_and_check_endpoint('global_pool', net) or not num_classes:
......
......@@ -17,13 +17,11 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
from tensorflow.contrib import slim as contrib_slim
import tensorflow.compat.v1 as tf
import tf_slim as slim
from nets.nasnet import nasnet
slim = contrib_slim
class NASNetTest(tf.test.TestCase):
......@@ -32,7 +30,7 @@ class NASNetTest(tf.test.TestCase):
height, width = 32, 32
num_classes = 10
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
with slim.arg_scope(nasnet.nasnet_cifar_arg_scope()):
logits, end_points = nasnet.build_nasnet_cifar(inputs, num_classes)
auxlogits = end_points['AuxLogits']
......@@ -49,7 +47,7 @@ class NASNetTest(tf.test.TestCase):
height, width = 224, 224
num_classes = 1000
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
logits, end_points = nasnet.build_nasnet_mobile(inputs, num_classes)
auxlogits = end_points['AuxLogits']
......@@ -66,7 +64,7 @@ class NASNetTest(tf.test.TestCase):
height, width = 331, 331
num_classes = 1000
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
with slim.arg_scope(nasnet.nasnet_large_arg_scope()):
logits, end_points = nasnet.build_nasnet_large(inputs, num_classes)
auxlogits = end_points['AuxLogits']
......@@ -83,7 +81,7 @@ class NASNetTest(tf.test.TestCase):
height, width = 32, 32
num_classes = None
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
with slim.arg_scope(nasnet.nasnet_cifar_arg_scope()):
net, end_points = nasnet.build_nasnet_cifar(inputs, num_classes)
self.assertFalse('AuxLogits' in end_points)
......@@ -96,7 +94,7 @@ class NASNetTest(tf.test.TestCase):
height, width = 224, 224
num_classes = None
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
net, end_points = nasnet.build_nasnet_mobile(inputs, num_classes)
self.assertFalse('AuxLogits' in end_points)
......@@ -109,7 +107,7 @@ class NASNetTest(tf.test.TestCase):
height, width = 331, 331
num_classes = None
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
with slim.arg_scope(nasnet.nasnet_large_arg_scope()):
net, end_points = nasnet.build_nasnet_large(inputs, num_classes)
self.assertFalse('AuxLogits' in end_points)
......@@ -122,7 +120,7 @@ class NASNetTest(tf.test.TestCase):
height, width = 32, 32
num_classes = 10
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
with slim.arg_scope(nasnet.nasnet_cifar_arg_scope()):
_, end_points = nasnet.build_nasnet_cifar(inputs, num_classes)
endpoints_shapes = {'Stem': [batch_size, 32, 32, 96],
......@@ -153,7 +151,7 @@ class NASNetTest(tf.test.TestCase):
'Predictions': [batch_size, num_classes]}
self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
for endpoint_name in endpoints_shapes:
tf.compat.v1.logging.info('Endpoint name: {}'.format(endpoint_name))
tf.logging.info('Endpoint name: {}'.format(endpoint_name))
expected_shape = endpoints_shapes[endpoint_name]
self.assertTrue(endpoint_name in end_points)
self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
......@@ -164,9 +162,9 @@ class NASNetTest(tf.test.TestCase):
height, width = 32, 32
num_classes = 10
for use_aux_head in (True, False):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
config = nasnet.cifar_config()
config.set_hparam('use_aux_head', int(use_aux_head))
with slim.arg_scope(nasnet.nasnet_cifar_arg_scope()):
......@@ -179,7 +177,7 @@ class NASNetTest(tf.test.TestCase):
height, width = 224, 224
num_classes = 1000
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
_, end_points = nasnet.build_nasnet_mobile(inputs, num_classes)
endpoints_shapes = {'Stem': [batch_size, 28, 28, 88],
......@@ -204,7 +202,7 @@ class NASNetTest(tf.test.TestCase):
'Predictions': [batch_size, num_classes]}
self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
for endpoint_name in endpoints_shapes:
tf.compat.v1.logging.info('Endpoint name: {}'.format(endpoint_name))
tf.logging.info('Endpoint name: {}'.format(endpoint_name))
expected_shape = endpoints_shapes[endpoint_name]
self.assertTrue(endpoint_name in end_points)
self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
......@@ -215,9 +213,9 @@ class NASNetTest(tf.test.TestCase):
height, width = 224, 224
num_classes = 1000
for use_aux_head in (True, False):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
config = nasnet.mobile_imagenet_config()
config.set_hparam('use_aux_head', int(use_aux_head))
with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
......@@ -230,7 +228,7 @@ class NASNetTest(tf.test.TestCase):
height, width = 331, 331
num_classes = 1000
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
with slim.arg_scope(nasnet.nasnet_large_arg_scope()):
_, end_points = nasnet.build_nasnet_large(inputs, num_classes)
endpoints_shapes = {'Stem': [batch_size, 42, 42, 336],
......@@ -261,7 +259,7 @@ class NASNetTest(tf.test.TestCase):
'Predictions': [batch_size, num_classes]}
self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
for endpoint_name in endpoints_shapes:
tf.compat.v1.logging.info('Endpoint name: {}'.format(endpoint_name))
tf.logging.info('Endpoint name: {}'.format(endpoint_name))
expected_shape = endpoints_shapes[endpoint_name]
self.assertTrue(endpoint_name in end_points)
self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
......@@ -272,9 +270,9 @@ class NASNetTest(tf.test.TestCase):
height, width = 331, 331
num_classes = 1000
for use_aux_head in (True, False):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
config = nasnet.large_imagenet_config()
config.set_hparam('use_aux_head', int(use_aux_head))
with slim.arg_scope(nasnet.nasnet_large_arg_scope()):
......@@ -287,19 +285,19 @@ class NASNetTest(tf.test.TestCase):
height, width = 224, 224
num_classes = 1000
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
# Force all Variables to reside on the device.
with tf.compat.v1.variable_scope('on_cpu'), tf.device('/cpu:0'):
with tf.variable_scope('on_cpu'), tf.device('/cpu:0'):
with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
nasnet.build_nasnet_mobile(inputs, num_classes)
with tf.compat.v1.variable_scope('on_gpu'), tf.device('/gpu:0'):
with tf.variable_scope('on_gpu'), tf.device('/gpu:0'):
with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
nasnet.build_nasnet_mobile(inputs, num_classes)
for v in tf.compat.v1.get_collection(
tf.compat.v1.GraphKeys.GLOBAL_VARIABLES, scope='on_cpu'):
for v in tf.get_collection(
tf.GraphKeys.GLOBAL_VARIABLES, scope='on_cpu'):
self.assertDeviceEqual(v.device, '/cpu:0')
for v in tf.compat.v1.get_collection(
tf.compat.v1.GraphKeys.GLOBAL_VARIABLES, scope='on_gpu'):
for v in tf.get_collection(
tf.GraphKeys.GLOBAL_VARIABLES, scope='on_gpu'):
self.assertDeviceEqual(v.device, '/gpu:0')
def testUnknownBatchSizeMobileModel(self):
......@@ -307,13 +305,13 @@ class NASNetTest(tf.test.TestCase):
height, width = 224, 224
num_classes = 1000
with self.test_session() as sess:
inputs = tf.compat.v1.placeholder(tf.float32, (None, height, width, 3))
inputs = tf.placeholder(tf.float32, (None, height, width, 3))
with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
logits, _ = nasnet.build_nasnet_mobile(inputs, num_classes)
self.assertListEqual(logits.get_shape().as_list(),
[None, num_classes])
images = tf.random.uniform((batch_size, height, width, 3))
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(tf.global_variables_initializer())
output = sess.run(logits, {inputs: images.eval()})
self.assertEquals(output.shape, (batch_size, num_classes))
......@@ -328,7 +326,7 @@ class NASNetTest(tf.test.TestCase):
num_classes,
is_training=False)
predictions = tf.argmax(input=logits, axis=1)
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(tf.global_variables_initializer())
output = sess.run(predictions)
self.assertEquals(output.shape, (batch_size,))
......@@ -337,7 +335,7 @@ class NASNetTest(tf.test.TestCase):
height, width = 32, 32
num_classes = 10
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
config = nasnet.cifar_config()
config.set_hparam('data_format', 'NCHW')
with slim.arg_scope(nasnet.nasnet_cifar_arg_scope()):
......@@ -351,7 +349,7 @@ class NASNetTest(tf.test.TestCase):
height, width = 224, 224
num_classes = 1000
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
config = nasnet.mobile_imagenet_config()
config.set_hparam('data_format', 'NCHW')
with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
......@@ -365,7 +363,7 @@ class NASNetTest(tf.test.TestCase):
height, width = 331, 331
num_classes = 1000
inputs = tf.random.uniform((batch_size, height, width, 3))
tf.compat.v1.train.create_global_step()
tf.train.create_global_step()
config = nasnet.large_imagenet_config()
config.set_hparam('data_format', 'NCHW')
with slim.arg_scope(nasnet.nasnet_large_arg_scope()):
......@@ -379,7 +377,7 @@ class NASNetTest(tf.test.TestCase):
height, width = 32, 32
num_classes = 10
inputs = tf.random.uniform((batch_size, height, width, 3))
global_step = tf.compat.v1.train.create_global_step()
global_step = tf.train.create_global_step()
with slim.arg_scope(nasnet.nasnet_cifar_arg_scope()):
logits, end_points = nasnet.build_nasnet_cifar(inputs,
num_classes,
......@@ -398,14 +396,14 @@ class NASNetTest(tf.test.TestCase):
height, width = 32, 32
num_classes = 10
for use_bounded_activation in (True, False):
tf.compat.v1.reset_default_graph()
tf.reset_default_graph()
inputs = tf.random.uniform((batch_size, height, width, 3))
config = nasnet.cifar_config()
config.set_hparam('use_bounded_activation', use_bounded_activation)
with slim.arg_scope(nasnet.nasnet_cifar_arg_scope()):
_, _ = nasnet.build_nasnet_cifar(
inputs, num_classes, config=config)
for node in tf.compat.v1.get_default_graph().as_graph_def().node:
for node in tf.get_default_graph().as_graph_def().node:
if node.op.startswith('Relu'):
self.assertEqual(node.op == 'Relu6', use_bounded_activation)
......
......@@ -31,13 +31,11 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
from tensorflow.contrib import framework as contrib_framework
from tensorflow.contrib import slim as contrib_slim
import tensorflow.compat.v1 as tf
arg_scope = contrib_framework.arg_scope
slim = contrib_slim
import tf_slim as slim
arg_scope = slim.arg_scope
DATA_FORMAT_NCHW = 'NCHW'
DATA_FORMAT_NHWC = 'NHWC'
INVALID = 'null'
......@@ -56,14 +54,14 @@ def calc_reduction_layers(num_cells, num_reduction_layers):
return reduction_layers
@contrib_framework.add_arg_scope
@slim.add_arg_scope
def get_channel_index(data_format=INVALID):
assert data_format != INVALID
axis = 3 if data_format == 'NHWC' else 1
return axis
@contrib_framework.add_arg_scope
@slim.add_arg_scope
def get_channel_dim(shape, data_format=INVALID):
assert data_format != INVALID
assert len(shape) == 4
......@@ -75,7 +73,7 @@ def get_channel_dim(shape, data_format=INVALID):
raise ValueError('Not a valid data_format', data_format)
@contrib_framework.add_arg_scope
@slim.add_arg_scope
def global_avg_pool(x, data_format=INVALID):
"""Average pool away the height and width spatial dimensions of x."""
assert data_format != INVALID
......@@ -87,7 +85,7 @@ def global_avg_pool(x, data_format=INVALID):
return tf.reduce_mean(input_tensor=x, axis=[2, 3])
@contrib_framework.add_arg_scope
@slim.add_arg_scope
def factorized_reduction(net, output_filters, stride, data_format=INVALID):
"""Reduces the shape of net without information loss due to striding."""
assert data_format != INVALID
......@@ -101,8 +99,8 @@ def factorized_reduction(net, output_filters, stride, data_format=INVALID):
stride_spec = [1, 1, stride, stride]
# Skip path 1
path1 = tf.compat.v2.nn.avg_pool2d(
input=net,
path1 = tf.nn.avg_pool2d(
net,
ksize=[1, 1, 1, 1],
strides=stride_spec,
padding='VALID',
......@@ -120,9 +118,8 @@ def factorized_reduction(net, output_filters, stride, data_format=INVALID):
pad_arr = [[0, 0], [0, 0], [0, 1], [0, 1]]
path2 = tf.pad(tensor=net, paddings=pad_arr)[:, :, 1:, 1:]
concat_axis = 1
path2 = tf.compat.v2.nn.avg_pool2d(
input=path2,
path2 = tf.nn.avg_pool2d(
path2,
ksize=[1, 1, 1, 1],
strides=stride_spec,
padding='VALID',
......@@ -138,7 +135,7 @@ def factorized_reduction(net, output_filters, stride, data_format=INVALID):
return final_path
@contrib_framework.add_arg_scope
@slim.add_arg_scope
def drop_path(net, keep_prob, is_training=True):
"""Drops out a whole example hiddenstate with the specified probability."""
if is_training:
......@@ -324,10 +321,10 @@ class NasNetABaseCell(object):
self._filter_size = int(self._num_conv_filters * filter_scaling)
i = 0
with tf.compat.v1.variable_scope(scope):
with tf.variable_scope(scope):
net = self._cell_base(net, prev_layer)
for iteration in range(5):
with tf.compat.v1.variable_scope('comb_iter_{}'.format(iteration)):
with tf.variable_scope('comb_iter_{}'.format(iteration)):
left_hiddenstate_idx, right_hiddenstate_idx = (
self._hiddenstate_indices[i],
self._hiddenstate_indices[i + 1])
......@@ -340,17 +337,17 @@ class NasNetABaseCell(object):
operation_right = self._operations[i+1]
i += 2
# Apply conv operations
with tf.compat.v1.variable_scope('left'):
with tf.variable_scope('left'):
h1 = self._apply_conv_operation(h1, operation_left,
stride, original_input_left,
current_step)
with tf.compat.v1.variable_scope('right'):
with tf.variable_scope('right'):
h2 = self._apply_conv_operation(h2, operation_right,
stride, original_input_right,
current_step)
# Combine hidden states using 'add'.
with tf.compat.v1.variable_scope('combine'):
with tf.variable_scope('combine'):
h = h1 + h2
if self._use_bounded_activation:
h = tf.nn.relu6(h)
......@@ -358,7 +355,7 @@ class NasNetABaseCell(object):
# Add hiddenstate to the list of hiddenstates we can choose from
net.append(h)
with tf.compat.v1.variable_scope('cell_output'):
with tf.variable_scope('cell_output'):
net = self._combine_unused_states(net)
return net
......@@ -419,7 +416,7 @@ class NasNetABaseCell(object):
should_reduce = should_reduce and not used_h
if should_reduce:
stride = 2 if final_height != curr_height else 1
with tf.compat.v1.variable_scope('reduction_{}'.format(idx)):
with tf.variable_scope('reduction_{}'.format(idx)):
net[idx] = factorized_reduction(
net[idx], final_num_filters, stride)
......@@ -431,7 +428,7 @@ class NasNetABaseCell(object):
net = tf.concat(values=states_to_combine, axis=concat_axis)
return net
@contrib_framework.add_arg_scope # No public API. For internal use only.
@slim.add_arg_scope # No public API. For internal use only.
def _apply_drop_path(self, net, current_step=None,
use_summaries=False, drop_connect_version='v3'):
"""Apply drop_path regularization.
......@@ -460,24 +457,23 @@ class NasNetABaseCell(object):
layer_ratio = (self._cell_num + 1)/float(num_cells)
if use_summaries:
with tf.device('/cpu:0'):
tf.compat.v1.summary.scalar('layer_ratio', layer_ratio)
tf.summary.scalar('layer_ratio', layer_ratio)
drop_path_keep_prob = 1 - layer_ratio * (1 - drop_path_keep_prob)
if drop_connect_version in ['v1', 'v3']:
# Decrease the keep probability over time
if current_step is None:
current_step = tf.compat.v1.train.get_or_create_global_step()
current_step = tf.train.get_or_create_global_step()
current_step = tf.cast(current_step, tf.float32)
drop_path_burn_in_steps = self._total_training_steps
current_ratio = current_step / drop_path_burn_in_steps
current_ratio = tf.minimum(1.0, current_ratio)
if use_summaries:
with tf.device('/cpu:0'):
tf.compat.v1.summary.scalar('current_ratio', current_ratio)
tf.summary.scalar('current_ratio', current_ratio)
drop_path_keep_prob = (1 - current_ratio * (1 - drop_path_keep_prob))
if use_summaries:
with tf.device('/cpu:0'):
tf.compat.v1.summary.scalar('drop_path_keep_prob',
drop_path_keep_prob)
tf.summary.scalar('drop_path_keep_prob', drop_path_keep_prob)
net = drop_path(net, drop_path_keep_prob)
return net
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment