Added padding to conv2d layer

parent af8da631
Pipeline #677 failed with stages
......@@ -65,7 +65,10 @@ public:
return _dims;
}
std::size_t getSize() const noexcept;
std::size_t getSize() const noexcept
{
return getSizeImpl(_dims);
}
const DataVector& getData() const noexcept
{
......@@ -160,6 +163,8 @@ public:
void resize(std::size_t i, std::size_t j, std::size_t k, std::size_t l);
void pad(std::pair<std::size_t, std::size_t> v_padding, std::pair<std::size_t, std::size_t> h_padding);
void setData(DataVector data) noexcept
{
PT_ASSERT(_data.size() == data.size());
......@@ -234,6 +239,18 @@ public:
protected:
DimsVector _dims;
DataVector _data;
static std::size_t getSizeImpl(const DimsVector& dims) noexcept
{
std::size_t size = 1;
for(std::size_t dim : dims)
{
size *= dim;
}
return size;
}
};
}
......
......@@ -161,6 +161,32 @@ bool Conv2DLayer::apply(LayerData& layerData) const
return false;
}
std::pair<std::size_t, std::size_t> v_padding, h_padding;
if (ww[1] == 3) {
v_padding.first = 1;
v_padding.second = 1;
}
else if (ww[1] == 2) {
v_padding.first = 0;
v_padding.second = 1;
}
else {
v_padding.first = 0;
v_padding.second = 0;
}
if (ww[2] == 3) {
h_padding.first = 1;
h_padding.second = 1;
}
else if (ww[2] == 2) {
h_padding.first = 0;
h_padding.second = 1;
}
else {
h_padding.first = 0;
h_padding.second = 0;
}
layerData.in.pad(v_padding, h_padding);
auto offsetY = ww[1] - 1;
auto offsetX = ww[2] - 1;
Tensor& out = layerData.out;
......
......@@ -281,23 +281,6 @@ std::unique_ptr<Tensor> Tensor::create(std::size_t dims, std::istream& stream)
return tensor;
}
std::size_t Tensor::getSize() const noexcept
{
std::size_t size = 0;
if(! _dims.empty())
{
++size;
for(auto dim : _dims)
{
size *= dim;
}
}
return size;
}
void Tensor::copyTo(Tensor& other) const
{
other._dims.clear();
......@@ -360,6 +343,35 @@ void Tensor::resize(std::size_t i, std::size_t j, std::size_t k, std::size_t l)
_data.resize(i * j * k * l);
}
void Tensor::pad(std::pair<std::size_t, std::size_t> v_padding, std::pair<std::size_t, std::size_t> h_padding) {
PT_ASSERT(v_padding.first >= 0);
PT_ASSERT(v_padding.second >= 0);
PT_ASSERT(h_padding.first >= 0);
PT_ASSERT(h_padding.second >= 0);
PT_ASSERT(_dims.size() == 3);
Tensor new_tensor;
auto new_dim0 = _dims[0] + v_padding.first + v_padding.second;
auto new_dim1 = _dims[1] + h_padding.first + h_padding.second;
new_tensor.resize(new_dim0, new_dim1, _dims[2]);
for (std::size_t i = 0; i < new_dim0; i++)
for (std::size_t j = 0; j < new_dim1; j++)
for (std::size_t k = 0; k < _dims[2]; k++) {
if (i < v_padding.first)
new_tensor(i, j, k) = 0.;
else if (new_dim0 - i <= v_padding.second)
new_tensor(i, j, k) = 0.;
else if (j < h_padding.first)
new_tensor(i, j, k) = 0.;
else if (new_dim1 - j <= h_padding.second)
new_tensor(i, j, k) = 0.;
else
new_tensor(i, j, k) = this->operator()(i - v_padding.first, j - h_padding.first, k);
}
resize(new_dim0, new_dim1, _dims[2]);
this->setData(new_tensor._data);
}
void Tensor::fill(Type value) noexcept
{
std::fill(begin(), end(), value);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment