作业介绍
- 作业主页:Assignment #1
- 作业目的:
- 在之前的作业中,我们已经能够编写简单的分类器,接收原始RGB像素输入来进行分类,并且获得了不错的性能,本次作业中,我们将尝试使用从像素值中提取的稍微高级一点的特征(例如颜色直方图、HOG特征,SIFT特特征)来提升我们分类器的性能。
- 官方给的示例代码:assigment #1 code
1. 加载数据
from cs231n.features import color_histogram_hsv, hog_feature
def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=1000):
cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'
X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)
mask = list(range(num_training, num_training + num_validation))
X_val = X_train[mask]
y_val = y_train[mask]
mask = list(range(num_training))
X_train = X_train[mask]
y_train = y_train[mask]
mask = list(range(num_test))
X_test = X_test[mask]
y_test = y_test[mask]
return X_train, y_train, X_val, y_val, X_test, y_test
try:
del X_train, y_train
del X_test, y_test
print('Clear previously loaded data.')
except:
pass
X_train, y_train, X_val, y_val, X_test, y_test = get_CIFAR10_data()
2. 提取特征
- 我们将提取HOG(方向梯度特征),其更注重图像的纹理特征,而忽略图像的颜色特征
- 而且,我们也会提取HSV 色彩空间的 H 颜色通道的颜色直方图(color histogram)特征,其更加注重颜色特征
- 最后,我们将两个特征联合起来(concatenation)。
fearture.py
中hog_feature
提取HOG特征,color_histogram_hsv
提取颜色直方图,它们都作用于一张图像,并返回一个维特征向量
extract_features(imgs, feature_fns, verbose=False)
输入多个图像imgs
和多个特征提取函数feature_fns
,然后返回多个图像的特征,每个图像占一行,然后每行的特征就是多个特征提取函数的特征联合起来
from cs231n.features import *
num_color_bins = 10
feature_fns = [hog_feature, lambda img: color_histogram_hsv(img, nbin=num_color_bins)]
X_train_feats = extract_features(X_train, feature_fns, verbose=True)
X_val_feats = extract_features(X_val, feature_fns)
X_test_feats = extract_features(X_test, feature_fns)
mean_feat = np.mean(X_train_feats, axis=0, keepdims=True)
X_train_feats -= mean_feat
X_val_feats -= mean_feat
X_test_feats -= mean_feat
std_feat = np.std(X_train_feats, axis=0, keepdims=True)
X_train_feats /= std_feat
X_val_feats /= std_feat
X_test_feats /= std_feat
X_train_feats = np.hstack([X_train_feats, np.ones