Skip to content

Commit

Permalink
Setup HW 1
Browse files Browse the repository at this point in the history
  • Loading branch information
BeibinLi committed Mar 26, 2021
1 parent d4460d8 commit 078c812
Show file tree
Hide file tree
Showing 22 changed files with 11,093 additions and 0 deletions.
74 changes: 74 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
OPENCV=0
OPENMP=0
DEBUG=0

OBJ=load_image.o process_image.o args.o test.o

EXOBJ=main.o

VPATH=./src/:./:./src/hw1
SLIB=visionlib.so
ALIB=visionlib.a
EXEC=main
OBJDIR=./obj/

CC=gcc
CPP=g++ -std=c++11
AR=ar
ARFLAGS=rcs
OPTS=-Ofast
LDFLAGS= -lm -pthread
COMMON= -Iinclude/ -Isrc/
CFLAGS=-Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC

ifeq ($(OPENMP), 1)
CFLAGS+= -fopenmp
endif

ifeq ($(DEBUG), 1)
OPTS=-O0 -g
COMMON= -Iinclude/ -Isrc/
else
CFLAGS+= -flto
endif

CFLAGS+=$(OPTS)

ifeq ($(OPENCV), 1)
COMMON+= -DOPENCV
CFLAGS+= -DOPENCV
LDFLAGS+= `pkg-config --libs opencv` -lstdc++ # This may need to be opencv4 or a specific path
COMMON+= `pkg-config --cflags opencv`
endif

