Deep Learning & Art: Neural Style Transfer(吴恩达课程)

# UNQ_C1
# GRADED FUNCTION: compute_content_costdef compute_content_cost(content_output, generated_output):"""Computes the content costArguments:a_C -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing content of the image C a_G -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing content of the image GReturns: J_content -- scalar that you compute using equation 1 above."""a_C = content_output[-1]a_G = generated_output[-1]### START CODE HERE# Retrieve dimensions from a_G (≈1 line)m, n_H, n_W, n_C = a_G.get_shape().as_list()# Reshape a_C and a_G (≈2 lines)a_C_unrolled = tf.reshape(a_C, (m, n_H * n_W, n_C))a_G_unrolled = tf.reshape(a_C, (m, -1, n_C))# compute the cost with tensorflow (≈1 line)J_content =  tf.reduce_sum((a_C-a_G)**2)/(4*n_H*n_W*n_C)### END CODE HEREreturn J_content
# UNQ_C2
# GRADED FUNCTION: gram_matrixdef gram_matrix(A):"""Argument:A -- matrix of shape (n_C, n_H*n_W)Returns:GA -- Gram matrix of A, of shape (n_C, n_C)"""  ### START CODE HERE#(≈1 line)GA = A @ tf.transpose(A)### END CODE HEREreturn GA
# UNQ_C3
# GRADED FUNCTION: compute_layer_style_costdef compute_layer_style_cost(a_S, a_G):"""Arguments:a_S -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing style of the image S a_G -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing style of the image GReturns: J_style_layer -- tensor representing a scalar value, style cost defined above by equation (2)"""### START CODE HERE# Retrieve dimensions from a_G (≈1 line)m, n_H, n_W, n_C = a_G.get_shape().as_list()# Reshape the images to have them of shape (n_C, n_H*n_W) (≈2 lines)a_S = tf.transpose(tf.reshape(a_S,[n_H*n_W,n_C]))a_G = tf.transpose(tf.reshape(a_G,[n_H*n_W,n_C]))# Computing gram_matrices for both images S and G (≈2 lines)GS = gram_matrix(a_S)GG = gram_matrix(a_G)# Computing the loss (≈1 line)J_style_layer =(tf.reduce_sum((GS-GG)**2)) / (4*(n_C**2)*((n_H*n_W)**2))### END CODE HEREreturn J_style_layer
def compute_style_cost(style_image_output, generated_image_output, STYLE_LAYERS=STYLE_LAYERS):"""Computes the overall style cost from several chosen layersArguments:style_image_output -- our tensorflow modelgenerated_image_output --STYLE_LAYERS -- A python list containing:- the names of the layers we would like to extract style from- a coefficient for each of themReturns: J_style -- tensor representing a scalar value, style cost defined above by equation (2)"""# initialize the overall style costJ_style = 0# Set a_S to be the hidden layer activation from the layer we have selected.# The last element of the array contains the content layer image, which must not to be used.a_S = style_image_output[:-1]# Set a_G to be the output of the choosen hidden layers.# The last element of the array contains the content layer image, which must not to be used.a_G = generated_image_output[:-1]for i, weight in zip(range(len(a_S)), STYLE_LAYERS):  # Compute style_cost for the current layerJ_style_layer = compute_layer_style_cost(a_S[i], a_G[i])# Add weight * J_style_layer of this layer to overall style costJ_style += weight[1] * J_style_layerreturn J_style
# UNQ_C4
# GRADED FUNCTION: total_cost
@tf.function()
def total_cost(J_content, J_style, alpha = 10, beta = 40):"""Computes the total cost functionArguments:J_content -- content cost coded aboveJ_style -- style cost coded abovealpha -- hyperparameter weighting the importance of the content costbeta -- hyperparameter weighting the importance of the style costReturns:J -- total cost as defined by the formula above."""### START CODE HERE#(≈1 line)J = alpha*J_content+beta*J_style### START CODE HEREreturn J
# UNQ_C5
# GRADED FUNCTION: train_stepoptimizer = tf.keras.optimizers.Adam(learning_rate=0.01)@tf.function()
def train_step(generated_image):with tf.GradientTape() as tape:# In this function you must use the precomputed encoded images a_S and a_C# Compute a_G as the vgg_model_outputs for the current generated image### START CODE HERE#(1 line)a_G = vgg_model_outputs(generated_image)# Compute the style cost#(1 line)J_style = compute_style_cost(a_S, a_G)#(2 lines)# Compute the content costJ_content = compute_content_cost(a_C, a_G)# Compute the total costJ = total_cost(J_content, J_style)### END CODE HEREgrad = tape.gradient(J, generated_image)optimizer.apply_gradients([(grad, generated_image)])generated_image.assign(clip_0_1(generated_image))# For grading purposesreturn J

