Python keras.backend.set_learning_phase() Examples
The following are 30
code examples of keras.backend.set_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: h5topb.py From panotti with MIT License | 7 votes |
def export_h5_to_pb(path_to_h5, export_path): # Set the learning phase to Test since the model is already trained. K.set_learning_phase(0) # Load the Keras model keras_model = load_model(path_to_h5) # Build the Protocol Buffer SavedModel at 'export_path' builder = saved_model_builder.SavedModelBuilder(export_path) # Create prediction signature to be used by TensorFlow Serving Predict API signature = predict_signature_def(inputs={"images": keras_model.input}, outputs={"scores": keras_model.output}) with K.get_session() as sess: # Save the meta graph and the variables builder.add_meta_graph_and_variables(sess=sess, tags=[tag_constants.SERVING], signature_def_map={"predict": signature}) builder.save()
Example #2
Source File: test_transformer.py From BERT with Apache License 2.0 | 6 votes |
def test_save_load_weights(self): for backend in self.list_backends(): try: set_keras_backend(backend) except ModuleNotFoundError: continue K.set_learning_phase(0) # test for use_attn_mask in [True, False]: model = self.create_small_model(use_attn_mask) path = '/tmp/{}.model'.format(uuid.uuid4()) try: model.save_weights(path) model.load_weights(path) except Exception as e: raise e finally: if os.path.exists(path): os.remove(path)
Example #3
Source File: utils.py From KerasDeepSpeech with GNU Affero General Public License v3.0 | 6 votes |
def load_cmodel_checkpoint(path, summary=True): #this is a terrible hack from keras.utils.generic_utils import get_custom_objects # get_custom_objects().update({"tf": tf}) get_custom_objects().update({"clipped_relu": clipped_relu}) get_custom_objects().update({"selu": selu}) # get_custom_objects().update({"TF_NewStatus": None}) cfilename = path+".h5" K.set_learning_phase(1) loaded_model = load_model(cfilename) if(summary): loaded_model.summary() return loaded_model
Example #4
Source File: test_transformer.py From BERT-keras with GNU General Public License v3.0 | 6 votes |
def test_save_load_weights(self): for backend in self.list_backends(): try: set_keras_backend(backend) except ModuleNotFoundError: continue K.set_learning_phase(0) # test for use_attn_mask in [True, False]: model = self.create_small_model(use_attn_mask) path = '/tmp/{}.model'.format(uuid.uuid4()) try: model.save_weights(path) model.load_weights(path) except Exception as e: raise e finally: if os.path.exists(path): os.remove(path)
Example #5
Source File: test_transformer.py From BERT-keras with GNU General Public License v3.0 | 6 votes |
def test_save_load_all(self): for backend in self.list_backends(): try: set_keras_backend(backend) except ModuleNotFoundError: continue K.set_learning_phase(0) # test for use_attn_mask in [True, False]: model = self.create_small_model(use_attn_mask) path = '/tmp/{}.model'.format(uuid.uuid4()) try: model.save(path) new_model = keras.models.load_model(path, custom_objects={'MultiHeadAttention': MultiHeadAttention, 'LayerNormalization': LayerNormalization, 'Gelu': Gelu}) TestTransformer.compare_two_models(model, new_model) except Exception as e: raise e finally: if os.path.exists(path): os.remove(path)
Example #6
Source File: test_switchnorm.py From keras-switchnorm with MIT License | 6 votes |
def test_switchnorm_trainable(): bn_mean = 0.5 bn_std = 10. def get_model(bn_mean, bn_std): input = Input(shape=(1,)) x = SwitchNormalization()(input) model = Model(input, x) model.set_weights([np.array([1.]), np.array([0.]), np.array([-1e3, -1e3, 1.0]), np.array([-1e3, -1e3, 1.0]), np.array([bn_mean]), np.array([bn_std ** 2])]) return model # Simulates training-mode with trainable layer. Should use mini-switch statistics. K.set_learning_phase(1) model = get_model(bn_mean, bn_std) model.compile(loss='mse', optimizer='rmsprop') out = model.predict(input_4) assert_allclose((input_4 - np.mean(input_4)) / np.std(input_4), out, atol=1e-3)
Example #7
Source File: yolov3.py From keras-onnx with MIT License | 6 votes |
def __init__(self, model_path='model_data/yolo.h5', anchors_path='model_data/yolo_anchors.txt', yolo3_dir=None): self.yolo3_dir = yolo3_dir self.model_path = model_path self.anchors_path = anchors_path self.classes_path = 'model_data/coco_classes.txt' self.score = 0.3 self.iou = 0.45 self.class_names = self._get_class() self.anchors = self._get_anchors() self.sess = K.get_session() self.model_image_size = (416, 416) # fixed size or (None, None), hw self.session = None self.final_model = None # Generate colors for drawing bounding boxes. hsv_tuples = [(x / len(self.class_names), 1., 1.) for x in range(len(self.class_names))] self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) self.colors = list( map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors)) np.random.seed(10101) # Fixed seed for consistent colors across runs. np.random.shuffle(self.colors) # Shuffle colors to decorrelate adjacent classes. np.random.seed(None) # Reset seed to default. K.set_learning_phase(0)
Example #8
Source File: test_transformer.py From BERT with Apache License 2.0 | 6 votes |
def test_save_load_all(self): for backend in self.list_backends(): try: set_keras_backend(backend) except ModuleNotFoundError: continue K.set_learning_phase(0) # test for use_attn_mask in [True, False]: model = self.create_small_model(use_attn_mask) path = '/tmp/{}.model'.format(uuid.uuid4()) try: model.save(path) new_model = keras.models.load_model(path, custom_objects={'MultiHeadAttention': MultiHeadAttention, 'LayerNormalization': LayerNormalization, 'Gelu': Gelu}) TestTransformer.compare_two_models(model, new_model) except Exception as e: raise e finally: if os.path.exists(path): os.remove(path)
Example #9
Source File: h5tf.py From models with Apache License 2.0 | 6 votes |
def export_h5_to_pb(path_to_h5, export_path): # Set the learning phase to Test since the model is already trained. K.set_learning_phase(0) # Load the Keras model keras_model = load_model(path_to_h5) # Build the Protocol Buffer SavedModel at 'export_path' builder = saved_model_builder.SavedModelBuilder(export_path) # Create prediction signature to be used by TensorFlow Serving Predict API signature = predict_signature_def(inputs={ "http": keras_model.input}, outputs={"probability": keras_model.output}) with K.get_session() as sess: # Save the meta graph and the variables builder.add_meta_graph_and_variables(sess=sess, tags=[tag_constants.SERVING], signature_def_map={"predict": signature}) builder.save()
Example #10
Source File: image_utils.py From spark-deep-learning with Apache License 2.0 | 6 votes |
def executeKerasInceptionV3(image_df, uri_col="filePath"): """ Apply Keras InceptionV3 Model on input DataFrame. :param image_df: Dataset. contains a column (uri_col) for where the image file lives. :param uri_col: str. name of the column indicating where each row's image file lives. :return: ({str => np.array[float]}, {str => (str, str, float)}). image file uri to prediction probability array, image file uri to top K predictions (class id, class description, probability). """ K.set_learning_phase(0) model = InceptionV3(weights="imagenet") values = {} topK = {} for row in image_df.select(uri_col).collect(): raw_uri = row[uri_col] image = loadAndPreprocessKerasInceptionV3(raw_uri) values[raw_uri] = model.predict(image) topK[raw_uri] = decode_predictions(values[raw_uri], top=5)[0] return values, topK
Example #11
Source File: test_tf_keras_layers.py From tf-coreml with Apache License 2.0 | 5 votes |
def setUpClass(self): """ Set up the unit test by loading common utilities. """ K.set_learning_phase(0)
Example #12
Source File: punctuator.py From keras-punctuator with MIT License | 5 votes |
def writeTensorflowDashboardLog(): from tensorflow.python.framework import ops with ops.Graph().as_default(): from keras import backend as K K.set_learning_phase(0) model = createModel() model.load_weights(KERAS_WEIGHTS_FILE) sess = K.get_session() testGraph(sess, '') import tensorflow as tf writer = tf.summary.FileWriter("tmp", graph=sess.graph) writer.flush() writer.close()
Example #13
Source File: TYY_demo_centerface_bbox_age_gender.py From MaskInsightface with Apache License 2.0 | 5 votes |
def model_init(args): K.set_learning_phase(0) # make sure its testing mode # load model and weights stage_num = [3, 3, 3] lambda_local = 1 lambda_d = 1 model_age = SSR_net(args.image_size, stage_num, lambda_local, lambda_d)() model_age.load_weights(args.age_model) model_gender = SSR_net_general(args.image_size, stage_num, lambda_local, lambda_d)() model_gender.load_weights(args.gender_model) return model_gender, model_age
Example #14
Source File: cats_and_dogs.py From uncertainty-adversarial-paper with MIT License | 5 votes |
def define_model_resnet(): K.set_learning_phase(True) rn50 = ResNet50(weights='imagenet', include_top='False') a = Dropout(rate=0.5)(rn50.output) a = Dense(2, activation='softmax')(a) model = keras.models.Model(inputs=rn50.input, outputs=a) # freeze resnet layers for layer in rn50.layers: layer.trainable = False return model
Example #15
Source File: punctuator.py From keras-punctuator with MIT License | 5 votes |
def saveWithSavedModel(): # K.set_learning_phase(0) # all new operations will be in test mode from now on # wordIndex = loadWordIndex() model = createModel() model.load_weights(KERAS_WEIGHTS_FILE) export_path = os.path.join(PUNCTUATOR_DIR, 'graph') # where to save the exported graph shutil.rmtree(export_path, True) export_version = 1 # version number (integer) import tensorflow as tf sess = tf.Session() saver = tf.train.Saver(sharded=True) from tensorflow.contrib.session_bundle import exporter model_exporter = exporter.Exporter(saver) signature = exporter.classification_signature(input_tensor=model.input,scores_tensor=model.output) # model_exporter.init(sess.graph.as_graph_def(),default_graph_signature=signature) tf.initialize_all_variables().run(session=sess) # model_exporter.export(export_path, tf.constant(export_version), sess) from tensorflow.python.saved_model import builder as saved_model_builder builder = saved_model_builder.SavedModelBuilder(export_path) from tensorflow.python.saved_model import signature_constants from tensorflow.python.saved_model import tag_constants legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op') from tensorflow.python.saved_model.signature_def_utils_impl import predict_signature_def signature_def = predict_signature_def( {signature_constants.PREDICT_INPUTS: model.input}, {signature_constants.PREDICT_OUTPUTS: model.output}) builder.add_meta_graph_and_variables( sess, [tag_constants.SERVING], signature_def_map={ signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature_def }, legacy_init_op=legacy_init_op) builder.save()
Example #16
Source File: keras2graph.py From Intel-Movidius-NCS-Keras with Apache License 2.0 | 5 votes |
def keras_to_tf(tf_model_path): saver = tf.train.Saver() with K.get_session() as sess: K.set_learning_phase(0) saver.save(sess, tf_model_path) return True
Example #17
Source File: latent_plots.py From uncertainty-adversarial-paper with MIT License | 5 votes |
def get_models(): _, encoder, decoder = define_VAE() encoder.load_weights('save/enc_weights.h5') decoder.load_weights('save/dec_weights.h5') K.set_learning_phase(True) model = keras.models.load_model('save/mnist_cnn_run_1.h5') mc_model = U.MCModel(model, model.input, n_mc=50) #we have been using more mc samples elsewhere, but save time for now return mc_model, encoder, decoder
Example #18
Source File: latent_plots.py From uncertainty-adversarial-paper with MIT License | 5 votes |
def get_ML_ensemble(): _, encoder, decoder = define_VAE() encoder.load_weights('save/enc_weights.h5') decoder.load_weights('save/dec_weights.h5') K.set_learning_phase(False) ms = [] for name in filter(lambda x: 'mnist_cnn' in x, os.listdir('save')): print('loading model {}'.format(name)) model = load_model('save/' + name) ms.append(model) model = U.MCEnsembleWrapper(ms, n_mc=1) return model, encoder, decoder
Example #19
Source File: Dronet.py From rpg_public_dronet with MIT License | 5 votes |
def __init__(self, json_model_path, weights_path, target_size=(200, 200), crop_size=(150, 150), imgs_rootpath="../models"): self.pub = rospy.Publisher("cnn_predictions", CNN_out, queue_size=5) self.feedthrough_sub = rospy.Subscriber("state_change", Bool, self.callback_feedthrough, queue_size=1) self.land_sub = rospy.Subscriber("land", Empty, self.callback_land, queue_size=1) self.use_network_out = False self.imgs_rootpath = imgs_rootpath # Set keras utils K.set_learning_phase(TEST_PHASE) # Load json and create model model = utils.jsonToModel(json_model_path) # Load weights model.load_weights(weights_path) print("Loaded model from {}".format(weights_path)) model.compile(loss='mse', optimizer='sgd') self.model = model self.target_size = target_size self.crop_size = crop_size
Example #20
Source File: ROC_curves_cats.py From uncertainty-adversarial-paper with MIT License | 5 votes |
def load_model(deterministic=False, name='save/cats_dogs_rn50_w_run.h5'): lp = not deterministic K.set_learning_phase(lp) model = define_model_resnet() model.load_weights(name) model.compile(loss='categorical_crossentropy', optimizer='sgd') return model
Example #21
Source File: generate_app_models.py From spark-deep-learning with Apache License 2.0 | 5 votes |
def gen_model(name, license, model, model_file, version=VERSION, featurize=True): g = tf.Graph() with tf.Session(graph=g) as session: K.set_learning_phase(0) inTensor = tf.placeholder(dtype=tf.string, shape=[], name="%s_input" % name) decoded = tf.decode_raw(inTensor, tf.uint8) imageTensor = tf.to_float( tf.reshape( decoded, shape=[ 1, model.inputShape()[0], model.inputShape()[1], 3])) m = model.model(preprocessed=model.preprocess(imageTensor), featurize=featurize) outTensor = tf.to_double(tf.reshape(m.output, [-1]), name="%s_sparkdl_output__" % name) gdef = tfx.strip_and_freeze_until([outTensor], session.graph, session, False) g2 = tf.Graph() with tf.Session(graph=g2) as session: tf.import_graph_def(gdef, name='') filename = "sparkdl-%s_%s.pb" % (name, version) print('writing out ', filename) tf.train.write_graph(g2.as_graph_def(), logdir="./", name=filename, as_text=False) with open("./" + filename, "r") as f: h = sha256(f.read()).digest() base64_hash = b64encode(h) print('h', base64_hash) model_file.write(indent( scala_template % { "license": license, "name": name, "height": model.inputShape()[0], "width": model.inputShape()[1], "filename": filename, "base64": base64_hash},2)) return g2
Example #22
Source File: run_bottleneck.py From CarND-Transfer-Learning-Lab with MIT License | 5 votes |
def main(_): if FLAGS.dataset == 'cifar10': (X_train, y_train), (_, _) = cifar10.load_data() X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=0) else: with open('data/train.p', mode='rb') as f: train = pickle.load(f) X_train, X_val, y_train, y_val = train_test_split(train['features'], train['labels'], test_size=0.33, random_state=0) train_output_file = "{}_{}_{}.p".format(FLAGS.network, FLAGS.dataset, 'bottleneck_features_train') validation_output_file = "{}_{}_{}.p".format(FLAGS.network, FLAGS.dataset, 'bottleneck_features_validation') print("Resizing to", (w, h, ch)) print("Saving to ...") print(train_output_file) print(validation_output_file) with tf.Session() as sess: K.set_session(sess) K.set_learning_phase(1) model = create_model() print('Bottleneck training') train_gen = gen(sess, X_train, y_train, batch_size) bottleneck_features_train = model.predict_generator(train_gen(), X_train.shape[0]) data = {'features': bottleneck_features_train, 'labels': y_train} pickle.dump(data, open(train_output_file, 'wb')) print('Bottleneck validation') val_gen = gen(sess, X_val, y_val, batch_size) bottleneck_features_validation = model.predict_generator(val_gen(), X_val.shape[0]) data = {'features': bottleneck_features_validation, 'labels': y_val} pickle.dump(data, open(validation_output_file, 'wb'))
Example #23
Source File: model.py From KerasDeepSpeech with GNU Affero General Public License v3.0 | 5 votes |
def build_ds5_no_ctc_and_xfer_weights(loaded_model, input_dim=161, fc_size=1024, rnn_size=512, output_dim=29, initialization='glorot_uniform', conv_layers=4): """ Pure CNN implementation""" K.set_learning_phase(0) for ind, i in enumerate(loaded_model.layers): print(ind, i) kernel_size = 11 # conv_depth_1 = 64 # conv_depth_2 = 256 # input_data = Input(shape=(None, input_dim), name='the_input') #batch x time x spectro size conv = ZeroPadding1D(padding=(0, 2048))(input_data) #pad on time dimension x = Conv1D(filters=128, name='conv_1', kernel_size=kernel_size, padding='valid', activation='relu', strides=2, weights = loaded_model.layers[2].get_weights())(conv) # x = Conv1D(filters=1024, name='conv_2', kernel_size=kernel_size, padding='valid', activation='relu', strides=2, # weights=loaded_model.layers[3].get_weights())(x) # Last Layer 5+6 Time Dist Dense Layer & Softmax x = TimeDistributed(Dense(fc_size, activation='relu', weights=loaded_model.layers[3].get_weights()))(x) y_pred = TimeDistributed(Dense(output_dim, name="y_pred", activation="softmax"))(x) model = Model(inputs=input_data, outputs=y_pred) return model
Example #24
Source File: model.py From KerasDeepSpeech with GNU Affero General Public License v3.0 | 5 votes |
def graves(input_dim=26, rnn_size=512, output_dim=29, std=0.6): """ Implementation of Graves 2006 model Architecture: Gaussian Noise on input BiDirectional LSTM Reference: ftp://ftp.idsia.ch/pub/juergen/icml2006.pdf """ K.set_learning_phase(1) input_data = Input(name='the_input', shape=(None, input_dim)) # x = BatchNormalization(axis=-1)(input_data) x = GaussianNoise(std)(input_data) x = Bidirectional(LSTM(rnn_size, return_sequences=True, implementation=0))(x) y_pred = TimeDistributed(Dense(output_dim, activation='softmax'))(x) # Input of labels and other CTC requirements labels = Input(name='the_labels', shape=[None,], dtype='int32') input_length = Input(name='input_length', shape=[1], dtype='int32') label_length = Input(name='label_length', shape=[1], dtype='int32') # Keras doesn't currently support loss funcs with extra parameters # so CTC loss is implemented in a lambda layer loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred, labels, input_length, label_length]) model = Model(inputs=[input_data, labels, input_length, label_length], outputs=[loss_out]) return model
Example #25
Source File: tf_image_test.py From spark-deep-learning with Apache License 2.0 | 5 votes |
def test_prediction_vs_tensorflow_inceptionV3(self): output_col = "prediction" image_df = image_utils.getSampleImageDF() # An example of how a pre-trained keras model can be used with TFImageTransformer with KSessionWrap() as (sess, g): with g.as_default(): K.set_learning_phase(0) # this is important but it's on the user to call it. # nChannels needed for input_tensor in the InceptionV3 call below image_string = utils.imageInputPlaceholder(nChannels=3) resized_images = tf.image.resize_images(image_string, InceptionV3Constants.INPUT_SHAPE) # keras expects array in RGB order, we get it from image schema in BGR => # need to flip preprocessed = preprocess_input(imageIO._reverseChannels(resized_images)) model = InceptionV3(input_tensor=preprocessed, weights="imagenet") graph = tfx.strip_and_freeze_until([model.output], g, sess, return_graph=True) transformer = TFImageTransformer(channelOrder='BGR', inputCol="image", outputCol=output_col, graph=graph, inputTensor=image_string, outputTensor=model.output, outputMode="vector") transformed_df = transformer.transform(image_df.limit(10)) self.assertDfHasCols(transformed_df, [output_col]) collected = transformed_df.collect() transformer_values, transformer_topK = self.transformOutputToComparables(collected, output_col, lambda row: row['image']['origin']) tf_values, tf_topK = self._executeTensorflow(graph, image_string.name, model.output.name, image_df) self.compareClassSets(tf_topK, transformer_topK) self.compareClassOrderings(tf_topK, transformer_topK) self.compareArrays(tf_values, transformer_values, decimal=5)
Example #26
Source File: test_builder.py From spark-deep-learning with Apache License 2.0 | 5 votes |
def test_keras_consistency(self): """ Exported model in Keras should get same result as original """ img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg')) def keras_load_and_preproc(fpath): img = load_img(fpath, target_size=(299, 299)) img_arr = img_to_array(img) img_iv3_input = iv3.preprocess_input(img_arr) return np.expand_dims(img_iv3_input, axis=0) imgs_iv3_input = np.vstack([keras_load_and_preproc(fp) for fp in img_fpaths]) model_ref = InceptionV3(weights="imagenet") preds_ref = model_ref.predict(imgs_iv3_input) with IsolatedSession(using_keras=True) as issn: K.set_learning_phase(0) model = InceptionV3(weights="imagenet") gfn = issn.asGraphFunction(model.inputs, model.outputs) with IsolatedSession(using_keras=True) as issn: K.set_learning_phase(0) feeds, fetches = issn.importGraphFunction(gfn, prefix="InceptionV3") preds_tgt = issn.run(fetches[0], {feeds[0]: imgs_iv3_input}) np.testing.assert_array_almost_equal(preds_tgt, preds_ref, decimal=5)
Example #27
Source File: test_transformer.py From BERT-keras with GNU General Public License v3.0 | 5 votes |
def test_different_backends_work(self): for use_attn_mask in [True, False]: orig_backend = K.backend() for backend in self.list_backends(orig_backend): try: set_keras_backend(backend) except ModuleNotFoundError: pass K.set_learning_phase(0) # test model = self.create_small_model(use_attn_mask) del model set_keras_backend(orig_backend)
Example #28
Source File: test_transformer.py From BERT-keras with GNU General Public License v3.0 | 5 votes |
def test_same_result(self): orig_backend = K.backend() batch_size = 3 xmb = np.random.randint(0, self.vocab_size, (batch_size, self.max_len, 2), dtype=np.int32) xmb[:, :, 1] = np.random.randint(0, self.max_len, (batch_size, self.max_len), dtype=np.int32) for use_attn_mask in [True, False]: inputs = [xmb[:, :, 0], np.zeros((batch_size, self.max_len), dtype=np.int32), xmb[:, :, 1]] results_x = {} if use_attn_mask: mask = create_attention_mask(None, True, batch_size, self.max_len) inputs.append(mask) for backend in self.list_backends(orig_backend): try: set_keras_backend(backend) except ModuleNotFoundError: continue K.set_learning_phase(0) # test model = self.create_small_model(use_attn_mask) model = load_openai_transformer(use_attn_mask=use_attn_mask, max_len=self.max_len, use_one_embedding_dropout=True) results_x[backend] = model.predict(inputs, batch_size=batch_size) del model set_keras_backend(orig_backend) for k1 in results_x.keys(): for k2 in results_x.keys(): if k1 == k2: continue assert np.allclose(results_x[k1], results_x[k2], atol=1.e-4, rtol=1.e-4)
Example #29
Source File: test_pieces.py From spark-deep-learning with Apache License 2.0 | 5 votes |
def test_bare_keras_module(self): """ Keras GraphFunctions should give the same result as standard Keras models """ img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg')) for model_gen, preproc_fn, target_size in [(InceptionV3, iv3.preprocess_input, model_sizes['InceptionV3']), (Xception, xcpt.preprocess_input, model_sizes['Xception']), (ResNet50, rsnt.preprocess_input, model_sizes['ResNet50'])]: keras_model = model_gen(weights="imagenet") _preproc_img_list = [] for fpath in img_fpaths: img = load_img(fpath, target_size=target_size) # WARNING: must apply expand dimensions first, or ResNet50 preprocessor fails img_arr = np.expand_dims(img_to_array(img), axis=0) _preproc_img_list.append(preproc_fn(img_arr)) imgs_input = np.vstack(_preproc_img_list) preds_ref = keras_model.predict(imgs_input) gfn_bare_keras = GraphFunction.fromKeras(keras_model) with IsolatedSession(using_keras=True) as issn: K.set_learning_phase(0) feeds, fetches = issn.importGraphFunction(gfn_bare_keras) preds_tgt = issn.run(fetches[0], {feeds[0]: imgs_input}) np.testing.assert_array_almost_equal(preds_tgt, preds_ref, decimal=self.featurizerCompareDigitsExact)
Example #30
Source File: shared_params.py From spark-deep-learning with Apache License 2.0 | 5 votes |
def _loadTFGraph(self, sess, graph): """ Loads the Keras model into memory, then uses the passed-in session to load the model's inference-related ops into the passed-in Tensorflow graph. :return: A tuple (graph, input_name, output_name) where graph is the TF graph corresponding to the Keras model's inference subgraph, input_name is the name of the Keras model's input tensor, and output_name is the name of the Keras model's output tensor. """ keras_backend = K.backend() assert keras_backend == "tensorflow", \ "Only tensorflow-backed Keras models are supported, tried to load Keras model " \ "with backend %s." % (keras_backend) with graph.as_default(): K.set_learning_phase(0) # Inference phase model = load_model(self.getModelFile()) out_op_name = tfx.op_name(model.output, graph) stripped_graph = tfx.strip_and_freeze_until([out_op_name], graph, sess, return_graph=True) return stripped_graph, model.input.name, model.output.name