当前位置: 首页 > news >正文

监控做斗鱼直播网站百度网站公司信息推广怎么做的

监控做斗鱼直播网站,百度网站公司信息推广怎么做的,wordpress 主题编写,私人可以买服务器吗遗传算法与深度学习实战——使用进化策略实现EvoLisa 0. 前言1. 使用进化策略实现 EvoLisa2. 运行结果相关链接 0. 前言 我们已经学习了进化策略 (Evolutionary Strategies, ES) 的基本原理,并且尝试使用 ES 解决了函数逼近问题。函数逼近是一个很好的基准问题&…

遗传算法与深度学习实战——使用进化策略实现EvoLisa

    • 0. 前言
    • 1. 使用进化策略实现 EvoLisa
    • 2. 运行结果
    • 相关链接

0. 前言

我们已经学习了进化策略 (Evolutionary Strategies, ES) 的基本原理,并且尝试使用 ES 解决了函数逼近问题。函数逼近是一个很好的基准问题,但为了充分展示 ES 的作用,本节中,我们将重新思考 EvoLisa 问题,采用 ES 作为解决策略,以将 ES 和常规遗传算法进行对比。

1. 使用进化策略实现 EvoLisa

接下来,使用进化策略 (Evolutionary Strategies, ES) 通过复现 EvoLisa 项目重建《蒙娜丽莎》图像。

