AutoKeras: TypeError: '<' not supported between instances of 'NoneType' and 'int'

703 Views Asked by At

Getting error in Autkeras model, where as same data work in keras model

Dataset example

image                  label
train/class0/3.jpg      0
train/class1/2.jpg      1
train/class1/6.jpg      1
train/class1/4.jpg      1
train/class0/7.jpg      0

load function

def load(image_path,label):
  img = tf.io.read_file(image_path)
  img = tf.image.decode_jpeg(img, channels=3)
  #img = tf.image.convert_image_dtype(img, tf.float32)
  img = tf.cast(img, tf.float32) / 255.0
  label = tf.cast(label, tf.int32)
  return img, label

load data

 bs=2
 train_ds = tf.data.Dataset.from_tensor_slices((train_df.image,train_df.label)).map(load).batch(bs)

keras model

model = tf.keras.Sequential([
  layers.InputLayer((224,224,3)),                         
  layers.Conv2D(32, 3, activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(32, 3, activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(32, 3, activation='relu'),
  layers.MaxPooling2D(),
  layers.Flatten(),
  layers.Dense(128, activation='relu'),
  layers.Dense(1,activation='sigmoid')
])

train keras model

model.compile(optimizer='adam',loss=tf.losses.BinaryCrossentropy(),metrics=['accuracy'])
model.fit(train_ds,epochs=1)

AutoKeras model

import autokeras as ak
clf = ak.ImageClassifier(overwrite=False, max_trials=1)
clf.fit(train_ds, epochs=1)

Error log in autokeras model

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 104, in build
    model = self.hypermodel.build(hp)
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 64, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/autokeras/graph.py", line 250, in build
    outputs = block.build(hp, inputs=temp_inputs)
  File "/usr/local/lib/python3.7/dist-packages/autokeras/engine/block.py", line 38, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 64, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/autokeras/blocks/wrapper.py", line 108, in build
    output_node = self._build_block(hp, output_node, block_type)
  File "/usr/local/lib/python3.7/dist-packages/autokeras/blocks/wrapper.py", line 77, in _build_block
    return basic.ResNetBlock().build(hp, output_node)
  File "/usr/local/lib/python3.7/dist-packages/autokeras/engine/block.py", line 38, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 64, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/autokeras/blocks/basic.py", line 688, in build
    if input_node.shape[1] < min_size or input_node.shape[2] < min_size:
TypeError: '<' not supported between instances of 'NoneType' and 'int'
1

There are 1 best solutions below

1
Frut Dzentready On

Too short to be real answer but. If you chek train_ds.element_spec you will see something like [None,None,3] or even [None,None,None]. In first case only thing you need to do is to resize image with img = tf.image.resize(img, [128, 128]) In second case you allso need to set channels on decode_<img> (i see that you allready set this)

Example used with loading from disk (tested to work with autokeras)

def process_path(filename):
    parts = tf.strings.split(filename, os.sep)
    label = int(parts[-2])

    image = tf.io.read_file(filename)
    image = tf.io.decode_png(image, channels=3)
    image = tf.image.convert_image_dtype(image, tf.float32)
    image = tf.image.resize(image, [256, 256])
    return image, label

import pathlib
load_from = pathlib.Path(load_from)
train_files_ds = tf.data.Dataset.list_files(str(load_from / 'train' / '*/*.png'))
train_ds: tf.data.Dataset = train_files_ds.map(process_path)

import autokeras as ak
clf = ak.ImageClassifier(overwrite=False, max_trials=1)
clf.fit(train_ds, epochs=1)