我正在做一个插画修图AI程序项目

问题描述 投票:0回答:0
import os
import cv2 # OpenCV library
import numpy as np
from PIL import Image # PIL library

# Set the path to the directory containing input images
input_dir = 'C:/Users/user/practice/easy_ai/pixiv_og_11062020'

# Set the path to the directory to save preprocessed images
preprocessed_dir = 'C:/Users/user/practice/easy_ai/preprocessed/images/directory'

# Get a list of all image filenames in the input directory
image_files = [f for f in os.listdir(input_dir) if f.endswith('.jpg') or f.endswith('.png')]

# Loop through each image and preprocess it
for i, filename in enumerate(image_files):
    # Load the image using OpenCV
    img_path = os.path.join(input_dir, filename)
    img = cv2.imread(img_path)

    # Convert the image to RGB format (if necessary)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    # Adjust brightness and contrast using Histogram Equalization
    img_yuv = cv2.cvtColor(img, cv2.COLOR_RGB2YUV)
    img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0])
    img = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2RGB)

    # Apply a median filter to remove noise
    img = cv2.medianBlur(img, 3)

    # Normalize the color channels using mean normalization
    img = img / 255.0
    mean = np.mean(img)
    std = np.std(img)
    img = (img - mean) / std

    # Save the preprocessed image to a new file
    preprocessed_path = os.path.join(preprocessed_dir, f'preprocessed_image_{i}.jpg')
    Image.fromarray((img * 255).astype(np.uint8)).save(preprocessed_path)

用上面的代码运行预处理后,

import tensorflow as tf
import os
import cv2
import numpy as np

# assuming generated_image is the generated image and discriminator_input_shape is the shape of the discriminator input
def preprocess_image(image_folder, filename, discriminator_input_shape):
    img = cv2.imread(os.path.join(image_folder, filename))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = tf.image.resize(img, discriminator_input_shape[:2])
    img = np.array(img) / 127.5 - 1.0
    return img

# Define the function to save generated images
def save_imgs(epoch, generator, sample_dir='samples'):
    if not os.path.exists(sample_dir):
        os.makedirs(sample_dir)

    # Generate a batch of fake images
    noise = np.random.normal(0, 1, (25, 100))
    gen_imgs = generator.predict(noise)

    # Rescale images 0 - 1
    gen_imgs = 0.5 * gen_imgs + 0.5

    # Save generated images
    for i in range(gen_imgs.shape[0]):
        cv2.imwrite(os.path.join(sample_dir, f"{epoch}_{i}.png"), cv2.cvtColor(gen_imgs[i] * 255, cv2.COLOR_RGB2BGR))


image_folder = 'C:/Users/user/practice/easy_ai/preprocessed/images/directory'
discriminator_input_shape = (32, 32, 3)
images = []

for filename in os.listdir(image_folder):
    img = preprocess_image(image_folder, filename, discriminator_input_shape)
    images.append(img)
    
images=np.array(images)

# Define the generator and discriminator models
from keras.models import Sequential, Model
from keras.layers import Input, Dense, Reshape, Flatten, Conv2D, Conv2DTranspose, LeakyReLU
from keras.optimizers import Adam

def build_generator():
    noise_shape = (100,)
    model = Sequential()
    model.add(Dense(256 * 4 * 4, input_shape=noise_shape))
    model.add(Reshape((4, 4, 256)))
    model.add(Conv2DTranspose(128, kernel_size=4, strides=2, padding='same'))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Conv2DTranspose(64, kernel_size=4, strides=2, padding='same'))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Conv2DTranspose(3, kernel_size=4, strides=2, padding='same', activation='tanh'))
    noise = Input(shape=noise_shape)
    image = model(noise)
    return Model(inputs=noise, outputs=image)

def build_discriminator():
    img_shape = discriminator_input_shape
    model = Sequential()
    model.add(Conv2D(32, kernel_size=3, strides=2, input_shape=img_shape, padding='same'))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Conv2D(64, kernel_size=3, strides=2, padding='same'))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Conv2D(128, kernel_size=3, strides=2, padding='same'))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Flatten())
    model.add(Dense(1, activation='sigmoid'))
    image = Input(shape=img_shape)
    validity = model(image)
    return Model(inputs=image, outputs=validity)

# Combine the generator and discriminator models into a GAN model
generator = build_generator()
discriminator = build_discriminator()
discriminator.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.0002, beta_1=0.5), metrics=['accuracy'])
z = Input(shape=(100,))
img = generator(z)
discriminator.trainable = False
valid = discriminator(img)
gan = Model(z, valid)
gan.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.0002, beta_1=0.5))

# Train the GAN model
batch_size = 32
epochs = 10000
sample_interval = 1000

for epoch in range(epochs):
    # Select a random batch of images
    idx = np.random.randint(0, images.shape[0], batch_size)
    imgs = images[idx]

    # Generate a batch of fake images
    noise = np.random.normal(0, 1, (batch_size, 100))
    gen_imgs = generator.predict(noise)

    # Resize generated images to match discriminator input shape
    resized_gen_imgs = tf.image.resize(gen_imgs, discriminator.input_shape[1:3])

    # Train the discriminator
    d_loss_real = discriminator.train_on_batch(imgs, np.ones((batch_size, 1)))
    d_loss_fake = discriminator.train_on_batch(resized_gen_imgs, np.zeros((batch_size, 1)))
    d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

    # Train the generator
    noise = np.random.normal(0, 1, (batch_size, 100))
    valid_y = np.array([1] * batch_size)
    g_loss = gan.train_on_batch(noise, valid_y)

    # Print progress
    print("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100 * d_loss[1], `your text`g_loss))

    # Save generated images at certain intervals
    if epoch % sample_interval == 0:
        save_imgs(epoch, generator)

我好像用上面的代码连模型都建好了,但是我想学习原始数据,那么如何划分数据和写学习代码呢? 我把原图按目录分了,分了好几次。一路修改代码走到这里,就是不知道怎么分割图片。并学习构建的模型。

deep-learning artificial-intelligence
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.