Exemple #1
0
def build_net(images,
              n_class=None,
              is_training=True,
              reuse=False,
              scope='mobilenet_half'):

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

        with slim.arg_scope([slim.batch_norm], is_training=is_training):

            net, endpoints = mobilenet_v1_base(images,
                                               depth_multiplier=0.5,
                                               conv_defs=_CONV_DEFS,
                                               scope=vscope)

            net = slim.flatten(net)

            net = slim.fully_connected(
                net,
                512,
                weights_initializer=tf.truncated_normal_initializer(
                    stddev=0.001),
                activation_fn=None,
                scope='fc5')

            if isinstance(n_class, int):
                net = slim.fully_connected(net,
                                           n_class,
                                           activation_fn=None,
                                           scope='logits')

            # add summarys
            slim.summarize_collection(tf.GraphKeys.MODEL_VARIABLES)

            return net
Exemple #2
0
    def build_model(self,
                    input_imgs,
                    is_training,
                    targets,
                    masks=None,
                    privileged_input=None):
        '''Builds the model. Assumes that the input is from range [0, 1].
        Args:
            input_imgs: list of input images (scaled between -1 and 1) with the
                       dimensions specified in the cfg
            is_training: flag for whether the model is in training mode or not
            mask: mask used for computing sum of squares loss. If None, we assume
                  it is np.ones.
        '''
        print('building model')
        cfg = self.cfg
        self.is_training = is_training

        if self.decoder_only:
            encoder_output = input_imgs  # Assume that the input is the representation
        else:
            encoder_output = self.build_encoder(input_imgs, is_training)
        # encoder_output = self.build_encoder(input_imgs, is_training)

        final_output = self.build_siamese_output_postprocess(
            encoder_output, is_training)

        losses = self.get_losses(final_output,
                                 targets,
                                 is_softmax='l2_loss' not in cfg)
        # use weight regularization
        if 'omit_weight_reg' in cfg and cfg['omit_weight_reg']:
            add_reg = False
        else:
            add_reg = True

        # get losses
        regularization_loss = tf.add_n(slim.losses.get_regularization_losses(),
                                       name='losses/regularization_loss')
        total_loss = slim.losses.get_total_loss(
            add_regularization_losses=add_reg, name='losses/total_loss')

        self.input_images = input_imgs
        self.targets = targets
        self.masks = masks
        self.encoder_output = encoder_output
        self.losses = losses
        self.task_loss = losses[0]
        self.total_loss = total_loss
        self.decoder_output = final_output
        # add summaries
        slim.summarize_variables()
        slim.summarize_weights()
        slim.summarize_biases()
        slim.summarize_activations()
        slim.summarize_collection(tf.GraphKeys.LOSSES)
        tf.summary.scalar('accuracy', self.accuracy)
        slim.summarize_tensor(regularization_loss)
        slim.summarize_tensor(total_loss)
        self.model_built = True
Exemple #3
0
    def build_model(self, input_imgs, is_training, targets=None, masks=None, privileged_input=None):
        '''Builds the model. Assumes that the input is from range [0, 1].
            Args:
            input_imgs: list of input images (scaled between -1 and 1) with the
                       dimensions specified in the cfg
            is_training: flag for whether the model is in training mode or not
            mask: mask used for computing sum of squares loss. If None, we assume
                  it is np.ones.
        '''
        print('building model')
        cfg = self.cfg
        self.is_training = is_training
        self.input_images = input_imgs
        self.target_images = targets
        self.masks = masks
        self.targets = targets

        if masks is None:
            masks = tf.constant( 1, dtype=tf.float32, shape=[], name='constant_mask' )

        if self.decoder_only:
            self.encoder_output = input_imgs # Assume that the input is the representation
        else:
            self.encoder_output = self.build_encoder(input_imgs, is_training)
        
        self.decoder_output = self.build_decoder(self.encoder_output, is_training)

        resized_output = tf.reshape(self.decoder_output, [-1, self.cfg[ 'target_num_channels' ]])
        resized_target = tf.reshape(targets, [-1])
        masks = tf.reshape(masks, [-1])
        losses = self.get_losses( resized_output, resized_target, masks)

        # use weight regularization
        if 'omit_weight_reg' in cfg and cfg['omit_weight_reg']:
            add_reg = False
        else:
            add_reg = True
        
        # get losses
        regularization_loss = tf.add_n( slim.losses.get_regularization_losses(), name='losses/regularization_loss' )
        total_loss = slim.losses.get_total_loss( add_regularization_losses=add_reg,
                                                 name='losses/total_loss')

        self.losses = losses
        self.total_loss = total_loss

        # add summaries
        if self.extended_summaries:
            slim.summarize_variables()
            slim.summarize_weights()
            slim.summarize_biases()
            slim.summarize_activations()
        slim.summarize_collection(tf.GraphKeys.LOSSES)
        slim.summarize_tensor( regularization_loss )
        slim.summarize_tensor( total_loss )
        self.model_built = True
