Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
ND Crop layer #3570
Conversation
|
@longjon So, my attempt at a ND crop layer, if this is approximately what you are looking for I can try to fix the GPU part. Edit: For the GPU part just implementing the 4D case would seem like a reasonable compromise. |
|
Actually I'm confused about the parameter interface here... what I had in mind is something mirroring the conv layer interface, with a scalar (i.e., The way it would work would be the same way conv layer works: if only one crop value is specified, all the dimensions after the specified The reason for doing it this way is because the crop values need to be determined from the net specification, and the net specification doesn't know how many dimensions the data will have! If the layers we are "cropping through" include conv/pool layers with non-singleton kernel sizes, then we know exactly how many dimensions to crop; but if not, all we can do is propagate the It would be nice to have an ND GPU kernel, but it's fine to punt that to get the basic functionality in. Thanks! |
|
@longjon The parameters should do what you want now. I substituted the copy kernel with repeated calls to caffe_copy, which if I understood correctly will call cudaMemcpy, and thus perform the copy operation on the GPU. My intuition is that calling this thousands of times is probably less efficient than writing a custom kernel. |
|
The parameters look right now.
|
|
@longjon The |
longjon
referenced
this pull request
Jan 30, 2016
Merged
Python/net spec coordinate map and crop computation #3613
BlGene
changed the title from
ND Crop layer [WIP] to ND Crop layer
Feb 1, 2016
bjosv79
commented
Feb 19, 2016
|
It seems to a few problems with the backward pass of
|
|
@bjosv79 Thanks. All those things seemed correct, I changed them ( but haven't tested yet ). |
shelhamer
added enhancement JL ES
labels
Feb 25, 2016
shelhamer
commented on an outdated diff
Feb 27, 2016
| @@ -306,7 +306,7 @@ message ParamSpec { | ||
| // NOTE | ||
| // Update the next available ID when you add a new LayerParameter field. | ||
| // | ||
| -// LayerParameter next available layer-specific ID: 143 (last added: scale_param) | ||
| +// LayerParameter next available layer-specific ID: 144 (last added: crop_param) |
|
|
shelhamer
commented on an outdated diff
Feb 27, 2016
| @@ -0,0 +1,67 @@ | ||
| +#ifndef CAFFE_CROP_LAYER_HPP_ | ||
| +#define CAFFE_CROP_LAYER_HPP_ | ||
| + | ||
| +#include <utility> | ||
| +#include <vector> | ||
| + | ||
| +#include "caffe/blob.hpp" | ||
| +#include "caffe/layer.hpp" | ||
| +#include "caffe/proto/caffe.pb.h" | ||
| + | ||
| +namespace caffe { | ||
| + | ||
| +/** | ||
| + * @brief Takes a Blob and crop it along either the width or height dimension, |
shelhamer
Owner
|
shelhamer
commented on an outdated diff
Feb 27, 2016
| + | ||
| +template <typename Dtype> | ||
| +void CropLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, | ||
| + const vector<Blob<Dtype>*>& top) { | ||
| + CHECK_EQ(bottom.size(), 2) << "Wrong number of bottom blobs."; | ||
| + // parameter setup moved to Reshape because it depends on size. | ||
| +} | ||
| + | ||
| +template <typename Dtype> | ||
| +void CropLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, | ||
| + const vector<Blob<Dtype>*>& top) { | ||
| + const CropParameter& param = this->layer_param_.crop_param(); | ||
| + // bottom[0] supplies the data | ||
| + // bottom[1] supplies the size | ||
| + int input_dim = bottom[0]->num_axes(); | ||
| + CHECK_LT(param.axis(), input_dim) << "crop axis bigger than input dim"; |
shelhamer
Owner
|
shelhamer
commented on an outdated diff
Feb 27, 2016
| + } | ||
| + top[0]->Reshape(new_shape); | ||
| +} | ||
| + | ||
| +// recursive copy function | ||
| +template <typename Dtype> | ||
| +void CropLayer<Dtype>::crop_copy(const vector<Blob<Dtype>*>& bottom, | ||
| + const vector<Blob<Dtype>*>& top, | ||
| + const vector<int>& offsets, | ||
| + vector<int> indices, | ||
| + int cur_dim, | ||
| + const Dtype* src_data, | ||
| + Dtype* dest_data, | ||
| + bool is_forward) { | ||
| + if (cur_dim + 1 < top[0]->num_axes()) { | ||
| + // We are not yet at the final dimension, call copy recursivley |
|
|
shelhamer
commented on an outdated diff
Feb 27, 2016
| + bottom[0]->offset(ind_off); | ||
| + // NOLINT_NEXT_LINE(whitespace/operators) | ||
| + copy_kernel<<<CAFFE_GET_BLOCKS(lines), CAFFE_CUDA_NUM_THREADS>>>( | ||
| + lines, height, width, | ||
| + dest_outer_stride, dest_inner_stride, | ||
| + src_outer_stride, src_inner_stride, | ||
| + top_diff, bottom_diff); | ||
| + } | ||
| + } | ||
| +} | ||
| + | ||
| +template <typename Dtype> | ||
| +void CropLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom, | ||
| + const vector<Blob<Dtype>*>& top) { | ||
| + std::vector<int> indices(top[0]->num_axes(), 0); | ||
| + // This works because crop_copy uses caffe_copy which calls cudaMemcpy. |
|
|
shelhamer
added the
focus
label
Feb 27, 2016
This was referenced Feb 27, 2016
|
@BlGene I pushed a rebase on the latest master to my fork https://github.com/shelhamer/caffe/tree/crop-nd with a few other additions -- feel free to integrate my changes if the commits save you time. |
shelhamer
commented on an outdated diff
Feb 28, 2016
| @@ -597,6 +598,16 @@ message ConvolutionParameter { | ||
| optional bool force_nd_im2col = 17 [default = false]; | ||
| } | ||
| +message CropParameter { | ||
| + // If only one crop_offset is specified, all the dimensions after the specified | ||
| + // axis would be cropped (by the same amount); otherwise the number of crop | ||
| + // values specified must be equal to the number of dimensions following axis, and | ||
| + // the trailing dimensions would be cropped accordingly. | ||
| + // Protip: standard dimensions are ( N,C,H,W ) | ||
| + optional uint32 axis = 1 [default = 2]; |
shelhamer
Owner
|
shelhamer
added a commit
to longjon/caffe
that referenced
this pull request
Feb 28, 2016
|
|
shelhamer |
81a6a6a
|
|
@shelhamer this last commit should address all the points you mentioned. |
This was referenced Feb 29, 2016
shelhamer
commented on an outdated diff
Mar 1, 2016
| +void CropLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, | ||
| + const vector<Blob<Dtype>*>& top) { | ||
| + // All logic that depends only on the number of dimensions is here, | ||
| + // the rest is in Reshape because it depends on Blob size. | ||
| + // bottom[0] supplies the data | ||
| + // bottom[1] supplies the size | ||
| + const CropParameter& param = this->layer_param_.crop_param(); | ||
| + CHECK_EQ(bottom.size(), 2) << "Wrong number of bottom blobs."; | ||
| + int input_dim = bottom[0]->num_axes(); | ||
| + const int start_axis = bottom[0]->CanonicalAxisIndex(param.axis()); | ||
| + CHECK_LT(start_axis, input_dim) << "crop axis bigger than input dim"; | ||
| + if (param.offset_size() > 1) { | ||
| + // the number of crop values specified must be equal to the number | ||
| + // of dimensions following axis | ||
| + CHECK_EQ(start_axis + param.offset_size(), input_dim) | ||
| + << "number of crop values specified must be equal to the number of " |
|
|
|
Other then the last trivial comment all that's needed now are tests. @BlGene do you have these planned, or should I take a stab at it? |
|
@shelhamer |
shelhamer
added a commit
to longjon/caffe
that referenced
this pull request
Mar 2, 2016
|
|
shelhamer |
15dc5d8
|
shelhamer
added a commit
to longjon/caffe
that referenced
this pull request
Mar 2, 2016
|
|
shelhamer |
d155544
|
shelhamer
added a commit
that referenced
this pull request
Mar 3, 2016
|
|
shelhamer |
464939f
|
shelhamer
added a commit
that referenced
this pull request
Mar 3, 2016
|
|
shelhamer |
d2b85df
|
shelhamer
added a commit
to longjon/caffe
that referenced
this pull request
Mar 3, 2016
|
|
shelhamer |
1f8f213
|
shelhamer
added a commit
to longjon/caffe
that referenced
this pull request
Mar 3, 2016
|
|
shelhamer |
d5a051a
|
shelhamer
added a commit
to longjon/caffe
that referenced
this pull request
Mar 4, 2016
|
|
shelhamer |
f4c762e
|
shelhamer
added a commit
to longjon/caffe
that referenced
this pull request
Mar 4, 2016
|
|
shelhamer |
fee422d
|
shelhamer
added a commit
to longjon/caffe
that referenced
this pull request
Mar 4, 2016
|
|
shelhamer |
988b98c
|
|
@BlGene I revised the tests in my latest commits to shelhamer/caffe:nd-crop to
so that all the TODOs are covered. Can you incorporate my changes into this branch for merge? Please squash commits to simplify history (including mine, I don't mind) into something like
I've added tests to #3613 so we can merge both once this is ready. |
shelhamer
removed ES JL
labels
Mar 4, 2016
|
@shelhamer Squashed! |
|
@BlGene thanks for the quick reply! Sorry to be so obsessed with history but can you reword the messages to make the subject line (first line of the message) more complete? That is, b6ee629 could be "Crop: fixes, tests, and negative axis indexing" and 631ff09 could be "Crop: more tests and test tuning" with the rest of the details in a list. With that this will be a model PR and finally merged! Thanks for all your work on this and the attention to detail. I'm excited to have all the FCN tools in master soon. |
shelhamer
closed this
Mar 4, 2016
shelhamer
reopened this
Mar 4, 2016
shelhamer
added a commit
to longjon/caffe
that referenced
this pull request
Mar 4, 2016
|
|
shelhamer |
6fa55a1
|
shelhamer
added a commit
to shelhamer/caffe
that referenced
this pull request
Mar 4, 2016
|
|
shelhamer |
05a3216
|
shelhamer
added a commit
to shelhamer/caffe
that referenced
this pull request
Mar 4, 2016
|
|
shelhamer |
7135e3c
|
shelhamer
added a commit
to longjon/caffe
that referenced
this pull request
Mar 5, 2016
|
|
shelhamer |
25b9ef9
|
|
@shelhamer OK, I fixed the commit messages. I'm happy to have FCN functionality merged too. Thanks for the attention to detail. |
longjon
and others
added some commits
Dec 27, 2014
shelhamer
added a commit
that referenced
this pull request
Mar 5, 2016
|
|
shelhamer |
ca1ce4a
|
shelhamer
merged commit ca1ce4a
into
BVLC:master
Mar 5, 2016
1 check passed
BlGene
deleted the
BlGene:crop-nd branch
Mar 6, 2016
zouxiaochuan
added a commit
to zouxiaochuan/caffe
that referenced
this pull request
Mar 17, 2016
|
|
shelhamer + zouxiaochuan |
bdb72aa
|
|
Just a quick question from my side, can the ND crop be used to implement a rectangular crop as in #1980? |
|
This layer crops blob B to have the same shape as Blob A. So its a bit hacky, but you could create a dummy blob (A) that has the desired shape and crop the (B) blob to this size. BR, Max |
SvenTwo
added a commit
to SvenTwo/caffe
that referenced
this pull request
Apr 6, 2016
|
|
shelhamer + SvenTwo |
84b3f07
|
emaggiori
pushed a commit
to emaggiori/caffe
that referenced
this pull request
Apr 8, 2016
|
|
shelhamer + |
6fa5155
|
This was referenced Apr 10, 2016
sato-cloudian
added a commit
to sato-cloudian/caffe
that referenced
this pull request
Aug 10, 2016
|
|
shelhamer + sato-cloudian |
7dbb645
|
fxbit
added a commit
to Yodigram/caffe
that referenced
this pull request
Sep 1, 2016
|
|
shelhamer + fxbit |
d49d210
|
fxbit
added a commit
to Yodigram/caffe
that referenced
this pull request
Sep 1, 2016
|
|
shelhamer + fxbit |
4cdf891
|
zouxiaochuan
added a commit
to zouxiaochuan/caffe
that referenced
this pull request
Oct 24, 2016
|
|
shelhamer + zouxiaochuan |
9ee8fb3
|
zouxiaochuan
added a commit
to zouxiaochuan/caffe
that referenced
this pull request
Oct 24, 2016
|
|
shelhamer + zouxiaochuan |
d9ab5a7
|
zouxiaochuan
added a commit
to zouxiaochuan/caffe
that referenced
this pull request
Feb 15, 2017
|
|
shelhamer + zouxiaochuan |
f0effaf
|
zouxiaochuan
added a commit
to zouxiaochuan/caffe
that referenced
this pull request
Feb 15, 2017
|
|
shelhamer + zouxiaochuan |
fa42eb2
|
zouxiaochuan
added a commit
to zouxiaochuan/caffe
that referenced
this pull request
Feb 15, 2017
|
|
shelhamer + zouxiaochuan |
518f9aa
|
zouxiaochuan
added a commit
to zouxiaochuan/caffe
that referenced
this pull request
Feb 15, 2017
|
|
shelhamer + zouxiaochuan |
0c6d453
|
BlGene commentedJan 19, 2016
Something along the lines of what a ND crop layer would look like.
Based on: PR #1976 -> PR #3565 -> this
Todo
Please see PR #3613 for how to set the crop parameters.