首页 > 其他分享 >利用卷积反卷积实现图片自编码器

利用卷积反卷积实现图片自编码器

时间:2022-11-10 15:03:00浏览次数:43  
标签:编码器 IMG 卷积 same dataset padding shape train 图片


手写数字

from tensorflow.keras.layers import Conv2D,MaxPooling2D,Input,Conv2DTranspose,Flatten,Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Sequential,Model,load_model
import tensorflow as tf
import numpy as np
import cv2

IMG_H = 28
IMG_W = 28
IMG_C = 1

def tf_generate(x_train,y_train):
dataset = tf.data.Dataset.from_tensor_slices((x_train,y_train))
dataset = dataset.repeat()
dataset = dataset.shuffle(100).batch(1).repeat()
return dataset

def train_dataset(dataset):
latent_dim = IMG_H//2*IMG_W//2*IMG_C
input_img = Input(shape =(IMG_H,IMG_W,IMG_C))
x = Conv2D(32,3,padding='same',activation='relu')(input_img)
x = Conv2D(64,3,padding='same',activation='relu',strides=(2,2))(x)
x = Conv2D(64,3, padding='same',activation='relu')(x)
x = Conv2D(64,3,padding='same',activation='relu')(x)
x = Flatten()(x)
out = Dense(latent_dim)(x)
y = Flatten()(out)
y = tf.reshape(out,(1,IMG_H//2,IMG_W//2,IMG_C))
y = Conv2DTranspose(64,3,padding="same",use_bias=False)(y)
y = Conv2DTranspose(64,3,padding="same",use_bias=False)(y)
y = Conv2DTranspose(64,3,strides=(2,2),padding="same",use_bias=False)(y)
y = Conv2DTranspose(32,3,padding="same",use_bias=False)(y)
y = Conv2DTranspose(1,1,padding="same",use_bias=False)(y)
model = Model(input_img,y)
model.summary()
model.compile(loss="mse", optimizer="adam",metrics=["accuracy"])
history = model.fit(dataset, batch_size=1,epochs=20,steps_per_epoch=1000)
model.save("auto_mnist.h5")
def predict(test):
model = load_model("auto_mnist.h5")
cv2.imwrite("test.png",test)
x_test = cv2.resize(test,(IMG_H, IMG_W))/255.0
x_test = x_test.reshape(1,IMG_H,IMG_W,1)
result = model.predict(x_test)
print (result[0].shape)
result = result[0].reshape(IMG_H,IMG_W,1)
cv2.imwrite("result.png",result*255.0)

def main():
def train():
mnist=tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test)=mnist.load_data()
x_train = tf.expand_dims(x_train,axis=-1)/255
x_test = tf.expand_dims(x_test,axis=-1)
print(x_train.shape)
dataset = tf_generate(x_train,x_train)
train_dataset(dataset)
def test():
mnist=tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test)=mnist.load_data()
predict(x_test[0])
train()
#test()
main()

原图

利用卷积反卷积实现图片自编码器_ide


模型输出

利用卷积反卷积实现图片自编码器_ide_02

彩色图片

from tensorflow.keras.layers import Conv2D,MaxPooling2D,Input,Conv2DTranspose,Flatten,Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Sequential,Model,load_model
import tensorflow as tf
import numpy as np
import cv2

IMG_H = 512
IMG_W = 512
IMG_C = 3 ## Change this to 1 for grayscale.


def tf_dataset(image_path1,image_path2):
X_train = np.empty((len(image_path1), IMG_H, IMG_W, IMG_C))
Y_train = np.empty((len(image_path2), IMG_H, IMG_W, IMG_C))
for index,src in enumerate(image_path1):
img = cv2.imread(src)
image = cv2.resize(img,(IMG_H, IMG_W))/255.0
X_train[index] = image
for index,src in enumerate(image_path2):
img = cv2.imread(src)
image = cv2.resize(img,(IMG_H, IMG_W))/255.0
Y_train[index] = image
return X_train,Y_train


def tf_generate(x_train,y_train):
dataset = tf.data.Dataset.from_tensor_slices((x_train,y_train))
dataset = dataset.repeat()
dataset = dataset.shuffle(100).batch(1).repeat()
return dataset


def train_dataset(dataset):
latent_dim = 4*4*512
input_img = Input(shape =(224,224,3))

x = Conv2D(32,3,padding='same',activation='relu')(input_img)
print (x.shape)
x = Conv2D(64,3,padding='same',activation='relu',strides=(2,2))(x)
print (x.shape)
x = x+Conv2D(64,3, padding='same',activation='relu',strides=(1,1))(x)
print (x.shape)
x = Conv2D(128,3, padding='same',activation='relu',strides=(2,2))(x)
print (x.shape)
x = x+Conv2D(128,3, padding='same',activation='relu')(x)
print (x.shape)
x = Conv2D(256,3, padding='same',activation='relu',strides=(2,2))(x)
print (x.shape)
x = x+Conv2D(256,3, padding='same',activation='relu')(x)
print (x.shape)
x = Conv2D(512,3, padding='same',activation='relu',strides=(2,2))(x)
print (x.shape)
x = x+Conv2D(512,3, padding='same',activation='relu')(x)
print (x.shape)


