由于猫和狗的数据在这里,所以就做了一下分类的神经网络

1、首先进行图像处理:

import csv
import glob
import os
import randomos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as npgpus = tf.config.experimental.list_physical_devices(device_type='GPU')
for gpu in gpus:tf.config.experimental.set_memory_growth(device=gpu, enable=True)# 加载处理数据集def load_csv(root, filename, name2label):# 从csv文件返回images,labels列表# root:数据集根目录,filename:csv文件名, name2label:类别名编码表if not os.path.exists(os.path.join(root, filename)):# 如果csv文件不存在,则创建images = []for name in name2label.keys(): # 遍历所有子目录,获得所有的图片# 只考虑后缀为png,jpg,jpeg的图片:'pokemon\\mewtwo\\00001.pngimages += glob.glob(os.path.join(root, name, '*png'))  # glob.glob()字符串匹配images += glob.glob(os.path.join(root, name, '*.jpg'))images += glob.glob(os.path.join(root, name, '*.jpeg'))# 打印数据集信息:1167, 'pokemon\\bulbasaur\\00000000.png'print(len(images), images)random.shuffle(images)  # 随机打散顺序# 创建csv文件,并存储图片路径及其label信息with open(os.path.join(root, filename), mode='w', newline='') as f:writer = csv.writer(f)for img in images:name = img.split(os.sep)[-2]  # 倒数第二个元素(就是name)label = name2label[name]writer.writerow([img, label])print('written into csv file:', filename)# 此时已经有csv文件,直接读取images, labels = [], []with open(os.path.join(root, filename)) as f:reader = csv.reader(f)for row in reader:# 'pokemon\\bulbasaur\\00000000.png', 0img, label = rowlabel = int(label)images.append(img)labels.append(label)# 返回图片路径list和标签listreturn images, labelsdef load_train(root, mode='train'):# 创建数字编码表name2label = {}  # 'sq...':0# 遍历根目录下的子文件夹,并排序,保证映射关系固定for name in  sorted(os.listdir(os.path.join(root))):# 跳过非文件夹if not os.path.isdir(os.path.join(root, name)):continue# 给每个类别编码一个数字name2label[name] = len(name2label.keys())# 读取Lable信息# [file1,file2],[3, 1]images, labels = load_csv(root, 'image.csv', name2label)if mode == 'train':  # 60%images = images[:int(0.6 * len(images))]labels = labels[:int(0.6 * len(labels))]elif mode == 'val':  # 20% = 60%->80%images = images[int(0.6 * len(images)):int(0.8 * len(images))]labels = labels[int(0.6 * len(labels)):int(0.8 * len(labels))]else:  # 20% = 80%->100%images = images[int(0.8 * len(images)):]labels = labels[int(0.8 * len(labels)):]return images, labels, name2label# 这里的mean和std根据真实的数据计算获得,比如ImageNet
img_mean = tf.constant([0.485, 0.456, 0.406])
img_std = tf.constant([0.229, 0.224, 0.225])def normalize(x, mean=img_mean, std=img_std):# 标准化x = (x-mean)/stdreturn xdef denormalize(x, mean=img_mean, std=img_std):# 标准化的逆过程x = x*std + meanreturn xdef preprocess(x, y):# x: 图片的路径List,y:图片的数字编码Listx = tf.io.read_file(x)  # 根据路径读取图片x = tf.image.decode_jpeg(x, channels=3)  # 图片解码x = tf.image.resize(x, [244, 244])  # 图片缩放# data augmentation(数据增强)# x = tf.image.random_flip_up_down(x)  # 上下翻转x = tf.image.random_flip_left_right(x)  # 左右翻转x = tf.image.random_crop(x, [224, 224, 3])x = tf.cast(x, dtype=tf.float32)/255.# 0~1 => D(0,1)  normalizex = normalize(x)  # 标准化y = tf.convert_to_tensor(y)return x, ydef main():import time# 加载pokemon数据集,指定加载训练集images, labels, table = load_train('train1', 'train')print('images:', len(images), images)print('labels:', len(labels), labels)print('table:', table)# images: string path# labels: numberdb = tf.data.Dataset.from_tensor_slices((images, labels))db = db.shuffle(1000).map(preprocess).batch(32)# 创建TensorBoard(可视化)对象writter = tf.summary.create_file_writer('logs')for step, (x, y) in enumerate(db):# x:[32, 224, 224, 3]# y:[32]with writter.as_default():x = denormalize(x)  # 反向normalize,方便可视化# 写入图片数据tf.summary.image('img', x, step=step, max_outputs=9)time.sleep(3)if __name__ == '__main__':main()

