Hexo

  • Home

  • Archives

CPP-Primer-Notes

Posted on 2019-07-23 Edited on 2019-08-26

Chapter 2 Varibales and Basic Types

Some languages, such as Smalltalk and Python, check types at run time. In contrast, C++ is a statically typed language: type checking is done at compile time.
As a consequence, the compiler must know the type of every name used in the program.

Types determine the meaning of the data and operations in our programs.

2.1 Primitive Built-in Types

Includes arithmetic types and void. The void type has no associated values and can be used in only a few circumestances.

2.1.1 Arithmetic Types

Includes integral types and floating-point types. The standard guarantees minimum sizes of different type. Different compiler are allowed to use larger sizes for these tyes.

  • Q: What is significant digits?
  • A: Effective decimal numbers
    • float has 6 significant digits minimum;
    • double has 10 signifiicant digits minimum;

A char is the same size as a single machine byte. A “word” si referred to a small number of bytes. Most computers a byte contains 8 bits and a word is either 32 or 63 bits, that is, 4 or 8 bytes.

The Type determins how many bits are used and how to interpret those bits.

Signed an Unsigned Types
The types int, short, long, long long are all signed. The type unsigned int may be abbreviated as unsigned.

There are 3 distinct basic character types: char, unsigned char, signed char. Which of the other two character representatinos is equivalent to char depends on the compiler.

IN an unsigned type, all the bits represent the value. An 8-bit unsigned char can hold the values from 0 through 255 inclusize. An 8-bit signed char is guaranteed to be able to hold values from -127 through 127. Most modern machines use representations that allow values from -128 through 127.

  • Do not use plain char or bool in arithmetic expressions. Use them only to hold characters or thuth values. Computtations using char are especially problematic because char is signed on some machines and unsigned on others.
  • Use double for floating-point computatins. float usually odes not have enough precision and the cosst of double-percision calculations versus single-precision is negligible. In fact, on some machines, double-precision operations are faster than single[P34].

2.1.2 Type Conversions

  1. nonbool arithmetic types to a bool object, the result is false if the value is 0 and true otherwise;
  2. bool true is 1 and bool false is 0;
  3. Assigning a floating-point value to an object of integral type, the value is truncated.
  4. When an integral value to an object of floating-point type, the frational part is zero. Precision may be lost ifg the integer has more bits than the floating-point object can accommodate.
  5. OOR (out-of-range) value to unsigned type, the result is the remainder of the value modulo the number of values the target type can hold.
  6. OOR value to signed type, the reult is undefined. Undefined behavior results from errors that the compiler is not requierd to detect.

2.1.3 Literals

Integer and Floating-Point Literals:

  • decimal: You should know what a decimal is you stpuid fuck;
  • octal: Integer literals that begin with 0, for example 024;
  • hexadecimal: Integer literals that begin with 0x or 0X;

Note: For a negative decimal literal, -42, the minus sign is not part of the literal, the minus sign is an operator that negates the value of its (literal) operand.

Character and Character String Literals:
The compiler appends a null character (\0) to every string literal. Thus the actual size of a string literal is one more than its apparent size.

Escape Sequences:An escape sequence begins with a backslash.

Specifying the Type of a Literal
We can override the default type of an integer, floating-point, or character lietral by supylying a suffix or prefix from Table 2.2 [P40];

1
42ULL     // unsigned integer litearal, type is unsigned long long

Boolean and Pointer Literals:

1
bool test = false;  // true or false are literals of type bool;

2.2 Variables

2.2.1 Variable Definitions

Most generally, an object is a region of memory that can contain data and has a type.

Initilaizer: An object gets the specified value at the moment it is created.

The initialization an d assignment are different operations in C++. Initialization happens when a variable is given a value when it is created. Assignment obliterates an object’s current value and replaces that value with a new one.

List Initialization:

1
2
int units_sold = {0};
int units_sold{0};

The curly braces for initializeation was introduced as part of the new standard, which is referred to as list initalization.

Import: this form of initialization will not be allowed if the initializer might lead to the loss of information by compiler.

Default INitialization:
When a varibale is defined without an initializer, it is default initialized. One excpetion, variables of built-in type defined inside a function are uninitialized.

2.2.2 Variable Declarations and Definitions

Use extern to explict declare variable without defination. It is an error to provide an initializer on an extern inside a function. extern with an initializer is an defination.

1
2
extern int i;  // declares but does not define i
int j; // declares and defines j

