-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
52 changed files
with
5,434 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
os: osx | ||
language: c | ||
|
||
script: | ||
- make lib_test_coverage | ||
|
||
after_success: | ||
- bash <(curl -s https://codecov.io/bash) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,18 @@ | ||
# Netapix | ||
 | ||
|
||
[](https://travis-ci.org/pkondrashkov/c_travis_coverage) | ||
[](https://codecov.io/gh/codecov/Netapix/branch/master) | ||
 | ||
 | ||
|
||
|
||
# Requirements | ||
|
||
|
||
# Installation | ||
|
||
|
||
# Usage | ||
|
||
|
||
# Documentation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
-include makefile | ||
|
||
# Source, Executable, Library Defines. | ||
APP_SRC_DIR = example/src | ||
APP_OBJ_DIR = example/obj | ||
APP_SRC := $(subst $(APP_SRC_DIR)/,, $(shell find $(APP_SRC_DIR) -maxdepth 1 -name '*.c')) | ||
APP_OBJ = $(APP_SRC:.c=.o) | ||
EXEC_PATH = example/bin | ||
EXEC = $(EXEC_PATH)/netapix | ||
|
||
# Compiler, Include, Linker Defines. | ||
CC = gcc | ||
APP_INCLUDE = -I./include/ -I.$(APP_SRC_DIR) | ||
APP_CFLAGS = $(APP_INCLUDE) -w | ||
LIBPATH = -L./lib -lnetapix | ||
LFLAGS = -o $(EXEC) $(LIBPATH) | ||
|
||
example_app: example_mkdir $(EXEC) | ||
|
||
# Compile and Assemble C Source Files into Object Files. | ||
$(APP_OBJ_DIR)/%.o: $(APP_SRC_DIR)/%.c | ||
$(CC) $(APP_CFLAGS) -c $< -o $@ | ||
|
||
# Compile binary and link with external Libraries. | ||
$(EXEC): $(addprefix $(APP_OBJ_DIR)/, $(APP_OBJ)) | ||
$(CC) $(APP_CFLAGS) $(LFLAGS) $(addprefix $(APP_OBJ_DIR)/, $(APP_OBJ)) | ||
|
||
# Create obj directory for .o files. | ||
example_mkdir: | ||
mkdir -p $(APP_OBJ_DIR) | ||
mkdir -p $(EXEC_PATH) | ||
|
||
# Clean Up Exectuable, Objects, Library, Coverage files d | ||
example_clean: | ||
rm -rf $(EXEC) $(APP_OBJ_DIR) | ||
rm -rf $(APP_SRC:.c=.gcda) $(APP_SRC:.c=.gcno) | ||
|
||
.PHONY: example_all example_clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// constants.h | ||
// netapix | ||
// | ||
// Created by Pavel Kondrashkov on 6/27/18. | ||
// Copyright © 2018 Touchlane LLC. All rights reserved. | ||
// | ||
|
||
#ifndef constants_h | ||
#define constants_h | ||
|
||
#define FATAL_ERROR_NETAPIX_MODE_USAGE_FAIL_MSG "usage: %s [train | run]\n" | ||
#define FATAL_ERROR_NETAPIX_MODE_TRAIN_USAGE_FAIL_MSG "usage: %s %s [path/config.npx] [path/dataset/*.npt] [path/weights.npw (optional)]\n" | ||
#define FATAL_ERROR_NETAPIX_MODE_RUN_USAGE_FAIL_MSG "usage: %s %s [path/input.npi] [path/weights.npw] [path/output/ (optional)]\n" | ||
#define FATAL_ERROR_REMOVING_OUTPUT_DIRECTORY_FAIL_MSG "Could not remove output directory %s.\n" | ||
#define FATAL_ERROR_COPY_FILE_FAIL_MSG "Could not copy file from %s to %s.\n" | ||
#define ERROR_OUTPUT_DIRECTORY_EXISTS_MSG "Output directory %s exists. Override? Y\\N\n" | ||
#define DEFAULT_OUTPUT_WEIGHTS_DIRECTORY_NAME "weights" | ||
#define DEFAULT_OUTPUT_WEIGHTS_PARAMS_DIRECTORY_NAME "weights/params" | ||
#define DEFAULT_OUTPUT_RUN_DIRECTORY_NAME "output" | ||
#endif /* constants_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// | ||
// main.c | ||
// Parser | ||
// | ||
// Created by Evgeny Dedovets on 4/20/18. | ||
// Copyright © 2018 Touchlane LLC. All rights reserved. | ||
// | ||
|
||
#include "netapix.h" | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include "options.h" | ||
#include "constants.h" | ||
|
||
int train_mode(int argc, char *argv[]); | ||
int run_mode(int argc, char *argv[]); | ||
|
||
int main(int argc, char *argv[]) { | ||
netapix_mode mode = determine_run_mode(argc, argv); | ||
switch (mode) { | ||
case TRAIN_MODE: | ||
train_mode(argc, argv); | ||
break; | ||
case RUN_MODE: | ||
run_mode(argc, argv); | ||
break; | ||
case NDF_MODE: | ||
printf(FATAL_ERROR_NETAPIX_MODE_USAGE_FAIL_MSG, argv[0]); | ||
return 0; | ||
} | ||
return 0; | ||
} | ||
|
||
int train_mode(int argc, char *argv[]) { | ||
if (argc < 4){ | ||
printf(FATAL_ERROR_NETAPIX_MODE_TRAIN_USAGE_FAIL_MSG, argv[0], argv[1]); | ||
return 1; | ||
} | ||
|
||
char *npx_path = argv[2]; | ||
char *train_path = argv[3]; | ||
char *weights_path = (argc > 4) ? argv[4] : NULL; | ||
|
||
char *params_save_path = NULL; | ||
char *output_path = NULL; | ||
if (weights_path) { | ||
output_path = make_output_save_path(weights_path, DEFAULT_OUTPUT_WEIGHTS_DIRECTORY_NAME); | ||
params_save_path = make_output_save_path(weights_path, DEFAULT_OUTPUT_WEIGHTS_PARAMS_DIRECTORY_NAME); | ||
} else { | ||
output_path = make_output_save_path(npx_path, DEFAULT_OUTPUT_WEIGHTS_DIRECTORY_NAME); | ||
params_save_path = output_path; | ||
} | ||
|
||
if (prepare_output_path(output_path, 0) || prepare_output_path(params_save_path, 1)) { | ||
return 0; | ||
} | ||
if (copy_param_files(npx_path, weights_path, params_save_path)) { | ||
return 0; | ||
} | ||
|
||
train(npx_path, train_path, weights_path, output_path); | ||
return 0; | ||
} | ||
|
||
int run_mode(int argc, char *argv[]) { | ||
if (argc < 4){ | ||
printf(FATAL_ERROR_NETAPIX_MODE_RUN_USAGE_FAIL_MSG, argv[0], argv[1]); | ||
return 1; | ||
} | ||
char *input_path = argv[2]; | ||
char *weights_path = argv[3]; | ||
char *output_path = (argc > 4) ? argv[4] : NULL; | ||
|
||
output_path = make_output_save_path(output_path ? output_path : "./", DEFAULT_OUTPUT_RUN_DIRECTORY_NAME); | ||
if (prepare_output_path(output_path, 1)) { | ||
return 0; | ||
} | ||
|
||
char *input_name = remove_ext(last_path_component(input_path)); | ||
output_path = realloc(output_path, (strlen(output_path) + | ||
strlen(input_name) + | ||
strlen(".npo") + 1) * sizeof(*output_path)); | ||
sprintf(output_path, "%s%s.npo", output_path, input_name); | ||
|
||
run(input_path, weights_path, output_path); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// | ||
// options.c | ||
// netapix | ||
// | ||
// Created by Pavel Kondrashkov on 6/27/18. | ||
// Copyright © 2018 Touchlane LLC. All rights reserved. | ||
// | ||
|
||
#include "options.h" | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <sys/stat.h> | ||
#include <ftw.h> | ||
#include <dirent.h> | ||
|
||
#include "constants.h" | ||
|
||
netapix_mode determine_run_mode(int argc, char *argv[]) { | ||
if (argc < 2) { | ||
return NDF_MODE; | ||
} | ||
if (0 == strcmp(argv[1], "train")) { | ||
return TRAIN_MODE; | ||
} else if (0 == strcmp(argv[1], "run")) { | ||
return RUN_MODE; | ||
} | ||
return NDF_MODE; | ||
} | ||
|
||
char *remove_ext(char *path) { | ||
size_t len = strlen(path); | ||
size_t i; | ||
size_t dot_index = -1; | ||
char *copy_str = malloc((len + 1) * sizeof(*copy_str)); | ||
strcpy(copy_str, path); | ||
for (i = 0; i < len; ++i) { | ||
if (path[i] == '.') { | ||
dot_index = i; | ||
} | ||
} | ||
if (dot_index != -1) { | ||
copy_str[dot_index] = '\0'; | ||
} | ||
return copy_str; | ||
} | ||
|
||
char *last_path_component(char *path) { | ||
char *component = strrchr(path, '/'); | ||
return component ? component + 1 : path; | ||
} | ||
|
||
char *remove_last_path_component(char *path) { | ||
size_t len = strlen(path); | ||
char *copy_str = malloc((len + 1) * sizeof(*copy_str)); | ||
strcpy(copy_str, path); | ||
char *component = strrchr(copy_str, '/'); | ||
*component = '\0'; | ||
return copy_str; | ||
} | ||
|
||
int unlink_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { | ||
int rv = remove(path); | ||
if (rv) { | ||
printf(FATAL_ERROR_REMOVING_OUTPUT_DIRECTORY_FAIL_MSG, path); | ||
} | ||
return rv; | ||
} | ||
|
||
int prepare_output_path(char *output_path, int force_create) { | ||
if (!force_create && 0 == directory_exists(output_path)) { | ||
char answer; | ||
printf(ERROR_OUTPUT_DIRECTORY_EXISTS_MSG, output_path); | ||
do { answer = fgetc(stdin); } while (answer != 'Y' && answer != 'N'); | ||
if (answer == 'N') { | ||
return 1; | ||
} | ||
nftw(output_path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS); | ||
} | ||
mkdir(output_path, S_IRWXU); | ||
return 0; | ||
} | ||
|
||
char *make_output_save_path(char *base_path, char *directory_name) { | ||
char *output_base_path = remove_last_path_component(base_path); | ||
char *output_path = malloc( (strlen(output_base_path) + | ||
strlen(directory_name) + | ||
strlen("//") + 1) * sizeof(*output_path) ); /// + 1 char for the null char. | ||
sprintf(output_path, "%s/%s/", output_base_path, directory_name); | ||
return output_path; | ||
} | ||
|
||
int copy_param_files(char *config_file_path, char *weights_file_path, char *output_path) { | ||
if (config_file_path) { | ||
char *config_file_name = "config.npx"; | ||
char *config_copy_file_path = malloc((strlen(output_path) + | ||
strlen(config_file_name) + 1) * sizeof(char)); | ||
sprintf(config_copy_file_path, "%s%s", output_path, config_file_name); | ||
if (copy_file(config_file_path, config_copy_file_path)) { | ||
return 1; | ||
} | ||
} | ||
if (weights_file_path) { | ||
char *weights_file_name = last_path_component(weights_file_path); | ||
char *weights_copy_file_path = malloc((strlen(output_path) + | ||
strlen(weights_file_name) + 1) * sizeof(char)); | ||
sprintf(weights_copy_file_path, "%s%s", output_path, weights_file_name); | ||
if (copy_file(weights_file_path, weights_copy_file_path)) { | ||
return 1; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
int copy_file(char *from_path, char *to_path) { | ||
FILE *from_file, *to_file; | ||
from_file = fopen(from_path, "rb"); | ||
if (!from_file) { | ||
return 1; | ||
} | ||
to_file = fopen(to_path, "wb"); | ||
if (!to_file) { | ||
fclose(from_file); | ||
return 1; | ||
} | ||
size_t read_data, write_data; | ||
unsigned char buff[8192]; | ||
do { | ||
read_data = fread(buff, 1, sizeof(buff), from_file); | ||
if (read_data) { | ||
write_data = fwrite(buff, 1, read_data, to_file); | ||
} else { | ||
write_data = 0; | ||
} | ||
} while ((read_data > 0) && (read_data == write_data)); | ||
if (write_data) { | ||
fclose(from_file); | ||
fclose(to_file); | ||
return 1; | ||
} | ||
fclose(from_file); | ||
fclose(to_file); | ||
return 0; | ||
} | ||
|
||
int directory_exists(char *path) { | ||
DIR *dir = opendir(path); | ||
if (dir) { | ||
/// Directory exists. | ||
closedir(dir); | ||
return 0; | ||
} | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// options.h | ||
// netapix | ||
// | ||
// Created by Pavel Kondrashkov on 6/27/18. | ||
// Copyright © 2018 Touchlane LLC. All rights reserved. | ||
// | ||
|
||
#ifndef options_h | ||
#define options_h | ||
|
||
#include <stdio.h> | ||
|
||
typedef enum { | ||
TRAIN_MODE, | ||
RUN_MODE, | ||
NDF_MODE | ||
} netapix_mode; | ||
|
||
netapix_mode determine_run_mode(int argc, char *argv[]); | ||
char *make_output_save_path(char *base_path, char *output_name); | ||
char *remove_ext(char *path); | ||
char *last_path_component(char *path); | ||
int directory_exists(char *path); | ||
int prepare_output_path(char *output_path, int force_create); | ||
int copy_file(char *from_path, char *to_path); | ||
int copy_param_files(char *config_file_path, char *weights_file_path, char *output_path); | ||
|
||
#endif /* options_h */ |
Oops, something went wrong.