| | |
| | | endif |
| | | |
| | | OBJS = $(addprefix $(OBJDIR), $(OBJ)) |
| | | DEPS = $(wildcard src/*.h) Makefile |
| | | |
| | | all: $(EXEC) |
| | | |
| | | $(EXEC): $(OBJS) |
| | | $(CC) $(COMMON) $(CFLAGS) $(LDFLAGS) $^ -o $@ |
| | | |
| | | $(OBJDIR)%.o: %.c |
| | | $(OBJDIR)%.o: %.c $(DEPS) |
| | | $(CC) $(COMMON) $(CFLAGS) -c $< -o $@ |
| | | |
| | | $(OBJDIR)%.o: %.cu |
| | | $(OBJDIR)%.o: %.cu $(DEPS) |
| | | $(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $@ |
| | | |
| | | .PHONY: clean |
| | |
| | | |
| | | float scale = 1./sqrt(inputs); |
| | | for(i = 0; i < inputs*outputs; ++i){ |
| | | //layer->weights[i] = scale*rand_normal(); |
| | | layer->weights[i] = 2*scale*rand_uniform() - scale; |
| | | } |
| | | |
| | | for(i = 0; i < outputs; ++i){ |
| | |
| | | layer->biases = calloc(n, sizeof(float)); |
| | | layer->bias_updates = calloc(n, sizeof(float)); |
| | | float scale = 1./sqrt(size*size*c); |
| | | for(i = 0; i < c*n*size*size; ++i) layer->filters[i] = scale*rand_normal(); |
| | | for(i = 0; i < c*n*size*size; ++i) layer->filters[i] = 2*scale*rand_uniform() - scale; |
| | | for(i = 0; i < n; ++i){ |
| | | layer->biases[i] = scale; |
| | | } |
| | |
| | | return float_to_image(w,h,c,layer.output); |
| | | } |
| | | |
| | | crop_layer *make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip) |
| | | crop_layer *make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip, float angle) |
| | | { |
| | | fprintf(stderr, "Crop Layer: %d x %d -> %d x %d x %d image\n", h,w,crop_height,crop_width,c); |
| | | crop_layer *layer = calloc(1, sizeof(crop_layer)); |
| | |
| | | layer->w = w; |
| | | layer->c = c; |
| | | layer->flip = flip; |
| | | layer->angle = angle; |
| | | layer->crop_width = crop_width; |
| | | layer->crop_height = crop_height; |
| | | layer->output = calloc(crop_width*crop_height * c*batch, sizeof(float)); |
| | |
| | | int crop_width; |
| | | int crop_height; |
| | | int flip; |
| | | float angle; |
| | | float *output; |
| | | #ifdef GPU |
| | | float *output_gpu; |
| | |
| | | } crop_layer; |
| | | |
| | | image get_crop_image(crop_layer layer); |
| | | crop_layer *make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip); |
| | | crop_layer *make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip, float angle); |
| | | void forward_crop_layer(const crop_layer layer, network_state state); |
| | | |
| | | #ifdef GPU |
| | |
| | | int flip = (layer.flip && rand()%2); |
| | | int dh = rand()%(layer.h - layer.crop_height + 1); |
| | | int dw = rand()%(layer.w - layer.crop_width + 1); |
| | | float angle = rand_uniform() - .5; |
| | | float radians = layer.angle*3.14159/180.; |
| | | float angle = 2*radians*rand_uniform() - radians; |
| | | if(!state.train){ |
| | | angle = 0; |
| | | flip = 0; |
| | |
| | | forward_crop_layer_kernel<<<cuda_gridsize(size), BLOCK>>>(state.input, size, layer.c, layer.h, layer.w, |
| | | layer.crop_height, layer.crop_width, dh, dw, flip, angle, layer.output_gpu); |
| | | check_error(cudaPeekAtLastError()); |
| | | |
| | | /* |
| | | cuda_pull_array(layer.output_gpu, layer.output, size); |
| | | image im = float_to_image(layer.crop_width, layer.crop_height, layer.c, layer.output + 14*(size/layer.batch)); |
| | | show_image(im, "cropped"); |
| | | cvWaitKey(0); |
| | | */ |
| | | } |
| | | |
| | |
| | | |
| | | int main(int argc, char **argv) |
| | | { |
| | | //test_resize(argv[1]); |
| | | //test_convolutional_layer(); |
| | | if(argc < 2){ |
| | | fprintf(stderr, "usage: %s <function>\n", argv[0]); |
| | |
| | | run_imagenet(argc, argv); |
| | | } else if (0 == strcmp(argv[1], "detection")){ |
| | | run_detection(argc, argv); |
| | | } else if (0 == strcmp(argv[1], "test")){ |
| | | test_resize(argv[2]); |
| | | } else if (0 == strcmp(argv[1], "captcha")){ |
| | | run_captcha(argc, argv); |
| | | } else if (0 == strcmp(argv[1], "change")){ |
| | |
| | | d.y = make_matrix(n, k); |
| | | for(i = 0; i < n; ++i){ |
| | | image orig = load_image_color(random_paths[i], 0, 0); |
| | | translate_image(orig, -128); |
| | | scale_image(orig, 1./128); |
| | | int oh = orig.h; |
| | | int ow = orig.w; |
| | | |
| | |
| | | float sx = (float)swidth / ow; |
| | | float sy = (float)sheight / oh; |
| | | |
| | | /* |
| | | float angle = rand_uniform()*.1 - .05; |
| | | image rot = rotate_image(orig, angle); |
| | | free_image(orig); |
| | | orig = rot; |
| | | */ |
| | | |
| | | int flip = rand()%2; |
| | | image cropped = crop_image(orig, pleft, ptop, swidth, sheight); |
| | | float dx = ((float)pleft/ow)/sx; |
| | |
| | | printf("Loading data: %d\n", rand()); |
| | | struct load_args a = *(struct load_args*)ptr; |
| | | *a.d = load_data_detection_jitter_random(a.n, a.paths, a.m, a.classes, a.w, a.h, a.num_boxes, a.background); |
| | | translate_data_rows(*a.d, -128); |
| | | scale_data_rows(*a.d, 1./128); |
| | | free(ptr); |
| | | return 0; |
| | | } |
| | |
| | | X.vals[i][j] = (double)bytes[j+1]; |
| | | } |
| | | } |
| | | translate_data_rows(d, -144); |
| | | translate_data_rows(d, -128); |
| | | scale_data_rows(d, 1./128); |
| | | //normalize_data_rows(d); |
| | | fclose(fp); |
| | |
| | | fclose(fp); |
| | | } |
| | | //normalize_data_rows(d); |
| | | translate_data_rows(d, -144); |
| | | translate_data_rows(d, -128); |
| | | scale_data_rows(d, 1./128); |
| | | return d; |
| | | } |
| | |
| | | load_thread = load_data_detection_thread(imgs, paths, plist->size, classes, net.w, net.h, side, side, background, &buffer); |
| | | |
| | | /* |
| | | image im = float_to_image(im_dim, im_dim, 3, train.X.vals[114]); |
| | | image im = float_to_image(net.w, net.h, 3, train.X.vals[114]); |
| | | draw_detection(im, train.y.vals[114], 7); |
| | | */ |
| | | |
| | |
| | | return copy; |
| | | } |
| | | |
| | | |
| | | void show_image(image p, char *name) |
| | | { |
| | | int x,y,k; |
| | |
| | | for(i = 0; i < w; ++i){ |
| | | int r = j + dy; |
| | | int c = i + dx; |
| | | float val = 128; |
| | | float val = 0; |
| | | if (r >= 0 && r < im.h && c >= 0 && c < im.w) { |
| | | val = get_pixel(im, c, r, k); |
| | | } |
| | |
| | | return cropped; |
| | | } |
| | | |
| | | image grayscale_image(image im) |
| | | { |
| | | assert(im.c == 3); |
| | | int i, j, k; |
| | | image gray = make_image(im.w, im.h, im.c); |
| | | float scale[] = {0.114, 0.587, 0.299}; |
| | | for(k = 0; k < im.c; ++k){ |
| | | for(j = 0; j < im.h; ++j){ |
| | | for(i = 0; i < im.w; ++i){ |
| | | gray.data[i+im.w*j] += scale[k]*get_pixel(im, i, j, k); |
| | | } |
| | | } |
| | | } |
| | | memcpy(gray.data + im.w*im.h*1, gray.data, sizeof(float)*im.w*im.h); |
| | | memcpy(gray.data + im.w*im.h*2, gray.data, sizeof(float)*im.w*im.h); |
| | | return gray; |
| | | } |
| | | |
| | | image blend_image(image fore, image back, float alpha) |
| | | { |
| | | assert(fore.w == back.w && fore.h == back.h && fore.c == back.c); |
| | | image blend = make_image(fore.w, fore.h, fore.c); |
| | | int i, j, k; |
| | | for(k = 0; k < fore.c; ++k){ |
| | | for(j = 0; j < fore.h; ++j){ |
| | | for(i = 0; i < fore.w; ++i){ |
| | | float val = alpha * get_pixel(fore, i, j, k) + |
| | | (1 - alpha)* get_pixel(back, i, j, k); |
| | | set_pixel(blend, i, j, k, val); |
| | | } |
| | | } |
| | | } |
| | | return blend; |
| | | } |
| | | |
| | | image saturate_image(image im, float sat) |
| | | { |
| | | image gray = grayscale_image(im); |
| | | image blend = blend_image(im, gray, sat); |
| | | free_image(gray); |
| | | return blend; |
| | | } |
| | | |
| | | image brightness_image(image im, float b) |
| | | { |
| | | image bright = make_image(im.w, im.h, im.c); |
| | | } |
| | | |
| | | float billinear_interpolate(image im, float x, float y, int c) |
| | | { |
| | | int ix = (int) floorf(x); |
| | |
| | | image rot = rotate_image(big, .02); |
| | | image rot2 = rotate_image(big, 3.14159265/2.); |
| | | image test = rotate_image(im, .6); |
| | | image gray = grayscale_image(im); |
| | | image sat = saturate_image(im, 2); |
| | | image sat2 = saturate_image(im, .5); |
| | | show_image(im, "original"); |
| | | show_image(gray, "gray"); |
| | | show_image(sat, "sat"); |
| | | show_image(sat2, "sat2"); |
| | | /* |
| | | show_image(small, "smaller"); |
| | | show_image(big, "bigger"); |
| | | show_image(crop, "crop"); |
| | |
| | | show_image(rot, "rot"); |
| | | show_image(rot2, "rot2"); |
| | | show_image(test, "test"); |
| | | */ |
| | | cvWaitKey(0); |
| | | } |
| | | |
| | |
| | | int crop_height = option_find_int(options, "crop_height",1); |
| | | int crop_width = option_find_int(options, "crop_width",1); |
| | | int flip = option_find_int(options, "flip",0); |
| | | float angle = option_find_float(options, "angle",0); |
| | | |
| | | int batch,h,w,c; |
| | | h = params.h; |
| | |
| | | batch=params.batch; |
| | | if(!(h && w && c)) error("Layer before crop layer must output image."); |
| | | |
| | | crop_layer *layer = make_crop_layer(batch,h,w,c,crop_height,crop_width,flip); |
| | | crop_layer *layer = make_crop_layer(batch,h,w,c,crop_height,crop_width,flip, angle); |
| | | option_unused(options); |
| | | return layer; |
| | | } |