Does Oneiros Dream?

そこら辺にいるクソガキのブログ

Which "pooling" do you choose?

In the main "pooling" method, there is max pooling.

If the background in a figure is almost uniform, "Max pooling" must be best practice.

Is that right?

I tried the following.
1. Conv pooling
2. Conv pooling and max pooling
3. max pooling

The accuracy of each method is following.

1. 98.5 - 99.1%
2. 99.0 - 99.2%
3. 99.3%

Therefore, If the background in a figure is almost uniform, "Max pooling" is best.

The Best Develop Environment for Machine Learning

This is oneiroi.
Recently I started machine learning (Mainly "deep learning") so I will explain best develop environment.

"conda" is best.

1. Install "conda" from pyenv
( Install one of the following: conda, mini-conda, anaconda )

2. Install python and opencv form conda

$ conda create -n py33con python=3.3 anaconda
$ source activete py33con
$ python
$ conda install -c https://conda.binstar.org/menpo opencv

3. enjoy

How to use OpenGL for OSX

Difference by the OS of OpenGL is below.

Mac:

#include <GLUT/glut.h>
#include <OpenGL/gl.h>

Windows & Linux:

#include <GL/glut.h>
#include <GL/gl.h>

ex.

#ifdef __APPLE__
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#else
#include <GL/glut.h>
#include <GL/gl.h>
#endif

/* Copyright (c) Mark J. Kilgard, 1997. */

/* This program is freely distributable without licensing fees 
   and is provided without guarantee or warrantee expressed or 
   implied. This program is -not- in the public domain. */

/* This program was requested by Patrick Earl; hopefully someone else
   will write the equivalent Direct3D immediate mode program. */

GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0};  /* Red diffuse light. */
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */
GLfloat n[6][3] = {  /* Normals for the 6 faces of a cube. */
  {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };
GLint faces[6][4] = {  /* Vertex indices for the 6 faces of a cube. */
  {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
GLfloat v[8][3];  /* Will be filled in with X,Y,Z vertexes. */

void drawBox(void)
{
  int i;

  for (i = 0; i < 6; i++) {
    glBegin(GL_QUADS);
    glNormal3fv(&n[i][0]);
    glVertex3fv(&v[faces[i][0]][0]);
    glVertex3fv(&v[faces[i][1]][0]);
    glVertex3fv(&v[faces[i][2]][0]);
    glVertex3fv(&v[faces[i][3]][0]);
    glEnd();
  }
}

void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  drawBox();
  glutSwapBuffers();
}

void init(void)
{
  /* Setup cube vertex data. */
  v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
  v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
  v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
  v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
  v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
  v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;

  /* Enable a single OpenGL light. */
  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHTING);

  /* Use depth buffering for hidden surface elimination. */
  glEnable(GL_DEPTH_TEST);

  /* Setup the view of the cube. */
  glMatrixMode(GL_PROJECTION);
  gluPerspective( /* field of view in degree */ 40.0,
    /* aspect ratio */ 1.0,
    /* Z near */ 1.0, /* Z far */ 10.0);
  glMatrixMode(GL_MODELVIEW);
  gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
    0.0, 0.0, 0.0,      /* center is at (0,0,0) */
    0.0, 1.0, 0.);      /* up is in positive Y direction */

  /* Adjust cube position to be asthetic angle. */
  glTranslatef(0.0, 0.0, -1.0);
  glRotatef(60, 1.0, 0.0, 0.0);
  glRotatef(-20, 0.0, 0.0, 1.0);
}

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutCreateWindow(argc[0]);
  glutDisplayFunc(display);
  init();
  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}

https://www.opengl.org/archives/resources/code/samples/glut_examples/examples/cube.c


Build command

g++ -Wall -Wextra -O0 -g -Isrc  -o Build/debug/./src/main.o -c -MMD -MP -MF Build/debug/./src/main.d src/main.cpp
g++  -o Build/debug/helloworld Build/debug/./src/main.o -framework OpenGL -framework GLUT 

Enjoy!

CentOS + Unicron + Ruby on Rails + Nginx

Passenger + Apache + Ruby on Rails is not very good.
In particular there is a difference in performance.

Therefore, I constructed CentOS + Unicron + Ruby on Rails + Nginx.
Incidentally, the performance was not much difference : )

Work are the following.





# confirmation of spec
$ cat /proc/version
$ cat /proc/cpuinfo
$ df -h

# confirmation of process
$ ps aux

# confirmation of iptables
$ sudo iptables --list


# confirmation of user
$ cat /etc/passwd
$ id

# confirmation of package
$ yum list installed

# getenforce

# update of package
$ sudo yum update
[y]

# update of system
$ sudo yum upgrade
[y]

# Installation of Nginx, MySql
$ sudo yum install -y nginx mysql mysql-server mysql-devel

# registered in the startup (run level35)
$ sudo chkconfig --level 35 nginx on
$ sudo chkconfig --level 35 mysqld on

# confirmation of the startup
$ chkconfig --list

