My 404s Don't code and drive

Concatenating Numpy Vectors Stored In Files

Sometimes you have vectors or matrices stored in files, and you need to read them and concatenate them. One way of doing this is reading them into a list and then using Numpy’s hstack or vstack to create the vector or matrix. Like this for concatenating rows

result = np.hstack([np.genfromtxt(fName,delimiter=";") for fName in fList])

where the read matrix or vector all are of size MxNf, that is the rows, M, are the same across all read matrices while Nf can vary. The same goes for vertical stacking but then the sizes are reversed, that is, NfxM, and we have,

result = np.vstack([np.genfromtxt(fName,delimiter=";") for fName in fList])

Batch Resize Images With Padding

ImageMagick comes with a tool to perform batch image transforms, it is called mogrify. To perform a resize of a set of images keeping the aspect ratio and padding with a white background we can use the following command,

mogrify -resize 100x100 -background white -gravity center -extent 100x100 -path ./resize_directory *.png

This command resizes all png images in the directory to a 100x100 and pads with white to keep the aspect ratio.

Caffe import - TypeError got an unexpected keyword argument 'syntax' error

Got the following error when importing the caffe package for python on Mac and OSX 10.12.

from caffe.proto import caffe_pb2
File "caffe/proto/caffe_pb2.py", line 23, in <module>
x01(\x0b\x32\x1a.caffe.HDF5OutputParameter\".\n\nPoolMethod\x12\x07\n\x03MAX\x10\x00\x12\x07\n\x03\x41VE\x10\x01\x12\x0e\n\nSTOCHASTIC\x10\x02\"W\n\x0ePReLUParameter\x12&\n\x06\x66iller\x18\x01 \x01(\x0b\x32\x16.caffe.FillerParameter\x12\x1d\n\x0e\x63hannel_shared\x18\x02 \x01(\x08:\x05\x66\x61lse*\x1c\n\x05Phase\x12\t\n\x05TRAIN\x10\x00\x12\x08\n\x04TEST\x10\x01')
TypeError: __init__() got an unexpected keyword argument 'syntax'

This was resolved by editing the caffe_pb2.py file and commenting out all lines which said syntax='proto2'

Python Pip Install Under Different Python Versions

Due to python 2 and 3 it usual to have different version of installs working. A smooth method for installing packages using pip for different versions is to call pip via the python version. Like this (for Ubuntu python version 3.5),

sudo python3.5 -m pip install ipython numpy

Setting Install Directory When Using CMake

Sometimes we want to install a build to a different directory. In CMake this can be done by setting the CMAKE_INSTALL_PREFIX variable. When we are building OpenCV3 for examle we would do,

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ../opencv-3