tensorflow复习

1.数据读取 tfrecord

  • 调用 tf.train.slice_input_producer,从 本地文件里抽取tensor,准备放入Filename Queue(文件名队列)中;

  • 调用 tf.train.batch,从文件名队列中提取tensor,使用单个或多个线程,准备放入文件队列;
  • 调用 tf.train.Coordinator() 来创建一个线程协调器,用来管理之后在Session中启动的所有线程;
  • 调用tf.train.start_queue_runners, 启动入队线程,由多个或单个线程,按照设定规则,把文件读入Filename Queue中。函数返回线程ID的列表,一般情况下,系统有多少个核,就会启动多少个入队线程(入队具体使用多少个线程在tf.train.batch中定义);
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
if is_train:
    while not coord.should_stop():
        i +=1
        if i < 2000:
            lrn_rate = 0.01
        elif i < 4000:
            _lrn_rate = 0.001
        else:
            _lrn_rate = 0.0001
        kp = 0.5
        sess.run(train_step,feed_dict={lr:lrn_rate,keep_prob:kp})

2.  通过placeholder

      train_files = []

      train_file_path = "train_filename.txt"

      with open(train_file_path) as file:
             for line in file.readlines():
                    root_name = line.split('.')[0]
                    train_files.append(root_name)

      total_train_images = train_files

      total_train_length = len(total_train_images)

      start = total_train_index
      end = total_train_index + batch_size

      for i in range(start, end):
            if i == total_train_length:
                np.random.shuffle(total_train_images)
                train_epoch +=1

           idx = i % total_train_length
           image_name = total_train_images[idx]
           image_path = os.path.join(origin_data_folder, image_name+'.jpg')
           ground_truth_path = os.path.join(ground_truth_folder, image_name+'.mat')
           image_paths.append((image_path, ground_truth_path))

      for i in range(num_epochs):
            data, labels = load_batch(batch_size=batch_size, path_func=get_images_labels)

           global_step = lesion_net.train_step(batch_data=data, batch_labels=labels)

       def train_step(self, batch_data, batch_labels):
            feed_dict = {
                self.data: batch_data,
                self.ground_truth: batch_labels
            }
            _, loss, global_loss, global_step, summaries = \
                   self.sess.run([self.train_op, self.mse, self.global_loss,
                                         self.global_step, self.train_summaries_merge], feed_dict=feed_dict)

            with tf.name_scope('Data'):
                self.data = tf.placeholder(shape=[None, 1024, 1024, 3], dtype=tf.float32)
                self.ground_truth = tf.placeholder(shape=[None, 1024, 1024, 1], dtype=tf.float32)

3. loss and summary

        with tf.name_scope('LesionTrain'):
            self.mse = tf.reduce_mean(tf.square(lesion_map - self.ground_truth))

            regularization_losses = tf.losses.get_regularization_losses('Lesion')
            self.regularization_losses = tf.get_collection(key=tf.GraphKeys.REGULARIZATION_LOSSES, scope='Lesion')
            print(self.regularization_losses)
            self.global_loss = tf.add_n([self.mse] + self.regularization_losses, 'global_loss')

            # only trainable variables
            self.train_vars = tf.get_collection(key=tf.GraphKeys.TRAINABLE_VARIABLES, scope='Lesion')
            # for train_var in self.train_vars:
            #     print(train_var)
            self.global_step = tf.Variable(0, trainable=False)

            # 31,000 - 49,500
            learning_rate = 0.0001
            optimizer = tf.train.AdagradOptimizer(learning_rate=learning_rate)
            self.train_op = optimizer.minimize(var_list=self.train_vars,
                                               loss=self.mse, global_step=self.global_step)

            # add summaries
            tf.summary.scalar('learning_rate', learning_rate)
            tf.summary.scalar('mse', self.mse)
            tf.summary.scalar('loss', self.global_loss)

        # merge all summaries and write to file
        self.train_summaries_merge = tf.summary.merge_all()
        self.summary_writer = tf.summary.FileWriter('Summaries', graph=self.sess.graph)

4.       if pretrained_model:
                print('Restoring pretrained model: %s' % pretrained_model)
                saver.restore(sess, pretrained_model)

5.    new_layer_names = ['InceptionResnetV1/Bottleneck/','InceptionResnetV1/tf_add']

        newlayer_need_update=[]
        oldlayer_need_restore = []
        # Create a saver
        for var in tf.trainable_variables:
            if any( elem in var.name for elem in new_layer_names):
                newlayer_need_update.append(var)
            else:
                oldlayer_need_restore.append(var)
        loader = tf.train.Saver
