我正在使用Keras进行一些ML,并将此生成器用于数据和标签:
def createBatchGenerator(driving_log,batch_size=32):
batch_images = np.zeros((batch_size, 66, 200, 3))
batch_steering = np.zeros(batch_size)
while 1:
for i in range(batch_size):
x,y = get_preprocessed_row(driving_log)
batch_images[i]=x
batch_steering[i]=y
yield batch_images, batch_steering
当我在本地使用它时运行正常,但是当我在带有GPU的AWS g2.2xlarge上运行它时,我得到了这个错误" ValueError:生成器已经执行"。有人可以帮我解决这个问题吗?
答案 0 :(得分:20)
您需要创建一个generator that can support multi-threading以确保一次由两个线程调用生成器:
import threading
class createBatchGenerator:
def __init__(self, driving_log,batch_size=32):
self.driving_log = driving_log
self.batch_size = batch_size
self.lock = threading.Lock()
def __iter__(self):
return self
def __next__(self):
with self.lock:
batch_images = np.zeros((batch_size, 66, 200, 3))
batch_steering = np.zeros(batch_size)
for i in range(self.batch_size):
x,y = get_preprocessed_row(self.driving_log)
batch_images[i]=x
batch_steering[i]=y
return batch_images, batch_steering