Pytorch (ii)--build and customize the network

Source: Internet
Author: User
Tags function definition pytorch

Directory Connections
(1) Data processing
(2) Build and customize the network
(3) Test your pictures with a well-trained model
(4) Processing of video data
(5) Pytorch source code modification to increase the CONVLSTM layer
(6) Understanding of gradient reverse transfer (backpropogate)
(total) Pytorch encounters fascinating bug Pytorch learning and use (ii)

Recently, just looking at a paper related to the Siamese network, there is no example,caffe in the Pytorch, just use Pytorch implementation. (PS: Picture opens more clearly)
The main steps are: Data preprocessing model building model training data pretreatment

The network structure of the Siamese is as follows:

By entering two pictures X1 and X2, through the weighted shared CNN, each Gets an output eigenvector GW (X1) g_w (x_1) and GW (X2) g_w (x_2), and then uses the distance of the eigenvector to measure the similarity of the two pictures.

We still use the handwritten data set minist as a test (mainly with mnist for reference), the network input picture is paired, so in the construction of the training batch needs to define 2 pictures, its label is two pictures is the same number. According to the previous article, redefine Transforms.compose () and Torchvision.datasets.SIAMESE ().

Since there is no change in the reading and processing of the data, compose () is not modified. Torchvision.datasets.SIAMESE () The adjacent picture is bundled together as a pair of input images, if the same number, then the label is 1, the difference is 0.
As shown below:

As shown in the figure, there are 2 pictures in each batch, but in training we want each adjacent batch to be a pair of pictures, so do the following: Images.view (batch*2, 1, 28, 28) Get the following result:
the construction of the network

Siamese's network definition and lenet model are basically consistent, the only difference is to generate 10 of the number of categories of probability of the top layer to generate a two-dimensional vector of the feature layer, the last loss function for the comparative loss function (Contrastive_loss) ( very sad, The loss function is temporarily not implemented in Pytorch ). So you need to implement the Contrastive_loss layer yourself. contrastive_loss function Definition

First, understand the Caffe in the Siamese network such as and implementation. Contrastive_loss_layer is defined as follows:

Its forward () is implemented as follows:

In the above code, BOTTOM[0],BOTTOM[1] The characteristics of the 2 images stored, bottom[2],bottom[3] The label of the 2 images stored, and then according to whether they are the same as the following calculation:
loss=⎧⎩⎨| | ai−bi| | 22,max{0,m−| | ai−bi| | 22}2,if A = = bif A! = b loss = \begin{cases} | | a_i-b_i| | _2^2, & \text{if a = = b} \\[2ex] Max \{0, M-| | a_i-b_i| | _2^2\}^2, & \text{if A! = b} \end{cases}

Loss=12n⋅loss Loss = \frac{1}{2n} \cdot Loss
In thesis learning a similarity Metric discriminatively, with the application to the face verification, its defined loss function is:

Requirements Meet:

When EW (x1,x′2) −>0 e_w (x_1,x_2^{'})->0, EW (X1,X2) −>m

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.