Deep Learning Art: Neural Style Transfer(吴恩达课程)相关推荐

  1. 15.深度学习练习:Deep Learning Art: Neural Style Transfer

    本文节选自吴恩达老师<深度学习专项课程>编程作业,在此表示感谢. 课程链接:https://www.deeplearning.ai/deep-learning-specialization ...

  2. Transfer Learning with MobileNetV2(吴恩达课程)

    Transfer Learning with MobileNetV2(吴恩达课程) # UNQ_C1 # GRADED FUNCTION: data_augmenter def data_augmen ...

  3. Ex6_机器学习_吴恩达课程作业(Python):SVM支持向量机(Support Vector Machines)

    Ex6_机器学习_吴恩达课程作业(Python):SVM支持向量机(Support Vector Machines) 文章目录 Ex6_机器学习_吴恩达课程作业(Python):SVM支持向量机(Su ...

  4. Residual Networks(吴恩达课程)

    Residual Networks(吴恩达课程) # UNQ_C1 # GRADED FUNCTION: identity_blockdef identity_block(X, f, filters, ...

  5. Image Segmentation with U-Net(吴恩达课程)

    Image Segmentation with U-Net(吴恩达课程) # UNQ_C1 # GRADED FUNCTION: conv_block def conv_block(inputs=No ...

  6. Autonomous Driving - Car Detection(吴恩达课程)

    Autonomous Driving - Car Detection(吴恩达课程) # UNQ_C1 (UNIQUE CELL IDENTIFIER, DO NOT EDIT) # GRADED FU ...

  7. 干货|机器学习零基础?不要怕,吴恩达课程笔记第三周!逻辑回归与正则

    吴恩达Coursera机器学习课系列笔记 课程笔记|吴恩达Coursera机器学习 Week1 笔记-机器学习基础 干货|机器学习零基础?不要怕,吴恩达机器学习课程笔记2-多元线性回归 1 Logis ...

  8. 机器学习吴恩达课程总结(一)

    文章目录 1. 第一章 简介 1.1 机器学习(Machine Learning) 1.2 有监督学习(Supervised Learning) 1.3 无监督学习(Unsupervised Lear ...

  9. 吴恩达课程翻译_中文学习资源:斯坦福大学CS231n计算机视觉课程

    hi,我是为你们的xio习操碎了心的和鲸社区男运营 我们的网站:和鲸社区 Kesci.co 我们的公众号:和鲸社区(ID:heywhale-kesci) 有干货,来! 大家好,此次本鲸给大家翻译的项目 ...

最新文章

  1. 2022-2028年中国蛋制品行业市场专项调查及前瞻分析报告
  2. 密码学研究-数字签名
  3. jQuery-EasyUI异步加载树形菜单
  4. 在MSBuild.exe中使用条件编译(Conditional Compile)
  5. MySQL 配置文件优化
  6. c++11 智能指针 unique_ptr、shared_ptr与weak_ptr
  7. 升级到ubuntu9.10Alpha5的艰苦路程
  8. modelsim10.1a安装破解说明
  9. 余姚中考能用计算机吗,2018年余姚中考政策有大变化,2020年取消奖励加分项目...
  10. Java工程师学习指南 中级篇
  11. quatus ii 常见错误及其改正方法
  12. python线程池原理_Django异步任务线程池实现原理
  13. 读大道至简第四章有感
  14. ElementUI中前端分页的实现
  15. 近年来的Java面试题汇总。帮你圆大厂梦。
  16. Exp外贸/出口英文商城系统在国际电商贸易中的角色扮演
  17. 实现Pomodoro计时器的Vue组件
  18. java中flush()的作用的是什么?与close有什么联系
  19. 第十二届Revit开发训练营4月4日~9日在武汉举办
  20. 一文带你吃透操作系统

热门文章

  1. A40i使用笔记:文件IO驱动方式(慢)(无法应用于大部分ic)
  2. C语言程序死循环问题解析——变量被修改
  3. 生活随记-知识的价值
  4. 资料搜集-JAVA系统的梳理知识9-高级计算机网络
  5. 滴滴低调推广重疾险,为何TMD都对金融兴趣浓厚?
  6. 探索创客教育在线管理实施体系
  7. 2020年工具钳工(高级)新版试题及工具钳工(高级)模拟考试题库
  8. 闲聊程序员的那些事儿
  9. u盘固定盘符_winpe系统下固定U盘盘符的方法
  10. STL的rotate函数分析