pythonkerasconv-neural-networkmax-pooling

AttributeError: module 'keras.backend' has no attribute 'set_image_dim_ordering'


model = Sequential()
K.set_image_dim_ordering('th')
model.add(Convolution2D(30, 5, 5, border_mode= 'valid' , input_shape=(1, 10, 10),activation= 'relu' ))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(15, 3, 3, activation= 'relu' ))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation= 'relu' ))
model.add(Dense(50, activation= 'relu' ))
model.add(Dense(10, activation= 'softmax' ))
# Compile model
model.compile(loss= 'categorical_crossentropy' , optimizer= 'adam' , metrics=[ 'accuracy' ])

I get an error when I use set_image_dim_ordering() from keras.backend

This the error report : AttributeError: module 'keras.backend' has no attribute 'set_image_dim_ordering'

My Import statement

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Convolution2D
from keras.layers.convolutional import MaxPooling2D
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
from keras import backend as K
from subprocess import check_output

Solution

  • A couple things to look at. This is a known issue and reported for version 2.2.5 but should work with 2.2.4 and before.

    However, you should stop using this method anyway because it is legacy now and has been replaced by image_data_format: [code]

    keras.backend.image_data_format()
    keras.backend.set_image_data_format(data_format)
    

    It should continue to work but there is a bug right now, at least.

    EDIT: Poster reported below does not work or returns an error with their code, though the method does seem to exist.

    Some have reported that you may be able to access the method using K.common but I haven't tested:

    K.common.image_dim_ordering()
    K.common.set_image_dim_ordering(dim_ordering)