/ofxDarknet

darknet neural network addon for openFrameworks

Primary LanguageCMIT LicenseMIT

ofxDarknet

ofxDarknet is a openFrameworks wrapper for darknet.

Darknet is an open source neural network framework written in C and CUDA. It is fast, easy to install, and supports CPU and GPU computation. http://pjreddie.com/darknet/

Features

YOLO: Real-Time Object Detection aka Dense Captioning (http://pjreddie.com/darknet/yolo/)

Darknet comes with two pre-trained models for this task. Additionally each has a smaller (and faster) but therefore less accurate version:

MS COCO dataset (80 different classes)

	std::string datacfg = ofToDataPath( "cfg/coco.data" );
	std::string cfgfile = ofToDataPath( "cfg/tiny-yolo.cfg" );
	std::string weightfile = ofToDataPath( "tiny-yolo.weights" );
	std::string nameslist = ofToDataPath( "cfg/names.list" );
	darknet.init( cfgfile, weightfile, datacfg, nameslist );

Pascal VOC dataset (20 different classes)

	std::string datacfg = ofToDataPath( "cfg/voc.data" );
	std::string cfgfile = ofToDataPath( "cfg/tiny-yolo-voc.cfg" );
	std::string weightfile = ofToDataPath( "tiny-yolo-voc.weights" );
	std::string nameslist = ofToDataPath( "cfg/voc.names" );
	darknet.init( cfgfile, weightfile, datacfg, nameslist );
	float thresh = 0.25;
	std::vector< detected_object > detections = darknet.yolo( image.getPixelsRef(), thresh );

	for( detected_object d : detections )
	{
		ofSetColor( d.color );
		glLineWidth( ofMap( d.probability, 0, 1, 0, 8 ) );
		ofNoFill();
		ofDrawRectangle( d.rect );
		ofDrawBitmapStringHighlight( d.label + ": " + ofToString(d.probability), d.rect.x, d.rect.y + 20 );
	}

YOLO2

Imagenet Classification (http://pjreddie.com/darknet/imagenet/)

In order to classify an image with more classes, this is the spot. This classifies an image according to the 1000-class ImageNet Challenge.

	std::string datacfg = ofToDataPath( "cfg/imagenet1k.data" );
	std::string cfgfile = ofToDataPath( "cfg/darknet.cfg" );
	std::string weightfile = ofToDataPath( "darknet.weights" );
	std::string nameslist = ofToDataPath( "cfg/imagenet.shortnames.list" );
	darknet.init( cfgfile, weightfile, datacfg, nameslist );

	classifications = darknet.classify( image.getPixelsRef() );
	int offset = 20;
	for( classification c : classifications )
	{
		std::stringstream ss;
		ss << c.label << " : " << ofToString( c.probability );
		ofDrawBitmapStringHighlight( ss.str(), 20, offset );
		offset += 20;
	}

Classification

vgg-conv.cfg & vgg-conv.weights

	std::string cfgfile = ofToDataPath( "cfg/vgg-conv.cfg" );
	std::string weightfile = ofToDataPath( "vgg-conv.weights" );
	darknet.init( cfgfile, weightfile );
	
	int max_layer = 13;
	int range = 3;
	int norm = 1;
	int rounds = 4;
	int iters = 20;
	int octaves = 4;
	float rate = 0.01;
	float thresh = 1.0;
	nightmare = darknet.nightmate( image.getPixelsRef(), max_layer, range, norm, rounds, iters, octaves, rate, thresh );

DeepDream

Darknet pre-trained weights files:

ofxDarknet custom pre-trained weight files (each trained for 20h on NVidia TitanX):

  • Anonymous - Hypersphere Hypersphere, written by Anonymous with the help of the 4chan board /lit/ (of The Legacy of Totalitarianism in a Tundra fame) is an epic tale spanning over 700 pages. A postmodern collaborative writing effort containing Slavoj Žižek erotica, top secret Donald Trump emails, poetry, repair instructions for future cars, a history of bottles in the Ottoman empire; actually, it contains everything since it takes place in the Hypersphere, and the Hypersphere is a big place; really big in fact.
  • Books on art history & aesthetics
  • Books on digital culture
	std::string cfgfile = ofToDataPath( "cfg/rnn.cfg" );
	std::string weightfile = ofToDataPath( "shakespeare.weights" );
	darknet.init( cfgfile, weightfile );

	int character_count = 100;
	float temperature = 0.8;
	std::string seed_text = "openframeworks is ";
	std::string generated_text = darknet.rnn( character_count, seed_text, temperature );

RNN

You can train your own RNN models with darknet

	// no need to init
	darknet.train_rnn( ofToDataPath( "training_text.txt" ), "cfg/rnn.cfg" );

Setup

Windows

Install the dependencies for building darknet on Windows 10:

There are some more necessary steps that don't work with the OF project generator:

  • Compile as Debug or Release in x64 mode
  • Within VS2015 Solution Explorer, rightclick on the generated project -> Build Dependencies -> Build Customizations -> Tick CUDA 8.0
  • C/C++ -> All Options -> Compile As -> Default
  • Copy pthreadVC2.dll from ofxDarknet\libs\3rdparty\dll\x64 to your applications bin folder

OSX

An OSX version is on the way and will be updated here..

Training your own models

YOLO

tcb

Credits