ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

《边学Python边学数据科学》#5 TensorFlow上的DeepDream – 译学馆
未登陆,请登陆后再发表信息
最新评论 (0)
播放视频

《边学Python边学数据科学》#5 TensorFlow上的DeepDream

Deep Dream in TensorFlow - Learn Python for Data Science #5

西拉杰:大家好!我是西拉杰!
SIRAJ:-Hello World, it’s Siraj!
在这个视频中 我们将用Python和TensorFlow
In this video, we’re gonna create a neural network
来建造一个让图片产生
that makes images become really trippy
酷炫迷幻效果的神经网络
using Python and TensorFlow.
我们人类的磕药历史
We humans have been using psychedelic drugs
可以追溯到史前
since prehistoric time.
这蘑菇不简单啊!
-That wasn’t a normal mushroom!
它们会带给我们一次
They help manifest parts of our mind
通常不会有的体验
to conscious experience that wouldn’t normally otherwise.
磕药最常见的一个体验
One of the most common experiences
就是产生幻觉
from psychedelics are trippy visuals.
那些反复磕药的人称这是一种
People who have taken psychedelics repeatedly describe experiences of seeing
兼备睁眼和闭眼视觉的体验
both open-eyed and closed-eyed visuals.
但这些药究竟在脑袋里做了什么
But how? What are these drugs doing in our brain
让我们看见了本不存在的东西呢?
that makes us see things that aren’t really there?
要想找到答案 传统方法是
The traditional way for us to find out
用磁共振成像仪器检测那些
is to test live human subjects tripping balls
精神状态受药物影响的人 但现在 人工神经网络
under an fMRI machine, but recently, we’ve gotten artificial neural networks
也能做到 谷歌基于
that do the same thing, Google trained
带标签的图片数据集训练了一个神经网络
a neural network on a labeled dataset of
这些图片从松鼠到寺庙包罗万象
images — everything from squirrels to temples —
在训练这些图片的时候
and as it trained on these images,
神经网络在每一层都创建了内在表征
it built internal representations at each layer. Eventually,
最前面的两层网络学习一些低层特征
the first two layers learned low-level features
比如线啊角啊 然后一层比一层抽象
like lines and edges, and they got progressively more abstract
最后的几层网络则学习表示
so the last few layers were representations
脸以及更大的形状
for faces and big shapes.
所以我们看到的网络训练出来的每一个
So when we visualize one of the higher-level representations it built,
高层表达都是特征的混合体
we can see that it contains a mixture of features
比如混合着狗狗的眼睛和鸟的头
like the eyes of a dog and the head of a bird.
啊[推开]
Eugh!
而如果给网络一张新图片不去分类
And when they gave it a novel image and asked
只让它最大化新图片和网络中特定一层
not to classify it but to maximize the similarity between the image
特征之间的相似性时
and a representation at a particular layer,
就得到了一张颇为迷幻的图
the result was a very trippy image.
这一切听上去非常像磕药的经历
It all sounds very similar to the drug experience
这太疯狂了
which is insane.
我们的碳基大脑使用化学信号传递信息
Our brains are carbon-based and they use chemical signals as messengers.
但神经网络甚至不存在于物理空间中
A neural network doesn’t even exist in physical space at all.
它是一个表现在二元硅晶管上的
It’s an abstract concept being represented
抽象概念
on binary silicon transistors.
猜测这两个系统会衍生出
There’s no reason to expect that these two systems
相同的处理视觉信息的机制本应是毫无道理的
would develop the same mechanism for processing visual information.
即便存在相似性 自然选择也不同于
Even with existing similarities, natural selection is very different from using
使用梯度下降的方法改变神经元连接的权值
gradient descent to alter weights of connection between nodes.
但有没有可能 不知怎的
Could it be that, somehow,
有一种理想的物体识别方式
encoded in the fundamental rules of the universe
预编码在了宇宙的基本法则中呢?
there’s this ideal way of doing object recognition? TRINITY:
—崔尼蒂:他在干啥?
-What is he doing? MORPHEUS:
—墨菲斯:他开始相信了
-He’s beginning to believe.
—西拉杰(扮作尼奥):你太对了 墨菲斯
SIRAJ (AS NEO):-You’re damn right, Morpheus.
我们每天都更加了解人工神经网络
We’re getting closer to understanding it every day,
通过研究它 我们会更加了解大脑
and we can learn a lot about the brain including human development,
比如人类发展 治疗认知障碍
treatment for cognitive disabilities, and drug effects,
以及药物作用
from studying artificial neural networks.
装好依赖环境后
After we install our dependencies,
我们将在TensorFlow中重现谷歌Deep Dream代码
we’re gonna replicate Google’s Deep Dream code
然后用一张新图片测试它
in TensorFlow, and then test it out on a novel image.
NumPy会用来进行数学计算
NumPy will be used to perform math calculations.
functools的partical子模块
The’partial’ submodule of functools
让我们可以创建包含一个
lets us create new versions of functions
或多个参数的新版本函数
with one or more arguments filled in.
这有助于反复使用 也就是少写点代码
This is good for reusability, which means less code to write.
Pillow是一个图像库 Image模块能帮我们修改图片
Pillow is an imaging library, and Image will help us modify our images.
TensorFlow是机器学习库
TensorFlow is our machine learning library.
Urllib让我们可以从网页上下载数据
urllib will let us download data from the web.
OS让我们可以调用依赖操作系统的功能
os will let us use operating system-dependent functionality,
ziptools则让我们自由奔跑!
and ziptools [sic] will let us run FREE! No.
不 其实它只是解压用的
It’ll just let us unzip files.
脚本最上面已列出了所有加载包
Our imports are already at the top of our script
我们现在去下载谷歌预先训练好了的神经网络
so we’re going to download Google’s pre-trained neural network,
创建一个TensorFlow会话
create a TensorFlow session,
挑一个神经层和预训练网络来优化输入的图片
pick a layer and a pre-trained network to enhance our input image,
将梯度上升方法反复应用到该层
apply our gradient ascent to that layer repeatedly,
然后输出新生成的Deep Dream图片
and then output our newly Deep Dream’d image.
让我们从在main方法中下载
Let’s start by downloading
谷歌的预训练神经网络Inception开始吧
Google’s pre-trained neural network called”Inception” in our main() method.
在变量url里存个链接
We’ll store a link to it in the’url’ variable,
创建一个存放解压后文件的目录
create a data directory where we will extract it to,
然后用OS模块检索模型名称
then use the os module to retrieve the model name
创建本地压缩包路径
and create a local ZIP file path.
如果路径下是空的
If there is nothing at that path,
可以将url变量作为参数
we can download it using the urllib module
用Urllib模块下载 然后存在model url变量里
with the’url’ variable as a parameter and store it in the’model_url’ variable.
以wb模式打开本地压缩文件
We’ll open our ZIP file with the’wb’ flag
然后我们就可以以二进制写入
so we can write to it in binary,
把下载的数据写进去了
then write the downloaded data to it.
下一步用ZipFile模块解压
Then we’ll extract that using the ZipFile module.
好了现在可以创建TensorFlow会话了
Now we can create our TensorFlow session.
把Inception的图形文件加载到model_fn变量里
We’ll load our Inception graph file into our’model_fn’ variable,
然后用TensorFlow里的
then initialize the graph
Graph()函数初始化图像
using the Graph() function of TensorFlow.
然后用这个图像来初始化一个会话
Now we can initialize a session using that graph.
用FastGFile()函数打开
We’ll open our existing saved Inception graph
已经保存的Inception图像
using the FastGFile() function
然后把它指向已存图像
and point it to the saved graph.
一旦打开它 就可以读取图像
Once we’ve opened it, we can read that graph
并相应地
and parse it accordingly
用TensorFlow图像定义模块的
using the ParseFromString() method
ParseFromString()函数解析它
of TensorFlow’s graph definition module.
我们需要定义输入 所以要用
We need to define our input, so we’ll create
placeholder()方法创建输入张量
an input tensor using the placeholder() method
将它命名为input 32位大小
called’input’ with the size of 32 bits.
然后定义图像网络的像素均值
Then we’ll define image net mean value of pixels
在一个图像里为117
in an image as 117.
从图片中移除该均值有利于
By removing this value from our image, it
学习图像特征 因此
will help us with feature learning, so we
从输入张量中减去该均值
will subtract it from our input tensor
把结果保留在preprocessed变量中
and store the value in our’preprocessed’ variable.
然后加载作为新处理的张量
Then we’ll load the’graph_def’ variable we initialized
初始化过的graph_def变量
as the newly processed tensor.
现在我们已经得到TensorFlow模型了
So now we’ve got our TensorFlow model,
我们已经从网上下载下来
we’ve downloaded from the web, and
并以多图层的形式
we’ve loaded it into our session as
加载到了会话中了
a graph with a bunch of layers.
这是一种卷积神经网络
It’s a convolutional neural network,
一种用来识别图片的神经网络
the type of neural net that helps recognize images.
下面我们把所有层赋值到数组里
Let’s load all those layers into an array
并存储到layers对象中
and store them in our’layers’ object.
对于每一个Tensorflow的运算
So for every TensorFlow operation in our graph,
如果是一个卷积层 就载入数组中
if it’s a convolutional layer, load it into our array.
然后就得到了层层层……[音乐]耶
So we’ve got ♫ layers, lay-layers, lay- ♫ Yeah,
我们正在努力奋斗 嗯我知道了
we’re balling hard right now, I know. OK,
因此每一个卷积层
so each of our convolutional layers
输出成千上万的特征通道来传递图中的数据
output tens of hundreds of feature channels to pass data
然后就可以把它们都收集起来
in the graph, and we can collect them all
存储在feature_num变量中
and store them in the’feature_nums’ variable.
现在先在终端打印出来看看
Let’s print them out and visualize what we’ve got
我们得到了啥
in terminal first.
我们在这可以看到总层数和
We can see our number of layers and the total number
特征通道的总数
of feature channels right here.
现在从我们的模型里
Let’s now pick a layer from our model
取出需要增强的某一层
that we’re going to enhance.
取出低层级的一层后
We’ll pick a lower-level layer and pick one
选一条特征通道看看
of the feature channels to visualize.
现在用Pillow Image子模块的
It’s time to load our input image
open()函数加载待输入图片
using the Pillow Image submodule’s open() method
并存到在image变量里
and store it in our image variable.
我们会用Numpy格式化它
We’ll format it accordingly using NumPy
对其应用“Deep Dream”里的
and perform Deep Dream on it
render_deepdream()函数
with our render_deepdream() function
并把焦点放在先前挑出的层
with a focus on the layer we selected earlier.
在Deep Dream函数里
In our Deep Dream function,
可以看到几个预先定义的超参数
we can see a couple of our predefined hyperparameters.
我们先来定义优化目标
We’ll start by defining our optimization objective,
也就是降低我们输入层的均值
which is to reduce the mean of our input layer.
gradients()函数用于计算
The gradients() function lets us compute
相对于输入张量
the symbolic gradient of our optimized tensor
优化后张量的符号梯度
with respect to our input tensor.
现在把图片分成若干个模糊处理后的图片
Now we can split our image into a number of octaves
用NumPy调整每个图片的大小
and say for each octave, let’s resize it using NumPy
然后加到图片块的数组里
and add it to our array of image octaves.
之后就可以通过每块的迭代
Then we can generate details octave by octave
生成图片细节
by iterating through each.
用一些随机形状
Random shapes are applied to the
通过用calc_grad_tiled()函数多次迭代
image to blur tile boundaries over multiple iterations
来模糊图片像素方块的边界
using the calc_grad_tiled() function.
这里用的本质上就是梯度上升
We’re essentially applying gradient ascent here
来最大化损失函数
to maximize our loss function,
每次迭代它都会将保存在这一层的表征
which merges our saved representation in this layer with our input image
和输入图片合并
more and more every iteration.
所以总结来说
So to break it down,
神经网络是一种学习大脑如何
neural networks are a great test for learning
运作以及反馈刺激的极好的实验
about how the brain functions and responds to certain stimuli.
它们将学到的越来越抽象的表征
They store increasingly abstract representations
保存在各层中
of what they learn in their layers,
这样就可以基于预训练卷积神经网络的任意一层
and we can create trippy images by applying a gradient ascent process to them
使用梯度上升做迷幻图了
based on any chosen layer of a pre-trained convolutional neural network.
上一个视频的编程挑战第一名是
The winner of the coding challenge from the last video
阿维鲁普·查克拉波蒂
is Avhirup Chakhraborty.
他做了一个超酷的IPython笔记本
He created a pretty cool IPython Notebook
来演示他的代码 并且用Keras
to demo his code, and trained his neural net
使用价格和情感数据训练神经网络
on both price and sentiment data using Keras.
本周最佳!
Badass of the week!
第二名是维克托·休拉瑞
And the runner-up is Victor Ciurana.
他的代码组织清晰 统计用户输入
Very well-documented code that asks for user input
来预测股票
on stock prediction.
这次的编程挑战呢
The coding challenge for this video
是优化这份代码
is to modify this code
让它不仅对图片施加魔法
so it works not just on images,
还可以作用于视频 详见README
but video. Details are in the README.
在评论区
Post your GitHub link
留下你们的github地址 我会在下一个视频里公布获胜者
in the comments and I’ll announce the winner in the next video. Also,
另外我建了一个Slack频道
I created a Slack channel
便于我们这些编程魔法师彼此学习
for all of us programming Wizards to learn from each other.
下方是注册链接
The link to sign up below.
现在我要保持清醒 谢谢观看
For now, I’ve got to stay sober, so thanks for watching.

发表评论

译制信息
视频概述

和西拉杰一起对图片施魔法

听录译者

收集自网络

翻译译者

鹿琳

审核员

审核员_MZ

视频来源

https://www.youtube.com/watch?v=MrBzgvUNr4w

相关推荐