Note: Varibales must be defined exactly once but can be declared many times.

2.2.3 Identifiers

  • The identifier in programs may not contain two consective underscores;
  • Nor can an identifier begin with an underscore followed immediately by an uppercase letter;
  • Identifiers defined outside a function may not begin with an underscore.

2.2.4 Scope of a Name

Nested Scopes: The global scope has no name, hence when the scope operator has an empty left-hand side, it is a request to fetch the name on the right-hand side from the global scope.

2.3 Compound Types

A compound type is a type thas is defined in terms of another type, including references and pointers.
A declaration is a base type followed by a list of declarators.

2.3.1 References

Reference refers to “lvalue reference”, instead of “rvalue reference” introduced in new standard. A reference defines an alternative name for an object.

1
2
3
int ival = 1024;
int &refVal = ival;
int &refVal2;

A Reference Is an Alias
The reference is bound to its initializer, and there is no way to rebind a reference to refer to a different object. A reference must be initialized.
A reference is just another name for an already existing object.
After a reference has been defined, all operations on that reference are actuall operations on the object to which the reference is bound.

Reference Denfinitions
With 2 exceptions that will be covered in S 2.4.1 and 15.2.3, the type of reference and the obect to which the reference refers must match exactly. A reference may be bound only to an object, not to a literal or to the result of a more general experession.
[cmake]

2.3.2 Pointers

A Pointer is a compound type that “points to” another type.

1
2
int *ip1, *ip2;
double dp, *dp2;

Taking the Address of an Object
A poiter holds the address of another object. We get the address of an object by usin the address-of operator (the & operator).

1
2
int ival = 42;
int *p = &ival; // p holds the address of ival; p is a pointer to ival

With 2 excpetion will be covered in S:2.4.2 and S:15.2.3, the typees of the pointer and the object ot which it points must muatch.

1
2
3
4
5
double dval;
double *pd = &dval;
double *pd2 = pd;
int *pi = pd;
pi = &dval;

Point Value
The walue (i.e., the address) stored in a pointer can be in one of four states:

  1. An object;
  2. The location just immediately past the end of an object;
  3. A unll pointer, indicating that is is not bound to any object;
  4. Invalid, values other than the preceding three are invalid.

Using a Pointer to Access an Object
Ust the dereference operator (the * operator) to access the object:

1
2
3
int ival = 42;
int *p = *ival;
std::cout << *p;

Dereferencoing a pointer yields the object ot which the pointer points.

Note: Some symbols have multiple meannings

1
2
3
4
5
6
int i = 42;
int &r = i;
int *p;
p = &i; // & used as the address-of operation
*p = i // * used as dereference operation
int &r2 = *p; // & as part of the declaration, * as derefernec operation

Null Pointer:

1
2
3
4
5
int *p1 = nullptr;
int *p2 = 0;

/* must #include cstdlib */
int *p3 = NULL;

Older programs sometimes use a preprocessor variable named NULL, which the cstdlib header defines as 0. A preprocessor is a program that runs before the compiler. Preprocessor varibales are managed by the preprocessor, and are not part of the std namespace.

Assignment and Pointers
A reference is not an object.

Other Pointer Operations
Any nonzero pointer evalutaes as true. Two pointers are equal if they hold the same address or both are null pointers. In continously memory, a pointer one past the end of a different object ot heldo the same ddress as a pointer to an object.

void* Pointers
The type void* is a special pointer type that can hold the address of any object, it holds an address but the type of the object at that address is unknown. Generally, a void* pointer is used to dieal with memory as memory, rather than using the pointer to access the object stored in that memory.

2.3.3 Understanding Compound Type Declarations

Defining Multiple Variable: It could be really confusing, put whitespace between the type modifier and the name being declared. Use First Style to declare variables.

1
int* p1, p2;  //  p1 is a pointer to int; p2 is an int

Pointer to Pointers: We indicate each pointer level by its own *. ** for a pointer to a pointer, *** for a pointer to a pointer to a pointer.

1
2
3
4
5
6
7
8
9
int ival = 1024;
int * pi = &ival; // pi points to an int
int **ppi = &pi; // ppi points to a pointer points to an int

cout << "The value of ival\n"
<< "direct value: " << ival << "\n"
<< "indirect value: " << *pi << "\n"
<< "doubly indirect value: " << **ppi // deference twice
<< endl;

