Skip to content

Commit

Permalink
remove uses of tmpnam
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdonahue committed Sep 7, 2014
1 parent 3cf3df8 commit fb0a3d0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 59 deletions.
27 changes: 27 additions & 0 deletions include/caffe/util/io.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef CAFFE_UTIL_IO_H_
#define CAFFE_UTIL_IO_H_

#include <unistd.h>

This comment has been minimized.

Copy link
@ziab

ziab Jul 5, 2015

Please consider platform independent implementation of below helpers. I'm having hard time compiling the project for Windows.

#include <string>

#include "google/protobuf/message.h"
Expand All @@ -21,6 +22,32 @@ namespace caffe {

using ::google::protobuf::Message;

inline void MakeTempFilename(string* temp_filename) {
temp_filename->clear();
*temp_filename = "/tmp/caffe_test.XXXXXX";
char* temp_filename_cstr = new char[temp_filename->size()];
// NOLINT_NEXT_LINE(runtime/printf)
strcpy(temp_filename_cstr, temp_filename->c_str());
int fd = mkstemp(temp_filename_cstr);
CHECK_GE(fd, 0) << "Failed to open a temporary file at: " << *temp_filename;
close(fd);
*temp_filename = temp_filename_cstr;
delete temp_filename_cstr;
}

inline void MakeTempDir(string* temp_dirname) {
temp_dirname->clear();
*temp_dirname = "/tmp/caffe_test.XXXXXX";
char* temp_dirname_cstr = new char[temp_dirname->size()];
// NOLINT_NEXT_LINE(runtime/printf)
strcpy(temp_dirname_cstr, temp_dirname->c_str());
char* mkdtemp_result = mkdtemp(temp_dirname_cstr);
CHECK(mkdtemp_result != NULL)
<< "Failed to create a temporary directory at: " << *temp_dirname;
*temp_dirname = temp_dirname_cstr;
delete temp_dirname_cstr;
}

bool ReadProtoFromTextFile(const char* filename, Message* proto);

inline bool ReadProtoFromTextFile(const string& filename, Message* proto) {
Expand Down
5 changes: 4 additions & 1 deletion src/caffe/test/test_data_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "caffe/common.hpp"
#include "caffe/filler.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/io.hpp"
#include "caffe/vision_layers.hpp"

#include "caffe/test/test_caffe_main.hpp"
Expand All @@ -21,11 +22,13 @@ class DataLayerTest : public MultiDeviceTest<TypeParam> {
protected:
DataLayerTest()
: backend_(DataParameter_DB_LEVELDB),
filename_(new string(tmpnam(NULL))),
blob_top_data_(new Blob<Dtype>()),
blob_top_label_(new Blob<Dtype>()),
seed_(1701) {}
virtual void SetUp() {
filename_.reset(new string());
MakeTempDir(filename_.get());
*filename_ += "/db";
blob_top_vec_.push_back(blob_top_data_);
blob_top_vec_.push_back(blob_top_label_);
}
Expand Down
4 changes: 2 additions & 2 deletions src/caffe/test/test_hdf5_output_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class HDF5OutputLayerTest : public MultiDeviceTest<TypeParam> {

protected:
HDF5OutputLayerTest()
: output_file_name_(tmpnam(NULL)),
input_file_name_(
: input_file_name_(
CMAKE_SOURCE_DIR "caffe/test/test_data/sample_data.h5"),
blob_data_(new Blob<Dtype>()),
blob_label_(new Blob<Dtype>()),
num_(5),
channels_(8),
height_(5),
width_(5) {
MakeTempFilename(&output_file_name_);
}

virtual ~HDF5OutputLayerTest() {
Expand Down
15 changes: 8 additions & 7 deletions src/caffe/test/test_image_data_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "caffe/common.hpp"
#include "caffe/filler.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/io.hpp"
#include "caffe/vision_layers.hpp"

#include "caffe/test/test_caffe_main.hpp"
Expand All @@ -21,16 +22,16 @@ class ImageDataLayerTest : public MultiDeviceTest<TypeParam> {
protected:
ImageDataLayerTest()
: seed_(1701),
filename_(new string(tmpnam(NULL))),
blob_top_data_(new Blob<Dtype>()),
blob_top_label_(new Blob<Dtype>()) {}
virtual void SetUp() {
MakeTempFilename(&filename_);
blob_top_vec_.push_back(blob_top_data_);
blob_top_vec_.push_back(blob_top_label_);
Caffe::set_random_seed(seed_);
// Create a Vector of files with labels
std::ofstream outfile(filename_->c_str(), std::ofstream::out);
LOG(INFO) << "Using temporary file " << *filename_;
std::ofstream outfile(filename_.c_str(), std::ofstream::out);
LOG(INFO) << "Using temporary file " << filename_;
for (int i = 0; i < 5; ++i) {
outfile << EXAMPLES_SOURCE_DIR "images/cat.jpg " << i;
}
Expand All @@ -43,7 +44,7 @@ class ImageDataLayerTest : public MultiDeviceTest<TypeParam> {
}

int seed_;
shared_ptr<string> filename_;
string filename_;
Blob<Dtype>* const blob_top_data_;
Blob<Dtype>* const blob_top_label_;
vector<Blob<Dtype>*> blob_bottom_vec_;
Expand All @@ -57,7 +58,7 @@ TYPED_TEST(ImageDataLayerTest, TestRead) {
LayerParameter param;
ImageDataParameter* image_data_param = param.mutable_image_data_param();
image_data_param->set_batch_size(5);
image_data_param->set_source(this->filename_->c_str());
image_data_param->set_source(this->filename_.c_str());
image_data_param->set_shuffle(false);
ImageDataLayer<Dtype> layer(param);
layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_);
Expand All @@ -83,7 +84,7 @@ TYPED_TEST(ImageDataLayerTest, TestResize) {
LayerParameter param;
ImageDataParameter* image_data_param = param.mutable_image_data_param();
image_data_param->set_batch_size(5);
image_data_param->set_source(this->filename_->c_str());
image_data_param->set_source(this->filename_.c_str());
image_data_param->set_new_height(256);
image_data_param->set_new_width(256);
image_data_param->set_shuffle(false);
Expand Down Expand Up @@ -111,7 +112,7 @@ TYPED_TEST(ImageDataLayerTest, TestShuffle) {
LayerParameter param;
ImageDataParameter* image_data_param = param.mutable_image_data_param();
image_data_param->set_batch_size(5);
image_data_param->set_source(this->filename_->c_str());
image_data_param->set_source(this->filename_.c_str());
image_data_param->set_shuffle(true);
ImageDataLayer<Dtype> layer(param);
layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_);
Expand Down
49 changes: 0 additions & 49 deletions src/caffe/test/test_window_data_layer.cpp

This file was deleted.

0 comments on commit fb0a3d0

Please sign in to comment.