(一)人工智能,深度学习,卷积神经网络,keras 自己制作数据集 ,自己搭建模型, 对多种类图片进行分类 ,史上最详细。 tensorflow,小白必备,代码讲解,多图预警,

阶段性工作总结吧, 按照我下面的方法。最白的小白,也能轻松上车,因为它无敌般的详细

1 准备工作

1, 首先配置keras程序环境,具体安装步骤可参考我上一篇博客:
https://blog.csdn.net/a721867783/article/details/89682176

2, 数据集准备: 我们把多种图片,按种类划分分为不同的文件夹。 也就是每个种类文件夹只包含一个种类的图片。让后把所有文件夹放入一个文件夹下命名为train。具体如下图
在这里插入图片描述
在这里插入图片描述
如上图所示,一共有四类,每一个文件夹一类图片。建议每一类多取图片数据集越大越好。

2 数据集制备

本此数据集制备采用步骤如下:
(1),采用random.shuffle(),函数对原始数据的顺序进行随机打乱,增加模型输入的随机性,提高模型的鲁棒性,更好的训练模型。(其实也没啥大用,装个门面罢了)。

(2),
在这里插入图片描述
如图所示将图片转化为数组的格式,然后放到data数组中。然后通过下图中的程序来转化为,图片分类专用格式

(3),数据集的标签数据label,由文件夹的名字分类而来。所以文件夹的名字,代表就是训练中种类的名字。

完整代码如下:

def load_data(path):
    print("[INFO] loading images...")
    data = []
    labels = []
    # grab the image paths and randomly shuffle them
    imagePaths = sorted(list(paths.list_images(path)))
    #顺序打乱
    random.seed(42)
    random.shuffle(imagePaths)
    # loop over the input images
    for imagePath in imagePaths:
        # load the image, pre-process it, and store it in the data list
        image = cv2.imread(imagePath)
        #图像插值大小为240*240
        image = cv2.resize(image, (240, 240))
        image = img_to_array(image)
        data.append(image)

        # extract the class label from the image path and update the
        # labels list
        label = int(imagePath.split(os.path.sep)[-2])       
        labels.append(label)
    
    # scale the raw pixel intensities to the range [0, 1]
    data = np.array(data, dtype="float") / 255.0
    labels = np.array(labels)
    print(labels)

    # convert the labels from integers to vectors
    labels = to_categorical(labels)  
    print(labels)                       
    return data,labels


if __name__=='__main__':
   # args = args_parse()
 #   file_path = args["dataset"]
    trainX,trainY = load_data('D:\\sort1\\sort\\')
    print('数据载入')

提示:要修改的只有trainX,trainY = load_data(‘D:\sort1\sort\’)文件夹路径就可以。

提示:文件夹的名字为类的名字,不要随便搞名字

3 网络模型 搭建

网络模型仿照dense-net网络模型搭建,本网络模型采用,输入层,卷积层,池化层,全连接层,输出层,本模型的激活函数设置为relu、softmax,等。行啦!!,这也没啥好说的,不懂得直接复制代码,一个字都不用改直接拿来用了就ok。想具体了解深入交流的加我qq。

好啦!!,废话少说上模型代码

input1  = Input(shape=(240,240,3),name = 'input1')



#############定义多输入
x1 = Conv2D(32, (3, 3),padding='same' )(input1)#input is height,width,deep
x1 = Activation('relu')(x1)
x1 = Conv2D(32, (3, 3),padding='same')(x1)
x1 = Activation('relu')(x1)
x1 = MaxPooling2D(pool_size=(2, 2),strides = (2, 2))(x1)
x1 = Conv2D(48, (3, 3),padding='same')(x1)
x1 = Activation('relu')(x1)
x1 = Conv2D(48, (3, 3),padding='same')(x1)
x1 = Activation('relu')(x1)
x1 = MaxPooling2D(pool_size=(2, 2),strides = (2, 2))(x1)
x1 = Conv2D(64, (3, 3),padding='same')(x1)
x1 = Activation('relu')(x1)
x1 = Conv2D(64, (3, 3),padding='same')(x1)
x1 = Activation('relu')(x1)
x1 = MaxPooling2D(pool_size=(2, 2),strides = (2, 2))(x1)
# the model so far outputs 3D feature maps (height, width, features)
#base_model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
x1 = Flatten()(x1)
x1 = Dense(256)(x1)
x1 = Activation('relu')(x1)
x1 = Dropout(0.7)(x1)
category_predict1 = Dense(100, activation='relu', name='category_predict1')(x1)
output = Dense(5,activation='softmax', name='output')(category_predict1)
model = Model(inputs=[input1], outputs=[output])
model.compile(optimizer=Adam(lr=0.0001),
              loss='categorical_crossentropy',  
              metrics=['accuracy'])

#训练参数调整
print('开始训练')
H = model.fit([trainX],[trainY],epochs=2,batch_size=2)



#保存模型
print('info:saving model.....')
model.save('model_sort.h5')
print('训练完成')

提示:input1 = Input(shape=(240,240,3),name = ‘input1’)其中shape是调节输入网络模型的尺寸,分别为图片的高和宽,根据需要自己调整。

提示:(optimizer=Adam(lr=0.0001),lr为学习率,可调节模型收敛速度的快慢,但是学习率太大精度降低,学习率太小收敛速度较慢。适中就行

提示:model.save(‘model_sort.h5’),修改模型储存的名字

提示:H = model.fit([trainX],[trainY],epochs=2,batch_size=2),epoch修改的为训练次数,一般情况下次数越多,训练精度越高。batch_size修改的为每次训练时送入网络模型的图片多少,土豪配置可以调的大,屌丝配置调到最低,到最低还跑不了,建议换电脑

在训练模型的之前所有导入库如下:

from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
from sklearn.model_selection import train_test_split
from keras.preprocessing.image import img_to_array
from keras.utils import to_categorical
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import random
import cv2
import os
import sys
from keras.models import Model
#from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import random
import cv2
import os
import sys
#定义模型
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense, Input,Concatenate
sys.path.append('..')

提示:多导入没事少了就不行就像男人,钱多了在多了感觉不出来什么大变化,少了就完蛋。

在这里插入图片描述
扯淡: 男人就要向他学习,勤于思考。

4 准确率损失曲线绘制

准确率损失曲线,使我们模型训练结果的重要参考,也是我们像导师,领导交差的主要成果。所以在这里也分享下把。准确率,顾名思义为模型预测准确率。损失曲线与其想法,越低越好。

print('准确率损失曲线绘制')
plt.style.use('ggplot')
plt.figure()
N = 2
fig =  plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
#plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), H.history["acc"], label="train_acc")
#plt.plot(np.arange(0, N), H.history["val_acc"], label="val_acc")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")
plt.show()
plt.savefig('a.jpg',dpi=800)
print('绘制完成')

提示:N的个数随着训练次数的变化而变化,并且二者相等

有空在下篇博客再写下测试代码吧

主要参考:原创
https://www.cnblogs.com/skyfsm/p/8051705.html
有需要深入交流的或者代做的给我私信。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值