def apply_2d_or_3d_cnn(model_object, radar_image_matrix, sounding_matrix=None, num_examples_per_batch=100, verbose=False, return_features=False, feature_layer_name=None): """Applies CNN to either 2-D or 3-D radar images (and possibly soundings). :param model_object: Trained instance of `keras.models.Model` or `keras.models.Sequential`. :param radar_image_matrix: E-by-M-by-N-by-C numpy array of storm-centered radar images. :param sounding_matrix: [may be None] numpy array (E x H_s x F_s) of storm-centered sounding. :param num_examples_per_batch: Number of examples per batch. Will apply CNN to this many examples at once. If `num_examples_per_batch is None`, will apply CNN to all examples at once. :param verbose: Boolean flag. If True, will print progress messages. :param return_features: Boolean flag. If True, this method will return features (activations of an intermediate layer). If False, this method will return probabilistic predictions. :param feature_layer_name: [used only if return_features = True] Name of layer for which features will be returned. If return_features = True... :return: feature_matrix: E-by-Z numpy array of features, where Z = number of outputs from the given layer. If return_features = False... :return: class_probability_matrix: E-by-K numpy array of class probabilities. class_probability_matrix[i, k] is the forecast probability that the [i]th storm object belongs to the [k]th class. Classes are mutually exclusive and collectively exhaustive, so the sum across each row is 1. """ dl_utils.check_radar_images(radar_image_matrix=radar_image_matrix, min_num_dimensions=4, max_num_dimensions=5) num_examples = radar_image_matrix.shape[0] if sounding_matrix is not None: dl_utils.check_soundings(sounding_matrix=sounding_matrix, num_examples=num_examples) if num_examples_per_batch is None: num_examples_per_batch = num_examples + 0 else: error_checking.assert_is_integer(num_examples_per_batch) error_checking.assert_is_greater(num_examples_per_batch, 0) num_examples_per_batch = min([num_examples_per_batch, num_examples]) error_checking.assert_is_boolean(verbose) error_checking.assert_is_boolean(return_features) if return_features: model_object_to_use = model_to_feature_generator( model_object=model_object, feature_layer_name=feature_layer_name) else: model_object_to_use = model_object output_matrix = None for i in range(0, num_examples, num_examples_per_batch): this_first_index = i this_last_index = min( [i + num_examples_per_batch - 1, num_examples - 1]) these_indices = numpy.linspace(this_first_index, this_last_index, num=this_last_index - this_first_index + 1, dtype=int) if verbose: print('Applying model to examples {0:d}-{1:d} of {2:d}...').format( this_first_index + 1, this_last_index + 1, num_examples) if sounding_matrix is None: these_outputs = model_object_to_use.predict( radar_image_matrix[these_indices, ...], batch_size=len(these_indices)) else: these_outputs = model_object_to_use.predict( [ radar_image_matrix[these_indices, ...], sounding_matrix[these_indices, ...] ], batch_size=len(these_indices)) if output_matrix is None: output_matrix = these_outputs + 0. else: output_matrix = numpy.concatenate((output_matrix, these_outputs), axis=0) if verbose: print 'Have applied model to all {0:d} examples!'.format(num_examples) if return_features: return output_matrix return _binary_probabilities_to_matrix(output_matrix)
def apply_2d3d_cnn(model_object, reflectivity_matrix_dbz, azimuthal_shear_matrix_s01, sounding_matrix=None, num_examples_per_batch=100, verbose=False, return_features=False, feature_layer_name=None): """Applies CNN to both 2-D and 3-D radar images (and possibly soundings). M = number of rows in each reflectivity image N = number of columns in each reflectivity image :param model_object: Trained instance of `keras.models.Model` or `keras.models.Sequential`. :param reflectivity_matrix_dbz: numpy array (E x M x N x H_r x 1) of storm-centered reflectivity images. :param azimuthal_shear_matrix_s01: numpy array (E x 2M x 2N x C) of storm-centered azimuthal-shear images. :param sounding_matrix: See doc for `apply_2d_or_3d_cnn`. :param num_examples_per_batch: Same. :param verbose: Same. :param return_features: Same. :param feature_layer_name: Same. If return_features = True... :return: feature_matrix: See doc for `apply_2d_or_3d_cnn`. If return_features = False... :return: class_probability_matrix: See doc for `apply_2d_or_3d_cnn`. """ dl_utils.check_radar_images(radar_image_matrix=reflectivity_matrix_dbz, min_num_dimensions=5, max_num_dimensions=5) dl_utils.check_radar_images(radar_image_matrix=azimuthal_shear_matrix_s01, min_num_dimensions=4, max_num_dimensions=4) if sounding_matrix is not None: dl_utils.check_soundings(sounding_matrix=sounding_matrix, num_examples=reflectivity_matrix_dbz.shape[0]) dl_utils.check_soundings( sounding_matrix=sounding_matrix, num_examples=azimuthal_shear_matrix_s01.shape[0]) num_examples = reflectivity_matrix_dbz.shape[0] if num_examples_per_batch is None: num_examples_per_batch = num_examples + 0 else: error_checking.assert_is_integer(num_examples_per_batch) error_checking.assert_is_greater(num_examples_per_batch, 0) num_examples_per_batch = min([num_examples_per_batch, num_examples]) error_checking.assert_is_boolean(verbose) error_checking.assert_is_boolean(return_features) if return_features: model_object_to_use = model_to_feature_generator( model_object=model_object, feature_layer_name=feature_layer_name) else: model_object_to_use = model_object output_matrix = None for i in range(0, num_examples, num_examples_per_batch): this_first_index = i this_last_index = min( [i + num_examples_per_batch - 1, num_examples - 1]) these_indices = numpy.linspace(this_first_index, this_last_index, num=this_last_index - this_first_index + 1, dtype=int) if verbose: print('Applying model to examples {0:d}-{1:d} of {2:d}...').format( this_first_index + 1, this_last_index + 1, num_examples) if sounding_matrix is None: these_outputs = model_object_to_use.predict( [ reflectivity_matrix_dbz[these_indices, ...], azimuthal_shear_matrix_s01[these_indices, ...] ], batch_size=len(these_indices)) else: these_outputs = model_object_to_use.predict( [ reflectivity_matrix_dbz[these_indices, ...], azimuthal_shear_matrix_s01[these_indices, ...], sounding_matrix[these_indices, ...] ], batch_size=len(these_indices)) if output_matrix is None: output_matrix = these_outputs + 0. else: output_matrix = numpy.concatenate((output_matrix, these_outputs), axis=0) if verbose: print 'Have applied model to all {0:d} examples!'.format(num_examples) if return_features: return output_matrix return _binary_probabilities_to_matrix(output_matrix)
def apply_cnn_soundings_only( model_object, sounding_matrix, num_examples_per_batch=100, verbose=False, return_features=False, feature_layer_name=None): """Applies CNN to soundings only. :param model_object: See doc for `apply_2d_or_3d_cnn`. :param sounding_matrix: numpy array (E x H_s x F_s) of storm-centered soundings. :param num_examples_per_batch: See doc for `apply_2d_or_3d_cnn`. :param verbose: Same. :param return_features: Same. :param feature_layer_name: Same. If return_features = True... :return: feature_matrix: See doc for `apply_2d_or_3d_cnn`. If return_features = False... :return: class_probability_matrix: See doc for `apply_2d_or_3d_cnn`. """ num_examples = sounding_matrix.shape[0] if sounding_matrix is not None: dl_utils.check_soundings( sounding_matrix=sounding_matrix, num_examples=num_examples) if num_examples_per_batch is None: num_examples_per_batch = num_examples + 0 else: error_checking.assert_is_integer(num_examples_per_batch) error_checking.assert_is_greater(num_examples_per_batch, 0) num_examples_per_batch = min([num_examples_per_batch, num_examples]) error_checking.assert_is_boolean(verbose) error_checking.assert_is_boolean(return_features) if return_features: model_object_to_use = model_to_feature_generator( model_object=model_object, feature_layer_name=feature_layer_name) else: model_object_to_use = model_object output_matrix = None for i in range(0, num_examples, num_examples_per_batch): this_first_index = i this_last_index = min( [i + num_examples_per_batch - 1, num_examples - 1] ) these_indices = numpy.linspace( this_first_index, this_last_index, num=this_last_index - this_first_index + 1, dtype=int) if verbose: print(( 'Applying model to examples {0:d}-{1:d} of {2:d}...' ).format( this_first_index + 1, this_last_index + 1, num_examples )) these_outputs = model_object_to_use.predict( sounding_matrix[these_indices, ...], batch_size=len(these_indices) ) if output_matrix is None: output_matrix = these_outputs + 0. else: output_matrix = numpy.concatenate( (output_matrix, these_outputs), axis=0 ) if verbose: print('Have applied model to all {0:d} examples!'.format(num_examples)) if return_features: return output_matrix if len(output_matrix.shape) == 2 and output_matrix.shape[1] == 1: output_matrix = output_matrix[:, 0] if len(output_matrix.shape) == 1: output_matrix = dl_utils.event_probs_to_multiclass(output_matrix) return output_matrix