Skip to content

Commit f609028

Browse files
committed
Release 0.0.1
2 parents 126d253 + 0c8d773 commit f609028

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+5434
-1
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
os: osx
2+
language: c
3+
4+
script:
5+
- make lib_test_coverage
6+
7+
after_success:
8+
- bash <(curl -s https://codecov.io/bash)

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
1-
# Netapix
1+
![LOGO](https://github.com/touchlane/Netapix/blob/master/assets/logo.svg)
2+
3+
[![Build Status](https://travis-ci.org/pkondrashkov/c_travis_coverage.svg?branch=master)](https://travis-ci.org/pkondrashkov/c_travis_coverage)
4+
[![codecov.io](https://codecov.io/gh/touchlane/Netapix/branch/master/graph/badge.svg)](https://codecov.io/gh/codecov/Netapix/branch/master)
5+
![License](https://img.shields.io/badge/license-MIT-blue.svg)
6+
![Platform](https://img.shields.io/badge/platform-MacOS%2FUbuntu-lightgrey.svg)
7+
8+
9+
# Requirements
10+
11+
12+
# Installation
13+
14+
15+
# Usage
16+
17+
18+
# Documentation

example/config.mk

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
-include makefile
2+
3+
# Source, Executable, Library Defines.
4+
APP_SRC_DIR = example/src
5+
APP_OBJ_DIR = example/obj
6+
APP_SRC := $(subst $(APP_SRC_DIR)/,, $(shell find $(APP_SRC_DIR) -maxdepth 1 -name '*.c'))
7+
APP_OBJ = $(APP_SRC:.c=.o)
8+
EXEC_PATH = example/bin
9+
EXEC = $(EXEC_PATH)/netapix
10+
11+
# Compiler, Include, Linker Defines.
12+
CC = gcc
13+
APP_INCLUDE = -I./include/ -I.$(APP_SRC_DIR)
14+
APP_CFLAGS = $(APP_INCLUDE) -w
15+
LIBPATH = -L./lib -lnetapix
16+
LFLAGS = -o $(EXEC) $(LIBPATH)
17+
18+
example_app: example_mkdir $(EXEC)
19+
20+
# Compile and Assemble C Source Files into Object Files.
21+
$(APP_OBJ_DIR)/%.o: $(APP_SRC_DIR)/%.c
22+
$(CC) $(APP_CFLAGS) -c $< -o $@
23+
24+
# Compile binary and link with external Libraries.
25+
$(EXEC): $(addprefix $(APP_OBJ_DIR)/, $(APP_OBJ))
26+
$(CC) $(APP_CFLAGS) $(LFLAGS) $(addprefix $(APP_OBJ_DIR)/, $(APP_OBJ))
27+
28+
# Create obj directory for .o files.
29+
example_mkdir:
30+
mkdir -p $(APP_OBJ_DIR)
31+
mkdir -p $(EXEC_PATH)
32+
33+
# Clean Up Exectuable, Objects, Library, Coverage files d
34+
example_clean:
35+
rm -rf $(EXEC) $(APP_OBJ_DIR)
36+
rm -rf $(APP_SRC:.c=.gcda) $(APP_SRC:.c=.gcno)
37+
38+
.PHONY: example_all example_clean

example/src/constants.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// constants.h
3+
// netapix
4+
//
5+
// Created by Pavel Kondrashkov on 6/27/18.
6+
// Copyright © 2018 Touchlane LLC. All rights reserved.
7+
//
8+
9+
#ifndef constants_h
10+
#define constants_h
11+
12+
#define FATAL_ERROR_NETAPIX_MODE_USAGE_FAIL_MSG "usage: %s [train | run]\n"
13+
#define FATAL_ERROR_NETAPIX_MODE_TRAIN_USAGE_FAIL_MSG "usage: %s %s [path/config.npx] [path/dataset/*.npt] [path/weights.npw (optional)]\n"
14+
#define FATAL_ERROR_NETAPIX_MODE_RUN_USAGE_FAIL_MSG "usage: %s %s [path/input.npi] [path/weights.npw] [path/output/ (optional)]\n"
15+
#define FATAL_ERROR_REMOVING_OUTPUT_DIRECTORY_FAIL_MSG "Could not remove output directory %s.\n"
16+
#define FATAL_ERROR_COPY_FILE_FAIL_MSG "Could not copy file from %s to %s.\n"
17+
18+
#define ERROR_OUTPUT_DIRECTORY_EXISTS_MSG "Output directory %s exists. Override? Y\\N\n"
19+
#define DEFAULT_OUTPUT_WEIGHTS_DIRECTORY_NAME "weights"
20+
#define DEFAULT_OUTPUT_WEIGHTS_PARAMS_DIRECTORY_NAME "weights/params"
21+
#define DEFAULT_OUTPUT_RUN_DIRECTORY_NAME "output"
22+
23+
#endif /* constants_h */

example/src/netapix.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//
2+
// main.c
3+
// Parser
4+
//
5+
// Created by Evgeny Dedovets on 4/20/18.
6+
// Copyright © 2018 Touchlane LLC. All rights reserved.
7+
//
8+
9+
#include "netapix.h"
10+
#include <stdlib.h>
11+
#include <stdio.h>
12+
#include <string.h>
13+
14+
#include "options.h"
15+
#include "constants.h"
16+
17+
int train_mode(int argc, char *argv[]);
18+
int run_mode(int argc, char *argv[]);
19+
20+
int main(int argc, char *argv[]) {
21+
netapix_mode mode = determine_run_mode(argc, argv);
22+
switch (mode) {
23+
case TRAIN_MODE:
24+
train_mode(argc, argv);
25+
break;
26+
case RUN_MODE:
27+
run_mode(argc, argv);
28+
break;
29+
case NDF_MODE:
30+
printf(FATAL_ERROR_NETAPIX_MODE_USAGE_FAIL_MSG, argv[0]);
31+
return 0;
32+
}
33+
return 0;
34+
}
35+
36+
int train_mode(int argc, char *argv[]) {
37+
if (argc < 4){
38+
printf(FATAL_ERROR_NETAPIX_MODE_TRAIN_USAGE_FAIL_MSG, argv[0], argv[1]);
39+
return 1;
40+
}
41+
42+
char *npx_path = argv[2];
43+
char *train_path = argv[3];
44+
char *weights_path = (argc > 4) ? argv[4] : NULL;
45+
46+
char *params_save_path = NULL;
47+
char *output_path = NULL;
48+
if (weights_path) {
49+
output_path = make_output_save_path(weights_path, DEFAULT_OUTPUT_WEIGHTS_DIRECTORY_NAME);
50+
params_save_path = make_output_save_path(weights_path, DEFAULT_OUTPUT_WEIGHTS_PARAMS_DIRECTORY_NAME);
51+
} else {
52+
output_path = make_output_save_path(npx_path, DEFAULT_OUTPUT_WEIGHTS_DIRECTORY_NAME);
53+
params_save_path = output_path;
54+
}
55+
56+
if (prepare_output_path(output_path, 0) || prepare_output_path(params_save_path, 1)) {
57+
return 0;
58+
}
59+
if (copy_param_files(npx_path, weights_path, params_save_path)) {
60+
return 0;
61+
}
62+
63+
train(npx_path, train_path, weights_path, output_path);
64+
return 0;
65+
}
66+
67+
int run_mode(int argc, char *argv[]) {
68+
if (argc < 4){
69+
printf(FATAL_ERROR_NETAPIX_MODE_RUN_USAGE_FAIL_MSG, argv[0], argv[1]);
70+
return 1;
71+
}
72+
char *input_path = argv[2];
73+
char *weights_path = argv[3];
74+
char *output_path = (argc > 4) ? argv[4] : NULL;
75+
76+
output_path = make_output_save_path(output_path ? output_path : "./", DEFAULT_OUTPUT_RUN_DIRECTORY_NAME);
77+
if (prepare_output_path(output_path, 1)) {
78+
return 0;
79+
}
80+
81+
char *input_name = remove_ext(last_path_component(input_path));
82+
output_path = realloc(output_path, (strlen(output_path) +
83+
strlen(input_name) +
84+
strlen(".npo") + 1) * sizeof(*output_path));
85+
sprintf(output_path, "%s%s.npo", output_path, input_name);
86+
87+
run(input_path, weights_path, output_path);
88+
return 0;
89+
}

example/src/options.c

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
//
2+
// options.c
3+
// netapix
4+
//
5+
// Created by Pavel Kondrashkov on 6/27/18.
6+
// Copyright © 2018 Touchlane LLC. All rights reserved.
7+
//
8+
9+
#include "options.h"
10+
#include <stdlib.h>
11+
#include <stdio.h>
12+
#include <string.h>
13+
#include <sys/stat.h>
14+
#include <ftw.h>
15+
#include <dirent.h>
16+
17+
#include "constants.h"
18+
19+
netapix_mode determine_run_mode(int argc, char *argv[]) {
20+
if (argc < 2) {
21+
return NDF_MODE;
22+
}
23+
if (0 == strcmp(argv[1], "train")) {
24+
return TRAIN_MODE;
25+
} else if (0 == strcmp(argv[1], "run")) {
26+
return RUN_MODE;
27+
}
28+
return NDF_MODE;
29+
}
30+
31+
char *remove_ext(char *path) {
32+
size_t len = strlen(path);
33+
size_t i;
34+
size_t dot_index = -1;
35+
char *copy_str = malloc((len + 1) * sizeof(*copy_str));
36+
strcpy(copy_str, path);
37+
for (i = 0; i < len; ++i) {
38+
if (path[i] == '.') {
39+
dot_index = i;
40+
}
41+
}
42+
if (dot_index != -1) {
43+
copy_str[dot_index] = '\0';
44+
}
45+
return copy_str;
46+
}
47+
48+
char *last_path_component(char *path) {
49+
char *component = strrchr(path, '/');
50+
return component ? component + 1 : path;
51+
}
52+
53+
char *remove_last_path_component(char *path) {
54+
size_t len = strlen(path);
55+
char *copy_str = malloc((len + 1) * sizeof(*copy_str));
56+
strcpy(copy_str, path);
57+
char *component = strrchr(copy_str, '/');
58+
*component = '\0';
59+
return copy_str;
60+
}
61+
62+
int unlink_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
63+
int rv = remove(path);
64+
if (rv) {
65+
printf(FATAL_ERROR_REMOVING_OUTPUT_DIRECTORY_FAIL_MSG, path);
66+
}
67+
return rv;
68+
}
69+
70+
int prepare_output_path(char *output_path, int force_create) {
71+
if (!force_create && 0 == directory_exists(output_path)) {
72+
char answer;
73+
printf(ERROR_OUTPUT_DIRECTORY_EXISTS_MSG, output_path);
74+
do { answer = fgetc(stdin); } while (answer != 'Y' && answer != 'N');
75+
if (answer == 'N') {
76+
return 1;
77+
}
78+
nftw(output_path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
79+
}
80+
mkdir(output_path, S_IRWXU);
81+
return 0;
82+
}
83+
84+
char *make_output_save_path(char *base_path, char *directory_name) {
85+
char *output_base_path = remove_last_path_component(base_path);
86+
char *output_path = malloc( (strlen(output_base_path) +
87+
strlen(directory_name) +
88+
strlen("//") + 1) * sizeof(*output_path) ); /// + 1 char for the null char.
89+
sprintf(output_path, "%s/%s/", output_base_path, directory_name);
90+
return output_path;
91+
}
92+
93+
int copy_param_files(char *config_file_path, char *weights_file_path, char *output_path) {
94+
if (config_file_path) {
95+
char *config_file_name = "config.npx";
96+
char *config_copy_file_path = malloc((strlen(output_path) +
97+
strlen(config_file_name) + 1) * sizeof(char));
98+
sprintf(config_copy_file_path, "%s%s", output_path, config_file_name);
99+
if (copy_file(config_file_path, config_copy_file_path)) {
100+
return 1;
101+
}
102+
}
103+
if (weights_file_path) {
104+
char *weights_file_name = last_path_component(weights_file_path);
105+
char *weights_copy_file_path = malloc((strlen(output_path) +
106+
strlen(weights_file_name) + 1) * sizeof(char));
107+
sprintf(weights_copy_file_path, "%s%s", output_path, weights_file_name);
108+
if (copy_file(weights_file_path, weights_copy_file_path)) {
109+
return 1;
110+
}
111+
}
112+
return 0;
113+
}
114+
115+
int copy_file(char *from_path, char *to_path) {
116+
FILE *from_file, *to_file;
117+
from_file = fopen(from_path, "rb");
118+
if (!from_file) {
119+
return 1;
120+
}
121+
to_file = fopen(to_path, "wb");
122+
if (!to_file) {
123+
fclose(from_file);
124+
return 1;
125+
}
126+
size_t read_data, write_data;
127+
unsigned char buff[8192];
128+
do {
129+
read_data = fread(buff, 1, sizeof(buff), from_file);
130+
if (read_data) {
131+
write_data = fwrite(buff, 1, read_data, to_file);
132+
} else {
133+
write_data = 0;
134+
}
135+
} while ((read_data > 0) && (read_data == write_data));
136+
if (write_data) {
137+
fclose(from_file);
138+
fclose(to_file);
139+
return 1;
140+
}
141+
fclose(from_file);
142+
fclose(to_file);
143+
return 0;
144+
}
145+
146+
int directory_exists(char *path) {
147+
DIR *dir = opendir(path);
148+
if (dir) {
149+
/// Directory exists.
150+
closedir(dir);
151+
return 0;
152+
}
153+
return 1;
154+
}

example/src/options.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// options.h
3+
// netapix
4+
//
5+
// Created by Pavel Kondrashkov on 6/27/18.
6+
// Copyright © 2018 Touchlane LLC. All rights reserved.
7+
//
8+
9+
#ifndef options_h
10+
#define options_h
11+
12+
#include <stdio.h>
13+
14+
typedef enum {
15+
TRAIN_MODE,
16+
RUN_MODE,
17+
NDF_MODE
18+
} netapix_mode;
19+
20+
netapix_mode determine_run_mode(int argc, char *argv[]);
21+
char *make_output_save_path(char *base_path, char *output_name);
22+
char *remove_ext(char *path);
23+
char *last_path_component(char *path);
24+
int directory_exists(char *path);
25+
int prepare_output_path(char *output_path, int force_create);
26+
int copy_file(char *from_path, char *to_path);
27+
int copy_param_files(char *config_file_path, char *weights_file_path, char *output_path);
28+
29+
#endif /* options_h */

0 commit comments

Comments
 (0)