【深度学习 图像分类】图像分类任务细节

无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。人工智能教程

实现一个完整的图像分类任务,大致需要分为五个步骤:

1、选择开源框架
目前常用的深度学习框架主要包括tensorflow、keras、pytorch、mxnet,caffe等;
2、构建并读取数据集
根据任务需求搜集相关图像搭建相应的数据集,常见的方式包括:网络爬虫、实地拍摄、公共数据使用等。随后根据所选开源框架读取数据集。
3、框架搭建
选择合适的网络模型、损失函数以及优化方式,以完成整体框架的搭建
4、训练并调试参数
通过训练选定合适超参数
5、测试准确率
在测试集上验证模型的最终性能

在这里插入图片描述

Pytorch中封装了相应的数据读取的类函数,通过调用torch.utils.data.Datasets函数,则可以实现读取功能。

需要特别强调的是对图像进行去均值处理,很多同学不明白为何要减去均值,其主要的原因是图像作为一种平稳的数据分布,通过减去数据对应维度的统计平均值,可以消除公共部分,以凸显个体之间的特征和差异。

损失函数则选择交叉熵损失函数

优化方式选择SGD、Adam优化两种

以下是使用 TensorFlow 完整最大均值差异(Max-Mean-Diff,MMD)图像分类的代码示例: 首先,导入必要的库: ``` python import tensorflow as tf import numpy as np import matplotlib.pyplot as plt from tensorflow.keras.datasets import cifar10 ``` 然后,加载 CIFAR-10 数据集: ``` python (x_train, y_train), (x_test, y_test) = cifar10.load_data() ``` 接着,对数据进行预处理: ``` python x_train = x_train / 255.0 x_test = x_test / 255.0 y_train = tf.keras.utils.to_categorical(y_train, num_classes=10) y_test = tf.keras.utils.to_categorical(y_test, num_classes=10) ``` 定义 MMD 损失函数: ``` python def maximum_mean_discrepancy(x, y, kernel=rbf_kernel): cost = tf.reduce_mean(kernel(x, x)) cost += tf.reduce_mean(kernel(y, y)) cost -= 2 * tf.reduce_mean(kernel(x, y)) cost = tf.where(cost > 0, cost, 0, name='mmd_loss') return cost def rbf_kernel(x, y, gamma=0.05): K_xx = tf.exp(-gamma * tf.square(tf.norm(x - x, ord='euclidean', axis=-1))) K_yy = tf.exp(-gamma * tf.square(tf.norm(y - y, ord='euclidean', axis=-1))) K_xy = tf.exp(-gamma * tf.square(tf.norm(x - y, ord='euclidean', axis=-1))) return K_xx + K_yy - 2 * K_xy ``` 定义模型: ``` python def create_model(): model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(32, (3,3), activation='relu', padding='same', input_shape=(32,32,3)), tf.keras.layers.BatchNormalization(), tf.keras.layers.Conv2D(32, (3,3), activation='relu', padding='same'), tf.keras.layers.BatchNormalization(), tf.keras.layers.MaxPooling2D((2,2)), tf.keras.layers.Dropout(0.2), tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding='same'), tf.keras.layers.BatchNormalization(), tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding='same'), tf.keras.layers.BatchNormalization(), tf.keras.layers.MaxPooling2D((2,2)), tf.keras.layers.Dropout(0.3), tf.keras.layers.Conv2D(128, (3,3), activation='relu', padding='same'), tf.keras.layers.BatchNormalization(), tf.keras.layers.Conv2D(128, (3,3), activation='relu', padding='same'), tf.keras.layers.BatchNormalization(), tf.keras.layers.MaxPooling2D((2,2)), tf.keras.layers.Dropout(0.4), tf.keras.layers.Flatten(), tf.keras.layers.Dense(10, activation='softmax') ]) return model ``` 编译模型训练: ``` python model = create_model() optimizer = tf.keras.optimizers.Adam(learning_rate=0.001) model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) history = model.fit(x_train, y_train, epochs=50, validation_data=(x_test, y_test)) ``` 评估模型: ``` python test_loss, test_acc = model.evaluate(x_test, y_test) print('Test accuracy:', test_acc) ``` 最后,绘制训练和验证准确率和损失函数的变化图: ``` python plt.plot(history.history['accuracy'], label='accuracy') plt.plot(history.history['val_accuracy'], label = 'val_accuracy') plt.plot(history.history['loss'], label = 'loss') plt.plot(history.history['val_loss'], label = 'val_loss') plt.xlabel('Epoch') plt.ylabel('Accuracy/Loss') plt.ylim([0, 1]) plt.legend(loc='lower right') plt.show() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东华果汁哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值