Skip to content

Commit 4f1bbf4

Browse files
committed
Some changes on the files.
1 parent 3c0a5bd commit 4f1bbf4

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
<<<<<<< HEAD
21

3-
4-
=======
5-
>>>>>>> 3fd407e (Run the project on windows Powershell)
62
CC=g++
73
CFLAGS= -std=gnu++11 -O2 -Iinclude -I/usr/include/python3.10
84
LDFLAGS= -lpython3.10

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
## Code structure
88
* filter.hpp defines a 3-D convolutinal kernel class with a bias term. It contains some helper functions to allocate memory to tensors and to normalize them.
99
* Convolution.hpp defines a convolutional layer. One can set the stride and zero-padding of the filter in this. Also, dimensions of the output layer are calculated automatically.
10-
* conv2d method takes as argument a 3-D data volume and a list of filters (one filter generates one activation map). For example, applying a 3 x 3 x 3 filter on a 512 x 512 x 3 image (with 1 zero padding and 1 stride) will generate an 2-D output layer of 512 x 512. See example (taken from course [cs231n](http://cs231n.stanford.edu/syllabus.html)).
11-
![One filter](./images/one_map.png)
10+
* conv2d method takes as argument a 3-D data volume and a list of filters (one filter generates one activation map). For example, applying a 3 x 3 x 3 filter on a 300 x 300 x 3 image (with 1 zero padding and 1 stride) will generate an 2-D output layer of 300 x 300.
1211
* List of filters would make the output layer. Shape of output layer as well as the data block is returned by the function conv2d.
13-
![Many filter](./images/multi_map.png)
12+
1413
* main.cpp runs some example filters on a batch of 3 images. It generates 3 filters, one as an edge detector for each color channel (see push\_filter). Then defines
1514
a convolution layer with given params and applies the layer to each of the images. It then writes the output to a different file.
1615
*Inside helpers directory, we have make\_mats.py and load\_img.py that are used to generate images\-matrices and vice versa.
@@ -55,7 +54,7 @@ g++ -std=gnu++11 -O2 conv2d_layer.hpp -o conv2d_layer.o
5554
g++ -std=gnu++11 -O2 main.cpp -o main
5655
```
5756

58-
* List of images to use is in file make\_mats.py. In the demo it uses a batch of 3 512 \* 512 \* 3 (color) images.
57+
* List of images to use is in file make\_mats.py. In the demo it uses a batch of 3 300 \* 300 \* 3 (color) images.
5958

6059
```bash
6160
python3 make_mats.py img_mats/out.dat
@@ -78,9 +77,6 @@ python3 load_img.py img_mats/filter_out.dat out_mats
7877
You can checkout the image results in the out_mats directory. The output images are generated by applying a filter to the input images.
7978
The filter is defined in filter.txt, which is the given kernel with 1/273 normalization factor. So the Matrix is stored on the file after normalization.
8079

81-
<<<<<<< HEAD
8280
The output images are stored in out_mats inside output directory.
8381

84-
=======
8582
The output images are stored in out_mats inside output directory.
86-
>>>>>>> 3fd407e (Run the project on windows Powershell)

helpers/load_img.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
if __name__ == "__main__":
99

1010
f = open(sys.argv[1] ,"r")
11-
<<<<<<< HEAD
1211
shape = list(map(int, f.readline().split()))
1312
for idx in range(shape[0]):
1413
im = np.empty(shape=(shape[1], shape[2], shape[3]))
1514
for i in range(shape[1]):
16-
=======
1715
# Read the number of images
1816
shape = list(map(int, f.readline().split()))
1917

@@ -25,7 +23,6 @@
2523
# Read the image
2624
for i in range(shape[1]):
2725

28-
>>>>>>> 3fd407e (Run the project on windows Powershell)
2926
row = f.readline().split()
3027
assert(len(row) == shape[2]) # 300
3128
for j in range(shape[2]):

src/Filter.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
#include "Filter.hpp"
22

33
// allocate memory for a tensor
4+
5+
// Explanation of the function get_tensor:
6+
// This function takes in three integers x, y, and z, and returns a 3D tensor of size x*y*z.
7+
// The tensor is a 3D array of doubles, and is allocated on the heap.
8+
// The function first allocates memory for an array of pointers to pointers to pointers of doubles.
9+
// Then, it allocates memory for each row of the tensor, which is an array of pointers to pointers of doubles.
10+
// Finally, it allocates memory for each element of the tensor, which is an array of doubles.
11+
// The function returns the 3D tensor.
12+
13+
// What does tensor mean?
14+
// A tensor is a generalization of scalars, vectors, and matrices to higher dimensions.
15+
// In this case, the tensor is a 3D array of doubles, which is used to store the weights of the filter.
16+
17+
418
double ***get_tensor(int x, int y, int z) {
519
double ***ret = new double**[x];
620
for (int i = 0; i < x; i++) {
@@ -13,14 +27,12 @@ double ***get_tensor(int x, int y, int z) {
1327
}
1428

1529
// Constructor
16-
Filter::Filter(int _window, int _depth)
17-
: window(_window), depth(_depth) {
30+
Filter::Filter(int _window, int _depth) : window(_window), depth(_depth) {
1831
w = get_tensor(window, window, depth);
1932
}
2033

2134
// Constructor with initial weights
22-
Filter::Filter(double ***_w, int _window, int _depth, int _b)
23-
: window(_window), depth(_depth), b(_b) {
35+
Filter::Filter(double ***_w, int _window, int _depth, int _b) : window(_window), depth(_depth), b(_b) {
2436
w = get_tensor(window, window, depth);
2537
for (int i = 0; i < window; ++i) {
2638
for (int j = 0; j < window; ++j) {

src/main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,21 @@ int main(int argc, char *argv[]) {
8383
}
8484

8585
int width, num_images, height, depth;
86+
87+
// As requested in the assignment, we will use a stride of 1
88+
// As an option feature, we will also use padding of 1
8689
int stride = 1, padding = 1;
90+
91+
8792
ifile >> num_images >> width >> height >> depth;
93+
8894
ofile << num_images << " ";
8995
cerr << num_images << " ";
9096

9197
Convolution clayer(width, height, depth, w_size, stride, padding, filters.size());
98+
9299
double ***input;
100+
93101
input = get_tensor(width, height, depth);
94102

95103
#pragma omp parallel for // Parallelize the loop

0 commit comments

Comments
 (0)