Keras 实现一个简单GAN
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的更多相关文章
- 【小白学PyTorch】15 TF2实现一个简单的服装分类任务
[新闻]:机器学习炼丹术的粉丝的人工智能交流群已经建立,目前有目标检测.医学图像.时间序列等多个目标为技术学习的分群和水群唠嗑的总群,欢迎大家加炼丹兄为好友,加入炼丹协会.微信:cyx64501661 ...
- 哪种缓存效果高?开源一个简单的缓存组件j2cache
背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...
- 在Openfire上弄一个简单的推送系统
推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...
- ASP.NET Aries 入门开发教程2:配置出一个简单的列表页面
前言: 朋友们都期待我稳定地工作,但创业公司若要躺下,也非意念可控. 若人生注定了风雨飘摇,那就雨中前行了. 最机开始看聊新的工作机会,欢迎推荐,创业公司也可! 同时,趁着自由时间,抓紧把这系列教程给 ...
- 计算机程序的思维逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库
57节介绍了字节流, 58节介绍了字符流,它们都是以流的方式读写文件,流的方式有几个限制: 要么读,要么写,不能同时读和写 不能随机读写,只能从头读到尾,且不能重复读,虽然通过缓冲可以实现部分重读,但 ...
- 如何开发一个简单的HTML5 Canvas 小游戏
原文:How to make a simple HTML5 Canvas game 想要快速上手HTML5 Canvas小游戏开发?下面通过一个例子来进行手把手教学.(如果你怀疑我的资历, A Wiz ...
- CSharpGL(24)用ComputeShader实现一个简单的图像边缘检测功能
CSharpGL(24)用ComputeShader实现一个简单的图像边缘检测功能 效果图 这是红宝书里的例子,在这个例子中,下述功能全部登场,因此这个例子可作为使用Compute Shader的典型 ...
- CSharpGL(23)用ComputeShader实现一个简单的ParticleSimulator
CSharpGL(23)用ComputeShader实现一个简单的ParticleSimulator 我还没有用过Compute Shader,所以现在把红宝书里的例子拿来了,加入CSharpGL中. ...
- 应用OpenMP的一个简单的设计模式
小喵的唠叨话:最近很久没写博客了,一是因为之前写的LSoftmax后馈一直没有成功,所以在等作者的源码.二是最近没什么想写的东西.前两天,在预处理图片的时候,发现处理200w张图片,跑了一晚上也才处理 ...
随机推荐
- sudo command
sudo -i : login as sudo password: change the password of current login user exit : logout
- :first :first-child .first()和.get() .eq()
:first .first()只匹配一个元素,而 :first-child 将为每个父元素匹配一个子元素 .get()得到的是dom 元素 $('li').get()没有参数返回一个数组 .eq() ...
- HI3516EV100 RTMP添加音频
1.先试试只发音频 失败 2.保存音频,发送视频,成功
- AIX系统日志
1.系统错误日志 存放路径:/var/adm/ras/errlog 说明:该日志记录了系统所检测到的软硬件故障和错误,尤其对系统的硬件故障有很大的参考价值,是AIX提供的最有价值的日志之一, errl ...
- java远程调用linux的命令或者脚本
转载自:http://eksliang.iteye.com/blog/2105862 Java通过SSH2协议执行远程Shell脚本(ganymed-ssh2-build210.jar) 使用步骤如下 ...
- Kafka研究【一】:bring up环境
kafka是干什么的,有和特性,我这里就不多说,详情自己研究官方文档. 0. 背景介绍 我需要在三台机器上分别部署kafka broker的实例,构建成一个集群.kafka的broker集群,是基于z ...
- maven添加自定义jar
mvn install:install-file -Dfile=自定义jar文件路径 -DgroupId=所属组 -DartifactId=项目名 -Dversion=版本 -Dpackaging=j ...
- KC705开发板关于MIG的配置
KC705开发板关于MIG的配置
- WPF Demo12 布局
1 <Window x:Class="布局.DockPanel1" xmlns="http://schemas.microsoft.com/winfx/2006/x ...
- 黄聪:微信公众平台开发OAuth2.0网页授权(转)
微信公众平台开发 OAuth2.0网页授权认证 网页授权获取用户基本信息 作者:方倍工作室 微信公众平台最近新推出微信认证,认证后可以获得高级接口权限,其中一个是OAuth2.0网页授权,很多朋友在使 ...