Skip to content

Instantly share code, notes, and snippets.

@lukovkin
Created December 10, 2016 15:54
Show Gist options
  • Save lukovkin/ec46022ef7f8e79d419ccb2e8528087a to your computer and use it in GitHub Desktop.
Save lukovkin/ec46022ef7f8e79d419ccb2e8528087a to your computer and use it in GitHub Desktop.
Working Deconv2D example for Keras 1.1.2 and TensorFlow 0.11-0.12
import os
os.environ["KERAS_BACKEND"] = "tensorflow"
import numpy as np
from keras.models import Sequential
from keras.layers import Deconvolution2D
import warnings
# apply a 3x3 transposed convolution with stride 1x1 and 3 output filters on a 12x12 image:
model = Sequential()
model.add(Deconvolution2D(3, 3, 3, output_shape=(32, 14, 14, 3), border_mode='valid', input_shape=(12, 12, 3)))
model.summary()
# Note that you will have to change the output_shape depending on the backend used.
# we can predict with the model and print the shape of the array.
dummy_input = np.ones((32, 12, 12, 3))
# For TensorFlow dummy_input = np.ones((32, 12, 12, 3))
preds = model.predict(dummy_input)
print(preds.shape)
# Theano GPU: (None, 3, 13, 13)
# Theano CPU: (None, 3, 14, 14)
# TensorFlow: (None, 14, 14, 3)
model = Sequential()
model.add(Deconvolution2D(3, 3, 3, output_shape=(32, 25, 25, 3), subsample=(2, 2), border_mode='valid', input_shape=(12, 12, 3)))
model.summary()
# we can predict with the model and print the shape of the array.
dummy_input = np.ones((32, 12, 12, 3))
# For TensorFlow dummy_input = np.ones((32, 12, 12, 3))
preds = model.predict(dummy_input)
print(preds.shape)
# Theano GPU: (None, 3, 25, 25)
# Theano CPU: (None, 3, 25, 25)
# TensorFlow: (None, 25, 25, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment