Skip to content

Instantly share code, notes, and snippets.

@christopher-beckham
Last active April 1, 2017 16:30
Show Gist options
  • Select an option

  • Save christopher-beckham/bdea8be425ccbe7cec946c7692a7e050 to your computer and use it in GitHub Desktop.

Select an option

Save christopher-beckham/bdea8be425ccbe7cec946c7692a7e050 to your computer and use it in GitHub Desktop.
opfromgraph_doesnt_work_yet
import theano
from theano import tensor as T
from theano import OpFromGraph
import lasagne
from lasagne.layers import *
from lasagne.init import *
from lasagne.nonlinearities import *
from lasagne.objectives import *
from lasagne.updates import *
from lasagne.regularization import *
import sys
import os
class OpFromGraphLayer(Layer):
def __init__(self, incoming, block_fn, block_fn_args, inline=False,**kwargs):
super(OpFromGraphLayer, self).__init__(incoming, **kwargs)
if isinstance(incoming, InputLayer):
l_in = incoming
else:
# we must create a new input layer (and therefore a new input var)
# which has the incoming layer's output shape
l_in = InputLayer(incoming.output_shape)
final = block_fn(l_in, **block_fn_args)
final_out = get_output(final, l_in.input_var)
self.final = final
self.op = OpFromGraph([l_in.input_var], [final_out], inline=inline)
for layer in get_all_layers(final):
self.params.update( layer.params )
def get_output_shape_for(self, input_shape):
#return self.out_shp
return get_output_shape(self.final, input_shape)
def get_output_for(self, input, **kwargs):
return self.op(input)
def residual_block(l_inp, n_out_channels, stride=1):
conv1 = Conv2DLayer(l_inp, num_filters=n_out_channels, filter_size=3, stride=stride)
#conv1 = BatchNormLayer(conv1)
conv2 = Conv2DLayer(conv1, num_filters=n_out_channels, filter_size=3, stride=stride)
return conv2
l_in = InputLayer((None,3,224,224))
l_resblock = OpFromGraphLayer(l_in, block_fn=residual_block, block_fn_args={'n_out_channels':32}, inline=False)
l_resblock2 = OpFromGraphLayer(l_resblock, block_fn=residual_block, block_fn_args={'n_out_channels':64, 'stride':2}, inline=False)
for layer in get_all_layers(l_resblock2):
print layer, layer.output_shape
X = T.tensor4('X')
tmp = get_output(l_resblock, X)
theano.printing.pydotprint(tmp, outfile='test.png', var_with_name_simple=True)
tmp.eval({X:np.ones((1,3,224,224))})
theano.printing.pydotprint(tmp, outfile='test.png', var_with_name_simple=True)
# doesn't work for l_resblock2, but does for l_resblock
tmp.eval({X:np.ones((1,3,224,224))})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment