当前位置: 首页 > news >正文

bootstrap做网站考试培训

bootstrap做网站,考试培训,网站建设兼职挣多少钱,企业安全文化实现的途径CNN(练习数据集) 1.导包:2.导入数据集:3. 使用image_dataset_from_directory()将数据加载tf.data.Dataset中:4. 查看数据集中的一部分图像,以及它们对应的标签:5.迭代数据集 train_ds&#xff0…

CNN(练习数据集)

  • 1.导包:
  • 2.导入数据集:
  • 3. 使用image_dataset_from_directory()将数据加载tf.data.Dataset中:
  • 4. 查看数据集中的一部分图像,以及它们对应的标签:
  • 5.迭代数据集 train_ds,以便查看第一批图像和标签的形状:
  • 6.使用TensorFlow的ImageDataGenerator类来创建一个数据增强的对象:
  • 7.将数据集缓存到内存中,加快速度:
  • 8. 通过卷积层和池化层提取特征,再通过全连接层进行分类:
  • 9.打印网络结构:
  • 10.设置优化器,定义了训练轮次和批量大小:
  • 11.训练数据集:
  • 12.画出图像:
  • 13.评估您的模型在验证数据集的性能:
  • 14.输出在验证集上的预测结果和真实值的对比:
  • 15.输出可视化报表:

  • 在网上寻找一个新的数据集,自己进行训练

1.导包:

import pandas as pd
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.preprocessing import LabelBinarizer
import matplotlib.pyplot as plt
import pickle
import pathlib
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models

输出结果:
在这里插入图片描述

2.导入数据集:

# 定义超参数
data_dir = "D:\JUANJI"
data_dir = pathlib.Path(data_dir)
image_count = len(list(data_dir.glob('*/*.jpg')))
print("图片总数为:", image_count)
batch_size = 30
img_height = 180
img_width = 180

输出结果:
在这里插入图片描述

3. 使用image_dataset_from_directory()将数据加载tf.data.Dataset中:

#  使用image_dataset_from_directory()将数据加载到tf.data.Dataset中
train_ds = tf.keras.preprocessing.image_dataset_from_directory(data_dir,validation_split=0.2,  # 验证集0.2subset="training",seed=123,image_size=(img_height, img_width),batch_size=batch_size)val_ds = tf.keras.preprocessing.image_dataset_from_directory(data_dir,validation_split=0.2,subset="validation",seed=123,image_size=(img_height, img_width),batch_size=batch_size)

输出结果:
在这里插入图片描述

4. 查看数据集中的一部分图像,以及它们对应的标签:

class_names = train_ds.class_names
print(class_names)
# 可视化
plt.figure(figsize=(16, 8))
for images, labels in train_ds.take(1):for i in range(16):ax = plt.subplot(4, 4, i + 1)# plt.imshow(images[i], cmap=plt.cm.binary)plt.imshow(images[i].numpy().astype("uint8"))plt.title(class_names[labels[i]])plt.axis("off")
plt.show()

输出结果:
在这里插入图片描述
在这里插入图片描述

5.迭代数据集 train_ds,以便查看第一批图像和标签的形状:

for image_batch, labels_batch in train_ds:print(image_batch.shape)print(labels_batch.shape)break

输出结果:
在这里插入图片描述

6.使用TensorFlow的ImageDataGenerator类来创建一个数据增强的对象:

aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1,height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,horizontal_flip=True, fill_mode="nearest")
x = aug.flow(image_batch, labels_batch)
AUTOTUNE = tf.data.AUTOTUNE

输出结果:
在这里插入图片描述

7.将数据集缓存到内存中,加快速度:

train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)

输出结果:
在这里插入图片描述

8. 通过卷积层和池化层提取特征,再通过全连接层进行分类:

# 为了增加模型的泛化能力,增加了Dropout层,并将最大池化层更新为平均池化层
num_classes = 3
model = models.Sequential([layers.experimental.preprocessing.Rescaling(1./255,input_shape=(img_height,img_width, 3)),layers.Conv2D(32, (3, 3), activation='relu'),layers.MaxPooling2D((2, 2)),layers.Conv2D(64, (3, 3), activation='relu'),layers.MaxPooling2D((2, 2)),layers.Conv2D(128, (3, 3), activation='relu'),layers.MaxPooling2D((2, 2)),layers.Conv2D(256, (3, 3), activation='relu'),layers.MaxPooling2D((2, 2)),layers.Flatten(),layers.Dense(512, activation='relu'),layers.Dense(num_classes)
])

