Saturday, February 24, 2018

Scientific Python Environment on Windows with Tensorflow

Google has made Tensorflow quite easy to install with GPU support, but getting it working along side SciPy and Numpy in a single Windows environment can be tricky. This is usually because Scipy looks for a specific build of Numpy that uses Intel MKL. This guide will allow you to set it up easily.

Firstly, I setup WinPython on my machine. You get Scikit packages, Jupyter, Scipy, Numpy, Spyder etc. in simple easy distribution that doesn't have to be installed but kept side-by-side with multiple setups. Avoid the "Zero" builds that have minimal packages. I used Python 3.5 Qt 5 installer.

Second, go to the Tensorflow website and determine the CUDA version you need and then install CUDA of that version. If you install the incorrect version, you'll keep getting the error:
ImportError: Could not find 'cudart64_90.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. Download and install CUDA 9.0 from this URL: https://developer.nvidia.com/cuda-toolkit
Here, I installed CUDA 9.1 when instead the Python Package Index version of Tensorflow (v1.5 at the time of writing) required 9.0. Afterwards, install the cuDNN that matches the CUDA you have installed. This requires a sign up before login at the time of writing. Older versions of Tensorflow did not require cuDNN installed, but this is now compulsory.

Third, install tensorflow-gpu via the WinPython Command Prompt by issuing the usual command that Google recommends:
pip install --upgrade --ignore-installed tensorflow-gpu
This will give you a working Tensorflow, Numpy environment, but Scipy will be broken giving the error:
    from scipy.linalg import _fblas

ImportError: DLL load failed: The specified module could not be found.
This is where you will need to install the Numpy MKL version, which you can find here. Then use the WinPython Control Panel to upgrade the Numpy package. Make sure to download the Numpy MKL wheel to match your Python distribution.

That's it!

Cheers Shakes - L3mming

Friday, July 28, 2017

Setting up a Mac OSX Development Environment for VTK, ITK, Qt 4 and SMILI

I recently got access to a Mac and decided to improve support for SMILI on Mac OSX. This required setting up the Mac OSX development environment and so I log my experiences/steps in this post.

Firstly some notes. This is to setup VTK 5 and/or 6, ITK 4.10+ and Qt 4 with the current version of Mac OSX at the time - Sierra (10.12).

The first steps involved setting up Homebrew. This allows one to install Git and CMake for the command-line.
brew install make

Next we installed Xcode from the AppStore. You should not run CMake on libraries until Xcode has been installed.

Then (or during the XCode install) we download the required libraries for your environment:

  • Insight Toolkit (ITK) - I used version 4.10.1, but newer versions of ITK 4 should be OK.
  • Visualisation Toolkit (VTK) - I used version 6.3.0, but also a modified version of 5.8.0 (for MSVC 2013) that I made available here. Versions newer than 6.3.0 had visualisation bugs introduced into SMILI.
  • Qt 4.8.x - Just use the home-brew version described here. I tried using version 4.8.6 that you can find here. Download the .tar.gz version for Mac OSX. But it had issues, see below. So the HomeBrew version as described here.

Qt 4

I Failed: After Xcode was installed, I began compiling Qt. Turns out that using the zip version messes up the line endings and breaks the configure script. So make sure to download the TAR version. When compiling, There was a compile error for the 'qpaintengine_mac.cpp' file. The fix for Qt 4.8.6 is here. There are issues with 10.9 and libstd++ which I haven't figured out yet. I will update this side of the post once I have. I will leave the configure command I tried using here for future reference:
./configure -fast -release -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -DMACOSX_DEPLOYMENT_TARGET=10.9 -arch x86_64
You could also try 4.8.7 from here. It required the same fix as 4.8.6 from here.

I Succeeded: by using the HomeBrew Qt4 version as described here. I just did step 2 as I had a fresh install. No compilation, just downloaded and installed. This will become outdated at some point unless maintained, so you may need to search for other recipes. This link might help in that case.


ITK

No issues. Just build fine from source. I usually enable review to get access to less known ITK classes. I used the following CMake command to generate the project for Sublime Text:
ccmake -G "Sublime Text 2 - Unix Makefiles" -DCMAKE_OSX_SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/ -DCMAKE_OSX_DEPLOYMENT_TARGET=10.6 ..

VTK

I Failed: Once Qt 4 is built and installed, I enabled VTK_USE_QT to ON. I had two major issues. The first issue I had was with the duplicate symbols error with TIFF. Installing lib tiff with HomeBrew solved the problem, but could potentially caused crashes in certain versions of Mac OSX, so will post a better solution later. The second was a garbage collection compilation error that I have yet to find solution for.

I succeeded: Compiling VTK 6.3 had no issues at all. I used the same CMake command as for ITK. I used deployment target of 10.6 to ensure support for Mac OSX 10.6.

EDIT: I succeeded: Compiling VTK 5.8 by ensuring that the VTK_REQUIRED_OBJCXX_FLAGS was empty. This was because of a 'garbage collection not found' error. This is because GC memory management model has been deprecated and removed. See the COCOA VTK page for more details.

SMILI

Once the above dependencies are built, SMILI v1.01 had no issues. I reused the CMake command for ITK above. I built both 10.6 and 10.9 deployment targets for ITK, VTK and SMILI for testing.

More will be added as I encounter/fix issues.

Cheers Shakes - L3mming

Friday, January 22, 2016

Paperwhite eBook Reader Experiences for Research Papers and Maths

I have been wanting an easier way to read journal papers for a while and always thought about getting an eBook reader, since e-ink should be suitable right?

Well, reading a number of reviews and guides, it seems previous e-book readers didn't seem to handle PDFs well and most times didn't handle maths well either. That's why most guides etc. recommend tools like K2pdfopt to convert PDFs. But this year I though I'd give it a shot and test to see if the myths were true and I'm happy to report that it is not true for the new models, PDFs work great and maths are flawless.... at least for the Amazon Paperwhite 2015 model.

Here's excerpts from my thesis (available open access here) displayed on the device. ;)

Portrait Mode


You can zoom like on any other tablet, with much slower update rates, but not a major drama since once you get the view you want, you can read it like paper.

Portrait Mode Zoomed

I usually use the landscape mode, where each page is shown in three sections.

Landscape mode

Surprisingly, the default PDF behavior was better than the converted PDF files were. Battery life is about a month and the reader also has back illumination for night reading, though would preferred a night mode (white text and black background) like some apps on Android tablets, but no biggy. I did find using the free Calibre to rename and reorder PDFs a must as the reader doesn't read the PDF meta data, so the PDFs are shown with filenames of the PDFs on the home screen. Adding sub-directories would be nice too on the home screen.

All in all, I would highly recommend the reader to scientists for reading scholarly articles and properly displaying mathematics.

Cheers Shakes - L3mming

Friday, June 19, 2015

VTK 5 and FFMPEG shenanigans on Windows

Over the years, trying to get VTK 5 to work with FFMPEG has always been tricky. Thankfully, VTK 6 has no such problems, but for those that have legacy code that use VTK 5, this is still a problem.

In this post I will cover some necessary tweaks, steps and links to getting FFMPEG IO working with your project that uses VTK 5, such as the 'Animate' plugin for sMILX and SMILI.

Firstly, I have found VTK 5.8 to be far more stable and workable than 5.10.1. In my scientific visualisation software SMILI, certain features don't work at all with the same code that runs fine on VTK 6 and 5.8.

Secondly, it seems VTK 5 has issues with Visual Studio 2013 (required to support Windows 8), but a patched version of VTK 5.10.1 available on GitHub works OK, though I had to fix a few round brackets here and here.

For version 1.2 of FFMPEG, this is what I recently did to get VTK 5.8 working with FFMPEG. The best link for FFMPEG deprecation changes are found here. Summarised, I had to fix vtkFFMPEGWriter.cxx as the following:
  1. av_set_parameters  - comment out
  2. URL_WRONLY to AVIO_FLAG_WRITE
  3. url_fopen to avio_open and url_fclose to avio_close
  4. av_write_header to avformat_write_header, last arg to NULL
EDIT: You may also need AVCodecID, PKT_FLAG_KEY is replaced by AV_PKT_FLAG_KEY and replacing CODEC_TYPE_VIDEO with AVMEDIA_TYPE_VIDEO.
EDIT 2: I have created a GitHub VTK 5.8.0 fork with these changes applied.

No changes are required for VTK 6.1.0 at the time of writing.

Hope that helps
Cheers Shakes - L3mming

Friday, June 5, 2015

Mesh/Surface Processing with Command-line SMILI tools

I recently had to do some processing for a prostate segmentation scheme I've been working on and thought some uses of SMILI tools would be helpful to others. Majority of surface processing tasks can be achieved with the milxModelApp in SMILI, a easy to use open source medical imaging library and viewer.

My tasks involved involved:
  1. copying scalars from an atlas mesh to lots of other meshes
  2. using scalars to clip the meshes
  3. computing the surface distance of the meshes to their corresponding labelled images that were created by clinical experts
  4. computing the scalar statistics across the population of meshes (my meshes already have point to point correspondence) 
Luckily, the milxModelApp application provides most of these without needing to script for multiple surfaces. Furthermore, the Qt framework is not required for these applications as they are VTK/ITK only applications (i.e. built on milxSMILI, the GUI-independent sub-library of SMILI).The following examples are for Version 0.998 (SMILI Release Beta 1).

