Few-sample and high-accuracy cat species identification based on CNN+data enhancement+residual network Resnet50—deep learning algorithm application (including all engineering source codes)+dataset+model (5)

Table of Contents of Series Articles

Few-sample and high-accuracy cat species identification based on CNN+data enhancement+residual network Resnet50—deep learning algorithm application (including all engineering source codes)+dataset+model (1)

Few-sample and high-accuracy cat species identification based on CNN+data enhancement+residual network Resnet50—deep learning algorithm application (including all engineering source codes)+dataset+model (2)

Few-sample and high-accuracy cat species identification based on CNN+data enhancement+residual network Resnet50—deep learning algorithm application (including all engineering source codes)+dataset+model (3)

Few-sample and high-accuracy cat species identification based on CNN+data enhancement+residual network Resnet50—deep learning algorithm application (including all engineering source codes)+dataset+model (4)

Few-sample and high-accuracy cat species identification based on CNN+data enhancement+residual network Resnet50—deep learning algorithm application (including all engineering source codes)+dataset+model (5)

Few-sample and high-accuracy cat species identification based on CNN+data enhancement+residual network Resnet50—deep learning algorithm application (including all engineering source codes)+dataset+model (6)



Insert image description here

Preface

This project is based on the convolutional neural network (CNN) model to train the collected cat image data. By using data augmentation technology and combining residual networks, it aims to improve the performance of the model to achieve accurate identification of different cat types.

First, the project utilizes a CNN model, a deep learning model specifically designed for image recognition tasks. Through multiple convolution and pooling layers, this model can effectively capture features in images and provide powerful learning capabilities for cat category recognition.

Secondly, by training on the collected data, this project aims to build a model that can accurately identify cat types. Images of a variety of cats are included to ensure the model can generalize to different species and scenarios.

To further improve model performance, data augmentation techniques were employed. Data augmentation generates more variants by rotating, flipping, scaling and other operations on the images in the training set, which helps the model better adapt to different viewing angles and conditions.

At the same time, introducing the idea of ​​residual network can help solve the problem of gradient disappearance in deep network training and improve the training effect of the model. This combined approach makes the model more robust and accurate.

Finally, through this project, the goal of accurately identifying cat types was achieved. This has the potential for practical application in the pet field, zoological research, etc., providing an efficient and reliable tool for related fields.

overall design

This part includes the overall system structure diagram and system flow chart.

Overall system structure diagram

The overall structure of the system is shown in the figure.

Insert image description here

System flow chart

The system flow is shown in the figure.

Insert image description here

Operating environment

This part includes computing cloud servers, Python environment, TensorFlow environment and MySQL environment.

详见行客.

Module implementation

This project includes 5 modules: data preprocessing, data enhancement, ordinary CNN model, residual network model, and model generation. The function introduction and related codes of each module are given below.

1. Data preprocessing

Open your browser and search for pictures of Ragdoll, Bombay, Siamese and British Shorthair cats. Use the batch downloader to download images and select images with obvious features as a data set. The pictures used include 101 Ragdoll cats, 97 Bombay cats, 101 Parrot cats and 85 British Shorthair cats, for a total of 384 pictures. (Among them /cat_kind_model/cat_data_100 and /cat_kind_model/cat_data_224 in the project code can also be downloaded)

详见行客.

2. Data enhancement

The so-called data enhancement is to expand the existing data set through operations such as flipping, rotating, scaling, random cropping, shifting, and adding noise. The amount of data in this project is small and it is impossible to extract the deep features of the image. When using a deep residual network, it is easy to cause the model to overfit.

详见行客.

3. Ordinary CNN model

After processing the image data format, convert it into an array as input to the model, extract tags based on the file name, and define the model structure, optimizer, loss function, and performance indicators. This project uses Keras to provide a convolutional neural network similar to VGG.

1) Model structure

详见行客.

2) Model optimization

详见行客.

3) Model training

详见行客.

4) Model saving

详见行客.

4. Residual network model

This section includes an introduction to residual networks, model structure, and model training.

1) Introduction to residual network

详见行客.

2) Model structure

详见行客.

3) Model training

详见行客.

4) Model saving

详见行客.

5. Model generation

