def set_model(self, model): """Set model params and params useful for writing""" self.model = model self.sess = K.get_session() self.train_file_writer = tf.summary.FileWriter(self.training_log_dir) self.val_file_writer = tf.summary.FileWriter(self.val_log_dir) super(MetricsBoard, self).set_model(model)
def reset_weights(model, session=None): """ reset weights of model with the appropriate initializer. Note: only uses "kernel_initializer" and "bias_initializer" does not close session. Reference: https://www.codementor.io/nitinsurya/how-to-re-initialize-keras-model-weights-et41zre2g Parameters: model: keras model to reset session (optional): the current session """ if session is None: session = K.get_session() for layer in model.layers: reset = False if hasattr(layer, 'kernel_initializer'): layer.kernel.initializer.run(session=session) reset = True if hasattr(layer, 'bias_initializer'): layer.bias.initializer.run(session=session) reset = True if not reset: print('Could not find initializer for layer %s. skipping', layer.name)
def __init__(self, config, remote=False): self.config = config self.num_actions = self.config.num_actions # change to len of moveDict self.batch_size = 1 self.remote = remote if self.remote: self.ipaddress = input("Enter ip address of server: ") self.port = input("Enter port: ") self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) else: model_files = os.listdir('models/') model_files.sort() if model_files: model_filepath = 'models/' + model_files[-1] print('Loading model {}...'.format(model_filepath)) self.model = load_model(model_filepath) else: self.model = self.build_model() self.model.predict(np.random.randn(1, 8, 8, 18)) self.session = K.get_session() self.graph = tf.get_default_graph() self.graph.finalize()
def infer(self, yield_single_examples=False): ''' only for infer ''' #load data mode = utils.INFER # data must be init before model build infer_ds, infer_task = self.input_data(mode=mode) infer_gen = tf.data.make_one_shot_iterator(infer_ds) self.model_fn(mode=mode) assert self._built #load model infer_func = self.get_metric_func() for _ in range(len(infer_task)): batch_data = K.get_session().run(infer_gen.get_next()[0]) batch_input = batch_data['inputs'] batch_uttid = batch_data['uttids'].tolist() batch_predict = infer_func(batch_input)[0] batch_decode = py_ctc.ctc_greedy_decode(batch_predict, 0, unique=True) for utt_index, uttid in enumerate(batch_uttid): logging.info("utt ID: {}".format(uttid)) logging.info("infer result: {}".format( batch_decode[utt_index]))
def get_measure_tf(model, in_width, in_channels, number_of_measures, tmp_dir_name, benchmark_loc, tmp_file_name='model.pb'): """given a model, saves that model as a .pb file and benchmarks the time needed for a prediction in C++ using the benchmark tool associated with tf (this tool does not return median but only mean so we will use that instead) :return: the mean of number_of_measures trials As of now, this function does not work with my benchmark binary since the binary wasn't compiled at the same time than the whl pckg I used on the rasberry pi (I couldn't make the cross compilation work) so I got a whl for tf 1.13.1 for rasberry-pi on the internet""" model.compile(optimizer=SGD(), loss='binary_crossentropy') frozen_graph = freeze_session( keras_backend.get_session(), output_names=[out.op.name for out in model.outputs]) tf.train.write_graph(frozen_graph, tmp_dir_name, tmp_file_name, as_text=False) # Loads model and get measures command_line = "{} --graph={} --input_layer_shape='1, {}, {}, {}' --num_runs={} |& tr -d '\n' | awk {}" \ .format(benchmark_loc, os.path.join(tmp_dir_name, tmp_file_name), in_width, in_width, in_channels, number_of_measures, "'{print $NF}'") result = subprocess.check_output(command_line, shell=True, executable='/bin/bash') result = float(result) / 10**6 # result given in microseconds return result
def eval(self): ''' only eval''' mode = utils.EVAL #get eval dataset # data must be init before model build eval_ds, eval_task = self.input_data(mode=mode) eval_gen = tf.data.make_one_shot_iterator(eval_ds) #get eval model self.model_fn(mode=mode) assert self._built #load model eval_func = self.get_metric_func() target_seq_list, predict_seq_list = [], [] for _ in range(len(eval_task)): batch_data = K.get_session().run(eval_gen.get_next()[0]) batch_input = batch_data['inputs'] batch_target = batch_data['targets'].tolist() batch_predict = eval_func(batch_input)[0] batch_decode = py_ctc.ctc_greedy_decode(batch_predict, 0, unique=True) target_seq_list += batch_target predict_seq_list += batch_decode token_errors = metrics_lib.token_error( predict_seq_list=predict_seq_list, target_seq_list=target_seq_list, eos_id=0) logging.info("eval finish!") logging.info("Token Error: {}".format(token_errors))
def savepb(model, sess=None, pbname="result.pb"): #tf.keras.backend.set_learning_phase(0) #K.set_learning_phase(0) model_outputs = model.outputs output_names = [] outputs = [] for i, output in enumerate(model_outputs): output_names.append("output_{}".format(i)) outputs.append(tf.identity(output, name = output_names[i])) input_names = [node.op.name for node in model.inputs] print("[ INFO ] inputs: ", input_names) print("[ INFO ] outputs: ", output_names) if sess is None: sess = K.get_session() graph_def = sess.graph.as_graph_def() #graphdef_inf = graph_util.remove_training_nodes(graph_def) #graphdef_frozen = graph_util.convert_variables_to_constants(sess, graphdef_inf, output_names) graphdef_frozen = graph_util.convert_variables_to_constants(sess, graph_def, output_names) graph_io.write_graph(graphdef_frozen, ".", pbname, as_text=False) print("[ INFO ] Saved pb into: ", pbname) print("[ INFO ] inputs: ", input_names) print("[ INFO ] outputs: ", output_names)
def tf_vector_generator(vectors, onehots, batch_size, shuffle_size=1000, num_parallel_calls=tf.data.experimental.AUTOTUNE): # Get shapes from input data vec_size = vectors.shape vec_size = (None, vec_size[1]) onehot_size = onehots.shape onehot_size = (None, onehot_size[1]) vectors_tensor = tf.placeholder(tf.float32, shape=vec_size) onehots_tensor = tf.placeholder(tf.float32, shape=onehot_size) # Create dataset dataset = tf.data.Dataset.from_tensor_slices((vectors_tensor, onehots_tensor)) dataset = dataset.shuffle(shuffle_size, reshuffle_each_iteration=True).repeat() dataset = dataset.batch(batch_size) dataset = dataset.prefetch(1) iterator = dataset.make_initializable_iterator() init_op = iterator.initializer next_val = iterator.get_next() with K.get_session().as_default() as sess: sess.run(init_op, feed_dict={vectors_tensor: vectors, onehots_tensor: onehots}) while True: inputs, labels = sess.run(next_val) yield inputs, labels
def SaveFrozenGraphV1(model, config=None): # see: https://leimao.github.io/blog/Save-Load-Inference-From-TF-Frozen-Graph/ pb_dir = os.path.join(config['dir']) sess = K.get_session() graph = tfv1.get_default_graph() input_graph_def = graph.as_graph_def() tfv1.graph_util.remove_training_nodes(input_graph_def) # code.interact(local=dict(globals(), **locals())) graph_def = tfv1.graph_util.extract_sub_graph( input_graph_def, [model.input.op.name, model.output.op.name]) output_node_names = [model.output.op.name] output_graph_def = tfv1.graph_util.convert_variables_to_constants( sess, graph_def, output_node_names) # code.interact(local=dict(globals(), **locals())) layers = [op.name for op in graph.get_operations()] pprint.pprint(layers[0]) tfv1.io.write_graph(output_graph_def, pb_dir, "model.pb", as_text=False)
def fetch_train_iterators(self): train_iterator = self.tfdataset_train.make_one_shot_iterator() fetch_val = train_iterator.get_next() with K.get_session().as_default() as sess: while True: *inputs, outputs = sess.run(fetch_val) yield inputs, outputs
def tf_augmented_image_generator_for_segmentation(images, targets, batch_size, map_fn, augment_targets=False, shuffle_size=1000, num_parallel_calls=tf.data.experimental.AUTOTUNE): # Get shapes from input data img_size = images.shape img_size = (None,) + img_size[1:] target_size = targets.shape target_size = (None,) + target_size[1:] images_tensor = tf.compat.v1.placeholder(tf.float32, shape=img_size) targets_tensor = tf.compat.v1.placeholder(tf.float32, shape=target_size) # Create dataset dataset = tf.data.Dataset.from_tensor_slices((images_tensor, targets_tensor)) if map_fn is not None: if augment_targets is False: dataset = dataset.map(lambda x, y: (map_fn(x), y), num_parallel_calls=num_parallel_calls) else: dataset = dataset.map(lambda x, y: map_fn(x, y), num_parallel_calls=num_parallel_calls) dataset = dataset.shuffle(shuffle_size, reshuffle_each_iteration=True).repeat() dataset = dataset.batch(batch_size) dataset = dataset.prefetch(1) iterator = dataset.make_initializable_iterator() init_op = iterator.initializer next_val = iterator.get_next() with K.get_session().as_default() as sess: sess.run(init_op, feed_dict={images_tensor: images, targets_tensor: targets}) while True: inputs, labels = sess.run(next_val) yield inputs, labels
def grad_romml(self, k): w_r = self.forward_reduced(k) e_NN = self.dl_model.predict([[k.vector()[:]]])[0] reduced_fwd_obs = np.dot(self.B_obs_phi, w_r) romml_fwd_obs = reduced_fwd_obs + e_NN reduced_adj_rhs = np.dot(self.B_obs_phi.T, self.data - romml_fwd_obs) psi = self.psi_p.getDenseArray() A_r = self._A_r.getDenseArray() v_r = np.linalg.solve(A_r.T, reduced_adj_rhs) psi_v_r = np.dot(psi, v_r) A_phi_w_r = np.dot(self.dA_dsigmak_phi, w_r).T dROM_dk = np.dot(A_phi_w_r, self.B_obs) f_x_dp_x = np.dot(psi_v_r.T, dROM_dk) x_inp = [k.vector()[:]] session = get_session() f_eps_dp_eps = session.run(self.NN_grad, feed_dict={self.dl_model.input: x_inp, self.reduced_fwd_obs: reduced_fwd_obs, self.data_ph: self.data})[0] loss = session.run(self.loss, feed_dict={self.dl_model.input: x_inp, self.reduced_fwd_obs: reduced_fwd_obs, self.data_ph: self.data}) self.NN_grad_val = f_eps_dp_eps.reshape(f_eps_dp_eps.size) grad = f_x_dp_x + self.NN_grad_val return grad, loss
def get_intial_features(model, content_image, style_image): stacked_images = np.concatenate([style_image, content_image], axis=0) with K.get_session().as_default(): K.get_session().run(tf.global_variables_initializer()) output = K.get_session().run(model.output, feed_dict={model.input: stacked_images}) target_style_features = [ style_layer[0] for style_layer in output[:len(style_layers)] ] target_content_features = [ content_layer[1] for content_layer in output[len(style_layers):] ] return target_style_features, target_content_features
def reset_weights(model): session = K.get_session() for layer in model.layers: if hasattr(layer, 'kernel_initializer'): layer.kernel.initializer.run(session=session) if hasattr(layer, 'bias_initializer'): layer.bias.initializer.run(session=session)
def from_keras(cls, keras_model, dataset): import tensorflow.keras.backend as K sess = K.get_session() outputs = keras_model.outputs inputs = keras_model.inputs return cls(sess, outputs, inputs, dataset)
def set_weights_ema(self): return K.get_session().run( tensorflow.group(*[ K.update(p, e_p / (1 - K.pow(ema_decay, self.total_iterations))) for e_p, p in zip(self.params_ema, self.params_for_ema_tracking) ]))
def reset_keras(self, delete_list=None): import tensorflow as tf import tensorflow.keras.backend as K if delete_list is None: K.clear_session() s = tf.InteractiveSession() K.set_session(s) else: import gc #sess = K.get_session() K.clear_session() #sess.close() sess = K.get_session() try: for d in delete_list: del d except: pass print(gc.collect( )) # if it's done something you should see a number being outputted # use the same config as you used to create the session config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 1 config.gpu_options.visible_device_list = "0" K.set_session(tf.Session(config=config)) # load model agai self.set_base_model()
def save_tflite(self, model, path, tmp_path, post_training_quantize=True): sess = K.get_session() self.write_file( path + ".tflite", tf.lite.TFLiteConverter.from_session(sess, model.inputs, model.outputs).convert())
def save_model(model, filename): # Print status. print('Saving model "%s" ...' % filename) # Clean temporary folder. clean_dir(TEMP_DIR) # Get model inputs. model_inputs_dict = dict() model_inputs_array = [] for idx in range(len(model.inputs)): model_inputs_dict['input_%d' % idx] = model.inputs[idx] model_inputs_array.append(model.inputs[idx].op.name) # Get model outputs. model_outputs_dict = dict() model_outputs_array = [] for idx in range(len(model.outputs)): if idx == 0: output_name = model.outputs[idx].op.name else: output_name = model.outputs[idx].name model_outputs_dict[output_name] = model.outputs[idx] model_outputs_array.append(output_name) # Save TensorFlow checkpoint. tf.saved_model.simple_save(keras_backend.get_session(), os.path.join(TEMP_DIR, 'checkpoint'), inputs=model_inputs_dict, outputs=model_outputs_dict) # Freeze TensorFlow graph. freeze_graph.freeze_graph( None, None, None, None, model.outputs[0].op.name, None, None, os.path.join(TEMP_DIR, 'model.pb'), False, "", input_saved_model_dir=os.path.join(TEMP_DIR, 'checkpoint'), ) # Convert and save TensorFlowLite model. converter = tf.lite.TFLiteConverter.from_frozen_graph( os.path.join(TEMP_DIR, 'model.pb'), input_arrays=model_inputs_array, output_arrays=model_outputs_array) converter.dump_graphviz_video = False tflite_model = converter.convert() model_filename = os.path.join(OUT_DIR, filename) if not model_filename.endswith('.tflite'): model_filename += '.tflite' open(model_filename, 'wb').write(tflite_model) # Clean temporary folder. rm_dir(TEMP_DIR)
def plot_images_in_tensorboard(self, image, gt, epoch, plot_tag="TB Image Plot"): sess = K.get_session() # Make a fake batch so we can use the default inference model curr_image = np.expand_dims(image, axis=0) image_batch = np.repeat(curr_image, self.infer_model.infer_batch_size, axis=0) # Execute the model on this image infer_results = self.infer_model(image_batch) infer_results = infer_results[0, :, :] # Because continuous "not a valid bounding box" warnings make me anxious corrected_infer_results = tf.maximum(infer_results, 0.0) corrected_infer_results = tf.minimum(corrected_infer_results, 1.0) corrected_infer_results = sess.run(corrected_infer_results) # Mark up the images with the predicted bounding boxes drawn_images = self.markup_images(curr_image[0, :, :, :], corrected_infer_results, gt) image_protobuf = self.make_image_protobuf(drawn_images) # Push a sample image from this batch to TensorBoard summary = tf.Summary(value=[tf.Summary.Value(tag=plot_tag, image=image_protobuf)]) self.get_writer().add_summary(summary, epoch) self.get_writer().flush()
def keras_to_uff(model_choice, weights): """ This is important to READ. This must be done in a Tensorflow Version <2. In otherwords a Tensorflow version where graph computation was the norm. Due to the changes in Tensorflow 2, converting to uff is basically impossible to do since all the uff conversions now use DEPRECATED and REMOVED functions and classes. If I'm honest, this is a PITA. Reference for this python file: https://devtalk.nvidia.com/default/topic/1028464/jetson-tx2/converting-tf-model-to-tensorrt-uff-format/ """ K.set_learning_phase(0) if model_choice == 'fastscnn': model = fast_scnn.model(num_classes=20, input_size=(1024, 2048, 3)) input_size = '1024x2048' elif model_choice == 'deeplabv3+': # Its important here to set the output stride to 8 for inferencing model = deeplabv3plus.model(num_classes=20, input_size=(1024, 2048, 3), depthwise=True, output_stride=8) input_size = '1024x2048' elif model_choice == 'separable_unet': # Was trained on a lower resolution model = separable_unet.model(num_classes=20, input_size=(512, 1024, 3)) input_size = '512x1024' # Whatever the model is, load the weights chosen model.load_weights(weights) # Plot the model for visual purposes in case anyone asks what you used tf.keras.utils.plot_model(model, to_file=os.path.join('./results', model_choice, model_choice + '.png'), show_shapes=True) # Get the outputs outputs = [] for output in model.outputs: outputs.append(output.name.split(":")[0]) # Set the filename for the frozen graph frozen_graph = os.path.join('./results', model_choice, model_choice + '_' + input_size + '.pb') # Let's begin session = K.get_session() # Get the graph definition and remove training nodes, ignore deprecation warnings here... graph_def = tf.graph_util.convert_variables_to_constants( session, session.graph_def, outputs) graph_def = tf.graph_util.remove_training_nodes(graph_def) # Write frozen graph to file with open(frozen_graph, 'wb') as f: f.write(graph_def.SerializeToString()) f.close() # Get the uff filename uff_filename = frozen_graph.replace('.pb', '.uff') # Convert and save as uff and we're done uff_model = uff.from_tensorflow_frozen_model(frozen_graph, outputs, output_filename=uff_filename)
def run(): data_root = "C:/Users/MBaer/Desktop/webserver/Images" saved_model = "C:/Users/MBaer/Desktop/imf/savedmodel" feature_extractor_url = "https://tfhub.dev/google/imagenet/resnet_v2_152/feature_vector/1" #this image_generator = tf.keras.preprocessing.image.ImageDataGenerator( rescale=1 / 255) IMAGE_SIZE = [224, 224] image_data = image_generator.flow_from_directory(data_root, target_size=IMAGE_SIZE) model = tf.contrib.saved_model.load_keras_model(saved_model) #model = tf.keras.models.load_model(saved_model) import tensorflow.keras.backend as K sess = K.get_session() init = tf.global_variables_initializer() sess.run(init) model.compile(optimizer=tf.train.AdamOptimizer(), loss='categorical_crossentropy', metrics=['accuracy']) result_data = model.predict(image_data) label_names = ['Pass', 'Fail'] label_names = np.array(label_names, dtype='<U10') predictions = label_names[np.argmax(result_data, axis=-1)] return (predictions[0])
def on_epoch_end(self, epoch, logs=None): # build graph if not self._built: self.build_graph() self._built = True sess = K.get_session() outputs = [] for i in range(self._validation_steps): if self._verbose > 0: print(f"Validation on {self._dataset_name}: {i+1:04d}/" f"{self._validation_steps:04d}") # run graph out = sess.run(self._output_tensors) # reshape (vector -> matrix) out = [o.reshape(-1, 1) if o.ndim == 1 else o for o in out] # concatenate out = np.concatenate(out, axis=1) # append outputs.append(out) # concatenate outputs outputs = np.concatenate(outputs, axis=0) # get mean loss mean_loss = outputs[:, 0].mean() # add to logs logs[self._dataset_name + '_loss'] = mean_loss # save file # note: unlike in keras callbacks, epoch is in range [0, n_epochs-1] fp = self._output_filepath.format(epoch=epoch) np.save(fp, outputs)
def __init__(self): #, **kwargs): self.__dict__.update(self._defaults) # set up default values #self.__dict__.update(kwargs) # and update with user overrides self.class_names = self._get_class() self.anchors = self._get_anchors() self.sess = K.get_session() self.boxes, self.scores, self.classes = self.generate()
def initialize_uninitialized_variables(): sess = K.get_session() uninitialized_variables = set([i.decode('ascii') for i in sess.run(tf.report_uninitialized_variables())]) init_op = tf.variables_initializer( [v for v in tf.global_variables() if v.name.split(':')[0] in uninitialized_variables] ) sess.run(init_op)
def diag_fisher(model, data): """ This function assumes that the last layer of the model has softmax activation """ if model.layers[-1].activation.__name__ != 'softmax': from_logits = False if model.layers[-1].activation.__name__ != 'linear': from_logits = True else: raise InputError( "The last layer has to have softmax or linear activation") xs = tf.Variable(data) with tf.GradientTape() as tape: y = model(xs) max_vals = tf.reduce_max(y, axis=1, keepdims=True) cond = tf.equal(y, max_vals) y_pred = tf.where(cond, tf.ones_like(y), tf.zeros_like(y)) cc = K.categorical_crossentropy(y_pred, y, from_logits=False) tape_grad = tape.gradient(cc, model.trainable_variables) sess = K.get_session() sess.run(tf.variables_initializer([xs])) grads = sess.run(tape_grad) fisher = [g**2 for g in grads] fisher_flatten = np.concatenate([np.reshape(f, (-1)) for f in fisher]).reshape(-1) return fisher_flatten
def __init__(self, model_name, model_path): if self.is_tf_gpu_version() is True: print('use tf GPU version,', tf.__version__) else: print('use tf CPU version,', tf.__version__) # these three parameters are no need to modify self.model_name = model_name self.model_path = os.path.join(os.path.dirname(__file__), 'trained_weights_final.h5') self.input_image_key = 'images' self.anchors_path = os.path.join(os.path.dirname(__file__), 'yolo_anchors.txt') self.classes_path = os.path.join(os.path.dirname(__file__), 'train_classes.txt') # self.score = 0.01####0.6026 self.score = 0.01 self.iou = 0.45 self.model_image_size = (416, 416) self.label_map = parse_classify_rule( os.path.join(os.path.dirname(__file__), 'classify_rule.json')) self.class_names = self._get_class() self.anchors = self._get_anchors() self.sess = K.get_session() with self.sess.as_default(): with self.sess.graph.as_default(): self.K_learning_phase = K.learning_phase() self.boxes, self.scores, self.classes = self.generate() print('load weights file success')
def initialize(self): ''' re-initialize a trained model :param model: given a model :return: a new model which has replaced the original CuDNNLSTM with LSTM ''' session = K.get_session() new_model = Sequential() for i in range(len(self.model.layers)): if not self.model.layers[i].get_config()['name'].find( 'batch_normalization') != -1: for v in self.model.layers[i].__dict__: v_arg = getattr(self.model.layers[i], v) if hasattr(v_arg, 'initializer'): initializer_method = getattr(v_arg, 'initializer') initializer_method.run(session=session) print('reinitializing layer {}.{}'.format( self.model.layers[i].name, v)) print(new_model.summary()) new_model.compile(loss=self.model.loss, optimizer=self.model.optimizer) return new_model
def save_to_pb(keras_model, export_path): """ Save keras model to protobuf for Tensorflow Serving. Source: https://medium.com/@johnsondsouza23/export-keras-model-to-protobuf-for-tensorflow-serving-101ad6c65142 Parameters ---------- keras_model: Keras model instance export_path: str """ # Set the learning phase to Test since the model is already trained. K.set_learning_phase(0) # 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()
def batch_eval(model, test_dataset, file_name='./result.npy', max_size=1000000): """ batch evaluation on test datset and save result to numpy file :param model: :param test_dataset: :param file_name: :param max_size: :return: """ iter_test = test_dataset.make_one_shot_iterator() next_element = iter_test.get_next() result = np.array([]) labels = np.array([]) game_ids = np.array([]) while True: try: if result.size > max_size: break x, y = K.get_session().run(next_element) p = model.predict(x) labels = np.append(labels, np.array(y).reshape(p.size)) result = np.append(result, np.array(p).reshape(p.size)) game_ids = np.append(game_ids, np.array(x['sourceGameId']).reshape(p.size)) except tf.errors.OutOfRangeError: break pairs = np.stack((labels, result, game_ids), axis=-1) np.save(file_io.FileIO(file_name, 'wb'), np.array(pairs), allow_pickle=False) print("DONE!")