Reference to Pointers: A reference is not an object. Hence we may not have a pointer to a reference. But we can define a reference to a pointer.

1
2
3
4
5
int val = 42;
int *pi;
int *&r = pi; // r is a reference to pi
r = &val; // r is a reference, assign to r equals to assigning to pi
*r = 0; // dereference r yields i, this is assigning i to be 0

Notes: As illustrate above, you can use **pi to indicate a pointer pointing to a pointer, but you can’t use &&p to indicate a reference refering to a reference, cause reference is not a project.

const Qualifier

Since wo can’t change the value of const, it must be initialized.

By Default, const Objects Are Local to a File: When a const object is initialized from a compile-time constant, the compiler will usually repalce uses of the varibale with its corresponding value during compilation. When we define a const with the same name in multiple files, it is as if we had written definitions for separate variables in each file. If a single instance of a const variable, we use the keyword extern on both its definition and declaration(s).

e.g., if you what to share const accross different files, you must define the varibale as extern.

2.4.1 Reference to const

A reference to const cannot be used to change the object to which the reference is bound.
TERMINOLOGY: const Reference is a reference to const.

Initialization and References to const: One of the 2 exceptions to the rule that the type of a reference must match the type of the object to which it refers: we can initialize a reference to const from any expression that can be converted to the type of the reference.

1
2
3
4
5
int i = 42;
const int &r1 = i; // we can bind a const int& to a plain
const int &r2 = 42; // ok: r1 is a reference to const
const int &r3 = r1 * 2; // ok: r3 is a reference to const
int &r4 = r1 * 2; // error: r4 is a nonconst reference

Important: AReference to const May Refer to an Object That is Not const:

1
2
3
4
5
int i = 42;
int &r1 = i; // r1 bound to i
const int &r2 = i; // r2 also bound to i; but connot be used to change i;
r1 = 0; // r1 is not const; i is now 0
r2 = 0; // error: r2 is a reference to const

2.4.2 Pointers and const

Like a reference to cost, a pointer to const may not be used to change the object to which the pointer points.
Important: There is no guarantee that an object pointed to by a pointer to const won’t change.

const Pointers: We indicate that the pointer is const by putting the const after *.

1
2
3
4
int errNumb = 0;
int *const curErr = &errNumb; // curErr will always point to errNumb
const double pi = 3.14159;
const double *const pip = &pi; // pip is a const pointer to a const object

2.4.3 Top-level const

The term top-level const to indicate that the pointer itself is a const. When a pointer can point to a const object, we refer to that const as a low-level const.

2.4.4 constexpr and Constant Expressions

A constant expression is an expression whose value cannot change tnd that can be evalutaed at compile time.

1
2
3
4
const int max_files = 20;         //  this is const expression
const int limit = max_files + 1; // this is const expression
int staff_size = 27; // not a constant expression
cnost int sz = get_size(); // not a constant because it is not known util run time

C++ 11: Under new standard, we can aske the compiler to verify that a varibale is a constant expression by declaring the varibale in a constexpr declaration. Also a function simple enough to be evaluate at compile time could also be used as constexpr.

Literal Types: The types that we can use in a constexpr are known as “literal types” because they are simple enough to have literal values.

Pointers and constexpr: When we define a pointer in a constexpr declaration, the constexpr specifier applies to the pointer, not the type to which the pointer points.

2.5 Dealing with Types

2.5.1 Type Aliases

A type alias is a name that is a synonym for another type, whichi simplyfing complicated type definitions, making those types easier to use.

1
2
typedef double wages;
typedef wages base, *p;

The new standard introduced a second way to define a type alias, via an alias declaration:

1
using SI = Sales_item;

Pointers, const, nad Type Aliases:

1
2
3
typedef char *pstring;
const pstring cstr = 0;
const char *cstr = 0; // this is illegal

2.5.2 The auto Type Specifier

1
auto item = val1 + val2;

2.6 Deifining Our Own Data Structures

2.6.1 Defining the Sales_data Type

The close curly that ends the class body must be fgollowed by a semicolon, so we can define varibales after the class body:

1
2
3
4
5
struct Sales_data {
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
} accum, trans, *salesptr;

Class Data Members: A type of members in a struct.

2.6.2 Using the Sales_data Class

2.6.3 Wrinting Our Own Header Files

A header might be included more than twice, we need to write our headers in a way that is safe even if the header is included multiple time.

Note: Whenever a header is updated, the source files that use that header must be recompiled to get the new or changed declarations.

