Fixed memory leaks for Yolo: train, test

pull/901/head^2
AlexeyAB 7 years ago
parent 768b190b10
commit ec68838342
  1. 5
      build/darknet/darknet.vcxproj
  2. 2
      src/convolutional_layer.c
  3. 27
      src/demo.c
  4. 30
      src/detector.c
  5. 1
      src/layer.c
  6. 12
      src/list.c
  7. 1
      src/list.h
  8. 8
      src/utils.c

@ -89,9 +89,10 @@
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>C:\opencv_3.0\opencv\build\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>C:\opencv_3.0\opencv\build\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_CRTDBG_MAP_ALLOC;_MBCS;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;_CRT_RAND_S;GPU;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>CUDNN;_CRTDBG_MAP_ALLOC;_MBCS;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;_CRT_RAND_S;GPU;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<UndefinePreprocessorDefinitions>CUDNN;OPENCV;</UndefinePreprocessorDefinitions> <UndefinePreprocessorDefinitions>OPENCV;</UndefinePreprocessorDefinitions>
<MultiProcessorCompilation>true</MultiProcessorCompilation> <MultiProcessorCompilation>true</MultiProcessorCompilation>
<ForcedIncludeFiles>stdlib.h;crtdbg.h;%(ForcedIncludeFiles)</ForcedIncludeFiles>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>

@ -502,7 +502,7 @@ void resize_convolutional_layer(convolutional_layer *l, int w, int h)
size_t total_byte; size_t total_byte;
check_error(cudaMemGetInfo(&free_byte, &total_byte)); check_error(cudaMemGetInfo(&free_byte, &total_byte));
if (l->workspace_size > free_byte || l->workspace_size >= total_byte / 2) { if (l->workspace_size > free_byte || l->workspace_size >= total_byte / 2) {
printf(" used slow CUDNN algo without Workspace! Need memory: %d, available: %d\n", l->workspace_size, (free_byte < total_byte/2) ? free_byte : total_byte/2); printf(" used slow CUDNN algo without Workspace! Need memory: %zu, available: %zu\n", l->workspace_size, (free_byte < total_byte/2) ? free_byte : total_byte/2);
cudnn_convolutional_setup(l, cudnn_smallest); cudnn_convolutional_setup(l, cudnn_smallest);
l->workspace_size = get_workspace_size(*l); l->workspace_size = get_workspace_size(*l);
} }

@ -312,6 +312,33 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
cvReleaseVideoWriter(&output_video_writer); cvReleaseVideoWriter(&output_video_writer);
printf("output_video_writer closed. \n"); printf("output_video_writer closed. \n");
} }
// free memory
cvReleaseImage(&show_img);
cvReleaseImage(&in_img);
free_image(in_s);
free(avg);
for (j = 0; j < FRAMES; ++j) free(predictions[j]);
for (j = 0; j < FRAMES; ++j) free_image(images[j]);
for (j = 0; j < l.w*l.h*l.n; ++j) free(probs[j]);
free(boxes);
free(probs);
free_ptrs(names, net.layers[net.n - 1].classes);
int i;
const int nsize = 8;
for (j = 0; j < nsize; ++j) {
for (i = 32; i < 127; ++i) {
free_image(alphabet[j][i]);
}
free(alphabet[j]);
}
free(alphabet);
free_network(net);
} }
#else #else
void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int cam_index, const char *filename, char **names, int classes, void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int cam_index, const char *filename, char **names, int classes,

@ -1,8 +1,3 @@
#ifdef _DEBUG
#include <stdlib.h>
#include <crtdbg.h>
#endif
#include "network.h" #include "network.h"
#include "region_layer.h" #include "region_layer.h"
#include "cost_layer.h" #include "cost_layer.h"
@ -221,8 +216,25 @@ void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, i
sprintf(buff, "%s/%s_final.weights", backup_directory, base); sprintf(buff, "%s/%s_final.weights", backup_directory, base);
save_weights(net, buff); save_weights(net, buff);
//cvReleaseImage(&img); #ifdef OPENCV
//cvDestroyAllWindows(); cvReleaseImage(&img);
cvDestroyAllWindows();
#endif
// free memory
pthread_join(load_thread, 0);
free_data(buffer);
free(base);
free(paths);
free_list_contents(plist);
free_list(plist);
free_list_contents_kvp(options);
free_list(options);
free(nets);
free_network(net);
} }
@ -1150,6 +1162,7 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam
// free memory // free memory
free_ptrs(names, net.layers[net.n - 1].classes); free_ptrs(names, net.layers[net.n - 1].classes);
free_list_contents_kvp(options);
free_list(options); free_list(options);
int i; int i;
@ -1236,6 +1249,9 @@ void run_detector(int argc, char **argv)
if (filename[strlen(filename) - 1] == 0x0d) filename[strlen(filename) - 1] = 0; if (filename[strlen(filename) - 1] == 0x0d) filename[strlen(filename) - 1] = 0;
demo(cfg, weights, thresh, hier_thresh, cam_index, filename, names, classes, frame_skip, prefix, out_filename, demo(cfg, weights, thresh, hier_thresh, cam_index, filename, names, classes, frame_skip, prefix, out_filename,
http_stream_port, dont_show, ext_output); http_stream_port, dont_show, ext_output);
free_list_contents_kvp(options);
free_list(options);
} }
else printf(" There isn't such command: %s", argv[2]); else printf(" There isn't such command: %s", argv[2]);
} }

