Truly


  • Home

  • Archives

virtual-function

Posted on 2018-12-27 | In cpp

cited from https://www.geeksforgeeks.org/pure-virtual-functions-and-abstract-classes/

Pure Virtual Functions and Abstract Classes in C++

Sometimes implementation of all function cannot be provided in a base class because we don’t know the implementation. Such a class is called abstract class. For example, let Shape be a base class. We cannot provide implementation of function draw() in Shape, but we know every derived class must have implementation of draw(). Similarly an Animal class doesn’t have implementation of move() (assuming that all animals move), but all animals must know how to move. We cannot create objects of abstract classes.

A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration. See the following example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream> 
using namespace std;

class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};

// This class inherits from Base and implements fun()
class Derived: public Base
{
int y;
public:
void fun() { cout << "fun() called"; }
};

int main(void)
{
Derived d;
d.fun();
return 0;
}

output:

1
fun() called

Some Interesting Facts

  • A class is abstract if it has at least one pure virtual function.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    // pure virtual functions make a class abstract 
    #include<iostream>
    using namespace std;

    class Test
    {
    int x;
    public:
    virtual void show() = 0;
    int getX() { return x; }
    };

    int main(void)
    {
    Test t;
    return 0;
    }

    output:

    1
    2
    3
    Compiler Error: cannot declare variable 't' to be of abstract
    type 'Test' because the following virtual functions are pure
    within 'Test': note: virtual void Test::show()
  • We can have pointers and references of abstract class type.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #include<iostream> 
    using namespace std;

    class Base
    {
    public:
    virtual void show() = 0;
    };

    class Derived: public Base
    {
    public:
    void show() { cout << "In Derived \n"; }
    };

    int main(void)
    {
    Base *bp = new Derived();
    bp->show();
    return 0;
    }
  • If we do not override the pure virtual function in derived class, then derived class also becomes abstract class.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    #include<iostream> 
    using namespace std;
    class Base
    {
    public:
    virtual void show() = 0;
    };

    class Derived : public Base { };

    int main(void)
    {
    Derived d;
    return 0;
    }

    output:

    1
    2
    3
    Compiler Error: cannot declare variable 'd' to be of abstract type 
    'Derived' because the following virtual functions are pure within
    'Derived': virtual void Base::show()
  • An abstract class can have constructors.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    #include<iostream> 
    using namespace std;

    // An abstract class with constructor
    class Base
    {
    protected:
    int x;
    public:
    virtual void fun() = 0;
    Base(int i) { x = i; }
    };

    class Derived: public Base
    {
    int y;
    public:
    Derived(int i, int j):Base(i) { y = j; }
    void fun() { cout << "x = " << x << ", y = " << y; }
    };

    int main(void)
    {
    Derived d(4, 5);
    d.fun();
    return 0;
    }

    Output:

    1
    x = 4, y = 5

Interface vs Abstract Classes:

An interface does not have implementation of any of its methods, it can be considered as a collection of method declarations. In C++, an interface can be simulated by making all methods as pure virtual. In Java, there is a separate keyword for interface.

CASENet

Posted on 2018-10-25

author

Zhiding Yu
Carnegie Mellon University
yzhiding@andrew.cmu.edu
Chen Feng Ming-Yu Liuy Srikumar Ramalingamy
Mitsubishi Electric Research Laboratories (MERL)
cfeng@merl.com, mingyul@nvidia.com, srikumar@cs.utah.edu

abstract

Boundary and edge cues are highly beneficial in improving a wide variety of vision tasks such as semantic segmentation, object recognition, stereo, and object proposal generation. Recently, the problem of edge detection has been revisited and significant progress has been made with deep learning. While classical edge detection is a challenging binary problem in itself, the category-aware semantic edge detection by nature is an even more challenging multi-label problem. We model the problem such that each edge pixel can be associated with more than one class as they appear in contours or junctions belonging to two or more semantic classes. To this end, we propose a novel end-to-end deep semantic edge learning architecture based on ResNet and a new skip-layer architecture where category-wise edge activations at the top convolution layer share and are fused with the same set of bottom layer features. We then propose a multi-label loss function to supervise the fused activations. We show that our proposed architecture benefits this problem with better performance, and we outperform the current state-of-the-art semantic edge detection methods by a large margin on standard data sets such as SBD and Cityscapes.

SLAM

Posted on 2018-10-19 | In computer vision

introducation

