Skip to main content
Tweeted twitter.com/StackStats/status/1477610632769949697
gave additional context as to why I want to do this
Source Link
kram1032
  • 277
  • 1
  • 9

Say I have a rectangle $R: \left[{0,W}\right] \times \left[{0,H}\right]$ where $W$ is the width and $H$ is the height, with coordinates $\left(x, y\right) \in \mathbb{R}^2$.

I would like to randomly pick out (axis-aligned) rectangles $r_s: \left[{x_s-\frac{w_s}{2},x_s+\frac{w_s}{2}}\right] \times \left[{y_s-\frac{h_s}{2},y_s+\frac{h_s}{2}}\right]$ such that:

  • every position in $R$ is equally likely to be picked (in the limit, if I overlapped all $r_s$, the density with which each point in $\left(x, y\right) \in R$ is sampled will be uniform)
  • the sampled relative areas $a_s= \frac{w_s \times h_s}{W \times H}$ and aspect ratios $\rho_s = \frac{H}{W} \frac{w_s}{h_s}$ are also even (I'm not quite sure what the appropriate way to say this would be. But effectively, ideally, I'd like the algorithm to be "scale-free" with respect to $a_s$ if it can be. If it can't, then whatever closest notion it actually can be instead is fine. For $\rho_s$, I think some sort of cutoff is necessary to avoid divisions by 0, but that is fine. Let's just say there is a $\rho_{max}$ and a corresponding $\rho_{min}=\frac{1}{\rho_{max}}$)

Additionally, overscanning is allowed (the rₛ may land partially outside R), but at least half the area of the each rₛ must land within R. - If that helps, this also means the $r_s$ may actually occasionally be larger than $R$. I don't mind if they are but this is not a requirement. I think the largest possible $r_s$ would be $r_{max}: \left[\frac{W}{2}+\frac{\sqrt{2}W}{2},\frac{W}{2}-\frac{\sqrt{2}W}{2}\right] \times \left[\frac{H}{2}+\frac{\sqrt{2}H}{2},\frac{H}{2}-\frac{\sqrt{2}H}{2}\right]$ with relative area $a_{max} = 2$

Is it possible to sample in this manner? And how might one do so efficiently?


The use-case is machine learning and image processing using pytorch, so code is also welcome, however I should be fine if you give me a basic algorithm:

In particular, I have an AI that can process images of at most some fixed resolution. I am generating an image larger than that context size, and in order for the AI to still approximately get the full image, I am sending it pieces of the whole, scaled to fit the AI's context. If I do this unevenly, the AI over-focuses on parts of the image. I'd like it to consider the whole thing as evenly as possible. Every part of the image should be covered evenly, and every scale as well.
Technically, the varying aspect ratio isn't as necessary, however I found that this can help with the robustness of my results.

Say I have a rectangle $R: \left[{0,W}\right] \times \left[{0,H}\right]$ where $W$ is the width and $H$ is the height, with coordinates $\left(x, y\right) \in \mathbb{R}^2$.

I would like to randomly pick out (axis-aligned) rectangles $r_s: \left[{x_s-\frac{w_s}{2},x_s+\frac{w_s}{2}}\right] \times \left[{y_s-\frac{h_s}{2},y_s+\frac{h_s}{2}}\right]$ such that:

  • every position in $R$ is equally likely to be picked (in the limit, if I overlapped all $r_s$, the density with which each point in $\left(x, y\right) \in R$ is sampled will be uniform)
  • the sampled relative areas $a_s= \frac{w_s \times h_s}{W \times H}$ and aspect ratios $\rho_s = \frac{H}{W} \frac{w_s}{h_s}$ are also even (I'm not quite sure what the appropriate way to say this would be. But effectively, ideally, I'd like the algorithm to be "scale-free" with respect to $a_s$ if it can be. If it can't, then whatever closest notion it actually can be instead is fine. For $\rho_s$, I think some sort of cutoff is necessary to avoid divisions by 0, but that is fine. Let's just say there is a $\rho_{max}$ and a corresponding $\rho_{min}=\frac{1}{\rho_{max}}$)