@ -11,6 +11,7 @@ void free_layer(layer l)
#endif #endif
return; return;
} }
if (l.mask) free(l.mask);
if (l.cweights) free(l.cweights); if (l.cweights) free(l.cweights);
if (l.indexes) free(l.indexes); if (l.indexes) free(l.indexes);
if (l.input_layers) free(l.input_layers); if (l.input_layers) free(l.input_layers);

@ -1,6 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "list.h" #include "list.h"
#include "option_list.h"
list *make_list() list *make_list()
{ {
@ -79,6 +80,17 @@ void free_list_contents(list *l)
} }
} }
void free_list_contents_kvp(list *l)
{
node *n = l->front;
while (n) {
kvp *p = n->val;
free(p->key);
free(n->val);
n = n->next;
}
}
void **list_to_array(list *l) void **list_to_array(list *l)
{ {
void **a = calloc(l->size, sizeof(void*)); void **a = calloc(l->size, sizeof(void*));

@ -22,5 +22,6 @@ void **list_to_array(list *l);
void free_list(list *l); void free_list(list *l);
void free_list_contents(list *l); void free_list_contents(list *l);
void free_list_contents_kvp(list *l);
#endif #endif

@ -175,6 +175,7 @@ void find_replace(char *str, char *orig, char *rep, char *output)
sprintf(buffer, "%s", str); sprintf(buffer, "%s", str);
if(!(p = strstr(buffer, orig))){ // Is 'orig' even in 'str'? if(!(p = strstr(buffer, orig))){ // Is 'orig' even in 'str'?
sprintf(output, "%s", str); sprintf(output, "%s", str);
free(buffer);
return; return;
} }
@ -194,6 +195,7 @@ void find_replace_extension(char *str, char *orig, char *rep, char *output)
int chars_from_end = strlen(buffer) - offset; int chars_from_end = strlen(buffer) - offset;
if (!p || chars_from_end != strlen(orig)) { // Is 'orig' even in 'str' AND is 'orig' found at the end of 'str'? if (!p || chars_from_end != strlen(orig)) { // Is 'orig' even in 'str' AND is 'orig' found at the end of 'str'?
sprintf(output, "%s", str); sprintf(output, "%s", str);
free(buffer);
return; return;
} }
@ -206,13 +208,13 @@ void find_replace_extension(char *str, char *orig, char *rep, char *output)
void replace_image_to_label(char *input_path, char *output_path) { void replace_image_to_label(char *input_path, char *output_path) {
//find_replace(input_path, "images", "labels", output_path); // COCO //find_replace(input_path, "images", "labels", output_path); // COCO
find_replace(input_path, "images/train2014/", "labels/train2014/", output_path); // COCO find_replace(input_path, "images/train2014/", "labels/train2014/", output_path); // COCO
find_replace(input_path, "images/val2014/", "labels/val2014/", output_path); // COCO find_replace(output_path, "images/val2014/", "labels/val2014/", output_path); // COCO
//find_replace(output_path, "JPEGImages", "labels", output_path); // PascalVOC //find_replace(output_path, "JPEGImages", "labels", output_path); // PascalVOC
find_replace(output_path, "VOC2007/JPEGImages", "VOC2007/labels", output_path); // PascalVOC find_replace(output_path, "VOC2007/JPEGImages", "VOC2007/labels", output_path); // PascalVOC
find_replace(output_path, "VOC2012/JPEGImages", "VOC2012/labels", output_path); // PascalVOC find_replace(output_path, "VOC2012/JPEGImages", "VOC2012/labels", output_path); // PascalVOC
// replace only ext of files // replace only ext of files
find_replace_extension(output_path, ".jpg", ".txt", output_path); find_replace_extension(output_path, ".jpg", ".txt", output_path);
find_replace_extension(output_path, ".JPG", ".txt", output_path); find_replace_extension(output_path, ".JPG", ".txt", output_path); // error
find_replace_extension(output_path, ".jpeg", ".txt", output_path); find_replace_extension(output_path, ".jpeg", ".txt", output_path);
find_replace_extension(output_path, ".JPEG", ".txt", output_path); find_replace_extension(output_path, ".JPEG", ".txt", output_path);
find_replace_extension(output_path, ".png", ".txt", output_path); find_replace_extension(output_path, ".png", ".txt", output_path);
@ -285,7 +287,7 @@ void strip(char *s)
size_t offset = 0; size_t offset = 0;
for(i = 0; i < len; ++i){ for(i = 0; i < len; ++i){
char c = s[i]; char c = s[i];
if(c==' '||c=='\t'||c=='\n'||c =='\r') ++offset; if(c==' '||c=='\t'||c=='\n'||c =='\r'||c==0x0d||c==0x0a) ++offset;
else s[i-offset] = c; else s[i-offset] = c;
} }
s[len-offset] = '\0'; s[len-offset] = '\0';

Loading…
Cancel
Save