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. 阿里Dragonfly docker p2p 镜像分发试用

      阿里的Dragonfly p2p 镜像分发已经开源了,同时加入了cncf ,很给力 拉取镜像 docker pull registry.cn-hangzhou.aliyuncs.com/alidr ...

  2. day8 大纲

    01 昨日内容回顾 文件操作 文件操作的流程: 1,打开文件创建文件句柄. 2,对文件句柄进行操作. 3,关闭文件句柄. 读, r r+ rb r+b read() 全部读取 read(n) 读取一部 ...

  3. 启用 webpack 的模块热替换特性

    启用 webpack 的模块热替换特性: module.exports = { //... devServer: { hot: true } } 注意,必须有 webpack.HotModuleRep ...

  4. thinkphp5 列表页数据分页查询-带搜索条件

    一.控制器部分 <?php namespace app\user\controller; use app\user\model\HelpCenterManual as HelpCenterMan ...

  5. 四、springboot(一)入门

    1.创建maven工程 2.修改jdk版本为1.8以上 3.项目结构如下 4.添加依赖jar包 <parent> <groupId>org.springframework.bo ...

  6. Spark代码Eclipse远程调试

    我们在编写Spark Application或者是阅读源码的时候,我们很想知道代码的运行情况,比如参数设置的是否正确等等.用Logging方式来调试是一个可以选择的方式,但是,logging方式调试代 ...

  7. spark-streaming-kafka-0-8 和 0-10的使用区别

    一.spark-streaming-kafka-0-8_2.11-2.0.2.jar 1.pom.xml <!-- https://mvnrepository.com/artifact/org. ...

  8. FLIR ONE PRO热成像仪

    FLIR ONE PRO热成像仪 https://www.chiphell.com/thread-1774218-1-1.html

  9. Winfrom DataGridView中使用Tooltip

    第一步:添加DataGridView.Tooltip控件,略 第二步:设置ToolTip 相关属性,略,参考下图 第三步:DataGridView 添加 CellMouseEnter.CellMous ...

  10. php大量数据 10M数据从查询到下载 【内存溢出,查询过慢】解决方案

    功能描述:做数据导出 功能分析:1.采用csv的格式,因为csv的格式比excel小 2. 3W条数据,100个字段需要全部导出 开始 直接查询 //此处使用的laravel框架,具体含义一看就懂 t ...