There are three main parts to the model application: one is to input cat pictures from the local photo album; the other is to convert the input pictures into data and make predictions in the input trained model; the third is to output relevant information pre-stored in the database based on the prediction results.

1) Build a Django project

Use the shortcut keyswin+R, entercmd and press Enter to open the command line.

Enter the virtual environment:

cd H: # anaconda的安装路径
cd ProgramData\Anaconda3\Scripts
activate tensorflow

Create a Django project:

django-admin startproject cat_kind_view

Enter the project directory and create an application named cat:

cd cat_kind_view
python manage.py startapp cat

Open the created project with PyCharm, as shown in the figure.

Insert image description here

Add the content you want to display on the webpage in thecat_kind_view/cat/views.py file. The relevant code is as follows:

from django.shortcuts import render  #导入各种模块
from django.http import HttpResponse
from catclass import settings
from . import models
from .prediction import testcat
import json
#创建视图
def index(request):
    return render(request, 'index.html')
def catinfo(request):
    if request.method == "POST":
        f1 = request.FILES['pic1']
        #用于识别
        fname = '%s/pic/%s' % (settings.MEDIA_ROOT, f1.name)
        with open(fname, 'wb') as pic:
            for c in f1.chunks():
                pic.write(c)
        #用于显示
        fname1 = './static/img/%s' % f1.name
        with open(fname1, 'wb') as pic:
            for c in f1.chunks():
                pic.write(c)
        num = testcat(f1.name)
        if(num == 0):
            num = 4
        #通过ID获取猫的信息
        name = models.Catinfo.objects.get(id = num)
        return render(request, 'info.html', {
    
    'nameinfo': name.nameinfo, 'feature': name.feature, 'livemethod': name.livemethod, 'feednn': name.feednn, 'feedmethod': name.feedmethod, 'picname': f1.name})
    else:
        return HttpResponse("上传失败!")
def appupload(request):
    if request.method == "POST":
        f1 = request.FILES['pic1']
        #用于识别
        fname = '%s/pic/%s' % (settings.MEDIA_ROOT, f1.name)
        with open(fname, 'wb') as pic:
            for c in f1.chunks():
                pic.write(c)
        #用于显示
        fname1 = './static/img/%s' % f1.name
        with open(fname1, 'wb') as pic:
            for c in f1.chunks():
                pic.write(c)
        num = testcat(f1.name)
        if (num == 0):
            num = 4
        #通过ID获取猫的信息
        name = models.Catinfo.objects.get(id = num)
        return render(request, 'app.html', {
    
    'nameinfo': name.nameinfo, 'feature': name.feature, 'livemethod': name.livemethod, 'feednn': name.feednn, 'feedmethod': name.feedmethod, 'picname': f1.name})
    else:
        return HttpResponse("上传失败!")
#在cat_kind_view/cat目录下新建urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
    url(r'^$', views.index),
    url(r'^info/', views.catinfo),
    url(r'^appupload/', views.appupload)
]
#在cat_kind_view/ cat_kind_view/urls.py文件中加上新建的urls
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('cat.urls')),  #连接到cat/urls
]
#在cat_kind_view/cat_kind_view/settings.py中做如下改动
#定义应用
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'cat'  # 加上自己的APP
]
#LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-Hans'    #修改语言为汉语
# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'  #修改时区为上海

2) Input the picture and predict

Create a folder to save the input images and add to the cat_kind_view/cat directory. The function of this file is to process the input image and make predictions using the trained model. prediction.py

import os  #导入各种模块
from PIL import Image
import numpy as np
import keras
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Flatten
from keras.optimizers import SGD
from keras.layers import Conv2D, MaxPooling2D
def prepicture(picname):  #定义预处理
    img = Image.open('./media/pic/' + picname)
    new_img = img.resize((100, 100), Image.BILINEAR)
    new_img.save(os.path.join('./media/pic/', os.path.basename(picname)))
def read_image2(filename):
    img = Image.open('./media/pic/' + filename).convert('RGB')
    return np.array(img)
