Skip to content

Commit

Permalink
upgrade to opencv 090 (#33)
Browse files Browse the repository at this point in the history
* opencv 090

* clippy fix
  • Loading branch information
jimexist committed Apr 18, 2024
1 parent 47408d5 commit e4657d0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ clap = { version = "4.4.18", features = ["derive"], optional = true }
env_logger = { version = "0.11.0" }
hf-hub = { version = "0.3.2" }
log = { version = "0.4.20" }
opencv = { version = "0.88.8", default-features = false, features = [
opencv = { version = "0.90.0", default-features = false, features = [
'imgproc',
'imgcodecs',
] }
Expand Down
2 changes: 1 addition & 1 deletion src/bbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn get_dilation_matrix(segmap: &mut Mat, stats_row: &[i32]) -> crate::Result<Mat
let ey = (y + h + niter + 1).min(segmap.rows());
Rect::new(sx, sy, ex - sx, ey - sy)
};
let mut roi = Mat::roi(segmap, roi)?;
let mut roi = Mat::roi(segmap, roi)?.clone_pointee();
let kernel = imgproc::get_structuring_element(
imgproc::MORPH_RECT,
Size::new(1 + niter, 1 + niter),
Expand Down
3 changes: 2 additions & 1 deletion src/postprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ impl ImageChunks {
let result = Mat::roi(
&resized_image,
core::Rect::new(0, 0, self.original_size.width, self.original_size.height),
)?;
)?
.clone_pointee();
Ok(result)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn read_chunked_resized_image<P: AsRef<Path>>(image_path: P) -> crate::Resul
.map(|i| {
let start = (i as i32) * IMAGE_CHUNK_HEIGHT;
let roi: core::Rect_<i32> = core::Rect::new(0, start, image.cols(), IMAGE_CHUNK_HEIGHT);
let chunk = Mat::roi(&image, roi)?;
let chunk = Mat::roi(&image, roi)?.clone_pointee();
let size = core::Size::new(INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE);
resize(chunk, size)
})
Expand Down
26 changes: 13 additions & 13 deletions src/recognition/swin_transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ struct SwinPatchEmbeddings {

impl SwinPatchEmbeddings {
fn new(config: &SwinConfig, vb: VarBuilder) -> Result<Self> {
let num_channels = config.num_channels as usize;
let patch_size = config.patch_size as usize;
let hidden_size = config.embed_dim as usize;
let num_channels = config.num_channels;
let patch_size = config.patch_size;
let hidden_size = config.embed_dim;
let projection = conv2d(
num_channels,
hidden_size,
Expand Down Expand Up @@ -209,7 +209,7 @@ impl SwinIntermediate {

impl Module for SwinIntermediate {
fn forward(&self, x: &Tensor) -> Result<Tensor> {
let x = self.dense.forward(&x)?;
let x = self.dense.forward(x)?;
let x = self.intermediate_act_fn.forward(&x)?;
Ok(x)
}
Expand All @@ -229,7 +229,7 @@ impl SwinSelfOutput {

impl Module for SwinSelfOutput {
fn forward(&self, x: &Tensor) -> Result<Tensor> {
let x = self.dense.forward(&x)?;
let x = self.dense.forward(x)?;
Ok(x)
}
}
Expand All @@ -248,7 +248,7 @@ impl SwinOutput {

impl Module for SwinOutput {
fn forward(&self, x: &Tensor) -> Result<Tensor> {
let x = self.dense.forward(&x)?;
let x = self.dense.forward(x)?;
Ok(x)
}
}
Expand Down Expand Up @@ -373,11 +373,11 @@ impl SwinSelfAttention {
(left - right)?
};
let relative_grid = {
let bias = Tensor::full(window_size - 1, &grid.shape().clone(), device)?;
let bias = Tensor::full(window_size - 1, grid.shape().clone(), device)?;
let relative_grid = (grid + bias)?;
let m1 = relative_grid.i(0)?;
let m2 = relative_grid.i(1)?;
let scalar = Tensor::full(2 * window_size - 1, &m1.shape().clone(), device)?;
let scalar = Tensor::full(2 * window_size - 1, m1.shape().clone(), device)?;
let m1 = (m1 * scalar)?;
Tensor::stack(&[m1, m2], 2)?
};
Expand All @@ -395,9 +395,9 @@ impl SwinSelfAttention {
impl Module for SwinSelfAttention {
fn forward(&self, x: &Tensor) -> Result<Tensor> {
debug_assert_eq!(3, x.dims().len(), "Input tensor must have 3 dimensions");
let key_layer = self.transpose_for_scores(&self.key.forward(&x)?)?;
let query_layer = self.transpose_for_scores(&self.query.forward(&x)?)?;
let value_layer = self.transpose_for_scores(&self.value.forward(&x)?)?;
let key_layer = self.transpose_for_scores(&self.key.forward(x)?)?;
let query_layer = self.transpose_for_scores(&self.query.forward(x)?)?;
let value_layer = self.transpose_for_scores(&self.value.forward(x)?)?;
let attention_scores = (query_layer.matmul(&key_layer.t()?))?;
let attention_scores = (attention_scores / (self.attention_head_size as f64).sqrt())?;
let attention_scores = attention_scores.broadcast_add(&self.relative_position_bias)?;
Expand Down Expand Up @@ -722,7 +722,7 @@ impl SwinEncoder {
let downsample = i < config.depths.len() - 1;
SwinStage::new(
config,
dim as usize,
dim,
depth,
num_heads,
downsample,
Expand Down Expand Up @@ -943,7 +943,7 @@ mod test {
let y = embedding.patch_embeddings.forward(&x)?;
let y_sum: f32 = y.to_dtype(DType::F32)?.sum_all()?.to_scalar()?;
// assert_eq!(y_sum, -112499.0938);
assert!(approx_eq!(f32, y_sum, -112499.0938, epsilon = 20.));
assert!(approx_eq!(f32, y_sum, -112_499.09, epsilon = 20.));
}
{
let y = embedding.forward(&x)?;
Expand Down

0 comments on commit e4657d0

Please sign in to comment.