我正在尝试了解fit_generator的工作方式,我有问题。
通过“拟合”功能,在测试模型时,我将得到以下输出:
...翻转:原文:-0.027306326664984226-预测: [-0.01349835]
未翻转:原文:0.02869351301342249-预测:[0.01401607]
已翻转:原始:-0.02869351301342249-已预测:[-0.00223063]
不翻转:原文:0.027306326664984226-预测:[0.01300101] ...
但是用fit_generator我有这个:
...
已翻转:原始:-0.01975388266146183-已预测:[0.01830903]
未翻转:原文:0.018212568247690797-预测:[0.01830903]
已翻转:原始:-0.018212568247690797-已预测:[0.01830903]
...
即所有图片上的结果相同(我认为这太适合了)
为什么?
我的代码很合适:
def flip_sequence(self, images, steer):
complete_images = deque()
for image in images:
image = cv2.flip(image, 1)
complete_images.append(image)
if steer != 0.0:
steer = -steer
else:
steer = 0.0
return np.array(complete_images), steer
def EvalModel(self, model):
for i in range(0, 5):
data = np.load("{}/data_{}.npy".format(self._patch, i))
random.shuffle(data)
for image, _, speed_sequence, steer, throttle, brake in data:
image2, steer2 = self.flip_sequence(image, steer)
image = np.array(image).astype('float32')
image = image / 255
image2 = np.array(image2).astype('float32')
image2 = image2 / 255
image = np.expand_dims(image, axis=0)
image2 = np.expand_dims(image2, axis=0)
non_flipped_prediction = model.predict(image)[0][0]
flipped_prediction = model.predict(image2)[0][0]
print("NON-FLIPPED: ")
print(f"ORIGINAL: {steer} - PREDICTED: {non_flipped_prediction}")
print("FLIPPED: ")
print(f"ORIGINAL: {steer2} - PREDICTED: {flipped_prediction}")
def load_data(self, start, end):
Vehicle_steers = deque()
Vehicle_throttles = deque()
Vehicle_brakes = deque()
Radar_images = deque()
Road_images = deque()
for i in range(start, end + 1):
index = random.randint(start, end+1)
data = np.load("{}/data_{}.npy".format(self._patch, index))
random.shuffle(data)
if i % 10 == 0:
print(f"Loading {index} - {i}")
for image, road_image_sequence, speed_sequence, steer, throttle, brake in data:
image = np.array(image).astype('float32')
image = image / 255
Radar_images.append(image)
Vehicle_throttles.append(throttle)
Vehicle_brakes.append(brake)
Vehicle_steers.append(steer)
return np.array(Radar_images), np.array(Vehicle_steers), np.array(Vehicle_brakes), np.array(Vehicle_throttles)
data_loader = DataLoader("some_patch")
RADAR_IMAGES, VEHICLE_STEERS, VEHICLE_BRAKES, VEHICLE_THROTTLES = data_loader.load_data(0, 50)
RADAR_INPUT = keras.layers.Input(shape=(5, 100, 100, 3), name='image_input')
...
...
model.fit(RADAR_IMAGES,
[VEHICLE_STEERS, VEHICLE_BRAKES, VEHICLE_THROTTLES], epochs=5,
callbacks=[checkpoint], batch_size=16, verbose=1, validation_split=0.025)
并使用fit_generator进行编码:
...
def load_data(self, start, end):
Vehicle_steers = deque()
Vehicle_throttles = deque()
Vehicle_brakes = deque()
Radar_images = deque()
Road_images = deque()
while True:
index = random.randint(start, end+1)
data = np.load("{}/data_{}.npy".format(self._patch, index))
for image, road_image_sequence, speed_sequence, steer, throttle, brake in data:
image = np.array(image).astype('float32')
image = image / 255
Radar_images.append(image)
Vehicle_throttles.append(throttle)
Vehicle_brakes.append(brake)
Vehicle_steers.append(steer)
if np.array(Radar_images).shape[0] >= 16:
yield [np.array(Radar_images)], [np.array(Vehicle_steers), np.array(Vehicle_brakes), np.array(Vehicle_throttles)]
Vehicle_steers.clear()
Vehicle_throttles.clear()
Vehicle_brakes.clear()
Radar_images.clear()
Road_images.clear()
...
model.fit_generator(data_loader.load_data(0, 50),
steps_per_epoch=5000//16, epochs=5, verbose=1)