1

我必须将密集网络中的 fc 层转换为 conv 层。下面是密集网络的架构。

# Dense Block
def denseblock(input, num_filter = 12, dropout_rate = 0.0):
    global compression
    temp = input
    for _ in range(l): 
        BatchNorm = layers.BatchNormalization()(temp)
        relu = layers.Activation('relu')(BatchNorm)
        Conv2D_3_3 = layers.Conv2D(int(num_filter*compression), (3,3), use_bias=False ,padding='same')(relu)
        if dropout_rate>0:
            Conv2D_3_3 = layers.Dropout(dropout_rate)(Conv2D_3_3)
        concat = layers.Concatenate(axis=-1)([temp,Conv2D_3_3])

        temp = concat

    return temp

## transition Block
def transition(input, num_filter = 12, dropout_rate = 0.0):
    global compression
    BatchNorm = layers.BatchNormalization()(input)
    relu = layers.Activation('relu')(BatchNorm)
    Conv2D_BottleNeck = layers.Conv2D(int(num_filter*compression), (1,1), use_bias=False ,padding='same')(relu)
    if dropout_rate>0:
         Conv2D_BottleNeck = layers.Dropout(dropout_rate)(Conv2D_BottleNeck)
    avg = layers.AveragePooling2D(pool_size=(2,2))(Conv2D_BottleNeck)
    return avg

#output layer
def output_layer(input):
    global compression
    BatchNorm = layers.BatchNormalization()(input)
    relu = layers.Activation('relu')(BatchNorm)
    AvgPooling = layers.AveragePooling2D(pool_size=(2,2))(relu)
    flat = layers.Flatten()(AvgPooling)
    output = layers.Dense(num_classes, activation='softmax')(flat)
    return output



input = layers.Input(shape=(img_height, img_width, channel,))
First_Conv2D = layers.Conv2D(num_filter, (3,3), use_bias=False ,padding='same')(input)

First_Block = denseblock(First_Conv2D, num_filter, dropout_rate)
First_Transition = transition(First_Block, num_filter, dropout_rate)

Second_Block = denseblock(First_Transition, num_filter, dropout_rate)
Second_Transition = transition(Second_Block, num_filter, dropout_rate)

Third_Block = denseblock(Second_Transition, num_filter, dropout_rate)
Third_Transition = transition(Third_Block, num_filter, dropout_rate)

Last_Block = denseblock(Third_Transition,  num_filter, dropout_rate)
output = output_layer(Last_Block)

我已经通过这些行更改了输出层的最后两行

fc2conv1 = layers.Conv2D(864, 2, 1, padding='valid')(AvgPooling)
output = layers.Conv2D(10, 1, 1, padding='valid')(fc2conv1)

但是在训练模型时出现错误。

ValueError: A target array with shape (5000, 10) was passed for an output of shape (None, 1, 1, 10) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output.

完整的错误跟踪

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-40-245431de828d> in <module>
      5     verbose = 1,
      6     validation_data=(x_val, y_val),
----> 7     callbacks = [lr_scheduler, csv_logger, model_chkpt, callbacks]
      8 )

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\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, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    726         max_queue_size=max_queue_size,
    727         workers=workers,
--> 728         use_multiprocessing=use_multiprocessing)
    729 
    730   def evaluate(self,

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, **kwargs)
    222           validation_data=validation_data,
    223           validation_steps=validation_steps,
--> 224           distribution_strategy=strategy)
    225 
    226       total_samples = _get_total_number_of_samples(training_data_adapter)

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py in _process_training_inputs(model, x, y, batch_size, epochs, sample_weights, class_weights, steps_per_epoch, validation_split, validation_data, validation_steps, shuffle, distribution_strategy, max_queue_size, workers, use_multiprocessing)
    562                                     class_weights=class_weights,
    563                                     steps=validation_steps,
--> 564                                     distribution_strategy=distribution_strategy)
    565     elif validation_steps:
    566       raise ValueError('`validation_steps` should not be specified if '

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py in _process_inputs(model, x, y, batch_size, epochs, sample_weights, class_weights, shuffle, steps, distribution_strategy, max_queue_size, workers, use_multiprocessing)
    592         batch_size=batch_size,
    593         check_steps=False,
--> 594         steps=steps)
    595   adapter = adapter_cls(
    596       x,

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset)
   2536           # Additional checks to avoid users mistakenly using improper loss fns.
   2537           training_utils.check_loss_and_target_compatibility(
-> 2538               y, self._feed_loss_fns, feed_output_shapes)
   2539 
   2540       # If sample weight mode has not been set and weights are None for all the

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_utils.py in check_loss_and_target_compatibility(targets, loss_fns, output_shapes)
    741           raise ValueError('A target array with shape ' + str(y.shape) +
    742                            ' was passed for an output of shape ' + str(shape) +
--> 743                            ' while using as loss `' + loss_name + '`. '
    744                            'This loss expects targets to have the same shape '
    745                            'as the output.')

ValueError: A target array with shape (5000, 10) was passed for an output of shape (None, 1, 1, 10) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output.
4

1 回答 1

0

如果您正在执行分类问题,您的输出层必须是 2D 的,更准确地说,它必须具有维度 (n_batches, n_class)。尝试添加一个展平层以返回 2D:

fc2conv1 = layers.Conv2D(864, 2, 1, padding='valid')(AvgPooling)
flatten = layers.Flatten()(fc2conv1)
output = layers.Dense(num_classes, activation='softmax')(flatten)
于 2020-05-13T06:59:25.653 回答