两种不同方式实现resize

使用warpaffine实现resize

#include <opencv2/core/mat.hpp>
void resize_by_using_warp(cv::Mat ori,cv::Mat& out, int w, int h){
    float ori_w = ori.cols;
    float ori_h = ori.rows;
    float xsacle = float(w)/ori_w;
    float yscale = float(h)/ori_h;
    cv::Mat center = cv::Mat::eye(3, 3, CV_32FC1);
    
    center.at<float>(0, 2) = -1 * ori_w / 2;
    center.at<float>(1, 2) = -1 * ori_h / 2;

    cv::Mat scale = cv::Mat::eye(3, 3, CV_32FC1);
    scale.at<float>(0, 0) = xsacle;
    scale.at<float>(1, 1) = yscale;

    cv::Mat recenter = cv::Mat::eye(3, 3, CV_32FC1);
    recenter.at<float>(0, 2) = ori_w / 2 / xsacle;
    recenter.at<float>(1, 2) = ori_h / 2 / yscale;

    cv::Mat compose = recenter.mul(scale.mul(center));
    cv::warpAffine(ori,out, cv::Mat(compose, cv::Range(0, 2), cv::Range::all()), {w, h});
}

MNN ImageProcess实现 resize

#include <MNN/ImageProcess.hpp>
#include <MNN/Interpreter.hpp>

