Simplicity
Simplicity

Reputation: 48916

Keras - TypeError: 'NoneType' object cannot be interpreted as an index

I have the following code snippet:

model = load_model('model.h5')

optimizer = optimizers.Adam(lr=0.0001)

for root, dirs, files in os.walk(test_directory):
    sortedFiles = sorted(files, key=lambda x:int(x.split('.')[0]))
    for file in sortedFiles[0:]:
        img = cv2.imread(root + '/' + file)
        test_images.append(img)

test_images = np.array(test_images)
test_images = test_images.reshape((None,512,512,3))

Why am I getting:

TypeError: 'NoneType' object cannot be interpreted as an index

at:

test_images = test_images.reshape((None,512,512,3))

Thanks a lot.

Upvotes: 2

Views: 1859

Answers (2)

Lina Achaji
Lina Achaji

Reputation: 121

The None used in data shapes in Keras refers to the possibility that the input can have variable dimension size. It can vary depending on how many examples you give for training. This can be defined in the input layer of your Functional Keras model, such as :

inputs = Input(shape=(512,512,3,))

inputs
>> <tf.Tensor 'input_1:0' shape=(None, 512, 512, 3) dtype=float32>

In contrast, the input data to your model (test_images in your case) should have a fixed size defined by the (Batch_size, image_shape) where the Batch_size can vary from an input instance to another.

For instance, if you have 1000 samples data, you should get something like that:

test_images.shape
>> (1000, 512, 512, 3)

Upvotes: 0

qscgy
qscgy

Reputation: 311

While keras sometimes has None in data shapes, Numpy does not. If you want to have a dimension of variable size, you put -1 instead. Change your last line to:

test_images = test_images.reshape((-1,512,512,3))

Upvotes: 1

Related Questions