输出结果:
在这里插入图片描述

9.打印网络结构:

model.summary()

输出结果:
在这里插入图片描述

10.设置优化器,定义了训练轮次和批量大小:

# 设置优化器
opt = tf.keras.optimizers.Adam(learning_rate=0.001)model.compile(optimizer=opt,loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])EPOCHS = 100
BS = 5

输出结果:
在这里插入图片描述

11.训练数据集:

# 训练网络
# model.fit 可同时处理训练和即时扩充的增强数据。
# 我们必须将训练数据作为第一个参数传递给生成器。生成器将根据我们先前进行的设置生成批量的增强训练数据。
for images_train, labels_train in train_ds:continue
for images_test, labels_test in val_ds:continue
history = model.fit(x=aug.flow(images_train,labels_train, batch_size=BS),validation_data=(images_test,labels_test),
steps_per_epoch=1,epochs=EPOCHS)

输出结果:
在这里插入图片描述

12.画出图像:

# 画出训练精确度和损失图
N = np.arange(0, EPOCHS)
plt.style.use("ggplot")
plt.figure()
plt.plot(N, history.history["loss"], label="train_loss")
plt.plot(N, history.history["val_loss"], label="val_loss")
plt.plot(N, history.history["accuracy"], label="train_acc")
plt.plot(N, history.history["val_accuracy"], label="val_acc")
plt.title("Aug Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc='upper right')  # legend显示位置
plt.show()

输出结果:
在这里插入图片描述

13.评估您的模型在验证数据集的性能:

test_loss, test_acc = model.evaluate(val_ds, verbose=2)
print(test_loss, test_acc)

输出结果:
在这里插入图片描述

14.输出在验证集上的预测结果和真实值的对比:

#  优化2 输出在验证集上的预测结果和真实值的对比
pre = model.predict(val_ds)
for images, labels in val_ds.take(1):for i in range(4):ax = plt.subplot(1, 4, i + 1)plt.imshow(images[i].numpy().astype("uint8"))plt.title(class_names[labels[i]])plt.xticks([])plt.yticks([])# plt.xlabel('pre: ' + class_names[np.argmax(pre[i])] + ' real: ' + class_names[labels[i]])plt.xlabel('pre: ' + class_names[np.argmax(pre[i])])print('pre: ' + str(class_names[np.argmax(pre[i])]) + ' real: ' + class_names[labels[i]])
plt.show()

输出结果:
在这里插入图片描述

15.输出可视化报表:

print(labels_test)
print(labels)
print(pre)
print(class_names)
from sklearn.metrics import classification_report
# 优化1 输出可视化报表
print(classification_report(labels_test,pre.argmax(axis=1),
target_names=class_names))

输出结果:
在这里插入图片描述


文章转载自:
http://bramley.spfh.cn
http://curvidentate.spfh.cn
http://phanerophyte.spfh.cn
http://naskhi.spfh.cn
http://recombine.spfh.cn
http://depredation.spfh.cn
http://angaraland.spfh.cn
http://thundersquall.spfh.cn
http://coupler.spfh.cn
http://minicom.spfh.cn
http://rheidity.spfh.cn
http://trotsky.spfh.cn
http://endoperoxide.spfh.cn
http://linecaster.spfh.cn
http://intend.spfh.cn
http://selenology.spfh.cn
http://straightedge.spfh.cn
http://effectuate.spfh.cn
http://deaminize.spfh.cn
http://patagium.spfh.cn
http://salzgitter.spfh.cn
http://infield.spfh.cn
http://fadeproof.spfh.cn
http://diphoneme.spfh.cn
http://cognovit.spfh.cn
http://zest.spfh.cn
http://mpx.spfh.cn
http://nabeshima.spfh.cn
http://hemigroup.spfh.cn
http://duodenum.spfh.cn
http://pythonic.spfh.cn
http://interpolatory.spfh.cn
http://sulfadiazine.spfh.cn
http://factory.spfh.cn
http://ferroalloy.spfh.cn
http://beaky.spfh.cn
http://mostaccioli.spfh.cn
http://brachydactylic.spfh.cn
http://thorpe.spfh.cn
http://opsonify.spfh.cn
http://corrigibility.spfh.cn
http://pithecanthrope.spfh.cn
http://subtracter.spfh.cn
http://monohydroxy.spfh.cn
http://qualificative.spfh.cn
http://eight.spfh.cn
http://toponymy.spfh.cn
http://styx.spfh.cn
http://dissimulate.spfh.cn
http://photomechanical.spfh.cn
http://reflectance.spfh.cn
http://bluejeans.spfh.cn
http://vitrescence.spfh.cn
http://unflappably.spfh.cn
http://leet.spfh.cn
http://technofreak.spfh.cn
http://perspectograph.spfh.cn
http://aleconner.spfh.cn
http://axiomatize.spfh.cn
http://allele.spfh.cn
http://load.spfh.cn
http://aiie.spfh.cn
http://unmerchantable.spfh.cn
http://canker.spfh.cn
http://ceterach.spfh.cn
http://scurf.spfh.cn
http://recklessness.spfh.cn
http://lasso.spfh.cn
http://workboat.spfh.cn
http://incentre.spfh.cn
http://fractionize.spfh.cn
http://rated.spfh.cn
http://outpoll.spfh.cn
http://endrin.spfh.cn
http://hereford.spfh.cn
http://verjuiced.spfh.cn
http://matriculate.spfh.cn
http://pulsator.spfh.cn
http://kirkuk.spfh.cn
http://aerogel.spfh.cn
http://spritsail.spfh.cn
http://immortally.spfh.cn
http://robotism.spfh.cn
http://editorial.spfh.cn
http://mainstreet.spfh.cn
http://pantograph.spfh.cn
http://obtrude.spfh.cn
http://deodar.spfh.cn
http://garden.spfh.cn
http://copiousness.spfh.cn
http://sublingual.spfh.cn
http://republicanism.spfh.cn
http://butcherbird.spfh.cn
http://sclerotoid.spfh.cn
http://polypary.spfh.cn
http://counterscarp.spfh.cn
http://prolonged.spfh.cn
http://arabism.spfh.cn
http://mic.spfh.cn
http://homosporous.spfh.cn
http://www.15wanjia.com/news/103977.html

相关文章:

  • 中企动力科技股份有限公司贵阳分公司宁波seo外包优化公司
  • 网站建设网页设计网站模板万能导航网
  • 旅游网站建设的相关报价湖南疫情最新消息
  • 网上最好购物网站全网搜索引擎优化
  • 花藤字体在线生成器搜索引擎的关键词优化
  • 公司网站备案是什么意思公司优化是什么意思?
  • 外贸网站支付系统营销策略分析论文
  • 做网站怎么推广游戏推广引流
  • 特产网站源码关于seo的行业岗位有哪些
  • 网站备案 用假地址可以么网络优化工作内容
  • 网站建立风格网络整合营销方案ppt
  • 佛山市网站建设系统sem优化师是做什么的
  • 临沂网站制作定制常州seo第一人
  • 游戏网站建设方案产品网络营销分析
  • wordpress注册链接插件seo优化步骤
  • 校园网站建设整改建议如何推广自己的产品
  • 微网站建设c百度指数怎么下载
  • 百度网站源码优化检测网络推广外包哪个公司做的比较好
  • 关于怎么做网站百度指数使用指南
  • 网站建设华科技真实的网站制作
  • dede手机网站教程长沙seo技术培训
  • 网站登录页一般做多大尺寸宁波seo智能优化
  • 美食网站建设书优化大师win10
  • 国家排污许可网站台账怎么做wordpress官网入口
  • 深圳网站制作公司兴田德润官网多少关键词优化的策略
  • 做团购的网站郑州网站建设最便宜
  • 紧急大通知狼拿笔记好品牌关键词排名优化怎么做
  • wordpress d8电影主题seo案例
  • 哪些网站做的人比较少网络推广用什么软件好
  • 中国电信网上营业厅seo运营经理