Uncover what Deep CNNs are doing with Python and Tensorflow
Key Takeaways
This video demonstrates how to visualize the outputs of intermediate CNN layers using Python and Tensorflow, with a focus on computer vision tasks such as image classification and processing. The video covers topics such as loading pre-trained models, specifying intermediate layers, and visualizing filter outputs using matplotlib subplots.
Full Transcript
i've been doing a ton of work with convolutional neural network layers as of late namely around building generators for gan architectures so i'm trying to build super resolution models and models that are able to remove smoke from different types of images one of the techniques that i found ridiculously useful when it comes to working with cnns is taking a look at the outputs from intermediate layers aka taking a look at what cnns see we're going to take a look at exactly how to do this in this tutorial let's do it what's happening guys my name is nicholas renaud and in this video we're going to be taking a look at what cnns see so rather than taking a look at a fully trained cnn architecture or deep learning architecture that uses convolutional neural networks i'm going to show you how to strip that back and take a look at the outputs from each set of different cnn layers as well as maybe max pooling layers this is ridiculously useful particularly if your deep neural network isn't quite outputting what you're expecting you can actually take it a little bit further back and see what is happening at each stage of the deep neural network these are the types of architectures that you tend to encounter when you're building really sophisticated models so if you're working with gans or you're working with object detection models this technique will probably help you ready to do it let's get to it all righty guys so we're going to go on ahead and take a look at what cnn layers see so if you haven't heard of cnn's before it is a convolutional neural network layer and it is a common deep learning hidden layer or intermediate layer that you typically use for image based processing or spatial data so what we're going to be doing is we're going to be taking a pre-existing model and we're going to see what the specific intermediate cnn layers see this is something which i found ridiculously useful as of late so a couple of times this morning or a couple of times this week i was actually trying to work on some gans that i've got coming up for future tutorials and i was getting output from the generators which were just a black image and i'm like well hold on something has gone terribly wrong here either that or i have no idea what is happening so using this technique has actually been ridiculously helpful in terms of sort of debugging and taking a look at what the cnn is focusing on whether or not we've even got the right outputs and the right activations so rather than just stepping through a jupiter notebook like i've been doing lately we're actually going to write this one up from scratch because that's kind of how i prefer doing it and let me know in the comments below if that's sort of what you prefer as well now we are going to be picking up where we left off from the image classification tutorial so if you want to pick that one up i'm going to link it somewhere up there and i'll probably throw it down in the description below but if you actually take a look we actually went through and built an entire image classification model so the link is at github.com image classification so you can actually pick that one up there um and all of the code and data and models i believe the models are there yeah so you've actually got this image classifier h5 so i've actually linked the final train model now i've called it something else in this tutorial i think it's happy sad classifier but you could pick up this h5 and do it with the or through the exact same process that we're going to be going through to date so let's actually kick this thing off so the first thing that we need to do is import a bunch of stuff we are going to first up import either tools and this is really going to be used to actually loop through the outputs outputs of cnn and it just makes it a little bit easier to visualize in a grid pattern because we're going to use matplotlib in a sec to actually visualize that so let's import matplotlib so we're going to use that to do some vis so matplotlib is going to be used and specifically we're going to be using the plot.iamshow function to actually visualize the outputs from the intermediate cnn layers we are also going to need opencv and numpy right so we've imported opencv there oh import opencv and we're really going to use that to open images with python and we're going to bring in numpy as well and numpy is really going to be used i think we're just using the expand dimms function so this like wraps an array inside of another set of arrays so you'll see that in a second so that's opencv that's numpy we're now going to need a bunch of stuff from tensorflow so import and self-flow as tf we're going to need the sequential model so import actually it's going to be from tensorflow keras dot models import load model and we're also going to need the sequential api uh what have we done import uh we spelt this wrong tensorflow cool all right so we've gone and brought in a bunch of stuff there so the last two lines that i wrote were import tensorflowers tf and then from tensorflow.keras.models import load underscore model and sequential let me just double check we've got everything that is looking pretty good all right cool so tensorflow is going to be used as our main deep learning architecture so main dl architecture and we also need the load model function and the load model function is going to be used to load up that h5 class that i had there or the serialized model now i've got it called here what do we got it called happysadmodel.h5 really great naming nick but anyway you sort of get what it is the the deeply learning architecture or the deep learning model that we're going to be using is an image classifier and it classifies people as either happy or sad so i mean maybe good work nick i don't know sequential is going to be used in a really unique way so what we're going to do is once we've loaded up that h5 model right we're actually going to specify which intermediate layers we want to bring in now we can actually use the model.summary function to visualize what layers are actually available so you'll see that in a second so this is going to be for modeling over here cool all right so we've got those now imported the first thing that we actually want to do is actually load up our models we can type model equals load underscore model and then we just need to pass through the path to the h5 file so let's say for example you're doing this with a different h5 file you just need to pass through the path to your specific h5 or serialized model so we are going to load up the existing trained model here and if we actually take a look you can see it is called happy sad model so we're actually going to pass through and that's inside of a folder called models so this is my base or root folder from which i'm working on this is the neural uh the jupyter notebook that i'm currently working on cnn layers dot ipnb and i'll make all this code available in the description below as well so you can pick this up if you want so what are we doing we're loading up our model first so dot forward slash models forward slash uh what's happy sadmodel.h5 cool so this will actually load up our existing h5 model right so it's going to go into here it's going to grab this h5 file and it's going to load it up so if i run that you can see that that's loaded successfully if we type in model you can see it's loaded up a sequential model which is exactly what we want but you're probably thinking nick what the hell is happening here i've got a model how are we going to specify which layers probably the most useful thing when it comes to working with tensorflow is that you can actually type in dot summary after a sequential model and it's going to show you what layers are actually available inside of that specific model so here we can actually see that we've got a convolution layer you can see that it's specified there so if you see conf 2d or con 3d or conv-1d these are convolutional neural network layers we've also got a max pooling layer so we could actually take a look at the outputs following the conv 2d layer but also the max pooling layer we could do the same for this layer this layer this layer this layer uh if we're going to do it with dense we need a little bit of tweaking but for now let's focus on the convolutional layers because i find that that's where a lot of the time you tend to have to do a bit of debugging to ensure that your neural network is doing the right thing now how do we actually go about getting these layers well we can actually type in model let's actually just quickly make a comment here so view the model summary and what are we doing so we are taking a look at our laser model.layers so you can see there that we can actually grab specific layers from a neural network or right now i haven't shown you how to grab them but you can see that we can actually output all of the different layers if i wanted to grab a single layer i can actually go give me layer 0 and this layer over here corresponds to this one here so you can see that we've actually gone and done that now we can also grab it by name but because i haven't actually defined the name it's going to be a little bit crappy but we can type model dot i think get layer by name get layer and then i think we pass through so let's say for example we wanted this layer conv 2d which is the first layer so you can see that we're now grabbing that layer if i wanted to grab this one i can pass through the name to that and i've passed through a space there but you can see it's now grabbing that layer so you've got a little bit of flexibility when it comes to actually grabbing these layers we're not going to do it like this we're going to use the regular slicing so let's say for example we wanted to grab the first i don't know one two three four layers right actually let's grab the first three because then we're still going to be outputting from a convolution we can actually go and specify that we want the first three and all i'm doing is i'm passing through a colon and then the number three so this is basically saying get me everything or from the third position onwards grab me everything behind and because we start at zero it's going to grab layers 0 1 and 2. 0 1 and 2. so you can see that we've now got the specific layers that we want now we can actually store this or we should probably store this inside of an intermediate variable now if we didn't really want to we could still actually do something a little bit unique so we could type in uh let's call it intermodal uh actually let's call it output cnn model i don't know let's call it into model so intermediate model right and then we could set it past those specific layers so model.lase we could actually pass that through to the sequential api which we brought in from up here so if you've never seen the sequential api i highly recommend you go and take a look at the what is it the image classification tutorial that we did because it was super super useful and we actually went through a ton of stuff now actually rather than defining this as the intermediate model let's actually call this intermediate layers so we know that we're going to be storing the intermediate layers inside of this variable right so this represents the train keep in mind these are trained layers trained intermediate layers now what's cool about the keras sequential api is that we can take these intermediate layers and push it into a new model so we can actually take those intermediate layers and actually pass it through this through to the sequential model now this is how you might actually go about doing transfer learning you could actually take these layers and then stack on whatever else you wanted after the fact to be able to effectively transfer that knowledge from the existing train deep neural network let me know if you want to trial on transfer learning as well okay so we've got our intermediate layers so we can run that so again if we go and take a look at our intermediate layers you can see that we've got all of our intermediate layers there now what we actually want to do because right now there's no real way to pass through some input right right so there's no way to actually go and type in dot predict there's no way to actually go and output something as a result of this so we can actually take those intermediate layers and wrap it inside of the sequential api so we're going to create a new variable called intermediate model and we're going to pass those intermediate layers to our sequential api oh i'm going to tabbed boom boom all right so this should be the intermediate model with trained trained existing layers right so now if we take a look intermediate model we now have it wrapped inside of our sequential engine which means we can type in dot predict and all of that good stuff we should also be able to type in dot summary and we've now got an intermediate model how sick is that so this means that we can now load up an image and we can actually visualize it by passing or visualize the outputs of this final output layer by going through this intermediate model right which is super super useful so let's actually add a comment here the intermediate model is now represented as just the three layer just the x layers that we took i put x because really we could change this right so we could grab four layers we could grab five layers grab all of the convolutional neural network layers but for now we're going to take a look at the last three and that is our intermediate model okay what's next we actually need to grab an image so let's go and grab an image now key thing to keep in mind is that our intermediate model is going to need images of a specific shape so we can take a look at that and this is sort of good practice as well because if you're working with neural networks that you've never seen before you're actually going to get the chance to see how to determine how to work with them right so i can type in intermediate let me bring that up a little bit let's minimize that so i can type in intermediate dot input shape uh intermediate model dot input shape and this is telling me that the image needs to be of size 256 by 256 by three so if we're going to load up an image we need to go and convert it to be 256 250 6x3 so it'll be 256 pixels what is it uh so rose height so it'll be 256 pixels wide by 256 pixels high by three channels i can never get wet which goes first rows or height but just know that it needs to be 256 by 256 by three okay so what do we want to do first things first we actually need to go and actually get an image now i've got a bunch of images that were originally from the image classification tutorials i've got this one here which is a person which is happy and this dude here which is deep in thought and i guess i would say probably not so happy so we can actually load those in using opencv and remember we imported opencv up here so import cv2 which means we can use the cv2.iamread function let's actually add a comment for this line here though um take a look determine required input shape all right so load image um so load the image and we're going to load in the happy one first so the file name is happy.test or happytest.jpg so let's copy that you could work with a bunch of different images as well so we're going to call it image equals cv2.imread pass through the full file name or the path to the file because we're working inside of the root folder we can just pass through the file name and we should be good and we need to resize it as well so we're going to type in cv2.resize and we're going to pass through our image and we're going to pass through the new dimension so it should be 256 by 256. i think that'll work i can't remember if we need to pass through the channels let's try that okay so resize dot shape right that's looking good all right so we've gone and done or written two lines of code there so the first one is image equals cv2 dot i'm read and to that we're passing through the full file path to our specific image which in this case is happy test.jpg and then we're taking that variable so image and we're passing that through to the cv2.resize function you can see that that this is going to here and then we're specifying the new dimensions for that image which is going to be 256 by 256 and then that variable is then going to be called resize so we can see that resize.shape is 256 by 256x3 which just so happens to match the input shape of our intermediate model so this means we'll be able to take this image now pass it through to our intermediate model and get some output so if we actually do that without a comment for this last line view the shape of the resized image uh so what are we doing we're making prediction make a prediction so if we type in intermediate model uh dot predict and all right i'm gonna throw an error here because i want you to sort of see why we need to do a particular step so if i type in intermediate model dot predict and pass through resize this is gonna throw an error you're probably thinking nick what the hell like we just went and resized this image to be the same as the input shape why we get an error and if we go and take a look at our error you can see it's saying input 0 of layer sequential underscore one is incompatible with the layer expected shape none 256 256 by three let's zoom out how did i zoom in it should be that my bad you're probably thinking but we've made a 256 by 256x3 nick what is happening well it's saying found shape i hope my head's not covering that found shape equals three eight three thirty two that looks like it might be a little bit wrong thirty two by two two fifty six by three might be transforming it but anyway it's saying found shape 32 256 by 3. this is because we haven't gone and passed it through correctly when we go and make a prediction using the dot predict function it is actually expecting you to be passing through a batch of data so this means the first dimension will represent a whole bunch of images not just one this is easily resolved we can just type in np.expand underscore dimms and then pass through what dimension we want to expand on which will be the first dimension which is zero boom now we've got a prediction so that's a really easy fix and that's the reason that we actually bring in numpy over here so if i type in my intermediate underscore model dot predict and then wrap it inside of np.expanddims i'm now going to get the correct shape so if i actually show you this right all i'm doing is wrapping it inside of another set of arrays so now we've got the batch size by the pixels by or by the width by the height and then by the channel so now we can actually pass it through dot predict perfect okay so now if we actually store this inside of a variable called y-hat it's common terminology when it comes to uh notation for a prediction we've now got our prediction so if we take a look at y hat we've got our outputs from our intermediate convolutional neural network pretty cool right so if we take a look at dot shape the outputs that we're getting are 125 by 125 by 32 and the 32 is probably because we have 32 convolution of 32 filters in our final layer which is exactly that so you can see that our convolutional neural network layer actually has 32 filters and we're getting the output of each one of those filters so you can actually specify which filter that you'd want to actually go and take a look at as well for now we're just going to take a look at all of them because we're interested to see what is happening in the background right so so far let's take a quick recap of what we've done so we've gone and imported a bunch of dependencies so eta tools map plot lib we then went and loaded our model using the load model function from over here i showed you how to visualize the models and we can type in model.summary or intermedia underscore model.summary to do that and this is pretty common if you're working with any sequential models in keras we then took a look at how we can grab specific layers how we can pass those through to a new sequential model to be able to actually build up and use that predict function uh summary you've sort of seen that before uh where are we up to that just did a little quick weird skip and we can take a look at the input shape and we know that that none function base or that none uh space represents the number of batches that we're going to be passing through we then went and loaded our image and we went and resized it keep in mind we can go and visualize our image using plot dot i am show this is our resized image as well right all right so we can go and visualize that just by using plot dot i'm show and you can see that we've got this weird colorized or coloration and that's because opencv reads in an image as bgr matplotlib visualizes it as rgb so we can fix that using cv2.cvt color so this is almost like a color conversion function inside of opencv and then we pass through the new color conversion code so cv2.bgr oh actually what is it cv2.color i think yep and then it's uh bgr2rgb right so you can see the colors the color representation is a little bit more accurate there now so if we take a look at the happy test image even though it's huge you can see the colors are the same right cool i'm showing you this now because it's going to come in handy or quite a fair bit when we actually go and visualize it in a second so this is uh what is it so vis the resized image okay now all that we want to do is we want to visualize the output from our neural network so rather than actually just seeing that we've got outputs we probably want to see what those actually look like now they're going to be some sort of representation of this but once it's passed through the convolutional neural network so i'm going to write some code and then we'll break it down because there's a fair bit to come here so first up we're going to specify the number of columns and number of rows we're going to have in our visualization grid so let's actually create this a vis grid and we're going to specify the number of rows i don't know we'll set that to four the number of columns we'll set that to four so this means we're going to have a four by four grid so we'll see 16 filters at the time or at a time you could definitely expand this out might get a little bit smaller but you sort of got the idea and then we're going to create the grid so we can use plot.subplots for this so we're going to call fig.ax equals plot.subplots and then we are going to specify the number of columns so n calls equals the number of columns and rows equals the number of rows it should be n rows and then we want to pass through fig size all right so let's quickly break down what we've got and done so far so the first thing that we're doing is we're specifying how big our visualization grid is now if we wanted to just visualize one filter at a time we could definitely do that let's actually quickly take a look at how to do that first up so remember y hat is so we've got one filter or what we've got one output but we've got 125 pixels by 125 pixels by 32. so if i actually went and said grab the entire amount or let's sorry we don't want the shape so if i went and said y hat zero type in dot shape so we've now got 125 by 125 by 32. now if i said zero and then grab everything grab everything and then grab just one so we've now got a pixel output which is 125 by 125. i could actually output zero here now if i took that output and passed through to plot.i am show let's try this you can see that that is the output from one convolutional neural network layer so this sort of gives you a lot of flexibility when it comes to visualizing those outputs now we can see that it's definitely highlighted the teeth so maybe that's an indicator that teeth is a really important determinant of the of determining whether or not something is happy or sad we can take a look at another filter so if i pass through dot one here or just one so again highlighting different areas now so it looks like cheeks are being highlighted doesn't look like it's doing too much with the the arms and whatnot what about two okay so again teeth looks like it's highlighting the arm there let's zoom in a little bit more so you can see that again we've got some different highlights so it looks like the teeth again are being highlighted what about three okay so again teeth uh looks like hair we've sort of got the outline of hair teeth so it looks like teeth might be a really important determinant of whether or not you're happy or sad so again teeth are being highlighted there looks like that filter's not really picking up all that much that one might not be all that useful okay so this one looks like it's outlining the outline of her body so this might be very much focused on her body or the shape of her pose for example but this might take a little bit of time so let's actually go and finish our visualization grid and this is what i tend to do so rather than doing one by one which is still definitely a possible this actually sort of gives you the ability to visualize multiple outputs at the same time so again we've written num underscore rows equals four num underscore columns and we set that equal to four so we're going to be able to visualize a four by four grid and then we're starting to create a subplot and think of this just like a grid right so you're gonna have multiple plots and when you've got multiple plots we're typically referring to those as subplots so it's just multiple plots under one bigger plot so we can refer to the figure from and so this is a matplotlib function i don't really know why we call it figure but i know that what's really important are the axes so the axes represent each individual subplot so we'll actually when i create this we'll actually get 16 different values inside of this ax value so fig comma ax equals plot dot subplots and then we're specifying the number of columns that we want in our sub subplot so n calls equals num code so we're setting it to that n rows equals num rows so we're setting it to that and then we're specifying the figure size which is going to be 10 by 10. what we can then do is loop through each one of the outputs inside of the or our y hat variable and visualize those individually so let's go ahead and do that okay before we go any further let's actually quickly take a look at what i've written there so this was something that i was working on to make it or to ideally try to get this into a single liner and i was playing around with this this week either tools.product effectively allows you to create a whole bunch of combinations from two different lists so if you're trying to work out how to visualize the product of two different lists either tools.product super easy right so i can type in either tools dot product pass through i don't know um let's create two lists right so one two four for example and then i don't know let's create another list uh five six seven so if we actually app with the values to vowels uh and then if we type in dot what how do we get the next one for val in print val so you can see that we're actually getting every single combination that can be made between these two values so every single permutation on combination right which effectively allows you to do exactly what you need so in this particular case we need every single permutation of rows and columns so we're going to have position 0 1 or 0 0 0 1 zero two zero three zero that's it then one zero one two one three so this will allow us to get all of the different axes from this ax value over here and effectively what we're gonna do is once we've got the specific position inside of our grid we're going to visualize using ax dot i'm show so i just wanted to sort of give you a little bit of background as to why we're doing this now over here what we're doing is we're just passing through the range for our number of columns and number of rows so if i substitute these two values out plug these in here so what we've actually got are all of the different positions for our subplots so that is effectively what is it row one row two row three and row four so on and so forth and then you've got the individual column positions as well so that is that over so that's pretty much explaining that and then we're sub putting that into a list and then we're enumerating so we get both an index and the ax index so the index is going to be used to grab the values from our y hat variable so if we actually take a look i can then go let's do the entire thing right so we can type in enumerate put this inside of a list and then we are effectively getting an idx and then ax idx so if i print out our index so this is going to determine which position we want to grab from our y hat variable so if i actually went and said y hat and then we're going to grab the first value all the pixels also all the width or the height and then we're grabbing that specific index so you can see we're grabbing the output from each individual filter from our convolutional neural network by doing this cool cool okay so now the last thing that we need to do is actually go and plot those out so let's do that and there we go so we're now outputting the results from all of our filters so we can delete this we don't need that don't need that so you can see that effectively what we're doing is we're just visualizing a grid so rather than doing it one by one over here we're just outputting it a little bit more nicely so let's quickly take a look at that last line of code so what we're doing is we're grabbing the axes from over here we're using our indexes to be able to grab each individual position that we want so ax underscore index 1 i think represents i can never remember which one's the columns versus the rows but you sort of get the idea let's actually play around with this so if i go number of columns um three now four let's actually do five right so if i go five and then let's say we grab more so what is that going to do we're gonna screw up so we've now got a new we don't have anything so wait one two three four five okay so that's worked out okay what if i set this through two i know four and four what breaks first okay so that looks like the five is referring to the column so that means that the index value one is going to be trying to think now so let's actually take a look at the matplotlib um documentation subplots so what do we have here some matplotlib subplots we have x and no wait hold on so we've got the two we go subplots in two directions so we've got when stacking two directions the returned axis is a 2d numpy array if you have set parameters for it it's highly the iterator for some plots using 2d grid for ax um this really still doesn't tell us one ax1 ax2 so ax1 ax2 so that is going to be so the first the first set of values are going to be the rows so ax1 x2 represents the rows so row row green red and then the second two values represent the columns okay all right cool so this means that the first two values are actually representing the rows so let's actually take a look at ax right so this is row one this is row two this is row three this is row four right so if i went and said grab this and made it uh num rose 10 that means that we should effectively get 10 values in that first set of arrays so four columns so if i go and type in ax now there you go so we've now got 10 different rows exactly that all right cool that's making more sense it's killing me that i couldn't remember that work okay so then what we're doing is we are effectively grabbing our so if we went and said this first value to the number of what is it number of rows so num rows some calls then we ideally we should be switching these right because we want to grab the first which would be the rows i'm probably confusing you guys a whole heap more but i want to make sure that this makes sense okay yeah so that still works right okay so if you're going to the first value that you want to pass through to your ax underscore index is going to be the number of rows or the position of the rows the second value is going to be the position of the columns so just keep that in mind but again you can play around with this see how it works and and go through it in a bit more detail so i think i switched these around so yeah so i originally had columns and then rows but now rows and columns sort of makes more sense because the rows are the first values that you're going to get out of this ax value but again i figured i'd show you what that effectively looks like and that is how to visualize the intermediate outputs of a cnn layer so i know i sort of went through a bunch more detail there um it doesn't look all that great you can obviously make this plot a whole heap bigger by changing this fig size value i think if you go 20 there you go so you just got a bunch more space around it but that at least allows you to see it in a little bit better detail but let's take a look so again very much focus on the person's teeth being white because that is obviously a key determinant of you smiling and you sort of being happy now we could obviously pass through a whole different image so if we went and grabbed our sadtest.jpg sadtest.jpg but that's our sad dude let's see what that's visualizing okay so again it's sort of outputting his body let's take a look through the subplots hmm there's no real key feature here apart from the fact that it's not really highlighting the mouth so i mean it sort of at least shows that we're getting different outputs from our cnn layers but there's no real sort of trend here whereas when we were taking a look at a happy photo was very much focused on that person's mouth so again this is a really unique way of actually taking a look at how your cnn layers work and delving into that a whole bunch more hopefully this has been useful let me know in the comments below if you like more sort of tech or deep learning deep dives like this i found it useful because i'm using it quite a fair bit at the moment but that sort of gives you an idea as to how it all works now quickly recap so we went and brought in a bunch of dependencies we went and loaded our existing h5 model and if you want to grab the one that i was using just grab image classifier from the previous image classification tutorial we then took a look at the summary the intermediate layers uh we took a look at our summary over here for our intermediate model we then loaded up some images resized them and then we went and took a look at how we can make predictions with our intermediate model and last but not least how we can visualize our output grid and that about wraps it up thanks again for tuning in guys peace hold up guys editing nick here so i just realized that i didn't show you how to go and pass through a different number of layers to the model so let's jump back on over to the computer and do that right so the last thing that i wanted to quickly show you is how you go about doing this if you wanted to visualize a different set of layers right so keep in mind that the thing that we were looking at up here was this layer so we're looking at cons 2d underscore one so that was the core layer that we're currently looking at at a point in time so what would we need to do in order to visualize a different layer or the outputs from a different layer well all we really need to do is change the number of layers that we're grabbing as part of our intermediate layers variable so i could go and grab let's say we wanted to grab the outputs after a max pooling layout so i could pass through the index four which would mean i'd grab everything before index four so if we take a look at intermediate layers now you can see that we've now we're now ending up at this max pooling layer and again it's pretty much the same step so we can go and run through the next couple of lines of code and now you can see that because we've gone and applied a max pooling layer it's a little bit more pixelated because that's effectively what a max pooling layer is doing it's grabbing the max value from a specific region and sort of condensing it down and condensing the amount of information that's being passed through to the next layer in our artificial neural network so if we now go and visualize that again you can see that it's sort of emphasizing the mouth and highlighting the mouth a whole heap more now again we can go and change this and take a look at our sad test image and what have we gone down there we just got to get rid of the second.jpg so again it's visualizing our dude what's that highlighting here maybe his t-shirt maybe his hands looks like it's highlighting in the background so again no real sort of pattern or trend there with our sad test now again if we wanted to go and get a different layer so you really just need to change the index for the specific layers that you're pulling so let's say we wanted to grab i don't know uh this layer over here so com to underscore or columns 2d underscore 2. so this would be 0 1 2 3 4 yeah 4. so we'd effectively be grabbing everything before index five so if we go and change our value inside of model.layers replugging in my zoom so i can zoom in so really all we're doing in this particular case is we're changing the index that we're passing through over here so remember we wanted to we started off with index three if we want to go and grab the next layer we can grab index five so on and so forth so if we go and run this now so you can see that we've now got zero one two three four different or the fourth layer effectively fifth layer the fourth index so if we go and take a look at our visualization you can see that that's almost reduced it to nothing right so if we go and take a look at our output it looks like we're throwing an error and why are we getting so we've got zero one two three four okay so right now we're trying to visualize 20 different cur this is actually a good error right so right now we're trying to visualize 20 different filters so four by five now if you keep in mind that the last output layer that we had only had 16 filters so hence we're getting that error so what we need to do is readjust it so if we go and change that back to four over here we should be good to go and there you go so you can see that we're now visualizing all of those different layers and you can see that some of the kernels are actually returning back absolutely nothing so nothing nothing nothing nothing nothing nothing nothing nothing that's okay it might just be an indicator that because this particular image doesn't have certain features that it's looking at identifying it's not returning anything back now we can try this with happy test or a happy image okay so you can see that mouth is being highlighted in one but over here it's looking very much of the pose of the person's body so maybe it's found that pose is just as important in this particular case this is where skip connections are really really useful because you're still able to maintain certain information from previous layers in a specific architecture but i figured i'd quickly show you how to go and change the number of layers that you're able to visualize so you if you want to dig into it a little bit more that you definitely can that about does wrap it up this time yes thanks so much for tuning guys hopefully you enjoyed this video if you did be sure to give it a big thumbs up hit subscribe and tick that bell and let me know what you thought of this almost sort of tech technique short i found it it's really really useful if you're building deep neural network architectures and you're just trying to work out have i got the right architecture have i got the right activation do i have enough complexity in my neural network to be able to handle my specific problem anyway thanks again for tuning in guys peace you
Original Description
Learn how to visualize the outputs of intermediate CNN layers. This helps a ton to see how your computer vision models are making predictions.
Get the code: https://github.com/nicknochnack/CNNLayers
Chapters
0:00 - Start
0:31 - Explainer
1:16 - Code
3:13 - Import Dependencies
5:52 - Load a Trained Model
8:10 - Accessing Layers
10:45 - Creating an Intermediate Model
13:04 - Load an Image
16:26 - Predictions with the Intermediate Model
21:57 - Visualising the Output
25:05 - Create a Viz Grid
36:20 - Visualising Different Layers
40:24 - Ending
Oh, and don't forget to connect with me!
LinkedIn: https://bit.ly/324Epgo
Facebook: https://bit.ly/3mB1sZD
GitHub: https://bit.ly/3mDJllD
Patreon: https://bit.ly/2OCn3UW
Join the Discussion on Discord: https://bit.ly/3dQiZsV
Happy coding!
Nick
P.s. Let me know how you go and drop a comment if you need a hand!
#cnn #python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Nicholas Renotte · Nicholas Renotte · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Face Detection - Build An Image Classifier with IBM Watson - Part 7
Nicholas Renotte
Food Image Classification - Build An Image Classifier with IBM Watson - Part 6
Nicholas Renotte
General Image Classification - Build An Image Classifier with IBM Watson - Part 5
Nicholas Renotte
Installing Watson Developer Cloud - Build An Image Classifier with IBM Watson - Part 4
Nicholas Renotte
Generating Credentials - Build An Image Classifier with IBM Watson - Part 3
Nicholas Renotte
Creating A Service - Build An Image Classifier with IBM Watson - Part 2
Nicholas Renotte
Getting an IBMid - Build An Image Classifier with IBM Watson - Part 1
Nicholas Renotte
How to Analyse Review Data - Part 2 - Python Yelp Sentiment Analysis
Nicholas Renotte
How to Lemmatize Text - Part 4 - Python Yelp Sentiment Analysis
Nicholas Renotte
How to Calculate Sentiment Using TextBlob - Part 5 - Python Yelp Sentiment Analysis
Nicholas Renotte
How to Collect Business Reviews Using Python - Part 1 - Python Yelp Sentiment Analysis
Nicholas Renotte
How to Clean Text Based Data for NLP - Part 3 - Python Yelp Sentiment Analysis
Nicholas Renotte
How to Setup a IBM Watson Personality Insights Service - Part 1 - Watson Personality Insights
Nicholas Renotte
How to Create a Customer Profile with IBM Watson - Part 2 - Watson Personality Insights
Nicholas Renotte
Visualising The Profile Part 3 Watson Personality Insights
Nicholas Renotte
How to Plot Personality Insights Features at Lightspeed - Part 4 - IBM Watson Personality Insights
Nicholas Renotte
Getting Started With IBM Watson Studio Machine Learning - Part 1 - Predicting Used Car Prices
Nicholas Renotte
Upload and Visualize Data In IBM Watson Studio - Part 2 - Predicting Used Car Prices
Nicholas Renotte
Clean Data and Feature Engineer in IBM Watson Studio - Part 3 - Predict Used Car Prices
Nicholas Renotte
Using Watson Model Builder to Predict Car Prices - Part 4 - Predicting Used Car Prices
Nicholas Renotte
Deploy and Make Predictions With Watson Studio - Part 5 - Predicting Used Car Prices
Nicholas Renotte
Getting Started With IBM Watson Discovery - Part 1 - Stock News Crawler
Nicholas Renotte
How to Run Advanced Queries with Watson Discovery - Part 5 - Stock News Crawler
Nicholas Renotte
How to Run Search Queries with IBM Watson Discovery - Part 4 - Stock News Crawler
Nicholas Renotte
How to Understand the Watson Discovery Data Schema - Part 3 - Stock News Crawler
Nicholas Renotte
How to Build a Watson Discovery Web Crawler - Part 2 - Stock News Crawler
Nicholas Renotte
AI learns what to do next using Tensorflow and Python
Nicholas Renotte
Chatbot Crash Course for Absolute Beginners - Full 20 Minute Tutorial
Nicholas Renotte
Shopify Customer Service Chatbot using Python Automation
Nicholas Renotte
Building a Reddit Keyword Research Chatbot
Nicholas Renotte
Chatbot App Tutorial with Javascript Node.js [Part 1]
Nicholas Renotte
Javascript Chatbot From Scratch with React.Js [Part 2]
Nicholas Renotte
Predicting Churn with Automated Python Machine Learning
Nicholas Renotte
Sales Forecasting in Excel with Machine Learning and Python Automation
Nicholas Renotte
Automate Budgeting with Python and Planning Analytics
Nicholas Renotte
AI vs Machine Learning vs Deep Learning vs Data Science
Nicholas Renotte
Optimizing Marketing Spend using Linear Programming || Marketing Opt PT.1
Nicholas Renotte
Solving Optimization Problems with Python Linear Programming
Nicholas Renotte
Loading Data into Planning Analytics with Python || Marketing Opt PT.2
Nicholas Renotte
Building Marketing Dashboards with Planning Analytics Workspace || Marketing Opt PT.3
Nicholas Renotte
Optimizing Resource Allocation with Docplex and Planning Analytics || Marketing Opt PT.4
Nicholas Renotte
Exploratory Data Analysis With Pandas || Python Machine Learning PT.1
Nicholas Renotte
Preparing Pandas Dataframes for Machine Learning || Python Machine Learning PT.2
Nicholas Renotte
Python Machine Learning with Scikit Learn - Regression || Python Machine Learning PT.3
Nicholas Renotte
Deploying Machine Learning Models with Watson Machine Learning || Python Machine Learning PT.4
Nicholas Renotte
Mind Blowing Machine Learning Apps with Node.JS and Watson Machine Learning || Python ML PT.5
Nicholas Renotte
Build FAST Machine Learning Apps with Javascript React.Js and Watson || Python ML PT.6
Nicholas Renotte
Analyzing Twitter Accounts with Python and Personality Insights
Nicholas Renotte
Converting Speech to Text in 10 Minutes with Python and Watson
Nicholas Renotte
Build a Face Mask Detector in 20 Minutes with Watson and Python
Nicholas Renotte
AI Text to Speech in 10 Minutes with Python and Watson TTS
Nicholas Renotte
Pandas for Data Science in 20 Minutes | Python Crash Course
Nicholas Renotte
Language Translation and Identification in 10 Minutes with Python and Watson AI
Nicholas Renotte
Analyse ANY Conversation in 10 Minutes with Python and Watson Tone Analyser
Nicholas Renotte
Deep Reinforcement Learning Tutorial for Python in 20 Minutes
Nicholas Renotte
NumPy for Beginners in 15 minutes | Python Crash Course
Nicholas Renotte
Real Time Pose Estimation with Tensorflow.Js and Javascript
Nicholas Renotte
Transcribe Video to Text with Python and Watson in 15 Minutes
Nicholas Renotte
Serverless Functions for TM1/Planning Analytics in 20 Minutes
Nicholas Renotte
Building a AI Budget Bot for Planning Analytics with Watson Assistant in 20 Minutes
Nicholas Renotte
More on: CV Basics
View skill →Related Reads
📰
📰
📰
📰
Help Choosing Neural Network Architecture for Matrix Classification
Reddit r/deeplearning
How to Choose the Best Deep Learning Model for Medical Imaging
Medium · Deep Learning
Another Way to Read Neural Geometry
Medium · Data Science
Another Way to Read Neural Geometry
Medium · Deep Learning
Chapters (13)
Start
0:31
Explainer
1:16
Code
3:13
Import Dependencies
5:52
Load a Trained Model
8:10
Accessing Layers
10:45
Creating an Intermediate Model
13:04
Load an Image
16:26
Predictions with the Intermediate Model
21:57
Visualising the Output
25:05
Create a Viz Grid
36:20
Visualising Different Layers
40:24
Ending
🎓
Tutor Explanation
DeepCamp AI