我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用keras.backend.shape()。
def mean_acc(y_true, y_pred): s = K.shape(y_true) # reshape such that w and h dim are multiplied together y_true_reshaped = K.reshape( y_true, tf.stack( [-1, s[1]*s[2], s[-1]] ) ) y_pred_reshaped = K.reshape( y_pred, tf.stack( [-1, s[1]*s[2], s[-1]] ) ) # correctly classified clf_pred = K.one_hot( K.argmax(y_pred_reshaped), nb_classes = s[-1]) equal_entries = K.cast(K.equal(clf_pred,y_true_reshaped), dtype='float32') * y_true_reshaped correct_pixels_per_class = K.sum(equal_entries, axis=1) n_pixels_per_class = K.sum(y_true_reshaped,axis=1) acc = correct_pixels_per_class / n_pixels_per_class acc_mask = tf.is_finite(acc) acc_masked = tf.boolean_mask(acc,acc_mask) return K.mean(acc_masked)
def __init__(self, output_dim, num_senses, num_hyps, use_attention=False, return_attention=False, **kwargs): # Set output_dim in kwargs so that we can pass it along to LSTM's init kwargs['output_dim'] = output_dim self.num_senses = num_senses self.num_hyps = num_hyps self.use_attention = use_attention self.return_attention = return_attention super(OntoAttentionLSTM, self).__init__(**kwargs) # Recurrent would have set the input shape to cause the input dim to be 3. Change it. self.input_spec = [InputSpec(ndim=5)] if self.consume_less == "cpu": # In the LSTM implementation in Keras, consume_less = cpu causes all gates' inputs to be precomputed # and stored in memory. However, this doesn't work with OntoLSTM since the input to the gates is # dependent on the previous timestep's output. warnings.warn("OntoLSTM does not support consume_less = cpu. Changing it to mem.") self.consume_less = "mem" #TODO: Remove this dependency. if K.backend() == "tensorflow" and not self.unroll: warnings.warn("OntoLSTM does not work with unroll=False when backend is TF. Changing it to True.") self.unroll = True
def build(self, input_shape): self.input_spec = [InputSpec(shape=input_shape)] input_dim = input_shape[-1] reader_input_shape = self.get_reader_input_shape(input_shape) print >>sys.stderr, "NSE reader input shape:", reader_input_shape writer_input_shape = (input_shape[0], 1, self.output_dim * 2) # Will process one timestep at a time print >>sys.stderr, "NSE writer input shape:", writer_input_shape composer_input_shape = self.get_composer_input_shape(input_shape) print >>sys.stderr, "NSE composer input shape:", composer_input_shape self.reader.build(reader_input_shape) self.writer.build(writer_input_shape) self.composer.build(composer_input_shape) # Aggregate weights of individual components for this layer. reader_weights = self.reader.trainable_weights writer_weights = self.writer.trainable_weights composer_weights = self.composer.trainable_weights self.trainable_weights = reader_weights + writer_weights + composer_weights if self.initial_weights is not None: self.set_weights(self.initial_weights) del self.initial_weights
def step(self, input_t, states): reader_states = states[:2] flattened_mem_tm1, flattened_shared_mem_tm1 = states[2:4] writer_h_tm1, writer_c_tm1 = states[4:] input_mem_shape = K.shape(flattened_mem_tm1) mem_shape = (input_mem_shape[0], input_mem_shape[1]/self.output_dim, self.output_dim) mem_tm1 = K.reshape(flattened_mem_tm1, mem_shape) shared_mem_tm1 = K.reshape(flattened_shared_mem_tm1, mem_shape) reader_constants = self.reader.get_constants(input_t) reader_states += reader_constants o_t, [_, reader_c_t] = self.reader.step(input_t, reader_states) z_t, m_rt = self.summarize_memory(o_t, mem_tm1) shared_z_t, shared_m_rt = self.summarize_memory(o_t, shared_mem_tm1) c_t = self.compose_memory_and_output([o_t, m_rt, shared_m_rt]) # Collecting the necessary variables to directly call writer's step function. writer_constants = self.writer.get_constants(c_t) # returns dropouts for W and U (all 1s, see init) writer_states = [writer_h_tm1, writer_c_tm1] + writer_constants # Making a call to writer's step function, Equation 5 h_t, [_, writer_c_t] = self.writer.step(c_t, writer_states) # h_t, writer_c_t: (batch_size, output_dim) mem_t = self.update_memory(z_t, h_t, mem_tm1) shared_mem_t = self.update_memory(shared_z_t, h_t, shared_mem_tm1) return h_t, [o_t, reader_c_t, K.batch_flatten(mem_t), K.batch_flatten(shared_mem_t), h_t, writer_c_t]
def call(self, inputs, mask=None, initial_state=None, training=None): inputs_shape = K.shape(inputs) zeros = tf.zeros( shape=[ inputs_shape[0], inputs_shape[1] - 1, self.layer.units ] ) outputs = self.layer.call( inputs=inputs, mask=mask, initial_state=initial_state, training=training ) outputs = K.reshape( tf.slice(outputs, [0, inputs_shape[1] - 1, 0], [-1, 1, -1]), shape=(inputs_shape[0], 1, self.layer.units) ) outputs = K.concatenate([outputs, zeros], axis=1) if 0 < self.layer.dropout + self.layer.recurrent_dropout: outputs._uses_learning_phase = True return outputs
def step(self, inputs, states): h_tm1 = states[0] # previous memory #B_U = states[1] # dropout matrices for recurrent units #B_W = states[2] h_tm1a = K.dot(h_tm1, self.Wa) eij = K.dot(K.tanh(h_tm1a + K.dot(inputs[:, :self.h_dim], self.Ua)), self.Va) eijs = K.repeat_elements(eij, self.h_dim, axis=1) #alphaij = K.softmax(eijs) # batchsize * lenh h batchsize * lenh * ndim #ci = K.permute_dimensions(K.permute_dimensions(self.h, [2,0,1]) * alphaij, [1,2,0]) #cisum = K.sum(ci, axis=1) cisum = eijs*inputs[:, :self.h_dim] #print(K.shape(cisum), cisum.shape, ci.shape, self.h.shape, alphaij.shape, x.shape) zr = K.sigmoid(K.dot(inputs[:, self.h_dim:], self.Wzr) + K.dot(h_tm1, self.Uzr) + K.dot(cisum, self.Czr)) zi = zr[:, :self.units] ri = zr[:, self.units: 2 * self.units] si_ = K.tanh(K.dot(inputs[:, self.h_dim:], self.W) + K.dot(ri*h_tm1, self.U) + K.dot(cisum, self.C)) si = (1-zi) * h_tm1 + zi * si_ return si, [si] #h_tm1, [h_tm1]
def build(self, input_shape): if isinstance(input_shape, list): input_shape = input_shape[0] embedding_size = input_shape[-1] / 2 # Create a trainable weight variable for this layer. # input_shape is bidirectional RNN input shape # kernel shape (mp_dim * 2 * self.strategy, embedding_size) self.kernel = self.add_weight((self.mp_dim, embedding_size * 2 * self.strategy), name='kernel', initializer='glorot_uniform', trainable=True) self.kernel_full_fw = self.kernel[:, :embedding_size] self.kernel_full_bw = self.kernel[:, embedding_size: embedding_size * 2] self.kernel_attentive_fw = self.kernel[:, embedding_size * 2: embedding_size * 3] self.kernel_attentive_bw = self.kernel[:, embedding_size * 3: embedding_size * 4] self.kernel_max_attentive_fw = self.kernel[:, embedding_size * 4: embedding_size * 5] self.kernel_max_attentive_bw = self.kernel[:, embedding_size * 5: embedding_size * 6] self.kernel_max_pool_fw = self.kernel[:, embedding_size * 6: embedding_size * 7] self.kernel_max_pool_bw = self.kernel[:, embedding_size * 7:] self.built = True super(MultiPerspective, self).build(input_shape)
def _cosine_matrix(self, x1, x2): """Cosine similarity matrix. Calculate the cosine similarities between each forward (or backward) contextual embedding h_i_p and every forward (or backward) contextual embeddings of the other sentence # Arguments x1: (batch_size, x1_timesteps, embedding_size) x2: (batch_size, x2_timesteps, embedding_size) # Output shape (batch_size, x1_timesteps, x2_timesteps) """ # expand h1 shape to (batch_size, x1_timesteps, 1, embedding_size) x1 = K.expand_dims(x1, axis=2) # expand x2 shape to (batch_size, 1, x2_timesteps, embedding_size) x2 = K.expand_dims(x2, axis=1) # cosine matrix (batch_size, h1_timesteps, h2_timesteps) cos_matrix = self._cosine_similarity(x1, x2) return cos_matrix
def _mean_attentive_vectors(self, x2, cosine_matrix): """Mean attentive vectors. Calculate mean attentive vector for the entire sentence by weighted summing all the contextual embeddings of the entire sentence # Arguments x2: sequence vectors, (batch_size, x2_timesteps, embedding_size) cosine_matrix: cosine similarities matrix of x1 and x2, (batch_size, x1_timesteps, x2_timesteps) # Output shape (batch_size, x1_timesteps, embedding_size) """ # (batch_size, x1_timesteps, x2_timesteps, 1) expanded_cosine_matrix = K.expand_dims(cosine_matrix, axis=-1) # (batch_size, 1, x2_timesteps, embedding_size) x2 = K.expand_dims(x2, axis=1) # (batch_size, x1_timesteps, embedding_size) weighted_sum = K.sum(expanded_cosine_matrix * x2, axis=2) # (batch_size, x1_timesteps, 1) sum_cosine = K.expand_dims(K.sum(cosine_matrix, axis=-1) + self.epsilon, axis=-1) # (batch_size, x1_timesteps, embedding_size) attentive_vector = weighted_sum / sum_cosine return attentive_vector
def _max_pooling_matching(self, h1, h2, w): """Max pooling matching operation. # Arguments h1: (batch_size, h1_timesteps, embedding_size) h2: (batch_size, h2_timesteps, embedding_size) w: weights of one direction, (mp_dim, embedding_size) # Output shape (batch_size, h1_timesteps, mp_dim) """ # h1 * weights, (batch_size, h1_timesteps, mp_dim, embedding_size) h1 = self._time_distributed_multiply(h1, w) # h2 * weights, (batch_size, h2_timesteps, mp_dim, embedding_size) h2 = self._time_distributed_multiply(h2, w) # reshape v1 to (batch_size, h1_timesteps, 1, mp_dim, embedding_size) h1 = K.expand_dims(h1, axis=2) # reshape v1 to (batch_size, 1, h2_timesteps, mp_dim, embedding_size) h2 = K.expand_dims(h2, axis=1) # cosine similarity, (batch_size, h1_timesteps, h2_timesteps, mp_dim) cos = self._cosine_similarity(h1, h2) # (batch_size, h1_timesteps, mp_dim) matching = K.max(cos, axis=2) return matching
def _attentive_matching(self, h1, h2, cosine_matrix, w): """Attentive matching operation. # Arguments h1: (batch_size, h1_timesteps, embedding_size) h2: (batch_size, h2_timesteps, embedding_size) cosine_matrix: weights of hidden state h2, (batch_size, h1_timesteps, h2_timesteps) w: weights of one direction, (mp_dim, embedding_size) # Output shape (batch_size, h1_timesteps, mp_dim) """ # h1 * weights, (batch_size, h1_timesteps, mp_dim, embedding_size) h1 = self._time_distributed_multiply(h1, w) # attentive vector (batch_size, h1_timesteps, embedding_szie) attentive_vec = self._mean_attentive_vectors(h2, cosine_matrix) # attentive_vec * weights, (batch_size, h1_timesteps, mp_dim, embedding_size) attentive_vec = self._time_distributed_multiply(attentive_vec, w) # matching vector, (batch_size, h1_timesteps, mp_dim) matching = self._cosine_similarity(h1, attentive_vec) return matching
def _max_attentive_matching(self, h1, h2, cosine_matrix, w): """Max attentive matching operation. # Arguments h1: (batch_size, h1_timesteps, embedding_size) h2: (batch_size, h2_timesteps, embedding_size) cosine_matrix: weights of hidden state h2, (batch_size, h1_timesteps, h2_timesteps) w: weights of one direction, (mp_dim, embedding_size) # Output shape (batch_size, h1_timesteps, mp_dim) """ # h1 * weights, (batch_size, h1_timesteps, mp_dim, embedding_size) h1 = self._time_distributed_multiply(h1, w) # max attentive vector (batch_size, h1_timesteps, embedding_szie) max_attentive_vec = self._max_attentive_vectors(h2, cosine_matrix) # max_attentive_vec * weights, (batch_size, h1_timesteps, mp_dim, embedding_size) max_attentive_vec = self._time_distributed_multiply(max_attentive_vec, w) # matching vector, (batch_size, h1_timesteps, mp_dim) matching = self._cosine_similarity(h1, max_attentive_vec) return matching
def HAN1(MAX_NB_WORDS, MAX_WORDS, MAX_SENTS, EMBEDDING_DIM, WORDGRU, embedding_matrix, DROPOUTPER): #model = Sequential() wordInputs = Input(shape=(MAX_WORDS,), name='word1', dtype='float32') wordEmbedding = Embedding(MAX_NB_WORDS, EMBEDDING_DIM, weights=[embedding_matrix], mask_zero=True, trainable=True, name='emb1')(wordInputs) #Assuming all the sentences have same number of words. Check for input_length again. hij = Bidirectional(GRU(WORDGRU, name='gru1', return_sequences=True))(wordEmbedding) wordDrop = Dropout(DROPOUTPER, name='drop1')(hij) alpha_its, Si = AttentionLayer(name='att1')(wordDrop) v6 = Dense(1, activation="sigmoid", name="dense")(Si) #model.add(Dense(1, activation="sigmoid", name="documentOut3")) model = Model(inputs=[wordInputs] , outputs=[v6]) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) return model
def kSparse(self, x, topk): print 'run regular k-sparse' dim = int(x.get_shape()[1]) if topk > dim: warnings.warn('Warning: topk should not be larger than dim: %s, found: %s, using %s' % (dim, topk, dim)) topk = dim k = dim - topk values, indices = tf.nn.top_k(-x, k) # indices will be [[0, 1], [2, 1]], values will be [[6., 2.], [5., 4.]] # We need to create full indices like [[0, 0], [0, 1], [1, 2], [1, 1]] my_range = tf.expand_dims(tf.range(0, tf.shape(indices)[0]), 1) # will be [[0], [1]] my_range_repeated = tf.tile(my_range, [1, k]) # will be [[0, 0], [1, 1]] full_indices = tf.stack([my_range_repeated, indices], axis=2) # change shapes to [N, k, 1] and [N, k, 1], to concatenate into [N, k, 2] full_indices = tf.reshape(full_indices, [-1, 2]) to_reset = tf.sparse_to_dense(full_indices, tf.shape(x), tf.reshape(values, [-1]), default_value=0., validate_indices=False) res = tf.add(x, to_reset) return res
def to_configs(states, verbose=True, **kwargs): base = setting['base'] width = states.shape[1] // base height = states.shape[1] // base load(width,height) def build(): P = len(setting['panels']) states = Input(shape=(height*base,width*base)) error = build_error(states, height, width, base) matches = 1 - K.clip(K.sign(error - threshold),0,1) # a, h, w, panel matches = K.reshape(matches, [K.shape(states)[0], height * width, -1]) # a, pos, panel matches = K.permute_dimensions(matches, [0,2,1]) # a, panel, pos config = matches * K.arange(height*width,dtype='float') config = K.sum(config, axis=-1) return Model(states, wrap(states, config)) model = build() return model.predict(states, **kwargs)
def validate_transitions_cpu_old(transitions, **kwargs): pre = np.array(transitions[0]) suc = np.array(transitions[1]) base = setting['base'] width = pre.shape[1] // base height = pre.shape[1] // base load(width,height) pre_validation = validate_states(pre, **kwargs) suc_validation = validate_states(suc, **kwargs) results = [] for pre, suc, pre_validation, suc_validation in zip(pre, suc, pre_validation, suc_validation): if pre_validation and suc_validation: c = to_configs(np.array([pre, suc]), verbose=False) succs = successors(c[0], width, height) results.append(np.any(np.all(np.equal(succs, c[1]), axis=1))) else: results.append(False) return results
def tensor_linear_interpolation(image, x, y, cval): # image: batch tensor, x,y: number import math x0, x1 = math.floor(x), math.ceil(x) y0, y1 = math.floor(y), math.ceil(y) dx0, dx1 = x - x0, x1 - x dy0, dy1 = y - y0, y1 - y shape = K.int_shape(image)[1:] results = [] if 0 <= y0 and y0 < shape[0] and 0 <= x0 and x0 < shape[1]: results.append((y0,x0,dy1*dx1)) if 0 <= y0 and y0 < shape[0] and 0 <= x1 and x1 < shape[1]: results.append((y0,x1,dy1*dx0)) if 0 <= y1 and y1 < shape[0] and 0 <= x0 and x0 < shape[1]: results.append((y1,x0,dy0*dx1)) if 0 <= y1 and y1 < shape[0] and 0 <= x1 and x1 < shape[1]: results.append((y1,x1,dy0*dx0)) return results
def tensor_swirl(image, center=None, strength=1, radius=100, rotation=0, cval=0.0, **kwargs): # **kwargs is for unsupported options (ignored) cval = tf.fill(K.shape(image)[0:1], cval) shape = K.int_shape(image)[1:3] if center is None: center = np.array(shape) / 2 ys = np.expand_dims(np.repeat(np.arange(shape[0]), shape[1]),-1) xs = np.expand_dims(np.tile (np.arange(shape[1]), shape[0]),-1) map_xs, map_ys = swirl_mapping(xs, ys, center, rotation, strength, radius) mapping = np.zeros((*shape, *shape)) for map_x, map_y, x, y in zip(map_xs, map_ys, xs, ys): results = tensor_linear_interpolation(image, map_x, map_y, cval) for _y, _x, w in results: # mapping[int(y),int(x),int(_y),int(_x),] = w mapping[int(_y),int(_x),int(y),int(x),] = w results = tf.tensordot(image, K.variable(mapping), [[1,2],[0,1]]) # results = K.reshape(results, K.shape(image)) return results
def generate_cpu(configs, **kwargs): configs = np.array(configs) import math size = int(math.sqrt(len(configs[0]))) base = panels.shape[1] dim = base*size def generate(config): figure_big = np.zeros((dim+2*pad,dim+2*pad)) figure = figure_big[pad:-pad,pad:-pad] for pos,value in enumerate(config): x = pos % size y = pos // size if value > 0: figure[y*base:(y+1)*base, x*base:(x+1)*base] = panels[0] else: figure[y*base:(y+1)*base, x*base:(x+1)*base] = panels[1] return figure_big return preprocess(batch_swirl([ generate(c) for c in configs ]))
def generate_gpu2(configs,**kwargs): configs = np.array(configs) import math size = int(math.sqrt(len(configs[0]))) base = panels.shape[1] dim = base*size def build(): P = 2 configs = Input(shape=(size*size,)) _configs = 1 - K.round((configs/2)+0.5) # from -1/1 to 1/0 configs_one_hot = K.one_hot(K.cast(_configs,'int32'), P) configs_one_hot = K.reshape(configs_one_hot, [-1,P]) _panels = K.variable(panels) _panels = K.reshape(_panels, [P, base*base]) states = tf.matmul(configs_one_hot, _panels) states = K.reshape(states, [-1, size, size, base, base]) states = K.permute_dimensions(states, [0, 1, 3, 2, 4]) states = K.reshape(states, [-1, size*base, size*base, 1]) states = K.spatial_2d_padding(states, padding=((pad,pad),(pad,pad))) states = K.squeeze(states, -1) states = tensor_swirl(states, radius=dim+2*pad * relative_swirl_radius, **swirl_args) return Model(configs, wrap(configs, states)) return preprocess(build().predict(configs,**kwargs))
def to_configs(states, verbose=True, **kwargs): base = panels.shape[1] dim = states.shape[1] - pad*2 size = dim // base def build(): states = Input(shape=(dim+2*pad,dim+2*pad)) s = tensor_swirl(states, radius=dim+2*pad * relative_swirl_radius, **unswirl_args) error = build_errors(s,base,pad,dim,size) matches = 1 - K.clip(K.sign(error - threshold),0,1) # a, h, w, panel matches = K.reshape(matches, [K.shape(states)[0], size * size, -1]) # a, pos, panel config = matches * K.arange(2,dtype='float') config = K.sum(config, axis=-1) # this is 0,1 configs; for compatibility, we need -1 and 1 config = - (config - 0.5)*2 return Model(states, wrap(states, K.round(config))) return build().predict(states, **kwargs)
def generate_cpu(configs, **kwargs): import math size = int(math.sqrt(len(configs[0]))) base = panels.shape[1] dim = base*size def generate(config): figure = np.zeros((dim,dim)) for pos,value in enumerate(config): x = pos % size y = pos // size if value > 0: figure[y*base:(y+1)*base, x*base:(x+1)*base] = panels[0] else: figure[y*base:(y+1)*base, x*base:(x+1)*base] = panels[1] return preprocess(figure) return np.array([ generate(c) for c in configs ]).reshape((-1,dim,dim))
def to_configs(states, verbose=True, **kwargs): base = panels.shape[1] size = states.shape[1]//base dim = states.shape[1] def build(): states = Input(shape=(dim,dim)) error = build_errors(states,base,dim,size) matches = 1 - K.clip(K.sign(error - threshold),0,1) # a, h, w, panel matches = K.reshape(matches, [K.shape(states)[0], size * size, -1]) # a, pos, panel config = matches * K.arange(2,dtype='float') config = K.sum(config, axis=-1) # this is 0,1 configs; for compatibility, we need -1 and 1 config = - (config - 0.5)*2 return Model(states, wrap(states, K.round(config))) model = build() return model.predict(states, **kwargs)
def _build(self,input_shape): _encoder = self.build_encoder(input_shape) _decoder = self.build_decoder(input_shape) x = Input(shape=input_shape) z = Sequential([flatten, *_encoder])(x) y = Sequential(_decoder)(flatten(z)) z2 = Input(shape=K.int_shape(z)[1:]) y2 = Sequential(_decoder)(flatten(z2)) self.loss = bce self.encoder = Model(x, z) self.decoder = Model(z2, y2) self.net = Model(x, y) self.autoencoder = self.net
def _buildEncoder(self, x, latent_rep_size, max_length, epsilon_std = 0.01): h = Convolution1D(9, 9, activation = 'relu', name='conv_1')(x) h = Convolution1D(9, 9, activation = 'relu', name='conv_2')(h) h = Convolution1D(10, 11, activation = 'relu', name='conv_3')(h) h = Flatten(name='flatten_1')(h) h = Dense(435, activation = 'relu', name='dense_1')(h) def sampling(args): z_mean_, z_log_var_ = args batch_size = K.shape(z_mean_)[0] epsilon = K.random_normal(shape=(batch_size, latent_rep_size), mean=0., std = epsilon_std) return z_mean_ + K.exp(z_log_var_ / 2) * epsilon z_mean = Dense(latent_rep_size, name='z_mean', activation = 'linear')(h) z_log_var = Dense(latent_rep_size, name='z_log_var', activation = 'linear')(h) def vae_loss(x, x_decoded_mean): x = K.flatten(x) x_decoded_mean = K.flatten(x_decoded_mean) xent_loss = max_length * objectives.binary_crossentropy(x, x_decoded_mean) kl_loss = - 0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis = -1) return xent_loss + kl_loss return (vae_loss, Lambda(sampling, output_shape=(latent_rep_size,), name='lambda')([z_mean, z_log_var]))
def region_style_loss(style_image, target_image, style_mask, target_mask): '''Calculate style loss between style_image and target_image, for one common region specified by their (boolean) masks ''' assert 3 == K.ndim(style_image) == K.ndim(target_image) assert 2 == K.ndim(style_mask) == K.ndim(target_mask) if K.image_dim_ordering() == 'th': masked_style = style_image * style_mask masked_target = target_image * target_mask nb_channels = K.shape(style_image)[0] else: masked_style = K.permute_dimensions( style_image, (2, 0, 1)) * style_mask masked_target = K.permute_dimensions( target_image, (2, 0, 1)) * target_mask nb_channels = K.shape(style_image)[-1] s = gram_matrix(masked_style) / K.mean(style_mask) / nb_channels c = gram_matrix(masked_target) / K.mean(target_mask) / nb_channels return K.mean(K.square(s - c))
def batch_gather(reference, indices): '''Batchwise gathering of row indices. The numpy equivalent is reference[np.arange(batch_size), indices]. # Arguments reference: tensor with ndim >= 2 of shape (batch_size, dim1, dim2, ..., dimN) indices: 1d integer tensor of shape (batch_size) satisfiying 0 <= i < dim2 for each element i. # Returns A tensor with shape (batch_size, dim2, ..., dimN) equal to reference[1:batch_size, indices] ''' batch_size = K.shape(reference)[0] indices = tf.pack([tf.range(batch_size), indices], axis=1) return tf.gather_nd(reference, indices)
def batch_gather(reference, indices): '''Batchwise gathering of row indices. The numpy equivalent is reference[np.arange(batch_size), indices], # Arguments reference: tensor with ndim >= 2 of shape (batch_size, dim1, dim2, ..., dimN) indices: 1d integer tensor of shape (batch_size) satisfiying 0 <= i < dim2 for each element i. # Returns A tensor with shape (batch_size, dim2, ..., dimN) equal to reference[1:batch_size, indices] ''' batch_size = K.shape(reference)[0] return reference[T.arange(batch_size), indices]
def region_style_loss(style_image, target_image, style_mask, target_mask): '''Calculate style loss between style_image and target_image, for one common region specified by their (boolean) masks ''' assert 3 == K.ndim(style_image) == K.ndim(target_image) assert 2 == K.ndim(style_mask) == K.ndim(target_mask) if K.image_data_format() == 'channels_first': masked_style = style_image * style_mask masked_target = target_image * target_mask num_channels = K.shape(style_image)[0] else: masked_style = K.permute_dimensions( style_image, (2, 0, 1)) * style_mask masked_target = K.permute_dimensions( target_image, (2, 0, 1)) * target_mask num_channels = K.shape(style_image)[-1] s = gram_matrix(masked_style) / K.mean(style_mask) / num_channels c = gram_matrix(masked_target) / K.mean(target_mask) / num_channels return K.mean(K.square(s - c))
def call(self, x, mask=None): # x should be an output and a target assert len(x) == 2 losses = _per_sample_loss(self.loss, mask, x) if self.fast: grads = K.sqrt(sum([ K.sum(K.square(g), axis=1) for g in K.gradients(losses, self.parameter_list) ])) else: nb_samples = K.shape(losses)[0] grads = K.map_fn( lambda i: self._grad_norm(losses[i]), K.arange(0, nb_samples), dtype=K.floatx() ) return K.reshape(grads, (-1, 1))
def build(self, input_shape): assert len(input_shape) == 2 input_dim = input_shape[1] self.W_quad_ex = self.add_weight(shape=(input_dim, self.quadratic_filters_ex), name='W_quad_ex', initializer=self.W_quad_ex_initializer, regularizer=self.W_quad_ex_regularizer, constraint=self.W_quad_ex_constraint) self.W_quad_sup = self.add_weight(shape=(input_dim, self.quadratic_filters_sup), name='W_quad_sup', initializer=self.W_quad_sup_initializer, regularizer=self.W_quad_sup_regularizer, constraint=self.W_quad_sup_constraint) self.W_lin = self.add_weight(shape=(input_dim, 1), name='W_lin', initializer=self.W_lin_initializer, regularizer=self.W_lin_regularizer, constraint=self.W_lin_constraint)
def build(self, input_shape): assert len(input_shape) >= 2 input_dim = input_shape[-1] self.kernel = self.add_weight(shape=(input_dim, self.units), initializer=self.kernel_initializer, name='kernel', regularizer=self.kernel_regularizer, constraint=self.kernel_constraint) if self.tied_k: k_size = (1,) else: k_size = (self.units,) self.k = self.add_weight(shape=k_size, initializer=self.k_initializer, name='k', regularizer=self.k_regularizer, constraint=self.k_constraint) self.input_spec = InputSpec(min_ndim=2, axes={-1: input_dim}) self.built = True
def call(self, x, mask=None): # computes a probability distribution over the timesteps # uses 'max trick' for numerical stability # reshape is done to avoid issue with Tensorflow # and 1-dimensional weights logits = K.dot(x, self.W) x_shape = K.shape(x) logits = K.reshape(logits, (x_shape[0], x_shape[1])) ai = K.exp(logits - K.max(logits, axis=-1, keepdims=True)) # masked timesteps have zero weight if mask is not None: mask = K.cast(mask, K.floatx()) ai = ai * mask att_weights = ai / (K.sum(ai, axis=1, keepdims=True) + K.epsilon()) weighted_input = x * K.expand_dims(att_weights) result = K.sum(weighted_input, axis=1) if self.return_attention: return [result, att_weights] return result
def call(self, X, mask=None): input_shape = self.input_spec[0].shape x = K.reshape(X[0], (-1, input_shape[2])) target = X[1].flatten() if self.trainable else None Y = h_softmax(x, K.shape(x)[0], self.output_dim, self.n_classes, self.n_outputs_per_class, self.W1, self.b1, self.W2, self.b2, target) output_dim = 1 if self.trainable else self.output_dim input_length = K.shape(X[0])[1] y = K.reshape(Y, (-1, input_length, output_dim)) return y
def temporal_padding(x, paddings=(1, 0), padvalue=0): '''Pad the middle dimension of a 3D tensor with `padding[0]` values left and `padding[1]` values right. Modified from keras.backend.temporal_padding https://github.com/fchollet/keras/blob/3bf913d/keras/backend/theano_backend.py#L590 TODO: Implement for tensorflow (supposebly more easy) ''' if not isinstance(paddings, (tuple, list, ndarray)): paddings = (paddings, paddings) input_shape = x.shape output_shape = (input_shape[0], input_shape[1] + sum(paddings), input_shape[2]) output = T.zeros(output_shape) # Set pad value and set subtensor of actual tensor output = T.set_subtensor(output[:, :paddings[0], :], padvalue) output = T.set_subtensor(output[:, paddings[1]:, :], padvalue) output = T.set_subtensor(output[:, paddings[0]:x.shape[1] + paddings[0], :], x) return output
def mean_IoU(y_true, y_pred): s = K.shape(y_true) # reshape such that w and h dim are multiplied together y_true_reshaped = K.reshape( y_true, tf.stack( [-1, s[1]*s[2], s[-1]] ) ) y_pred_reshaped = K.reshape( y_pred, tf.stack( [-1, s[1]*s[2], s[-1]] ) ) # correctly classified clf_pred = K.one_hot( K.argmax(y_pred_reshaped), nb_classes = s[-1]) equal_entries = K.cast(K.equal(clf_pred,y_true_reshaped), dtype='float32') * y_true_reshaped intersection = K.sum(equal_entries, axis=1) union_per_class = K.sum(y_true_reshaped,axis=1) + K.sum(y_pred_reshaped,axis=1) iou = intersection / (union_per_class - intersection) iou_mask = tf.is_finite(iou) iou_masked = tf.boolean_mask(iou,iou_mask) return K.mean( iou_masked )
def preprocess_image(image_path, load_dims=False, style_image=False): global img_WIDTH, img_HEIGHT, aspect_ratio, b_scale_ratio_height, b_scale_ratio_width img = imread(image_path, mode="RGB") # Prevents crashes due to PNG images (ARGB) if load_dims: img_WIDTH = img.shape[0] img_HEIGHT = img.shape[1] aspect_ratio = img_HEIGHT / img_WIDTH if style_image: b_scale_ratio_width = float(img.shape[0]) / img_WIDTH b_scale_ratio_height = float(img.shape[1]) / img_HEIGHT img = imresize(img, (img_width, img_height)) img = img.transpose((2, 0, 1)).astype('float64') img = np.expand_dims(img, axis=0) return img # util function to convert a tensor into a valid image
def find_analogy_patches(a, a_prime, b, patch_size=3, patch_stride=1): '''This is for precalculating the analogy_loss Since A, A', and B never change we only need to calculate the patch matches once. ''' # extract patches from feature maps a_patches, a_patches_norm = make_patches(K.variable(a), patch_size, patch_stride) a_prime_patches, a_prime_patches_norm = make_patches(K.variable(a_prime), patch_size, patch_stride) b_patches, b_patches_norm = make_patches(K.variable(b), patch_size, patch_stride) # find best patches and calculate loss p = find_patch_matches(b_patches, b_patches_norm, a_patches / a_patches_norm) #best_patches = a_prime_patches[p] best_patches = K.reshape(a_prime_patches[p], K.shape(b_patches)) f = K.function([], best_patches) best_patches = f([]) return best_patches
def sample_standard_normal_noise(inputs, **kwargs): from keras.backend import shape, random_normal n_samples = kwargs.get('n_samples', shape(inputs)[0]) n_basis_noise_vectors = kwargs.get('n_basis', -1) data_dim = kwargs.get('data_dim', 1) noise_dim = kwargs.get('noise_dim', data_dim) seed = kwargs.get('seed', 7) if n_basis_noise_vectors > 0: samples_isotropic = random_normal(shape=(n_samples, n_basis_noise_vectors, noise_dim), mean=0, stddev=1, seed=seed) else: samples_isotropic = random_normal(shape=(n_samples, noise_dim), mean=0, stddev=1, seed=seed) op_mode = kwargs.get('mode', 'none') if op_mode == 'concatenate': concat = Concatenate(axis=1, name='enc_noise_concatenation')([inputs, samples_isotropic]) return concat elif op_mode == 'add': resized_noise = Dense(data_dim, activation=None, name='enc_resized_noise_sampler')(samples_isotropic) added_noise_data = Add(name='enc_adding_noise_data')([inputs, resized_noise]) return added_noise_data return samples_isotropic
def sample_adaptive_normal_noise(inputs, **kwargs): from keras.backend import shape, random_normal, sqrt seed = kwargs.get('seed', 7) latent_dim = kwargs.get('latent_dim', 2) if isinstance(inputs, list): mu, sigma2 = inputs n_samples = kwargs.get('n_samples', shape(mu)[0]) samples_isotropic = random_normal(shape=(n_samples, latent_dim), mean=0, stddev=1, seed=seed) samples = mu + sqrt(sigma2) * samples_isotropic return samples else: samples_isotropic = random_normal(shape=(shape(inputs)[0], latent_dim), mean=0, stddev=1, seed=seed) return samples_isotropic
def content_loss(base, combination): channel_dim = 0 if K.image_dim_ordering() == "th" else -1 channels = K.shape(base)[channel_dim] size = img_width * img_height if args.content_loss_type == 1: multiplier = 1 / (2. * channels ** 0.5 * size ** 0.5) elif args.content_loss_type == 2: multiplier = 1 / (channels * size) else: multiplier = 1. return multiplier * K.sum(K.square(combination - base)) # the 3rd loss function, total variation loss, # designed to keep the generated image locally coherent
def call(self, x): input_shape = K.shape(x) len_i, len_j = input_shape[1], input_shape[2] outputs = [] for nb_bins in self.nb_bins_per_level: bin_size_i = K.cast(len_i, 'int32') // nb_bins bin_size_j = K.cast(len_j, 'int32') // nb_bins for i, j in product(range(nb_bins), range(nb_bins)): # each combination of i,j is a unique rectangle i1, i2 = bin_size_i * i, bin_size_i * (i + 1) j1, j2 = bin_size_j * j, bin_size_j * (j + 1) pooled_features = K.max(x[:, i1:i2, j1:j2, :], axis=(1, 2)) outputs.append(pooled_features) return K.concatenate(outputs, axis=1)
def find_analogy_patches(a, a_prime, b, patch_size=3, patch_stride=1): '''This is for precalculating the analogy_loss Since A, A', and B never change we only need to calculate the patch matches once. ''' # extract patches from feature maps a_patches, a_patches_norm = patches.make_patches(K.variable(a), patch_size, patch_stride) a_prime_patches, a_prime_patches_norm = patches.make_patches(K.variable(a_prime), patch_size, patch_stride) b_patches, b_patches_norm = patches.make_patches(K.variable(b), patch_size, patch_stride) # find best patches and calculate loss p = patches.find_patch_matches(b_patches, b_patches_norm, a_patches / a_patches_norm) #best_patches = a_prime_patches[p] best_patches = K.reshape(a_prime_patches[p], K.shape(b_patches)) f = K.function([], best_patches) best_patches = f([]) return best_patches
def reconstruct_from_patches_2d(patches, image_size): '''This is from scikit-learn. I thought it was a little overkill to require it just for this function. ''' i_h, i_w = image_size[:2] p_h, p_w = patches.shape[1:3] img = np.zeros(image_size, dtype=np.float32) # compute the dimensions of the patches array n_h = i_h - p_h + 1 n_w = i_w - p_w + 1 for p, (i, j) in zip(patches, product(range(n_h), range(n_w))): img[i:i + p_h, j:j + p_w] += p for i in range(i_h): for j in range(i_w): # divide by the amount of overlap # XXX: is this the most efficient way? memory-wise yes, cpu wise? img[i, j] /= float(min(i + 1, p_h, i_h - i) * min(j + 1, p_w, i_w - j)) return img
def make_patches_grid(x, patch_size, patch_stride): '''Break image `x` up into a grid of patches. input shape: (channels, rows, cols) output shape: (rows, cols, channels, patch_rows, patch_cols) ''' from theano.tensor.nnet.neighbours import images2neibs # TODO: all K, no T x = K.expand_dims(x, 0) xs = K.shape(x) num_rows = 1 + (xs[-2] - patch_size) // patch_stride num_cols = 1 + (xs[-1] - patch_size) // patch_stride num_channels = xs[-3] patches = images2neibs(x, (patch_size, patch_size), (patch_stride, patch_stride), mode='valid') # neibs are sorted per-channel patches = K.reshape(patches, (num_channels, K.shape(patches)[0] // num_channels, patch_size, patch_size)) patches = K.permute_dimensions(patches, (1, 0, 2, 3)) # arrange in a 2d-grid (rows, cols, channels, px, py) patches = K.reshape(patches, (num_rows, num_cols, num_channels, patch_size, patch_size)) patches_norm = K.sqrt(K.sum(K.square(patches), axis=(2,3,4), keepdims=True)) return patches, patches_norm