def build_net(images,
              n_class=None,
              is_training=True,
              reuse=False,
              scope='resnet_50_half'):

    with tf.variable_scope(scope, reuse=reuse):

        with slim.arg_scope([slim.batch_norm], is_training=is_training):

            net_spec = [[[i, i, i * 4] for i in [32, 64, 128, 256]],
                        [3, 4, 6, 3]]

            with slim.arg_scope([slim.conv2d],
                                normalizer_fn=None,
                                activation_fn=None):

                net = conv2d_same(images, 32, 5, stride=2, scope='conv1')

            for i, spec in enumerate(zip(*net_spec)):

                stride = 2 if i != 0 else 1

                block_spec, n_block = spec
                for j in range(n_block):
                    net = pre_bottleneck_block(net,
                                               block_spec,
                                               stride=stride,
                                               scope='res_%d_%d' %
                                               (i + 1, j + 1))
                    stride = 1

            net = slim.batch_norm(net,
                                  activation_fn=tf.nn.relu,
                                  scope='postnorm')
            net = slim.conv2d(net, 256, 1, stride=1, scope='last_conv')
            net = slim.flatten(net)
            net = slim.fully_connected(
                net,
                128,
                # weights_initializer=tf.truncated_normal_initializer(stddev=0.01),
                activation_fn=None,
                scope='fc5')

            if isinstance(n_class, int):
                net = slim.flatten(net)
                net = slim.fully_connected(net,
                                           n_class,
                                           activation_fn=None,
                                           scope='logits')

            # add summarys
            slim.summarize_collection(tf.GraphKeys.MODEL_VARIABLES)

            return net
    def build_elbo(self, n_samples, training=False):
        cfg = self.config
        reuse = False
        if training:
            reuse = True
        z = self.variational.sample(self.data,
                                    n_samples=n_samples,
                                    reuse=reuse)
        log_q_z = self.variational.log_prob(z, reuse=reuse)
        self.log_q_z = log_q_z
        log_p_x_z = self.model.log_prob(self.data, z, reuse=reuse)
        if cfg['optim/deterministic_annealing'] and training:
            self.build_magnitude()
            tf.summary.scalar('c/magnitude', self.magnitude)
            magnitude = tf.maximum(1., self.magnitude)
            elbo = log_p_x_z - magnitude * log_q_z
        else:
            elbo = log_p_x_z - log_q_z
        if training:
            self.elbo_loss = elbo
            _, variance = tf.nn.moments(elbo, [0])
            self.elbo_variance = tf.reduce_mean(variance)
            self.log_q_z_loss = log_q_z
            self.variational.build_entropy(z)
            self.q_z_sample = z
            slim.summarize_collection('variational')
            slim.summarize_collection('model')
            slim.summarize_activations('variational')
            slim.summarize_activations('model')
        else:
            self.elbo = elbo
            self.log_q_z = log_q_z
            self.log_p_x_hat = (
                tf.reduce_logsumexp(elbo, [0], keep_dims=True) -
                tf.log(float(cfg['q/n_samples_stats'])))
            tf.summary.scalar('o/log_p_x_hat',
                              tf.reduce_mean(self.log_p_x_hat))

            def sum_mean(x):
                return tf.reduce_sum(tf.reduce_mean(x, 0))

            self.elbo_sum = sum_mean(elbo)
            self.q_entropy = -sum_mean(log_q_z)
            self.E_log_lik = sum_mean(log_p_x_z)
            tf.summary.scalar('o/elbo_sum', sum_mean(elbo))
            tf.summary.scalar('o/elbo_mean',
                              sum_mean(elbo) / cfg['batch_size'])
            tf.summary.scalar('o/E_log_q_z', sum_mean(log_q_z))
            tf.summary.scalar('o/E_log_p_x_z', self.E_log_lik)