# reboot and confirmation of the startup
$ sudo reboot
$ ps aux | grep nginx
$ ps aux | grep mysqld

# confirmation of ruby version
$ ruby -v

# confirmation of version
$ gem -v

# Installation of bundle
$ gem install

# Installation of rails
$ gem install rails

[Error]
Running 'patch' for libxml2 2.9.2... ERROR, review '/home/ec2-user/.gem/ruby/2.0/gems/nokogiri-1.6.6.2/ext/nokogiri/tmp/x86_64-redhat-linux-gnu/ports/libxml2/2.9.2/patch.log' to see what happened.
.
.
.
Gem files will remain installed in /home/ec2-user/.gem/ruby/2.0/gems/nokogiri-1.6.6.2 for inspection.
Results logged to /home/ec2-user/.gem/ruby/2.0/gems/nokogiri-1.6.6.2/ext/nokogiri/gem_make.out

# When you install the nokogiri, an error has occurred in libxml2.
# Therefore, install them
sudo yum install -y gcc gcc-c++ ruby-devel libxml2 libxml2-devel libxslt libxslt-devel

# confirmation of version
xml2-config --version

# confirmation of version
xslt-config --version

# Installing nokogiri using the system libraries
gem install nokogiri -- --use-system-libraries

# Installation of rails
$ gem install rails

# confirmation
$ rails -v

# make work directory and move to the directory
$ sudo mkdir /var/ror/; cd /var/ror

# create app using the rails
$ rails new sample --database=mysql

[Error]
/usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:126:in `require': cannot load such file -- io/console (LoadError)
from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:126:in `require'
from /home/ec2-user/.gem/ruby/2.0/gems/thor-0.19.1/lib/thor/shell/basic.rb:2:in `'
from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:73:in `require'
from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:73:in `require'
.
.
.
# maybe io/console error?
# Installation of io/console
$ gem install io-console

# re: create app using the rails
$ rails new test --database=mysql

[Error]
create
/usr/share/ruby/2.0/fileutils.rb:245:in `mkdir': Permission denied - /var/ror/test (Errno::EACCES)
.
.
.

# Permission error
# chenge permission
$ sudo chown -R ec2-user:ec2-user /var/ror/

# re: re: create app using the rails
$ rails new test --database=mysql
$ cd /var/ror/test/

# Initial setting
$ vim /var/ror/test/Gemfile

[Gemfile]

        source 'https://rubygems.org'

        # Error avoidance
        ENV['NOKOGIRI_USE_SYSTEM_LIBRARIES'] = 'YES'
        gem 'io-console'
        # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
        gem 'rails', '4.2.0'
        # Use mysql as the database for Active Record
        gem 'mysql2'
        # Use SCSS for stylesheets
        gem 'sass-rails', '~> 5.0'
        # Use Uglifier as compressor for JavaScript assets
        gem 'uglifier', '>= 1.3.0'
        # Use CoffeeScript for .coffee assets and views
        gem 'coffee-rails', '~> 4.1.0'
        # See https://github.com/sstephenson/execjs#readme for more supported runtimes
        gem 'therubyracer', platforms: :ruby

        # Use jquery as the JavaScript library
        gem 'jquery-rails'
        # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
        gem 'turbolinks'
        # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
        gem 'jbuilder', '~> 2.0'
        # bundle exec rake doc:rails generates the API under doc/api.
        gem 'sdoc', '~> 0.4.0', group: :doc

        # Use ActiveModel has_secure_password
        gem 'bcrypt', '~> 3.1.7'

        # Use Unicorn as the app server
        gem 'unicorn'

        # Use Capistrano for deployment
        gem 'capistrano-rails', group: :development

        group :development, :test do
          # Call 'byebug' anywhere in the code to stop execution and get a debugger console
          gem 'byebug'

          # Access an IRB console on exception pages or by using <%= console %> in views
          gem 'web-console', '~> 2.0'

          # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
          gem 'spring'
        end

# Setting of Unicron
$ vi /var/ror/test/config/unicorn.rb

[unicorn.rb]

        @app_path = '/var/ror/test'
        
        worker_processes 2
        working_directory "#{@app_path}/"
        
        # This loads the application in the master process before forking
        # worker processes
        # Read more about it here:
        # http://unicorn.bogomips.org/Unicorn/Configurator.html
        preload_app true
        
        timeout 30
        
        # This is where we specify the socket.
        # We will point the upstream Nginx module to this socket later on
        listen "/tmp/unicorn.sock", :backlog => 64
        pid "/tmp/unicorn.pid"
        
        # Set the path of the log files inside the log folder of the testapp
        stderr_path "#{@app_path}/log/unicorn.stderr.log"
        stdout_path "#{@app_path}/log/unicorn.stdout.log"
        
        before_fork do |server, worker|
        defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
        
        old_pid = "#{server.config[:pid]}.oldbin"
        if old_pid != server.pid
        begin
          sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
          Process.kill(sig, File.read(old_pid).to_i)
        rescue Errno::ENOENT, Errno::ESRCH
        end
        end
        
        sleep 1
        end
        
        after_fork do |server, worker|
        defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
        end

