Beispiel #1
0
    def __call__(self, i, reuse=False):

        act_fn = get_activation(self.config.get('activation', 'relu'),
                                self.config.get('activation_params', {}))

        norm_fn, norm_params = get_normalization(
            self.config.get('batch_norm', 'batch_norm'),
            self.config.get('batch_norm_params', self.normalizer_params))

        winit_fn = get_weightsinit(
            self.config.get('weightsinit', 'normal'),
            self.config.get('weightsinit_params', '0.00 0.02'))

        filters = self.config.get('nb_filters', 32)

        # fully connected parameters
        including_top = self.config.get("including top", True)
        nb_fc_nodes = self.config.get('nb_fc_nodes', [1024, 1024])

        # output stage parameters
        output_dims = self.config.get("output dims",
                                      0)  # zero for no output layer
        output_act_fn = get_activation(
            self.config.get('output_activation', 'none'),
            self.config.get('output_activation_params', ''))

        with tf.variable_scope(self.name):
            if reuse:
                tf.get_variable_scope().reuse_variables()
            else:
                assert tf.get_variable_scope().reuse is False

            end_points = {}

            # x : 299 * 299 * 3
            x = tcl.conv2d(i,
                           filters,
                           3,
                           stride=2,
                           activation_fn=act_fn,
                           normalizer_fn=norm_fn,
                           normalizer_params=norm_params,
                           padding='VALID',
                           weights_initializer=winit_fn,
                           scope='conv1')
            end_points['conv1'] = x

            # x : 149 * 149 * 32
            x = tcl.conv2d(x,
                           filters,
                           3,
                           stride=1,
                           activation_fn=act_fn,
                           normalizer_fn=norm_fn,
                           normalizer_params=norm_params,
                           padding='VALID',
                           weights_initializer=winit_fn,
                           scope='conv2')
            end_points['conv2'] = x

            # x : 147 * 147 * 32
            x = tcl.conv2d(x,
                           2 * filters,
                           3,
                           stride=1,
                           activation_fn=act_fn,
                           normalizer_fn=norm_fn,
                           normalizer_params=norm_params,
                           padding='SAME',
                           weights_initializer=winit_fn,
                           scope='conv3')
            end_points['conv3'] = x

            # x : 147 * 147 * 64
            x = tcl.max_pool2d(x, 3, stride=2, padding='VALID', scope='pool1')
            end_points['pool1'] = x

            # x : 73 * 73 * 64
            x = tcl.conv2d(x,
                           80,
                           3,
                           stride=1,
                           activation_fn=act_fn,
                           normalizer_fn=norm_fn,
                           normalizer_params=norm_params,
                           padding='VALID',
                           weights_initializer=winit_fn,
                           scope='conv4')
            end_points['conv4'] = x

            # x : 71 * 71 * 80
            x = tcl.conv2d(x,
                           192,
                           3,
                           stride=2,
                           activation_fn=act_fn,
                           normalizer_fn=norm_fn,
                           normalizer_params=norm_params,
                           padding='VALID',
                           weights_initializer=winit_fn,
                           scope='conv5')
            end_points['conv5'] = x

            # x : 35 * 35 * 192
            x = tcl.conv2d(x,
                           288,
                           3,
                           stride=1,
                           activation_fn=act_fn,
                           normalizer_fn=norm_fn,
                           normalizer_params=norm_params,
                           padding='SAME',
                           weights_initializer=winit_fn,
                           scope='conv6')
            end_points['conv6'] = x

            # x : 35 * 35 * 288
            x, end_points = inception_v3_figure5('inception1a',
                                                 x,
                                                 end_points,
                                                 act_fn=act_fn,
                                                 norm_fn=norm_fn,
                                                 norm_params=norm_params,
                                                 winit_fn=winit_fn)
            x, end_points = inception_v3_figure5('inception1b',
                                                 x,
                                                 end_points,
                                                 act_fn=act_fn,
                                                 norm_fn=norm_fn,
                                                 norm_params=norm_params,
                                                 winit_fn=winit_fn)
            x, end_points = inception_v3_figure5_downsample(
                'inception1c',
                x,
                end_points,
                act_fn=act_fn,
                norm_fn=norm_fn,
                norm_params=norm_params,
                winit_fn=winit_fn)

            # x : 17 * 17 * 768
            x, end_points = inception_v3_figure6('inception2a',
                                                 x,
                                                 end_points,
                                                 n=7,
                                                 act_fn=act_fn,
                                                 norm_fn=norm_fn,
                                                 norm_params=norm_params,
                                                 winit_fn=winit_fn)
            x, end_points = inception_v3_figure6('inception2b',
                                                 x,
                                                 end_points,
                                                 n=7,
                                                 act_fn=act_fn,
                                                 norm_fn=norm_fn,
                                                 norm_params=norm_params,
                                                 winit_fn=winit_fn)
            x, end_points = inception_v3_figure6('inception2c',
                                                 x,
                                                 end_points,
                                                 n=7,
                                                 act_fn=act_fn,
                                                 norm_fn=norm_fn,
                                                 norm_params=norm_params,
                                                 winit_fn=winit_fn)
            x, end_points = inception_v3_figure6('inception2d',
                                                 x,
                                                 end_points,
                                                 n=7,
                                                 act_fn=act_fn,
                                                 norm_fn=norm_fn,
                                                 norm_params=norm_params,
                                                 winit_fn=winit_fn)
            x, end_points = inception_v3_figure6_downsample(
                'inception2e',
                x,
                end_points,
                n=7,
                act_fn=act_fn,
                norm_fn=norm_fn,
                norm_params=norm_params,
                winit_fn=winit_fn)

            # x : 8 * 8 * 1280
            x, end_points = inception_v3_figure7('inception3a',
                                                 x,
                                                 end_points,
                                                 act_fn=act_fn,
                                                 norm_fn=norm_fn,
                                                 norm_params=norm_params,
                                                 winit_fn=winit_fn)
            x, end_points = inception_v3_figure7('inception3b',
                                                 x,
                                                 end_points,
                                                 act_fn=act_fn,
                                                 norm_fn=norm_fn,
                                                 norm_params=norm_params,
                                                 winit_fn=winit_fn)

            # construct top fully connected layer
            if including_top:
                with tf.variable_scope("logits"):
                    x = tcl.avg_pool2d(x,
                                       kernel_size=[8, 8],
                                       padding="VALID",
                                       scope="avgpool_1a_8x8")
                    x = tcl.dropout(x, keep_prob=0.5, scope="dropout_1b")
                    end_points["global_avg_pooling"] = x
                    x = tcl.flatten(x)

                    if output_dims != 0:
                        x = tcl.fully_connected(x,
                                                output_dims,
                                                activation_fn=output_act_fn,
                                                normalizer_fn=None,
                                                weights_initializer=winit_fn,
                                                scope='fc_out')
                        end_points['fc_out'] = x

            return x, end_points
