예제 #1
0
    def _build_model(self, visual_images):
        """
        Builds a ResNet-50 network using slim.
        """

        # visual_images = tf.placeholder(tf.float32, [None, self.height, self.width, self.channels], name='visual_images')
        is_training = tf.placeholder(tf.bool, name='is_training')
        keep_prob = tf.placeholder(tf.float32, name='keep_prob')

        with slim.arg_scope(resnet.resnet_arg_scope(weight_decay=5e-4)):
            output, network = resnet50.resnet_v1_50(
                visual_images,
                num_classes=self.num_classes,
                is_training=is_training,
                global_pool=False)

        # output = tf.squeeze(output, [1, 2])

        network.update({
            'input': visual_images,
            'is_training': is_training,
            'keep_prob': keep_prob
        })

        self.output = output
        self.network = network
        self.train_vars2 = slim.get_trainable_variables(
            self.scope + '/block') + slim.get_trainable_variables(self.scope +
                                                                  '/conv1')
        self.train_vars = slim.get_trainable_variables(
            self.scope + '/logits') + slim.get_trainable_variables(self.scope +
                                                                   '/conv_map')
예제 #2
0
    def _build_model(self, acoustic_images):
        """
        Builds the hybrid model using slim and base functions.
        """

        # acoustic_images = tf.placeholder(tf.float32, [None, self.height, self.width, self.channels], name='acoustic_images')
        is_training = tf.placeholder(tf.bool, name='is_training')
        keep_prob = tf.placeholder(tf.float32, name='keep_prob')

        end_points = OrderedDict({
            'input': acoustic_images,
            'is_training': is_training,
            'keep_prob': keep_prob
        })

        mean, std, dualcam_net_output, dualcam_net_end_points = self._build_network(acoustic_images, is_training=is_training,
                                                                             scope=self.scope)

        end_points.update(dualcam_net_end_points)

        self.mean = mean
        self.std = std
        self.output = dualcam_net_output  # shared_net_output
        self.network = end_points

        self.train_vars = slim.get_trainable_variables(self.scope)
예제 #3
0
    def _build_model(self, visual_images):
        """
        Builds a ResNet-18 network using slim.
        """

        is_training = tf.placeholder(tf.bool, name='is_training')
        keep_prob = tf.placeholder(tf.float32, name='keep_prob')
        with slim.arg_scope(resnet18_v1.resnet_arg_scope(weight_decay=5e-4)):
            output, network = resnet18_v1.resnet_v1_18(
                visual_images,
                num_classes=self.num_classes,
                map=self.map,
                is_training=is_training)
        if not self.map:
            output = tf.squeeze(output, [1, 2])

        network.update({
            'input': visual_images,
            'is_training': is_training,
            'keep_prob': keep_prob
        })
        self.output = output
        self.network = network
        # we train all network because we don't have a checkpoint
        self.train_vars = slim.get_trainable_variables(self.scope)
예제 #4
0
def restore_var(model_path, num_class):

    x = tf.placeholder(shape=[1, None, None, 3], dtype=tf.float32)

    net, end_points = nets.inception.inception_v2(x, num_classes=num_class)
    output = end_points['Mixed_5c']
    # output=end_points['vgg_16/conv5/conv5_3']
    # train_var=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
    train_var = slim.get_trainable_variables()
    # for var in train_var:
    #     print(var)
    #     print(type(var))

    restore_var = slim.get_variables_to_restore(exclude=['InceptionV2/Logits'])
    for var in restore_var:
        print(var)
        print(type(var))

    init = tf.global_variables_initializer()

    variable_restore_op = slim.assign_from_checkpoint_fn(
        model_path, restore_var, ignore_missing_vars=True)
    data = np.ones(shape=[1, 256, 256, 3], dtype=np.float32)
    with tf.Session() as sess:
        sess.run(init)
        variable_restore_op(sess)

        val = sess.run(output, feed_dict={x: data})
        print(val.shape)
    def _build_model(self, acoustic_images):
        """
        Builds the hybrid model using slim and base functions.
        """

        # acoustic_images = tf.placeholder(tf.float32, [None, self.height, self.width, self.channels], name='acoustic_images')
        is_training = tf.placeholder(tf.bool, name='is_training')
        keep_prob = tf.placeholder(tf.float32, name='keep_prob')

        end_points = OrderedDict({
            'input': acoustic_images,
            'is_training': is_training,
            'keep_prob': keep_prob
        })

        dualcam_net_output, dualcam_net_end_points = self._build_network(
            acoustic_images, scope=self.scope)

        end_points.update(dualcam_net_end_points)

        # shared_net_output, shared_net_end_points = shared.shared_net(dualcam_net_output,
        # num_classes=self.num_classes,
        # is_training=is_training,
        # keep_prob=keep_prob,
        # spatial_squeeze=True,
        # scope=self.scope)

        # end_points.update(shared_net_end_points)

        self.output = dualcam_net_output  # shared_net_output
        self.network = end_points

        self.train_vars = slim.get_trainable_variables(self.scope)
예제 #6
0
    def train_op(self, large_image, alpha, reflectance):

        with tf.variable_scope(tf.get_variable_scope(),
                               reuse=tf.AUTO_REUSE) as scope:

            global_step = tf.train.get_or_create_global_step()

            learning_rate = tf.train.exponential_decay(
                learning_rate=self.learning_rate,
                global_step=global_step,
                decay_rate=0.96,
                decay_steps=10000)
            alpha_logits, reflectance_logits = self.interface(large_image)
            loss = self.calc_losses(alpha_logits, reflectance_logits, alpha,
                                    reflectance)

            optimizer = tf.train.AdamOptimizer(learning_rate)\
                .minimize(loss,var_list=slim.get_trainable_variables('cloud_net'),global_step=global_step)
            update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
            if update_ops:
                updates = tf.group(*update_ops)
                loss = control_flow_ops.with_dependencies([updates], loss)
            tf.summary.scalar('learning_rate', learning_rate)

        return optimizer, loss, global_step
 def list_trainable_variables(self, listed_parts=None):
     model_parts = {
         "nothing":
         lambda: [],
         "fc":
         lambda: [self._fc_w, self._fc_b],
         "cnn":
         lambda: slim.get_trainable_variables("resnet_v2_50"),
         "conv1":
         lambda: slim.get_trainable_variables("resnet_v2_50/conv1"),
         "block1":
         lambda: slim.get_trainable_variables("resnet_v2_50/block1"),
         "block2":
         lambda: slim.get_trainable_variables("resnet_v2_50/block2"),
         "block3":
         lambda: slim.get_trainable_variables("resnet_v2_50/block3"),
         "block4":
         lambda: slim.get_trainable_variables("resnet_v2_50/block4"),
         "postnorm":
         lambda: slim.get_trainable_variables("resnet_v2_50/postnorm"),
         "all":
         lambda: tf.trainable_variables()
     }
     if listed_parts:
         lst = []
         for part in listed_parts:
             lst += model_parts[part]()
     else:
         lst = tf.trainable_variables()
     return lst
예제 #8
0
def test():
    # tensor
    input_image_tensor = tf.placeholder(dtype=tf.float32,
                                        shape=[None, 40, 40, 1],
                                        name='image-input')
    predicted_tensor = ResNet50Model(input_image_tensor, is_training=True)
    predicted_tensor = tf.nn.softmax(predicted_tensor)
    global_step = tf.Variable(initial_value=0, trainable=False)

    # hyper-parameters
    test_total = 320000
    batch_size = 34
    epoch_num = 1

    # 输入数据
    reader = Reader(
        '/home/give/homework/cv/dataset/affiNist/training_and_validation_batches',
        '/home/give/homework/cv/dataset/affiNist/test.mat',
        batch_size=batch_size)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)

        ckpt = tf.train.latest_checkpoint(FLAGS.restore_model_path)
        print('continue training from previous checkpoint from %s' % ckpt)
        start_step = int(os.path.basename(ckpt).split('-')[1])
        variable_restore_op = slim.assign_from_checkpoint_fn(
            ckpt, slim.get_trainable_variables(), ignore_missing_vars=True)
        variable_restore_op(sess)
        sess.run(tf.assign(global_step, start_step))
        start = 0
        predicted = []
        while start < test_total:
            end = start + batch_size
            if end > test_total:
                end = test_total
            cur_batch_images = reader.test_images[start:end]
            predicted_array = sess.run(tf.argmax(predicted_tensor, axis=1),
                                       feed_dict={
                                           input_image_tensor:
                                           np.expand_dims(cur_batch_images,
                                                          axis=3)
                                       })
            predicted.extend(predicted_array)
            print 'Batch Accuracy[%d, %d] : %.4f' % (
                start, test_total,
                np.mean(
                    np.asarray(
                        predicted_array == reader.test_labels[start:end],
                        np.float32)))
            start = end
        predicted = np.array(predicted)
        print 'Total Accuracy: ', np.mean(
            np.asarray(predicted == reader.test_labels, np.float32))
        calculate_acc_error(predicted, reader.test_labels)
예제 #9
0
def get_finetune_trainable_variables(opts):
    trainable_variables = []
    for scope in opts.trainable_scopes:
        trainable_variables.extend(slim.get_trainable_variables(scope))

    tf.logging.info("Finetune variables:")
    for var in trainable_variables:
        tf.logging.info(var)

    return trainable_variables
예제 #10
0
def inception_v2_ssd(img,cfg):
    with slim.arg_scope(inception_v2.inception_v2_arg_scope()):
        logits, end_point = inception_v2.inception_v2_base(img)

        Mixed_3c = end_point['Mixed_3c']
        Mixed_4e = end_point['Mixed_4e']
        cell_11 = end_point['Mixed_5c']
        vbs = slim.get_trainable_variables()
        cell_11 = tf.image.resize_bilinear(cell_11,size=[32,32])
        cell_11 = tf.concat([cell_11,Mixed_4e],axis=3)

        cell_7 = tf.image.resize_bilinear(Mixed_4e,size=[64,64])
        cell_7 = tf.concat([cell_7,Mixed_3c],axis=3)





    cell_11 = slim.conv2d(cell_11,1024,kernel_size=1,activation_fn=slim.nn.relu)

    cell_7 = slim.conv2d(cell_7, 512, kernel_size=3, activation_fn=slim.nn.relu)
    cell_7 = slim.conv2d(cell_7, 256, kernel_size=1, activation_fn=slim.nn.relu)

    cv6 = slim.conv2d(cell_11, 1024, kernel_size=3, rate=6, activation_fn=slim.nn.relu, scope='conv6')
    cv7 = slim.conv2d(cv6, 1024, kernel_size=1, activation_fn=slim.nn.relu, scope='conv7')

    s = utils.normalize_to_target(cell_7, target_norm_value=12.0, dim=1)

    cv8 = slim.conv2d(cv7, 256, kernel_size=1, stride=1, scope='conv8_0')
    cv8 = slim.conv2d(cv8, 512, kernel_size=3, stride=2, scope='conv8_1')

    cv9 = slim.conv2d(cv8, 128, kernel_size=1, stride=1, scope='conv9_0')
    cv9 = slim.conv2d(cv9, 256, kernel_size=3, stride=2, scope='conv9_1')

    cv10 = slim.conv2d(cv9, 128, kernel_size=1, stride=1, scope='conv10_0')
    cv10 = slim.conv2d(cv10, 256, kernel_size=3, stride=2, scope='conv10_1')

    cv11 = slim.conv2d(cv10, 128, kernel_size=1, stride=1, scope='conv11_0')
    cv11 = slim.conv2d(cv11, 256, kernel_size=3, stride=2, scope='conv11_1')
    source = [s, cv7, cv8, cv9, cv10, cv11]
    conf = []
    loc = []
    for cv, num in zip(source, cfg.Config['aspect_num']):
        print(num)
        loc.append(slim.conv2d(cv, num * 4, kernel_size=3, stride=1, activation_fn=None))

        conf.append(
                slim.conv2d(cv, num * cfg.Config['num_classes'], kernel_size=3, stride=1, activation_fn=None))
        print(loc)
    loc = tf.concat([tf.reshape(o, shape=(cfg.batch_size, -1, 4)) for o in loc], axis=1)
    conf = tf.concat([tf.reshape(o, shape=(cfg.batch_size, -1, cfg.Config['num_classes'])) for o in conf],
                         axis=1)

    return loc, conf, vbs
예제 #11
0
def train(img_dir, gt_dir, train_list, pretrained_path):
    img_input = tf.placeholder(tf.float32,
                               shape=[BATCH_SIZE, IMG_SIZE, IMG_SIZE, 3])
    gt_input = tf.placeholder(
        tf.float32, shape=[BATCH_SIZE, IMG_SIZE // 4, IMG_SIZE // 4, 6])

    network = east_model.EAST(training=True, max_len=IMG_SIZE)
    pred_score, pred_gmt = network.build(img_input)
    loss = network.loss(gt_input[:, :, :, 0:1], pred_score,
                        gt_input[:, :, :, 1:6], pred_gmt)

    global_step = tf.get_variable('global_step', [],
                                  initializer=tf.constant_initializer(0),
                                  trainable=False)
    learning_rate = tf.train.exponential_decay(0.0001,
                                               global_step,
                                               decay_steps=10000,
                                               decay_rate=0.94,
                                               staircase=True)
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(update_ops):
        optimizer = tf.train.AdamOptimizer(
            learning_rate=learning_rate).minimize(loss=loss,
                                                  global_step=global_step)

    restore_op = slim.assign_from_checkpoint_fn(pretrained_path,
                                                slim.get_trainable_variables(),
                                                ignore_missing_vars=True)
    saver = tf.train.Saver()
    cfg = tf.ConfigProto()
    cfg.gpu_options.allow_growth = True
    with tf.Session(config=cfg) as sess:
        if RESTORE:
            saver.restore(sess, tf.train.latest_checkpoint('./checkpoint'))
        else:
            sess.run(tf.global_variables_initializer())
            restore_op(sess)

        data_list = data_processor.read_lines(train_list)
        for step in range(100001):
            img_batch, gt_batch = data_processor.next_batch(
                img_dir, gt_dir, data_list, BATCH_SIZE)
            s, g, l, lr, _ = sess.run(
                [pred_score, pred_gmt, loss, learning_rate, optimizer],
                feed_dict={
                    img_input: img_batch,
                    gt_input: gt_batch
                })
            if step % 1000 == 0 and step > 0:
                saver.save(sess=sess,
                           save_path='./checkpoint/east.ckpt',
                           global_step=step)
            if step % 100 == 0:
                print(step, lr, l)
예제 #12
0
    def __init__(self, input_shape=None, num_classes=14, nr_frames=5):
        self.scope = 'resnet_v1_50'

        self.num_classes = num_classes

        self.height = input_shape[0]
        self.width = input_shape[1]
        self.channels = input_shape[2]
        self.nr_frames = nr_frames
        self.output, self.network = self._build_model()

        self.train_vars = slim.get_trainable_variables(self.scope + '/logits')
예제 #13
0
    def __init__(self, input_shape=None, num_classes=14):

        self.scope = 'SoundNet'

        self.num_classes = num_classes

        self.height = input_shape[0]  # 22050 * 5
        self.width = input_shape[1]  # 1
        self.channels = input_shape[2]  # 1

        self.output, self.network = self._build_model()

        self.train_vars = slim.get_trainable_variables(self.scope)
예제 #14
0
def main(_):

    # download and conver flower_photos dataset to tfrecord
    download_dataset.maybe_download_and_extract(FLOWERS_DATA_DIR, _DATA_URL)
    convert_flowers_to_tfrecord.run(FLOWERS_DATA_DIR)

    with tf.Graph().as_default() as g:
        tf.logging.set_verbosity(tf.logging.INFO)
        dataset = flowers.get_split('train', FLOWERS_DATA_DIR)
        images, _, labels = load_batch(dataset,
                                       batch_size=BATCH_SIZE,
                                       is_training=True)

        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            logits, _ = resnet_v2.resnet_v2_50(images,
                                               num_classes=dataset.num_classes,
                                               is_training=True)
            logits = tf.squeeze(tf.convert_to_tensor(logits, tf.float32))

        one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes)
        loss = tf.losses.softmax_cross_entropy(onehot_labels=one_hot_labels,
                                               logits=logits)
        slim.losses.add_loss(loss)
        total_loss = slim.losses.get_total_loss()
        tf.summary.scalar('losses/total_loss', total_loss)

        # 梯度根性只更新最后一层
        var2training = slim.get_trainable_variables(
            scope="resnet_v2_50/logits")
        optimizer = tf.train.AdamOptimizer()
        train_op = slim.learning.create_train_op(
            total_loss, optimizer, variables_to_train=var2training)

        init_fn = get_init_fn(
            checkpoint_exclude_scopes=CHECKPOINT_EXCLUDE_SCOPES,
            checkpoint_dir=PRETRAIN_DIR)

        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True

        print("start training")
        final_loss = slim.learning.train(train_op,
                                         logdir=TRAIN_DIR,
                                         init_fn=init_fn,
                                         number_of_steps=NUMBER_OF_STEPS,
                                         trace_every_n_steps=500,
                                         log_every_n_steps=50,
                                         session_config=config,
                                         save_interval_secs=60)

        print('Finished training. Last batch loss %f' % final_loss)
예제 #15
0
    def __init__(self, input_shape=None, num_classes=14, num_frames=12):

        self.scope = 'DualCamNet'

        self.num_classes = num_classes
        self.num_frames = num_frames

        self.height = input_shape[0]
        self.width = input_shape[1]
        self.channels = input_shape[2]

        self.output, self.network = self._build_model()

        self.train_vars = slim.get_trainable_variables(self.scope)
    def _build_model(self, f):
        """
        Builds the hybrid model using slim and base functions.
        """

        mean, std, dualcam_net_output, dualcam_net_end_points = self._build_network2(
            f, is_training=self.is_training, scope=self.scope)

        self.end_points.update(dualcam_net_end_points)
        self.mean = mean
        self.std = std
        self.output = dualcam_net_output  # shared_net_output
        self.network = self.end_points
        self.train_vars = slim.get_trainable_variables(self.scope + '/')
예제 #17
0
    def print_model():
        def get_nb_params_shape(shape):
            nb_params = 1
            for dim in shape:
                nb_params = nb_params * int(dim)
            return nb_params

        tot_nb_params = 0
        for trainable_variable in slim.get_trainable_variables():
            print(trainable_variable.name, trainable_variable.shape)
            vshape = trainable_variable.get_shape()  # e.g [D,F] or [W,H,C]
            current_nb_params = get_nb_params_shape(vshape)
            tot_nb_params = tot_nb_params + current_nb_params
        print('Total number of trainable params', tot_nb_params)
예제 #18
0
    def __init__(self, s_size, a_size, scope, trainer):
        with tf.variable_scope(scope):
            print("Scope", scope)
            with tf.variable_scope("regular"):
                self.network = self.make_network(s_size, a_size)
            with tf.variable_scope("target"):
                self.target_network = self.make_network(s_size, a_size)

            # Only the worker network need ops for loss functions and gradient updating.
            if scope != 'global':
                self.actions = tf.placeholder(
                    shape=[None], dtype=tf.int32)  # Index of actions taken
                self.actions_onehot = tf.one_hot(
                    self.actions, a_size,
                    dtype=tf.float32)  # 1-hot tensor of actions taken

                # losses!
                self.target_q_t = tf.placeholder('float32', [None],
                                                 name='target_q_t')
                self.q_acted = tf.reduce_sum(self.network.value *
                                             self.actions_onehot,
                                             reduction_indices=1,
                                             name='q_acted')
                self.loss = tf.reduce_mean(tf.square(self.target_q_t -
                                                     self.q_acted),
                                           name='loss')

                # Get gradients from local network using local losses
                self.local_vars = tf.get_collection(
                    tf.GraphKeys.TRAINABLE_VARIABLES, scope)
                self.gradients = tf.gradients(self.loss, self.local_vars)
                self.var_norms = tf.global_norm(self.local_vars)
                grads, self.grad_norms = tf.clip_by_global_norm(
                    self.gradients, 40.0)

                # Apply local gradients to global network
                global_vars = tf.get_collection(
                    tf.GraphKeys.TRAINABLE_VARIABLES, 'global/regular')
                target_weights = tf.get_collection(
                    tf.GraphKeys.TRAINABLE_VARIABLES, "global/target")

                # set global target network to be the same as local network
                weights = slim.get_trainable_variables(scope=scope +
                                                       "/regular")
                self.assign_op = {}
                for w, t_w in zip(weights, target_weights):
                    self.assign_op[w.name] = t_w.assign(w)

                self.apply_grads = trainer.apply_gradients(
                    zip(grads, global_vars))
예제 #19
0
def model(inputs,cfg):
    source = []
    with tf.variable_scope('vgg_16',default_name=None, values=[inputs]) as sc:
        end_points_collection = sc.original_name_scope + '_end_points'
        # Collect outputs for conv2d, fully_connected and max_pool2d.
        with slim.arg_scope([slim.conv2d,  slim.max_pool2d]):
            net = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], scope='conv1')
            net = slim.max_pool2d(net, [2, 2], scope='pool1')
            net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3], scope='conv2')
            net = slim.max_pool2d(net, [2, 2], scope='pool2')
            net = slim.repeat(net, 3, slim.conv2d, 256, [3, 3], scope='conv3')
            net = slim.max_pool2d(net, [2, 2], scope='pool3')
            cov_43 = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv4')
            net = slim.max_pool2d(cov_43, [2, 2], scope='pool4')
            net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv5')
            net = slim.max_pool2d(net, kernel_size=3,stride=1, scope='pool5')
            vbs = slim.get_trainable_variables()
            utils.count_parm()
            #vbs =None
            cv6 = slim.conv2d(net,1024,kernel_size=3,rate=6,activation_fn=slim.nn.relu,scope='conv6')
            cv7 = slim.conv2d(cv6,1024,kernel_size=1,activation_fn=slim.nn.relu,scope='conv7')

            s = utils.normalize_to_target(cov_43, target_norm_value=2.0, dim=1)



            cv8 = slim.conv2d(cv7,256,kernel_size=1,stride=1,scope='conv8_0')
            cv8 = slim.conv2d(cv8, 512, kernel_size=3, stride=2, scope='conv8_1')

            cv9 = slim.conv2d(cv8, 128, kernel_size=1, stride=1, scope='conv9_0')
            cv9 = slim.conv2d(cv9, 256, kernel_size=3, stride=2, scope='conv9_1')

            cv10 = slim.conv2d(cv9, 128, kernel_size=1, stride=1, scope='conv10_0')
            cv10 = slim.conv2d(cv10, 256, kernel_size=3, stride=2, scope='conv10_1')

            cv11 = slim.conv2d(cv10, 128, kernel_size=1, stride=1, scope='conv11_0')
            cv11 = slim.conv2d(cv11, 256, kernel_size=3, stride=2, scope='conv11_1')
            source = [s,cv7,cv8,cv9,cv10,cv11]
            conf = []
            loc = []
            for cv,num in zip(source,cfg.Config['aspect_num']):

                loc.append(slim.conv2d(cv,num*4,kernel_size=3,stride=1,activation_fn=None))

                conf.append(slim.conv2d(cv, num*cfg.Config['num_classes'], kernel_size=3, stride=1, activation_fn=None))
            print(loc)
        loc = tf.concat([tf.reshape(o, shape=(cfg.batch_size, -1, 4)) for o in loc], axis=1)
        conf = tf.concat([tf.reshape(o, shape=(cfg.batch_size, -1, cfg.Config['num_classes'])) for o in conf], axis=1)
        return loc,conf,vbs
예제 #20
0
def resnetv2_ssd(img):
    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        net, end_points = resnet_v2.resnet_v2_50(img, is_training=True)

    c1 = end_points['resnet_v2_50/block1']
    c2 = end_points['resnet_v2_50/block2']
    base_16_0 = end_points['resnet_v2_50/block3']
    base_16_1 = end_points['resnet_v2_50/block4']
    vbs = slim.get_trainable_variables()
    # vbs = None
    base_16_0 = slim.conv2d(base_16_0, 512, 1)
    base_16_1 = slim.conv2d(base_16_1, 512, 1)
    c3 = tf.concat([base_16_0, base_16_1], axis=3)

    return c1, c2, c3, vbs
예제 #21
0
    def __init__(self, num_classes=None):

        self.scope = 'AVNet'

        self.num_classes = num_classes

        self.video_height = 224
        self.video_width = 224
        self.video_channels = 3

        self.audio_height = 36
        self.audio_width = 48
        self.audio_channels = 12

        self.num_frames = 12

        self.output, self.network = self._build_model()

        self.audio_train_vars = slim.get_trainable_variables('DualCamNet')
        self.video_train_vars = slim.get_trainable_variables(
            'resnet_v1_50/logits')
        self.audio_video_train_vars = slim.get_trainable_variables(self.scope)

        self.train_vars = self.audio_train_vars + self.video_train_vars + self.audio_video_train_vars
예제 #22
0
def inception_v2_ssd(img,cfg):
    with slim.arg_scope(inception_v2.inception_v2_arg_scope()):
        logits, end_point = inception_v2.inception_v2_base(img)

        Mixed_3c = end_point['Mixed_3c']
        Mixed_4e = end_point['Mixed_4e']
        Mixed_5c = end_point['Mixed_5c']
        vbs = slim.get_trainable_variables()
        #vbs = None
        cell_11 = tf.image.resize_bilinear(Mixed_5c,size=[int(32*(cfg.image_size/512)),int(32*(cfg.image_size/512))])
        cell_11 = tf.concat([cell_11,Mixed_4e],axis=3)

        cell_7 = tf.image.resize_bilinear(Mixed_4e,size=[int(64*(cfg.image_size/512)),int(64*(cfg.image_size/512))])
        cell_7 = tf.concat([cell_7,Mixed_3c],axis=3)


    mask_fp = get_mask_fp(Mixed_3c ,Mixed_4e,Mixed_5c)

    cell_11 = slim.conv2d(cell_11,1024,kernel_size=1,activation_fn=slim.nn.relu)

    cell_7 = slim.conv2d(cell_7, 512, kernel_size=3, activation_fn=slim.nn.relu)
    cell_7 = slim.conv2d(cell_7, 256, kernel_size=1, activation_fn=slim.nn.relu)

    cv6 = slim.conv2d(cell_11, 1024, kernel_size=3, rate=6, activation_fn=slim.nn.relu, scope='conv6')
    cv7 = slim.conv2d(cv6, 1024, kernel_size=1, activation_fn=slim.nn.relu, scope='conv7')

    s = utils.normalize_to_target(cell_7, target_norm_value=cfg.norm_value, dim=1)

    cv8 = inception(cv7, out_put=512, name='cv8', stride=2)
    cv9 = inception(cv8, out_put=256, name='cv9', stride=2)
    cv10 = inception(cv9, out_put=256, name='cv10', stride=2)
    cv11 = inception(cv10, out_put=256,name= 'cv11', stride=2)

    source = [s, cv7, cv8, cv9, cv10, cv11]
    conf = []
    loc = []
    for cv, num in zip(source, cfg.Config['aspect_num']):

        loc.append(slim.conv2d(cv, num * 4, kernel_size=3, stride=1, activation_fn=None))

        conf.append(
                slim.conv2d(cv, num * cfg.Config['num_classes'], kernel_size=3, stride=1, activation_fn=None))

    loc = tf.concat([tf.reshape(o, shape=(cfg.batch_size, -1, 4)) for o in loc], axis=1)
    conf = tf.concat([tf.reshape(o, shape=(cfg.batch_size, -1, cfg.Config['num_classes'])) for o in conf],
                         axis=1)

    return loc, conf,mask_fp, vbs
예제 #23
0
def variables_to_restore_and_train(pre_trained_model):
    if 'vgg_16' in pre_trained_model:
        exclude = ['fully_connected']
        train_sc = ['fully_connected']
    elif 'inception_v3' in pre_trained_model:
        exclude = [
            'InceptionV3/Logits', 'InceptionV3/AuxLogits', 'fully_connected'
        ]
        train_sc = ['fully_connected']
    elif 'resnet_v1_50' in pre_trained_model:
        exclude = ['resnet_v1_50/logits']
        train_sc = ['resnet_v1_50/logits']
    else:
        exclude = []
        train_sc = []
    variables_to_restore = slim.get_variables_to_restore(exclude=exclude)
    variables_to_train = []
    for sc in train_sc:
        variables_to_train += slim.get_trainable_variables(sc)
    return variables_to_train, variables_to_restore
예제 #24
0
    def _build_model(self, acoustic_images):
        """
        Builds the hybrid model using slim and base functions.
        """
        is_training = tf.placeholder(tf.bool, name='is_training')
        keep_prob = tf.placeholder(tf.float32, name='keep_prob')

        end_points = OrderedDict({
            'input': acoustic_images,
            'is_training': is_training,
            'keep_prob': keep_prob
        })

        dualcam_net_output, dualcam_net_end_points = self._build_network(
            acoustic_images, is_training=is_training, scope=self.scope)

        end_points.update(dualcam_net_end_points)

        self.output = dualcam_net_output
        self.network = end_points

        self.train_vars = slim.get_trainable_variables(self.scope)
예제 #25
0
def create_restore_hook(opts):
    variables_to_restore = slim.get_variables_to_restore(
        exclude=opts.exclude_restore_scopes)
    tf.logging.info('Restore variables: ')
    for var in variables_to_restore:
        tf.logging.info(var)

    checkpoint_path = opts.pretrained_weights_path
    if tf.gfile.IsDirectory(checkpoint_path):
        checkpoint_path = tf.train.latest_checkpoint(checkpoint_path)

    tf.logging.info('checkpoint_path = {}'.format(checkpoint_path))

    init_fn = slim.assign_from_checkpoint_fn(checkpoint_path,
                                             variables_to_restore,
                                             ignore_missing_vars=False)

    tf.logging.info('Global trainable variables: ')
    for var in slim.get_trainable_variables():
        tf.logging.info(var)

    return _RestoreHook(init_fn)
예제 #26
0
    def _build_opt(self, c,
            freeze_cnn=cfg.FREEZE_CNN,
            freeze_rnn=cfg.FREEZE_RNN,
            log=no_op):
        log('- build-opt -')
        opt = tf.train.AdamOptimizer(learning_rate=self.learning_rate_)

        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)

        if freeze_cnn:
            log('freezing cnn')
            train_vars = [v for v in slim.get_trainable_variables() if ('vo/sconv' not in v.name) and ('vo/conv' not in v.name)]
            if freeze_rnn:
                train_vars = [v for v in train_vars if ('vo/reduction' not in v.name) and ('vo/rnn' not in v.name)]
            log('train variables:')
            for v in train_vars:
                log('\t {} : {}'.format(v.name, v.shape))
            log('---------')
        else:
            # train everything
            train_vars = None

        with tf.control_dependencies(update_ops):
            #grads, vars = zip(*opt.compute_gradients(c))
            #grads_c, _ = tf.clip_by_global_norm(grads, 1.0)
            #train_op = opt.apply_gradients(zip(grads_c, vars), global_step=self.step_)
            ##train_op = opt.minimize(c, global_step=self.step_)

            train_op = tf.contrib.layers.optimize_loss(c, self.step_,
                    learning_rate=self.learning_rate_,
                    optimizer='Adam',
                    clip_gradients=3.0,
                    summaries=['loss', 'learning_rate', 'global_gradient_norm', 'gradients'],
                    variables=train_vars
                    )

        log('-------------')
        return train_op
예제 #27
0
    def _build_model(self, melspectrum):
        """
        Builds the hybrid model using slim and base functions.
        """

        # acoustic_images = tf.placeholder(tf.float32, [None, self.height, self.width, self.channels], name='acoustic_images')
        # is_training = tf.placeholder(tf.bool, name='is_training')
        # keep_prob = tf.placeholder(tf.float32, name='keep_prob')

        # end_points = OrderedDict({
        #     'input': melspectrum,
        #     'is_training': is_training,
        #     'keep_prob': keep_prob
        # })

        output = self._build_network(melspectrum, scope=self.scope)

        # end_points.update(dualcam_net_end_points)

        self.output = output  # shared_net_output
        # self.network = end_points

        self.train_vars = slim.get_trainable_variables(self.scope)
예제 #28
0
def validation_dice(val_dir):
    input_image_tensor = tf.placeholder(dtype=tf.float32,
                                        shape=[
                                            None, Config.vox_size[0],
                                            Config.vox_size[1],
                                            Config.vox_size[2], 1
                                        ],
                                        name='image_tensor')
    input_gt_tensor = tf.placeholder(dtype=tf.int32,
                                     shape=[
                                         None, Config.vox_size[0],
                                         Config.vox_size[1], Config.vox_size[2]
                                     ],
                                     name='gt_tensor')
    pred_last, pred_6, pred_3 = model(input_image_tensor)
    global_step = tf.train.get_or_create_global_step()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())

        saver = tf.train.Saver(tf.global_variables(), max_to_keep=5)

        ckpt = tf.train.latest_checkpoint(FLAGS.model_restore_path)
        print('continue training from previous checkpoint from %s' % ckpt)
        start_step = int(os.path.basename(ckpt).split('-')[1])
        variable_restore_op = slim.assign_from_checkpoint_fn(
            ckpt, slim.get_trainable_variables(), ignore_missing_vars=True)
        variable_restore_op(sess)
        sess.run(tf.assign(global_step, start_step))

        # obtain the data of validation
        image_paths = glob(os.path.join(val_dir, 'volume-*.nii'))
        for image_path in image_paths:
            basename = os.path.basename(image_path)
            file_id = basename.split('-')[1].split('.')[0]
            gt_path = os.path.join(val_dir, 'segmentation-' + file_id + '.nii')
            pred_result = compute_onefile()
    def _build_model(self, spectrogram):
        """
        Builds the model using slim.
        """

        is_training = tf.placeholder(tf.bool, name='is_training')
        keep_prob = tf.placeholder(tf.float32, name='keep_prob')

        end_points = OrderedDict({
            'input': spectrogram,
            'is_training': is_training,
            'keep_prob': keep_prob
        })
        with slim.arg_scope(self._build_arg_scope(weight_decay=5e-4)):
            hearnet_output, hearnet_end_points = self._build_network(
                spectrogram,
                num_classes=self.num_classes,
                is_training=is_training,
                keep_prob=keep_prob,
                scope=self.scope)

        end_points.update(hearnet_end_points)

        shared_net_output, shared_net_end_points = shared.shared_net(
            hearnet_output,
            num_classes=self.num_classes,
            is_training=is_training,
            keep_prob=keep_prob,
            spatial_squeeze=True,
            scope=self.scope,
            embedding=self.embedding)

        end_points.update(shared_net_end_points)
        self.output = shared_net_output
        self.network = end_points
        self.train_vars = slim.get_trainable_variables(self.scope)
예제 #30
0
def main(argv=None):
    import os
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu_list
    if not tf.gfile.Exists(FLAGS.checkpoint_path):
        # tf.gfile.MkDir(FLAGS.checkpoint_path)
        os.makedirs(FLAGS.checkpoint_path)
    else:
        if not FLAGS.restore:
            tf.gfile.DeleteRecursively(FLAGS.checkpoint_path)
            tf.gfile.MkDir(FLAGS.checkpoint_path)

    input_images = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='input_images')
    input_score_maps = tf.placeholder(tf.float32, shape=[None, None, None, 1], name='input_score_maps')
    if FLAGS.geometry == 'RBOX':
        input_geo_maps = tf.placeholder(tf.float32, shape=[None, None, None, 5], name='input_geo_maps')
    else:
        input_geo_maps = tf.placeholder(tf.float32, shape=[None, None, None, 8], name='input_geo_maps')
    input_training_masks = tf.placeholder(tf.float32, shape=[None, None, None, 1], name='input_training_masks')

    global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False)
    learning_rate = tf.train.exponential_decay(FLAGS.learning_rate, global_step, decay_steps=10000,
                                               decay_rate=0.94, staircase=True)
    # add summary
    tf.summary.scalar('learning_rate', learning_rate)
    opt = tf.train.AdamOptimizer(learning_rate)
    # opt = tf.train.MomentumOptimizer(learning_rate, 0.9)


    # split
    input_images_split = tf.split(input_images, len(gpus))
    input_score_maps_split = tf.split(input_score_maps, len(gpus))
    input_geo_maps_split = tf.split(input_geo_maps, len(gpus))
    input_training_masks_split = tf.split(input_training_masks, len(gpus))

    tower_grads = []
    reuse_variables = None
    for i, gpu_id in enumerate(gpus):
        with tf.device('/gpu:%d' % gpu_id):
            with tf.name_scope('model_%d' % gpu_id) as scope:
                iis = input_images_split[i]
                isms = input_score_maps_split[i]
                igms = input_geo_maps_split[i]
                itms = input_training_masks_split[i]
                total_loss, model_loss = tower_loss(iis, isms, igms, itms, reuse_variables)
                batch_norm_updates_op = tf.group(*tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope))
                reuse_variables = True

                grads = opt.compute_gradients(total_loss)
                tower_grads.append(grads)

    grads = average_gradients(tower_grads)
    apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)

    summary_op = tf.summary.merge_all()
    # save moving average
    variable_averages = tf.train.ExponentialMovingAverage(
        FLAGS.moving_average_decay, global_step)
    variables_averages_op = variable_averages.apply(tf.trainable_variables())
    # batch norm updates
    with tf.control_dependencies([variables_averages_op, apply_gradient_op, batch_norm_updates_op]):
        train_op = tf.no_op(name='train_op')

    saver = tf.train.Saver(tf.global_variables())
    summary_writer = tf.summary.FileWriter(FLAGS.checkpoint_path, tf.get_default_graph())

    init = tf.global_variables_initializer()

    if FLAGS.pretrained_model_path is not None:
        variable_restore_op = slim.assign_from_checkpoint_fn(FLAGS.pretrained_model_path, slim.get_trainable_variables(),
                                                             ignore_missing_vars=True)
    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
        if FLAGS.restore:
            print('continue training from previous checkpoint')
            ckpt = tf.train.latest_checkpoint(FLAGS.checkpoint_path)
            saver.restore(sess, ckpt)
        else:
            sess.run(init)
            if FLAGS.pretrained_model_path is not None:
                variable_restore_op(sess)

        data_generator = read_icdar.get_batch(num_workers=FLAGS.num_readers,
                                              input_size=FLAGS.input_size,
                                              batch_size=FLAGS.batch_size_per_gpu * len(gpus))

        start = time.time()
        for step in range(FLAGS.start_steps, FLAGS.max_steps):
            data = next(data_generator)
            feed_dict = {input_images: data[0],
                         input_score_maps: data[2],
                         input_geo_maps: data[3],
                         input_training_masks: data[4]}
            ml, tl, _ = sess.run([model_loss, total_loss, train_op], feed_dict=feed_dict)
            if np.isnan(tl):
                print('Loss diverged, stop training')
                break

            if step % 10 == 0:
                avg_time_per_step = (time.time() - start)/10
                avg_examples_per_second = (10 * FLAGS.batch_size_per_gpu * len(gpus))/(time.time() - start)
                start = time.time()
                print('Step {:06d}, model loss {:.4f}, total loss {:.4f}, {:.2f} seconds/step, '
                      '{:.2f} examples/second'.format(step, ml, tl, avg_time_per_step, avg_examples_per_second))
                logging.info('Step {:06d}, model loss {:.4f}, total loss {:.4f}, {:.2f} seconds/step, '
                             '{:.2f} examples/second'.format(step, ml, tl, avg_time_per_step, avg_examples_per_second))
            if step % FLAGS.save_checkpoint_steps == 0:
                saver.save(sess, FLAGS.checkpoint_path + 'model.ckpt', global_step=global_step)

            if step % FLAGS.save_summary_steps == 0:
                _, tl, summary_str = sess.run([train_op, total_loss, summary_op], feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, global_step=step)
예제 #31
0
def main(argv=None):
    import os
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu_list
    if not tf.gfile.Exists(FLAGS.checkpoint_path):
        tf.gfile.MkDir(FLAGS.checkpoint_path)
    else:
        if not FLAGS.restore:
            tf.gfile.DeleteRecursively(FLAGS.checkpoint_path)
            tf.gfile.MkDir(FLAGS.checkpoint_path)

    input_images = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='input_images')
    input_score_maps = tf.placeholder(tf.float32, shape=[None, None, None, 1], name='input_score_maps')
    if FLAGS.geometry == 'RBOX':
        input_geo_maps = tf.placeholder(tf.float32, shape=[None, None, None, 5], name='input_geo_maps')
    else:
        input_geo_maps = tf.placeholder(tf.float32, shape=[None, None, None, 8], name='input_geo_maps')
    input_training_masks = tf.placeholder(tf.float32, shape=[None, None, None, 1], name='input_training_masks')

    global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False)
    learning_rate = tf.train.exponential_decay(FLAGS.learning_rate, global_step, decay_steps=10000, decay_rate=0.94, staircase=True)
    # add summary
    tf.summary.scalar('learning_rate', learning_rate)
    opt = tf.train.AdamOptimizer(learning_rate)
    # opt = tf.train.MomentumOptimizer(learning_rate, 0.9)


    # split
    input_images_split = tf.split(input_images, len(gpus))
    input_score_maps_split = tf.split(input_score_maps, len(gpus))
    input_geo_maps_split = tf.split(input_geo_maps, len(gpus))
    input_training_masks_split = tf.split(input_training_masks, len(gpus))

    tower_grads = []
    reuse_variables = None
    for i, gpu_id in enumerate(gpus):
        with tf.device('/gpu:%d' % gpu_id):
            with tf.name_scope('model_%d' % gpu_id) as scope:
                iis = input_images_split[i]
                isms = input_score_maps_split[i]
                igms = input_geo_maps_split[i]
                itms = input_training_masks_split[i]
                total_loss, model_loss = tower_loss(iis, isms, igms, itms, reuse_variables)
                batch_norm_updates_op = tf.group(*tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope))
                reuse_variables = True

                grads = opt.compute_gradients(total_loss)
                tower_grads.append(grads)

    grads = average_gradients(tower_grads)
    apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)

    summary_op = tf.summary.merge_all()
    # save moving average
    variable_averages = tf.train.ExponentialMovingAverage(
        FLAGS.moving_average_decay, global_step)
    variables_averages_op = variable_averages.apply(tf.trainable_variables())
    # batch norm updates
    with tf.control_dependencies([variables_averages_op, apply_gradient_op, batch_norm_updates_op]):
        train_op = tf.no_op(name='train_op')

    saver = tf.train.Saver(tf.global_variables())
    summary_writer = tf.summary.FileWriter(FLAGS.checkpoint_path, tf.get_default_graph())

    init = tf.global_variables_initializer()

    if FLAGS.pretrained_model_path is not None:
        variable_restore_op = slim.assign_from_checkpoint_fn(FLAGS.pretrained_model_path, slim.get_trainable_variables(),
                                                             ignore_missing_vars=True)
    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
        if FLAGS.restore:
            print 'continue training from previous checkpoint'
            ckpt = tf.train.latest_checkpoint(FLAGS.checkpoint_path)
            saver.restore(sess, ckpt)
        else:
            sess.run(init)
            if FLAGS.pretrained_model_path is not None:
                variable_restore_op(sess)

        data_generator = icdar.get_batch(num_workers=FLAGS.num_readers,
                                         input_size=FLAGS.input_size,
                                         batch_size=FLAGS.batch_size * len(gpus))

        start = time.time()
        for step in xrange(FLAGS.max_steps):
            data = data_generator.next()
            ml, tl, _ = sess.run([model_loss, total_loss, train_op], feed_dict={input_images: data[0],
                                                                                input_score_maps: data[2],
                                                                                input_geo_maps: data[3],
                                                                                input_training_masks: data[4]})
            if np.isnan(tl):
                print 'Loss diverged, stop training'
                break

            if step % 10 == 0:
                avg_time_per_step = (time.time() - start)/10
                avg_examples_per_second = (10 * FLAGS.batch_size * len(gpus))/(time.time() - start)
                start = time.time()
                print 'Step {:06d}, model loss {:.4f}, total loss {:.4f}, {:.2f} seconds/step, {:.2f} examples/second'.format(
                    step, ml, tl, avg_time_per_step, avg_examples_per_second)

            if step % FLAGS.save_checkpoint_steps == 0:
                saver.save(sess, FLAGS.checkpoint_path + 'model.ckpt', global_step=global_step)

            if step % FLAGS.save_summary_steps == 0:
                _, tl, summary_str = sess.run([train_op, total_loss, summary_op], feed_dict={input_images: data[0],
                                                                                             input_score_maps: data[2],
                                                                                             input_geo_maps: data[3],
                                                                                             input_training_masks: data[4]})
                summary_writer.add_summary(summary_str, global_step=step)