Additionally, overscanning is allowed (the rₛ may land partially outside R), but at least half the area of the each rₛ must land within R. - If that helps, this also means the $r_s$ may actually occasionally be larger than $R$. I don't mind if they are but this is not a requirement. I think the largest possible $r_s$ would be $r_{max}: \left[\frac{W}{2}+\frac{\sqrt{2}W}{2},\frac{W}{2}-\frac{\sqrt{2}W}{2}\right] \times \left[\frac{H}{2}+\frac{\sqrt{2}H}{2},\frac{H}{2}-\frac{\sqrt{2}H}{2}\right]$ with relative area $a_{max} = 2$

Is it possible to sample in this manner? And how might one do so efficiently?


The use-case is machine learning and image processing using pytorch, so code is also welcome, however I should be fine if you give me a basic algorithm:

In particular, I have an AI that can process images of at most some fixed resolution. I am generating an image larger than that context size, and in order for the AI to still approximately get the full image, I am sending it pieces of the whole, scaled to fit the AI's context. If I do this unevenly, the AI over-focuses on parts of the image. I'd like it to consider the whole thing as evenly as possible. Every part of the image should be covered evenly, and every scale as well

Say I have a rectangle $R: \left[{0,W}\right] \times \left[{0,H}\right]$ where $W$ is the width and $H$ is the height, with coordinates $\left(x, y\right) \in \mathbb{R}^2$.

I would like to randomly pick out (axis-aligned) rectangles $r_s: \left[{x_s-\frac{w_s}{2},x_s+\frac{w_s}{2}}\right] \times \left[{y_s-\frac{h_s}{2},y_s+\frac{h_s}{2}}\right]$ such that:

  • every position in $R$ is equally likely to be picked (in the limit, if I overlapped all $r_s$, the density with which each point in $\left(x, y\right) \in R$ is sampled will be uniform)
  • the sampled relative areas $a_s= \frac{w_s \times h_s}{W \times H}$ and aspect ratios $\rho_s = \frac{H}{W} \frac{w_s}{h_s}$ are also even (I'm not quite sure what the appropriate way to say this would be. But effectively, ideally, I'd like the algorithm to be "scale-free" with respect to $a_s$ if it can be. If it can't, then whatever closest notion it actually can be instead is fine. For $\rho_s$, I think some sort of cutoff is necessary to avoid divisions by 0, but that is fine. Let's just say there is a $\rho_{max}$ and a corresponding $\rho_{min}=\frac{1}{\rho_{max}}$)

Additionally, overscanning is allowed (the rₛ may land partially outside R), but at least half the area of the each rₛ must land within R. - If that helps, this also means the $r_s$ may actually occasionally be larger than $R$. I don't mind if they are but this is not a requirement. I think the largest possible $r_s$ would be $r_{max}: \left[\frac{W}{2}+\frac{\sqrt{2}W}{2},\frac{W}{2}-\frac{\sqrt{2}W}{2}\right] \times \left[\frac{H}{2}+\frac{\sqrt{2}H}{2},\frac{H}{2}-\frac{\sqrt{2}H}{2}\right]$ with relative area $a_{max} = 2$

Is it possible to sample in this manner? And how might one do so efficiently?


The use-case is machine learning and image processing using pytorch, so code is also welcome, however I should be fine if you give me a basic algorithm:

In particular, I have an AI that can process images of at most some fixed resolution. I am generating an image larger than that context size, and in order for the AI to still approximately get the full image, I am sending it pieces of the whole, scaled to fit the AI's context. If I do this unevenly, the AI over-focuses on parts of the image. I'd like it to consider the whole thing as evenly as possible. Every part of the image should be covered evenly, and every scale as well.
Technically, the varying aspect ratio isn't as necessary, however I found that this can help with the robustness of my results.

added 473 characters in body
Source Link
kram1032
  • 277
  • 1
  • 9

Say I have a rectangle $R: \left[{0,W}\right] \times \left[{0,H}\right]$ where $W$ is the width and $H$ is the height, with coordinates $\left(x, y\right) \in \mathbb{R}^2$.