def testcat(picname):
    #预处理图片变成100*100
    prepicture(picname)
    x_test = []
    x_test.append(read_image2(picname))
    x_test = np.array(x_test)
    x_test = x_test.astype('float32')
    x_test /= 255
    keras.backend.clear_session()
    model = Sequential()
    #输入为三通道的100*100图像
    #应用32个卷积滤波器,每个大小为3*3
    model.add(Conv2D(32,(3,3),activation='relu',input_shape=(100,100,3)))
    model.add(Conv2D(32, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(256, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4, activation='softmax'))
    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
    model.load_weights('./cat/cat_weight.h5')    #使用训练好的模型预测
    classes = model.predict_classes(x_test)[0]
    #target = ['布偶猫', '孟买猫', '暹罗猫', '英国短毛猫']
    #print(target[classes])
    return classes

3) Link to database

The relevant code for this part is as follows:

#在cat_kind_view/cat_kind_view/settings.py
DATABASES = {
    
    
    'default': {
    
    
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'catkind',     #数据库名
        'USER': 'root',         #用户名
        'PASSWORD': 'root',    #密码
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}
#在cat_kind_view/cat /__init__.py中引入数据库
import pymysql
pymysql.version_info = (1, 3, 13, "final", 0)
pymysql.install_as_MySQLdb()
#在cat_kind_view/cat /__init__.py中写入数据库中的名称和格式
from django.db import models
#创建模型
class Catinfo(models.Model):
    name = models.CharField(max_length=10)
    nameinfo = models.CharField(max_length=1000)
    feature = models.CharField(max_length=1000)
    livemethod = models.CharField(max_length=1000)
    feednn = models.CharField(max_length=1000)
    feedmethod = models.CharField(max_length=1000)

Enter the following command in the command line terminal to generate the migration file0001_initial.py.

python manage.py makemigrations

Enter the following command in the command line terminal to perform migration and generate the corresponding table in the database.

python manage.py migrate

Open the database inNavicat for MySQL and find that some lists are generated by Django, as shown in the figure,cat_catinfo is generated by the catinfo class in the application cat surface.

Insert image description here

Add relevant information about different types of cats to the tablecat_catinfo, as shown in the figure.

Insert image description here

This information corresponds one-to-one with the primary key ID based on the prediction results (labels), and displays the information in the table on the web page.

4) Beautify the web page

The relevant code for this part is as follows:

