Kong
Kong

Reputation: 2410

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.

Upvotes: 22

Views: 60301

Answers (4)

Rahman
Rahman

Reputation: 25

It might be late but still it can be useful to those who use IntelliJ IDEA for python programming. If you want to use Conv2D of Tensorflow 2.x, then first, download tensorflow package in your IDE and import Conv2D as below:

from tensorflow.python.keras.layers import Conv2D

Upvotes: 0

Wilmar van Ommeren
Wilmar van Ommeren

Reputation: 7689

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

Upvotes: 28

pushd93
pushd93

Reputation: 405

Following packages which are very essential for CNN (Convolutional Neural Networks) are reorganized into different packages

from keras.layers.convolutional import Conv2D
from keras.layers import Dense
from keras.layers.convolutional import MaxPooling2D
from keras.layers import Flatten

Whenever you get an import error always google the name for the package and the library it is associated for example google "Keras Convolution2D". It will direct you to the keras documentation. That will easily give away the path to import.

Upvotes: 6

user2707001
user2707001

Reputation: 1691

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

Upvotes: 4

Related Questions