This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def preprocessImage(image, width, height): | |
""" | |
Preprocessing script to convert image into neural net input array | |
:param image: (cv2 RBG image) | |
:param width: (int) | |
:param height: (int) | |
:return: (numpy array) | |
""" | |
image = cv2.resize(image, (width, height), interpolation=cv2.INTER_LINEAR) | |
x = image.flatten() # create a big 1D-array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Extracted from https://github.com/araffin/cpp-arduino-serial/ | |
#include <robust_serial.hpp> | |
// Open Serial Port | |
serial_file.open(serial_filename); | |
// Send the order "MOTOR", i.e. to change the speed of the car | |
write_order(serial_file, MOTOR); | |
// with parameter speed=56 (going forward at 56% of the maximum speed) | |
// The parameter "speed" is encoded as a 8 bits (1 bytes) signed int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From https://github.com/sergionr2/RacingRobot | |
// replicated in https://github.com/araffin/cpp-arduino-serial | |
#ifndef ORDER_H | |
#define ORDER_H | |
// Define the orders that can be sent and received | |
enum Order { | |
HELLO = 0, // Hello order to initiate communication with the Arduino | |
SERVO = 1, // Command the Servomotor (control the direction of the car) | |
MOTOR = 2, // Command the Motor (control the speed of the car) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If we have received data | |
if (Serial.available() > 0) | |
{ | |
// Read the order sent by the computer | |
order_received = Serial.read(); | |
// If the received byte is the character 'a' | |
if (order_received == 'a') | |
// This correspond to the action GO_FORWARD | |
// It will be used later to send speed order to the motor | |
action = GO_FORWARD; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Extracted from https://github.com/araffin/rust-arduino-serial | |
extern crate robust_arduino_serial; | |
use robust_arduino_serial::*; | |
// Open Serial Port | |
let mut port = serial::open(&serial_port).unwrap(); | |
// Please see the original file to have a complete example | |
... | |
// Send the order "MOTOR", i.e. to change the speed of the car | |
// equivalent to write_i8(&mut port, Order::MOTOR as i8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from stable_baselines.common.cmd_util import make_atari_env | |
from stable_baselines.common.policies import CnnPolicy | |
from stable_baselines import PPO2 | |
# There already exists an environment generator | |
# that will make and wrap atari environments correctly | |
env = make_atari_env('DemonAttackNoFrameskip-v4', num_env=8, seed=0) | |
model = PPO2(CnnPolicy, env, verbose=1) | |
model.learn(total_timesteps=10000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import imageio | |
import numpy as np | |
from stable_baselines.common.policies import MlpPolicy | |
from stable_baselines import A2C | |
model = A2C(MlpPolicy, "LunarLander-v2").learn(100000) | |
images = [] | |
obs = model.env.reset() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gym | |
from stable_baselines.common.policies import MlpPolicy | |
from stable_baselines.common.vec_env import DummyVecEnv, VecNormalize | |
from stable_baselines import PPO2 | |
env = DummyVecEnv([lambda: gym.make("Reacher-v2")]) | |
# Automatically normalize the input features | |
env = VecNormalize(env, norm_obs=True, norm_reward=False, | |
clip_obs=10.) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from stable_baselines.common.cmd_util import make_atari_env | |
from stable_baselines.common.policies import CnnPolicy | |
from stable_baselines.common.vec_env import VecFrameStack | |
from stable_baselines import ACER | |
# There already exists an environment generator | |
# that will make and wrap atari environments correctly. | |
# Here we are also multiprocessing training (num_env=4 => 4 processes) | |
env = make_atari_env('PongNoFrameskip-v4', num_env=4, seed=0) | |
# Frame-stacking with 4 frames |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from stable_baselines.common.policies import FeedForwardPolicy | |
from stable_baselines import A2C | |
# Custom MLP policy of three layers of size 128 each | |
class CustomPolicy(FeedForwardPolicy): | |
def __init__(self, *args, **kwargs): | |
super(CustomPolicy, self).__init__(*args, **kwargs, | |
layers=[128, 128, 128], | |
feature_extraction="mlp") |
OlderNewer