Skip to content

Commit 04b7a0d

Browse files
committed
Modernizes codebase with dependency updates and API improvements
Updates burn and related dependencies to latest versions for better performance and features. Refactors image utilities from struct-based to free functions for cleaner API. Adds tracing-based logging, MiMalloc allocator for memory efficiency, and workspace-wide linting rules. Enhances documentation with inline re-exports and improves error handling in inference and training modules.
1 parent f72b034 commit 04b7a0d

20 files changed

Lines changed: 1073 additions & 970 deletions

File tree

Cargo.lock

Lines changed: 240 additions & 216 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ cfg-if = "1.0.4"
3636
libm = "0.2.15"
3737
rand = "0.9.2"
3838
stacker = "0.1.22"
39+
tracing = "0.1"
40+
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
3941
walkdir = "2.5.0"
4042

4143
# Testing and benchmarking
@@ -44,5 +46,43 @@ rstest = "0.26.1"
4446
# Model hub integration
4547
hf-hub = { version = "0.4.3", default-features = false, features = ["ureq", "rustls-tls"] }
4648

49+
[workspace.lints.rust]
50+
ambiguous_negative_literals = "warn"
51+
missing_debug_implementations = "warn"
52+
redundant_imports = "warn"
53+
redundant_lifetimes = "warn"
54+
trivial_numeric_casts = "warn"
55+
unsafe_op_in_unsafe_fn = "warn"
56+
unused_lifetimes = "warn"
57+
58+
[workspace.lints.clippy]
59+
cargo = { level = "warn", priority = -1 }
60+
complexity = { level = "warn", priority = -1 }
61+
correctness = { level = "warn", priority = -1 }
62+
pedantic = { level = "warn", priority = -1 }
63+
perf = { level = "warn", priority = -1 }
64+
style = { level = "warn", priority = -1 }
65+
suspicious = { level = "warn", priority = -1 }
66+
allow_attributes_without_reason = "warn"
67+
assertions_on_result_states = "warn"
68+
clone_on_ref_ptr = "warn"
69+
deref_by_slicing = "warn"
70+
empty_drop = "warn"
71+
empty_enum_variants_with_brackets = "warn"
72+
empty_structs_with_brackets = "warn"
73+
fn_to_numeric_cast_any = "warn"
74+
if_then_some_else_none = "warn"
75+
map_err_ignore = "warn"
76+
redundant_type_annotations = "warn"
77+
renamed_function_params = "warn"
78+
semicolon_outside_block = "warn"
79+
string_to_string = "warn"
80+
undocumented_unsafe_blocks = "warn"
81+
unnecessary_safety_comment = "warn"
82+
unnecessary_safety_doc = "warn"
83+
unneeded_field_pattern = "warn"
84+
unused_result_ok = "warn"
85+
literal_string_with_formatting_args = "allow"
86+
4787
[profile.dev.package.image]
4888
opt-level = 3

crates/birefnet-backbones/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
mod backbones;
77

8+
#[doc(inline)]
89
pub use backbones::{
910
pvt_v2::{PvtV2Config, PyramidVisionTransformerImpr},
1011
resnet::{ResNetBackbone, ResNetConfig},
@@ -276,4 +277,4 @@ mod tests {
276277
use burn::backend::Cpu;
277278

278279
pub type TestBackend = Cpu;
279-
}
280+
}

crates/birefnet-extra-ops/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ mod identity;
99
mod trunc_normal;
1010

1111
// Convenient re-exports
12+
#[doc(inline)]
1213
pub use drop_path::{DropPath, DropPathConfig};
14+
#[doc(inline)]
1315
pub use erfinv::{Erfinv, erfinv};
16+
#[doc(inline)]
1417
pub use identity::Identity;
18+
#[doc(inline)]
1519
pub use trunc_normal::{trunc_normal, trunc_normal_};
1620

1721
#[cfg(test)]
1822
mod tests {
1923
use burn::backend::Cpu;
2024

2125
pub type TestBackend = Cpu;
22-
}
26+
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
pub mod postprocessing;
22

3-
pub use postprocessing::*;
3+
#[doc(inline)]
4+
pub use postprocessing::{
5+
apply_threshold, fill_holes, gaussian_blur, morphological_closing, morphological_opening,
6+
postprocess_mask, remove_small_components, resize_tensor, tensor_to_image_data,
7+
};
48

59
#[cfg(test)]
610
mod tests {
711
use burn::backend::Cpu;
812

913
pub type TestBackend = Cpu;
10-
}
14+
}

crates/birefnet-inference/src/postprocessing.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! segmentation masks and images.
55
66
use anyhow::{Context, Result};
7-
use birefnet_util::ImageUtils;
7+
use birefnet_util::{dynamic_image_to_tensor, tensor_to_dynamic_image};
88
use burn::tensor::{Tensor, backend::Backend};
99
use image::{self, imageops::FilterType};
1010

