def on_batch_end(self, n_update, logs=None): self.cum_update += 1 if self.epoch_count + self.reload_epoch < self.start_sampling_on_epoch: return elif self.cum_update % self.each_n_updates != 0: return # Evaluate on each set separately for s in self.set_name: if self.beam_search: params_prediction = {'max_batch_size': self.batch_size, 'n_parallel_loaders': self.extra_vars[ 'n_parallel_loaders'], 'predict_on_sets': [s], 'n_samples': self.n_samples, 'pos_unk': False} params_prediction.update(checkDefaultParamsBeamSearch(self.extra_vars)) predictions, truths, sources = self.model_to_eval.predictBeamSearchNet(self.ds, params_prediction) else: params_prediction = {'batch_size': self.batch_size, 'n_parallel_loaders': self.extra_vars[ 'n_parallel_loaders'], 'predict_on_sets': [s], 'n_samples': self.n_samples, } # Convert predictions postprocess_fun = None if self.is_3DLabel: postprocess_fun = [self.ds.convert_3DLabels_to_bboxes, self.extra_vars[s]['references_orig_sizes']] predictions = self.model_to_eval.predictNet(self.ds, params_prediction, postprocess_fun=postprocess_fun) if self.print_sources: if self.in_pred_idx is not None: sources = [srcs[self.in_pred_idx][0] for srcs in sources] sources = decode_predictions_beam_search(sources, self.index2word_x, pad_sequences=True, verbose=self.verbose) if s in predictions: if params_prediction['pos_unk']: samples = predictions[s][0] alphas = predictions[s][1] heuristic = self.extra_vars['heuristic'] else: samples = predictions[s] alphas = None heuristic = None predictions = predictions[s] if self.is_text: if self.out_pred_idx is not None: samples = samples[self.out_pred_idx] # Convert predictions into sentences if self.beam_search: predictions = decode_predictions_beam_search(samples, self.index2word_y, alphas=alphas, x_text=sources, heuristic=heuristic, mapping=self.extra_vars.get('mapping', None), verbose=self.verbose) else: predictions = decode_predictions(samples, 1, self.index2word_y, self.sampling_type, verbose=self.verbose) truths = decode_predictions_one_hot(truths, self.index2word_y, verbose=self.verbose) # Apply detokenization function if needed if self.extra_vars.get('apply_detokenization', False): if self.print_sources: sources = map(self.extra_vars['detokenize_f'], sources) predictions = map(self.extra_vars['detokenize_f'], predictions) truths = map(self.extra_vars['detokenize_f'], truths) # Write samples if self.print_sources: # Write samples for i, (source, sample, truth) in list(enumerate(zip(sources, predictions, truths))): if sys.version_info.major == 2: source = str(source.encode('utf-8')) sample = str(sample.encode('utf-8')) truth = str(truth.encode('utf-8')) print("Source (%d): %s" % (i, source)) print("Hypothesis (%d): %s" % (i, sample)) print("Reference (%d): %s" % (i, truth)) print("") else: for i, (sample, truth) in list(enumerate(zip(predictions, truths))): if sys.version_info.major == 2: sample = str(sample.encode('utf-8')) truth = str(truth.encode('utf-8')) print("Hypothesis (%d): %s" % (i, sample)) print("Reference (%d): %s" % (i, truth)) print("")
def on_batch_end(self, n_update, logs={}): self.cum_update += 1 if self.epoch_count + self.reload_epoch < self.start_sampling_on_epoch: return elif self.cum_update % self.each_n_updates != 0: return # Evaluate on each set separately for s in self.set_name: # Apply model predictions params_prediction = { 'batch_size': self.batch_size, 'n_parallel_loaders': self.extra_vars['n_parallel_loaders'], 'predict_on_sets': [s], 'n_samples': self.n_samples, 'pos_unk': False, 'heuristic': 0, 'mapping': None } if self.beam_search: params_prediction.update( checkDefaultParamsBeamSearch(self.extra_vars)) predictions, truths, sources = self.model_to_eval.predictBeamSearchNet( self.ds, params_prediction) else: # Convert predictions postprocess_fun = None if self.is_3DLabel: postprocess_fun = [ self.ds.convert_3DLabels_to_bboxes, self.extra_vars[s]['references_orig_sizes'] ] predictions = self.model_to_eval.predictNet( self.ds, params_prediction, postprocess_fun=postprocess_fun) if self.print_sources: if self.in_pred_idx is not None: sources = [srcs for srcs in sources[0][self.in_pred_idx]] sources = decode_predictions_beam_search(sources, self.index2word_x, pad_sequences=True, verbose=self.verbose) if s in predictions: if params_prediction['pos_unk']: samples = predictions[s][0] alphas = predictions[s][1] heuristic = params_prediction['heuristic'] else: samples = predictions[s] alphas = None heuristic = None predictions = predictions[s] if self.is_text: if self.out_pred_idx is not None: samples = samples[self.out_pred_idx] # Convert predictions into sentences if self.beam_search: predictions = decode_predictions_beam_search( samples, self.index2word_y, alphas=alphas, x_text=sources, heuristic=heuristic, mapping=params_prediction['mapping'], verbose=self.verbose) else: predictions = decode_predictions(samples, 1, self.index2word_y, self.sampling_type, verbose=self.verbose) truths = decode_predictions_one_hot(truths, self.index2word_y, verbose=self.verbose) # Write samples if self.print_sources: # Write samples for i, (source, sample, truth) in enumerate( zip(sources, predictions, truths)): print("Source (%d): %s" % (i, source)) print("Hypothesis (%d): %s" % (i, sample)) print("Reference (%d): %s" % (i, truth)) print("") else: for i, (sample, truth) in enumerate(zip(predictions, truths)): print("Hypothesis (%d): %s" % (i, sample)) print("Reference (%d): %s" % (i, truth)) print("")
def on_batch_end(self, n_update, logs=None): self.cum_update += 1 if self.epoch_count + self.reload_epoch < self.start_sampling_on_epoch: return elif self.cum_update % self.each_n_updates != 0: return # Evaluate on each set separately for s in self.set_name: if self.beam_search: params_prediction = {'max_batch_size': self.batch_size, 'n_parallel_loaders': self.extra_vars['n_parallel_loaders'], 'predict_on_sets': [s], 'n_samples': self.n_samples, 'pos_unk': False} params_prediction.update(checkDefaultParamsBeamSearch(self.extra_vars)) predictions, truths, sources = self.model_to_eval.predictBeamSearchNet(self.ds, params_prediction) else: params_prediction = {'batch_size': self.batch_size, 'n_parallel_loaders': self.extra_vars['n_parallel_loaders'], 'predict_on_sets': [s], 'n_samples': self.n_samples, 'verbose': self.verbose, } # Convert predictions postprocess_fun = None if self.is_3DLabel: postprocess_fun = [self.ds.convert_3DLabels_to_bboxes, self.extra_vars[s]['references_orig_sizes']] predictions = self.model_to_eval.predictNet(self.ds, params_prediction, postprocess_fun=postprocess_fun) if self.print_sources: if self.in_pred_idx is not None: sources = [srcs[self.in_pred_idx][0] for srcs in sources] sources = decode_predictions_beam_search(sources, self.index2word_x, pad_sequences=True, verbose=self.verbose) if s in predictions: if params_prediction['pos_unk']: samples = predictions[s][0] alphas = predictions[s][1] heuristic = self.extra_vars['heuristic'] else: samples = predictions[s] alphas = None heuristic = None predictions = predictions[s] if self.is_text: if self.out_pred_idx is not None: samples = samples[self.out_pred_idx] # Convert predictions into sentences if self.beam_search: predictions = decode_predictions_beam_search(samples, self.index2word_y, glossary=self.extra_vars.get('glossary', None), alphas=alphas, x_text=sources, heuristic=heuristic, mapping=self.extra_vars.get('mapping', None), verbose=self.verbose) else: predictions = decode_predictions(samples, 1, self.index2word_y, self.sampling_type, verbose=self.verbose) truths = decode_predictions_one_hot(truths, self.index2word_y, verbose=self.verbose) # Apply detokenization function if needed if self.extra_vars.get('apply_detokenization', False): if self.print_sources: sources = list(map(self.extra_vars['detokenize_f'], sources)) predictions = list(map(self.extra_vars['detokenize_f'], predictions)) truths = list(map(self.extra_vars['detokenize_f'], truths)) # Write samples if self.print_sources: # Write samples for i, (source, sample, truth) in list(enumerate(zip(sources, predictions, truths))): if sys.version_info.major == 2: source = str(source.encode('utf-8')) sample = str(sample.encode('utf-8')) truth = str(truth.encode('utf-8')) print("Source (%d): %s" % (i, source)) print("Hypothesis (%d): %s" % (i, sample)) print("Reference (%d): %s" % (i, truth)) print("") else: for i, (sample, truth) in list(enumerate(zip(predictions, truths))): if sys.version_info.major == 2: sample = str(sample.encode('utf-8')) truth = str(truth.encode('utf-8')) print("Hypothesis (%d): %s" % (i, sample)) print("Reference (%d): %s" % (i, truth)) print("")