Keras 实现一个简单GAN

代码中需提供:

Loss Function  参见Keras 或者 Tensorflow 文档

model_param_matrix   反向调整的模型参数/参数矩阵

epoch 迭代轮数

W 以及调整的方式

import numpy as np
from keras.preprocessing import image
from keras.applications import inception_v3
from keras import backend as K
from PIL import Image
import tensorflow as tf #Prepare the input
# Load the image
img = image.load_img("name.png", target_size=(299, 299))
original_image = image.img_to_array(img) # Scale the image so all pixel intensities are between [-1, 1] as the model expects
original_image /= 255.
original_image -= 0.5
original_image *= 2. # Add a 4th dimension for batch size (as Keras expects)
original_image = np.expand_dims(original_image, axis=0) # Create a copy of the input image to process
processed_image = np.copy(original_image) # How much to update the hacked image in each iteration
learning_rate = 0.01 # Define the cost function.
cost_function = #Loss Function# # We'll ask Keras to calculate the gradient based on the input image and the currently predicted class
#BP
gradient_function = K.gradients(cost_function, model_param_matrix)[0] # Create a Keras function that we can call to calculate the current cost and gradient
grab_cost_and_gradients_from_model = K.function([model_input_layer, K.learning_phase()],
[cost_function, gradient_function]) cost = 0.0 epoch = 1000
for iter in range(epoch):
# Check how close the image is to our target class and grab the gradients we
# can use to push it one more step in that direction.
# Note: It's really important to pass in '0' for the Keras learning mode here!
# Keras layers behave differently in prediction vs. train modes! cost, gradients = grab_cost_and_gradients_from_model([processed_image, 0]) # Adjust the params according to gradients (GD)
W -= gradients * learning_rate print("Model's predicted likelihood that the image is a XXX: {:.8}%".format(cost * 100))

  

Keras 实现一个简单GAN的更多相关文章

  1. 【小白学PyTorch】15 TF2实现一个简单的服装分类任务

    [新闻]:机器学习炼丹术的粉丝的人工智能交流群已经建立,目前有目标检测.医学图像.时间序列等多个目标为技术学习的分群和水群唠嗑的总群,欢迎大家加炼丹兄为好友,加入炼丹协会.微信:cyx64501661 ...

  2. 哪种缓存效果高?开源一个简单的缓存组件j2cache

    背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...

  3. 在Openfire上弄一个简单的推送系统

    推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...

  4. ASP.NET Aries 入门开发教程2:配置出一个简单的列表页面

    前言: 朋友们都期待我稳定地工作,但创业公司若要躺下,也非意念可控. 若人生注定了风雨飘摇,那就雨中前行了. 最机开始看聊新的工作机会,欢迎推荐,创业公司也可! 同时,趁着自由时间,抓紧把这系列教程给 ...

  5. 计算机程序的思维逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库

    57节介绍了字节流, 58节介绍了字符流,它们都是以流的方式读写文件,流的方式有几个限制: 要么读,要么写,不能同时读和写 不能随机读写,只能从头读到尾,且不能重复读,虽然通过缓冲可以实现部分重读,但 ...

  6. 如何开发一个简单的HTML5 Canvas 小游戏

    原文:How to make a simple HTML5 Canvas game 想要快速上手HTML5 Canvas小游戏开发?下面通过一个例子来进行手把手教学.(如果你怀疑我的资历, A Wiz ...

  7. CSharpGL(24)用ComputeShader实现一个简单的图像边缘检测功能

    CSharpGL(24)用ComputeShader实现一个简单的图像边缘检测功能 效果图 这是红宝书里的例子,在这个例子中,下述功能全部登场,因此这个例子可作为使用Compute Shader的典型 ...

  8. CSharpGL(23)用ComputeShader实现一个简单的ParticleSimulator

    CSharpGL(23)用ComputeShader实现一个简单的ParticleSimulator 我还没有用过Compute Shader,所以现在把红宝书里的例子拿来了,加入CSharpGL中. ...

  9. 应用OpenMP的一个简单的设计模式

    小喵的唠叨话:最近很久没写博客了,一是因为之前写的LSoftmax后馈一直没有成功,所以在等作者的源码.二是最近没什么想写的东西.前两天,在预处理图片的时候,发现处理200w张图片,跑了一晚上也才处理 ...

随机推荐

  1. Debug outlook add-in (office.js) 小技巧

    这几天在使用office.js 做outlook add-in的时候出现了一个问题: 不知道运行时去调试. 这里给大家介绍两个调试add-in 的方法. office365 其他软件 add-ins ...

  2. Python __name__变量

    原文: http://blog.csdn.net/u011511601/article/details/53504355 Python使用缩进对齐组织代码的执行,所有没有缩进的代码,都会在载入时自动执 ...

  3. react-redux 知识点

    React-Redux 使用 如果只使用redux,那么流程是这样的: component --> dispatch(action) --> reducer --> subscrib ...

  4. [c/c++]fopen用法及参数介绍

    函数定义: FILE * fopen ( const char * filename, const char * mode ); 参数mode: "r" read: 为输入操作打开 ...

  5. pandas读取MySql/SqlServer数据 (转)

    在 Anacondas环境中,conda install pymssql ,一直报包冲突,所以采用先在 https://www.lfd.uci.edu/~gohlke/pythonlibs/#nump ...

  6. jmeter-场景-上传文件-send-a-file

    jmeter --上传文件 jmeter-场景-上传文件-send-a-file 简要说就3点: POST请求 Request的参数都写在路径内,不写在表单里 上传的文件写在表单里 只要记住以上3点, ...

  7. ML平台_饿了么实践

    (转载至:https://zhuanlan.zhihu.com/p/28592540) 说到机器学习.大数据,大家听到的是 Hadoop 和 Spark 居多,它们跟 TensorFlow 是一个什么 ...

  8. WPF实现打印用户界面功能

    方式一:public bool Print(string pathStr) { try { if (File.Exists(pathStr) == false) return false; var p ...

  9. CheckFail设计很垃圾

        function checkFail(node, onError, fuckIE) {         var id = node.src;//检测是否死链         node.onlo ...

  10. 树莓派Raspberry Pi zero w无线联网实测

    第一次学习树莓派,使用的是Raspberry Pi zero w的型号. 刚开始,就只有一块板子!!!这要怎么开发啊 经过网上查阅资料,发现可以通过WiFi连接PC端实现开发测试 准备材料:一根USB ...