1.将png转化为vsc文件格式

import osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers,Sequentialtf.random.set_seed(22)
np.random.seed(22)assert tf.__version__.startswith('2.')class ResnetBlock(keras.Model):def __init__(self, channels, strides=1):super(ResnetBlock, self).__init__()self.channels = channelsself.strides = stridesself.conv1 = layers.Conv2D(channels, (3, 3), strides=strides,padding='same')self.bn1 = keras.layers.BatchNormalization()self.conv2 = layers.Conv2D(channels, (3, 3), strides=1,padding='same')self.bn2 = keras.layers.BatchNormalization()if strides != 1:self.down_conv = layers.Conv2D(channels, (1, 1), strides=strides)self.down_bn = tf.keras.layers.BatchNormalization()def call(self, inputs, training=None):residual = inputsx = self.conv1(inputs)x = tf.nn.relu(x)x = self.bn1(x, training=training)x = self.conv2(x)x = tf.nn.relu(x)x = self.bn2(x, training=training)# 残差连接if self.strides != 1:residual = self.down_conv(inputs)residual = tf.nn.relu(residual)residual = self.down_bn(residual, training=training)x = x + residualx = tf.nn.relu(x)return xclass ResNet(keras.Model):def __init__(self, num_classes, initial_filters=16):super(ResNet, self).__init__()# self.stem = layers.Conv2D(initial_filters, (3, 3), strides=3, padding='valid')self.stem = Sequential([layers.Conv2D(initial_filters, (3, 3), strides=3, padding='valid'),layers.BatchNormalization(),layers.Activation('relu'),layers.MaxPool2D(pool_size=[2, 2], strides=(1, 1), padding='same')])self.blocks = keras.models.Sequential([ResnetBlock(initial_filters * 2, strides=3),ResnetBlock(initial_filters * 2, strides=1),layers.Dropout(rate=0.5),ResnetBlock(initial_filters * 4, strides=3),ResnetBlock(initial_filters * 4, strides=1),layers.Dropout(rate=0.5),ResnetBlock(initial_filters * 8, strides=2),ResnetBlock(initial_filters * 8, strides=1),layers.Dropout(rate=0.5),ResnetBlock(initial_filters * 16, strides=2),ResnetBlock(initial_filters * 16, strides=1),])self.final_bn = layers.BatchNormalization()self.avg_pool = layers.GlobalMaxPool2D()self.fc = layers.Dense(num_classes)def call(self, inputs, training=None):# print('x:',inputs.shape)out = self.stem(inputs)out = tf.nn.relu(out)# print('stem:',out.shape)out = self.blocks(out, training=training)# print('res:',out.shape)out = self.final_bn(out, training=training)# out = tf.nn.relu(out)out = self.avg_pool(out)# print('avg_pool:',out.shape)out = self.fc(out)# print('out:',out.shape)return outdef main():num_classes = 5resnet18 = ResNet(num_classes=5)resnet18.build(input_shape=(4, 224, 224, 3))resnet18.summary()if __name__ == '__main__':main()

2.基于VGG神经网络,进行二分类,由于只训练了400-500张的数据集,正确率只有91%。

import osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import cv2.cv2 as cv
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, optimizers, Sequential
from tensorflow.keras.callbacks import EarlyStopping
from image预处理 import load_train, normalize
from resnet18 import ResNettf.random.set_seed(1234)
np.random.seed(1234)# transfergpus = tf.config.experimental.list_physical_devices(device_type='GPU')
for gpu in gpus:tf.config.experimental.set_memory_growth(device=gpu, enable=True)def preprocess(x, y):# x: 图片的路径List,y:图片的数字编码Listx = tf.io.read_file(x)  # 根据路径读取图片x = tf.image.decode_jpeg(x, channels=3)  # 图片解码x = tf.image.resize(x, [244, 244])  # 图片缩放# data augmentation(数据增强)# x = tf.image.random_flip_up_down(x)  # 上下翻转x = tf.image.random_flip_left_right(x)  # 左右翻转x = tf.image.random_crop(x, [224, 224, 3])x = tf.cast(x, dtype=tf.float32) / 255.# 0~1 => D(0,1)  normalizex = normalize(x)  # 标准化y = tf.convert_to_tensor(y)y = tf.one_hot(y, depth=5)return x, ybatchsz = 16images, labels, _ = load_train('train1', mode='train')
db_train = tf.data.Dataset.from_tensor_slices((images, labels))
db_train = db_train.map(preprocess).shuffle(500).batch(batchsz)images2, labels2, _ = load_train('train1', mode='val')
db_val = tf.data.Dataset.from_tensor_slices((images2, labels2))
db_val = db_val.map(preprocess).batch(batchsz)images3, labels3, _ = load_train('train1', mode='test')
db_test = tf.data.Dataset.from_tensor_slices((images3, labels3))
db_test = db_test.map(preprocess).batch(batchsz)if not os.path.exists(os.path.join(r'D:\甘露\train', 'train.h5')):# 导入已经训练好的经典网络net = keras.applications.VGG19(weights='imagenet', include_top=False, pooling='max')net.trainable = Falsenewnet = Sequential([net,layers.Dense(5)])# resnet = ResNet(5)newnet.build(input_shape=(None, 224, 224, 3))newnet.summary()# 监听指定指标early_stopping = EarlyStopping(monitor='val_accuracy',min_delta=0.001,patience=5  # 连续5次没有增加0.001)newnet.compile(optimizer=optimizers.Adam(1e-3),loss=tf.losses.CategoricalCrossentropy(from_logits=True),metrics=['accuracy'])newnet.fit(db_train, epochs=5, validation_data=db_val, validation_freq=1, callbacks=[early_stopping])newnet.evaluate(db_test)newnet.save('facialmask.h5')print('saved total model.')
else:newnet = tf.keras.models.load_model('facialmask.h5')print('load model from file!')table = ['狗', '猫']x = tf.io.read_file('2.png')  # 根据路径读取图片
img = cv.imread('2.png')
cv.imshow('3', img)
x = tf.image.decode_jpeg(x, channels=3)  # 图片解码
x = tf.image.resize(x, [224, 224])  # 图片缩放x = tf.cast(x, dtype=tf.float32) / 255.
# 0~1 => D(0,1)  normalize
x = normalize(x)  # 标准化
x = tf.reshape(x, [1, 224, 224, 3])logits = newnet(x)
prob = tf.nn.softmax(logits, axis=1)
pred = tf.argmax(prob, axis=1)
pred = tf.cast(pred, dtype=tf.int32)
num = int(pred)
print(table[num])
cv.waitKey(0)

3.训练结果:

由于电脑配置的问题,epoch只能30次

