我的身材有问题,我该如何解决?我该怎么办?
这是错误消息:
File "run_demo.py", line 116, in main
model = get_crfrnn_model_def()
File "./src/crfrnn_model.py", line 70, in get_crfrnn_model_def
score_fused = Add()([score2, score_pool4c])
ValueError: Operands could not be broadcast together with shapes (3, 34, 4) (3, 34, 3)
代码如下:
def get_crfrnn_model_def():
channels, height, weight = 3, 500, 500
# Input
input_shape = (height, weight, 3)
img_input = Input(shape=input_shape)
# Add plenty of zero padding
x = ZeroPadding2D(padding=(100, 100))(img_input)
# VGG-16 convolution block 1
x = Conv2D(64, (3, 3), activation='relu', padding='valid', name='conv1_1')(x)
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_2')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool1')(x)
# VGG-16 convolution block 2
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_1')(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_2')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool2', padding='same')(x)
# VGG-16 convolution block 3
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_1')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_2')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_3')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool3', padding='same')(x)
pool3 = x
# VGG-16 convolution block 4
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_1')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_2')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_3')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool4', padding='same')(x)
pool4 = x
# VGG-16 convolution block 5
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_1')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_2')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_3')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool5', padding='same')(x)
# Fully-connected layers converted to convolution layers
x = Conv2D(4096, (7, 7), activation='relu', padding='valid', name='fc6')(x)
x = Dropout(0.5)(x)
x = Conv2D(4096, (1, 1), activation='relu', padding='valid', name='fc7')(x)
x = Dropout(0.5)(x)
x = Conv2D(3, (1, 1), padding='valid', name='score-fr')(x)
print(x)
# Deconvolution
score2 = Conv2DTranspose(3, (4, 4), strides=2, name='score2')(x)
print(score2)
# Skip connections from pool4
score_pool4 = Conv2D(3, (1, 1), name='score-pool4')(pool4)
score_pool4c = Cropping2D((5, 5),name='score-pool4c')(score_pool4)
print('asd')
print(score_pool4c)
score_fused = Add()([score2, score_pool4c])
score4 = Conv2DTranspose(3, (4, 4), strides=2, name='score4', use_bias=False)(score_fused)
# Skip connections from pool3
score_pool3 = Conv2D(3, (1, 1), name='score-pool3')(pool3)
score_pool3c = Cropping2D((9, 9))(score_pool3)
# Fuse things together
score_final = Add()([score4, score_pool3c])
# Final up-sampling and cropping
upsample = Conv2DTranspose(3, (16, 16), strides=8, name='upsample', use_bias=False)(score_final)
upscore = Cropping2D(((31, 37), (31, 37)))(upsample)
output = CrfRnnLayer(image_dims=(height, weight),
num_classes=3,
theta_alpha=160.,
theta_beta=3.,
theta_gamma=3.,
num_iterations=10,
name='crfrnn')([upscore, img_input]
model = Model(img_input, output, name='crfrnn_net')
return model```
'''
def get_crfrnn_model_def():
channels, height, weight = 3, 500, 500
input_shape = (3,height, weight)
img_input = Input(shape=input_shape)
# Add plenty of zero padding
x = ZeroPadding2D(padding=(100, 100))(img_input)
# VGG-16 convolution block 1
x = Conv2D(64, (3, 3), activation='relu', padding='valid', name='conv1_1')(x)
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_2')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool1')(x)
# VGG-16 convolution block 2
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_1')(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_2')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool2', padding='same')(x)
# VGG-16 convolution block 3
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_1')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_2')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_3')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool3', padding='same')(x)
pool3 = x
# VGG-16 convolution block 4
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_1')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_2')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_3')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool4', padding='same')(x)
pool4 = x
# VGG-16 convolution block 5
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_1')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_2')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_3')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool5', padding='same')(x)
# Fully-connected layers converted to convolution layers
x = Conv2D(4096, (7, 7), activation='relu', padding='valid', name='fc6')(x)
x = Dropout(0.5)(x)
x = Conv2D(4096, (1, 1), activation='relu', padding='valid', name='fc7')(x)
x = Dropout(0.5)(x)
x = Conv2D(3, (1, 1), padding='valid', name='score-fr')(x)
# Deconvolution
score2 = Conv2DTranspose(3, (4, 4), strides=2, name='score2')(x)
# Skip connections from pool4
score_pool4 = Conv2D(3, (1, 1), name='score-pool4')(pool4)
score_pool4c = Cropping2D((5, 5))(score_pool4)
score_fused = Add()([score2, score_pool4c])
score4 = Conv2DTranspose(3, (4, 4), strides=2, name='score4', use_bias=False)(score_fused)
# Skip connections from pool3
score_pool3 = Conv2D(3, (1, 1), name='score-pool3')(pool3)
score_pool3c = Cropping2D((9, 9))(score_pool3)
# Fuse things together
score_final = Add()([score4, score_pool3c])
# Final up-sampling and cropping
upsample = Conv2DTranspose(3, (16, 16), strides=8, name='upsample', use_bias=False)(score_final)
upscore = Cropping2D(((31, 37), (31, 37)))(upsample)
output = CrfRnnLayer(image_dims=(height, weight),
num_classes=3,
theta_alpha=160.,
theta_beta=3.,
theta_gamma=3.,
num_iterations=10,
name='crfrnn')([upscore, img_input])
# Build the model
model = Model(img_input, output, name='crfrnn_net')
model.summary()
return model
get_crfrnn_model_def()
我的主文件:
def getImageArr(path, width, height, imgNorm="sub_mean", odering='channels_first'):
try:
img = cv2.imread(path, 1)
if imgNorm == "sub_and_divide":
img = np.float32(cv2.resize(img, (width, height))) / 127.5 - 1
elif imgNorm == "sub_mean":
img = cv2.resize(img, (width, height))
img = img.astype(np.float32)
img[:, :, 0] -= 103.939
img[:, :, 1] -= 116.779
img[:, :, 2] -= 123.68
elif imgNorm == "divide":
img = cv2.resize(img, (width, height))
img = img.astype(np.float32)
img = img / 255.0
if odering == 'channels_first':
img = np.rollaxis(img, 2, 0)
return img
except Exception as e:
print(path, e)
img = np.zeros((height, width, 3))
if odering == 'channels_first':
img = np.rollaxis(img, 2, 0)
return img
def getSegmentationArr(path, nClasses, width, height):
seg_labels = np.zeros((height, width, nClasses))
try:
img = cv2.imread(path, 1)
img = cv2.resize(img, (width, height))
img = img[:, :, 0]
for c in range(nClasses):
seg_labels[:, :, c] = (img == c).astype(int)
except Exception as e:
print(e)
seg_labels = np.reshape(seg_labels, (width * height, nClasses))
return seg_labels
def imageSegmentationGenerator(images_path, segs_path, batch_size, n_classes, input_height, input_width, output_height,
output_width):
images = glob.glob(images_path + "*.jpg")
images.sort()
segmentations = glob.glob(segs_path + "*.jpg") + glob.glob(segs_path + "*.png") + glob.glob(segs_path + "*.jpeg")
segmentations.sort()
assert len(images) == len(segmentations)
zipped =itertools.cycle(zip(images, segmentations))
while True:
X = []
Y = []
for _ in range(batch_size):
im,seg = next(zipped,(None,None))
X.append(getImageArr(im, input_width, input_height))
Y.append(getSegmentationArr(seg, n_classes, output_width, output_height))
def main():
input_file = 'image.jpg'
output_file = 'labels.png'
saved_model_path = 'crfrnn_keras_model.h5'
m = get_crfrnn_model_def()
parser = argparse.ArgumentParser()
args = parser.parse_known_args()[0]
parser.add_argument("--epoch_number", type = int, default = 1 )
parser.add_argument("--test_images", type = str , default = "/home/qwe/Downloads/leaf-image-segmentation-segnet-master/predict/0.png")
parser.add_argument("--output_path", type = str , default = "/home/qwe/Downloads/leaf-image-segmentation-segnet-master/predictans")
parser.add_argument("--input_height", type=int , default = 500 )
parser.add_argument("--input_width", type=int , default = 500 )
parser.add_argument("--optimizer_name", type=str, default="adadelta")
m.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
G = imageSegmentationGenerator('xtrain/', 'ytrain/', 8,3,
input_height=500, input_width=500, output_height=500, output_width=500)
for ep in range(1):
m.fit_generator(G, 100, epochs=1)
if __name__ == '__main__':
main()
我的主文件:
def getImageArr(path, width, height, imgNorm="sub_mean", odering='channels_first'):
try:
img = cv2.imread(path, 1)
if imgNorm == "sub_and_divide":
img = np.float32(cv2.resize(img, (width, height))) / 127.5 - 1
elif imgNorm == "sub_mean":
img = cv2.resize(img, (width, height))
img = img.astype(np.float32)
img[:, :, 0] -= 103.939
img[:, :, 1] -= 116.779
img[:, :, 2] -= 123.68
elif imgNorm == "divide":
img = cv2.resize(img, (width, height))
img = img.astype(np.float32)
img = img / 255.0
if odering == 'channels_first':
img = np.rollaxis(img, 2, 0)
return img
except Exception as e:
print(path, e)
img = np.zeros((height, width, 3))
if odering == 'channels_first':
img = np.rollaxis(img, 2, 0)
return img
def getSegmentationArr(path, nClasses, width, height):
seg_labels = np.zeros((height, width, nClasses))
try:
img = cv2.imread(path, 1)
img = cv2.resize(img, (width, height))
img = img[:, :, 0]
for c in range(nClasses):
seg_labels[:, :, c] = (img == c).astype(int)
except Exception as e:
print(e)
seg_labels = np.reshape(seg_labels, (width * height, nClasses))
return seg_labels
def imageSegmentationGenerator(images_path, segs_path, batch_size, n_classes, input_height, input_width, output_height,
output_width):
images = glob.glob(images_path + "*.jpg")
images.sort()
segmentations = glob.glob(segs_path + "*.jpg") + glob.glob(segs_path + "*.png") + glob.glob(segs_path + "*.jpeg")
segmentations.sort()
assert len(images) == len(segmentations)
zipped =itertools.cycle(zip(images, segmentations))
while True:
X = []
Y = []
for _ in range(batch_size):
im,seg = next(zipped,(None,None))
X.append(getImageArr(im, input_width, input_height))
Y.append(getSegmentationArr(seg, n_classes, output_width, output_height))
def main():
input_file = 'image.jpg'
output_file = 'labels.png'
saved_model_path = 'crfrnn_keras_model.h5'
m = get_crfrnn_model_def()
parser = argparse.ArgumentParser()
args = parser.parse_known_args()[0]
parser.add_argument("--epoch_number", type = int, default = 1 )
parser.add_argument("--test_images", type = str , default = "/home/qwe/Downloads/leaf-image-segmentation-segnet-master/predict/0.png")
parser.add_argument("--output_path", type = str , default = "/home/qwe/Downloads/leaf-image-segmentation-segnet-master/predictans")
parser.add_argument("--input_height", type=int , default = 500 )
parser.add_argument("--input_width", type=int , default = 500 )
parser.add_argument("--optimizer_name", type=str, default="adadelta")
m.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
G = imageSegmentationGenerator('xtrain/', 'ytrain/', 8,3,
input_height=500, input_width=500, output_height=500, output_width=500)
for ep in range(1):
m.fit_generator(G, 100, epochs=1)
if __name__ == '__main__':
main()
答案 0 :(得分:0)
该错误指出,对于Add()
操作,形状必须相等。它们是:“(3,34,4)(3,34,3)”
例如,在以下位置更改内核大小:
score_pool4 = Conv2D(3, (1, 1), name='score-pool4')(pool4)
从3到4,应解决此特定问题。