吴恩达deeplearning Lesson4 Week3
写在前面:
在Coursera相关练习的discuss中受益良多!以后在编程练习时遇到问题一定第一时间去讨论区进行搜索,解决了我不少疑虑的问题。
1、可以直接搜索与expect不相关的结果,如果你犯的错误足够经典,则会有很多人都会碰到你的问题并能给予回答。比如在一个函数
2、也可以搜索问题的关键词。
比如Does the optimizer update weights as well? 你可以直接搜索optimizer
Art+Generation+with+Neural+Style+Transfer
论文分析:https://blog.csdn.net/shenxiaolu1984/article/details/52090012
在初始照片上增加噪声,与目标风格图片进行比较。
(选取经过VGG网络的同一深度的卷积层的输出,将其与其的转置相乘得到G矩阵。两张图片的G矩阵的范数误差即为cost,反向传播改变图片像素值)。
我的理解是固化神经网络参数,通过改变输入噪声图片的像素值来减少误差J。
遇到的困惑1 优化器会不会同时优化网络的weight?
答,不会。load模型的时候,模型的参数都是constant,无法被训练改变。
只有input层给的变量是varible,可以对其进行优化。
论坛讲解见:https://www.coursera.org/learn/convolutional-neural-networks/programming/owWbQ/art-generation-with-neural-style-transfer/discussions/threads/50Lv2tBkEeexuwrR6wbv6A
遇到的困惑2 为啥需要在计算cost的时候要加转置?
答:必须的。
输入形状[1, n_H,n_W,n_C] 期望输出形状[n_C,n_H*n_W]
如果想要结果正确,必须先转置成[1,n_C,n_H,n_W] or [1,n_C,n_W,n_H] 才能根据此输入进行正确reshape再输出
对tensor进行sess.run 与否的结果
前面代码省略。
# Assign the content image to be the input of the VGG model.
sess.run(model['input'].assign(content_image))
# Select the output tensor of layer conv4_2
out = model['conv4_2']
# Set a_C to be the hidden layer activation from the layer we have selected
a_C = sess.run(out)
# Set a_G to be the hidden layer activation from same layer. Here, a_G references model['conv4_2']
# and isn't evaluated yet. Later in the code, we'll assign the image G as the model input, so that
# when we run the session, this will be the activations drawn from the appropriate layer, with G as input.
a_G = out #这里只是把张量位置设为同一层,并不是数,只是个张量
print(a_G)
# Compute the content cost
J_content = compute_content_cost(a_C, a_G)
Tensor(“Relu_9:0”, shape=(1, 38, 50, 512), dtype=float32)
这里的a_C是具体的算出来的矩阵值,a_G是图里未计算的Tensor。
这里不好理解是体现在out是model[‘conv4_2’]的输出,而a_C的sess中前面代码已经指定输入了(图片),所以sess.run可以根据模型算出结果。a_G还没有指定所在的sess(将在下面指定,现在还没定义),所以是个tensor。
sess.run(model[‘input’].assign(style_image))
作用是将style_image作为input层的输入。
结果
输入原图:
风格图:
结果:
Face+Recognition+for+the+Happy+House
人脸识别思路
一次训练需要三张图片,两张人1一张非人1,让网络输出一个128维的向量,两个人1为A、P,非人1为N,f代表网络的前向传播结果,改网络的cost如下:
使同类样本embedding后的向量相减的结果 小于 异类向量相减的结果
为了防止出现全零的情况,需要加一个alpha。
J
=
∑
i
=
1
m
[
∣
∣
f
(
A
(
i
)
)
−
f
(
P
(
i
)
)
∣
∣
2
2
⎵
(1)
−
∣
∣
f
(
A
(
i
)
)
−
f
(
N
(
i
)
)
∣
∣
2
2
⎵
(2)
+
α
]
\mathcal{J} = \sum^{m}_{i=1} \large[ \small \underbrace{\mid \mid f(A^{(i)}) - f(P^{(i)}) \mid \mid_2^2}_\text{(1)} - \underbrace{\mid \mid f(A^{(i)}) - f(N^{(i)}) \mid \mid_2^2}_\text{(2)} + \alpha \large ] \small
J=i=1∑m[(1)
∣∣f(A(i))−f(P(i))∣∣22−(2)
∣∣f(A(i))−f(N(i))∣∣22+α]
然后做一个max操作,小于0就不关心,大于0就算进cost值。目的是训练参数让cost减少。
.
import cv2出错
没有cv2这个模块。
- 解决方法:conda install opencv (不成功)
今天发现清华的conda源 关闭了,难过,conda这么好用可是以后只能50kb/s下载了。。
https://zhuanlan.zhihu.com/p/64766956
- 直接pip 解决问题
pip install opencv-python
tf.reduce_sum
basic_loss = tf.reduce_sum(tf.subtract(pos_dist , neg_dist),axis=-1)+alpha #这里也要相加 但是注意加上axis=-1 保持形状
这里需要加上axis=-1,来使tf.reduce_sum在最后一维相加。
如果不加,就会返回1个数,就是整个tensor所有值相加。
Python 字典(Dictionary) items()方法
for (name, db_enc) in database.items():
Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组
https://www.runoob.com/python/att-dictionary-items.html
np.linalg.norm 正则化
https://blog.csdn.net/hqh131360239/article/details/79061535
默认就是求二范数
代码:
for (name, db_enc) in database.items():
# Compute L2 distance between the target "encoding" and the current "emb" from the database. (≈ 1 line)
dist = np.linalg.norm(encoding-database[name])
# If this distance is less than the min_dist, then set min_dist to dist, and identity to name. (≈ 3 lines)
if dist < min_dist:
min_dist = dist
identity = name
其他
1、这个v3版本的作业ipynb,出现了一些问题,出现了正确结果与expect结果不符的情况。
在verify函数中:
2、出现了离线作业fr_utils.py报错的问题
Total Params: 3743280
Traceback (most recent call last):
File "C:/Users/51530/PycharmProjects/DL/wuenda/Face/faceV3.py", line 60, in
load_weights_from_FaceNet(FRmodel)
File "C:\Users\51530\PycharmProjects\DL\wuenda\Face\fr_utils.py", line 133, in load_weights_from_FaceNet
weights_dict = load_weights()
File "C:\Users\51530\PycharmProjects\DL\wuenda\Face\fr_utils.py", line 154, in load_weights
conv_w = genfromtxt(paths[name + '_w'], delimiter=',', dtype=None)
File "E:\anaconda\lib\site-packages\numpy\lib\npyio.py", line 1867, in genfromtxt
raise ValueError(errmsg)
ValueError: Some errors were detected !
Line #7 (got 2 columns instead of 1)
Line #12 (got 3 columns instead of 1)
Line #15 (got 2 columns instead of 1)
解决方法:weights文件夹里的csv内容不对,去github上下载对的文件
https://github.com/Skuldur/facenet-face-recognition