Exemple #6
0
    def build_summary_ops(self, graph):
        """Build summary ops. Add summaries for variables, weights, biases, activations, and losses.

        Returns:
            summary_op: The (merged) summary op.
            summary_writer: A summary writer.
        """
        # add summaries
        slim.summarize_variables()
        slim.summarize_weights()
        slim.summarize_biases()
        slim.summarize_activations()
        slim.summarize_collection(tf.GraphKeys.LOSSES)

        with tf.name_scope('summary_ops'):
            summary_op = tf.summary.merge_all()
            summary_writer = tf.summary.FileWriter(cfg.DIR.LOG_PATH, graph=graph)
        self.summary_op = summary_op
        self.summary_writer = summary_writer
Exemple #7
0
    def _build_summaries( self ):
        if not self.losses_built:
            raise RuntimeError( "Cannot _build_summaries until 'get_losses' ({0}) and _build_metrics ({1}) is run".format(
                self.losses_built, self.metrics_built ) )

        # add check for losses, metrics built
        if  self.extended_summaries:
            slim.summarize_variables()
            slim.summarize_weights()
            slim.summarize_biases()
            slim.summarize_activations()
        tf.summary.scalar( 'metrics/d_accuracy_on_real', self.real_accuracy )
        tf.summary.scalar( 'metrics/d_accuracy_on_fake', self.fake_accuracy )

        # losses
        slim.summarize_collection(tf.GraphKeys.LOSSES)
        slim.summarize_tensor( self.encoder_regularization_loss )
        slim.summarize_tensor( self.decoder_regularization_loss )
        slim.summarize_tensor( self.discriminator_regularization_loss )
        slim.summarize_tensor( self.loss_g_total ) #, tag='losses/generator_total_loss' )
        slim.summarize_tensor( self.loss_d_total ) #, tag='losses/discriminator_total_loss' )
        self.summaries_built = True
Exemple #8
0
def build_net(images,
              n_class=None,
              is_training=True,
              reuse=False,
              alpha=1.25,
              scope='mobile_id'):

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

        with slim.arg_scope([slim.batch_norm], is_training=is_training):

            net = conv2d_same(images,
                              int(32 * alpha),
                              3,
                              stride=2,
                              scope='conv1')

            net = mobile_block(net,
                               int(16 * alpha),
                               3,
                               stride=1,
                               t=1,
                               scope='mblock1_1')
            net = mobile_block(net,
                               int(16 * alpha),
                               3,
                               stride=1,
                               scope='mblock1_2')

            net = mobile_block(net,
                               int(24 * alpha),
                               3,
                               stride=2,
                               scope='mblock2_1')
            net = mobile_block(net,
                               int(24 * alpha),
                               3,
                               stride=1,
                               scope='mblock2_2')
            net = mobile_block(net,
                               int(24 * alpha),
                               3,
                               stride=1,
                               scope='mblock2_3')

            net = mobile_block(net,
                               int(32 * alpha),
                               3,
                               stride=2,
                               scope='mblock3_1')
            net = mobile_block(net,
                               int(32 * alpha),
                               3,
                               stride=1,
                               scope='mblock3_2')
            net = mobile_block(net,
                               int(32 * alpha),
                               3,
                               stride=1,
                               scope='mblock3_3')
            net = mobile_block(net,
                               int(32 * alpha),
                               3,
                               stride=1,
                               scope='mblock3_4')

            net = mobile_block(net,
                               int(64 * alpha),
                               3,
                               stride=2,
                               scope='mblock4_1')
            for i in range(3):
                net = mobile_block(net,
                                   int(64 * alpha),
                                   3,
                                   stride=1,
                                   scope='mblock4_%d' % (i + 2))

            for i in range(4):
                net = mobile_block(net,
                                   int(96 * alpha),
                                   3,
                                   stride=1,
                                   scope='mblock5_%d' % (i + 1))

            net = mobile_block(net,
                               int(160 * alpha),
                               3,
                               stride=2,
                               scope='mblock6_1')
            for i in range(3):
                net = mobile_block(net,
                                   int(160 * alpha),
                                   3,
                                   stride=1,
                                   scope='mblock6_%d' % (i + 2))

            net = mobile_block(net,
                               int(320 * alpha),
                               3,
                               stride=1,
                               scope='mblock7_1')
            # net = mobile_block(net, int(320*alpha), 3, stride=1, scope='mblock7_2')

            net = slim.conv2d(net, 1280, 1, stride=1, scope='final_point_conv')

            net = slim.flatten(net)
            net = slim.fully_connected(
                net,
                128,
                weights_initializer=tf.truncated_normal_initializer(
                    stddev=0.001),
                normalizer_fn=slim.batch_norm,
                # normalizer_params={'param_initializers': {'gamma': tf.constant_initializer(0.0001)}},
                activation_fn=None,
                scope='fc1')

            if isinstance(n_class, int):
                net = slim.flatten(net)
                net = slim.fully_connected(net,
                                           n_class,
                                           activation_fn=None,
                                           scope='logits')

            # add summarys
            slim.summarize_collection(tf.GraphKeys.MODEL_VARIABLES)

            return net
