【发布时间】:2019-10-13 15:21:14
【问题描述】:
您好,我需要将模型的第一个卷积从 rgb/resnet_v1_50/conv1/weights:0 (float32_ref 7x7x3x64) 更改为 rgb/resnet_v1_50/conv1/weights:0 (float32_ref 7x7x4x64),因此基本上增加了过滤器的数量从 3 到 4 接受 4 个通道的图像,但在其他地方保留预训练的权重(只是额外的通道初始化 ramdonly)。
你知道如何在 Tensorflow 1.x 中做到这一点(我更喜欢 PyTorch 人......)?
在 PyTorch 中我这样做:
net = model.resnet50(num_classes=dataset_train.num_classes(),pretrained=True)
new_conv1 = nn.Conv2d(4, 64, kernel_size=7, stride=2,padding=3,bias=False)
conv1 = net.conv1
with torch.no_grad():
new_conv1.weight[:, :3, :, :]= conv1.weight
new_conv1.bias = conv1.bias
net.conv1 = new_conv1
这是在 tensorflow 中创建模型的方式:
def single_stream(self, images, modality, is_training, reuse=False):
with tf.variable_scope(modality, reuse=reuse):
with slim.arg_scope(resnet_v1.resnet_arg_scope()):
_, end_points = resnet_v1.resnet_v1_50(
images, self.no_classes, is_training=is_training, reuse=reuse)
# last bottleneck before logits
net = end_points[modality + '/resnet_v1_50/block4']
if 'autoencoder' in self.mode:
return net
with tf.variable_scope(modality + '/resnet_v1_50', reuse=reuse):
bottleneck = slim.conv2d(net, self.hidden_repr_size, [
7, 7], padding='VALID', activation_fn=tf.nn.relu, scope='f_repr')
net = slim.conv2d(bottleneck, self.no_classes, [
1, 1], activation_fn=None, scope='_logits_')
if ('train_hallucination' in self.mode or 'test_disc' in self.mode or 'train_eccv' in self.mode):
return net, bottleneck
return net
我可以使用 build_model 中的命令:self.images = tf.placeholder(tf.float32, [None, 224, 224, 4], modality + '_images') 有效将 3 更改为 4: rgb/resnet_v1_50/conv1/weights:0 (float32_ref 7x7x4x64) [12544, bytes: 50176] 但问题出在检查点上!
非常感谢您的帮助!
【问题讨论】:
标签: python tensorflow deep-learning