Starting on server/client

This commit is contained in:
Joseph Redmon 2014-12-03 08:48:07 -08:00
parent b77a8f3987
commit ff67f03476
9 changed files with 251 additions and 84 deletions

View File

@ -33,7 +33,7 @@ VPATH=./src/
EXEC=cnn EXEC=cnn
OBJDIR=./obj/ OBJDIR=./obj/
OBJ=network.o network_gpu.o image.o cnn.o connected_layer.o maxpool_layer.o activations.o list.o option_list.o parser.o utils.o data.o matrix.o softmax_layer.o mini_blas.o convolutional_layer.o gemm.o normalization_layer.o opencl.o im2col.o col2im.o axpy.o dropout_layer.o crop_layer.o freeweight_layer.o cost_layer.o OBJ=network.o network_gpu.o image.o cnn.o connected_layer.o maxpool_layer.o activations.o list.o option_list.o parser.o utils.o data.o matrix.o softmax_layer.o mini_blas.o convolutional_layer.o gemm.o normalization_layer.o opencl.o im2col.o col2im.o axpy.o dropout_layer.o crop_layer.o freeweight_layer.o cost_layer.o server.o
OBJS = $(addprefix $(OBJDIR), $(OBJ)) OBJS = $(addprefix $(OBJDIR), $(OBJ))
all: $(EXEC) all: $(EXEC)

172
src/cnn.c
View File