I would like to randomly pick out (axis-aligned) rectangles $r_s: \left[{x_s-\frac{w_s}{2},x_s+\frac{w_s}{2}}\right] \times \left[{y_s-\frac{h_s}{2},y_s+\frac{h_s}{2}}\right]$ such that:

  • every position in $R$ is equally likely to be picked (in the limit, if I overlapped all $r_s$, the density with which each point in $\left(x, y\right) \in R$ is sampled will be uniform)
  • the sampled relative areas $a_s= \frac{w_s \times h_s}{W \times H}$ and aspect ratios $\rho_s = \frac{H}{W} \frac{w_s}{h_s}$ are also even (I'm not quite sure what the appropriate way to say this would be. But effectively, ideally, I'd like the algorithm to be "scale-free" with respect to $a_s$ if it can be. If it can't, then whatever closest notion it actually can be instead is fine. For $\rho_s$, I think some sort of cutoff is necessary to avoid divisions by 0, but that is fine. Let's just say there is a $\rho_{max}$ and a corresponding $\rho_{min}=\frac{1}{\rho_{max}}$)

Additionally, overscanning is allowed (the rₛ may land partially outside R), but at least half the area of the each rₛ must land within R. - If that helps, this also means the $r_s$ may actually occasionally be larger than $R$. I don't mind if they are but this is not a requirement. I think the largest possible $r_s$ would be $r_{max}: \left[\frac{W}{2}+\frac{\sqrt{2}W}{2},\frac{W}{2}-\frac{\sqrt{2}W}{2}\right] \times \left[\frac{H}{2}+\frac{\sqrt{2}H}{2},\frac{H}{2}-\frac{\sqrt{2}H}{2}\right]$ with relative area $a_{max} = 2$

Is it possible to sample in this manner? And how might one do so efficiently?

 

(The The use-case is machine learning and image processing using pytorch, so code is also welcome, however I should be fine if you give me a basic algorithm):

In particular, I have an AI that can process images of at most some fixed resolution. I am generating an image larger than that context size, and in order for the AI to still approximately get the full image, I am sending it pieces of the whole, scaled to fit the AI's context. If I do this unevenly, the AI over-focuses on parts of the image. I'd like it to consider the whole thing as evenly as possible. Every part of the image should be covered evenly, and every scale as well

Say I have a rectangle $R: \left[{0,W}\right] \times \left[{0,H}\right]$ where $W$ is the width and $H$ is the height, with coordinates $\left(x, y\right) \in \mathbb{R}^2$.

I would like to randomly pick out (axis-aligned) rectangles $r_s: \left[{x_s-\frac{w_s}{2},x_s+\frac{w_s}{2}}\right] \times \left[{y_s-\frac{h_s}{2},y_s+\frac{h_s}{2}}\right]$ such that:

  • every position in $R$ is equally likely to be picked (in the limit, if I overlapped all $r_s$, the density with which each point in $\left(x, y\right) \in R$ is sampled will be uniform)
  • the sampled relative areas $a_s= \frac{w_s \times h_s}{W \times H}$ and aspect ratios $\rho_s = \frac{H}{W} \frac{w_s}{h_s}$ are also even (I'm not quite sure what the appropriate way to say this would be. But effectively, ideally, I'd like the algorithm to be "scale-free" with respect to $a_s$ if it can be. If it can't, then whatever closest notion it actually can be instead is fine. For $\rho_s$, I think some sort of cutoff is necessary to avoid divisions by 0, but that is fine. Let's just say there is a $\rho_{max}$ and a corresponding $\rho_{min}=\frac{1}{\rho_{max}}$)

Additionally, overscanning is allowed (the rₛ may land partially outside R), but at least half the area of the each rₛ must land within R. - If that helps, this also means the $r_s$ may actually occasionally be larger than $R$. I don't mind if they are but this is not a requirement. I think the largest possible $r_s$ would be $r_{max}: \left[\frac{W}{2}+\frac{\sqrt{2}W}{2},\frac{W}{2}-\frac{\sqrt{2}W}{2}\right] \times \left[\frac{H}{2}+\frac{\sqrt{2}H}{2},\frac{H}{2}-\frac{\sqrt{2}H}{2}\right]$ with relative area $a_{max} = 2$

Is it possible to sample in this manner? And how might one do so efficiently?

(The use-case is machine learning and image processing using pytorch, so code is also welcome, however I should be fine if you give me a basic algorithm)

Say I have a rectangle $R: \left[{0,W}\right] \times \left[{0,H}\right]$ where $W$ is the width and $H$ is the height, with coordinates $\left(x, y\right) \in \mathbb{R}^2$.