Each one of the above tasks can be achieved as:
  1. The --scalarcopy  option in milxModelApp provides a way to do this. I did:
    milxModelApp --scalarcopy focus_atlases/focus_bladder_atlas.vtk results/bladder/asm_bladder_*.vtk -p results_scalars/bladder/
    Copy scalars from atlas mesh (focus_bladder_atlas.vtk) to other meshes in directory (results/bladder/asm_bladder_*.vtk) and output to another directory with same names.
  2. The --clip  option in milxModelApp provides a way to do this. I did:
    milxModelApp --clip 1 results_scalars/bladder/asm_bladder_*.vtk -p results_clipped/bladder/
    Clip meshes in directory keeping only parts with value of 1 and output to another directory with same names.
  3. We need to write a script for the milxHausdorffDistance application that provides this using distance maps in a straight forward way. This script is now in the repository as scripts/ batch_hausdorff.py. An example usage of the application:
    milxHausdorffDistance results_clipped/bone/asm_bone_4.vtk -p Hausdorff/bone/bone__ -o Hausdorff/bone/bone__004.vtk --label manuals_renamed/bone/manual_case_bone_004.nii.gz --labelvalue 1 -c 004
    The distance map of the manual is created and the distances stored on the mesh thus giving the surface distances to the manual.
  4. The --scalarstats option in milxModelApp provides this. I did:
    milxModelApp --scalarstats Hausdorff/bladder/bladder__*.vtk -o bladder_stats.vtk
    Compute scalars stats (mean, variance etc. per point) of meshes in directory and output single mesh (of first mesh) with stats as arrays in mesh. You can then use sMILX viewer to view the meshes and change the loaded arrays via the Right Click->Show->Load Array menu.
Note that the milxDeformableModel app is an application derived from milxModelApp and additionally provides options for voxelising surfaces and applying image orientation to surfaces.

Hope this helps
Cheers Shakes - L3mming

Saturday, February 28, 2015

Compiling VTK 5.8.0 on Windows 64 with Visual Studio 2013

This is a quick post to cover the steps necessary to compile VTK 5.8.0, the most stable VTK 5 version in my experience, on Windows x64 with VS 2013 (to support Windows 8.1). The information is on the internet but scattered about so I thought I'd collect it just in case its useful for someone.

EDIT: Note that VTK 5.8.0 builds out of the box on Ubuntu 16.04 still, if you ensure that you uncomment the line with the define for GLX_GLXEXT_LEGACY in the Rendering/vtkXOpenGLRenderWindow.cxx file.

There are a few things that are incompatible with the new compiler. Firstly, the vtkOStreamWrapper error. This is covered in great detail here. Basically replace the offending line with:
//VTKOSTREAM_OPERATOR(ostream&);
vtkOStreamWrapper& vtkOStreamWrapper::operator << (ostream& a) {
    this->ostr << (void *)&a;
    return *this;
}
Then there are the ifstream->read() errors. Replace the
if ( this->IFile->read(result, 80) == 0)
with the
if ( this->IFile->read(result, 80).fail())
Lastly, there are the make_pair errors, solved here. You need to replace the
this->Map->insert(vtkstd::make_pair< vtkVariant, vtkVariant >(from, to));
with
this->Map->insert(vtkstd::make_pair(fromto)); // Addendum
in the vtkMapArrayValues.cxx file. You might need to add the include:
#ifdef _WINDOWS
  #include  // Addendum for vs11 to find 'greater'
#endif
to vtkAdjacencyMatrixToEdgeTable.cxx. I also had to add the above include and 
#include 
to vtkAdjacencyMatrixToEdgeTable.cxx, vtkNormalizeMatrixVectors.cxx, vtkPairwiseExtractHistogram2D.cxx, vtkParallelCoordinatesRepresentation.cxx, vtkChartXY.cxx, vtkControlPointsItem.cxx, to get rid of std max and min errors, 

HTHCheers Shakes - L3mming

Thursday, February 12, 2015

HP 4000b Bluetooth Mouse and Ubuntu

This quick post cover howto get the HP 4000b Bluetooth mouse working flawlessly with Ubuntu 14.10 + (and maybe even older versions). These bluetooth mice are useful for ultra-portable devices that have no or few USB ports, like my Dell XPS 13, keeping the port free for other uses.

The HP 4000b mouse is an affordable bluetooth mouse that does work out of the box with Ubuntu but drops out after use for a few minutes or so. This post covers the solution to this problem that worked for me, and this post helpful for TLP users.

In summary, add the following to your rc.local file (the one I used was /etc/rc.local):
# Prevents the Bluetooth USB card from getting reset which disconnects the mouse
BTUSB_DEV="8087:07dc"
BTUSB_BINDING="$(lsusb -d "$BTUSB_DEV" |
    cut -f 1 -d : |
    sed -e 's,Bus ,,' -e 's, Device ,/,' |
    xargs -I {} udevadm info -q path -n /dev/bus/usb/{} |
    xargs basename)"


echo "Disabling autosuspend for Bluetooth USB Soundcard: $BTUSB_BINDING..."
echo -1 > "/sys/bus/usb/devices/$BTUSB_BINDING/power/autosuspend_delay_ms"
And in the TLP config (/etc/default/tlp), add the USB_BLACKLIST="8087:07da". The ID for my device was 8087:07dc as in the rc.local file. These IDs need to match and can be found using lsusb.

HTH
Cheers Shakes - L3mming