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

做个企业网站 优帮云鄞州区建网站外包

做个企业网站 优帮云,鄞州区建网站外包,手机开发框架,电子政务网站开发和设计的关系准备 官方教程: 任意风格的快速风格转换 模型下载地址: https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2 学习 加载要处理的内容图片和风格图片 # 用于将图像裁剪为方形def crop_center(image):# 图片原始形状shape image…

准备

官方教程: 任意风格的快速风格转换

模型下载地址: https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2

学习

加载要处理的内容图片和风格图片

# 用于将图像裁剪为方形def crop_center(image):# 图片原始形状shape = image.shape# 新形状new_shape = min(shape[1], shape[2])offset_y = max(shape[1]-shape[2], 0) // 2offset_x = max(shape[2]-shape[1], 0) // 2# 返回新图片image = tf.image.crop_to_bounding_box(image, offset_y, offset_x, new_shape, new_shape)return image# 加载并预处理图片def load_image(image_url, image_size=(256, 256), preserve_aspect_ratio=True):# 缓存图像文件image_path = tf.keras.utils.get_file(os.path.basename(image_url)[-128:], image_url)# 加载并转换为float32 numpy数组,添加批次维度,并规范化为范围[0,1]。img = tf.io.decode_image(tf.io.read_file(image_path),channels=3, dtype=tf.float32)[tf.newaxis, ...]img = crop_center(img)img = tf.image.resize(img, image_size, preserve_aspect_ratio=True)return img# 展示图片def show_n(images, titles=('',)):n = len(images)image_sizes = [image.shape[1] for image in images]w = (image_sizes[0] * 6) // 320plt.figure(figsize=(w * n, w))gs = gridspec.GridSpec(1, n, width_ratios=image_sizes)for i in range(n):plt.subplot(gs[i])plt.imshow(images[i][0], aspect='equal')plt.axis('off')plt.title(titles[i] if len(titles) > i else '')plt.show()content_image_url = 'https://scpic3.chinaz.net/files/default/imgs/2023-11-16/6e397d19e172be9f_s.jpg'
style_image_url = 'https://scpic3.chinaz.net/files/default/imgs/2023-11-05/d217bbaf821e3a73_s.jpg'
output_image_size = 384# 调整内容图像的大小
content_img_size = (output_image_size, output_image_size)
#  样式图片大小
style_img_size = (256, 256)
# 加载并展示图片
content_image = load_image(content_image_url, content_img_size)
style_image = load_image(style_image_url, style_img_size)
style_image = tf.nn.avg_pool(style_image, ksize=[3, 3], strides=[1, 1], padding='SAME')
show_n([content_image, style_image], ['Content image', 'Style image'])

在这里插入图片描述

加载模型进行风格迁移

# 加载模型
hub_module = hub.load('./magenta_arbitrary-image-stylization-v1-256_2')
# 风格迁移
outputs = hub_module(tf.constant(content_image), tf.constant(style_image))
stylized_image = outputs[0]
# 展示迁移后的图片
show_n([content_image, style_image, stylized_image], titles=['Original content image', 'Style image', 'Stylized image'])

在这里插入图片描述

加载本地图片
加载本地图片的话,只需要将加载网络图片的代码改成下面的

def load_image(image_url, image_size=(256, 256), preserve_aspect_ratio=True):# 缓存图像文件# image_path = tf.keras.utils.get_file(#     os.path.basename(image_url)[-128:], image_url)# 加载并转换为float32 numpy数组,添加批次维度,并规范化为范围[0,1]。img = tf.io.decode_image(tf.io.read_file(image_url),channels=3, dtype=tf.float32)[tf.newaxis, ...]img = crop_center(img)img = tf.image.resize(img, image_size, preserve_aspect_ratio=True)return img

下面的效果图是基于一只狗和梵高的星空生成的

在这里插入图片描述

完整代码


