图像特征练习

期末结束后又回来啦~

对于如何分类一个图像,我们不仅可以直接在像素(raw pixels)上进行分类,我们还可以提取图上的一些特征。比如方向梯度直方图(HOG: histogram of oriented gradient)、颜色直方图(color histogram)。

Roughly speaking, HOG should capture the texture of the image while ignoring
color information, and the color histogram represents the color of the input
image while ignoring texture.

至于如何提取这些东西。。已经帮我们做好了。所以现在要做的是调参看看效果。。

SVM

for rate in learning_rates:
    for strength in regularization_strengths:
        svm = LinearSVM()
        svm.train(X_train_feats, y_train, learning_rate = rate, reg = strength)
        # 看训练集上效果怎么样
        yTrainpred = svm.predict(X_train_feats)
        trainAccuracy = np.mean(y_train == yTrainpred)
        # 看验证集上效果怎么样
        yValpred = svm.predict(X_val_feats)
        valAccuracy = np.mean(y_val == yValpred)
        results[(rate, strength)] = (trainAccuracy, valAccuracy)
        # 选取验证集上最好的那个
        if(best_val < valAccuracy):
            best_val = valAccuracy
            best_svm = svm

有41.9%的测试准确率,比之前高了5个点。

问题1

在问题之前,先展示了被错误分类的图片
misclassified

Describe the misclassification results that you see. Do they make sense?

还可以。。外型上差不多

训练一个双层特征神经网络

从SVM的结果可以看到,对于特征的效果要比没有特征的好。现在用之前的双层神经网络在特征上训练一下,看看效果怎么样。

learning_rates = [1e-2, 1e0]
regularization_strengths = [1e-3, 1e-2]
# layer_sizes = [40, 50, 60]
best_val = -1.0
part = 4 # 把区间平均n等分,得到n+1个点
print("Rate\t\tStrength\tHiddenSize\tAccuracy\tIsbetter")
for i in range(part + 1):
    for j in range(part + 1):
        rate = i * (learning_rates[1] - learning_rates[0]) / part + learning_rates[0] # 当前步长
        strength = j * (regularization_strengths[1] - regularization_strengths[0]) / part + regularization_strengths[0] # 当前惩罚系数
        net = TwoLayerNet(input_dim, hidden_dim, num_classes)
        net.train(X_train_feats, y_train, X_val_feats, y_val,
            num_iters=1000, batch_size=200,
            learning_rate=rate, learning_rate_decay=0.95,
            reg=strength, verbose=False)
        # 看训练集上效果怎么样
        yTrainpred = net.predict(X_train_feats)
        trainAccuracy = np.mean(y_train == yTrainpred)
        # 看验证集上效果怎么样
        yValpred = net.predict(X_val_feats)
        valAccuracy = np.mean(y_val == yValpred)
        # 选取验证集上最好的那个
        if(best_val < valAccuracy):
            best_val = valAccuracy
            best_net = net
            print("%f\t%.7f\t%d\t\t%f\t**" %(rate, strength, hidden_dim, valAccuracy))
        else:
            print("%f\t%.7f\t%d\t\t%f" %(rate, strength, hidden_dim, valAccuracy))
print("Done")

Rate:0.505000 Strength:0.0010000 Accuracy:0.569000
测试效果:
56.1% 确实不错哎。。