Skip to content

Commit

Permalink
fix calloc realloc failure
Browse files Browse the repository at this point in the history
  • Loading branch information
cyyever committed Sep 13, 2019
1 parent c71701b commit 011eed6
Show file tree
Hide file tree
Showing 49 changed files with 489 additions and 504 deletions.
4 changes: 2 additions & 2 deletions src/activation_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ layer make_activation_layer(int batch, int inputs, ACTIVATION activation)
l.outputs = inputs;
l.batch=batch;

l.output = (float*)calloc(batch * inputs, sizeof(float));
l.delta = (float*)calloc(batch * inputs, sizeof(float));
l.output = (float*)xcalloc(batch * inputs, sizeof(float));
l.delta = (float*)xcalloc(batch * inputs, sizeof(float));

l.forward = forward_activation_layer;
l.backward = backward_activation_layer;
Expand Down
4 changes: 2 additions & 2 deletions src/avgpool_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ avgpool_layer make_avgpool_layer(int batch, int w, int h, int c)
l.outputs = l.out_c;
l.inputs = h*w*c;
int output_size = l.outputs * batch;
l.output = (float*)calloc(output_size, sizeof(float));
l.delta = (float*)calloc(output_size, sizeof(float));
l.output = (float*)xcalloc(output_size, sizeof(float));
l.delta = (float*)xcalloc(output_size, sizeof(float));
l.forward = forward_avgpool_layer;
l.backward = backward_avgpool_layer;
#ifdef GPU
Expand Down
16 changes: 8 additions & 8 deletions src/batchnorm_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ layer make_batchnorm_layer(int batch, int w, int h, int c)
layer.h = layer.out_h = h;
layer.w = layer.out_w = w;
layer.c = layer.out_c = c;
layer.output = (float*)calloc(h * w * c * batch, sizeof(float));
layer.delta = (float*)calloc(h * w * c * batch, sizeof(float));
layer.output = (float*)xcalloc(h * w * c * batch, sizeof(float));
layer.delta = (float*)xcalloc(h * w * c * batch, sizeof(float));
layer.inputs = w*h*c;
layer.outputs = layer.inputs;

layer.scales = (float*)calloc(c, sizeof(float));
layer.scale_updates = (float*)calloc(c, sizeof(float));
layer.scales = (float*)xcalloc(c, sizeof(float));
layer.scale_updates = (float*)xcalloc(c, sizeof(float));
int i;
for(i = 0; i < c; ++i){
layer.scales[i] = 1;
}

layer.mean = (float*)calloc(c, sizeof(float));
layer.variance = (float*)calloc(c, sizeof(float));
layer.mean = (float*)xcalloc(c, sizeof(float));
layer.variance = (float*)xcalloc(c, sizeof(float));

layer.rolling_mean = (float*)calloc(c, sizeof(float));
layer.rolling_variance = (float*)calloc(c, sizeof(float));
layer.rolling_mean = (float*)xcalloc(c, sizeof(float));
layer.rolling_variance = (float*)xcalloc(c, sizeof(float));

