Ejemplo n.º 1
0
def feature_to_image_upconv(features, skip_features, variables, batch_size, phase, train_config):

    output = features
    for i in range(0, len(variables)):
        layer_id_v = "v_%s_%i" % ('feat2img', i)
        sh = output.get_shape().as_list()
        output_shape = [sh[1]*variables[i].stride[1],
                        sh[2]*variables[i].stride[2]]
        if i < len(skip_features):
            output = layers.layer_upconv2d_v2(layer_id_v,
                                                   variables[i], batch_size,
                                                   tf.concat([output,skip_features[i]],axis = -1),
                                                   phase,
                                                   train_config).out
        else:
            output = layers.layer_upconv2d_v2(layer_id_v,
                                                   variables[i],batch_size,
                                                   output,
                                                   phase,
                                                   train_config).out

    return output
Ejemplo n.º 2
0
    def create_2D_decoder(self, decoder_config):

        decoder = dict()
        decoder_id = decoder_config['id']
        ids = []
        for i in range(0, len(self.config.layer_config)):
            ids.append(self.config.layer_config[i]['id'])
        pos_layout = ids.index(decoder_id)
        print('creating decoder pipeline ' + decoder_id)

        # create a decoder pathway (center view only)
        with tf.variable_scope(decoder_id):

            decoder['id'] = decoder_id
            decoder['channels'] = decoder_config['channels']
            decoder['loss_fn'] = decoder_config['loss_fn']
            decoder['weight'] = decoder_config['weight']
            decoder['train'] = decoder_config['train']
            decoder['preferred_gpu'] = decoder_config['preferred_gpu']
            decoder['2D_variables'] = []
            decoder['concat_variables'] = []
            decoder['upscale_variables'] = []
            decoder['start'] = self.config.layer_config[pos_layout]['start']
            decoder['end'] = self.config.layer_config[pos_layout]['end']
            decoder['no_relu'] = decoder_config['no_relu']

            sh = self.encoders_3D[decoder_id]['feature_shape']
            decoder['nodes'] = sh[2] * sh[3] * sh[4] * self.config.patch_weight
            if self.config.layers['merge_encoders']:
                decoder['upconv_in'] = layers.bn_dense(
                    self.merged['features_transposed'],
                    self.merged['encoder_nodes'], decoder['nodes'], self.phase,
                    self.config.training, 'bn_decoder_' + decoder_id + '_in')
            else:

                decoder['upconv_in'] = layers.bn_dense(
                    self.encoders_3D[decoder_id]['features_transposed'],
                    self.encoders_3D[decoder_id]['encoder_nodes'],
                    decoder['nodes'], self.phase, self.config.training,
                    'bn_decoder_' + decoder_id + '_in')

            sh = self.encoders_3D[decoder_id]['feature_shape']
            decoder['upconv'] = tf.reshape(
                decoder['upconv_in'],
                [-1, sh[2], sh[3], sh[4] * self.config.patch_weight])
            decoder['layers'] = []

            ########################################################################################################
            # decoder variables
            layout = []
            layout.insert(0, self.config.layer_config[pos_layout]['layout'][0])
            for i in range(0, len(self.config.layers['encoder_3D'])):
                layout.append(self.config.layers['encoder_3D'][i])

            last_layer = min(len(layout), self.max_layer)
            patches = self.config.nviews
            for i in range(0, last_layer):
                layer_id_cat = "skip_patch_%s_%i" % (decoder_id, i)
                print('    generating decoder ' + layer_id_cat)
                patches = math.ceil(patches / layout[i]['stride'][1])
                decoder['concat_variables'].append(
                    layers.concat_variables(layer_id_cat,
                                            layout[i],
                                            patches,
                                            input_features=[],
                                            output_features=[]))
            # maybe change later
            layer_id_cat = "skip_patch_%s_%i" % (decoder_id, i + 1)
            decoder['concat_variables'].insert(
                0,
                layers.concat_variables(
                    layer_id_cat,
                    layout[i],
                    self.config.nviews,
                    input_features=self.config.layer_config[pos_layout]
                    ['layout'][0]['conv'][-2],
                    output_features=self.config.layer_config[pos_layout]
                    ['upscale'][0]['conv'][-2]))

            layout[0] = self.config.layer_config[pos_layout]['upscale'][0]
            for i in range(0, last_layer):
                layer_id = "decoder_%s_%i" % (decoder_id, i)
                print('    generating upconvolution variables ' + layer_id)
                decoder['2D_variables'].append(
                    layers.decoder_variables_2D(layer_id, layout[i], i,
                                                last_layer,
                                                self.config.patch_weight,
                                                self.config.D))

            for i in range(0, last_layer):
                layer_id = "decoder_%s_layer%i" % (decoder_id,
                                                   last_layer - i - 1)
                layer_id_cat = "skip_patch_%s_layer%i" % (decoder_id,
                                                          last_layer - i - 1)
                print('    generating upconvolution layer structure ' +
                      layer_id)
                out_channels = -1
                # evil hack
                no_relu = False
                decoder['layers'].insert(
                    -1 - i,
                    layers.layer_upconv2d_v2(layer_id,
                                             decoder['2D_variables'][-1 - i],
                                             self.batch_size,
                                             decoder['upconv'],
                                             self.phase,
                                             self.config.training,
                                             out_channels=out_channels,
                                             no_relu=no_relu))
                if self.pinhole:
                    if i != last_layer - 1:
                        concat_cv = layers.layer_conv_one(
                            layer_id_cat, decoder['concat_variables'][-2 - i],
                            layers.layer_concat(
                                self.encoders_3D[decoder_id]['conv_layers_v'][
                                    -2 - i].out, self.encoders_3D[decoder_id]
                                ['conv_layers_h'][-2 - i].out).out).out
                    else:
                        start = self.config.layer_config[pos_layout]['start']
                        end = self.config.layer_config[pos_layout]['end']
                        concat_cv = layers.layer_conv_one(
                            layer_id_cat, decoder['concat_variables'][-2 - i],
                            layers.layer_concat(
                                self.stack_v[:, :, :, :, start:end],
                                self.stack_h[:, :, :, :, start:end]).out).out
                    decoder['upconv'] = tf.concat(
                        [decoder['layers'][-1 - i].out, concat_cv], axis=3)
                else:
                    decoder['upconv'] = decoder['layers'][-1 - i].out

            # decoder['layers'] = tf.reverse(decoder['layers'],0)
            decoder['upconv_reduce'] = decoder[
                'upconv'][:, loss_min_coord_2D:loss_max_coord_2D,
                          loss_min_coord_2D:loss_max_coord_2D, :]

            decoder['input'] = tf.placeholder(
                tf.float32, [None, self.H_HR, self.W_HR, decoder['channels']])
            decoder['input_reduce'] = decoder[
                'input'][:, loss_min_coord_2D:loss_max_coord_2D_1,
                         loss_min_coord_2D:loss_max_coord_2D_1, :]

            layout = []
            for i in range(0, len(self.config.layers['upscale'])):
                layout.append(self.config.layers['upscale'][i])
            last_layer_up = min(len(layout), self.max_layer)
            for i in range(0, last_layer_up):
                layer_id_upscale = "upscale_%s_%i" % (decoder_id, i)
                print('    creating variables for ' + layer_id_upscale)
                decoder['upscale_variables'].append(
                    layers.upscale_variables(layer_id_upscale, layout[i]))

            layout_final = self.config.layer_config[pos_layout]['final'][0]

            no_relu = False
            for upscale in range(0, last_layer_up):
                out_channels = -1
                layer_id = "upscale_%s_%i" % (decoder_id, upscale)
                print('    creating layers for ' + layer_id)
                decoder['upconv'] = layers.layer_upconv2d_v2(
                    layer_id,
                    decoder['upscale_variables'][upscale],
                    self.batch_size,
                    decoder['upconv'],
                    self.phase,
                    self.config.training,
                    out_channels=out_channels,
                    no_relu=no_relu).out

            no_relu = decoder['no_relu']
            decoder['SR'] = layers.layer_pure_conv2D("upscale_final",
                                                     layout_final,
                                                     decoder['upconv'],
                                                     self.phase,
                                                     self.config.training,
                                                     no_relu=no_relu).out

            self.decoders_2D[decoder_id] = decoder