# import os
from matplotlib import gridspec
import matplotlib.pylab as plt
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub# 用于将图像裁剪为方形def crop_center(image):# 图片原始形状shape = image.shape# 新形状new_shape = min(shape[1], shape[2])offset_y = max(shape[1]-shape[2], 0) // 2offset_x = max(shape[2]-shape[1], 0) // 2# 返回新图片image = tf.image.crop_to_bounding_box(image, offset_y, offset_x, new_shape, new_shape)return image# 加载并预处理图片def load_image(image_url, image_size=(256, 256), preserve_aspect_ratio=True):# 缓存图像文件# image_path = tf.keras.utils.get_file(#     os.path.basename(image_url)[-128:], image_url)# 加载并转换为float32 numpy数组,添加批次维度,并规范化为范围[0,1]。img = tf.io.decode_image(tf.io.read_file(image_url),channels=3, dtype=tf.float32)[tf.newaxis, ...]img = crop_center(img)img = tf.image.resize(img, image_size, preserve_aspect_ratio=True)return img# 展示图片def show_n(images, titles=('',)):n = len(images)image_sizes = [image.shape[1] for image in images]w = (image_sizes[0] * 6) // 320plt.figure(figsize=(w * n, w))gs = gridspec.GridSpec(1, n, width_ratios=image_sizes)for i in range(n):plt.subplot(gs[i])plt.imshow(images[i][0], aspect='equal')plt.axis('off')plt.title(titles[i] if len(titles) > i else '')plt.show()content_image_url = 'image/dog.png'
style_image_url = 'image/fangao.png'
output_image_size = 384# 调整内容图像的大小
content_img_size = (output_image_size, output_image_size)
#  样式图片大小
style_img_size = (256, 256)
# 加载图片
content_image = load_image(content_image_url, content_img_size)
style_image = load_image(style_image_url, style_img_size)
style_image = tf.nn.avg_pool(style_image, ksize=[3, 3], strides=[1, 1], padding='SAME')
# 展示图片
# show_n([content_image, style_image], ['Content image', 'Style image'])# 加载模型
hub_module = hub.load('./magenta_arbitrary-image-stylization-v1-256_2')
# 风格迁移
outputs = hub_module(tf.constant(content_image), tf.constant(style_image))
stylized_image = outputs[0]
# 展示迁移后的图片
show_n([content_image, style_image, stylized_image], titles=['Original content image', 'Style image', 'Stylized image'])
http://www.yayakq.cn/news/669878/

相关文章:

  • 綦江中国建设银行官网站宁夏 网站制作
  • 点墨网站网站运营设计
  • 外贸网站seo教程南通高端网站设计开发
  • 网站外链是什么意思凡科商城app
  • 徐州招聘网站哪个好最新新闻热点事件2022年
  • 周口城乡建设局网站网站开发工程师符号代码
  • 网站建设属于技术活吗斗图在线制作
  • 网站保定网站建设多少钱中国网站建设公司排行
  • 广西医院响应式网站建设方案wordpress插件赚钱
  • 大连模板网站制作电话网页设计与制作教程读书心得
  • 沈阳网站关键词优化服务好2核4G服务器wordpress
  • 咸宁网站seo台州网站哪家专业
  • 高端做网站价格上海移动端网络推广哪家强
  • 怎么把网站扒下来wordpress 消息框
  • 程序源代码网站网站开发是什么职位
  • 济南建设网站的公司福田网站改版
  • 企业网站开发报告dx365.wordpress
  • 鞍山网站设计制作购买网站空间域名
  • 网站企业地图环球资源网的网站特色
  • 深圳网站推广外包建设工程交易中心网
  • 建设微信网站设计制作如何破解网站后台网址
  • 徐州网站开发怎样为什么做电商网站
  • 网站不能风格南山网站公司
  • 自己做网站能赚钱吗做简单的企业网站需要学哪些
  • 塘沽网站优化网站制作报价是否合法
  • 网站分页样式wordpress访问地址修改
  • wordpress 网站底部美化域名注册西部数码
  • 自助下单网站咋做wordpress+贴吧主题
  • P2P网站怎么建设宁波淘宝网站建设
  • 网站建设人员岗位职责网站建设合同制