layer.forward = forward_batchnorm_layer;
layer.backward = backward_batchnorm_layer;
Expand Down
2 changes: 1 addition & 1 deletion src/blas.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void reorg_cpu(float *x, int out_w, int out_h, int out_c, int batch, int stride,

void flatten(float *x, int size, int layers, int batch, int forward)
{
float* swap = (float*)calloc(size * layers * batch, sizeof(float));
float* swap = (float*)xcalloc(size * layers * batch, sizeof(float));
int i,c,b;
for(b = 0; b < batch; ++b){
for(c = 0; c < layers; ++c){
Expand Down
3 changes: 2 additions & 1 deletion src/box.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "box.h"
#include "utils.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
Expand Down Expand Up @@ -379,7 +380,7 @@ int nms_comparator(const void *pa, const void *pb)
void do_nms_sort_v2(box *boxes, float **probs, int total, int classes, float thresh)
{
int i, j, k;
sortable_bbox* s = (sortable_bbox*)calloc(total, sizeof(sortable_bbox));
sortable_bbox* s = (sortable_bbox*)xcalloc(total, sizeof(sortable_bbox));

for(i = 0; i < total; ++i){
s[i].index = i;
Expand Down
28 changes: 14 additions & 14 deletions src/classifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ float validate_classifier_single(char *datacfg, char *filename, char *weightfile

float *get_regression_values(char **labels, int n)
{
float* v = (float*)calloc(n, sizeof(float));
float* v = (float*)xcalloc(n, sizeof(float));
int i;
for(i = 0; i < n; ++i){
char *p = strchr(labels[i], ' ');
Expand All @@ -35,7 +35,7 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
char *base = basecfg(cfgfile);
printf("%s\n", base);
printf("%d\n", ngpus);
network* nets = (network*)calloc(ngpus, sizeof(network));
network* nets = (network*)xcalloc(ngpus, sizeof(network));

srand(time(0));
int seed = rand();
Expand Down Expand Up @@ -415,7 +415,7 @@ void validate_classifier_10(char *datacfg, char *filename, char *weightfile)

float avg_acc = 0;
float avg_topk = 0;
int* indexes = (int*)calloc(topk, sizeof(int));
int* indexes = (int*)xcalloc(topk, sizeof(int));

for(i = 0; i < m; ++i){
int class_id = -1;
Expand All @@ -442,7 +442,7 @@ void validate_classifier_10(char *datacfg, char *filename, char *weightfile)
images[7] = crop_image(im, 0, 0, w, h);
images[8] = crop_image(im, -shift, shift, w, h);
images[9] = crop_image(im, shift, shift, w, h);
float* pred = (float*)calloc(classes, sizeof(float));
float* pred = (float*)xcalloc(classes, sizeof(float));
for(j = 0; j < 10; ++j){
float *p = network_predict(net, images[j].data);
if(net.hierarchy) hierarchy_predictions(p, net.outputs, net.hierarchy, 1);
Expand Down Expand Up @@ -489,7 +489,7 @@ void validate_classifier_full(char *datacfg, char *filename, char *weightfile)

float avg_acc = 0;
float avg_topk = 0;
int* indexes = (int*)calloc(topk, sizeof(int));
int* indexes = (int*)xcalloc(topk, sizeof(int));

int size = net.w;
for(i = 0; i < m; ++i){
Expand Down Expand Up @@ -567,7 +567,7 @@ float validate_classifier_single(char *datacfg, char *filename, char *weightfile

float avg_acc = 0;
float avg_topk = 0;
int* indexes = (int*)calloc(topk, sizeof(int));
int* indexes = (int*)xcalloc(topk, sizeof(int));

for(i = 0; i < m; ++i){
int class_id = -1;
Expand Down Expand Up @@ -638,7 +638,7 @@ void validate_classifier_multi(char *datacfg, char *filename, char *weightfile)

float avg_acc = 0;
float avg_topk = 0;
int* indexes = (int*)calloc(topk, sizeof(int));
int* indexes = (int*)xcalloc(topk, sizeof(int));

for(i = 0; i < m; ++i){
int class_id = -1;
Expand All @@ -649,7 +649,7 @@ void validate_classifier_multi(char *datacfg, char *filename, char *weightfile)
break;
}
}
float* pred = (float*)calloc(classes, sizeof(float));
float* pred = (float*)xcalloc(classes, sizeof(float));
image im = load_image_color(paths[i], 0, 0);
for(j = 0; j < nscales; ++j){
image r = resize_min(im, scales[j]);
Expand Down Expand Up @@ -694,7 +694,7 @@ void try_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filena

char **names = get_labels(name_list);
clock_t time;
int* indexes = (int*)calloc(top, sizeof(int));
int* indexes = (int*)xcalloc(top, sizeof(int));
char buff[256];
char *input = buff;
while(1){
Expand Down Expand Up @@ -781,7 +781,7 @@ void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *fi
int i = 0;
char **names = get_labels(name_list);
clock_t time;
int* indexes = (int*)calloc(top, sizeof(int));
int* indexes = (int*)xcalloc(top, sizeof(int));
if(!indexes) {
error("calloc failed");
}
Expand Down Expand Up @@ -971,7 +971,7 @@ void threat_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_i
char *name_list = option_find_str(options, "names", 0);
char **names = get_labels(name_list);

int* indexes = (int*)calloc(top, sizeof(int));
int* indexes = (int*)xcalloc(top, sizeof(int));

if(!cap) error("Couldn't connect to webcam.\n");
create_window_cv("Threat", 0, 512, 512);
Expand Down Expand Up @@ -1110,7 +1110,7 @@ void gun_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_inde
char *name_list = option_find_str(options, "names", 0);
char **names = get_labels(name_list);

int* indexes = (int*)calloc(top, sizeof(int));
int* indexes = (int*)xcalloc(top, sizeof(int));

if(!cap) error("Couldn't connect to webcam.\n");
cvNamedWindow("Threat Detection", CV_WINDOW_NORMAL);
Expand Down Expand Up @@ -1193,7 +1193,7 @@ void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_ind
char *name_list = option_find_str(options, "names", 0);
char **names = get_labels(name_list);

int* indexes = (int*)calloc(top, sizeof(int));
int* indexes = (int*)xcalloc(top, sizeof(int));

if(!cap) error("Couldn't connect to webcam.\n");
create_window_cv("Classifier", 0, 512, 512);
Expand Down Expand Up @@ -1258,7 +1258,7 @@ void run_classifier(int argc, char **argv)
for(i = 0; i < len; ++i){
if (gpu_list[i] == ',') ++ngpus;
}
gpus = (int*)calloc(ngpus, sizeof(int));
gpus = (int*)xcalloc(ngpus, sizeof(int));
for(i = 0; i < ngpus; ++i){
gpus[i] = atoi(gpu_list);
gpu_list = strchr(gpu_list, ',')+1;
Expand Down
51 changes: 15 additions & 36 deletions src/coco.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ void validate_coco(char *cfgfile, char *weightfile)
FILE *fp = fopen(buff, "w");
fprintf(fp, "[\n");

box* boxes = (box*)calloc(side * side * l.n, sizeof(box));
float** probs = (float**)calloc(side * side * l.n, sizeof(float*));
for(j = 0; j < side*side*l.n; ++j) probs[j] = (float*)calloc(classes, sizeof(float));
box* boxes = (box*)xcalloc(side * side * l.n, sizeof(box));
float** probs = (float**)xcalloc(side * side * l.n, sizeof(float*));
for(j = 0; j < side*side*l.n; ++j) probs[j] = (float*)xcalloc(classes, sizeof(float));

int m = plist->size;
int i=0;
Expand All @@ -173,11 +173,11 @@ void validate_coco(char *cfgfile, char *weightfile)
float iou_thresh = .5;

int nthreads = 8;
image* val = (image*)calloc(nthreads, sizeof(image));
image* val_resized = (image*)calloc(nthreads, sizeof(image));
image* buf = (image*)calloc(nthreads, sizeof(image));
image* buf_resized = (image*)calloc(nthreads, sizeof(image));
pthread_t* thr = (pthread_t*)calloc(nthreads, sizeof(pthread_t));
image* val = (image*)xcalloc(nthreads, sizeof(image));
image* val_resized = (image*)xcalloc(nthreads, sizeof(image));
image* buf = (image*)xcalloc(nthreads, sizeof(image));
image* buf_resized = (image*)xcalloc(nthreads, sizeof(image));
pthread_t* thr = (pthread_t*)xcalloc(nthreads, sizeof(pthread_t));

load_args args = {0};
args.w = net.w;
Expand Down Expand Up @@ -249,29 +249,17 @@ void validate_coco_recall(char *cfgfile, char *weightfile)

int j, k;
/* unused code,why?
FILE** fps = (FILE**)calloc(classes, sizeof(FILE*));
if(!fps) {
error("calloc failed");
}
FILE** fps = (FILE**)xcalloc(classes, sizeof(FILE*));
for(j = 0; j < classes; ++j){
char buff[1024];
snprintf(buff, 1024, "%s%s.txt", base, coco_classes[j]);
fps[j] = fopen(buff, "w");
}
*/
box* boxes = (box*)calloc(side * side * l.n, sizeof(box));
if(!boxes) {
error("calloc failed");
}
float** probs = (float**)calloc(side * side * l.n, sizeof(float*));
if(!probs) {
error("calloc failed");
}
box* boxes = (box*)xcalloc(side * side * l.n, sizeof(box));
float** probs = (float**)xcalloc(side * side * l.n, sizeof(float*));
for(j = 0; j < side*side*l.n; ++j) {
probs[j] = (float*)calloc(classes, sizeof(float));
if(!probs[j]) {
error("calloc failed");
}
probs[j] = (float*)xcalloc(classes, sizeof(float));
}

int m = plist->size;
Expand Down Expand Up @@ -349,19 +337,10 @@ void test_coco(char *cfgfile, char *weightfile, char *filename, float thresh)
char buff[256];
char *input = buff;
int j;
box* boxes = (box*)calloc(l.side * l.side * l.n, sizeof(box));
if(!boxes) {
error("calloc failed");
}
float** probs = (float**)calloc(l.side * l.side * l.n, sizeof(float*));
if(!probs) {
error("calloc failed");
}
box* boxes = (box*)xcalloc(l.side * l.side * l.n, sizeof(box));
float** probs = (float**)xcalloc(l.side * l.side * l.n, sizeof(float*));
for(j = 0; j < l.side*l.side*l.n; ++j) {
probs[j] = (float*)calloc(l.classes, sizeof(float));
if(!probs[j]) {
error("calloc failed");
}
probs[j] = (float*)xcalloc(l.classes, sizeof(float));
}
while(1){
if(filename){
Expand Down
10 changes: 5 additions & 5 deletions src/compare.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ int bbox_comparator(const void *a, const void *b)

image im1 = load_image_color(box1.filename, net.w, net.h);
image im2 = load_image_color(box2.filename, net.w, net.h);
float* X = (float*)calloc(net.w * net.h * net.c, sizeof(float));
float* X = (float*)xcalloc(net.w * net.h * net.c, sizeof(float));
memcpy(X, im1.data, im1.w*im1.h*im1.c*sizeof(float));
memcpy(X+im1.w*im1.h*im1.c, im2.data, im2.w*im2.h*im2.c*sizeof(float));
float *predictions = network_predict(net, X);
Expand Down Expand Up @@ -205,7 +205,7 @@ void bbox_fight(network net, sortable_bbox *a, sortable_bbox *b, int classes, in
{
image im1 = load_image_color(a->filename, net.w, net.h);
image im2 = load_image_color(b->filename, net.w, net.h);
float* X = (float*)calloc(net.w * net.h * net.c, sizeof(float));
float* X = (float*)xcalloc(net.w * net.h * net.c, sizeof(float));
memcpy(X, im1.data, im1.w*im1.h*im1.c*sizeof(float));
memcpy(X+im1.w*im1.h*im1.c, im2.data, im2.w*im2.h*im2.c*sizeof(float));
float *predictions = network_predict(net, X);
Expand Down Expand Up @@ -239,7 +239,7 @@ void SortMaster3000(char *filename, char *weightfile)
char **paths = (char **)list_to_array(plist);
int N = plist->size;
free_list(plist);
sortable_bbox* boxes = (sortable_bbox*)calloc(N, sizeof(sortable_bbox));
sortable_bbox* boxes = (sortable_bbox*)xcalloc(N, sizeof(sortable_bbox));
printf("Sorting %d boxes...\n", N);
for(i = 0; i < N; ++i){
boxes[i].filename = paths[i];
Expand Down Expand Up @@ -274,13 +274,13 @@ void BattleRoyaleWithCheese(char *filename, char *weightfile)
int N = plist->size;
int total = N;
free_list(plist);
sortable_bbox* boxes = (sortable_bbox*)calloc(N, sizeof(sortable_bbox));
sortable_bbox* boxes = (sortable_bbox*)xcalloc(N, sizeof(sortable_bbox));
printf("Battling %d boxes...\n", N);
for(i = 0; i < N; ++i){
boxes[i].filename = paths[i];
boxes[i].net = net;
boxes[i].classes = classes;
boxes[i].elos = (float*)calloc(classes, sizeof(float));
boxes[i].elos = (float*)xcalloc(classes, sizeof(float));
for(j = 0; j < classes; ++j){
boxes[i].elos[j] = 1500;
}
Expand Down
32 changes: 16 additions & 16 deletions src/connected_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ connected_layer make_connected_layer(int batch, int steps, int inputs, int outpu
l.activation = activation;
l.learning_rate_scale = 1;

l.output = (float*)calloc(total_batch * outputs, sizeof(float));
l.delta = (float*)calloc(total_batch * outputs, sizeof(float));
l.output = (float*)xcalloc(total_batch * outputs, sizeof(float));
l.delta = (float*)xcalloc(total_batch * outputs, sizeof(float));

l.weight_updates = (float*)calloc(inputs * outputs, sizeof(float));
l.bias_updates = (float*)calloc(outputs, sizeof(float));
l.weight_updates = (float*)xcalloc(inputs * outputs, sizeof(float));
l.bias_updates = (float*)xcalloc(outputs, sizeof(float));

l.weights = (float*)calloc(outputs * inputs, sizeof(float));
l.biases = (float*)calloc(outputs, sizeof(float));
l.weights = (float*)xcalloc(outputs * inputs, sizeof(float));
l.biases = (float*)xcalloc(outputs, sizeof(float));

l.forward = forward_connected_layer;
l.backward = backward_connected_layer;
Expand All @@ -98,22 +98,22 @@ connected_layer make_connected_layer(int batch, int steps, int inputs, int outpu
}

if(batch_normalize){
l.scales = (float*)calloc(outputs, sizeof(float));
l.scale_updates = (float*)calloc(outputs, sizeof(float));
l.scales = (float*)xcalloc(outputs, sizeof(float));
l.scale_updates = (float*)xcalloc(outputs, sizeof(float));
for(i = 0; i < outputs; ++i){
l.scales[i] = 1;
}

l.mean = (float*)calloc(outputs, sizeof(float));
l.mean_delta = (float*)calloc(outputs, sizeof(float));
l.variance = (float*)calloc(outputs, sizeof(float));
l.variance_delta = (float*)calloc(outputs, sizeof(float));
l.mean = (float*)xcalloc(outputs, sizeof(float));
l.mean_delta = (float*)xcalloc(outputs, sizeof(float));
l.variance = (float*)xcalloc(outputs, sizeof(float));
l.variance_delta = (float*)xcalloc(outputs, sizeof(float));

l.rolling_mean = (float*)calloc(outputs, sizeof(float));
l.rolling_variance = (float*)calloc(outputs, sizeof(float));
l.rolling_mean = (float*)xcalloc(outputs, sizeof(float));
l.rolling_variance = (float*)xcalloc(outputs, sizeof(float));

l.x = (float*)calloc(total_batch * outputs, sizeof(float));
l.x_norm = (float*)calloc(total_batch * outputs, sizeof(float));
l.x = (float*)xcalloc(total_batch * outputs, sizeof(float));
l.x_norm = (float*)xcalloc(total_batch * outputs, sizeof(float));
}

#ifdef GPU
Expand Down
Loading

0 comments on commit 011eed6

Please sign in to comment.