vgg16迁移学习transfer_learing

楼主最近学习莫烦大神的tensorflow教程,总结一下莫烦大神如何把vgg16的参数权重迁移过来
首先,下载好vgg16的参数和权重
下载后,是一个.npy后缀的文件,用numpy的load方法加载好,传给data_dict,data_dict相当于python中词典dictionary

self.data_dict = np.load(vgg16_npy_path, encoding='latin1').item()

自己定义卷积层时,固定卷积核参数为常数,从data_dict中提取

def conv_layer(self, bottom, name):
        with tf.variable_scope(name):   # CNN's filter is constant, NOT Variable that can be trained
            conv = tf.nn.conv2d(bottom, self.data_dict[name][0], [1, 1, 1, 1], padding='SAME')
            lout = tf.nn.relu(tf.nn.bias_add(conv, self.data_dict[name][1]))
            return lout

pooling层的size,stride ,padding都是超参数,要与vgg设置一致即可

def max_pool(self, bottom, name):
        return tf.nn.max_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)

莫烦大神完整代码传送门

你可能感兴趣的:(莫烦pytorch学习)