import random
import numpy as npfrom deap import algorithms
from deap import base
from deap import creator
from deap import toolsimport os
import cv2
import urllib.request
import matplotlib.pyplot as plt
from IPython.display import clear_outputdef load_target_image(image_url, color=True, size=None):image_path = "target_image"    urllib.request.urlretrieve(image_url,image_path)if color:target = cv2.imread(image_path, cv2.IMREAD_COLOR)# Switch from bgr to rgbtarget = cv2.cvtColor(target, cv2.COLOR_BGR2RGB)else:target = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)if size:# Only resizes image if it is needed!target = cv2.resize(src=target, dsize=size, interpolation=cv2.INTER_AREA)return targetdef show_image(img_arr):    plt.figure(figsize=(10,10))plt.axis("off")plt.imshow(img_arr/255)plt.show()def show_results(history, img_arr, org):plt.figure(figsize=(10,10))plt.tight_layout()plt.subplot(221)plt.axis("off")plt.imshow(img_arr/255)plt.title('best of generation')plt.subplot(222)plt.axis("off")plt.imshow(org/255)plt.title('target image')plt.subplot(212)lh = len(history)plt.xlim([lh-50, lh])plt.plot(history)plt.title('min fitness by generation') plt.show()polygons = 255 #@param {type:"slider", min:10, max:1000, step:1}
size = 32 #@param {type:"slider", min:16, max:1000, step:2}
target_image = "Mona Lisa" #@param ["Mona Lisa", "Stop Sign", "Landscape", "Celebrity", "Art", "Abstract"]
report_every_gen = 10 #@param {type:"slider", min:1, max:100, step:1}
number_generations = 10000 #@param {type:"slider", min:100, max:10000, step:10}POLYGONS = polygons
SIZE = (size, size)target_urls = { "Mona Lisa" : 'https://upload.wikimedia.org/wikipedia/commons/b/b7/Mona_Lisa_face_800x800px.jpg',"Stop Sign" : 'https://images.uline.com/is/image//content/dam/images/H/H2500/H-2381.jpg',"Landscape" : 'https://www.adorama.com/alc/wp-content/uploads/2018/11/landscape-photography-tips-yosemite-valley-feature.jpg',"Celebrity" : 'https://s.abcnews.com/images/Entertainment/WireAP_91d6741d1954459f9993bd7a2f62b6bb_16x9_992.jpg',"Art" : "http://www.indianruminations.com/wp-content/uploads/what-is-modern-art-definition-2.jpg","Abstract" : "https://scx2.b-cdn.net/gfx/news/2020/abstractart.jpg"}target_image_url = target_urls[target_image]
target = load_target_image(target_image_url, size=SIZE)
show_image(target)
print(target.shape)#polygon genes
GENE_LENGTH = 10
NUM_GENES = POLYGONS * GENE_LENGTH#create a sample invidiual
individual = np.random.uniform(0,1,NUM_GENES)
print(individual)
# [0.62249533 0.44090963 0.14777921 ... 0.57283261 0.9325435  0.25907929]def extract_genes(genes, length): for i in range(0, len(genes), length): yield genes[i:i + length]def render_individual(individual):if isinstance(individual,list):individual = np.array(individual)canvas = np.zeros(SIZE+(3,))radius_avg = (SIZE[0] + SIZE[1]) / 2 / 6genes = extract_genes(individual, GENE_LENGTH)for gene in genes:try:overlay = canvas.copy()# alternative drawing methods circle or rectangle# circle brush uses a GENE_LENGTH of 7# center = (0, 1) [2]# radius = (2) [3]# color = (3,4,5) [6]# alpha = (6) [7]#cv2.circle(#    overlay,#    center=(int(gene[1] * SIZE[1]), int(gene[0] * SIZE[0])),#    radius=int(gene[2] * radius_avg),#    color=color,#    thickness=-1,#)# rectangle brush uses GENE_LENGTH = 8# top left = (0, 1) [2]# btm right = (2, 3) [4]# color = (4, 5, 6) [7]# alpha = (7) [8]#cv2.rectangle(overlay, (x1, y1), (x2, y2), color, -1)    # polyline brush uses GENE_LENGTH = 10# pts = (0, 1), (2, 3), (4, 5) [6]      # color = (6, 7, 8) [9]# alpha = (9) [10]x1 = int(gene[0] * SIZE[0])x2 = int(gene[2] * SIZE[0])x3 = int(gene[4] * SIZE[0])y1 = int(gene[1] * SIZE[1])y2 = int(gene[3] * SIZE[1])y3 = int(gene[5] * SIZE[1])color = (gene[6:-1] * 255).astype(int).tolist() pts = np.array([[x1,y1],[x2,y2],[x3,y3]], np.int32)  pts = pts.reshape((-1, 1, 2))pts = np.array([[x1,y1],[x2,y2],[x3,y3]])cv2.fillPoly(overlay, [pts], color)alpha = gene[-1]canvas = cv2.addWeighted(overlay, alpha, canvas, 1 - alpha, 0)  except:passreturn canvasrender = render_individual(individual)
show_image(render)from skimage.metrics import structural_similarity as ss
#@title Fitness Function
def fitness_mse(render):"""Calculates Mean Square Error Fitness for a render"""error = (np.square(render - target)).mean(axis=None)return errordef fitness_ss(render):"""Calculated Structural Similiarity Fitness"""index = ss(render, target, multichannel=True)return 1-indexprint(fitness_mse(render))IND_SIZE = NUM_GENES
MIN_VALUE = -1
MAX_VALUE = 1
MIN_STRATEGY = 0.5
MAX_STRATEGY = 5CXPB = .6
MUTPB = .3creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, typecode="d", fitness=creator.FitnessMin, strategy=None)
creator.create("Strategy", list, typecode="d")def generateES(icls, scls, size, imin, imax, smin, smax):  ind = icls(random.uniform(imin, imax) for _ in range(size))  ind.strategy = scls(random.uniform(smin, smax) for _ in range(size))  return inddef checkStrategy(minstrategy):def decorator(func):def wrappper(*args, **kargs):children = func(*args, **kargs)for child in children:for i, s in enumerate(child.strategy):if s < minstrategy:child.strategy[i] = minstrategyreturn childrenreturn wrappper
return decoratordef uniform(low, up, size=None):try:return [random.uniform(a, b) for a, b in zip(low, up)]except TypeError:return [random.uniform(a, b) for a, b in zip([low] * size, [up] * size)]def clamp(low, up, n):return max(low, min(n, up))def custom_blend(ind1, ind2, alpha):    for i, (x1, s1, x2, s2) in enumerate(zip(ind1, ind1.strategy,ind2, ind2.strategy)):# Blend the valuesgamma = (1. + 2. * alpha) * random.random() - alphaind1[i] = clamp(0.0, 1.0, (1. - gamma) * x1 + gamma * x2)ind2[i] = clamp(0.0, 1.0, gamma * x1 + (1. - gamma) * x2)# Blend the strategiesgamma = (1. + 2. * alpha) * random.random() - alphaind1.strategy[i] = (1. - gamma) * s1 + gamma * s2ind2.strategy[i] = gamma * s1 + (1. - gamma) * s2return ind1, ind2toolbox = base.Toolbox()
toolbox.register("individual", generateES, creator.Individual, creator.Strategy,IND_SIZE, MIN_VALUE, MAX_VALUE, MIN_STRATEGY, MAX_STRATEGY)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("mate", custom_blend, alpha=0.5)
toolbox.register("mutate", tools.mutESLogNormal, c=1.0, indpb=0.06)
toolbox.register("select", tools.selTournament, tournsize=5)toolbox.decorate("mate", checkStrategy(MIN_STRATEGY))
toolbox.decorate("mutate", checkStrategy(MIN_STRATEGY))def evaluate(individual):render = render_individual(individual)print('.', end='')
return fitness_mse(render),  #using MSE for fitness#toolbox.register("mutate", tools.mutGaussian, mu=0.0, sigma=.1, indpb=.25)
toolbox.register("evaluate", evaluate)NGEN = number_generations
RGEN = report_every_gen
CXPB = .6
MUTPB = .3
MU, LAMBDA = 100, 250
pop = toolbox.population(n=MU)
hof = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean)
stats.register("std", np.std)
stats.register("min", np.min)
stats.register("max", np.max) best = None
history = []for g in range(NGEN):pop, logbook = algorithms.eaMuCommaLambda(pop, toolbox, mu=MU, lambda_=LAMBDA, cxpb=CXPB, mutpb=MUTPB, ngen=RGEN, stats=stats, halloffame=hof, verbose=False)best = hof[0]#pop, logbook = algorithms.eaSimple(pop, toolbox, #         cxpb=CXPB, mutpb=MUTPB, ngen=100, stats=stats, halloffame=hof, verbose=False)#best = hof[0] clear_output()  render = render_individual(best) history.extend([clamp(0.0, 5000.0, l["min"]) for l in logbook])show_results(history, render, target)  print(f"Gen ({(g+1)*RGEN}) : best fitness = {fitness_mse(render)}")

