机器学习中的卷积(Convolution)是一种特殊的数学运算,主要应用于信号处理和图像处理领域。在卷积神经网络(Convolutional Neural Network,简称 CNN)中,卷积操作用于提取图像或其他数据的特征,从而实现分类、回归等任务。卷积的基本思想是将一个函数(信号或图像)与一个卷积核(一个小的函数)进行组合,以产生一个新的函数(输出信号或图像)。卷积操作具有以下几个特点:1. 局部性:卷积操作主要关注输入数据中的一个小区域,可以减少计算量和参数数量。2. 权值共享:卷积核中的权值在整个卷积过程中保持不变,减少了训练过程中的参数数量。3. 平移不变性:卷积操作具有平移不变性,即对输入数据进行平移后,输出结果不会发生变化。在机器学习中,卷积操作通常用于以下场景:1. 图像处理:卷积神经网络(CNN)中的卷积操作可以用于提取图像特征,例如边缘、纹理等,从而实现图像分类、目标检测等任务。2. 信号处理:卷积可以用于信号处理,例如滤波、特征提取等。3. 自然语言处理:卷积也可以用于自然语言处理领域,例如文本分类、情感分析等。要使用卷积操作,需要先确定卷积核的大小和数量,然后对输入数据进行划分,将卷积核应用于每个小区域,并计算输出结果。在深度学习框架(如 TensorFlow、PyTorch 等)中,提供了丰富的卷积操作和相关的函数,方便开发者进行实现。总之,卷积操作是机器学习中的一种重要技术,可以用于处理图像、信号等数据,并具有局部性、权值共享和平移不变性等特点。通过使用卷积操作,可以有效地提取数据特征,从而实现各种机器学习任务。
Ch 09: Concept 01
Using the CIFAR-10 dataset
Download the CIFAR-10 dataset for Python here https://www.cs.toronto.edu/~kriz/cifar.html.
The documentation comes with the following helper function:
import pickle
def unpickle(file):
fo = open(file, 'rb')
dict = pickle.load(fo, encoding='latin1')
fo.close()
return dict
Each image is a flat vector of colored pixel values. We'll convert it to a matrix of pixels instead to make it easer to reason about the image.
First, let's grayscale the image so that we can deal with a more natural representation.
Then, we'll crop the edges of the image away to further similify the number of dimensions in the input data.
Finally, we'll normalize the input by subtracting the mean pixel intensity and dividing by the standard deviation.
import numpy as np
def clean(data):
imgs = data.reshape(data.shape[0], 3, 32, 32)
grayscale_imgs = imgs.mean(1)
cropped_imgs = grayscale_imgs[:, 4:28, 4:28]
img_data = cropped_imgs.reshape(data.shape[0], -1)
img_size = np.shape(img_data)[1]
means = np.mean(img_data, axis=1)
meansT = means.reshape(len(means), 1)
stds = np.std(img_data, axis=1)
stdsT = stds.reshape(len(stds), 1)
adj_stds = np.maximum(stdsT, 1.0 / np.sqrt(img_size))
normalized = (img_data - meansT) / adj_stds
return normalized
Here's a helper function to load and clean all the images:
def read_data(directory):
names = unpickle('{}/batches.meta'.format(directory))['label_names']
print('names', names)
data, labels = [], []
for i in range(1, 6):
filename = '{}/data_batch_{}'.format(directory, i)
batch_data = unpickle(filename)
if len(data) > 0:
data = np.vstack((data, batch_data['data']))
labels = np.hstack((labels, batch_data['labels']))
else:
data = batch_data['data']
labels = batch_data['labels']
print(np.shape(data), np.shape(labels))
data = clean(data)
data = data.astype(np.float32)
return names, data, labels
Let's display some images from the dataset:
%matplotlib inline
import matplotlib.pyplot as plt
import random
random.seed(1)
names, data, labels = read_data('./cifar-10-batches-py')
def show_some_examples(names, data, labels):
plt.figure()
rows, cols = 4, 4
random_idxs = random.sample(range(len(data)), rows * cols)
for i in range(rows * cols):
plt.subplot(rows, cols, i + 1)
j = random_idxs[i]
plt.title(names[labels[j]])
img = np.reshape(data[j, :], (24, 24))
plt.imshow(img, cmap='Greys_r')
plt.axis('off')
plt.tight_layout()
plt.savefig('cifar_examples.png')
show_some_examples(names, data, labels)
names ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
(50000, 3072) (50000,)