Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ project(torchvision)
set(CMAKE_CXX_STANDARD 11)

find_package(Torch REQUIRED)
find_package(OpenCV REQUIRED COMPONENTS core imgproc imgcodecs)

file(GLOB HEADERS torchvision/csrc/vision.h torchvision/csrc/general.h)

file(GLOB HEADERS torchvision/csrc/vision.h)
file(GLOB MODELS_HEADERS torchvision/csrc/models/*.h)
file(GLOB MODELS_SOURCES torchvision/csrc/models/*.h torchvision/csrc/models/*.cpp)

add_library (${PROJECT_NAME} SHARED ${MODELS_SOURCES})
file(GLOB DATASETS_HEADERS torchvision/csrc/datasets/*.h)
file(GLOB DATASETS_SOURCES torchvision/csrc/datasets/*.h torchvision/csrc/datasets/*.cpp)

add_library (${PROJECT_NAME} SHARED ${MODELS_SOURCES} ${DATASETS_SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE "${OpenCV_LIBS}")
target_link_libraries(${PROJECT_NAME} PUBLIC "${TORCH_LIBRARIES}")

install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME})
install(FILES ${MODELS_HEADERS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}/models)
#install(FILES ${DATASETS_HEADERS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}/datasets)

90 changes: 90 additions & 0 deletions torchvision/csrc/datasets/datasetsimpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "datasetsimpl.h"

#include <dirent.h>
#include <opencv2/opencv.hpp>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

namespace vision {
namespace datasets {
namespace datasetsimpl {
std::vector<std::string> lsdir(const std::string& path) {
std::vector<std::string> list;
auto dp = opendir(path.c_str());

if (dp != nullptr) {
auto ep = readdir(dp);

while (ep != nullptr) {
std::string name = ep->d_name;
if (name != "." && name != "..")
list.emplace_back(std::move(name));
}

closedir(dp);
}

return list;
}

std::string tolower(std::string str) {
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
return str;
}

inline bool comp(const std::string& A, const std::string& B) {
return tolower(A) < tolower(B);
};

void sort_names(std::vector<std::string>& data) {
std::sort(data.begin(), data.end(), comp);
}

bool isdir(const std::string& path) {
struct stat st;
if (stat(path.c_str(), &st) == 0)
return st.st_mode & S_IFDIR;
return false;
}

bool isfile(const std::string& path) {
struct stat st;
if (stat(path.c_str(), &st) == 0)
return st.st_mode & S_IFREG;
return false;
}

bool exists(const std::string& path) {
struct stat st;
return stat(path.c_str(), &st) == 0;
}

torch::Tensor read_image(const std::string& path) {
auto mat = cv::imread(path);
TORCH_CHECK(!mat.empty(), "Failed to read image \"", path, "\".");

cv::cvtColor(mat, mat, cv::COLOR_BGR2RGB);
std::vector<torch::Tensor> tensors;
std::vector<cv::Mat> channels(size_t(mat.channels()));
cv::split(mat, channels);

for (auto& channel : channels)
tensors.push_back(
torch::from_blob(channel.ptr(), {mat.rows, mat.cols}, torch::kUInt8));

auto output = torch::cat(tensors)
.view({mat.channels(), mat.rows, mat.cols})
.to(torch::kFloat);
return output / 255;
}

std::string absolute_path(const std::string& path) {
char rpath[PATH_MAX];
realpath(path.c_str(), rpath);
return std::string(rpath);
}

} // namespace datasetsimpl
} // namespace datasets
} // namespace vision
42 changes: 42 additions & 0 deletions torchvision/csrc/datasets/datasetsimpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef DATASETSIMPL_H
#define DATASETSIMPL_H

#include <torch/torch.h>

#ifndef TORCH_CHECK
#define TORCH_CHECK AT_CHECK
#endif

namespace vision {
namespace datasets {
namespace datasetsimpl {

std::vector<std::string> lsdir(const std::string& path);

std::string tolower(std::string str);

void sort_names(std::vector<std::string>& data);

bool isdir(const std::string& path);

bool isfile(const std::string& path);

bool exists(const std::string& path);

std::string absolute_path(const std::string& path);

inline std::string join(const std::string& str) {
return str;
}
template <typename... Tail>
inline std::string join(const std::string& head, Tail&&... tail) {
return head + "/" + join(std::forward<Tail>(tail)...);
}

torch::Tensor read_image(const std::string& path);

} // namespace datasetsimpl
} // namespace datasets
} // namespace vision

#endif // DATASETSIMPL_H
File renamed without changes.
2 changes: 1 addition & 1 deletion torchvision/csrc/models/alexnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define ALEXNET_H

#include <torch/torch.h>
#include "general.h"
#include "../general.h"

namespace vision {
namespace models {
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/models/densenet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define DENSENET_H

#include <torch/torch.h>
#include "general.h"
#include "../general.h"

namespace vision {
namespace models {
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/models/googlenet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define GOOGLENET_H

#include <torch/torch.h>
#include "general.h"
#include "../general.h"

namespace vision {
namespace models {
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/models/inception.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define INCEPTION_H

#include <torch/torch.h>
#include "general.h"
#include "../general.h"

namespace vision {
namespace models {
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/models/mnasnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define MNASNET_H

#include <torch/torch.h>
#include "general.h"
#include "../general.h"

namespace vision {
namespace models {
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/models/mobilenet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define MOBILENET_H

#include <torch/torch.h>
#include "general.h"
#include "../general.h"

namespace vision {
namespace models {
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/models/resnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define RESNET_H

#include <torch/torch.h>
#include "general.h"
#include "../general.h"

namespace vision {
namespace models {
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/models/shufflenetv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define SHUFFLENETV2_H

#include <torch/torch.h>
#include "general.h"
#include "../general.h"

namespace vision {
namespace models {
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/models/squeezenet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define SQUEEZENET_H

#include <torch/torch.h>
#include "general.h"
#include "../general.h"

namespace vision {
namespace models {
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/models/vgg.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define VGG_H

#include <torch/torch.h>
#include "general.h"
#include "../general.h"

namespace vision {
namespace models {
Expand Down