2. 运行结果

下图显示了代码的运行结果,作为对比,图中还显示了使用经典遗传算法生成的结果。

代码运行结果

相关链接

遗传算法与深度学习实战(1)——进化深度学习
遗传算法与深度学习实战(4)——遗传算法(Genetic Algorithm)详解与实现
遗传算法与深度学习实战(5)——遗传算法中常用遗传算子
遗传算法与深度学习实战(6)——遗传算法框架DEAP
遗传算法与深度学习实战(7)——DEAP框架初体验
遗传算法与深度学习实战(10)——使用遗传算法重建图像
遗传算法与深度学习实战(14)——进化策略详解与实现

http://www.yayakq.cn/news/125443/

相关文章:

  • 张云网站建设太原自助建站软件
  • 图书网站开发的实践意义开放平台官网
  • 网站是否能够被恶意镜像手机ui设计是什么
  • 网站开发的国内外现状中国核工业第五建设有限公司
  • 长春网站建设开发保护环境做网站素材
  • 建设用地规划证查询网站南博会官方网站建设投入
  • 中国工程建设监理协会网站做网站设计的
  • 手机网站微信登陆淘宝网站域名
  • 个人网站的搭建浙江建设厅 继续教育 网站
  • 搞一个网站花多少钱曲靖做网站需要多少钱
  • wordpress后台换中文seo网站是什么意思
  • 网站代备案系统百度下载免费安装
  • 商城网站建设行情新网网站模板
  • 网站设计公司有用吗灵感素材库
  • 国家大宗商品交易平台seo云优化软件破解版
  • 网站研发费用吗百度搜索指数在线查询
  • 太原网站设计公司网赌网站做流量渗透
  • 手机网站建设文章中关村在线手机参数对比
  • 北太平桥网站建设北京城乡建设和住房门户网站
  • 网站建设合同属于印花税的哪个税目手机端网站如何做排名
  • 北京网站建设推广个人网站怎么申请注册
  • 深圳建网站服务商汕尾招聘网
  • 湘潭高端网站建设福田祥菱v2双排后双轮报价
  • 上海通信管理局网站做跨境电商的血泪教训
  • 吉县网站建设30个成功的电子商务网站设计
  • 怎样做网站海报页面设计器翻译成英文
  • 做网站需要哪些技术支持wordpress菜单页面顺序
  • 爱站关键词挖掘查询工具广州短视频seo哪家好
  • 网站开发技术要学什么椒江哪里可以做公司网站
  • 标准论坛网站建设做vip兼职设计师的网站有哪些