simultaneous localization and mapping
Attempts to reconstruct the 3D world & camera position in real-time.

pipeline

  1. extract local features
  2. establish 2D-3D matches
  3. camera pose estimation
    • RANSAC + n-Point-Pose Algorithm

CSAPP-review

Posted on 2018-10-18 | In system

process and thread

  • process:
    • process context + code + stack
    • thread + code, data, kernel context
  • thread:
    • Each thread has its own logical control flow Each thread has its own logical control flow
    • Each thread shares the same code, data and kernel context
    • Each thread has its own stack for local variables

32bit machine and 64 bit machine

virtual memory

  • idea: cache

MLE-MAP

Posted on 2018-10-18

士不可以不弘毅

Posted on 2018-10-18 | In 思考

士不可以不弘毅,任重而道远。
最近师兄要考qua,听他们聊phd考qua的一些事项,主要考察两点:

  1. 一点是domain knowledge,教授从不同的角度切入去问你问题,问到你不会为止,来探知你knowledge的boundary。
  2. 基本的research的能力,就是你需要很好的描述你要解决的问题challenge在哪里,大家是一般是怎么做的,然后针对这个问题你自己又有什么新思路,和一些初步的探索。在向教授们阐述和讨论的过程中,你还要体现研究这个问题的impact。
    然后CMU,这边考qua就两次机会,不过的话就要quit了。

这些给了我在找工作上面很多启示,

  1. 和phd比我,找工作拿那么多面试,其实试错的机会特别多,而他们只有两次,这样子去想,其实自己的一些失利也不算什么。
  2. 我发现自己面试有时候是算法能想出来但是不能熟练的做出来,有时候是dl,ml,cs的一些问题卡主,其实换个角度想就是自己的知识体系(domain knowledge)不完善。这点在之后的面试准备上面要注意了。对于,刷题别人常常和我说,多刷,但是我现在感觉更重要的是多总结,什么算法适合什么样子的题目,然后他们有什么区别,利弊在哪里。还有dl和ml里面的算法和model,什么时候那些场景适合用这个model,他们异同是什么。还有就是cs这块,由于很长时间没有去碰了,也有些生疏了,但是如果要在系统上面deploy algorithms,这个也是很重要的门槛,需要好好复习和整理。
  3. 上面讨论了自己的知识体系不完善,但另一个角度去想,和phd们比,自己对自己要求还是不够严格,他们过qua必须要有很solid的domain knowledge,如果自己能做到他们这样子,我感觉在找工作上面应该也没有什么问题了。
  4. 关于research换个角度想是独立解决问题的能力,其实这个是反应在自己的项目里面的,对于自己要解决的问题,自己也要看看这个domain里面大家都是怎么做的,自己看下能不能找到更好的办法去解决,不要仅仅满足做了一个项目,有点这个经验,要更有追求一点,找到更simple but robust的solution去解决sensor fusion的问题。这个还有很多可以挖的地方。

想起来,一个段子,对于有些人来说,他们考100分是因为卷子只有100分,好好提高自己,实力到了,很多事情自然就都顺了。自己从4月份开始写博客到现在,感觉还是有很多收获的,知识就是一点一滴的去积累,继续坚持吧,不走捷径就是捷径。

tf-idf

Posted on 2018-10-17 | In machine learning

tf: term frequency

  • 定义:
    词频(term frequency,tf)指的是某一个给定的词语在该文件中出现的频率。这个数字是对词数(term count)的归一化,以防止它偏向长的文件。
  • term:
    $tf_{i, j} = \frac{n_{i, j}}{\sum_k n_{k, j}}$ $n_{i, j}$表示词i在文件$d_j$里面出现的次数

inverse docment frequency

  • 定义:
    可以由总文件数目除以包含该词语之文件的数目,再将得到的商取以10为底的对数得到
  • term:
    $idf_i = lg \frac{|D|}{|{j:t_i \in d_j}| + 1}$

leetcode212-word-search-II

Posted on 2018-10-10 | In leetcode

Given a 2D board and a list of words from the dictionary, find all words in the board.

Each word must be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

Example:

1
2
3
4
5
6
7
8
9
10
Input: 
words = ["oath","pea","eat","rain"] and board =
[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]