基于VGG的猫狗识别相关推荐

  1. 基于卷积神经网络VGG的猫狗识别

    !有需要本项目的实验源码的可以私信博主! 摘要:随着大数据时代的到来,深度学习.数据挖掘.图像处理等已经成为了一个热门研究方向.深度学习是一个复杂的机器学习算法,在语音和图像识别方面取得的效果,远远超 ...

  2. 猫狗大战——基于TensorFlow的猫狗识别(2)

    微信公众号:龙跃十二 我是小玉,一个平平无奇的小天才! 上篇文章我们说了关于猫狗大战这个项目的一些准备工作,接下来,我们看看具体的代码详解. 猫狗大战--基于TensorFlow的猫狗识别(1) 文件 ...

  3. 猫狗大战——基于TensorFlow的猫狗识别(1)

    微信公众号:龙跃十二 我是小玉,一个平平无奇的小天才! 简介: 关于猫狗识别是机器学习和深度学习的一个经典实例,下来小玉把自己做的基于CNN卷积神经网络利用Tensorflow框架进行猫狗的识别的程序 ...

  4. 基于卷积神经网络(CNN)的猫狗识别

    目录 引言 1.什么是卷积神经网络? 1.1什么是神经网络? 1.2什么是卷积? 2.准备工作 2.1一些知识: 2.2keras 2.3Conv2D 2.4 MaxPooling2D 3.基于卷积神 ...

  5. 毕设:基于CNN卷积神经网络的猫狗识别、狗品种识别(Tensorflow、Keras、Kaggle竞赛)

    基于卷积神经网络的图像识别算法及其应用研究 毕业快一年了,拿出来分享给大家,我和网上唯一的区别就是,我能够同时实现两个方案(猫狗识别和狗品种识别),我当时也是网上各种查,花了2,3个月的时间,一个萝卜 ...

  6. PyTorch实现猫狗识别VGG

    转载:https://magolor.cn/2020/01/14/2020-01-14-blog-01/ Kaggle 猫狗识别比赛地址:https://www.kaggle.com/c/dogs-v ...

  7. 基于kaggle数据集的猫狗识别(超详细版本)

    目录 下载kaggle数据集 创建新的小数据集 构建猫狗分类的小型卷积神经网络 猫狗分类的网络架构 模型的配置 图像的预处理 利用批量生成器拟合模型 绘制精度和损失 结果显示 随机增强后的训练图像显示 ...

  8. 【Deep Learning】基于 Keras 的猫狗分类识别

    基于 Keras 的猫狗分类识别 更新: 本文代码github连接:https://github.com/Sdamu/Keras_pratice    本篇主要实现利用 Keras 来实现 Kaggl ...

  9. 基于卷积神经网络的猫狗识别

    卷积神经网络的猫狗识别 任务需求 环境配置 识别猫狗实例 首先导入库 数据预处理: 读取数据: 定义网络: 调整机器学习率 定义训练过程 训练开始 训练结果 用于测试代码 结果 任务需求 按照 htt ...

最新文章

  1. 获取命令帮助的六种方法
  2. 卸载注册表_系统小技巧:软件卸载不了?这里办法多
  3. 卓语言对泛型类的使用
  4. Oracle中的伪列
  5. c语言指针 r,C语言指针的高级操作
  6. 中海达数据怎么转rinex_GPS_OEM原始数据向Rinex格式转换的方法
  7. 键盘ASCII对照表
  8. go与python的前景_为什么说GO语言是未来前景看好的编程语言
  9. 百度编辑器嵌套秀米编辑器遇到的问题
  10. pcl命名空间:segmentation/extract_clusters.h与segmentation/imp/extract_clusters.hpp
  11. 【深入理解计算机系统】CSAPP-实验四:ArchLab全网最详细
  12. js 获取当前日期的前三个月
  13. quill光标位置插入html,quill编辑器+word文档上传,插入指定位置
  14. git: patch 是什么/ 如何用
  15. android.view.WindowLeaked解决办法
  16. video.js 视频截图、录制、自定义全屏,hls、flv、mp4视频播放
  17. eqq for shell
  18. SQLDER--工具参数--中英文对照
  19. C语言的冒泡排序优化及鸡尾酒排序问题分解知识点
  20. C++课程设计----电话管理系统

热门文章

  1. 歌德巴赫猜想---java
  2. 部门管理中非递归搜索部门以及所管辖下部门
  3. netbean 偶尔无法设置断点问题
  4. 单片机实验四-七段数码管及键盘控制
  5. android学习步骤
  6. 基于激活聚类的后门检测:Detecting Backdoor Attacks on Deep Neural Networks by Activation Clustering
  7. MFC添加加瓦系列一MFC编写的增量更新软件
  8. 广州大学计算机投档分数线,2021年广州大学最低投档分数线及录取位次
  9. python plot画简单的曲线图
  10. php类中遍历中的rewind方法,PHP rewind( )用法及代码示例