基于tensorflow的最大均值差异 水果图像分类 完整代码 自己搭建模型 且绘制损失准确率曲线 并实现数据散点图可视化以及混淆矩阵
时间: 2023-06-13 21:09:23 浏览: 262
基于Tensorflow的水果识别平台
5星 · 资源好评率100%
很高兴为您提供帮助。以下是基于TensorFlow的最大均值差异水果图像分类的完整代码,包括模型搭建、训练和可视化。
首先,我们需要下载水果图像数据集,这里我选择了Fruits 360数据集(https://www.kaggle.com/moltean/fruits)。
接下来,我们需要导入所需的库:
``` python
import os
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
```
然后,我们定义一些超参数:
``` python
batch_size = 32
img_height = 100
img_width = 100
epochs = 20
```
接着,我们使用tf.keras.preprocessing.image.ImageDataGenerator来加载数据并进行数据增强:
``` python
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1./255, # 归一化
rotation_range=30, # 随机旋转
width_shift_range=0.1, # 随机水平平移
height_shift_range=0.1, # 随机竖直平移
zoom_range=0.1, # 随机缩放
horizontal_flip=True, # 随机水平翻转
validation_split=0.2 # 验证集比例
)
train_generator = train_datagen.flow_from_directory(
directory='fruits-360/Training',
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical',
subset='training'
)
validation_generator = train_datagen.flow_from_directory(
directory='fruits-360/Training',
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical',
subset='validation'
)
```
接下来,我们使用Keras构建模型:
``` python
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(img_height, img_width, 3)),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(131, activation='softmax')
])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
```
然后,我们开始训练模型:
``` python
history = model.fit(
train_generator,
epochs=epochs,
validation_data=validation_generator
)
```
接下来,我们绘制损失准确率曲线:
``` python
# 绘制训练和验证准确率
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epochs)
plt.figure(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
# 绘制训练和验证损失
plt.subplot(2, 1, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
```
最后,我们绘制数据散点图可视化以及混淆矩阵:
``` python
# 数据散点图可视化
x_test = []
y_test = []
for i in os.listdir('fruits-360/Test'):
for j in os.listdir('fruits-360/Test/' + i):
img = tf.keras.preprocessing.image.load_img(
'fruits-360/Test/' + i + '/' + j, target_size=(img_height, img_width)
)
img = tf.keras.preprocessing.image.img_to_array(img)
img = img / 255.
x_test.append(img)
y_test.append(i)
x_test = np.array(x_test)
y_test = np.array(y_test)
y_pred = model.predict(x_test)
y_pred = np.argmax(y_pred, axis=1)
plt.figure(figsize=(10, 10))
plt.scatter(np.arange(len(y_test)), y_test, c=y_pred)
plt.colorbar()
plt.show()
# 混淆矩阵
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(10, 10))
plt.imshow(cm, cmap='binary')
plt.show()
```
至此,我们完成了基于TensorFlow的最大均值差异水果图像分类的完整代码,包括模型搭建、训练和可视化。
阅读全文