Output: ["eat","oath"]

  • idea:
    trie + dfs

  • code:

    • trie:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      class TrieNode {
      public:
      bool is_end;
      vector<TrieNode*> children;
      TrieNode() {
      is_end = false;
      children = vector<TrieNode*>(26, NULL);
      }
      };

      class Trie {
      public:
      TrieNode* getRoot() {
      return root;
      }

      Trie(vector<string>& words) {
      root = new TrieNode;
      for (int i = 0; i < words.size(); i++) {
      addWord(words[i]);
      }
      }

      void addWord(const string& word) {
      TrieNode* cur = root;
      for (int i = 0; i < word.size(); i++) {
      int index = word[i] - 'a';
      if (cur->children[index] == NULL) {
      cur->children[index] = new TrieNode();
      }
      cur = cur->children[index];
      }
      cur->is_end = true;
      }

      private:
      TrieNode* root;
      };
    • body

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      class Solution {
      public:
      vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
      Trie* trie = new Trie(words);
      TrieNode* root = trie->getRoot();
      set<string> result_set;
      row = board.size();
      col = board[0].size();
      for (int r = 0; r < row; r++) {
      for (int c = 0; c < col; c++) {
      findWords(board, r, c, root, "", result_set);
      }
      }

      vector<string> result;
      for (auto candidate : result_set) {
      result.push_back(candidate);
      }
      return result;
      }

      private:
      int row;
      int col;
      void findWords(vector<vector<char>>& board, int r, int c, TrieNode* root, string word, set<string>& result) {
      if (r < 0 || r == row || c < 0 || c == col || board[r][c] == ' ') {
      return;
      }

      if (root->children[board[r][c] - 'a'] != NULL) {
      word = word + board[r][c];
      root = root->children[board[r][c] - 'a'];
      if (root->is_end) {
      result.insert(word);
      }

      char backtrace = board[r][c];
      board[r][c] = ' ';
      findWords(board, r + 1, c, root, word, result);
      findWords(board, r - 1, c, root, word, result);
      findWords(board, r, c + 1, root, word, result);
      findWords(board, r, c - 1, root, word, result);
      board[r][c] = backtrace;
      }
      }
      };

人脸识别

Posted on 2018-10-05 | In computer vision

Expression Invariant 3D Face Recognition with a Morphable Model

Author

Brian Amberg
brian.amberg@unibas.ch
Reinhard Knothe
reinhard.knothe@unibas.ch
Thomas Vetter
thomas.vetter@unibas.ch

Abstract

We describe an expression-invariant method for face recognition by fitting an identity/expression separated 3D Morphable Model to shape data. The expression model greatly improves recognition and retrieval rates in the uncooperative setting, while achieving recognition rates on par with the best recognition algorithms in the face recognition great vendor test. The fitting is performed with a robust nonrigid ICP algorithm. It is able to perform face recognition in a fully automated scenario and on noisy data. The system was evaluated on two datasets, one with a high noise level and strong expressions, and the standard UND range scan database, showing that while expression invariance increases recognition and retrieval performance for the expression dataset, it does not decrease performance on the neutral dataset. The high recognition rates are achieved even with a purely shape based method, without taking image data into account.

Conceptions:

  • Registration: Registering two surfaces means finding a mapping between a template surface and a target surface that describes the position of semantically corresponding points.
  • Dense registration: methods find a mapping from each point in the template onto the target while sparse methods find correspondence only for selected feature points. We present a dense registration method.
  • Regularisation:
    • To choose the “correct” deformation from all possible warps, a registration algorithm has to impose constraints on the deformation. In this context, this is called regularisation of the deformation field.
    • for here: minimizing the difference between transformations acting on neighbouring vertices of a mesh.

Fitting

The main difference, is that the deformation model is a statistical model and the optimisation in each step is an iterative method, which finds the minimum of a convex function.

  • steps:
    Repeat until convergence:
    1. Find candidate correspondences by searching for the closest compatible point for each model vertex.
    2. Weight the correspondences by their distance using a robust estimator.
    3. Fit the 3DMM to these correspondences using a regularization strength of $\theta_i$.
    4. Continue with the lower $\theta_{i + 1}$ if the median change in vertex position is smaller than a threshold.

Nonrigid optimal step ICP algorithms

Deformation

CNN

Posted on 2018-10-03 | In deep learning

Group convolution

1…456…17

Chu Lin

去人迹罕至的地方,留下自己的足迹。

166 posts
17 categories
94 tags
© 2022 Chu Lin
Powered by Hexo
|
Theme — NexT.Muse v5.1.4