EXOBJS = $(addprefix $(OBJDIR), $(EXOBJ))
OBJS = $(addprefix $(OBJDIR), $(OBJ))
DEPS = $(wildcard src/*.h) Makefile

all: obj $(SLIB) $(ALIB) $(EXEC)
#all: obj $(EXEC)


$(EXEC): $(EXOBJS) $(OBJS)
$(CC) $(COMMON) $(CFLAGS) $^ -o $@ $(LDFLAGS)

$(ALIB): $(OBJS)
$(AR) $(ARFLAGS) $@ $^

$(SLIB): $(OBJS)
$(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS)

$(OBJDIR)%.o: %.c $(DEPS)
$(CC) $(COMMON) $(CFLAGS) -c $< -o $@

$(OBJDIR)%.o: %.cpp $(DEPS)
$(CPP) $(COMMON) $(CFLAGS) -c $< -o $@

obj:
mkdir -p obj

.PHONY: clean

clean:
rm -rf $(OBJS) $(SLIB) $(ALIB) $(EXEC) $(EXOBJS) $(OBJDIR)/*

34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Welcome to CSE 576, 2021 Spring

In this repository you will find instructions on how to build your own image processing/computer vision library from (mostly) scratch. The work is divided out into different homework assignments, found in the `src/` directory.

To get started, make sure you have `git`, a C compiler, and `make` installed. Then run:

```
git clone https://github.com/UW-CSE-576-2021SP/Homework
cd homeworks
make
```

and check to see that everything compiles correctly. We recommend using Linux or MacOS for the homeworks since installing `make` is easier. Linux uses GNU C++ compiler, while MacOS uses XCode C++ compiler.

## Get started on HW1

Open up the README for homework 1 in src/hw1/README.md, or view it [here](src/hw1/README.md). Good luck and have fun!

## Instructions for Mac Users
In MacOS, make sure you have the latest version of Xcode and perform `xcode-select --install`.
If `make` still results in an error, try [this](https://github.com/frida/frida/issues/338#issuecomment-426777849) solution.

## Instructions for Windows Users
We do **NOT** recommend Windows OS for this assignment because C++ compilation is more complex under the Windows environment. However, if you only have Windows computers available, you can still manage your Python packages, C++ compiler, and Makefile with Anaconda.

Installation Steps:
1. Download [Anaconda](https://www.anaconda.com/distribution/) with Python 3.6+
2. Install Anaconda with "admin" rights: PLEASE select "All Users (requires admin privileges)", "Add Anaconda to the system PATH environment variable", and "Register Anaconda as the system Python 3.x".
3. Open "Command Prompt" (aka "cmd") with admin rights, then:
- run the command `conda install -c msys2 m2-base m2w64-gcc` to install C++ compiler
- run the command `conda install -c conda-forge make cmake` to install Make.
6. Now, you can follow the same instructions as Mac/Linux users do.


10 changes: 10 additions & 0 deletions mnist.labels
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
num0
num1
num2
num3
num4
num5
num6
num7
num8
num9
70 changes: 70 additions & 0 deletions src/args.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "args.h"

void del_arg(int argc, char **argv, int index)
{
int i;
for(i = index; i < argc-1; ++i) argv[i] = argv[i+1];
argv[i] = 0;
}

int find_arg(int argc, char* argv[], char *arg)
{
int i;
for(i = 0; i < argc; ++i) {
if(!argv[i]) continue;
if(0==strcmp(argv[i], arg)) {
del_arg(argc, argv, i);
return 1;
}
}
return 0;
}

int find_int_arg(int argc, char **argv, char *arg, int def)
{
int i;
for(i = 0; i < argc-1; ++i){
if(!argv[i]) continue;
if(0==strcmp(argv[i], arg)){
def = atoi(argv[i+1]);
del_arg(argc, argv, i);
del_arg(argc, argv, i);
break;
}
}
return def;
}

float find_float_arg(int argc, char **argv, char *arg, float def)
{
int i;
for(i = 0; i < argc-1; ++i){
if(!argv[i]) continue;
if(0==strcmp(argv[i], arg)){
def = atof(argv[i+1]);
del_arg(argc, argv, i);
del_arg(argc, argv, i);
break;
}
}
return def;
}

char *find_char_arg(int argc, char **argv, char *arg, char *def)
{
int i;
for(i = 0; i < argc-1; ++i){
if(!argv[i]) continue;
if(0==strcmp(argv[i], arg)){
def = argv[i+1];
del_arg(argc, argv, i);
del_arg(argc, argv, i);
break;
}
}
return def;
}

9 changes: 9 additions & 0 deletions src/args.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef ARGS_H
#define ARGS_H

int find_arg(int argc, char* argv[], char *arg);
int find_int_arg(int argc, char **argv, char *arg, int def);
float find_float_arg(int argc, char **argv, char *arg, float def);
char *find_char_arg(int argc, char **argv, char *arg, char *def);

#endif
126 changes: 126 additions & 0 deletions src/data.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "image.h"
#include "list.h"

data random_batch(data d, int n)
{
matrix X = {0};
matrix y = {0};
X.shallow = y.shallow = 1;
X.rows = y.rows = n;
X.cols = d.X.cols;
y.cols = d.y.cols;
X.data = calloc(n, sizeof(double*));
y.data = calloc(n, sizeof(double*));
int i;
for(i = 0; i < n; ++i){
int ind = rand()%d.X.rows;
X.data[i] = d.X.data[ind];
y.data[i] = d.y.data[ind];
}
data c;
c.X = X;
c.y = y;
return c;
}

list *get_lines(char *filename)
{
char *path;
FILE *file = fopen(filename, "r");
if(!file) {
fprintf(stderr, "Couldn't open file %s\n", filename);
exit(0);
}
list *lines = make_list();
while((path=fgetl(file))){
list_insert(lines, path);
}
fclose(file);
return lines;
}

data load_classification_data(char *images, char *label_file, int bias)
{
list *image_list = get_lines(images);
list *label_list = get_lines(label_file);
int k = label_list->size;
char **labels = (char **)list_to_array(label_list);

int n = image_list->size;
node *nd = image_list->front;
int cols = 0;
int i;
int count = 0;
matrix X;
matrix y = make_matrix(n, k);
while(nd){
char *path = (char *)nd->val;
image im = load_image(path);
if (!cols) {
cols = im.w*im.h*im.c;
X = make_matrix(n, cols + (bias != 0));
}
for (i = 0; i < cols; ++i){
X.data[count][i] = im.data[i];
}
if(bias) X.data[count][cols] = 1;

for (i = 0; i < k; ++i){
if(strstr(path, labels[i])){
y.data[count][i] = 1;
}
}
++count;
nd = nd->next;
}
free_list(image_list);
data d;
d.X = X;
d.y = y;
return d;
}


char *fgetl(FILE *fp)
{
if(feof(fp)) return 0;
size_t size = 512;
char *line = malloc(size*sizeof(char));
if(!fgets(line, size, fp)){
free(line);
return 0;
}

size_t curr = strlen(line);

while((line[curr-1] != '\n') && !feof(fp)){
if(curr == size-1){
size *= 2;
line = realloc(line, size*sizeof(char));
if(!line) {
fprintf(stderr, "malloc failed %ld\n", size);
exit(0);
}
}
size_t readsize = size-curr;
if(readsize > INT_MAX) readsize = INT_MAX-1;
fgets(&line[curr], readsize, fp);
curr = strlen(line);
}
if(line[curr-1] == '\n') line[curr-1] = '\0';

return line;
}

void free_data(data d)
{
free_matrix(d.X);
free_matrix(d.y);
}



Loading

0 comments on commit 078c812

Please sign in to comment.