#在cat_kind_view/cat_kind_view/settings.py 
TEMPLATES = [
    {
    
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],  #加入模板路径
        'APP_DIRS': True,
        'OPTIONS': {
    
    
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
#静态文件(CSS, JavaScript, Images)
#参考https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')  #加上static路径,界面美化
]
MEDIA_ROOT = os.path.join(BASE_DIR, "./media") #加上media路径,界面美化

The code directory for beautifying the web page is shown in the figure.

Insert image description here

static/css/style.cssSet the format of the homepage, such as background, font, type and color of each component, etc.

body,ul,li,dl,dt,dd,p,ol,h1,h2,h3,h4,h5,h6,form,img,table,fieldset,legend {
    
    
	margin: 0;
	padding: 0;
}
html,body {
    
    
	height: 100%;
	width: 100%;
}
ul,li,ol {
    
    
	list-style: none;
}
img,fieldset {
    
    
	border: 0;
}
img {
    
    
	display: block;
}
a {
    
    
	text-decoration: none;
	color: #333;
}
h1,h2,h3,h4,h5,h6 {
    
    
	font-weight: 100;
}
body {
    
    
	font-size: 12px;
	font-family: "微软雅黑";
}
input,a {
    
    
	outline: none;
}
* {
    
    
	box-sizing: border-box;
}
.bc {
    
    
	background: url(../img/image001.jpg) no-repeat;
	background-size: 100% 100%;
}
.main1 {
    
    
	width: 100%;
	height: 100%;
	padding-top: 100px;
	padding-left: 100px;
}
.main {
    
    
	width: 600px;
	height: 500px;
	background-color: rgba(255,255,255,0.3);
	box-shadow: 0px 0px 5px 5px #888888;
}
.title {
    
    
	width: 100%;
	height: 55px;
	padding-top: 20px;
}
.title p {
    
    
	font-size: 30px;
	text-align: center;
}
.select {
    
    
	width: 100%;
	height: 200px;
}
.xfile {
    
    
	width: 200px;
	height: 100px;
	display: none;
	border: 0px;
}
.xxfile,.xxsub {
    
    
	width: 300px;
	height: 60px;
	background-color: rgba(0,0,0,0.5);
	margin-top: 50px;
	margin-left: 150px;
	font-size: 25px;
	border-radius: 10px;
	border: 0px;
	color: white;
	font-family: "微软雅黑";
	transition: linear 0.2s;
}
.xxfile:hover,.xxsub:hover {
    
    
	color: black;
	background-color: rgba(255,255,255,0.5);
}
.select .xxsub {
    
    
	margin-top: 30px;
}
.foot {
    
    
	width: 100%;
	height: 245px;
	padding-top: 30px;
}
.foot ul {
    
    
	display: flex;
	justify-content: space-around;
}
.foot dl {
    
    
	height: 30px;
	font-size: 16px;
	line-height: 30px;
}
.foot dd {
    
    
	height: 40px;
	font-size: 26px;
	line-height: 40px;
}

static/css/info.cssSet the information page format, such as background, font, type of each component, color, etc.

body,ul,li,dl,dt,dd,p,ol,h1,h2,h3,h4,h5,h6,form,img,table,fieldset,legend {
    
    
	margin: 0;
	padding: 0;
}
ul,li,ol {
    
    
	list-style: none;
}
img,fieldset {
    
    
	border: 0;
}
img {
    
    
	display: block;
}
a {
    
    
	text-decoration: none;
	color: #333;
}
h1,h2,h3,h4,h5,h6 {
    
    
	font-weight: 100;
}
body {
    
    
	font-size: 12px;
	font-family: "微软雅黑";
	color: black;
}
input,a {
    
    
	outline: none;
}
* {
    
    
	box-sizing: border-box;
}
body {
    
    
	background: url(../img/lianzhengaijiujiu003.jpg) no-repeat;
	background-size: cover;
}
.top {
    
    
	width: 1000px;
	height: 40px;
	background-color: cornflowerblue;
	margin: 0 auto;
	line-height: 40px;
	font-size: 24px;
	color: white;
	padding-left: 20px;
}
.m1 {
    
    
	width: 1000px;
	height: 210px;
	margin: 0 auto;
	margin-top: 20px;
	display: flex;
	justify-content: space-between;
}
.catlogo {
    
    
	width: 210px;
	height: 210px;
	padding: 4px;
	border: 1px solid gray;
}
.catlogo img {
    
    
	width: 200px;
	height: 200px;
}
.catname {
    
    
	width: 770px;
	font-size: 18px;
	padding: 10px;
	text-align: justify;
	background: rgba(255,255,255,0.7);
	border-radius: 10px;
}
.m2,.m3 {
    
    
	width: 1000px;
	height: 400px;
	display: flex;
	justify-content: space-between;
	margin: 0 auto;
	margin-top: 10px;
}
.feature {
    
    
	width: 400px;
	height: 400px;
	text-align: justify;
	background: rgba(255,255,255,0.7);
	border-radius: 10px;
}
.live {
    
    
	width: 560px;
	height: 400px;
	text-align: justify;
	background: rgba(255,255,255,0.7);
	border-radius: 10px;
}
.featt,.maintitle,.methodtitle {
    
    
	width: 100%;
	height: 40px;
	line-height: 40px;
	font-size: 24px;
	color: white;
	padding-left: 20px;
	background: cornflowerblue;
}
.nn {
    
    
	font-size: 18px;
	margin: 8px;
}
.m3 {
    
    
	height: 450px;
}
.feedmethod {
    
    
	width: 510px;
	height: 440px;
	text-align: justify;
	background: rgba(255,255,255,0.7);
	border-radius: 10px;
}
.feedmain {
    
    
	width: 450px;
	height: 440px;
	text-align: justify;
	background: rgba(255,255,255,0.7);
	border-radius: 10px;
}
.footcat {
    
    
	position: fixed;
	right: 0;
	bottom: 0;
	width: 200px;
	height: 300px;
}

static/templates/index.htmlContent displayed on the homepage:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="/static/css/style.css" />
<script type="text/javascript" src="/static/js/cat.js" ></script>
<title>猫的种类识别</title>
</head>
<body class="bc">
<div class="main1">
<div class="main">
	<div class="title">
		<p>The&nbsp;&nbsp;&nbsp;Kinds&nbsp;&nbsp;&nbsp;Of&nbsp;&nbsp;&nbsp;Cats&nbsp;&nbsp;&nbsp;Identification</p>
	</div>
	<div class="select">
		<form action="/info/" method="post" enctype="multipart/form-data">
            {% csrf_token %}
			<input type="file" class="xfile" id="btn_file" name="pic1"/>
			<button type="button" onclick="F_Open_dialog()" class="xxfile">选择图片</button>
			<br />
			<input type="submit" class="xxsub" value="种类识别"/>
		</form>
	</div>
	<div class="foot">
		<ul>
		<li>
			<ol>
				<dd>识别的种类</dd>
				<dl>布偶猫</dl>
				<dl>孟买猫</dl>
				<dl>暹罗猫</dl>
				<dl>英国短毛猫</dl>
			</ol>
		</li>
		<li>
			<ol>
				<dd>应用说明</dd>
				<dl>上传图片均为jpg</dl>
				<dl>识别精度大约90%以上</dl>
			</ol>
		</li>
		</ul>
	</div>
</div>
</div>
</body>
</html>

static/templates/app.htmlWhat the information page displays:

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title></title>
<script src="/static/js/mui.min.js"></script>
<link href="/static/css/mui.min.css" rel="stylesheet"/>
<link rel="stylesheet" type="text/css" href="/static/css/appinfo.css"/>
</head>
<body>
<header class="mui-bar mui-bar-nav ihead">
    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left iha"></a>
    <h1 class="mui-title">识别结果</h1>
</header>
<div class="mui-scroll-wrapper">
<div class="mui-scroll">
<div class="ifhead ihh">种类</div>
<div class="ipic">
    <img src="/static/img/{
     
     { picname }}" />
    <div class="iname">
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
   
   { nameinfo }}</p>
    </div>
