Skip to content

Instantly share code, notes, and snippets.

@alexlee-gk
alexlee-gk / visualization.py
Created October 6, 2018 20:59
Convert a feature map into an image.
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 April 19, 2018 23:44
Equivalence and timing tests for upsample_conv2d and conv_pool2d.
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 December 7, 2018 21:25
SSIM TensorFlow implementation that matches scikit-image's compare_ssim
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 June 25, 2022 21:46
ffmpeg save or encode GIFs (with palette generation) from numpy images
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 November 1, 2024 06:09
Use integrated graphics for display and NVIDIA GPU for CUDA on Ubuntu 14.04

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 October 17, 2016 11:25
Locally connected 2D layer using Lasagne and Theano
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 February 22, 2022 14:49
Get color and depth image as numpy array using Panda3d 1.10.0 and python 3.5
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 April 3, 2016 00:55
Installing TensorFlow on Instructional Machines with GPU support

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 January 12, 2022 15:34
Install blender as a module with python 3.5 and pyenv

#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 February 9, 2016 02:15
Install caffe with python 3.5 and pyenv

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