Joseph Redmon
2014-01-24 ace5aeb0f59fdceb99e607af9780added20da37c
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 
void gemm(int TA, int TB, int M, int N, int K, double ALPHA, 
                    double *A, int lda, 
                    double *B, int ldb,
                    double BETA,
                    double *C, int ldc)
{
    // Assume TA = TB = 0, beta = 1 LULZ
    int i,j,k;
    for(i = 0; i < M; ++i){
        for(k = 0; k < K; ++k){
            for(j = 0; j < N; ++j){
                C[i*ldc+j] += ALPHA*A[i*lda+k]*B[k*ldb+j];
            }
        }
    }
}
 
void im2row(double *image, int h, int w, int c, int size, int stride, double *matrix)
{
    int i;
    int mc = c;
    int mw = (size*size);
    int mh = ((h-size)/stride+1)*((w-size)/stride+1);
    int msize = mc*mw*mh;
    for(i = 0; i < msize; ++i){
        int channel = i/(mh*mw);
        int block =   (i%(mh*mw))/mw;
        int position = i%mw;
        int block_h = block/((w-size)/stride+1);
        int block_w = block%((w-size)/stride+1);
        int ph, pw, pc;
        ph = position/size+block_h;
        pw = position%size+block_w;
        pc = channel;
        matrix[i] = image[pc*h*w+ph*w+pw];
    }
}
void im2col(double *image, int h, int w, int c, int size, int stride, double *matrix)
{
    int b,p;
    int blocks = ((h-size)/stride+1)*((w-size)/stride+1);
    int pixels = (size*size*c);
    for(b = 0; b < blocks; ++b){
        int block_h = b/((w-size)/stride+1);
        int block_w = b%((w-size)/stride+1);
        for(p = 0; p < pixels; ++p){
            int ph, pw, pc;
            int position = p%(size*size);
            pc = p/(size*size);
            ph = position/size+block_h;
            pw = position%size+block_w;
            matrix[b+p*blocks] = image[pc*h*w+ph*w+pw];
        }
    }
}
 
//From Berkeley Vision's Caffe!
void im2col_cpu(double* data_im, const int channels,
        const int height, const int width, const int ksize, const int stride,
        double* data_col) 
 {
    int c,h,w;
    int height_col = (height - ksize) / stride + 1;
    int width_col = (width - ksize) / stride + 1;
    int channels_col = channels * ksize * ksize;
    for ( c = 0; c < channels_col; ++c) {
        int w_offset = c % ksize;
        int h_offset = (c / ksize) % ksize;
        int c_im = c / ksize / ksize;
        for ( h = 0; h < height_col; ++h) {
            for ( w = 0; w < width_col; ++w) {
                data_col[(c * height_col + h) * width_col + w] =
                    data_im[(c_im * height + h * stride + h_offset) * width
                    + w * stride + w_offset];
            }
        }
    }
}