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张图片,跑了一晚上也才处理 ...
随机推荐
- script标签的同步和异步
1).把<script>标签放在<head>中意味着必须等到全部的js代码都下载解析和执行完成以后,才开始展现页面内容,为避免这个问题一般把js代码全部放在<body&g ...
- python中in,not in,比较运算符,格式化输出,编码
一,python中的in,和not in python中in的作用是检测或查找,例如: c = ‘你好大号胡覅但是啊飞碟说’ b = ‘你好’ print(b in c ) 结果: True c = ...
- day01 计算机的基础知识
1.编程语言: 人与计算机沟通的表达方式. 2.编程: 程序员用计算机能理解的表达方式(编程语言)把程序员想让计算机实现的功能写到文件里,这些文件称之为程序. 3.计算机硬件组成: 控制器:是计算机的 ...
- oracle之 AWR固定基线
前言:可以创建AWR基线来为数据库建立已保存的工作负载视图,以便以后用来与其他AWR快照进行比较. 1. 手工创建 AWR 固定基线(固定基线如果没有设置过期时间,会永久保存) -- 根据时间创建DB ...
- vue-cli 2.x 项目优化之:引入本地静态库文件
demo地址:https://github.com/cag2050/vue_cli_optimize_static_resource vue-cli 将静态资源文件放到 static 文件夹下并引用: ...
- linux配置 ssh 免密 登录
1:第一步给每一台电脑生成公私钥 使用 在 root 目录下面 使用 ssh-keygen -t rsa ,然后不停敲回车.就可以生成公私钥.默认放在 .ssh 目录下面. 2:进入.ssh ...
- BeanShell用法(摘抄至网络)
说明:本文部分资料摘抄至 来源: http://www.cnblogs.com/puresoul/p/4915350.html 来源: http://www.cnblogs.com/puresoul/ ...
- 深入解析 ext2 文件系统 (转)
http://blog.chinaunix.net/uid-24774106-id-3266816.html 很久以来,就想写一篇关于ext 家族文件系统的文章,源于我刚工作的时候,曾经一不小心rm ...
- 亚马逊 AWS ip反向解析:Configurable Reverse DNS for Amazon EC2’s Elastic IP Addresses
I’d like to call your attention to a new feature that we rolled out earlier this month. You can now ...
- SD-SDI播出系统---使用GTX TX产生恢复时钟
SD-SDI播出系统---使用GTX TX产生恢复时钟 1. 2.SD-SDI恢复时钟的生成