Python 在Keras中编写用于图像预处理的自定义函数
我是一个初学者,使用来自数据帧的keras\u flow\u课程,培训关于糖尿病视网膜病变的图像数据集。但我的模特一直不合身。所以我尝试了预处理,通过编写一个自定义的预处理函数,在我的图像数据生成器类中传递,使用OpenCV的自适应阈值实现。当我在Keras之外使用该函数时,它工作得非常好,但当我将其添加到图像数据生成器类并拟合模型时,它会在第一个历元开始之前返回一个类型错误,即Python 在Keras中编写用于图像预处理的自定义函数,python,opencv,keras,typeerror,image-preprocessing,Python,Opencv,Keras,Typeerror,Image Preprocessing,我是一个初学者,使用来自数据帧的keras\u flow\u课程,培训关于糖尿病视网膜病变的图像数据集。但我的模特一直不合身。所以我尝试了预处理,通过编写一个自定义的预处理函数,在我的图像数据生成器类中传递,使用OpenCV的自适应阈值实现。当我在Keras之外使用该函数时,它工作得非常好,但当我将其添加到图像数据生成器类并拟合模型时,它会在第一个历元开始之前返回一个类型错误,即内置操作的参数类型错误 以下是预处理代码: def preprocess(im): im = cv2.imr
内置操作的参数类型错误
以下是预处理代码:
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
当我用数据帧中的图像测试它时,它在Keras之外运行良好,但当我将它添加到图像数据生成器类时,它抛出了一个错误
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)
然后,我从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)
然后,我使用model.fit_generator
拟合模型,这会抛出类型错误:内置操作的参数类型不正确
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
TypeError回溯(最近一次调用)
在()
2.
3历史记录=模型拟合发生器(列车发电机,验证数据=有效数据,历元数=100,每历元数=10步,
---->4个验证步骤=10,详细=1,回调=[lr\U缩减])
5.
6.
包装器中的~/var/python/lib/python3.6/site-packages/keras/legacy/interfaces.py(*args,**kwargs)
89 warnings.warn('Update your`'+object\u name+'`调用+
90'Keras 2 API:'+签名,堆栈级别=2)
--->91返回函数(*args,**kwargs)
92包装器._原始函数=func
93返回包装器
~/var/python/lib/python3.6/site-packages/keras/engine/training.py in-fit\u生成器(self、生成器、每个历元的步骤、历元、冗余、回调、验证数据、验证步骤、类权重、最大队列大小、工作者、使用多处理、无序、初始历元)
1416使用多处理=使用多处理,
1417洗牌=洗牌,
->1418初始_历元=初始_历元)
1419
1420@interfaces.legacy\u生成器\u方法\u支持
~/var/python/lib/python3.6/site-packages/keras/engine/training\u generator.py in-fit\u generator(模型、生成器、每个历元的步骤、历元、详细信息、回调、验证数据、验证步骤、类权重、最大队列大小、工作者、使用多处理、无序、初始历元)
179批次指数=0
180当步骤完成时<每个步骤:
-->181发电机输出=下一个(发电机输出)
182
183如果不是HASTATR(发电机输出,'''.'透镜''.'':
get(self)中的~/var/python/lib/python3.6/site-packages/keras/utils/data\u utils.py
599例外情况除外,如e:
600自动停止()
-->601六.重新播放(*sys.exc_info())
602
603
reraise中的~/var/python/lib/python3.6/site-packages/six.py(tp,value,tb)
691如果值.\uuuu回溯\uuuuu不是tb:
692通过回溯(tb)提升值
-->693提高价值
694最后:
695值=无
get(self)中的~/var/python/lib/python3.6/site-packages/keras/utils/data\u utils.py
593尝试:
594当self.u正在运行()时:
-->595 inputs=self.queue.get(block=True).get()
596 self.queue.task_done()
597如果输入不是无:
get中的~/var/python/lib/python3.6/multiprocessing/pool.py(self,timeout)
642返回自身值
643其他:
-->644提高自我价值
645
646 def_设置(自、i、obj):
工人中的~/var/python/lib/python3.6/multiprocessing/pool.py(inqueue、outqueue、initializer、initargs、maxtasks、wrap_异常)
117作业,i,func,args,kwds=任务
118尝试:
-->119结果=(真,函数(*args,**kwds))
120例外情况除外,如e:
121如果换行异常和func不是(帮助程序)重新出现(异常):
get\u索引中的~/var/python/lib/python3.6/site-packages/keras/utils/data\u utils.py(uid,i)
399索引'i'处的值。
400 """
-->401返回共享序列[uid][i]
402
403
~/var/python/lib/python3.6/site-packages/keras\u preprocessing/image/iterator.py in\uuuuuu getitem\uuuuuu(self,idx)
63索引数组=self.index数组[self.batch\u size*idx:
64自我批处理大小*(idx+1)]
--->65返回自身。获取转换样本的批次(索引数组)
66
67 def____________(自):
~/var/python/lib/python3.6/site-packages/keras\u preprocessing/image/iterator.py in\u get\u batches\u of\u transformed\u样本(self,index\u数组)
233 params=self.image\u data\u generator.get\u random\u transform(x.shape)
234 x=自身。图像数据发生器。应用变换(x,参数)
-->235 x=自映像数据发生器标准化(x)
236批次_x[i]=x
237#可选择将增强映像保存到磁盘以进行调试
标准化(self,x)中的~/var/python/lib/python3.6/site-packages/keras\u preprocessing/image/image\u data\u generator.py
695 """
696如果self.preprocessing_函数:
-->697 x=自预处理函数(x)
698如果自重缩放:
699 x*=自重缩放
在预处理中(im)
1 def预处理(im):
---->2 im=cv2.imread(im,1)
3 im=cv2.CVT颜色(im,cv2.COLOR\u BGR2GRAY)
4 im=cv2。调整大小(im,(300300))
5 im.调整大小(300、300、1)
TypeError:内置操作的参数类型错误
TypeError回溯(最近一次调用上次)
在()
2.
3历史记录=模型拟合发生器(列车发电机,验证数据=有效数据,历元数=100,每历元数=10步,
----> 4