Ejemplo n.º 1
0
    def __init__(self, network_path, n_channel=3, n_classes=10, image_size=24):
        self.image_size = image_size

        # 输入变量
        self.images = tf.placeholder(
            dtype=tf.float32, shape=[None, image_size, image_size, n_channel], name='images')
        self.labels = tf.placeholder(
            dtype=tf.int64, shape=[None], name='labels')
        self.keep_prob = tf.placeholder(
            dtype=tf.float32, name='keep_prob')
        self.global_step = tf.Variable( 
            0, dtype=tf.int32, name='global_step')
        
        network_option_path = os.path.join(network_path)
        self.network_option = yaml.load(open(network_option_path, 'r'))
        
        # 网络结构
        print()
        self.conv_lists, self.dense_lists = [], []
        for layer_dict in self.network_option['net']['conv_first']:
            layer = ConvLayer(
                x_size=layer_dict['x_size'], y_size=layer_dict['y_size'], 
                x_stride=layer_dict['x_stride'], y_stride=layer_dict['y_stride'], 
                n_filter=layer_dict['n_filter'], activation=layer_dict['activation'], 
                batch_normal=layer_dict['bn'], weight_decay=1e-4, 
                data_format='channels_last', name=layer_dict['name'], 
                input_shape=(image_size, image_size, n_channel))
            self.conv_lists.append(layer)
        
        for i in range(self.network_option['n_layers']):
            conv_list = []
            for layer_dict in self.network_option['net']['conv_layer%d' % (i+1)]:
                if layer_dict['type'] == 'conv':
                    layer = ConvLayer(
                        x_size=layer_dict['x_size'], y_size=layer_dict['y_size'], 
                        x_stride=layer_dict['x_stride'], y_stride=layer_dict['y_stride'], 
                        n_filter=layer_dict['n_filter'], activation=layer_dict['activation'], 
                        batch_normal=layer_dict['bn'], weight_decay=1e-4, 
                        data_format='channels_last', name=layer_dict['name'], prev_layer=layer)
                conv_list.append(layer)
            self.conv_lists.append(conv_list)
        
        for layer_dict in self.network_option['net']['dense_first']:
            layer = DenseLayer(
                hidden_dim=layer_dict['hidden_dim'], activation=layer_dict['activation'],
                dropout=layer_dict['dropout'], keep_prob=self.keep_prob,
                batch_normal=layer_dict['bn'], weight_decay=1e-4, 
                name=layer_dict['name'],
                input_shape=(int(image_size/8) * int(image_size/8) * 512, ))
            self.dense_lists.append(layer)
        for layer_dict in self.network_option['net']['dense']:
            layer = DenseLayer(
                hidden_dim=layer_dict['hidden_dim'], activation=layer_dict['activation'],
                dropout=layer_dict['dropout'], keep_prob=self.keep_prob,
                batch_normal=layer_dict['bn'], weight_decay=1e-4, 
                name=layer_dict['name'], prev_layer=layer)
            self.dense_lists.append(layer)
        print()

        self._inference(self.images)
