import keras
import keras.backend as K
from keras import layers
from keras.layers import *
from keras.models import *
def identity_block(input_tensor, kernel_size, filters, stage, block):
filters1, filters2, filters3 = filters
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
x = conv2d(filters1, (1, 1), name=conv_name_base + '2a')(input_tensor)
x = batchnormalization(name=bn_name_base + '2a')(x)
x = activation('relu')(x)
x = conv2d(filters2, kernel_size,padding='same', name=conv_name_base + '2b')(x)
x = batchnormalization(name=bn_name_base + '2b')(x)
x = activation('relu')(x)
x = conv2d(filters3, (1, 1), name=conv_name_base + '2c')(x)
x = batchnormalization(name=bn_name_base + '2c')(x)
x = layers.add([x, input_tensor])
x = activation('relu')(x)
return x
def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)):
filters1, filters2, filters3 = filters
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
x = Conv2D(filters1, (1, 1), strides=strides,
name=conv_name_base + '2a')(input_tensor)
x = BatchNormalization(name=bn_name_base + '2a')(x)
x = Activation('relu')(x)
x = Conv2D(filters2, kernel_size, padding='same',
name=conv_name_base + '2b')(x)
x = BatchNormalization(name=bn_name_base + '2b')(x)
x = Activation('relu')(x)
x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x)
x = BatchNormalization(name=bn_name_base + '2c')(x)
shortcut = Conv2D(filters3, (1, 1), strides=strides,
name=conv_name_base + '1')(input_tensor)
shortcut = BatchNormalization(name=bn_name_base + '1')(shortcut)
x = layers.add([x, shortcut])
x = Activation('relu')(x)
return x
def get_resnet50_encoder(input_height=224, input_width=224):
img_input = Input([input_height, input_width, 3])
# 416,416,3 -> 208,208,64
x = ZeroPadding2D((3, 3))(img_input)
x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)
x = BatchNormalization(name='bn_conv1')(x)
x = Activation('relu')(x)
f1 = x
# 208,208,64 -> 104,104,64 -> 104,104,256
x = MaxPooling2D((3, 3), strides=(2, 2), padding="same")(x)
x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
# f2是hw方向压缩两次的结果
f2 = x
# 104,104,256 -> 52,52,512
x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')
# f3是hw方向压缩三次的结果
f3 = x
# 52,52,512 -> 26,26,1024
x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')
# f4是hw方向压缩四次的结果
f4 = x
# 26,26,1024 -> 13,13,2048
x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')
# f5是hw方向压缩五次的结果
f5 = x
return img_input, [f1 , f2 , f3 , f4 , f5]
import keras
import keras.backend as K
是灰色的,conv2d、batchnormalization等函数也是Unresolved reference状态。
环境配置:
python3.6
tensorflow-gpu:1.13.2
keras:2.1.5
scipy:1.2.1
numpy:1.17.0
Keras:2.1.5
matplotlib:3.1.2
opencv_python:4.1.2.30
tensorflow_gpu:1.13.2
tqdm:4.60.0
Pillow:8.2.0
h5py:2.10.0
是 直接 python 你的文件.py 报错,还是 IDE 中报错
建议检查一下
1 查看 开发机器有几个python
2 查看 path 里的python 目录指向
3 ide 的python 解释器指向
把上面3个问题查清楚后, 去检查对应的 python 是否正确安装了相关库
另外, 确保你工作目录里的py文件, 没有采用和库名一样的命名.