# Setting of Nginx
$ sudo vi /etc/nginx/conf.d/myapp.conf

[myapp.conf]

        upstream unicorn_server {
            server unix:/tmp/unicorn.sock
            fail_timeout=0;
        }
        
        server {
            listen 80;
            client_max_body_size 4G;
            server_name _;
        
            keepalive_timeout 5;
        
            root /var/www/ror/test/public;
        
            location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
        
                # If you don't find the filename in the static files
                # Then request it from the unicorn server
                if (!-f $request_filename) {
                    proxy_pass http://unicorn_server;
                    break;
                }
            }
        
            error_page 500 502 503 504 /500.html;
            location = /500.html {
                root /var/www/ror/test/public;
            }
        }

# Autostart setting of Unicron
$ vi /etc/init.d/unicorn_rails

[unicorn_rails]

        #!/bin/sh
        ### BEGIN INIT INFO
        # Provides:          unicorn
        # chkconfig:         3456 99 35
        # Short-Description: starts the unicorn web server
        # Description:       starts unicorn
        ### END INIT INFO


        NAME="Unicorn"
        ENV=development

        ROOT_DIR="/var/ror/test"

        PID="${ROOT_DIR}/tmp/pids/unicorn.pid"
        CONF="${ROOT_DIR}/config/unicorn.rb"
        OPTIONS=""
        CMD="bundle exec unicorn_rails -c ${CONF} -E ${ENV} -D ${OPTIONS}"


        PATH="/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin"

        start()
        {
          if [ -e $PID ]; then
            echo "$NAME already started"
            exit 1
          fi
          echo "start $NAME"
          cd $ROOT_DIR
          gem install bundler
          $CMD
        }

        stop()
        {
          if [ ! -e $PID ]; then
            echo "$NAME not started"
            exit 1
          fi
          echo "stop $NAME"
          kill -QUIT `cat ${PID}`
          rm -f $PID
        }

        force_stop()
        {
          if [ ! -e $PID ]; then
            echo "$NAME not started"
            exit 1
          fi
          echo "stop $NAME"
          kill -INT `cat ${PID}`
          rm -f $PID
        }

        reload()
        {
          if [ ! -e $PID ]; then
            echo "$NAME not started"
            start
            exit 0
          fi
          echo "reload $NAME"
          kill -HUP `cat ${PID}`
        }

        restart()
        {
            stop
            start
        }

        case "$1" in
          start)
            start
            ;;
          stop)
            stop
            ;;
          force-stop)
            force_stop
            ;;
          reload)
            reload
            ;;
          restart)
            restart
            ;;
          *)
            echo "Syntax Error: release [start|stop|force-stop|reload|restart]"
            ;;
        esac

# Adding Unicron to the startup
$ sudo chkconfig --add unicorn_rails

# create page
$ rails g controller Roots index

# Adding routes
$ vi /var/ror/test/config/routes.rb

[routes.rb]

        root :to => 'roots#index'

# run unicorn
$ bundle exec unicorn_rails -c config/unicorn.rb -E development -D

[Error]
/var/ror/test/vendor/bundle/ruby/2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/mysql2_adapter.rb:23:in `rescue in mysql2_connection'
.
.
.

# SQL error...
# Setting of SQL
$ mysql_secure_installation

$ mysql -u root -p

$ vi /var/ror/test/conifg/database.yml

[database.yml]

             # MySQL.  Versions 5.0+ are recommended.
             #
             # Install the MYSQL driver
             #   gem install mysql2
             #
             # Ensure the MySQL gem is defined in your Gemfile
             #   gem 'mysql2'
             #
             # And be sure to use new-style password hashing:
             #   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
             #
             default: &default
               adapter: mysql2
               encoding: utf8
               pool: 5
               username: root
               password: amazon
               socket: /var/lib/mysql/mysql.sock

$ rake db:create

# re: run unicorn
$ bundle exec unicorn_rails -c config/unicorn.rb -E development -D

# create "Hello world"
$ rm -rf /var/ror/test/app/views/roots/index.html.erb
$ echo "HELLO WORLD" > /var/ror/test/app/views/roots/index.html.erb
$ rm -rf /var/ror/test/app/views/layouts/application.html.erb
$ echo "<%= yield -%>" > /var/ror/test/app/views/layouts/application.html.erb


$ curl YOUR IP
Return >> HELLO WORLD


Enjoy!

Data structure alignment

I was suffering with data structure alignment.
But,
I have found

"#pragma pack(1)".

argument: 1~16.
It can be packed a element of memory.

ex:

#ifndef __ETH_H__
#define __ETH_H__

#include <iostream>

class foo{
public:
	#pragma pack(1)
	struct sc{
		ushort a;
		short b;
		char c;
	};
	#pragma pack()

};

#endif


GOD