def save2file(self, model: keras.models.Model): model_json = model.to_json() with open( os.path.join(self.context.model_dir, self.context.model_name), 'w') as f: f.write(model_json) model.save_weights( os.path.join(self.context.model_dir, self.context.weight_name))
def callback( self, kmodel: keras.models.Model, epoch: int = None, batch: int = None, ) -> None: print('SAVING: %s' % self._url) kmodel.save_weights(self._url, overwrite=True)
def save_model(model: keras.models.Model, filepath: str) -> None: """Saves model to serialized file. Args: model: Keras model object. filepath: Filepath to which model is saved. Returns: None. """ _logger.debug("Save model to {}".format(filepath)) model.save(filepath, overwrite=True)
def save(directory: str, name: str, active_model: keras.models.Model, target_model: keras.models.Model, memory=None): if not os.path.isdir(directory): os.mkdir(directory) active_model.save("{}/{}_active.h5f".format(directory, name)) target_model.save("{}/{}_target.h5f".format(directory, name)) if memory is not None: with open("{}/{}_memory.obj".format(directory, name), 'wb') as handler: pickle.dump(memory, handler, pickle.HIGHEST_PROTOCOL)
def pick_model_weights(model: keras.models.Model, dataset_name, path='../output/Models/Weights'): model_names = [] for path in glob.glob('{}/{}_*.h5'.format(path, dataset_name)): model_names.append(path) print('Please enter you model number form list below:') for i, path in enumerate(model_names): print('{}. {}'.format(i + 1, path)) model_number = int(input('?')) - 1 model.load_weights(model_names[model_number]) return model
def fit(self, model: keras.models.Model, X, y): tensor_board = keras.callbacks.TensorBoard( log_dir=self.context.tensor_board_dir, histogram_freq=1) early_stopping = keras.callbacks.EarlyStopping(monitor='val_loss', mode='auto', patience=20) model.fit(X, y, batch_size=self.context.batch_size, epochs=self.context.epochs, validation_split=self.context.validation_split, callbacks=[tensor_board, early_stopping]) return model
def save_model(m: keras.models.Model, p: str = None, *args, **kwargs): if p is None: p = os.path.join(nb_dir, '.train_result') os.makedirs(p, exist_ok=True) window = kwargs.pop('window', None) days = kwargs.pop('days', None) stockcode = kwargs.pop('stockcode', None) if stockcode is None or window is None or days is None: raise ValueError() p = _get_model_file_path(stockcode, window, days, p) os.makedirs(os.path.dirname(p), exist_ok=True) m.save(p) return p
def train(model: keras.models.Model, config: TrainingConfig): training_generator, validation_generator = config.get_generators() callback_list = list() if config.use_tensorboard: print("using tensorboard") tb_callback = callbacks.TensorBoard(log_dir=config.tensorboard_log_dir, write_graph=False, update_freq=5000 ) callback_list.append(tb_callback) if config.reduce_lr_on_plateau: print("reducing learning rate on plateau") lr_callback = callbacks.ReduceLROnPlateau( factor=config.reduce_lr_on_plateau_factor, patience=config.reduce_lr_on_plateau_patience, cooldown=config.reduce_lr_on_plateau_cooldown, min_delta=config.reduce_lr_on_plateau_delta ) callback_list.append(lr_callback) if config.save_colored_image_progress: print("saving progression every {} epochs".format(config.image_progression_period)) op_callback = OutputProgress(config.image_paths_to_save, config.dim_in, config.image_progression_log_dir, every_n_epochs=config.image_progression_period) callback_list.append(op_callback) if config.periodically_save_model: print("saving model every {} epcohs".format(config.periodically_save_model_period)) p_save_callback = callbacks.ModelCheckpoint(config.periodically_save_model_path, period=config.periodically_save_model_period) callback_list.append(p_save_callback) if config.save_best_model: print("saving best model") best_save_callback = callbacks.ModelCheckpoint(config.save_best_model_path, save_best_only=True) callback_list.append(best_save_callback) model.fit_generator(generator=training_generator, validation_data=validation_generator, use_multiprocessing=True, workers=config.n_workers, max_queue_size=config.queue_size, verbose=1, epochs=config.n_epochs, callbacks=callback_list)
def check_model_precision(self, model: keras.models.Model, state: "State") -> keras.models.Model: """ Check the model's precision. If this is a new model, then Rewrite an existing model's training precsion mode from mixed-float16 to float32 or vice versa. This is not easy to do in keras, so we edit the model's config to change the dtype policy for compatible layers. Create a new model from this config, then port the weights from the old model to the new model. Parameters ---------- model: :class:`keras.models.Model` The original saved keras model to rewrite the dtype state: ~:class:`plugins.train.model._base.model.State` The State information for the model Returns ------- :class:`keras.models.Model` The original model with the datatype updated """ if get_backend() == "amd": # Mixed precision not supported on amd return model if self.use_mixed_precision and not state.mixed_precision_layers: # Switching to mixed precision on a model which was started in FP32 prior to the # ability to switch between precisions on a saved model is not supported as we # do not have the compatible layer names logger.warning("Switching from Full Precision to Mixed Precision is not supported on " "older model files. Reverting to Full Precision.") return model config = model.get_config() if not self.use_mixed_precision and not state.mixed_precision_layers: # Switched to Full Precision, get compatible layers from model if not already stored state.add_mixed_precision_layers(self._get_mixed_precision_layers(config["layers"])) self._switch_precision(config["layers"], state.mixed_precision_layers) new_model = keras.models.Model().from_config(config) new_model.set_weights(model.get_weights()) logger.info("Mixed precision has been updated from '%s' to '%s'", not self.use_mixed_precision, self.use_mixed_precision) del model return new_model
def train(self, full_model: keras.models.Model, input_data, epochs=100, validation_data=None, validation_split=None, batch_size=32, **kwargs): """Train the given autoencoder model with the builder's configuration.""" given_input_shape = input_data.shape[1:] if given_input_shape != self.__input_shape: raise ValueError( f"Input shape {given_input_shape} does not match {self.__input_shape}" ) callbacks = [] if self.__early_stopping is not None and ( validation_data is not None or validation_split is not None): callbacks.append( keras.callbacks.EarlyStopping(patience=self.__early_stopping)) return full_model.fit(input_data, input_data, epochs=epochs, batch_size=batch_size, callbacks=callbacks, validation_data=validation_data, validation_split=validation_split, shuffle=True, **kwargs)
def callback( self, kmodel: keras.models.Model, epoch: int = None, batch: int = None, ) -> None: kmodel.stop_training = True
def _get_model_weight_metrics(model: keras.models.Model, obs_horizon, n_channels, n_noise_channels): weight_matrix = model.get_weights()[0] W_variance = np.var(weight_matrix) weight_tensor = food_search_env._unflatten_weight_matrix(weight_matrix, obs_horizon, n_channels, n_actions=4) noise_part = weight_tensor[:, :, -n_noise_channels:, :] W_noise_part_variance = np.var(noise_part) outside_part = np.r_[ weight_tensor[:, 0, :].ravel(), weight_tensor[:, -1, :].ravel(), weight_tensor[0, 1:-1, :].ravel(), weight_tensor[-1, 1:-1, :].ravel() ] W_outside_part_variance = np.var(outside_part) inside_part = weight_tensor[1:-1, 1:-1, :] W_inside_part_variance = np.var(inside_part) # n_inside = inside_part.size # n_outside = weight_matrix.size # assert n_outside == weight_tensor.size # n_total = n_inside + n_outside # W_outside_part_variance = (n_total / n_inside)*W_variance - (n_outside/n_inside) * W_inside_part_variance return { 'W_variance': W_variance, 'W_noise_part_variance': W_noise_part_variance, 'W_outside_part_variance': W_outside_part_variance, 'W_inside_part_variance': W_inside_part_variance }
def predict_and_plot_single_image(img_path: str, model: keras.models.Model, output: Union[Literal["print"], str] = "") \ -> None: """ predict_and_plot_single_image from file and displays the figure takes a file path and a keras-retinanet loaded model, predicts the instances in the image and displays the results :param img_path: file path of the image to use for prediction :type img_path: str :param model: Keras retinanet loaded model :type model: keras.models.Model :param output: Can be either literally "print" or a file path to where the figure will be saved, defaults to "" :type output: Union[Literal[, optional """ # Output is displaying the image as a side effect # load image image = read_image_bgr(img_path) # copy to draw on draw = image.copy() draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB) # Plot the image without boxes pyplot.figure(figsize=(10, 10)) pyplot.axis('off') pyplot.imshow(draw) # TODO make it such that the figures are shown side by side and # TODO add argument for figure size if output == "print": pyplot.show(output) else: pyplot.savefig(output) # preprocess image for network image = preprocess_image(image) image, scale = resize_image(image) # process image start = time.time() boxes, scores, labels = model.predict_on_batch( np.expand_dims(image, axis=0)) print("processing time: ", time.time() - start) # correct for image scale boxes /= scale print(scores) # visualize detections plt = plot_predictions(img=draw, boxes=boxes, scores=scores, labels=labels, label_dict=LABELS_TO_NAMES) plt.show()
def test_online(model: keras.models.Model): cam = cv2.VideoCapture(0) while True: ret_val, img = cam.read() if ret_val: pred = model.predict(np.array([cv2.resize(img, (224, 224))])) pred = keras.applications.vgg16.decode_predictions(pred) labels = [label[1] for label in pred[0]] show_image("Webcam", img, labels)
def __init__(self, saved_model: keras.models.Model, switch_sides: bool) -> None: logger.debug("Initializing: %s (saved_model: %s, switch_sides: %s)", self.__class__.__name__, saved_model, switch_sides) self._config = saved_model.get_config() self._input_idx = 1 if switch_sides else 0 self._output_idx = 0 if switch_sides else 1 self._input_names = [inp[0] for inp in self._config["input_layers"]] self._model = self._make_inference_model(saved_model) logger.debug("Initialized: %s", self.__class__.__name__)
def save_model(model: keras.models.Model, model_type: int): if model_type == ann_normalize.TYPE_FCN: model.save(fcn_norm_fix_model_path) elif model_type == ann_normalize.TYPE_CONV: model.save(conv_norm_fix_model_path) else: model.save(conv_bn_norm_fix_model_path)
def save_normed_model(model: keras.models.Model, model_type: int): if model_type == TYPE_FCN: model.save(fcn_norm_model_path) elif model_type == TYPE_CONV: model.save(conv_norm_model_path) else: model.save(conv_bn_norm_model_path)
def convert(self, model: keras.models.Model, input_orders: List[Order] = None) -> Graph: """ Convert kerasmodel into WebDNN IR Graph. Currently, only TensorFlow backend is supported. Args: model (`keras.models.Model`): keras model input_orders (list of `webdnn.graph.order.Order`): order of input tensors. If `None` is passed, default order (`OrderNC` for 2D, `OrderNTC` for 3D, `OrderNHWC` for 4D) is used. If `input_orders=None`, default orders are assigned to all input tensors. If `input_orders[0]=None`, only first input tensor are converted with the default order. Returns: (:class:`~webdnn.graph.graph.Graph`): WebDNN IR Graph """ if not model.built: model.build(None) self._convert_tensors(model.inputs, input_orders) for depth in sorted(list(model.nodes_by_depth.keys()), reverse=True): for node in model.nodes_by_depth[depth]: self.convert_operator(node.outbound_layer) # Check that all output tensors from current layer are converted into WebDNN Variable for tensor in node.output_tensors: if not self.has_variable(tensor): raise AssertionError( f"[KerasConverter] {node.outbound_layer} outputs {tensor}, but it was not converted into " f"WebDNN Variable by {self._handler_map[self.__class__.__name__][self.serialize_operator_type(node.outbound_layer)]}" ) return Graph([ self.get_variable(t) for t in _to_list(self.get_input_tensor(model)) ], [ self.get_variable(t) for t in _to_list(self.get_output_tensor(model)) ])
def evaluate_model(model: keras.models.Model, input_data, cr_codes, classes=DEFAULT_CLASSES): ''' Convenience function to quickly evaluate model performance ''' predictions = model.predict(input_data) params = dict(epochs=0, lr=0) result = Result.from_predictions(predictions, cr_codes, params, 'AUTO_EVAL', '') print(result.describe())
def rmse_score(self, model: keras.models.Model, Xtrain, Xtest, ytrain, ytest, index=0): """ root mean squared error """ pred_train, pred_test = model.predict(Xtrain), model.predict(Xtest) pred_list_train = np.array([pred_train[:, index]]) pred_list_test = np.array([pred_test[:, index]]) from sklearn.metrics import mean_squared_error rmse_train = [ np.sqrt(mean_squared_error(ytrain[:, index], _y)) for _y in pred_list_train ] rmse_test = [ np.sqrt(mean_squared_error(ytest[:, index], _y)) for _y in pred_list_test ] return rmse_train, rmse_test
def permutation_importance(model: keras.models.Model, x_que: np.ndarray, x_pro: np.ndarray, y: np.ndarray, fn: dict, n_trials: int) -> pd.DataFrame: """ Calculate model feature importance via random permutations of feature values :param model: model to evaluate :param x_que: pre-processed questions data :param x_pro: pre-processed professionals data :param y: target labels :param fn: dict with feature names of both questions and professionals :param n_trials: number of shuffles for each feature :return: Pandas DataFrame with importance of each feature """ # model performance on normal, non-shuffled data base_loss, base_acc = model.evaluate([x_que, x_pro], y) losses = [] for i, name in enumerate(fn['que'] + fn['pro']): loss = 0 for j in range(n_trials): x_que_i, x_pro_i = copy.deepcopy(x_que), copy.deepcopy(x_pro) if name in fn['que']: x_que_i[:, i] = shuffle(x_que_i[:, i]) else: x_pro_i[:, i - len(fn['que'])] = shuffle( x_pro_i[:, i - len(fn['que'])]) loss += model.evaluate([x_que_i, x_pro_i], y, verbose=0)[0] losses.append(loss / n_trials) fi = pd.DataFrame({'importance': losses}, index=fn['que'] + fn['pro']) fi.sort_values(by='importance', inplace=True, ascending=True) fi['importance'] -= base_loss return fi
def predict_with_model(tokenizer, text, model: keras.models.Model): input_text = [i for i in text if chinese_regex.match(i)] input_text = input_text[:tokenizer.max_length-2] input_token = tokenizer.tokenize(input_text) input_x = keras.preprocessing.sequence.pad_sequences([input_token], maxlen=tokenizer.max_length, padding='post') predict_idx = model.predict(input_x)[0].argmax(1) labels = tokenizer.label_de_tokenize(predict_idx, length=len(input_text)) final = '' for i in range(len(input_text)): final += input_text[i] if labels[i] != 'O': final += '{}'.format(labels[i]) return final
def test_offline(model: keras.models.Model): images = load_data() pred_images = images_to_pred_images(images) pred = model.predict(np.array(pred_images)) pred = keras.applications.vgg16.decode_predictions(pred) labels = list() for p in pred: l = list() for label in p: l.append(label[1]) labels.append(l) show_images("image", images, labels)
def predict_chunked_image(img_path: str, model: keras.models.Model, chunk_sizes: Tuple[int, int] = (500, 500)): print("Reading Image") image = read_image_bgr(img_path) print("Processing Image") image = preprocess_image(image) img_gen = chunk_image_generator(image, chunk_size=chunk_sizes, displacement=chunk_sizes) results = [] i = 0 print("Predicting") for x_start, y_start, chunk in img_gen: chunk, scale = resize_image(chunk) boxes, scores, labels = model.predict_on_batch( np.expand_dims(chunk, axis=0)) boxes /= scale out_dict = { 'x_start': x_start, 'y_start': y_start, 'scale': scale, 'boxes': boxes, 'scores': scores, 'labels': labels } i += 1 print("Done with image {i} at x:{x}, y:{y}".format(i=i, x=x_start, y=y_start)) results.append(out_dict) return (results)
def predict_gene(model: keras.models.Model, eder: Simple_ED, maxlen, pre_str=None, str_len=None): if pre_str == None: seed = np.random.randint(0, eder.text_len - maxlen) pre_str = eder.text[seed:seed + maxlen] if str_len == None: str_len = maxlen * 10 gene_str = [] prefix = pre_str for i in range(str_len): code = eder.encode(prefix) pred = model.predict(code.reshape((1, ) + code.shape), verbose=0) ci = sample(pred[0]) c = eder.characters[ci] gene_str.append(c) prefix = prefix[1:] + c print("pre_str:{}\n{}".format(pre_str, ''.join(gene_str)))
def get_and_save_model_output_on_batches( model: keras.models.Model, batches: image.DirectoryIterator, model_output_to="path/to/output/to.bc", labels_output_to="optional/path/to/output/onehot/labels/to.bc", num_batches_to_save: int = None): # valid_batches = ut.get_batches(path_data_valid, shuffle=False) if num_batches_to_save is None: num_batches_to_save = int(batches.n / batches.batch_size) if model_output_to == "path/to/output/to.bc": print( 'Must provide a path to output data to, such as: path_data_model + "valid_model_out.bc"' ) output_labels = True if labels_output_to == "optional/path/to/output/onehot/labels/to.bc": output_labels = False model_out, labels_out = [], [] for i in range(num_batches_to_save): print(f"batch {i} of {num_batches_to_save}") imgs, labels = next(batches) model_out.append(model.predict( imgs, batch_size=8, verbose=1)) # May want to change batch_size on a powerful GPU! labels_out.append(labels) model_out = np.concatenate(model_out, axis=0) labels_out = np.concatenate( labels_out, axis=0) # This is automatically converted to one-hot! print( f"\nmodel_out.shape, labels_out.shape is [[{model_out.shape, labels_out.shape}]]" ) save_array(model_output_to, model_out) print(f"Saved model's output to {model_output_to}") if output_labels: save_array(labels_output_to, labels_out) print(f"Saved labels's in one-hot format to {labels_output_to}")
def compileModel(model:keras.models.Model): model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['binary_accuracy', 'accuracy'])
def test_model(model: keras.models.Model, dataX: np.array, dataY: np.array) -> float: preds = model.predict(dataX, batch_size=1, verbose=1) preds = np.argmax(preds, axis=-1) accu = np.sum(preds == np.argmax(dataY, axis=-1)) / len(preds) return accu
def eval_dist(dp: utils.DataPoint, model: keras.models.Model, datagen: keras.preprocessing.image.ImageDataGenerator): """returns the distance between predicted location and true location""" x = preproc_x(dp.x, datagen) y_pred = model.predict(x, batch_size=1).squeeze() return K.eval(mode_distance(dp.y, y_pred))
def track_lif(lif_path: str, out_path: str, model: keras.models.Model) -> None: """ Applies ML model (model object) to everything in the lif file. This will write a trackmate xml file via the method tm_xml.write_xml(), and save output tiff image stacks from the lif file. Args: lif_path (str): Path to the lif file out_path (str): Path to output directory model (str): A trained keras.models.Model object Returns: None """ print("loading LIF") lif_data = LifFile(lif_path) print("Iterating over lif") for image in lif_data.get_iter_image(): folder_path = "/".join(str(image.path).strip("/").split('/')[1:]) path = folder_path + "/" + str(image.name) name = image.name if os.path.exists(os.path.join(out_path, path + '.tif.xml')) \ or os.path.exists(os.path.join(out_path, path + '.tif.trackmate.xml')): print(str(path) + '.xml' + ' exists, skipping') continue make_dirs = os.path.join(out_path, folder_path) if not os.path.exists(make_dirs): os.makedirs(make_dirs) print("Processing " + str(path)) start = time.time() # initialize XML creation for this file tm_xml = trackmateXML() i = 1 image_out = image.get_frame() # Initialize the output image images_to_append = [] for frame in image.get_iter_t(): images_to_append.append(frame) np_image = np.asarray(frame.convert('RGB')) image_array = np_image[:, :, ::-1].copy() tm_xml.filename = name + '.tif' tm_xml.imagepath = os.path.join(out_path, folder_path) if tm_xml.nframes < i: # set nframes to the maximum i tm_xml.nframes = i tm_xml.frame = i # preprocess image for network image_array = preprocess_image(image_array) image_array, scale = resize_image(image_array) # process image boxes, scores, labels = model.predict_on_batch( np.expand_dims(image_array, axis=0)) # correct for image scale boxes /= scale # filter the detection boxes pre_passed_boxes = [] pre_passed_scores = [] for box, score, label in zip(boxes[0], scores[0], labels[0]): if score >= 0.2: pre_passed_boxes.append(box.tolist()) pre_passed_scores.append(score.tolist()) passed_boxes, passed_scores = filter_boxes( in_boxes=pre_passed_boxes, in_scores=pre_passed_scores, _passed_boxes=[], _passed_scores=[]) # These are necessary print("found " + str(len(passed_boxes)) + " cells in " + str(path) + " frame " + str(i)) # tell the trackmate writer to add the passed_boxes to the final output xml tm_xml.add_frame_spots(passed_boxes, passed_scores) i += 1 # write the image to trackmate, prepare for next image print("processing time: ", time.time() - start) tm_xml.write_xml() image_out.save(os.path.join(out_path, path + '.tif'), format="tiff", append_images=images_to_append[1:], save_all=True, compression='tiff_lzw')