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

huangapple go评论55阅读模式
英文:

I'm having a hard time while conguring a Keras/tensorflow model

问题

这是您提供的代码的翻译部分:

我尝试创建一个能够帮助我生成和修改视频的AI模型之前我曾遇到一些错误但我成功地绕过了它们无论如何现在我陷入了一个可怕且恼人的错误中尝试解决它让我的头疼我真的需要一些帮助**务必**帮我

错误信息显示X样本输入数组的大小为16Y样本目标数组的大小为32这是完整的错误信息

[![完整的错误信息](https://i.stack.imgur.com/7OzgN.png)](https://i.stack.imgur.com/7OzgN.png)

以下是我的**完整代码**

```python
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

# 构建生成器和判别器模型

# ...(生成器和判别器的代码)...

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

# 处理训练数据

# ...(处理训练数据的代码)...

# 训练GAN模型

# ...(GAN模型的训练代码)...

# 保存生成器模型

generator.save(os.path.join(model_folder, 'video_generator.h5'))

您提到由于系统内存不足,将批处理大小从32减小到16。您还尝试了很多方法,包括将帧图像的大小从32调整为16,减小批处理大小从64到32再到16,以及限制帧数为1000帧等。

英文:

i'm trying to create this ai model that can help me to generate, modify videos. I've faced some error previously but i was able to bypass them. Anyway noww i'm stuck in a terrible, annoying error and my head hurt me while trying to solve it, i'm tired i readlly need some help please:

The error says that the sizes of X samples(input arrays) =16 and Y samples(target arrays) =32 are not equal, this is the full error:

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

and here is my full code :

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

The batch size was 32 and i had to reduce because of some lack in my system memory.

Basically i tried everything, from resizing the frame images to 32 then to 16, reducing the batch size from 64 to 32 to 16. limited the frames to 1000 frame.... alot of methods and updtes...i am not able to remember.

答案1

得分: 2

根据 @m13op22 提到的,问题出在 g_loss = gan.train_on_batch(noise, y)。应该改成这样吗?:

g_loss = gan.train_on_batch(X, y)
英文:

As mentioned by @m13op22, the problem comes from g_loss = gan.train_on_batch(noise, y). Should it be this instead?:

g_loss = gan.train_on_batch(X, y)

huangapple
  • 本文由 发表于 2023年3月1日 09:44:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定