{"action":"created","issue":{"url":"https://api.github.com/repos/fchollet/keras/issues/3395","repository_url":"https://api.github.com/repos/fchollet/keras","labels_url":"https://api.github.com/repos/fchollet/keras/issues/3395/labels{/name}","comments_url":"https://api.github.com/repos/fchollet/keras/issues/3395/comments","events_url":"https://api.github.com/repos/fchollet/keras/issues/3395/events","html_url":"https://github.com/fchollet/keras/issues/3395","id":169374490,"number":3395,"title":"Deconvolution2D Inconsistent Output Shape","user":{"login":"rmkemker","id":14336785,"avatar_url":"https://avatars.githubusercontent.com/u/14336785?v=3","gravatar_id":"","url":"https://api.github.com/users/rmkemker","html_url":"https://github.com/rmkemker","followers_url":"https://api.github.com/users/rmkemker/followers","following_url":"https://api.github.com/users/rmkemker/following{/other_user}","gists_url":"https://api.github.com/users/rmkemker/gists{/gist_id}","starred_url":"https://api.github.com/users/rmkemker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rmkemker/subscriptions","organizations_url":"https://api.github.com/users/rmkemker/orgs","repos_url":"https://api.github.com/users/rmkemker/repos","events_url":"https://api.github.com/users/rmkemker/events{/privacy}","received_events_url":"https://api.github.com/users/rmkemker/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2016-08-04T13:29:39Z","updated_at":"2016-10-26T13:34:55Z","closed_at":null,"body":"I am having issues with the Deconvolution2D function.  First of all, I am using the theano backend with the bleeding edge Keras (pulled changes this morning) and Theano.  I am trying to replicate the FCN for Semantic Segmentation code in Keras.  The code below is my model.  There is also a graphical model at the very bottom of this post.  When I build the model, the Deconvolution2D get_output_shape returns (batch_size,21,512,512) even though I explicitly state that the output_shape should be (21,500,500).  My solution was to use a custom crop function (which is working well) to force it to be (21,500,500).  \r\n  \r\n    inputData = Input(batch_shape=(batch_size,3,500,500))\r\n    pad1_1 = ZeroPadding2D((100,100))(inputData)\r\n    conv1_1 = Convolution2D(64,3,3,activation='relu')(pad1_1)\r\n    pad1_2 = ZeroPadding2D((1,1))(conv1_1)\r\n    conv1_2 = Convolution2D(64,3,3,activation='relu')(pad1_2)\r\n    pool1 = MaxPooling2D((2,2), strides=(2,2))(conv1_2)\r\n\r\n    pad2_1 = ZeroPadding2D((1,1))(pool1)\r\n    conv2_1 = Convolution2D(128,3,3,activation='relu')(pad2_1)\r\n    pad2_2 = ZeroPadding2D((1,1))(conv2_1)\r\n    conv2_2 = Convolution2D(128,3,3,activation='relu')(pad2_2)\r\n    pool2 = MaxPooling2D((2,2), strides=(2,2))(conv2_2)\r\n    \r\n    pad3_1 = ZeroPadding2D((1,1))(pool2)\r\n    conv3_1 = Convolution2D(256,3,3,activation='relu')(pad3_1)\r\n    pad3_2 = ZeroPadding2D((1,1))(conv3_1)\r\n    conv3_2 = Convolution2D(256,3,3,activation='relu')(pad3_2)\r\n    pad3_3 = ZeroPadding2D((1,1))(conv3_2)\r\n    conv3_3 = Convolution2D(256,3,3,activation='relu')(pad3_3)\r\n    pool3 = MaxPooling2D((2,2), strides=(2,2))(conv3_3)  \r\n\r\n    pad4_1 = ZeroPadding2D((1,1))(pool3)\r\n    conv4_1 = Convolution2D(512,3,3,activation='relu')(pad4_1)\r\n    pad4_2 = ZeroPadding2D((1,1))(conv4_1)\r\n    conv4_2 = Convolution2D(512,3,3,activation='relu')(pad4_2)\r\n    pad4_3 = ZeroPadding2D((1,1))(conv4_2)\r\n    conv4_3 = Convolution2D(512,3,3,activation='relu')(pad4_3)\r\n    pool4 = MaxPooling2D((2,2), strides=(2,2))(conv4_3)    \r\n\r\n    pad5_1 = ZeroPadding2D((1,1))(pool4)\r\n    conv5_1 = Convolution2D(512,3,3,activation='relu')(pad5_1)\r\n    pad5_2 = ZeroPadding2D((1,1))(conv5_1)\r\n    conv5_2 = Convolution2D(512,3,3,activation='relu')(pad5_2)\r\n    pad5_3 = ZeroPadding2D((1,1))(conv5_2)\r\n    conv5_3 = Convolution2D(512,3,3,activation='relu')(pad5_3)\r\n    pool5 = MaxPooling2D((2,2), strides=(2,2))(conv5_3)\r\n           \r\n    fc6 = Convolution2D(4096,7,7,activation='relu')(pool5)\r\n    drop6 = Dropout(0.5)(fc6)\r\n    fc7 = Convolution2D(4096,1,1,activation='relu')(drop6)\r\n    drop7 = Dropout(0.5)(fc7)\r\n    score_fr = Convolution2D(21,1,1)(drop7)\r\n\r\n    deconv  = Deconvolution2D(21,64,64,output_shape=(batch_size,21,500,500),subsample=(32,32),bias=False)(score_fr)\r\n    crop = Cropping2D(((6,6),(6,6)))(deconv)\r\n\r\n    flat = Reshape((21,500*500))(crop)\r\n    permute = Permute((2,1))(flat)    \r\n    soft = Activation('softmax')(permute)\r\n    model = Model(input=inputData, output=soft)    \r\n\r\n    sgd = SGD(lr=1e-4, decay=5e-4, momentum=0.9, nesterov=True)\r\n    model.compile(loss='categorical_crossentropy', optimizer=sgd)\r\n    history = model.fit(trainData, trainGT, batch_size=batch_size, nb_epoch=10, verbose=1)\r\n\r\nWhen I run model.fit()  I get an error (below) that says that the output of my crop layer is actually (batch_size,21,488,488) which means that the K.deconv2d is actually outputting the shape that I requested. \r\n\r\n\r\n    File \"<ipython-input-5-271f8399bbb6>\", line 1, in <module>\r\n    runfile('/home/klab/Documents/Python Scripts/VOC2012/net.py', wdir='/home/klab/Documents/Python Scripts/VOC2012')\r\n\r\n    File \"/home/klab/anaconda3/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py\", line 699, in runfile\r\n    execfile(filename, namespace)\r\n\r\n    File \"/home/klab/anaconda3/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py\", line 88, in execfile\r\n    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)\r\n\r\n    File \"/home/klab/Documents/Python Scripts/VOC2012/net.py\", line 193, in <module>\r\n    history = model.fit(trainData, trainGT, batch_size=batch_size, nb_epoch=10, verbose=1)\r\n\r\n    File \"/home/klab/anaconda3/lib/python3.5/site-packages/Keras-1.0.6-py3.5.egg/keras/engine/training.py\", line 1107, in fit\r\n    callback_metrics=callback_metrics)\r\n\r\n    File \"/home/klab/anaconda3/lib/python3.5/site-packages/Keras-1.0.6-py3.5.egg/keras/engine/training.py\", line 825, in _fit_loop\r\n    outs = f(ins_batch)\r\n\r\n    File \"/home/klab/anaconda3/lib/python3.5/site-packages/Keras-1.0.6-py3.5.egg/keras/backend/theano_backend.py\", line 644, in __call__\r\n    return self.function(*inputs)\r\n\r\n    File \"/home/klab/anaconda3/lib/python3.5/site-packages/theano/compile/function_module.py\", line 879, in __call__\r\n    storage_map=getattr(self.fn, 'storage_map', None))\r\n    \r\n    File \"/home/klab/anaconda3/lib/python3.5/site-packages/theano/gof/link.py\", line 325, in raise_with_op\r\n    reraise(exc_type, exc_value, exc_trace)\r\n\r\n    File \"/home/klab/anaconda3/lib/python3.5/site-packages/six.py\", line 685, in reraise\r\n    raise value.with_traceback(tb)\r\n\r\n    File \"/home/klab/anaconda3/lib/python3.5/site-packages/theano/compile/function_module.py\", line 866, in __call__\r\n    self.fn() if output_subset is None else\\\r\n\r\n    ValueError: GpuReshape: cannot reshape input of shape (1, 21, 488, 488) to shape (0, 21, 250000).\r\n    Apply node that caused the error: GpuReshape{3}(GpuSubtensor{::, ::, int64:int64:, int64:int64:}.0, TensorConstant{[    -1   ..21 250000]})\r\n    Toposort index: 522\r\n    Inputs types: [CudaNdarrayType(float32, 4D), TensorType(int64, vector)]\r\n    Inputs shapes: [(1, 21, 488, 488), (3,)]\r\n    Inputs strides: [(0, 250000, 500, 1), (8,)]\r\n    Inputs values: ['not shown', array([    -1,     21, 250000])]\r\n    Outputs clients: [[GpuDimShuffle{0,2,1}(GpuReshape{3}.0), GpuElemwise{Composite{Cast{float32}(EQ(i0, i1))},no_inplace}(GpuDimShuffle{0,x,1}.0, GpuReshape{3}.0)]]\r\n\r\nI am going to checkout the deconv2d function in the theano backend file, but I thought I would bring it up to see if this is a problem for anyone else (or if I am just messing up something simple).  \r\n\r\n![current_fcn_model](https://cloud.githubusercontent.com/assets/14336785/17402801/ee11c5da-5a22-11e6-9b76-5a35a815c1a0.png)"},"comment":{"url":"https://api.github.com/repos/fchollet/keras/issues/comments/256348937","html_url":"https://github.com/fchollet/keras/issues/3395#issuecomment-256348937","issue_url":"https://api.github.com/repos/fchollet/keras/issues/3395","id":256348937,"user":{"login":"chaithu536","id":3897888,"avatar_url":"https://avatars.githubusercontent.com/u/3897888?v=3","gravatar_id":"","url":"https://api.github.com/users/chaithu536","html_url":"https://github.com/chaithu536","followers_url":"https://api.github.com/users/chaithu536/followers","following_url":"https://api.github.com/users/chaithu536/following{/other_user}","gists_url":"https://api.github.com/users/chaithu536/gists{/gist_id}","starred_url":"https://api.github.com/users/chaithu536/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chaithu536/subscriptions","organizations_url":"https://api.github.com/users/chaithu536/orgs","repos_url":"https://api.github.com/users/chaithu536/repos","events_url":"https://api.github.com/users/chaithu536/events{/privacy}","received_events_url":"https://api.github.com/users/chaithu536/received_events","type":"User","site_admin":false},"created_at":"2016-10-26T13:34:55Z","updated_at":"2016-10-26T13:34:55Z","body":"I faced the same issue with the output shape. I override the method 'get_output_shape_for()' of the class 'Deconvolution2D' to obtain the desired upsampling output shape. It worked.\r\n\r\nclass Deconv2D_custom(Deconvolution2D):\r\n    \r\n    def get_output_shape_for(self, input_shape):\r\n        \r\n        upsample_rate = self.subsample[0]\r\n        \r\n        if self.dim_ordering == 'th':\r\n            rows = input_shape[2]*upsample_rate\r\n            cols = input_shape[3]*upsample_rate\r\n            \r\n        elif self.dim_ordering == 'tf':\r\n            rows = input_shape[1]*upsample_rate\r\n            cols = input_shape[2]*upsample_rate\r\n        else:\r\n            raise Exception('Invalid dim_ordering: ' + self.dim_ordering)\r\n\r\n        if self.dim_ordering == 'th':\r\n            return (input_shape[0], self.nb_filter, rows, cols)\r\n        elif self.dim_ordering == 'tf':\r\n            return (input_shape[0], rows, cols, self.nb_filter)\r\n        else:\r\n            raise Exception('Invalid dim_ordering: ' + self.dim_ordering)\r\n\r\n\r\n"}}
{"actor":{"display_login":"chaithu536"}}