void mnn_resize(cv::Mat ori_mat, cv::Mat& new_mat, const cv::Size& target_size){
    cv::Mat gray_mat;
    if(ori_mat.channels() == 1){
        gray_mat = ori_mat;
    }
    else{
        std::vector<cv::Mat> channels;
        cv::split(ori_mat, channels);//split make contigous??
        gray_mat = channels[0];
    }

    int originalWidth = gray_mat.cols;
    int originalHeight = gray_mat.rows;
    CV::ImageProcess::Config preProcessConfig;
    preProcessConfig.sourceFormat = CV::GRAY;
    preProcessConfig.destFormat   = CV::BGR;
    preProcessConfig.filterType   = CV::BILINEAR;

    auto pretreat = std::shared_ptr<CV::ImageProcess>(CV::ImageProcess::create(preProcessConfig));
    CV::Matrix trans;
    trans.postScale(1.0 / target_size.width, 1.0 / target_size.height);
    trans.postScale(originalWidth, originalHeight);
    pretreat->setMatrix(trans);
    const uint8_t* fram_ptr = reinterpret_cast<uint8_t*>(gray_mat.data);
    new_mat = cv::Mat::zeros(target_size, CV_8UC3);// cv::Mat holder buffer ptr
    std::shared_ptr<Tensor> tensor(Tensor::create<uint8_t>(std::vector<int>{1, target_size.height, target_size.width, 3}, new_mat.data, Tensor::TENSORFLOW));
    pretreat->convert(fram_ptr, originalWidth, originalHeight, 0, tensor.get());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,这是一个非常流行的机器学习问题,我们可以使用卷积神经网络(Convolutional Neural Network,CNN)来解决手写数字识别问题。 首先,我们需要准备一个手写数字数据集,MNIST 数据集是一个非常著名的手写数字数据集,我们可以直接从 Keras 库中加载 MNIST 数据集。 接下来,我们可以使用两种不同的 CNN 模型进行训练:LeNet-5 和 AlexNet。 LeNet-5 是一个经典的 CNN 模型,它由 Yann LeCun 在 1998 年提出,是用于识别手写数字的第一个卷积神经网络。AlexNet 是一个更深的 CNN 模型,由 Alex Krizhevsky 在 2012 年提出,它在 ImageNet 图像分类比赛中获得了第一名。 下面我们来详细介绍如何实现 LeNet-5 和 AlexNet 模型: ### LeNet-5 LeNet-5 模型包含了两个卷积层和三个全连接层。可以使用 Keras 库来实现该模型。 ```python from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense model = Sequential() # 第一层卷积层 model.add(Conv2D(filters=6, kernel_size=(5, 5), activation='relu', input_shape=(28, 28, 1))) model.add(MaxPooling2D(pool_size=(2, 2))) # 第二层卷积层 model.add(Conv2D(filters=16, kernel_size=(5, 5), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) # 将卷积层的输出展平成一维向量 model.add(Flatten()) # 全连接层 model.add(Dense(units=120, activation='relu')) model.add(Dense(units=84, activation='relu')) model.add(Dense(units=10, activation='softmax')) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) ``` ### AlexNet AlexNet 模型包含了五个卷积层和三个全连接层。同样可以使用 Keras 库来实现该模型。 ```python from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout model = Sequential() # 第一层卷积层 model.add(Conv2D(filters=96, kernel_size=(11, 11), strides=(4, 4), activation='relu', input_shape=(227, 227, 3))) model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2))) # 第二层卷积层 model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu')) model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2))) # 第三层卷积层 model.add(Conv2D(filters=384, kernel_size=(3, 3), activation='relu')) # 第四层卷积层 model.add(Conv2D(filters=384, kernel_size=(3, 3), activation='relu')) # 第五层卷积层 model.add(Conv2D(filters=256, kernel_size=(3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2))) # 将卷积层的输出展平成一维向量 model.add(Flatten()) # 全连接层 model.add(Dense(units=4096, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(units=4096, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(units=1000, activation='relu')) model.add(Dense(units=10, activation='softmax')) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) ``` 在训练模型之前,我们需要将数据集转换为适合 CNN 模型的格式。对于 LeNet-5 模型,我们需要将 MNIST 数据集的图像大小调整为 28x28,并将其转换为灰度图像。对于 AlexNet 模型,我们需要将 MNIST 数据集的图像大小调整为 227x227,并将其转换为彩色图像。 ```python from keras.datasets import mnist from keras.utils import to_categorical import cv2 # 加载 MNIST 数据集 (x_train, y_train), (x_test, y_test) = mnist.load_data() # 将图像大小调整为 28x28 x_train = [cv2.resize(x, (28, 28)) for x in x_train] x_test = [cv2.resize(x, (28, 28)) for x in x_test] # 将图像转换为灰度图像 x_train = [cv2.cvtColor(x, cv2.COLOR_BGR2GRAY) for x in x_train] x_test = [cv2.cvtColor(x, cv2.COLOR_BGR2GRAY) for x in x_test] # 将图像转换为 4D 张量 x_train = np.expand_dims(x_train, axis=-1) x_test = np.expand_dims(x_test, axis=-1) # 对标签进行 one-hot 编码 y_train = to_categorical(y_train) y_test = to_categorical(y_test) ``` ```python from keras.datasets import mnist from keras.utils import to_categorical import cv2 # 加载 MNIST 数据集 (x_train, y_train), (x_test, y_test) = mnist.load_data() # 将图像大小调整为 227x227 x_train = [cv2.resize(x, (227, 227)) for x in x_train] x_test = [cv2.resize(x, (227, 227)) for x in x_test] # 将图像转换为彩色图像 x_train = [cv2.cvtColor(x, cv2.COLOR_GRAY2RGB) for x in x_train] x_test = [cv2.cvtColor(x, cv2.COLOR_GRAY2RGB) for x in x_test] # 将图像转换为 4D 张量 x_train = np.array(x_train) x_test = np.array(x_test) # 对标签进行 one-hot 编码 y_train = to_categorical(y_train) y_test = to_categorical(y_test) ``` 现在我们可以训练 LeNet-5 和 AlexNet 模型,并比较它们的测试结果。 ```python # 训练 LeNet-5 模型 history1 = model1.fit(x_train, y_train, batch_size=128, epochs=20, validation_data=(x_test, y_test)) # 训练 AlexNet 模型 history2 = model2.fit(x_train, y_train, batch_size=128, epochs=20, validation_data=(x_test, y_test)) ``` 最后,我们可以使用测试集来评估模型的性能。 ```python # 测试 LeNet-5 模型 score1 = model1.evaluate(x_test, y_test, verbose=0) print('Test loss:', score1[0]) print('Test accuracy:', score1[1]) # 测试 AlexNet 模型 score2 = model2.evaluate(x_test, y_test, verbose=0) print('Test loss:', score2[0]) print('Test accuracy:', score2[1]) ``` 通过比较测试结果,我们可以看到 AlexNet 模型的测试准确率要高于 LeNet-5 模型。 ### 回答2: 利用卷积神经网络(Convolutional Neural Network, CNN)可以有效地实现手写数字识别。在进行手写数字识别时,我们常用的是MNIST数据集,该数据集包含了大量的手写数字样本。 首先,我们可以使用LeNet-5模型进行训练和测试。LeNet-5是一种经典的卷积神经网络模型,它包含了两个卷积层和三个全连接层。首先,输入的手写数字图像经过卷积层和池化层,提取图像特征。然后,这些特征被传递到全连接层进行分类。最后,使用Softmax函数将每个数字标记为0到9之间的概率。 其次,我们可以使用更深层的卷积神经网络,例如:VGG模型进行训练和测试。VGG模型有16层甚至更多的卷积层和全连接层,具有更强的图像特征提取能力。与LeNet-5相比,VGG模型有更多的参数,能够更好地适应更复杂的手写数字图像特征。 接下来,我们对两种不同的神经网络进行测试比较。如使用MNIST数据集作为输入,经过LeNet-5和VGG模型训练后,我们可以得到两个模型的识别准确率。可能发现,VGG模型相对于LeNet-5模型在手写数字识别任务上具有更高的准确性,这是由于VGG模型具有更深的网络结构和更多的参数,能够更好地提取手写数字图像的复杂特征。 总结而言,利用卷积神经网络实现手写数字识别,我们可以选择不同的网络结构进行训练和测试。根据实际测试结果可以得出,深层网络模型(如VGG模型)相对于浅层网络模型(如LeNet-5模型)在手写数字识别任务上可能表现更好,因为深层模型对于提取图像特征具有更强的能力。 ### 回答3: 卷积神经网络(Convolutional Neural Network,CNN)是一种在图像识别领域中广泛应用的深度学习算法。实现手写数字识别的一种常见方法是使用CNN。 对于手写数字识别任务,我们可以使用两种不同的CNN进行训练并比较测试结果。下面将分别介绍这两种网络的具体实现。 第一种CNN网络采用了经典的LeNet-5结构,该网络由卷积层、池化层和全连接层组成。输入层接收手写数字的像素图像,经过卷积和池化层的特征提取后,通过全连接层进行分类并输出识别结果。 第二种CNN网络采用了更深层次的结构,例如VGGNet或ResNet等。这些网络深度更深、参数更多,可以更好地捕获图像中的细节特征。同样,输入层接收手写数字的像素图像,但中间的卷积层和池化层更深,全连接层用于分类和输出识别结果。 这两种网络的训练过程类似,都是通过大量手写数字图像进行迭代训练,通过反向传播算法调整权重参数,使得网络能够更好地识别手写数字。 在进行了相同数量的训练迭代后,我们可以通过对一批新的手写数字图像进行测试来比较这两种网络的性能。我们可以评估它们的准确率、召回率和F1分数等指标,来判断它们在手写数字识别任务上的表现。 综上所述,利用卷积神经网络实现手写数字识别是一种常见的方法。我们可以使用不同的CNN网络进行训练,并通过测试数据来比较它们的性能,从而选择合适的网络结构用于手写数字识别。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值