what's up everybody and welcome to part
seven of our unconventional neural
networks series in this tutorial what
we're gonna be doing is getting into
deep dreams so the following here is a
video I posted a little bit ago it's a
video of a deep dream basically where I
let the machine have a deep dream we
zoom in a little bit have a deep dream a
little bit more it's actually a few
pixels at a time and then you can
basically take those as frames and
render a video out of it and this is the
result we're gonna run through all the
steps there but the first step of course
is just to have a single deep dream
example so with that this is like an
example of just one frame so we start
with the Andromeda galaxy and then let a
deep dream kind of run wild for a few
iterations and you get something like
this and that's basically what deep
dream is so so how do we do that so
first of all let me I'm just gonna close
out of this and the main bit of code
that we're gonna use is actually from
here it's from these tensorflow
tutorials which I'll link in the
text-based version of this tutorial
because they're actually really good
tutorials they're a bit dated but the
information that's in them is very good
so not only do the does he cover stuff
on on deep dream but there's just a
whole bunch of other stuff that is is
useful information to know how to
incorporate it with tensorflow just a
real good tutorial series we're
currently just gonna be working with
part 14 here but yeah definitely check
it out
so in order to follow along you will
need one thing for sure well first of
all you're going to need Python 3 6 I'm
using tensorflow 1 7 you should be able
to get by actually with anything 1.4 or
greater I'm using 1.7 and then you're
also gonna need open CV so pip install
OpenCV - python otherwise you can always
go to the unofficial binaries if you're
on Windows and you can't get it just
search for open C actually just open CV
one word click on that boom download the
one that matches you okay otherwise what
I've done actually is I start I hosted
on Python programming net I'll try to
remember put a link to this in the
description but I might forget - just
deep dreaming start zip so go ahead
grab that I'm just gonna grab that and
put that in my working directory here
right and once you have that let's go
ahead and just extract it's kind of
large because we have the inception
model I also have a starting image I
would suggest you use something
relatively small this is an 800 by 450
the larger the image the more processing
it's going to take to run through that
entire image but at the end of this I'll
show you another deep dream that I made
and this time I did a full like it's a
1080p dream it starts off the starry
night but it's actually a 1080p you
could even go to 4k but it will take a
really long time anyway once you've got
that extracted this is all our starting
code and basically download we don't
really need to touch that inception we
don't need to touch that we've got dream
image and then later we'll make
something for a deep dream so this is
basically the actual dream code that
belongs to this Magnus Eric H vas
Pedersen anyway this is basically
identical to what we were just looking
at in an eye Python notebook I just kind
of turned it into a module and then we
can use dream image here which let's see
if I've yes so this one I have populated
so we don't even have to really run I'll
just run through what I've done I
couldn't remember if I actually populate
this or not so so what's going on here
is let's just explain it I guess line by
line I have some notes up here that all
we can reference as well but basically
everything you need to know is in here
in fact I want to open this up in
sublime over
so from the little basically package
that I grabbed I'm just gonna say we're
gonna import the model load image and
recursive optimize which is what's going
to the recursive optimize function there
that's what's basically doing the deep
dream then we specify which layer we
want to use now each layer is a little
different so like layer one is like just
kind of like waviness basically it's
really not much then layer two and
basically as you go the layers are more
likely to get more and more complex
layer two is like lines layer three is
like boxes and squares and stuff flair
for is circles I could even just add it
in here layer five is like eyes like
it's very good at it like sees eyes it's
kind of a creepy layer to be honest but
it very quickly transitions into like
dogs bears just kind of cute furry
animals layer seven gets into like faces
and buildings eight is like fish frogs
reptiles stuff like that
ten is like monkeys lizards snakes ducks
like kind of like and in the it's sort
of like everything before it and then
layer eleven I can't remember what layer
eleven is it's like sometimes like it's
like weird-looking things like I can't
really figure out exactly what layer
eleven is but again it's always like a
combination of everything before it so
anyway coming back down here that's the
layer so you'll specify which layer are
we going to attempt to use in this case
we also need a file name in my case it's
not saved in the starry night it's
actually just local so it's just the
sorry night 800 by 450 and let me just
confirm that real quick make sure so
it's actually not in this directory yet
oh he's in the story night oh and I
saved oh man I so I made this all like
days ago and I've you know days and
internet time is you know like a year so
so I forgot I did this anyway so they're
all here for you but I encourage you you
don't have to use my images I encourage
you to use your own images but I guess
let's just leave I'll leave that there
cool I just made my job so easy anyway
you can use any image you want just take
note of the resolution but other than
that and even in this case we don't even
have to pass a resolution later to make
the video you do need to pass
resolution because we're gonna be like
zooming in a resizing hopefully actually
one of you guys will come up with a
better way than what I'm doing I'm sure
someone has something smarter than what
I'm doing so anyways maybe you won't
need it but anyway moving on so then
once we have the filename we're just
loading in the image from basically it's
it's using the deep dream load image and
in fact we I'm kind of curious what
their little function is for that cuz
I'm not sure what's special about theirs
[Music]
is that deep dreamer load image yeah I
was gonna say okay yes so basically it's
all doing is just using pill load the
image convert it to float 32 anyway
nothing fancy anyway so reason the image
that's gonna pass it to that recursive
optimize layer that's what's actually
gonna do the dream the other thing to
pay attention to is like how many
iterations are we going to take what's
the rescale factor num repeats and blend
the two major ones that I tend to kind
of muck around with is number of
iterations a number of repeats also
rescale factor is kind of takes into
consideration how many iterations you're
gonna do but all these things you can
just kind of tinker with and I think the
best way to get an idea for what they do
is to tinker with them and see what they
do but I will just say the number of
iterations is generally gonna be it's
like how clear is the dream versus
they're so like how smooth of a
transition are we gonna make so like for
example from here to here is a lot of
iterations like that's that's gonna take
a lot I forget how many exactly that is
but I would guess I don't know forty
sixty something like that like many many
many iterations whereas when you're
doing a deep dream for example if you
wanted it to be relatively smooth and
not be super jumpy you'd probably have
number of iterations as one like each
frame is just one iteration from there
so yeah so yeah and then num repeats
this is just basically how clear will
the dream itself be so like the number
of repeats that you're gonna have that's
like how many passes over the data or
how far deep into the layer I guess it's
really hard for me to put it into words
I really like I said I think the
probably the best thing to do is just to
run
through and see what I mean but anyway
moving along then we're gonna just go
ahead and clip it so the reason why
we're doing that is because you let the
neural network run wild it doesn't
understand that the bounds are 255 like
it doesn't understand that you you can't
go below zero it probably won't go below
zero but because it never got negative
data in the first place but it doesn't
understand that it can't necessarily go
beyond 255 so we want to make sure we
clip it then we're going to go ahead and
convert the type and then we convert it
to an image we save the image and then
if we want to we can make that image pop
up so what I'm gonna go ahead and do I'm
not quite sure what I changed but it
says I changed something so I'll save it
and then and in fact actually I can just
run it from here so this should work now
I'm doing 20 iterations so that might
actually take a decent amount of time I
might pause it while this is going
each iteration that you go do is gonna
be a little slower and then depending on
how deep you are in the network it's
gonna take even longer so I forget oh
we're only on layer 3 so this actually
should be pretty quick so we're starting
with this image which is you know in
general pretty wavy image so the results
already up for us so this was where we
started was with this thing and we ended
on this image which is you know it's
actually kind of interesting because it
you pretty much just changed anybody
who's like an art person like you you've
changed like the entire thing like it's
supposed to just be like strokes and
very wavy and like almost no jagged
edges except for the buildings and then
boom everything's jagged edges
anyway pretty cool like it just looks
cool so that's how you can at least
perform a deep dream on a single image
again I suggest to you that you go ahead
and try to do it with a variety of
images you don't have to use me--why
images if you don't want and then like
look 1 another thing I'll do is just
like let's say let's say instead let's
do layer well so let's do 3 but in this
this case let's do only one iteration
actually let's do like two iterations
just so it's not just like the original
image
this should go pretty darn quick we'll
see yeah so as you can see this doesn't
I mean it kind of changed it but but
barely like you wouldn't even I think if
you just saw this image you wouldn't say
oh that's been changed from the original
like unless you saw them side-by-side I
don't think you'd even recognize that
there's a difference but then like let's
go back to like 20 iterations and then
let's do layer mmm
it's to layer well so actually let me
just be clear layers start at zero so we
would say layer 11 but that's actually
layer 12 just for the record so we'll do
that and let's see what that one looks
like and for this I'm gonna pause
because it's probably gonna be a little
slow so let me pause it and we'll be
back when it's done okay so actually
here are a few different ones so this
would be with eight repeats just on
layer three so just a little more this
is with ten repeats so you can already
see like how much more in depth it gets
this would be layer 10 with 20
iterations and eight repeats looks like
another layer three just with a
different rescale then this would be 20
durations 0.9 rescale it looks like
layer eight or nine and didn't actually
save which layer that was and then again
I'm gonna guess this is layer 11 or
actually layer 12 but the index would be
11 anyway this would be 40 iterations 25
repeats in a point nine nine rescale so
anyways those are some examples but like
I said you definitely want to play
around and you could probably spend a
long time playing with your own images
and just doing even single images that's
the outputs are pretty cool but in the
next tutorial what I'm going to show you
guys is how you can do it to a video
just on basically your own dream and how
you can actually just make it frame by
frame so that's what we're going to be
doing the next tutorial if you have
questions comments concerns whatever
feel free to leave them below otherwise
I will see you in the next tutorial