A Brief Introduction to the Preprocessor: The most common technique for making it safe to include a header multiple times relies on the preprocessor, which inherts from C is a progrom that runs before the compiler and changes the source text of our programs. #include is a processor.

WARNING: Preprocessor variable names do not respect C++ scoping rules.

Note: What is Header guards ?

1
2
3
4
5
/* header guards for sales data */
#ifndef SALES_DATA_H
#define SALES_DATA_H

#endif SALES_DATA_H

Acknowledgmets

  • With all respect to Mr. Zeng Xianliang, my colleague, who introduced this stunning book to me which he himself never really read it;
  • Full respect to the author of the book: Cpp Primer, Stanley B. Lippman, Josee Lajoie, Barbara E. Moo. Their great works perserves;
  • Didn’t came up who else is helping me with it at current stage.

Deep Learning Environment

Posted on 2019-07-04 Edited on 2020-01-10

Installation structions for configuration DL environment

  • Ubuntu 18.04 LTS, kernel version 4.18 (You may use uname -r to check your kernel version, currently, kernel version 5.0.0 is suffering the losing of Nvidia driver from time to time)
  • Nvidia Tesla P40 24G/Tesla P4 8G/Nvidia Quadro P2000 8G
  • CUDA 10.0
  • CUDNN 7.6.1 for CUDA 10.0
  • opencv 3.4.5
  • tensorflow-gpu 1.12.0
  • caffe 1.0, from BVLC
  • RefineDet, also with BVLC aligned
  • pytorch 1.1, which libtorch takes gcc>=6. for compilation on Ubuntu 18.04

Tips:

  • The installation may differ from many subtle differences, like: opencv version/cuda version, and of course, different modules included in your opencv. Even the author, me, install it very differently from time to time. The most import thing is: you are reseponsible for your environment, so if you had met problems during your installation, you should be able to solve them yourself and this blog is only providing a guide for the environment configuration instead of guaranting a successufully installation;
  • At current stage, an update of Ubuntu kernel, will cause the loss of Nvidia driver. A version of 418 works the best for 4.18~4.24 version of Ubuntu kernel;
  • DO NOT use sudo apt autoremove, unless you know what you are doing/removing;
  • Read the instructions carefully until you know what you are doing;
  • make -j may cause OOM problem, do not use it unless you know your hardware’s capability;

The full installation (until the successful installation of pytorch) will takes around 3 ~ 4 hours, depends on how familiar you are with Linux system…: Good luck, now let’s launch!

Install system dependency

Add Aliyun source

If you are not in China, you may skip this

1
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

you will have no vim before you proceed

1
sudo gedit /etc/apt/sources.list

add following configurations to the source list:

1
2
3
4
5
6
7
8
9
10
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

Refresh system

1
2
sudo apt update
sudo apt upgrade

Necessary tools for linux

1
2
sudo apt install openssh-server
sudo apt install net-tools

Installing: protobuf/opencv/hdf5/boost/openblas/atlas/lapack/gflags/glog/lmdb

1
2
3
4
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

Recommanded packages: freeglut, x11, xmu, xi gl1-mesa, glu1-mesa, glu1-mesa-dev: compiling cuda will throw exceptions: Missing recommanded libraries … if you don’t install them

1
sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev

Other necessary packages:

1
2
3
4
sudo apt-get install graphviz  # Install graphviz, or will cause pytest failed later
sudo apt install git
sudo apt-get install vim-gtk
sudo apt-get install ibus-pinyin

Install Nvidia drivers

Disable nouveau

1
sudo vim /etc/modprobe.d/blacklist.conf

Add, no need to source:

1
blacklist nouveau

Install drivers

using .run file

1
sudo bash ./NVIDIA-Linux-x86_64-418.67.run

Check, running following command should have popups

1
nvidia-smi

Reinstall drivers

1
2
3
sudo apt-get purge nvidia*
sudo add-apt-repository ppa:graphics-drivers
sudo bash NVIDIA-Linux-x86_64-418.64.run

Install CUDA

You don’t need to downgrade gcc installing CUDA 10.0

NOTES: Currently, Nvidia driver 4.10 and previous does not work with CUDA 10.0, so you’d better use CUDA 10.1 instead.

Install cuda

Do NOT install the drivers from cuda_***.run. However, later installation shows that the driver also works.

1
sudo bash ./cuda_10.0.130_418.64_linux.run

