3
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

2 Answers2

5

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)
MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31
  • but, after I apply `K.common.image_dim_ordering()` in my code another error came out `ValueError: Negative dimension size caused by subtracting 3 from 1 for 'conv2d_5/convolution' (op: 'Conv2D') with input shapes: [?,1,16,32], [3,3,32,15].` – Moch. Chamdani M Sep 22 '19 at 03:03
  • You should still use the `set` method for it. Did the error come with the `set` or with what I pasted above? – MyNameIsCaleb Sep 22 '19 at 03:06
  • Use the `set` method like you were doing before just with `common` added in and see if you get the error. Although you shouldn't get the error with that one either, in theory. – MyNameIsCaleb Sep 22 '19 at 03:09
  • It seems that may not work then. The rest of my answer stands that this is a known error and the method is legacy so you should read up on `image_data_format` and how to use that instead. – MyNameIsCaleb Sep 22 '19 at 03:13
  • Also it could be that there is something wrong with your code that is causing that, but that isn't part of your original question which was how to get to that method. – MyNameIsCaleb Sep 22 '19 at 03:15
  • But, when I try to fit the model the other error comes, `model.fit(xTrain, yTrain, epochs=20, batch_size= 160) score = model.evaluate(xTest, yTest, batch_size=128)` and the error what I got is `ValueError: Please provide as model inputs either a single array or a list of arrays` – Moch. Chamdani M Sep 22 '19 at 03:26
  • This is beyond the scope of your initial question. You can post a new question if you want to for that error. If my answer solved the question you posted please vote and accept it. @DaniCham – MyNameIsCaleb Sep 22 '19 at 03:27
1

Try replacing K.set_image_dim_ordering('th') with K.image_dim_ordering='th'. It worked for me.

I am not sure but mostly the issue is related to the keras version you are using.

Neel Joshi
  • 37
  • 4