我在配置Keras/Tensorflow模型时遇到了困难

qq24tv8q  于 2023-03-02  发布在  其他
关注(0)|答案(1)|浏览(149)

我试图创建这个人工智能模型,可以帮助我生成,修改视频。我以前遇到过一些错误,但我能够绕过他们。无论如何,现在我陷入了一个可怕的,恼人的错误和我的头伤害我,而试图解决它,我累了,我readlly需要一些帮助
错误说明X样本(输入数组)=16和Y样本(目标数组)=32的大小不相等,这是完整错误:

这是我的完整代码

import os
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, Reshape, BatchNormalization, Conv2DTranspose, Conv2D, LeakyReLU, Flatten
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam

processed_data_folder = 'data/processed_data/'
model_folder = 'models/'
image_size = 64
batch_size = 16
epochs = 50
learning_rate = 0.0002
beta_1 = 0.5

def build_generator():
    model = Sequential()

    model.add(Dense(4 * 4 * 512, input_shape=(100,)))
    model.add(Reshape((4, 4, 512)))
    model.add(BatchNormalization())
    model.add(Conv2DTranspose(256, kernel_size=5, strides=2, padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(Conv2DTranspose(128, kernel_size=5, strides=2, padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(Conv2DTranspose(64, kernel_size=5, strides=2, padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(Conv2DTranspose(3, kernel_size=5, strides=2, padding='same', activation='tanh'))

    return model

def build_discriminator():
    model = Sequential()

    model.add(Conv2D(64, kernel_size=5, strides=2, padding='same', input_shape=(image_size, image_size, 3)))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Conv2D(128, kernel_size=5, strides=2, padding='same'))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Conv2D(256, kernel_size=5, strides=2, padding='same'))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Conv2D(512, kernel_size=5, strides=2, padding='same'))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Flatten())
    model.add(Dense(1, activation='sigmoid'))

    return model

generator = build_generator()
discriminator = build_discriminator()

discriminator.compile(loss='binary_crossentropy', optimizer=Adam(lr=learning_rate, beta_1=beta_1), metrics=['accuracy'])

discriminator.trainable = False
gan = Sequential([generator, discriminator])
gan.compile(loss='binary_crossentropy', optimizer=Adam(lr=learning_rate, beta_1=beta_1))

X_train = []
for video_folder in os.listdir(processed_data_folder):
    frames_folder = os.path.join(processed_data_folder, video_folder, 'frames')
    frames = []
    for frame_file in os.listdir(frames_folder):
        frame_path = os.path.join(frames_folder, frame_file)
        frame = cv2.imread(frame_path)
        frame = cv2.resize(frame, (image_size, image_size))
        frames.append(frame)
    X_train.append(frames)
X_train = np.array(X_train)

X_train = np.reshape(X_train, (-1, image_size, image_size, 3))
print(X_train.shape)

X_train = X_train / 127.5 - 1.

def generate_noise(batch_size, noise_shape):
    return np.random.normal(0, 1, size=(batch_size, noise_shape))

for epoch in range(epochs):

    noise = generate_noise(batch_size, 100)

    generated_images = generator.predict(noise)

    idx = np.random.randint(0, X_train.shape[0], batch_size)
    
    real_images = X_train[idx]

    real_labels = np.ones((batch_size, 1))
    
    fake_labels = np.zeros((batch_size, 1))
    
    d_loss_real = discriminator.train_on_batch(real_images, real_labels)
    
    d_loss_fake = discriminator.train_on_batch(generated_images, fake_labels)

    X = np.concatenate([real_images, generated_images])
    y = np.concatenate([real_labels, fake_labels])

    g_loss = gan.train_on_batch(noise, y)

    print(f"Epoch {epoch+1}/{epochs} [D real loss: {d_loss_real[0]:.4f}, D fake loss: {d_loss_fake[0]:.4f}, G loss: {g_loss:.4f}]")
    
    discriminator.trainable = True 
    
    generator_history = generator.fit(X_train, np.ones((X_train.shape[0], 1)), batch_size=batch_size, epochs=10, shuffle=True) 
    
    discriminator_history = discriminator.fit(X_train, y_train, batch_size=batch_size, epochs=10, shuffle=True)
    
generator.save(os.path.join(model_folder, 'video_generator.h5'))

批处理大小是32,由于系统内存不足,我不得不减少批处理大小。
基本上我尝试了一切,从调整帧图像到32然后到16,减少批量大小从64到32到16。限制帧到1000帧...很多方法和更新...我不记得了。

yx2lnoni

yx2lnoni1#

如@m13op22所述,问题出在g_loss = gan.train_on_batch(noise, y),应该是这个吗?:

g_loss = gan.train_on_batch(X, y)

相关问题