y = x+Conv2DTranspose(512,3,padding="same",use_bias=False)(x)
print (y.shape)
y = Conv2DTranspose(256,3,padding="same",strides=(2,2),use_bias=False)(y)
print (y.shape)
y = y+Conv2DTranspose(256,3,padding="same",use_bias=False)(y)
print (y.shape)
y = Conv2DTranspose(128,3,padding="same",strides=(2,2),use_bias=False)(y)
print (y.shape)
y = y+Conv2DTranspose(128,3,padding="same",use_bias=False)(y)
print (y.shape)
y = Conv2DTranspose(64,3,padding="same",strides=(2,2),use_bias=False)(y)
print (y.shape)
y = y+Conv2DTranspose(64,3,padding="same",use_bias=False)(y)
print (y.shape)
y = Conv2DTranspose(32,3,padding="same",strides=(2,2),use_bias=False)(y)
print (y.shape)
out = Conv2DTranspose(3,3,padding="same",use_bias=False)(y)
print (y.shape)

model = Model(input_img,out)
model.summary()
model.compile(loss="mse", optimizer="adam",metrics=["accuracy"])
history = model.fit(dataset, batch_size=1,epochs=4,steps_per_epoch=100)
model.save("auto_new_style_a_b.h5")


def predict():
src = "./0.png"
model = load_model("auto_style_a_b.h5")
img = cv2.imread(src)
cv2.imwrite("test.png",img)
x_test = cv2.resize(img,(IMG_H, IMG_W))/255.0

x_test = x_test.reshape(1,IMG_H,IMG_W,3)
result = model.predict(x_test)
print (result[0].shape)

result = result[0].reshape(IMG_H,IMG_W,3)
cv2.imwrite("output5.png",result*255.0)



def main():
def train():
images_path1 = glob("./style/a/*.png")
images_path2 = glob("./style/b/*.png")
count_dataset = len(images_path1),len(images_path2)
print (count_dataset)
x_train,y_train = tf_dataset(images_path1,images_path2)
dataset = tf_generate(x_train,x_train)
train_dataset(dataset)

#train()
predict()

main()

原图

利用卷积反卷积实现图片自编码器_深度学习_03


模型输出

利用卷积反卷积实现图片自编码器_深度学习_04


说明:本想从真人到动漫图像转换输出,结果不好。

图片较小(64*64),转换结果不错,越大,结果越差。不同图片大小转换结果:(真人-动漫,动漫-真人)

利用卷积反卷积实现图片自编码器_深度学习_05


利用卷积反卷积实现图片自编码器_深度学习_06


利用卷积反卷积实现图片自编码器_tensorflow_07


标签:编码器,IMG,卷积,same,dataset,padding,shape,train,图片
From: https://blog.51cto.com/u_15872074/5841643

相关文章

  • 使用卷积神经网络实现简易的语音分类的对话系统
    一、思路1.收集简单的语音词汇,数量越多越好,当做数据集。2.为每个词汇建立词汇内容的标签,建立一个字典,键值为文本词语,键为数字标签,训练时只能使用数字表示。3.建立卷积神经......
  • simpread-(127 条消息) FastAPI 图片上传接口_waketzheng 的博客 - CSDN 博客_fastapi
    本文由简悦SimpRead转码,原文地址blog.csdn.net需求:上传一个图片,保存到服务器,然后返回一个URL设置静态资源文件夹#main.py#!/usr/bin/envpython3.8import......
  • 浅谈性能优化之图片压缩、加载和格式选择
    原文链接:浅谈性能优化之图片压缩、加载和格式选择在认识图片优化前,我们先了解下【二进制位数】与【色彩呈现】的关系。二进制位数与色彩在计算机中,一般用二进制数来......
  • vue中配置接口返回不带域名的图片地址显示问题
    前端上传图片,通过接口传给后端,需要前端渲染时,接口返回的图片路径如下图其中的path就是图片地址,而我们想渲染图片到前端页面,正常的思路是需要在前面加上服务器的域名1.......
  • 前端上传图片时添加水印
    前段时间有个功能需要上传图片并添加水印,于是就查了些资料,但是大部分都不太适用,或者说没反应。先说下需要用到的,canvas,添加水印,只能用这个去创建画布,把文字平铺到画布上......
  • 微博动态图片爬取
    微博动态图片爬取由于微博的网页端有反爬虫,需要登录,所以我们换个思路,曲线救国。我们找到微博在浏览器上面用于手机端的调试的APL,如何找到呢?我这边直接附上微博的手机端的地......
  • 直播平台怎么搭建,Android与Js互调之传递图片
    直播平台怎么搭建,Android与Js互调之传递图片添加addJavascriptInterface注解方法H5VerificationJavascriptInterface对象映射 publicclassH5VerificationJavascrip......
  • 截图汇总,图片空间
    目录​​写在前面​​​​一、Git​​​​二、java设计图​​​​三、浏览器​​​​四、用户管理、涉及、思考​​​​4.1、登陆​​​​五、Json​​​​六、JDK​​​​......
  • linux nginx 配置图片服务器
    location~.*\.(gif|jpg|jpeg|png)${expires24h;root/glxy/imgs;#指定图片存放路径access_log/glxy/imgs/log;#日志proxy_store......
  • python应用——借助tinify库实现批量压缩图片
    说在前面的话:转到移动组之后,干起了移动端混合开发,虽说现在的混合开发技术在不断的迭代了,但对比起网页应用来说有一些先天的不足,诸如,硬件性能就是很大的一方面。在pc上调......