Created
November 24, 2016 21:43
-
-
Save gvtulder/e41f1ee8eeab2ee06ecc47b3f8979d06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import numpy | |
import theano | |
import theano.tensor as T | |
from theano.tensor.nnet.abstract_conv import AbstractConv2d | |
from theano.tensor.nnet.abstract_conv import AbstractConv2d_gradInputs | |
from theano.tensor.nnet.corr import CorrMM_gradInputs | |
from theano.gpuarray.blas import GpuCorrMM_gradInputs | |
from theano.gpuarray.dnn import dnn_gradinput | |
def run_test_shapes(input_shape, filters_shape, output_shape, border_mode, subsample, filter_dilation, provide_shape): | |
output_fields = [input_shape, filters_shape, output_shape, border_mode, subsample, filter_dilation, provide_shape] | |
input = theano.shared(numpy.ones(input_shape, 'float32')) | |
filters = theano.shared(numpy.ones(filters_shape, 'float32')) | |
output = theano.shared(numpy.ones(output_shape, 'float32')) | |
def abstractconv_forward_shape(): | |
op = AbstractConv2d( | |
imshp=(input_shape if provide_shape else None), | |
kshp=(filters_shape if provide_shape else None), | |
border_mode=border_mode, | |
subsample=subsample, | |
filter_dilation=filter_dilation) | |
c = op(input, filters) | |
f = theano.function([], c, mode=theano.Mode(optimizer='fast_compile')) | |
res = f() | |
return res.shape | |
implementations = [] | |
def imp_abstractconv_backward_shape(): | |
op = AbstractConv2d_gradInputs( | |
imshp=(input_shape if provide_shape else None), | |
kshp=(filters_shape if provide_shape else None), | |
border_mode=border_mode, | |
subsample=subsample, | |
filter_dilation=filter_dilation) | |
c = op(filters, output, input_shape[-2:]) | |
f = theano.function([], c.shape) | |
return tuple(f()) | |
implementations.append(('abstract_bwd_shape', imp_abstractconv_backward_shape)) | |
def imp_cpu_corr(): | |
op = CorrMM_gradInputs( | |
border_mode=border_mode, | |
subsample=subsample, | |
filter_dilation=filter_dilation) | |
c = op(filters, output, (input_shape[-2:] if provide_shape else None)) | |
f = theano.function([], c, mode=theano.Mode(optimizer=None)) | |
res = f() | |
return res.shape | |
implementations.append(('cpu_corr', imp_cpu_corr)) | |
def imp_gpu_corr(): | |
op = GpuCorrMM_gradInputs( | |
border_mode=border_mode, | |
subsample=subsample, | |
filter_dilation=filter_dilation) | |
c = op(filters, output, (input_shape[-2:] if provide_shape else None)) | |
f = theano.function([], c, mode=theano.Mode(optimizer=None)) | |
res = f() | |
return res.shape | |
# implementations.append(('gpu_corr', imp_gpu_corr)) | |
def imp_gpu_dnn(): | |
assert provide_shape | |
assert filter_dilation == (1, 1) | |
c = dnn_gradinput( | |
filters, | |
output, | |
img_shp=input_shape, | |
border_mode=border_mode, | |
subsample=subsample) | |
f = theano.function([], c, mode=theano.Mode(optimizer=None)) | |
res = f() | |
return res.shape | |
implementations.append(('gpu_dnn', imp_gpu_dnn)) | |
try: | |
res_shape = abstractconv_forward_shape() | |
if res_shape == output_shape: | |
output_fields.append(' ' + str(res_shape)) | |
else: | |
output_fields.append('-' + str(res_shape)) | |
except Exception: | |
output_fields.append('-') | |
result_shapes = [] | |
shape_not_diff = [] | |
for (implementation_name, implementation) in implementations: | |
try: | |
res_shape = implementation() | |
result_shapes.append(res_shape) | |
if result_shapes[0] == res_shape: | |
output_fields.append(' ' + str(res_shape)) | |
else: | |
output_fields.append('!' + str(res_shape)) | |
except Exception: | |
output_fields.append('') | |
# all same? | |
if len(result_shapes) > 0 and any(shp != result_shapes[0] for shp in result_shapes): | |
output_fields.append('yes') | |
else: | |
output_fields.append('') | |
print(''.join(['%-17s' % str(s) for s in output_fields])) | |
fields = ('input_shape', 'filters_shape', 'output_shape', 'border_mode', 'subsample', 'filter_dil', 'provide_shape', 'fwd_shape', 'bwd_shape', 'cpu_corr', 'gpu_dnn', 'different?') | |
print(''.join(['%-17s' % s for s in fields])) | |
for border_mode in ('valid', 'full', 'half'): | |
for i in range(1, 20): | |
for f in range(1, 5): | |
for o in range(1, 20): | |
for s in (1, 2, 3): | |
for d in (1, 2, 3): | |
for provide_shape in (True, False): | |
input_shape = (1, 1, i, i) | |
filters_shape = (1, 1, f, f) | |
output_shape = (1, 1, o, o) | |
subsample = (s, s) | |
filter_dilation = (d, d) | |
run_test_shapes(input_shape, filters_shape, output_shape, border_mode, subsample, filter_dilation, provide_shape) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment