Again and again the error occurs: "Error when checking target: expected activation_1018 to have 3 dimensions, but got an array with shape (1, 256, 256, 1)". I understand that the problem occurs when I try to submit to the activation function an array of a higher dimension, but I cannot understand why Reshape does not solve this problem ...
Thank you in advance for your help or at least a tip!
import numpy as np from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, UpSampling2D from keras.layers import Activation, Reshape, Permute from keras.preprocessing.image import ImageDataGenerator from keras import models from keras.optimizers import SGD import json from keras.layers.normalization import BatchNormalization import os os.environ['KERAS_BACKEND'] = 'theano' os.environ['THEANO_FLAGS'] = 'mode=FAST_RUN, device=gpu0, floatX=float32, optimizer=fast_compile' img_w = 256 img_h = 256 n_labels = 1 kernel = (3, 3) # модель SegNet encoding_layers = [ Conv2D(64, kernel, padding='same', input_shape=(img_h, img_w, 1)), BatchNormalization(), Activation('relu'), Conv2D(64, kernel, padding='same'), BatchNormalization(), Activation('relu'), MaxPooling2D(), Conv2D(128, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(128, kernel, padding='same'), BatchNormalization(), Activation('relu'), MaxPooling2D(), Conv2D(256, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(256, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(256, kernel, padding='same'), Activation('relu'), MaxPooling2D(), Conv2D(512, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(512, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(512, kernel, padding='same'), BatchNormalization(), Activation('relu'), MaxPooling2D(), Conv2D(512, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(512, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(512, kernel, padding='same'), BatchNormalization(), Activation('relu'), MaxPooling2D(), ] autoencoder = models.Sequential() autoencoder.encoding_layers = encoding_layers for l in autoencoder.encoding_layers: autoencoder.add(l) print(l.input_shape,l.output_shape,l) decoding_layers = [ UpSampling2D(), Conv2D(512, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(512, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(512, kernel, padding='same'), BatchNormalization(), Activation('relu'), UpSampling2D(), Conv2D(512, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(512, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(256, kernel, padding='same'), BatchNormalization(), Activation('relu'), UpSampling2D(), Conv2D(256, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(256, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(128, kernel, padding='same'), BatchNormalization(), Activation('relu'), UpSampling2D(), Conv2D(128, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(64, kernel, padding='same'), BatchNormalization(), Activation('relu'), UpSampling2D(), Conv2D(64, kernel, padding='same'), BatchNormalization(), Activation('relu'), Conv2D(n_labels, (1, 1), padding='valid'), BatchNormalization(), ] autoencoder.decoding_layers = decoding_layers for l in autoencoder.decoding_layers: autoencoder.add(l) autoencoder.add(Reshape((img_h * img_w, n_labels))) #autoencoder.add(Permute((2, 1))) autoencoder.add(Activation('softmax')) # сохранение модели with open('model_5l.json', 'w') as outfile: outfile.write(json.dumps(json.loads(autoencoder.to_json()), indent=2)) sgd = SGD(lr=0.001, momentum=0.9, decay=0.0005, nesterov=False) autoencoder.compile(loss="categorical_crossentropy", optimizer=sgd, metrics=['accuracy']) epochs = 5 spe = 30 data_gen_args = dict(rescale = 1./255) image_datagen = ImageDataGenerator(**data_gen_args) mask_datagen = ImageDataGenerator(**data_gen_args) data_IMpath = "train_images" data_MSpath = "train_images_mask" seed = 1 image_generator = image_datagen.flow_from_directory( data_IMpath, class_mode=None, seed=seed, target_size = (256, 256), color_mode = 'grayscale', batch_size = 1) mask_generator = mask_datagen.flow_from_directory( data_MSpath, class_mode=None, seed=seed, target_size = (256, 256), color_mode = 'grayscale', batch_size = 1) train_generator = zip(image_generator, mask_generator) autoencoder.fit_generator(train_generator, steps_per_epoch = 15, epochs = 10) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-11-a3f68d5b5ee9> in <module>() ----> 1 autoencoder.fit_generator(train_generator, steps_per_epoch = 15, epochs = 10) ~\Anaconda3\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs) 89 warnings.warn('Update your `' + object_name + 90 '` call to the Keras 2 API: ' + signature, stacklevel=2) ---> 91 return func(*args, **kwargs) 92 wrapper._original_function = func 93 return wrapper ~\Anaconda3\lib\site-packages\keras\models.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch) 1313 use_multiprocessing=use_multiprocessing, 1314 shuffle=shuffle, -> 1315 initial_epoch=initial_epoch) 1316 1317 @interfaces.legacy_generator_methods_support ~\Anaconda3\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs) 89 warnings.warn('Update your `' + object_name + 90 '` call to the Keras 2 API: ' + signature, stacklevel=2) ---> 91 return func(*args, **kwargs) 92 wrapper._original_function = func 93 return wrapper ~\Anaconda3\lib\site-packages\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch) 2228 outs = self.train_on_batch(x, y, 2229 sample_weight=sample_weight, -> 2230 class_weight=class_weight) 2231 2232 if not isinstance(outs, list): ~\Anaconda3\lib\site-packages\keras\engine\training.py in train_on_batch(self, x, y, sample_weight, class_weight) 1875 x, y, 1876 sample_weight=sample_weight, -> 1877 class_weight=class_weight) 1878 if self.uses_learning_phase and not isinstance(K.learning_phase(), int): 1879 ins = x + y + sample_weights + [1.] ~\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size) 1478 output_shapes, 1479 check_batch_axis=False, -> 1480 exception_prefix='target') 1481 sample_weights = _standardize_sample_weights(sample_weight, 1482 self._feed_output_names) ~\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix) 111 ': expected ' + names[i] + ' to have ' + 112 str(len(shape)) + ' dimensions, but got array ' --> 113 'with shape ' + str(data_shape)) 114 if not check_batch_axis: 115 data_shape = data_shape[1:] ValueError: Error when checking target: expected activation_26 to have 3 dimensions, but got array with shape (1, 256, 256, 1) Architecture
_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_1 (Conv2D) (None, 256, 256, 64) 640 _________________________________________________________________ batch_normalization_1 (Batch (None, 256, 256, 64) 256 _________________________________________________________________ activation_1 (Activation) (None, 256, 256, 64) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 256, 256, 64) 36928 _________________________________________________________________ batch_normalization_2 (Batch (None, 256, 256, 64) 256 _________________________________________________________________ activation_2 (Activation) (None, 256, 256, 64) 0 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 128, 128, 64) 0 _________________________________________________________________ conv2d_3 (Conv2D) (None, 128, 128, 128) 73856 _________________________________________________________________ batch_normalization_3 (Batch (None, 128, 128, 128) 512 _________________________________________________________________ activation_3 (Activation) (None, 128, 128, 128) 0 _________________________________________________________________ conv2d_4 (Conv2D) (None, 128, 128, 128) 147584 _________________________________________________________________ batch_normalization_4 (Batch (None, 128, 128, 128) 512 _________________________________________________________________ activation_4 (Activation) (None, 128, 128, 128) 0 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 64, 64, 128) 0 _________________________________________________________________ conv2d_5 (Conv2D) (None, 64, 64, 256) 295168 _________________________________________________________________ batch_normalization_5 (Batch (None, 64, 64, 256) 1024 _________________________________________________________________ activation_5 (Activation) (None, 64, 64, 256) 0 _________________________________________________________________ conv2d_6 (Conv2D) (None, 64, 64, 256) 590080 _________________________________________________________________ batch_normalization_6 (Batch (None, 64, 64, 256) 1024 _________________________________________________________________ activation_6 (Activation) (None, 64, 64, 256) 0 _________________________________________________________________ conv2d_7 (Conv2D) (None, 64, 64, 256) 590080 _________________________________________________________________ activation_7 (Activation) (None, 64, 64, 256) 0 _________________________________________________________________ max_pooling2d_3 (MaxPooling2 (None, 32, 32, 256) 0 _________________________________________________________________ conv2d_8 (Conv2D) (None, 32, 32, 512) 1180160 _________________________________________________________________ batch_normalization_7 (Batch (None, 32, 32, 512) 2048 _________________________________________________________________ activation_8 (Activation) (None, 32, 32, 512) 0 _________________________________________________________________ conv2d_9 (Conv2D) (None, 32, 32, 512) 2359808 _________________________________________________________________ batch_normalization_8 (Batch (None, 32, 32, 512) 2048 _________________________________________________________________ activation_9 (Activation) (None, 32, 32, 512) 0 _________________________________________________________________ conv2d_10 (Conv2D) (None, 32, 32, 512) 2359808 _________________________________________________________________ batch_normalization_9 (Batch (None, 32, 32, 512) 2048 _________________________________________________________________ activation_10 (Activation) (None, 32, 32, 512) 0 _________________________________________________________________ max_pooling2d_4 (MaxPooling2 (None, 16, 16, 512) 0 _________________________________________________________________ conv2d_11 (Conv2D) (None, 16, 16, 512) 2359808 _________________________________________________________________ batch_normalization_10 (Batc (None, 16, 16, 512) 2048 _________________________________________________________________ activation_11 (Activation) (None, 16, 16, 512) 0 _________________________________________________________________ conv2d_12 (Conv2D) (None, 16, 16, 512) 2359808 _________________________________________________________________ batch_normalization_11 (Batc (None, 16, 16, 512) 2048 _________________________________________________________________ activation_12 (Activation) (None, 16, 16, 512) 0 _________________________________________________________________ conv2d_13 (Conv2D) (None, 16, 16, 512) 2359808 _________________________________________________________________ batch_normalization_12 (Batc (None, 16, 16, 512) 2048 _________________________________________________________________ activation_13 (Activation) (None, 16, 16, 512) 0 _________________________________________________________________ max_pooling2d_5 (MaxPooling2 (None, 8, 8, 512) 0 _________________________________________________________________ up_sampling2d_1 (UpSampling2 (None, 16, 16, 512) 0 _________________________________________________________________ conv2d_14 (Conv2D) (None, 16, 16, 512) 2359808 _________________________________________________________________ batch_normalization_13 (Batc (None, 16, 16, 512) 2048 _________________________________________________________________ activation_14 (Activation) (None, 16, 16, 512) 0 _________________________________________________________________ conv2d_15 (Conv2D) (None, 16, 16, 512) 2359808 _________________________________________________________________ batch_normalization_14 (Batc (None, 16, 16, 512) 2048 _________________________________________________________________ activation_15 (Activation) (None, 16, 16, 512) 0 _________________________________________________________________ conv2d_16 (Conv2D) (None, 16, 16, 512) 2359808 _________________________________________________________________ batch_normalization_15 (Batc (None, 16, 16, 512) 2048 _________________________________________________________________ activation_16 (Activation) (None, 16, 16, 512) 0 _________________________________________________________________ up_sampling2d_2 (UpSampling2 (None, 32, 32, 512) 0 _________________________________________________________________ conv2d_17 (Conv2D) (None, 32, 32, 512) 2359808 _________________________________________________________________ batch_normalization_16 (Batc (None, 32, 32, 512) 2048 _________________________________________________________________ activation_17 (Activation) (None, 32, 32, 512) 0 _________________________________________________________________ conv2d_18 (Conv2D) (None, 32, 32, 512) 2359808 _________________________________________________________________ batch_normalization_17 (Batc (None, 32, 32, 512) 2048 _________________________________________________________________ activation_18 (Activation) (None, 32, 32, 512) 0 _________________________________________________________________ conv2d_19 (Conv2D) (None, 32, 32, 256) 1179904 _________________________________________________________________ batch_normalization_18 (Batc (None, 32, 32, 256) 1024 _________________________________________________________________ activation_19 (Activation) (None, 32, 32, 256) 0 _________________________________________________________________ up_sampling2d_3 (UpSampling2 (None, 64, 64, 256) 0 _________________________________________________________________ conv2d_20 (Conv2D) (None, 64, 64, 256) 590080 _________________________________________________________________ batch_normalization_19 (Batc (None, 64, 64, 256) 1024 _________________________________________________________________ activation_20 (Activation) (None, 64, 64, 256) 0 _________________________________________________________________ conv2d_21 (Conv2D) (None, 64, 64, 256) 590080 _________________________________________________________________ batch_normalization_20 (Batc (None, 64, 64, 256) 1024 _________________________________________________________________ activation_21 (Activation) (None, 64, 64, 256) 0 _________________________________________________________________ conv2d_22 (Conv2D) (None, 64, 64, 128) 295040 _________________________________________________________________ batch_normalization_21 (Batc (None, 64, 64, 128) 512 _________________________________________________________________ activation_22 (Activation) (None, 64, 64, 128) 0 _________________________________________________________________ up_sampling2d_4 (UpSampling2 (None, 128, 128, 128) 0 _________________________________________________________________ conv2d_23 (Conv2D) (None, 128, 128, 128) 147584 _________________________________________________________________ batch_normalization_22 (Batc (None, 128, 128, 128) 512 _________________________________________________________________ activation_23 (Activation) (None, 128, 128, 128) 0 _________________________________________________________________ conv2d_24 (Conv2D) (None, 128, 128, 64) 73792 _________________________________________________________________ batch_normalization_23 (Batc (None, 128, 128, 64) 256 _________________________________________________________________ activation_24 (Activation) (None, 128, 128, 64) 0 _________________________________________________________________ up_sampling2d_5 (UpSampling2 (None, 256, 256, 64) 0 _________________________________________________________________ conv2d_25 (Conv2D) (None, 256, 256, 64) 36928 _________________________________________________________________ batch_normalization_24 (Batc (None, 256, 256, 64) 256 _________________________________________________________________ activation_25 (Activation) (None, 256, 256, 64) 0 _________________________________________________________________ conv2d_26 (Conv2D) (None, 256, 256, 1) 65 _________________________________________________________________ batch_normalization_25 (Batc (None, 256, 256, 1) 4 _________________________________________________________________ reshape_1 (Reshape) (None, 65536, 1) 0 _________________________________________________________________ activation_26 (Activation) (None, 65536, 1) 0 ================================================================= Total params: 29,456,773 Trainable params: 29,441,411 Non-trainable params: 15,362 _________________________________________________________________ None
print(autoencoder.summary())... - MaxU