def loadKerasModel(self, filePath): global PMMLMODELSTORAGE fO = pathlib.Path(filePath) keyToModel = fO.name.replace(fO.suffix, '') # print (PMMLMODELSTORAGE) try: model_graph = Graph() with model_graph.as_default(): tf_session = Session() with tf_session.as_default(): seqModel = load_model(filePath) tempDictModel = { 'modelObj': seqModel, 'model_graph': model_graph, 'modelGeneratedFrom': 'Keras', 'tf_session': tf_session, 'inputShape': seqModel.input_shape, } PMMLMODELSTORAGE[keyToModel] = tempDictModel messageToWorld = "Model Loaded Successfully" reStat = 200 except: messageToWorld = "Model load failed, please connect with admin" keyToModel = None reStat = 500 resultResp = {'message': messageToWorld, 'keytoModel': keyToModel} return JsonResponse(resultResp, status=reStat)
def get_fetchable_tensors(graph: tf.Graph, names: List[str]) -> Dict[str, tf.Tensor]: """Retrieve fetch tensors from graph. Parameters ---------- graph : tf.Graph A Tensorflow Graph names : List[str] List of operations or tensor names Returns ------- Dict[str, tf.Tensor] Mapping of names to tf.Tensor """ fetchable_tensors = {} for name in names: op_or_tensor = graph.as_graph_element(name) if isinstance(op_or_tensor, tf.Tensor): tensor = op_or_tensor else: if len(op_or_tensor.outputs) > 1: raise ValueError( f"Found more than one tensor for operation {op_or_tensor}") tensor = op_or_tensor.outputs[0] if not graph.is_fetchable(tensor): raise ValueError(f"{name} should be fetchable but is not") fetchable_tensors[name] = tensor return fetchable_tensors
def buildTripletPairs(datasets, filename): embed_graph = Graph() triplet_paths_array = [] with embed_graph.as_default(): X_input = Input((height, width, 3)) X = InceptionResnetV2(X_input) model = Model(inputs=X_input, outputs=X) with tf.Session(graph=embed_graph) as sess: sess.run(tf.global_variables_initializer()) for file_inc in range(max_nrof_epochs): image_paths, num_per_class = sample_people( datasets, people_per_batch, images_per_person) nrof_examples = people_per_batch * images_per_person emb_array = np.zeros((nrof_examples, embedding_size)) embeds = model.predict(np.stack(getImages(image_paths))) for loc in range(nrof_examples): emb_array[loc, :] = embeds[loc] triplets, nrof_random_negs, nrof_triplets = select_triplets( emb_array, num_per_class, image_paths, people_per_batch, 0.2) triplet_paths = list(itertools.chain(*triplets)) triplet_paths_array.extend( np.reshape(np.expand_dims(np.array(triplet_paths), 1), (-1, 3))) np.savetxt(filename, triplet_paths_array, fmt=("%s", "%s", "%s"), delimiter=",")
def load(): global session global graph global model global data_result data_result = DataResult(None, None) with open(script_dir + '/../temp/processed_data.json', 'r') as output: json_data = json.load(output) data_result.loadJSON(json_data) graph = Graph() with graph.as_default(): session = Session(graph=graph) with session.as_default(): temp_encoder = Encoder(data_result.input_data) temp_decoder = Decoder(data_result.output_data, temp_encoder) temp_model = Model([temp_encoder.inputs, temp_decoder.inputs], temp_decoder.outputs) temp_model.compile(optimizer='rmsprop', loss='categorical_crossentropy') temp_model.load_weights( os.path.dirname(__file__) + '/../model_weights.h5') model = temp_model
def test_internal_slice_multiple_layers(self): graph = Graph() with graph.as_default(): x1 = tf.placeholder('float32', (None, 5)) z1 = x1 @ tf.random.normal((5, 6)) x2 = tf.placeholder('float32', (None, 1)) z2 = x2 @ tf.random.normal((1, 2)) z3 = z2 @ tf.random.normal((2, 4)) z4 = tf.concat([z1, z3], axis=1) z5 = z4 @ tf.random.normal((10, 7)) y = z5 @ tf.random.normal((7, 3)) model = ModelWrapper( graph, [x1, x2], y, dict(cut_layer1=z1, cut_layer2=z2)) infl = InternalInfluence( model, Cut(['cut_layer1', 'cut_layer2']), ClassQoI(1), PointDoi()) res = infl.attributions( [np.array([[1., 2., 3., 4., 5.]]), np.array([[1.]])]) self.assertEqual(len(res), 2) self.assertEqual(res[0].shape, (1, 6)) self.assertEqual(res[1].shape, (1, 2))
def _parse_graph_topologic_order(graph_def, output_nodes=None): # https://en.wikipedia.org/wiki/Topological_sorting if output_nodes is None: output_nodes = _parse_graph_layers(graph_def)[-1] graph = Graph() with graph.as_default(): import_graph_def(graph_def, name='') queue = deepcopy(output_nodes) visited = set() # temporary mark perm_visit = set() # Permanent mark ops_torder = [] # L def visit(node_name): if node_name in perm_visit: return if node_name in visited: raise ValueError("Input graph is not a DAG") visited.add(node_name) op = graph.get_operation_by_name(node_name) for tensor in op.inputs: visit(tensor.op.name) perm_visit.add(node_name) ops_torder.insert(0, node_name) while queue: node_name = queue.pop(0) visit(node_name) # ops_bfs.reverse() return ops_torder, output_nodes
def input_to_feed_dict(graph: tf.Graph, input_data: Union[dict, xr.Dataset]) \ -> Dict[Union[Union[tf.Tensor, tf.Operation], Any], Any]: """ Converts some input data to a feedable dict for Tensorflow sessions based on the placeholders in a tf.Graph :param graph: tf.Graph object :param input_data: either xr.Dataset or some dict{"placeholder": data} :return: dict{"placeholder:0", data} for all placeholder names in `input_data` """ placeholders = { op.name: op for op in graph.get_operations() if op.type.lower().startswith("placeholder") } if isinstance(input_data, xr.Dataset): keys = input_data.variables.keys() else: keys = input_data.keys() keys = set(keys).intersection(placeholders.keys()) retval = {} for k in keys: retval[graph.get_tensor_by_name(k + ":0")] = input_data[k] return retval
def load_KerasGraph(path): print("> ====== loading Keras model for classification") thread_graph = Graph() with thread_graph.as_default(): thread_session = Session() with thread_session.as_default(): input_shape = (28, 28, 1) num_classes = 6 model = Sequential() model.add(Conv2D(32, kernel_size=(3, 3), input_shape=input_shape)) model.add(ReLU()) model.add(Conv2D(32, kernel_size=(3, 3))) model.add(ReLU()) model.add(Conv2D(32, kernel_size=(3, 3))) model.add(ReLU()) model.add(Conv2D(32, kernel_size=(3, 3))) model.add(ReLU()) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128)) model.add(ReLU()) model.add(Dropout(0.5)) model.add(Dense(num_classes)) model.add(Softmax()) model.load_weights(path) graph = tf.get_default_graph() print("> ====== Keras model loaded") return model, graph, thread_session
def pretrainSingleClass(self, modelname, dataset, class_name, batch_size, epochs, lr): #K.clear_session() graph2 = Graph() with graph2.as_default(): session2 = Session() with session2.as_default(): reader = LFWReader(dir_images=dataset, class_name=class_name) gen_train = TripletGeneratorSingleID(reader) gen_test = TripletGeneratorSingleID(reader) embedding_model, triplet_model = GetModel() for layer in embedding_model.layers[-3:]: layer.trainable = True for layer in embedding_model.layers[:-3]: layer.trainable = False triplet_model.compile(loss=None, optimizer=Adam(lr)) history = triplet_model.fit_generator(gen_train, validation_data=gen_test, epochs=epochs, verbose=1, steps_per_epoch=50, validation_steps=5) embedding_model.save_weights('./trained-models/weights/' + modelname + '.h5') self.embeddingmodel(modelname, dataset) K.get_session()
class DetectFace: def __init__(self): self.graph = Graph() with self.graph.as_default(): self.session = Session(graph=self.graph) with self.session.as_default(): self.detector = MTCNN() def processe(self, args): faces = [] image = args k.set_session(self.session) with self.graph.as_default(): results = self.detector.detect_faces(image) for res in results: x1, y1, width, height = res['box'] x1, y1 = abs(x1), abs(y1) x2, y2 = x1 + width, y1 + height face = image[y1:y2, x1:x2] faces.append(face) return faces
def __init__(self): # Set TF configuration config = tf.ConfigProto(gpu_options=tf.GPUOptions( per_process_gpu_memory_fraction=0.2)) config.gpu_options.allow_growth = True self.graph = Graph() with self.graph.as_default(): self.session = tf.Session(config=config) with self.session.as_default(): # Load model K.set_learning_phase(0) #with open("./data/model.json", 'r') as json_file: # loaded_model_json = json_file.read() #model = model_from_json(loaded_model_json) model = load_model("./data/model1.h5") K.set_learning_phase(0) # compile requirement for inrefence... model.compile(optimizer='SGD', loss='binary_crossentropy', metrics=['acc']) K.set_learning_phase(0) self.model = model
def loadModels(self, path): """Load models from the json file given.""" logger.info('Loading models from %s', path) self.data_location = None self.models = [] if path == self.data_location: return if not os.path.exists(path): logger.error('Could not find file: %s', path) self.data_location = path with open(path, 'r') as f: data = json.load(f) if 'models' not in data: logger.error('No models defined in data') return models = data['models'] for i, model in enumerate(models): if not model: self.models.append(None) continue vertices = data['joint_map'][i] graph = Graph() with graph.as_default(): session = Session() with session.as_default(): meta = model.get('meta') root = model.get('root') saver = tf.train.import_meta_graph(meta) saver.restore(session, tf.train.latest_checkpoint(root)) in_tensor = session.graph.get_tensor_by_name( model['input']) out_tensor = session.graph.get_tensor_by_name( model['output']) normalized = model['normalized'] verts_max, verts_min, trans_max, trans_min = None, None, None, None if normalized: trans_max = np.array(model['trans_max']) trans_min = np.array(model['trans_min']) verts_max = np.array(model['verts_max']) verts_min = np.array(model['verts_min']) tfmodel = TFModel(graph=session.graph, session=session, input_tensor=in_tensor, output_tensor=out_tensor, vertices=vertices, normalized=normalized, trans_max=trans_max, trans_min=trans_min, verts_max=verts_max, verts_min=verts_min) self.models.append(tfmodel)
def __init__(self, n_users, steps): global topics, producers self.n_users = n_users self.steps = steps self.graph = Graph() with self.graph.as_default(): self.sess = Session() K.set_session(self.sess) with self.graph.as_default(): self.model = keras.Sequential() self.users = [] futures = [] with concurrent.futures.ThreadPoolExecutor() as executor: for _ in range(n_users): futures.append( executor.submit(User, topics[_], producers[_], self.n_users, self.steps)) for i in futures: self.users.append(i.result()) for i in range(self.n_users): producers[i].flush()
def rsna_evaluate(model_path, backbone, anchor_boxes, score_threshold, nms_threshold, rsna_path, rsna_test_json, anchor_scales): """ Evaluate an json using retinanet model, print mAP based on GT and generate kaggle submission file. """ save_path = None from tensorflow import Graph, Session graph1 = Graph() with graph1.as_default(): session1 = Session() with session1.as_default(): model2 = models.load_model(model_path, backbone_name=backbone, convert=True, nms_threshold=nms_threshold, anchors_ratios=anchor_boxes, anchors_scales=anchor_scales) # create the generator generator = create_generator(rsna_test_json, rsna_path) map = evaluate(generator, model2, iou_threshold=0.5, score_threshold=score_threshold, max_detections=100, generate_kaggle_output='teste.csv') del model2 import gc gc.collect() with open('output_map.txt', 'a') as output_map: output_map.write('{} : {} \n'.format(model_path, map))
def __init__(self, n_users, num_steps): self.n_users = n_users self.steps = num_steps self.graph = Graph() with self.graph.as_default(): self.sess = Session() K.set_session(self.sess) with self.graph.as_default(): self.model = keras.Sequential() #self.users = [User(iplist[_],_) for _ in range(n_users)] self.users = [] futures = [] with concurrent.futures.ThreadPoolExecutor() as executor: for v in range(n_users): futures.append(executor.submit(User, iplist[v], v)) for i in futures: self.users.append(i.result())
def _mark_outputs_as_train_op(graph: tf.Graph, signature_def: SignatureDef) -> None: """Mark output nodes as training ops, so the optimizer ignores them""" train_op = GraphKeys.TRAIN_OP for _, tensor in signature_def.outputs.items(): name = _to_node_name(tensor.name) graph.add_to_collection(train_op, graph.get_operation_by_name(name))
def create_op_to_quant_ops_dict(graph: tf.Graph, conn_graph: ConnectedGraph, ops_with_param_names: List[str], indices: List[int], activation_op_names: List[str]) -> OpToQuantOpsDictType: """ Create an op to quant ops dictionary mapping connected graph ops to a list consisting of the activation quantizer and a dictionary mapping param type string to param quantizers. :param graph: Tensorflow graph containing inserted quantizers :param conn_graph: Connected graph of the original unquantized model :param ops_with_param_names: List of tf operation names for which parameter quantizers were inserted for :param indices: Indices of tf operations of which parameter quantizers were inserted for :param activation_op_names: List of tf operation names for which activation quantizers were inserted for :return: Dictionary mapping connected graph ops to a list consisting of the activation quantizer and a dictionary mapping param type string to param quantizers. """ op_to_quant_ops_dict = {} for op_with_param_name, index in zip(ops_with_param_names, indices): op_with_param = graph.get_operation_by_name(op_with_param_name) conn_graph_op = conn_graph.get_op_from_module_name(op_with_param_name) param_type = 'weight' if op_with_param.type == 'BiasAdd': param_type = 'bias' param_quantizer = get_param_quantizer(op_with_param, index) assert param_quantizer.type in ['QcQuantize', 'QcQuantizeRecurrentParam'] add_op_to_quant_ops_dict_entry(param_quantizer, conn_graph_op, True, param_type, op_to_quant_ops_dict) for activation_op_name in activation_op_names: activation_op = graph.get_operation_by_name(activation_op_name) conn_graph_op = conn_graph.get_op_from_module_name(activation_op_name) activation_quantizer = \ [consumer for consumer in activation_op.outputs[0].consumers() if consumer.type == 'QcQuantize'] if len(activation_quantizer) != 1: _logger.error('Expected one activation quantizer but found %s', len(activation_quantizer)) raise AssertionError add_op_to_quant_ops_dict_entry(activation_quantizer[0], conn_graph_op, False, '', op_to_quant_ops_dict) return op_to_quant_ops_dict
def __init__(self): os.environ['CUDA_VISIBLE_DEVICES'] = '0' model_dir_path = os.path.join(os.path.dirname(__file__), "../models") default_model_file_path = os.path.join(model_dir_path, "EAST_IC15+13_model.h5") json_file = open(os.path.join(model_dir_path, 'model.json'), 'r') # try: # os.makedirs(FLAGS.output_dir) # except OSError as e: # if e.errno != 17: # raise # load trained model loaded_model_json = json_file.read() json_file.close() self.graph1 = Graph() self.tf_session = None with self.graph1.as_default(): self.tf_session = Session() with self.tf_session.as_default(): self.model = model_from_json(loaded_model_json, custom_objects={ 'tf': tf, 'RESIZE_FACTOR': RESIZE_FACTOR }) self.model.load_weights(default_model_file_path) print("**** loading " + default_model_file_path + "......successful *******") # call super super(Phase1_0ImageLineContourExtractorHandler, self).__init__()
def __init__(self): self.graph = Graph() with self.graph.as_default(): self.session = Session(graph=self.graph) with self.session.as_default(): self.model = insightface.model_zoo.get_model( 'retinaface_r50_v1') self.model.prepare(ctx_id=-1, nms=0.4)
def __init__(self, n_users, steps): imports() preprocess(n_users, steps) self.graph = Graph() with self.graph.as_default(): self.sess = Session()
def load_KerasGraph(path): thread_graph = Graph() with thread_graph.as_default(): thread_session = Session() with thread_session.as_default(): model = keras.models.load_model(path)#model._make_predict_function() graph = tf.get_default_graph() return model, graph, thread_session
def clear_asset_annotations(graph: tf.Graph): """Clears the asset annotations. Args: graph: A `tf.Graph` object. """ graph.clear_collection(_ASSET_KEY_COLLECTION) graph.clear_collection(_ASSET_FILENAME_COLLECTION)
def __init__(self, ip, user_id=None): self.user_id = user_id self.graph = Graph() with self.graph.as_default(): self.sess = Session()
def __init__(self): self.graph = Graph() with self.graph.as_default(): self.sess = Session() K.set_session(self.sess)
def graphandsession(): model_graph = Graph() with model_graph.as_default(): tf_session = Session() with tf_session.as_default(): mdl=load_model("C:/Users/Ritik/Desktop/Machine Learning Practice/Bank Loan Classification/DjangoAPI/MyAPI/models/model.h5") return (model_graph,tf_session,mdl)
def load_from_file(filename): graph = Graph() with graph.as_default(): sess = utils.get_nn_config() with sess.as_default(): model = load_model(filename, compile=False) compile_model(model) return model, graph, sess return None
def create_tensor_dict(detection_graph: tensorflow.Graph) -> dict: tensor_dict = {} with tensorflow.compat.v1.Session(graph=detection_graph): ops = detection_graph.get_operations() all_tensor_names = {output.name for op in ops for output in op.outputs} for key in ['num_detections', 'detection_boxes', 'detection_scores', 'detection_classes']: tensor_name = key + ':0' if tensor_name in all_tensor_names: tensor_dict[key] = detection_graph.get_tensor_by_name(tensor_name) return tensor_dict
def set_graph_flow_context(graph: tf.Graph, active_context): """ sets graph context to active context provided :param graph: TensorFlow Graph (tf.Graph) :param active_context: context object to be set as current graph's context :return: """ # pylint: disable=protected-access graph._set_control_flow_context(active_context)
def load_KerasGraph(path): print("> ====== loading Keras model for classification") thread_graph = Graph() with thread_graph.as_default(): thread_session = Session() with thread_session.as_default(): model = keras.models.load_model(path) graph = tf.get_default_graph() print("> ====== Keras model loaded") return model, graph, thread_session
def load_single_model(path): graph = Graph() with graph.as_default(): session = Session() with session.as_default(): model = load_model(path) model._make_predict_function() MODELS.append(model) GRAPHS.append(graph) SESSIONS.append(session)
def create_detector(self, verbose, mtcnn_kwargs): """ Create the mtcnn detector """ self.verbose = verbose if self.verbose: print("Adding MTCNN detector") self.kwargs = mtcnn_kwargs mtcnn_graph = Graph() with mtcnn_graph.as_default(): mtcnn_session = Session() with mtcnn_session.as_default(): pnet, rnet, onet = create_mtcnn(mtcnn_session, self.data_path) mtcnn_graph.finalize() self.kwargs["pnet"] = pnet self.kwargs["rnet"] = rnet self.kwargs["onet"] = onet self.initialized = True
def load_model(self, verbose, dummy, ratio): """ Load the Keras Model """ self.verbose = verbose if self.verbose: print("Initializing keras model...") keras_graph = Graph() with keras_graph.as_default(): config = ConfigProto() if ratio: config.gpu_options.per_process_gpu_memory_fraction = ratio self.session = Session(config=config) with self.session.as_default(): self.model = keras.models.load_model( self.model_path, custom_objects={'TorchBatchNorm2D': TorchBatchNorm2D}) self.model.predict(dummy) keras_graph.finalize() self.initialized = True