├── .appveyor.yml ├── .gitignore ├── .travis.yml ├── LICENSE ├── MANIFEST.in ├── README.rst ├── bin ├── cythonize.py └── train.py ├── lightnet ├── __init__.pxd ├── __init__.py ├── __main__.py ├── _darknet │ ├── Makefile │ ├── activation_kernels.cu │ ├── activation_layer.c │ ├── activation_layer.h │ ├── activations.c │ ├── activations.h │ ├── avgpool_layer.c │ ├── avgpool_layer.h │ ├── avgpool_layer_kernels.cu │ ├── batchnorm_layer.c │ ├── batchnorm_layer.h │ ├── blas.c │ ├── blas.h │ ├── blas_kernels.cu │ ├── box.c │ ├── box.h │ ├── classifier.h │ ├── col2im.c │ ├── col2im.h │ ├── col2im_kernels.cu │ ├── connected_layer.c │ ├── connected_layer.h │ ├── convolutional_kernels.cu │ ├── convolutional_layer.c │ ├── convolutional_layer.h │ ├── cost_layer.c │ ├── cost_layer.h │ ├── crnn_layer.c │ ├── crnn_layer.h │ ├── crop_layer.c │ ├── crop_layer.h │ ├── crop_layer_kernels.cu │ ├── cuda.c │ ├── cuda.h │ ├── darknet.h │ ├── data.c │ ├── data.h │ ├── deconvolutional_kernels.cu │ ├── deconvolutional_layer.c │ ├── deconvolutional_layer.h │ ├── demo.c │ ├── demo.h │ ├── detection_layer.c │ ├── detection_layer.h │ ├── dropout_layer.c │ ├── dropout_layer.h │ ├── dropout_layer_kernels.cu │ ├── gemm.c │ ├── gemm.h │ ├── gru_layer.c │ ├── gru_layer.h │ ├── im2col.c │ ├── im2col.h │ ├── im2col_kernels.cu │ ├── image.c │ ├── image.h │ ├── layer.c │ ├── layer.h │ ├── list.c │ ├── list.h │ ├── local_layer.c │ ├── local_layer.h │ ├── lstm_layer.c │ ├── lstm_layer.h │ ├── matrix.c │ ├── matrix.h │ ├── maxpool_layer.c │ ├── maxpool_layer.h │ ├── maxpool_layer_kernels.cu │ ├── network.c │ ├── network.h │ ├── normalization_layer.c │ ├── normalization_layer.h │ ├── option_list.c │ ├── option_list.h │ ├── parser.c │ ├── parser.h │ ├── region_layer.c │ ├── region_layer.h │ ├── reorg_layer.c │ ├── reorg_layer.h │ ├── rnn_layer.c │ ├── rnn_layer.h │ ├── route_layer.c │ ├── route_layer.h │ ├── shortcut_layer.c │ ├── shortcut_layer.h │ ├── softmax_layer.c │ ├── softmax_layer.h │ ├── stb_image.h │ ├── stb_image_write.h │ ├── tree.c │ ├── tree.h │ ├── utils.c │ └── utils.h ├── about.py ├── cli.py ├── data │ ├── alexnet.cfg │ ├── cifar.cfg │ ├── cifar.test.cfg │ ├── coco.names │ ├── coco.template │ ├── darknet.cfg │ ├── darknet19.cfg │ ├── darknet19_448.cfg │ ├── darknet9000.cfg │ ├── densenet201.cfg │ ├── extraction.cfg │ ├── extraction.conv.cfg │ ├── extraction22k.cfg │ ├── go.cfg │ ├── go.test.cfg │ ├── gru.cfg │ ├── jnet-conv.cfg │ ├── resnet152.cfg │ ├── resnet50.cfg │ ├── rnn.cfg │ ├── rnn.train.cfg │ ├── strided.cfg │ ├── t1.test.cfg │ ├── tiny-yolo-voc.cfg │ ├── tiny-yolo.cfg │ ├── tiny.cfg │ ├── vgg-16.cfg │ ├── vgg-conv.cfg │ ├── voc.names │ ├── writing.cfg │ ├── yolo-voc.2.0.cfg │ ├── yolo-voc.cfg │ ├── yolo.2.0.cfg │ ├── yolo.cfg │ └── yolo9000.cfg ├── lightnet.pxd ├── lightnet.pyx └── util.py ├── requirements.txt ├── setup.py └── tests ├── COCO_val2014_000000000042.jpg ├── test_boxes.py ├── test_image.py └── test_network.py /.appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | 3 | matrix: 4 | 5 | # For Python versions available on Appveyor, see 6 | # http://www.appveyor.com/docs/installed-software#python 7 | # The list here is complete (excluding Python 2.6, which 8 | # isn't covered by this document) at the time of writing. 9 | 10 | - PYTHON: "C:\\Python27" 11 | - PYTHON: "C:\\Python35" 12 | - PYTHON: "C:\\Python27-x64" 13 | - PYTHON: "C:\\Python35-x64" 14 | - PYTHON: "C:\\Python36-x64" 15 | 16 | install: 17 | # We need wheel installed to build wheels 18 | - "%PYTHON%\\python.exe -m pip install wheel" 19 | - "%PYTHON%\\python.exe -m pip install cython" 20 | - "%PYTHON%\\python.exe -m pip install -r requirements.txt" 21 | - "%PYTHON%\\python.exe -m pip install -e ." 22 | 23 | build: off 24 | 25 | test_script: 26 | # Put your test command here. 27 | # If you don't need to build C extensions on 64-bit Python 3.3 or 3.4, 28 | # you can remove "build.cmd" from the front of the command, as it's 29 | # only needed to support those cases. 30 | # Note that you must use the environment variable %PYTHON% to refer to 31 | # the interpreter you're using - Appveyor does not do anything special 32 | # to put the Python version you want to use on PATH. 33 | - "%PYTHON%\\python.exe -m pytest tests/" 34 | 35 | after_test: 36 | # This step builds your wheels. 37 | # Again, you only need build.cmd if you're building C extensions for 38 | # 64-bit Python 3.3/3.4. And you need to use %PYTHON% to get the correct 39 | # interpreter 40 | - "%PYTHON%\\python.exe setup.py bdist_wheel" 41 | 42 | artifacts: 43 | # bdist_wheel puts your built wheel in the dist directory 44 | - path: dist\* 45 | 46 | #on_success: 47 | # You can use this step to upload your artifacts to a public website. 48 | # See Appveyor's documentation for more details. Or you can simply 49 | # access your wheels from the Appveyor "artifacts" tab for your build. 50 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.weights 2 | 3 | # Cython / C extensions 4 | cythonize.json 5 | spacy/*.html 6 | *.cpp 7 | *.so 8 | 9 | # Vim / VSCode / editors 10 | *.swp 11 | *.sw* 12 | Profile.prof 13 | .vscode 14 | .sass-cache 15 | 16 | # Python 17 | .Python 18 | .python-version 19 | __pycache__/ 20 | *.py[cod] 21 | .env/ 22 | .env* 23 | .~env/ 24 | .venv 25 | venv/ 26 | .dev 27 | .denv 28 | .pypyenv 29 | 30 | # Distribution / packaging 31 | env/ 32 | build/ 33 | develop-eggs/ 34 | dist/ 35 | eggs/ 36 | lib/ 37 | lib64/ 38 | parts/ 39 | sdist/ 40 | var/ 41 | *.egg-info/ 42 | .installed.cfg 43 | *.egg 44 | .eggs 45 | MANIFEST 46 | 47 | # Temporary files 48 | *.~* 49 | tmp/ 50 | 51 | # Installer logs 52 | pip-log.txt 53 | pip-delete-this-directory.txt 54 | 55 | # Unit test / coverage reports 56 | htmlcov/ 57 | .tox/ 58 | .coverage 59 | .cache 60 | nosetests.xml 61 | coverage.xml 62 | 63 | # Translations 64 | *.mo 65 | 66 | # Mr Developer 67 | .mr.developer.cfg 68 | .project 69 | .pydevproject 70 | 71 | # Rope 72 | .ropeproject 73 | 74 | # Django stuff: 75 | *.log 76 | *.pot 77 | 78 | # Windows 79 | *.bat 80 | Thumbs.db 81 | Desktop.ini 82 | 83 | # Mac OS X 84 | *.DS_Store 85 | 86 | # Komodo project files 87 | *.komodoproject 88 | 89 | # Other 90 | *.tgz 91 | 92 | # Pycharm project files 93 | *.idea 94 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | 3 | python: 4 | - "2.7" 5 | - "3.5" 6 | - "3.6" 7 | 8 | install: 9 | - if [ "$TRAVIS_OS_NAME" == "linux" ] ; then sudo apt-get install libopenblas-dev ; fi 10 | - pip install -r requirements.txt 11 | - pip install cython 12 | - python setup.py build_ext --inplace 13 | - pip install -e . 14 | - export PYTHONPATH=`pwd` 15 | - python -m lightnet download tiny-yolo 16 | - pip install pytest 17 | 18 | script: 19 | - python -m pytest tests 20 | 21 | 22 | notifications: 23 | email: false 24 | slack: 25 | secure: VSqtxg7u4NTZRfoZqjxPRPVS92KTy/mp62egfDZ9ujTP4VPxNe15QZuTB6r/ICPgEYqBtdhLc/aetuBcemt0bHfentV0F7bz7iDY/AFQC1h1i4G0D0wKMufuqOJFw9MOp2tSpuvCVzhCxR+Ymx/F9SaeYBAiwBawce4wu+qu3lA= 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (C) 2017 ExplosionAI UG (haftungsbeschränkt), 2014-2017 Joseph Redmon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE 2 | include README.rst 3 | include bin/cythonize.py 4 | include lightnet/_darknet/Makefile 5 | recursive-include lightnet/_darknet *.c 6 | recursive-include lightnet/_darknet *.cu 7 | recursive-include lightnet/_darknet *.h 8 | recursive-include lightnet/data *.cfg 9 | recursive-include lightnet/data *.data 10 | recursive-include lightnet/data *.names 11 | -------------------------------------------------------------------------------- /bin/train.py: -------------------------------------------------------------------------------- 1 | from lightnet.lightnet import train 2 | import plac 3 | from pathlib import Path 4 | 5 | try: 6 | unicode 7 | except NameError: 8 | unicode = str 9 | 10 | def path2bytes(loc): 11 | return unicode(Path(loc).resolve()).encode('utf8') 12 | 13 | def main(cfg_loc, weight_loc, images_loc): 14 | train(path2bytes(cfg_loc), path2bytes(weight_loc), 15 | path2bytes(images_loc), path2bytes('/tmp/yolo')) 16 | 17 | if __name__ == '__main__': 18 | plac.call(main) 19 | -------------------------------------------------------------------------------- /lightnet/__init__.pxd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/explosion/lightnet/e7283d95367ed2288a26f2744ad015f6dc0f17bd/lightnet/__init__.pxd -------------------------------------------------------------------------------- /lightnet/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf8 2 | from __future__ import unicode_literals 3 | 4 | from .lightnet import Network, Image, BoxLabels 5 | from .about import __version__ 6 | 7 | 8 | def load(name, path=None): 9 | return Network.load(name, path=path) 10 | -------------------------------------------------------------------------------- /lightnet/__main__.py: -------------------------------------------------------------------------------- 1 | # coding: utf8 2 | from __future__ import print_function 3 | # NB! This breaks in plac on Python 2!! 4 | # from __future__ import unicode_literals 5 | 6 | 7 | if __name__ == '__main__': 8 | import plac 9 | import sys 10 | try: 11 | from lightnet.cli import download 12 | except ImportError: 13 | from cli import download 14 | 15 | commands = { 16 | 'download': download, 17 | } 18 | if len(sys.argv) == 1: 19 | print(', '.join(commands), title="Available commands", exits=1) 20 | command = sys.argv.pop(1) 21 | sys.argv[0] = 'lightnet %s' % command 22 | if command in commands: 23 | plac.call(commands[command]) 24 | else: 25 | print( 26 | "Available: %s" % ', '.join(commands), 27 | title="Unknown command: %s" % command, 28 | exits=1) 29 | -------------------------------------------------------------------------------- /lightnet/_darknet/Makefile: -------------------------------------------------------------------------------- 1 | GPU=0 2 | CUDNN=0 3 | OPENCV=0 4 | OPENMP=0 5 | DEBUG=0 6 | 7 | ARCH= -gencode arch=compute_30,code=sm_30 \ 8 | -gencode arch=compute_35,code=sm_35 \ 9 | -gencode arch=compute_50,code=[sm_50,compute_50] \ 10 | -gencode arch=compute_52,code=[sm_52,compute_52] 11 | # -gencode arch=compute_20,code=[sm_20,sm_21] \ This one is deprecated? 12 | 13 | # This is what I use, uncomment if you know your arch and want to specify 14 | # ARCH= -gencode arch=compute_52,code=compute_52 15 | 16 | VPATH=./ 17 | SLIB=libdarknet.so 18 | ALIB=libdarknet.a 19 | EXEC=darknet 20 | OBJDIR=./obj/ 21 | 22 | CC=gcc 23 | NVCC=nvcc 24 | AR=ar 25 | ARFLAGS=rcs 26 | OPTS=-Ofast 27 | LDFLAGS= -lm -pthread 28 | COMMON= -I. 29 | CFLAGS=-Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC 30 | 31 | ifeq ($(OPENMP), 1) 32 | CFLAGS+= -fopenmp 33 | endif 34 | 35 | ifeq ($(DEBUG), 1) 36 | OPTS=-O0 -g 37 | endif 38 | 39 | CFLAGS+=$(OPTS) 40 | 41 | ifeq ($(OPENCV), 1) 42 | COMMON+= -DOPENCV 43 | CFLAGS+= -DOPENCV 44 | LDFLAGS+= `pkg-config --libs opencv` 45 | COMMON+= `pkg-config --cflags opencv` 46 | endif 47 | 48 | ifeq ($(GPU), 1) 49 | COMMON+= -DGPU -I/usr/local/cuda/include/ 50 | CFLAGS+= -DGPU 51 | LDFLAGS+= -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand 52 | endif 53 | 54 | ifeq ($(CUDNN), 1) 55 | COMMON+= -DCUDNN 56 | CFLAGS+= -DCUDNN 57 | LDFLAGS+= -lcudnn 58 | endif 59 | 60 | OBJ=gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o 61 | EXECOBJA=captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o attention.o darknet.o 62 | ifeq ($(GPU), 1) 63 | LDFLAGS+= -lstdc++ 64 | OBJ+=convolutional_kernels.o deconvolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o avgpool_layer_kernels.o 65 | endif 66 | 67 | EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA)) 68 | OBJS = $(addprefix $(OBJDIR), $(OBJ)) 69 | DEPS = $(wildcard ./*.h) Makefile ./darknet.h 70 | 71 | #all: obj backup results $(SLIB) $(ALIB) $(EXEC) 72 | all: obj $(ALIB) 73 | 74 | 75 | $(EXEC): $(EXECOBJ) $(ALIB) 76 | $(CC) $(COMMON) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(ALIB) 77 | 78 | $(ALIB): $(OBJS) 79 | $(AR) $(ARFLAGS) $@ $^ 80 | 81 | $(SLIB): $(OBJS) 82 | $(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS) 83 | 84 | $(OBJDIR)%.o: %.c $(DEPS) 85 | $(CC) $(COMMON) $(CFLAGS) -c $< -o $@ 86 | 87 | $(OBJDIR)%.o: %.cu $(DEPS) 88 | $(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $@ 89 | 90 | obj: 91 | mkdir -p obj 92 | backup: 93 | mkdir -p backup 94 | results: 95 | mkdir -p results 96 | 97 | .PHONY: clean 98 | 99 | clean: 100 | rm -rf $(OBJS) $(SLIB) $(ALIB) $(EXEC) $(EXECOBJ) 101 | 102 | -------------------------------------------------------------------------------- /lightnet/_darknet/activation_layer.c: -------------------------------------------------------------------------------- 1 | #include "activation_layer.h" 2 | #include "utils.h" 3 | #include "cuda.h" 4 | #include "blas.h" 5 | #include "gemm.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | layer make_activation_layer(int batch, int inputs, ACTIVATION activation) 13 | { 14 | layer l = {0}; 15 | l.type = ACTIVE; 16 | 17 | l.inputs = inputs; 18 | l.outputs = inputs; 19 | l.batch=batch; 20 | 21 | l.output = calloc(batch*inputs, sizeof(float*)); 22 | l.delta = calloc(batch*inputs, sizeof(float*)); 23 | 24 | l.forward = forward_activation_layer; 25 | l.backward = backward_activation_layer; 26 | #ifdef GPU 27 | l.forward_gpu = forward_activation_layer_gpu; 28 | l.backward_gpu = backward_activation_layer_gpu; 29 | 30 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 31 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 32 | #endif 33 | l.activation = activation; 34 | fprintf(stderr, "Activation Layer: %d inputs\n", inputs); 35 | return l; 36 | } 37 | 38 | void forward_activation_layer(layer l, network net) 39 | { 40 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 41 | activate_array(l.output, l.outputs*l.batch, l.activation); 42 | } 43 | 44 | void backward_activation_layer(layer l, network net) 45 | { 46 | gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta); 47 | copy_cpu(l.outputs*l.batch, l.delta, 1, net.delta, 1); 48 | } 49 | 50 | #ifdef GPU 51 | 52 | void forward_activation_layer_gpu(layer l, network net) 53 | { 54 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 55 | activate_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation); 56 | } 57 | 58 | void backward_activation_layer_gpu(layer l, network net) 59 | { 60 | gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu); 61 | copy_gpu(l.outputs*l.batch, l.delta_gpu, 1, net.delta_gpu, 1); 62 | } 63 | #endif 64 | -------------------------------------------------------------------------------- /lightnet/_darknet/activation_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef ACTIVATION_LAYER_H 2 | #define ACTIVATION_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_activation_layer(int batch, int inputs, ACTIVATION activation); 9 | 10 | void forward_activation_layer(layer l, network net); 11 | void backward_activation_layer(layer l, network net); 12 | 13 | #ifdef GPU 14 | void forward_activation_layer_gpu(layer l, network net); 15 | void backward_activation_layer_gpu(layer l, network net); 16 | #endif 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /lightnet/_darknet/activations.c: -------------------------------------------------------------------------------- 1 | #include "activations.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | char *get_activation_string(ACTIVATION a) 9 | { 10 | switch(a){ 11 | case LOGISTIC: 12 | return "logistic"; 13 | case LOGGY: 14 | return "loggy"; 15 | case RELU: 16 | return "relu"; 17 | case ELU: 18 | return "elu"; 19 | case RELIE: 20 | return "relie"; 21 | case RAMP: 22 | return "ramp"; 23 | case LINEAR: 24 | return "linear"; 25 | case TANH: 26 | return "tanh"; 27 | case PLSE: 28 | return "plse"; 29 | case LEAKY: 30 | return "leaky"; 31 | case STAIR: 32 | return "stair"; 33 | case HARDTAN: 34 | return "hardtan"; 35 | case LHTAN: 36 | return "lhtan"; 37 | default: 38 | break; 39 | } 40 | return "relu"; 41 | } 42 | 43 | ACTIVATION get_activation(char *s) 44 | { 45 | if (strcmp(s, "logistic")==0) return LOGISTIC; 46 | if (strcmp(s, "loggy")==0) return LOGGY; 47 | if (strcmp(s, "relu")==0) return RELU; 48 | if (strcmp(s, "elu")==0) return ELU; 49 | if (strcmp(s, "relie")==0) return RELIE; 50 | if (strcmp(s, "plse")==0) return PLSE; 51 | if (strcmp(s, "hardtan")==0) return HARDTAN; 52 | if (strcmp(s, "lhtan")==0) return LHTAN; 53 | if (strcmp(s, "linear")==0) return LINEAR; 54 | if (strcmp(s, "ramp")==0) return RAMP; 55 | if (strcmp(s, "leaky")==0) return LEAKY; 56 | if (strcmp(s, "tanh")==0) return TANH; 57 | if (strcmp(s, "stair")==0) return STAIR; 58 | fprintf(stderr, "Couldn't find activation function %s, going with ReLU\n", s); 59 | return RELU; 60 | } 61 | 62 | float activate(float x, ACTIVATION a) 63 | { 64 | switch(a){ 65 | case LINEAR: 66 | return linear_activate(x); 67 | case LOGISTIC: 68 | return logistic_activate(x); 69 | case LOGGY: 70 | return loggy_activate(x); 71 | case RELU: 72 | return relu_activate(x); 73 | case ELU: 74 | return elu_activate(x); 75 | case RELIE: 76 | return relie_activate(x); 77 | case RAMP: 78 | return ramp_activate(x); 79 | case LEAKY: 80 | return leaky_activate(x); 81 | case TANH: 82 | return tanh_activate(x); 83 | case PLSE: 84 | return plse_activate(x); 85 | case STAIR: 86 | return stair_activate(x); 87 | case HARDTAN: 88 | return hardtan_activate(x); 89 | case LHTAN: 90 | return lhtan_activate(x); 91 | } 92 | return 0; 93 | } 94 | 95 | void activate_array(float *x, const int n, const ACTIVATION a) 96 | { 97 | int i; 98 | for(i = 0; i < n; ++i){ 99 | x[i] = activate(x[i], a); 100 | } 101 | } 102 | 103 | float gradient(float x, ACTIVATION a) 104 | { 105 | switch(a){ 106 | case LINEAR: 107 | return linear_gradient(x); 108 | case LOGISTIC: 109 | return logistic_gradient(x); 110 | case LOGGY: 111 | return loggy_gradient(x); 112 | case RELU: 113 | return relu_gradient(x); 114 | case ELU: 115 | return elu_gradient(x); 116 | case RELIE: 117 | return relie_gradient(x); 118 | case RAMP: 119 | return ramp_gradient(x); 120 | case LEAKY: 121 | return leaky_gradient(x); 122 | case TANH: 123 | return tanh_gradient(x); 124 | case PLSE: 125 | return plse_gradient(x); 126 | case STAIR: 127 | return stair_gradient(x); 128 | case HARDTAN: 129 | return hardtan_gradient(x); 130 | case LHTAN: 131 | return lhtan_gradient(x); 132 | } 133 | return 0; 134 | } 135 | 136 | void gradient_array(const float *x, const int n, const ACTIVATION a, float *delta) 137 | { 138 | int i; 139 | for(i = 0; i < n; ++i){ 140 | delta[i] *= gradient(x[i], a); 141 | } 142 | } 143 | 144 | -------------------------------------------------------------------------------- /lightnet/_darknet/activations.h: -------------------------------------------------------------------------------- 1 | #ifndef ACTIVATIONS_H 2 | #define ACTIVATIONS_H 3 | #include "darknet.h" 4 | #include "cuda.h" 5 | #include "math.h" 6 | 7 | ACTIVATION get_activation(char *s); 8 | 9 | char *get_activation_string(ACTIVATION a); 10 | float activate(float x, ACTIVATION a); 11 | float gradient(float x, ACTIVATION a); 12 | void gradient_array(const float *x, const int n, const ACTIVATION a, float *delta); 13 | void activate_array(float *x, const int n, const ACTIVATION a); 14 | #ifdef GPU 15 | void activate_array_gpu(float *x, int n, ACTIVATION a); 16 | void gradient_array_gpu(float *x, int n, ACTIVATION a, float *delta); 17 | #endif 18 | 19 | static inline float stair_activate(float x) 20 | { 21 | int n = floor(x); 22 | if (n%2 == 0) return floor(x/2.); 23 | else return (x - n) + floor(x/2.); 24 | } 25 | static inline float hardtan_activate(float x) 26 | { 27 | if (x < -1) return -1; 28 | if (x > 1) return 1; 29 | return x; 30 | } 31 | static inline float linear_activate(float x){return x;} 32 | static inline float logistic_activate(float x){return 1./(1. + exp(-x));} 33 | static inline float loggy_activate(float x){return 2./(1. + exp(-x)) - 1;} 34 | static inline float relu_activate(float x){return x*(x>0);} 35 | static inline float elu_activate(float x){return (x >= 0)*x + (x < 0)*(exp(x)-1);} 36 | static inline float relie_activate(float x){return (x>0) ? x : .01*x;} 37 | static inline float ramp_activate(float x){return x*(x>0)+.1*x;} 38 | static inline float leaky_activate(float x){return (x>0) ? x : .1*x;} 39 | static inline float tanh_activate(float x){return (exp(2*x)-1)/(exp(2*x)+1);} 40 | static inline float plse_activate(float x) 41 | { 42 | if(x < -4) return .01 * (x + 4); 43 | if(x > 4) return .01 * (x - 4) + 1; 44 | return .125*x + .5; 45 | } 46 | 47 | static inline float lhtan_activate(float x) 48 | { 49 | if(x < 0) return .001*x; 50 | if(x > 1) return .001*(x-1) + 1; 51 | return x; 52 | } 53 | static inline float lhtan_gradient(float x) 54 | { 55 | if(x > 0 && x < 1) return 1; 56 | return .001; 57 | } 58 | 59 | static inline float hardtan_gradient(float x) 60 | { 61 | if (x > -1 && x < 1) return 1; 62 | return 0; 63 | } 64 | static inline float linear_gradient(float x){return 1;} 65 | static inline float logistic_gradient(float x){return (1-x)*x;} 66 | static inline float loggy_gradient(float x) 67 | { 68 | float y = (x+1.)/2.; 69 | return 2*(1-y)*y; 70 | } 71 | static inline float stair_gradient(float x) 72 | { 73 | if (floor(x) == x) return 0; 74 | return 1; 75 | } 76 | static inline float relu_gradient(float x){return (x>0);} 77 | static inline float elu_gradient(float x){return (x >= 0) + (x < 0)*(x + 1);} 78 | static inline float relie_gradient(float x){return (x>0) ? 1 : .01;} 79 | static inline float ramp_gradient(float x){return (x>0)+.1;} 80 | static inline float leaky_gradient(float x){return (x>0) ? 1 : .1;} 81 | static inline float tanh_gradient(float x){return 1-x*x;} 82 | static inline float plse_gradient(float x){return (x < 0 || x > 1) ? .01 : .125;} 83 | 84 | #endif 85 | 86 | -------------------------------------------------------------------------------- /lightnet/_darknet/avgpool_layer.c: -------------------------------------------------------------------------------- 1 | #include "avgpool_layer.h" 2 | #include "cuda.h" 3 | #include 4 | 5 | avgpool_layer make_avgpool_layer(int batch, int w, int h, int c) 6 | { 7 | fprintf(stderr, "avg %4d x%4d x%4d -> %4d\n", w, h, c, c); 8 | avgpool_layer l = {0}; 9 | l.type = AVGPOOL; 10 | l.batch = batch; 11 | l.h = h; 12 | l.w = w; 13 | l.c = c; 14 | l.out_w = 1; 15 | l.out_h = 1; 16 | l.out_c = c; 17 | l.outputs = l.out_c; 18 | l.inputs = h*w*c; 19 | int output_size = l.outputs * batch; 20 | l.output = calloc(output_size, sizeof(float)); 21 | l.delta = calloc(output_size, sizeof(float)); 22 | l.forward = forward_avgpool_layer; 23 | l.backward = backward_avgpool_layer; 24 | #ifdef GPU 25 | l.forward_gpu = forward_avgpool_layer_gpu; 26 | l.backward_gpu = backward_avgpool_layer_gpu; 27 | l.output_gpu = cuda_make_array(l.output, output_size); 28 | l.delta_gpu = cuda_make_array(l.delta, output_size); 29 | #endif 30 | return l; 31 | } 32 | 33 | void resize_avgpool_layer(avgpool_layer *l, int w, int h) 34 | { 35 | l->w = w; 36 | l->h = h; 37 | l->inputs = h*w*l->c; 38 | } 39 | 40 | void forward_avgpool_layer(const avgpool_layer l, network net) 41 | { 42 | int b,i,k; 43 | 44 | for(b = 0; b < l.batch; ++b){ 45 | for(k = 0; k < l.c; ++k){ 46 | int out_index = k + b*l.c; 47 | l.output[out_index] = 0; 48 | for(i = 0; i < l.h*l.w; ++i){ 49 | int in_index = i + l.h*l.w*(k + b*l.c); 50 | l.output[out_index] += net.input[in_index]; 51 | } 52 | l.output[out_index] /= l.h*l.w; 53 | } 54 | } 55 | } 56 | 57 | void backward_avgpool_layer(const avgpool_layer l, network net) 58 | { 59 | int b,i,k; 60 | 61 | for(b = 0; b < l.batch; ++b){ 62 | for(k = 0; k < l.c; ++k){ 63 | int out_index = k + b*l.c; 64 | for(i = 0; i < l.h*l.w; ++i){ 65 | int in_index = i + l.h*l.w*(k + b*l.c); 66 | net.delta[in_index] += l.delta[out_index] / (l.h*l.w); 67 | } 68 | } 69 | } 70 | } 71 | 72 | -------------------------------------------------------------------------------- /lightnet/_darknet/avgpool_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef AVGPOOL_LAYER_H 2 | #define AVGPOOL_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | typedef layer avgpool_layer; 10 | 11 | image get_avgpool_image(avgpool_layer l); 12 | avgpool_layer make_avgpool_layer(int batch, int w, int h, int c); 13 | void resize_avgpool_layer(avgpool_layer *l, int w, int h); 14 | void forward_avgpool_layer(const avgpool_layer l, network net); 15 | void backward_avgpool_layer(const avgpool_layer l, network net); 16 | 17 | #ifdef GPU 18 | void forward_avgpool_layer_gpu(avgpool_layer l, network net); 19 | void backward_avgpool_layer_gpu(avgpool_layer l, network net); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /lightnet/_darknet/avgpool_layer_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "avgpool_layer.h" 7 | #include "cuda.h" 8 | } 9 | 10 | __global__ void forward_avgpool_layer_kernel(int n, int w, int h, int c, float *input, float *output) 11 | { 12 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 13 | if(id >= n) return; 14 | 15 | int k = id % c; 16 | id /= c; 17 | int b = id; 18 | 19 | int i; 20 | int out_index = (k + c*b); 21 | output[out_index] = 0; 22 | for(i = 0; i < w*h; ++i){ 23 | int in_index = i + h*w*(k + b*c); 24 | output[out_index] += input[in_index]; 25 | } 26 | output[out_index] /= w*h; 27 | } 28 | 29 | __global__ void backward_avgpool_layer_kernel(int n, int w, int h, int c, float *in_delta, float *out_delta) 30 | { 31 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 32 | if(id >= n) return; 33 | 34 | int k = id % c; 35 | id /= c; 36 | int b = id; 37 | 38 | int i; 39 | int out_index = (k + c*b); 40 | for(i = 0; i < w*h; ++i){ 41 | int in_index = i + h*w*(k + b*c); 42 | in_delta[in_index] += out_delta[out_index] / (w*h); 43 | } 44 | } 45 | 46 | extern "C" void forward_avgpool_layer_gpu(avgpool_layer layer, network net) 47 | { 48 | size_t n = layer.c*layer.batch; 49 | 50 | forward_avgpool_layer_kernel<<>>(n, layer.w, layer.h, layer.c, net.input_gpu, layer.output_gpu); 51 | check_error(cudaPeekAtLastError()); 52 | } 53 | 54 | extern "C" void backward_avgpool_layer_gpu(avgpool_layer layer, network net) 55 | { 56 | size_t n = layer.c*layer.batch; 57 | 58 | backward_avgpool_layer_kernel<<>>(n, layer.w, layer.h, layer.c, net.delta_gpu, layer.delta_gpu); 59 | check_error(cudaPeekAtLastError()); 60 | } 61 | 62 | -------------------------------------------------------------------------------- /lightnet/_darknet/batchnorm_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef BATCHNORM_LAYER_H 2 | #define BATCHNORM_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_batchnorm_layer(int batch, int w, int h, int c); 9 | void forward_batchnorm_layer(layer l, network net); 10 | void backward_batchnorm_layer(layer l, network net); 11 | 12 | #ifdef GPU 13 | void forward_batchnorm_layer_gpu(layer l, network net); 14 | void backward_batchnorm_layer_gpu(layer l, network net); 15 | void pull_batchnorm_layer(layer l); 16 | void push_batchnorm_layer(layer l); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /lightnet/_darknet/box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include "darknet.h" 4 | 5 | typedef struct{ 6 | float dx, dy, dw, dh; 7 | } dbox; 8 | 9 | float box_rmse(box a, box b); 10 | dbox diou(box a, box b); 11 | box decode_box(box b, box anchor); 12 | box encode_box(box b, box anchor); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /lightnet/_darknet/classifier.h: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /lightnet/_darknet/col2im.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void col2im_add_pixel(float *im, int height, int width, int channels, 4 | int row, int col, int channel, int pad, float val) 5 | { 6 | row -= pad; 7 | col -= pad; 8 | 9 | if (row < 0 || col < 0 || 10 | row >= height || col >= width) return; 11 | im[col + width*(row + height*channel)] += val; 12 | } 13 | //This one might be too, can't remember. 14 | void col2im_cpu(float* data_col, 15 | int channels, int height, int width, 16 | int ksize, int stride, int pad, float* data_im) 17 | { 18 | int c,h,w; 19 | int height_col = (height + 2*pad - ksize) / stride + 1; 20 | int width_col = (width + 2*pad - ksize) / stride + 1; 21 | 22 | int channels_col = channels * ksize * ksize; 23 | for (c = 0; c < channels_col; ++c) { 24 | int w_offset = c % ksize; 25 | int h_offset = (c / ksize) % ksize; 26 | int c_im = c / ksize / ksize; 27 | for (h = 0; h < height_col; ++h) { 28 | for (w = 0; w < width_col; ++w) { 29 | int im_row = h_offset + h * stride; 30 | int im_col = w_offset + w * stride; 31 | int col_index = (c * height_col + h) * width_col + w; 32 | double val = data_col[col_index]; 33 | col2im_add_pixel(data_im, height, width, channels, 34 | im_row, im_col, c_im, pad, val); 35 | } 36 | } 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /lightnet/_darknet/col2im.h: -------------------------------------------------------------------------------- 1 | #ifndef COL2IM_H 2 | #define COL2IM_H 3 | 4 | void col2im_cpu(float* data_col, 5 | int channels, int height, int width, 6 | int ksize, int stride, int pad, float* data_im); 7 | 8 | #ifdef GPU 9 | void col2im_gpu(float *data_col, 10 | int channels, int height, int width, 11 | int ksize, int stride, int pad, float *data_im); 12 | #endif 13 | #endif 14 | -------------------------------------------------------------------------------- /lightnet/_darknet/col2im_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "col2im.h" 7 | #include "cuda.h" 8 | } 9 | 10 | // src: https://github.com/BVLC/caffe/blob/master/src/caffe/util/im2col.cu 11 | // You may also want to read: https://github.com/BVLC/caffe/blob/master/LICENSE 12 | 13 | __global__ void col2im_gpu_kernel(const int n, const float* data_col, 14 | const int height, const int width, const int ksize, 15 | const int pad, 16 | const int stride, 17 | const int height_col, const int width_col, 18 | float *data_im) { 19 | int index = blockIdx.x*blockDim.x+threadIdx.x; 20 | for(; index < n; index += blockDim.x*gridDim.x){ 21 | float val = 0; 22 | int w = index % width + pad; 23 | int h = (index / width) % height + pad; 24 | int c = index / (width * height); 25 | // compute the start and end of the output 26 | int w_col_start = (w < ksize) ? 0 : (w - ksize) / stride + 1; 27 | int w_col_end = min(w / stride + 1, width_col); 28 | int h_col_start = (h < ksize) ? 0 : (h - ksize) / stride + 1; 29 | int h_col_end = min(h / stride + 1, height_col); 30 | // equivalent implementation 31 | int offset = 32 | (c * ksize * ksize + h * ksize + w) * height_col * width_col; 33 | int coeff_h_col = (1 - stride * ksize * height_col) * width_col; 34 | int coeff_w_col = (1 - stride * height_col * width_col); 35 | for (int h_col = h_col_start; h_col < h_col_end; ++h_col) { 36 | for (int w_col = w_col_start; w_col < w_col_end; ++w_col) { 37 | val += data_col[offset + h_col * coeff_h_col + w_col * coeff_w_col]; 38 | } 39 | } 40 | data_im[index] += val; 41 | } 42 | } 43 | 44 | void col2im_gpu(float *data_col, 45 | int channels, int height, int width, 46 | int ksize, int stride, int pad, float *data_im){ 47 | // We are going to launch channels * height_col * width_col kernels, each 48 | // kernel responsible for copying a single-channel grid. 49 | int height_col = (height + 2 * pad - ksize) / stride + 1; 50 | int width_col = (width + 2 * pad - ksize) / stride + 1; 51 | int num_kernels = channels * height * width; 52 | col2im_gpu_kernel<<<(num_kernels+BLOCK-1)/BLOCK, 53 | BLOCK>>>( 54 | num_kernels, data_col, height, width, ksize, pad, 55 | stride, height_col, 56 | width_col, data_im); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /lightnet/_darknet/connected_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CONNECTED_LAYER_H 2 | #define CONNECTED_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_connected_layer(int batch, int inputs, int outputs, ACTIVATION activation, int batch_normalize, int adam); 9 | 10 | void forward_connected_layer(layer l, network net); 11 | void backward_connected_layer(layer l, network net); 12 | void update_connected_layer(layer l, update_args a); 13 | 14 | #ifdef GPU 15 | void forward_connected_layer_gpu(layer l, network net); 16 | void backward_connected_layer_gpu(layer l, network net); 17 | void update_connected_layer_gpu(layer l, update_args a); 18 | void push_connected_layer(layer l); 19 | void pull_connected_layer(layer l); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /lightnet/_darknet/convolutional_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CONVOLUTIONAL_LAYER_H 2 | #define CONVOLUTIONAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | typedef layer convolutional_layer; 11 | 12 | #ifdef GPU 13 | void forward_convolutional_layer_gpu(convolutional_layer layer, network net); 14 | void backward_convolutional_layer_gpu(convolutional_layer layer, network net); 15 | void update_convolutional_layer_gpu(convolutional_layer layer, update_args a); 16 | 17 | void push_convolutional_layer(convolutional_layer layer); 18 | void pull_convolutional_layer(convolutional_layer layer); 19 | 20 | void add_bias_gpu(float *output, float *biases, int batch, int n, int size); 21 | void backward_bias_gpu(float *bias_updates, float *delta, int batch, int n, int size); 22 | void adam_update_gpu(float *w, float *d, float *m, float *v, float B1, float B2, float eps, float decay, float rate, int n, int batch, int t); 23 | #ifdef CUDNN 24 | void cudnn_convolutional_setup(layer *l); 25 | #endif 26 | #endif 27 | 28 | convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int n, int groups, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int binary, int xnor, int adam); 29 | void resize_convolutional_layer(convolutional_layer *layer, int w, int h); 30 | void forward_convolutional_layer(const convolutional_layer layer, network net); 31 | void update_convolutional_layer(convolutional_layer layer, update_args a); 32 | image *visualize_convolutional_layer(convolutional_layer layer, char *window, image *prev_weights); 33 | void binarize_weights(float *weights, int n, int size, float *binary); 34 | void swap_binary(convolutional_layer *l); 35 | void binarize_weights2(float *weights, int n, int size, char *binary, float *scales); 36 | 37 | void backward_convolutional_layer(convolutional_layer layer, network net); 38 | 39 | void add_bias(float *output, float *biases, int batch, int n, int size); 40 | void backward_bias(float *bias_updates, float *delta, int batch, int n, int size); 41 | 42 | image get_convolutional_image(convolutional_layer layer); 43 | image get_convolutional_delta(convolutional_layer layer); 44 | image get_convolutional_weight(convolutional_layer layer, int i); 45 | 46 | int convolutional_out_height(convolutional_layer layer); 47 | int convolutional_out_width(convolutional_layer layer); 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /lightnet/_darknet/cost_layer.c: -------------------------------------------------------------------------------- 1 | #include "cost_layer.h" 2 | #include "utils.h" 3 | #include "cuda.h" 4 | #include "blas.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | COST_TYPE get_cost_type(char *s) 11 | { 12 | if (strcmp(s, "seg")==0) return SEG; 13 | if (strcmp(s, "sse")==0) return SSE; 14 | if (strcmp(s, "masked")==0) return MASKED; 15 | if (strcmp(s, "smooth")==0) return SMOOTH; 16 | if (strcmp(s, "L1")==0) return L1; 17 | fprintf(stderr, "Couldn't find cost type %s, going with SSE\n", s); 18 | return SSE; 19 | } 20 | 21 | char *get_cost_string(COST_TYPE a) 22 | { 23 | switch(a){ 24 | case SEG: 25 | return "seg"; 26 | case SSE: 27 | return "sse"; 28 | case MASKED: 29 | return "masked"; 30 | case SMOOTH: 31 | return "smooth"; 32 | case L1: 33 | return "L1"; 34 | } 35 | return "sse"; 36 | } 37 | 38 | cost_layer make_cost_layer(int batch, int inputs, COST_TYPE cost_type, float scale) 39 | { 40 | fprintf(stderr, "cost %4d\n", inputs); 41 | cost_layer l = {0}; 42 | l.type = COST; 43 | 44 | l.scale = scale; 45 | l.batch = batch; 46 | l.inputs = inputs; 47 | l.outputs = inputs; 48 | l.cost_type = cost_type; 49 | l.delta = calloc(inputs*batch, sizeof(float)); 50 | l.output = calloc(inputs*batch, sizeof(float)); 51 | l.cost = calloc(1, sizeof(float)); 52 | 53 | l.forward = forward_cost_layer; 54 | l.backward = backward_cost_layer; 55 | #ifdef GPU 56 | l.forward_gpu = forward_cost_layer_gpu; 57 | l.backward_gpu = backward_cost_layer_gpu; 58 | 59 | l.delta_gpu = cuda_make_array(l.output, inputs*batch); 60 | l.output_gpu = cuda_make_array(l.delta, inputs*batch); 61 | #endif 62 | return l; 63 | } 64 | 65 | void resize_cost_layer(cost_layer *l, int inputs) 66 | { 67 | l->inputs = inputs; 68 | l->outputs = inputs; 69 | l->delta = realloc(l->delta, inputs*l->batch*sizeof(float)); 70 | l->output = realloc(l->output, inputs*l->batch*sizeof(float)); 71 | #ifdef GPU 72 | cuda_free(l->delta_gpu); 73 | cuda_free(l->output_gpu); 74 | l->delta_gpu = cuda_make_array(l->delta, inputs*l->batch); 75 | l->output_gpu = cuda_make_array(l->output, inputs*l->batch); 76 | #endif 77 | } 78 | 79 | void forward_cost_layer(cost_layer l, network net) 80 | { 81 | if (!net.truth) return; 82 | if(l.cost_type == MASKED){ 83 | int i; 84 | for(i = 0; i < l.batch*l.inputs; ++i){ 85 | if(net.truth[i] == SECRET_NUM) net.input[i] = SECRET_NUM; 86 | } 87 | } 88 | if(l.cost_type == SMOOTH){ 89 | smooth_l1_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output); 90 | }else if(l.cost_type == L1){ 91 | l1_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output); 92 | } else { 93 | l2_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output); 94 | } 95 | l.cost[0] = sum_array(l.output, l.batch*l.inputs); 96 | } 97 | 98 | void backward_cost_layer(const cost_layer l, network net) 99 | { 100 | axpy_cpu(l.batch*l.inputs, l.scale, l.delta, 1, net.delta, 1); 101 | } 102 | 103 | #ifdef GPU 104 | 105 | void pull_cost_layer(cost_layer l) 106 | { 107 | cuda_pull_array(l.delta_gpu, l.delta, l.batch*l.inputs); 108 | } 109 | 110 | void push_cost_layer(cost_layer l) 111 | { 112 | cuda_push_array(l.delta_gpu, l.delta, l.batch*l.inputs); 113 | } 114 | 115 | int float_abs_compare (const void * a, const void * b) 116 | { 117 | float fa = *(const float*) a; 118 | if(fa < 0) fa = -fa; 119 | float fb = *(const float*) b; 120 | if(fb < 0) fb = -fb; 121 | return (fa > fb) - (fa < fb); 122 | } 123 | 124 | void forward_cost_layer_gpu(cost_layer l, network net) 125 | { 126 | if (!net.truth_gpu) return; 127 | if(l.smooth){ 128 | scal_gpu(l.batch*l.inputs, (1-l.smooth), net.truth_gpu, 1); 129 | add_gpu(l.batch*l.inputs, l.smooth * 1./l.inputs, net.truth_gpu, 1); 130 | } 131 | if (l.cost_type == MASKED) { 132 | mask_gpu(l.batch*l.inputs, net.input_gpu, SECRET_NUM, net.truth_gpu); 133 | } 134 | 135 | if(l.cost_type == SMOOTH){ 136 | smooth_l1_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu); 137 | } else if (l.cost_type == L1){ 138 | l1_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu); 139 | } else { 140 | l2_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu); 141 | } 142 | 143 | if (l.cost_type == SEG && l.noobject_scale != 1) { 144 | scale_mask_gpu(l.batch*l.inputs, l.delta_gpu, 0, net.truth_gpu, l.noobject_scale); 145 | scale_mask_gpu(l.batch*l.inputs, l.output_gpu, 0, net.truth_gpu, l.noobject_scale); 146 | } 147 | 148 | if(l.ratio){ 149 | cuda_pull_array(l.delta_gpu, l.delta, l.batch*l.inputs); 150 | qsort(l.delta, l.batch*l.inputs, sizeof(float), float_abs_compare); 151 | int n = (1-l.ratio) * l.batch*l.inputs; 152 | float thresh = l.delta[n]; 153 | thresh = 0; 154 | printf("%f\n", thresh); 155 | supp_gpu(l.batch*l.inputs, thresh, l.delta_gpu, 1); 156 | } 157 | 158 | if(l.thresh){ 159 | supp_gpu(l.batch*l.inputs, l.thresh*1./l.inputs, l.delta_gpu, 1); 160 | } 161 | 162 | cuda_pull_array(l.output_gpu, l.output, l.batch*l.inputs); 163 | l.cost[0] = sum_array(l.output, l.batch*l.inputs); 164 | } 165 | 166 | void backward_cost_layer_gpu(const cost_layer l, network net) 167 | { 168 | axpy_gpu(l.batch*l.inputs, l.scale, l.delta_gpu, 1, net.delta_gpu, 1); 169 | } 170 | #endif 171 | 172 | -------------------------------------------------------------------------------- /lightnet/_darknet/cost_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef COST_LAYER_H 2 | #define COST_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | typedef layer cost_layer; 7 | 8 | COST_TYPE get_cost_type(char *s); 9 | char *get_cost_string(COST_TYPE a); 10 | cost_layer make_cost_layer(int batch, int inputs, COST_TYPE type, float scale); 11 | void forward_cost_layer(const cost_layer l, network net); 12 | void backward_cost_layer(const cost_layer l, network net); 13 | void resize_cost_layer(cost_layer *l, int inputs); 14 | 15 | #ifdef GPU 16 | void forward_cost_layer_gpu(cost_layer l, network net); 17 | void backward_cost_layer_gpu(const cost_layer l, network net); 18 | #endif 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /lightnet/_darknet/crnn_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef CRNN_LAYER_H 3 | #define CRNN_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_crnn_layer(int batch, int h, int w, int c, int hidden_filters, int output_filters, int steps, ACTIVATION activation, int batch_normalize); 10 | 11 | void forward_crnn_layer(layer l, network net); 12 | void backward_crnn_layer(layer l, network net); 13 | void update_crnn_layer(layer l, update_args a); 14 | 15 | #ifdef GPU 16 | void forward_crnn_layer_gpu(layer l, network net); 17 | void backward_crnn_layer_gpu(layer l, network net); 18 | void update_crnn_layer_gpu(layer l, update_args a); 19 | void push_crnn_layer(layer l); 20 | void pull_crnn_layer(layer l); 21 | #endif 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /lightnet/_darknet/crop_layer.c: -------------------------------------------------------------------------------- 1 | #include "crop_layer.h" 2 | #include "cuda.h" 3 | #include 4 | 5 | image get_crop_image(crop_layer l) 6 | { 7 | int h = l.out_h; 8 | int w = l.out_w; 9 | int c = l.out_c; 10 | return float_to_image(w,h,c,l.output); 11 | } 12 | 13 | void backward_crop_layer(const crop_layer l, network net){} 14 | void backward_crop_layer_gpu(const crop_layer l, network net){} 15 | 16 | crop_layer make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip, float angle, float saturation, float exposure) 17 | { 18 | fprintf(stderr, "Crop Layer: %d x %d -> %d x %d x %d image\n", h,w,crop_height,crop_width,c); 19 | crop_layer l = {0}; 20 | l.type = CROP; 21 | l.batch = batch; 22 | l.h = h; 23 | l.w = w; 24 | l.c = c; 25 | l.scale = (float)crop_height / h; 26 | l.flip = flip; 27 | l.angle = angle; 28 | l.saturation = saturation; 29 | l.exposure = exposure; 30 | l.out_w = crop_width; 31 | l.out_h = crop_height; 32 | l.out_c = c; 33 | l.inputs = l.w * l.h * l.c; 34 | l.outputs = l.out_w * l.out_h * l.out_c; 35 | l.output = calloc(l.outputs*batch, sizeof(float)); 36 | l.forward = forward_crop_layer; 37 | l.backward = backward_crop_layer; 38 | 39 | #ifdef GPU 40 | l.forward_gpu = forward_crop_layer_gpu; 41 | l.backward_gpu = backward_crop_layer_gpu; 42 | l.output_gpu = cuda_make_array(l.output, l.outputs*batch); 43 | l.rand_gpu = cuda_make_array(0, l.batch*8); 44 | #endif 45 | return l; 46 | } 47 | 48 | void resize_crop_layer(layer *l, int w, int h) 49 | { 50 | l->w = w; 51 | l->h = h; 52 | 53 | l->out_w = l->scale*w; 54 | l->out_h = l->scale*h; 55 | 56 | l->inputs = l->w * l->h * l->c; 57 | l->outputs = l->out_h * l->out_w * l->out_c; 58 | 59 | l->output = realloc(l->output, l->batch*l->outputs*sizeof(float)); 60 | #ifdef GPU 61 | cuda_free(l->output_gpu); 62 | l->output_gpu = cuda_make_array(l->output, l->outputs*l->batch); 63 | #endif 64 | } 65 | 66 | 67 | void forward_crop_layer(const crop_layer l, network net) 68 | { 69 | int i,j,c,b,row,col; 70 | int index; 71 | int count = 0; 72 | int flip = (l.flip && rand()%2); 73 | int dh = rand()%(l.h - l.out_h + 1); 74 | int dw = rand()%(l.w - l.out_w + 1); 75 | float scale = 2; 76 | float trans = -1; 77 | if(l.noadjust){ 78 | scale = 1; 79 | trans = 0; 80 | } 81 | if(!net.train){ 82 | flip = 0; 83 | dh = (l.h - l.out_h)/2; 84 | dw = (l.w - l.out_w)/2; 85 | } 86 | for(b = 0; b < l.batch; ++b){ 87 | for(c = 0; c < l.c; ++c){ 88 | for(i = 0; i < l.out_h; ++i){ 89 | for(j = 0; j < l.out_w; ++j){ 90 | if(flip){ 91 | col = l.w - dw - j - 1; 92 | }else{ 93 | col = j + dw; 94 | } 95 | row = i + dh; 96 | index = col+l.w*(row+l.h*(c + l.c*b)); 97 | l.output[count++] = net.input[index]*scale + trans; 98 | } 99 | } 100 | } 101 | } 102 | } 103 | 104 | -------------------------------------------------------------------------------- /lightnet/_darknet/crop_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CROP_LAYER_H 2 | #define CROP_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | typedef layer crop_layer; 9 | 10 | image get_crop_image(crop_layer l); 11 | crop_layer make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip, float angle, float saturation, float exposure); 12 | void forward_crop_layer(const crop_layer l, network net); 13 | void resize_crop_layer(layer *l, int w, int h); 14 | 15 | #ifdef GPU 16 | void forward_crop_layer_gpu(crop_layer l, network net); 17 | #endif 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /lightnet/_darknet/cuda.c: -------------------------------------------------------------------------------- 1 | int gpu_index = 0; 2 | 3 | #ifdef GPU 4 | 5 | #include "cuda.h" 6 | #include "utils.h" 7 | #include "blas.h" 8 | #include 9 | #include 10 | #include 11 | 12 | void cuda_set_device(int n) 13 | { 14 | gpu_index = n; 15 | cudaError_t status = cudaSetDevice(n); 16 | check_error(status); 17 | } 18 | 19 | int cuda_get_device() 20 | { 21 | int n = 0; 22 | cudaError_t status = cudaGetDevice(&n); 23 | check_error(status); 24 | return n; 25 | } 26 | 27 | void check_error(cudaError_t status) 28 | { 29 | //cudaDeviceSynchronize(); 30 | cudaError_t status2 = cudaGetLastError(); 31 | if (status != cudaSuccess) 32 | { 33 | const char *s = cudaGetErrorString(status); 34 | char buffer[256]; 35 | printf("CUDA Error: %s\n", s); 36 | assert(0); 37 | snprintf(buffer, 256, "CUDA Error: %s", s); 38 | error(buffer); 39 | } 40 | if (status2 != cudaSuccess) 41 | { 42 | const char *s = cudaGetErrorString(status); 43 | char buffer[256]; 44 | printf("CUDA Error Prev: %s\n", s); 45 | assert(0); 46 | snprintf(buffer, 256, "CUDA Error Prev: %s", s); 47 | error(buffer); 48 | } 49 | } 50 | 51 | dim3 cuda_gridsize(size_t n){ 52 | size_t k = (n-1) / BLOCK + 1; 53 | size_t x = k; 54 | size_t y = 1; 55 | if(x > 65535){ 56 | x = ceil(sqrt(k)); 57 | y = (n-1)/(x*BLOCK) + 1; 58 | } 59 | dim3 d = {x, y, 1}; 60 | //printf("%ld %ld %ld %ld\n", n, x, y, x*y*BLOCK); 61 | return d; 62 | } 63 | 64 | #ifdef CUDNN 65 | cudnnHandle_t cudnn_handle() 66 | { 67 | static int init[16] = {0}; 68 | static cudnnHandle_t handle[16]; 69 | int i = cuda_get_device(); 70 | if(!init[i]) { 71 | cudnnCreate(&handle[i]); 72 | init[i] = 1; 73 | } 74 | return handle[i]; 75 | } 76 | #endif 77 | 78 | cublasHandle_t blas_handle() 79 | { 80 | static int init[16] = {0}; 81 | static cublasHandle_t handle[16]; 82 | int i = cuda_get_device(); 83 | if(!init[i]) { 84 | cublasCreate(&handle[i]); 85 | init[i] = 1; 86 | } 87 | return handle[i]; 88 | } 89 | 90 | float *cuda_make_array(float *x, size_t n) 91 | { 92 | float *x_gpu; 93 | size_t size = sizeof(float)*n; 94 | cudaError_t status = cudaMalloc((void **)&x_gpu, size); 95 | check_error(status); 96 | if(x){ 97 | status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice); 98 | check_error(status); 99 | } else { 100 | fill_gpu(n, 0, x_gpu, 1); 101 | } 102 | if(!x_gpu) error("Cuda malloc failed\n"); 103 | return x_gpu; 104 | } 105 | 106 | void cuda_random(float *x_gpu, size_t n) 107 | { 108 | static curandGenerator_t gen[16]; 109 | static int init[16] = {0}; 110 | int i = cuda_get_device(); 111 | if(!init[i]){ 112 | curandCreateGenerator(&gen[i], CURAND_RNG_PSEUDO_DEFAULT); 113 | curandSetPseudoRandomGeneratorSeed(gen[i], time(0)); 114 | init[i] = 1; 115 | } 116 | curandGenerateUniform(gen[i], x_gpu, n); 117 | check_error(cudaPeekAtLastError()); 118 | } 119 | 120 | float cuda_compare(float *x_gpu, float *x, size_t n, char *s) 121 | { 122 | float *tmp = calloc(n, sizeof(float)); 123 | cuda_pull_array(x_gpu, tmp, n); 124 | //int i; 125 | //for(i = 0; i < n; ++i) printf("%f %f\n", tmp[i], x[i]); 126 | axpy_cpu(n, -1, x, 1, tmp, 1); 127 | float err = dot_cpu(n, tmp, 1, tmp, 1); 128 | printf("Error %s: %f\n", s, sqrt(err/n)); 129 | free(tmp); 130 | return err; 131 | } 132 | 133 | int *cuda_make_int_array(int *x, size_t n) 134 | { 135 | int *x_gpu; 136 | size_t size = sizeof(int)*n; 137 | cudaError_t status = cudaMalloc((void **)&x_gpu, size); 138 | check_error(status); 139 | if(x){ 140 | status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice); 141 | check_error(status); 142 | } 143 | if(!x_gpu) error("Cuda malloc failed\n"); 144 | return x_gpu; 145 | } 146 | 147 | void cuda_free(float *x_gpu) 148 | { 149 | cudaError_t status = cudaFree(x_gpu); 150 | check_error(status); 151 | } 152 | 153 | void cuda_push_array(float *x_gpu, float *x, size_t n) 154 | { 155 | size_t size = sizeof(float)*n; 156 | cudaError_t status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice); 157 | check_error(status); 158 | } 159 | 160 | void cuda_pull_array(float *x_gpu, float *x, size_t n) 161 | { 162 | size_t size = sizeof(float)*n; 163 | cudaError_t status = cudaMemcpy(x, x_gpu, size, cudaMemcpyDeviceToHost); 164 | check_error(status); 165 | } 166 | 167 | float cuda_mag_array(float *x_gpu, size_t n) 168 | { 169 | float *temp = calloc(n, sizeof(float)); 170 | cuda_pull_array(x_gpu, temp, n); 171 | float m = mag_array(temp, n); 172 | free(temp); 173 | return m; 174 | } 175 | #else 176 | void cuda_set_device(int n){} 177 | 178 | #endif 179 | -------------------------------------------------------------------------------- /lightnet/_darknet/cuda.h: -------------------------------------------------------------------------------- 1 | #ifndef CUDA_H 2 | #define CUDA_H 3 | 4 | #include "darknet.h" 5 | 6 | #ifdef GPU 7 | 8 | void check_error(cudaError_t status); 9 | cublasHandle_t blas_handle(); 10 | int *cuda_make_int_array(int *x, size_t n); 11 | void cuda_random(float *x_gpu, size_t n); 12 | float cuda_compare(float *x_gpu, float *x, size_t n, char *s); 13 | dim3 cuda_gridsize(size_t n); 14 | 15 | #ifdef CUDNN 16 | cudnnHandle_t cudnn_handle(); 17 | #endif 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /lightnet/_darknet/data.h: -------------------------------------------------------------------------------- 1 | #ifndef DATA_H 2 | #define DATA_H 3 | #include 4 | 5 | #include "darknet.h" 6 | #include "matrix.h" 7 | #include "list.h" 8 | #include "image.h" 9 | #include "tree.h" 10 | 11 | static inline float distance_from_edge(int x, int max) 12 | { 13 | int dx = (max/2) - x; 14 | if (dx < 0) dx = -dx; 15 | dx = (max/2) + 1 - dx; 16 | dx *= 2; 17 | float dist = (float)dx/max; 18 | if (dist > 1) dist = 1; 19 | return dist; 20 | } 21 | void load_data_blocking(load_args args); 22 | 23 | 24 | void print_letters(float *pred, int n); 25 | data load_data_captcha(char **paths, int n, int m, int k, int w, int h); 26 | data load_data_captcha_encode(char **paths, int n, int m, int w, int h); 27 | data load_data_detection(int n, char **paths, int m, int w, int h, int boxes, int classes, float jitter, float hue, float saturation, float exposure); 28 | data load_data_tag(char **paths, int n, int m, int k, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure); 29 | matrix load_image_augment_paths(char **paths, int n, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure, int center); 30 | data load_data_super(char **paths, int n, int m, int w, int h, int scale); 31 | data load_data_augment(char **paths, int n, int m, char **labels, int k, tree *hierarchy, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure, int center); 32 | data load_data_regression(char **paths, int n, int m, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure); 33 | data load_go(char *filename); 34 | 35 | data load_data_region(int n, char **paths, int m, int w, int h, int size, int classes, float jitter, float hue, float saturation, float exposure); 36 | 37 | 38 | data load_data_writing(char **paths, int n, int m, int w, int h, int out_w, int out_h); 39 | 40 | void get_random_batch(data d, int n, float *X, float *y); 41 | data get_data_part(data d, int part, int total); 42 | data get_random_data(data d, int num); 43 | data load_categorical_data_csv(char *filename, int target, int k); 44 | void normalize_data_rows(data d); 45 | void scale_data_rows(data d, float s); 46 | void translate_data_rows(data d, float s); 47 | void randomize_data(data d); 48 | void randomize_boxes(box_label *b, int n); 49 | void correct_boxes(box_label *boxes, int n, float dx, float dy, float sx, float sy, int flip); 50 | data *split_data(data d, int part, int total); 51 | data concat_datas(data *d, int n); 52 | void fill_truth(char *path, char **labels, int k, float *truth); 53 | #endif 54 | -------------------------------------------------------------------------------- /lightnet/_darknet/deconvolutional_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "convolutional_layer.h" 7 | #include "deconvolutional_layer.h" 8 | #include "batchnorm_layer.h" 9 | #include "gemm.h" 10 | #include "blas.h" 11 | #include "im2col.h" 12 | #include "col2im.h" 13 | #include "utils.h" 14 | #include "cuda.h" 15 | } 16 | 17 | extern "C" void forward_deconvolutional_layer_gpu(layer l, network net) 18 | { 19 | int i; 20 | 21 | int m = l.size*l.size*l.n; 22 | int n = l.h*l.w; 23 | int k = l.c; 24 | 25 | fill_gpu(l.outputs*l.batch, 0, l.output_gpu, 1); 26 | 27 | for(i = 0; i < l.batch; ++i){ 28 | float *a = l.weights_gpu; 29 | float *b = net.input_gpu + i*l.c*l.h*l.w; 30 | float *c = net.workspace; 31 | 32 | gemm_gpu(1,0,m,n,k,1,a,m,b,n,0,c,n); 33 | 34 | col2im_gpu(net.workspace, l.out_c, l.out_h, l.out_w, l.size, l.stride, l.pad, l.output_gpu+i*l.outputs); 35 | } 36 | if (l.batch_normalize) { 37 | forward_batchnorm_layer_gpu(l, net); 38 | } else { 39 | add_bias_gpu(l.output_gpu, l.biases_gpu, l.batch, l.n, l.out_w*l.out_h); 40 | } 41 | activate_array_gpu(l.output_gpu, l.batch*l.n*l.out_w*l.out_h, l.activation); 42 | } 43 | 44 | extern "C" void backward_deconvolutional_layer_gpu(layer l, network net) 45 | { 46 | int i; 47 | 48 | constrain_gpu(l.outputs*l.batch, 1, l.delta_gpu, 1); 49 | gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu); 50 | 51 | if(l.batch_normalize){ 52 | backward_batchnorm_layer_gpu(l, net); 53 | } else { 54 | backward_bias_gpu(l.bias_updates_gpu, l.delta_gpu, l.batch, l.n, l.out_w*l.out_h); 55 | } 56 | 57 | //if(net.delta_gpu) memset(net.delta_gpu, 0, l.batch*l.h*l.w*l.c*sizeof(float)); 58 | 59 | for(i = 0; i < l.batch; ++i){ 60 | int m = l.c; 61 | int n = l.size*l.size*l.n; 62 | int k = l.h*l.w; 63 | 64 | float *a = net.input_gpu + i*m*k; 65 | float *b = net.workspace; 66 | float *c = l.weight_updates_gpu; 67 | 68 | im2col_gpu(l.delta_gpu + i*l.outputs, l.out_c, l.out_h, l.out_w, 69 | l.size, l.stride, l.pad, b); 70 | gemm_gpu(0,1,m,n,k,1,a,k,b,k,1,c,n); 71 | 72 | if(net.delta_gpu){ 73 | int m = l.c; 74 | int n = l.h*l.w; 75 | int k = l.size*l.size*l.n; 76 | 77 | float *a = l.weights_gpu; 78 | float *b = net.workspace; 79 | float *c = net.delta_gpu + i*n*m; 80 | 81 | gemm_gpu(0,0,m,n,k,1,a,k,b,n,1,c,n); 82 | } 83 | } 84 | } 85 | 86 | extern "C" void pull_deconvolutional_layer(layer l) 87 | { 88 | cuda_pull_array(l.weights_gpu, l.weights, l.c*l.n*l.size*l.size); 89 | cuda_pull_array(l.biases_gpu, l.biases, l.n); 90 | cuda_pull_array(l.weight_updates_gpu, l.weight_updates, l.c*l.n*l.size*l.size); 91 | cuda_pull_array(l.bias_updates_gpu, l.bias_updates, l.n); 92 | if (l.batch_normalize){ 93 | cuda_pull_array(l.scales_gpu, l.scales, l.n); 94 | cuda_pull_array(l.rolling_mean_gpu, l.rolling_mean, l.n); 95 | cuda_pull_array(l.rolling_variance_gpu, l.rolling_variance, l.n); 96 | } 97 | } 98 | 99 | extern "C" void push_deconvolutional_layer(layer l) 100 | { 101 | cuda_push_array(l.weights_gpu, l.weights, l.c*l.n*l.size*l.size); 102 | cuda_push_array(l.biases_gpu, l.biases, l.n); 103 | cuda_push_array(l.weight_updates_gpu, l.weight_updates, l.c*l.n*l.size*l.size); 104 | cuda_push_array(l.bias_updates_gpu, l.bias_updates, l.n); 105 | if (l.batch_normalize){ 106 | cuda_push_array(l.scales_gpu, l.scales, l.n); 107 | cuda_push_array(l.rolling_mean_gpu, l.rolling_mean, l.n); 108 | cuda_push_array(l.rolling_variance_gpu, l.rolling_variance, l.n); 109 | } 110 | } 111 | 112 | void update_deconvolutional_layer_gpu(layer l, update_args a) 113 | { 114 | float learning_rate = a.learning_rate*l.learning_rate_scale; 115 | float momentum = a.momentum; 116 | float decay = a.decay; 117 | int batch = a.batch; 118 | 119 | int size = l.size*l.size*l.c*l.n; 120 | 121 | if(a.adam){ 122 | adam_update_gpu(l.weights_gpu, l.weight_updates_gpu, l.m_gpu, l.v_gpu, a.B1, a.B2, a.eps, decay, learning_rate, size, batch, a.t); 123 | adam_update_gpu(l.biases_gpu, l.bias_updates_gpu, l.bias_m_gpu, l.bias_v_gpu, a.B1, a.B2, a.eps, decay, learning_rate, l.n, batch, a.t); 124 | if(l.scales_gpu){ 125 | adam_update_gpu(l.scales_gpu, l.scale_updates_gpu, l.scale_m_gpu, l.scale_v_gpu, a.B1, a.B2, a.eps, decay, learning_rate, l.n, batch, a.t); 126 | } 127 | }else{ 128 | axpy_gpu(size, -decay*batch, l.weights_gpu, 1, l.weight_updates_gpu, 1); 129 | axpy_gpu(size, learning_rate/batch, l.weight_updates_gpu, 1, l.weights_gpu, 1); 130 | scal_gpu(size, momentum, l.weight_updates_gpu, 1); 131 | 132 | axpy_gpu(l.n, learning_rate/batch, l.bias_updates_gpu, 1, l.biases_gpu, 1); 133 | scal_gpu(l.n, momentum, l.bias_updates_gpu, 1); 134 | 135 | if(l.scales_gpu){ 136 | axpy_gpu(l.n, learning_rate/batch, l.scale_updates_gpu, 1, l.scales_gpu, 1); 137 | scal_gpu(l.n, momentum, l.scale_updates_gpu, 1); 138 | } 139 | } 140 | } 141 | 142 | -------------------------------------------------------------------------------- /lightnet/_darknet/deconvolutional_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DECONVOLUTIONAL_LAYER_H 2 | #define DECONVOLUTIONAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | #ifdef GPU 11 | void forward_deconvolutional_layer_gpu(layer l, network net); 12 | void backward_deconvolutional_layer_gpu(layer l, network net); 13 | void update_deconvolutional_layer_gpu(layer l, update_args a); 14 | void push_deconvolutional_layer(layer l); 15 | void pull_deconvolutional_layer(layer l); 16 | #endif 17 | 18 | layer make_deconvolutional_layer(int batch, int h, int w, int c, int n, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int adam); 19 | void resize_deconvolutional_layer(layer *l, int h, int w); 20 | void forward_deconvolutional_layer(const layer l, network net); 21 | void update_deconvolutional_layer(layer l, update_args a); 22 | void backward_deconvolutional_layer(layer l, network net); 23 | 24 | #endif 25 | 26 | -------------------------------------------------------------------------------- /lightnet/_darknet/demo.h: -------------------------------------------------------------------------------- 1 | #ifndef DEMO_H 2 | #define DEMO_H 3 | 4 | #include "image.h" 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /lightnet/_darknet/detection_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DETECTION_LAYER_H 2 | #define DETECTION_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | typedef layer detection_layer; 8 | 9 | detection_layer make_detection_layer(int batch, int inputs, int n, int size, int classes, int coords, int rescore); 10 | void forward_detection_layer(const detection_layer l, network net); 11 | void backward_detection_layer(const detection_layer l, network net); 12 | 13 | #ifdef GPU 14 | void forward_detection_layer_gpu(const detection_layer l, network net); 15 | void backward_detection_layer_gpu(detection_layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /lightnet/_darknet/dropout_layer.c: -------------------------------------------------------------------------------- 1 | #include "dropout_layer.h" 2 | #include "utils.h" 3 | #include "cuda.h" 4 | #include 5 | #include 6 | 7 | dropout_layer make_dropout_layer(int batch, int inputs, float probability) 8 | { 9 | dropout_layer l = {0}; 10 | l.type = DROPOUT; 11 | l.probability = probability; 12 | l.inputs = inputs; 13 | l.outputs = inputs; 14 | l.batch = batch; 15 | l.rand = calloc(inputs*batch, sizeof(float)); 16 | l.scale = 1./(1.-probability); 17 | l.forward = forward_dropout_layer; 18 | l.backward = backward_dropout_layer; 19 | #ifdef GPU 20 | l.forward_gpu = forward_dropout_layer_gpu; 21 | l.backward_gpu = backward_dropout_layer_gpu; 22 | l.rand_gpu = cuda_make_array(l.rand, inputs*batch); 23 | #endif 24 | fprintf(stderr, "dropout p = %.2f %4d -> %4d\n", probability, inputs, inputs); 25 | return l; 26 | } 27 | 28 | void resize_dropout_layer(dropout_layer *l, int inputs) 29 | { 30 | l->rand = realloc(l->rand, l->inputs*l->batch*sizeof(float)); 31 | #ifdef GPU 32 | cuda_free(l->rand_gpu); 33 | 34 | l->rand_gpu = cuda_make_array(l->rand, inputs*l->batch); 35 | #endif 36 | } 37 | 38 | void forward_dropout_layer(dropout_layer l, network net) 39 | { 40 | int i; 41 | if (!net.train) return; 42 | for(i = 0; i < l.batch * l.inputs; ++i){ 43 | float r = rand_uniform(0, 1); 44 | l.rand[i] = r; 45 | if(r < l.probability) net.input[i] = 0; 46 | else net.input[i] *= l.scale; 47 | } 48 | } 49 | 50 | void backward_dropout_layer(dropout_layer l, network net) 51 | { 52 | int i; 53 | if(!net.delta) return; 54 | for(i = 0; i < l.batch * l.inputs; ++i){ 55 | float r = l.rand[i]; 56 | if(r < l.probability) net.delta[i] = 0; 57 | else net.delta[i] *= l.scale; 58 | } 59 | } 60 | 61 | -------------------------------------------------------------------------------- /lightnet/_darknet/dropout_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DROPOUT_LAYER_H 2 | #define DROPOUT_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | typedef layer dropout_layer; 8 | 9 | dropout_layer make_dropout_layer(int batch, int inputs, float probability); 10 | 11 | void forward_dropout_layer(dropout_layer l, network net); 12 | void backward_dropout_layer(dropout_layer l, network net); 13 | void resize_dropout_layer(dropout_layer *l, int inputs); 14 | 15 | #ifdef GPU 16 | void forward_dropout_layer_gpu(dropout_layer l, network net); 17 | void backward_dropout_layer_gpu(dropout_layer l, network net); 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /lightnet/_darknet/dropout_layer_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "dropout_layer.h" 7 | #include "cuda.h" 8 | #include "utils.h" 9 | } 10 | 11 | __global__ void yoloswag420blazeit360noscope(float *input, int size, float *rand, float prob, float scale) 12 | { 13 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 14 | if(id < size) input[id] = (rand[id] < prob) ? 0 : input[id]*scale; 15 | } 16 | 17 | void forward_dropout_layer_gpu(dropout_layer layer, network net) 18 | { 19 | if (!net.train) return; 20 | int size = layer.inputs*layer.batch; 21 | cuda_random(layer.rand_gpu, size); 22 | /* 23 | int i; 24 | for(i = 0; i < size; ++i){ 25 | layer.rand[i] = rand_uniform(); 26 | } 27 | cuda_push_array(layer.rand_gpu, layer.rand, size); 28 | */ 29 | 30 | yoloswag420blazeit360noscope<<>>(net.input_gpu, size, layer.rand_gpu, layer.probability, layer.scale); 31 | check_error(cudaPeekAtLastError()); 32 | } 33 | 34 | void backward_dropout_layer_gpu(dropout_layer layer, network net) 35 | { 36 | if(!net.delta_gpu) return; 37 | int size = layer.inputs*layer.batch; 38 | 39 | yoloswag420blazeit360noscope<<>>(net.delta_gpu, size, layer.rand_gpu, layer.probability, layer.scale); 40 | check_error(cudaPeekAtLastError()); 41 | } 42 | -------------------------------------------------------------------------------- /lightnet/_darknet/gemm.h: -------------------------------------------------------------------------------- 1 | #ifndef GEMM_H 2 | #define GEMM_H 3 | 4 | void gemm_bin(int M, int N, int K, float ALPHA, 5 | char *A, int lda, 6 | float *B, int ldb, 7 | float *C, int ldc); 8 | 9 | void gemm(int TA, int TB, int M, int N, int K, float ALPHA, 10 | float *A, int lda, 11 | float *B, int ldb, 12 | float BETA, 13 | float *C, int ldc); 14 | 15 | void gemm_cpu(int TA, int TB, int M, int N, int K, float ALPHA, 16 | float *A, int lda, 17 | float *B, int ldb, 18 | float BETA, 19 | float *C, int ldc); 20 | 21 | 22 | #ifdef CBLAS 23 | void gemm_cblas(int TA, int TB, int M, int N, int K, float ALPHA, 24 | float *A, int lda, 25 | float *B, int ldb, 26 | float BETA, 27 | float *C, int ldc); 28 | #endif 29 | 30 | #ifdef GPU 31 | void gemm_gpu(int TA, int TB, int M, int N, int K, float ALPHA, 32 | float *A_gpu, int lda, 33 | float *B_gpu, int ldb, 34 | float BETA, 35 | float *C_gpu, int ldc); 36 | 37 | void gemm_gpu(int TA, int TB, int M, int N, int K, float ALPHA, 38 | float *A, int lda, 39 | float *B, int ldb, 40 | float BETA, 41 | float *C, int ldc); 42 | #endif 43 | #endif 44 | -------------------------------------------------------------------------------- /lightnet/_darknet/gru_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef GRU_LAYER_H 3 | #define GRU_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_gru_layer(int batch, int inputs, int outputs, int steps, int batch_normalize, int adam); 10 | 11 | void forward_gru_layer(layer l, network state); 12 | void backward_gru_layer(layer l, network state); 13 | void update_gru_layer(layer l, update_args a); 14 | 15 | #ifdef GPU 16 | void forward_gru_layer_gpu(layer l, network state); 17 | void backward_gru_layer_gpu(layer l, network state); 18 | void update_gru_layer_gpu(layer l, update_args a); 19 | void push_gru_layer(layer l); 20 | void pull_gru_layer(layer l); 21 | #endif 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /lightnet/_darknet/im2col.c: -------------------------------------------------------------------------------- 1 | #include "im2col.h" 2 | #include 3 | inline float im2col_get_pixel(float *im, int height, int width, int channels, 4 | int row, int col, int channel, int pad) 5 | { 6 | row -= pad; 7 | col -= pad; 8 | 9 | if (row < 0 || col < 0 || 10 | row >= height || col >= width) return 0; 11 | return im[col + width*(row + height*channel)]; 12 | } 13 | 14 | //From Berkeley Vision's Caffe! 15 | //https://github.com/BVLC/caffe/blob/master/LICENSE 16 | void im2col_cpu(float* data_im, 17 | int channels, int height, int width, 18 | int ksize, int stride, int pad, float* data_col) 19 | { 20 | int c,h,w; 21 | int height_col = (height + 2*pad - ksize) / stride + 1; 22 | int width_col = (width + 2*pad - ksize) / stride + 1; 23 | 24 | int channels_col = channels * ksize * ksize; 25 | for (c = 0; c < channels_col; ++c) { 26 | int w_offset = c % ksize; 27 | int h_offset = (c / ksize) % ksize; 28 | int c_im = c / ksize / ksize; 29 | for (h = 0; h < height_col; ++h) { 30 | for (w = 0; w < width_col; ++w) { 31 | int im_row = h_offset + h * stride; 32 | int im_col = w_offset + w * stride; 33 | int col_index = (c * height_col + h) * width_col + w; 34 | data_col[col_index] = im2col_get_pixel(data_im, height, width, channels, 35 | im_row, im_col, c_im, pad); 36 | } 37 | } 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /lightnet/_darknet/im2col.h: -------------------------------------------------------------------------------- 1 | #ifndef IM2COL_H 2 | #define IM2COL_H 3 | 4 | void im2col_cpu(float* data_im, 5 | int channels, int height, int width, 6 | int ksize, int stride, int pad, float* data_col); 7 | 8 | #ifdef GPU 9 | 10 | void im2col_gpu(float *im, 11 | int channels, int height, int width, 12 | int ksize, int stride, int pad,float *data_col); 13 | 14 | #endif 15 | #endif 16 | -------------------------------------------------------------------------------- /lightnet/_darknet/im2col_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "im2col.h" 7 | #include "cuda.h" 8 | } 9 | 10 | // src: https://github.com/BVLC/caffe/blob/master/src/caffe/util/im2col.cu 11 | // You may also want to read: https://github.com/BVLC/caffe/blob/master/LICENSE 12 | 13 | __global__ void im2col_gpu_kernel(const int n, const float* data_im, 14 | const int height, const int width, const int ksize, 15 | const int pad, 16 | const int stride, 17 | const int height_col, const int width_col, 18 | float *data_col) { 19 | int index = blockIdx.x*blockDim.x+threadIdx.x; 20 | for(; index < n; index += blockDim.x*gridDim.x){ 21 | int w_out = index % width_col; 22 | int h_index = index / width_col; 23 | int h_out = h_index % height_col; 24 | int channel_in = h_index / height_col; 25 | int channel_out = channel_in * ksize * ksize; 26 | int h_in = h_out * stride - pad; 27 | int w_in = w_out * stride - pad; 28 | float* data_col_ptr = data_col; 29 | data_col_ptr += (channel_out * height_col + h_out) * width_col + w_out; 30 | const float* data_im_ptr = data_im; 31 | data_im_ptr += (channel_in * height + h_in) * width + w_in; 32 | for (int i = 0; i < ksize; ++i) { 33 | for (int j = 0; j < ksize; ++j) { 34 | int h = h_in + i; 35 | int w = w_in + j; 36 | 37 | *data_col_ptr = (h >= 0 && w >= 0 && h < height && w < width) ? 38 | data_im_ptr[i * width + j] : 0; 39 | 40 | //*data_col_ptr = data_im_ptr[ii * width + jj]; 41 | 42 | data_col_ptr += height_col * width_col; 43 | } 44 | } 45 | } 46 | } 47 | 48 | void im2col_gpu(float *im, 49 | int channels, int height, int width, 50 | int ksize, int stride, int pad, float *data_col){ 51 | // We are going to launch channels * height_col * width_col kernels, each 52 | // kernel responsible for copying a single-channel grid. 53 | int height_col = (height + 2 * pad - ksize) / stride + 1; 54 | int width_col = (width + 2 * pad - ksize) / stride + 1; 55 | int num_kernels = channels * height_col * width_col; 56 | im2col_gpu_kernel<<<(num_kernels+BLOCK-1)/BLOCK, 57 | BLOCK>>>( 58 | num_kernels, im, height, width, ksize, pad, 59 | stride, height_col, 60 | width_col, data_col); 61 | } 62 | -------------------------------------------------------------------------------- /lightnet/_darknet/image.h: -------------------------------------------------------------------------------- 1 | #ifndef IMAGE_H 2 | #define IMAGE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "box.h" 10 | #include "darknet.h" 11 | 12 | #ifndef __cplusplus 13 | #ifdef OPENCV 14 | int fill_image_from_stream(CvCapture *cap, image im); 15 | image ipl_to_image(IplImage* src); 16 | void ipl_into_image(IplImage* src, image im); 17 | void flush_stream_buffer(CvCapture *cap, int n); 18 | void show_image_cv(image p, const char *name, IplImage *disp); 19 | #endif 20 | #endif 21 | 22 | float get_color(int c, int x, int max); 23 | void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b); 24 | void draw_bbox(image a, box bbox, int w, float r, float g, float b); 25 | void draw_label(image a, int r, int c, image label, const float *rgb); 26 | void write_label(image a, int r, int c, image *characters, char *string, float *rgb); 27 | image image_distance(image a, image b); 28 | void scale_image(image m, float s); 29 | image rotate_crop_image(image im, float rad, float s, int w, int h, float dx, float dy, float aspect); 30 | image center_crop_image(image im, int w, int h); 31 | image random_crop_image(image im, int w, int h); 32 | image random_augment_image(image im, float angle, float aspect, int low, int high, int w, int h); 33 | augment_args random_augment_args(image im, float angle, float aspect, int low, int high, int w, int h); 34 | void letterbox_image_into(image im, int w, int h, image boxed); 35 | image resize_max(image im, int max); 36 | void translate_image(image m, float s); 37 | void embed_image(image source, image dest, int dx, int dy); 38 | void place_image(image im, int w, int h, int dx, int dy, image canvas); 39 | void saturate_image(image im, float sat); 40 | void exposure_image(image im, float sat); 41 | void distort_image(image im, float hue, float sat, float val); 42 | void saturate_exposure_image(image im, float sat, float exposure); 43 | void rgb_to_hsv(image im); 44 | void hsv_to_rgb(image im); 45 | void yuv_to_rgb(image im); 46 | void rgb_to_yuv(image im); 47 | 48 | 49 | image collapse_image_layers(image source, int border); 50 | image collapse_images_horz(image *ims, int n); 51 | image collapse_images_vert(image *ims, int n); 52 | 53 | void show_image_normalized(image im, const char *name); 54 | void show_images(image *ims, int n, char *window); 55 | void show_image_layers(image p, char *name); 56 | void show_image_collapsed(image p, char *name); 57 | 58 | void print_image(image m); 59 | 60 | image make_empty_image(int w, int h, int c); 61 | void copy_image_into(image src, image dest); 62 | 63 | image get_image_layer(image m, int l); 64 | 65 | #endif 66 | 67 | -------------------------------------------------------------------------------- /lightnet/_darknet/layer.c: -------------------------------------------------------------------------------- 1 | #include "layer.h" 2 | #include "cuda.h" 3 | 4 | #include 5 | 6 | void free_layer(layer l) 7 | { 8 | if(l.type == DROPOUT){ 9 | if(l.rand) free(l.rand); 10 | #ifdef GPU 11 | if(l.rand_gpu) cuda_free(l.rand_gpu); 12 | #endif 13 | return; 14 | } 15 | if(l.cweights) free(l.cweights); 16 | if(l.indexes) free(l.indexes); 17 | if(l.input_layers) free(l.input_layers); 18 | if(l.input_sizes) free(l.input_sizes); 19 | if(l.map) free(l.map); 20 | if(l.rand) free(l.rand); 21 | if(l.cost) free(l.cost); 22 | if(l.state) free(l.state); 23 | if(l.prev_state) free(l.prev_state); 24 | if(l.forgot_state) free(l.forgot_state); 25 | if(l.forgot_delta) free(l.forgot_delta); 26 | if(l.state_delta) free(l.state_delta); 27 | if(l.concat) free(l.concat); 28 | if(l.concat_delta) free(l.concat_delta); 29 | if(l.binary_weights) free(l.binary_weights); 30 | if(l.biases) free(l.biases); 31 | if(l.bias_updates) free(l.bias_updates); 32 | if(l.scales) free(l.scales); 33 | if(l.scale_updates) free(l.scale_updates); 34 | if(l.weights) free(l.weights); 35 | if(l.weight_updates) free(l.weight_updates); 36 | if(l.delta) free(l.delta); 37 | if(l.output) free(l.output); 38 | if(l.squared) free(l.squared); 39 | if(l.norms) free(l.norms); 40 | if(l.spatial_mean) free(l.spatial_mean); 41 | if(l.mean) free(l.mean); 42 | if(l.variance) free(l.variance); 43 | if(l.mean_delta) free(l.mean_delta); 44 | if(l.variance_delta) free(l.variance_delta); 45 | if(l.rolling_mean) free(l.rolling_mean); 46 | if(l.rolling_variance) free(l.rolling_variance); 47 | if(l.x) free(l.x); 48 | if(l.x_norm) free(l.x_norm); 49 | if(l.m) free(l.m); 50 | if(l.v) free(l.v); 51 | if(l.z_cpu) free(l.z_cpu); 52 | if(l.r_cpu) free(l.r_cpu); 53 | if(l.h_cpu) free(l.h_cpu); 54 | if(l.binary_input) free(l.binary_input); 55 | 56 | #ifdef GPU 57 | if(l.indexes_gpu) cuda_free((float *)l.indexes_gpu); 58 | 59 | if(l.z_gpu) cuda_free(l.z_gpu); 60 | if(l.r_gpu) cuda_free(l.r_gpu); 61 | if(l.h_gpu) cuda_free(l.h_gpu); 62 | if(l.m_gpu) cuda_free(l.m_gpu); 63 | if(l.v_gpu) cuda_free(l.v_gpu); 64 | if(l.prev_state_gpu) cuda_free(l.prev_state_gpu); 65 | if(l.forgot_state_gpu) cuda_free(l.forgot_state_gpu); 66 | if(l.forgot_delta_gpu) cuda_free(l.forgot_delta_gpu); 67 | if(l.state_gpu) cuda_free(l.state_gpu); 68 | if(l.state_delta_gpu) cuda_free(l.state_delta_gpu); 69 | if(l.gate_gpu) cuda_free(l.gate_gpu); 70 | if(l.gate_delta_gpu) cuda_free(l.gate_delta_gpu); 71 | if(l.save_gpu) cuda_free(l.save_gpu); 72 | if(l.save_delta_gpu) cuda_free(l.save_delta_gpu); 73 | if(l.concat_gpu) cuda_free(l.concat_gpu); 74 | if(l.concat_delta_gpu) cuda_free(l.concat_delta_gpu); 75 | if(l.binary_input_gpu) cuda_free(l.binary_input_gpu); 76 | if(l.binary_weights_gpu) cuda_free(l.binary_weights_gpu); 77 | if(l.mean_gpu) cuda_free(l.mean_gpu); 78 | if(l.variance_gpu) cuda_free(l.variance_gpu); 79 | if(l.rolling_mean_gpu) cuda_free(l.rolling_mean_gpu); 80 | if(l.rolling_variance_gpu) cuda_free(l.rolling_variance_gpu); 81 | if(l.variance_delta_gpu) cuda_free(l.variance_delta_gpu); 82 | if(l.mean_delta_gpu) cuda_free(l.mean_delta_gpu); 83 | if(l.x_gpu) cuda_free(l.x_gpu); 84 | if(l.x_norm_gpu) cuda_free(l.x_norm_gpu); 85 | if(l.weights_gpu) cuda_free(l.weights_gpu); 86 | if(l.weight_updates_gpu) cuda_free(l.weight_updates_gpu); 87 | if(l.biases_gpu) cuda_free(l.biases_gpu); 88 | if(l.bias_updates_gpu) cuda_free(l.bias_updates_gpu); 89 | if(l.scales_gpu) cuda_free(l.scales_gpu); 90 | if(l.scale_updates_gpu) cuda_free(l.scale_updates_gpu); 91 | if(l.output_gpu) cuda_free(l.output_gpu); 92 | if(l.delta_gpu) cuda_free(l.delta_gpu); 93 | if(l.rand_gpu) cuda_free(l.rand_gpu); 94 | if(l.squared_gpu) cuda_free(l.squared_gpu); 95 | if(l.norms_gpu) cuda_free(l.norms_gpu); 96 | #endif 97 | } 98 | -------------------------------------------------------------------------------- /lightnet/_darknet/layer.h: -------------------------------------------------------------------------------- 1 | #include "darknet.h" 2 | -------------------------------------------------------------------------------- /lightnet/_darknet/list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "list.h" 4 | 5 | list *make_list() 6 | { 7 | list *l = malloc(sizeof(list)); 8 | l->size = 0; 9 | l->front = 0; 10 | l->back = 0; 11 | return l; 12 | } 13 | 14 | /* 15 | void transfer_node(list *s, list *d, node *n) 16 | { 17 | node *prev, *next; 18 | prev = n->prev; 19 | next = n->next; 20 | if(prev) prev->next = next; 21 | if(next) next->prev = prev; 22 | --s->size; 23 | if(s->front == n) s->front = next; 24 | if(s->back == n) s->back = prev; 25 | } 26 | */ 27 | 28 | void *list_pop(list *l){ 29 | if(!l->back) return 0; 30 | node *b = l->back; 31 | void *val = b->val; 32 | l->back = b->prev; 33 | if(l->back) l->back->next = 0; 34 | free(b); 35 | --l->size; 36 | 37 | return val; 38 | } 39 | 40 | void list_insert(list *l, void *val) 41 | { 42 | node *new = malloc(sizeof(node)); 43 | new->val = val; 44 | new->next = 0; 45 | 46 | if(!l->back){ 47 | l->front = new; 48 | new->prev = 0; 49 | }else{ 50 | l->back->next = new; 51 | new->prev = l->back; 52 | } 53 | l->back = new; 54 | ++l->size; 55 | } 56 | 57 | void free_node(node *n) 58 | { 59 | node *next; 60 | while(n) { 61 | next = n->next; 62 | free(n); 63 | n = next; 64 | } 65 | } 66 | 67 | void free_list(list *l) 68 | { 69 | free_node(l->front); 70 | free(l); 71 | } 72 | 73 | void free_list_contents(list *l) 74 | { 75 | node *n = l->front; 76 | while(n){ 77 | free(n->val); 78 | n = n->next; 79 | } 80 | } 81 | 82 | void **list_to_array(list *l) 83 | { 84 | void **a = calloc(l->size, sizeof(void*)); 85 | int count = 0; 86 | node *n = l->front; 87 | while(n){ 88 | a[count++] = n->val; 89 | n = n->next; 90 | } 91 | return a; 92 | } 93 | -------------------------------------------------------------------------------- /lightnet/_darknet/list.h: -------------------------------------------------------------------------------- 1 | #ifndef LIST_H 2 | #define LIST_H 3 | #include "darknet.h" 4 | 5 | list *make_list(); 6 | int list_find(list *l, void *val); 7 | 8 | void list_insert(list *, void *); 9 | 10 | 11 | void free_list_contents(list *l); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /lightnet/_darknet/local_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef LOCAL_LAYER_H 2 | #define LOCAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | typedef layer local_layer; 11 | 12 | #ifdef GPU 13 | void forward_local_layer_gpu(local_layer layer, network net); 14 | void backward_local_layer_gpu(local_layer layer, network net); 15 | void update_local_layer_gpu(local_layer layer, update_args a); 16 | 17 | void push_local_layer(local_layer layer); 18 | void pull_local_layer(local_layer layer); 19 | #endif 20 | 21 | local_layer make_local_layer(int batch, int h, int w, int c, int n, int size, int stride, int pad, ACTIVATION activation); 22 | 23 | void forward_local_layer(const local_layer layer, network net); 24 | void backward_local_layer(local_layer layer, network net); 25 | void update_local_layer(local_layer layer, update_args a); 26 | 27 | void bias_output(float *output, float *biases, int batch, int n, int size); 28 | void backward_bias(float *bias_updates, float *delta, int batch, int n, int size); 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /lightnet/_darknet/lstm_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef LSTM_LAYER_H 2 | #define LSTM_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | #define USET 8 | 9 | layer make_lstm_layer(int batch, int inputs, int outputs, int steps, int batch_normalize, int adam); 10 | 11 | void forward_lstm_layer(layer l, network net); 12 | void update_lstm_layer(layer l, update_args a); 13 | 14 | #ifdef GPU 15 | void forward_lstm_layer_gpu(layer l, network net); 16 | void backward_lstm_layer_gpu(layer l, network net); 17 | void update_lstm_layer_gpu(layer l, update_args a); 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /lightnet/_darknet/matrix.c: -------------------------------------------------------------------------------- 1 | #include "matrix.h" 2 | #include "utils.h" 3 | #include "blas.h" 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | void free_matrix(matrix m) 11 | { 12 | int i; 13 | for(i = 0; i < m.rows; ++i) free(m.vals[i]); 14 | free(m.vals); 15 | } 16 | 17 | float matrix_topk_accuracy(matrix truth, matrix guess, int k) 18 | { 19 | int *indexes = calloc(k, sizeof(int)); 20 | int n = truth.cols; 21 | int i,j; 22 | int correct = 0; 23 | for(i = 0; i < truth.rows; ++i){ 24 | top_k(guess.vals[i], n, k, indexes); 25 | for(j = 0; j < k; ++j){ 26 | int class = indexes[j]; 27 | if(truth.vals[i][class]){ 28 | ++correct; 29 | break; 30 | } 31 | } 32 | } 33 | free(indexes); 34 | return (float)correct/truth.rows; 35 | } 36 | 37 | void scale_matrix(matrix m, float scale) 38 | { 39 | int i,j; 40 | for(i = 0; i < m.rows; ++i){ 41 | for(j = 0; j < m.cols; ++j){ 42 | m.vals[i][j] *= scale; 43 | } 44 | } 45 | } 46 | 47 | matrix resize_matrix(matrix m, int size) 48 | { 49 | int i; 50 | if (m.rows == size) return m; 51 | if (m.rows < size) { 52 | m.vals = realloc(m.vals, size*sizeof(float*)); 53 | for (i = m.rows; i < size; ++i) { 54 | m.vals[i] = calloc(m.cols, sizeof(float)); 55 | } 56 | } else if (m.rows > size) { 57 | for (i = size; i < m.rows; ++i) { 58 | free(m.vals[i]); 59 | } 60 | m.vals = realloc(m.vals, size*sizeof(float*)); 61 | } 62 | m.rows = size; 63 | return m; 64 | } 65 | 66 | void matrix_add_matrix(matrix from, matrix to) 67 | { 68 | assert(from.rows == to.rows && from.cols == to.cols); 69 | int i,j; 70 | for(i = 0; i < from.rows; ++i){ 71 | for(j = 0; j < from.cols; ++j){ 72 | to.vals[i][j] += from.vals[i][j]; 73 | } 74 | } 75 | } 76 | 77 | matrix copy_matrix(matrix m) 78 | { 79 | matrix c = {0}; 80 | c.rows = m.rows; 81 | c.cols = m.cols; 82 | c.vals = calloc(c.rows, sizeof(float *)); 83 | int i; 84 | for(i = 0; i < c.rows; ++i){ 85 | c.vals[i] = calloc(c.cols, sizeof(float)); 86 | copy_cpu(c.cols, m.vals[i], 1, c.vals[i], 1); 87 | } 88 | return c; 89 | } 90 | 91 | matrix make_matrix(int rows, int cols) 92 | { 93 | int i; 94 | matrix m; 95 | m.rows = rows; 96 | m.cols = cols; 97 | m.vals = calloc(m.rows, sizeof(float *)); 98 | for(i = 0; i < m.rows; ++i){ 99 | m.vals[i] = calloc(m.cols, sizeof(float)); 100 | } 101 | return m; 102 | } 103 | 104 | matrix hold_out_matrix(matrix *m, int n) 105 | { 106 | int i; 107 | matrix h; 108 | h.rows = n; 109 | h.cols = m->cols; 110 | h.vals = calloc(h.rows, sizeof(float *)); 111 | for(i = 0; i < n; ++i){ 112 | int index = rand()%m->rows; 113 | h.vals[i] = m->vals[index]; 114 | m->vals[index] = m->vals[--(m->rows)]; 115 | } 116 | return h; 117 | } 118 | 119 | float *pop_column(matrix *m, int c) 120 | { 121 | float *col = calloc(m->rows, sizeof(float)); 122 | int i, j; 123 | for(i = 0; i < m->rows; ++i){ 124 | col[i] = m->vals[i][c]; 125 | for(j = c; j < m->cols-1; ++j){ 126 | m->vals[i][j] = m->vals[i][j+1]; 127 | } 128 | } 129 | --m->cols; 130 | return col; 131 | } 132 | 133 | matrix csv_to_matrix(char *filename) 134 | { 135 | FILE *fp = fopen(filename, "r"); 136 | if(!fp) file_error(filename); 137 | 138 | matrix m; 139 | m.cols = -1; 140 | 141 | char *line; 142 | 143 | int n = 0; 144 | int size = 1024; 145 | m.vals = calloc(size, sizeof(float*)); 146 | while((line = fgetl(fp))){ 147 | if(m.cols == -1) m.cols = count_fields(line); 148 | if(n == size){ 149 | size *= 2; 150 | m.vals = realloc(m.vals, size*sizeof(float*)); 151 | } 152 | m.vals[n] = parse_fields(line, m.cols); 153 | free(line); 154 | ++n; 155 | } 156 | m.vals = realloc(m.vals, n*sizeof(float*)); 157 | m.rows = n; 158 | return m; 159 | } 160 | 161 | void matrix_to_csv(matrix m) 162 | { 163 | int i, j; 164 | 165 | for(i = 0; i < m.rows; ++i){ 166 | for(j = 0; j < m.cols; ++j){ 167 | if(j > 0) printf(","); 168 | printf("%.17g", m.vals[i][j]); 169 | } 170 | printf("\n"); 171 | } 172 | } 173 | 174 | void print_matrix(matrix m) 175 | { 176 | int i, j; 177 | printf("%d X %d Matrix:\n",m.rows, m.cols); 178 | printf(" __"); 179 | for(j = 0; j < 16*m.cols-1; ++j) printf(" "); 180 | printf("__ \n"); 181 | 182 | printf("| "); 183 | for(j = 0; j < 16*m.cols-1; ++j) printf(" "); 184 | printf(" |\n"); 185 | 186 | for(i = 0; i < m.rows; ++i){ 187 | printf("| "); 188 | for(j = 0; j < m.cols; ++j){ 189 | printf("%15.7f ", m.vals[i][j]); 190 | } 191 | printf(" |\n"); 192 | } 193 | printf("|__"); 194 | for(j = 0; j < 16*m.cols-1; ++j) printf(" "); 195 | printf("__|\n"); 196 | } 197 | -------------------------------------------------------------------------------- /lightnet/_darknet/matrix.h: -------------------------------------------------------------------------------- 1 | #ifndef MATRIX_H 2 | #define MATRIX_H 3 | #include "darknet.h" 4 | 5 | matrix copy_matrix(matrix m); 6 | void print_matrix(matrix m); 7 | 8 | matrix hold_out_matrix(matrix *m, int n); 9 | matrix resize_matrix(matrix m, int size); 10 | 11 | float *pop_column(matrix *m, int c); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /lightnet/_darknet/maxpool_layer.c: -------------------------------------------------------------------------------- 1 | #include "maxpool_layer.h" 2 | #include "cuda.h" 3 | #include 4 | 5 | image get_maxpool_image(maxpool_layer l) 6 | { 7 | int h = l.out_h; 8 | int w = l.out_w; 9 | int c = l.c; 10 | return float_to_image(w,h,c,l.output); 11 | } 12 | 13 | image get_maxpool_delta(maxpool_layer l) 14 | { 15 | int h = l.out_h; 16 | int w = l.out_w; 17 | int c = l.c; 18 | return float_to_image(w,h,c,l.delta); 19 | } 20 | 21 | maxpool_layer make_maxpool_layer(int batch, int h, int w, int c, int size, int stride, int padding) 22 | { 23 | maxpool_layer l = {0}; 24 | l.type = MAXPOOL; 25 | l.batch = batch; 26 | l.h = h; 27 | l.w = w; 28 | l.c = c; 29 | l.pad = padding; 30 | l.out_w = (w + 2*padding)/stride; 31 | l.out_h = (h + 2*padding)/stride; 32 | l.out_c = c; 33 | l.outputs = l.out_h * l.out_w * l.out_c; 34 | l.inputs = h*w*c; 35 | l.size = size; 36 | l.stride = stride; 37 | int output_size = l.out_h * l.out_w * l.out_c * batch; 38 | l.indexes = calloc(output_size, sizeof(int)); 39 | l.output = calloc(output_size, sizeof(float)); 40 | l.delta = calloc(output_size, sizeof(float)); 41 | l.forward = forward_maxpool_layer; 42 | l.backward = backward_maxpool_layer; 43 | #ifdef GPU 44 | l.forward_gpu = forward_maxpool_layer_gpu; 45 | l.backward_gpu = backward_maxpool_layer_gpu; 46 | l.indexes_gpu = cuda_make_int_array(0, output_size); 47 | l.output_gpu = cuda_make_array(l.output, output_size); 48 | l.delta_gpu = cuda_make_array(l.delta, output_size); 49 | #endif 50 | //fprintf(stderr, "max %d x %d / %d %4d x%4d x%4d -> %4d x%4d x%4d\n", size, size, stride, w, h, c, l.out_w, l.out_h, l.out_c); 51 | return l; 52 | } 53 | 54 | void resize_maxpool_layer(maxpool_layer *l, int w, int h) 55 | { 56 | l->h = h; 57 | l->w = w; 58 | l->inputs = h*w*l->c; 59 | 60 | l->out_w = (w + 2*l->pad)/l->stride; 61 | l->out_h = (h + 2*l->pad)/l->stride; 62 | l->outputs = l->out_w * l->out_h * l->c; 63 | int output_size = l->outputs * l->batch; 64 | 65 | l->indexes = realloc(l->indexes, output_size * sizeof(int)); 66 | l->output = realloc(l->output, output_size * sizeof(float)); 67 | l->delta = realloc(l->delta, output_size * sizeof(float)); 68 | 69 | #ifdef GPU 70 | cuda_free((float *)l->indexes_gpu); 71 | cuda_free(l->output_gpu); 72 | cuda_free(l->delta_gpu); 73 | l->indexes_gpu = cuda_make_int_array(0, output_size); 74 | l->output_gpu = cuda_make_array(l->output, output_size); 75 | l->delta_gpu = cuda_make_array(l->delta, output_size); 76 | #endif 77 | } 78 | 79 | void forward_maxpool_layer(const maxpool_layer l, network net) 80 | { 81 | int b,i,j,k,m,n; 82 | int w_offset = -l.pad; 83 | int h_offset = -l.pad; 84 | 85 | int h = l.out_h; 86 | int w = l.out_w; 87 | int c = l.c; 88 | 89 | for(b = 0; b < l.batch; ++b){ 90 | for(k = 0; k < c; ++k){ 91 | for(i = 0; i < h; ++i){ 92 | for(j = 0; j < w; ++j){ 93 | int out_index = j + w*(i + h*(k + c*b)); 94 | float max = -FLT_MAX; 95 | int max_i = -1; 96 | for(n = 0; n < l.size; ++n){ 97 | for(m = 0; m < l.size; ++m){ 98 | int cur_h = h_offset + i*l.stride + n; 99 | int cur_w = w_offset + j*l.stride + m; 100 | int index = cur_w + l.w*(cur_h + l.h*(k + b*l.c)); 101 | int valid = (cur_h >= 0 && cur_h < l.h && 102 | cur_w >= 0 && cur_w < l.w); 103 | float val = (valid != 0) ? net.input[index] : -FLT_MAX; 104 | max_i = (val > max) ? index : max_i; 105 | max = (val > max) ? val : max; 106 | } 107 | } 108 | l.output[out_index] = max; 109 | l.indexes[out_index] = max_i; 110 | } 111 | } 112 | } 113 | } 114 | } 115 | 116 | void backward_maxpool_layer(const maxpool_layer l, network net) 117 | { 118 | int i; 119 | int h = l.out_h; 120 | int w = l.out_w; 121 | int c = l.c; 122 | for(i = 0; i < h*w*c*l.batch; ++i){ 123 | int index = l.indexes[i]; 124 | net.delta[index] += l.delta[i]; 125 | } 126 | } 127 | 128 | -------------------------------------------------------------------------------- /lightnet/_darknet/maxpool_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef MAXPOOL_LAYER_H 2 | #define MAXPOOL_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | typedef layer maxpool_layer; 10 | 11 | image get_maxpool_image(maxpool_layer l); 12 | maxpool_layer make_maxpool_layer(int batch, int h, int w, int c, int size, int stride, int padding); 13 | void resize_maxpool_layer(maxpool_layer *l, int w, int h); 14 | void forward_maxpool_layer(const maxpool_layer l, network net); 15 | void backward_maxpool_layer(const maxpool_layer l, network net); 16 | 17 | #ifdef GPU 18 | void forward_maxpool_layer_gpu(maxpool_layer l, network net); 19 | void backward_maxpool_layer_gpu(maxpool_layer l, network net); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /lightnet/_darknet/maxpool_layer_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "maxpool_layer.h" 7 | #include "cuda.h" 8 | } 9 | 10 | __global__ void forward_maxpool_layer_kernel(int n, int in_h, int in_w, int in_c, int stride, int size, int pad, float *input, float *output, int *indexes) 11 | { 12 | int h = (in_h + 2*pad)/stride; 13 | int w = (in_w + 2*pad)/stride; 14 | int c = in_c; 15 | 16 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 17 | if(id >= n) return; 18 | 19 | int j = id % w; 20 | id /= w; 21 | int i = id % h; 22 | id /= h; 23 | int k = id % c; 24 | id /= c; 25 | int b = id; 26 | 27 | int w_offset = -pad; 28 | int h_offset = -pad; 29 | 30 | int out_index = j + w*(i + h*(k + c*b)); 31 | float max = -INFINITY; 32 | int max_i = -1; 33 | int l, m; 34 | for(l = 0; l < size; ++l){ 35 | for(m = 0; m < size; ++m){ 36 | int cur_h = h_offset + i*stride + l; 37 | int cur_w = w_offset + j*stride + m; 38 | int index = cur_w + in_w*(cur_h + in_h*(k + b*in_c)); 39 | int valid = (cur_h >= 0 && cur_h < in_h && 40 | cur_w >= 0 && cur_w < in_w); 41 | float val = (valid != 0) ? input[index] : -INFINITY; 42 | max_i = (val > max) ? index : max_i; 43 | max = (val > max) ? val : max; 44 | } 45 | } 46 | output[out_index] = max; 47 | indexes[out_index] = max_i; 48 | } 49 | 50 | __global__ void backward_maxpool_layer_kernel(int n, int in_h, int in_w, int in_c, int stride, int size, int pad, float *delta, float *prev_delta, int *indexes) 51 | { 52 | int h = (in_h + 2*pad)/stride; 53 | int w = (in_w + 2*pad)/stride; 54 | int c = in_c; 55 | int area = (size-1)/stride; 56 | 57 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 58 | if(id >= n) return; 59 | 60 | int index = id; 61 | int j = id % in_w; 62 | id /= in_w; 63 | int i = id % in_h; 64 | id /= in_h; 65 | int k = id % in_c; 66 | id /= in_c; 67 | int b = id; 68 | 69 | int w_offset = -pad; 70 | int h_offset = -pad; 71 | 72 | float d = 0; 73 | int l, m; 74 | for(l = -area; l < area+1; ++l){ 75 | for(m = -area; m < area+1; ++m){ 76 | int out_w = (j-w_offset)/stride + m; 77 | int out_h = (i-h_offset)/stride + l; 78 | int out_index = out_w + w*(out_h + h*(k + c*b)); 79 | int valid = (out_w >= 0 && out_w < w && 80 | out_h >= 0 && out_h < h); 81 | d += (valid && indexes[out_index] == index) ? delta[out_index] : 0; 82 | } 83 | } 84 | prev_delta[index] += d; 85 | } 86 | 87 | extern "C" void forward_maxpool_layer_gpu(maxpool_layer layer, network net) 88 | { 89 | int h = layer.out_h; 90 | int w = layer.out_w; 91 | int c = layer.c; 92 | 93 | size_t n = h*w*c*layer.batch; 94 | 95 | forward_maxpool_layer_kernel<<>>(n, layer.h, layer.w, layer.c, layer.stride, layer.size, layer.pad, net.input_gpu, layer.output_gpu, layer.indexes_gpu); 96 | check_error(cudaPeekAtLastError()); 97 | } 98 | 99 | extern "C" void backward_maxpool_layer_gpu(maxpool_layer layer, network net) 100 | { 101 | size_t n = layer.h*layer.w*layer.c*layer.batch; 102 | 103 | backward_maxpool_layer_kernel<<>>(n, layer.h, layer.w, layer.c, layer.stride, layer.size, layer.pad, layer.delta_gpu, net.delta_gpu, layer.indexes_gpu); 104 | check_error(cudaPeekAtLastError()); 105 | } 106 | 107 | -------------------------------------------------------------------------------- /lightnet/_darknet/network.h: -------------------------------------------------------------------------------- 1 | // Oh boy, why am I about to do this.... 2 | #ifndef NETWORK_H 3 | #define NETWORK_H 4 | #include "darknet.h" 5 | 6 | #include "image.h" 7 | #include "layer.h" 8 | #include "data.h" 9 | #include "tree.h" 10 | 11 | 12 | #ifdef GPU 13 | void pull_network_output(network *net); 14 | #endif 15 | 16 | void compare_networks(network *n1, network *n2, data d); 17 | char *get_layer_string(LAYER_TYPE a); 18 | 19 | network *make_network(int n); 20 | 21 | 22 | float network_accuracy_multi(network *net, data d, int n); 23 | int get_predicted_class_network(network *net); 24 | void print_network(network *net); 25 | int resize_network(network *net, int w, int h); 26 | void calc_network_cost(network *net); 27 | 28 | float **make_probs(network *net); 29 | #endif 30 | 31 | -------------------------------------------------------------------------------- /lightnet/_darknet/normalization_layer.c: -------------------------------------------------------------------------------- 1 | #include "normalization_layer.h" 2 | #include "blas.h" 3 | 4 | #include 5 | 6 | layer make_normalization_layer(int batch, int w, int h, int c, int size, float alpha, float beta, float kappa) 7 | { 8 | fprintf(stderr, "Local Response Normalization Layer: %d x %d x %d image, %d size\n", w,h,c,size); 9 | layer layer = {0}; 10 | layer.type = NORMALIZATION; 11 | layer.batch = batch; 12 | layer.h = layer.out_h = h; 13 | layer.w = layer.out_w = w; 14 | layer.c = layer.out_c = c; 15 | layer.kappa = kappa; 16 | layer.size = size; 17 | layer.alpha = alpha; 18 | layer.beta = beta; 19 | layer.output = calloc(h * w * c * batch, sizeof(float)); 20 | layer.delta = calloc(h * w * c * batch, sizeof(float)); 21 | layer.squared = calloc(h * w * c * batch, sizeof(float)); 22 | layer.norms = calloc(h * w * c * batch, sizeof(float)); 23 | layer.inputs = w*h*c; 24 | layer.outputs = layer.inputs; 25 | 26 | layer.forward = forward_normalization_layer; 27 | layer.backward = backward_normalization_layer; 28 | #ifdef GPU 29 | layer.forward_gpu = forward_normalization_layer_gpu; 30 | layer.backward_gpu = backward_normalization_layer_gpu; 31 | 32 | layer.output_gpu = cuda_make_array(layer.output, h * w * c * batch); 33 | layer.delta_gpu = cuda_make_array(layer.delta, h * w * c * batch); 34 | layer.squared_gpu = cuda_make_array(layer.squared, h * w * c * batch); 35 | layer.norms_gpu = cuda_make_array(layer.norms, h * w * c * batch); 36 | #endif 37 | return layer; 38 | } 39 | 40 | void resize_normalization_layer(layer *layer, int w, int h) 41 | { 42 | int c = layer->c; 43 | int batch = layer->batch; 44 | layer->h = h; 45 | layer->w = w; 46 | layer->out_h = h; 47 | layer->out_w = w; 48 | layer->inputs = w*h*c; 49 | layer->outputs = layer->inputs; 50 | layer->output = realloc(layer->output, h * w * c * batch * sizeof(float)); 51 | layer->delta = realloc(layer->delta, h * w * c * batch * sizeof(float)); 52 | layer->squared = realloc(layer->squared, h * w * c * batch * sizeof(float)); 53 | layer->norms = realloc(layer->norms, h * w * c * batch * sizeof(float)); 54 | #ifdef GPU 55 | cuda_free(layer->output_gpu); 56 | cuda_free(layer->delta_gpu); 57 | cuda_free(layer->squared_gpu); 58 | cuda_free(layer->norms_gpu); 59 | layer->output_gpu = cuda_make_array(layer->output, h * w * c * batch); 60 | layer->delta_gpu = cuda_make_array(layer->delta, h * w * c * batch); 61 | layer->squared_gpu = cuda_make_array(layer->squared, h * w * c * batch); 62 | layer->norms_gpu = cuda_make_array(layer->norms, h * w * c * batch); 63 | #endif 64 | } 65 | 66 | void forward_normalization_layer(const layer layer, network net) 67 | { 68 | int k,b; 69 | int w = layer.w; 70 | int h = layer.h; 71 | int c = layer.c; 72 | scal_cpu(w*h*c*layer.batch, 0, layer.squared, 1); 73 | 74 | for(b = 0; b < layer.batch; ++b){ 75 | float *squared = layer.squared + w*h*c*b; 76 | float *norms = layer.norms + w*h*c*b; 77 | float *input = net.input + w*h*c*b; 78 | pow_cpu(w*h*c, 2, input, 1, squared, 1); 79 | 80 | const_cpu(w*h, layer.kappa, norms, 1); 81 | for(k = 0; k < layer.size/2; ++k){ 82 | axpy_cpu(w*h, layer.alpha, squared + w*h*k, 1, norms, 1); 83 | } 84 | 85 | for(k = 1; k < layer.c; ++k){ 86 | copy_cpu(w*h, norms + w*h*(k-1), 1, norms + w*h*k, 1); 87 | int prev = k - ((layer.size-1)/2) - 1; 88 | int next = k + (layer.size/2); 89 | if(prev >= 0) axpy_cpu(w*h, -layer.alpha, squared + w*h*prev, 1, norms + w*h*k, 1); 90 | if(next < layer.c) axpy_cpu(w*h, layer.alpha, squared + w*h*next, 1, norms + w*h*k, 1); 91 | } 92 | } 93 | pow_cpu(w*h*c*layer.batch, -layer.beta, layer.norms, 1, layer.output, 1); 94 | mul_cpu(w*h*c*layer.batch, net.input, 1, layer.output, 1); 95 | } 96 | 97 | void backward_normalization_layer(const layer layer, network net) 98 | { 99 | // TODO This is approximate ;-) 100 | // Also this should add in to delta instead of overwritting. 101 | 102 | int w = layer.w; 103 | int h = layer.h; 104 | int c = layer.c; 105 | pow_cpu(w*h*c*layer.batch, -layer.beta, layer.norms, 1, net.delta, 1); 106 | mul_cpu(w*h*c*layer.batch, layer.delta, 1, net.delta, 1); 107 | } 108 | 109 | #ifdef GPU 110 | void forward_normalization_layer_gpu(const layer layer, network net) 111 | { 112 | int k,b; 113 | int w = layer.w; 114 | int h = layer.h; 115 | int c = layer.c; 116 | scal_gpu(w*h*c*layer.batch, 0, layer.squared_gpu, 1); 117 | 118 | for(b = 0; b < layer.batch; ++b){ 119 | float *squared = layer.squared_gpu + w*h*c*b; 120 | float *norms = layer.norms_gpu + w*h*c*b; 121 | float *input = net.input_gpu + w*h*c*b; 122 | pow_gpu(w*h*c, 2, input, 1, squared, 1); 123 | 124 | const_gpu(w*h, layer.kappa, norms, 1); 125 | for(k = 0; k < layer.size/2; ++k){ 126 | axpy_gpu(w*h, layer.alpha, squared + w*h*k, 1, norms, 1); 127 | } 128 | 129 | for(k = 1; k < layer.c; ++k){ 130 | copy_gpu(w*h, norms + w*h*(k-1), 1, norms + w*h*k, 1); 131 | int prev = k - ((layer.size-1)/2) - 1; 132 | int next = k + (layer.size/2); 133 | if(prev >= 0) axpy_gpu(w*h, -layer.alpha, squared + w*h*prev, 1, norms + w*h*k, 1); 134 | if(next < layer.c) axpy_gpu(w*h, layer.alpha, squared + w*h*next, 1, norms + w*h*k, 1); 135 | } 136 | } 137 | pow_gpu(w*h*c*layer.batch, -layer.beta, layer.norms_gpu, 1, layer.output_gpu, 1); 138 | mul_gpu(w*h*c*layer.batch, net.input_gpu, 1, layer.output_gpu, 1); 139 | } 140 | 141 | void backward_normalization_layer_gpu(const layer layer, network net) 142 | { 143 | // TODO This is approximate ;-) 144 | 145 | int w = layer.w; 146 | int h = layer.h; 147 | int c = layer.c; 148 | pow_gpu(w*h*c*layer.batch, -layer.beta, layer.norms_gpu, 1, net.delta_gpu, 1); 149 | mul_gpu(w*h*c*layer.batch, layer.delta_gpu, 1, net.delta_gpu, 1); 150 | } 151 | #endif 152 | -------------------------------------------------------------------------------- /lightnet/_darknet/normalization_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef NORMALIZATION_LAYER_H 2 | #define NORMALIZATION_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_normalization_layer(int batch, int w, int h, int c, int size, float alpha, float beta, float kappa); 9 | void resize_normalization_layer(layer *layer, int h, int w); 10 | void forward_normalization_layer(const layer layer, network net); 11 | void backward_normalization_layer(const layer layer, network net); 12 | void visualize_normalization_layer(layer layer, char *window); 13 | 14 | #ifdef GPU 15 | void forward_normalization_layer_gpu(const layer layer, network net); 16 | void backward_normalization_layer_gpu(const layer layer, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /lightnet/_darknet/option_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "option_list.h" 5 | #include "utils.h" 6 | 7 | list *read_data_cfg(char *filename) 8 | { 9 | FILE *file = fopen(filename, "r"); 10 | if(file == 0) file_error(filename); 11 | char *line; 12 | int nu = 0; 13 | list *options = make_list(); 14 | while((line=fgetl(file)) != 0){ 15 | ++ nu; 16 | strip(line); 17 | switch(line[0]){ 18 | case '\0': 19 | case '#': 20 | case ';': 21 | free(line); 22 | break; 23 | default: 24 | if(!read_option(line, options)){ 25 | fprintf(stderr, "Config file error line %d, could parse: %s\n", nu, line); 26 | free(line); 27 | } 28 | break; 29 | } 30 | } 31 | fclose(file); 32 | return options; 33 | } 34 | 35 | metadata get_metadata(char *file) 36 | { 37 | metadata m = {0}; 38 | list *options = read_data_cfg(file); 39 | 40 | char *name_list = option_find_str(options, "names", 0); 41 | if(!name_list) name_list = option_find_str(options, "labels", 0); 42 | if(!name_list) { 43 | fprintf(stderr, "No names or labels found\n"); 44 | } else { 45 | m.names = get_labels(name_list); 46 | } 47 | m.classes = option_find_int(options, "classes", 2); 48 | free_list(options); 49 | return m; 50 | } 51 | 52 | int read_option(char *s, list *options) 53 | { 54 | size_t i; 55 | size_t len = strlen(s); 56 | char *val = 0; 57 | for(i = 0; i < len; ++i){ 58 | if(s[i] == '='){ 59 | s[i] = '\0'; 60 | val = s+i+1; 61 | break; 62 | } 63 | } 64 | if(i == len-1) return 0; 65 | char *key = s; 66 | option_insert(options, key, val); 67 | return 1; 68 | } 69 | 70 | void option_insert(list *l, char *key, char *val) 71 | { 72 | kvp *p = malloc(sizeof(kvp)); 73 | p->key = key; 74 | p->val = val; 75 | p->used = 0; 76 | list_insert(l, p); 77 | } 78 | 79 | void option_unused(list *l) 80 | { 81 | node *n = l->front; 82 | while(n){ 83 | kvp *p = (kvp *)n->val; 84 | if(!p->used){ 85 | fprintf(stderr, "Unused field: '%s = %s'\n", p->key, p->val); 86 | } 87 | n = n->next; 88 | } 89 | } 90 | 91 | char *option_find(list *l, char *key) 92 | { 93 | node *n = l->front; 94 | while(n){ 95 | kvp *p = (kvp *)n->val; 96 | if(strcmp(p->key, key) == 0){ 97 | p->used = 1; 98 | return p->val; 99 | } 100 | n = n->next; 101 | } 102 | return 0; 103 | } 104 | char *option_find_str(list *l, char *key, char *def) 105 | { 106 | char *v = option_find(l, key); 107 | if(v) return v; 108 | //if(def) fprintf(stderr, "%s: Using default '%s'\n", key, def); 109 | return def; 110 | } 111 | 112 | int option_find_int(list *l, char *key, int def) 113 | { 114 | char *v = option_find(l, key); 115 | if(v) return atoi(v); 116 | //fprintf(stderr, "%s: Using default '%d'\n", key, def); 117 | return def; 118 | } 119 | 120 | int option_find_int_quiet(list *l, char *key, int def) 121 | { 122 | char *v = option_find(l, key); 123 | if(v) return atoi(v); 124 | return def; 125 | } 126 | 127 | float option_find_float_quiet(list *l, char *key, float def) 128 | { 129 | char *v = option_find(l, key); 130 | if(v) return atof(v); 131 | return def; 132 | } 133 | 134 | float option_find_float(list *l, char *key, float def) 135 | { 136 | char *v = option_find(l, key); 137 | if(v) return atof(v); 138 | //fprintf(stderr, "%s: Using default '%lf'\n", key, def); 139 | return def; 140 | } 141 | -------------------------------------------------------------------------------- /lightnet/_darknet/option_list.h: -------------------------------------------------------------------------------- 1 | #ifndef OPTION_LIST_H 2 | #define OPTION_LIST_H 3 | #include "list.h" 4 | 5 | typedef struct{ 6 | char *key; 7 | char *val; 8 | int used; 9 | } kvp; 10 | 11 | 12 | int read_option(char *s, list *options); 13 | void option_insert(list *l, char *key, char *val); 14 | char *option_find(list *l, char *key); 15 | int option_find_int_quiet(list *l, char *key, int def); 16 | float option_find_float(list *l, char *key, float def); 17 | float option_find_float_quiet(list *l, char *key, float def); 18 | void option_unused(list *l); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /lightnet/_darknet/parser.h: -------------------------------------------------------------------------------- 1 | #ifndef PARSER_H 2 | #define PARSER_H 3 | #include "darknet.h" 4 | #include "network.h" 5 | 6 | void save_network(network net, char *filename); 7 | void save_weights_double(network net, char *filename); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /lightnet/_darknet/region_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef REGION_LAYER_H 2 | #define REGION_LAYER_H 3 | 4 | #include "darknet.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_region_layer(int batch, int h, int w, int n, int classes, int coords); 9 | void forward_region_layer(const layer l, network net); 10 | void backward_region_layer(const layer l, network net); 11 | void resize_region_layer(layer *l, int w, int h); 12 | 13 | #ifdef GPU 14 | void forward_region_layer_gpu(const layer l, network net); 15 | void backward_region_layer_gpu(layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /lightnet/_darknet/reorg_layer.c: -------------------------------------------------------------------------------- 1 | #include "reorg_layer.h" 2 | #include "cuda.h" 3 | #include "blas.h" 4 | 5 | #include 6 | 7 | 8 | layer make_reorg_layer(int batch, int w, int h, int c, int stride, int reverse, int flatten, int extra) 9 | { 10 | layer l = {0}; 11 | l.type = REORG; 12 | l.batch = batch; 13 | l.stride = stride; 14 | l.extra = extra; 15 | l.h = h; 16 | l.w = w; 17 | l.c = c; 18 | l.flatten = flatten; 19 | if(reverse){ 20 | l.out_w = w*stride; 21 | l.out_h = h*stride; 22 | l.out_c = c/(stride*stride); 23 | }else{ 24 | l.out_w = w/stride; 25 | l.out_h = h/stride; 26 | l.out_c = c*(stride*stride); 27 | } 28 | l.reverse = reverse; 29 | 30 | l.outputs = l.out_h * l.out_w * l.out_c; 31 | l.inputs = h*w*c; 32 | if(l.extra){ 33 | l.out_w = l.out_h = l.out_c = 0; 34 | l.outputs = l.inputs + l.extra; 35 | } 36 | 37 | int output_size = l.outputs * batch; 38 | l.output = calloc(output_size, sizeof(float)); 39 | l.delta = calloc(output_size, sizeof(float)); 40 | 41 | l.forward = forward_reorg_layer; 42 | l.backward = backward_reorg_layer; 43 | #ifdef GPU 44 | l.forward_gpu = forward_reorg_layer_gpu; 45 | l.backward_gpu = backward_reorg_layer_gpu; 46 | 47 | l.output_gpu = cuda_make_array(l.output, output_size); 48 | l.delta_gpu = cuda_make_array(l.delta, output_size); 49 | #endif 50 | return l; 51 | } 52 | 53 | void resize_reorg_layer(layer *l, int w, int h) 54 | { 55 | int stride = l->stride; 56 | int c = l->c; 57 | 58 | l->h = h; 59 | l->w = w; 60 | 61 | if(l->reverse){ 62 | l->out_w = w*stride; 63 | l->out_h = h*stride; 64 | l->out_c = c/(stride*stride); 65 | }else{ 66 | l->out_w = w/stride; 67 | l->out_h = h/stride; 68 | l->out_c = c*(stride*stride); 69 | } 70 | 71 | l->outputs = l->out_h * l->out_w * l->out_c; 72 | l->inputs = l->outputs; 73 | int output_size = l->outputs * l->batch; 74 | 75 | l->output = realloc(l->output, output_size * sizeof(float)); 76 | l->delta = realloc(l->delta, output_size * sizeof(float)); 77 | 78 | #ifdef GPU 79 | cuda_free(l->output_gpu); 80 | cuda_free(l->delta_gpu); 81 | l->output_gpu = cuda_make_array(l->output, output_size); 82 | l->delta_gpu = cuda_make_array(l->delta, output_size); 83 | #endif 84 | } 85 | 86 | void forward_reorg_layer(const layer l, network net) 87 | { 88 | int i; 89 | if(l.flatten){ 90 | memcpy(l.output, net.input, l.outputs*l.batch*sizeof(float)); 91 | if(l.reverse){ 92 | flatten(l.output, l.w*l.h, l.c, l.batch, 0); 93 | }else{ 94 | flatten(l.output, l.w*l.h, l.c, l.batch, 1); 95 | } 96 | } else if (l.extra) { 97 | for(i = 0; i < l.batch; ++i){ 98 | copy_cpu(l.inputs, net.input + i*l.inputs, 1, l.output + i*l.outputs, 1); 99 | } 100 | } else if (l.reverse){ 101 | reorg_cpu(net.input, l.w, l.h, l.c, l.batch, l.stride, 1, l.output); 102 | } else { 103 | reorg_cpu(net.input, l.w, l.h, l.c, l.batch, l.stride, 0, l.output); 104 | } 105 | } 106 | 107 | void backward_reorg_layer(const layer l, network net) 108 | { 109 | int i; 110 | if(l.flatten){ 111 | memcpy(net.delta, l.delta, l.outputs*l.batch*sizeof(float)); 112 | if(l.reverse){ 113 | flatten(net.delta, l.w*l.h, l.c, l.batch, 1); 114 | }else{ 115 | flatten(net.delta, l.w*l.h, l.c, l.batch, 0); 116 | } 117 | } else if(l.reverse){ 118 | reorg_cpu(l.delta, l.w, l.h, l.c, l.batch, l.stride, 0, net.delta); 119 | } else if (l.extra) { 120 | for(i = 0; i < l.batch; ++i){ 121 | copy_cpu(l.inputs, l.delta + i*l.outputs, 1, net.delta + i*l.inputs, 1); 122 | } 123 | }else{ 124 | reorg_cpu(l.delta, l.w, l.h, l.c, l.batch, l.stride, 1, net.delta); 125 | } 126 | } 127 | 128 | #ifdef GPU 129 | void forward_reorg_layer_gpu(layer l, network net) 130 | { 131 | int i; 132 | if(l.flatten){ 133 | if(l.reverse){ 134 | flatten_gpu(net.input_gpu, l.w*l.h, l.c, l.batch, 0, l.output_gpu); 135 | }else{ 136 | flatten_gpu(net.input_gpu, l.w*l.h, l.c, l.batch, 1, l.output_gpu); 137 | } 138 | } else if (l.extra) { 139 | for(i = 0; i < l.batch; ++i){ 140 | copy_gpu(l.inputs, net.input_gpu + i*l.inputs, 1, l.output_gpu + i*l.outputs, 1); 141 | } 142 | } else if (l.reverse) { 143 | reorg_gpu(net.input_gpu, l.w, l.h, l.c, l.batch, l.stride, 1, l.output_gpu); 144 | }else { 145 | reorg_gpu(net.input_gpu, l.w, l.h, l.c, l.batch, l.stride, 0, l.output_gpu); 146 | } 147 | } 148 | 149 | void backward_reorg_layer_gpu(layer l, network net) 150 | { 151 | if(l.flatten){ 152 | if(l.reverse){ 153 | flatten_gpu(l.delta_gpu, l.w*l.h, l.c, l.batch, 1, net.delta_gpu); 154 | }else{ 155 | flatten_gpu(l.delta_gpu, l.w*l.h, l.c, l.batch, 0, net.delta_gpu); 156 | } 157 | } else if (l.extra) { 158 | int i; 159 | for(i = 0; i < l.batch; ++i){ 160 | copy_gpu(l.inputs, l.delta_gpu + i*l.outputs, 1, net.delta_gpu + i*l.inputs, 1); 161 | } 162 | } else if(l.reverse){ 163 | reorg_gpu(l.delta_gpu, l.w, l.h, l.c, l.batch, l.stride, 0, net.delta_gpu); 164 | } else { 165 | reorg_gpu(l.delta_gpu, l.w, l.h, l.c, l.batch, l.stride, 1, net.delta_gpu); 166 | } 167 | } 168 | #endif 169 | -------------------------------------------------------------------------------- /lightnet/_darknet/reorg_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef REORG_LAYER_H 2 | #define REORG_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_reorg_layer(int batch, int w, int h, int c, int stride, int reverse, int flatten, int extra); 10 | void resize_reorg_layer(layer *l, int w, int h); 11 | void forward_reorg_layer(const layer l, network net); 12 | void backward_reorg_layer(const layer l, network net); 13 | 14 | #ifdef GPU 15 | void forward_reorg_layer_gpu(layer l, network net); 16 | void backward_reorg_layer_gpu(layer l, network net); 17 | #endif 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /lightnet/_darknet/rnn_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef RNN_LAYER_H 3 | #define RNN_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | #define USET 9 | 10 | layer make_rnn_layer(int batch, int inputs, int outputs, int steps, ACTIVATION activation, int batch_normalize, int adam); 11 | 12 | void forward_rnn_layer(layer l, network net); 13 | void backward_rnn_layer(layer l, network net); 14 | void update_rnn_layer(layer l, update_args a); 15 | 16 | #ifdef GPU 17 | void forward_rnn_layer_gpu(layer l, network net); 18 | void backward_rnn_layer_gpu(layer l, network net); 19 | void update_rnn_layer_gpu(layer l, update_args a); 20 | void push_rnn_layer(layer l); 21 | void pull_rnn_layer(layer l); 22 | #endif 23 | 24 | #endif 25 | 26 | -------------------------------------------------------------------------------- /lightnet/_darknet/route_layer.c: -------------------------------------------------------------------------------- 1 | #include "route_layer.h" 2 | #include "cuda.h" 3 | #include "blas.h" 4 | 5 | #include 6 | 7 | route_layer make_route_layer(int batch, int n, int *input_layers, int *input_sizes) 8 | { 9 | //fprintf(stderr,"route "); 10 | route_layer l = {0}; 11 | l.type = ROUTE; 12 | l.batch = batch; 13 | l.n = n; 14 | l.input_layers = input_layers; 15 | l.input_sizes = input_sizes; 16 | int i; 17 | int outputs = 0; 18 | for(i = 0; i < n; ++i){ 19 | //fprintf(stderr," %d", input_layers[i]); 20 | outputs += input_sizes[i]; 21 | } 22 | //fprintf(stderr, "\n"); 23 | l.outputs = outputs; 24 | l.inputs = outputs; 25 | l.delta = calloc(outputs*batch, sizeof(float)); 26 | l.output = calloc(outputs*batch, sizeof(float));; 27 | 28 | l.forward = forward_route_layer; 29 | l.backward = backward_route_layer; 30 | #ifdef GPU 31 | l.forward_gpu = forward_route_layer_gpu; 32 | l.backward_gpu = backward_route_layer_gpu; 33 | 34 | l.delta_gpu = cuda_make_array(l.delta, outputs*batch); 35 | l.output_gpu = cuda_make_array(l.output, outputs*batch); 36 | #endif 37 | return l; 38 | } 39 | 40 | void resize_route_layer(route_layer *l, network *net) 41 | { 42 | int i; 43 | layer first = net->layers[l->input_layers[0]]; 44 | l->out_w = first.out_w; 45 | l->out_h = first.out_h; 46 | l->out_c = first.out_c; 47 | l->outputs = first.outputs; 48 | l->input_sizes[0] = first.outputs; 49 | for(i = 1; i < l->n; ++i){ 50 | int index = l->input_layers[i]; 51 | layer next = net->layers[index]; 52 | l->outputs += next.outputs; 53 | l->input_sizes[i] = next.outputs; 54 | if(next.out_w == first.out_w && next.out_h == first.out_h){ 55 | l->out_c += next.out_c; 56 | }else{ 57 | //printf("%d %d, %d %d\n", next.out_w, next.out_h, first.out_w, first.out_h); 58 | l->out_h = l->out_w = l->out_c = 0; 59 | } 60 | } 61 | l->inputs = l->outputs; 62 | l->delta = realloc(l->delta, l->outputs*l->batch*sizeof(float)); 63 | l->output = realloc(l->output, l->outputs*l->batch*sizeof(float)); 64 | 65 | #ifdef GPU 66 | cuda_free(l->output_gpu); 67 | cuda_free(l->delta_gpu); 68 | l->output_gpu = cuda_make_array(l->output, l->outputs*l->batch); 69 | l->delta_gpu = cuda_make_array(l->delta, l->outputs*l->batch); 70 | #endif 71 | 72 | } 73 | 74 | void forward_route_layer(const route_layer l, network net) 75 | { 76 | int i, j; 77 | int offset = 0; 78 | for(i = 0; i < l.n; ++i){ 79 | int index = l.input_layers[i]; 80 | float *input = net.layers[index].output; 81 | int input_size = l.input_sizes[i]; 82 | for(j = 0; j < l.batch; ++j){ 83 | copy_cpu(input_size, input + j*input_size, 1, l.output + offset + j*l.outputs, 1); 84 | } 85 | offset += input_size; 86 | } 87 | } 88 | 89 | void backward_route_layer(const route_layer l, network net) 90 | { 91 | int i, j; 92 | int offset = 0; 93 | for(i = 0; i < l.n; ++i){ 94 | int index = l.input_layers[i]; 95 | float *delta = net.layers[index].delta; 96 | int input_size = l.input_sizes[i]; 97 | for(j = 0; j < l.batch; ++j){ 98 | axpy_cpu(input_size, 1, l.delta + offset + j*l.outputs, 1, delta + j*input_size, 1); 99 | } 100 | offset += input_size; 101 | } 102 | } 103 | 104 | #ifdef GPU 105 | void forward_route_layer_gpu(const route_layer l, network net) 106 | { 107 | int i, j; 108 | int offset = 0; 109 | for(i = 0; i < l.n; ++i){ 110 | int index = l.input_layers[i]; 111 | float *input = net.layers[index].output_gpu; 112 | int input_size = l.input_sizes[i]; 113 | for(j = 0; j < l.batch; ++j){ 114 | copy_gpu(input_size, input + j*input_size, 1, l.output_gpu + offset + j*l.outputs, 1); 115 | } 116 | offset += input_size; 117 | } 118 | } 119 | 120 | void backward_route_layer_gpu(const route_layer l, network net) 121 | { 122 | int i, j; 123 | int offset = 0; 124 | for(i = 0; i < l.n; ++i){ 125 | int index = l.input_layers[i]; 126 | float *delta = net.layers[index].delta_gpu; 127 | int input_size = l.input_sizes[i]; 128 | for(j = 0; j < l.batch; ++j){ 129 | axpy_gpu(input_size, 1, l.delta_gpu + offset + j*l.outputs, 1, delta + j*input_size, 1); 130 | } 131 | offset += input_size; 132 | } 133 | } 134 | #endif 135 | -------------------------------------------------------------------------------- /lightnet/_darknet/route_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef ROUTE_LAYER_H 2 | #define ROUTE_LAYER_H 3 | #include "network.h" 4 | #include "layer.h" 5 | 6 | typedef layer route_layer; 7 | 8 | route_layer make_route_layer(int batch, int n, int *input_layers, int *input_size); 9 | void forward_route_layer(const route_layer l, network net); 10 | void backward_route_layer(const route_layer l, network net); 11 | void resize_route_layer(route_layer *l, network *net); 12 | 13 | #ifdef GPU 14 | void forward_route_layer_gpu(const route_layer l, network net); 15 | void backward_route_layer_gpu(const route_layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /lightnet/_darknet/shortcut_layer.c: -------------------------------------------------------------------------------- 1 | #include "shortcut_layer.h" 2 | #include "cuda.h" 3 | #include "blas.h" 4 | #include "activations.h" 5 | 6 | #include 7 | #include 8 | 9 | layer make_shortcut_layer(int batch, int index, int w, int h, int c, int w2, int h2, int c2) 10 | { 11 | fprintf(stderr,"Shortcut Layer: %d\n", index); 12 | layer l = {0}; 13 | l.type = SHORTCUT; 14 | l.batch = batch; 15 | l.w = w2; 16 | l.h = h2; 17 | l.c = c2; 18 | l.out_w = w; 19 | l.out_h = h; 20 | l.out_c = c; 21 | l.outputs = w*h*c; 22 | l.inputs = l.outputs; 23 | 24 | l.index = index; 25 | 26 | l.delta = calloc(l.outputs*batch, sizeof(float)); 27 | l.output = calloc(l.outputs*batch, sizeof(float));; 28 | 29 | l.forward = forward_shortcut_layer; 30 | l.backward = backward_shortcut_layer; 31 | #ifdef GPU 32 | l.forward_gpu = forward_shortcut_layer_gpu; 33 | l.backward_gpu = backward_shortcut_layer_gpu; 34 | 35 | l.delta_gpu = cuda_make_array(l.delta, l.outputs*batch); 36 | l.output_gpu = cuda_make_array(l.output, l.outputs*batch); 37 | #endif 38 | return l; 39 | } 40 | 41 | void forward_shortcut_layer(const layer l, network net) 42 | { 43 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 44 | shortcut_cpu(l.batch, l.w, l.h, l.c, net.layers[l.index].output, l.out_w, l.out_h, l.out_c, l.output); 45 | activate_array(l.output, l.outputs*l.batch, l.activation); 46 | } 47 | 48 | void backward_shortcut_layer(const layer l, network net) 49 | { 50 | gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta); 51 | axpy_cpu(l.outputs*l.batch, 1, l.delta, 1, net.delta, 1); 52 | shortcut_cpu(l.batch, l.out_w, l.out_h, l.out_c, l.delta, l.w, l.h, l.c, net.layers[l.index].delta); 53 | } 54 | 55 | #ifdef GPU 56 | void forward_shortcut_layer_gpu(const layer l, network net) 57 | { 58 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 59 | shortcut_gpu(l.batch, l.w, l.h, l.c, net.layers[l.index].output_gpu, l.out_w, l.out_h, l.out_c, l.output_gpu); 60 | activate_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation); 61 | } 62 | 63 | void backward_shortcut_layer_gpu(const layer l, network net) 64 | { 65 | gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu); 66 | axpy_gpu(l.outputs*l.batch, 1, l.delta_gpu, 1, net.delta_gpu, 1); 67 | shortcut_gpu(l.batch, l.out_w, l.out_h, l.out_c, l.delta_gpu, l.w, l.h, l.c, net.layers[l.index].delta_gpu); 68 | } 69 | #endif 70 | -------------------------------------------------------------------------------- /lightnet/_darknet/shortcut_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef SHORTCUT_LAYER_H 2 | #define SHORTCUT_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | layer make_shortcut_layer(int batch, int index, int w, int h, int c, int w2, int h2, int c2); 8 | void forward_shortcut_layer(const layer l, network net); 9 | void backward_shortcut_layer(const layer l, network net); 10 | 11 | #ifdef GPU 12 | void forward_shortcut_layer_gpu(const layer l, network net); 13 | void backward_shortcut_layer_gpu(const layer l, network net); 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /lightnet/_darknet/softmax_layer.c: -------------------------------------------------------------------------------- 1 | #include "softmax_layer.h" 2 | #include "blas.h" 3 | #include "cuda.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | softmax_layer make_softmax_layer(int batch, int inputs, int groups) 12 | { 13 | assert(inputs%groups == 0); 14 | fprintf(stderr, "softmax %4d\n", inputs); 15 | softmax_layer l = {0}; 16 | l.type = SOFTMAX; 17 | l.batch = batch; 18 | l.groups = groups; 19 | l.inputs = inputs; 20 | l.outputs = inputs; 21 | l.output = calloc(inputs*batch, sizeof(float)); 22 | l.delta = calloc(inputs*batch, sizeof(float)); 23 | 24 | l.forward = forward_softmax_layer; 25 | l.backward = backward_softmax_layer; 26 | #ifdef GPU 27 | l.forward_gpu = forward_softmax_layer_gpu; 28 | l.backward_gpu = backward_softmax_layer_gpu; 29 | 30 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 31 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 32 | #endif 33 | return l; 34 | } 35 | 36 | void forward_softmax_layer(const softmax_layer l, network net) 37 | { 38 | if(l.softmax_tree){ 39 | int i; 40 | int count = 0; 41 | for (i = 0; i < l.softmax_tree->groups; ++i) { 42 | int group_size = l.softmax_tree->group_size[i]; 43 | softmax_cpu(net.input + count, group_size, l.batch, l.inputs, 1, 0, 1, l.temperature, l.output + count); 44 | count += group_size; 45 | } 46 | } else { 47 | softmax_cpu(net.input, l.inputs/l.groups, l.batch, l.inputs, l.groups, l.inputs/l.groups, 1, l.temperature, l.output); 48 | } 49 | } 50 | 51 | void backward_softmax_layer(const softmax_layer l, network net) 52 | { 53 | axpy_cpu(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1); 54 | } 55 | 56 | #ifdef GPU 57 | 58 | void pull_softmax_layer_output(const softmax_layer layer) 59 | { 60 | cuda_pull_array(layer.output_gpu, layer.output, layer.inputs*layer.batch); 61 | } 62 | 63 | void forward_softmax_layer_gpu(const softmax_layer l, network net) 64 | { 65 | if(l.softmax_tree){ 66 | int i; 67 | int count = 0; 68 | for (i = 0; i < l.softmax_tree->groups; ++i) { 69 | int group_size = l.softmax_tree->group_size[i]; 70 | softmax_gpu(net.input_gpu + count, group_size, l.batch, l.inputs, 1, 0, 1, l.temperature, l.output_gpu + count); 71 | count += group_size; 72 | } 73 | } else { 74 | if(l.spatial){ 75 | softmax_gpu(net.input_gpu, l.c, l.batch*l.c, l.inputs/l.c, l.w*l.h, 1, l.w*l.h, 1, l.output_gpu); 76 | }else{ 77 | softmax_gpu(net.input_gpu, l.inputs/l.groups, l.batch, l.inputs, l.groups, l.inputs/l.groups, 1, l.temperature, l.output_gpu); 78 | } 79 | } 80 | } 81 | 82 | void backward_softmax_layer_gpu(const softmax_layer layer, network net) 83 | { 84 | axpy_gpu(layer.batch*layer.inputs, 1, layer.delta_gpu, 1, net.delta_gpu, 1); 85 | } 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /lightnet/_darknet/softmax_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef SOFTMAX_LAYER_H 2 | #define SOFTMAX_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | typedef layer softmax_layer; 7 | 8 | void softmax_array(float *input, int n, float temp, float *output); 9 | softmax_layer make_softmax_layer(int batch, int inputs, int groups); 10 | void forward_softmax_layer(const softmax_layer l, network net); 11 | void backward_softmax_layer(const softmax_layer l, network net); 12 | 13 | #ifdef GPU 14 | void pull_softmax_layer_output(const softmax_layer l); 15 | void forward_softmax_layer_gpu(const softmax_layer l, network net); 16 | void backward_softmax_layer_gpu(const softmax_layer l, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /lightnet/_darknet/tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "tree.h" 4 | #include "utils.h" 5 | #include "data.h" 6 | 7 | void change_leaves(tree *t, char *leaf_list) 8 | { 9 | list *llist = get_paths(leaf_list); 10 | char **leaves = (char **)list_to_array(llist); 11 | int n = llist->size; 12 | int i,j; 13 | int found = 0; 14 | for(i = 0; i < t->n; ++i){ 15 | t->leaf[i] = 0; 16 | for(j = 0; j < n; ++j){ 17 | if (0==strcmp(t->name[i], leaves[j])){ 18 | t->leaf[i] = 1; 19 | ++found; 20 | break; 21 | } 22 | } 23 | } 24 | fprintf(stderr, "Found %d leaves.\n", found); 25 | } 26 | 27 | float get_hierarchy_probability(float *x, tree *hier, int c, int stride) 28 | { 29 | float p = 1; 30 | while(c >= 0){ 31 | p = p * x[c*stride]; 32 | c = hier->parent[c]; 33 | } 34 | return p; 35 | } 36 | 37 | void hierarchy_predictions(float *predictions, int n, tree *hier, int only_leaves, int stride) 38 | { 39 | int j; 40 | for(j = 0; j < n; ++j){ 41 | int parent = hier->parent[j]; 42 | if(parent >= 0){ 43 | predictions[j*stride] *= predictions[parent*stride]; 44 | } 45 | } 46 | if(only_leaves){ 47 | for(j = 0; j < n; ++j){ 48 | if(!hier->leaf[j]) predictions[j*stride] = 0; 49 | } 50 | } 51 | } 52 | 53 | int hierarchy_top_prediction(float *predictions, tree *hier, float thresh, int stride) 54 | { 55 | float p = 1; 56 | int group = 0; 57 | int i; 58 | while(1){ 59 | float max = 0; 60 | int max_i = 0; 61 | 62 | for(i = 0; i < hier->group_size[group]; ++i){ 63 | int index = i + hier->group_offset[group]; 64 | float val = predictions[(i + hier->group_offset[group])*stride]; 65 | if(val > max){ 66 | max_i = index; 67 | max = val; 68 | } 69 | } 70 | if(p*max > thresh){ 71 | p = p*max; 72 | group = hier->child[max_i]; 73 | if(hier->child[max_i] < 0) return max_i; 74 | } else if (group == 0){ 75 | return max_i; 76 | } else { 77 | return hier->parent[hier->group_offset[group]]; 78 | } 79 | } 80 | return 0; 81 | } 82 | 83 | tree *read_tree(char *filename) 84 | { 85 | tree t = {0}; 86 | FILE *fp = fopen(filename, "r"); 87 | 88 | char *line; 89 | int last_parent = -1; 90 | int group_size = 0; 91 | int groups = 0; 92 | int n = 0; 93 | while((line=fgetl(fp)) != 0){ 94 | char *id = calloc(256, sizeof(char)); 95 | int parent = -1; 96 | sscanf(line, "%s %d", id, &parent); 97 | t.parent = realloc(t.parent, (n+1)*sizeof(int)); 98 | t.parent[n] = parent; 99 | 100 | t.child = realloc(t.child, (n+1)*sizeof(int)); 101 | t.child[n] = -1; 102 | 103 | t.name = realloc(t.name, (n+1)*sizeof(char *)); 104 | t.name[n] = id; 105 | if(parent != last_parent){ 106 | ++groups; 107 | t.group_offset = realloc(t.group_offset, groups * sizeof(int)); 108 | t.group_offset[groups - 1] = n - group_size; 109 | t.group_size = realloc(t.group_size, groups * sizeof(int)); 110 | t.group_size[groups - 1] = group_size; 111 | group_size = 0; 112 | last_parent = parent; 113 | } 114 | t.group = realloc(t.group, (n+1)*sizeof(int)); 115 | t.group[n] = groups; 116 | if (parent >= 0) { 117 | t.child[parent] = groups; 118 | } 119 | ++n; 120 | ++group_size; 121 | } 122 | ++groups; 123 | t.group_offset = realloc(t.group_offset, groups * sizeof(int)); 124 | t.group_offset[groups - 1] = n - group_size; 125 | t.group_size = realloc(t.group_size, groups * sizeof(int)); 126 | t.group_size[groups - 1] = group_size; 127 | t.n = n; 128 | t.groups = groups; 129 | t.leaf = calloc(n, sizeof(int)); 130 | int i; 131 | for(i = 0; i < n; ++i) t.leaf[i] = 1; 132 | for(i = 0; i < n; ++i) if(t.parent[i] >= 0) t.leaf[t.parent[i]] = 0; 133 | 134 | fclose(fp); 135 | tree *tree_ptr = calloc(1, sizeof(tree)); 136 | *tree_ptr = t; 137 | //error(0); 138 | return tree_ptr; 139 | } 140 | -------------------------------------------------------------------------------- /lightnet/_darknet/tree.h: -------------------------------------------------------------------------------- 1 | #ifndef TREE_H 2 | #define TREE_H 3 | #include "darknet.h" 4 | 5 | tree *read_tree(char *filename); 6 | int hierarchy_top_prediction(float *predictions, tree *hier, float thresh, int stride); 7 | float get_hierarchy_probability(float *x, tree *hier, int c, int stride); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /lightnet/_darknet/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | #include 4 | #include 5 | #include "darknet.h" 6 | #include "list.h" 7 | 8 | #define TIME(a) \ 9 | do { \ 10 | double start = what_time_is_it_now(); \ 11 | a; \ 12 | printf("%s took: %f seconds\n", #a, what_time_is_it_now() - start); \ 13 | } while (0) 14 | 15 | #define TWO_PI 6.2831853071795864769252866f 16 | 17 | double what_time_is_it_now(); 18 | void shuffle(void *arr, size_t n, size_t size); 19 | void sorta_shuffle(void *arr, size_t n, size_t size, size_t sections); 20 | void free_ptrs(void **ptrs, int n); 21 | int alphanum_to_int(char c); 22 | char int_to_alphanum(int i); 23 | int read_int(int fd); 24 | void write_int(int fd, int n); 25 | void read_all(int fd, char *buffer, size_t bytes); 26 | void write_all(int fd, char *buffer, size_t bytes); 27 | int read_all_fail(int fd, char *buffer, size_t bytes); 28 | int write_all_fail(int fd, char *buffer, size_t bytes); 29 | void find_replace(char *str, char *orig, char *rep, char *output); 30 | void malloc_error(); 31 | void file_error(char *s); 32 | void strip(char *s); 33 | void strip_char(char *s, char bad); 34 | list *split_str(char *s, char delim); 35 | char *fgetl(FILE *fp); 36 | list *parse_csv_line(char *line); 37 | char *copy_string(char *s); 38 | int count_fields(char *line); 39 | float *parse_fields(char *line, int n); 40 | void scale_array(float *a, int n, float s); 41 | void translate_array(float *a, int n, float s); 42 | float constrain(float min, float max, float a); 43 | int constrain_int(int a, int min, int max); 44 | float rand_uniform(float min, float max); 45 | float rand_scale(float s); 46 | int rand_int(int min, int max); 47 | void mean_arrays(float **a, int n, int els, float *avg); 48 | float dist_array(float *a, float *b, int n, int sub); 49 | float **one_hot_encode(float *a, int n, int k); 50 | float sec(clock_t clocks); 51 | void print_statistics(float *a, int n); 52 | 53 | #endif 54 | 55 | -------------------------------------------------------------------------------- /lightnet/about.py: -------------------------------------------------------------------------------- 1 | __title__ = 'lightnet' 2 | __version__ = '0.0.13' 3 | __summary__ = "Bringing pjreddie's DarkNet out of the shadows" 4 | __uri__ = 'https://explosion.ai' 5 | __author__ = 'Explosion AI' 6 | __email__ = 'contact@explosion.ai' 7 | __license__ = 'MIT' 8 | -------------------------------------------------------------------------------- /lightnet/cli.py: -------------------------------------------------------------------------------- 1 | # coding: utf8 2 | from __future__ import unicode_literals 3 | 4 | import plac 5 | import requests 6 | import os 7 | import sys 8 | from tqdm import tqdm 9 | from pathlib import Path 10 | 11 | 12 | model_paths = { 13 | 'yolo': 'https://pjreddie.com/media/files/yolo.weights', 14 | 'tiny-yolo': 'https://pjreddie.com/media/files/tiny-yolo.weights', 15 | } 16 | 17 | @plac.annotations( 18 | model=("model to download, shortcut or name)", "positional", None, str), 19 | direct=("force direct download from URL", "flag", "d", bool)) 20 | def download(cmd, model, direct=False): 21 | """ 22 | Download model from default download path. Models: tiny-yolo, yolo. 23 | """ 24 | if direct: 25 | url = model 26 | name = model.split('/')[-1] 27 | else: 28 | url = model_paths[model] 29 | name = model + '.weights' 30 | out_loc = Path(__file__).parent / 'data' / name 31 | download_file(url, out_loc) 32 | 33 | 34 | def download_file(url, path): 35 | r = requests.get(url, stream=True) 36 | total_size = int(r.headers.get('content-length', 0)) 37 | with Path(path).open('wb') as file_: 38 | with tqdm(total=total_size//1024, unit_scale=True, unit="K") as pbar: 39 | for data in r.iter_content(32*1024): 40 | file_.write(data) 41 | pbar.update(32) 42 | -------------------------------------------------------------------------------- /lightnet/data/alexnet.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=128 3 | subdivisions=1 4 | height=227 5 | width=227 6 | channels=3 7 | momentum=0.9 8 | decay=0.0005 9 | max_crop=256 10 | 11 | learning_rate=0.01 12 | policy=poly 13 | power=4 14 | max_batches=800000 15 | 16 | angle=7 17 | hue = .1 18 | saturation=.75 19 | exposure=.75 20 | aspect=.75 21 | 22 | [convolutional] 23 | filters=96 24 | size=11 25 | stride=4 26 | pad=0 27 | activation=relu 28 | 29 | [maxpool] 30 | size=3 31 | stride=2 32 | padding=0 33 | 34 | [convolutional] 35 | filters=256 36 | size=5 37 | stride=1 38 | pad=1 39 | activation=relu 40 | 41 | [maxpool] 42 | size=3 43 | stride=2 44 | padding=0 45 | 46 | [convolutional] 47 | filters=384 48 | size=3 49 | stride=1 50 | pad=1 51 | activation=relu 52 | 53 | [convolutional] 54 | filters=384 55 | size=3 56 | stride=1 57 | pad=1 58 | activation=relu 59 | 60 | [convolutional] 61 | filters=256 62 | size=3 63 | stride=1 64 | pad=1 65 | activation=relu 66 | 67 | [maxpool] 68 | size=3 69 | stride=2 70 | padding=0 71 | 72 | [connected] 73 | output=4096 74 | activation=relu 75 | 76 | [dropout] 77 | probability=.5 78 | 79 | [connected] 80 | output=4096 81 | activation=relu 82 | 83 | [dropout] 84 | probability=.5 85 | 86 | [connected] 87 | output=1000 88 | activation=linear 89 | 90 | [softmax] 91 | groups=1 92 | 93 | [cost] 94 | type=sse 95 | 96 | -------------------------------------------------------------------------------- /lightnet/data/cifar.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=128 3 | subdivisions=1 4 | height=28 5 | width=28 6 | channels=3 7 | max_crop=32 8 | min_crop=32 9 | 10 | hue=.1 11 | saturation=.75 12 | exposure=.75 13 | 14 | learning_rate=0.4 15 | policy=poly 16 | power=4 17 | max_batches = 5000 18 | momentum=0.9 19 | decay=0.0005 20 | 21 | 22 | [convolutional] 23 | batch_normalize=1 24 | filters=128 25 | size=3 26 | stride=1 27 | pad=1 28 | activation=leaky 29 | 30 | [convolutional] 31 | batch_normalize=1 32 | filters=128 33 | size=3 34 | stride=1 35 | pad=1 36 | activation=leaky 37 | 38 | [convolutional] 39 | batch_normalize=1 40 | filters=128 41 | size=3 42 | stride=1 43 | pad=1 44 | activation=leaky 45 | 46 | [maxpool] 47 | size=2 48 | stride=2 49 | 50 | [dropout] 51 | probability=.5 52 | 53 | [convolutional] 54 | batch_normalize=1 55 | filters=256 56 | size=3 57 | stride=1 58 | pad=1 59 | activation=leaky 60 | 61 | [convolutional] 62 | batch_normalize=1 63 | filters=256 64 | size=3 65 | stride=1 66 | pad=1 67 | activation=leaky 68 | 69 | [convolutional] 70 | batch_normalize=1 71 | filters=256 72 | size=3 73 | stride=1 74 | pad=1 75 | activation=leaky 76 | 77 | [maxpool] 78 | size=2 79 | stride=2 80 | 81 | [dropout] 82 | probability=.5 83 | 84 | [convolutional] 85 | batch_normalize=1 86 | filters=512 87 | size=3 88 | stride=1 89 | pad=1 90 | activation=leaky 91 | 92 | [convolutional] 93 | batch_normalize=1 94 | filters=512 95 | size=3 96 | stride=1 97 | pad=1 98 | activation=leaky 99 | 100 | [convolutional] 101 | batch_normalize=1 102 | filters=512 103 | size=3 104 | stride=1 105 | pad=1 106 | activation=leaky 107 | 108 | [dropout] 109 | probability=.5 110 | 111 | [convolutional] 112 | filters=10 113 | size=1 114 | stride=1 115 | pad=1 116 | activation=leaky 117 | 118 | [avgpool] 119 | 120 | [softmax] 121 | groups=1 122 | 123 | [cost] 124 | 125 | -------------------------------------------------------------------------------- /lightnet/data/cifar.test.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=128 3 | subdivisions=1 4 | height=32 5 | width=32 6 | channels=3 7 | momentum=0.9 8 | decay=0.0005 9 | 10 | learning_rate=0.4 11 | policy=poly 12 | power=4 13 | max_batches = 50000 14 | 15 | 16 | [convolutional] 17 | batch_normalize=1 18 | filters=128 19 | size=3 20 | stride=1 21 | pad=1 22 | activation=leaky 23 | 24 | [convolutional] 25 | batch_normalize=1 26 | filters=128 27 | size=3 28 | stride=1 29 | pad=1 30 | activation=leaky 31 | 32 | [convolutional] 33 | batch_normalize=1 34 | filters=128 35 | size=3 36 | stride=1 37 | pad=1 38 | activation=leaky 39 | 40 | [maxpool] 41 | size=2 42 | stride=2 43 | 44 | [dropout] 45 | probability=.5 46 | 47 | [convolutional] 48 | batch_normalize=1 49 | filters=256 50 | size=3 51 | stride=1 52 | pad=1 53 | activation=leaky 54 | 55 | [convolutional] 56 | batch_normalize=1 57 | filters=256 58 | size=3 59 | stride=1 60 | pad=1 61 | activation=leaky 62 | 63 | [convolutional] 64 | batch_normalize=1 65 | filters=256 66 | size=3 67 | stride=1 68 | pad=1 69 | activation=leaky 70 | 71 | [maxpool] 72 | size=2 73 | stride=2 74 | 75 | [dropout] 76 | probability=.5 77 | 78 | [convolutional] 79 | batch_normalize=1 80 | filters=512 81 | size=3 82 | stride=1 83 | pad=1 84 | activation=leaky 85 | 86 | [convolutional] 87 | batch_normalize=1 88 | filters=512 89 | size=3 90 | stride=1 91 | pad=1 92 | activation=leaky 93 | 94 | [convolutional] 95 | batch_normalize=1 96 | filters=512 97 | size=3 98 | stride=1 99 | pad=1 100 | activation=leaky 101 | 102 | [dropout] 103 | probability=.5 104 | 105 | [convolutional] 106 | filters=10 107 | size=1 108 | stride=1 109 | pad=1 110 | activation=leaky 111 | 112 | [avgpool] 113 | 114 | [softmax] 115 | groups=1 116 | temperature=3 117 | 118 | [cost] 119 | 120 | -------------------------------------------------------------------------------- /lightnet/data/coco.names: -------------------------------------------------------------------------------- 1 | person 2 | bicycle 3 | car 4 | motorbike 5 | aeroplane 6 | bus 7 | train 8 | truck 9 | boat 10 | traffic light 11 | fire hydrant 12 | stop sign 13 | parking meter 14 | bench 15 | bird 16 | cat 17 | dog 18 | horse 19 | sheep 20 | cow 21 | elephant 22 | bear 23 | zebra 24 | giraffe 25 | backpack 26 | umbrella 27 | handbag 28 | tie 29 | suitcase 30 | frisbee 31 | skis 32 | snowboard 33 | sports ball 34 | kite 35 | baseball bat 36 | baseball glove 37 | skateboard 38 | surfboard 39 | tennis racket 40 | bottle 41 | wine glass 42 | cup 43 | fork 44 | knife 45 | spoon 46 | bowl 47 | banana 48 | apple 49 | sandwich 50 | orange 51 | broccoli 52 | carrot 53 | hot dog 54 | pizza 55 | donut 56 | cake 57 | chair 58 | sofa 59 | pottedplant 60 | bed 61 | diningtable 62 | toilet 63 | tvmonitor 64 | laptop 65 | mouse 66 | remote 67 | keyboard 68 | cell phone 69 | microwave 70 | oven 71 | toaster 72 | sink 73 | refrigerator 74 | book 75 | clock 76 | vase 77 | scissors 78 | teddy bear 79 | hair drier 80 | toothbrush 81 | -------------------------------------------------------------------------------- /lightnet/data/coco.template: -------------------------------------------------------------------------------- 1 | classes= 80 2 | train = $DATA/coco/trainvalno5k.txt 3 | valid = $DATA/coco_val_5k.list 4 | names = $HERE/coco.names 5 | backup = $BACKUP 6 | eval=coco 7 | 8 | -------------------------------------------------------------------------------- /lightnet/data/darknet.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | # Train 3 | batch=1 4 | subdivisions=1 5 | # Test 6 | # batch=1 7 | # subdivisions=1 8 | height=224 9 | width=224 10 | channels=3 11 | momentum=0.9 12 | decay=0.0005 13 | max_crop=320 14 | 15 | learning_rate=0.1 16 | policy=poly 17 | power=4 18 | max_batches=1600000 19 | 20 | [convolutional] 21 | batch_normalize=1 22 | filters=16 23 | size=3 24 | stride=1 25 | pad=1 26 | activation=leaky 27 | 28 | [maxpool] 29 | size=2 30 | stride=2 31 | 32 | [convolutional] 33 | batch_normalize=1 34 | filters=32 35 | size=3 36 | stride=1 37 | pad=1 38 | activation=leaky 39 | 40 | [maxpool] 41 | size=2 42 | stride=2 43 | 44 | [convolutional] 45 | batch_normalize=1 46 | filters=64 47 | size=3 48 | stride=1 49 | pad=1 50 | activation=leaky 51 | 52 | [maxpool] 53 | size=2 54 | stride=2 55 | 56 | [convolutional] 57 | batch_normalize=1 58 | filters=128 59 | size=3 60 | stride=1 61 | pad=1 62 | activation=leaky 63 | 64 | [maxpool] 65 | size=2 66 | stride=2 67 | 68 | [convolutional] 69 | batch_normalize=1 70 | filters=256 71 | size=3 72 | stride=1 73 | pad=1 74 | activation=leaky 75 | 76 | [maxpool] 77 | size=2 78 | stride=2 79 | 80 | [convolutional] 81 | batch_normalize=1 82 | filters=512 83 | size=3 84 | stride=1 85 | pad=1 86 | activation=leaky 87 | 88 | [maxpool] 89 | size=2 90 | stride=2 91 | padding=1 92 | 93 | [convolutional] 94 | batch_normalize=1 95 | filters=1024 96 | size=3 97 | stride=1 98 | pad=1 99 | activation=leaky 100 | 101 | [convolutional] 102 | filters=1000 103 | size=1 104 | stride=1 105 | pad=1 106 | activation=leaky 107 | 108 | [avgpool] 109 | 110 | [softmax] 111 | groups=1 112 | 113 | [cost] 114 | type=sse 115 | 116 | -------------------------------------------------------------------------------- /lightnet/data/darknet19.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=128 3 | subdivisions=1 4 | height=224 5 | width=224 6 | channels=3 7 | momentum=0.9 8 | decay=0.0005 9 | max_crop=448 10 | 11 | learning_rate=0.1 12 | policy=poly 13 | power=4 14 | max_batches=1600000 15 | 16 | [convolutional] 17 | batch_normalize=1 18 | filters=32 19 | size=3 20 | stride=1 21 | pad=1 22 | activation=leaky 23 | 24 | [maxpool] 25 | size=2 26 | stride=2 27 | 28 | [convolutional] 29 | batch_normalize=1 30 | filters=64 31 | size=3 32 | stride=1 33 | pad=1 34 | activation=leaky 35 | 36 | [maxpool] 37 | size=2 38 | stride=2 39 | 40 | [convolutional] 41 | batch_normalize=1 42 | filters=128 43 | size=3 44 | stride=1 45 | pad=1 46 | activation=leaky 47 | 48 | [convolutional] 49 | batch_normalize=1 50 | filters=64 51 | size=1 52 | stride=1 53 | pad=1 54 | activation=leaky 55 | 56 | [convolutional] 57 | batch_normalize=1 58 | filters=128 59 | size=3 60 | stride=1 61 | pad=1 62 | activation=leaky 63 | 64 | [maxpool] 65 | size=2 66 | stride=2 67 | 68 | [convolutional] 69 | batch_normalize=1 70 | filters=256 71 | size=3 72 | stride=1 73 | pad=1 74 | activation=leaky 75 | 76 | [convolutional] 77 | batch_normalize=1 78 | filters=128 79 | size=1 80 | stride=1 81 | pad=1 82 | activation=leaky 83 | 84 | [convolutional] 85 | batch_normalize=1 86 | filters=256 87 | size=3 88 | stride=1 89 | pad=1 90 | activation=leaky 91 | 92 | [maxpool] 93 | size=2 94 | stride=2 95 | 96 | [convolutional] 97 | batch_normalize=1 98 | filters=512 99 | size=3 100 | stride=1 101 | pad=1 102 | activation=leaky 103 | 104 | [convolutional] 105 | batch_normalize=1 106 | filters=256 107 | size=1 108 | stride=1 109 | pad=1 110 | activation=leaky 111 | 112 | [convolutional] 113 | batch_normalize=1 114 | filters=512 115 | size=3 116 | stride=1 117 | pad=1 118 | activation=leaky 119 | 120 | [convolutional] 121 | batch_normalize=1 122 | filters=256 123 | size=1 124 | stride=1 125 | pad=1 126 | activation=leaky 127 | 128 | [convolutional] 129 | batch_normalize=1 130 | filters=512 131 | size=3 132 | stride=1 133 | pad=1 134 | activation=leaky 135 | 136 | [maxpool] 137 | size=2 138 | stride=2 139 | 140 | [convolutional] 141 | batch_normalize=1 142 | filters=1024 143 | size=3 144 | stride=1 145 | pad=1 146 | activation=leaky 147 | 148 | [convolutional] 149 | batch_normalize=1 150 | filters=512 151 | size=1 152 | stride=1 153 | pad=1 154 | activation=leaky 155 | 156 | [convolutional] 157 | batch_normalize=1 158 | filters=1024 159 | size=3 160 | stride=1 161 | pad=1 162 | activation=leaky 163 | 164 | [convolutional] 165 | batch_normalize=1 166 | filters=512 167 | size=1 168 | stride=1 169 | pad=1 170 | activation=leaky 171 | 172 | [convolutional] 173 | batch_normalize=1 174 | filters=1024 175 | size=3 176 | stride=1 177 | pad=1 178 | activation=leaky 179 | 180 | [convolutional] 181 | filters=1000 182 | size=1 183 | stride=1 184 | pad=1 185 | activation=linear 186 | 187 | [avgpool] 188 | 189 | [softmax] 190 | groups=1 191 | 192 | [cost] 193 | type=sse 194 | 195 | -------------------------------------------------------------------------------- /lightnet/data/darknet19_448.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=128 3 | subdivisions=4 4 | height=448 5 | width=448 6 | max_crop=512 7 | channels=3 8 | momentum=0.9 9 | decay=0.0005 10 | 11 | learning_rate=0.001 12 | policy=poly 13 | power=4 14 | max_batches=100000 15 | 16 | angle=7 17 | hue = .1 18 | saturation=.75 19 | exposure=.75 20 | aspect=.75 21 | 22 | [convolutional] 23 | batch_normalize=1 24 | filters=32 25 | size=3 26 | stride=1 27 | pad=1 28 | activation=leaky 29 | 30 | [maxpool] 31 | size=2 32 | stride=2 33 | 34 | [convolutional] 35 | batch_normalize=1 36 | filters=64 37 | size=3 38 | stride=1 39 | pad=1 40 | activation=leaky 41 | 42 | [maxpool] 43 | size=2 44 | stride=2 45 | 46 | [convolutional] 47 | batch_normalize=1 48 | filters=128 49 | size=3 50 | stride=1 51 | pad=1 52 | activation=leaky 53 | 54 | [convolutional] 55 | batch_normalize=1 56 | filters=64 57 | size=1 58 | stride=1 59 | pad=1 60 | activation=leaky 61 | 62 | [convolutional] 63 | batch_normalize=1 64 | filters=128 65 | size=3 66 | stride=1 67 | pad=1 68 | activation=leaky 69 | 70 | [maxpool] 71 | size=2 72 | stride=2 73 | 74 | [convolutional] 75 | batch_normalize=1 76 | filters=256 77 | size=3 78 | stride=1 79 | pad=1 80 | activation=leaky 81 | 82 | [convolutional] 83 | batch_normalize=1 84 | filters=128 85 | size=1 86 | stride=1 87 | pad=1 88 | activation=leaky 89 | 90 | [convolutional] 91 | batch_normalize=1 92 | filters=256 93 | size=3 94 | stride=1 95 | pad=1 96 | activation=leaky 97 | 98 | [maxpool] 99 | size=2 100 | stride=2 101 | 102 | [convolutional] 103 | batch_normalize=1 104 | filters=512 105 | size=3 106 | stride=1 107 | pad=1 108 | activation=leaky 109 | 110 | [convolutional] 111 | batch_normalize=1 112 | filters=256 113 | size=1 114 | stride=1 115 | pad=1 116 | activation=leaky 117 | 118 | [convolutional] 119 | batch_normalize=1 120 | filters=512 121 | size=3 122 | stride=1 123 | pad=1 124 | activation=leaky 125 | 126 | [convolutional] 127 | batch_normalize=1 128 | filters=256 129 | size=1 130 | stride=1 131 | pad=1 132 | activation=leaky 133 | 134 | [convolutional] 135 | batch_normalize=1 136 | filters=512 137 | size=3 138 | stride=1 139 | pad=1 140 | activation=leaky 141 | 142 | [maxpool] 143 | size=2 144 | stride=2 145 | 146 | [convolutional] 147 | batch_normalize=1 148 | filters=1024 149 | size=3 150 | stride=1 151 | pad=1 152 | activation=leaky 153 | 154 | [convolutional] 155 | batch_normalize=1 156 | filters=512 157 | size=1 158 | stride=1 159 | pad=1 160 | activation=leaky 161 | 162 | [convolutional] 163 | batch_normalize=1 164 | filters=1024 165 | size=3 166 | stride=1 167 | pad=1 168 | activation=leaky 169 | 170 | [convolutional] 171 | batch_normalize=1 172 | filters=512 173 | size=1 174 | stride=1 175 | pad=1 176 | activation=leaky 177 | 178 | [convolutional] 179 | batch_normalize=1 180 | filters=1024 181 | size=3 182 | stride=1 183 | pad=1 184 | activation=leaky 185 | 186 | [convolutional] 187 | filters=1000 188 | size=1 189 | stride=1 190 | pad=1 191 | activation=linear 192 | 193 | [avgpool] 194 | 195 | [softmax] 196 | groups=1 197 | 198 | [cost] 199 | type=sse 200 | 201 | -------------------------------------------------------------------------------- /lightnet/data/darknet9000.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | # Training 3 | # batch=128 4 | # subdivisions=4 5 | # Testing 6 | batch = 1 7 | subdivisions = 1 8 | height=448 9 | width=448 10 | max_crop=512 11 | channels=3 12 | momentum=0.9 13 | decay=0.0005 14 | 15 | learning_rate=0.001 16 | policy=poly 17 | power=4 18 | max_batches=100000 19 | 20 | angle=7 21 | hue=.1 22 | saturation=.75 23 | exposure=.75 24 | aspect=.75 25 | 26 | [convolutional] 27 | batch_normalize=1 28 | filters=32 29 | size=3 30 | stride=1 31 | pad=1 32 | activation=leaky 33 | 34 | [maxpool] 35 | size=2 36 | stride=2 37 | 38 | [convolutional] 39 | batch_normalize=1 40 | filters=64 41 | size=3 42 | stride=1 43 | pad=1 44 | activation=leaky 45 | 46 | [maxpool] 47 | size=2 48 | stride=2 49 | 50 | [convolutional] 51 | batch_normalize=1 52 | filters=128 53 | size=3 54 | stride=1 55 | pad=1 56 | activation=leaky 57 | 58 | [convolutional] 59 | batch_normalize=1 60 | filters=64 61 | size=1 62 | stride=1 63 | pad=1 64 | activation=leaky 65 | 66 | [convolutional] 67 | batch_normalize=1 68 | filters=128 69 | size=3 70 | stride=1 71 | pad=1 72 | activation=leaky 73 | 74 | [maxpool] 75 | size=2 76 | stride=2 77 | 78 | [convolutional] 79 | batch_normalize=1 80 | filters=256 81 | size=3 82 | stride=1 83 | pad=1 84 | activation=leaky 85 | 86 | [convolutional] 87 | batch_normalize=1 88 | filters=128 89 | size=1 90 | stride=1 91 | pad=1 92 | activation=leaky 93 | 94 | [convolutional] 95 | batch_normalize=1 96 | filters=256 97 | size=3 98 | stride=1 99 | pad=1 100 | activation=leaky 101 | 102 | [maxpool] 103 | size=2 104 | stride=2 105 | 106 | [convolutional] 107 | batch_normalize=1 108 | filters=512 109 | size=3 110 | stride=1 111 | pad=1 112 | activation=leaky 113 | 114 | [convolutional] 115 | batch_normalize=1 116 | filters=256 117 | size=1 118 | stride=1 119 | pad=1 120 | activation=leaky 121 | 122 | [convolutional] 123 | batch_normalize=1 124 | filters=512 125 | size=3 126 | stride=1 127 | pad=1 128 | activation=leaky 129 | 130 | [convolutional] 131 | batch_normalize=1 132 | filters=256 133 | size=1 134 | stride=1 135 | pad=1 136 | activation=leaky 137 | 138 | [convolutional] 139 | batch_normalize=1 140 | filters=512 141 | size=3 142 | stride=1 143 | pad=1 144 | activation=leaky 145 | 146 | [maxpool] 147 | size=2 148 | stride=2 149 | 150 | [convolutional] 151 | batch_normalize=1 152 | filters=1024 153 | size=3 154 | stride=1 155 | pad=1 156 | activation=leaky 157 | 158 | [convolutional] 159 | batch_normalize=1 160 | filters=512 161 | size=1 162 | stride=1 163 | pad=1 164 | activation=leaky 165 | 166 | [convolutional] 167 | batch_normalize=1 168 | filters=1024 169 | size=3 170 | stride=1 171 | pad=1 172 | activation=leaky 173 | 174 | [convolutional] 175 | batch_normalize=1 176 | filters=512 177 | size=1 178 | stride=1 179 | pad=1 180 | activation=leaky 181 | 182 | [convolutional] 183 | batch_normalize=1 184 | filters=1024 185 | size=3 186 | stride=1 187 | pad=1 188 | activation=leaky 189 | 190 | [convolutional] 191 | filters=9418 192 | size=1 193 | stride=1 194 | pad=1 195 | activation=linear 196 | 197 | [avgpool] 198 | 199 | [softmax] 200 | groups=1 201 | tree=data/9k.tree 202 | 203 | [cost] 204 | type=masked 205 | 206 | -------------------------------------------------------------------------------- /lightnet/data/extraction.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=128 3 | subdivisions=1 4 | height=224 5 | width=224 6 | max_crop=320 7 | channels=3 8 | momentum=0.9 9 | decay=0.0005 10 | 11 | learning_rate=0.1 12 | policy=poly 13 | power=4 14 | max_batches=1600000 15 | 16 | [convolutional] 17 | batch_normalize=1 18 | filters=64 19 | size=7 20 | stride=2 21 | pad=1 22 | activation=leaky 23 | 24 | [maxpool] 25 | size=2 26 | stride=2 27 | 28 | [convolutional] 29 | batch_normalize=1 30 | filters=192 31 | size=3 32 | stride=1 33 | pad=1 34 | activation=leaky 35 | 36 | [maxpool] 37 | size=2 38 | stride=2 39 | 40 | [convolutional] 41 | batch_normalize=1 42 | filters=128 43 | size=1 44 | stride=1 45 | pad=1 46 | activation=leaky 47 | 48 | [convolutional] 49 | batch_normalize=1 50 | filters=256 51 | size=3 52 | stride=1 53 | pad=1 54 | activation=leaky 55 | 56 | [convolutional] 57 | batch_normalize=1 58 | filters=256 59 | size=1 60 | stride=1 61 | pad=1 62 | activation=leaky 63 | 64 | [convolutional] 65 | batch_normalize=1 66 | filters=512 67 | size=3 68 | stride=1 69 | pad=1 70 | activation=leaky 71 | 72 | [maxpool] 73 | size=2 74 | stride=2 75 | 76 | [convolutional] 77 | batch_normalize=1 78 | filters=256 79 | size=1 80 | stride=1 81 | pad=1 82 | activation=leaky 83 | 84 | [convolutional] 85 | batch_normalize=1 86 | filters=512 87 | size=3 88 | stride=1 89 | pad=1 90 | activation=leaky 91 | 92 | [convolutional] 93 | batch_normalize=1 94 | filters=256 95 | size=1 96 | stride=1 97 | pad=1 98 | activation=leaky 99 | 100 | [convolutional] 101 | batch_normalize=1 102 | filters=512 103 | size=3 104 | stride=1 105 | pad=1 106 | activation=leaky 107 | 108 | [convolutional] 109 | batch_normalize=1 110 | filters=256 111 | size=1 112 | stride=1 113 | pad=1 114 | activation=leaky 115 | 116 | [convolutional] 117 | batch_normalize=1 118 | filters=512 119 | size=3 120 | stride=1 121 | pad=1 122 | activation=leaky 123 | 124 | [convolutional] 125 | batch_normalize=1 126 | filters=256 127 | size=1 128 | stride=1 129 | pad=1 130 | activation=leaky 131 | 132 | [convolutional] 133 | batch_normalize=1 134 | filters=512 135 | size=3 136 | stride=1 137 | pad=1 138 | activation=leaky 139 | 140 | [convolutional] 141 | batch_normalize=1 142 | filters=512 143 | size=1 144 | stride=1 145 | pad=1 146 | activation=leaky 147 | 148 | [convolutional] 149 | batch_normalize=1 150 | filters=1024 151 | size=3 152 | stride=1 153 | pad=1 154 | activation=leaky 155 | 156 | [maxpool] 157 | size=2 158 | stride=2 159 | 160 | [convolutional] 161 | batch_normalize=1 162 | filters=512 163 | size=1 164 | stride=1 165 | pad=1 166 | activation=leaky 167 | 168 | [convolutional] 169 | batch_normalize=1 170 | filters=1024 171 | size=3 172 | stride=1 173 | pad=1 174 | activation=leaky 175 | 176 | [convolutional] 177 | batch_normalize=1 178 | filters=512 179 | size=1 180 | stride=1 181 | pad=1 182 | activation=leaky 183 | 184 | [convolutional] 185 | batch_normalize=1 186 | filters=1024 187 | size=3 188 | stride=1 189 | pad=1 190 | activation=leaky 191 | 192 | [convolutional] 193 | filters=1000 194 | size=1 195 | stride=1 196 | pad=1 197 | activation=leaky 198 | 199 | [avgpool] 200 | 201 | [softmax] 202 | groups=1 203 | 204 | [cost] 205 | type=sse 206 | 207 | -------------------------------------------------------------------------------- /lightnet/data/extraction.conv.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=1 3 | subdivisions=1 4 | height=256 5 | width=256 6 | channels=3 7 | momentum=0.9 8 | decay=0.0005 9 | 10 | learning_rate=0.5 11 | policy=poly 12 | power=6 13 | max_batches=500000 14 | 15 | [convolutional] 16 | filters=64 17 | size=7 18 | stride=2 19 | pad=1 20 | activation=leaky 21 | 22 | [maxpool] 23 | size=2 24 | stride=2 25 | 26 | [convolutional] 27 | filters=192 28 | size=3 29 | stride=1 30 | pad=1 31 | activation=leaky 32 | 33 | [maxpool] 34 | size=2 35 | stride=2 36 | 37 | [convolutional] 38 | filters=128 39 | size=1 40 | stride=1 41 | pad=1 42 | activation=leaky 43 | 44 | [convolutional] 45 | filters=256 46 | size=3 47 | stride=1 48 | pad=1 49 | activation=leaky 50 | 51 | [convolutional] 52 | filters=256 53 | size=1 54 | stride=1 55 | pad=1 56 | activation=leaky 57 | 58 | [convolutional] 59 | filters=512 60 | size=3 61 | stride=1 62 | pad=1 63 | activation=leaky 64 | 65 | [maxpool] 66 | size=2 67 | stride=2 68 | 69 | [convolutional] 70 | filters=256 71 | size=1 72 | stride=1 73 | pad=1 74 | activation=leaky 75 | 76 | [convolutional] 77 | filters=512 78 | size=3 79 | stride=1 80 | pad=1 81 | activation=leaky 82 | 83 | [convolutional] 84 | filters=256 85 | size=1 86 | stride=1 87 | pad=1 88 | activation=leaky 89 | 90 | [convolutional] 91 | filters=512 92 | size=3 93 | stride=1 94 | pad=1 95 | activation=leaky 96 | 97 | [convolutional] 98 | filters=256 99 | size=1 100 | stride=1 101 | pad=1 102 | activation=leaky 103 | 104 | [convolutional] 105 | filters=512 106 | size=3 107 | stride=1 108 | pad=1 109 | activation=leaky 110 | 111 | [convolutional] 112 | filters=256 113 | size=1 114 | stride=1 115 | pad=1 116 | activation=leaky 117 | 118 | [convolutional] 119 | filters=512 120 | size=3 121 | stride=1 122 | pad=1 123 | activation=leaky 124 | 125 | [convolutional] 126 | filters=512 127 | size=1 128 | stride=1 129 | pad=1 130 | activation=leaky 131 | 132 | [convolutional] 133 | filters=1024 134 | size=3 135 | stride=1 136 | pad=1 137 | activation=leaky 138 | 139 | [maxpool] 140 | size=2 141 | stride=2 142 | 143 | [convolutional] 144 | filters=512 145 | size=1 146 | stride=1 147 | pad=1 148 | activation=leaky 149 | 150 | [convolutional] 151 | filters=1024 152 | size=3 153 | stride=1 154 | pad=1 155 | activation=leaky 156 | 157 | [convolutional] 158 | filters=512 159 | size=1 160 | stride=1 161 | pad=1 162 | activation=leaky 163 | 164 | [convolutional] 165 | filters=1024 166 | size=3 167 | stride=1 168 | pad=1 169 | activation=leaky 170 | 171 | [avgpool] 172 | 173 | [connected] 174 | output=1000 175 | activation=leaky 176 | 177 | [softmax] 178 | groups=1 179 | 180 | -------------------------------------------------------------------------------- /lightnet/data/extraction22k.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=128 3 | subdivisions=1 4 | height=224 5 | width=224 6 | max_crop=320 7 | channels=3 8 | momentum=0.9 9 | decay=0.0005 10 | 11 | learning_rate=0.01 12 | max_batches = 0 13 | policy=steps 14 | steps=444000,590000,970000 15 | scales=.5,.2,.1 16 | 17 | #policy=sigmoid 18 | #gamma=.00008 19 | #step=100000 20 | #max_batches=200000 21 | 22 | [convolutional] 23 | batch_normalize=1 24 | filters=64 25 | size=7 26 | stride=2 27 | pad=1 28 | activation=leaky 29 | 30 | [maxpool] 31 | size=2 32 | stride=2 33 | 34 | [convolutional] 35 | batch_normalize=1 36 | filters=192 37 | size=3 38 | stride=1 39 | pad=1 40 | activation=leaky 41 | 42 | [maxpool] 43 | size=2 44 | stride=2 45 | 46 | [convolutional] 47 | batch_normalize=1 48 | filters=128 49 | size=1 50 | stride=1 51 | pad=1 52 | activation=leaky 53 | 54 | [convolutional] 55 | batch_normalize=1 56 | filters=256 57 | size=3 58 | stride=1 59 | pad=1 60 | activation=leaky 61 | 62 | [convolutional] 63 | batch_normalize=1 64 | filters=256 65 | size=1 66 | stride=1 67 | pad=1 68 | activation=leaky 69 | 70 | [convolutional] 71 | batch_normalize=1 72 | filters=512 73 | size=3 74 | stride=1 75 | pad=1 76 | activation=leaky 77 | 78 | [maxpool] 79 | size=2 80 | stride=2 81 | 82 | [convolutional] 83 | batch_normalize=1 84 | filters=256 85 | size=1 86 | stride=1 87 | pad=1 88 | activation=leaky 89 | 90 | [convolutional] 91 | batch_normalize=1 92 | filters=512 93 | size=3 94 | stride=1 95 | pad=1 96 | activation=leaky 97 | 98 | [convolutional] 99 | batch_normalize=1 100 | filters=256 101 | size=1 102 | stride=1 103 | pad=1 104 | activation=leaky 105 | 106 | [convolutional] 107 | batch_normalize=1 108 | filters=512 109 | size=3 110 | stride=1 111 | pad=1 112 | activation=leaky 113 | 114 | [convolutional] 115 | batch_normalize=1 116 | filters=256 117 | size=1 118 | stride=1 119 | pad=1 120 | activation=leaky 121 | 122 | [convolutional] 123 | batch_normalize=1 124 | filters=512 125 | size=3 126 | stride=1 127 | pad=1 128 | activation=leaky 129 | 130 | [convolutional] 131 | batch_normalize=1 132 | filters=256 133 | size=1 134 | stride=1 135 | pad=1 136 | activation=leaky 137 | 138 | [convolutional] 139 | batch_normalize=1 140 | filters=512 141 | size=3 142 | stride=1 143 | pad=1 144 | activation=leaky 145 | 146 | [convolutional] 147 | batch_normalize=1 148 | filters=512 149 | size=1 150 | stride=1 151 | pad=1 152 | activation=leaky 153 | 154 | [convolutional] 155 | batch_normalize=1 156 | filters=1024 157 | size=3 158 | stride=1 159 | pad=1 160 | activation=leaky 161 | 162 | [maxpool] 163 | size=2 164 | stride=2 165 | 166 | [convolutional] 167 | batch_normalize=1 168 | filters=1024 169 | size=1 170 | stride=1 171 | pad=1 172 | activation=leaky 173 | 174 | [convolutional] 175 | batch_normalize=1 176 | filters=2048 177 | size=3 178 | stride=1 179 | pad=1 180 | activation=leaky 181 | 182 | [convolutional] 183 | batch_normalize=1 184 | filters=1024 185 | size=1 186 | stride=1 187 | pad=1 188 | activation=leaky 189 | 190 | [convolutional] 191 | batch_normalize=1 192 | filters=2048 193 | size=3 194 | stride=1 195 | pad=1 196 | activation=leaky 197 | 198 | [avgpool] 199 | 200 | [connected] 201 | output=21842 202 | activation=leaky 203 | 204 | [softmax] 205 | groups=1 206 | 207 | [cost] 208 | type=sse 209 | 210 | -------------------------------------------------------------------------------- /lightnet/data/go.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=512 3 | subdivisions=1 4 | height=19 5 | width=19 6 | channels=1 7 | momentum=0.9 8 | decay=0.0005 9 | 10 | burn_in=1000 11 | learning_rate=0.1 12 | policy=poly 13 | power=4 14 | max_batches=10000000 15 | 16 | [convolutional] 17 | filters=256 18 | size=3 19 | stride=1 20 | pad=1 21 | activation=relu 22 | batch_normalize=1 23 | 24 | [convolutional] 25 | filters=256 26 | size=3 27 | stride=1 28 | pad=1 29 | activation=relu 30 | batch_normalize=1 31 | 32 | [convolutional] 33 | filters=256 34 | size=3 35 | stride=1 36 | pad=1 37 | activation=relu 38 | batch_normalize=1 39 | 40 | [convolutional] 41 | filters=256 42 | size=3 43 | stride=1 44 | pad=1 45 | activation=relu 46 | batch_normalize=1 47 | 48 | [convolutional] 49 | filters=256 50 | size=3 51 | stride=1 52 | pad=1 53 | activation=relu 54 | batch_normalize=1 55 | 56 | [convolutional] 57 | filters=256 58 | size=3 59 | stride=1 60 | pad=1 61 | activation=relu 62 | batch_normalize=1 63 | 64 | [convolutional] 65 | filters=256 66 | size=3 67 | stride=1 68 | pad=1 69 | activation=relu 70 | batch_normalize=1 71 | 72 | [convolutional] 73 | filters=256 74 | size=3 75 | stride=1 76 | pad=1 77 | activation=relu 78 | batch_normalize=1 79 | 80 | [convolutional] 81 | filters=256 82 | size=3 83 | stride=1 84 | pad=1 85 | activation=relu 86 | batch_normalize=1 87 | 88 | [convolutional] 89 | filters=256 90 | size=3 91 | stride=1 92 | pad=1 93 | activation=relu 94 | batch_normalize=1 95 | 96 | [convolutional] 97 | filters=256 98 | size=3 99 | stride=1 100 | pad=1 101 | activation=relu 102 | batch_normalize=1 103 | 104 | [convolutional] 105 | filters=256 106 | size=3 107 | stride=1 108 | pad=1 109 | activation=relu 110 | batch_normalize=1 111 | 112 | [convolutional] 113 | filters=256 114 | size=3 115 | stride=1 116 | pad=1 117 | activation=relu 118 | batch_normalize=1 119 | 120 | [convolutional] 121 | filters=1 122 | size=1 123 | stride=1 124 | pad=1 125 | activation=linear 126 | 127 | [reorg] 128 | extra=1 129 | stride=1 130 | 131 | [softmax] 132 | 133 | [cost] 134 | type=sse 135 | 136 | -------------------------------------------------------------------------------- /lightnet/data/go.test.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=1 3 | subdivisions=1 4 | height=19 5 | width=19 6 | channels=1 7 | momentum=0.9 8 | decay=0.0005 9 | 10 | learning_rate=0.01 11 | policy=poly 12 | power=4 13 | max_batches=100000 14 | 15 | [convolutional] 16 | filters=256 17 | size=3 18 | stride=1 19 | pad=1 20 | activation=relu 21 | batch_normalize=1 22 | 23 | [convolutional] 24 | filters=256 25 | size=3 26 | stride=1 27 | pad=1 28 | activation=relu 29 | batch_normalize=1 30 | 31 | [convolutional] 32 | filters=256 33 | size=3 34 | stride=1 35 | pad=1 36 | activation=relu 37 | batch_normalize=1 38 | 39 | [convolutional] 40 | filters=256 41 | size=3 42 | stride=1 43 | pad=1 44 | activation=relu 45 | batch_normalize=1 46 | 47 | [convolutional] 48 | filters=256 49 | size=3 50 | stride=1 51 | pad=1 52 | activation=relu 53 | batch_normalize=1 54 | 55 | [convolutional] 56 | filters=256 57 | size=3 58 | stride=1 59 | pad=1 60 | activation=relu 61 | batch_normalize=1 62 | 63 | [convolutional] 64 | filters=256 65 | size=3 66 | stride=1 67 | pad=1 68 | activation=relu 69 | batch_normalize=1 70 | 71 | [convolutional] 72 | filters=256 73 | size=3 74 | stride=1 75 | pad=1 76 | activation=relu 77 | batch_normalize=1 78 | 79 | [convolutional] 80 | filters=256 81 | size=3 82 | stride=1 83 | pad=1 84 | activation=relu 85 | batch_normalize=1 86 | 87 | [convolutional] 88 | filters=256 89 | size=3 90 | stride=1 91 | pad=1 92 | activation=relu 93 | batch_normalize=1 94 | 95 | [convolutional] 96 | filters=256 97 | size=3 98 | stride=1 99 | pad=1 100 | activation=relu 101 | batch_normalize=1 102 | 103 | [convolutional] 104 | filters=256 105 | size=3 106 | stride=1 107 | pad=1 108 | activation=relu 109 | batch_normalize=1 110 | 111 | [convolutional] 112 | filters=256 113 | size=3 114 | stride=1 115 | pad=1 116 | activation=relu 117 | batch_normalize=1 118 | 119 | [convolutional] 120 | filters=1 121 | size=1 122 | stride=1 123 | pad=1 124 | activation=linear 125 | 126 | [reorg] 127 | extra=1 128 | stride=1 129 | 130 | [softmax] 131 | 132 | [cost] 133 | type=sse 134 | 135 | -------------------------------------------------------------------------------- /lightnet/data/gru.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | subdivisions=1 3 | batch = 256 4 | inputs=256 5 | momentum=0.9 6 | decay=0.0 7 | time_steps=128 8 | learning_rate=.002 9 | adam=1 10 | 11 | policy=constant 12 | power=4 13 | max_batches=1000000 14 | 15 | [gru] 16 | output = 1024 17 | 18 | [gru] 19 | output = 1024 20 | 21 | [gru] 22 | output = 1024 23 | 24 | [connected] 25 | output=256 26 | activation=linear 27 | 28 | [softmax] 29 | 30 | [cost] 31 | type=sse 32 | 33 | -------------------------------------------------------------------------------- /lightnet/data/jnet-conv.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=1 3 | subdivisions=1 4 | height=10 5 | width=10 6 | channels=3 7 | learning_rate=0.01 8 | momentum=0.9 9 | decay=0.0005 10 | 11 | [convolutional] 12 | filters=32 13 | size=3 14 | stride=1 15 | pad=1 16 | activation=leaky 17 | 18 | [convolutional] 19 | filters=32 20 | size=3 21 | stride=1 22 | pad=1 23 | activation=leaky 24 | 25 | [maxpool] 26 | stride=2 27 | size=2 28 | 29 | [convolutional] 30 | filters=64 31 | size=3 32 | stride=1 33 | pad=1 34 | activation=leaky 35 | 36 | [convolutional] 37 | filters=64 38 | size=3 39 | stride=1 40 | pad=1 41 | activation=leaky 42 | 43 | [maxpool] 44 | stride=2 45 | size=2 46 | 47 | [convolutional] 48 | filters=128 49 | size=3 50 | stride=1 51 | pad=1 52 | activation=leaky 53 | 54 | [convolutional] 55 | filters=128 56 | size=3 57 | stride=1 58 | pad=1 59 | activation=leaky 60 | 61 | [maxpool] 62 | stride=2 63 | size=2 64 | 65 | [convolutional] 66 | filters=256 67 | size=3 68 | stride=1 69 | pad=1 70 | activation=leaky 71 | 72 | [convolutional] 73 | filters=256 74 | size=3 75 | stride=1 76 | pad=1 77 | activation=leaky 78 | 79 | [maxpool] 80 | stride=2 81 | size=2 82 | 83 | [convolutional] 84 | filters=512 85 | size=3 86 | stride=1 87 | pad=1 88 | activation=leaky 89 | 90 | [convolutional] 91 | filters=512 92 | size=3 93 | stride=1 94 | pad=1 95 | activation=leaky 96 | 97 | [maxpool] 98 | stride=2 99 | size=2 100 | 101 | [convolutional] 102 | filters=1024 103 | size=3 104 | stride=1 105 | pad=1 106 | activation=leaky 107 | 108 | [convolutional] 109 | filters=1024 110 | size=3 111 | stride=1 112 | pad=1 113 | activation=leaky 114 | 115 | [maxpool] 116 | size=2 117 | stride=2 118 | 119 | -------------------------------------------------------------------------------- /lightnet/data/rnn.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | subdivisions=1 3 | inputs=256 4 | batch = 1 5 | momentum=0.9 6 | decay=0.001 7 | max_batches = 2000 8 | time_steps=1 9 | learning_rate=0.1 10 | policy=steps 11 | steps=1000,1500 12 | scales=.1,.1 13 | 14 | [rnn] 15 | batch_normalize=1 16 | output = 1024 17 | hidden=1024 18 | activation=leaky 19 | 20 | [rnn] 21 | batch_normalize=1 22 | output = 1024 23 | hidden=1024 24 | activation=leaky 25 | 26 | [rnn] 27 | batch_normalize=1 28 | output = 1024 29 | hidden=1024 30 | activation=leaky 31 | 32 | [connected] 33 | output=256 34 | activation=leaky 35 | 36 | [softmax] 37 | 38 | [cost] 39 | type=sse 40 | 41 | -------------------------------------------------------------------------------- /lightnet/data/rnn.train.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | subdivisions=1 3 | inputs=256 4 | batch = 128 5 | momentum=0.9 6 | decay=0.001 7 | max_batches = 2000 8 | time_steps=576 9 | learning_rate=0.1 10 | policy=steps 11 | steps=1000,1500 12 | scales=.1,.1 13 | 14 | [rnn] 15 | batch_normalize=1 16 | output = 1024 17 | hidden=1024 18 | activation=leaky 19 | 20 | [rnn] 21 | batch_normalize=1 22 | output = 1024 23 | hidden=1024 24 | activation=leaky 25 | 26 | [rnn] 27 | batch_normalize=1 28 | output = 1024 29 | hidden=1024 30 | activation=leaky 31 | 32 | [connected] 33 | output=256 34 | activation=leaky 35 | 36 | [softmax] 37 | 38 | [cost] 39 | type=sse 40 | 41 | -------------------------------------------------------------------------------- /lightnet/data/strided.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=128 3 | subdivisions=4 4 | height=256 5 | width=256 6 | channels=3 7 | momentum=0.9 8 | decay=0.0005 9 | 10 | learning_rate=0.01 11 | policy=steps 12 | scales=.1,.1,.1 13 | steps=200000,300000,400000 14 | max_batches=800000 15 | 16 | 17 | [crop] 18 | crop_height=224 19 | crop_width=224 20 | flip=1 21 | angle=0 22 | saturation=1 23 | exposure=1 24 | shift=.2 25 | 26 | [convolutional] 27 | filters=64 28 | size=7 29 | stride=2 30 | pad=1 31 | activation=ramp 32 | 33 | [convolutional] 34 | filters=192 35 | size=3 36 | stride=2 37 | pad=1 38 | activation=ramp 39 | 40 | [convolutional] 41 | filters=128 42 | size=1 43 | stride=1 44 | pad=1 45 | activation=ramp 46 | 47 | [convolutional] 48 | filters=256 49 | size=3 50 | stride=2 51 | pad=1 52 | activation=ramp 53 | 54 | [convolutional] 55 | filters=128 56 | size=1 57 | stride=1 58 | pad=1 59 | activation=ramp 60 | 61 | [convolutional] 62 | filters=256 63 | size=3 64 | stride=1 65 | pad=1 66 | activation=ramp 67 | 68 | [convolutional] 69 | filters=128 70 | size=1 71 | stride=1 72 | pad=1 73 | activation=ramp 74 | 75 | [convolutional] 76 | filters=512 77 | size=3 78 | stride=2 79 | pad=1 80 | activation=ramp 81 | 82 | [convolutional] 83 | filters=256 84 | size=1 85 | stride=1 86 | pad=1 87 | activation=ramp 88 | 89 | [convolutional] 90 | filters=512 91 | size=3 92 | stride=1 93 | pad=1 94 | activation=ramp 95 | 96 | [convolutional] 97 | filters=256 98 | size=1 99 | stride=1 100 | pad=1 101 | activation=ramp 102 | 103 | [convolutional] 104 | filters=512 105 | size=3 106 | stride=1 107 | pad=1 108 | activation=ramp 109 | 110 | [convolutional] 111 | filters=256 112 | size=1 113 | stride=1 114 | pad=1 115 | activation=ramp 116 | 117 | [convolutional] 118 | filters=512 119 | size=3 120 | stride=1 121 | pad=1 122 | activation=ramp 123 | 124 | [convolutional] 125 | filters=256 126 | size=1 127 | stride=1 128 | pad=1 129 | activation=ramp 130 | 131 | [convolutional] 132 | filters=512 133 | size=3 134 | stride=1 135 | pad=1 136 | activation=ramp 137 | 138 | [convolutional] 139 | filters=256 140 | size=1 141 | stride=1 142 | pad=1 143 | activation=ramp 144 | 145 | [convolutional] 146 | filters=1024 147 | size=3 148 | stride=2 149 | pad=1 150 | activation=ramp 151 | 152 | [convolutional] 153 | filters=512 154 | size=1 155 | stride=1 156 | pad=1 157 | activation=ramp 158 | 159 | [convolutional] 160 | filters=1024 161 | size=3 162 | stride=1 163 | pad=1 164 | activation=ramp 165 | 166 | [maxpool] 167 | size=3 168 | stride=2 169 | 170 | [connected] 171 | output=4096 172 | activation=ramp 173 | 174 | [dropout] 175 | probability=0.5 176 | 177 | [connected] 178 | output=1000 179 | activation=ramp 180 | 181 | [softmax] 182 | 183 | [cost] 184 | type=sse 185 | 186 | -------------------------------------------------------------------------------- /lightnet/data/t1.test.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=1 3 | subdivisions=1 4 | height=224 5 | width=224 6 | channels=3 7 | momentum=0.9 8 | decay=0.0005 9 | 10 | learning_rate=0.0005 11 | policy=steps 12 | steps=200,400,600,20000,30000 13 | scales=2.5,2,2,.1,.1 14 | max_batches = 40000 15 | 16 | [convolutional] 17 | filters=16 18 | size=3 19 | stride=1 20 | pad=1 21 | activation=leaky 22 | 23 | [maxpool] 24 | size=2 25 | stride=2 26 | 27 | [convolutional] 28 | filters=32 29 | size=3 30 | stride=1 31 | pad=1 32 | activation=leaky 33 | 34 | [maxpool] 35 | size=2 36 | stride=2 37 | 38 | [convolutional] 39 | filters=64 40 | size=3 41 | stride=1 42 | pad=1 43 | activation=leaky 44 | 45 | [maxpool] 46 | size=2 47 | stride=2 48 | 49 | [convolutional] 50 | filters=128 51 | size=3 52 | stride=1 53 | pad=1 54 | activation=leaky 55 | 56 | [maxpool] 57 | size=2 58 | stride=2 59 | 60 | [convolutional] 61 | filters=256 62 | size=3 63 | stride=1 64 | pad=1 65 | activation=leaky 66 | 67 | [maxpool] 68 | size=2 69 | stride=2 70 | 71 | [convolutional] 72 | filters=512 73 | size=3 74 | stride=1 75 | pad=1 76 | activation=leaky 77 | 78 | [convolutional] 79 | filters=1024 80 | size=3 81 | stride=1 82 | pad=1 83 | activation=leaky 84 | 85 | [convolutional] 86 | filters=1024 87 | size=3 88 | stride=1 89 | pad=1 90 | activation=leaky 91 | 92 | [convolutional] 93 | filters=256 94 | size=3 95 | stride=1 96 | pad=1 97 | activation=leaky 98 | 99 | [connected] 100 | output= 1470 101 | activation=linear 102 | 103 | [detection] 104 | classes=20 105 | coords=4 106 | rescore=1 107 | side=7 108 | num=2 109 | softmax=0 110 | sqrt=1 111 | jitter=.2 112 | 113 | object_scale=1 114 | noobject_scale=.5 115 | class_scale=1 116 | coord_scale=5 117 | 118 | -------------------------------------------------------------------------------- /lightnet/data/tiny-yolo-voc.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=64 3 | subdivisions=8 4 | width=416 5 | height=416 6 | channels=3 7 | momentum=0.9 8 | decay=0.0005 9 | angle=0 10 | saturation = 1.5 11 | exposure = 1.5 12 | hue=.1 13 | 14 | learning_rate=0.001 15 | max_batches = 40200 16 | policy=steps 17 | steps=-1,100,20000,30000 18 | scales=.1,10,.1,.1 19 | 20 | [convolutional] 21 | batch_normalize=1 22 | filters=16 23 | size=3 24 | stride=1 25 | pad=1 26 | activation=leaky 27 | 28 | [maxpool] 29 | size=2 30 | stride=2 31 | 32 | [convolutional] 33 | batch_normalize=1 34 | filters=32 35 | size=3 36 | stride=1 37 | pad=1 38 | activation=leaky 39 | 40 | [maxpool] 41 | size=2 42 | stride=2 43 | 44 | [convolutional] 45 | batch_normalize=1 46 | filters=64 47 | size=3 48 | stride=1 49 | pad=1 50 | activation=leaky 51 | 52 | [maxpool] 53 | size=2 54 | stride=2 55 | 56 | [convolutional] 57 | batch_normalize=1 58 | filters=128 59 | size=3 60 | stride=1 61 | pad=1 62 | activation=leaky 63 | 64 | [maxpool] 65 | size=2 66 | stride=2 67 | 68 | [convolutional] 69 | batch_normalize=1 70 | filters=256 71 | size=3 72 | stride=1 73 | pad=1 74 | activation=leaky 75 | 76 | [maxpool] 77 | size=2 78 | stride=2 79 | 80 | [convolutional] 81 | batch_normalize=1 82 | filters=512 83 | size=3 84 | stride=1 85 | pad=1 86 | activation=leaky 87 | 88 | [maxpool] 89 | size=2 90 | stride=1 91 | 92 | [convolutional] 93 | batch_normalize=1 94 | filters=1024 95 | size=3 96 | stride=1 97 | pad=1 98 | activation=leaky 99 | 100 | ########### 101 | 102 | [convolutional] 103 | batch_normalize=1 104 | size=3 105 | stride=1 106 | pad=1 107 | filters=1024 108 | activation=leaky 109 | 110 | [convolutional] 111 | size=1 112 | stride=1 113 | pad=1 114 | filters=125 115 | activation=linear 116 | 117 | [region] 118 | anchors = 1.08,1.19, 3.42,4.41, 6.63,11.38, 9.42,5.11, 16.62,10.52 119 | bias_match=1 120 | classes=20 121 | coords=4 122 | num=5 123 | softmax=1 124 | jitter=.2 125 | rescore=1 126 | 127 | object_scale=5 128 | noobject_scale=1 129 | class_scale=1 130 | coord_scale=1 131 | 132 | absolute=1 133 | thresh = .6 134 | random=1 135 | -------------------------------------------------------------------------------- /lightnet/data/tiny-yolo.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | # Training 3 | # batch=64 4 | # subdivisions=2 5 | # Testing 6 | batch=1 7 | subdivisions=1 8 | width=416 9 | height=416 10 | channels=3 11 | momentum=0.9 12 | decay=0.0005 13 | angle=0 14 | saturation = 1.5 15 | exposure = 1.5 16 | hue=.1 17 | 18 | learning_rate=0.001 19 | burn_in=1000 20 | max_batches = 500200 21 | policy=steps 22 | steps=400000,450000 23 | scales=.1,.1 24 | 25 | [convolutional] 26 | batch_normalize=1 27 | filters=16 28 | size=3 29 | stride=1 30 | pad=1 31 | activation=leaky 32 | 33 | [maxpool] 34 | size=2 35 | stride=2 36 | 37 | [convolutional] 38 | batch_normalize=1 39 | filters=32 40 | size=3 41 | stride=1 42 | pad=1 43 | activation=leaky 44 | 45 | [maxpool] 46 | size=2 47 | stride=2 48 | 49 | [convolutional] 50 | batch_normalize=1 51 | filters=64 52 | size=3 53 | stride=1 54 | pad=1 55 | activation=leaky 56 | 57 | [maxpool] 58 | size=2 59 | stride=2 60 | 61 | [convolutional] 62 | batch_normalize=1 63 | filters=128 64 | size=3 65 | stride=1 66 | pad=1 67 | activation=leaky 68 | 69 | [maxpool] 70 | size=2 71 | stride=2 72 | 73 | [convolutional] 74 | batch_normalize=1 75 | filters=256 76 | size=3 77 | stride=1 78 | pad=1 79 | activation=leaky 80 | 81 | [maxpool] 82 | size=2 83 | stride=2 84 | 85 | [convolutional] 86 | batch_normalize=1 87 | filters=512 88 | size=3 89 | stride=1 90 | pad=1 91 | activation=leaky 92 | 93 | [maxpool] 94 | size=2 95 | stride=1 96 | 97 | [convolutional] 98 | batch_normalize=1 99 | filters=1024 100 | size=3 101 | stride=1 102 | pad=1 103 | activation=leaky 104 | 105 | ########### 106 | 107 | [convolutional] 108 | batch_normalize=1 109 | size=3 110 | stride=1 111 | pad=1 112 | filters=512 113 | activation=leaky 114 | 115 | [convolutional] 116 | size=1 117 | stride=1 118 | pad=1 119 | filters=425 120 | activation=linear 121 | 122 | [region] 123 | anchors = 0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828 124 | bias_match=1 125 | classes=80 126 | coords=4 127 | num=5 128 | softmax=1 129 | jitter=.2 130 | rescore=0 131 | 132 | object_scale=5 133 | noobject_scale=1 134 | class_scale=1 135 | coord_scale=1 136 | 137 | absolute=1 138 | thresh = .6 139 | random=1 140 | -------------------------------------------------------------------------------- /lightnet/data/tiny.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | # Train 3 | batch=128 4 | subdivisions=1 5 | # Test 6 | # batch=1 7 | # subdivisions=1 8 | height=224 9 | width=224 10 | channels=3 11 | momentum=0.9 12 | decay=0.0005 13 | max_crop=320 14 | 15 | learning_rate=0.1 16 | policy=poly 17 | power=4 18 | max_batches=1600000 19 | 20 | angle=7 21 | hue=.1 22 | saturation=.75 23 | exposure=.75 24 | aspect=.75 25 | 26 | [convolutional] 27 | batch_normalize=1 28 | filters=16 29 | size=3 30 | stride=1 31 | pad=1 32 | activation=leaky 33 | 34 | [maxpool] 35 | size=2 36 | stride=2 37 | 38 | [convolutional] 39 | batch_normalize=1 40 | filters=32 41 | size=3 42 | stride=1 43 | pad=1 44 | activation=leaky 45 | 46 | [maxpool] 47 | size=2 48 | stride=2 49 | 50 | [convolutional] 51 | batch_normalize=1 52 | filters=16 53 | size=1 54 | stride=1 55 | pad=1 56 | activation=leaky 57 | 58 | [convolutional] 59 | batch_normalize=1 60 | filters=128 61 | size=3 62 | stride=1 63 | pad=1 64 | activation=leaky 65 | 66 | [convolutional] 67 | batch_normalize=1 68 | filters=16 69 | size=1 70 | stride=1 71 | pad=1 72 | activation=leaky 73 | 74 | [convolutional] 75 | batch_normalize=1 76 | filters=128 77 | size=3 78 | stride=1 79 | pad=1 80 | activation=leaky 81 | 82 | [maxpool] 83 | size=2 84 | stride=2 85 | 86 | [convolutional] 87 | batch_normalize=1 88 | filters=32 89 | size=1 90 | stride=1 91 | pad=1 92 | activation=leaky 93 | 94 | [convolutional] 95 | batch_normalize=1 96 | filters=256 97 | size=3 98 | stride=1 99 | pad=1 100 | activation=leaky 101 | 102 | [convolutional] 103 | batch_normalize=1 104 | filters=32 105 | size=1 106 | stride=1 107 | pad=1 108 | activation=leaky 109 | 110 | [convolutional] 111 | batch_normalize=1 112 | filters=256 113 | size=3 114 | stride=1 115 | pad=1 116 | activation=leaky 117 | 118 | [maxpool] 119 | size=2 120 | stride=2 121 | 122 | [convolutional] 123 | batch_normalize=1 124 | filters=64 125 | size=1 126 | stride=1 127 | pad=1 128 | activation=leaky 129 | 130 | [convolutional] 131 | batch_normalize=1 132 | filters=512 133 | size=3 134 | stride=1 135 | pad=1 136 | activation=leaky 137 | 138 | [convolutional] 139 | batch_normalize=1 140 | filters=64 141 | size=1 142 | stride=1 143 | pad=1 144 | activation=leaky 145 | 146 | [convolutional] 147 | batch_normalize=1 148 | filters=512 149 | size=3 150 | stride=1 151 | pad=1 152 | activation=leaky 153 | 154 | [convolutional] 155 | batch_normalize=1 156 | filters=128 157 | size=1 158 | stride=1 159 | pad=1 160 | activation=leaky 161 | 162 | [convolutional] 163 | filters=1000 164 | size=1 165 | stride=1 166 | pad=1 167 | activation=linear 168 | 169 | [avgpool] 170 | 171 | [softmax] 172 | groups=1 173 | 174 | [cost] 175 | type=sse 176 | 177 | -------------------------------------------------------------------------------- /lightnet/data/vgg-16.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=128 3 | subdivisions=4 4 | height=256 5 | width=256 6 | channels=3 7 | learning_rate=0.00001 8 | momentum=0.9 9 | decay=0.0005 10 | 11 | [crop] 12 | crop_height=224 13 | crop_width=224 14 | flip=1 15 | exposure=1 16 | saturation=1 17 | angle=0 18 | 19 | [convolutional] 20 | filters=64 21 | size=3 22 | stride=1 23 | pad=1 24 | activation=relu 25 | 26 | [convolutional] 27 | filters=64 28 | size=3 29 | stride=1 30 | pad=1 31 | activation=relu 32 | 33 | [maxpool] 34 | size=2 35 | stride=2 36 | 37 | [convolutional] 38 | filters=128 39 | size=3 40 | stride=1 41 | pad=1 42 | activation=relu 43 | 44 | [convolutional] 45 | filters=128 46 | size=3 47 | stride=1 48 | pad=1 49 | activation=relu 50 | 51 | [maxpool] 52 | size=2 53 | stride=2 54 | 55 | [convolutional] 56 | filters=256 57 | size=3 58 | stride=1 59 | pad=1 60 | activation=relu 61 | 62 | [convolutional] 63 | filters=256 64 | size=3 65 | stride=1 66 | pad=1 67 | activation=relu 68 | 69 | [convolutional] 70 | filters=256 71 | size=3 72 | stride=1 73 | pad=1 74 | activation=relu 75 | 76 | [maxpool] 77 | size=2 78 | stride=2 79 | 80 | [convolutional] 81 | filters=512 82 | size=3 83 | stride=1 84 | pad=1 85 | activation=relu 86 | 87 | [convolutional] 88 | filters=512 89 | size=3 90 | stride=1 91 | pad=1 92 | activation=relu 93 | 94 | [convolutional] 95 | filters=512 96 | size=3 97 | stride=1 98 | pad=1 99 | activation=relu 100 | 101 | [maxpool] 102 | size=2 103 | stride=2 104 | 105 | [convolutional] 106 | filters=512 107 | size=3 108 | stride=1 109 | pad=1 110 | activation=relu 111 | 112 | [convolutional] 113 | filters=512 114 | size=3 115 | stride=1 116 | pad=1 117 | activation=relu 118 | 119 | [convolutional] 120 | filters=512 121 | size=3 122 | stride=1 123 | pad=1 124 | activation=relu 125 | 126 | [maxpool] 127 | size=2 128 | stride=2 129 | 130 | [connected] 131 | output=4096 132 | activation=relu 133 | 134 | [dropout] 135 | probability=.5 136 | 137 | [connected] 138 | output=4096 139 | activation=relu 140 | 141 | [dropout] 142 | probability=.5 143 | 144 | [connected] 145 | output=1000 146 | activation=linear 147 | 148 | [softmax] 149 | groups=1 150 | 151 | [cost] 152 | type=sse 153 | 154 | -------------------------------------------------------------------------------- /lightnet/data/vgg-conv.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=1 3 | subdivisions=1 4 | width=224 5 | height=224 6 | channels=3 7 | learning_rate=0.00001 8 | momentum=0.9 9 | decay=0.0005 10 | 11 | [convolutional] 12 | filters=64 13 | size=3 14 | stride=1 15 | pad=1 16 | activation=relu 17 | 18 | [convolutional] 19 | filters=64 20 | size=3 21 | stride=1 22 | pad=1 23 | activation=relu 24 | 25 | [maxpool] 26 | size=2 27 | stride=2 28 | 29 | [convolutional] 30 | filters=128 31 | size=3 32 | stride=1 33 | pad=1 34 | activation=relu 35 | 36 | [convolutional] 37 | filters=128 38 | size=3 39 | stride=1 40 | pad=1 41 | activation=relu 42 | 43 | [maxpool] 44 | size=2 45 | stride=2 46 | 47 | [convolutional] 48 | filters=256 49 | size=3 50 | stride=1 51 | pad=1 52 | activation=relu 53 | 54 | [convolutional] 55 | filters=256 56 | size=3 57 | stride=1 58 | pad=1 59 | activation=relu 60 | 61 | [convolutional] 62 | filters=256 63 | size=3 64 | stride=1 65 | pad=1 66 | activation=relu 67 | 68 | [maxpool] 69 | size=2 70 | stride=2 71 | 72 | [convolutional] 73 | filters=512 74 | size=3 75 | stride=1 76 | pad=1 77 | activation=relu 78 | 79 | [convolutional] 80 | filters=512 81 | size=3 82 | stride=1 83 | pad=1 84 | activation=relu 85 | 86 | [convolutional] 87 | filters=512 88 | size=3 89 | stride=1 90 | pad=1 91 | activation=relu 92 | 93 | [maxpool] 94 | size=2 95 | stride=2 96 | 97 | [convolutional] 98 | filters=512 99 | size=3 100 | stride=1 101 | pad=1 102 | activation=relu 103 | 104 | [convolutional] 105 | filters=512 106 | size=3 107 | stride=1 108 | pad=1 109 | activation=relu 110 | 111 | [convolutional] 112 | filters=512 113 | size=3 114 | stride=1 115 | pad=1 116 | activation=relu 117 | 118 | [maxpool] 119 | size=2 120 | stride=2 121 | 122 | -------------------------------------------------------------------------------- /lightnet/data/voc.names: -------------------------------------------------------------------------------- 1 | aeroplane 2 | bicycle 3 | bird 4 | boat 5 | bottle 6 | bus 7 | car 8 | cat 9 | chair 10 | cow 11 | diningtable 12 | dog 13 | horse 14 | motorbike 15 | person 16 | pottedplant 17 | sheep 18 | sofa 19 | train 20 | tvmonitor 21 | -------------------------------------------------------------------------------- /lightnet/data/writing.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=128 3 | subdivisions=2 4 | height=256 5 | width=256 6 | channels=3 7 | learning_rate=0.00000001 8 | momentum=0.9 9 | decay=0.0005 10 | seen=0 11 | 12 | [convolutional] 13 | filters=32 14 | size=3 15 | stride=1 16 | pad=1 17 | activation=leaky 18 | 19 | [convolutional] 20 | filters=32 21 | size=3 22 | stride=1 23 | pad=1 24 | activation=leaky 25 | 26 | [convolutional] 27 | filters=32 28 | size=3 29 | stride=1 30 | pad=1 31 | activation=leaky 32 | 33 | [convolutional] 34 | filters=1 35 | size=3 36 | stride=1 37 | pad=1 38 | activation=logistic 39 | 40 | [cost] 41 | 42 | -------------------------------------------------------------------------------- /lightnet/data/yolo-voc.2.0.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=64 3 | subdivisions=8 4 | height=416 5 | width=416 6 | channels=3 7 | momentum=0.9 8 | decay=0.0005 9 | angle=0 10 | saturation = 1.5 11 | exposure = 1.5 12 | hue=.1 13 | 14 | learning_rate=0.0001 15 | max_batches = 45000 16 | policy=steps 17 | steps=100,25000,35000 18 | scales=10,.1,.1 19 | 20 | [convolutional] 21 | batch_normalize=1 22 | filters=32 23 | size=3 24 | stride=1 25 | pad=1 26 | activation=leaky 27 | 28 | [maxpool] 29 | size=2 30 | stride=2 31 | 32 | [convolutional] 33 | batch_normalize=1 34 | filters=64 35 | size=3 36 | stride=1 37 | pad=1 38 | activation=leaky 39 | 40 | [maxpool] 41 | size=2 42 | stride=2 43 | 44 | [convolutional] 45 | batch_normalize=1 46 | filters=128 47 | size=3 48 | stride=1 49 | pad=1 50 | activation=leaky 51 | 52 | [convolutional] 53 | batch_normalize=1 54 | filters=64 55 | size=1 56 | stride=1 57 | pad=1 58 | activation=leaky 59 | 60 | [convolutional] 61 | batch_normalize=1 62 | filters=128 63 | size=3 64 | stride=1 65 | pad=1 66 | activation=leaky 67 | 68 | [maxpool] 69 | size=2 70 | stride=2 71 | 72 | [convolutional] 73 | batch_normalize=1 74 | filters=256 75 | size=3 76 | stride=1 77 | pad=1 78 | activation=leaky 79 | 80 | [convolutional] 81 | batch_normalize=1 82 | filters=128 83 | size=1 84 | stride=1 85 | pad=1 86 | activation=leaky 87 | 88 | [convolutional] 89 | batch_normalize=1 90 | filters=256 91 | size=3 92 | stride=1 93 | pad=1 94 | activation=leaky 95 | 96 | [maxpool] 97 | size=2 98 | stride=2 99 | 100 | [convolutional] 101 | batch_normalize=1 102 | filters=512 103 | size=3 104 | stride=1 105 | pad=1 106 | activation=leaky 107 | 108 | [convolutional] 109 | batch_normalize=1 110 | filters=256 111 | size=1 112 | stride=1 113 | pad=1 114 | activation=leaky 115 | 116 | [convolutional] 117 | batch_normalize=1 118 | filters=512 119 | size=3 120 | stride=1 121 | pad=1 122 | activation=leaky 123 | 124 | [convolutional] 125 | batch_normalize=1 126 | filters=256 127 | size=1 128 | stride=1 129 | pad=1 130 | activation=leaky 131 | 132 | [convolutional] 133 | batch_normalize=1 134 | filters=512 135 | size=3 136 | stride=1 137 | pad=1 138 | activation=leaky 139 | 140 | [maxpool] 141 | size=2 142 | stride=2 143 | 144 | [convolutional] 145 | batch_normalize=1 146 | filters=1024 147 | size=3 148 | stride=1 149 | pad=1 150 | activation=leaky 151 | 152 | [convolutional] 153 | batch_normalize=1 154 | filters=512 155 | size=1 156 | stride=1 157 | pad=1 158 | activation=leaky 159 | 160 | [convolutional] 161 | batch_normalize=1 162 | filters=1024 163 | size=3 164 | stride=1 165 | pad=1 166 | activation=leaky 167 | 168 | [convolutional] 169 | batch_normalize=1 170 | filters=512 171 | size=1 172 | stride=1 173 | pad=1 174 | activation=leaky 175 | 176 | [convolutional] 177 | batch_normalize=1 178 | filters=1024 179 | size=3 180 | stride=1 181 | pad=1 182 | activation=leaky 183 | 184 | 185 | ####### 186 | 187 | [convolutional] 188 | batch_normalize=1 189 | size=3 190 | stride=1 191 | pad=1 192 | filters=1024 193 | activation=leaky 194 | 195 | [convolutional] 196 | batch_normalize=1 197 | size=3 198 | stride=1 199 | pad=1 200 | filters=1024 201 | activation=leaky 202 | 203 | [route] 204 | layers=-9 205 | 206 | [reorg] 207 | stride=2 208 | 209 | [route] 210 | layers=-1,-3 211 | 212 | [convolutional] 213 | batch_normalize=1 214 | size=3 215 | stride=1 216 | pad=1 217 | filters=1024 218 | activation=leaky 219 | 220 | [convolutional] 221 | size=1 222 | stride=1 223 | pad=1 224 | filters=125 225 | activation=linear 226 | 227 | [region] 228 | anchors = 1.08,1.19, 3.42,4.41, 6.63,11.38, 9.42,5.11, 16.62,10.52 229 | bias_match=1 230 | classes=20 231 | coords=4 232 | num=5 233 | softmax=1 234 | jitter=.2 235 | rescore=1 236 | 237 | object_scale=5 238 | noobject_scale=1 239 | class_scale=1 240 | coord_scale=1 241 | 242 | absolute=1 243 | thresh = .6 244 | random=0 245 | -------------------------------------------------------------------------------- /lightnet/data/yolo-voc.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | # Testing 3 | batch=1 4 | subdivisions=1 5 | # Training 6 | # batch=64 7 | # subdivisions=8 8 | height=416 9 | width=416 10 | channels=3 11 | momentum=0.9 12 | decay=0.0005 13 | angle=0 14 | saturation = 1.5 15 | exposure = 1.5 16 | hue=.1 17 | 18 | learning_rate=0.001 19 | burn_in=1000 20 | max_batches = 80200 21 | policy=steps 22 | steps=40000,60000 23 | scales=.1,.1 24 | 25 | [convolutional] 26 | batch_normalize=1 27 | filters=32 28 | size=3 29 | stride=1 30 | pad=1 31 | activation=leaky 32 | 33 | [maxpool] 34 | size=2 35 | stride=2 36 | 37 | [convolutional] 38 | batch_normalize=1 39 | filters=64 40 | size=3 41 | stride=1 42 | pad=1 43 | activation=leaky 44 | 45 | [maxpool] 46 | size=2 47 | stride=2 48 | 49 | [convolutional] 50 | batch_normalize=1 51 | filters=128 52 | size=3 53 | stride=1 54 | pad=1 55 | activation=leaky 56 | 57 | [convolutional] 58 | batch_normalize=1 59 | filters=64 60 | size=1 61 | stride=1 62 | pad=1 63 | activation=leaky 64 | 65 | [convolutional] 66 | batch_normalize=1 67 | filters=128 68 | size=3 69 | stride=1 70 | pad=1 71 | activation=leaky 72 | 73 | [maxpool] 74 | size=2 75 | stride=2 76 | 77 | [convolutional] 78 | batch_normalize=1 79 | filters=256 80 | size=3 81 | stride=1 82 | pad=1 83 | activation=leaky 84 | 85 | [convolutional] 86 | batch_normalize=1 87 | filters=128 88 | size=1 89 | stride=1 90 | pad=1 91 | activation=leaky 92 | 93 | [convolutional] 94 | batch_normalize=1 95 | filters=256 96 | size=3 97 | stride=1 98 | pad=1 99 | activation=leaky 100 | 101 | [maxpool] 102 | size=2 103 | stride=2 104 | 105 | [convolutional] 106 | batch_normalize=1 107 | filters=512 108 | size=3 109 | stride=1 110 | pad=1 111 | activation=leaky 112 | 113 | [convolutional] 114 | batch_normalize=1 115 | filters=256 116 | size=1 117 | stride=1 118 | pad=1 119 | activation=leaky 120 | 121 | [convolutional] 122 | batch_normalize=1 123 | filters=512 124 | size=3 125 | stride=1 126 | pad=1 127 | activation=leaky 128 | 129 | [convolutional] 130 | batch_normalize=1 131 | filters=256 132 | size=1 133 | stride=1 134 | pad=1 135 | activation=leaky 136 | 137 | [convolutional] 138 | batch_normalize=1 139 | filters=512 140 | size=3 141 | stride=1 142 | pad=1 143 | activation=leaky 144 | 145 | [maxpool] 146 | size=2 147 | stride=2 148 | 149 | [convolutional] 150 | batch_normalize=1 151 | filters=1024 152 | size=3 153 | stride=1 154 | pad=1 155 | activation=leaky 156 | 157 | [convolutional] 158 | batch_normalize=1 159 | filters=512 160 | size=1 161 | stride=1 162 | pad=1 163 | activation=leaky 164 | 165 | [convolutional] 166 | batch_normalize=1 167 | filters=1024 168 | size=3 169 | stride=1 170 | pad=1 171 | activation=leaky 172 | 173 | [convolutional] 174 | batch_normalize=1 175 | filters=512 176 | size=1 177 | stride=1 178 | pad=1 179 | activation=leaky 180 | 181 | [convolutional] 182 | batch_normalize=1 183 | filters=1024 184 | size=3 185 | stride=1 186 | pad=1 187 | activation=leaky 188 | 189 | 190 | ####### 191 | 192 | [convolutional] 193 | batch_normalize=1 194 | size=3 195 | stride=1 196 | pad=1 197 | filters=1024 198 | activation=leaky 199 | 200 | [convolutional] 201 | batch_normalize=1 202 | size=3 203 | stride=1 204 | pad=1 205 | filters=1024 206 | activation=leaky 207 | 208 | [route] 209 | layers=-9 210 | 211 | [convolutional] 212 | batch_normalize=1 213 | size=1 214 | stride=1 215 | pad=1 216 | filters=64 217 | activation=leaky 218 | 219 | [reorg] 220 | stride=2 221 | 222 | [route] 223 | layers=-1,-4 224 | 225 | [convolutional] 226 | batch_normalize=1 227 | size=3 228 | stride=1 229 | pad=1 230 | filters=1024 231 | activation=leaky 232 | 233 | [convolutional] 234 | size=1 235 | stride=1 236 | pad=1 237 | filters=125 238 | activation=linear 239 | 240 | 241 | [region] 242 | anchors = 1.3221, 1.73145, 3.19275, 4.00944, 5.05587, 8.09892, 9.47112, 4.84053, 11.2364, 10.0071 243 | bias_match=1 244 | classes=20 245 | coords=4 246 | num=5 247 | softmax=1 248 | jitter=.3 249 | rescore=1 250 | 251 | object_scale=5 252 | noobject_scale=1 253 | class_scale=1 254 | coord_scale=1 255 | 256 | absolute=1 257 | thresh = .6 258 | random=1 259 | -------------------------------------------------------------------------------- /lightnet/data/yolo.2.0.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=1 3 | subdivisions=1 4 | width=416 5 | height=416 6 | channels=3 7 | momentum=0.9 8 | decay=0.0005 9 | angle=0 10 | saturation = 1.5 11 | exposure = 1.5 12 | hue=.1 13 | 14 | learning_rate=0.001 15 | max_batches = 120000 16 | policy=steps 17 | steps=-1,100,80000,100000 18 | scales=.1,10,.1,.1 19 | 20 | [convolutional] 21 | batch_normalize=1 22 | filters=32 23 | size=3 24 | stride=1 25 | pad=1 26 | activation=leaky 27 | 28 | [maxpool] 29 | size=2 30 | stride=2 31 | 32 | [convolutional] 33 | batch_normalize=1 34 | filters=64 35 | size=3 36 | stride=1 37 | pad=1 38 | activation=leaky 39 | 40 | [maxpool] 41 | size=2 42 | stride=2 43 | 44 | [convolutional] 45 | batch_normalize=1 46 | filters=128 47 | size=3 48 | stride=1 49 | pad=1 50 | activation=leaky 51 | 52 | [convolutional] 53 | batch_normalize=1 54 | filters=64 55 | size=1 56 | stride=1 57 | pad=1 58 | activation=leaky 59 | 60 | [convolutional] 61 | batch_normalize=1 62 | filters=128 63 | size=3 64 | stride=1 65 | pad=1 66 | activation=leaky 67 | 68 | [maxpool] 69 | size=2 70 | stride=2 71 | 72 | [convolutional] 73 | batch_normalize=1 74 | filters=256 75 | size=3 76 | stride=1 77 | pad=1 78 | activation=leaky 79 | 80 | [convolutional] 81 | batch_normalize=1 82 | filters=128 83 | size=1 84 | stride=1 85 | pad=1 86 | activation=leaky 87 | 88 | [convolutional] 89 | batch_normalize=1 90 | filters=256 91 | size=3 92 | stride=1 93 | pad=1 94 | activation=leaky 95 | 96 | [maxpool] 97 | size=2 98 | stride=2 99 | 100 | [convolutional] 101 | batch_normalize=1 102 | filters=512 103 | size=3 104 | stride=1 105 | pad=1 106 | activation=leaky 107 | 108 | [convolutional] 109 | batch_normalize=1 110 | filters=256 111 | size=1 112 | stride=1 113 | pad=1 114 | activation=leaky 115 | 116 | [convolutional] 117 | batch_normalize=1 118 | filters=512 119 | size=3 120 | stride=1 121 | pad=1 122 | activation=leaky 123 | 124 | [convolutional] 125 | batch_normalize=1 126 | filters=256 127 | size=1 128 | stride=1 129 | pad=1 130 | activation=leaky 131 | 132 | [convolutional] 133 | batch_normalize=1 134 | filters=512 135 | size=3 136 | stride=1 137 | pad=1 138 | activation=leaky 139 | 140 | [maxpool] 141 | size=2 142 | stride=2 143 | 144 | [convolutional] 145 | batch_normalize=1 146 | filters=1024 147 | size=3 148 | stride=1 149 | pad=1 150 | activation=leaky 151 | 152 | [convolutional] 153 | batch_normalize=1 154 | filters=512 155 | size=1 156 | stride=1 157 | pad=1 158 | activation=leaky 159 | 160 | [convolutional] 161 | batch_normalize=1 162 | filters=1024 163 | size=3 164 | stride=1 165 | pad=1 166 | activation=leaky 167 | 168 | [convolutional] 169 | batch_normalize=1 170 | filters=512 171 | size=1 172 | stride=1 173 | pad=1 174 | activation=leaky 175 | 176 | [convolutional] 177 | batch_normalize=1 178 | filters=1024 179 | size=3 180 | stride=1 181 | pad=1 182 | activation=leaky 183 | 184 | 185 | ####### 186 | 187 | [convolutional] 188 | batch_normalize=1 189 | size=3 190 | stride=1 191 | pad=1 192 | filters=1024 193 | activation=leaky 194 | 195 | [convolutional] 196 | batch_normalize=1 197 | size=3 198 | stride=1 199 | pad=1 200 | filters=1024 201 | activation=leaky 202 | 203 | [route] 204 | layers=-9 205 | 206 | [reorg] 207 | stride=2 208 | 209 | [route] 210 | layers=-1,-3 211 | 212 | [convolutional] 213 | batch_normalize=1 214 | size=3 215 | stride=1 216 | pad=1 217 | filters=1024 218 | activation=leaky 219 | 220 | [convolutional] 221 | size=1 222 | stride=1 223 | pad=1 224 | filters=425 225 | activation=linear 226 | 227 | [region] 228 | anchors = 0.738768,0.874946, 2.42204,2.65704, 4.30971,7.04493, 10.246,4.59428, 12.6868,11.8741 229 | bias_match=1 230 | classes=80 231 | coords=4 232 | num=5 233 | softmax=1 234 | jitter=.2 235 | rescore=1 236 | 237 | object_scale=5 238 | noobject_scale=1 239 | class_scale=1 240 | coord_scale=1 241 | 242 | absolute=1 243 | thresh = .6 244 | random=0 245 | -------------------------------------------------------------------------------- /lightnet/data/yolo.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | # Testing 3 | #batch=1 4 | #subdivisions=1 5 | # Training 6 | batch=64 7 | subdivisions=8 8 | width=608 9 | height=608 10 | channels=3 11 | momentum=0.9 12 | decay=0.0005 13 | angle=0 14 | saturation = 1.5 15 | exposure = 1.5 16 | hue=.1 17 | 18 | learning_rate=0.001 19 | burn_in=1000 20 | max_batches = 500200 21 | policy=steps 22 | steps=400000,450000 23 | scales=.1,.1 24 | 25 | [convolutional] 26 | batch_normalize=1 27 | filters=32 28 | size=3 29 | stride=1 30 | pad=1 31 | activation=leaky 32 | 33 | [maxpool] 34 | size=2 35 | stride=2 36 | 37 | [convolutional] 38 | batch_normalize=1 39 | filters=64 40 | size=3 41 | stride=1 42 | pad=1 43 | activation=leaky 44 | 45 | [maxpool] 46 | size=2 47 | stride=2 48 | 49 | [convolutional] 50 | batch_normalize=1 51 | filters=128 52 | size=3 53 | stride=1 54 | pad=1 55 | activation=leaky 56 | 57 | [convolutional] 58 | batch_normalize=1 59 | filters=64 60 | size=1 61 | stride=1 62 | pad=1 63 | activation=leaky 64 | 65 | [convolutional] 66 | batch_normalize=1 67 | filters=128 68 | size=3 69 | stride=1 70 | pad=1 71 | activation=leaky 72 | 73 | [maxpool] 74 | size=2 75 | stride=2 76 | 77 | [convolutional] 78 | batch_normalize=1 79 | filters=256 80 | size=3 81 | stride=1 82 | pad=1 83 | activation=leaky 84 | 85 | [convolutional] 86 | batch_normalize=1 87 | filters=128 88 | size=1 89 | stride=1 90 | pad=1 91 | activation=leaky 92 | 93 | [convolutional] 94 | batch_normalize=1 95 | filters=256 96 | size=3 97 | stride=1 98 | pad=1 99 | activation=leaky 100 | 101 | [maxpool] 102 | size=2 103 | stride=2 104 | 105 | [convolutional] 106 | batch_normalize=1 107 | filters=512 108 | size=3 109 | stride=1 110 | pad=1 111 | activation=leaky 112 | 113 | [convolutional] 114 | batch_normalize=1 115 | filters=256 116 | size=1 117 | stride=1 118 | pad=1 119 | activation=leaky 120 | 121 | [convolutional] 122 | batch_normalize=1 123 | filters=512 124 | size=3 125 | stride=1 126 | pad=1 127 | activation=leaky 128 | 129 | [convolutional] 130 | batch_normalize=1 131 | filters=256 132 | size=1 133 | stride=1 134 | pad=1 135 | activation=leaky 136 | 137 | [convolutional] 138 | batch_normalize=1 139 | filters=512 140 | size=3 141 | stride=1 142 | pad=1 143 | activation=leaky 144 | 145 | [maxpool] 146 | size=2 147 | stride=2 148 | 149 | [convolutional] 150 | batch_normalize=1 151 | filters=1024 152 | size=3 153 | stride=1 154 | pad=1 155 | activation=leaky 156 | 157 | [convolutional] 158 | batch_normalize=1 159 | filters=512 160 | size=1 161 | stride=1 162 | pad=1 163 | activation=leaky 164 | 165 | [convolutional] 166 | batch_normalize=1 167 | filters=1024 168 | size=3 169 | stride=1 170 | pad=1 171 | activation=leaky 172 | 173 | [convolutional] 174 | batch_normalize=1 175 | filters=512 176 | size=1 177 | stride=1 178 | pad=1 179 | activation=leaky 180 | 181 | [convolutional] 182 | batch_normalize=1 183 | filters=1024 184 | size=3 185 | stride=1 186 | pad=1 187 | activation=leaky 188 | 189 | 190 | ####### 191 | 192 | [convolutional] 193 | batch_normalize=1 194 | size=3 195 | stride=1 196 | pad=1 197 | filters=1024 198 | activation=leaky 199 | 200 | [convolutional] 201 | batch_normalize=1 202 | size=3 203 | stride=1 204 | pad=1 205 | filters=1024 206 | activation=leaky 207 | 208 | [route] 209 | layers=-9 210 | 211 | [convolutional] 212 | batch_normalize=1 213 | size=1 214 | stride=1 215 | pad=1 216 | filters=64 217 | activation=leaky 218 | 219 | [reorg] 220 | stride=2 221 | 222 | [route] 223 | layers=-1,-4 224 | 225 | [convolutional] 226 | batch_normalize=1 227 | size=3 228 | stride=1 229 | pad=1 230 | filters=1024 231 | activation=leaky 232 | 233 | [convolutional] 234 | size=1 235 | stride=1 236 | pad=1 237 | filters=425 238 | activation=linear 239 | 240 | 241 | [region] 242 | anchors = 0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828 243 | bias_match=1 244 | classes=80 245 | coords=4 246 | num=5 247 | softmax=1 248 | jitter=.3 249 | rescore=1 250 | 251 | object_scale=5 252 | noobject_scale=1 253 | class_scale=1 254 | coord_scale=1 255 | 256 | absolute=1 257 | thresh = .6 258 | random=1 259 | -------------------------------------------------------------------------------- /lightnet/data/yolo9000.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | # Testing 3 | batch=1 4 | subdivisions=1 5 | # Training 6 | # batch=64 7 | # subdivisions=8 8 | batch=1 9 | subdivisions=1 10 | height=544 11 | width=544 12 | channels=3 13 | momentum=0.9 14 | decay=0.0005 15 | 16 | learning_rate=0.001 17 | burn_in=1000 18 | max_batches = 500200 19 | policy=steps 20 | steps=400000,450000 21 | scales=.1,.1 22 | 23 | hue=.1 24 | saturation=.75 25 | exposure=.75 26 | 27 | [convolutional] 28 | batch_normalize=1 29 | filters=32 30 | size=3 31 | stride=1 32 | pad=1 33 | activation=leaky 34 | 35 | [maxpool] 36 | size=2 37 | stride=2 38 | 39 | [convolutional] 40 | batch_normalize=1 41 | filters=64 42 | size=3 43 | stride=1 44 | pad=1 45 | activation=leaky 46 | 47 | [maxpool] 48 | size=2 49 | stride=2 50 | 51 | [convolutional] 52 | batch_normalize=1 53 | filters=128 54 | size=3 55 | stride=1 56 | pad=1 57 | activation=leaky 58 | 59 | [convolutional] 60 | batch_normalize=1 61 | filters=64 62 | size=1 63 | stride=1 64 | pad=1 65 | activation=leaky 66 | 67 | [convolutional] 68 | batch_normalize=1 69 | filters=128 70 | size=3 71 | stride=1 72 | pad=1 73 | activation=leaky 74 | 75 | [maxpool] 76 | size=2 77 | stride=2 78 | 79 | [convolutional] 80 | batch_normalize=1 81 | filters=256 82 | size=3 83 | stride=1 84 | pad=1 85 | activation=leaky 86 | 87 | [convolutional] 88 | batch_normalize=1 89 | filters=128 90 | size=1 91 | stride=1 92 | pad=1 93 | activation=leaky 94 | 95 | [convolutional] 96 | batch_normalize=1 97 | filters=256 98 | size=3 99 | stride=1 100 | pad=1 101 | activation=leaky 102 | 103 | [maxpool] 104 | size=2 105 | stride=2 106 | 107 | [convolutional] 108 | batch_normalize=1 109 | filters=512 110 | size=3 111 | stride=1 112 | pad=1 113 | activation=leaky 114 | 115 | [convolutional] 116 | batch_normalize=1 117 | filters=256 118 | size=1 119 | stride=1 120 | pad=1 121 | activation=leaky 122 | 123 | [convolutional] 124 | batch_normalize=1 125 | filters=512 126 | size=3 127 | stride=1 128 | pad=1 129 | activation=leaky 130 | 131 | [convolutional] 132 | batch_normalize=1 133 | filters=256 134 | size=1 135 | stride=1 136 | pad=1 137 | activation=leaky 138 | 139 | [convolutional] 140 | batch_normalize=1 141 | filters=512 142 | size=3 143 | stride=1 144 | pad=1 145 | activation=leaky 146 | 147 | [maxpool] 148 | size=2 149 | stride=2 150 | 151 | [convolutional] 152 | batch_normalize=1 153 | filters=1024 154 | size=3 155 | stride=1 156 | pad=1 157 | activation=leaky 158 | 159 | [convolutional] 160 | batch_normalize=1 161 | filters=512 162 | size=1 163 | stride=1 164 | pad=1 165 | activation=leaky 166 | 167 | [convolutional] 168 | batch_normalize=1 169 | filters=1024 170 | size=3 171 | stride=1 172 | pad=1 173 | activation=leaky 174 | 175 | [convolutional] 176 | batch_normalize=1 177 | filters=512 178 | size=1 179 | stride=1 180 | pad=1 181 | activation=leaky 182 | 183 | [convolutional] 184 | batch_normalize=1 185 | filters=1024 186 | size=3 187 | stride=1 188 | pad=1 189 | activation=leaky 190 | 191 | [convolutional] 192 | filters=28269 193 | size=1 194 | stride=1 195 | pad=1 196 | activation=linear 197 | 198 | [region] 199 | anchors = 0.77871, 1.14074, 3.00525, 4.31277, 9.22725, 9.61974 200 | bias_match=1 201 | classes=9418 202 | coords=4 203 | num=3 204 | softmax=1 205 | jitter=.2 206 | rescore=1 207 | 208 | object_scale=5 209 | noobject_scale=1 210 | class_scale=1 211 | coord_scale=1 212 | 213 | thresh = .6 214 | absolute=1 215 | random=1 216 | 217 | tree=data/9k.tree 218 | map = data/coco9k.map 219 | -------------------------------------------------------------------------------- /lightnet/util.py: -------------------------------------------------------------------------------- 1 | from contextlib import contextmanager 2 | from tempfile import mkdtemp 3 | import shutil 4 | 5 | 6 | @contextmanager 7 | def make_temp_dir(): 8 | path = mkdtemp() 9 | yield path 10 | shutil.rmtree(path) 11 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pathlib 2 | numpy 3 | plac 4 | requests 5 | msgpack-python 6 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import shutil 3 | import io 4 | import os 5 | import json 6 | import distutils.command.build_ext 7 | import subprocess 8 | import sys 9 | from setuptools import Extension, setup 10 | import platform 11 | import numpy 12 | 13 | try: 14 | import cython 15 | use_cython = True 16 | except ImportError: 17 | use_cython = False 18 | 19 | 20 | class ExtensionBuilder(distutils.command.build_ext.build_ext): 21 | def build_extensions(self): 22 | if use_cython: 23 | subprocess.check_call([sys.executable, 'bin/cythonize.py'], 24 | env=os.environ) 25 | 26 | cuda_include_dir = '/usr/local/cuda/include/' 27 | if 'CUDA_INCLUDE_DIR' in os.environ: 28 | cuda_include_dir = os.environ['CUDA_INCLUDE_DIR'] 29 | 30 | cuda_library_dir = '/usr/local/cuda/lib64/' 31 | if 'CUDA_LIBRARY_DIR' in os.environ: 32 | cuda_library_dir = os.environ['CUDA_LIBRARY_DIR'] 33 | 34 | use_gpu = False 35 | if 'GPU' in os.environ and os.environ['GPU'] == '1': 36 | use_gpu = True 37 | 38 | use_cudnn = False 39 | if 'CUDNN' in os.environ and os.environ['CUDNN'] == '1': 40 | use_gpu = True 41 | use_cudnn = True 42 | 43 | make_command = ['make'] 44 | if use_gpu: 45 | make_command.append('GPU=1') 46 | if use_cudnn: 47 | make_command.append('CUDNN=1') 48 | 49 | darknet_dir = os.path.join(PWD, 'lightnet', '_darknet') 50 | subprocess.check_call(make_command, cwd=darknet_dir) 51 | 52 | for e in self.extensions: 53 | e.include_dirs.append(numpy.get_include()) 54 | e.undef_macros.append("FORTIFY_SOURCE") 55 | e.extra_compile_args.append("-DCBLAS") 56 | e.extra_compile_args.append('-g') 57 | e.library_dirs.append(darknet_dir) 58 | e.extra_link_args.append('-g') 59 | e.extra_link_args.append('-ldarknet') 60 | if use_gpu: 61 | e.include_dirs.append(cuda_include_dir) 62 | e.library_dirs.append(cuda_library_dir) 63 | e.extra_link_args.append('-lcuda') 64 | e.extra_link_args.append('-lcudart') 65 | e.extra_link_args.append('-lcublas') 66 | e.extra_link_args.append('-lcurand') 67 | e.extra_link_args.append('-lstdc++') 68 | if use_cudnn: 69 | e.extra_link_args.append('-lcudnn') 70 | if sys.platform == 'darwin': 71 | e.extra_compile_args.append('-D__APPLE__') 72 | e.extra_link_args.append('-lblas') 73 | else: 74 | e.extra_link_args.append('-lopenblas') 75 | distutils.command.build_ext.build_ext.build_extensions(self) 76 | 77 | 78 | def get_c_sources(start_dir): 79 | c_sources = [] 80 | excludes = [] 81 | for path, subdirs, files in os.walk(start_dir): 82 | for exc in excludes: 83 | if exc in path: 84 | break 85 | else: 86 | for name in files: 87 | if name.endswith('.c'): 88 | c_sources.append(os.path.join(path, name)) 89 | return c_sources 90 | 91 | 92 | PWD = os.path.join(os.path.dirname(__file__)) 93 | INCLUDE = os.path.join(PWD, 'lightnet', '_darknet') 94 | 95 | c_files = get_c_sources(os.path.join(PWD, 'lightnet', '_darknet')) 96 | 97 | with io.open(os.path.join(PWD, 'lightnet', 'about.py'), encoding='utf8') as f: 98 | about = {} 99 | exec(f.read(), about) 100 | 101 | with io.open(os.path.join(PWD, 'README.rst'), encoding='utf8') as f: 102 | readme = f.read() 103 | 104 | setup( 105 | setup_requires=['numpy'], 106 | install_requires=['numpy', 'plac', 'requests', 'pathlib', 'tqdm', 107 | 'msgpack-python'], 108 | ext_modules=[ 109 | Extension('lightnet.lightnet', ['lightnet/lightnet.c']), 110 | ], 111 | cmdclass={'build_ext': ExtensionBuilder}, 112 | package_data={'': ['*.json', '*.pyx', '*.pxd', '_darknet/*.h', 113 | 'data/*.cfg', 'data/*.template', 'data/*.names'] + c_files}, 114 | 115 | name=about['__title__'], 116 | zip_safe=False, 117 | packages=['lightnet'], 118 | version=about['__version__'], 119 | author=about['__author__'], 120 | author_email=about['__email__'], 121 | url=about['__uri__'], 122 | license=about['__license__'], 123 | description=about['__summary__'], 124 | long_description=readme, 125 | classifiers=[ 126 | 'Development Status :: 4 - Beta', 127 | 'Environment :: Console', 128 | 'Intended Audience :: Developers', 129 | 'Intended Audience :: Information Technology', 130 | 'License :: OSI Approved :: MIT License', 131 | 'Operating System :: POSIX :: Linux', 132 | 'Operating System :: MacOS :: MacOS X', 133 | 'Programming Language :: Cython', 134 | 'Programming Language :: Python :: 2.6', 135 | 'Programming Language :: Python :: 2.7', 136 | 'Programming Language :: Python :: 3.3', 137 | 'Programming Language :: Python :: 3.4', 138 | 'Programming Language :: Python :: 3.5', 139 | 'Programming Language :: Python :: 3.6', 140 | 'Topic :: Scientific/Engineering' 141 | ], 142 | ) 143 | -------------------------------------------------------------------------------- /tests/COCO_val2014_000000000042.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/explosion/lightnet/e7283d95367ed2288a26f2744ad015f6dc0f17bd/tests/COCO_val2014_000000000042.jpg -------------------------------------------------------------------------------- /tests/test_boxes.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import numpy 3 | from lightnet.lightnet import BoxLabels 4 | 5 | @pytest.fixture 6 | def ids(): 7 | return numpy.asarray([0, 2], dtype='i') 8 | 9 | @pytest.fixture 10 | def xywh(): 11 | return numpy.asarray( 12 | [[2., 2., 2., 2.], 13 | [1., 1., 1., 1.]], dtype='f') 14 | 15 | 16 | def test_BoxLabels_init(ids, xywh): 17 | labels = BoxLabels(ids, xywh) 18 | assert labels.x == list(xywh[:, 0]) 19 | assert labels.y == list(xywh[:, 1]) 20 | assert labels.h == list(xywh[:, 2]) 21 | assert labels.w == list(xywh[:, 3]) 22 | print(labels.left) 23 | print(labels.right) 24 | print(labels.top) 25 | print(labels.bottom) 26 | -------------------------------------------------------------------------------- /tests/test_image.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from numpy.testing import assert_equal 3 | 4 | from lightnet.lightnet import Image 5 | 6 | def test_make_image(): 7 | img = Image.blank(10, 10, 10) 8 | img2 = Image.blank(100, 10, 10) 9 | 10 | def test_random_image(): 11 | img = Image.random(10, 10, 10) 12 | img2 = Image.random(100, 10, 10) 13 | 14 | def test_image_from_bytes(): 15 | path = Path("tests/COCO_val2014_000000000042.jpg") 16 | loaded = Image.load_color(path) 17 | with path.open('rb') as file_: 18 | raw = file_.read() 19 | made = Image.from_bytes(raw) 20 | assert_equal(made.data, loaded.data) 21 | 22 | -------------------------------------------------------------------------------- /tests/test_network.py: -------------------------------------------------------------------------------- 1 | from __future__ import unicode_literals 2 | from lightnet import Network, Image, BoxLabels 3 | from lightnet.lightnet import DetectionData 4 | import numpy 5 | import pytest 6 | from pathlib import Path 7 | 8 | @pytest.fixture 9 | def ids_xywh(): 10 | return (numpy.asarray([16], dtype='i'), 11 | numpy.asarray([[0.606688, 0.341381, 0.544156, 0.510000]], dtype='f')) 12 | 13 | @pytest.fixture 14 | def image(): 15 | return Image.load_color(Path("tests/COCO_val2014_000000000042.jpg")) 16 | 17 | @pytest.fixture 18 | def box_labels(ids_xywh): 19 | ids, xywh = ids_xywh 20 | return BoxLabels(ids, xywh) 21 | 22 | def test_init(): 23 | nn = Network() 24 | 25 | def test_load(): 26 | net = Network.load("tiny-yolo") 27 | 28 | @pytest.mark.xfail 29 | def test_from_disk(image): 30 | net = Network().from_disk(Path('lightnet/data/tiny-yolo').resolve()) 31 | results = net(image) 32 | assert results is not None 33 | 34 | def test_to_from_bytes(image): 35 | net = Network.load('tiny-yolo') 36 | data = net.to_bytes() 37 | results = net(image) 38 | loaded = Network().from_bytes(data) 39 | results2 = net(image) 40 | assert results == results2 41 | 42 | 43 | def test_detect(image): 44 | net = Network.load("tiny-yolo") 45 | result = net(image) 46 | 47 | def test_box_labels(box_labels): 48 | pass 49 | 50 | 51 | def test_detection_data(image, box_labels): 52 | net = Network.load("tiny-yolo") 53 | data = DetectionData([image], [box_labels], 54 | net.width, net.height, net.max_boxes, net.num_classes) 55 | #assert data.X_shape == (1, net.width * net.height * 3) 56 | #assert data.y_shape == (1, net.num_boxes * 5) 57 | 58 | def test_update(image, box_labels): 59 | net = Network.load("tiny-yolo") 60 | for i in range(10): 61 | loss = net.update([image], [box_labels]) 62 | print(loss) 63 | 64 | 65 | def test_evaluate(image, box_labels): 66 | net = Network.load("tiny-yolo") 67 | acc = net.evaluate([image], [box_labels]) 68 | assert 'fp' in acc 69 | assert 'fn' in acc 70 | assert 'tp' in acc 71 | assert 'r' in acc 72 | assert 'p' in acc 73 | assert 'f' in acc 74 | --------------------------------------------------------------------------------