预训练的图像分类模型_使用预训练模型进行图像分类

预训练的图像分类模型

预训练的图像分类模型

使用预训练模型进行图像分类 (Image Classification Using Pre-Trained Model)

In this lesson, you will learn to use a pre-trained model to detect objects in a given image. You will use squeezenet pre-trained module that detects and classifies the objects in a given image with a great accuracy.

在本课程中,您将学习使用预先训练的模型来检测给定图像中的对象。 您将使用squeezenet预训练模块,该模块可以非常精确地检测和分类给定图像中的对象。

Open a new Juypter notebook and follow the steps to develop this image classification application.

打开一个新的Juypter笔记本,并按照以下步骤开发此图像分类应用程序。

导入库 (Importing Libraries)

First, we import the required packages using the below code −

首先,我们使用以下代码导入所需的软件包-


from caffe2.proto import caffe2_pb2
from caffe2.python import core, workspace, models
import numpy as np
import skimage.io
import skimage.transform
from matplotlib import pyplot
import os
import urllib.request as urllib2
import operator

Next, we set up a few variables

接下来,我们设置一些变量 -


INPUT_IMAGE_SIZE = 227
mean = 128

The images used for training will obviously be of varied sizes. All these images must be converted into a fixed size for accurate training. Likewise, the test images and the image which you want to predict in the production environment must also be converted to the size, the same as the one used during training. Thus, we create a variable above called INPUT_IMAGE_SIZE having value 227. Hence, we will convert all our images to the size 227x227 before using it in our classifier.

用于训练的图像显然将具有各种尺寸。 所有这些图像都必须转换为固定大小才能进行精确训练。 同样,测试图像和要在生产环境中预测的图像也必须转换为尺寸,与训练期间使用的尺寸相同。 因此,我们在上面创建了一个名为INPUT_IMAGE_SIZE的变量,其值为227 。 因此,我们将在分类器中使用图像之前将所有图像转换为227x227尺寸。

We also declare a variable called mean having value 128, which is used later for improving the classification results.

我们还声明了一个名为mean的变量,其值为128 ,以后将用于改进分类结果。

Next, we will develop two functions for processing the image.

接下来,我们将开发两个功能来处理图像。

图像处理 (Image Processing)

The image processing consists of two steps. First one is to resize the image, and the second one is to centrally crop the image. For these two steps, we will write two functions for resizing and cropping.

图像处理包括两个步骤。 第一个是调整图像大小,第二个是集中裁剪图像。 对于这两个步骤,我们将编写两个用于调整大小和裁剪的函数。

图像调整大小 (Image Resizing)

First, we will write a function for resizing the image. As said earlier, we will resize the image to 227x227. So let us define the function resize as follows −

首先,我们将编写一个用于调整图像大小的函数。 如前所述,我们将图像调整为227x227 。 因此,让我们定义函数调整大小 ,如下所示:


def resize(img, input_height, input_width):

We obtain the aspect ratio of the image by dividing the width by the height.

我们通过将宽度除以高度来获得图像的长宽比。


original_aspect = img.shape[1]/float(img.shape[0])

If the aspect ratio is greater than 1, it indicates that the image is wide, that to say it is in the landscape mode. We now adjust the image height and return the resized image using the following code −

如果宽高比大于1,则表示图像很宽,也就是说处于风景模式。 现在,我们使用以下代码调整图像高度并返回调整后的图像大小-


if(original_aspect>1):
   new_height = int(original_aspect * input_height)
   return skimage.transform.resize(img, (input_width,
   new_height), mode='constant', anti_aliasing=True, anti_aliasing_sigma=None)

If the aspect ratio is less than 1, it indicates the portrait mode. We now adjust the width using the following code −

如果宽高比小于1 ,则表示纵向模式 。 现在我们使用以下代码调整宽度-


if(original_aspect<1):
   new_width = int(input_width/original_aspect)
   return skimage.transform.resize(img, (new_width,
   input_height), mode='constant', anti_aliasing=True, anti_aliasing_sigma=None)

If the aspect ratio equals 1, we do not make any height/width adjustments.

如果宽高比等于1 ,则我们不进行任何高度/宽度调整。


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值