Skip to content

Instantly share code, notes, and snippets.

@kgrm
kgrm / deconv_fixed.py
Created April 25, 2017 08:15
fixed theano deconv example
import numpy as np
from keras.models import Sequential
from keras.layers import Deconv2D, Lambda
def cropped_shape(inshape):
samples, ch, w, h = inshape
return (samples, ch, w - 2, h - 2)
mdl = Sequential()
mdl.add(Deconv2D(256, input_shape=(1, 7, 7), padding='valid', kernel_size=4, strides=2, data_format="channels_first"))
from keras.layers import Input, Dense, Lambda
from keras.models import Model
def eucl_dist(inputs):
x, y = inputs
return ((x - y)**2).sum(axis=-1)
x = Input((32,))
y1 = Dense(8)(x)
y2 = Dense(8)(x)
import numpy as np
import sys, os
if len(sys.argv) > 1 and sys.argv[1] == "gpu":
os.environ["THEANO_FLAGS"] = "device=gpu2, "
os.environ["THEANO_FLAGS"] += "dnn.enabled=True, "
os.environ["THEANO_FLAGS"] += "lib.cnmem=0.95"
from keras.layers import Input, Convolution2D, MaxPooling2D, AveragePooling2D
from keras.layers import Flatten, BatchNormalization, Activation
from keras.regularizers import l2
from keras.models import Model
import sys, os, cv2
import caffe
import numpy as np
model_file = "YOURNET.prototxt"
pretrained = "YOURNET.caffemodel"
# Load a pretrained CAFFE neural network and an image,
# use the network to extract features from the image,
# then save the feature vector to a text file.