python ValueError:无法将输入数组从形状(640,1400,3)广播到形状(640,1400,3,3)

tzdcorbm  于 2023-05-16  发布在  Python
关注(0)|答案(1)|浏览(121)

下面的代码用于使用图像目录训练自动编码器。目录结构如下所示:

main.py
training_images
    img1.png
    img2.png
    img3.png
    ...
def loadImage(image):
    image = image.astype("float32")
    image = tf.image.resize_with_pad(image, 640, 1400)
    image = (image / 127.5) - 1
    return image

def loadImages():
    dataGenerator = tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function=loadImage, zoom_range=0.2, 
                                  horizontal_flip=True,rotation_range=0.05)
    trainX = dataGenerator.flow_from_directory("/kaggle/working/trainingImages/",
                                            target_size = (640, 1400, 3),
                                            batch_size = 1,
                                            shuffle = True,
                                            save_to_dir="augmented",
                                            class_mode = "input",
                                            subset = "training")

    return trainX
trainX = loadImages()

class AutoEncoder(tf.keras.Model):
    def __init__(self):
        super(AutoEncoder, self).__init__()
        self.encoder = tf.keras.Sequential([
        tf.keras.layers.Input(shape=(640, 1400, 3)),
        tf.keras.layers.Conv2D(16, (3, 3), activation="relu", padding="same", strides=2),
        tf.keras.layers.Conv2D(8, (3, 3), activation="relu", padding="same", strides=2)])

        self.decoder = tf.keras.Sequential([
          tf.keras.layers.Conv2DTranspose(8, kernel_size=3, strides=2, activation="relu", padding="same"),
          tf.keras.layers.Conv2DTranspose(16, kernel_size=3, strides=2, activation="relu", padding="same"),
          tf.keras.layers.Conv2D(1, kernel_size=(3, 3), activation="sigmoid", padding="same")])

    def call(self, x):
        encoded = self.encoder(x)
        decoded = self.decoder(encoded)
        return decoded

autoencoder = AutoEncoder()
autoencoder.compile(optimizer="adam", loss=tf.keras.losses.MeanSquaredError())
autoencoder.fit(trainX,
                epochs=10,
                shuffle=True,
                validation_data=trainX)

目录中的每个图像都具有不同的形状,因此,我们使用函数loadImage来重塑和规范化每个图像。

3bygqnnd

3bygqnnd1#

函数loadImage()返回形状为(640, 1400)的Tensor,但指定给tf.keras.layer.Input()的形状不同:(640, 1400, 3)

相关问题