Beispiel #2
0
    def deconv2d(self,
                 name,
                 x,
                 nb_filters,
                 ksize,
                 stride,
                 *,
                 norm_fn='none',
                 norm_params=None,
                 act_fn='relu',
                 winit_fn='xavier',
                 binit_fn='zeros',
                 padding='SAME',
                 has_bias=True,
                 disp=True,
                 collect_end_points=True):

        if callable(act_fn):
            act_fn_str = 'func'
            act_fn = act_fn
        else:
            act_fn_str = self.config.get(name + ' activation', act_fn)
            act_fn = get_activation(act_fn_str)

        if callable(norm_fn):
            norm_fn_str = 'func'
            norm_fn = norm_fn
        else:
            norm_fn_str = self.config.get(name + ' normalization', norm_fn)
            norm_fn = get_normalization(norm_fn_str)

        winit_fn_str = self.config.get(name + ' weightsinit', winit_fn)
        if 'special' in winit_fn_str:
            split = winit_fn_str.split()
            winit_name = split[0]
            if winit_name == 'he_uniform':
                input_nb_filters = int(x.get_shape()[-1])
                fan_in = input_nb_filters * (ksize**2) / (stride**2)
                fan_out = nb_filters * (ksize**2)
                filters_stdev = np.sqrt(4.0 / (fan_in + fan_out))
                winit_fn = self.uniform_initializer(filters_stdev)
            else:
                raise Exception('Error weights initializer function name : ' +
                                winit_fn_str)
        else:
            winit_fn = get_weightsinit(winit_fn_str)
        binit_fn_str = self.config.get(name + ' biasesinit', binit_fn)
        binit_fn = get_weightsinit(binit_fn_str)
        _padding = self.config.get(name + ' padding', padding)

        if self.using_tcl_library:
            x = tcl.conv2d_transpose(x,
                                     nb_filters,
                                     ksize,
                                     stride=stride,
                                     use_bias=True,
                                     activation_fn=act_fn,
                                     normalizer_fn=norm_fn,
                                     normalizer_params=norm_params,
                                     weights_initializer=winit_fn,
                                     padding=_padding,
                                     scope=name)
        else:
            x = tl.conv2d_transpose(x,
                                    nb_filters,
                                    ksize,
                                    strides=stride,
                                    padding=_padding,
                                    use_bias=has_bias,
                                    kernel_initializer=winit_fn,
                                    bias_initializer=binit_fn,
                                    trainable=True,
                                    name=name)
            with tf.variable_scope(name):
                if norm_fn is not None:
                    norm_params = norm_params or {}
                    x = norm_fn(x, **norm_params)
                if act_fn is not None:
                    x = act_fn(x)

        if disp:
            print('\t\tDeonv2D(' + str(name) + ') --> ', x.get_shape(), '  ',
                  (act_fn_str, norm_fn_str, winit_fn_str, _padding))
        if collect_end_points:
            self.end_points[name] = x
        return x