Ejemplo n.º 2
0
 def inference(self, images):
     network_option_path = os.path.join('src/network/vgg.yaml')
     self.network_option = yaml.load(open(network_option_path, 'r'))
     
     # 网络结构
     self.layer_lists = []
     for layer_dict in self.network_option['vgg']['conv_first']:
         layer = ConvLayer(
             x_size=layer_dict['x_size'], y_size=layer_dict['y_size'], 
             x_stride=layer_dict['x_stride'], y_stride=layer_dict['y_stride'], 
             n_filter=layer_dict['n_filter'], activation=layer_dict['activation'], 
             batch_normal=layer_dict['bn'], weight_decay=0.0, 
             name=layer_dict['name'], 
             input_shape=(self.image_size, self.image_size, self.n_channel))
         self.layer_lists.append(layer)
     
     for layer_dict in self.network_option['vgg']['conv']:
         if layer_dict['type'] == 'conv':
             layer = ConvLayer(
                 x_size=layer_dict['x_size'], y_size=layer_dict['y_size'], 
                 x_stride=layer_dict['x_stride'], y_stride=layer_dict['y_stride'], 
                 n_filter=layer_dict['n_filter'], activation=layer_dict['activation'], 
                 batch_normal=layer_dict['bn'], weight_decay=0.0, 
                 name=layer_dict['name'], prev_layer=layer)
         elif layer_dict['type'] == 'pool':
             layer = PoolLayer(
                 x_size=layer_dict['x_size'], y_size=layer_dict['y_size'], 
                 x_stride=layer_dict['x_stride'], y_stride=layer_dict['y_stride'], 
                 mode=layer_dict['mode'], resp_normal=False, 
                 name=layer_dict['name'], prev_layer=layer)
         self.layer_lists.append(layer)
     
     for layer_dict in self.network_option['vgg']['dense_first']:
         layer = DenseLayer(
             hidden_dim=layer_dict['hidden_dim'], activation=layer_dict['activation'],
             dropout=layer_dict['dropout'], keep_prob=self.keep_prob,
             batch_normal=layer_dict['bn'], weight_decay=0.0, 
             name=layer_dict['name'],
             input_shape=(int(self.image_size/8) * int(self.image_size/8) * 256, ))
         self.layer_lists.append(layer)
     for layer_dict in self.network_option['vgg']['dense']:
         layer = DenseLayer(
             hidden_dim=layer_dict['hidden_dim'], activation=layer_dict['activation'],
             dropout=layer_dict['dropout'], keep_prob=self.keep_prob,
             batch_normal=layer_dict['bn'], weight_decay=0.0, 
             name=layer_dict['name'], prev_layer=layer)
         self.layer_lists.append(layer)
     
     # 数据流
     hidden_state = images
     for layer in self.layer_lists[0:21]:
         hidden_state = layer.get_output(inputs=hidden_state)
     hidden_state = tf.reshape(hidden_state, (-1, int(self.image_size/8) * int(self.image_size/8) * 256))
     for layer in self.layer_lists[21:]:
         hidden_state = layer.get_output(inputs=hidden_state)
     logits = hidden_state
     
     return logits
