惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
Lohrmann on Cybersecurity
Martin Fowler
Martin Fowler
Engineering at Meta
Engineering at Meta
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
TaoSecurity Blog
TaoSecurity Blog
博客园_首页
Vercel News
Vercel News
Hugging Face - Blog
Hugging Face - Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Last Week in AI
Last Week in AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
The Exploit Database - CXSecurity.com
量子位
Project Zero
Project Zero
A
Arctic Wolf
小众软件
小众软件
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
宝玉的分享
宝玉的分享
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Troy Hunt's Blog
P
Privacy & Cybersecurity Law Blog
Security Latest
Security Latest
B
Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
K
Kaspersky official blog
C
Check Point Blog
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Webroot Blog
Webroot Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
人人都是产品经理
人人都是产品经理
AI
AI
Cisco Talos Blog
Cisco Talos Blog
MyScale Blog
MyScale Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
S
Schneier on Security
P
Palo Alto Networks Blog

kekxv 技术日志

基于 kekxv/gitea-pages 与 Gitea Actions 构建静态站点托管服务 Json简单工具 在Windows上运行Code Server:结合WSL打造你的云端VS Code开发环境 安卓sdkmanager工具换源 boost bazel starter bazel 供应商模式 PVE引导丢失修复 NSFW图像检测 警惕c++内置变量指针 关于内网springboot启动慢记录 网页转换为chrome插件 nginx代理的一种使用方式 YOLOv8 训练自己的数据 luckfox-交叉编译之bazel gitea actions CICD 自动化 Linux限制进程使用率 影音中心Jellyfin快速部署 OCR & 人脸算法 -- opencv dnn tensorflow gpu 安装(ubuntu22.04)
深度学习记录-简单
kekxv · 2022-07-08 · via kekxv 技术日志

目前 tensorflow 发展到了 2.9.+
的版本,大部分的功能都完善,且使用简单,同时教程文档也非常完善:https://www.tensorflow.org/tutorials?hl=zh-cn

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115





"""
训练文件
"""

import glob
import os

import tensorflow as tf
from keras import layers
from tensorflow import keras

import numpy as np

print(tf.__version__)
gpus = tf.config.list_physical_devices(device_type='GPU')
if len(gpus) > 0:
tf.config.experimental.set_memory_growth(gpus[0], True)


Image_dir = 'image'
img_height = 180
img_width = 180
batch_size = 32
epochs = 15
model_path = "model.h5"

if __name__ == '__main__':
train_ds = tf.keras.utils.image_dataset_from_directory(
Image_dir + "/train",
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.utils.image_dataset_from_directory(
Image_dir + "/test",
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
class_names = train_ds.class_names
print(class_names)
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)

normalization_layer = layers.Rescaling(1. / 255)

normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
image_batch, labels_batch = next(iter(normalized_ds))
first_image = image_batch[0]

print(np.min(first_image), np.max(first_image))
num_classes = len(class_names)
data_augmentation = keras.Sequential(
[
keras.layers.RandomFlip("horizontal", input_shape=(img_height, img_width, 3)),
keras.layers.RandomRotation(0.1),
keras.layers.RandomZoom(0.1),
]
)
model = None
if os.path.exists(model_path):

model = tf.keras.models.load_model(model_path)
model.summary()
else:
model = keras.Sequential([
data_augmentation,
keras.layers.Rescaling(1. / 255),
keras.layers.Conv2D(16, 3, padding='same', activation='relu'),
keras.layers.MaxPooling2D(),
keras.layers.Conv2D(32, 3, padding='same', activation='relu'),
keras.layers.MaxPooling2D(),
keras.layers.Conv2D(64, 3, padding='same', activation='relu'),
keras.layers.MaxPooling2D(),
keras.layers.Conv2D(128, 3, padding='same', activation='relu'),
keras.layers.MaxPooling2D(),
keras.layers.Dropout(0.2),
keras.layers.Flatten(),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(num_classes)
])
try:
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])

history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
except KeyboardInterrupt:
pass

print('\n\n\n')

test_loss, test_acc = model.evaluate(val_ds, verbose=2)
print('\nTest accuracy:', test_acc)

test_loss, test_acc = model.evaluate(train_ds, verbose=2)

print('\nTest accuracy:', test_acc)


model.save(model_path)

exit()

该代码会自动找到 image/train 里面的文件夹,并将文件夹作为类别,加载各自类别(文件夹)里面的图像;然后对其进行训练。可以快速得到一个分类模型。
完整教程 : https://www.tensorflow.org/tutorials/images/classification

前面的分类训练保存的模型为 keras/hdf5 模型,好处是可以单文件存储,但是使用的时候需要依赖keras/tensorflow,同时目前 opencv dnn 模块还不支持加载keras/hdf5
模型,所以我们可以选择将其转换为其他格式的模型:

例如 tensorflowpb模型:
https://stackoverflow.com/questions/69633595/load-onnx-model-in-opencv-dnn