def image_to_feature(view_proc, input, variables, phase, train_config): features = input if view_proc == '3D': for i in range(0, len(variables)): layer_id_v = "v_%s_%i" % ('img2feat', i) # print(' generating convolutional layer structure for %s %i' % ('img2feat', i)) features = layers.layer_conv3d(layer_id_v, variables[i], features, phase, train_config).out features_cat = tf.concat([features[:, 0, :, :, :], features[:, 1, :, :, :], features[:, 2, :, :, :]], axis=-1) elif view_proc == '2D': for i in range(0, len(variables)): layer_id_v = "v_%s_%i" % ('img2feat', i) # print(' generating convolutional layer structure for %s %i' % ('img2feat', i)) features = layers.layer_conv2d(layer_id_v, variables[i], features, phase, train_config).out features_cat = features return features_cat
def create_encoder_layers(self): print(' initializing model layer structure') layout = self.config.layers['autoencoder'] # followup layers have same kernel sizes with tf.device('/device:GPU:%i' % (self.config.layers['preferred_gpu'])): if self.features_v == None: self.features_v = self.stack_v self.features_v = tf.reshape(self.features_v, self.stack_shape) if self.features_h == None: self.features_h = self.stack_h self.features_h = tf.reshape(self.features_h, self.stack_shape) first_layer = len(self.conv_layers_v) last_layer = min(len(layout), self.max_layer) for i in range(first_layer, last_layer): layer_id_v = "v_ae_%i" % i layer_id_h = "h_ae_%i" % i print(' generating downconvolution layer structure ' + layer_id_v) # setup encoder layer and connect to previous one self.conv_layers_v.append( layers.layer_conv3d(layer_id_v, self.encoder_variables[i], self.features_v, self.phase, self.config.training)) # setup encoder layer and connect to previous one self.conv_layers_h.append( layers.layer_conv3d(layer_id_h, self.encoder_variables[i], self.features_h, self.phase, self.config.training)) # update layer shapes self.encoder_variables[i].input_shape = self.conv_layers_v[ i].input_shape self.encoder_variables[i].output_shape = self.conv_layers_v[ i].output_shape # final encoder layer: vertical/horizontal features self.features_v = self.conv_layers_v[-1].out self.features_h = self.conv_layers_h[-1].out
def create_3D_encoder(self, encoder_config): encoder = dict() encoder_id = encoder_config['id'] ids = [] for i in range(0, len(self.config.layer_config)): ids.append(self.config.layer_config[i]['id']) pos = ids.index(encoder_id) layout = [] layout.insert(0, self.config.layer_config[pos]['layout'][0]) for i in range(0, len(self.config.layers['encoder_3D'])): layout.append(self.config.layers['encoder_3D'][i]) print('creating encoder pipeline for ' + encoder_id) with tf.variable_scope(encoder_id): encoder['id'] = encoder_id encoder['channels'] = encoder_config['channels'] encoder['preferred_gpu'] = encoder_config['preferred_gpu'] encoder['variables'] = [] encoder['features_v'] = None encoder['features_h'] = None encoder['conv_layers_v'] = [] encoder['conv_layers_h'] = [] #################################################################################################### # create encoder variables last_layer = min(len(layout), self.max_layer) for i in range(0, last_layer): layer_id = "encoder_%i" % i print(' creating 3D encoder variables ' + layer_id) encoder['variables'].append( layers.encoder_variables(layer_id, layout[i])) #################################################################################################### # create 3D encoder layers for stacks start = self.config.layer_config[pos]['start'] end = self.config.layer_config[pos]['end'] shape = [ self.stack_shape[0], self.stack_shape[1], self.stack_shape[2], self.stack_shape[3], encoder['channels'] ] if encoder['features_v'] == None: encoder['features_v'] = self.stack_v[:, :, :, :, start:end] encoder['features_v'] = tf.reshape( encoder['features_v'], shape) # why we need to reshape ? if encoder['features_h'] == None: encoder['features_h'] = self.stack_h[:, :, :, :, start:end] encoder['features_h'] = tf.reshape(encoder['features_h'], shape) print(' CREATING 3D encoder layers for %s ' % encoder_id) for i in range(0, last_layer): layer_id_v = "v_%s_%i" % (encoder_id, i) layer_id_h = "h_%s_%i" % (encoder_id, i) print( ' generating downconvolution layer structure for %s %i' % (encoder_id, i)) encoder['conv_layers_v'].append( layers.layer_conv3d(layer_id_v, encoder['variables'][i], encoder['features_v'], self.phase, self.config.training)) encoder['conv_layers_h'].append( layers.layer_conv3d(layer_id_h, encoder['variables'][i], encoder['features_h'], self.phase, self.config.training)) # update layer shapes encoder['variables'][i].input_shape = encoder['conv_layers_v'][ i].input_shape encoder['variables'][i].output_shape = encoder[ 'conv_layers_v'][i].output_shape # final encoder layer: vertical/horizontal features encoder['features_v'] = encoder['conv_layers_v'][-1].out encoder['features_h'] = encoder['conv_layers_h'][-1].out #################################################################################################### # create dense layers print(' creating dense layers for %s' % encoder_id) encoder['feature_shape'] = encoder['features_v'].shape.as_list() sh = encoder['feature_shape'] encoder['encoder_input_size'] = sh[1] * sh[2] * sh[3] * sh[4] # setup shared feature space between horizontal/vertical encoder encoder['features'] = tf.concat([ tf.reshape(encoder['features_h'], [-1, encoder['encoder_input_size']]), tf.reshape(encoder['features_v'], [-1, encoder['encoder_input_size']]) ], 1) encoder['features_transposed'] = tf.concat([ tf.reshape( tf.transpose(encoder['features_h'], [0, 1, 3, 2, 4]), [-1, encoder['encoder_input_size']]), tf.reshape(encoder['features_v'], [-1, encoder['encoder_input_size']]) ], 1) encoder['encoder_nodes'] = encoder['features'].shape.as_list()[1] # with tf.variable_scope('input'): # encoder['input_features'] = tf.placeholder(tf.float32, encoder['features'].shape.as_list()) self.encoders_3D[encoder_id] = encoder