我要添加的图像变换(我称之为ResizeTransformer
),其如何添加图像大小调整变换CNTK
- 调整大小的图像的较小的尺寸给定的尺寸,同时保持原始的宽高比
要在不实施单独ResizeTransformer
做到这一点我想修改this file 但是该类ScaleTransformer : public ImageTransformerBase
类,这个类实现StreamInformation ScaleTransformer::Transform(const StreamInformation& inputStream)
与变换流的目的,让所有的SAM大小相同。 我的查询如下:
为什么要实现这个功能是必需的?这是否会增加任何性能优势,或者这对于更基本的目的而言很重要?
我是否必须将
ResizeTransformer()
作为单独的类来实施?在这种情况下,我必须执行
StreamInformation ResizeTransformer::Transform(const StreamInformation& inputStream
吗?
需要为这个改造 这个转换需要,因为一个人的数据集中的所有图像可以是不同大小的,有人可能想从每个图像中提取的多个补丁。在这种情况下,最佳解决方案是将图像的较小尺寸调整为大于剪裁尺寸的特定尺寸,然后从中提取尺寸为C
的多个补丁。这种数据增强在我所了解的某些论文中得到了实践。
PS: 我做如下补充,努力增加ResizeTransformer
我感到困惑的是如何对其进行测试。 C++编译成功,意味着C++代码是正确的。但我想在python中使用它。
添置header file在我的系统: `
class ResizeTransformer : public ImageTransformerBase
{
public:
explicit ResizeTransformer(const Microsoft::MSR::CNTK::ConfigParameters& config);
private:
enum class ResizeMode
{
ResizeMin = 0,
ResizeMax = 0
};
ResizeMode resize_mode;
size_t resized_length;
void Apply(uint8_t copyId, cv::Mat &mat) override;
};
而到了source file:
ResizeTransformer::ResizeTransformer(const ConfigParameters& config) : ImageTransformerBase(config)
{
resized_length = config(L"resized_length");
if (resized_length <= 0)
RuntimeError("Cannot resize any dimension of an image to zero or negative number.");
string resize_type = config(L"resize_type", "ResizeMin");
if (resize_type == "ResizeMin")
resize_mode = ResizeMode::ResizeMin;
else if (resize_type == "ResizeMax")
resize_mode = ResizeMode::ResizeMax;
else RuntimeError("Invalid resize_type. Must be one of ResizeMin and ResizeMax");
}
void ResizeTransformer::Apply(uint8_t, cv::Mat &mat)
{
float height = mat.rows;
float width = mat.cols;
float aspectratio = height/width;
float newheight{};
float newwidth{};
if (resize_mode == ResizeMode::ResizeMin)
{
if(height <=width)
{
newheight = resized_length;
newwidth = newheight/aspectratio;
}
else
{
newheight = aspectratio * resized_length;
newwidth = resized_length;
}
}
else
{
if(height <=width)
{
newheight = aspectratio * resized_length;
newwidth = resized_length;
}
else
{
newheight = resized_length;
newwidth = newheight/aspectratio;
}
}
resize(mat, mat, cv::Size2f(newwidth, newheight));
}
我添加以下行this file
transformations.push_back(Transformation{ std::make_shared<ResizeTransformer>(featureStream), featureName });
然后我说的跟在012之后
CNTK_API ImageTransform ReaderResize(int resized_length,
const wchar_t* resize_type = L"ResizeMin");
最后添加以下功能this file
def resize(resized_length, resize_type='ResizeMin'):
'''
Resize transform that can be used to pass to `map_features`
Given an input image, it will resize a given dimension to
a fixed size (resized_length), while preserving the aspect ratio.
Args:
resized_length (int): A positive integer. It is the resized value of the
dimension which has to be resized. The other dimension is resized while
maintaining the aspect ratio.
resize_type (str, default 'ResizeMin'): 'ResizeMin' or 'ResizeMax'.
When 'ResizeMin', the smaller dimension of the image is resized to a fixed size
given by resized_length, with the larger dimension resized in a way to preserve
the priginal aspect ratio. When 'ResizeMax', the same operation is performed
but now the larger dimension of the image is resized to a fixed size.
Returns:
A dictionary like object describing the ResizeTransform.
'''
return cntk_py.reader_resize(resized_length, resize_type)
我终于决定修改ScaleTransform并进行了更改。你的回答对未来的变化非常有用:)非常感谢。 – Ujjwal