예제 #1
0
    def test(self):
        TXT = 'train/testImageList.txt'
        template = '''################## Summary #####################
        Test Number: %d
        Time Consume: %.03f s
        FPS: %.03f
        LEVEL - %d
        Mean Error:
            Left Eye       = %f
            Right Eye      = %f
            Nose           = %f
            Left Mouth     = %f
            Right Mouth    = %f
        Failure:
            Left Eye       = %f
            Right Eye      = %f
            Nose           = %f
            Left Mouth     = %f
            Right Mouth    = %f
        '''

        t = time.clock()
        # Restore variables from disk.
        try:
            saver = tf.train.Saver()
            ckpt = os.path.join(self.logdir, 'model.ckpt')
            saver.restore(self.sess, ckpt)
            print("Model restored.")
        except ValueError:
            print("Model not in model.ckpt")
            return

        data = getDataFromTxt(TXT)
        error = np.zeros((len(data), 5))
        for i in range(len(data)):
            imgPath, bbox, landmarkGt, _, _, _, _ = data[i]
            print imgPath
            img = cv2.imread(imgPath, cv2.IMREAD_GRAYSCALE)

            assert (img is not None)
            logger("process %s" % imgPath)

            # Crop
            f_bbox = bbox.subBBox(-0.05, 1.05, -0.05, 1.05)
            f_face = img[int(f_bbox.top):int(f_bbox.bottom) + 1,
                         int(f_bbox.left):int(f_bbox.right) + 1]

            # Resize
            f_face = cv2.resize(f_face, (39, 39))
            f_face = f_face.reshape((39, 39, 1))
            f_face = f_face / 255.0
            landmarkP = self.sess.run(self.yhat_1,
                                      feed_dict={self.x: [f_face]})
            landmarkP = landmarkP.reshape((5, 2))

            # real landmark
            landmarkP = bbox.reprojectLandmark(landmarkP)
            landmarkGt = bbox.reprojectLandmark(landmarkGt)
            error[i] = evaluateError(landmarkGt, landmarkP, bbox)

        t = time.clock() - t
        N = len(error)
        fps = N / t
        errorMean = error.mean(0)

        # failure
        failure = np.zeros(5)
        threshold = 0.05
        for i in range(5):
            failure[i] = float(sum(error[:, i] > threshold)) / N

        # log string
        s = template % (N, t, fps, 0, errorMean[0], errorMean[1], errorMean[2], \
            errorMean[3], errorMean[4], failure[0], failure[1], failure[2], \
            failure[3], failure[4])
        print s

        name = 'fl_' + str(self.attribute) + '_crossstitch'
        logfile = 'log/' + name + '.log'
        with open(logfile, 'w') as fd:
            fd.write(s)
            fd.write('\n')

            alphas = [
                var for var in tf.all_variables()
                if 'alpha' in var.name and 'Adam' not in var.name
            ]
            a_ws = [
                var for var in tf.all_variables()
                if 'a_w' in var.name and 'Adam' not in var.name
            ]
            b_ws = [
                var for var in tf.all_variables()
                if 'b_w' in var.name and 'Adam' not in var.name
            ]

            for alpha in alphas:
                fd.write(alpha.name + ": " +
                         str(self.sess.run(tf.reduce_mean(alpha))) + '\n')

            for a_w, b_w in zip(a_ws, b_ws):
                fd.write(a_w.name + ": " +
                         str(self.sess.run(tf.reduce_mean(a_w))) + '\n')
                fd.write(b_w.name + ": " +
                         str(self.sess.run(tf.reduce_mean(b_w))) + '\n')

        # plot error hist
        plotError(error, name)
예제 #2
0
    def test(self):
        template = '''################## Summary #####################
        Test Number: %d
        Time Consume: %.03f s
        FPS: %.03f
        LEVEL - %d
        Mean Error:
            Left Eye       = %f
            Right Eye      = %f
            Nose           = %f
            Left Mouth     = %f
            Right Mouth    = %f
        Failure:
            Left Eye       = %f
            Right Eye      = %f
            Nose           = %f
            Left Mouth     = %f
            Right Mouth    = %f
        '''

        t = time.clock()
        # Restore variables from disk.
        try:
            saver = tf.train.Saver()
            ckpt = os.path.join(self.logdir, 'model.ckpt')
            saver.restore(self.sess, ckpt)
            print("Model restored.")
        except ValueError:
            print("Model not in model.ckpt")
            return

        TXT = 'train/testImageList.txt'
        data = getDataFromTxt(TXT)
        error = np.zeros((len(data), 5))
        for i in range(len(data)):
            imgPath, bbox, landmarkGt, _, _, _, _ = data[i]
            img = cv2.imread(imgPath, cv2.IMREAD_GRAYSCALE)
            
            assert(img is not None)
            logger("process %s" % imgPath)

            #landmarkP = P(img, bbox)

            # Crop
            f_bbox = bbox.subBBox(-0.05, 1.05, -0.05, 1.05)
            f_face = img[int(f_bbox.top):int(f_bbox.bottom)+1,int(f_bbox.left):int(f_bbox.right)+1]

            # Resize
            f_face = cv2.resize(f_face, (39, 39))
            f_face = f_face.reshape((39, 39, 1))
            f_face = f_face / 255.0
            landmarkP = self.sess.run(self.yhat, feed_dict={self.x: [f_face]})
            landmarkP = landmarkP.reshape((5,2))

            # real landmark
            landmarkP = bbox.reprojectLandmark(landmarkP)
            landmarkGt = bbox.reprojectLandmark(landmarkGt)
            error[i] = evaluateError(landmarkGt, landmarkP, bbox)

        t = time.clock() - t
        N = len(error)
        fps = N / t
        errorMean = error.mean(0)
        print(error)
        print(errorMean)
        # failure
        failure = np.zeros(5)
        threshold = 0.05
        for i in range(5):
            failure[i] = float(sum(error[:, i] > threshold)) / N
        # log string
        s = template % (N, t, fps, 0, errorMean[0], errorMean[1], errorMean[2], \
            errorMean[3], errorMean[4], failure[0], failure[1], failure[2], \
            failure[3], failure[4])
        print s

        logfile = 'log/regression.log'
        with open(logfile, 'w') as fd:
            fd.write(s)

        # plot error hist
        plotError(error, 'regression')