yolov5详解

包括模型训练、模型推理、等内容. 主要目的:

  1. 了解基于xxx 进行深度学习模型训练的系统支持,包括梯度下降xxx等等概念。
  2. 了解模型训练的流程,以及怎么训练出一个好模型。

模型训练

数据收集和准备

选择数据集:freiburg-groceries(计算机视觉领域的数据集,主要用于目标检测和图像分类任务)。

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
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer
import os
import numpy as np
from PIL import Image

# Define the path to your dataset
dataset_path = '/kaggle/input/freiburg-groceries/images/OIL/'

# Load and preprocess your dataset
image_paths = [os.path.join(dataset_path, filename) for filename in os.listdir(dataset_path)]
images = []
labels = []

for image_path in image_paths:
# Load and resize the image to a common size (e.g., 224x224)
image = Image.open(image_path)
image = image.resize((224, 224))
image = np.array(image) / 255.0 # Normalize pixel values to [0, 1]
images.append(image)

# Extract the class label from the image path (assuming the path structure)
label = image_path.split(os.path.sep)[-2] # Assuming the label is the second-to-last directory
labels.append(label)

# Convert labels to one-hot encoded vectors
label_binarizer = LabelBinarizer()
labels = label_binarizer.fit_transform(labels)

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(images, labels, test_size=0.2, random_state=42)

# Create a simple CNN model
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(len(label_binarizer.classes_), activation='softmax') # Output layer with the number of classes
])

# Compile the model
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])

# Train the model
model.fit(np.array(X_train), np.array(y_train), epochs=10, batch_size=32, validation_split=0.2)

# Evaluate the model on the test data
test_loss, test_acc = model.evaluate(np.array(X_test), np.array(y_test))
print(f'Test accuracy: {test_acc}')