Ejemplo n.º 3
0
    def __init__(self, network_path, n_channel=3, n_classes=10, image_size=24):
        # 输入变量
        self.images = tf.placeholder(
            dtype=tf.float32,
            shape=[None, image_size, image_size, n_channel],
            name='images')
        self.labels = tf.placeholder(dtype=tf.int64,
                                     shape=[None],
                                     name='labels')
        self.keep_prob = tf.placeholder(dtype=tf.float32, name='keep_prob')
        self.global_step = tf.Variable(0, dtype=tf.int32, name='global_step')

        network_option_path = os.path.join(network_path)
        self.network_option = yaml.load(open(network_option_path, 'r'))
        # 网络结构
        print()
        self.conv_lists, self.dense_lists = [], []
        for layer_dict in self.network_option['net']['conv_first']:
            layer = ConvLayer(x_size=layer_dict['x_size'],
                              y_size=layer_dict['y_size'],
                              x_stride=layer_dict['x_stride'],
                              y_stride=layer_dict['y_stride'],
                              n_filter=layer_dict['n_filter'],
                              activation=layer_dict['activation'],
                              batch_normal=layer_dict['bn'],
                              weight_decay=1e-4,
                              data_format='channels_last',
                              name=layer_dict['name'],
                              input_shape=(image_size, image_size, n_channel))
            self.conv_lists.append(layer)

        for layer_dict in self.network_option['net']['conv']:
            if layer_dict['type'] == 'conv':
                layer = ConvLayer(x_size=layer_dict['x_size'],
                                  y_size=layer_dict['y_size'],
                                  x_stride=layer_dict['x_stride'],
                                  y_stride=layer_dict['y_stride'],
                                  n_filter=layer_dict['n_filter'],
                                  activation=layer_dict['activation'],
                                  batch_normal=layer_dict['bn'],
                                  weight_decay=1e-4,
                                  data_format='channels_last',
                                  name=layer_dict['name'],
                                  prev_layer=layer)
            elif layer_dict['type'] == 'pool':
                layer = PoolLayer(x_size=layer_dict['x_size'],
                                  y_size=layer_dict['y_size'],
                                  x_stride=layer_dict['x_stride'],
                                  y_stride=layer_dict['y_stride'],
                                  mode=layer_dict['mode'],
                                  resp_normal=False,
                                  data_format='channels_last',
                                  name=layer_dict['name'],
                                  prev_layer=layer)
            self.conv_lists.append(layer)

        for layer_dict in self.network_option['net']['dense_first']:
            layer = DenseLayer(hidden_dim=layer_dict['hidden_dim'],
                               activation=layer_dict['activation'],
                               dropout=layer_dict['dropout'],
                               keep_prob=self.keep_prob,
                               batch_normal=layer_dict['bn'],
                               weight_decay=1e-4,
                               name=layer_dict['name'],
                               input_shape=(int(image_size / 8) *
                                            int(image_size / 8) * 256, ))
            self.dense_lists.append(layer)
        for layer_dict in self.network_option['net']['dense']:
            layer = DenseLayer(hidden_dim=layer_dict['hidden_dim'],
                               activation=layer_dict['activation'],
                               dropout=layer_dict['dropout'],
                               keep_prob=self.keep_prob,
                               batch_normal=layer_dict['bn'],
                               weight_decay=1e-4,
                               name=layer_dict['name'],
                               prev_layer=layer)
            self.dense_lists.append(layer)
        print()

        # 数据流
        hidden_state = self.images
        for layer in self.conv_lists:
            hidden_state = layer.get_output(inputs=hidden_state)
        hidden_state = tf.reshape(
            hidden_state,
            [-1, int(image_size / 8) * int(image_size / 8) * 256])
        for layer in self.dense_lists:
            hidden_state = layer.get_output(inputs=hidden_state)
        logits = hidden_state

        # 目标函数
        self.objective = tf.reduce_mean(
            tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
                                                           labels=self.labels))
        self.avg_loss = self.objective

        # 优化器
        lr = tf.cond(
            tf.less(self.global_step, 50000), lambda: tf.constant(0.001),
            lambda: tf.cond(tf.less(self.global_step, 100000), lambda: tf.
                            constant(0.0001), lambda: tf.constant(0.00001)))
        self.optimizer = tf.train.AdamOptimizer(learning_rate=lr).minimize(
            self.avg_loss, global_step=self.global_step)

        # 观察值
        correct_prediction = tf.equal(self.labels, tf.argmax(logits, 1))
        self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
