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

手写数字

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()

原图
请添加图片描述
模型输出
请添加图片描述

彩色图片

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()

原图
请添加图片描述
模型输出
在这里插入图片描述
说明:本想从真人到动漫图像转换输出,结果不好。
图片较小(64*64),转换结果不错,越大,结果越差。

不同图片大小转换结果:(真人-动漫,动漫-真人)
请添加图片描述
请添加图片描述
请添加图片描述

猜你喜欢

转载自blog.csdn.net/qq_38641985/article/details/122286273