Пример #1
0
def main():

    # Parse arguments.
    parser = argparse.ArgumentParser()
    kwargs = {
        'type': str,
        'help': 'The model file path.',
        'required': True
    }
    parser.add_argument('-m', '--model', **kwargs)
    kwargs = {
        'type': int,
        'default': 10,
        'help': 'The number of samples to evaluate. default: 10'
    }
    parser.add_argument('-n', '--num', **kwargs)
    kwargs = {
        'type': str,
        'default': 'val',
        'help': 'Type of dataset to use. "val" or "test". default: "val"'
    }
    parser.add_argument('-t', '--type', **kwargs)
    args = parser.parse_args()

    # Prepare training data.
    dataset = np.load('./temp/dataset.npz')
    val_x = dataset[f'{args.type}_x']
    val_y = dataset[f'{args.type}_y']

    if args.num < 0 or len(val_x) < args.num:
        args.num = len(val_x)

    # Prepare tensorflow.
    config = tf.ConfigProto(device_count={'GPU': 0})
    session = tf.Session(config=config)
    keras.backend.tensorflow_backend.set_session(session)

    # Prepare model.
    model = SegNet(shape=(360, 480, 3))
    model.load_weights(args.model)

    # Output results.
    head, tail = os.path.split(args.model)
    filename, ext = os.path.splitext(tail)
    os.makedirs(f'{head}/{filename}/', exist_ok=True)

    for i, x, y, t in zip(range(args.num), val_x, model.predict(val_x[:args.num]), val_y):
        cv2.imwrite(f'{head}/{filename}/{args.type}-{i}-input.png', x * 255)
        cv2.imwrite(f'{head}/{filename}/{args.type}-{i}-prediction.png', y * 255)
        cv2.imwrite(f'{head}/{filename}/{args.type}-{i}-teacher.png', t * 255)
Пример #2
0
x_test = np.delete(x_test, len(x_test) - 1, axis=0)

y_train = sc.fit_transform(np.asarray(controls))

# Prepare model for training
model = SegNet((IMG_H, IMG_W))

model.compile(optimizer='adam', loss='mean_squared_error')

model.fit(x_train, y_train, epochs=EPOCHS, batch_size=BATCH_SIZE)

if not os.path.exists('models'):
    os.mkdir('models')

model.save_weights('models/res.h5', save_format='h5')

# Predict on testing dataset
predictions = model.predict(x_test)
predictions = sc.inverse_transform(predictions)
print(predictions)

# Show results and compare
plt.plot(testControls, color='blue', label=f'Real steering')
plt.plot(predictions, color='red', label=f'Predicted steering')
plt.title(f"Steering Angle Prediction")
plt.xlabel('Frame')
plt.ylabel('Steering Angle')
plt.legend()

plt.show()