Tuesday, February 28, 2017

raspberry pi camera

python3
sudo apt-get install python3-picamera
https://www.raspberrypi.org/documentation/usage/camera/python/README.md


allow cv2.VideoCapture(0)
sudo modprobe bcm2835-v4l2

allow user access camera
sudo usermod XXXX -a -G video

single image display
import cv2
im = cv2.imread('test.jpg')
cv2.imshow('image', im)
cv2.waitKey()

video display
import cv2
ca = cv2.VideoCapture(0)
cv2.startWindowThread()
cv2.namedWindow('preview')
while True:
  _,  im = ca.read()
  cv2.imshow('preview', im)
  key = cv2.waitKey(1) & 0xFF
  if key == ord('q'):
    break

Monday, February 27, 2017

open cv 3 on ubuntu


sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get update
cd opencv-3.2.0/
mkdir build
cd build/
sudo apt-get install python3-dev
sudo apt-get install java-7-openjdk
sudo pip3 install --upgrade pip
sudo pip3 install numpy
export PYTHON3_EXECUTABLE=/usr/bin/python3
export PYTHON_INCLUDE_DIR=/usr/include/python3
export PYTHON_INCLUDE_DIR=/usr/include/python3.4
export PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.4m/
export PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.4m.so
export PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.4/dist-packages/numpy/core/include/
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make -j4
sudo make install

For raspberry pi 3:
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev


export PYTHON3_EXECUTABLE=/usr/bin/python3
export PYTHON_INCLUDE_DIR=/usr/include/python3.4

export PYTHON_INCLUDE_DIR2=/usr/include/arm-linux-gnueabihf/python3.4m/
export PYTHON_LIBRARY=/usr/lib/arm-linux-gnueabihf/libpython3.4m.so
export PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.4/dist-packages/numpy/core/include/
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_QT=OFF -D WITH_TBB=OFF ..
http://stackoverflow.com/questions/26371187/how-can-i-disable-opengl-support-opencv

Sunday, February 26, 2017

tensorflow cifar10

https://www.tensorflow.org/tutorials/deep_cnn

First time the training is very slow.
On GTX 970,  ~33 hours.
On GTX 860m, ~40 hours.
On Raspberry pi 3, ~200 hours.

I decided to use GTX 860m to train and will update back after 40 hours. (20170226, 9AM)

Or I am thinking of using multiple GTX860m on multiple PCs.  I have 3 of them.
https://www.tensorflow.org/deploy/distributed

cifar10_image.py: This is a short script displaying 32x32 images from cifar10 bin file
import numpy as np
import PIL.Image

#display larger images
def DisplayPicture(byte, fmt='jpeg'):
print(byte[0])
a = np.fromstring(byte[1:3073], dtype=np.uint8)
b = np.reshape(a, (3, 1024))
c = b.T
d = np.concatenate([c, c, c, c, c, c, c, c], axis=1)
e = np.reshape(d, [32, 32*3*8])
f = np.concatenate([e, e, e, e, e, e, e, e], axis=1)
g = np.reshape(f, [1, 3072*8*8])
im = PIL.Image.frombytes('RGB', (32*8,32*8), g)
im.show()

#open file 
#read an image
#record format: 1 byte type, 1024 byte red, 1024 byte green, 1024 byte blue
#32 x 32, row-major
f = open('data_batch_1.bin', 'rb')
byte = f.read(3073)
while byte != '':
DisplayPicture(byte)
byte = f.read(3073)
f.close




Further reading:
https://www.cs.toronto.edu/~kriz/cifar.html
http://groups.csail.mit.edu/vision/TinyImages/

Saturday, February 25, 2017

tensorflow Mandelbrot example

The last two lines of DisplayFractal() will show the picture.   The https://www.tensorflow.org/tutorials/mandelbrot is not correct.

# Import libraries for simulation
import tensorflow as tf
import numpy as np

# Imports for visualization
import PIL.Image
from io import BytesIO
#from IPython.display import Image, display

def DisplayFractal(a, fmt='jpeg'):
  """Display an array of iteration counts as a
     colorful picture of a fractal."""
  a_cyclic = (6.28*a/20.0).reshape(list(a.shape)+[1])
  img = np.concatenate([10+20*np.cos(a_cyclic),
                        30+50*np.sin(a_cyclic),
                        155-80*np.cos(a_cyclic)], 2)
  img[a==a.max()] = 0
  a = img
  a = np.uint8(np.clip(a, 0, 255))
  #f = BytesIO()
  #PIL.Image.fromarray(a).save(f, fmt)
  #display(Image(data=f.getvalue()))
  im = PIL.Image.fromarray(a)
  im.show()

sess = tf.InteractiveSession()
# Use NumPy to create a 2D array of complex numbers

Y, X = np.mgrid[-1.3:1.3:0.005, -2:1:0.005]
Z = X+1j*Y

xs = tf.constant(Z.astype(np.complex64))
zs = tf.Variable(xs)
ns = tf.Variable(tf.zeros_like(xs, tf.float32))

tf.global_variables_initializer().run()

# Compute the new values of z: z^2 + x
zs_ = zs*zs + xs

