forked from flong998/badouai-ai-special-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
import numpy as np | ||
from keras import layers | ||
from keras.layers import Conv2D,Dense,BatchNormalization,Activation,Input,concatenate,AveragePooling2D,MaxPooling2D,GlobalAveragePooling2D | ||
from keras.applications.imagenet_utils import decode_predictions | ||
from keras.models import Model | ||
from keras.preprocessing import image | ||
|
||
|
||
def conv2d_bn(x,filters,num_row,num_col,strides=(1,1),padding='same',name=None): | ||
if name is not None: | ||
bn_name = name + '_bn' | ||
conv_name = name + '_conv' | ||
elif name is None: | ||
bn_name = None | ||
conv_name = None | ||
|
||
|
||
x = Conv2D(filters,(num_row,num_col),strides=strides,padding=padding,use_bias=False,name=conv_name)(x) | ||
x = BatchNormalization(scale=False, name=bn_name)(x) # scale-是否进行缩放 | ||
x = Activation('relu',name=name)(x) | ||
return x | ||
|
||
def InceptionV3(inputs=[299,299,3],classes=1000): | ||
input_image = Input(shape=inputs) | ||
|
||
# 在进行block之前,先提取公用特征。不是所有特征都要区分尺度的,Inception分叉的主要目的是提取不同尺度的特征。 | ||
# output_size = 149*149*32 | ||
x = conv2d_bn(input_image,32,3,3,strides=(2,2),padding='valid') | ||
# output_size = 147*147*32 | ||
x = conv2d_bn(x,32,3,3,padding='valid') | ||
# output_size = 147*147*64 | ||
x = conv2d_bn(x, 64, 3, 3) | ||
# output_size = 73*73*64 | ||
x = MaxPooling2D((3, 3), strides=(2, 2))(x) | ||
# output_size = 71*71*80 | ||
x = conv2d_bn(x, 80, 1, 1, padding='valid') | ||
# output_size = 69*69*192 | ||
x = conv2d_bn(x, 192, 3, 3, padding='valid') | ||
# output_size = 35*35*192 | ||
x = MaxPooling2D((3, 3), strides=(2, 2))(x) | ||
|
||
# --------------------------------# | ||
# Block1 35x35 | ||
# --------------------------------# | ||
# Block1 part1 | ||
# 35 x 35 x 192 -> 35 x 35 x 256 | ||
branch1x1 = conv2d_bn(x, 64, 1, 1) # 1*1 卷积 | ||
|
||
branch5x5 = conv2d_bn(x, 48, 1, 1) # branch1x1和branch5x5 是并列关系,因为输入都是x | ||
branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) # 标准是3*3,但我们训练用的是imagenet数据集,用5*5,效果会更好。因为imagenet里特别小尺度的图片相对来说少。 | ||
|
||
branch3x3dbl = conv2d_bn(x, 64, 1, 1) # branch1x1和branch5x5和branch3x3dbl 是并列关系,因为输入都是x | ||
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) | ||
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) # 2个3*3=1个5*5 | ||
|
||
branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) | ||
branch_pool = conv2d_bn(branch_pool, 32, 1, 1) | ||
|
||
# CONCAT-合并 通道数:64+64+96+32 = 256 | ||
x = concatenate( | ||
[branch1x1, branch5x5, branch3x3dbl, branch_pool], | ||
axis=3, # 按channel合并。tensor的排列方式-nhwc | ||
name='mixed0') | ||
|
||
# Block1 part2 | ||
# 35 x 35 x 256 -> 35 x 35 x 288 | ||
branch1x1 = conv2d_bn(x, 64, 1, 1) | ||
|
||
branch5x5 = conv2d_bn(x, 48, 1, 1) | ||
branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) | ||
|
||
branch3x3dbl = conv2d_bn(x, 64, 1, 1) | ||
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) | ||
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) | ||
|
||
branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) | ||
branch_pool = conv2d_bn(branch_pool, 64, 1, 1) | ||
|
||
# 64+64+96+64 = 288 | ||
x = concatenate( | ||
[branch1x1, branch5x5, branch3x3dbl, branch_pool], | ||
axis=3, | ||
name='mixed1') | ||
|
||
# Block1 part3 | ||
# 35 x 35 x 288 -> 35 x 35 x 288 | ||
branch1x1 = conv2d_bn(x, 64, 1, 1) | ||
|
||
branch5x5 = conv2d_bn(x, 48, 1, 1) | ||
branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) | ||
|
||
branch3x3dbl = conv2d_bn(x, 64, 1, 1) | ||
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) | ||
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) | ||
|
||
branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) | ||
branch_pool = conv2d_bn(branch_pool, 64, 1, 1) | ||
|
||
# 64+64+96+64 = 288 | ||
x = concatenate( | ||
[branch1x1, branch5x5, branch3x3dbl, branch_pool], | ||
axis=3, | ||
name='mixed2') | ||
|
||
# --------------------------------# | ||
# Block2 17x17 | ||
# --------------------------------# | ||
# Block2 part1 | ||
# 35 x 35 x 288 -> 17 x 17 x 768 | ||
branch3x3 = conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding='valid') | ||
|
||
branch3x3dbl = conv2d_bn(x, 64, 1, 1) | ||
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) | ||
branch3x3dbl = conv2d_bn( | ||
branch3x3dbl, 96, 3, 3, strides=(2, 2), padding='valid') | ||
|
||
branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) | ||
x = concatenate( | ||
[branch3x3, branch3x3dbl, branch_pool], axis=3, name='mixed3') | ||
|
||
# Block2 part2 | ||
# 17 x 17 x 768 -> 17 x 17 x 768 | ||
branch1x1 = conv2d_bn(x, 192, 1, 1) | ||
|
||
branch7x7 = conv2d_bn(x, 128, 1, 1) | ||
branch7x7 = conv2d_bn(branch7x7, 128, 1, 7) | ||
branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) | ||
|
||
branch7x7dbl = conv2d_bn(x, 128, 1, 1) | ||
branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1) | ||
branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 1, 7) # 7*7 | ||
branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1) | ||
branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) # 7*7。2个7*7代替1个9*7 | ||
|
||
branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) | ||
branch_pool = conv2d_bn(branch_pool, 192, 1, 1) | ||
x = concatenate( | ||
[branch1x1, branch7x7, branch7x7dbl, branch_pool], | ||
axis=3, | ||
name='mixed4') | ||
|
||
# Block2 part3 and part4 | ||
# 17 x 17 x 768 -> 17 x 17 x 768 -> 17 x 17 x 768 | ||
for i in range(2): | ||
branch1x1 = conv2d_bn(x, 192, 1, 1) | ||
|
||
branch7x7 = conv2d_bn(x, 160, 1, 1) | ||
branch7x7 = conv2d_bn(branch7x7, 160, 1, 7) | ||
branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) | ||
|
||
branch7x7dbl = conv2d_bn(x, 160, 1, 1) | ||
branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1) | ||
branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7) | ||
branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1) | ||
branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) | ||
|
||
branch_pool = AveragePooling2D( | ||
(3, 3), strides=(1, 1), padding='same')(x) | ||
branch_pool = conv2d_bn(branch_pool, 192, 1, 1) | ||
x = concatenate( | ||
[branch1x1, branch7x7, branch7x7dbl, branch_pool], | ||
axis=3, | ||
name='mixed' + str(5 + i)) | ||
|
||
# Block2 part5 | ||
# 17 x 17 x 768 -> 17 x 17 x 768 | ||
branch1x1 = conv2d_bn(x, 192, 1, 1) | ||
|
||
branch7x7 = conv2d_bn(x, 192, 1, 1) | ||
branch7x7 = conv2d_bn(branch7x7, 192, 1, 7) | ||
branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) | ||
|
||
branch7x7dbl = conv2d_bn(x, 192, 1, 1) | ||
branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1) | ||
branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) | ||
branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1) | ||
branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) | ||
|
||
branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) | ||
branch_pool = conv2d_bn(branch_pool, 192, 1, 1) | ||
x = concatenate( | ||
[branch1x1, branch7x7, branch7x7dbl, branch_pool], | ||
axis=3, | ||
name='mixed7') | ||
|
||
# --------------------------------# | ||
# Block3 8x8 | ||
# --------------------------------# | ||
# Block3 part1 | ||
# 17 x 17 x 768 -> 8 x 8 x 1280 | ||
branch3x3 = conv2d_bn(x, 192, 1, 1) | ||
branch3x3 = conv2d_bn(branch3x3, 320, 3, 3, | ||
strides=(2, 2), padding='valid') | ||
|
||
branch7x7x3 = conv2d_bn(x, 192, 1, 1) | ||
branch7x7x3 = conv2d_bn(branch7x7x3, 192, 1, 7) | ||
branch7x7x3 = conv2d_bn(branch7x7x3, 192, 7, 1) | ||
branch7x7x3 = conv2d_bn( | ||
branch7x7x3, 192, 3, 3, strides=(2, 2), padding='valid') | ||
|
||
branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) | ||
x = concatenate( | ||
[branch3x3, branch7x7x3, branch_pool], axis=3, name='mixed8') | ||
|
||
# Block3 part2 part3 | ||
# 8 x 8 x 1280 -> 8 x 8 x 2048 -> 8 x 8 x 2048 | ||
for i in range(2): | ||
branch1x1 = conv2d_bn(x, 320, 1, 1) | ||
|
||
branch3x3 = conv2d_bn(x, 384, 1, 1) | ||
branch3x3_1 = conv2d_bn(branch3x3, 384, 1, 3) | ||
branch3x3_2 = conv2d_bn(branch3x3, 384, 3, 1) | ||
branch3x3 = concatenate( | ||
[branch3x3_1, branch3x3_2], axis=3, name='mixed9_' + str(i)) | ||
|
||
branch3x3dbl = conv2d_bn(x, 448, 1, 1) | ||
branch3x3dbl = conv2d_bn(branch3x3dbl, 384, 3, 3) | ||
branch3x3dbl_1 = conv2d_bn(branch3x3dbl, 384, 1, 3) | ||
branch3x3dbl_2 = conv2d_bn(branch3x3dbl, 384, 3, 1) | ||
branch3x3dbl = concatenate( | ||
[branch3x3dbl_1, branch3x3dbl_2], axis=3) | ||
|
||
branch_pool = AveragePooling2D( | ||
(3, 3), strides=(1, 1), padding='same')(x) | ||
branch_pool = conv2d_bn(branch_pool, 192, 1, 1) | ||
x = concatenate( | ||
[branch1x1, branch3x3, branch3x3dbl, branch_pool], | ||
axis=3, | ||
name='mixed' + str(9 + i)) | ||
# 平均池化后全连接。 | ||
x = GlobalAveragePooling2D()(x) | ||
x = Dense(classes,activation='softmax', name='predictions')(x) | ||
|
||
model = Model(input_image,x,name='inception_v3') | ||
return model | ||
|
||
def preprocess_input(x): | ||
x /= 255. | ||
x -= 0.5 | ||
x *= 2. | ||
return x | ||
|
||
if __name__=='__main__': | ||
model = InceptionV3() | ||
model.load_weights('E:/YAN/HelloWorld/cv/【12】图像识别/代码/inceptionV3_tf/inception_v3_weights_tf_dim_ordering_tf_kernels.h5') | ||
img = image.load_img('E:/YAN/HelloWorld/cv/【12】图像识别/代码/inceptionV3_tf/elephant.jpg',target_size=(299,299)) | ||
img = image.img_to_array(img) | ||
img = np.expand_dims(img,axis=0) | ||
img = preprocess_input(img) | ||
preds = model.predict(img) | ||
print('Predicted:',decode_predictions(preds,1)) |