Beispiel #3
0
    def fc(self,
           name,
           x,
           nb_nodes,
           *,
           norm_fn='none',
           norm_params=None,
           act_fn='none',
           winit_fn='xavier',
           binit_fn='zeros',
           has_bias=True,
           disp=True,
           collect_end_points=True):

        if callable(act_fn):
            act_fn_str = 'func'
            act_fn = act_fn
        else:
            act_fn_str = self.config.get(name + ' activation', act_fn)
            act_fn = get_activation(act_fn_str)

        if callable(norm_fn):
            norm_fn_str = 'func'
            norm_fn = norm_fn
        else:
            norm_fn_str = self.config.get(name + ' normalization', norm_fn)
            norm_fn = get_normalization(norm_fn_str)

        winit_fn_str = self.config.get(name + ' weightsinit', winit_fn)
        if 'special' in winit_fn_str:
            split = winit_fn_str.split()
            winit_name = split[0]
            if winit_name == 'glorot_uniform':
                input_nb_nodes = int(x.get_shape()[-1])
                filters_stdev = np.sqrt(2.0 / (input_nb_nodes + nb_nodes))
                winit_fn = self.uniform_initializer(filters_stdev)
            else:
                raise Exception('Error weights initializer function name : ' +
                                winit_fn_str)
        else:
            winit_fn = get_weightsinit(winit_fn_str)

        binit_fn_str = self.config.get(name + ' biasesinit', binit_fn)
        binit_fn = get_weightsinit(binit_fn_str)

        if self.using_tcl_library:
            x = tcl.fully_connected(x,
                                    nb_nodes,
                                    activation_fn=act_fn,
                                    normalizer_fn=norm_fn,
                                    normalizer_params=norm_params,
                                    weights_initializer=winit_fn,
                                    scope=name)
        else:
            x = tl.dense(x,
                         nb_nodes,
                         use_bias=has_bias,
                         kernel_initializer=winit_fn,
                         bias_initializer=binit_fn,
                         trainable=True,
                         name=name)

            with tf.variable_scope(name):
                if norm_fn is not None:
                    norm_params = norm_params or {}
                    x = norm_fn(x, **norm_params)
                if act_fn is not None:
                    x = act_fn(x)

        if disp:
            print('\t\tFC(' + str(name) + ') --> ', x.get_shape(), '  ',
                  (act_fn_str, norm_fn_str, winit_fn_str))
        if collect_end_points:
            self.end_points[name] = x
        return x
Beispiel #4
0
    def __call__(self, i, condition=None):

        act_fn = get_activation(self.config.get('activation', 'relu'))

        norm_fn, norm_params = get_normalization(
            self.config.get('batch_norm', 'batch_norm'),
            self.config.get('batch_norm_params', self.normalizer_params))

        winit_fn = get_weightsinit(
            self.config.get('weightsinit', 'normal 0.00 0.02'))

        nb_fc_nodes = self.config.get('nb_fc_nodes', [1024, 1024])

        output_dims = self.config.get("output dims", 3)
        output_act_fn = get_activation(
            self.config.get('output_activation', 'none'))

        x, end_points = self.network(i)

        x = tcl.flatten(x)
        if condition is not None:
            x = tf.concatenate([x, condition], axis=-1)

        with tf.variable_scope(self.name):
            if self.reuse:
                tf.get_variable_scope().reuse_variables()
            else:
                assert tf.get_variable_scope().reuse is False
                self.reuse = True

            for ind, nb_nodes in enumerate(nb_fc_nodes):
                x = tcl.fully_connected(x,
                                        nb_nodes,
                                        activation_fn=act_fn,
                                        normalizer_fn=norm_fn,
                                        normalizer_params=norm_params,
                                        weights_initializer=winit_fn,
                                        scope='fc%d' % ind)

            if self.output_distribution == 'gaussian':
                mean = tcl.fully_connected(x,
                                           output_dims,
                                           activation_fn=output_act_fn,
                                           weights_initializer=winit_fn,
                                           scope='fc_out_mean')
                log_var = tcl.fully_connected(x,
                                              output_dims,
                                              activation_fn=output_act_fn,
                                              weights_initializer=winit_fn,
                                              scope='fc_out_log_var')
                return mean, log_var

            elif self.output_distribution == 'mean':
                mean = tcl.fully_connected(x,
                                           output_dims,
                                           activation_fn=output_act_fn,
                                           weights_initializer=winit_fn,
                                           scope='fc_out_mean')
                return mean

            elif self.output_distribution == 'none':
                out = tcl.fully_connected(x,
                                          output_dims,
                                          activation_fn=output_act_fn,
                                          weights_initializer=winit_fn,
                                          scope='fc_out_mean')
                return out
            else:
                raise Exception("None output distribution named " +
                                self.output_distribution)