# Have we diverged with this new value?
not_diverged = tf.abs(zs_) < 4

# Operation to update the zs and the iteration count.
#
# Note: We keep computing zs after they diverge! This
#       is very wasteful! There are better, if a little
#       less simple, ways to do this.
#
step = tf.group(
  zs.assign(zs_),
  ns.assign_add(tf.cast(not_diverged, tf.float32))
  )

for i in range(200): step.run()
#DisplayFractal(ns.eval())


media download

http://www.stormcolin.com/cmedia/

Friday, February 24, 2017

expand ubuntu partition on vmwave esxi 5

Original

admin@droneteambasevm:~$ df -k                                                        
Filesystem                             1K-blocks    Used Available Use% Mounted on    
/dev/mapper/UbntSR--vCAC--Tmp--vg-root  14048320 3345632   9966024  26% /        
none                                           4       0         4   0% /sys/fs/cgroup
udev                                     3042888       4   3042884   1% /dev     
tmpfs                                     610808    3036    607772   1% /run     
none                                        5120       0      5120   0% /run/lock 
none                                     3054024       0   3054024   0% /run/shm 
none                                      102400       0    102400   0% /run/user 
/dev/sda1                                 240972   38813    189718  17% /boot

admin@droneteambasevm:~$ sudo fdisk -l                               
[sudo] password for admin:                                           
                                                                     
Disk /dev/sda: 107.4 GB, 107374182400 bytes                          
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes                               
Sector size (logical/physical): 512 bytes / 512 bytes                
I/O size (minimum/optimal): 512 bytes / 512 bytes                    
Disk identifier: 0x000a8393                                          
                                                                     
   Device Boot      Start         End      Blocks   Id  System       
/dev/sda1   *        2048      499711      248832   83  Linux        
/dev/sda2          501758    33552383    xxxxxxxx    5  Extended     
/dev/sda5          501760    33552383    xxxxxxxx   83  Linux        

Step 1:
sudo fdisk /dev/sda
d 5
d 2
n 2 (extended)
n 5 (logical 501760 to 33552383)
n 6 (logical reset)
t 5 8e
t 6 8e
p

This is results.
Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a8393

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          499712   209715199   104607744    5  Extended
/dev/sda5          501760    33552383    16525312   8e  Linux LVM
/dev/sda6        33554432   209715199    88080384   83  Linux LVM

sudo reboot

Step 2:
admin@droneteambasevm:~$ sudo vgextend UbntSR-vCAC-Tmp-vg /dev/sda6
[sudo] password for admin:
  No physical volume label read from /dev/sda6
  Physical volume "/dev/sda6" successfully created
  Volume group "UbntSR-vCAC-Tmp-vg" successfully extended
admin@droneteambasevm:~$ sudo lvextend -L +84G /dev/UbntSR-vCAC-Tmp-vg/root
  Extending logical volume root to 97.74 GiB
  Logical volume root successfully resized
admin@droneteambasevm:~$ df -k
Filesystem                             1K-blocks    Used Available Use% Mounted on
/dev/mapper/UbntSR--vCAC--Tmp--vg-root  14048320 3347364   9964292  26% /
none                                           4       0         4   0% /sys/fs/cgroup
udev                                     3042888       4   3042884   1% /dev
tmpfs                                     610808    3040    607768   1% /run
none                                        5120       0      5120   0% /run/lock
none                                     3054024       0   3054024   0% /run/shm
none                                      102400       0    102400   0% /run/user
/dev/sda1                                 240972   38813    189718  17% /boot
admin@droneteambasevm:~$ sudo resize2fs /dev/UbntSR-vCAC-Tmp-vg/root
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/UbntSR-vCAC-Tmp-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 7
The filesystem on /dev/UbntSR-vCAC-Tmp-vg/root is now 25621504 blocks long.

admin@droneteambasevm:~$ df -k
Filesystem                             1K-blocks    Used Available Use% Mounted on
/dev/mapper/UbntSR--vCAC--Tmp--vg-root 100746672 3364584  93122788   4% /
none                                           4       0         4   0% /sys/fs/cgroup
udev                                     3042888       4   3042884   1% /dev
tmpfs                                     610808    3040    607768   1% /run
none                                        5120       0      5120   0% /run/lock
none                                     3054024       0   3054024   0% /run/shm
none                                      102400       0    102400   0% /run/user
/dev/sda1                                 240972   38813    189718  17% /boot


Tuesday, February 21, 2017

tensorflow 1.0 on raspberry pi 3

Still ongoing.

Reference:
https://github.com/samjabrahams/tensorflow-on-raspberry-pi/blob/master/GUIDE.md


bazel:
/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java
Add @ 600th line
    } catch (InterruptedException e) {
      throw new RepositoryFunctionException(
          new IOException("thread interrupted"), Transience.TRANSIENT);

tensorflow:
git clone --recurse-submodules https://github.com/tensorflow/tensorflow
git checkout r1.0
cd tensorflow
...

/WORKSPACE
manually change the url line for numericjs_numeric_min_js to the following
url = "http://numericjs.com/numeric/lib/numeric-1.2.6.min.js",

Then, ./configure