我正在尝试在数据集上训练AtrousFCN_VGG16_16s模型。模型架构为:
def AtrousFCN_Vgg16_16s(input_shape=(512, 512, 3),
weight_decay=0.,batch_momentum=0.9, batch_shape=None, classes=1):
if batch_shape:
img_input = Input(batch_shape=batch_shape)
image_size = batch_shape[1:3]
else:
img_input = Input(shape=input_shape)
image_size = input_shape[0:2]
# Block 1
conv1 = Conv2D(64, (3, 3), activation='relu', padding='same',
name='block1_conv1', kernel_regularizer=l2(weight_decay))(img_input)
conv1 = Conv2D(64, (3, 3), activation='relu', padding='same',
name='block1_conv2', kernel_regularizer=l2(weight_decay))(conv1)
pool1 = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(conv1)
#Block 2
conv2 = Conv2D(128, (3, 3), activation='relu', padding='same',
name='block2__
conv1', kernel_regularizer=l2(weight_decay))(pool1)
conv2 = Conv2D(128, (3, 3), activation='relu', padding='same',
name='block2__conv2', kernel_regularizer=l2(weight_decay))(conv2)
pool2 = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(conv2)
# Block 3
conv3 = Conv2D(256, (3, 3), activation='relu',padding='same',name='block3__
conv1', kernel_regularizer=l2(weight_decay))(pool2)
conv3 = Conv2D(256, (3, 3), activation='relu', padding='same',
name='block3__
conv2', kernel_regularizer=l2(weight_decay))(conv3)
conv3 = Conv2D(256, (3, 3), activation='relu', padding='same'name='block3__
conv3', kernel_regularizer=l2(weight_decay))(conv3)
pool3 = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(conv3)
# Block 4
conv4 = Conv2D(512, (3, 3), activation='relu',padding='same',name='block4__
conv1', kernel_regularizer=l2(weight_decay))(pool3)
conv4 = Conv2D(512, (3, 3), activation='relu',padding='same',name='block4__
conv2', kernel_regularizer=l2(weight_decay))(conv4)
conv4 = Conv2D(512, (3, 3), activation='relu',padding='same',name='block4__
conv3', kernel_regularizer=l2(weight_decay))(conv4)
pool4 = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(conv4)
# Block 5
conv5 = Conv2D(512, (3, 3), activation='relu',padding='same',name='block5__
conv1', kernel_regularizer=l2(weight_decay))(pool4)
conv5 = Conv2D(512, (3, 3), activation='relu',padding='same',name='block5__
conv2', kernel_regularizer=l2(weight_decay))(conv5)
conv5 = Conv2D(512, (3, 3), activation='relu',padding='same',name='block5__
conv3', kernel_regularizer=l2(weight_decay))(conv5)
# Convolutional layers transfered from fully-connected layers
fc1 = Conv2D(4096, (7, 7),
activation='relu',padding='same',dilation_rate(2, 2), name='fc1',
kernel_regularizer=l2(weight_decay))(conv5)
fc1 = Dropout(0.5)(fc1)
fc2 = Conv2D(4096, (1, 1), activation='relu', padding='same', name='fc2',
kernel_regularizer=l2(weight_decay))(fc1)
fc2 = Dropout(0.5)(fc2)
#classifying layer
out =
Conv2D(classes(1,1),kernel_initializer='he_normal',activation='sigmoid',
padding='valid', strides=(1, 1), kernel_regularizer=l2(weight_decay))(fc2)
out = UpSampling2D(size=(512, 512), interpolation = 'bilinear')(out)
model = Model(img_input, out)
return model
我收到的错误将我指向具有的度量函数:
def dice_coeff(y_true, y_pred):
#smooth = 1.
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
im_sum = K.sum(y_true_f + y_pred_f)
if im_sum == 0:
return 1
# Compute Dice coefficient
return 2. * intersection / im_sum
#score = (2. * intersection) / (K.sum(y_true_f) + K.sum(y_pred_f))
#return score
def dice_loss(y_true, y_pred):
loss = 1 - dice_coeff(y_true, y_pred)
return loss
这是错误:
Traceback (most recent call last):
File "code/slide_segmentation/train.py", line 80, in <module>
results = m.fit_generator(train_gen, epochs=50, steps_per_epoch =
(842//4),validation_data=val_gen, validation_steps=(211//4),
callbacks=callbacks_list)
File "/usr/local/lib/python3.5/dist-packages/keras/legacy/interfaces.py", line
91, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line
1418, in fit_generator
initial_epoch=initial_epoch)
File "/usr/local/lib/python3.5/dist-
packages/keras/engine/training_generator.py", line 217, in fit_generator
class_weight=class_weight)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line
1217, in train_on_batch
outputs = self.train_function(ins)
File "/usr/local/lib/python3.5/dist-
packages/keras/backend/tensorflow_backend.py", line 2715, in __call__
return self._call(inputs)
File "/usr/local/lib/python3.5/dist-
packages/keras/backend/tensorflow_backend.py", line 2675, in _call
fetched = self._callable_fn(*array_vals)
File "/usr/local/lib/python3.5/dist-
packages/tensorflow/python/client/session.py", line 1439, in __call__
run_metadata_ptr)
File "/usr/local/lib/python3.5/dist-
packages/tensorflow/python/framework/errors_impl.py", line 528, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible
shapes: [1048576] vs. [1073741824]
[[{{node metrics/dice_coeff/add}} = Add[T=DT_FLOAT,
_device="/job:localhost/replica:0/task:0/device:GPU:0"]
(metrics/dice_coeff/Reshape, metrics/dice_coeff/Reshape_1)]]
[[{{node loss/add_15/_283}} = _Recv[client_terminated=false,
recv_device="/job:localhost/replica:0/task:0/device:CPU:0",
send_device="/job:localhost/replica:0/task:0/device:GPU:0",
send_device_incarnation=1, tensor_name="edge_1954_loss/add_15",
tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
很明显,以下行是导致错误的原因:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible
shapes: [1048576] vs. [1073741824]
[[{{node metrics/dice_coeff/add}} = Add[T=DT_FLOAT,
_device="/job:localhost/replica:0/task:0/device:GPU:0"]
(metrics/dice_coeff/Reshape, metrics/dice_coeff/Reshape_1)]]
比较y_true和y_pred的形状时,我缺少1024的因子[1073741824/1048576]。这是体系结构本身的问题吗?我是否遗漏了一些明显的东西(形状错误的非常精确的性质使我相信自己是)。