(var_list =oldlayer_need_restore)

        saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)

6.    Model

        print('Model directory: %s' % model_exp)
        meta_file, ckpt_file = get_model_filenames(model_exp)

        print('Metagraph file: %s' % meta_file)
        print('Checkpoint file: %s' % ckpt_file)

        saver = tf.train.import_meta_graph(os.path.join(model_exp, meta_file), input_map=input_map)
        saver.restore(session, os.path.join(model_exp, ckpt_file))

7.   variable

        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        variable_arrary = [var for var in tf.global_variables()]
        variablevalues = sess.run(variable_arrary)
        variable_names = [v.name for v in tf.global_variables()]
        for k, m  in zip(variable_names,variablevalues):
            print("Variable: ", k)
            print("Shape: ", m.shape)
            print()

8.      statistics

          print('Saving statistics')
          with h5py.File(stat_file_name, 'w') as f:
                  for key, value in stat.iteritems():
                        f.create_dataset(key, data=value)

API

9.tf.nn

# def conv2d(name, l_input, w, b):
#     return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(l_input, w, strides=[1, 1, 1, 1], padding='SAME'),b), name=name)
#
# # 最大下采样操作
# def max_pool(name, l_input, k):
#     return tf.nn.max_pool(l_input, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME', name=name)
#
# # 归一化操作
# def norm(name, l_input, lsize=4):
#     return tf.nn.lrn(l_input, lsize, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name=name)
#
# weights = {
#     'wc1': tf.Variable(tf.random_normal([3, 3, 3, 64])),
#     'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128])),
#     'wc3': tf.Variable(tf.random_normal([3, 3, 128, 256])),
#     'wd1': tf.Variable(tf.random_normal([4*4*256, 1024])),
#     'wd2': tf.Variable(tf.random_normal([1024, 1024])),
#     'out': tf.Variable(tf.random_normal([1024, 2]))
# }
def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)
def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)
def print_activations(t):
    print(t.op.name, ' ', t.get_shape().as_list())
def conv_op(input_op, name, kh, kw, n_out, dh, dw):
    input_op = tf.convert_to_tensor(input_op)
    n_in = input_op.get_shape()[-1].value
    with tf.name_scope(name) as scope:
        kernel = tf.get_variable(scope + "w",
                                 shape=[kh, kw, n_in, n_out],
                                 dtype=tf.float32,
                                 initializer=tf.contrib.layers.xavier_initializer_conv2d())
        conv = tf.nn.conv2d(input_op, kernel, (1, dh, dw, 1), padding='SAME')
        bias_init_val = tf.constant(0.0, shape=[n_out], dtype=tf.float32)
        biases = tf.Variable(bias_init_val, trainable=True, name='b')
        z = tf.nn.bias_add(conv, biases)
        activation = tf.nn.relu(z, name=scope)
        return activation
def _conv(self, name, x, filter_size, in_filters, out_filters, strides):
  """Convolution."""
  with tf.variable_scope(name):
    n = filter_size * filter_size * out_filters
    kernel = tf.get_variable(
        'DW', [filter_size, filter_size, in_filters, out_filters],
        tf.float32, initializer=tf.random_normal_initializer(
            stddev=np.sqrt(2.0/n)))
    return tf.nn.conv2d(x, kernel, strides, padding='SAME')

def _relu(self, x, leakiness=0.0):
  """Relu, with optional leaky support."""
  return tf.where(tf.less(x, 0.0), leakiness * x, x, name='leaky_relu')

def _fully_connected(self, x, out_dim):
  """FullyConnected layer for final output."""
  x = tf.reshape(x, [self.hps.batch_size, -1])
  w = tf.get_variable(
      'DW', [x.get_shape()[1], out_dim],
      initializer=tf.uniform_unit_scaling_initializer(factor=1.0))
  b = tf.get_variable('biases', [out_dim],
                      initializer=tf.constant_initializer())
  return tf.nn.xw_plus_b(x, w, b)
