Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keras - cannot import name Conv2D

I recently got the deep learning docker from https://github.com/floydhub/dl-docker running and while trying out the tutorials, received an error when importing the keras layers module.

from __future__ import print_function
import keras
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-13-3a12c6f32fcf> in <module>()
      5 from keras.models import Sequential
      6 from keras.layers import Dense, Dropout, Activation, Flatten
----> 7 from keras.layers import Conv2D, MaxPooling2D

ImportError: cannot import name Conv2D

I am running with ubuntu 14.04, python version 2.7.6 on the ipython notebook and the following versions of the deep learning libraries on docker.

ARG THEANO_VERSION=rel-0.8.2
ARG TENSORFLOW_VERSION=0.12.1 
ARG TENSORFLOW_ARCH=cpu
ARG KERAS_VERSION=1.2.0
ARG LASAGNE_VERSION=v0.1
ARG TORCH_VERSION=latest
ARG CAFFE_VERSION=master

Im not sure if the problem lies with the version because it seems that there no related issues on the github thread.

like image 276
Kong Avatar asked May 23 '17 09:05

Kong


2 Answers

Try this: from keras.layers.convolutional import Conv2D

Importing changed with the new keras. Are you sure you are not using keras >= 2?


NOTE:

With tensorflow 2.0 keras is included. You can now import the layer with:

from tensorflow.keras.layers import Conv2D
like image 168
Wilmar van Ommeren Avatar answered Sep 28 '22 04:09

Wilmar van Ommeren


For Keras 1.2.0 (the current one on floydhub as of print(keras.__version__)) use these imports for Conv2D (which you use) and Conv2DTranspose (used in the Keras examples):

from keras.layers import Convolution2D as Conv2D
from keras.layers.convolutional import Deconv2D as Conv2DTranspose
like image 34
user2707001 Avatar answered Sep 28 '22 04:09

user2707001