Exemple #9
0
    def build_model(self,
                    input_imgs,
                    is_training,
                    targets=None,
                    masks=None,
                    privileged_input=None):
        '''Builds the model. Assumes that the input is from range [0, 1].
            Args:
            input_imgs: list of input images (scaled between -1 and 1) with the
                       dimensions specified in the cfg
            is_training: flag for whether the model is in training mode or not
            mask: mask used for computing sum of squares loss. If None, we assume
                  it is np.ones.
        '''
        print('building model')
        cfg = self.cfg
        self.is_training = is_training

        if masks is None:
            masks = tf.constant(1,
                                dtype=tf.float32,
                                shape=[],
                                name='constant_mask')

        net = ResNet50UpProj({'data': input_imgs}, cfg['batch_size'], 1, False)
        decoder_output = net.get_output()
        decoder_output = decoder_output * 128.
        decoder_output = tf.log(decoder_output + 1.) / 11.090354888959125
        #         if self.decoder_only:
        # encoder_output = input_imgs Assume that the input is the representation
        # else:
        # encoder_output = self.build_encoder(input_imgs, is_training)
        # print("enc:", encoder_output.shape)
        # decoder_output = self.build_decoder(encoder_output, is_training)
        # print("tar:", targets.shape)

        # set up losses
        if targets is None:
            losses = self.get_losses(decoder_output, input_imgs, masks)
        else:
            losses = self.get_losses(decoder_output, targets, masks)

        # use weight regularization
        if 'omit_weight_reg' in cfg and cfg['omit_weight_reg']:
            add_reg = False
        else:
            add_reg = True

        # get losses
        #regularization_loss = tf.add_n( slim.losses.get_regularization_losses(), name='losses/regularization_loss' )
        #total_loss = slim.losses.get_total_loss( add_regularization_losses=add_reg,
        #                                         name='losses/total_loss')

        self.input_images = input_imgs
        self.target_images = targets
        self.targets = targets
        self.masks = masks
        self.decoder_output = decoder_output
        self.losses = losses
        self.total_loss = losses[0]
        # self.init_op = tf.global_variables_initializer()

        # add summaries
        if self.extended_summaries:
            slim.summarize_variables()
            slim.summarize_weights()
            slim.summarize_biases()
            slim.summarize_activations()
        slim.summarize_collection(tf.GraphKeys.LOSSES)
        #slim.summarize_tensor( regularization_loss )
        #slim.summarize_tensor( total_loss )
        self.model_built = True
