Skip to content

Instantly share code, notes, and snippets.

@alexlee-gk
alexlee-gk / visualization.py
Created Oct 6, 2018
Convert a feature map into an image.
View visualization.py
import matplotlib.pyplot as plt
import numpy as np
def vis_square(data, grid_shape=None, padsize=1, padval=0, cmap=None, data_min=None, data_max=None):
data_min = data_min if data_min is not None else data.min()
data_max = data_max if data_max is not None else data.max()
data = (data - data_min) / (data_max - data_min)
lead_shape = data.shape[:-3]
@alexlee-gk
alexlee-gk / test_conv_upsample_pool_ops.py
Created Apr 19, 2018
Equivalence and timing tests for upsample_conv2d and conv_pool2d.
View test_conv_upsample_pool_ops.py
import numpy as np
import tensorflow as tf
from video_prediction.ops import pad2d_paddings, conv2d, deconv2d, upsample2d, upsample_conv2d, pool2d, conv_pool2d
def test_upsample_conv2d():
sess = tf.Session()
batch = 16
for strides in ([2, 2], [3, 4]):
@alexlee-gk
alexlee-gk / ssim.py
Last active Dec 7, 2018
SSIM TensorFlow implementation that matches scikit-image's compare_ssim
View ssim.py
import tensorflow as tf
from tensorflow.python.util import nest
def _with_flat_batch(flat_batch_fn):
def fn(x, *args, **kwargs):
shape = tf.shape(x)
flat_batch_x = tf.reshape(x, tf.concat([[-1], shape[-3:]], axis=0))
flat_batch_r = flat_batch_fn(flat_batch_x, *args, **kwargs)
r = nest.map_structure(lambda x: tf.reshape(x, tf.concat([shape[:-3], x.shape[1:]], axis=0)),
@alexlee-gk
alexlee-gk / ffmpeg_gif.py
Last active Jun 25, 2022
ffmpeg save or encode GIFs (with palette generation) from numpy images
View ffmpeg_gif.py
import os
import numpy as np
def save_gif(gif_fname, images, fps=4):
"""
To generate a gif from image files, first generate palette from images
and then generate the gif from the images and the palette.
ffmpeg -i input_%02d.jpg -vf palettegen -y palette.png
@alexlee-gk
alexlee-gk / configure_cuda_p70.md
Last active Jun 9, 2022
Use integrated graphics for display and NVIDIA GPU for CUDA on Ubuntu 14.04
View configure_cuda_p70.md

This was tested on a ThinkPad P70 laptop with an Intel integrated graphics and an NVIDIA GPU:

lspci | egrep 'VGA|3D'
00:02.0 VGA compatible controller: Intel Corporation Device 191b (rev 06)
01:00.0 VGA compatible controller: NVIDIA Corporation GM204GLM [Quadro M3000M] (rev a1)

A reason to use the integrated graphics for display is if installing the NVIDIA drivers causes the display to stop working properly. In my case, Ubuntu would get stuck in a login loop after installing the NVIDIA drivers. This happened regardless if I installed the drivers from the "Additional Drivers" tab in "System Settings" or the ppa:graphics-drivers/ppa in the command-line.

@alexlee-gk
alexlee-gk / locally_connected2d.py
Last active Oct 17, 2016
Locally connected 2D layer using Lasagne and Theano
View locally_connected2d.py
import numpy as np
import theano
import theano.tensor as T
import lasagne.layers as L
class LocallyConnected2DLayer(L.Conv2DLayer):
"""Similar to Conv2DLayer except that the filter weights are unshared
This implementation computes the output tensor by iterating over the filter
@alexlee-gk
alexlee-gk / ram_image_example.py
Last active Feb 22, 2022
Get color and depth image as numpy array using Panda3d 1.10.0 and python 3.5
View ram_image_example.py
import numpy as np
import time
import cv2
from direct.showbase.ShowBase import ShowBase
from panda3d.core import FrameBufferProperties, WindowProperties
from panda3d.core import GraphicsPipe, GraphicsOutput
from panda3d.core import Texture
from panda3d.core import loadPrcFileData
loadPrcFileData('', 'show-frame-rate-meter true')
@alexlee-gk
alexlee-gk / install_tensorflow_instructional_machine.md
Last active Apr 3, 2016
Installing TensorFlow on Instructional Machines with GPU support
View install_tensorflow_instructional_machine.md

Installing TensorFlow on Instructional Machines with GPU support

These instructions were adapted from here.

The following was tested on the hive machines (e.g. hive10.cs.berkeley.edu).

Install cuDNN and setup CUDA environment variables

Download and install cuDNN.

@alexlee-gk
alexlee-gk / install_blender_pyenv.md
Last active Jan 12, 2022
Install blender as a module with python 3.5 and pyenv
View install_blender_pyenv.md

#Install blender as a module with python 3.5 and pyenv

Tested on Ubuntu 14.04.

Setting up a new python environment using pyenv

Follow instructions from here.

Installing boost

Follow instructions from here.

@alexlee-gk
alexlee-gk / install_caffe_pyenv.md
Created Feb 9, 2016
Install caffe with python 3.5 and pyenv
View install_caffe_pyenv.md

Install caffe with python 3.5 and pyenv

Tested on Ubuntu 14.04.

Setting up a new python environment using pyenv

Install desired version of python 3 (e.g. 3.5.1). Make sure to use the --enable-shared flag to generate python shared libraries, which will later be linked to.

env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.5.1