def ConvNet_init(dataloader=None,
                 n_channel=3,
                 n_classes=10,
                 image_size=24,
                 network_path=None,
                 setting=None):
    # 输入变量
    image_size = image_size
    images = tf.placeholder(dtype=tf.float32,
                            shape=[None, image_size, image_size, n_channel],
                            name='images')
    labels = tf.placeholder(dtype=tf.int64, shape=[None], name='labels')
    keep_prob = tf.placeholder(dtype=tf.float32, name='keep_prob')
    global_step = tf.Variable(0, dtype=tf.int32, name='global_step')
    setting = setting
    batch_size = setting.batch_size
    network_option_path = os.path.join(network_path)
    network_option = yaml.load(open(network_option_path, 'r'))
    dataloader = dataloader

    # 网络结构
    print()
    conv_lists, dense_lists = [], []
    for layer_dict in network_option['net']['conv_first']:
        layer = ConvLayer(x_size=layer_dict['x_size'],
                          y_size=layer_dict['y_size'],
                          x_stride=layer_dict['x_stride'],
                          y_stride=layer_dict['y_stride'],
                          n_filter=layer_dict['n_filter'],
                          activation=layer_dict['activation'],
                          batch_normal=layer_dict['bn'],
                          weight_decay=1e-4,
                          data_format='channels_last',
                          name=layer_dict['name'],
                          input_shape=(image_size, image_size, n_channel))
        conv_lists.append(layer)

    for layer_dict in network_option['net']['conv']:
        if layer_dict['type'] == 'conv':
            layer = ConvLayer(x_size=layer_dict['x_size'],
                              y_size=layer_dict['y_size'],
                              x_stride=layer_dict['x_stride'],
                              y_stride=layer_dict['y_stride'],
                              n_filter=layer_dict['n_filter'],
                              activation=layer_dict['activation'],
                              batch_normal=layer_dict['bn'],
                              weight_decay=1e-4,
                              data_format='channels_last',
                              name=layer_dict['name'],
                              prev_layer=layer)
        elif layer_dict['type'] == 'pool':
            layer = PoolLayer(x_size=layer_dict['x_size'],
                              y_size=layer_dict['y_size'],
                              x_stride=layer_dict['x_stride'],
                              y_stride=layer_dict['y_stride'],
                              mode=layer_dict['mode'],
                              resp_normal=False,
                              data_format='channels_last',
                              name=layer_dict['name'],
                              prev_layer=layer)
        conv_lists.append(layer)

    for layer_dict in network_option['net']['dense_first']:
        layer = DenseLayer(hidden_dim=layer_dict['hidden_dim'],
                           activation=layer_dict['activation'],
                           dropout=layer_dict['dropout'],
                           keep_prob=keep_prob,
                           batch_normal=layer_dict['bn'],
                           weight_decay=1e-4,
                           name=layer_dict['name'],
                           input_shape=(int(image_size / 8) *
                                        int(image_size / 8) * 256, ))
        dense_lists.append(layer)
    for layer_dict in network_option['net']['dense']:
        layer = DenseLayer(hidden_dim=layer_dict['hidden_dim'],
                           activation=layer_dict['activation'],
                           dropout=layer_dict['dropout'],
                           keep_prob=keep_prob,
                           batch_normal=layer_dict['bn'],
                           weight_decay=1e-4,
                           name=layer_dict['name'],
                           prev_layer=layer)
        dense_lists.append(layer)
    print()

    # 数据流
    scope_name = 'first'

    generate_first_dict = generate_init_dataflow(scope_name)
