链接:https://pan.baidu.com/s/1mTqblxzWcYIRF7_kk8MQQA
提取码:7x6w
资料的下载真的很感谢(14条消息) 【中文】【吴恩达课后编程作业】Course 4 - 卷积神经网络 - 第二周作业_何宽的博客-CSDN博客
【博主使用的python版本:3.6.8】
对于此作业,您将使用 Keras。
在进入问题之前,请运行下面的单元格以加载所需的包。
import tensorflow as tf
import numpy as np
import scipy.misc
from tensorflow.keras.applications.resnet_v2 import ResNet50V2
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet_v2 import preprocess_input, decode_predictions
from tensorflow.keras import layers
from tensorflow.keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, GlobalMaxPooling2D
from tensorflow.keras.models import Model, load_model
from resnets_utils import *
from tensorflow.keras.initializers import random_uniform, glorot_uniform, constant, identity
from tensorflow.python.framework.ops import EagerTensor
from matplotlib.pyplot import imshow
from test_utils import summary, comparator
import public_tests


恒等块是 ResNet 中使用的标准块,对应于输入激活(例如a[L])与输出激活(例如a[L+!])具有相同维度的情况。为了充实 ResNet 身份块中发生的不同步骤,下面是一个显示各个步骤的替代图表:

上面的路径是“捷径”。较低的路径是“主要路径”。在此图中,请注意每层中的 CONV2D 和 ReLU 步骤。为了加快训练速度,添加了 BatchNorm 步骤。不要担心这很难实现 - 你会看到BatchNorm只是Keras中的一行代码!
在本练习中,您将实际实现此标识块的一个功能稍微强大的版本,其中跳过连接“跳过”3 个隐藏层而不是 2 个层。它看起来像这样:

主路径的第一部分:
kernel_initializer = initializer(seed=0).主路径的第二部分:
主路径的第三部分:
主路径的最后一部分:
接下来我们就要实现残差网络的恒等块了
我们已将初始值设定项参数添加到函数中。此参数接收一个初始值设定项函数,类似于包 tensorflow.keras.initializers 或任何其他自定义初始值设定项中包含的函数。默认情况下,它将设置为random_uniform
请记住,这些函数接受种子参数,该参数可以是所需的任何值,但在此笔记本中必须将其设置为 0 才能进行评分。
下面是实际使用函数式 API 的强大功能来创建快捷方式路径的地方:
def identity_block(X, f, filters, training=True, initializer=random_uniform):
"""
实现图 4 中定义的恒等块
Arguments:
X -- 形状的输入张量(m、n_H_prev、n_W_prev、n_C_prev)
f -- 整数,指定主路径中间 CONV 窗口的形状
filters -- python 整数列表,定义主路径的 CONV 层中的过滤器数量
训练 -- True:在训练模式下行为
错误:在推理模式下行为
初始值设定项 -- 设置图层的初始权重。等于随机统一初始值设定项
Returns:
X -- output of the identity block, tensor of shape (n_H, n_W, n_C)
"""
# Retrieve Filters
F1, F2, F3 = filters
# Save the input value. You'll need this later to add back to the main path.
X_shortcut = X
cache = []
# 主路径的第一个组成部分
X = Conv2D(filters = F1, kernel_size = 1, strides = (1,1), padding = 'valid', kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis = 3)(X, training = training) # Default axis
X = Activation('relu')(X)
### START CODE HERE
## Second component of main path (≈3 lines)
X = Conv2D(filters = F2, kernel_size = f,strides = (1, 1),padding='same',kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis = 3)(X, training=training)
X = Activation('relu')(X)
## Third component of main path (≈2 lines)
X = Conv2D(filters = F3, kernel_size = 1, strides = (1, 1), padding='valid', kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis = 3)(X, training=training)
## Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines)
X = Add()([X_shortcut,X])
X = Activation('relu')(X)
### END CODE HERE
return X
我们来测试一下:
np.random.seed(1)
X1 = np.ones((1, 4, 4, 3)) * -1
X2 = np.ones((1, 4, 4, 3)) * 1
X3 = np.ones((1, 4, 4, 3)) * 3
#按着X1,X2,X3的顺序排序
X = np.concatenate((X1, X2, X3), axis = 0).astype(np.float32)
A3 = identity_block(X, f=2, filters=[4, 4, 3],
initializer=lambda seed=0:constant(value=1),
training=False)
print('\033[1mWith training=False\033[0m\n')
A3np = A3.numpy()
print(np.around(A3.numpy()[:,(0,-1),:,:].mean(axis = 3), 5))
resume = A3np[:,(0,-1),:,:].mean(axis = 3)
print(resume[1, 1, 0])
print('\n\033[1mWith training=True\033[0m\n')
np.random.seed(1)
A4 = identity_block(X, f=2, filters=[3, 3, 3],
initializer=lambda seed=0:constant(value=1),
training=True)
print(np.around(A4.numpy()[:,(0,-1),:,:].mean(axis = 3), 5))
public_tests.identity_block_test(identity_block)
With training=False
[[[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]]
[[192.71234 192.71234 192.71234 96.85617]
[ 96.85617 96.85617 96.85617 48.92808]]
[[578.1371 578.1371 578.1371 290.5685 ]
[290.5685 290.5685 290.5685 146.78426]]]
96.85617
With training=True
[[[0. 0. 0. 0. ]
[0. 0. 0. 0. ]]
[[0.40739 0.40739 0.40739 0.40739]
[0.40739 0.40739 0.40739 0.40739]]
[[4.99991 4.99991 4.99991 3.25948]
[3.25948 3.25948 3.25948 2.40739]]]
All tests passed!
ResNet“卷积块”是第二种块类型。当输入和输出维度不匹配时,可以使用这种类型的块。与标识块的区别在于快捷方式路径中有一个 CONV2D 层:

