基於Python的人臉檢測與分類

專注的阿熊發表於2022-05-27

def loadModel(self,model_path = RPN_BATCH_PATH):

         """

         model_path 中載入模型

         """

         with tf.compat.v1.variable_scope('RPN', reuse=tf.compat.v1.AUTO_REUSE):

             weights = {

                 'rpn_1':tf.compat.v1.get_variable(name = 'w_rpn_1_1',shape = [3,3,K*K*2,1]), # : 1:1 的卷積

                 'rpn_2':tf.compat.v1.get_variable(name = 'w_rpn_1_2',shape = [3,6,K*K*2,1]), # : 1:2 的卷積

                 'rpn_3':tf.compat.v1.get_variable(name = 'w_rpn_2_1',shape = [6,3,K*K*2,1]), # : 2:1 的卷積

                 'rpn_4':tf.compat.v1.get_variable(name = 'w_rpn_2_2',shape = [6,6,K*K*2,1]),

                 'rpn_5':tf.compat.v1.get_variable(name = 'w_rpn_2_4',shape = [6,12,K*K*2,1]),

                 'rpn_6':tf.compat.v1.get_variable(name = 'w_rpn_4_2',shape = [12,6,K*K*2,1]),

                 'rpn_7':tf.compat.v1.get_variable(name = 'w_rpn_4_4',shape = [12,12,K*K*2,1]),

                 'rpn_8':tf.compat.v1.get_variable(name = 'w_rpn_4_8',shape = [12,24,K*K*2,1]),

                 'rpn_9':tf.compat.v1.get_variable(name = 'w_rpn_8_4',shape = [24,12,K*K*2,1])

             }

             biases = {

                 'rpn_1':tf.compat.v1.get_variable(name = 'b_rpn_1_1',shape = [1,]),

                 'rpn_2':tf.compat.v1.get_variable(name = 'b_rpn_1_2',shape = [1,]),

                 'rpn_3':tf.compat.v1.get_variable(name = 'b_rpn_2_1',shape = [1,]),

                 'rpn_4':tf.compat.v1.get_variable(name = 'b_rpn_2_2',shape = [1,]),

                 'rpn_5':tf.compat.v1.get_variable(name = 'b_rpn_2_4',shape = [1,]),

                 'rpn_6':tf.compat.v1.get_variable(name = 'b_rpn_4_2',shape = [1,]),

                 'rpn_7':tf.compat.v1.get_variable(name = 'b_rpn_4_4',shape = [1,]),

                 'rpn_8':tf.compat.v1.get_variable(name = 'b_rpn_4_8',shape = [1,]),

                 'rpn_9':tf.compat.v1.get_variable(name = 'b_rpn_8_4',shape = [1,])

             }

         with tf.compat.v1.variable_scope('BBOX', reuse=tf.compat.v1.AUTO_REUSE):

             weights['bbox'] = tf.compat.v1.get_variable(name = 'w_bbox',shape = [K,K,K*K*2,4]) # 分類

             biases['bbox'] = tf.compat.v1.get_variable(name = 'b_bbox',shape = [4,]) # 分類

         weights['down'] = tf.compat.v1.get_variable(name = 'w_down',shape = [1,1,2048,1024])# 降取樣

         weights['feature'] = tf.compat.v1.get_variable(name = 'w_feature',shape = [1,1,1024,K*K*2])

         biases['down'] = tf.compat.v1.get_variable(name = 'b_down',shape = [1024,]) # 降取樣

         biases['feature'] = tf.compat.v1.get_variable(name = 'b_feature',shape = [K*K*2,])

         self.img = tf.compat.v1.placeholder(dtype = tf.float32,shape = (1,self.h,self.w,3))

         # 使用無 pool1&pool5 RESNET 101

         net, endpoints = my_resnet(self.img,global_pool = False,num_classes=None,is_training=True,reuse = tf.compat.v1.AUTO_REUSE) # net's w&h = original_img's w&h / 16

         net = tf.nn.conv2d(input = net,filter = weights['down'],strides = [1, 1, 1, 1],padding = 'VALID')

         net =跟單網gendan5.com tf.add(net,biases['down'])

         # 生成 feature_map

         self.feature_map = tf.nn.conv2d(input = net,filter = weights['feature'],strides = [1, 1, 1, 1],padding = 'VALID')

         self.feature_map = tf.add(self.feature_map,biases['feature'])

         self.pred_rpn = [None]*9

         for i in range(9):

             r = tf.nn.conv2d(input = self.feature_map,filter = weights['rpn_' + str(i+1)],strides = [1, 1, 1, 1],padding = 'VALID')

             r = tf.reshape(r,r.get_shape().as_list()[1:-1])

             self.pred_rpn[i] = tf.add(r,biases['rpn_' + str(i+1)])

             self.pred_rpn[i] = tf.sigmoid(self.pred_rpn[i])

         self.select = tf.compat.v1.placeholder(dtype = tf.float32,shape = (self.RPN_RESULT_NUM,K,K,K*K*2))

         self.pre_bbox = tf.nn.conv2d(self.select,weights['bbox'],[1,1,1,1],padding = 'VALID')

         self.pre_bbox = tf.add(self.pre_bbox,biases['bbox'])

         self.pre_bbox = tf.reshape(self.pre_bbox,shape = (self.RPN_RESULT_NUM,4))

         saver = tf.compat.v1.train.Saver(tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.TRAINABLE_VARIABLES))

         self.sess =  tf.compat.v1.Session()

         init = tf.compat.v1.global_variables_initializer()

         self.sess.run(init)

         saver.restore(self.sess,RPN_BATCH_PATH)


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2897592/,如需轉載,請註明出處,否則將追究法律責任。

相關文章