Ejemplo n.º 5
0
    def __init__(self, n_channel=3, n_classes=10, image_size=24):
        # 输入变量
        self.images = tf.placeholder(
            dtype=tf.float32,
            shape=[None, image_size, image_size, n_channel],
            name='images')
        self.labels = tf.placeholder(dtype=tf.int64,
                                     shape=[None],
                                     name='labels')
        self.keep_prob = tf.placeholder(dtype=tf.float32, name='keep_prob')
        self.global_step = tf.Variable(0, dtype=tf.int32, name='global_step')

        # 网络结构
        print()
        conv_layer1 = ConvLayer(y_size=3,
                                x_size=3,
                                y_stride=1,
                                x_stride=1,
                                n_filter=64,
                                activation='relu',
                                data_format='channels_last',
                                batch_normal=True,
                                weight_decay=1e-4,
                                input_shape=(image_size, image_size,
                                             n_channel),
                                name='conv1')
        pool_layer1 = PoolLayer(y_size=2,
                                x_size=2,
                                y_stride=2,
                                x_stride=2,
                                input_shape=(image_size, image_size, 64),
                                data_format='channels_last',
                                mode='max',
                                resp_normal=False,
                                name='pool1')

        conv_layer2 = ConvLayer(y_size=3,
                                x_size=3,
                                y_stride=1,
                                x_stride=1,
                                n_filter=128,
                                activation='relu',
                                data_format='channels_last',
                                batch_normal=True,
                                weight_decay=1e-4,
                                input_shape=(int(image_size / 2),
                                             int(image_size / 2), 64),
                                name='conv2')
        pool_layer2 = PoolLayer(y_size=2,
                                x_size=2,
                                y_stride=2,
                                x_stride=2,
                                input_shape=(int(image_size / 2),
                                             int(image_size / 2), 128),
                                data_format='channels_last',
                                mode='max',
                                resp_normal=False,
                                name='pool2')

        conv_layer3 = ConvLayer(y_size=3,
                                x_size=3,
                                y_stride=1,
                                x_stride=1,
                                n_filter=256,
                                activation='relu',
                                data_format='channels_last',
                                batch_normal=True,
                                weight_decay=1e-4,
                                input_shape=(int(image_size / 4),
                                             int(image_size / 4), 128),
                                name='conv3')
        pool_layer3 = PoolLayer(y_size=2,
                                x_size=2,
                                y_stride=2,
                                x_stride=2,
                                input_shape=(int(image_size / 4),
                                             int(image_size / 4), 256),
                                data_format='channels_last',
                                mode='max',
                                resp_normal=False,
                                name='pool3')

        dense_layer1 = DenseLayer(input_shape=(int(image_size / 8) *
                                               int(image_size / 8) * 256, ),
                                  hidden_dim=1024,
                                  activation='relu',
                                  batch_normal=True,
                                  dropout=True,
                                  keep_prob=self.keep_prob,
                                  weight_decay=1e-4,
                                  name='dense1')

        dense_layer2 = DenseLayer(input_shape=(1024, ),
                                  hidden_dim=n_classes,
                                  activation='none',
                                  batch_normal=False,
                                  dropout=False,
                                  keep_prob=None,
                                  weight_decay=1e-4,
                                  name='dense2')
        print()

        # 数据流
        hidden_conv1 = conv_layer1.get_output(inputs=self.images)
        hidden_pool1 = pool_layer1.get_output(inputs=hidden_conv1)
        hidden_conv2 = conv_layer2.get_output(inputs=hidden_pool1)
        hidden_pool2 = pool_layer2.get_output(inputs=hidden_conv2)
        hidden_conv3 = conv_layer3.get_output(inputs=hidden_pool2)
        hidden_pool3 = pool_layer3.get_output(inputs=hidden_conv3)
        input_dense1 = tf.reshape(
            hidden_pool3,
            [-1, int(image_size / 8) * int(image_size / 8) * 256])
        output_dense1 = dense_layer1.get_output(inputs=input_dense1)
        logits = dense_layer2.get_output(inputs=output_dense1)

        # 目标函数
        self.objective = tf.reduce_mean(
            tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
                                                           labels=self.labels))
        self.avg_loss = self.objective

        # 优化器
        lr = tf.cond(
            tf.less(self.global_step, 50000), lambda: tf.constant(0.001),
            lambda: tf.cond(tf.less(self.global_step, 100000), lambda: tf.
                            constant(0.0001), lambda: tf.constant(0.00001)))
        self.optimizer = tf.train.AdamOptimizer(learning_rate=lr).minimize(
            self.avg_loss, global_step=self.global_step)

        # 观察值
        correct_prediction = tf.equal(self.labels, tf.argmax(logits, 1))
        self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