主路径的第一个组成部分:
主路径的第二个组成部分:
主路径的第三个组成部分:
快捷方式路径:
最后一步:
def convolutional_block(X, f, filters, s = 2, training=True, initializer=glorot_uniform):
"""
图 4 中定义的卷积块的实现
Arguments:
X -- 形状的输入张量(m、n_H_prev、n_W_prev、n_C_prev)
f -- 整数,指定主路径中间 CONV 窗口的形状
filters -- python 整数列表,定义主路径的 CONV 层中的过滤器数量
s -- 整数,指定要使用的步幅
训练 -- True:在训练模式下行为
错误:在推理模式下行为
初始值设定项 -- 设置图层的初始权重。等于 Glorot 统一初始值设定项,
也称为泽维尔均匀初始值设定项。
Returns:
X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C)
"""
# Retrieve Filters
F1, F2, F3 = filters
# Save the input value
X_shortcut = X
##### MAIN PATH #####
# First component of main path glorot_uniform(seed=0)
X = Conv2D(filters = F1, kernel_size = 1, strides = (s, s), padding='valid', kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis = 3)(X, training=training)
X = Activation('relu')(X)
### START CODE HERE
## Second component of main path (≈3 lines)
X = Conv2D(filters = F2, kernel_size = f,strides = (1, 1),padding='same',kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis = 3)(X, training=training)
X = Activation('relu')(X)
## Third component of main path (≈2 lines)
X = Conv2D(filters = F3, kernel_size = 1, strides = (1, 1), padding='valid', kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis = 3)(X, training=training)
##### SHORTCUT PATH ##### (≈2 lines)
X_shortcut = Conv2D(filters = F3, kernel_size = 1, strides = (s, s), padding='valid', kernel_initializer = initializer(seed=0))(X_shortcut)
X_shortcut = BatchNormalization(axis = 3)(X_shortcut, training=training)
### END CODE HERE
# Final step: Add shortcut value to main path (Use this order [X, X_shortcut]), and pass it through a RELU activation
X = Add()([X, X_shortcut])
X = Activation('relu')(X)
return X
我们测试一下:
from outputs import convolutional_block_output1, convolutional_block_output2
np.random.seed(1)
#X = np.random.randn(3, 4, 4, 6).astype(np.float32)
X1 = np.ones((1, 4, 4, 3)) * -1
X2 = np.ones((1, 4, 4, 3)) * 1
X3 = np.ones((1, 4, 4, 3)) * 3
X = np.concatenate((X1, X2, X3), axis = 0).astype(np.float32)
A = convolutional_block(X, f = 2, filters = [2, 4, 6], training=False)
assert type(A) == EagerTensor, "Use only tensorflow and keras functions"
assert tuple(tf.shape(A).numpy()) == (3, 2, 2, 6), "Wrong shape."
assert np.allclose(A.numpy(), convolutional_block_output1), "Wrong values when training=False."
print(A[0])
B = convolutional_block(X, f = 2, filters = [2, 4, 6], training=True)
assert np.allclose(B.numpy(), convolutional_block_output2), "Wrong values when training=True."
print('\033[92mAll tests passed!')
tf.Tensor(
[[[0. 0.66683817 0. 0. 0.88853896 0.5274254 ]
[0. 0.65053666 0. 0. 0.89592844 0.49965227]]
[[0. 0.6312079 0. 0. 0.8636247 0.47643146]
[0. 0.5688321 0. 0. 0.85534114 0.41709304]]], shape=(2, 2, 6), dtype=float32)
All tests passed!
您现在拥有构建非常深入的 ResNet 所需的块。下图详细描述了该神经网络的架构。图中的“ID BLOCK”代表“身份块”,“ID BLOCK x3”表示您应该将 3 个恒等快块堆叠在一起。