@@ -209,7 +209,7 @@ pub fn resize_tensor<B: Backend>(
209209

210210
// This approach converts tensor to image, resizes, then converts back to tensor.
211211
// It's inefficient but works without a direct tensor interpolation implementation.
212-
let dynamic_image = ImageUtils::tensor_to_dynamic_image(tensor, false)
212+
let dynamic_image = tensor_to_dynamic_image(tensor, false)
213213
.context("Failed to convert tensor to image for resizing")?;
214214

215215
let resized_image = dynamic_image.resize_exact(
@@ -219,6 +219,6 @@ pub fn resize_tensor<B: Backend>(
219219
);
220220

221221
// Convert back to tensor
222-
ImageUtils::dynamic_image_to_tensor(resized_image, device)
222+
dynamic_image_to_tensor(resized_image, device)
223223
.context("Failed to convert resized image back to tensor")
224224
}

crates/birefnet-loss/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,25 @@ mod threshold_regularization;
7878
mod birefnet_loss;
7979

8080
// Re-export core loss functions and configurations
81+
#[doc(inline)]
8182
pub use birefnet_loss::{BiRefNetLoss, BiRefNetLossConfig};
83+
#[doc(inline)]
8284
pub use classification::{ClassificationLoss, ClassificationLossConfig};
85+
#[doc(inline)]
8386
pub use contour::{ContourLoss, ContourLossConfig};
87+
#[doc(inline)]
8488
pub use iou::{IoULoss, IoULossConfig};
89+
#[doc(inline)]
8590
pub use mae::{MaeLoss, MaeLossConfig};
91+
#[doc(inline)]
8692
pub use patch_iou::{PatchIoULoss, PatchIoULossConfig};
93+
#[doc(inline)]
8794
pub use pixel::{LossWeightsConfig, PixLoss, PixLossConfig};
95+
#[doc(inline)]
8896
pub use ssim::{SSIMLoss, SSIMLossConfig};
97+
#[doc(inline)]
8998
pub use structure::{StructureLoss, StructureLossConfig};
99+
#[doc(inline)]
90100
pub use threshold_regularization::{
91101
ThresholdRegularizationLoss, ThresholdRegularizationLossConfig,
92102
};
@@ -96,4 +106,4 @@ mod tests {
96106
use burn::backend::Cpu;
97107

98108
pub type TestBackend = Cpu;
99-
}
109+
}

crates/birefnet-metric/src/lib.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,33 @@ pub mod utils;
7878
pub mod weighted_f_measure;
7979

8080
// Re-export main types and traits
81+
#[doc(inline)]
8182
pub use biou::{BIoUMetric, BIoUMetricConfig};
83+
#[doc(inline)]
8284
pub use f_measure::{FMeasureMetric, FMeasureMetricConfig};
83-
pub use input::*;
85+
#[doc(inline)]
86+
pub use input::{
87+
BIoUInput, BiRefNetLossInput, EMeasureInput, FMeasureInput, MAEInput, MSEInput, SMeasureInput,
88+
WeightedFMeasureInput,
89+
};
90+
#[doc(inline)]
8491
pub use loss::LossMetric;
92+
#[doc(inline)]
8593
pub use mae::{MAEMetric, MAEMetricConfig};
94+
#[doc(inline)]
8695
pub use mse::{MSEMetric, MSEMetricConfig};
8796
// Re-export lesser-used items with warning
8897
#[deprecated(note = "S-measure is not yet fully implemented and tested")]
89-
pub use s_measure::*;
90-
pub use utils::*;
91-
pub use weighted_f_measure::{WeightedFMeasureMetric, WeightedFMeasureMetricConfig};
98+
#[doc(inline)]
99+
pub use s_measure::{SMeasureInput as SMeasureInputDeprecated, SMeasureMetric, SMeasureMetricConfig, calculate_s_measure};
100+
#[doc(inline)]
101+
pub use utils::{AllMetricsResult, calculate_all_metrics};
102+
#[doc(inline)]
103+
pub use weighted_f_measure::{WeightedFMeasureMetric, WeightedFMeasureMetricConfig, calculate_weighted_f_measure};
92104

93105
#[cfg(test)]
94106
mod tests {
95107
use burn::backend::Cpu;
96108

97109
pub type TestBackend = Cpu;
98-
}
110+
}

crates/birefnet-metric/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct AllMetricsResult {
3131
pub fn calculate_all_metrics<B: Backend>(
3232
predictions: Tensor<B, 4>,
3333
targets: Tensor<B, 4>,
34-
threshold: f64,
34+
_threshold: f64,
3535
) -> AllMetricsResult {
3636
// Ensure predictions and targets have correct shape
3737
let [batch_size, _, _, _] = predictions.dims();

crates/birefnet-model/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@ mod models;
2828
pub mod training;
2929

3030
// Re-export specific configuration types instead of wildcard
31+
#[doc(inline)]
3132
pub use config::{
3233
Backbone, BackboneConfig, DecBlk, DecChannelsInter, DecoderAttention, DecoderConfig,
3334
InterpolationStrategy, LateralBlock, ModelConfig, MultiScaleInput, Optimizer, PathConfig,
3435
PreprocMethods, PromptForLocation, Refine, RefineConfig, SqueezeBlock, Task, TaskConfig,
3536
};
37+
#[doc(inline)]
3638
pub use error::{BiRefNetError, BiRefNetResult};
39+
#[doc(inline)]
3740
pub use models::birefnet::{BiRefNet, BiRefNetConfig, BiRefNetRecord};
3841
#[cfg(feature = "train")]
42+
#[doc(inline)]
3943
pub use training::{BiRefNetBatch, BiRefNetOutput};
4044

4145
#[cfg(test)]
@@ -45,4 +49,4 @@ mod tests {
4549
pub type TestBackend = Cpu;
4650

4751
pub type TestAutodiffBackend = Autodiff<TestBackend>;
48-
}
52+
}

0 commit comments

Comments
 (0)