@ -312,7 +312,8 @@ void train_detection_net()
network net = parse_network_cfg("cfg/detnet.cfg"); network net = parse_network_cfg("cfg/detnet.cfg");
printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
int imgs = 1000/net.batch+1; int imgs = 1000/net.batch+1;
srand(time(0)); //srand(time(0));
srand(23410);
int i = 0; int i = 0;
list *plist = get_paths("/home/pjreddie/data/imagenet/horse.txt"); list *plist = get_paths("/home/pjreddie/data/imagenet/horse.txt");
char **paths = (char **)list_to_array(plist); char **paths = (char **)list_to_array(plist);
@ -323,6 +324,31 @@ void train_detection_net()
time=clock(); time=clock();
data train = load_data_detection_random(imgs*net.batch, paths, plist->size, 256, 256, 8, 8, 256); data train = load_data_detection_random(imgs*net.batch, paths, plist->size, 256, 256, 8, 8, 256);
//translate_data_rows(train, -144); //translate_data_rows(train, -144);
/*
image im = float_to_image(256, 256, 3, train.X.vals[0]);
float *truth = train.y.vals[0];
int j;
int r, c;
for(r = 0; r < 8; ++r){
for(c = 0; c < 8; ++c){
j = (r*8 + c) * 5;
if(truth[j]){
int d = 256/8;
int y = r*d+truth[j+1]*d;
int x = c*d+truth[j+2]*d;
int h = truth[j+3]*256;
int w = truth[j+4]*256;
printf("%f %f %f %f\n", truth[j+1], truth[j+2], truth[j+3], truth[j+4]);
printf("%d %d %d %d\n", x, y, w, h);
printf("%d %d %d %d\n", x-w/2, y-h/2, x+w/2, y+h/2);
draw_box(im, x-w/2, y-h/2, x+w/2, y+h/2);
}
}
}
show_image(im, "box");
cvWaitKey(0);
*/
normalize_data_rows(train); normalize_data_rows(train);
printf("Loaded: %lf seconds\n", sec(clock()-time)); printf("Loaded: %lf seconds\n", sec(clock()-time));
time=clock(); time=clock();
@ -334,7 +360,7 @@ void train_detection_net()
free_data(train); free_data(train);
if(i%10==0){ if(i%10==0){
char buff[256]; char buff[256];
sprintf(buff, "/home/pjreddie/imagenet_backup/imagenet_%d.cfg", i); sprintf(buff, "/home/pjreddie/imagenet_backup/detnet_%d.cfg", i);
save_network(net, buff); save_network(net, buff);
} }
} }
@ -345,7 +371,7 @@ void train_imagenet()
{ {
float avg_loss = 1; float avg_loss = 1;
//network net = parse_network_cfg("/home/pjreddie/imagenet_backup/alexnet_1270.cfg"); //network net = parse_network_cfg("/home/pjreddie/imagenet_backup/alexnet_1270.cfg");
network net = parse_network_cfg("cfg/trained_alexnet.cfg"); network net = parse_network_cfg("cfg/alexnet.part");
printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
int imgs = 1000/net.batch+1; int imgs = 1000/net.batch+1;
srand(time(0)); srand(time(0));
@ -371,7 +397,7 @@ void train_imagenet()
free_data(train); free_data(train);
if(i%10==0){ if(i%10==0){
char buff[256]; char buff[256];
sprintf(buff, "/home/pjreddie/imagenet_backup/imagenet_%d.cfg", i); sprintf(buff, "/home/pjreddie/imagenet_backup/alexnet_%d.cfg", i);
save_network(net, buff); save_network(net, buff);
} }
} }
@ -399,6 +425,7 @@ void validate_imagenet(char *filename)
char **part = paths+(i*m/splits); char **part = paths+(i*m/splits);
int num = (i+1)*m/splits - i*m/splits; int num = (i+1)*m/splits - i*m/splits;
data val = load_data(part, num, labels, 1000, 256, 256); data val = load_data(part, num, labels, 1000, 256, 256);
normalize_data_rows(val); normalize_data_rows(val);
printf("Loaded: %d images in %lf seconds\n", val.X.rows, sec(clock()-time)); printf("Loaded: %d images in %lf seconds\n", val.X.rows, sec(clock()-time));
time=clock(); time=clock();
@ -411,25 +438,48 @@ void validate_imagenet(char *filename)
} }
} }
void draw_detection(image im, float *box)
{
int j;
int r, c;
for(r = 0; r < 8; ++r){
for(c = 0; c < 8; ++c){
j = (r*8 + c) * 5;
printf("Prob: %f\n", box[j]);
if(box[j] > .999){
int d = 256/8;
int y = r*d+box[j+1]*d;
int x = c*d+box[j+2]*d;
int h = box[j+3]*256;
int w = box[j+4]*256;
printf("%f %f %f %f\n", box[j+1], box[j+2], box[j+3], box[j+4]);
printf("%d %d %d %d\n", x, y, w, h);
printf("%d %d %d %d\n", x-w/2, y-h/2, x+w/2, y+h/2);
draw_box(im, x-w/2, y-h/2, x+w/2, y+h/2);
}
}
}
show_image(im, "box");
cvWaitKey(0);
}
void test_detection() void test_detection()
{ {
network net = parse_network_cfg("cfg/detnet_test.cfg"); network net = parse_network_cfg("cfg/detnet_test.cfg");
//imgs=1;
srand(2222222); srand(2222222);
int i = 0;
clock_t time; clock_t time;
char filename[256]; char filename[256];
int indexes[10];
while(1){ while(1){
fgets(filename, 256, stdin); fgets(filename, 256, stdin);
strtok(filename, "\n");
image im = load_image_color(filename, 256, 256); image im = load_image_color(filename, 256, 256);
z_normalize_image(im); z_normalize_image(im);
printf("%d %d %d\n", im.h, im.w, im.c); printf("%d %d %d\n", im.h, im.w, im.c);
float *X = im.data; float *X = im.data;
time=clock(); time=clock();
float *predictions = network_predict(net, X); float *predictions = network_predict(net, X);
top_predictions(net, 10, indexes);
printf("%s: Predicted in %f seconds.\n", filename, sec(clock()-time)); printf("%s: Predicted in %f seconds.\n", filename, sec(clock()-time));
draw_detection(im, predictions);
free_image(im); free_image(im);
} }
} }
@ -446,6 +496,7 @@ void test_imagenet()
int indexes[10]; int indexes[10];
while(1){ while(1){
fgets(filename, 256, stdin); fgets(filename, 256, stdin);
strtok(filename, "\n");
image im = load_image_color(filename, 256, 256); image im = load_image_color(filename, 256, 256);
z_normalize_image(im); z_normalize_image(im);
printf("%d %d %d\n", im.h, im.w, im.c); printf("%d %d %d\n", im.h, im.w, im.c);
@ -731,6 +782,14 @@ void test_gpu_net()
#endif #endif
} }
void test_server()
{
server_update();
}
void test_client()
{
client_update();
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -744,6 +803,9 @@ int main(int argc, char *argv[])
else if(0==strcmp(argv[1], "nist")) train_nist(); else if(0==strcmp(argv[1], "nist")) train_nist();
else if(0==strcmp(argv[1], "test_correct")) test_gpu_net(); else if(0==strcmp(argv[1], "test_correct")) test_gpu_net();
else if(0==strcmp(argv[1], "test")) test_imagenet(); else if(0==strcmp(argv[1], "test")) test_imagenet();
else if(0==strcmp(argv[1], "server")) test_server();
else if(0==strcmp(argv[1], "client")) test_client();
else if(0==strcmp(argv[1], "detect")) test_detection();
else if(0==strcmp(argv[1], "visualize")) test_visualize(argv[2]); else if(0==strcmp(argv[1], "visualize")) test_visualize(argv[2]);
else if(0==strcmp(argv[1], "valid")) validate_imagenet(argv[2]); else if(0==strcmp(argv[1], "valid")) validate_imagenet(argv[2]);
#ifdef GPU #ifdef GPU
@ -754,8 +816,8 @@ int main(int argc, char *argv[])
} }
/* /*
void visualize_imagenet_topk(char *filename) void visualize_imagenet_topk(char *filename)
{ {
int i,j,k,l; int i,j,k,l;
int topk = 10; int topk = 10;
network net = parse_network_cfg("cfg/voc_imagenet.cfg"); network net = parse_network_cfg("cfg/voc_imagenet.cfg");
@ -780,53 +842,53 @@ void visualize_imagenet_topk(char *filename)
if(im.h < 200 || im.w < 200) continue; if(im.h < 200 || im.w < 200) continue;
printf("Processing %dx%d image\n", im.h, im.w); printf("Processing %dx%d image\n", im.h, im.w);
resize_network(net, im.h, im.w, im.c); resize_network(net, im.h, im.w, im.c);
//scale_image(im, 1./255); //scale_image(im, 1./255);
translate_image(im, -144); translate_image(im, -144);
forward_network(net, im.data, 0, 0); forward_network(net, im.data, 0, 0);
image out = get_network_image(net); image out = get_network_image(net);
int dh = (im.h - h)/(out.h-1); int dh = (im.h - h)/(out.h-1);
int dw = (im.w - w)/(out.w-1); int dw = (im.w - w)/(out.w-1);
//printf("%d %d\n", dh, dw); //printf("%d %d\n", dh, dw);
for(k = 0; k < out.c; ++k){ for(k = 0; k < out.c; ++k){
float topv = 0; float topv = 0;
int topi = -1; int topi = -1;
int topj = -1; int topj = -1;
for(i = 0; i < out.h; ++i){ for(i = 0; i < out.h; ++i){
for(j = 0; j < out.w; ++j){ for(j = 0; j < out.w; ++j){
float val = get_pixel(out, i, j, k); float val = get_pixel(out, i, j, k);
if(val > topv){ if(val > topv){
topv = val; topv = val;
topi = i; topi = i;
topj = j; topj = j;
} }
} }
} }
if(topv){ if(topv){
image sub = get_sub_image(im, dh*topi, dw*topj, h, w); image sub = get_sub_image(im, dh*topi, dw*topj, h, w);
for(l = 0; l < topk; ++l){ for(l = 0; l < topk; ++l){
if(topv > score[k][l]){ if(topv > score[k][l]){
float swap = score[k][l]; float swap = score[k][l];
score[k][l] = topv; score[k][l] = topv;
topv = swap; topv = swap;
image swapi = vizs[k][l]; image swapi = vizs[k][l];
vizs[k][l] = sub; vizs[k][l] = sub;
sub = swapi; sub = swapi;
} }
} }
free_image(sub); free_image(sub);
} }
} }
free_image(im); free_image(im);
if(count%50 == 0){ if(count%50 == 0){
image grid = grid_images(vizs, num, topk); image grid = grid_images(vizs, num, topk);
//show_image(grid, "IMAGENET Visualization"); //show_image(grid, "IMAGENET Visualization");
save_image(grid, "IMAGENET Grid Single Nonorm"); save_image(grid, "IMAGENET Grid Single Nonorm");
free_image(grid); free_image(grid);
} }
} }
//cvWaitKey(0); //cvWaitKey(0);
} }
void visualize_imagenet_features(char *filename) void visualize_imagenet_features(char *filename)

View File

@ -65,7 +65,7 @@ convolutional_layer *make_convolutional_layer(int batch, int h, int w, int c, in
layer->bias_updates = calloc(n, sizeof(float)); layer->bias_updates = calloc(n, sizeof(float));
layer->bias_momentum = calloc(n, sizeof(float)); layer->bias_momentum = calloc(n, sizeof(float));
float scale = 1./(size*size*c); float scale = 1./(size*size*c);
scale = .01; scale = .05;
for(i = 0; i < c*n*size*size; ++i) layer->filters[i] = scale*2*(rand_uniform()-.5); for(i = 0; i < c*n*size*size; ++i) layer->filters[i] = scale*2*(rand_uniform()-.5);
for(i = 0; i < n; ++i){ for(i = 0; i < n; ++i){
//layer->biases[i] = rand_normal()*scale + scale; //layer->biases[i] = rand_normal()*scale + scale;

View File

@ -31,8 +31,8 @@ void fill_truth_detection(char *path, float *truth, int height, int width, int n
while(fscanf(file, "%d %d %d %d", &x, &y, &w, &h) == 4){ while(fscanf(file, "%d %d %d %d", &x, &y, &w, &h) == 4){
int i = x/box_width; int i = x/box_width;
int j = y/box_height; int j = y/box_height;
float dh = (float)(x%box_width)/box_height; float dw = (float)(x%box_width)/box_height;
float dw = (float)(y%box_width)/box_width; float dh = (float)(y%box_width)/box_width;
float sh = h/scale; float sh = h/scale;
float sw = w/scale; float sw = w/scale;
//printf("%d %d %f %f\n", i, j, dh, dw); //printf("%d %d %f %f\n", i, j, dh, dw);

View File

@ -4,6 +4,23 @@
int windows = 0; int windows = 0;
void draw_box(image a, int x1, int y1, int x2, int y2)
{
int i, c;
for(c = 0; c < a.c; ++c){
for(i = x1; i < x2; ++i){
a.data[i + y1*a.w + c*a.w*a.h] = 0;
a.data[i + y2*a.w + c*a.w*a.h] = 0;
}
}
for(c = 0; c < a.c; ++c){
for(i = y1; i < y2; ++i){
a.data[x1 + i*a.w + c*a.w*a.h] = 0;
a.data[x2 + i*a.w + c*a.w*a.h] = 0;
}
}
}
image image_distance(image a, image b) image image_distance(image a, image b)
{ {
int i,j; int i,j;
@ -424,7 +441,7 @@ image load_image_color(char *filename, int h, int w)
} }
if(h && w && (src->height != h || src->width != w)){ if(h && w && (src->height != h || src->width != w)){
//printf("Resized!\n"); //printf("Resized!\n");
IplImage *resized = resizeImage(src, h, w, 1); IplImage *resized = resizeImage(src, h, w, 0);
cvReleaseImage(&src); cvReleaseImage(&src);
src = resized; src = resized;
} }

View File

@ -11,6 +11,7 @@ typedef struct {
float *data; float *data;
} image; } image;
void draw_box(image a, int x1, int y1, int x2, int y2);
image image_distance(image a, image b); image image_distance(image a, image b);
void scale_image(image m, float s); void scale_image(image m, float s);
void translate_image(image m, float s); void translate_image(image m, float s);

View File

@ -88,7 +88,7 @@ cl_info cl_init()
} }
int index = getpid()%num_devices; int index = getpid()%num_devices;
index = 0; index = 1;
printf("%d rand, %d devices, %d index\n", getpid(), num_devices, index); printf("%d rand, %d devices, %d index\n", getpid(), num_devices, index);
info.device = devices[index]; info.device = devices[index];
fprintf(stderr, "Found %d device(s)\n", num_devices); fprintf(stderr, "Found %d device(s)\n", num_devices);

84
src/server.c Normal file
View File

@ -0,0 +1,84 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /* needed for sockaddr_in */
#include <stdio.h> /* needed for sockaddr_in */
#include <string.h> /* needed for sockaddr_in */
#include <netdb.h>
#include "server.h"
#define MESSAGESIZE 512
#define SERVER_PORT 9876
#define CLIENT_PORT 9879
#define STR(x) #x
#define PARAMETER_SERVER localhost
int socket_setup(int port)
{
static int fd = 0; /* our socket */
if(fd) return fd;
struct sockaddr_in myaddr; /* our address */
/* create a UDP socket */
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("cannot create socket\n");
fd=0;
return 0;
}
/* bind the socket to any valid IP address and a specific port */
memset((char *)&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(port);
if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
perror("bind failed");
fd=0;
return 0;
}
return fd;
}
void server_update()
{
int fd = socket_setup(SERVER_PORT);
struct sockaddr_in remaddr; /* remote address */
socklen_t addrlen = sizeof(remaddr); /* length of addresses */
int recvlen; /* # bytes received */
unsigned char buf[MESSAGESIZE]; /* receive buffer */
recvlen = recvfrom(fd, buf, MESSAGESIZE, 0, (struct sockaddr *)&remaddr, &addrlen);
buf[recvlen] = 0;
printf("received %d bytes\n", recvlen);
printf("%s\n", buf);
}
void client_update()
{
int fd = socket_setup(CLIENT_PORT);
struct hostent *hp; /* host information */
struct sockaddr_in servaddr; /* server address */
char *my_message = "this is a test message";
/* fill in the server's address and data */
memset((char*)&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERVER_PORT);
/* look up the address of the server given its name */
hp = gethostbyname("localhost");
if (!hp) {
fprintf(stderr, "could not obtain address of %s\n", "localhost");
}
/* put the host's address into the server address structure */
memcpy((void *)&servaddr.sin_addr, hp->h_addr_list[0], hp->h_length);
/* send a message to the server */
if (sendto(fd, my_message, strlen(my_message), 0, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("sendto failed");
}
}

3
src/server.h Normal file
View File

@ -0,0 +1,3 @@
void server_update();
void client_update();