零填充用 (3,3) 的填充填充输入
步骤一:
步骤二:
步骤三:
步骤四:
步骤五:
二维平均池化使用形状为 (2,2) 的窗口。
“扁平化”层没有任何超参数。
全连接(密集)层使用 softmax 激活将其输入减少到类数。
def ResNet50(input_shape = (64, 64, 3), classes = 6):
"""
流行的 ResNet50 架构的分阶段实现:
CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3
-> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> FLATTEN -> DENSE
Arguments:
input_shape -- shape of the images of the dataset
classes -- integer, number of classes
Returns:
model -- a Model() instance in Keras
"""
# Define the input as a tensor with shape input_shape
X_input = Input(input_shape)
# Zero-Padding
X = ZeroPadding2D((3, 3))(X_input)
# Stage 1
X = Conv2D(64, (7, 7), strides = (2, 2), kernel_initializer = glorot_uniform(seed=0))(X)
X = BatchNormalization(axis = 3)(X)
X = Activation('relu')(X)
X = MaxPooling2D((3, 3), strides=(2, 2))(X)
# Stage 2
X = convolutional_block(X, f = 3, filters = [64, 64, 256], s = 1)
X = identity_block(X, 3, [64, 64, 256])
X = identity_block(X, 3, [64, 64, 256])
### START CODE HERE
## Stage 3 (≈4 lines)
X = convolutional_block(X, f = 3, filters = [128,128,512], s = 2)
X = identity_block(X, 3, [128,128,512])
X = identity_block(X, 3, [128,128,512])
X = identity_block(X, 3, [128,128,512])
## Stage 4 (≈6 lines)
X = convolutional_block(X, f = 3, filters = [256, 256, 1024], s = 2)
X = identity_block(X, 3, [256, 256, 1024])
X = identity_block(X, 3, [256, 256, 1024])
X = identity_block(X, 3, [256, 256, 1024])
X = identity_block(X, 3, [256, 256, 1024])
X = identity_block(X, 3, [256, 256, 1024])
## Stage 5 (≈3 lines)
X = convolutional_block(X, f = 3, filters = [512, 512, 2048], s = 2)
X = identity_block(X, 3, [512, 512, 2048])
X = identity_block(X, 3, [512, 512, 2048])
## AVGPOOL (≈1 line). Use "X = AveragePooling2D(...)(X)"
X = AveragePooling2D((2, 2))(X)
### END CODE HERE
# output layer
X = Flatten()(X)
X = Dense(classes, activation='softmax', kernel_initializer = glorot_uniform(seed=0))(X)
# Create model
model = Model(inputs = X_input, outputs = X)
return model
模型建立完成了,我们来看一下参数
model = ResNet50(input_shape = (64, 64, 3), classes = 6)
print(model.summary())
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 64, 64, 3)] 0
__________________________________________________________________________________________________
zero_padding2d (ZeroPadding2D) (None, 70, 70, 3) 0 input_1[0][0]
__________________________________________________________________________________________________
conv2d_20 (Conv2D) (None, 32, 32, 64) 9472 zero_padding2d[0][0]
__________________________________________________________________________________________________
batch_normalization_20 (BatchNo (None, 32, 32, 64) 256 conv2d_20[0][0]
__________________________________________________________________________________________________
activation_18 (Activation) (None, 32, 32, 64) 0 batch_normalization_20[0][0]
__________________________________________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 15, 15, 64) 0 activation_18[0][0]
__________________________________________________________________________________________________
conv2d_21 (Conv2D) (None, 15, 15, 64) 4160 max_pooling2d[0][0]
__________________________________________________________________________________________________
batch_normalization_21 (BatchNo (None, 15, 15, 64) 256 conv2d_21[0][0]
__________________________________________________________________________________________________
activation_19 (Activation) (None, 15, 15, 64) 0 batch_normalization_21[0][0]
__________________________________________________________________________________________________
conv2d_22 (Conv2D) (None, 15, 15, 64) 36928 activation_19[0][0]
__________________________________________________________________________________________________
batch_normalization_22 (BatchNo (None, 15, 15, 64) 256 conv2d_22[0][0]
__________________________________________________________________________________________________
activation_20 (Activation) (None, 15, 15, 64) 0 batch_normalization_22[0][0]
__________________________________________________________________________________________________
conv2d_23 (Conv2D) (None, 15, 15, 256) 16640 activation_20[0][0]
__________________________________________________________________________________________________
conv2d_24 (Conv2D) (None, 15, 15, 256) 16640 max_pooling2d[0][0]
__________________________________________________________________________________________________
batch_normalization_23 (BatchNo (None, 15, 15, 256) 1024 conv2d_23[0][0]
__________________________________________________________________________________________________
batch_normalization_24 (BatchNo (None, 15, 15, 256) 1024 conv2d_24[0][0]
__________________________________________________________________________________________________
add_6 (Add) (None, 15, 15, 256) 0 batch_normalization_23[0][0]
batch_normalization_24[0][0]
__________________________________________________________________________________________________
activation_21 (Activation) (None, 15, 15, 256) 0 add_6[0][0]
__________________________________________________________________________________________________
conv2d_25 (Conv2D) (None, 15, 15, 64) 16448 activation_21[0][0]
__________________________________________________________________________________________________
batch_normalization_25 (BatchNo (None, 15, 15, 64) 256 conv2d_25[0][0]
__________________________________________________________________________________________________
activation_22 (Activation) (None, 15, 15, 64) 0 batch_normalization_25[0][0]
__________________________________________________________________________________________________
conv2d_26 (Conv2D) (None, 15, 15, 64) 36928 activation_22[0][0]
__________________________________________________________________________________________________
batch_normalization_26 (BatchNo (None, 15, 15, 64) 256 conv2d_26[0][0]
__________________________________________________________________________________________________
activation_23 (Activation) (None, 15, 15, 64) 0 batch_normalization_26[0][0]
__________________________________________________________________________________________________
conv2d_27 (Conv2D) (None, 15, 15, 256) 16640 activation_23[0][0]
__________________________________________________________________________________________________
batch_normalization_27 (BatchNo (None, 15, 15, 256) 1024 conv2d_27[0][0]
__________________________________________________________________________________________________
add_7 (Add) (None, 15, 15, 256) 0 activation_21[0][0]
batch_normalization_27[0][0]
__________________________________________________________________________________________________
activation_24 (Activation) (None, 15, 15, 256) 0 add_7[0][0]
__________________________________________________________________________________________________
conv2d_28 (Conv2D) (None, 15, 15, 64) 16448 activation_24[0][0]
__________________________________________________________________________________________________
batch_normalization_28 (BatchNo (None, 15, 15, 64) 256 conv2d_28[0][0]
__________________________________________________________________________________________________
activation_25 (Activation) (None, 15, 15, 64) 0 batch_normalization_28[0][0]
__________________________________________________________________________________________________
conv2d_29 (Conv2D) (None, 15, 15, 64) 36928 activation_25[0][0]
__________________________________________________________________________________________________
batch_normalization_29 (BatchNo (None, 15, 15, 64) 256 conv2d_29[0][0]
__________________________________________________________________________________________________
activation_26 (Activation) (None, 15, 15, 64) 0 batch_normalization_29[0][0]
__________________________________________________________________________________________________
conv2d_30 (Conv2D) (None, 15, 15, 256) 16640 activation_26[0][0]
__________________________________________________________________________________________________
batch_normalization_30 (BatchNo (None, 15, 15, 256) 1024 conv2d_30[0][0]
__________________________________________________________________________________________________
add_8 (Add) (None, 15, 15, 256) 0 activation_24[0][0]
batch_normalization_30[0][0]
__________________________________________________________________________________________________
activation_27 (Activation) (None, 15, 15, 256) 0 add_8[0][0]
__________________________________________________________________________________________________
conv2d_31 (Conv2D) (None, 8, 8, 128) 32896 activation_27[0][0]
__________________________________________________________________________________________________
batch_normalization_31 (BatchNo (None, 8, 8, 128) 512 conv2d_31[0][0]
__________________________________________________________________________________________________
activation_28 (Activation) (None, 8, 8, 128) 0 batch_normalization_31[0][0]
__________________________________________________________________________________________________
conv2d_32 (Conv2D) (None, 8, 8, 128) 147584 activation_28[0][0]
__________________________________________________________________________________________________
batch_normalization_32 (BatchNo (None, 8, 8, 128) 512 conv2d_32[0][0]
__________________________________________________________________________________________________
activation_29 (Activation) (None, 8, 8, 128) 0 batch_normalization_32[0][0]
__________________________________________________________________________________________________
conv2d_33 (Conv2D) (None, 8, 8, 512) 66048 activation_29[0][0]
__________________________________________________________________________________________________
conv2d_34 (Conv2D) (None, 8, 8, 512) 131584 activation_27[0][0]
__________________________________________________________________________________________________
batch_normalization_33 (BatchNo (None, 8, 8, 512) 2048 conv2d_33[0][0]
__________________________________________________________________________________________________
batch_normalization_34 (BatchNo (None, 8, 8, 512) 2048 conv2d_34[0][0]
__________________________________________________________________________________________________
add_9 (Add) (None, 8, 8, 512) 0 batch_normalization_33[0][0]
batch_normalization_34[0][0]
__________________________________________________________________________________________________
activation_30 (Activation) (None, 8, 8, 512) 0 add_9[0][0]
__________________________________________________________________________________________________
conv2d_35 (Conv2D) (None, 8, 8, 128) 65664 activation_30[0][0]
__________________________________________________________________________________________________
batch_normalization_35 (BatchNo (None, 8, 8, 128) 512 conv2d_35[0][0]
__________________________________________________________________________________________________
activation_31 (Activation) (None, 8, 8, 128) 0 batch_normalization_35[0][0]
__________________________________________________________________________________________________
conv2d_36 (Conv2D) (None, 8, 8, 128) 147584 activation_31[0][0]
__________________________________________________________________________________________________
batch_normalization_36 (BatchNo (None, 8, 8, 128) 512 conv2d_36[0][0]
__________________________________________________________________________________________________
activation_32 (Activation) (None, 8, 8, 128) 0 batch_normalization_36[0][0]
__________________________________________________________________________________________________
conv2d_37 (Conv2D) (None, 8, 8, 512) 66048 activation_32[0][0]
__________________________________________________________________________________________________
batch_normalization_37 (BatchNo (None, 8, 8, 512) 2048 conv2d_37[0][0]
__________________________________________________________________________________________________
add_10 (Add) (None, 8, 8, 512) 0 activation_30[0][0]
batch_normalization_37[0][0]
__________________________________________________________________________________________________
activation_33 (Activation) (None, 8, 8, 512) 0 add_10[0][0]
__________________________________________________________________________________________________
conv2d_38 (Conv2D) (None, 8, 8, 128) 65664 activation_33[0][0]
__________________________________________________________________________________________________
batch_normalization_38 (BatchNo (None, 8, 8, 128) 512 conv2d_38[0][0]
__________________________________________________________________________________________________
activation_34 (Activation) (None, 8, 8, 128) 0 batch_normalization_38[0][0]
__________________________________________________________________________________________________
conv2d_39 (Conv2D) (None, 8, 8, 128) 147584 activation_34[0][0]
__________________________________________________________________________________________________
batch_normalization_39 (BatchNo (None, 8, 8, 128) 512 conv2d_39[0][0]
__________________________________________________________________________________________________
activation_35 (Activation) (None, 8, 8, 128) 0 batch_normalization_39[0][0]
__________________________________________________________________________________________________
conv2d_40 (Conv2D) (None, 8, 8, 512) 66048 activation_35[0][0]
__________________________________________________________________________________________________
batch_normalization_40 (BatchNo (None, 8, 8, 512) 2048 conv2d_40[0][0]
__________________________________________________________________________________________________
add_11 (Add) (None, 8, 8, 512) 0 activation_33[0][0]
batch_normalization_40[0][0]
__________________________________________________________________________________________________
activation_36 (Activation) (None, 8, 8, 512) 0 add_11[0][0]
__________________________________________________________________________________________________
conv2d_41 (Conv2D) (None, 8, 8, 128) 65664 activation_36[0][0]
__________________________________________________________________________________________________
batch_normalization_41 (BatchNo (None, 8, 8, 128) 512 conv2d_41[0][0]
__________________________________________________________________________________________________
activation_37 (Activation) (None, 8, 8, 128) 0 batch_normalization_41[0][0]
__________________________________________________________________________________________________
conv2d_42 (Conv2D) (None, 8, 8, 128) 147584 activation_37[0][0]
__________________________________________________________________________________________________
batch_normalization_42 (BatchNo (None, 8, 8, 128) 512 conv2d_42[0][0]
__________________________________________________________________________________________________
activation_38 (Activation) (None, 8, 8, 128) 0 batch_normalization_42[0][0]
__________________________________________________________________________________________________
conv2d_43 (Conv2D) (None, 8, 8, 512) 66048 activation_38[0][0]
__________________________________________________________________________________________________
batch_normalization_43 (BatchNo (None, 8, 8, 512) 2048 conv2d_43[0][0]
__________________________________________________________________________________________________
add_12 (Add) (None, 8, 8, 512) 0 activation_36[0][0]
batch_normalization_43[0][0]
__________________________________________________________________________________________________
activation_39 (Activation) (None, 8, 8, 512) 0 add_12[0][0]
__________________________________________________________________________________________________
conv2d_44 (Conv2D) (None, 4, 4, 256) 131328 activation_39[0][0]
__________________________________________________________________________________________________
batch_normalization_44 (BatchNo (None, 4, 4, 256) 1024 conv2d_44[0][0]
__________________________________________________________________________________________________
activation_40 (Activation) (None, 4, 4, 256) 0 batch_normalization_44[0][0]
__________________________________________________________________________________________________
conv2d_45 (Conv2D) (None, 4, 4, 256) 590080 activation_40[0][0]
__________________________________________________________________________________________________
batch_normalization_45 (BatchNo (None, 4, 4, 256) 1024 conv2d_45[0][0]
__________________________________________________________________________________________________
activation_41 (Activation) (None, 4, 4, 256) 0 batch_normalization_45[0][0]
__________________________________________________________________________________________________
conv2d_46 (Conv2D) (None, 4, 4, 1024) 263168 activation_41[0][0]
__________________________________________________________________________________________________
conv2d_47 (Conv2D) (None, 4, 4, 1024) 525312 activation_39[0][0]
__________________________________________________________________________________________________
batch_normalization_46 (BatchNo (None, 4, 4, 1024) 4096 conv2d_46[0][0]
__________________________________________________________________________________________________
batch_normalization_47 (BatchNo (None, 4, 4, 1024) 4096 conv2d_47[0][0]
__________________________________________________________________________________________________
add_13 (Add) (None, 4, 4, 1024) 0 batch_normalization_46[0][0]
batch_normalization_47[0][0]
__________________________________________________________________________________________________
activation_42 (Activation) (None, 4, 4, 1024) 0 add_13[0][0]
__________________________________________________________________________________________________
conv2d_48 (Conv2D) (None, 4, 4, 256) 262400 activation_42[0][0]
__________________________________________________________________________________________________
batch_normalization_48 (BatchNo (None, 4, 4, 256) 1024 conv2d_48[0][0]
__________________________________________________________________________________________________
activation_43 (Activation) (None, 4, 4, 256) 0 batch_normalization_48[0][0]
__________________________________________________________________________________________________
conv2d_49 (Conv2D) (None, 4, 4, 256) 590080 activation_43[0][0]
__________________________________________________________________________________________________
batch_normalization_49 (BatchNo (None, 4, 4, 256) 1024 conv2d_49[0][0]
__________________________________________________________________________________________________
activation_44 (Activation) (None, 4, 4, 256) 0 batch_normalization_49[0][0]
__________________________________________________________________________________________________
conv2d_50 (Conv2D) (None, 4, 4, 1024) 263168 activation_44[0][0]
__________________________________________________________________________________________________
batch_normalization_50 (BatchNo (None, 4, 4, 1024) 4096 conv2d_50[0][0]
__________________________________________________________________________________________________
add_14 (Add) (None, 4, 4, 1024) 0 activation_42[0][0]
batch_normalization_50[0][0]
__________________________________________________________________________________________________
activation_45 (Activation) (None, 4, 4, 1024) 0 add_14[0][0]
__________________________________________________________________________________________________
conv2d_51 (Conv2D) (None, 4, 4, 256) 262400 activation_45[0][0]
__________________________________________________________________________________________________
batch_normalization_51 (BatchNo (None, 4, 4, 256) 1024 conv2d_51[0][0]
__________________________________________________________________________________________________
activation_46 (Activation) (None, 4, 4, 256) 0 batch_normalization_51[0][0]
__________________________________________________________________________________________________
conv2d_52 (Conv2D) (None, 4, 4, 256) 590080 activation_46[0][0]
__________________________________________________________________________________________________
batch_normalization_52 (BatchNo (None, 4, 4, 256) 1024 conv2d_52[0][0]
__________________________________________________________________________________________________
activation_47 (Activation) (None, 4, 4, 256) 0 batch_normalization_52[0][0]
__________________________________________________________________________________________________
conv2d_53 (Conv2D) (None, 4, 4, 1024) 263168 activation_47[0][0]
__________________________________________________________________________________________________
batch_normalization_53 (BatchNo (None, 4, 4, 1024) 4096 conv2d_53[0][0]
__________________________________________________________________________________________________
add_15 (Add) (None, 4, 4, 1024) 0 activation_45[0][0]
batch_normalization_53[0][0]
__________________________________________________________________________________________________
activation_48 (Activation) (None, 4, 4, 1024) 0 add_15[0][0]
__________________________________________________________________________________________________
conv2d_54 (Conv2D) (None, 4, 4, 256) 262400 activation_48[0][0]
__________________________________________________________________________________________________
batch_normalization_54 (BatchNo (None, 4, 4, 256) 1024 conv2d_54[0][0]
__________________________________________________________________________________________________
activation_49 (Activation) (None, 4, 4, 256) 0 batch_normalization_54[0][0]
__________________________________________________________________________________________________
conv2d_55 (Conv2D) (None, 4, 4, 256) 590080 activation_49[0][0]
__________________________________________________________________________________________________
batch_normalization_55 (BatchNo (None, 4, 4, 256) 1024 conv2d_55[0][0]
__________________________________________________________________________________________________
activation_50 (Activation) (None, 4, 4, 256) 0 batch_normalization_55[0][0]
__________________________________________________________________________________________________
conv2d_56 (Conv2D) (None, 4, 4, 1024) 263168 activation_50[0][0]
__________________________________________________________________________________________________
batch_normalization_56 (BatchNo (None, 4, 4, 1024) 4096 conv2d_56[0][0]
__________________________________________________________________________________________________
add_16 (Add) (None, 4, 4, 1024) 0 activation_48[0][0]
batch_normalization_56[0][0]
__________________________________________________________________________________________________
activation_51 (Activation) (None, 4, 4, 1024) 0 add_16[0][0]
__________________________________________________________________________________________________
conv2d_57 (Conv2D) (None, 4, 4, 256) 262400 activation_51[0][0]
__________________________________________________________________________________________________
batch_normalization_57 (BatchNo (None, 4, 4, 256) 1024 conv2d_57[0][0]
__________________________________________________________________________________________________
activation_52 (Activation) (None, 4, 4, 256) 0 batch_normalization_57[0][0]
__________________________________________________________________________________________________
conv2d_58 (Conv2D) (None, 4, 4, 256) 590080 activation_52[0][0]
__________________________________________________________________________________________________
batch_normalization_58 (BatchNo (None, 4, 4, 256) 1024 conv2d_58[0][0]
__________________________________________________________________________________________________
activation_53 (Activation) (None, 4, 4, 256) 0 batch_normalization_58[0][0]
__________________________________________________________________________________________________
conv2d_59 (Conv2D) (None, 4, 4, 1024) 263168 activation_53[0][0]
__________________________________________________________________________________________________
batch_normalization_59 (BatchNo (None, 4, 4, 1024) 4096 conv2d_59[0][0]
__________________________________________________________________________________________________
add_17 (Add) (None, 4, 4, 1024) 0 activation_51[0][0]
batch_normalization_59[0][0]
__________________________________________________________________________________________________
activation_54 (Activation) (None, 4, 4, 1024) 0 add_17[0][0]
__________________________________________________________________________________________________
conv2d_60 (Conv2D) (None, 4, 4, 256) 262400 activation_54[0][0]
__________________________________________________________________________________________________
batch_normalization_60 (BatchNo (None, 4, 4, 256) 1024 conv2d_60[0][0]
__________________________________________________________________________________________________
activation_55 (Activation) (None, 4, 4, 256) 0 batch_normalization_60[0][0]
__________________________________________________________________________________________________
conv2d_61 (Conv2D) (None, 4, 4, 256) 590080 activation_55[0][0]
__________________________________________________________________________________________________
batch_normalization_61 (BatchNo (None, 4, 4, 256) 1024 conv2d_61[0][0]
__________________________________________________________________________________________________
activation_56 (Activation) (None, 4, 4, 256) 0 batch_normalization_61[0][0]
__________________________________________________________________________________________________
conv2d_62 (Conv2D) (None, 4, 4, 1024) 263168 activation_56[0][0]
__________________________________________________________________________________________________
batch_normalization_62 (BatchNo (None, 4, 4, 1024) 4096 conv2d_62[0][0]
__________________________________________________________________________________________________
add_18 (Add) (None, 4, 4, 1024) 0 activation_54[0][0]
batch_normalization_62[0][0]
__________________________________________________________________________________________________
activation_57 (Activation) (None, 4, 4, 1024) 0 add_18[0][0]
__________________________________________________________________________________________________
conv2d_63 (Conv2D) (None, 2, 2, 512) 524800 activation_57[0][0]
__________________________________________________________________________________________________
batch_normalization_63 (BatchNo (None, 2, 2, 512) 2048 conv2d_63[0][0]
__________________________________________________________________________________________________
activation_58 (Activation) (None, 2, 2, 512) 0 batch_normalization_63[0][0]
__________________________________________________________________________________________________
conv2d_64 (Conv2D) (None, 2, 2, 512) 2359808 activation_58[0][0]
__________________________________________________________________________________________________
batch_normalization_64 (BatchNo (None, 2, 2, 512) 2048 conv2d_64[0][0]
__________________________________________________________________________________________________
activation_59 (Activation) (None, 2, 2, 512) 0 batch_normalization_64[0][0]
__________________________________________________________________________________________________
conv2d_65 (Conv2D) (None, 2, 2, 2048) 1050624 activation_59[0][0]
__________________________________________________________________________________________________
conv2d_66 (Conv2D) (None, 2, 2, 2048) 2099200 activation_57[0][0]
__________________________________________________________________________________________________
batch_normalization_65 (BatchNo (None, 2, 2, 2048) 8192 conv2d_65[0][0]
__________________________________________________________________________________________________
batch_normalization_66 (BatchNo (None, 2, 2, 2048) 8192 conv2d_66[0][0]
__________________________________________________________________________________________________
add_19 (Add) (None, 2, 2, 2048) 0 batch_normalization_65[0][0]
batch_normalization_66[0][0]
__________________________________________________________________________________________________
activation_60 (Activation) (None, 2, 2, 2048) 0 add_19[0][0]
__________________________________________________________________________________________________
conv2d_67 (Conv2D) (None, 2, 2, 512) 1049088 activation_60[0][0]
__________________________________________________________________________________________________
batch_normalization_67 (BatchNo (None, 2, 2, 512) 2048 conv2d_67[0][0]
__________________________________________________________________________________________________
activation_61 (Activation) (None, 2, 2, 512) 0 batch_normalization_67[0][0]
__________________________________________________________________________________________________
conv2d_68 (Conv2D) (None, 2, 2, 512) 2359808 activation_61[0][0]
__________________________________________________________________________________________________
batch_normalization_68 (BatchNo (None, 2, 2, 512) 2048 conv2d_68[0][0]
__________________________________________________________________________________________________
activation_62 (Activation) (None, 2, 2, 512) 0 batch_normalization_68[0][0]
__________________________________________________________________________________________________
conv2d_69 (Conv2D) (None, 2, 2, 2048) 1050624 activation_62[0][0]
__________________________________________________________________________________________________
batch_normalization_69 (BatchNo (None, 2, 2, 2048) 8192 conv2d_69[0][0]
__________________________________________________________________________________________________
add_20 (Add) (None, 2, 2, 2048) 0 activation_60[0][0]
batch_normalization_69[0][0]
__________________________________________________________________________________________________
activation_63 (Activation) (None, 2, 2, 2048) 0 add_20[0][0]
__________________________________________________________________________________________________
conv2d_70 (Conv2D) (None, 2, 2, 512) 1049088 activation_63[0][0]
__________________________________________________________________________________________________
batch_normalization_70 (BatchNo (None, 2, 2, 512) 2048 conv2d_70[0][0]
__________________________________________________________________________________________________
activation_64 (Activation) (None, 2, 2, 512) 0 batch_normalization_70[0][0]
__________________________________________________________________________________________________
conv2d_71 (Conv2D) (None, 2, 2, 512) 2359808 activation_64[0][0]
__________________________________________________________________________________________________
batch_normalization_71 (BatchNo (None, 2, 2, 512) 2048 conv2d_71[0][0]
__________________________________________________________________________________________________
activation_65 (Activation) (None, 2, 2, 512) 0 batch_normalization_71[0][0]
__________________________________________________________________________________________________
conv2d_72 (Conv2D) (None, 2, 2, 2048) 1050624 activation_65[0][0]
__________________________________________________________________________________________________
batch_normalization_72 (BatchNo (None, 2, 2, 2048) 8192 conv2d_72[0][0]
__________________________________________________________________________________________________
add_21 (Add) (None, 2, 2, 2048) 0 activation_63[0][0]
batch_normalization_72[0][0]
__________________________________________________________________________________________________
activation_66 (Activation) (None, 2, 2, 2048) 0 add_21[0][0]
__________________________________________________________________________________________________
average_pooling2d (AveragePooli (None, 1, 1, 2048) 0 activation_66[0][0]
__________________________________________________________________________________________________
flatten (Flatten) (None, 2048) 0 average_pooling2d[0][0]
__________________________________________________________________________________________________
dense (Dense) (None, 6) 12294 flatten[0][0]
==================================================================================================
Total params: 23,600,006
Trainable params: 23,546,886
Non-trainable params: 53,120
__________________________________________________________________________________________________
None
from outputs import ResNet50_summary
model = ResNet50(input_shape = (64, 64, 3), classes = 6)
实体化结束,下面我们对模型进行编译
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
接下来我们就是加载数据集并进行训练
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()
# Normalize image vectors
X_train = X_train_orig / 255.
X_test = X_test_orig / 255.
# Convert training and test labels to one hot matrices
Y_train = convert_to_one_hot(Y_train_orig, 6).T
Y_test = convert_to_one_hot(Y_test_orig, 6).T
print ("number of training examples = " + str(X_train.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))
number of training examples = 1080 number of test examples = 120 X_train shape: (1080, 64, 64, 3) Y_train shape: (1080, 6) X_test shape: (120, 64, 64, 3) Y_test shape: (120, 6)
model.fit(X_train, Y_train, epochs = 10, batch_size = 32)
Epoch 1/10 34/34 [==============================] - 10s 129ms/step - loss: 2.2749 - accuracy: 0.4417 Epoch 2/10 34/34 [==============================] - 3s 92ms/step - loss: 1.0135 - accuracy: 0.6889 Epoch 3/10 34/34 [==============================] - 3s 92ms/step - loss: 0.3830 - accuracy: 0.8694 Epoch 4/10 34/34 [==============================] - 3s 91ms/step - loss: 0.2390 - accuracy: 0.9241 Epoch 5/10 34/34 [==============================] - 3s 91ms/step - loss: 0.1527 - accuracy: 0.9519 0s - loss: 0.1 Epoch 6/10 34/34 [==============================] - 3s 91ms/step - loss: 0.1050 - accuracy: 0.9648 Epoch 7/10 34/34 [==============================] - 3s 91ms/step - loss: 0.1824 - accuracy: 0.9444 Epoch 8/10 34/34 [==============================] - 3s 92ms/step - loss: 0.5906 - accuracy: 0.8074 Epoch 9/10 34/34 [==============================] - 3s 91ms/step - loss: 0.4195 - accuracy: 0.8676 Epoch 10/10 34/34 [==============================] - 3s 91ms/step - loss: 0.3377 - accuracy: 0.9194
我们对模型进行评估
preds = model.evaluate(X_test, Y_test)
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))
4/4 [==============================] - 1s 31ms/step - loss: 0.4844 - accuracy: 0.8417 Loss = 0.4843602478504181 Test Accuracy = 0.8416666388511658
博主已经在手势数据集上训练了自己的RESNET50模型的权重,你可以使用下面的代码载并运行博主的训练模型,
pre_trained_model = tf.keras.models.load_model('resnet50.h5')
然后测试一下博主训练出来的权值:
preds = pre_trained_model.evaluate(X_test, Y_test)
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))
img_path = 'C:/Users/Style/Desktop/kun.png'
img = image.load_img(img_path, target_size=(64, 64))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = x/255.0
print('Input image shape:', x.shape)
imshow(img)
prediction = model.predict(x)
print("Class prediction vector [p(0), p(1), p(2), p(3), p(4), p(5)] = ", prediction)
print("Class:", np.argmax(prediction))
Input image shape: (1, 64, 64, 3) Class prediction vector [p(0), p(1), p(2), p(3), p(4), p(5)] = [[0.01301673 0.8742848 0.00662233 0.05449386 0.02079306 0.03078919]] Class: 1
pre_trained_model.summary()
我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b
网络编程套接字网络编程基础知识理解源`IP`地址和目的`IP`地址理解源MAC地址和目的MAC地址认识端口号理解端口号和进程ID理解源端口号和目的端口号认识`TCP`协议认识`UDP`协议网络字节序socket编程接口`sockaddr``UDP`网络程序服务器端代码逻辑:需要用到的接口服务器端代码`udp`客户端代码逻辑`udp`客户端代码`TCP`网络程序服务器代码逻辑多个版本服务器单进程版本多进程版本多线程版本线程池版本服务器端代码客户端代码逻辑客户端代码TCP协议通讯流程TCP协议的客户端/服务器程序流程三次握手(建立连接)数据传输四次挥手(断开连接)TCP和UDP对比网络编程基础知识
目录前言滤波电路科普主要分类实际情况单位的概念常用评价参数函数型滤波器简单分析滤波电路构成低通滤波器RC低通滤波器RL低通滤波器高通滤波器RC高通滤波器RL高通滤波器部分摘自《LC滤波器设计与制作》,侵权删。前言最近需要学习放大电路和滤波电路,但是由于只在之前做音乐频谱分析仪的时候简单了解过一点点运放,所以也是相当从零开始学习了。滤波电路科普主要分类滤波器:主要是从不同频率的成分中提取出特定频率的信号。有源滤波器:由RC元件与运算放大器组成的滤波器。可滤除某一次或多次谐波,最普通易于采用的无源滤波器结构是将电感与电容串联,可对主要次谐波(3、5、7)构成低阻抗旁路。无源滤波器:无源滤波器,又称
最近在学习CAN,记录一下,也供大家参考交流。推荐几个我觉得很好的CAN学习,本文也是在看了他们的好文之后做的笔记首先是瑞萨的CAN入门,真的通透;秀!靠这篇我竟然2天理解了CAN协议!实战STM32F4CAN!原文链接:https://blog.csdn.net/XiaoXiaoPengBo/article/details/116206252CAN详解(小白教程)原文链接:https://blog.csdn.net/xwwwj/article/details/105372234一篇易懂的CAN通讯协议指南1一篇易懂的CAN通讯协议指南1-知乎(zhihu.com)视频推荐CAN总线个人知识总
深度学习部署:Windows安装pycocotools报错解决方法1.pycocotools库的简介2.pycocotools安装的坑3.解决办法更多Ai资讯:公主号AiCharm本系列是作者在跑一些深度学习实例时,遇到的各种各样的问题及解决办法,希望能够帮助到大家。ERROR:Commanderroredoutwithexitstatus1:'D:\Anaconda3\python.exe'-u-c'importsys,setuptools,tokenize;sys.argv[0]='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-instal
我完全不是程序员,正在学习使用Ruby和Rails框架进行编程。我目前正在使用Ruby1.8.7和Rails3.0.3,但我想知道我是否应该升级到Ruby1.9,因为我真的没有任何升级的“遗留”成本。缺点是什么?我是否会遇到与普通gem的兼容性问题,或者甚至其他我不太了解甚至无法预料的问题? 最佳答案 你应该升级。不要坚持从1.8.7开始。如果您发现不支持1.9.2的gem,请避免使用它们(因为它们很可能不被维护)。如果您对gem是否兼容1.9.2有任何疑问,您可以在以下位置查看:http://www.railsplugins.or
如何学习ruby的正则表达式?(对于假人) 最佳答案 http://www.rubular.com/在Ruby中使用正则表达式时是一个很棒的工具,因为它可以立即将结果可视化。 关于ruby-我如何学习ruby的正则表达式?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/1881231/
是否可以在不实际下载文件的情况下检查文件是否存在?我有这么大的(~40mb)文件,例如:http://mirrors.sohu.com/mysql/MySQL-6.0/MySQL-6.0.11-0.glibc23.src.rpm这与ruby不严格相关,但如果发件人可以设置内容长度就好了。RestClient.get"http://mirrors.sohu.com/mysql/MySQL-6.0/MySQL-6.0.11-0.glibc23.src.rpm",headers:{"Content-Length"=>100} 最佳答案
我在这方面尝试了很多URL,在我遇到这个特定的之前,它们似乎都很好:require'rubygems'require'nokogiri'require'open-uri'doc=Nokogiri::HTML(open("http://www.moxyst.com/fashion/men-clothing/underwear.html"))putsdoc这是结果:/Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:353:in`open_http':404NotFound(OpenURI::HT
深度学习12.CNN经典网络VGG16一、简介1.VGG来源2.VGG分类3.不同模型的参数数量4.3x3卷积核的好处5.关于学习率调度6.批归一化二、VGG16层分析1.层划分2.参数展开过程图解3.参数传递示例4.VGG16各层参数数量三、代码分析1.VGG16模型定义2.训练3.测试一、简介1.VGG来源VGG(VisualGeometryGroup)是一个视觉几何组在2014年提出的深度卷积神经网络架构。VGG在2014年ImageNet图像分类竞赛亚军,定位竞赛冠军;VGG网络采用连续的小卷积核(3x3)和池化层构建深度神经网络,网络深度可以达到16层或19层,其中VGG16和VGG