Exemple #10
0
def run_test_train_dataset(selected_model):

    #Create the sess, and use some options for better using gpu
    print("Create session")
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True  # Do not assign whole gpu memory, just use it on the go
    config.allow_soft_placement = True  # If a operation is not define it the default device, let it execute in another.
    sess = tf.Session(config=config)

    with tf.device('/cpu:0'):
        #input setup
        #train dataset setup
        train_dataset = train_dataset_prepare(FLAGS)
        train_iterator = train_dataset.make_one_shot_iterator()
        #test dataset setup
        test_dir = '/scratch_net/ofsoundof/yawli/BSD68/'
        test_iterator = test_dataset_prepare(
            test_path_prepare, test_dir,
            FLAGS.sigma).make_initializable_iterator()
        #create dataset handle and iterator
        handle = tf.placeholder(tf.string, shape=[])
        iterator = tf.data.Iterator.from_string_handle(
            handle, train_dataset.output_types, train_dataset.output_shapes)
        image_gt, image_n = iterator.get_next()

    with tf.device('/device:GPU:' + FLAGS.sge_gpu):
        image_gt = image_gt / 255 * MAX_RGB
        image_n = image_n / 255 * MAX_RGB

        global_step = tf.Variable(1, name='global_step', trainable=False)
        image_dn = selected_model(image_n, FLAGS)
        MSE_dn, PSNR_dn = comp_mse_psnr(image_dn, image_gt, MAX_RGB)
        MSE_n, PSNR_n = comp_mse_psnr(image_n, image_gt, MAX_RGB)
        PSNR_gain = PSNR_dn - PSNR_n
        loss = tf.reduce_sum(tf.squared_difference(
            image_gt, image_dn)) + tf.reduce_sum(
                tf.losses.get_regularization_losses())
        train_op, global_step = training(loss, FLAGS, global_step)

    #train summary
    tf.summary.scalar('MSE_dn', MSE_dn)
    tf.summary.scalar('PSNR_dn', PSNR_dn)
    tf.summary.scalar('MSE_n', MSE_n)
    tf.summary.scalar('PSNR_n', PSNR_n)
    tf.summary.scalar('PSNR_gain', PSNR_gain)
    slim.summarize_collection(collection=tf.GraphKeys.TRAINABLE_VARIABLES)
    #test summaries
    psnr_validate = tf.placeholder(tf.float32)
    tf.summary.scalar('psnr_validate', psnr_validate)
    merged = tf.summary.merge_all()

    #Get the dataset handle
    train_handle = sess.run(train_iterator.string_handle())
    test_handle = sess.run(test_iterator.string_handle())

    print("Create checkpoint directory")
    # if not FLAGS.restore:
    FLAGS.checkpoint = FLAGS.checkpoint + '_' + datetime.datetime.now(
    ).strftime("%Y-%m-%d-%H-%M-%S")
    with open('checkpoint.txt', 'w'
              ) as text_file:  #save at the current directory, used for testing
        text_file.write(FLAGS.checkpoint)
    LOG_DIR = os.path.join('/scratch_net/ofsoundof/yawli/logs',
                           FLAGS.checkpoint)
    # LOG_DIR = os.path.join('/home/yawli/Documents/hashnets/logs', FLAGS.checkpoint )
    # assert (not os.path.exists(LOG_DIR)), 'LOG_DIR %s already exists'%LOG_DIR

    print("Create summary file writer")
    train_writer = tf.summary.FileWriter(os.path.join(LOG_DIR, 'train'),
                                         sess.graph)

    print("Create saver")
    saver = tf.train.Saver(max_to_keep=100)

    #Always init, then optionally restore
    print("Initialization")
    sess.run(tf.global_variables_initializer())

    if FLAGS.restore:
        print('Restore model from checkpoint {}'.format(
            tf.train.latest_checkpoint(LOG_DIR)))
        saver.restore(sess, tf.train.latest_checkpoint(LOG_DIR))
        checkpoint_filename = 'checkpoint'
    else:
        checkpoint_filename = 'checkpoint'
    # checkpoint_filename = 'checkpoint'
    score_all = []
    model_index = []
    image_path_dn, _ = test_path_prepare(test_dir, FLAGS.sigma)
    image_path_dn = [
        os.path.join(LOG_DIR, os.path.basename(i)) for i in image_path_dn
    ]
    i = sess.run(global_step) + 1
    print("Start iteration")
    while i <= FLAGS.max_iter:
        if i % FLAGS.summary_interval == 0:
            # test set5
            sess.run(test_iterator.initializer)
            score_per = np.zeros((4, 69))
            for j in range(68):

                img_dn, img_n, img_gt = sess.run(
                    [image_dn, image_n, image_gt],
                    feed_dict={handle: test_handle})
                scipy.misc.toimage(np.squeeze(img_dn) * 255 / MAX_RGB,
                                   cmin=0,
                                   cmax=255).save(image_path_dn[j])
                # The current scipy version started to normalize all images so that min(data) become black and max(data)
                # become white. This is unwanted if the data should be exact grey levels or exact RGB channels.
                # The solution: scipy.misc.toimage(image_array, cmin=0.0, cmax=255).save('...') or use imageio library
                # from IPython import embed; embed();
                score = image_converter(np.squeeze(img_dn), np.squeeze(img_n),
                                        np.squeeze(img_gt), False, 0, MAX_RGB)
                score_per[:, j] = score
            score_per[:, -1] = np.mean(score_per[:, :-1], axis=1)
            print('PSNR results for Set5: SR {}, Bicubic {}'.format(
                score_per[1, -1], score_per[0, -1]))

            time_start = time.time()
            [_, i, l, mse_dn, mse_n, psnr_dn, psnr_n, psnr_gain, summary] =\
                sess.run([train_op, global_step, loss, MSE_dn, MSE_n, PSNR_dn, PSNR_n, PSNR_gain, merged],
                         feed_dict={handle: train_handle, psnr_validate: score_per[1, -1]})#, options=options, run_metadata=run_metadata)
            # from IPython import embed; embed(); exit()
            duration = time.time() - time_start
            train_writer.add_summary(summary, i)
            print(
                "Iter %d, total loss %.5f; denoise (mse %.5f, psnr %.5f); noise (mse %.5f, psnr %.5f); psnr_gain:%f"
                % (i - 1, l, mse_dn, psnr_dn, mse_n, psnr_n, psnr_gain))
            print('Training time for one iteration: {}'.format(duration))

            if (i - 1) % FLAGS.checkpoint_interval == 0:
                save_path = saver.save(sess,
                                       os.path.join(LOG_DIR, 'model'),
                                       global_step=i,
                                       latest_filename=checkpoint_filename,
                                       write_meta_graph=True)
                print("Saved checkpoint to {}".format(save_path))
                print("Flushing train writer")
                train_writer.flush()

            model_index.append(i)
            score_all.append([
                score_per[0, -1], score_per[1, -1],
                score_per[1, -1] - score_per[0, -1], score_per[2, -1],
                score_per[3, -1], score_per[3, -1] - score_per[2, -1]
            ])
        else:
            [_, i, _] = sess.run([train_op, global_step, loss],
                                 feed_dict={handle: train_handle})

    #write to csv files
    descriptor5 = 'Average PSNR (dB)/SSIM for Set5, Scale ' + str(
        FLAGS.upscale) + ', different model parameters during training' + '\n'
    descriptor = [descriptor5, '', '', '', '', '', '']
    header_mul = ['Iteration'] + ['N', 'DN', 'Gain'] * 2
    model_index_array = np.expand_dims(np.array(model_index), axis=0)
    score_all_array = np.transpose(np.array(score_all))
    written_content = np.concatenate((model_index_array, score_all_array),
                                     axis=0)
    written_content_fmt = ['{:<8}'] + ['{:<8.4f}', '{:<8.4f}', '{:<8.4f}'] * 2
    content = content_generator_mul_line(descriptor, written_content,
                                         written_content_fmt, header_mul)
    file_writer(os.path.join(LOG_DIR, 'psnr_ssim.csv'), 'a', content)

    #write to pickle files
    variables = {
        'index': np.array(model_index),
        'noise': score_all_array[0, :],
        'denoise': score_all_array[1, :],
        'gain': score_all_array[2, :]
    }
    pickle_out = open(os.path.join(LOG_DIR, 'psnr_bsd68.pkl'), 'wb')
    pickle.dump(variables, pickle_out)
    pickle_out.close()
Exemple #11
0
    def build_model(self,
                    input_imgs,
                    is_training,
                    targets,
                    masks=None,
                    privileged_input=None):
        '''Builds the model. Assumes that the input is from range [0, 1].
        Args:
            input_imgs: list of input images (scaled between -1 and 1) with the
                       dimensions specified in the cfg
            is_training: flag for whether the model is in training mode or not
            mask: mask used for computing sum of squares loss. If None, we assume
                  it is np.ones.
        '''
        print('building model')
        cfg = self.cfg
        self.is_training = is_training

        if self.decoder_only:
            encoder_output = input_imgs  # Assume that the input is the representation
        else:
            encoder_output = self.build_encoder(input_imgs, is_training)

        final_output_12 = self.build_siamese_output_postprocess(
            encoder_output, is_training, scope="three_layer_fc_network12")

        final_output_23 = self.build_siamese_output_postprocess(
            encoder_output, is_training, scope="three_layer_fc_network23")

        final_output_13 = self.calculate_combined_relative_camera_pose(
            self.denormalize_fixated_camera_pose(final_output_12),
            self.denormalize_fixated_camera_pose(final_output_23))

        final_output_13 = self.normalize_fixated_camera_pose(final_output_13)

        #final_output = tf.concat(1, [final_output_12, final_output_13, final_output_23])

        target12 = tf.slice(targets, [0, 0], [self.cfg['batch_size'], 6])
        target13 = tf.slice(targets, [0, 6], [self.cfg['batch_size'], 6])
        target23 = tf.slice(targets, [0, 12], [self.cfg['batch_size'], 6])

        final_output = [final_output_12, final_output_13, final_output_23]
        target_total = [target12, target13, target23]

        losses = self.get_losses(final_output,
                                 target_total,
                                 is_softmax='l2_loss' not in cfg)
        # use weight regularization
        if 'omit_weight_reg' in cfg and cfg['omit_weight_reg']:
            add_reg = False
        else:
            add_reg = True

        # get losses
        regularization_loss = tf.add_n(slim.losses.get_regularization_losses(),
                                       name='losses/regularization_loss')
        total_loss = slim.losses.get_total_loss(
            add_regularization_losses=add_reg, name='losses/total_loss')

        self.input_images = input_imgs
        self.targets = targets
        self.encoder_output = encoder_output
        self.losses = losses
        self.total_loss = total_loss
        self.decoder_output = final_output
        # add summaries
        if self.extended_summaries:
            slim.summarize_variables()
            slim.summarize_weights()
            slim.summarize_biases()
            slim.summarize_activations()
        slim.summarize_collection(tf.GraphKeys.LOSSES)
        tf.summary.scalar('accuracy', self.accuracy)
        slim.summarize_tensor(regularization_loss)
        slim.summarize_tensor(total_loss)
        self.model_built = True
