Масштаб не работает Керас - PullRequest
0 голосов
/ 10 мая 2018

Я пытаюсь собрать и обучить версию SegNet из здесь в Керасе и получить ValueError во время обучения. Мои данные о тренировках имеют вид:

RGB изображения:

train.shape

(382,200,200,3)

Маски с одноразовыми метками для 3 региональных классов:

label.shape

(382,200,200,3)

Модель выглядит следующим образом:

inshape=(200, 200, 3)
classes=3

# c.f. https://github.com/alexgkendall/SegNet-Tutorial/blob/master/Example_Models/bayesian_segnet_camvid.prototxt
img_input = Input(shape=inshape)
x = img_input
# Encoder
x = Conv2D(64, (3, 3), padding="same")(x)
print(x.shape)
#x = BatchNormalization()(x)
x = Activation("relu")(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
print(x.shape)

x = Conv2D(128, (3, 3), padding="same")(x)
print(x.shape)
#x = BatchNormalization()(x)
x = Activation("relu")(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
print(x.shape)

x = Conv2D(256, (3, 3), padding="same")(x)
print(x.shape)
#x = BatchNormalization()(x)
x = Activation("relu")(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
print(x.shape)

x = Conv2D(512, (3, 3), padding="same")(x)
print(x.shape)
#x = BatchNormalization()(x)
x = Activation("relu")(x)

# Decoder
x = Conv2D(512, (3, 3), padding="same")(x)
print(x.shape)
#x = BatchNormalization()(x)
x = Activation("relu")(x)

x = UpSampling2D(size=(2, 2))(x)
print(x.shape)
x = Conv2D(256, (3, 3), padding="same")(x)
print(x.shape)
#x = BatchNormalization()(x)
x = Activation("relu")(x)

x = UpSampling2D(size=(2, 2))(x)
print(x.shape)
x = Conv2D(128, (3, 3), padding="same")(x)
print(x.shape)
#x = BatchNormalization()(x)
x = Activation("relu")(x)

x = UpSampling2D(size=(2, 2))(x)
print(x.shape)
x = Conv2D(128, (3, 3), padding="same")(x)
print(x.shape)
#x = BatchNormalization()(x)
x = Activation("relu")(x)

x = Convolution2D(classes, (1,1), padding="valid")(x)
print(x.shape)


#x = Dense(classes,activation='softmax')(x)

x = Reshape((inshape[0]*inshape[1],classes), input_shape = (200,200,3))(x)
#x = x.reshape((-1,40000,3))
print(x.shape)
x = Activation("softmax")(x)

model = Model(img_input, x)

В распечатке выписок:

(?, 200, 200, 64)
(?, 100, 100, 64)
(?, 100, 100, 128)
(?, 50, 50, 128)
(?, 50, 50, 256)
(?, 25, 25, 256)
(?, 25, 25, 512)
(?, 25, 25, 512)
(?, 50, 50, 512)
(?, 50, 50, 256)
(?, 100, 100, 256)
(?, 100, 100, 128)
(?, 200, 200, 128)
(?, 200, 200, 128)
(?, 200, 200, 3)
(?, 40000, 3)

Компиляция с:

model.compile(loss = 'categorical_crossentropy', optimizer = 'adadelta', metrics=["accuracy"])

работает нормально. Установка с:

model.fit(train,label,epochs = 1)

выдает следующую ошибку:

    ValueError                                Traceback (most recent call last)
<ipython-input-28-fd1c2ff82d80> in <module>()
----> 1 model.fit(train,label,epochs = 1)

c:\program files\python36\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1628             sample_weight=sample_weight,
   1629             class_weight=class_weight,
-> 1630             batch_size=batch_size)
   1631         # Prepare validation data.
   1632         do_validation = False

c:\program files\python36\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)

c:\program files\python36\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_60 to have 3 dimensions, but got array with shape (383, 200, 200, 3)

Любой совет будет высоко ценится.

1 Ответ

0 голосов
/ 11 мая 2018

Отвечая на мой собственный вопрос.

Я не изменял маски перед их передачей. На этапе обучения выходные данные сети сравниваются с маской.После изменения формы сетевой вывод был действительно (-1,40000,3), но маска не была изменена, чтобы соответствовать этому.

Изменение маски до правильного вывода устраняет проблему.

...