def _batch_norm(self, name, x):
  """Batch normalization."""
  with tf.variable_scope(name):
    params_shape = [x.get_shape()[-1]]

    beta = tf.get_variable(
        'beta', params_shape, tf.float32,
        initializer=tf.constant_initializer(0.0, tf.float32))
    gamma = tf.get_variable(
        'gamma', params_shape, tf.float32,
        initializer=tf.constant_initializer(1.0, tf.float32))

    if self.mode == 'train':
      mean, variance = tf.nn.moments(x, [0, 1, 2], name='moments')

      moving_mean = tf.get_variable(
          'moving_mean', params_shape, tf.float32,
          initializer=tf.constant_initializer(0.0, tf.float32),
          trainable=False)
      moving_variance = tf.get_variable(
          'moving_variance', params_shape, tf.float32,
          initializer=tf.constant_initializer(1.0, tf.float32),
          trainable=False)

      self._extra_train_ops.append(moving_averages.assign_moving_average(
          moving_mean, mean, 0.9))
      self._extra_train_ops.append(moving_averages.assign_moving_average(
          moving_variance, variance, 0.9))
    else:
      mean = tf.get_variable(
          'moving_mean', params_shape, tf.float32,
          initializer=tf.constant_initializer(0.0, tf.float32),
          trainable=False)
      variance = tf.get_variable(
          'moving_variance', params_shape, tf.float32,
          initializer=tf.constant_initializer(1.0, tf.float32),
          trainable=False)
      tf.summary.histogram(mean.op.name, mean)
      tf.summary.histogram(variance.op.name, variance)
    # epsilon used to be 1e-5. Maybe 0.001 solves NaN problem in deeper net.
    y = tf.nn.batch_normalization(
        x, mean, variance, beta, gamma, 0.001)
    y.set_shape(x.get_shape())
    return y

slim API

                # 149 x 149 x 32
                net = slim.conv2d(inputs, 32, 3, stride=2, padding='VALID',
                                  scope='Conv2d_1a_3x3')
                end_points['Conv2d_1a_3x3'] = net
                # 147 x 147 x 32
                net = slim.conv2d(net, 32, 3, padding='VALID',
                                  scope='Conv2d_2a_3x3')
                end_points['Conv2d_2a_3x3'] = net
                # 147 x 147 x 64
                net = slim.conv2d(net, 64, 3, scope='Conv2d_2b_3x3')
                end_points['Conv2d_2b_3x3'] = net
                # 73 x 73 x 64
                net = slim.max_pool2d(net, 3, stride=2, padding='VALID',
                                      scope='MaxPool_3a_3x3')
                end_points['MaxPool_3a_3x3'] = net
                # 73 x 73 x 80
                net = slim.conv2d(net, 80, 1, padding='VALID',
                                  scope='Conv2d_3b_1x1')
                end_points['Conv2d_3b_1x1'] = net
                # 71 x 71 x 192
                net = slim.conv2d(net, 192, 3, padding='VALID',
                                  scope='Conv2d_4a_3x3')
                end_points['Conv2d_4a_3x3'] = net
                # 35 x 35 x 256
                net = slim.conv2d(net, 256, 3, stride=2, padding='VALID',
                                  scope='Conv2d_4b_3x3')
                end_points['Conv2d_4b_3x3'] = net

def reduction_a(net, k, l, m, n):
    with tf.variable_scope('Branch_0'):
        tower_conv = slim.conv2d(net, n, 3, stride=2, padding='VALID',
                                 scope='Conv2d_1a_3x3')
    with tf.variable_scope('Branch_1'):
        tower_conv1_0 = slim.conv2d(net, k, 1, scope='Conv2d_0a_1x1')
        tower_conv1_1 = slim.conv2d(tower_conv1_0, l, 3,
                                    scope='Conv2d_0b_3x3')
        tower_conv1_2 = slim.conv2d(tower_conv1_1, m, 3,
                                    stride=2, padding='VALID',
                                    scope='Conv2d_1a_3x3')
    with tf.variable_scope('Branch_2'):
        tower_pool = slim.max_pool2d(net, 3, stride=2, padding='VALID',
                                     scope='MaxPool_1a_3x3')
    net = tf.concat([tower_conv, tower_conv1_2, tower_pool], 3)
    return net

 

                with tf.variable_scope('Logits'):
                    end_points['PrePool'] = net
                    #pylint: disable=no-member
                    # 1*1*1792
                    net = slim.avg_pool2d(net, net.get_shape()[1:3], padding='VALID',
                                          scope='AvgPool_1a_8x8')
                    #1792
                    net = slim.flatten(net)
          
                    net = slim.dropout(net, dropout_keep_prob, is_training=is_training,
                                       scope='Dropout')
          
                    end_points['PreLogitsFlatten'] = net
                #512
                net = slim.fully_connected(net, bottleneck_layer_size, activation_fn=None,
                        scope='Bottleneck', reuse=False)

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值