Форма ввода автоэнкодера: ожидаемый input_1 будет иметь форму (256, 256, 3), но получил массив с формой (256, 256, 4) - PullRequest
0 голосов
/ 22 апреля 2020

Я пытаюсь построить авто-кодировщик, но я получаю следующую ошибку, и я не могу понять почему.

ValueError: Ошибка при проверке ввода: ожидается, что input_1 будет иметь форму (256, 256, 3) но получил массив с формой (256, 256, 4)

Если я печатаю форму изображения, я получаю (256, 256, 3), но я все равно получаю ошибку относительно формы.

Любая помощь будет be fantasti c.

Ubuntu 18.04 | Python 3.7.6 | Тензор потока 2.1


from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, Dropout, Conv2DTranspose, UpSampling2D, add
from tensorflow.keras.models import Model
from tensorflow.keras import regularizers

import os
import re
from scipy import ndimage, misc
from skimage.transform import resize, rescale
from matplotlib import pyplot
import numpy as np

#Functions

def train_batches(just_load_dataset=False):

    batches = 256 # Number of images to have at the same time in a batch

    batch = 0 # Number if images in the current batch (grows over time and then resets for each batch)
    batch_nb = 0 # Batch current index

    max_batches = -1 # If you want to train only on a limited number of images to finish the training even faster.

    ep = 4 # Number of epochs

    images = []
    x_train_n = []
    x_train_down = []

    x_train_n2 = [] # Resulting high res dataset
    x_train_down2 = [] # Resulting low res dataset

    for root, dirnames, filenames in os.walk(input_dir):
        for filename in filenames:
            if re.search("\.(jpg|jpeg|JPEG|png|bmp|tiff)$", filename):
                if batch_nb == max_batches: # If we limit the number of batches, just return earlier
                    return x_train_n2, x_train_down2
                filepath = os.path.join(root, filename)
                image = pyplot.imread(filepath)

                if len(image.shape) > 2:

                    image_resized = resize(image, (256, 256))
                    x_train_n.append(image_resized)
                    x_train_down.append(rescale(rescale(image_resized, 0.5), 2.0))
                    batch += 1
                    if batch == batches:
                        batch_nb += 1

                        x_train_n2 = np.array(x_train_n)
                        x_train_down2 = np.array(x_train_down)

                        if just_load_dataset:
                            return x_train_n2, x_train_down2

                        print('Training batch', batch_nb, '(', batches, ')')

                        autoencoder.fit(x_train_down2, x_train_n2, epochs=ep, batch_size=10, shuffle=True, validation_split=0.15)

                        x_train_n = []
                        x_train_down = []

                        batch = 0

    return x_train_n2, x_train_down2

#Script

input_dir="/mnt/vanguard/datasets/ffhq-dataset/thumbnails256x256"

n = 256
chan = 3
input_img = Input(shape=(n, n, chan))


# Encoder
l1 = Conv2D(64, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(input_img)
l2 = Conv2D(64, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(l1)
l3 = MaxPooling2D(padding='same')(l2)
l3 = Dropout(0.3)(l3)
l4 = Conv2D(128, (3, 3),  padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(l3)
l5 = Conv2D(128, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(l4)
l6 = MaxPooling2D(padding='same')(l5)
l7 = Conv2D(256, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(l6)

# Decoder
l8 = UpSampling2D()(l7)
l9 = Conv2D(128, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(l8)
l10 = Conv2D(128, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(l9)
l11 = add([l5, l10])
l12 = UpSampling2D()(l11)
l13 = Conv2D(64, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(l12)
l14 = Conv2D(64, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(l13)
l15 = add([l14, l2])

#chan = 3, for RGB
decoded = Conv2D(chan, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(l15)

# Create neural network
autoencoder = Model(input_img, decoded)
autoencoder_hfenn = Model(input_img, decoded)
autoencoder.summary()

autoencoder.compile(optimizer='adadelta', loss='mean_squared_error')

x_train_n = []
x_train_down = []
x_train_n, x_train_down = train_batches()

1 Ответ

0 голосов
/ 28 апреля 2020

Изменение масштаба x_train_down.append(rescale(rescale(image_resized, 0.5), 2.0)) вызывает проблему. opencv может использоваться: small = cv2.resize(image_resized, (0,0), fx=0.5, fy=0.5); large = cv2.resize(small, (0,0), fx=2.0, fy=2.0) для ухудшения качества изображения. Также обратите внимание, что это интенсивное вычисление на GPU. Либо размер изображения должен быть уменьшен, либо попробуйте графический процессор с большим объемом памяти (K80).

...