Jean.L 2022-11-15 22:12 采纳率: 0%
浏览 2

tensorflow的session中有多个模型的话可以分别保存参数吗?

目前想把tensorflow的模型转成pytorch,但遇到了模型保存的问题。
如题,我的代码里面的模型是这样的:

class EWE_Resnet:
    def __init__(self, image, label, w_label, bs, num_class, lr, factors, temperatures, target, is_training, metric,
                 layers):
        self.num_class = num_class
        self.batch_size = bs
        self.lr = lr
        self.b1 = 0.9
        self.b2 = 0.99
        self.epsilon = 1e-5
        self.target = target
        self.w = w_label
        self.x = image
        self.Y = label
        self.y = tf.argmax(self.Y, 1)
        self.layers = layers
        self.temp = temperatures
        self.snnl_func = functools.partial(snnl, metric=metric)
        self.factor_1 = factors[0]
        self.factor_2 = factors[1]
        self.factor_3 = factors[2]
        self.is_training = is_training
        self.prediction = self.pred()
        self.error = self.error_rate()
        self.snnl_loss = self.snnl()
        self.ce_loss = self.cross_entropy()
        self.optimize = self.optimizer()
        self.snnl_trigger = self.snnl_gradient() # Some functions
        self.ce_trigger = self.ce_gradient()

    def pred(self, reuse=tf.compat.v1.AUTO_REUSE):
        res = []
        with tf.variable_scope("network", reuse=reuse):

            if self.layers > 34:
                residual_block = resnet.bottle_resblock
            else:
                residual_block = resnet.resblock

            residual_list = resnet.get_residual_layer(self.layers)

            ch = 64
            x = self.x
            # print(x)
            x = resnet.conv(x, channels=ch, kernel=3, stride=1, scope='conv')

            for i in range(residual_list[0]):
                x = tf.cond(tf.greater(self.is_training, 0),
                            lambda: residual_block(x, channels=ch, is_training=True, downsample=False,
                                   scope='resblock0_' + str(i)),
                            lambda: residual_block(x, channels=ch, is_training=False, downsample=False,
                                           scope='resblock0_' + str(i)))

            ########################################################################################################

            x = tf.cond(tf.greater(self.is_training, 0),
                        lambda: residual_block(x, channels=ch * 2, is_training=True, downsample=True,
                                       scope='resblock1_0'),
                        lambda: residual_block(x, channels=ch * 2, is_training=False, downsample=True,
                                       scope='resblock1_0'))


            for i in range(1, residual_list[1]):
                x = tf.cond(tf.greater(self.is_training, 0),
                            lambda: residual_block(x, channels=ch * 2, is_training=True, downsample=False,
                                           scope='resblock1_' + str(i)),
                            lambda: residual_block(x, channels=ch * 2, is_training=False, downsample=False,
                                           scope='resblock1_' + str(i)))

            ########################################################################################################

            x = tf.cond(tf.greater(self.is_training, 0),
                        lambda: residual_block(x, channels=ch * 4, is_training=True, downsample=True,
                                       scope='resblock2_0'),
                        lambda: residual_block(x, channels=ch * 4, is_training=False, downsample=True,
                                       scope='resblock2_0'))

            for i in range(1, residual_list[2]):
                x = tf.cond(tf.greater(self.is_training, 0),
                            lambda: residual_block(x, channels=ch * 4, is_training=True, downsample=False,
                                           scope='resblock2_' + str(i)),
                            lambda: residual_block(x, channels=ch * 4, is_training=False, downsample=False,
                                           scope='resblock2_' + str(i)))
            ########################################################################################################
            res.append(x)

            x = tf.cond(tf.greater(self.is_training, 0),
                        lambda: residual_block(x, channels=ch * 8, is_training=True, downsample=True,
                                       scope='resblock_3_0'),
                        lambda: residual_block(x, channels=ch * 8, is_training=False, downsample=True,
                                       scope='resblock_3_0'))


            for i in range(1, residual_list[3]):
                res.append(x)
                x = tf.cond(tf.greater(self.is_training, 0),
                            lambda: residual_block(x, channels=ch * 8, is_training=True, downsample=False,
                                           scope='resblock_3_' + str(i)),
                            lambda: residual_block(x, channels=ch * 8, is_training=False, downsample=False,
                                           scope='resblock_3_' + str(i)))

            ########################################################################################################

            x = tf.cond(tf.greater(self.is_training, 0),
                        lambda: resnet.batch_norm(x, True, scope='batch_norm'),
                        lambda: resnet.batch_norm(x, False, scope='batch_norm'), name='conv_4_2')
            # conv_4_2 = tf.Variable(x, name='conv_4_2')
            # conv_4_2 = x
            x = resnet.relu(x)
            # print(x)
            x = resnet.global_avg_pooling(x)
            res.append(x)
            # print(x)
            x = resnet.fully_conneted(x, units=self.num_class, scope='logit')
            res.append(x)
            # print(x)
            return res

    def error_rate(self):
        mistakes = tf.not_equal(tf.argmax(self.Y, 1), tf.argmax(self.prediction[-1], 1))
        return tf.reduce_mean(tf.cast(mistakes, tf.float32))
    
def cross_entropy(self):
    log_prob = tf.math.log(tf.nn.softmax(self.prediction[-1]) + 1e-12)
    cross_entropy = - tf.reduce_sum(self.Y * log_prob)
    return cross_entropy

def optimizer(self):
    optimizer = tf.train.AdamOptimizer(self.lr, self.b1, self.b2, self.epsilon)
    snnl = self.snnl()
    soft_nearest_neighbor = self.factor_1 * snnl[0] + self.factor_2 * snnl[1] + self.factor_3 * snnl[2]
    soft_nearest_neighbor = tf.cast(tf.greater(tf.math.reduce_mean(self.w), 0), tf.float32) * soft_nearest_neighbor
    return optimizer.minimize(self.ce_loss - soft_nearest_neighbor), tf.gradients(snnl, self.temp)

我想问一下怎么才能将这个模型的所有参数拿出来(后续再转成torch模型),还有一个就是load上来之后可以实现函数的调用吗?
或者有没有别的更便捷些的方法呢?
万分感谢大家的解答!

  • 写回答

1条回答 默认 最新

  • 爱晚乏客游 2022-11-16 10:16
    关注

    看的模型,和session中的运算关系不大吧?
    两个模型是什么意思?有两个模型结构的还是啥?有详细的代码贴出来看下吗

    评论

报告相同问题?

问题事件

  • 修改了问题 11月16日
  • 创建了问题 11月15日

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序