I would like to randomly pick out (axis-aligned) rectangles $r_s: \left[{x_s-\frac{w_s}{2},x_s+\frac{w_s}{2}}\right] \times \left[{y_s-\frac{h_s}{2},y_s+\frac{h_s}{2}}\right]$ such that:

  • every position in $R$ is equally likely to be picked (in the limit, if I overlapped all $r_s$, the density with which each point in $\left(x, y\right) \in R$ is sampled will be uniform)
  • the sampled relative areas $a_s= \frac{w_s \times h_s}{W \times H}$ and aspect ratios $\rho_s = \frac{H}{W} \frac{w_s}{h_s}$ are also even (I'm not quite sure what the appropriate way to say this would be. But effectively, ideally, I'd like the algorithm to be "scale-free" with respect to $a_s$ if it can be. If it can't, then whatever closest notion it actually can be instead is fine. For $\rho_s$, I think some sort of cutoff is necessary to avoid divisions by 0, but that is fine. Let's just say there is a $\rho_{max}$ and a corresponding $\rho_{min}=\frac{1}{\rho_{max}}$)

Additionally, overscanning is allowed (the rₛ may land partially outside R), but at least half the area of the each rₛ must land within R. - If that helps, this also means the $r_s$ may actually occasionally be larger than $R$. I don't mind if they are but this is not a requirement. I think the largest possible $r_s$ would be $r_{max}: \left[\frac{W}{2}+\frac{\sqrt{2}W}{2},\frac{W}{2}-\frac{\sqrt{2}W}{2}\right] \times \left[\frac{H}{2}+\frac{\sqrt{2}H}{2},\frac{H}{2}-\frac{\sqrt{2}H}{2}\right]$ with relative area $a_{max} = 2$

Is it possible to sample in this manner? And how might one do so efficiently?

 

The use-case is machine learning and image processing using pytorch, so code is also welcome, however I should be fine if you give me a basic algorithm:

In particular, I have an AI that can process images of at most some fixed resolution. I am generating an image larger than that context size, and in order for the AI to still approximately get the full image, I am sending it pieces of the whole, scaled to fit the AI's context. If I do this unevenly, the AI over-focuses on parts of the image. I'd like it to consider the whole thing as evenly as possible. Every part of the image should be covered evenly, and every scale as well

Source Link
kram1032
  • 277
  • 1
  • 9

Uniformly sample pieces of a rectangle

Say I have a rectangle $R: \left[{0,W}\right] \times \left[{0,H}\right]$ where $W$ is the width and $H$ is the height, with coordinates $\left(x, y\right) \in \mathbb{R}^2$.

I would like to randomly pick out (axis-aligned) rectangles $r_s: \left[{x_s-\frac{w_s}{2},x_s+\frac{w_s}{2}}\right] \times \left[{y_s-\frac{h_s}{2},y_s+\frac{h_s}{2}}\right]$ such that:

  • every position in $R$ is equally likely to be picked (in the limit, if I overlapped all $r_s$, the density with which each point in $\left(x, y\right) \in R$ is sampled will be uniform)
  • the sampled relative areas $a_s= \frac{w_s \times h_s}{W \times H}$ and aspect ratios $\rho_s = \frac{H}{W} \frac{w_s}{h_s}$ are also even (I'm not quite sure what the appropriate way to say this would be. But effectively, ideally, I'd like the algorithm to be "scale-free" with respect to $a_s$ if it can be. If it can't, then whatever closest notion it actually can be instead is fine. For $\rho_s$, I think some sort of cutoff is necessary to avoid divisions by 0, but that is fine. Let's just say there is a $\rho_{max}$ and a corresponding $\rho_{min}=\frac{1}{\rho_{max}}$)

Additionally, overscanning is allowed (the rₛ may land partially outside R), but at least half the area of the each rₛ must land within R. - If that helps, this also means the $r_s$ may actually occasionally be larger than $R$. I don't mind if they are but this is not a requirement. I think the largest possible $r_s$ would be $r_{max}: \left[\frac{W}{2}+\frac{\sqrt{2}W}{2},\frac{W}{2}-\frac{\sqrt{2}W}{2}\right] \times \left[\frac{H}{2}+\frac{\sqrt{2}H}{2},\frac{H}{2}-\frac{\sqrt{2}H}{2}\right]$ with relative area $a_{max} = 2$

Is it possible to sample in this manner? And how might one do so efficiently?

(The use-case is machine learning and image processing using pytorch, so code is also welcome, however I should be fine if you give me a basic algorithm)