Stanford UFLDL教程 Exercise:Convolution and Pooling

通过加载从STL-10数据集中学习到的特征,实现并测试卷积与池化操作,用于对从减少的STL-10数据集中的64x64图像进行分类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Exercise:Convolution and Pooling

Contents

[hide]

Convolution and Pooling

In this exercise you will use the features you learned on 8x8 patches sampled from images from the STL-10 dataset inthe earlier exercise on linear decoders for classifying images from a reduced STL-10 dataset applyingconvolution and pooling. The reduced STL-10 dataset comprises 64x64 images from 4 classes (airplane, car, cat, dog).

In the file cnn_exercise.zip we have provided some starter code. You should write your code at the places indicated "YOUR CODE HERE" in the files.

For this exercise, you will need to modify cnnConvolve.m andcnnPool.m.

Dependencies

The following additional files are required for this exercise:

You will also need:

If you have not completed the exercises listed above, we strongly suggest you complete them first.

Step 1: Load learned features

In this step, you will use the features from Exercise:Learning color features with Sparse Autoencoders. If you have completed that exercise, you can load the color features that were previously saved. To verify that the features are good, the visualized features should look like the following:

CNN Features Good.png

Step 2: Implement and test convolution and pooling

In this step, you will implement convolution and pooling, and test them on a small part of the data set to ensure that you have implemented these two functions correctly. In the next step, you will actually convolve and pool the features with the STL-10 images.

Step 2a: Implement convolution

Implement convolution, as described in feature extraction using convolution, in the function cnnConvolve incnnConvolve.m. Implementing convolution is somewhat involved, so we will guide you through the process below.

First, we want to compute σ(Wx(r,c) +b) for all valid (r,c) (valid meaning that the entire 8x8 patch is contained within the image; this is as opposed to afull convolution, which allows the patch to extend outside the image, with the area outside the image assumed to be 0), whereW and b are the learned weights and biases from the input layer to the hidden layer, andx(r,c) is the 8x8 patch with the upper left corner at(r,c). To accomplish this, one naive method is to loop over all such patches and computeσ(Wx(r,c) + b) for each of them; while this is fine in theory, it can very slow. Hence, we usually use Matlab's built in convolution functions, which are well optimized.

Observe that the convolution above can be broken down into the following three small steps. First, computeWx(r,c) for all(r,c). Next, add b to all the computed values. Finally, apply the sigmoid function to the resulting values. This doesn't seem to buy you anything, since the first step still requires a loop. However, you can replace the loop in the first step with one of MATLAB's optimized convolution functions,conv2, speeding up the process significantly.

However, there are two important points to note in using conv2.

First, conv2 performs a 2-D convolution, but you have 5 "dimensions" - image number, feature number, row of image, column of image, and (color) channel of image - that you want to convolve over. Because of this, you will have to convolve each feature and image channel separately for each image, using the row and column of the image as the 2 dimensions you convolve over. This means that you will need three outer loops over the image numberimageNum, feature number featureNum, and the channel number of the imagechannel. Inside the three nested for-loops, you will perform a conv2 2-D convolution, using the weight matrix for thefeatureNum-th feature and channel-th channel, and the image matrix for theimageNum-th image.

Second, because of the mathematical definition of convolution, the feature matrix must be "flipped" before passing it toconv2. The following implementation tip explains the "flipping" of feature matrices when using MATLAB's convolution functions:

Implementation tip: Using conv2 and convn

Because the mathematical definition of convolution involves "flipping" the matrix to convolve with (reversing its rows and its columns), to use MATLAB's convolution functions, you must first "flip" the weight matrix so that when MATLAB "flips" it according to the mathematical definition the entries will be at the correct place. For example, suppose you wanted to convolve two matricesimage (a large image) and W (the feature) using conv2(image, W), and W is a 3x3 matrix as below:

 W =  \begin{pmatrix}  1 & 2 & 3 \\  4 & 5 & 6 \\  7 & 8 & 9  \\ \end{pmatrix}

If you use conv2(image, W), MATLAB will first "flip" W, reversing its rows and columns, before convolvingW with image, as below:

 \begin{pmatrix}  1 & 2 & 3 \\  4 & 5 & 6 \\  7 & 8 & 9  \\ \end{pmatrix} \xrightarrow{flip} \begin{pmatrix}  9 & 8 & 7 \\  6 & 5 & 4 \\  3 & 2 & 1  \\ \end{pmatrix}

If the original layout of W was correct, after flipping, it would be incorrect. For the layout to be correct after flipping, you will have to flipW before passing it into conv2, so that after MATLAB flips W in conv2, the layout will be correct. For conv2, this means reversing the rows and columns, which can be done withflipud and fliplr, as shown below:

% Flip W for use in conv2
W = flipud(fliplr(W));

Next, to each of the convolvedFeatures, you should then add b, the corresponding bias for thefeatureNum-th feature.

However, there is one additional complication. If we had not done any preprocessing of the input patches, you could just follow the procedure as described above, and apply the sigmoid function to obtain the convolved features, and we'd be done. However, because you preprocessed the patches before learning features on them, you must also apply the same preprocessing steps to the convolved patches to get the correct feature activations.

In particular, you did the following to the patches:

  1. subtract the mean patch, meanPatch to zero the mean of the patches
  2. ZCA whiten using the whitening matrix ZCAWhite.

These same three steps must also be applied to the input image patches.

Taking the preprocessing steps into account, the feature activations that you should compute is\sigma(W(T(x-\bar{x})) + b), whereT is the whitening matrix and \bar{x} is the mean patch. Expanding this, you obtain\sigma(WTx - WT\bar{x} + b), which suggests that you should convolve the images withWT rather than W as earlier, and you should add(b - WT\bar{x}), rather than justb to convolvedFeatures, before finally applying the sigmoid function.

Step 2b: Check your convolution

We have provided some code for you to check that you have done the convolution correctly. The code randomly checks the convolved values for a number of (feature, row, column) tuples by computing the feature activations usingfeedForwardAutoencoder for the selected features and patches directly using the sparse autoencoder.

Step 2c: Pooling

Implement pooling in the function cnnPool in cnnPool.m. You should implementmean pooling (i.e., averaging over feature responses) for this part.

Step 2d: Check your pooling

We have provided some code for you to check that you have done the pooling correctly. The code runscnnPool against a test matrix to see if it produces the expected result.

Step 3: Convolve and pool with the dataset

In this step, you will convolve each of the features you learned with the full 64x64 images from the STL-10 dataset to obtain the convolved features for both the training and test sets. You will then pool the convolved features to obtain the pooled features for both training and test sets. The pooled features for the training set will be used to train your classifier, which you can then test on the test set.

Because the convolved features matrix is very large, the code provided does the convolution and pooling 50 features at a time to avoid running out of memory.

Step 4: Use pooled features for classification

In this step, you will use the pooled features to train a softmax classifier to map the pooled features to the class labels. The code in this section usessoftmaxTrain from the softmax exercise to train a softmax classifier on the pooled features for 500 iterations, which should take around a few minutes.

Step 5: Test classifier

Now that you have a trained softmax classifier, you can see how well it performs on the test set. These pooled features for the test set will be run through the softmax classifier, and the accuracy of the predictions will be computed. You should expect to get an accuracy of around 80%.

from: http://ufldl.stanford.edu/wiki/index.php/Exercise:Convolution_and_Pooling

内容概要:本文详细探讨了基于阻尼连续可调减振器(CDC)的半主动悬架系统的控制策略。首先建立了CDC减振器的动力学模型,验证了其阻尼特性,并通过实验确认了模型的准确性。接着,搭建了1/4车辆悬架模型,分析了不同阻尼系数对悬架性能的影响。随后,引入了PID、自适应模糊PID和模糊-PID并联三种控制策略,通过仿真比较它们的性能提升效果。研究表明,模糊-PID并联控制能最优地提升悬架综合性能,在平顺性和稳定性间取得最佳平衡。此外,还深入分析了CDC减振器的特性,优化了控制策略,并进行了系统级验证。 适用人群:从事汽车工程、机械工程及相关领域的研究人员和技术人员,尤其是对车辆悬架系统和控制策略感兴趣的读者。 使用场景及目标:①适用于研究和开发基于CDC减振器的半主动悬架系统的工程师;②帮助理解不同控制策略(如PID、模糊PID、模糊-PID并联)在悬架系统中的应用及其性能差异;③为优化车辆行驶舒适性和稳定性提供理论依据和技术支持。 其他说明:本文不仅提供了详细的数学模型和仿真代码,还通过实验数据验证了模型的准确性。对于希望深入了解CDC减振器工作原理及其控制策略的读者来说,本文是一份极具价值的参考资料。同时,文中还介绍了多种控制策略的具体实现方法及其优缺点,为后续的研究和实际应用提供了有益的借鉴。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值