Title: | Auto Visual Inference with Computer Vision Models |
---|---|
Description: | Provides automated visual inference of residual plots using computer vision models, facilitating diagnostic checks for classical normal linear regression models. |
Authors: | Weihao Li [aut, cre, cph] |
Maintainer: | Weihao Li <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.4.1 |
Built: | 2024-11-19 01:25:15 UTC |
Source: | https://github.com/tengmcing/autovi |
This is the class of auto visual inference,
inherited from bandicoot::BASE. It is an environment
with S3 class bandicoot_oop
.
auto_vi( fitted_model, keras_model = NULL, data = NULL, node_index = 1L, env = new.env(parent = parent.frame()), init_call = sys.call() ) residual_checker( fitted_model, keras_model_name = "vss_phn_32", data = NULL, node_index = 1L, env = new.env(parent = parent.frame()), init_call = sys.call() )
auto_vi( fitted_model, keras_model = NULL, data = NULL, node_index = 1L, env = new.env(parent = parent.frame()), init_call = sys.call() ) residual_checker( fitted_model, keras_model_name = "vss_phn_32", data = NULL, node_index = 1L, env = new.env(parent = parent.frame()), init_call = sys.call() )
fitted_model |
Model. A model object, e.g. |
keras_model |
Keras model. A trained computer vision model. |
data |
Data frame. The data used to fit the model. |
node_index |
Integer. An index indicating which node of the output layer contains the visual signal strength. This is particularly useful when the keras model has more than one output nodes. |
env |
Environment. The instance environment. |
init_call |
Call. Contents of the |
keras_model_name |
Character. A model name to be used by
|
An instance environment.
auto_vi()
: Class constructor, same as AUTO_VI$instantiate()
.
residual_checker()
: Class constructor, same as AUTO_VI$instantiate()
, but
uses the keras_model_name
argument rather than keras_model
.
Direct:
A:
B:
C:
F:
G:
I:
L:
N:
P:
R:
S:
V:
This function will be called after an instance is built. User input will be stored in the environment.
AUTO_VI$..init..(fitted_model, keras_model = NULL, data = NULL, node_index = 1L)
fitted_model |
Model. A model object, e.g. |
keras_model |
Keras model. A trained computer vision model. |
data |
Data frame. The data used to fit the model. |
node_index |
Integer. An index indicating which node of the output layer contains the visual signal strength. This is particularly useful when the keras model has more than one output nodes. |
Return the object itself.
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi
This function returns a string representation of the object.
AUTO_VI$..str..()
A string.
AUTO_VI$..str..() my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi$..str..()
AUTO_VI$..str..() my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi$..str..()
This function computes auxiliary variables
including monotonic measure (measure_monotonic
),
sparse measure (measure_sparse
), splines measure (measure_splines
),
striped measure (measure_striped
), and the number of observation (n
).
Scagnostics are computed using
cassowaryr::sc_monotonic()
, cassowaryr::sc_sparse2()
, cassowaryr::sc_splines()
,
and cassowaryr::sc_striped()
.
If you wish to calculate additional auxiliary variables for your keras
model, please override this method. Ensure that it accepts a data frame
with columns named .fitted
and .resid
as input and returns
a single row tibble.
AUTO_VI$auxiliary(data = seflf$get_fitted_and_resid())
data |
Data frame. A data frame containing variables |
A tibble.
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi$auxiliary()
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi$auxiliary()
This default method gets bootstrapped residuals from a fitted linear model by sampling the observations with replacement then refit the model. User needs to override this method if a different bootstrapping scheme is needed.
AUTO_VI$boot_method( fitted_model = self$fitted_model, data = self$get_data() )
fitted_model |
|
data |
Data frame. The data used to fit the model.
See also |
A tibble with two columns .fitted
and .resid
.
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) null_resid <- my_vi$boot_method() my_vi$plot_resid(null_resid)
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) null_resid <- my_vi$boot_method() my_vi$plot_resid(null_resid)
This function bootstrap the data and refits the model by using
AUTO_VI$boot_method()
, then predicts the visual signal strength
of the bootstrapped residual plots.
AUTO_VI$boot_vss( draws = 100L, fitted_model = self$fitted_model, keras_model = self$keras_model, data = self$get_data(), node_index = 1L, keep_boot_data = FALSE, keep_boot_plot = FALSE, extract_feature_from_layer = NULL )
draws |
Integer. Number of simulation draws. |
fitted_model |
Model. A model object, e.g. |
keras_model |
Keras model. A trained computer vision model. |
data |
Data frame. The data used to fit the model.
See also |
node_index |
Integer. An index indicating which node of the output layer contains the visual signal strength. This is particularly useful when the keras model has more than one output nodes. |
keep_boot_data |
Boolean. Whether to keep the bootstrapped data. |
keep_boot_plot |
Boolean. Whether to keep the bootstrapped plots. |
extract_feature_from_layer |
Character/Integer. A layer name or an integer layer index for extracting features from a layer. |
A tibble.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$boot_vss() }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$boot_vss() }
This function conducts a visual inference
check with a computer vision model. The result will be stored in
self$check_result
.
AUTO_VI$check( null_draws = 100L, boot_draws = 100L, fitted_model = self$fitted_model, keras_model = self$keras_model, null_method = self$null_method, data = self$get_data(), node_index = self$node_index, keep_data = FALSE, keep_plot = FALSE, extract_feature_from_layer = NULL )
null_draws |
Integer. Number of simulation draws for
|
boot_draws |
Integer. Number of simulation draws for
|
fitted_model |
Model. A model object, e.g. |
keras_model |
Keras model. A trained computer vision model. |
null_method |
Function. A method to simulate residuals from the null
hypothesis distribution. For |
data |
Data frame. The data used to fit the model.
See also |
node_index |
Integer. An index indicating which node of the output layer contains the visual signal strength. This is particularly useful when the keras model has more than one output nodes. |
keep_data |
Boolean. Whether to keep the simulated data. |
keep_plot |
Boolean. Whether to keep the simulated plots. |
extract_feature_from_layer |
Character/Integer. A layer name or an integer layer index for extracting features from a layer. |
Return the object itself.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$check() myvi }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$check() myvi }
A list, will be initialized after the method AUTO_VI$check()
is run.
This function conducts principal component analysis for features extracted from keras model.
AUTO_VI$feature_pca( feature = self$check_result$observed, null_feature = self$check_result$null, boot_feature = self$check_result$boot, center = TRUE, scale = TRUE, pattern = "^f_.*$" )
feature |
Dataframe. A data frame where columns represent features and rows represent observations. It should have only one row. |
null_feature |
Dataframe. A data frame where columns represent features and rows represent observations. These features are extracted during the evaluation of null plots. |
boot_feature |
Dataframe. A data frame where columns represent features and rows represent observations. These features are extracted during the evaluation of bootstrapped plots. |
center |
Boolean. Whether to subtract the mean from the feature. |
scale |
Boolean. Whether to divide the feature by its standard deviation. |
pattern |
Character. A regrex pattern to search for features in the
provided DataFrame. See also |
Features need to be extracted while running the method
AUTO_VI$check()
and AUTO_VI$lineup_check()
by providing the argument
extract_feature_from_layer
. Features with zero variance will be ignored
from the analysis. See also stats::prcomp()
. By default, features are
assumed to follow the naming convention
"f_(index)", where index is from one to the number of features.
A tibble of the raw features and the rotated features with
attributes sdev
and rotation
representing the
standard deviation of the principal
components and the rotation matrix respectively.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$lineup_check(extract_feature_from_layer = "global_max_pooling2d") myvi$feature_pca() }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$lineup_check(extract_feature_from_layer = "global_max_pooling2d") myvi$feature_pca() }
This function draws a summary Plot for principal component analysis conducted on extracted features
AUTO_VI$feature_pca_plot( feature_pca = self$feature_pca(), x = PC1, y = PC2, col_by_set = TRUE)
feature_pca |
Dataframe. A data frame containing the rotated features. |
x |
Symbol. The x variable. See also ggplot2::tidyeval. |
y |
Symbol. The y variable. See also ggplot2::tidyeval. |
col_by_set |
Boolena. Whether to color points by sets (observed, null, and boot). |
By default, it will visualize PC2 vs PC1. User can choose to visualize other principal components.
A ggplot
.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$lineup_check(extract_feature_from_layer = "global_max_pooling2d") myvi$feature_pca_plot() }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$lineup_check(extract_feature_from_layer = "global_max_pooling2d") myvi$feature_pca_plot() }
This function gets the data out of a model object
by using stats::model.frame()
if self$data
is NULL
.
AUTO_VI$get_data(fitted_model = self$fitted_model)
fitted_model |
Model. A model object, e.g. |
A tibble.
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi$get_data()
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi$get_data()
This function gets fitted values and residuals
out of a model object by using stats::fitted()
and stats::resid()
.
AUTO_VI$get_fitted_and_resid(fitted_model = self$fitted_model)
fitted_model |
Model. A model object, e.g. |
A tibble.
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi$get_fitted_and_resid()
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi$get_fitted_and_resid()
This function estimates the likelihood of observing the visual signal strength in terms of the bootstrapped distribution and the simulated null distribution, and computes the ratio between these two likelihood.
AUTO_VI$likelihood_ratio( vss = self$check_result$observed$vss, dist_1 = self$check_result$boot$vss, dist_2 = self$check_result$null$vss )
vss |
Numeric. The observed visual signal strength. |
dist_1 |
Numeric. A vector of visual signal strength for plots following the first distribution (bootstrap distribution by default). |
dist_2 |
Numeric. A vector of visual signal strength for plots following the second distribution (null distribution by default). |
A named vector with three elements likelihood_1
,
likelihood_2
and likelihood_ratio
.
dist_1 <- rnorm(100, 0, 1) dist_2 <- rnorm(100, 1, 1) AUTO_VI$likelihood_ratio(0, dist_1, dist_2)
dist_1 <- rnorm(100, 0, 1) dist_2 <- rnorm(100, 1, 1) AUTO_VI$likelihood_ratio(0, dist_1, dist_2)
This function conducts a visual inference lineup
check with a computer vision model. The result will be stored in
self$check_result
.
AUTO_VI$lineup_check( lineup_size = 20L, fitted_model = self$fitted_model, keras_model = self$keras_model, null_method = self$null_method, data = self$get_data(), node_index = self$node_index, extract_feature_from_layer = NULL )
lineup_size |
Integer. Number of plots in a lineup. |
fitted_model |
Model. A model object, e.g. |
keras_model |
Keras model. A trained computer vision model. |
null_method |
Function. A method to simulate residuals from the null
hypothesis distribution. For |
data |
Data frame. The data used to fit the model.
See also |
node_index |
Integer. An index indicating which node of the output layer contains the visual signal strength. This is particularly useful when the keras model has more than one output nodes. |
extract_feature_from_layer |
Character/Integer. A layer name or an integer layer index for extracting features from a layer. |
Return the object itself.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$lineup_check() myvi }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$lineup_check() myvi }
This default method gets rotated residuals from a fitted linear model using AUTO_VI$rotate_resid. User needs to override this method if the fitted model is not a linear regression model.
AUTO_VI$null_method(fitted_model = self$fitted_model)
fitted_model |
|
A tibble with two columns .fitted
and .resid
.
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) null_resid <- my_vi$null_method() my_vi$plot_resid(null_resid)
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) null_resid <- my_vi$null_method() my_vi$plot_resid(null_resid)
This function simulates null plots from the null hypothesis distribution, and predicts the visual signal strength.
AUTO_VI$null_vss( draws = 100L, fitted_model = self$fitted_model, keras_model = self$keras_model, null_method = self$null_method, node_index = self$node_index, keep_null_data = FALSE, keep_null_plot = FALSE, extract_feature_from_layer = NULL )
draws |
Integer. Number of simulation draws. |
fitted_model |
Model. A model object, e.g. |
keras_model |
Keras model. A trained computer vision model. |
null_method |
Function. A method to simulate residuals from the null
hypothesis distribution. For |
node_index |
Integer. An index indicating which node of the output layer contains the visual signal strength. This is particularly useful when the keras model has more than one output nodes. |
keep_null_data |
Boolean. Whether to keep the simulated null data. |
keep_null_plot |
Boolean. Whether to keep the simulated null plots. |
extract_feature_from_layer |
Character/Integer. A layer name or an integer layer index for extracting features from a layer. |
A tibble.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$null_vss() }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$null_vss() }
This function computes the p-value of observing the visual signal strength of the original residual plot based on the null distribution.
AUTO_VI$p_value( vss = self$check_result$observed$vss, null_dist = self$check_result$null$vss )
vss |
Numrice. A single numeric value indicating the visual signal strength for the true residual plot. |
null_dist |
Numeric. A vector of numeric values indicating the visual signal strength for null residual plots. |
A numeric value representing the desired p-value.
vss <- 1 null_dist <- rnorm(100, 0, 1) AUTO_VI$p_value(vss, null_dist)
vss <- 1 null_dist <- rnorm(100, 0, 1) AUTO_VI$p_value(vss, null_dist)
This function draws a lineup of standard residual plots consisting of a true residual plot and several null residual plots.
AUTO_VI$plot_lineup( lineup_size = 20L, data = self$get_fitted_and_resid(), null_method = self$null_method, theme = ggplot2::theme_light(), alpha = 1, size = 0.5, stroke = 0.5, remove_axis = TRUE, remove_legend = TRUE, remove_grid_line = TRUE, add_zero_line = TRUE, remove_facet_label = FALSE, display_answer = TRUE )
lineup_size |
Numeric. Number of plots in a lineup. |
data |
Data frame. A data frame containing variables |
null_method |
Function. A function that takes a fitted model as input,
and outputs a data frame containing variables |
theme |
|
alpha |
Numeric. Alpha of dot. Value between 0 and 1. |
size |
Numeric. Size of dot. Value between 0 and 1. |
stroke |
Numeric. Stroke of dot. Value between 0 and 1. |
remove_axis |
Boolean. Whether or not to remove the axis. |
remove_legend |
Boolean. Whether or not to remove the legend. |
remove_grid_line |
Boolean. Whether or not to remove the grid lines. |
add_zero_line |
Boolean. Whether or not to add a zero horizontal line. |
remove_facet_label |
Boolean. Whether or not to remove facet labels. |
display_answer |
Boolean. Whether or not to display the answer in title. |
A ggplot
.
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi$plot_lineup()
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi$plot_lineup()
This function draws a pair of standard residual plots consisting of a true residual plot and a null residual plot.
AUTO_VI$plot_pair( data = self$get_fitted_and_resid(), null_data = self$null_method(), theme = ggplot2::theme_light(), alpha = 1, size = 0.5, stroke = 0.5, remove_axis = TRUE, remove_legend = TRUE, remove_grid_line = TRUE, add_zero_line = TRUE )
data |
Data frame. A data frame containing variables |
null_data |
Data frame. A data frame containing variables |
theme |
|
alpha |
Numeric. Alpha of dot. Value between 0 and 1. |
size |
Numeric. Size of dot. Value between 0 and 1. |
stroke |
Numeric. Stroke of dot. Value between 0 and 1. |
remove_axis |
Boolean. Whether or not to remove the axis. |
remove_legend |
Boolean. Whether or not to remove the legend. |
remove_grid_line |
Boolean. Whether or not to remove the grid lines. |
add_zero_line |
Boolean. Whether or not to add a zero horizontal line. |
A ggplot
.
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi$plot_pair()
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi$plot_pair()
This function draws a standard residual plot.
AUTO_VI$plot_resid( data = self$get_fitted_and_resid(), theme = ggplot2::theme_light(base_size = 11/5), alpha = 1, size = 0.5, stroke = 0.5, remove_axis = TRUE, remove_legend = TRUE, remove_grid_line = TRUE, add_zero_line = TRUE )
data |
Data frame. A data frame containing variables |
theme |
|
alpha |
Numeric. Alpha of dot. Value between 0 and 1. |
size |
Numeric. Size of dot. Value between 0 and 1. |
stroke |
Numeric. Stroke of dot. Value between 0 and 1. |
remove_axis |
Boolean. Whether or not to remove the axis. |
remove_legend |
Boolean. Whether or not to remove the legend. |
remove_grid_line |
Boolean. Whether or not to remove the grid lines. |
add_zero_line |
Boolean. Whether or not to add a zero horizontal line. |
A ggplot
.
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi$plot_resid()
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) my_vi$plot_resid()
This function gets rotated residuals from a fitted linear model. The rotated residuals are generated by first regressing random noises on the original regressors, then multiply the obtained residuals by original RSS divided by the current RSS. The results are the rotated residuals.
AUTO_VI$rotate_resid(fitted_model = self$fitted_mod)
fitted_model |
|
A tibble with two columns .fitted
and .resid
.
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) rotated_resid <- my_vi$rotate_resid() my_vi$plot_resid(rotated_resid)
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) rotated_resid <- my_vi$rotate_resid() my_vi$plot_resid(rotated_resid)
This is the default method of saving plot(s). It will use
save_plot()
to save the ggplot to a 420 (width) * 525 (height) PNG
file. If the trained images are generated differently, one can override
this method using bandicoot::register_method()
.
AUTO_VI$save_plot(p, path = NULL)
p |
ggplot. A plot or a list of plots. |
path |
Character. Character. Path(s) to save the image. |
The image path.
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) p <- my_vi$plot_resid() my_vi$save_plot(p)
my_vi <- auto_vi(fitted_model = lm(speed ~ dist, data = cars)) p <- my_vi$plot_resid() my_vi$save_plot(p)
The AUTO_VI$..str..()
method provides a string
representation of the object. If a check is performed, the string
will contain some simple statistics of the check result. This method
does this same thing as AUTO_VI$..str..()
, but it returns an
AUTO_VI_SUMMARY
object which stores those statistics, such as sample
quantiles of the distribution of null visual signal strength, in the object.
AUTO_VI$summary()
An AUTO_VI_SUMMARY
object.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$check() myvi_summary <- myvi$summary() print(myvi_summary) names(myvi_summary) }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$check() myvi_summary <- myvi$summary() print(myvi_summary) names(myvi_summary) }
This function draws a summary density plot for the result.
AUTO_VI$summary_plot( vss = self$check_result$observed$vss, null_dist = self$check_result$null$vss, boot_dist = self$check_result$boot$vss, p_value = self$check_result$p_value, likelihood_ratio = self$check_result$likelihood_ratio, density_alpha = 0.6 )
vss |
Numeric. Observed visual signal strength. |
null_dist |
Numeric. Null visual signal strength. |
boot_dist |
Numeric. Bootstrapped visual signal strength. |
p_value |
Numeric. P-value of the visual test. See also
|
likelihood_ratio |
Numeric. Likelihood ratio of the visual test. See
also |
density_alpha |
Numeric. Alpha value for the density. |
A ggplot
.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$check() myvi$summary_density_plot() }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$check() myvi$summary_density_plot() }
This function draws a summary plot for the result.
AUTO_VI$summary_plot(type = "auto", ...)
type |
Character. Either "auto", "density" or "rank". Option "auto"
will use the Boolean flag |
... |
Arguments passed to |
A ggplot
.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$lineup_check() myvi$summary_plot() }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$lineup_check() myvi$summary_plot() }
This function draws a summary rank plot for the result.
AUTO_VI$summary_plot( vss = self$check_result$observed$vss, null_dist = self$check_result$null$vss, p_value = self$check_result$p_value )
vss |
Numeric. Observed visual signal strength. |
null_dist |
Numeric. Null visual signal strength. |
p_value |
Numeric. P-value of the visual test. See also
|
A ggplot
.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$lineup_check() myvi$summary_rank_plot() }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$lineup_check() myvi$summary_rank_plot() }
This function predicts the visual signal strength.
AUTO_VI$vss( p = self$plot_resid(), auxiliary = NULL, keras_model = self$keras_model, node_index = self$node_index, extract_feature_from_layer = NULL )
p |
|
auxiliary |
Dataframe. A dataframe of auxiliary values. This is only used
when the keras model has multiple inputs. If it is not provided, the
values will be automatically computed based on the residual plot of the
fitted model. See also |
keras_model |
Keras model. A trained computer vision model. |
node_index |
Integer. An index indicating which node of the output layer contains the visual signal strength. This is particularly useful when the keras model has more than one output nodes. |
extract_feature_from_layer |
Character/Integer. A layer name or an integer layer index for extracting features from a layer. |
A tibble. The first column is vss
which is the prediction, the
rest of the columns are features extracted from a layer.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$vss() }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { myvi <- auto_vi(lm(dist ~ speed, data = cars), keras_model) myvi$vss() }
This function checks if a python library is available. If the library can
not be found by the importlib.util.find_spec
method, then an
error will be throw.
check_python_library_available(lib_name)
check_python_library_available(lib_name)
lib_name |
Character. A library name. |
No return. Called for side-effect.
try(check_python_library_available("numpy"))
try(check_python_library_available("numpy"))
This functions download the keras model from the TengMCing/autovi_data
Github repo using download.file()
and load the model.
get_keras_model(model_name, format = "npz")
get_keras_model(model_name, format = "npz")
model_name |
String. The model name. See also |
format |
String. The model format to download. Either "npz", "SavedModel" or "keras". |
Note that the "SavedModel" and "keras" formats are not supported in
tensorflow
versions above 2.15, as
reticulate::import("tensorflow")$keras$models$load_model
encounters issues
when loading models saved with the Keras 2 API.
Instead, using the "npz" format allows for rebuilding the model
from scratch and loading the weights from a ".npz" file, offering a more
reliable alternative.
A keras model.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) keras_model$summary()
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) keras_model$summary()
This is the class of keras wrapper,
inherited from bandicoot::BASE. It is an environment
with S3 class bandicoot_oop
.
keras_wrapper( keras_model = NULL, node_index = 1L, env = new.env(parent = parent.frame()), init_call = sys.call() )
keras_wrapper( keras_model = NULL, node_index = 1L, env = new.env(parent = parent.frame()), init_call = sys.call() )
keras_model |
Keras model. A trained computer vision model. |
node_index |
Integer. An index indicating which node of the output layer contains the visual signal strength. This is particularly useful when the keras model has more than one output nodes. |
env |
Environment. The instance environment. |
init_call |
Call. Contents of the |
An instance environment.
keras_wrapper()
: Class constructor, same as KERAS_WRAPPER$instantiate()
.
Direct:
G:
I:
L:
P:
S:
This function will be called after an instance is built. User input will be stored in the environment.
KERAS_WRAPPER$..init..(keras_mod = NULL, node_index = 1L)
keras_mod |
Keras model. A trained computer vision model. |
node_index |
Integer. An index indicating which node of the output layer contains the visual signal strength. This is particularly useful when the keras model has more than one output nodes. |
Return the object itself.
keras_wrapper()
keras_wrapper()
This function returns a string representation of the object.
KERAS_WRAPPER$..str..()
A string.
KERAS_WRAPPER$..str..() wrapper <- keras_wrapper() wrapper$..str..()
KERAS_WRAPPER$..str..() wrapper <- keras_wrapper() wrapper$..str..()
This function get the input image height (the input shape is (batch_size, height, width, channels)) of a keras model.
KERAS_WRAPPER$get_input_height(keras_model = self$keras_model)
keras_model |
Keras model. A trained computer vision model. |
An integer.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { keras_wrapper(keras_model)$get_input_height() }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { keras_wrapper(keras_model)$get_input_height() }
This function get the input image width (the input shape is (batch_size, height, width, channels)) of a keras model.
KERAS_WRAPPER$get_input_width(keras_model = self$keras_model)
keras_model |
Keras model. A trained computer vision model. |
An integer.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { keras_wrapper(keras_model)$get_input_width() }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { keras_wrapper(keras_model)$get_input_width() }
This function loads an image from file and convert it to a numpy array.
KERAS_WRAPPER$image_to_array( path, height = self$get_input_height(), width = self$get_input_width() )
path |
Character. Path to the image. |
height |
Integer. Target height of the image. |
width |
Integer. Target width of the image. |
A numpy array.
p <- ggplot2::ggplot(cars) + ggplot2::geom_point(ggplot2::aes(dist, speed)) path <- save_plot(p) result <- try(KERAS_WRAPPER$image_to_array(path, 32L, 32L)) if (!inherits(result, "try-error")) { result }
p <- ggplot2::ggplot(cars) + ggplot2::geom_point(ggplot2::aes(dist, speed)) path <- save_plot(p) result <- try(KERAS_WRAPPER$image_to_array(path, 32L, 32L)) if (!inherits(result, "try-error")) { result }
This function list all layer names of a keras model.
KERAS_WRAPPER$list_layer_name(keras_model = self$keras_model)
keras_model |
Keras model. A trained computer vision model. |
A vector of strings.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { keras_wrapper(keras_model)$list_layer_name() }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { keras_wrapper(keras_model)$list_layer_name() }
This function predicts the visual signal strength using the provided keras model, input array and optional auxiliary input array.
KERAS_WRAPPER$predict( input_array, auxiliary = NULL, keras_model = self$keras_model, node_index = self$node_index, extract_featrue_from_layer = NULL )
input_array |
Array/Numpy array. An input array, usually of the shape (batch_size, height, width, channels). |
auxiliary |
Array/Data frame. An auxiliary input array of the shape (batch_size, number_of_auxiliary_inputs). This is only needed if the keras model takes multiple inputs. |
keras_model |
Keras model. A trained computer vision model. |
node_index |
Integer. An index indicating which node of the output layer contains the visual signal strength. This is particularly useful when the keras model has more than one output nodes. |
extract_feature_from_layer |
Character/Integer. A layer name or an integer layer index for extracting features from a layer. |
A tibble. The first column is vss
which is the prediction, the
rest of the columns are features extracted from a layer.
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { wrapper <- keras_wrapper(keras_model) # Provide one 32 * 32 RGB image and one vector of length 5 as input wrapper$predict(input_array = array(255, dim = c(1, 32, 32, 3)), auxiliary = matrix(1, ncol = 5)) }
keras_model <- try(get_keras_model("vss_phn_32")) if (!inherits(keras_model, "try-error")) { wrapper <- keras_wrapper(keras_model) # Provide one 32 * 32 RGB image and one vector of length 5 as input wrapper$predict(input_array = array(255, dim = c(1, 32, 32, 3)), auxiliary = matrix(1, ncol = 5)) }
This function gets a table of available pre-trained computer vision models for predicting visual signal strength.
list_keras_model()
list_keras_model()
A tibble of available model names and paths.
list_keras_model()
list_keras_model()
This function removes a plot from a provided path.
remove_plot(path, check_ext = TRUE)
remove_plot(path, check_ext = TRUE)
path |
Character. Path to the image. |
check_ext |
Boolean. Whether to check the file extension. |
No return. Called for side-effect.
p <- ggplot2::ggplot(cars) + ggplot2::geom_point(ggplot2::aes(dist, speed)) path <- save_plot(p) remove_plot(path)
p <- ggplot2::ggplot(cars) + ggplot2::geom_point(ggplot2::aes(dist, speed)) path <- save_plot(p) remove_plot(path)
This function save a plot of a list of plots to provided path(s).
save_plot(p, path = NULL, width = 7/5, height = 7/4, ...)
save_plot(p, path = NULL, width = 7/5, height = 7/4, ...)
p |
|
path |
Character. Path(s) to save the image. |
width |
Numeric. Width of the image. |
height |
Numeric. Height of the image. |
... |
Other arguments passed to |
The image path(s).
p <- ggplot2::ggplot(cars) + ggplot2::geom_point(ggplot2::aes(dist, speed)) save_plot(p)
p <- ggplot2::ggplot(cars) + ggplot2::geom_point(ggplot2::aes(dist, speed)) save_plot(p)