</div>
<div class="ifhead">外形特征</div>
<div class="ifeature">
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
   
   { feature }}</p>
</div>
<div class="ifhead">生活习性</div>
<div class="ifeature">
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
   
   { livemethod }}</p>
</div>
<div class="ifhead">饲养须知</div>
<div class="ifeature">
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
   
   { feednn }}</p>
</div>
<div class="ifhead">饲养方法</div>
<div class="ifeature">
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
   
   { feedmethod }}</p>
</div>
</div>
</div>
<script type="text/javascript" charset="utf-8">
mui.init();
mui('.mui-scroll-wrapper').scroll({
      
      
	deceleration: 0.0005 //flick 减速系数,系数越大,滚动速度越慢,滚动距离越小,默认值0.0006
});
</script>
</body>
</html>

Other related blogs

Few-sample and high-accuracy cat species identification based on CNN+data enhancement+residual network Resnet50—deep learning algorithm application (including all engineering source codes)+dataset+model (1)

Few-sample and high-accuracy cat species identification based on CNN+data enhancement+residual network Resnet50—deep learning algorithm application (including all engineering source codes)+dataset+model (2)

Few-sample and high-accuracy cat species identification based on CNN+data enhancement+residual network Resnet50—deep learning algorithm application (including all engineering source codes)+dataset+model (3)

Few-sample and high-accuracy cat species identification based on CNN+data enhancement+residual network Resnet50—deep learning algorithm application (including all engineering source codes)+dataset+model (4)

Few-sample and high-accuracy cat species identification based on CNN+data enhancement+residual network Resnet50—deep learning algorithm application (including all engineering source codes)+dataset+model (6)

Project source code download

For details, please see my blog resource download page


Download other information

If you want to continue to understand the learning routes and knowledge systems related to artificial intelligence, you are welcome to read my other blog " Heavy | Complete Artificial Intelligence AI Learning - Basics Knowledge learning route, all materials can be downloaded directly from the network disk without following any routines
This blog refers to Github’s well-known open source platform, AI technology platform and experts in related fields : Datawhale, ApacheCN, AI Youdao and Dr. Huang Haiguang have about 100G of related information. I hope it can help all my friends.

Guess you like

Origin blog.csdn.net/qq_31136513/article/details/134990475