Configure system path

1
2
3
4
5
6
sudo vim ~/.bashrc

export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

source ~/.bashrc

Test

1
2
3
cd ~/NVIDIA_CUDA-10.0_Samples/1_Utilities/deviceQuery/samples/
sudo make
sudo ./deviceQuery

This indicates that the successfull installation of cuda:

1
2
3
- deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 10.1, CUDA Runtime
- Version = 10.1, NumDevs = 1
- Result = PASS

Note that the driver version and runtime version should better be the same here.

Install CUDNN

Latest CUDNN could be installed through dpkg, but you’d better install it from original file:

1
dpkg -i libcudnn7_7.6.2.24-1+cuda10.0_amd64.deb

Or, you can install it from original file.

1
2
3
4
tar -xvf cudnn-10.0-linux-x64-v7.6.1.34.tgz

sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/* /usr/local/cuda/lib64/

Install Anaconda

Damn idiots, you should know how to install anaconda and its environment

Install Anaconda from shell

1
bash Anaconda3-2019.03-Linux-x86_64.sh

Create virtual environment

1
2
conda create -n caffe-gpu python=3.6
conda activate caffe-gpu

Install opencv

Import notes:: Install from source! You are an grown-up now, you should be able to handle it. Earlier version, like 3.2.0 and before, using opencv may cause problem, please recompile opencv:

1
/usr/local/lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFReadRGBAStrip@LIBTIFF_4.0_apos

Then Download opencv 3.4.5 and opencv_contrib 3.4.5. Check your opencv version:

1
pkg-config --modversion opencv

In case of reinstall, you got to remove all opencv packages before you proceed, works like charm

1
sudo apt remove libopencv*

Install dependencies

I still didn’t figure out whether “pip install opencv-python” in the virtual environment will add opencv lib to LD_LIBRARY_PATH

1
2
3
4
5
6
sudo apt-get install build-essential
sudo apt-get install cmake libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install build-essential qt5-default ccache libv4l-dev libavresample-dev libgphoto2-dev libopenblas-base libopenblas-dev doxygen libvtk6-dev
sudo apt-get install python3-dev python3-numpy libgtk-3-dev libxvidcore-dev libx264-dev gfortran openexr
sudo apt-get install pkg-config

E: Unable to locate package libjasper-dev, This is because we had configured the Ali source for our ubuntu

1
2
3
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

If you are running a brand new os: i.e., an os just installed without any updates, you may fail install above packages, an upgrade of your system will solve the problem.

Install opencv 3.4.5 from source

1
2
unzip opencv-3.4.5.zip 
tar zxvf opencv_contrib-3.4.5.tar.gz

Make

Note: you should change the cmake paths according to your own system.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cd opencv-3.4.5
mkdir build && cd build
cmake \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_CUDA=ON \
-D BUILD_opencv_cudacodec=OFF \
-D ENABLE_PRECOMPILED_HEADERS=OFF\
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=/home/ubuntu/Installation/opencv_contrib-3.4.5/modules/ \
-D PYTHON_EXECUTABLE=/home/ubuntu/anaconda3/envs/torch-gpu/bin/python \
-D BUILD_TIFF=ON ..

make -j4
sudo make install

Opencv with CPU only:

1
2
3
4
5
6
7
8
9
10
11
cmake \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D ENABLE_PRECOMPILED_HEADERS=OFF\
-D ENABLE_FAST_MATH=1 \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=/home/ubuntu/Installation/opencv_contrib-3.4.5/modules/ \
-D PYTHON_EXECUTABLE=/home/ubuntu/anaconda3/envs/torch-cpu/bin/python \
-D BUILD_TIFF=ON ..

Possible problems

  1. ippicv downloads failed: If you are in mainland China, you may have trouble building the opencv project 3rd party lib: ippicv. You can download it manually from here. Then cd to ${OPENCV_ROOT}/third_party/ippicv/ and modify the ippicv.cmake file from:
1
"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"

to (depending on where you put lib ippicv):

1
"file://~/Downloads/"

Or:

1
https://github.com/opencv/opencv_3rdparty/blob/ippicv/internal_3.4_20190204/ippicv/
  1. opencv_cudacodec compile failed: cudacodec module is not used any more, so use -D BUILD_opencv_cudacodec=OFF to turn this module off in your installation;

  2. boostdesc or Xfeatures2d installation failed: Cannot find header 'boostdesc_bgm_bi.i' and so on. These files could not be downloaded due to the network of mainland China, download them independently (you may find them in a thread of OpenCV issue) and put them into modules/xfeatures2d/src.

Share the link library through the system

1
sudo ldconfig -v

Add opencv libraries to system path, and add /usr/local/lib to opencv.conf file

1
2
3
sudo vim /etc/ld.so.conf.d/opencv.conf 

sudo ldconfig

Configure bash

1
sudo gedit /etc/bash.bashrc

Add following command to the .bashrc file

1
2
3
4
5
6
7
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH


source /etc/bash.bashrc

sudo updatedb

Test

1
2
3
4
cd ${OPENCV_ROOT}/opencv-3.4.5/samples/cpp/example_cmake
cmake .
make
./opencv_example

You should see the popup of your camera, with Hello opencv on the left corner

Install OpenCV 4

Install system essentials:

1
2
3
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Install torch-gpu

You may refer to this link here.

Remove old environment

1
conda remove --name torch-gpu --all

create virtual env:

1
2
3
conda create -n torch-gpu python=3.6
conda activate torch-gpu
conda install pytorch torchvision cudatoolkit=10.1 -c pytorch

Install pylab:

1
2
pip install numpy scipy matplotlib lmdb
pip install pandas scikit-image filterpy

Install from source

A anaconda environment is highly recommended

Install dependencies

1
conda install numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing

Depending on your cuda version, it could be [magma-cuda92 | magma-cuda100 | magma-cuda101 ]

1
conda install -c pytorch magma-cuda101

Get PyTorch Source

1
git clone --recursive https://github.com/pytorch/pytorch

compile libtorch, Note that the core of libtorch is independent from python

1
cd pytorch

Please use GCC 6 or higher on Ubuntu 17.04 and higher. For more information, see: here.
You might need to export some required environment variables here. Normally setup.py sets good default env variables, but you’ll have to do that manually. The path: “$(dirname $(which conda))/../“ looks wierd but actually that`s what it is:

1
2
3
4
5
export NO_MKLDNN=1
export NO_SYSTEM_NCCL=1
export CUDNN_LIB_DIR="/usr/local/cuda-10.1/lib64"
export CUDNN_INCLUDE_DIR="/usr/local/cuda-10.1/include"
export CMAKE_PREFIX_PATH="$(dirname $(which conda))/../"

Install:

1
2
python setup.py install     # build and install
python setup.py clean --all # clean the build

The previous step take tens of minutes, after successed, you will see message:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/test/operator_gpu_test" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/test/math_gpu_test" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/test/mpi_gpu_test" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64:/usr/lib/x86_64-linux-gnu/openmpi/lib"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/test/conv_op_cache_cudnn_test" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/test/batch_matmul_op_gpu_test" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/test/elementwise_op_gpu_test" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/test/generate_proposals_op_gpu_test" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/test/generate_proposals_op_util_nms_gpu_test" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/test/operator_fallback_gpu_test" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/test/reshape_op_gpu_test" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/test/roi_align_op_gpu_test" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/test/utility_ops_gpu_test" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/bin/test_jit" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/bin/test_api" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/lib/libcaffe2_detectron_ops_gpu.so" to "$ORIGIN:/home/ubuntu/anaconda3/envs/torch-gpu/lib:/usr/local/cuda/lib64"
-- Set runtime path of "/home/ubuntu/Workspace/pytorch/torch/lib/libcaffe2_module_test_dynamic.so" to "$ORIGIN:/usr/local/cuda/lib64:/home/ubuntu/anaconda3/envs/torch-gpu/lib"

Install mkl

Note that the mkl is distinct from mkl-dnn: Download full mkl package from here and:

1
sudo ./install.sh

Note that the mkl default installation dir is /opt/intel. You should take sudo to make the proper installation.

Possibile problem

All the environment configuration is quite fun, it takes a lot of effort, but when you have finished all the work, you may feel indeed released. The hardest part is configure system dependencies.

1
fatal error: caffe/proto/caffe.pb.h: No such file or directory #include "caffe/protoc/caffe.pb.h"

you shall meet this problem using caffe: Source from here.

Highly recommanded installations

  • vscode, vscode is a fucking saver;
  • chrome, it’s 21 century now, use some real fucking browser bro.
12

Zepyhrus

12 posts
© 2020 Zepyhrus
Powered by Hexo v3.9.0
|
Theme – NexT.Mist v7.3.0