Exemple #12
0
def build_net(images,
              n_class=None,
              is_training=True,
              reuse=False,
              alpha=1,
              scope='mobile_id'):

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

        with slim.arg_scope([slim.batch_norm], is_training=is_training):

            net = conv2d_same(images,
                              int(64 * alpha),
                              3,
                              stride=2,
                              scope='conv1')

            net = slim.separable_conv2d(net,
                                        None,
                                        3,
                                        1,
                                        stride=1,
                                        padding='SAME',
                                        scope='depth_conv1')

            net = mobile_block(net,
                               int(64 * alpha),
                               3,
                               stride=2,
                               t=2,
                               scope='mblock1_1')
            for i in range(4):
                net = mobile_block(net,
                                   int(64 * alpha),
                                   3,
                                   stride=1,
                                   t=2,
                                   scope='mblock1_%d' % (i + 2))

            net = mobile_block(net,
                               int(128 * alpha),
                               3,
                               stride=2,
                               t=4,
                               scope='mblock2_1')
            for i in range(6):
                net = mobile_block(net,
                                   int(128 * alpha),
                                   3,
                                   stride=1,
                                   t=2,
                                   scope='mblock2_%d' % (i + 2))

            net = mobile_block(net,
                               int(128 * alpha),
                               3,
                               stride=2,
                               t=4,
                               scope='mblock3_1')
            for i in range(2):
                net = mobile_block(net,
                                   int(128 * alpha),
                                   3,
                                   stride=1,
                                   t=2,
                                   scope='mblock3_%d' % (i + 2))

            net = slim.conv2d(net, 512, 1, stride=1, scope='final_point_conv')

            net = slim.separable_conv2d(net,
                                        None, [7, 6],
                                        1,
                                        stride=1,
                                        padding='VALID',
                                        scope='GDConv')
            print(net.shape)

            net = slim.flatten(net)
            net = slim.fully_connected(
                net,
                128,
                # weights_initializer=tf.truncated_normal_initializer(stddev=0.001),
                normalizer_fn=slim.batch_norm,
                weights_regularizer=slim.l2_regularizer(4e-4),
                # normalizer_params={'param_initializers': {'gamma': tf.constant_initializer(0.1)}},
                activation_fn=None,
                scope='fc1')

            if isinstance(n_class, int):
                net = slim.flatten(net)
                net = slim.fully_connected(net,
                                           n_class,
                                           activation_fn=None,
                                           scope='logits')

            # add summarys
            slim.summarize_collection(tf.GraphKeys.MODEL_VARIABLES)

            return net