Python tensorflow.keras.models.load_model() Examples
The following are 30
code examples of tensorflow.keras.models.load_model().
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
tensorflow.keras.models
, or try the search function
.
Example #1
Source File: rerank_terms.py From nlp-architect with Apache License 2.0 | 6 votes |
def predict(self, input_table_file, generic_opinion_terms): """Predict classification class according to model. Args: input_table_file: feature(X) and labels(Y) table file generic_opinion_terms: generic opinion terms file name Returns: final_concat_opinion_lex: reranked_lex conctenated with generic lex """ x, terms, polarities = self.load_terms_and_generate_features(input_table_file) model = load_model(self.rerank_model_path) reranked_lexicon = model.predict(x, verbose=0) reranked_lex = {} for i, prediction in enumerate(reranked_lexicon): if not np.isnan(prediction[0]) and prediction[0] > self.PREDICTION_THRESHOLD: reranked_lex[terms[i]] = (prediction[0], polarities[terms[i]]) final_concat_opinion_lex = self._generate_concat_reranked_lex( reranked_lex, generic_opinion_terms ) return final_concat_opinion_lex
Example #2
Source File: keras_to_onnx.py From keras-YOLOv3-model-set with MIT License | 6 votes |
def onnx_convert_with_savedmodel(keras_model_file, output_file, op_set): # only available for TF 2.x if not tf.__version__.startswith('2'): raise ValueError('savedmodel convert only support in TF 2.x env') custom_object_dict = get_custom_objects() model = load_model(keras_model_file, custom_objects=custom_object_dict) # export to saved model model.save('tmp_savedmodel', save_format='tf') # use tf2onnx to convert to onnx model cmd = 'python -m tf2onnx.convert --saved-model tmp_savedmodel --output {} --opset {}'.format(output_file, op_set) process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) output, error = process.communicate() # clean saved model shutil.rmtree('tmp_savedmodel')
Example #3
Source File: ReadAnalogNeedleClass.py From water-meter-system-complete with MIT License | 6 votes |
def __init__(self): config = configparser.ConfigParser() config.read('./config/config.ini') self.log_Image = '' self.LogNames = '' if config.has_option('Analog_Counter', 'LogImageLocation'): self.log_Image = config['Analog_Counter']['LogImageLocation'] if config.has_option('Analog_Counter', 'LogNames'): zw_LogNames = config.get('Analog_Counter', 'LogNames').split(',') self.LogNames = [] for nm in zw_LogNames: self.LogNames.append(nm.strip()) else: self.LogNames = '' else: self.log_Image = '' self.model_file = config['Analog_Counter']['Modelfile'] self.CheckAndLoadDefaultConfig() self.model = load_model(self.model_file)
Example #4
Source File: validate_yolo.py From keras-YOLOv3-model-set with MIT License | 6 votes |
def validate_yolo_model(model_path, image_file, anchors, class_names, model_image_size, loop_count): custom_object_dict = get_custom_objects() model = load_model(model_path, compile=False, custom_objects=custom_object_dict) img = Image.open(image_file) image = np.array(img, dtype='uint8') image_data = preprocess_image(img, model_image_size) #origin image shape, in (height, width) format image_shape = tuple(reversed(img.size)) # predict once first to bypass the model building time model.predict([image_data]) start = time.time() for i in range(loop_count): prediction = model.predict([image_data]) end = time.time() print("Average Inference time: {:.8f}ms".format((end - start) * 1000 /loop_count)) if type(prediction) is not list: prediction = [prediction] prediction.sort(key=lambda x: len(x[0])) handle_prediction(prediction, image_file, image, image_shape, anchors, class_names, model_image_size) return
Example #5
Source File: helper.py From stacks-usecase with Apache License 2.0 | 6 votes |
def load_unet(checkpoint=False): """ Return unet model. Only for use in local training. checkpoint: True to return the latest ckeckpoint in models/, False to return a model named 'unet.h5' in models/ """ home_dir = get_directory() unet_names = glob.glob(os.path.join(home_dir, "models/checkpoints/unet-*.h5")) if checkpoint: try: unet_file = max(unet_names, key=os.path.getctime) unet_model = load_model(unet_file) except ValueError: print("Could not load checkpoint. Returning new model instead.") unet_model = unet_model() else: try: unet_model = load_model(os.path.join(home_dir, "models/unet.h5")) except ValueError: print("Could not load from 'models/unet.h5'. Returning new model instead.") unet_model = unet_model() return unet_model
Example #6
Source File: base.py From megnet with BSD 3-Clause "New" or "Revised" License | 6 votes |
def from_file(cls, filename: str) -> 'GraphModel': """ Class method to load model from filename for keras model filename.json for additional converters Args: filename: (str) model file name Returns GraphModel """ configs = loadfn(filename + '.json') from tensorflow.keras.models import load_model from megnet.layers import _CUSTOM_OBJECTS model = load_model(filename, custom_objects=_CUSTOM_OBJECTS) configs.update({'model': model}) return GraphModel(**configs)
Example #7
Source File: infer.py From stacks-usecase with Apache License 2.0 | 6 votes |
def infer(img): """inference function, accepts an abstract image file return generated image""" home_dir = get_directory() # load model backend.clear_session() gen_model = load_model(home_dir + "/models/generator_model.h5", compile=False) img = np.array(Image.open(img)) img = norm_data([img]) s_time = time.time() result = gen_model.predict(img[0].reshape(1, 256, 256, 3)) f_time = time.time() logger.info( "\033[92m" + "[INFO] " + "\033[0m" + "Inference done in: {:2.3f} seconds".format(f_time - s_time) ) # transform result from normalized to absolute values and convert to image object result = Image.fromarray(reverse_norm(result[0]), "RGB") # for debugging, uncomment the line below to inspect the generated image locally # result.save("generted_img.jpg", "JPEG") # convert image to bytes object to send it to the client binary_buffer = io.BytesIO() result.save(binary_buffer, format="JPEG") return b2a_base64(binary_buffer.getvalue())
Example #8
Source File: __init__.py From platypush with MIT License | 6 votes |
def _load_model(self, name: str, reload: bool = False) -> Model: if name in self.models and not reload: return self.models[name] model_dir = os.path.join(self._models_dir, name) assert os.path.isdir(model_dir), 'The model {} does not exist'.format(name) model = load_model(model_dir) model.input_labels = [] model.output_labels = [] labels_file = os.path.join(model_dir, 'labels.json') if os.path.isfile(labels_file): with open(labels_file, 'r') as f: labels = json.load(f) if 'input' in labels: model.input_labels = labels['input'] if 'output' in labels: model.output_labels = labels['output'] return model
Example #9
Source File: main.py From stacks-usecase with Apache License 2.0 | 6 votes |
def load_disc_gen(): """ load models for training. Separate from 'load_model'. Returns Discriminator, Generator""" home_dir = get_directory() generators = glob.glob(os.path.join(home_dir, "models/checkpoints/generator-*.h5")) discriminators = glob.glob( os.path.join(home_dir, "models/checkpoints/discriminator-*.h5") ) try: gen_model_file = max(generators, key=os.path.getctime) gen_model = load_model(gen_model_file) except ValueError: gen_model = generator() try: disc_model_file = max(discriminators, key=os.path.getctime) disc_model = load_model(disc_model_file) except ValueError: disc_model = discriminator() return disc_model, gen_model
Example #10
Source File: test_model.py From efficientnet with Apache License 2.0 | 5 votes |
def test_model_save_load(): model = efn.EfficientNetB0() model.save('/tmp/model.h5') new_model = load_model('/tmp/model.h5')
Example #11
Source File: siamese_similarity.py From nlp-journey with Apache License 2.0 | 5 votes |
def _load_model_by_path(self, model_path, weights_only=True): try: if weights_only: model = self._build_model() model.load_weights(model_path) else: model = load_model(model_path) except FileNotFoundError: model = None return model
Example #12
Source File: Segnet预测.py From Semantic-segmentation-of-remote-sensing-images with Apache License 2.0 | 5 votes |
def predict(args): # load the trained convolutional neural network print("载入网络权重中……") model = load_model(args["model"]) stride = args['stride'] print("进行预测分割拼图中……") for n in range(len(TEST_SET)): path = TEST_SET[n] #load the image image = cv2.imread(basePath+'train\\' + path) h,w,_ = image.shape padding_h = (h//stride + 1) * stride padding_w = (w//stride + 1) * stride padding_img = np.zeros((padding_h,padding_w,3),dtype=np.uint8) padding_img[0:h,0:w,:] = image[:,:,:] padding_img = padding_img.astype("float") / divisor padding_img = img_to_array(padding_img) mask_whole = np.zeros((padding_h,padding_w),dtype=np.uint8) for i in range(padding_h//stride): for j in range(padding_w//stride): crop = padding_img[i*stride:i*stride+image_size,j*stride:j*stride+image_size,:3] ch,cw,_= crop.shape if ch != 32 or cw != 32: print('尺寸不正确,请检查!') continue crop = np.expand_dims(crop, axis=0) pred = model.predict_classes(crop,verbose=2) pred = labelencoder.inverse_transform(pred[0]) pred = pred.reshape((32,32)).astype(np.uint8) mask_whole[i*stride:i*stride+image_size,j*stride:j*stride+image_size] = pred[:,:] cv2.imwrite(basePath+'predict/'+path,mask_whole[0:h,0:w])
Example #13
Source File: FCN8S预测.py From Semantic-segmentation-of-remote-sensing-images with Apache License 2.0 | 5 votes |
def predict(args): # load the trained convolutional neural network print("载入网络权重中……") model = load_model(args["model"],custom_objects={'dice_coef': dice_coef}) stride = args['stride'] print("进行预测分割拼图中……") for n in range(len(TEST_SET)): path = TEST_SET[n] #load the image image = cv2.imread(basePath+'train\\' + path) h,w,_ = image.shape padding_h = (h//stride + 1) * stride padding_w = (w//stride + 1) * stride padding_img = np.zeros((padding_h,padding_w,3),dtype=np.uint8) padding_img[0:h,0:w,:] = image[:,:,:] padding_img = padding_img.astype("float") / 255.0 padding_img = img_to_array(padding_img) mask_whole = np.zeros((padding_h,padding_w),dtype=np.uint8) for i in range(padding_h//stride): for j in range(padding_w//stride): crop = padding_img[i*stride:i*stride+image_size,j*stride:j*stride+image_size,:3] ch,cw,_ = crop.shape #print(ch,cw,_) if ch != 32 or cw != 32: print('尺寸不正确,请检查!') continue crop = np.expand_dims(crop, axis=0) pred = model.predict(crop,verbose=2) pred=np.argmax(pred,axis=3) pred=pred.flatten() pred = labelencoder.inverse_transform(pred) pred = pred.reshape((32,32)).astype(np.uint8) mask_whole[i*stride:i*stride+image_size,j*stride:j*stride+image_size] = pred[:,:] cv2.imwrite(basePath+'predict/'+path,mask_whole[0:h,0:w])
Example #14
Source File: model.py From n-beats with MIT License | 5 votes |
def load(filepath, custom_objects=None, compile=True): from tensorflow.keras.models import load_model return load_model(filepath, custom_objects, compile)
Example #15
Source File: model.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def load(self, file_path, custom_objects={}): # Create model input path self.model = load_model(file_path, custom_objects, compile=False) # Compile model self.model.compile(optimizer=Adam(lr=self.learninig_rate), loss=self.loss, metrics=self.metrics)
Example #16
Source File: keras_policy.py From rasa-for-botfront with Apache License 2.0 | 5 votes |
def load(cls, path: Text) -> "KerasPolicy": from tensorflow.keras.models import load_model if os.path.exists(path): featurizer = TrackerFeaturizer.load(path) meta_file = os.path.join(path, "keras_policy.json") if os.path.isfile(meta_file): meta = json.loads(rasa.utils.io.read_file(meta_file)) model_file = os.path.join(path, meta["model"]) with warnings.catch_warnings(): warnings.simplefilter("ignore") model = load_model(model_file) return cls( featurizer=featurizer, priority=meta["priority"], model=model, current_epoch=meta["epochs"], ) else: return cls(featurizer=featurizer) else: raise Exception( "Failed to load dialogue model. Path {} " "doesn't exist".format(os.path.abspath(path)) ) # pytype: enable=import-error # pytype: disable=module-attr
Example #17
Source File: extractors.py From ICCV2019-Horde with MIT License | 5 votes |
def GoogleNet(end_layer=None): config = configparser.ConfigParser() config.read(expanded_join('config.ini')) model_path = config['PROJECT_FOLDERS']['DATA_PATH'] model = load_model(expanded_join(model_path, 'GoogleNet_notop.h5'), custom_objects={'LRN': LRN}) if not end_layer is None: model = Model(inputs=model.input, outputs=model.get_layer(name=end_layer).output) return model
Example #18
Source File: extractors.py From ICCV2019-Horde with MIT License | 5 votes |
def BNInception(end_layer=None): config = configparser.ConfigParser() config.read(expanded_join('config.ini')) model_path = config['PROJECT_FOLDERS']['DATA_PATH'] model = load_model(expanded_join(model_path, 'BN-Inception_notop.h5')) if not end_layer is None: model = Model(inputs=model.input, outputs=model.get_layer(name=end_layer).output) return model
Example #19
Source File: eval.py From keras-YOLOv3-model-set with MIT License | 5 votes |
def load_eval_model(model_path): # support of tflite model if model_path.endswith('.tflite'): from tensorflow.lite.python import interpreter as interpreter_wrapper model = interpreter_wrapper.Interpreter(model_path=model_path) model.allocate_tensors() model_format = 'TFLITE' # support of MNN model elif model_path.endswith('.mnn'): model = MNN.Interpreter(model_path) model_format = 'MNN' # support of TF 1.x frozen pb model elif model_path.endswith('.pb'): model = load_graph(model_path) model_format = 'PB' # support of ONNX model elif model_path.endswith('.onnx'): model = onnxruntime.InferenceSession(model_path) model_format = 'ONNX' # normal keras h5 model elif model_path.endswith('.h5'): custom_object_dict = get_custom_objects() model = load_model(model_path, compile=False, custom_objects=custom_object_dict) model_format = 'H5' K.set_learning_phase(0) else: raise ValueError('invalid model file') return model, model_format
Example #20
Source File: model_statistics.py From keras-YOLOv3-model-set with MIT License | 5 votes |
def main(): parser = argparse.ArgumentParser(description='tf.keras model FLOPs & PARAMs checking tool') parser.add_argument('--model_path', help='model file to evaluate', type=str, required=True) args = parser.parse_args() custom_object_dict = get_custom_objects() model = load_model(args.model_path, compile=False, custom_objects=custom_object_dict) get_flops(model)
Example #21
Source File: post_train_quant_convert.py From keras-YOLOv3-model-set with MIT License | 5 votes |
def post_train_quant_convert(keras_model_file, annotation_file, sample_num, model_input_shape, output_file): #get input_shapes for converter input_shapes=list((1,)+model_input_shape+(3,)) with open(annotation_file) as f: annotation_lines = f.readlines() custom_object_dict = get_custom_objects() model = load_model(keras_model_file, custom_objects=custom_object_dict) converter = tf.lite.TFLiteConverter.from_keras_model(model) def data_generator(): n = len(annotation_lines) i = 0 for num in range(sample_num): image, _ = get_ground_truth_data(annotation_lines[i], model_input_shape, augment=True) i = (i+1) % n image = np.array([image], dtype=np.float32) yield [image] converter.optimizations = [tf.lite.Optimize.DEFAULT] #converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE] #converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_LATENCY] converter.representative_dataset = tf.lite.RepresentativeDataset(data_generator) #converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8] tflite_model = converter.convert() with open(output_file, "wb") as f: f.write(tflite_model)
Example #22
Source File: tensorflow_to_coreml.py From keras-YOLOv3-model-set with MIT License | 5 votes |
def coreml_convert(input_model_file, output_file, model_image_size): if input_model_file.endswith('.h5'): if not tf.__version__.startswith('2'): raise ValueError('tf.keras model convert only support in TF 2.x env') # tf.keras h5 model custom_object_dict = get_custom_objects() keras_model = load_model(input_model_file, custom_objects=custom_object_dict) # get input, output node names for the TF graph from tf.keras model # assume only 1 input input_name = keras_model.inputs[0].name.split(':')[0] output_names = [output.name.split(':')[0].split('/')[-1] for output in keras_model.outputs] assert len(output_names) == 1, 'h5 model convert only support YOLOv2 family with 1 prediction output.' elif input_model_file.endswith('.pb'): # NOTE: TF 1.x frozen pb graph need to specify input/output tensor name # so we need to hardcode the input/output tensor names here to get them from model input_name = 'image_input' # YOLOv2 model with 1 prediction output #output_names = ['predict_conv/BiasAdd'] # Tiny YOLOv3 model with 2 prediction outputs output_names = ['predict_conv_1/BiasAdd', 'predict_conv_2/BiasAdd'] # YOLOv3 model with 3 prediction outputs #output_names = ['predict_conv_1/BiasAdd', 'predict_conv_2/BiasAdd', 'predict_conv_3/BiasAdd'] else: raise ValueError('unsupported model type') input_name_shape_dict={input_name: (1,) + model_image_size + (3,)} # convert to CoreML model file model = tfcoreml.convert(tf_model_path=input_model_file, mlmodel_path=output_file, input_name_shape_dict=input_name_shape_dict, output_feature_names=output_names, minimum_ios_deployment_target='13') # save converted CoreML model #model.save(output_file)
Example #23
Source File: keras_to_onnx.py From keras-YOLOv3-model-set with MIT License | 5 votes |
def onnx_convert(keras_model_file, output_file, op_set): custom_object_dict = get_custom_objects() model = load_model(keras_model_file, custom_objects=custom_object_dict) # convert to onnx model onnx_model = keras2onnx.convert_keras(model, model.name, custom_op_conversions=custom_object_dict, target_opset=op_set) # save converted onnx model onnx.save_model(onnx_model, output_file)
Example #24
Source File: ocr_server.py From pottan-ocr with MIT License | 5 votes |
def main( crnn ): global model, lineHeight, app from tensorflow.keras import models model = models.load_model( crnn ) lineHeight = model.input.shape[1] if not os.path.exists(TEMP_DIR): os.makedirs(TEMP_DIR) return app
Example #25
Source File: ivis.py From ivis with GNU General Public License v2.0 | 5 votes |
def load_model(self, folder_path): """Load ivis model Parameters ---------- folder_path : string Path to serialised model files and metadata Returns ------- returns an ivis instance """ ivis_config = json.load(open(os.path.join(folder_path, 'ivis_params.json'), 'r')) self.__dict__ = ivis_config loss_function = triplet_loss(self.distance, self.margin) self.model_ = load_model(os.path.join(folder_path, 'ivis_model.h5'), custom_objects={'tf': tf, loss_function.__name__: loss_function }) self.encoder = self.model_.layers[3] # If a supervised model exists, load it supervised_path = os.path.join(folder_path, 'supervised_model.h5') if os.path.exists(supervised_path): self.supervised_model_ = load_model(supervised_path) return self
Example #26
Source File: models.py From gordo with GNU Affero General Public License v3.0 | 5 votes |
def __setstate__(self, state): if "model" in state: with h5py.File(state["model"], compression="lzf", mode="r") as h5: state["model"] = load_model(h5, compile=False) if "history" in state: state["model"].__dict__["history"] = state.pop("history") self.__dict__ = state return self
Example #27
Source File: deep_classifier.py From nlp-journey with Apache License 2.0 | 5 votes |
def __init__(self, model_path, config_path, train=False, train_file_path=None, vector_path=None): self.model_path = model_path self.config_path = config_path if not train: assert config_path is not None, 'The config path cannot be None.' config = load_config(self.config_path) if not config: (self.word_index, self.max_len, self.embeddings) = config self.model = load_model(self.model_path, self.build_model()) if not self.model: print('The model cannot be loaded:', self.model_path) else: self.vector_path = vector_path self.train_file_path = train_file_path self.x_train, self.y_train, self.x_test, self.y_test, self.word_index, self.max_index = self.load_data() self.max_len = self.x_train.shape[1] config = load_config(self.config_path) if not config: self.embeddings = load_bin_word2vec(self.word_index, self.vector_path, self.max_index) save_config((self.word_index, self.max_len, self.embeddings), self.config_path) else: (_, _, self.embeddings) = config self.model = self.train() save_model(self.model, self.model_path) # 全连接的一个简单的网络, 仅用来作为基类测试代码通过,速度快, 但是分类效果特别差
Example #28
Source File: __init__.py From gtzan.keras with MIT License | 5 votes |
def run(self): if self.args.type == "ml": X = make_dataset_ml(self.args) pipe = load(self.args.model) pred = get_genres(pipe.predict(X)[0], self.genres) print("{} is a {} song".format(self.args.song, pred)) else: X = make_dataset_dl(self.args) model = load_model(self.args.model) preds = model.predict(X) votes = majority_voting(preds, self.genres) print("{} is a {} song".format(self.args.song, votes[0][0])) print("most likely genres are: {}".format(votes[:3]))
Example #29
Source File: ml_agent.py From Grid2Op with Mozilla Public License 2.0 | 5 votes |
def load_network(self, path, name=None, ext="h5"): # nothing has changed path_model, path_target_model = self._get_path_model(path, name) self.model = load_model('{}.{}'.format(path_model, ext)) self.target_model = load_model('{}.{}'.format(path_target_model, ext)) print("Succesfully loaded network.")
Example #30
Source File: ml_agent.py From Grid2Op with Mozilla Public License 2.0 | 5 votes |
def load_network(self, path, name=None, ext="h5"): # nothing has changed path_model, path_target_model, path_modelQ, path_modelQ2, path_policy = self._get_path_model(path, name) self.model_value = load_model('{}.{}'.format(path_model, ext)) self.model_value_target = load_model('{}.{}'.format(path_target_model, ext)) self.model_Q = load_model('{}.{}'.format(path_modelQ, ext)) self.model_Q2 = load_model('{}.{}'.format(path_modelQ2, ext)) self.model_policy = load_model('{}.{}'.format(path_policy, ext)) print("Succesfully loaded network.")