• DCGAN生成目标训练图片


    前言:

    GAN的原理很简单,但是它有很多变体,如:DCGAN、CycleGAN、DeblurGAN等,它们也被用在不同地方,本文将用到DCGAN来生成头像图片,可以做到以假乱真的地步。

    1.首先调用程序对图片进行标准化

    代码如下:

    from skimage import io,transform,color
    import numpy as np
    ​
    def convert_gray(f,**args):
        """
        将彩色图片转换为灰度图片和调整大小,改变图像分辨率
        :return:
        """
        rgb = io.imread(f)
        # gray = color.rgb2gray(rgb)  #
        dst = transform.resize(rgb, (96, 96))  # 由于在后期测试的时候用的图片尺寸是96x96
        
        return dst
    ​
    datapath='your train path'
    str=datapath+'/*.jpg'   #识别.jpg的图像
    coll = io.ImageCollection(str,load_func=convert_gray)#批处理
    for i in range(len(coll)):
        io.imsave(r'your save paths'+np.str(i)+'.jpg',coll[i])

    2.调用程序

    • 训练图像 代码只能引用DCGAN的github代码:carpedm20/DCGAN-tensorflow

    • 调用命令:

      python main.py --input_height 96 --input_width 96 --output_height 48 --output_width 48 --dataset faces --crop --train --epoch 300 --input_fname_pattern "*.jpg"

       

    3. 切割图片

    由于生成的图片是由64张小尺寸图片拼接成一整张的,故需要进行对图片切割。

    切割代码如下:

    # encoding:utf-8
    from PIL import Image
    import sys
    import math
    import os
    ​
    def fill_image(image):
        """
        将图片填充为正方形
        :param image:
        :return:
        """
        width, height = image.size
        #选取长和宽中较大值作为新图片的
        new_image_length = width if width > height else height
        #生成新图片[白底]
        new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white')
        #将之前的图粘贴在新图上,居中
        if width > height:#原图宽大于高,则填充图片的竖直维度
            #(x,y)二元组表示粘贴上图相对下图的起始位置
            new_image.paste(image, (0, int((new_image_length - height) / 2)))
        else:
            new_image.paste(image,(int((new_image_length - width) / 2),0))
    ​
        return new_image
    ​
    ​
    def cut_image(image,cut_num):
        """
        切图
        :param image:
        :return:
        """
        flag_value = int(math.sqrt(cut_num))
        width, height = image.size
        item_width = int(width / flag_value)
        box_list = []
        for i in range(0,flag_value):
            for j in range(0,flag_value):
                box = (j*item_width,i*item_width,(j+1)*item_width,(i+1)*item_width)
                box_list.append(box)
        image_list = [image.crop(box) for box in box_list]
    ​
        return image_list
    ​
    ​
    def save_images(image_list):
        """
        保存
        :param image_list:
        :return:
        """
        index = 1
        dirs = './img_add/'
        if not os.path.exists(dirs):
            os.makedirs(dirs)
        for image in image_list:
            image.save(dirs+str(index) + '.png', 'PNG')
            index += 1def main(file_path,batch_size):
    ​
        image = Image.open(file_path)
        image = fill_image(image)
        image_list = cut_image(image,batch_size)
        save_images(image_list)
    ​
    if __name__ == '__main__':
        batch_size = 64
        file_path = "train.png"  # 图片路径
        main(file_path,batch_size)


    参考链接:https://blog.csdn.net/qq_34739497/article/details/79902356

  • 相关阅读:
    [Swift]LeetCode954. 二倍数对数组 | Array of Doubled Pairs
    [Xcode 实际操作]九、实用进阶-(18)图像人脸识别:对图片中的人像进行面部检测
    Hibernate or JPA Annotation中BLOB、CLOB注解写法
    install_driver(mysql) failed
    【Android】Android中AlertDialog对话框的使用实例
    POJ 2442 Sequence【堆】
    distributed OSGI demo
    DSP/BIOS使用之初窥门径——滴答时钟及烧写Flash
    提取工厂类
    【JAVA学习】struts2的action中使用session的方法
  • 原文地址:https://www.cnblogs.com/shierlou-123/p/11315890.html
Copyright © 2020-2023  润新知