Filters in Tensorflow

Hello guys. I’ve completed the assignment in Course 4 Week 1 and i was reviewing it to make sure i understand. While i review it, i notice something that makes me confused. So we implement Z = tf.keras.layers.Conv2D(filters=…, kernel_size=…, …). I was confused about how these filters were set up. I’ve learned about the vertical, horizontal edge detection filter. And so does TensorFlow really set these filters up? If i had say filters=6, does that means that TensorFlow has set 6 filters up? And how do you know what kind of the filter were? Thanks ahead.

When the filter values are learned, they don’t necessarily always end up detecting edges, etc.

If that’s an important characteristics of classifying the images, then it will usually turn out that way.

But we don’t know in advance what sort of characteristics the layers will learn.

Oh yea, so basically the filter has parameters values that are learned right? But you guys didn’t know what the filters learn? Am i right?

I think you remember the assignment of Week1, Convolutional Neural Networks; Step by Step.

We get a slice of A_prev, W, and b, and multiply a slice and weights, and add bias. That is really what you are taking about. From a Keras Layer view points, a convolutional filter is just a weight and bias which are “learnable”, as Tom said.

But you guys didn’t know what the filters learn?

We know what it is.
Conv2D inherits “Keras.layer”. So, you can set initializer to initialize filter, and get weights to check how a network learns those. An example is,

Z1 = tf.keras.layers.Conv2D(filters=1, kernel_size=(4,4), kernel_initializer='zeros',strides=1, padding='same')(input_img)

I set the number of filter =1, and kernel_initializer='zero" to fill “zero” just a testing.
Once we build a model, we can access each layer with specifying its layer number, which can be easily known from model.summary(). Here is a sample code.

Like the above, you can pull weights from any layers at anytime. You can get weights during training, and see how “filters” change overtime.

In this sense, we know what types of filters are used, and how those change, but, we may not predict the final shape of filters (kernels).

Oh so the type of filters depends on the weights? Since the weights can change in order to get a perfect weight.

Oh so the type of filters depends on the weights?

If you still stick on legacy filter types which are used for a computer vision, then, the answer is Yes.

As a result of optimization, it may be similar to a Sobel filter, but, we do not know. Those legacy filters for a computer vision worked well, but we really do not know the coefficient of “-1” at (0,0) is the best or “-1.1” is the best.
If you really want to test with legacy filters, it may be possible by preparing your initializer to create, say, Sobel filter, and specifying those weights non-trainable. I haven’t tried those, and do not know whether it works or not, though…

Thanks for your help! Appreciate it!

Oh and 2 more thing. First, is there an assignment that’s made for testing? I meant that we apply the best weights for the test, where we can put our own image and test it? Second, do we just give the input shape of 64 x 64 x 3 in the following image? If yes, then do we give the original image to the function? If this doesn’t make sense please do inform me since English is my 2nd language.

I think reusing one of examples in the course is a good starting point. :slight_smile:

The input size for this network is, as you marked up, 64x64x3. If you look at another cell above, you see the shape of X_train is (1080, 64, 64, 3). This means, there are 1080 samples with (64,64,3) size.
Yes, this network is being optimized for that image size.

In this exercise, an assignment focused on training a model and showing learning history for train and test data.

I’m not sure this trained model has the best weights to get the best results, since a model is relatively simple, and epochs = 100. We may need some hyper-parameter tuning to get the best result. But, this may be a starting point.

Then, I guess what you want to do is to test with your own image. That’s “model.predict()” which is not included in this exercise. (Additionally, we may want to “evaluate” this model with another data…) I think you can find another example/exercise which includes some prediction phase.
Then, you can use your image (hand ?) to see how model works.
At that time, you need to transform your own image to (64,64,3). Tensorflow has some services, like tf.image.resize\_images which may help you to convert images.

I suppose I covered your two questions. Hope this helps.

1 Like

Oh yes this helps me. Thanks for helping!!