Python keras.backend.learning_phase() Examples
The following are 30
code examples of keras.backend.learning_phase().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
keras.backend
, or try the search function
.
Example #1
Source File: util.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 8 votes |
def get_deep_representations(model, X, batch_size=256): """ TODO :param model: :param X: :param batch_size: :return: """ # last hidden layer is always at index -4 output_dim = model.layers[-4].output.shape[-1].value get_encoding = K.function( [model.layers[0].input, K.learning_phase()], [model.layers[-4].output] ) n_batches = int(np.ceil(X.shape[0] / float(batch_size))) output = np.zeros(shape=(len(X), output_dim)) for i in range(n_batches): output[i * batch_size:(i + 1) * batch_size] = \ get_encoding([X[i * batch_size:(i + 1) * batch_size], 0])[0] return output
Example #2
Source File: test_tiny_yolo.py From object-detection with MIT License | 6 votes |
def image_detection(sess, image_path, image_file, colors): # Preprocess your image image, image_data = preprocess_image(image_path + image_file, model_image_size = (416, 416)) # Run the session with the correct tensors and choose the correct placeholders in the feed_dict. # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0}) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={yolo_model.input:image_data, K.learning_phase():0}) # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Draw bounding boxes on the image file image = draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image #image.save(os.path.join("out", image_file), quality=90) cv2.imwrite(os.path.join("out", "tiny_yolo_" + image_file), image, [cv2.IMWRITE_JPEG_QUALITY, 90]) return out_scores, out_boxes, out_classes
Example #3
Source File: features.py From detection-2016-nipsws with MIT License | 6 votes |
def get_feature_map_4(model, im): im = im.astype(np.float32) dim_ordering = K.image_dim_ordering() if dim_ordering == 'th': # 'RGB'->'BGR' im = im[::-1, :, :] # Zero-center by mean pixel im[0, :, :] -= 103.939 im[1, :, :] -= 116.779 im[2, :, :] -= 123.68 else: # 'RGB'->'BGR' im = im[:, :, ::-1] # Zero-center by mean pixel im[:, :, 0] -= 103.939 im[:, :, 1] -= 116.779 im[:, :, 2] -= 123.68 im = im.transpose((2, 0, 1)) im = np.expand_dims(im, axis=0) inputs = [K.learning_phase()] + model.inputs _convout1_f = K.function(inputs, [model.layers[23].output]) feature_map = _convout1_f([0] + [im]) feature_map = np.array([feature_map]) feature_map = feature_map[0, 0, 0, :, :, :] return feature_map
Example #4
Source File: features.py From detection-2016-nipsws with MIT License | 6 votes |
def get_image_descriptor_for_image(image, model): im = cv2.resize(image, (224, 224)).astype(np.float32) dim_ordering = K.image_dim_ordering() if dim_ordering == 'th': # 'RGB'->'BGR' im = im[::-1, :, :] # Zero-center by mean pixel im[0, :, :] -= 103.939 im[1, :, :] -= 116.779 im[2, :, :] -= 123.68 else: # 'RGB'->'BGR' im = im[:, :, ::-1] # Zero-center by mean pixel im[:, :, 0] -= 103.939 im[:, :, 1] -= 116.779 im[:, :, 2] -= 123.68 im = im.transpose((2, 0, 1)) im = np.expand_dims(im, axis=0) inputs = [K.learning_phase()] + model.inputs _convout1_f = K.function(inputs, [model.layers[33].output]) return _convout1_f([0] + [im])
Example #5
Source File: features.py From detection-2016-nipsws with MIT License | 6 votes |
def get_conv_image_descriptor_for_image(image, model): im = cv2.resize(image, (224, 224)).astype(np.float32) dim_ordering = K.image_dim_ordering() if dim_ordering == 'th': # 'RGB'->'BGR' im = im[::-1, :, :] # Zero-center by mean pixel im[0, :, :] -= 103.939 im[1, :, :] -= 116.779 im[2, :, :] -= 123.68 else: # 'RGB'->'BGR' im = im[:, :, ::-1] # Zero-center by mean pixel im[:, :, 0] -= 103.939 im[:, :, 1] -= 116.779 im[:, :, 2] -= 123.68 im = im.transpose((2, 0, 1)) im = np.expand_dims(im, axis=0) inputs = [K.learning_phase()] + model.inputs _convout1_f = K.function(inputs, [model.layers[31].output]) return _convout1_f([0] + [im])
Example #6
Source File: util.py From lid_adversarial_subspace_detection with MIT License | 6 votes |
def get_deep_representations(model, X, batch_size=256): """ TODO :param model: :param X: :param batch_size: :return: """ # last hidden layer is always at index -4 output_dim = model.layers[-4].output.shape[-1].value get_encoding = K.function( [model.layers[0].input, K.learning_phase()], [model.layers[-4].output] ) n_batches = int(np.ceil(X.shape[0] / float(batch_size))) output = np.zeros(shape=(len(X), output_dim)) for i in range(n_batches): output[i * batch_size:(i + 1) * batch_size] = \ get_encoding([X[i * batch_size:(i + 1) * batch_size], 0])[0] return output
Example #7
Source File: test_yolov3.py From object-detection with MIT License | 6 votes |
def image_detection(sess, image_path, image_file, colors): # Preprocess your image image, image_data = preprocess_image(image_path + image_file, model_image_size = (416, 416)) # Run the session with the correct tensors and choose the correct placeholders in the feed_dict. # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0}) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={yolov3.input:image_data, K.learning_phase():0}) # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Draw bounding boxes on the image file image = draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image #image.save(os.path.join("out", image_file), quality=90) cv2.imwrite(os.path.join("out", "yolov3_" + image_file), image, [cv2.IMWRITE_JPEG_QUALITY, 90]) return out_scores, out_boxes, out_classes
Example #8
Source File: cifar10_query_based.py From blackbox-attacks with MIT License | 6 votes |
def one_shot_method(prediction, x, curr_sample, curr_target, p_t): grad_est = np.zeros((BATCH_SIZE, IMAGE_ROWS, IMAGE_COLS, NUM_CHANNELS)) DELTA = np.random.randint(2, size=(BATCH_SIZE, IMAGE_ROWS, IMAGE_COLS, NUM_CHANNELS)) np.place(DELTA, DELTA==0, -1) y_plus = np.clip(curr_sample + args.delta * DELTA, CLIP_MIN, CLIP_MAX) y_minus = np.clip(curr_sample - args.delta * DELTA, CLIP_MIN, CLIP_MAX) if args.CW_loss == 0: pred_plus = K.get_session().run([prediction], feed_dict={x: y_plus, K.learning_phase(): 0})[0] pred_plus_t = pred_plus[np.arange(BATCH_SIZE), list(curr_target)] pred_minus = K.get_session().run([prediction], feed_dict={x: y_minus, K.learning_phase(): 0})[0] pred_minus_t = pred_minus[np.arange(BATCH_SIZE), list(curr_target)] num_est = (pred_plus_t - pred_minus_t) grad_est = num_est[:, None, None, None]/(args.delta * DELTA) # Getting gradient of the loss if args.CW_loss == 0: loss_grad = -1.0 * grad_est/p_t[:, None, None, None] return loss_grad
Example #9
Source File: Deconvnet-keras.py From Deconvnet-keras with MIT License | 5 votes |
def up(self, data, learning_phase = 0): ''' function to compute dense output in forward pass # Arguments data: Data to be operated in forward pass learning_phase: learning_phase of Keras, 1 or 0 # Returns Result of dense layer ''' self.up_data = self.up_func([data, learning_phase]) return self.up_data
Example #10
Source File: query_methods.py From DiscriminativeActiveLearning with MIT License | 5 votes |
def dropout_predict(self, data): f = K.function([self.model.layers[0].input, K.learning_phase()], [self.model.layers[-1].output]) predictions = np.zeros((self.T, data.shape[0], self.num_labels)) for t in range(self.T): predictions[t,:,:] = f([data, 1])[0] final_prediction = np.mean(predictions, axis=0) prediction_uncertainty = np.std(predictions, axis=0) return final_prediction, prediction_uncertainty
Example #11
Source File: actionCNN.py From SupervisedChromeTrex with MIT License | 5 votes |
def loadCNN(wf_index): global get_output model = Sequential() model.add(Conv2D(nb_filters, (nb_conv, nb_conv), padding='valid', input_shape=(img_channels, img_rows, img_cols))) convout1 = Activation('relu') model.add(convout1) model.add(Conv2D(nb_filters, (nb_conv, nb_conv))) convout2 = Activation('relu') model.add(convout2) model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) model.add(Dropout(0.5)) model.add(Flatten()) model.add(Dense(128)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(nb_classes)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) # Model summary model.summary() # Model conig details model.get_config() if wf_index >= 0: #Load pretrained weights fname = WeightFileName[int(wf_index)] print("loading ", fname) model.load_weights(fname) layer = model.layers[-1] get_output = K.function([model.layers[0].input, K.learning_phase()], [layer.output,]) return model # This function does the guessing work based on input images
Example #12
Source File: attacks.py From lid_adversarial_subspace_detection with MIT License | 5 votes |
def fast_gradient_sign_method(sess, model, X, Y, eps, clip_min=None, clip_max=None, batch_size=256): """ TODO :param sess: :param model: predictions or after-softmax :param X: :param Y: :param eps: :param clip_min: :param clip_max: :param batch_size: :return: """ # Define TF placeholders for the input and output x = tf.placeholder(tf.float32, shape=(None,) + X.shape[1:]) y = tf.placeholder(tf.float32, shape=(None,) + Y.shape[1:]) adv_x = fgsm( x, model(x), eps=eps, clip_min=clip_min, clip_max=clip_max, y=y ) X_adv, = batch_eval( sess, [x, y], [adv_x], [X, Y], feed={K.learning_phase(): 0}, args={'batch_size': batch_size} ) return X_adv
Example #13
Source File: util.py From lid_adversarial_subspace_detection with MIT License | 5 votes |
def get_mc_predictions(model, X, nb_iter=50, batch_size=256): """ TODO :param model: :param X: :param nb_iter: :param batch_size: :return: """ output_dim = model.layers[-1].output.shape[-1].value get_output = K.function( [model.layers[0].input, K.learning_phase()], [model.layers[-1].output] ) def predict(): n_batches = int(np.ceil(X.shape[0] / float(batch_size))) output = np.zeros(shape=(len(X), output_dim)) for i in range(n_batches): output[i * batch_size:(i + 1) * batch_size] = \ get_output([X[i * batch_size:(i + 1) * batch_size], 1])[0] return output preds_mc = [] for i in tqdm(range(nb_iter)): preds_mc.append(predict()) return np.asarray(preds_mc)
Example #14
Source File: query_methods.py From DiscriminativeActiveLearning with MIT License | 5 votes |
def dropout_predict(self, data): f = K.function([self.model.layers[0].input, K.learning_phase()], [self.model.layers[-1].output]) predictions = np.zeros((self.T, data.shape[0], self.num_labels)) for t in range(self.T): predictions[t,:,:] = f([data, 1])[0] final_prediction = np.mean(predictions, axis=0) prediction_uncertainty = np.std(predictions, axis=0) return final_prediction, prediction_uncertainty
Example #15
Source File: gestureCNN.py From CNNGestureRecognizer with MIT License | 5 votes |
def visualizeLayer(model, img, input_image, layerIndex): layer = model.layers[layerIndex] get_activations = K.function([model.layers[0].input, K.learning_phase()], [layer.output,]) activations = get_activations([input_image, 0])[0] output_image = activations ## If 4 dimensional then take the last dimension value as it would be no of filters if output_image.ndim == 4: # Rearrange dimension so we can plot the result #o1 = np.rollaxis(output_image, 3, 1) #output_image = np.rollaxis(o1, 3, 1) output_image = np.moveaxis(output_image, 1, 3) print("Dumping filter data of layer{} - {}".format(layerIndex,layer.__class__.__name__)) filters = len(output_image[0,0,0,:]) fig=plt.figure(figsize=(8,8)) # This loop will plot the 32 filter data for the input image for i in range(filters): ax = fig.add_subplot(6, 6, i+1) #ax.imshow(output_image[img,:,:,i],interpolation='none' ) #to see the first filter ax.imshow(output_image[0,:,:,i],'gray') #ax.set_title("Feature map of layer#{} \ncalled '{}' \nof type {} ".format(layerIndex, # layer.name,layer.__class__.__name__)) plt.xticks(np.array([])) plt.yticks(np.array([])) plt.tight_layout() #plt.show() savedfilename = "img_" + str(img) + "_layer" + str(layerIndex)+"_"+layer.__class__.__name__+".png" fig.savefig(savedfilename) print("Create file - {}".format(savedfilename)) #plt.close(fig) else: print("Can't dump data of this layer{}- {}".format(layerIndex, layer.__class__.__name__))
Example #16
Source File: Deconvnet-keras.py From Deconvnet-keras with MIT License | 5 votes |
def up(self, data, learning_phase = 0): ''' function to compute Convolution output in forward pass # Arguments data: Data to be operated in forward pass learning_phase: learning_phase of Keras, 1 or 0 # Returns Convolved result ''' self.up_data = self.up_func([data, learning_phase]) return self.up_data
Example #17
Source File: Deconvnet-keras.py From Deconvnet-keras with MIT License | 5 votes |
def down(self, data, learning_phase = 0): ''' function to compute Deconvolution output in backward pass # Arguments data: Data to be operated in backward pass learning_phase: learning_phase of Keras, 1 or 0 # Returns Deconvolved result ''' self.down_data= self.down_func([data, learning_phase]) return self.down_data
Example #18
Source File: Deconvnet-keras.py From Deconvnet-keras with MIT License | 5 votes |
def __init__(self, layer): ''' # Arguments layer: an instance of Dense layer, whose configuration will be used to initiate DDense(input_shape, output_shape, weights) ''' self.layer = layer weights = layer.get_weights() W = weights[0] b = weights[1] #Set up_func for DDense input = Input(shape = layer.input_shape[1:]) output = Dense(output_dim = layer.output_shape[1], weights = [W, b])(input) self.up_func = K.function([input, K.learning_phase()], output) #Transpose W and set down_func for DDense W = W.transpose() self.input_shape = layer.input_shape self.output_shape = layer.output_shape b = np.zeros(self.input_shape[1]) flipped_weights = [W, b] input = Input(shape = self.output_shape[1:]) output = Dense( output_dim = self.input_shape[1], weights = flipped_weights)(input) self.down_func = K.function([input, K.learning_phase()], output)
Example #19
Source File: util.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_mc_predictions(model, X, nb_iter=50, batch_size=256): """ TODO :param model: :param X: :param nb_iter: :param batch_size: :return: """ output_dim = model.layers[-1].output.shape[-1].value get_output = K.function( [model.layers[0].input, K.learning_phase()], [model.layers[-1].output] ) def predict(): n_batches = int(np.ceil(X.shape[0] / float(batch_size))) output = np.zeros(shape=(len(X), output_dim)) for i in range(n_batches): output[i * batch_size:(i + 1) * batch_size] = \ get_output([X[i * batch_size:(i + 1) * batch_size], 1])[0] return output preds_mc = [] for i in tqdm(range(nb_iter)): preds_mc.append(predict()) return np.asarray(preds_mc)
Example #20
Source File: util.py From keras-transfer-learning-for-oxford102 with MIT License | 5 votes |
def get_activation_function(m, layer): x = [m.layers[0].input, K.learning_phase()] y = [m.get_layer(layer).output] return K.function(x, y)
Example #21
Source File: get_dr_txt.py From yolo3-keras with MIT License | 5 votes |
def detect_image(self, image_id, image): f = open("./input/detection-results/"+image_id+".txt","w") # 调整图片使其符合输入要求 boxed_image = letterbox_image(image, self.model_image_size) image_data = np.array(boxed_image, dtype='float32') image_data /= 255. image_data = np.expand_dims(image_data, 0) # Add batch dimension. # 预测结果 out_boxes, out_scores, out_classes = self.sess.run( [self.boxes, self.scores, self.classes], feed_dict={ self.yolo_model.input: image_data, self.input_image_shape: [image.size[1], image.size[0]], K.learning_phase(): 0 }) for i, c in enumerate(out_classes): predicted_class = self.class_names[int(c)] score = str(out_scores[i]) top, left, bottom, right = out_boxes[i] f.write("%s %s %s %s %s %s\n" % (predicted_class, score[:6], str(int(left)), str(int(top)), str(int(right)),str(int(bottom)))) f.close() return
Example #22
Source File: grad_cam.py From Face-and-Emotion-Recognition with MIT License | 5 votes |
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
Example #23
Source File: rationale_CNN.py From robotreviewer with GNU General Public License v3.0 | 5 votes |
def set_final_sentence_model(self): ''' allow convenient access to sentence-level predictions, after training ''' sent_prob_outputs = self.doc_model.get_layer("sentence_predictions") sent_model = K.function(inputs=self.doc_model.inputs + [K.learning_phase()], outputs=[sent_prob_outputs.output]) self.sentence_prob_model = sent_model
Example #24
Source File: grad_cam.py From Face-and-Emotion-Recognition with MIT License | 5 votes |
def compile_saliency_function(model, activation_layer='conv2d_7'): input_image = model.input layer_output = model.get_layer(activation_layer).output max_output = K.max(layer_output, axis=3) saliency = K.gradients(K.sum(max_output), input_image)[0] return K.function([input_image, K.learning_phase()], [saliency])
Example #25
Source File: features.py From detection-2016-nipsws with MIT License | 5 votes |
def get_feature_map_8(model, im): im = im.astype(np.float32) dim_ordering = K.image_dim_ordering() if dim_ordering == 'th': # 'RGB'->'BGR' im = im[::-1, :, :] # Zero-center by mean pixel im[0, :, :] -= 103.939 im[1, :, :] -= 116.779 im[2, :, :] -= 123.68 else: # 'RGB'->'BGR' im = im[:, :, ::-1] # Zero-center by mean pixel im[:, :, 0] -= 103.939 im[:, :, 1] -= 116.779 im[:, :, 2] -= 123.68 im = im.transpose((2, 0, 1)) im = np.expand_dims(im, axis=0) inputs = [K.learning_phase()] + model.inputs _convout1_f = K.function(inputs, model.outputs) feature_map = _convout1_f([0] + [im]) feature_map = np.array([feature_map]) feature_map = feature_map[0, 0, 0, :, :, :] return feature_map # get shallower feature map
Example #26
Source File: pico_viz_robot.py From robotreviewer with GNU General Public License v3.0 | 5 votes |
def load_models(self): def _load_embedding_model(arch_path, weight_path): json_str = open(arch_path).read() model = model_from_json(json_str) model.load_weights(weight_path) inputs = [model.inputs[0], K.learning_phase()] # to trace back from embeddings to activations on n-grams # we provide intermediate output from conv filters here. outputs = [model.get_layer('convolution1d_1').output, model.get_layer('convolution1d_2').output, model.get_layer('convolution1d_3').output, model.get_layer('study').output] return K.function(inputs, outputs) self.population_embedding_model = _load_embedding_model(population_arch_path, population_weight_path) self.PCA_dict["population"] = pickle.load(open(population_PCA_path, 'rb')) self.intervention_embedding_model = _load_embedding_model(intervention_arch_path, intervention_weight_path) self.PCA_dict["intervention"] = pickle.load(open(intervention_PCA_path, 'rb')) self.outcomes_embedding_model = _load_embedding_model(outcomes_arch_path, outcomes_weight_path) self.PCA_dict["outcomes"] = pickle.load(open(outcomes_PCA_path, 'rb')) f = open(vectorizer_path, 'rb') self.vectorizer = pickle.load(f, encoding="latin")
Example #27
Source File: kerascallback.py From delve with MIT License | 5 votes |
def save_intermediate_outputs(dense_outputs, obj): """Save outputs to obj.""" for tensor in dense_outputs: layer_name = tensor.name.split('/')[0] # Route intermediate output, aka. preactivation state func = K.function([obj.model.input] + [K.learning_phase()], [tensor]) intermediate_output = func([obj.input_data, 0.])[0] # batch_nr x width obj.preactivation_states[layer_name].append(intermediate_output)
Example #28
Source File: visualization.py From Aesthetic_attributes_maps with MIT License | 5 votes |
def get_features(image, model): ''' get the feature map of all activation layer for given image and given model :param image: input image path :param model: given model :return: all activation layers features ''' # image = load_image(image_src) feature_maps = np.zeros((10, 10, 15104)) activation_layers = ['activation_' + str(i) for i in range(4, 50, 3)] start_index = 0 for i, layer_name in enumerate(activation_layers): layer = model.get_layer(layer_name) nchannel = layer.output_shape[-1] conv_output = layer.output # Adujusting pooling size with respect to input layers` size if layer.output_shape[-2] == 74: conv_output = AveragePooling2D(pool_size=(7, 7))(conv_output) if layer.output_shape[-2] == 37: conv_output = AveragePooling2D(pool_size=(4, 4), border_mode='same')(conv_output) if layer.output_shape[-2] == 19: conv_output = AveragePooling2D(pool_size=(2, 2), border_mode='same')(conv_output) featuremap_function = K.function([model.input, K.learning_phase()], [conv_output]) output = featuremap_function([image, 0]) feature_maps[:, :, start_index:start_index+nchannel] = output[0][0, :, :, :] start_index = start_index + nchannel return feature_maps
Example #29
Source File: callbacks.py From keras-fcn with MIT License | 5 votes |
def on_batch_end(self, batch, logs=None): if self.validation_data and self.histogram_freq: if batch % self.histogram_freq == 0: for layer in self.model.layers: functor = K.function([self.model.input, K.learning_phase()], [layer.output]) layer_out = functor(self.validation_data) if np.any(np.isnan(layer_out)) or np.any(np.isinf(layer_out)): print('The output of {} becomes nan'.format(layer.name)) self.model.stop_training = True
Example #30
Source File: weightnorm.py From weightnorm with MIT License | 5 votes |
def data_based_init(model, input): # input can be dict, numpy array, or list of numpy arrays if type(input) is dict: feed_dict = input elif type(input) is list: feed_dict = {tf_inp: np_inp for tf_inp,np_inp in zip(model.inputs,input)} else: feed_dict = {model.inputs[0]: input} # add learning phase if required if model.uses_learning_phase and K.learning_phase() not in feed_dict: feed_dict.update({K.learning_phase(): 1}) # get all layer name, output, weight, bias tuples layer_output_weight_bias = [] for l in model.layers: if hasattr(l, 'W') and hasattr(l, 'b'): assert(l.built) layer_output_weight_bias.append( (l.name,l.get_output_at(0),l.W,l.b) ) # if more than one node, only use the first # iterate over our list and do data dependent init sess = K.get_session() for l,o,W,b in layer_output_weight_bias: print('Performing data dependent initialization for layer ' + l) m,v = tf.nn.moments(o, [i for i in range(len(o.get_shape())-1)]) s = tf.sqrt(v + 1e-10) updates = tf.group(W.assign(W/tf.reshape(s,[1]*(len(W.get_shape())-1)+[-1])), b.assign((b-m)/s)) sess.run(updates, feed_dict)