Ejemplo n.º 6
0
def ConvNet(network_path,
            n_channel=3,
            n_classes=10,
            image_size=24,
            name_scope=None):

    with tf.variable_scope(name_scope):
        # 输入变量
        net_dict = {}
        images_placeholder = tf.placeholder(
            dtype=tf.float32,
            shape=[None, image_size, image_size, n_channel],
            name='images')
        labels_placeholder = tf.placeholder(dtype=tf.int64,
                                            shape=[None],
                                            name='labels')
        keep_prob_placeholder = tf.placeholder(dtype=tf.float32,
                                               name='keep_prob')
        global_step_Variable = tf.Variable(0,
                                           dtype=tf.int32,
                                           name='global_step')

        network_option_path = os.path.join(network_path)
        network_option = yaml.load(open(network_option_path, 'r'))
        # 网络结构
        print()
        conv_lists, dense_lists = [], []
        for layer_dict in network_option['net']['conv_first']:
            layer = ConvLayer(x_size=layer_dict['x_size'],
                              y_size=layer_dict['y_size'],
                              x_stride=layer_dict['x_stride'],
                              y_stride=layer_dict['y_stride'],
                              n_filter=layer_dict['n_filter'],
                              activation=layer_dict['activation'],
                              batch_normal=layer_dict['bn'],
                              weight_decay=1e-4,
                              data_format='channels_last',
                              name=layer_dict['name'],
                              input_shape=(image_size, image_size, n_channel))
            conv_lists.append(layer)

        for layer_dict in network_option['net']['conv']:
            if layer_dict['type'] == 'conv':
                layer = ConvLayer(x_size=layer_dict['x_size'],
                                  y_size=layer_dict['y_size'],
                                  x_stride=layer_dict['x_stride'],
                                  y_stride=layer_dict['y_stride'],
                                  n_filter=layer_dict['n_filter'],
                                  activation=layer_dict['activation'],
                                  batch_normal=layer_dict['bn'],
                                  weight_decay=1e-4,
                                  data_format='channels_last',
                                  name=layer_dict['name'],
                                  prev_layer=layer)
            elif layer_dict['type'] == 'pool':
                layer = PoolLayer(x_size=layer_dict['x_size'],
                                  y_size=layer_dict['y_size'],
                                  x_stride=layer_dict['x_stride'],
                                  y_stride=layer_dict['y_stride'],
                                  mode=layer_dict['mode'],
                                  resp_normal=False,
                                  data_format='channels_last',
                                  name=layer_dict['name'],
                                  prev_layer=layer)
            conv_lists.append(layer)

        for layer_dict in network_option['net']['dense_first']:
            layer = DenseLayer(hidden_dim=layer_dict['hidden_dim'],
                               activation=layer_dict['activation'],
                               dropout=layer_dict['dropout'],
                               keep_prob=keep_prob_placeholder,
                               batch_normal=layer_dict['bn'],
                               weight_decay=1e-4,
                               name=layer_dict['name'],
                               input_shape=(int(image_size / 8) *
                                            int(image_size / 8) * 256, ))
            dense_lists.append(layer)
        for layer_dict in network_option['net']['dense']:
            layer = DenseLayer(hidden_dim=layer_dict['hidden_dim'],
                               activation=layer_dict['activation'],
                               dropout=layer_dict['dropout'],
                               keep_prob=keep_prob_placeholder,
                               batch_normal=layer_dict['bn'],
                               weight_decay=1e-4,
                               name=layer_dict['name'],
                               prev_layer=layer)
            dense_lists.append(layer)
        print()

        # 数据流
        hidden_state = images_placeholder
        for layer in conv_lists:
            hidden_state = layer.get_output(inputs=hidden_state)
        hidden_state = tf.reshape(
            hidden_state,
            [-1, int(image_size / 8) * int(image_size / 8) * 256])
        for layer in dense_lists:
            hidden_state = layer.get_output(inputs=hidden_state)
        logits = hidden_state

        # 目标函数

        objective = tf.reduce_mean(
            tf.nn.sparse_softmax_cross_entropy_with_logits(
                logits=logits, labels=labels_placeholder))
        avg_loss_op = objective

        # 优化器
        lr = tf.cond(
            tf.less(global_step_Variable, 20000), lambda: tf.constant(0.01),
            lambda: tf.cond(tf.less(global_step_Variable, 40000), lambda: tf.
                            constant(0.0001), lambda: tf.constant(0.00001)))
        optimizer_op = tf.train.AdamOptimizer(learning_rate=lr).minimize(
            avg_loss_op, global_step=global_step_Variable)

        # 观察值
        correct_prediction = tf.equal(labels_placeholder, tf.argmax(logits, 1))
        accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, 'float'))

        wrong_idx_op = tf.where(correct_prediction)

        net_dict['images_placeholder'] = images_placeholder
        net_dict['labels_placeholder'] = labels_placeholder
        net_dict['keep_prob_placeholder'] = keep_prob_placeholder
        net_dict['avg_loss_op'] = avg_loss_op
        net_dict['optimizer_op'] = optimizer_op
        net_dict['accuracy_op'] = accuracy_op
        net_dict['wrong_idx_op'] = wrong_idx_op
        net_dict['global_step_Variable'] = global_step_Variable
        return net_dict