Key error: 'labels' during data training making a model - python

Hi I am using virtual env and making a train model for my project and using keras 2.3.1 and tensorflow 2.2.0 All my code is working but I am run last line and exception's occur ,so line is here;
from Lib.data_loader import DataLoader
from Lib.resnet_model import Resnet3DBuilder
from Lib.HistoryGraph import HistoryGraph
import Lib.image as img
from Lib.utils import mkdirs
import os
from math import ceil
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint
target_size = (64,96)
nb_frames = 16 # here this will get number of pictres from datasets folder
skip = 1 # using resnet we skip different layers
nb_classes = 27
batch_size = 64
input_shape = (nb_frames,) + target_size + (3,)
workers = 8
use_multiprocessing = False
max_queue_size = 20
data_root = r"D:\FYP\DataSet"
csv_labels = r"D:\FYP\DataSet\jester-v1-labels.csv"
csv_train = r"D:\FYP\DataSet\jester-v1-train.csv"
csv_val = r"D:\FYP\DataSet\jester-v1-validation.csv"
csv_test = r"D:\FYP\DataSet\jester-v1-test.csv "
data_vid = r"D:\FYP\DataSet\videos"
model_name = 'resent_3d_model'
data_model = r"D:\FYP\DataSet\Model"
path_model = os.path.join(data_root, data_model, model_name)
path_vid = os.path.join(data_root, data_vid)
path_labels = os.path.join(data_root, csv_labels)
path_train = os.path.join(data_root, csv_train)
path_val = os.path.join(data_root, csv_val)
path_test = os.path.join(data_root, csv_test)
data = DataLoader(path_vid, path_labels, path_train, path_val)
mkdirs(path_model, 0o755)
mkdirs(os.path.join(path_model, "graphs"), 0o755)
gen = img.ImageDataGenerator()
gen_train = gen.flow_video_from_dataframe(data.train_df, path_vid, path_classes=path_labels, x_col='video_id', y_col="labels", target_size=target_size, batch_size=batch_size, nb_frames=nb_frames, skip=skip, has_ext=True)
gen_val = gen.flow_video_from_dataframe(data.val_df, path_vid, path_classes=path_labels, x_col='video_id', y_col="labels", target_size=target_size, batch_size=batch_size, nb_frames=nb_frames, skip=skip, has_ext=True)
resnet_model = Resnet3DBuilder.build_resnet_101(input_shape, nb_classes, drop_rate = 0.5)
optimizer = SGD(lr=0.01, momentum=0.9, decay=0.0001, nesterov=False)
resnet_model.compile(optimizer = optimizer, loss= "categorical_crossentropy" , metrics=["accuracy"])
model_file = os.path.join(path_model, 'resnetmodel.hdf5')
model_checkpointer = ModelCheckpoint(model_file, monitor='val_acc',verbose=1, save_best_only=True, mode='max')
history_graph = HistoryGraph(model_path_name = os.path.join(path_model, "graphs"))
nb_sample_train = data.train_df["video_id"].size
nb_sample_val = data.val_df["video_id"].size
resnet_model.fit_generator(
generator = gen_train,
steps_per_epoch = ceil(nb_sample_train/batch_size),
epochs=100,
validation_data=gen_val,
validation_steps=30,
shuffle=True,
verbose=1,
workers=workers,
max_queue_size = max_queue_size,
use_multiprocessing = use_multiprocessing,
callbacks = [model_checkpointer, history_graph])
And error is here below when I am running last line
Epoch 1/100
C:\Users\Virus\anaconda3\envs\HandGestureRecognitionSystem\lib\site-packages\keras\utils\data_utils.py:613: UserWarning: The input 80 could not be retrieved. It could be because a worker has died.
warnings.warn(
---------------------------------------------------------------------------
TimeoutError Traceback (most recent call last)
~\anaconda3\envs\HandGestureRecognitionSystem\lib\site-packages\keras\utils\data_utils.py in get(self)
609 future = self.queue.get(block=True)
--> 610 inputs = future.get(timeout=30)
611 except mp.TimeoutError:
~\anaconda3\envs\HandGestureRecognitionSystem\lib\multiprocessing\pool.py in get(self, timeout)
766 if not self.ready():
--> 767 raise TimeoutError
768 if self._success:
TimeoutError:
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
~\anaconda3\envs\HandGestureRecognitionSystem\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3079 try:
-> 3080 return self._engine.get_loc(casted_key)
3081 except KeyError as err:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'labels'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
<ipython-input-15-6810853f4b54> in <module>
----> 1 resnet_model.fit_generator(
2 generator = gen_train,
3 steps_per_epoch = ceil(nb_sample_train/batch_size),
4 epochs=100,
5 validation_data=gen_val,
~\anaconda3\envs\HandGestureRecognitionSystem\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~\anaconda3\envs\HandGestureRecognitionSystem\lib\site-packages\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1716 ```
1717 """
-> 1718 return training_generator.fit_generator(
1719 self, generator,
1720 steps_per_epoch=steps_per_epoch,
~\anaconda3\envs\HandGestureRecognitionSystem\lib\site-packages\keras\engine\training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
183 batch_index = 0
184 while steps_done < steps_per_epoch:
--> 185 generator_output = next(output_generator)
186
187 if not hasattr(generator_output, '__len__'):
~\anaconda3\envs\HandGestureRecognitionSystem\lib\site-packages\keras\utils\data_utils.py in get(self)
623 except Exception:
624 self.stop()
--> 625 six.reraise(*sys.exc_info())
626
627
~\anaconda3\envs\HandGestureRecognitionSystem\lib\site-packages\six.py in reraise(tp, value, tb)
701 if value.__traceback__ is not tb:
702 raise value.with_traceback(tb)
--> 703 raise value
704 finally:
705 value = None
~\anaconda3\envs\HandGestureRecognitionSystem\lib\site-packages\keras\utils\data_utils.py in get(self)
615 ' It could be because a worker has died.'.format(idx),
616 UserWarning)
--> 617 inputs = self.sequence[idx]
618 finally:
619 self.queue.task_done()
D:\HandGesturesProject\Lib\image.py in __getitem__(self, idx)
1534 index_array = self.index_array[self.batch_size * idx:
1535 self.batch_size * (idx + 1)]
-> 1536 return self._get_batches_of_transformed_samples(index_array)
1537
1538 def common_init(self, image_data_generator,
D:\HandGesturesProject\Lib\image.py in _get_batches_of_transformed_samples(self, index_array)
2243 dtype=self.dtype)
2244
-> 2245 for i, label in enumerate(self.df.iloc[index_array][self.y_col].values):
2246 batch_y[i, self.classes_indices[label]] = 1
2247
~\anaconda3\envs\HandGestureRecognitionSystem\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
3022 if self.columns.nlevels > 1:
3023 return self._getitem_multilevel(key)
-> 3024 indexer = self.columns.get_loc(key)
3025 if is_integer(indexer):
3026 indexer = [indexer]
~\anaconda3\envs\HandGestureRecognitionSystem\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3080 return self._engine.get_loc(casted_key)
3081 except KeyError as err:
-> 3082 raise KeyError(key) from err
3083
3084 if tolerance is not None:
KeyError: 'labels'
Anyone help me how to deal with it thankyou.

Related

How to use TimeseriesGenerator for GRU.fit()?

I am using TimeseriesGenerator for my problem.
The shapes for my train and test data are:
x_train - (306720, 20)
x_test - (306720,)
y_train - (4321, 20)
y_test - (4321,)
And their dtype is float64. And I dont need to use to.numpy() anymore.
I then use TimeSeriesGenerator
train_data = TimeseriesGenerator(x_train, x_test, length=144, batch_size=100)
test_data = TimeseriesGenerator(y_train, y_test, length=144, batch_size=100)
When I try to run
GRU = keras.models.Sequential([keras.layers.GRU(100), keras.layers.Dense(32, activation= 'relu')])
GRU.compile(loss="mae", optimizer="adam")
resultsGRU = GRU.fit(train_data, test_data, epochs = 5)
I get the following error:
File ~\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.__traceback__)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File ~\Anaconda3\lib\site-packages\keras\engine\data_adapter.py:997, in KerasSequenceAdapter.__init__(self, x, y, sample_weights, shuffle, workers, use_multiprocessing, max_queue_size, model, **kwargs)
984 def __init__(
985 self,
986 x,
(...)
994 **kwargs
995 ):
996 if not is_none_or_empty(y):
--> 997 raise ValueError(
998 "`y` argument is not supported when using "
999 "`keras.utils.Sequence` as input."
1000 )
1001 if not is_none_or_empty(sample_weights):
1002 raise ValueError(
1003 "`sample_weight` argument is not supported when using "
1004 "`keras.utils.Sequence` as input."
1005 )
ValueError: `y` argument is not supported when using `keras.utils.Sequence` as input.
I tried
x, y = train_data[0]
print(x.shape, y.shape)
to convert it to float before I use GRU.fit(), but I get this error
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
File ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py:3621, in Index.get_loc(self, key, method, tolerance)
3620 try:
-> 3621 return self._engine.get_loc(casted_key)
3622 except KeyError as err:
File ~\Anaconda3\lib\site-packages\pandas\_libs\index.pyx:136, in pandas._libs.index.IndexEngine.get_loc()
File ~\Anaconda3\lib\site-packages\pandas\_libs\index.pyx:163, in pandas._libs.index.IndexEngine.get_loc()
File pandas\_libs\hashtable_class_helper.pxi:2131, in pandas._libs.hashtable.Int64HashTable.get_item()
File pandas\_libs\hashtable_class_helper.pxi:2140, in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 4331
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
Input In [205], in <cell line: 1>()
----> 1 x, y = train_data[0]
2 print(x.shape, y.shape)
File ~\Anaconda3\lib\site-packages\keras\preprocessing\sequence.py:189, in TimeseriesGenerator.__getitem__(self, index)
177 rows = np.arange(
178 i,
179 min(i + self.batch_size * self.stride, self.end_index + 1),
180 self.stride,
181 )
183 samples = np.array(
184 [
185 self.data[row - self.length : row : self.sampling_rate]
186 for row in rows
187 ]
188 )
--> 189 targets = np.array([self.targets[row] for row in rows])
191 if self.reverse:
192 return samples[:, ::-1, ...], targets
File ~\Anaconda3\lib\site-packages\keras\preprocessing\sequence.py:189, in <listcomp>(.0)
177 rows = np.arange(
178 i,
179 min(i + self.batch_size * self.stride, self.end_index + 1),
180 self.stride,
181 )
183 samples = np.array(
184 [
185 self.data[row - self.length : row : self.sampling_rate]
186 for row in rows
187 ]
188 )
--> 189 targets = np.array([self.targets[row] for row in rows])
191 if self.reverse:
192 return samples[:, ::-1, ...], targets
File ~\Anaconda3\lib\site-packages\pandas\core\series.py:958, in Series.__getitem__(self, key)
955 return self._values[key]
957 elif key_is_scalar:
--> 958 return self._get_value(key)
960 if is_hashable(key):
961 # Otherwise index.get_value will raise InvalidIndexError
962 try:
963 # For labels that don't resolve as scalars like tuples and frozensets
File ~\Anaconda3\lib\site-packages\pandas\core\series.py:1069, in Series._get_value(self, label, takeable)
1066 return self._values[label]
1068 # Similar to Index.get_value, but we do not fall back to positional
-> 1069 loc = self.index.get_loc(label)
1070 return self.index._get_values_for_loc(self, loc, label)
File ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py:3623, in Index.get_loc(self, key, method, tolerance)
3621 return self._engine.get_loc(casted_key)
3622 except KeyError as err:
-> 3623 raise KeyError(key) from err
3624 except TypeError:
3625 # If we have a listlike key, _check_indexing_error will raise
3626 # InvalidIndexError. Otherwise we fall through and re-raise
3627 # the TypeError.
3628 self._check_indexing_error(key)
KeyError: 4331
Can anyone please explain what is wrong?
My whole code worked fine before, I re-ran it to check if everything really works and now I suddenly have this problem and I don't know how to fix it.

I could nopt make function predict to work

def custom_predictions(path):
img = ig.load_img(path, target_size=(64, 64), grayscale=False, color_mode='rgb', interpolation='nearest')
plt.imshow(img)
img = np.expand_dims(img, axis=0)
img.reshape(1,64,64,3)
print(np.shape(img))
prediction = np.argmax(model.predict(np.array(img)))
# result=loaded_model.predict_classes(img)
plt.title(labels[prediction])
plt.show()
custom_predictions('Desktop/data1a/training/00-damage/0007.JPEG')
(1, 64, 64, 3)
---------------------------------------------------------------------------
InternalError Traceback (most recent call last)
Input In [7], in <cell line: 1>()
----> 1 custom_predictions('Desktop/data1a/training/00-damage/0007.JPEG')
Input In [6], in custom_predictions(path)
5 img.reshape(1,64,64,3)
6 print(np.shape(img))
----> 7 prediction = np.argmax(model.predict(np.array(img)))
8 # result=loaded_model.predict_classes(img)
9 plt.title(labels[prediction])
File ~\anaconda3\envs\tf2.5\lib\site-packages\tensorflow\python\keras\engine\training.py:1696, in Model.predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
1690 except ValueError:
1691 warnings.warn('Using Model.predict with '
1692 'MultiWorkerDistributionStrategy or TPUStrategy and '
1693 'AutoShardPolicy.FILE might lead to out-of-order result'
1694 '. Consider setting it to AutoShardPolicy.DATA.')
-> 1696 data_handler = data_adapter.get_data_handler(
1697 x=x,
1698 batch_size=batch_size,
1699 steps_per_epoch=steps,
1700 initial_epoch=0,
1701 epochs=1,
1702 max_queue_size=max_queue_size,
1703 workers=workers,
1704 use_multiprocessing=use_multiprocessing,
1705 model=self,
1706 steps_per_execution=self._steps_per_execution)
1708 # Container that configures and calls `tf.keras.Callback`s.
1709 if not isinstance(callbacks, callbacks_module.CallbackList):
File ~\anaconda3\envs\tf2.5\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py:1364, in get_data_handler(*args, **kwargs)
1362 if getattr(kwargs["model"], "_cluster_coordinator", None):
1363 return _ClusterCoordinatorDataHandler(*args, **kwargs)
-> 1364 return DataHandler(*args, **kwargs)
File ~\anaconda3\envs\tf2.5\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py:1150, in DataHandler.__init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution, distribute)
1148 else:
1149 self._steps_per_execution = steps_per_execution
-> 1150 self._steps_per_execution_value = steps_per_execution.numpy().item()
1152 adapter_cls = select_data_adapter(x, y)
1153 self._verify_data_adapter_compatibility(adapter_cls)
File ~\anaconda3\envs\tf2.5\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py:628, in BaseResourceVariable.numpy(self)
626 def numpy(self):
627 if context.executing_eagerly():
--> 628 return self.read_value().numpy()
629 raise NotImplementedError(
630 "numpy() is only available when eager execution is enabled.")
File ~\anaconda3\envs\tf2.5\lib\site-packages\tensorflow\python\framework\ops.py:1094, in _EagerTensorBase.numpy(self)
1071 """Copy of the contents of this Tensor into a NumPy array or scalar.
1072
1073 Unlike NumPy arrays, Tensors are immutable, so this method has to copy
(...)
1091 NumPy dtype.
1092 """
1093 # TODO(slebedev): Consider avoiding a copy for non-CPU or remote tensors.
-> 1094 maybe_arr = self._numpy() # pylint: disable=protected-access
1095 return maybe_arr.copy() if isinstance(maybe_arr, np.ndarray) else maybe_arr
File ~\anaconda3\envs\tf2.5\lib\site-packages\tensorflow\python\framework\ops.py:1062, in _EagerTensorBase._numpy(self)
1060 return self._numpy_internal()
1061 except core._NotOkStatusException as e: # pylint: disable=protected-access
-> 1062 six.raise_from(core._status_to_exception(e.code, e.message), None)
File :3, in raise_from(value, from_value)
InternalError: stream did not block host until done; was already in an error state

KeyError: 'Failed to format this callback filepath: "saved_models\\dataweights.{epoch:02d}-{val_accuracy:.2f}.hdf5". Reason: \'val_accuracy\''

I already tried changing val_acc to val_accuracy however it didn't help the error message.
I changed it in both
keras.callbacks.ModelCheckpoint(saved_weight,monitor='val_accuracy', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', save_freq=2)
and os.path.join(P_MODELSAVE, 'dataweights.{epoch:02d}-{val_accuracy:.2f}.hdf5')
It's running on Jupyter Notebook and tensorflow 2.3.0 is in use.
This is the full error message:
~\Anaconda3\lib\site-packages\tensorflow\python\keras\callbacks.py in _get_file_path(self, epoch, logs)
1329 # placeholders can cause formatting to fail.
-> 1330 file_path = self.filepath.format(epoch=epoch + 1, **logs)
1331 except KeyError as e:
KeyError: 'val_accuracy'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-13-918d482685ad> in <module>
----> 1 model.fit(train_noisy_batches,
2 steps_per_epoch = train_batches.samples // batch_size,
3 epochs=epochs,
4 verbose=1,
5 validation_data=val_noisy_batches,
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
106 def _method_wrapper(self, *args, **kwargs):
107 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
--> 108 return method(self, *args, **kwargs)
109
110 # Running inside `run_distribute_coordinator` already.
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1101 logs = tmp_logs # No error, now safe to assign to logs.
1102 end_step = step + data_handler.step_increment
-> 1103 callbacks.on_train_batch_end(end_step, logs)
1104 epoch_logs = copy.copy(logs)
1105
~\Anaconda3\lib\site-packages\tensorflow\python\keras\callbacks.py in on_train_batch_end(self, batch, logs)
438 """
439 if self._should_call_train_batch_hooks:
--> 440 self._call_batch_hook(ModeKeys.TRAIN, 'end', batch, logs=logs)
441
442 def on_test_batch_begin(self, batch, logs=None):
~\Anaconda3\lib\site-packages\tensorflow\python\keras\callbacks.py in _call_batch_hook(self, mode, hook, batch, logs)
287 self._call_batch_begin_hook(mode, batch, logs)
288 elif hook == 'end':
--> 289 self._call_batch_end_hook(mode, batch, logs)
290 else:
291 raise ValueError('Unrecognized hook: {}'.format(hook))
~\Anaconda3\lib\site-packages\tensorflow\python\keras\callbacks.py in _call_batch_end_hook(self, mode, batch, logs)
307 batch_time = time.time() - self._batch_start_time
308
--> 309 self._call_batch_hook_helper(hook_name, batch, logs)
310
311 if self._check_timing:
~\Anaconda3\lib\site-packages\tensorflow\python\keras\callbacks.py in _call_batch_hook_helper(self, hook_name, batch, logs)
340 hook = getattr(callback, hook_name)
341 if getattr(callback, '_supports_tf_logs', False):
--> 342 hook(batch, logs)
343 else:
344 if numpy_logs is None: # Only convert once.
~\Anaconda3\lib\site-packages\tensorflow\python\keras\callbacks.py in on_train_batch_end(self, batch, logs)
1238 def on_train_batch_end(self, batch, logs=None):
1239 if self._should_save_on_batch(batch):
-> 1240 self._save_model(epoch=self._current_epoch, logs=logs)
1241
1242 def on_epoch_begin(self, epoch, logs=None):
~\Anaconda3\lib\site-packages\tensorflow\python\keras\callbacks.py in _save_model(self, epoch, logs)
1280 logs = tf_utils.to_numpy_or_python_type(logs)
1281 self.epochs_since_last_save = 0
-> 1282 filepath = self._get_file_path(epoch, logs)
1283
1284 try:
~\Anaconda3\lib\site-packages\tensorflow\python\keras\callbacks.py in _get_file_path(self, epoch, logs)
1330 file_path = self.filepath.format(epoch=epoch + 1, **logs)
1331 except KeyError as e:
-> 1332 raise KeyError('Failed to format this callback filepath: "{}". '
1333 'Reason: {}'.format(self.filepath, e))
1334 self._write_filepath = distributed_file_utils.write_filepath(
KeyError: 'Failed to format this callback filepath: "saved_models\\dataweights.{epoch:02d}-{val_accuracy:.2f}.hdf5". Reason: \'val_accuracy\''
My model.compile code is:
model.compile(optimizer=model_opt, loss='mse', metrics=['accuracy'])
My model.fit code is:
model.fit(train_noisy_batches, steps_per_epoch = train_batches.samples // batch_size,
epochs=epochs, verbose=1, validation_data=val_noisy_batches, validation_steps = train_batches.samples // batch_size,
callbacks=[modelchk, tensorboard, csv_logger], use_multiprocessing=False)
I don't really know how to fix this in the code or if this is a bigger issue. Please help.

Use IPython Widget Button to call Keras Training Function

I would like to use an ipython button to run a function that trains a deep learning model using Keras's fit.generator() and ImageDataGenerator(). I tried to use lambda to pass the arguments to the function, but it returns TypeError: expected str, bytes or os.PathLike object, not Button.
Code:
def trainGenerator(batch_size,train_path,image_folder,mask_folder,aug_dict,image_color_mode = "grayscale",
mask_color_mode = "grayscale",image_save_prefix = "image",mask_save_prefix = "mask",
flag_multi_class = False,num_class = 2,save_to_dir = None,target_size = (256,256),seed = 1):
image_datagen = ImageDataGenerator(**aug_dict)
mask_datagen = ImageDataGenerator(**aug_dict)
image_generator = image_datagen.flow_from_directory(
train_path,
classes = [image_folder],
class_mode = None,
color_mode = image_color_mode,
target_size = target_size,
batch_size = batch_size,
save_to_dir = save_to_dir,
save_prefix = image_save_prefix,
seed = seed)
mask_generator = mask_datagen.flow_from_directory(
train_path,
classes = [mask_folder],
class_mode = None,
color_mode = mask_color_mode,
target_size = target_size,
batch_size = batch_size,
save_to_dir = save_to_dir,
save_prefix = mask_save_prefix,
seed = seed)
train_generator = zip(image_generator, mask_generator)
for (img,mask) in train_generator:
img,mask = adjustData(img,mask,flag_multi_class,num_class)
yield (img,mask)
def segmentation_training(trainfolder, modelname):
data_gen_args = dict(rotation_range=0.1,
width_shift_range=[0.0, 0, 0.5],
height_shift_range=[0.0, 0, 0.5],
zoom_range=[0.5,1],
horizontal_flip=True,
fill_mode='nearest')
myGene = trainGenerator(2,trainfolder,'image','label',data_gen_args,save_to_dir = None)
model = unet()
model_checkpoint = ModelCheckpoint(os.path.join('Models',modelname+'.hdf5'), monitor='loss',verbose=1, save_best_only=True)
model.fit_generator(myGene,steps_per_epoch=3,epochs=1,callbacks=[model_checkpoint])
modelname = "test"
trainfolder = Path('Data/Segmentation/dataset/train')
btn = widgets.Button(description="Run")
btn.on_click(lambda trainfolder=trainfolder, modelname=modelname : segmentation_training(trainfolder,modelname))
display(btn)
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-41-d4282548b872> in <lambda>(trainfolder, modelname)
46 trainfolder = Path('Data/Segmentation/dataset/train')
47 btn = widgets.Button(description="Run")
---> 48 btn.on_click(lambda trainfolder=trainfolder, modelname=modelname : segmentation_training(trainfolder,modelname))
49 display(btn)
<ipython-input-41-d4282548b872> in segmentation_training(trainfolder, modelname)
40 model = unet()
41 model_checkpoint = ModelCheckpoint(os.path.join('Models',modelname+'.hdf5'), monitor='loss',verbose=1, save_best_only=True)
---> 42 model.fit_generator(myGene,steps_per_epoch=3,epochs=1,callbacks=[model_checkpoint])
43
44
~/virtualenv/lib/python3.6/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name +
90 '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~/virtualenv/lib/python3.6/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1413 use_multiprocessing=use_multiprocessing,
1414 shuffle=shuffle,
-> 1415 initial_epoch=initial_epoch)
1416
1417 #interfaces.legacy_generator_methods_support
~/virtualenv/lib/python3.6/site-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
175 batch_index = 0
176 while steps_done < steps_per_epoch:
--> 177 generator_output = next(output_generator)
178
179 if not hasattr(generator_output, '__len__'):
~/virtualenv/lib/python3.6/site-packages/keras/utils/data_utils.py in get(self)
791 success, value = self.queue.get()
792 if not success:
--> 793 six.reraise(value.__class__, value, value.__traceback__)
~/virtualenv/lib/python3.6/site-packages/six.py in reraise(tp, value, tb)
691 if value.__traceback__ is not tb:
692 raise value.with_traceback(tb)
--> 693 raise value
694 finally:
695 value = None
~/virtualenv/lib/python3.6/site-packages/keras/utils/data_utils.py in _data_generator_task(self)
656 # => Serialize calls to
657 # infinite iterator/generator's next() function
--> 658 generator_output = next(self._generator)
659 self.queue.put((True, generator_output))
660 else:
<ipython-input-41-d4282548b872> in trainGenerator(batch_size, train_path, image_folder, mask_folder, aug_dict, image_color_mode, mask_color_mode, image_save_prefix, mask_save_prefix, flag_multi_class, num_class, save_to_dir, target_size, seed)
13 save_to_dir = save_to_dir,
14 save_prefix = image_save_prefix,
---> 15 seed = seed)
16 mask_generator = mask_datagen.flow_from_directory(
17 train_path,
~/virtualenv/lib/python3.6/site-packages/keras_preprocessing/image.py in flow_from_directory(self, directory, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation)
962 follow_links=follow_links,
963 subset=subset,
--> 964 interpolation=interpolation)
965
966 def standardize(self, x):
~/virtualenv/lib/python3.6/site-packages/keras_preprocessing/image.py in __init__(self, directory, image_data_generator, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation)
1731 self.samples = sum(pool.map(function_partial,
1732 (os.path.join(directory, subdir)
-> 1733 for subdir in classes)))
1734
1735 print('Found %d images belonging to %d classes.' %
/usr/lib/python3.6/multiprocessing/pool.py in map(self, func, iterable, chunksize)
264 in a list that is returned.
265 '''
--> 266 return self._map_async(func, iterable, mapstar, chunksize).get()
267
268 def starmap(self, func, iterable, chunksize=None):
/usr/lib/python3.6/multiprocessing/pool.py in _map_async(self, func, iterable, mapper, chunksize, callback, error_callback)
374 raise ValueError("Pool not running")
375 if not hasattr(iterable, '__len__'):
--> 376 iterable = list(iterable)
377
378 if chunksize is None:
~/virtualenv/lib/python3.6/site-packages/keras_preprocessing/image.py in <genexpr>(.0)
1731 self.samples = sum(pool.map(function_partial,
1732 (os.path.join(directory, subdir)
-> 1733 for subdir in classes)))
1734
1735 print('Found %d images belonging to %d classes.' %
/usr/lib/python3.6/posixpath.py in join(a, *p)
78 will be discarded. An empty last part will result in a path that
79 ends with a separator."""
---> 80 a = os.fspath(a)
81 sep = _get_sep(a)
82 path = a
TypeError: expected str, bytes or os.PathLike object, not Button
When I run segmentation_train(trainpath,modelname) without the button implementation, it works fine. How can I call the function by pressing the button?
Thanks in advance
Your lambda is bound to the Button class it was passed into, which implicitly made the first parameter the Button object itself. The result was that the trainpath parameter, was actually a renamed btn instance of Button. The functions that were trying to use trainpath as a filepath string were confused and so threw the error.
If you want to keep the lambda, simply add self as the first parameter, and then ignore it:
btn.on_click(lambda self, trainfolder=trainfolder, modelname=modelname : segmentation_training(trainfolder,modelname))
Otherwise, there is another suggested implementation using functools and calling a function with explicit parameters:
import functools
def click_func(trainfolder,modelname):
segmentation_training(trainfolder,modelname)
btn.on_click(functools.partial(click_func,trainfolder=trainfolder,modelname=modelname))

Writing your custom function for image preprocessing in Keras

I am a beginner training an image dataset on diabetic retinopathy, using the keras_flow_from_dataframe class. But my model has been underfitting. So I tried preprocessing, by writing a custom preprocessing function to be passed in my image data generator class, using OpenCV's adaptive thresholding implementation. The function works very well when I use it outside of Keras, but when I add it to my image data generator class and fit my model, it returns a type error saying bad argument type for built-in operation before my first epoch starts.
Here's the preprocessing code:
def preprocess(im):
im = cv2.imread(im, 1)
im= cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
im=cv2.resize(im, (300,300))
im.resize(300, 300, 1)
block_size = 73
constant = 2
# ADAPTIVE GAUSSIAN THRESHOLDING
thr2 = cv2.adaptiveThreshold(im, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, block_size, constant)
return thr2
It runs well outside of Keras when I test it with the images in my dataframe, but when I add it to my image data generator class, it throws an error.
train_datagen = ImageDataGenerator(
rotation_range=30,
width_shift_range=0.4,
height_shift_range=0.4,
shear_range=0.3,
zoom_range=0.3,
horizontal_flip = True,
fill_mode='nearest',
preprocessing_function = preprocess)
valid_datagen = ImageDataGenerator(preprocessing_function = preprocess)
Then I load in my dataset from dataframe:
from keras.preprocessing.image import ImageDataGenerator
traingen = train_datagen.flow_from_dataframe(x_train, x_col='path', y_col='level',class_mode='other',
target_size=(300,300), color_mode='grayscale', batch_size=16)
validgen = valid_datagen.flow_from_dataframe(valid, x_col='path', y_col='level',class_mode='other',
target_size=(300,300), color_mode='grayscale', batch_size=16)
Then I fit the model using model.fit_generator, which then throws me the type error: bad argument type for built-in operation.
TypeError Traceback (most recent call last)
<ipython-input-126-30ceb84a2574> in <module>()
2
3 history = model.fit_generator(traingen, validation_data = validgen, epochs=100, steps_per_epoch=10,
----> 4 validation_steps=10, verbose=1, callbacks=[lr_reduction])
5
6
~/var/python/lib/python3.6/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~/var/python/lib/python3.6/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1416 use_multiprocessing=use_multiprocessing,
1417 shuffle=shuffle,
-> 1418 initial_epoch=initial_epoch)
1419
1420 #interfaces.legacy_generator_methods_support
~/var/python/lib/python3.6/site-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
179 batch_index = 0
180 while steps_done < steps_per_epoch:
--> 181 generator_output = next(output_generator)
182
183 if not hasattr(generator_output, '__len__'):
~/var/python/lib/python3.6/site-packages/keras/utils/data_utils.py in get(self)
599 except Exception as e:
600 self.stop()
--> 601 six.reraise(*sys.exc_info())
602
603
~/var/python/lib/python3.6/site-packages/six.py in reraise(tp, value, tb)
691 if value.__traceback__ is not tb:
692 raise value.with_traceback(tb)
--> 693 raise value
694 finally:
695 value = None
~/var/python/lib/python3.6/site-packages/keras/utils/data_utils.py in get(self)
593 try:
594 while self.is_running():
--> 595 inputs = self.queue.get(block=True).get()
596 self.queue.task_done()
597 if inputs is not None:
~/var/python/lib/python3.6/multiprocessing/pool.py in get(self, timeout)
642 return self._value
643 else:
--> 644 raise self._value
645
646 def _set(self, i, obj):
~/var/python/lib/python3.6/multiprocessing/pool.py in worker(inqueue, outqueue, initializer, initargs, maxtasks, wrap_exception)
117 job, i, func, args, kwds = task
118 try:
--> 119 result = (True, func(*args, **kwds))
120 except Exception as e:
121 if wrap_exception and func is not _helper_reraises_exception:
~/var/python/lib/python3.6/site-packages/keras/utils/data_utils.py in get_index(uid, i)
399 The value at index `i`.
400 """
--> 401 return _SHARED_SEQUENCES[uid][i]
402
403
~/var/python/lib/python3.6/site-packages/keras_preprocessing/image/iterator.py in __getitem__(self, idx)
63 index_array = self.index_array[self.batch_size * idx:
64 self.batch_size * (idx + 1)]
---> 65 return self._get_batches_of_transformed_samples(index_array)
66
67 def __len__(self):
~/var/python/lib/python3.6/site-packages/keras_preprocessing/image/iterator.py in _get_batches_of_transformed_samples(self, index_array)
233 params = self.image_data_generator.get_random_transform(x.shape)
234 x = self.image_data_generator.apply_transform(x, params)
--> 235 x = self.image_data_generator.standardize(x)
236 batch_x[i] = x
237 # optionally save augmented images to disk for debugging purposes
~/var/python/lib/python3.6/site-packages/keras_preprocessing/image/image_data_generator.py in standardize(self, x)
695 """
696 if self.preprocessing_function:
--> 697 x = self.preprocessing_function(x)
698 if self.rescale:
699 x *= self.rescale
<ipython-input-112-7bddefa5e731> in preprocess(im)
1 def preprocess(im):
----> 2 im = cv2.imread(im, 1)
3 im= cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
4 im=cv2.resize(im, (300,300))
5 im.resize(300, 300, 1)
TypeError: bad argument type for built-in operation
TypeError Traceback (most recent call last)
<ipython-input-126-30ceb84a2574> in <module>()
2
3 history = model.fit_generator(traingen, validation_data = validgen, epochs=100, steps_per_epoch=10,
----> 4 validation_steps=10, verbose=1, callbacks=[lr_reduction])
5
6
~/var/python/lib/python3.6/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~/var/python/lib/python3.6/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1416 use_multiprocessing=use_multiprocessing,
1417 shuffle=shuffle,
-> 1418 initial_epoch=initial_epoch)
1419
1420 #interfaces.legacy_generator_methods_support
~/var/python/lib/python3.6/site-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
179 batch_index = 0
180 while steps_done < steps_per_epoch:
--> 181 generator_output = next(output_generator)
182
183 if not hasattr(generator_output, '__len__'):
~/var/python/lib/python3.6/site-packages/keras/utils/data_utils.py in get(self)
599 except Exception as e:
600 self.stop()
--> 601 six.reraise(*sys.exc_info())
602
603
~/var/python/lib/python3.6/site-packages/six.py in reraise(tp, value, tb)
691 if value.__traceback__ is not tb:
692 raise value.with_traceback(tb)
--> 693 raise value
694 finally:
695 value = None
~/var/python/lib/python3.6/site-packages/keras/utils/data_utils.py in get(self)
593 try:
594 while self.is_running():
--> 595 inputs = self.queue.get(block=True).get()
596 self.queue.task_done()
597 if inputs is not None:
~/var/python/lib/python3.6/multiprocessing/pool.py in get(self, timeout)
642 return self._value
643 else:
--> 644 raise self._value
645
646 def _set(self, i, obj):
~/var/python/lib/python3.6/multiprocessing/pool.py in worker(inqueue, outqueue, initializer, initargs, maxtasks, wrap_exception)
117 job, i, func, args, kwds = task
118 try:
--> 119 result = (True, func(*args, **kwds))
120 except Exception as e:
121 if wrap_exception and func is not _helper_reraises_exception:
~/var/python/lib/python3.6/site-packages/keras/utils/data_utils.py in get_index(uid, i)
399 The value at index `i`.
400 """
--> 401 return _SHARED_SEQUENCES[uid][i]
402
403
~/var/python/lib/python3.6/site-packages/keras_preprocessing/image/iterator.py in __getitem__(self, idx)
63 index_array = self.index_array[self.batch_size * idx:
64 self.batch_size * (idx + 1)]
---> 65 return self._get_batches_of_transformed_samples(index_array)
66
67 def __len__(self):
~/var/python/lib/python3.6/site-packages/keras_preprocessing/image/iterator.py in _get_batches_of_transformed_samples(self, index_array)
233 params = self.image_data_generator.get_random_transform(x.shape)
234 x = self.image_data_generator.apply_transform(x, params)
--> 235 x = self.image_data_generator.standardize(x)
236 batch_x[i] = x
237 # optionally save augmented images to disk for debugging purposes
~/var/python/lib/python3.6/site-packages/keras_preprocessing/image/image_data_generator.py in standardize(self, x)
695 """
696 if self.preprocessing_function:
--> 697 x = self.preprocessing_function(x)
698 if self.rescale:
699 x *= self.rescale
<ipython-input-112-7bddefa5e731> in preprocess(im)
1 def preprocess(im):
----> 2 im = cv2.imread(im, 1)
3 im= cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
4 im=cv2.resize(im, (300,300))
5 im.resize(300, 300, 1)
TypeError: bad argument type for built-in operation
TypeError Traceback (most recent call last)
<ipython-input-126-30ceb84a2574> in <module>()
2
3 history = model.fit_generator(traingen, validation_data = validgen, epochs=100, steps_per_epoch=10,
----> 4 validation_steps=10, verbose=1, callbacks=[lr_reduction])
5
6
~/var/python/lib/python3.6/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~/var/python/lib/python3.6/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1416 use_multiprocessing=use_multiprocessing,
1417 shuffle=shuffle,
-> 1418 initial_epoch=initial_epoch)
1419
1420 #interfaces.legacy_generator_methods_support
~/var/python/lib/python3.6/site-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
179 batch_index = 0
180 while steps_done < steps_per_epoch:
--> 181 generator_output = next(output_generator)
182
183 if not hasattr(generator_output, '__len__'):
~/var/python/lib/python3.6/site-packages/keras/utils/data_utils.py in get(self)
599 except Exception as e:
600 self.stop()
--> 601 six.reraise(*sys.exc_info())
602
603
~/var/python/lib/python3.6/site-packages/six.py in reraise(tp, value, tb)
691 if value.__traceback__ is not tb:
692 raise value.with_traceback(tb)
--> 693 raise value
694 finally:
695 value = None
~/var/python/lib/python3.6/site-packages/keras/utils/data_utils.py in get(self)
593 try:
594 while self.is_running():
--> 595 inputs = self.queue.get(block=True).get()
596 self.queue.task_done()
597 if inputs is not None:
~/var/python/lib/python3.6/multiprocessing/pool.py in get(self, timeout)
642 return self._value
643 else:
--> 644 raise self._value
645
646 def _set(self, i, obj):
~/var/python/lib/python3.6/multiprocessing/pool.py in worker(inqueue, outqueue, initializer, initargs, maxtasks, wrap_exception)
117 job, i, func, args, kwds = task
118 try:
--> 119 result = (True, func(*args, **kwds))
120 except Exception as e:
121 if wrap_exception and func is not _helper_reraises_exception:
~/var/python/lib/python3.6/site-packages/keras/utils/data_utils.py in get_index(uid, i)
399 The value at index `i`.
400 """
--> 401 return _SHARED_SEQUENCES[uid][i]
402
403
~/var/python/lib/python3.6/site-packages/keras_preprocessing/image/iterator.py in __getitem__(self, idx)
63 index_array = self.index_array[self.batch_size * idx:
64 self.batch_size * (idx + 1)]
---> 65 return self._get_batches_of_transformed_samples(index_array)
66
67 def __len__(self):
~/var/python/lib/python3.6/site-packages/keras_preprocessing/image/iterator.py in _get_batches_of_transformed_samples(self, index_array)
233 params = self.image_data_generator.get_random_transform(x.shape)
234 x = self.image_data_generator.apply_transform(x, params)
--> 235 x = self.image_data_generator.standardize(x)
236 batch_x[i] = x
237 # optionally save augmented images to disk for debugging purposes
~/var/python/lib/python3.6/site-packages/keras_preprocessing/image/image_data_generator.py in standardize(self, x)
695 """
696 if self.preprocessing_function:
--> 697 x = self.preprocessing_function(x)
698 if self.rescale:
699 x *= self.rescale
<ipython-input-112-7bddefa5e731> in preprocess(im)
1 def preprocess(im):
----> 2 im = cv2.imread(im, 1)
3 im= cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
4 im=cv2.resize(im, (300,300))
5 im.resize(300, 300, 1)
TypeError: bad argument type for built-in operation
I have also thought about preprocessing the images and saving them to a folder then I'll load them from that folder to a dataframe, but it's computationally expensive and time consuming.
I ran into a problem like yours and my teacher helped me by pointing it to the docs of the tf preprocess_function, it said the preprocess_function argument is an image, you can read more on this.
That is why it gives you the error at cv2.imread(image). You should remove that line because im is an image that the generator gives you. There no need to load it because it's already loaded
My one work well, hope yours will be fine too.
I think the issue was with Keras parsing the openCV output, Because it ran well when i used another library to perform the processing called ImgAUg. Here's the link. https://pypi.org/project/imgaug/.
So i just scripted a preprocessing function with the library, and then i passed it into the keras imageDataGenerator class. It ran fine and didn't throw any error at me.

Categories

Resources