def setup_graphs(self): # Create shared network s, policy_network, value_network = self.model_factory(self, self.env[0], **self.kwargs) policy_network_params = policy_network.trainable_weights value_network_params = value_network.trainable_weights pi_values = policy_network(s) V_values = value_network(s) # Create shared target network st, target_policy_network, target_value_network = self.model_factory(self, self.env[0]) target_policy_network_params = target_policy_network.trainable_weights target_value_network_params = target_value_network.trainable_weights target_pi_values = target_policy_network(st) target_V_values = target_value_network(st) # Op for periodically updating target network with online network weights reset_local_policy_network_params = [policy_network_params[i].assign(target_policy_network_params[i]) for i in range(len(policy_network_params))] reset_local_value_network_params = [value_network_params[i].assign(target_value_network_params[i]) for i in range(len(value_network_params))] reset_target_policy_network_params = [target_policy_network_params[i].assign(policy_network_params[i]) for i in range(len(target_policy_network_params))] reset_target_value_network_params = [target_value_network_params[i].assign(value_network_params[i]) for i in range(len(target_value_network_params))] # Define A3C cost and gradient update equations a = tf.placeholder("float", [None, self.env[0].action_space.n]) R = tf.placeholder("float", [None, 1]) action_pi_values = tf.reduce_sum(tf.mul(pi_values, a), reduction_indices=1) # policy network update cost_pi = -K.log( tf.reduce_sum( action_pi_values ) ) * (R-V_values) #optimizer_pi = keras.optimizers.Adam(self.learning_rate, clipvalue=1e3) optimizer_pi = tf.train.RMSPropOptimizer(self.learning_rate) grad_update_pi = optimizer_pi.minimize(cost_pi, var_list=policy_network_params) grad_pi = K.gradients(cost_pi, policy_network_params) # value network update cost_V = tf.reduce_mean( tf.square( R - V_values ) ) #optimizer_V = keras.optimizers.Adam(self.learning_rate, clipvalue=1e3) optimizer_V = tf.train.RMSPropOptimizer(self.learning_rate) grad_update_V = optimizer_V.minimize(cost_V, var_list=value_network_params) grad_V = K.gradients(cost_V, value_network_params) # store variables and update functions for access self.graph_ops = { "R" : R, "s" : s, "pi_values" : pi_values, "V_values" : V_values, "st" : st, "reset_target_policy_network_params" : reset_target_policy_network_params, "reset_target_value_network_params" : reset_target_value_network_params, "reset_local_policy_network_params" : reset_local_policy_network_params, "reset_local_value_network_params" : reset_local_value_network_params, "a" : a, "grad_update_pi" : grad_update_pi, "cost_pi" : cost_pi, "grad_pi" : grad_pi, "grad_update_V" : grad_update_V, "cost_V" : cost_V, "grad_V" : grad_V }
def setup_graphs(self): # update network weights... set_weights_v = lambda x: [value_network_params[i].assign(x[i]) for i in range(len(x))] set_weights_p = lambda x: [policy_network_params[i].assign(x[i]) for i in range(len(x))] # Create shared network s, policy_network, value_network = self.model_factory(self, self.env[0], **self.kwargs) policy_network_params = policy_network.trainable_weights value_network_params = value_network.trainable_weights pi_values = policy_network(s) V_values = value_network(s) # Define A3C cost and gradient update equations a = tf.placeholder("float", [None, self.env[0].action_space.n]) R = tf.placeholder("float", [None, 1]) action_pi_values = tf.reduce_sum(tf.mul(pi_values, a), reduction_indices=1) # policy network update cost_pi = -K.log( tf.reduce_sum( action_pi_values ) ) * (R-V_values) #optimizer_pi = keras.optimizers.Adam(self.learning_rate, clipvalue=1e3) optimizer_pi = tf.train.RMSPropOptimizer(self.learning_rate) grad_update_pi = optimizer_pi.minimize(cost_pi, var_list=policy_network_params) grad_pi = K.gradients(cost_pi, policy_network_params) # value network update cost_V = tf.reduce_mean( tf.square( R - V_values ) ) #optimizer_V = keras.optimizers.Adam(self.learning_rate, clipvalue=1e3) optimizer_V = tf.train.RMSPropOptimizer(self.learning_rate) grad_update_V = optimizer_V.minimize(cost_V, var_list=value_network_params) grad_V = K.gradients(cost_V, value_network_params) # store variables and update functions for access self.graph_ops = { "R" : R, "s" : s, "pi_values" : pi_values, "V_values" : V_values, "a" : a, # policy network "grad_update_pi" : grad_update_pi, "cost_pi" : cost_pi, "grad_pi" : grad_pi, # value network "grad_update_V" : grad_update_V, "cost_V" : cost_V, "grad_V" : grad_V, "w_p" : policy_network.get_weights, "w_v" : value_network.get_weights, "set_weights_p" : set_weights_p, "set_weights_v" : set_weights_v, }
def __init__(self, input_tensor, losses, input_range=(0, 255), wrt_tensor=None, norm_grads=True): """Creates an optimizer that minimizes weighted loss function. Args: input_tensor: An input tensor of shape: `(samples, channels, image_dims...)` if `image_data_format= channels_first` or `(samples, image_dims..., channels)` if `image_data_format=channels_last`. losses: List of ([Loss](vis.losses#Loss), weight) tuples. input_range: Specifies the input range as a `(min, max)` tuple. This is used to rescale the final optimized input to the given range. (Default value=(0, 255)) wrt_tensor: Short for, with respect to. This instructs the optimizer that the aggregate loss from `losses` should be minimized with respect to `wrt_tensor`. `wrt_tensor` can be any tensor that is part of the model graph. Default value is set to None which means that loss will simply be minimized with respect to `input_tensor`. norm_grads: True to normalize gradients. Normalization avoids very small or large gradients and ensures a smooth gradient gradient descent process. If you want the actual gradient (for example, visualizing attention), set this to false. """ self.input_tensor = input_tensor self.input_range = input_range self.loss_names = [] self.loss_functions = [] self.wrt_tensor = self.input_tensor if wrt_tensor is None else wrt_tensor if self.input_tensor is self.wrt_tensor: self.wrt_tensor_is_input_tensor = True self.wrt_tensor = K.identity(self.wrt_tensor) else: self.wrt_tensor_is_input_tensor = False overall_loss = None for loss, weight in losses: # Perf optimization. Don't build loss function with 0 weight. if weight != 0: loss_fn = weight * loss.build_loss() overall_loss = loss_fn if overall_loss is None else overall_loss + loss_fn self.loss_names.append(loss.name) self.loss_functions.append(loss_fn) # Compute gradient of overall with respect to `wrt` tensor. if self.wrt_tensor_is_input_tensor: grads = K.gradients(overall_loss, self.input_tensor)[0] else: grads = K.gradients(overall_loss, self.wrt_tensor)[0] if norm_grads: grads = K.l2_normalize(grads) # The main function to compute various quantities in optimization loop. self.compute_fn = K.function([self.input_tensor, K.learning_phase()], self.loss_functions + [overall_loss, grads, self.wrt_tensor])
def EvaluateJacobian(model): #theano.function( [model.layers[0].input], T.jacobian(model.layers[-1].output.flatten(), model.layers[0].input) ) X = K.placeholder(shape=(15,15)) #specify the right placeholder Y = K.sum(K.square(X)) # loss function fn = K.function([X], K.gradients(Y, [X])) #function to call the gradient
def build_train_fn(model): # cost lr = T.scalar() labels = K.placeholder(ndim=2, dtype='int32') ob_input = model.inputs[0] raw_softmax_outputs = model.outputs[0] softmax_outputs = raw_softmax_outputs.dimshuffle((2,0,1)) softmax_outputs = softmax_outputs.reshape((softmax_outputs.shape[0], softmax_outputs.shape[1]*softmax_outputs.shape[2])) softmax_outputs = softmax_outputs.dimshuffle((1,0)) cost = categorical_crossentropy(softmax_outputs, labels).mean() # gradients trainable_vars = model.trainable_weights grads = K.gradients(cost, trainable_vars) grads = lasagne.updates.total_norm_constraint(grads, 100) updates = lasagne.updates.nesterov_momentum(grads, trainable_vars, lr, 0.99) for key, val in model.updates: updates[key] = val # train_fn train_fn = K.function([ob_input, labels, K.learning_phase(), lr], [softmax_outputs, cost], updates=updates) return train_fn
def build_model(self): """Build a Critic (Value) model that maps (state, action)> Q_values.""" # Input layers states = layers.Input(shape=(self.state_size,), name="states") actions = layers.Input(shape=(self.action_size,), name="actions") # Add some hidden layers to state pathway net_states = layers.Dense(units=32, activation="relu")(states) net_states = layers.Dense(units=64, activation="relu")(net_states) # Add some hidden layers to action pathway net_actions = layers.Dense(units=32, activation="relu")(actions) net_actions = layers.Dense(units=64, activation="relu")(net_actions) # Combine both pathways net = layers.Add()([net_states, net_actions]) net = layers.Activation('relu')(net) Q_values = layers.Dense(units=1, name='q_values')(net) self.model = models.Model(inputs=[states, actions], outputs=Q_values) optimizer = optimizers.Adam() self.model.compile(optimizer=optimizer, loss='mse') # Compute action gradients (derivative of Q values w.r.t. to actions) action_gradients = K.gradients(Q_values, actions) # Define an additional function to fetch action gradients (to be used # by actor model) self.get_action_gradients = K.function( inputs=[*self.model.inputs, K.learning_phase()], outputs=action_gradients)
def __init__( self, x_dim, u_dim, r_dim, model_path=None, model=None ): print("Init ForwardDynamicsAndRewardDNN") super(ForwardDynamicsAndRewardDNN, self).__init__( x_dim, u_dim, r_dim ) if model_path is not None: self.mdl = load_model( model_path, custom_objects={'atan2_loss': atan2_loss, 'cos': KK.cos} ) if model is not None: self.mdl = model x0 = KK.placeholder(shape=(None, self.x_dim), name='x0') u = KK.placeholder(shape=(None, self.u_dim), name='u') x1, cost = self.mdl([x0, u]) samp_symb = KK.placeholder( shape=(1, self.x_dim), name='samp_syb' ) loss = KK.expand_dims(mse(samp_symb, x1), axis=1) u_grads = KK.gradients([loss], [u]) self.meas_fn = KK.function( [x0, u, samp_symb], [x1, loss] + u_grads ) self.zero_control = None
def calc_loss_grad(model, combo_img, img_width, img_height): # get the symbolic outputs of each "key" layer outputs_dict = dict([(layer.name, layer.output) \ for layer in model.layers \ if layer.name[:5] == 'conv_']) # combine these loss functions into a single scalar loss = K.variable(0.) # content loss # use last non-fully-connected layer layer_feats = outputs_dict[last_layer] base_feats = layer_feats[0, :, :, :] combo_feats = layer_feats[2, :, :, :] cl = content_loss(base_feats, combo_feats) loss += content_weight * cl # style loss and spatial loss for name in outputs_dict: layer_feats = outputs_dict[name] style_feats = layer_feats[1, :, :, :] combo_feats = layer_feats[2, :, :, :] spatial = spatial_loss(style_feats, combo_feats) sl = style_loss(style_feats, combo_feats, img_width, img_height) loss += (spatial_weight / len(outputs_dict)) * spatial loss += (style_weight / len(outputs_dict)) * sl # single scalar loss loss += total_variation_weight * \ total_variation_loss(combo_img, img_width, img_height) # get the gradients of the generated image wrt the loss grads = K.gradients(loss, combo_img) return loss, grads
def process_conv_2d_layer(layer, input_img): """Generate images maximizing the activation of conv2d filters""" filter_cnt = layer.get_weights()[0].shape[-1] img_width, img_height, img_chans = input_img.shape[1:4] kept_filters = [] for filter_index in range(filter_cnt): print('{}:, filter {} of {}'.format( layer.name, filter_index, filter_cnt)) # we build a loss function that maximizes the activation # of the nth filter of the layer considered loss = K.mean(layer.output[:, :, :, filter_index]) # we compute the gradient of the input picture wrt this loss grads = K.gradients(loss, input_img)[0] # normalization trick: we normalize the gradient grads = normalize(grads) # this function returns the loss and grads given the input picture iterate = K.function([input_img], [loss, grads]) # we start from a gray image with some random noise input_img_data = np.random.random((1, img_width, img_height, img_chans)) # run gradient ascent for _ in range(GRADIENT_ASCENT_STEPS): loss_value, grads_value = iterate([input_img_data]) input_img_data += grads_value * GRADIENT_ASCENT_STEP_SIZE # decode the resulting input image img = deprocess_image(input_img_data[0]) kept_filters.append((img, loss_value)) return kept_filters
def grad_cam(input_model, model_x, orig_x, category_index, layer_name, class_names): output = input_model.output final_layer = Lambda(lambda x: target_category_loss(x, category_index, len(class_names))) output = final_layer(output) model = Model(inputs=input_model.input, outputs=output) loss = K.sum(model.layers[-1].output) conv_output = model.get_layer(layer_name).output grads = normalize(K.gradients(loss, conv_output)[0]) gradient_function = K.function([model.layers[0].input, K.learning_phase()], [conv_output, grads]) output, grads_val = gradient_function([model_x, 0]) output, grads_val = output[0, :], grads_val[0, :, :, :] weights = np.mean(grads_val, axis=(0, 1)) cam = np.zeros(output.shape[0: 2], dtype=np.float32) for i, w in enumerate(weights): cam += w * output[:, :, i] cam = np.maximum(cam, np.zeros(output.shape[0: 2], dtype=np.float32)) cam = cam.squeeze() cam = cv2.applyColorMap(np.uint8(255 * cam / np.max(cam)), cv2.COLORMAP_JET) cam = cv2.resize(cam, (np.shape(orig_x)[0], np.shape(orig_x)[1])) cam = 0.4 * cam + 0.6 * orig_x return np.uint8(cam)
def compile_saliency_function(model, activation_layer='block5_conv3'): input_img = model.input layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]]) layer_output = layer_dict[activation_layer].output max_output = K.max(layer_output, axis=3) saliency = K.gradients(K.sum(max_output), input_img)[0] return K.function([input_img, K.learning_phase()], [saliency])
def call(self): E = K.variable(np.random.random((1000,100)), name="entity_embeddings") R = K.variable(np.random.random((10,10000)), name="relation_embeddings") x = K.placeholder(shape=(1,3), name="spo") y = K.placeholder(ndim=0, name="y") batch_placeholder = K.cast(x, 'int32')[0] # print(batch_placeholder.eval()) s, o, p = [batch_placeholder[i] for i in range(3)] s2v = K.gather(E, s) o2v = K.gather(E, o) r2v = K.gather(R, p) def ccorr(a, b): return T.outer(a,b).flatten() # return T.arctan(s2v) + T.arctan(o2v) # return (s2v.dimshuffle('x', 'x', 0, 'x') + o2v.dimshuffle('x', 'x', 0, 'x')).flatten() # return T.nnet.conv2d(a.dimshuffle('x', 'x', 0, 'x'), b.dimshuffle('x', 'x', 0, 'x'), None, # None, # filter_flip=True, border_mode='half') # return self.ccorr1d_sc(a, b, border_mode='half') eta = K.dot(r2v, ccorr(s2v, o2v)) # py = 1/(1+K.exp(-eta)) # l = -K.log(py) # from theano import pp, function, printing # grad = T.grad(eta, E) # print(pp(grad)) # func = function([x], grad) func = K.function([x, y], K.gradients(eta, [s2v, o2v, r2v, E, R])) # for i in func.maker.fgraph.outputs: # print(pp(i)) # print (T.grad(py, s2v)) print (func([[[1,2,3]], -1]))
def gradient_penalty_loss(y_true, y_pred, averaged_samples, gradient_penalty_weight): """Calculates the gradient penalty loss for a batch of "averaged" samples. In Improved WGANs, the 1-Lipschitz constraint is enforced by adding a term to the loss function that penalizes the network if the gradient norm moves away from 1. However, it is impossible to evaluate this function at all points in the input space. The compromise used in the paper is to choose random points on the lines between real and generated samples, and check the gradients at these points. Note that it is the gradient w.r.t. the input averaged samples, not the weights of the discriminator, that we're penalizing! In order to evaluate the gradients, we must first run samples through the generator and evaluate the loss. Then we get the gradients of the discriminator w.r.t. the input averaged samples. The l2 norm and penalty can then be calculated for this gradient. Note that this loss function requires the original averaged samples as input, but Keras only supports passing y_true and y_pred to loss functions. To get around this, we make a partial() of the function with the averaged_samples argument, and use that for model training.""" # first get the gradients: # assuming: - that y_pred has dimensions (batch_size, 1) # - averaged_samples has dimensions (batch_size, nbr_features) # gradients afterwards has dimension (batch_size, nbr_features), basically # a list of nbr_features-dimensional gradient vectors gradients = K.gradients(y_pred, averaged_samples)[0] # compute the euclidean norm by squaring ... gradients_sqr = K.square(gradients) # ... summing over the rows ... gradients_sqr_sum = K.sum(gradients_sqr, axis=np.arange(1, len(gradients_sqr.shape))) # ... and sqrt gradient_l2_norm = K.sqrt(gradients_sqr_sum) # compute lambda * (1 - ||grad||)^2 still for each single sample gradient_penalty = gradient_penalty_weight * K.square(1 - gradient_l2_norm) # return the mean as loss over all the batch samples return K.mean(gradient_penalty)
def get_grad(gen_image_array): '''计算损失函数的梯度''' if gen_image_array != (1, target_width, target_height, 3): gen_image_array = gen_image_array.reshape((1, target_width, target_height, 3)) grad_fn = K.function([gModel.input], K.gradients(get_total_loss(gModel.input), [gModel.input])) grad = grad_fn([gen_image_array])[0].flatten().astype('float64') return grad
def visualize_mser_classifier(): model = load_model('mser_classifier') # Get the symbolic outputs of each "key" layer layer_dict = dict([(layer.name, layer) for layer in model.layers]) layer_name = 'block5_conv3' # Can be any integer from 0 to 511, as there are 512 filters in that layer kept_filters = [] for filter_index in range(200): print(filter_index) # Loss function that maximizes the activation # of the nth filter of the layer considered layer_output = layer_dict[layer_name].output loss = K.mean(layer_output[:, :, :, filter_index]) # Placeholder for the input images input_img = model.input # Dimensions of the generated pictures for each filter. img_width = 128 img_height = 128 # Compute the gradient of the input picture wrt this loss grads = K.gradients(loss, input_img)[0] # Normalize the gradient grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5) # This function returns the loss and grads given the input picture iterate = K.function([input_img], [loss, grads]) # Step size for gradient ascent step = 1 # Start with a random gray image input_img_data = np.random.random((1, img_width, img_height, 3)) * 20 + 128 # run gradient ascent for 20 steps for i in range(20): loss_value, grads_value = iterate([input_img_data]) input_img_data += grads_value * step print('Current loss value:', loss_value) if loss_value <= 0.: # some filters get stuck to 0, we can skip them break # Append generated image if loss_value > 0: img = deprocess_image(input_img_data[0]) kept_filters.append((img, loss_value)) # Stich the best 16 filters on a 4 x 4 grid n = 4 # The filters that have the highest loss are assumed to be better-looking. # Keep the best 64 filters. kept_filters.sort(key=lambda x: x[1], reverse=True) kept_filters = kept_filters[:n * n] # Build a black picture with enough space for # all 4 x 4 filters of size 128 x 128, with a 5px margin in between margin = 5 width = n * img_width + (n - 1) * margin height = n * img_height + (n - 1) * margin stitched_filters = np.zeros((width, height, 3)) # Fill the picture with the saved filters for i in range(n): for j in range(n): img, loss = kept_filters[i * n + j] stitched_filters[(img_width + margin) * i: (img_width + margin) * i + img_width, (img_height + margin) * j: (img_height + margin) * j + img_height, :] = img # Save the result to disk imsave('mser_classifier_stitched_filters_%dx%d.png' % (n, n), stitched_filters)
def visualize_layer(self, layer, model, keep_filters, out_path, vis_size): layer_output = layer.output num_filters = layer.nb_filter filters = [] img_width = vis_size[0] img_height = vis_size[1] for filter_index in xrange(num_filters): loss = K.mean(K.mean(layer_output[:, filter_index, :, :])) # compute the gradient of the input picture wrt this loss grads = K.gradients(loss, model.layers[0].input)[0] # normalization trick: we normalize the gradient grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5) # this function returns the loss and grads given the input picture iterate = K.function([model.layers[0].input], [loss, grads]) # step size for gradient ascent step = 1. input_img_data = np.random.random((1, 1, img_width, img_height)) * 20 + 128. for i in xrange(50): loss_value, grads_value = iterate([input_img_data]) input_img_data += grads_value * step if loss_value <= 0: break img = self.deprocess_image(input_img_data[0]) filters.append((img, loss_value)) filters.sort(key=lambda x: x[1], reverse=True) filters = filters[:keep_filters] # get number of grid rows and columns grid_r, grid_c = helpers.get_grid_dim(keep_filters) # create figure and axes fig, axes = plt.subplots(min([grid_r, grid_c]), max([grid_r, grid_c])) # iterate filters inside every channel for l, ax in enumerate(axes.flat): # get a single filter img = filters[l][0][:, :, 0] # put it on the grid ax.imshow(img, interpolation='bicubic', cmap='Greys') # remove any labels from the axes ax.set_xticks([]) ax.set_yticks([]) # save figure out_path = os.path.join(out_path, layer.name) + '.png' plt.savefig(out_path, bbox_inches='tight') """
def get_gradients(self, loss, params): grads = K.gradients(loss, params) if hasattr(self, 'clipnorm') and self.clipnorm > 0: norm = K.sqrt(sum([K.sum(K.square(g)) for g in grads])) grads = [clip_norm(g, self.clipnorm, norm) for g in grads] if hasattr(self, 'clipvalue') and self.clipvalue > 0: grads = [K.clip(g, -self.clipvalue, self.clipvalue) for g in grads] return grads
def call(self, x, mask=None): output = K.resize_images(x, self.size[0], self.size[1], self.dim_ordering) f = K.gradients(K.sum(self._master_layer.output), self._master_layer.input) * output return f
def XXXget_cost_grads_symbolic(self): """ Returns symbolic cost and symbolic gradients for the model """ trainable_params = self._get_trainable_params(self.model) #cost = self.model.model.total_loss cost = self.model.total_loss grads = K.gradients(cost, trainable_params) return cost, grads
def get_cost_grads(self): """ Returns the cost and flattened gradients for the model """ model = self.model trainable_params = self.get_trainable_params() #cost = model.model.total_loss cost = model.total_loss grads = K.gradients(cost, trainable_params) return cost, grads
def get_gradients(self, loss, params): grads = K.gradients(loss, params) if hasattr(self, 'scale') and self.scale != 1: grads = [g*K.variable(self.scale) for g in grads] if hasattr(self, 'clipnorm') and self.clipnorm > 0: norm = K.sqrt(sum([K.sum(K.square(g)) for g in grads])) grads = [K.switch(norm >= self.clipnorm, g * self.clipnorm / norm, g) for g in grads] if hasattr(self, 'clipvalue') and self.clipvalue > 0: grads = [K.clip(g, -self.clipvalue, self.clipvalue) for g in grads] return grads
def build(self, a_image, ap_image, b_image, output_shape): self.output_shape = output_shape loss = self.build_loss(a_image, ap_image, b_image) # get the gradients of the generated image wrt the loss grads = K.gradients(loss, self.net_input) outputs = [loss] if type(grads) in {list, tuple}: outputs += grads else: outputs.append(grads) self.f_outputs = K.function([self.net_input], outputs)
def build_model(self): """Build a critic (value) network that maps (state, action) pairs -> Q-values.""" # Define input layers states = layers.Input(shape=(self.state_size,), name='states') actions = layers.Input(shape=(self.action_size,), name='actions') # Add hidden layer(s) for state pathway net_states = layers.Dense(units=512, kernel_initializer=layers.initializers.RandomUniform(minval=-0.003, maxval=0.003))(states) net_states = layers.normalization.BatchNormalization()(net_states) net_states = layers.Activation('relu')(net_states) net_states = layers.Dense(units=256, kernel_initializer=layers.initializers.RandomUniform(minval=-0.003, maxval=0.003))(net_states) net_states = layers.normalization.BatchNormalization()(net_states) net_states = layers.Activation('relu')(net_states) net_states = layers.Dense(units=128, kernel_initializer=layers.initializers.RandomUniform(minval=-0.003, maxval=0.003))(net_states) net_states = layers.normalization.BatchNormalization()(net_states) # Add hidden layer(s) for action pathway net_actions = layers.Dense(units=256, kernel_initializer=layers.initializers.RandomUniform(minval=-0.003, maxval=0.003))(actions) net_actions = layers.normalization.BatchNormalization()(net_actions) net_actions = layers.Activation('relu')(net_actions) net_actions = layers.Dense(units=128, kernel_initializer=layers.initializers.RandomUniform(minval=-0.003, maxval=0.003))(net_actions) net_actions = layers.normalization.BatchNormalization()(net_actions) # Try different layer sizes, activations, add batch normalization, regularizers, etc. # Combine state and action pathways net = layers.Add()([net_states, net_actions]) net = layers.Activation('relu')(net) # Add more layers to the combined network if needed net = layers.Dense(units=512, kernel_initializer=layers.initializers.RandomUniform(minval=-0.003, maxval=0.003))(net) net = layers.normalization.BatchNormalization()(net) net = layers.Activation('relu')(net) # Add final output layer to prduce action values (Q values) Q_values = layers.Dense(units=1, kernel_initializer=layers.initializers.RandomUniform(minval=-0.003, maxval=0.003), name='q_values')(net) # Create Keras model self.model = models.Model(inputs=[states, actions], outputs=Q_values) # Define optimizer and compile model for training with built-in loss function optimizer = optimizers.Adam(self.CRITIC_LEARNING_RATE) self.model.compile(optimizer=optimizer, loss='mse') # Compute action gradients (derivative of Q values w.r.t. to actions) action_gradients = K.gradients(Q_values, actions) # Define an additional function to fetch action gradients (to be used by actor model) self.get_action_gradients = K.function( inputs=[*self.model.input, K.learning_phase()], outputs=action_gradients)
def compile_get_grads(self): scalar_objective = self.sum_objectives(self.objectives) self.grads_positions = [{} for _ in self.params] objectives = copy.copy(self.objectives) objectives[self.name] = scalar_objective self.evaluate_objective_names = list(objectives.keys()) gradients = [] for name, objective in objectives.items(): for i, grad in enumerate(K.gradients(objective, self.params)): self.grads_positions[i][name] = len(gradients) gradients.append(grad) self._gradients_fn = theano.function(self.inputs, gradients)
def compute_gradients(t_loss, param_list, t_inputs, n_inputs): """ Computes the derivaties of out (either loss or just Q) wrt the params and returns a flat np array NOTE: Make sure that params is a flat list (get using get_trainable_weights for eg) Make sure all necessary inputs for the grad computation is present in inputs t_inputs is the tensor inputs; and n_inputs is the numeric version """ # Iterate over each sets of weights in params x = np.empty(0) for param in param_list: c_grad = K.function(t_inputs, K.gradients(t_loss, [param])) grad = c_grad(n_inputs) # this is a list grad = np.asarray(grad).reshape(-1) # make it into flat array x = np.concatenate([x, grad]) return x
def build(self, a_image, ap_image, b_image, output_shape): self.output_shape = output_shape loss = self.build_loss(a_image, ap_image, b_image) # get the gradients of the generated image wrt the loss grads = K.gradients(loss, self.net_input) outputs = [loss] if type(grads) in {list, tuple}: outputs += grads else: outputs.append(grads) f_inputs = [self.net_input] for nnf in self.feature_nnfs: f_inputs.append(nnf.placeholder) self.f_outputs = K.function(f_inputs, outputs)
def main(): # Use gradient-descend to minimize the confidence of the "good" prediction lenet = LeNet() print(lenet._model.layers) # SoftMax activation on the last layer shrinks our gradients so much it is impossible to # do floating point computation, so we just peel it off from the output sym_outouts = layer_without_activation(lenet._model.layers[-1]) print("sym_outouts", sym_outouts) sym_inputs = lenet._model.inputs[0] print("sym_inputs", sym_inputs) for image_index in range(8): # K.set_floatx('float64') imageori = plt.imread("static/{}.jpg".format(image_index)).copy().astype("float") output_path = "broken/{}.jpg".format(image_index) original_prediction = predictimg(imageori, lenet) original_prediction_index = original_prediction[0] loss = -sym_outouts[0][original_prediction_index] # print("LOSS", loss) fn = K.function([sym_inputs], K.gradients(loss, sym_inputs)) imagenew = imageori.copy() for i in range(100): bump = fn([[imagenew]])[0][0] imagenew = np.clip(imagenew.copy() + bump, 0, 255) # saving to jpg alters the image so check whether our prediction is broken after a reload plt.imsave(output_path, imagenew.astype("uint8"), format="jpg") broken = plt.imread(output_path) new_prediction = predictimg(broken, lenet) # print("ITER", mse(imagenew, imageori)) if new_prediction[0] != original_prediction_index: break else: raise Exception("COULDNT BREAK {}".format(image_index)) broken = plt.imread(output_path) print("ORIGINAL PREDICTION", predictimg(imageori, lenet)) print("BROKEN PREDICTION", predictimg(broken, lenet)) print("mse(broken, imageori)", mse(broken, imageori)) print("mse(imagenew, imageori)", mse(imagenew, imageori)) print("FINISHED {}".format(image_index)) print()
def generate_pattern(modle, layer_name, filter_index, size=150): '''builds a loss func that maximizes the activation of the nth filter of the layer under consideration.''' layer_output = model.get_layer(layer_name).output loss = K.mean(layer_output[:, :, :, filter_index]) grads = K.gradients(loss, model.input)[0] grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5) iterate = K.function([model.input], [loss, grads]) input_img_data = np.random.random((1, size, size, 3)) * 20 + 128. step = 1 for i in range(40): loss_value, grads_value = iterate([input_img_data]) input_img_data += grads_value * step img = input_img_data[0] return deprocess_image(img)
def compile_gradient_function(input_model, category_index, layer_name): model = Sequential() model.add(input_model) num_classes = model.output_shape[1] target_layer = lambda x: target_category_loss(x, category_index, num_classes) model.add(Lambda(target_layer, output_shape = target_category_loss_output_shape)) loss = K.sum(model.layers[-1].output) conv_output = model.layers[0].get_layer(layer_name).output gradients = normalize(K.gradients(loss, conv_output)[0]) gradient_function = K.function([model.layers[0].input, K.learning_phase()], [conv_output, gradients]) return gradient_function
def get_output(self, train=False): X = self.get_input(train) if self.dim_ordering == 'th': output = K.repeat_elements(X, self.size[0], axis=2) output = K.repeat_elements(output, self.size[1], axis=3) elif self.dim_ordering == 'tf': output = K.repeat_elements(X, self.size[0], axis=1) output = K.repeat_elements(output, self.size[1], axis=2) else: raise Exception('Invalid dim_ordering: ' + self.dim_ordering) f = K.gradients(K.sum(self._master_layer.get_output(train)), self._master_layer.get_input(train)) * output return f
import numpy as np from keras import backend as K from keras.applications import VGG16 model = VGG16(weights='imagenet', include_top=False) layer_name = 'block3_conv1' filter_index = 0 layer_output = model.get_layer(layer_name).output loss = K.mean(layer_output[:, :, :, filter_index]) grads = K.gradients(loss, model.input)[0] grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5) iterate = K.function([model, input], [loss, grads]) loss_value, grads_value = iterate([np.zeros((1, 150, 150, 3))]) input_img_data = np.random.random((1, 150, 150, 3)) * 20 + 128. step = 1. for i in range(40): loss_value, grads_value = iterate([input_img_data]) input_img_data += grads_value * step def deprocess_image(x): x -= x.mean() x /= (x.std() + 1e-5) x *= 0.5 x = np.clip(x, 0, 1)
def build_model(self): """Build a critic (value) network that maps (state, action) pairs -> Q-values.""" # Define input layers states = layers.Input(shape=(self.state_size, ), name='states') actions = layers.Input(shape=(self.action_size, ), name='actions') # Add hidden layer(s) for state pathway net_states = layers.Dense( units=32, use_bias=False, kernel_regularizer=regularizers.l2(0.01), activity_regularizer=regularizers.l1(0.01))(states) net_states = layers.BatchNormalization()(net_states) net_states = layers.Activation('relu')(net_states) net_states = layers.Dropout(0.5)(net_states) net_states = layers.Dense( units=64, use_bias=False, kernel_regularizer=regularizers.l2(0.01), activity_regularizer=regularizers.l1(0.01))(states) net_states = layers.BatchNormalization()(net_states) net_states = layers.Activation('relu')(net_states) net_states = layers.Dropout(0.5)(net_states) net_states = layers.Dense( units=128, use_bias=False, kernel_regularizer=regularizers.l2(0.01), activity_regularizer=regularizers.l1(0.01))(states) net_states = layers.BatchNormalization()(net_states) net_states = layers.Activation('relu')(net_states) net_states = layers.Dropout(0.5)(net_states) # Add hidden layer(s) for action pathway net_actions = layers.Dense( units=32, use_bias=False, kernel_regularizer=regularizers.l2(0.01), activity_regularizer=regularizers.l1(0.01))(actions) net_actions = layers.BatchNormalization()(net_actions) net_actions = layers.Activation('relu')(net_actions) net_actions = layers.Dropout(0.5)(net_actions) net_actions = layers.Dense( units=64, use_bias=False, kernel_regularizer=regularizers.l2(0.01), activity_regularizer=regularizers.l1(0.01))(net_actions) net_actions = layers.BatchNormalization()(net_actions) net_actions = layers.Activation('relu')(net_actions) net_actions = layers.Dropout(0.5)(net_actions) net_actions = layers.Dense( units=128, use_bias=False, kernel_regularizer=regularizers.l2(0.01), activity_regularizer=regularizers.l1(0.01))(net_actions) net_actions = layers.BatchNormalization()(net_actions) net_actions = layers.Activation('relu')(net_actions) net_actions = layers.Dropout(0.5)(net_actions) # Try different layer sizes, activations, add batch normalization, regularizers, etc. # Combine state and action pathways net = layers.Add()([net_states, net_actions]) net = layers.Activation('relu')(net) # Add more layers to the combined network if needed # Add final output layer to produce action values (Q values) Q_values = layers.Dense(units=1, name='q_values')(net) # Create Keras model self.model = models.Model(inputs=[states, actions], outputs=Q_values) # Define optimizer and compile model for training with built-in loss function optimizer = optimizers.Adam() self.model.compile(optimizer=optimizer, loss='mse') # Compute action gradients (derivative of Q values w.r.t. to actions) action_gradients = K.gradients(Q_values, actions) # Define an additional function to fetch action gradients (to be used by actor model) self.get_action_gradients = K.function( inputs=[*self.model.input, K.learning_phase()], outputs=action_gradients)
sampled_base_image_features = random_content_features[0] sampled_combination_image_features = random_content_features[1] loss += content_weight * radial_basis_content_loss( sampled_base_image_features, sampled_combination_image_features) print("Content Loss computed") loss += compute_nabla_loss(total_style_combination_features, loss, i) loss += total_variation_weight * total_variation_loss( Learner.combination_image) grads = K.gradients(loss, Learner.combination_image) outputs = [loss] if isinstance(grads, (list, tuple)): outputs += grads else: outputs.append(grads) f_outputs = K.function([Learner.combination_image], outputs) evaluator = Evaluator() final_loss = run_experiment(x) loss_tracker.append(final_loss)
def _main(args): model_path = os.path.expanduser(args.model_path) assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.' anchors_path = os.path.expanduser(args.anchors_path) classes_path = os.path.expanduser(args.classes_path) test_path = os.path.expanduser(args.test_path) output_path = os.path.expanduser(args.output_path) if not os.path.exists(output_path): print('Creating output path {}'.format(output_path)) os.mkdir(output_path) sess = K.get_session() # TODO: Remove dependence on Tensorflow session. with open(classes_path) as f: class_names = f.readlines() class_names = [c.strip() for c in class_names] with open(anchors_path) as f: anchors = f.readline() anchors = [float(x) for x in anchors.split(',')] anchors = np.array(anchors).reshape(-1, 2) yolo_model = load_model(model_path) # https://keras.io/models/model/ # Verify model, anchors, and classes are compatible num_classes = len(class_names) num_anchors = len(anchors) # TODO: Assumes dim ordering is channel last model_output_channels = yolo_model.layers[-1].output_shape[-1] assert model_output_channels == num_anchors * (num_classes + 5), \ 'Mismatch between model and given anchor and class sizes. ' \ 'Specify matching anchors and classes with --anchors_path and ' \ '--classes_path flags.' print('{} model, anchors, and classes loaded.'.format(model_path)) # Check if model is fully convolutional, assuming channel last order. model_image_size = yolo_model.layers[0].input_shape[1:3] is_fixed_size = model_image_size != (None, None) # Generate colors for drawing bounding boxes. hsv_tuples = [(x / len(class_names), 1., 1.) for x in range(len(class_names))] colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) colors = list( map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors)) random.seed(10101) # Fixed seed for consistent colors across runs. random.shuffle(colors) # Shuffle colors to decorrelate adjacent classes. random.seed(None) # Reset seed to default. # Generate output tensor targets for filtered bounding boxes. # TODO: Wrap these backend operations with Keras layers. yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) input_image_shape = K.placeholder(shape=(2, )) # Get target scores for boxes, compute gradients boxes, scores, classes, target_scores = yolo_eval_adv( yolo_outputs, input_image_shape, score_threshold=args.score_threshold, iou_threshold=args.iou_threshold) correct_scores_loss = K.sum(scores) grad_correct, = K.gradients(correct_scores_loss, yolo_model.input) target_scores_loss = K.sum(target_scores) grad_target, = K.gradients(target_scores_loss, yolo_model.input) grad_sign = K.sign(grad_target) # target_boxes = np.array([[[0.5, 0.5, 0.25, 0.25, 0]], [[0.5, 0.5, 0.25, 0.25, 0]], [[0.5, 0.5, 0.25, 0.25, 0]], [[0.5, 0.5, 0.25, 0.25, 0]], [[0.5, 0.5, 0.25, 0.25, 0]]], dtype=np.float32) target_boxes = np.array([[[0.25, 0.25, 0.25, 0.25, 0]]], dtype=np.float32) detectors_mask, matching_true_boxes = get_detector_mask(target_boxes, anchors) args = (yolo_model.output, target_boxes, detectors_mask, matching_true_boxes) model_loss = yolo_loss(args, anchors, len(class_names)) grad_mloss, = K.gradients(model_loss, yolo_model.input) for image_file in os.listdir(test_path): try: image_type = imghdr.what(os.path.join(test_path, image_file)) if not image_type: continue except IsADirectoryError: continue image = Image.open(os.path.join(test_path, image_file)) original_size = image.size if is_fixed_size: # TODO: When resizing we can use minibatch input. resized_image = image.resize( tuple(reversed(model_image_size)), Image.BICUBIC) image_data = np.array(resized_image, dtype='float32') else: # Due to skip connection + max pooling in YOLO_v2, inputs must have # width and height as multiples of 32. new_image_size = (image.width - (image.width % 32), image.height - (image.height % 32)) resized_image = image.resize(new_image_size, Image.BICUBIC) image_data = np.array(resized_image, dtype='float32') print(image_data.shape) image_data /= 255. image_data = np.expand_dims(image_data, 0) # Add batch dimension. image_data_adv = image_data.copy() print("Generating adversarial image") for i in range(100): print("Iteration " + str(i+1)) mloss, g_mloss, = sess.run( [model_loss, grad_mloss], feed_dict={ yolo_model.input: image_data_adv, input_image_shape: [image.size[1], image.size[0]], K.learning_phase(): 0 } ) # Use sign instead of entire gradient # print(type(g_mloss)) print(mloss) r = g_mloss gamma = 1e-1 r = gamma * r # Gradient clipping? # image_data_adv = np.clip(image_data_adv + r, 0, image_data_adv) image_data_adv = image_data_adv - r ''' print("Generating adversarial image") for i in range(30): print("Iteration " + str(i+1)) g_correct, g_target, g_sign = sess.run( [grad_correct, grad_target, grad_sign], feed_dict={ yolo_model.input: image_data_adv, input_image_shape: [image.size[1], image.size[0]], K.learning_phase(): 0 } ) # Use sign instead of entire gradient r = g_sign gamma = 1e-5 r = gamma * r # Only factor in gradient from target # r = g_target # gamma = 0.01 # r = gamma * r / (np.max(np.abs(r)) + 1e-7) # Factor in correct and target gradient # gamma = 0.01 # g_correct = g_correct / (np.max(np.abs(g_correct)) + 1e-7) # g_target = g_target / (np.max(np.abs(g_target)) + 1e-7) # r = g_target-g_correct # r = gamma * r / (np.max(np.abs(r)) + 1e-7) # Gradient clipping? image_data_adv = np.clip(image_data_adv + r, 0, image_data_adv*(1+gamma)) # image_data_adv = image_data_adv / np.max(np.abs(image_data_adv)) # normalize ''' # image_data_adv = image_data_adv / np.max(np.abs(image_data_adv)) # normalize image_adv = getImage(image_data_adv, original_size) print("Testing adversarial image") out_boxes, out_scores, out_classes = sess.run( [boxes, scores, classes], feed_dict={ yolo_model.input: image_data_adv, input_image_shape: [image.size[1], image.size[0]], K.learning_phase(): 0 }) print('Found {} boxes for {}'.format(len(out_boxes), image_file)) font = ImageFont.truetype( font='font/FiraMono-Medium.otf', size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32')) thickness = (image.size[0] + image.size[1]) // 300 for i, c in reversed(list(enumerate(out_classes))): predicted_class = class_names[c] box = out_boxes[i] score = out_scores[i] label = '{} {:.2f}'.format(predicted_class, score) draw = ImageDraw.Draw(image_adv) label_size = draw.textsize(label, font) top, left, bottom, right = box top = max(0, np.floor(top + 0.5).astype('int32')) left = max(0, np.floor(left + 0.5).astype('int32')) bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32')) right = min(image.size[0], np.floor(right + 0.5).astype('int32')) print(label, (left, top), (right, bottom)) if top - label_size[1] >= 0: text_origin = np.array([left, top - label_size[1]]) else: text_origin = np.array([left, top + 1]) # My kingdom for a good redistributable image drawing library. for i in range(thickness): draw.rectangle( [left + i, top + i, right - i, bottom - i], outline=colors[c]) draw.rectangle( [tuple(text_origin), tuple(text_origin + label_size)], fill=colors[c]) draw.text(text_origin, label, fill=(0, 0, 0), font=font) del draw image_adv.save(os.path.join(output_path, image_file), quality=90) sess.close()
def build_model(self): """Build a critic (value) network that maps (state, action) pairs -> Q-values.""" # Define input layers # These two layers can first be processed via separate "pathways" (mini sub-networks), but eventually need to be combined. states = layers.Input(shape=(self.state_size, ), name='states') actions = layers.Input(shape=(self.action_size, ), name='actions') # Add hidden layer(s) for state pathway net_states = layers.Dense( units=400, activation='relu', kernel_regularizer=regularizers.l2(0.01))(states) # net_states = layers.Dense(units=400, kernel_regularizer=regularizers.l2(0.01))(states) # net_states = layers.BatchNormalization()(net_states) # net_states = layers.Activation('relu')(net_states) net_states = layers.Dense( units=300, activation='relu', kernel_regularizer=regularizers.l2(0.01))(net_states) # net_states = layers.Dense(units=300, kernel_regularizer=regularizers.l2(0.01))(net_states) # net_states = layers.BatchNormalization()(net_states) # net_states = layers.Activation('relu')(net_states) # Add hidden layer(s) for action pathway net_actions = layers.Dense( units=400, activation='relu', kernel_regularizer=regularizers.l2(0.01))(actions) net_actions = layers.Dense( units=300, activation='relu', kernel_regularizer=regularizers.l2(0.01))(net_actions) # Try different layer sizes, activations, add batch normalization, regularizers, etc. # Combine state and action pathways net = layers.Add()([net_states, net_actions]) net = layers.Activation('relu')(net) # Add more layers to the combined network if needed net = layers.Dense(units=64, activation='relu', kernel_regularizer=regularizers.l2(0.01))(net) # Add final output layer to prduce action values (Q values) # The final output of this model is the Q-value for any given (state, action) pair. Q_values = layers.Dense(units=1, name='q_values')(net) # Create Keras model self.model = models.Model(inputs=[states, actions], outputs=Q_values) # Define optimizer and compile model for training with built-in loss function optimizer = optimizers.Adam(lr=0.001) self.model.compile(optimizer=optimizer, loss='mse') # Compute action gradients (derivative of Q values w.r.t. to actions) # Compute the gradient of this Q-value with respect to the corresponding action vector, needed for training the actor model. action_gradients = K.gradients(Q_values, actions) # Define an additional function to fetch action gradients (to be used by actor model) self.get_action_gradients = K.function( inputs=[*self.model.input, K.learning_phase()], outputs=action_gradients)
def main(): parser = argparse.ArgumentParser( prog='plot_saliency.py', description='ML-Assignment3 visualize attention heat map.') parser.add_argument('--epoch', type=int, metavar='<#epoch>', default=1) args = parser.parse_args() model_name = "modelok" model_path = os.path.join(model_dir, model_name) emotion_classifier = load_model(model_path) print( colored("Loaded model from {}".format(model_name), 'yellow', attrs=['bold'])) ''' private_pixels = load_pickle('fer2013/test_with_ans_pixels.pkl') private_pixels = [ np.fromstring(private_pixels[i], dtype=float, sep=' ').reshape((1, 48, 48, 1)) for i in range(len(private_pixels)) ] ''' private_pixels = load_np('valid_p.npy') input_img = emotion_classifier.input img_ids = [17] for idx in img_ids: val_proba = emotion_classifier.predict( np.expand_dims(private_pixels[idx], axis=0)) pred = val_proba.argmax(axis=-1) print('pred=', pred) target = K.mean(emotion_classifier.output[:, pred]) grads = K.gradients(target, input_img)[0] fn = K.function([input_img, K.learning_phase()], [grads]) """ Implement your heatmap processing here! hint: Do some normalization or smoothening on grads """ heatmap = deprocessimage(grad) ''' =============== ''' thres = 0.5 see = private_pixels[idx] # for i in range(48): # for j in range(48): # print heatmap[i][j] print('see=', type(see), 'heatmap=', type(heatmap), 'fn=', type(fn)) see[np.where(np.array(heatmap) <= thres)] = np.mean(see) plt.figure() plt.imshow(heatmap, cmap=plt.cm.jet) plt.colorbar() plt.tight_layout() fig = plt.gcf() plt.draw() test_dir = os.path.join(cmap_dir, 'test') if not os.path.exists(test_dir): os.makedirs(test_dir) fig.savefig(os.path.join(test_dir, '{}.png'.format(idx)), dpi=100) plt.figure() plt.imshow(see, cmap='gray') plt.colorbar() plt.tight_layout() fig = plt.gcf() plt.draw() test_dir = os.path.join(partial_see_dir, 'test') if not os.path.exists(test_dir): os.makedirs(test_dir) fig.savefig(os.path.join(test_dir, '{}.png'.format(idx)), dpi=100)
#y = K.variable(value = Y_train) #label = K.variable(value = L_train) W = K.variable(value=1.) #np.random.rand(1)) b = K.variable(value=-1.) #np.random.rand(1)) U = K.square(W * x + b) lamda = K.variable(value=1.) #np.random.rand(1)) U2 = y - lamda * U U2 = K.tanh(U2) loss = K.square(U2 - label) loss = K.mean(loss) grad = K.gradients(loss, [W, b, lamda]) #--------------------uniformize gradient sum_grad = 0 for i in range(len(grad)): sum_grad += K.square(grad[i]) for i in range(len(grad)): grad[i] /= (K.sqrt(sum_grad) + 1e-5) #--------using sgd with momentum momentum = 0.7 for i in range(len(grad)): grad[i] = grad_t[i] * momentum + grad[i] step = K.variable(value=0.05)
content_feat = image_features[layer][CONTENT, :, :, :] target_feat = image_features[layer][TARGET, :, :, :] loss += content_weight * content_loss(content_feat, target_feat) nb_layers = len(style_feature_layers) for i, layer in enumerate(style_feature_layers): style_feat = image_features[layer][STYLE, :, :, :] target_feat = image_features[layer][TARGET, :, :, :] style_masks = mask_features[layer][STYLE, :, :, :] target_masks = mask_features[layer][TARGET, :, :, :] sl = style_loss(style_feat, target_feat, style_masks, target_masks) loss += (style_weight / (2**(nb_layers - (i + 1)))) * sl loss += total_variation_weight * total_variation_loss(target_image) loss_grads = K.gradients(loss, target_image) # Evaluator class for computing efficiency outputs = [loss] if type(loss_grads) in {list, tuple}: outputs += loss_grads else: outputs.append(loss_grads) f_outputs = K.function([target_image], outputs) def eval_loss_and_grads(x): if K.image_dim_ordering() == 'th': x = x.reshape((1, 3, img_nrows, img_ncols)) else:
#convolutional layer as the input instead of a fully-conected layer_output = layer_dict[layer_name].output #layer_output = model.layers[0].output for n in range(1,32): filter_index = n print('Processing filter %d' % filter_index) start_time = time.time() #build a loss function that maximizes the activation of n filters of the layer considered #TODO: select one of these loss function depending on which layer you're using #loss = backend.mean(layer_output[:, filter_index]) # loss function for dense layers loss = backend.mean(layer_output[:,filter_index,:,:]) # for non-dense layers #compute gradient of input picture wrt this loss grads = backend.gradients(loss, input_img)[0] #normalize gradient to avoid values grads /= (backend.sqrt(backend.mean(backend.square(grads))) + 1e-5) #this function returns loss and grads given the input picture iterate = backend.function([input_img, backend.learning_phase()],[loss,grads]) #set numpy's random seed for reproducibility numpy.random.seed(117) #start with random grayscale image to begin gradient ascent on input_img_data = numpy.random.random((1,1,img_width, img_height))*20+128 #run gradient ascent on current filter for x steps until loss function is maximized for i in range(1000): #compute loss and gradients
def _learner_thread(thread_id, session_global, graph_ops): logger = logging.getLogger("learner") logger.info("STARTING THREAD:%d" % thread_id) # params tensors actor_params = graph_ops["actor_params"] critic_params = graph_ops["critic_params"] env = AntTurnEnv({ 'server_ip': '127.0.0.1', 'server_port': VREP_PORT + thread_id, 'vrep_exec_path': None, 'vrep_scene_file': None, 'per_step_reward': PER_STEP_REWARD, 'final_reward': FINAL_REWARD, 'tolerance': TOLERANCE, 'spawn_radius': SPAWN_RADIUS }) gamma = 0.99 with tf.Graph().as_default(), tf.Session() as session: # make local networks lstate = tf.placeholder(tf.float32, [None, STATE_SIZE]) lgoal = tf.placeholder(tf.float32, [None, GOAL_SIZE]) laction = tf.placeholder(tf.float32, [None, ACTION_SIZE]) lactor, lact_grad, lactor_grads = create_actor_with_goal( STATE_SIZE, GOAL_SIZE, ACTION_SIZE) lcritic, l_action_grads = create_critic_with_goal( STATE_SIZE, GOAL_SIZE, ACTION_SIZE) # basics lq_values = lcritic([lstate, lgoal, laction]) lactor_action = lactor([lstate, lgoal]) # obtain critic grad_Q_wrt_a l_grad_Q_wrt_a = K.function( inputs=[lcritic.inputs[0], lcritic.inputs[1], lcritic.inputs[2]], outputs=l_action_grads) # obtain full actor grads l_actor_full_grads = K.function( inputs=[lactor.inputs[0], lactor.inputs[1], lact_grad], outputs=lactor_grads) # obtain full critic gradients target_qs = tf.placeholder(tf.float32, [None, 1]) critic_loss = tf.reduce_sum(tf.square(target_qs - lq_values)) critic_grads = K.gradients(critic_loss, lcritic.trainable_weights) l_critic_full_grads = K.function( inputs=[lstate, lgoal, laction, target_qs], outputs=critic_grads) lactor_params = lactor.trainable_weights lcritic_params = lcritic.trainable_weights lactor_set_params = lambda x: [ lactor_params[i].assign(x[i]) for i in range(len(x)) ] lcritic_set_params = lambda x: [ lcritic_params[i].assign(x[i]) for i in range(len(x)) ] session.run(tf.global_variables_initializer()) logger.info("Obtaining target network params") local_params = [session_global.run(param) for param in actor_params] lactor_set_params(local_params) local_params = [session_global.run(param) for param in critic_params] lcritic_set_params(local_params) num_goals_achieved = 0 for epoch in range(0, NUM_EPOCHS): cur_states = np.zeros((0, STATE_SIZE)) cur_goals = np.zeros((0, GOAL_SIZE)) next_states = np.zeros((0, STATE_SIZE)) next_goals = np.zeros((0, GOAL_SIZE)) actions = np.zeros((0, ACTION_SIZE)) rewards = np.zeros((0, 1)) non_terminal = np.zeros((0, 1)) episode = 0 for episode in range(0, NUM_EPISODES_PER_EPOCH): # cur_goal = randomly generated starting goal rand_goal = np.random.uniform(-np.pi / 12.0, +np.pi / 12.0) env.set_goal(rand_goal) cur_state, cur_goal = env.start() thread_goals[thread_id, epoch, episode] = cur_goal next_state, next_goal = cur_state, cur_goal num_steps = 0 total_reward = 0 logger.info("START:epoch:%d, episode:%d, goal:%f" % (epoch, episode, cur_goal)) # V-REP streaming should now be ready for step_no in range(0, MAX_EPISODE_LEN): action = session.run(lactor_action, feed_dict={ lstate: cur_state, lgoal: cur_goal })[0].reshape((1, -1)) # NOTE : adding a small random noise to system action += np.random.normal(loc=0.0, scale=0.05, size=action.shape) next_state, next_goal, reward, has_ended = env.step( np.hstack((action.reshape((-1, )), np.zeros(5)))) cur_states = np.vstack((cur_states, cur_state)) next_states = np.vstack((next_states, next_state)) cur_goals = np.vstack((cur_goals, cur_goal)) next_goals = np.vstack((next_goals, next_goal)) rewards = np.vstack((rewards, reward)) if has_ended: non_terminal = np.vstack((non_terminal, 0.0)) num_goals_achieved += 1 else: non_terminal = np.vstack((non_terminal, 1.0)) actions = np.vstack((actions, action)) total_reward += reward num_steps += 1 if has_ended: logger.info("achieved goal") break cur_state = next_state cur_goal = next_goal env.reset() thread_num_steps[thread_id, epoch, episode] = num_steps thread_episodic_reward[thread_id, epoch, episode] = total_reward logger.info("END:epoch:%d, episode:%d, goal:%f, num_steps:%d" % (epoch, episode, rand_goal, num_steps)) logger.info("preparing updates") for update_no in range(0, NUM_UPDATES_PER_EPOCH): # obtain critic q-values logger.info("update:%d" % update_no) q_values = session.run(lq_values, feed_dict={ lstate: next_states, lgoal: next_goals, laction: actions }).reshape((-1, 1)) # obtain targets for critic targets = rewards + gamma * np.multiply( non_terminal, q_values).reshape((-1, 1)) # compute full actor and critic grads # actor update # logger.info("ACTOR update") local_action_grads = l_grad_Q_wrt_a( [cur_states, cur_goals, actions])[0] full_local_actor_grads = l_actor_full_grads( [cur_states, cur_goals, local_action_grads]) graph_ops["actor_grad_copy"](full_local_actor_grads) session_global.run(graph_ops["actor_grad_apply"]) # critic update # logger.info("CRITIC update") full_local_critic_grads = l_critic_full_grads( [cur_states, cur_goals, actions, targets]) graph_ops["critic_grad_copy"](full_local_critic_grads) session_global.run(graph_ops["critic_grad_apply"]) # update params from server logger.info("RESET TARGET NETWORK PARAMS") local_params = [ session_global.run(param) for param in actor_params ] lactor_set_params(local_params) local_params = [ session_global.run(param) for param in critic_params ] lcritic_set_params(local_params) # compute losses for summary thread_critic_loss[thread_id, epoch] = session.run(critic_loss, feed_dict={ lstate: cur_states, lgoal: cur_goals, laction: actions, target_qs: targets }) thread_goals_achieved[thread_id, epoch] = 100 * num_goals_achieved * 1.0 / ( (epoch + 1) * NUM_EPISODES_PER_EPOCH * 1.0) # if out of sync, re-sync target networks with server if np.mod(epoch, RESET_TARGET_NETWORK_EPOCHS) == 0: logger.info("RESET TARGET NETWORK PARAMS") local_params = [ session_global.run(param) for param in actor_params ] lactor_set_params(local_params) local_params = [ session_global.run(param) for param in critic_params ] lcritic_set_params(local_params) logger.info("EXITING THREAD:%d" % thread_id)
# plt.show() # make a content model # try different cutoffs to see the images that result content_model = VGG16_AvgPool_CutOff(shape, 11) # make the target target = K.variable(content_model.predict(x)) # try to match the image # define our loss in keras loss = K.mean(K.square(target - content_model.output)) # gradients which are needed by the optimizer grads = K.gradients(loss, content_model.input) # just like theano.function get_loss_and_grads = K.function(inputs=[content_model.input], outputs=[loss] + grads) def get_loss_and_grads_wrapper(x_vec): # scipy's minimizer allows us to pass back # function value f(x) and its gradient f'(x) # simultaneously, rather than using the fprime arg # # we cannot use get_loss_and_grads() directly # input to minimizer func must be a 1-D array # input to get_loss_and_grads must be [batch_of_images] # # gradient must also be a 1-D array
def dream(base_image_path, result_prefix, feature_number, mixed_layers): # These are the names of the layers # for which we try to maximize activation, # as well as their weight in the final loss # we try to maximize. # You can tweak these setting to obtain new visual effects. settings = { 'features': { # 'mixed0': 1, # 'mixed3': 0.5, # 'mixed4': 2., # 'mixed5': 1.5, }, } for ml in mixed_layers: settings['features'][ml] = 1 def preprocess_image(image_path): # Util function to open, resize and format pictures # into appropriate tensors. img = plt.imread(image_path) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = inception_v3.preprocess_input(img) return img def deprocess_image(x): # Util function to convert a tensor into a valid image. if K.image_data_format() == 'channels_first': x = x.reshape((3, x.shape[2], x.shape[3])) x = x.transpose((1, 2, 0)) else: x = x.reshape((x.shape[1], x.shape[2], 3)) x /= 2. x += 0.5 x *= 255. x = np.clip(x, 0, 255).astype('uint8') return x K.set_learning_phase(0) # Build the InceptionV3 network with our placeholder. # The model will be loaded with pre-trained ImageNet weights. model = inception_v3.InceptionV3(weights='imagenet', include_top=False) dream = model.input print('Model loaded.') # Get the symbolic outputs of each "key" layer (we gave them unique names). layer_dict = dict([(layer.name, layer) for layer in model.layers]) # Define the loss. loss = K.variable(0.) for layer_name in settings['features']: # Add the L2 norm of the features of a layer to the loss. assert layer_name in layer_dict.keys( ), 'Layer ' + layer_name + ' not found in model.' coeff = settings['features'][layer_name] x = layer_dict[layer_name].output # We avoid border artifacts by only involving non-border pixels in the loss. scaling = K.prod(K.cast(K.shape(x), 'float32')) if K.image_data_format() == 'channels_first': loss += coeff * K.sum(K.square(x[:, feature_number, 2:-2, 2:-2])) / scaling else: loss += coeff * K.sum(K.square(x[:, 2:-2, 2:-2, feature_number])) / scaling # Compute the gradients of the dream wrt the loss. grads = K.gradients(loss, dream)[0] # Normalize gradients. grads /= K.maximum(K.mean(K.abs(grads)), K.epsilon()) # Set up function to retrieve the value # of the loss and gradients given an input image. outputs = [loss, grads] fetch_loss_and_grads = K.function([dream], outputs) def eval_loss_and_grads(x): outs = fetch_loss_and_grads([x]) loss_value = outs[0] grad_values = outs[1] return loss_value, grad_values def resize_img(img, size): img = np.copy(img) if K.image_data_format() == 'channels_first': factors = (1, 1, float(size[0]) / img.shape[2], float(size[1]) / img.shape[3]) else: factors = (1, float(size[0]) / img.shape[1], float(size[1]) / img.shape[2], 1) return scipy.ndimage.zoom(img, factors, order=1) def gradient_ascent(x, iterations, step, max_loss=None): for i in range(iterations): loss_value, grad_values = eval_loss_and_grads(x) if max_loss is not None and loss_value > max_loss: break print('..Loss value at', i, ':', loss_value) x += step * grad_values return x def save_img(img, fname): pil_img = deprocess_image(np.copy(img)) scipy.misc.imsave(fname, pil_img) """Process: - Load the original image. - Define a number of processing scales (i.e. image shapes), from smallest to largest. - Resize the original image to the smallest scale. - For every scale, starting with the smallest (i.e. current one): - Run gradient ascent - Upscale image to the next scale - Reinject the detail that was lost at upscaling time - Stop when we are back to the original size. To obtain the detail lost during upscaling, we simply take the original image, shrink it down, upscale it, and compare the result to the (resized) original image. """ # Playing with these hyperparameters will also allow you to achieve new effects step = 0.01 # Gradient ascent step size num_octave = 3 # Number of scales at which to run gradient ascent octave_scale = 1.4 # Size ratio between scales iterations = 20 # Number of ascent steps per scale max_loss = 10. #Original: # # Playing with these hyperparameters will also allow you to achieve new effects # step = 0.01 # Gradient ascent step size # num_octave = 3 # Number of scales at which to run gradient ascent # octave_scale = 1.4 # Size ratio between scales # iterations = 20 # Number of ascent steps per scale # max_loss = 10. img = preprocess_image(base_image_path) if K.image_data_format() == 'channels_first': original_shape = img.shape[2:] else: original_shape = img.shape[1:3] successive_shapes = [original_shape] for i in range(1, num_octave): shape = tuple([int(dim / (octave_scale**i)) for dim in original_shape]) successive_shapes.append(shape) successive_shapes = successive_shapes[::-1] original_img = np.copy(img) shrunk_original_img = resize_img(img, successive_shapes[0]) for shape in successive_shapes: print('Processing image shape', shape) img = resize_img(img, shape) img = gradient_ascent(img, iterations=iterations, step=step, max_loss=max_loss) upscaled_shrunk_original_img = resize_img(shrunk_original_img, shape) same_size_original = resize_img(original_img, shape) lost_detail = same_size_original - upscaled_shrunk_original_img img += lost_detail shrunk_original_img = resize_img(original_img, shape) save_img(img, fname=result_prefix + "fn_" + str(feature_number) + "_" + mixed_layers[0] + "_" + os.path.basename(base_image_path))
style_features = layer_features[1, :, :, :] combination_features = layer_features[2, :, :, :] sl = style_loss(style_features, combination_features) loss += ((style_weight / len(feature_layers)) * sl) def total_variation_loss(x): a = backend.square(x[:, :height-1, :width-1, :] - x[:, 1:, :width-1, :]) b = backend.square(x[:, :height-1, :width-1, :] - x[:, :height-1, 1:, :]) return backend.sum(backend.pow(a + b, 1.25)) loss+=( total_variation_weight * total_variation_loss(combination_image)) grads = backend.gradients(loss, combination_image) outputs = [loss] outputs += grads f_outputs = backend.function([combination_image], outputs) def eval_loss_and_grads(x): x = x.reshape((1, height, width, 3)) outs = f_outputs([x]) loss_value = outs[0] grad_values = outs[1].flatten().astype('float64') return loss_value, grad_values class Evaluator(object): def __init__(self):
loss = K.variable(0.) layer_features = outputs_dict[content_layer] target_image_features = layer_features[0, :, :, :] combination_features = layer_features[2, :, :, :] loss += content_weight * content_loss(target_image_features, combination_features) for layer_name in style_layers: layer_features = outputs_dict[layer_name] style_reference_features = layer_features[1, :, :, :] combination_features = layer_features[2, :, :, :] sl = style_loss(style_reference_features, combination_features) loss += (style_weight / len(style_layers)) * sl loss += total_variation_weight * total_variation_loss(combination_image) # Get the gradients of the generated image wrt the loss grads = K.gradients(loss, combination_image)[0] # Function to fetch the values of the current loss and the current gradients fetch_loss_and_grads = K.function([combination_image], [loss, grads]) class Evaluator(object): def __init__(self): self.loss_value = None self.grads_values = None def loss(self, x): assert self.loss_value is None x = x.reshape((1, img_height, img_width, 3)) outs = fetch_loss_and_grads([x]) loss_value = outs[0]
def synTexture(self, im=None, G0_from=None,onlyGram=False,maxiter=500): model = self.model use_caffe = self.use_caffe conv_layers = [0,1,3,4,6,7,8,9,11,12,13,14,16,17,18,19] mm=np.array([ 0.40760392, 0.45795686, 0.48501961]) if im is not None: im0=im if not use_caffe: im[:,:,0] -= 103.939 im[:,:,1] -= 116.779 im[:,:,2] -= 123.68 else: # caffe im = im/255.0 im[:,:,0]=im[:,:,0]-np.mean(im[:,:,0])+mm[0] # b im[:,:,1]=im[:,:,1]-np.mean(im[:,:,1])+mm[1] # g im[:,:,2]=im[:,:,2]-np.mean(im[:,:,2])+mm[2] # r #im = im.transpose((2,0,1)) im = np.expand_dims(im, axis=0) # Test pretrained model #model = VGG_19_1('dataout.h5',onlyconv=True) #vgg19_weights.h5 #sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) #model.compile(optimizer=sgd, loss='categorical_crossentropy') print 'predicting' out = model.predict(im) #conv_layers = [1,3,6,8,11,13,15,17,20,22,24,26,29,31,33,35] activations = get_activations(model,im,conv_layers) G0, F0 = get_gram_matrices(activations) elif G0_from is not None: G0 = pickle.load(open(G0_from,'r')) print 'using saved G0 file (probably compressed)' #G0 = compress_gram(G0) if onlyGram: return G0, F0 G0_symb, N_l, M_l = get_gram_matrices_symb(model,sel=conv_layers) errf = get_gram_error(G0,G0_symb,N_l,M_l) #print errf grads = K.gradients(errf,model.input) #opt = Adam() #opt.get_gradients #updates = opt.get_updates([model.input],[],[errf]) #train = K.function([model.input],[errf, model.input],updates=updates) coef=0.5 if use_caffe else 128.0 gray_im = np.random.randn(224,224) im_iter = np.stack([gray_im,gray_im,gray_im],axis=2)*coef #im_iter = np.random.randn(im0.shape[0],im0.shape[1],im0.shape[2])*coef for i in range(3): if use_caffe: im_iter[:,:,i]=im_iter[:,:,i]+mm[i] else: im_iter[:,:,i]=im_iter[:,:,i]+0*128.0 iters = 10000 plt.figure() _,pp=plt.subplots(2,2) costs=[] grads_fun = K.function([model.input],grads) cost_fun = K.function([model.input],[errf]) """ x = Input(batch_shape=(1,224,224,3)) #opt_model = Model(inputs=x,outputs=G0_symb) class CustomLossLayer(Layer): def __init__(self, **kwargs): self.is_placeholder = True super(CustomLossLayer, self).__init__(**kwargs) def call(self, inputs): loss = get_gram_error(G0,inputs,N_l,M_l) self.add_loss(loss, inputs=inputs) return inputs y = CustomLossLayer()(G0_symb) # G0_symb is a list print y opt_model = Model([model.input], y) rmsprop = RMSprop() opt_model.compile(optimizer='rmsprop', loss=None) epochs=100 opt_model.fit([im_iter], shuffle=True, epochs=epochs, verbose=2, batch_size=1) res = opt_model.predict(im_iter) plt.imshow(im_iter) plt.show(block=False) plt.pause(0.01) """ #""" if use_caffe: ranges = [-1,4] limits = [0.0,1.0] else: ranges = [-8,-3] limits =[-255, 255.0] def limit_im(im): im[im<limits[0]]=limits[0] im[im>limits[1]]=limits[1] return im def best_stepsize(im0, grad, steps=np.logspace(ranges[0],ranges[1],15)): best = -1 best_loss = np.inf for s in steps: new_im = limit_im(im0-s*grad) #loss = cost_fun([np.expand_dims(im0-s*grad,0)])[0] loss = cost_fun([np.expand_dims(new_im,0)])[0] #print loss if loss<best_loss: best = s best_loss = loss return best # bfgs? imsize = [224,224,3] #maxiter = 2000 method = 'L-BFGS-B' #method = 'BFGS' global cur_iter cur_iter=0 global stats_im stats_im = [] global res_im res_im = [] def callback(x): global cur_iter global stats_im global res_im #print 'random number',np.random.rand(1) if not cur_iter%15: im = np.reshape(x,imsize)[:,:,::-1] im = np.sum(im,axis=2)/3.0 # turn to grayscale plt.imshow(im,cmap=plt.cm.gray) plt.imsave('res/syn_res.png',im,cmap=plt.cm.gray) if cur_iter==225: plt.imsave('res/exp_%d_res_%d.png'%(self.exp_no,cur_iter),im,cmap=plt.cm.gray) # saveing all stats stats_im = get_stats(im) stats_src, stats_tar = self.stats res_im = im with open('res/exp_%d_stats.txt'%self.exp_no,'w') as f: f.write('stats src') f.write(str(stats_src)) f.write('\nstats tar') f.write(str(stats_tar)) f.write('\nstats syn') f.write(str(stats_im)) print 'SAVED STATS' plt.title('iter %d'%cur_iter) cur_iter+=1 plt.show(block=False) plt.pause(0.01) m=20 reshape_im = lambda x: np.reshape(x, imsize) bounds = (np.ones_like(im_iter)*limits[0],np.ones_like(im_iter)*limits[1]) bounds = zip(bounds[0].flatten(),bounds[1].flatten()) min_fun = lambda x: cost_fun([np.expand_dims(reshape_im(x),0)])[0].astype(np.float64) grad_fun = lambda x: np.squeeze(grads_fun([np.expand_dims(reshape_im(x),0)])[0]).flatten().astype(np.float64) try: res = minimize(min_fun,im_iter.flatten().astype(np.float64), jac=grad_fun,method=method,bounds=bounds,callback=callback, options={'disp': False, 'maxiter':maxiter, 'maxcor': m, 'ftol': 0, 'gtol': 0}) except: pass stats_src, stats_tar = self.stats return stats_src, stats_tar, stats_im, res_im #pp[0,0].imshow(im_iter) #pp[0,1].imshow(res[::-1]) #plt.show(block=False) #plt.pause(0.01) #raw_input('done') #""" """
layer_dict = dict([(layer.name, layer) for layer in model.layers]) input_img = layer_dict["embedding_1"].output num_filters = layer_dict[layer_name].output.shape[2] filters = [] for filter_index in range(num_filters): start_time = time.time() # We build a loss function that maximizes the activation # of the nth filter of the layer considered layer_output = layer_dict[layer_name].output loss = K.mean(layer_output[:, :, filter_index]) grads = K.gradients(loss, input_img)[0] grads = normalize(grads) iterate = K.function([input_img], [loss, grads]) step = 0.3 input_img_data = np.random.random((1, img_width, img_height)) input_img_data = (input_img_data - 0.5) * 50 + 128 # Run gradient ascent for i in range(30): loss_value, grads_value = iterate([input_img_data]) input_img_data += grads_value * step if loss_value <= 0.:
def call(self, x, **kwargs): return K.gradients(self.y, x)[0]
def _generate_filter_image(input_img, layer_output, filter_index): """Generates image for one particular filter. # Arguments input_img: The input-image Tensor. layer_output: The output-image Tensor. filter_index: The to be processed filter number. Assumed to be valid. #Returns Either None if no image could be generated. or a tuple of the image (array) itself and the last loss. """ s_time = time.time() # we build a loss function that maximizes the activation # of the nth filter of the layer considered if K.image_data_format() == 'channels_first': loss = K.mean(layer_output[:, filter_index, :, :]) else: loss = K.mean(layer_output[:, :, :, filter_index]) # we compute the gradient of the input picture wrt this loss grads = K.gradients(loss, input_img)[0] # normalization trick: we normalize the gradient grads = normalize(grads) # this function returns the loss and grads given the input picture iterate = K.function([input_img], [loss, grads]) # we start from a gray image with some random noise intermediate_dim = tuple( int(x / (upscaling_factor ** upscaling_steps)) for x in output_dim) if K.image_data_format() == 'channels_first': input_img_data = np.random.random( (1, 3, intermediate_dim[0], intermediate_dim[1])) else: input_img_data = np.random.random( (1, intermediate_dim[0], intermediate_dim[1], 3)) input_img_data = (input_img_data - 0.5) * 20 + 128 # Slowly upscaling towards the original size prevents # a dominating high-frequency of the to visualized structure # as it would occur if we directly compute the 412d-image. # Behaves as a better starting point for each following dimension # and therefore avoids poor local minima for up in reversed(range(upscaling_steps)): # we run gradient ascent for e.g. 20 steps for _ in range(epochs): loss_value, grads_value = iterate([input_img_data]) input_img_data += grads_value * step # some filters get stuck to 0, we can skip them if loss_value <= K.epsilon(): return None # Calculate upscaled dimension intermediate_dim = tuple( int(x / (upscaling_factor ** up)) for x in output_dim) # Upscale img = deprocess_image(input_img_data[0]) img = np.array(pil_image.fromarray(img).resize(intermediate_dim, pil_image.BICUBIC)) input_img_data = np.expand_dims( process_image(img, input_img_data[0]), 0) # decode the resulting input image img = deprocess_image(input_img_data[0]) e_time = time.time() print('Costs of filter {:3}: {:5.0f} ( {:4.2f}s )'.format(filter_index, loss_value, e_time - s_time)) return img, loss_value
def hackAutoEncode(): raw_images = getDataFromMnist() generator_images = np.copy(raw_images) # image = get_images(generator_images) # image = (image / 2.0 + 0.5) * 255.0 # Image.fromarray(image.astype(np.uint8)).save('./hackAutoEncode/100mnist-raw.png') # image = 255.0 - image # Image.fromarray(image.astype(np.uint8)).save('./hackAutoEncode/100mnist-raw-w.png') # 将获取的图片reshape成想要的形状 generator_images = generator_images.reshape((100, 784)) # 获取模型 model = trainAutoEncode() # 都伪装成0 object_type_to_fake = getZeroFromMnist() # 获取标签为0的图片 object_type_to_fake = object_type_to_fake.reshape((28 * 28, )) object_type_to_fake = np.expand_dims(object_type_to_fake, axis=0) model_input_layer = model.layers[0].input model_output_layer = model.layers[-1].output # 生成的图像与图案0之间的差为损失函数 下面的三种损失函数都可以用 # cost_function = binary_crossentropy(y_pred,object_type_to_fake) cost_function = K.mean(K.square(model_output_layer-object_type_to_fake)) # cost_function = K.mean(K.binary_crossentropy(model_output_layer, object_type_to_fake)) gradient_function = K.gradients(cost_function, model_input_layer)[0] grab_cost_and_gradients_from_model = K.function([model_input_layer, K.learning_phase()], [cost_function, gradient_function]) cost = 0.0 e = 0.007 for index in range(100): mnist_image_raw = generator_images[index] mnist_image_hacked = np.copy(mnist_image_raw) mnist_image_hacked = mnist_image_hacked.reshape(28*28, ) mnist_image_hacked = np.expand_dims(mnist_image_hacked, axis=0) # 调整的极限 灰度图片 max_change_above = mnist_image_raw + 1.0 max_change_below = mnist_image_raw - 1.0 i = 0 cost = 100 while cost > -12.0 and i < 500: cost, gradients = grab_cost_and_gradients_from_model([mnist_image_hacked, 0]) n = np.sign(gradients) mnist_image_hacked -= n * e mnist_image_hacked = np.clip(mnist_image_hacked, max_change_below, max_change_above) mnist_image_hacked = np.clip(mnist_image_hacked, -1.0, 1.0) print("image_seg: {}, batch:{} Cost: {:.8}%".format(index, i, cost * 100)) i += 1 # 覆盖原有图片 generator_images[index] = mnist_image_hacked autoEncode_images = np.copy(generator_images) generator_images = generator_images.reshape(100, 28, 28, 1) # 保存图片 image = get_images(generator_images) image = (image/2.0+0.5)*255.0 Image.fromarray(image.astype(np.uint8)).save("./hackAutoEncode/100mnist-hacked.png") # image=255.0-image # Image.fromarray(image.astype(np.uint8)).save("hackAutoEncode/100mnist-hacked-w.png") # 灰度图像里面黑是0 白是255 可以把中间状态的处理下 # image[image>127]=255 # Image.fromarray(image.astype(np.uint8)).save("hackAutoEncode/100mnist-hacked-w-good.png") for index in range(100): mnist_image_raw = autoEncode_images[index] mnist_image_hacked = np.copy(mnist_image_raw) mnist_image_hacked=mnist_image_hacked.reshape(28*28) mnist_image_hacked = np.expand_dims(mnist_image_hacked, axis=0) preds = model.predict(mnist_image_hacked) autoEncode_images[index] = preds autoEncode_images = autoEncode_images.reshape((100, 28, 28, 1)) image = get_images(autoEncode_images) image = (image / 2.0 + 0.5) * 255.0 Image.fromarray(image.astype(np.uint8)).save("./hackAutoEncode/100mnist-hacked-autoEncode-b.png") image = 255.0-image Image.fromarray(image.astype(np.uint8)).save("./hackAutoEncode/100mnist-hacked-autoEncode-w.png") # 灰度图像里面黑是0 白是255 可以把中间状态的处理下 image[image > 127] = 255 Image.fromarray(image.astype(np.uint8)).save("./hackAutoEncode/100mnist-hacked-autoEncode-w-good.png")
img_name = "creative_commons_elephant.jpg" img_cam_name = "elephant_cam.jpg" img = image.load_img(img_path + img_name, target_size=(224, 224)) x = image.img_to_array(img) # 形状为(224,224,3)的float32格式的Numpy数组 x = np.expand_dims(x, axis=0) # 形状为(1,224,224,3)的float32格式的Numpy数组 x = preprocess_input(x) # 对输入数据进行预处理(按通道进行颜色标准化) # 使用VGG16模型预测图片的类别 preds = model.predict(x) print("预测结果:", decode_predictions(preds, top=3)[0]) print("预测结果对应的编号:", np.argmax(preds[0])) print("Code 5-42:使用 Grid-CAM 算法寻找图像中哪些部分最像非洲象") african_elephant_output = model.output[:, 386] last_conv_layer = model.get_layer('block5_conv3') # K.gradients()报错cost must be a scalar。是因为 Theano 不支持,请切换成 TensorFlow grads = K.gradients(african_elephant_output, last_conv_layer.output)[0] pooled_grads = K.mean(grads, axis=(0, 1, 2)) iterate = K.function([model.input], [pooled_grads, last_conv_layer.output[0]]) pooled_grads_value, conv_layer_output_value = iterate([x]) for i in range(512): conv_layer_output_value[:, :, i] *= pooled_grads_value[i] pass print("Code 5-43:绘制热力图") heatmap = np.mean(conv_layer_output_value, axis=-1) heatmap = np.maximum(heatmap, 0) heatmap /= np.max(heatmap) plt.matshow(heatmap) plt.title("图5-35:测试图像的“非洲象”类激活热力图") print("Code 5-44:将热力图与原始图像叠加")
def model_hander(idx, input_size=224, ch=3, iter_num=40): # [memo] # 描画された filter がノイズっぽい場合は # iteration の回数が足りない可能性.. model = VGG16(weights='imagenet', include_top=False) model.summary() print("\n") # select layer & filter number ---------- layer_name = 'block5_conv3' filter_index = idx print("\nget filter: {} in layer: {}".format(filter_index, layer_name)) # target層の n番目のフィルタの活性化を最大化する損失関数を定義 layer_output = model.get_layer(layer_name).output print("\nlayer output: ", layer_output) # Tensor("block5_conv3/Relu:0", shape=(None, None, None, 512), dtype=float32) print("type(layer output): ", type(layer_output)) # <class 'tensorflow.python.framework.ops.Tensor'> loss = K.mean(layer_output[:, :, :, filter_index]) print("\nloss: ", loss) # loss: Tensor("Mean:0", shape=(), dtype=float32) print("type(loss): ", type(loss)) # <class 'tensorflow.python.framework.ops.Tensor'> # 入力に関する損失関数の勾配を取得 # gradients の戻り値はテンソルのリスト (今回の場合は サイズ1 のリスト) # このため、リストの 0番目の要素だけを持つようにしている。 grads = K.gradients(loss, model.input)[0] print("\ngrads all: ", K.gradients(loss, model.input)) # grads all: [<tf.Tensor 'gradients_1/block1_conv1/convolution_grad/Conv2DBackpropInput:0' shape=(None, None, None, 3) dtype=float32>] print("\ngrads: ", grads) # Tensor("gradients/block1_conv1/convolution_grad/Conv2DBackpropInput:0", shape=(None, None, None, 3), dtype=float32) print("type(grads): ", type(grads)) # <class 'tensorflow.python.framework.ops.Tensor'> # 勾配の正規化 # 1e-5 を足しているのは 0 除算回避のため grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5) # 入力画像に基づいて、損失テンソルと勾配テンソルを計算する。 # iterate は引数に Numpy のテンソル (サイズ 1 のテンソルのリスト) をとり # 戻り値に 2つの Numpy テンソル (損失値と勾配値) のリストを返す関数として # keras の backend 関数で定義。 iterate = K.function([model.input], [loss, grads]) print("\niter: ", iterate) # <tensorflow.python.keras.backend.EagerExecutionFunction object at 0x13d7f6f60> print("type(iter): ", type(iterate)) # <class 'tensorflow.python.keras.backend.EagerExecutionFunction'> # test #loss_value, grads_value = iterate([np.zeros((1, input_size, input_size, 3))]) # input として ノイズが入ったグレースケール画像を使用 input_img = np.random.random((1, input_size, input_size, ch))*20 + 128. """ print("\nshowing input image...") plt.imshow(input_img[0]/255) plt.show() """ # 勾配上昇法を 40 step 実行 print("started gradient accendant...") lr = 1 # 各勾配の更新の大きさ for i in range(iter_num): # 損失関数と勾配を計算 loss_val, grads_val = iterate([input_img]) #print(" loss_val: ", loss_val) #print(" grads_val: ", grads_val) input_img += grads_val*lr print(" => Now step {} has done.".format(i+1)) img = input_img[0] return img
def generate_movie(steps, movie_name, *, target_size): if not steps: steps = random.sample(list(range(512)), 10) layer = layer_dict[LAYER_NAME] input_img = model.input sizes = [] next_size = target_size for i in range(SCALE_STEPS): sizes.insert(0, int(next_size)) next_size *= 0.9 initial_size, *sizes = sizes img_data = np.random.uniform(size=(1, initial_size, initial_size, 3)) + 128. loss = K.mean(layer.output[:, :, :, steps[0]]) print('about to start.') grads = K.gradients(loss, input_img)[0] grads = normalize(grads) iterate = K.function([input_img], [loss, grads]) for octave, size in enumerate(sizes): img_data = resize_img(img_data, (size, size)) loss_value, grads_value = iterate([img_data]) img_data += grads_value print('initialized.') scaled = int(target_size * 1.05) with imageio.get_writer(movie_name, mode='I', fps=25) as writer: pairs = list(zip(steps[:-1], steps[1:])) for prev_step, next_step in tqdm(pairs): for i in range(SCALE_STEPS * 3): weight = max(1.0, i / SCALE_STEPS) loss = (K.mean(layer.output[:, :, :, next_step]) * weight + K.mean(layer.output[:, :, :, prev_step]) * (1 - weight)) grads = K.gradients(loss, input_img)[0] grads = normalize(grads) iterate = K.function([input_img], [loss, grads]) img_data = resize_img(img_data, (scaled, scaled)) start = (scaled - target_size) // 2 # center crop img_data = img_data[:, start:start + target_size, start:start + target_size, :] # add a little noise img_data += np.random.uniform(size=(1, target_size, target_size, 3)) * 16 for i in range(7): loss_value, grads_value = iterate([img_data]) if i == 0: grads_value[0, :, :, 0] = ndimage.gaussian_filter( grads_value[0, :, :, 0], sigma=2) grads_value[0, :, :, 1] = ndimage.gaussian_filter( grads_value[0, :, :, 1], sigma=2) grads_value[0, :, :, 2] = ndimage.gaussian_filter( grads_value[0, :, :, 2], sigma=2) img_data += grads_value img_data[0] = visstd(img_data[0]) writer.append_data(np.uint8(img_data[0])) prev_step = next_step
def __init__( self, model, bounds, channel_axis="auto", preprocessing=(0, 1), predicts="probabilities", ): from keras import backend as K if channel_axis == "auto": if K.image_data_format() == "channels_first": channel_axis = 1 else: channel_axis = 3 super(KerasModel, self).__init__(bounds=bounds, channel_axis=channel_axis, preprocessing=preprocessing) import keras from pkg_resources import parse_version assert parse_version(keras.__version__) >= parse_version( "2.0.7"), "Keras version needs to be 2.0.7 or newer" if predicts == "probs": predicts = "probabilities" assert predicts in ["probabilities", "logits"] inputs = model.input labels = K.placeholder(shape=(None, )) predictions = model.output shape = K.int_shape(predictions) _, num_classes = shape assert num_classes is not None self._num_classes = num_classes if predicts == "probabilities": if K.backend() == "tensorflow": (predictions, ) = predictions.op.inputs loss = K.sparse_categorical_crossentropy(labels, predictions, from_logits=True) else: # pragma: no cover logging.warning( "relying on numerically unstable conversion from probabilities to softmax" ) loss = K.sparse_categorical_crossentropy(labels, predictions, from_logits=False) # transform the probability predictions into logits, so that # the rest of this code can assume predictions to be logits predictions = self._to_logits(predictions) elif predicts == "logits": loss = K.sparse_categorical_crossentropy(labels, predictions, from_logits=True) loss = K.sum(loss, axis=0) (gradient, ) = K.gradients(loss, [inputs]) backward_grad_logits = K.placeholder(shape=predictions.shape) backward_loss = K.sum(K.batch_dot(predictions, backward_grad_logits, axes=-1), axis=0) (backward_grad_inputs, ) = K.gradients(backward_loss, [inputs]) self._loss_fn = K.function([inputs, labels], [loss]) self._forward_fn = K.function([inputs], [predictions]) self._gradient_fn = K.function([inputs, labels], [gradient]) self._backward_fn = K.function([backward_grad_logits, inputs], [backward_grad_inputs]) self._forward_and_gradient_fn = K.function([inputs, labels], [predictions, gradient])
def get_kept_filters(layer_dict, layer_name, input_img, image_width, image_height, m): kept_filters = [] for filter_index in range(m): print('Processing filter %d' % filter_index) start_time = time.time() # funcion de perdida que amximiza la activacion del filtro layer_output = layer_dict[layer_name].output if K.image_data_format() == 'channels_first': loss = K.mean(layer_output[:, filter_index, :, :]) else: loss = K.mean(layer_output[:, :, :, filter_index]) # calcula el gradiente de la imagen inicial grads = K.gradients(loss, input_img)[0] # normalizar el gradiente grads = normalize(grads) # regresa la perdida y el gradiente iterate = K.function([input_img], [loss, grads]) # tamaño de paso step = 1. # una imagen gris con ruido aleatorio if K.image_data_format() == 'channels_first': input_img_data = np.random.random((1, 3, img_width, img_height)) else: input_img_data = np.random.random((1, img_width, img_height, 3)) input_img_data = (input_img_data - 0.5) * 20 + 128 # ascenso del grandiente en 20 pasos for i in range(20): loss_value, grads_value = iterate([input_img_data]) input_img_data += grads_value * step if loss_value <= 0.: # Eliminar filtros que se quedan en 0 break # decodificar la imagen resultante if loss_value > 0: img = deprocess_image(input_img_data[0]) kept_filters.append((img, loss_value)) end_time = time.time() print('Filter %d processed in %ds' % (filter_index, end_time - start_time)) n = len(kept_filters[0][0][0][0]) kept_filters.sort(key=lambda x: x[1], reverse=True) kept_filters = kept_filters[:n * n] margin = 5 width = n * img_width + (n - 1) * margin height = n * img_height + (n - 1) * margin stitched_filters = np.zeros((width, height, 3)) # Guardar la imágen for i in range(n): for j in range(n): img, loss = kept_filters[i * n + j] stitched_filters[(img_width + margin) * i: (img_width + margin) * i + img_width, (img_height + margin) * j: (img_height + margin) * j + img_height, :] = img # save the result to disk imsave('{layer}_{n}x{n}.png'.format(layer=layer_name, n=n), stitched_filters)
required_tensor = a ** 2 + b ** 2 + c ** 2 + 2 * b * c required_function = K.function(inputs=(a, b, c), outputs=(required_tensor,)) print("Exercise 1 complete") # Exercise 2 """x can be initialized in various ways It can be initialized as either x = K.variable(value=np.array(1)) Or it can initialized through one of K.ones(shape=()) or K.zeros(shape=())""" x = K.ones(shape=()) tanh_tensor = (K.exp(x) - K.exp(-x)) / (K.exp(x) + K.exp(-x)) gradient_tensor_list = K.gradients(loss=tanh_tensor, variables=(tanh_tensor,)) """Note that the above variable is a list of tensors each which holds the gradient of the tanh_tensor with respect to each of the variables given as the input. In this case, since there is only one variable, the gradient_tensor_list is a list with only element in it""" required_function = K.function(inputs=(x,), outputs=[tanh_tensor] + gradient_tensor_list) """Note that the input for the parameter outputs above can either be a list or a tuple. In the use above, we added two lists which would be concatenation operation in python which results in a single list holding the elements in both the lists""" print("Exercise 2 complete") # Exercise 3
# symbolic_conv_outputs = symbolic_conv_outputs[:2] # make a big model that outputs multiple layers' outputs multi_output_model = Model(vgg.input, symbolic_conv_outputs) # calculate the targets that are output at each layer style_layers_outputs = [ K.variable(y) for y in multi_output_model.predict(x) ] # calculate the total style loss loss = 0 for symbolic, actual in zip(symbolic_conv_outputs, style_layers_outputs): # gram_matrix() expects a (H, W, C) as input loss += style_loss(symbolic[0], actual[0]) grads = K.gradients(loss, multi_output_model.input) # just like theano.function get_loss_and_grads = K.function(inputs=[multi_output_model.input], outputs=[loss] + grads) def get_loss_and_grads_wrapper(x_vec): l, g = get_loss_and_grads([x_vec.reshape(*batch_shape)]) return l.astype(np.float64), g.flatten().astype(np.float64) final_img = minimize(get_loss_and_grads_wrapper, 10, batch_shape) plt.imshow(scale_img(final_img)) plt.title('The essence of STYLE') plt.show()
for p in range(n_part_param_layers[layer_id]) ]) trainable_count_id_start_end = np.insert(np.cumsum(trainable_count), 0, 0) t = 0 FI_train_all = np.zeros(n_train) # FI for all parameters grad_K = tf.concat( axis=1, values=[ tf.concat( axis=0, values=[ K.flatten(b)[..., None] for b in K.gradients(model.output[0, k], model.trainable_weights) ]) for k in range(n_class) ]) iterate = K.function([model.input], [grad_K, model.output]) for i in range(int(n_train * (set_part - 1)), int(n_train * set_part)): grad, pred_P = iterate([x_train[i][None]]) L0 = grad @ np.diag(((pred_P[0, ])**0.5 + epsilon)**-1) f_grad = (grad[:, y_train[i]] / (pred_P[0, y_train[i]] + epsilon)).T # shape=(1,...) #All trainable parameters
shape = layer_dict[layer_name].output_shape # we avoid border artifacts by only involving non-border pixels in the loss loss -= coeff * K.sum(K.square(x[:, :, 2:shape[2] - 2, 2:shape[3] - 2])) / np.prod(shape[1:]) # add continuity loss (gives image local coherence, can result in an artful blur) loss += settings['continuity'] * continuity_loss(dream) / (3 * img_width * img_height) # add image L2 norm to loss (prevents pixels from taking very high values, makes image darker) loss += settings['dream_l2'] * K.sum( K.square(dream)) / (3 * img_width * img_height) # feel free to further modify the loss as you see fit, to achieve new effects... # compute the gradients of the dream wrt the loss grads = K.gradients(loss, dream) # set up helper functions to extract the loss and gradients # from the computational graph as Numpy arrays f_grads = K.function([dream], grads) def eval_grads(x): x = x.reshape((1, 3, img_width, img_height)) return np.array(f_grads([x])).flatten().astype('float64') f_loss = K.function([dream], [loss]) def eval_loss(x):
# 3. Build a cost function that minimizes the distance between the expected gain and actual gain # ---------------------------------------------------------------------------------------------- l1_output_cb = contour_integration_model.layers[1].output l2_output_cb = contour_integration_model.layers[2].output input_cb = contour_integration_model.input # Mean Square Error Loss current_gain = l2_output_cb[:, tgt_filter_idx, tgt_neuron_loc, tgt_neuron_loc] / \ (l1_output_cb[:, tgt_filter_idx, tgt_neuron_loc, tgt_neuron_loc] + 1e-5) loss = (expected_gains - current_gain)**2 / expected_gains.shape[0] alpha_cb = contour_integration_model.layers[2].alpha bias_cb = contour_integration_model.layers[2].bias # Gradients of alpha and bias wrt to the loss function grads = K.gradients(loss, [alpha_cb, bias_cb]) grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5 ) # Normalize gradients, prevents too large or small gradients iterate = K.function([input_cb], [loss, grads]) # 4. Now iterate to find the best fit alpha and bias # -------------------------------------------------- step = 0.025 old_loss = 10000000 fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.set_xlabel("Iteration") ax.set_ylabel('Loss')
loss = K.variable(0.0, name='Loss') layer_features = outputs_dict[content_layer] content_features = layer_features[0, :, :, :] combination_features = layer_features[2, :, :, :] loss += content_weight * content_loss(content_features, combination_features) for layer_name in style_layers: layer_features = outputs_dict[layer_name] style_features = layer_features[1, :, :, :] combination_features = layer_features[2, :, :, :] sl = style_loss(style_features, combination_features) loss += (style_weight / len(style_layers)) * sl loss += total_variation_weight * total_variation_loss(canvas) grads = K.gradients(loss, canvas)[0] fetch_loss_and_grads = K.function([canvas], [loss, grads]) initial = content.copy() learning_rate = 1e-4 loss_history = [] iterations = 1000 for i in range(iterations): print(i) loss_i, grads_i = fetch_loss_and_grads([initial]) initial -= learning_rate * grads_i loss_history.append(loss_i) image = initial K.clear_session() image = image.reshape(canvas.shape)[0]