def main(): global args args = parser.parse_args() use_gpu = torch.cuda.is_available() # Load and process data print('Loading dataset') time_data = time.time() POS, NEG, train_iter_pos, train_iter_neg, val_iter_pos, val_iter_neg = preprocess(args.v, args.b) print('Loaded data. |POS| = {}, |NEG| = {}. Time: {:.2f}.'.format(len(POS.vocab), len(NEG.vocab), time.time() - time_data)) # Load embeddings if available LOAD_EMBEDDINGS = False if LOAD_EMBEDDINGS: np_emb_e1_file = 'scripts/emb-{}-de.npy'.format(len(POS.vocab)) np_emb_e2_file = 'scripts/emb-{}-de.npy'.format(len(NEG.vocab)) np_emb_d1_file = 'scripts/emb-{}-de.npy'.format(len(POS.vocab)) np_emb_d2_file = 'scripts/emb-{}-de.npy'.format(len(NEG.vocab)) embedding_e1, epmbedding_e2, embedding_d1, embedding_d2 = load_embeddings(np_emb_e1_file, np_emb_e2_file, np_emb_d1_file, np_emb_d2_file) print('Loaded embedding vectors from np files') else: embedding_e1 = (torch.rand(len(POS.vocab), args.emb) - 0.5) * 2 embedding_e2 = (torch.rand(len(NEG.vocab), args.emb) - 0.5) * 2 embedding_d1 = (torch.rand(len(POS.vocab), args.emb) - 0.5) * 2 embedding_d2 = (torch.rand(len(NEG.vocab), args.emb) - 0.5) * 2 print('Initialized embedding vectors') # Create model tokens = [NEG.vocab.stoi[x] for x in ['<s>', '</s>', '<pad>', '<unk>']]
def telemetry(sid, data): # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) # frames incoming from the simulator are in RGB format image_array = cv2.cvtColor(np.asarray(image), code=cv2.COLOR_RGB2BGR) # perform preprocessing (crop, resize etc.) image_array = preprocess(frame_bgr=image_array) # add singleton batch dimension image_array = np.expand_dims(image_array, axis=0) # This model currently assumes that the features of the model are just the images. Feel free to change this. steering_angle = float(model.predict(image_array, batch_size=1)) # The driving model currently just outputs a constant throttle. Feel free to edit this. throttle = 0.28 print(steering_angle, throttle) send_control(steering_angle, throttle)
def telemetry(sid, data): if data: # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) image_array = np.asarray(image) # training images are loaded in BGR colorspace using cv2 while drive.py load images in RGB to predict the steering angles. # RGB -> BGR # cropping image_array = cv2.cvtColor(image_array, cv2.COLOR_RGB2BGR) image_array = preprocess(frame_bgr=image_array, verbose=False) steering_angle = float(model.predict(image_array[None, :, :, :], batch_size=1)) throttle = controller.update(float(speed)) print(steering_angle, throttle) send_control(steering_angle, throttle) # save frame if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
def telemetry(sid, data): if data: sio.emit('telemetry', data) # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) # frames incoming from the simulator are in RGB format image_array = cv2.cvtColor(np.asarray(image), code=cv2.COLOR_RGB2BGR) # perform preprocessing (crop, resize etc.) image_array = preprocess(frame_bgr=image_array) # add singleton batch dimension image_array = np.expand_dims(image_array, axis=0) # This model currently assumes that the features of the model are just the images. Feel free to change this. steering_angle = float(model.predict(image_array, batch_size=1)) # The driving model currently just outputs a constant throttle. Feel free to edit this. throttle = 0.2 print(steering_angle, throttle) send_control(steering_angle, throttle) # save frame if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
def telemetry(sid, data): if data: # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) image_array = np.asarray(image) image_array = preprocess(image_array) steering_angle = float( model.predict(image_array[None, :, :, :], batch_size=1)) throttle = controller.update(float(speed)) print(steering_angle, throttle) send_control(steering_angle, throttle) # save frame if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
def generate_model(): answer_dir = '../data/datasets/answer.txt' query_dir = '../data/datasets/query.txt' with open(answer_dir, 'r') as fr1, open(query_dir, 'r') as fr2: sentences = fr1.readlines() querys = fr2.readlines() sentences.extend(querys) sentences = [sen.split() for sen in sentences] for i in range(len(sentences)): for j in range(len(sentences[i])): sentences[i][j] = preprocess(sentences[i][j]) model = Word2Vec(sentences, hs=1, min_count=1, window=5, size=100) model.save('../data/w2v/w2v.txt', )
def telemetry(sid, data): if data: # The current steering angle of the car steering_angle = float(data["steering_angle"]) # The current throttle of the car throttle = float(data["throttle"]) # The current speed of the car speed = float(data["speed"]) # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) # frames incoming from the simulator are in RGB format image_array = cv2.cvtColor(np.asarray(image), code=cv2.COLOR_RGB2BGR) # perform preprocessing (crop, resize etc.) image_array = preprocess(frame_bgr=image_array) # add singleton batch dimension image_array = np.expand_dims(image_array, axis=0) # This model currently assumes that the features of the model are just the images. Feel free to change this. steering_angle = float(model.predict(image_array, batch_size=1)) # lower the throttle as the speed increases # if the speed is above the current speed limit, we are on a downhill. # make sure we slow down first and then go back to the original max speed. global speed_limit if speed > speed_limit: speed_limit = MIN_SPEED # slow down else: speed_limit = MAX_SPEED throttle = 1.0 - steering_angle**2 - (speed / speed_limit)**2 print('{} {} {}'.format(steering_angle, throttle, speed)) # save frame if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) #print(steering_angle, throttle) send_control(steering_angle, throttle) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
""" Created on Wed Sep 20 07:26:13 2017 @author: lxf96 """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import numpy as np import load_data Imgs, labels = load_data.getData() train_Img = load_data.preprocess(Imgs) labels = np.zeros((50000)) for i in range(500): labels[i*100:(i+1)*100]=np.array(range(100)) from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D, Flatten, Reshape, Lambda, concatenate, Activation, BatchNormalization from keras.layers.advanced_activations import LeakyReLU, PReLU from keras.optimizers import SGD, Adadelta, Adagrad,Adam, rmsprop from keras import objectives from keras.callbacks import TensorBoard from keras import backend as K from keras.models import Model from keras.losses import binary_crossentropy from keras.callbacks import EarlyStopping input_img2 = Input(shape=(64,64,1))
# visualize_bias_parameter_effect(train_data) # np.float(train_data[:, 3]) from load_data import preprocess for i in range(len(train_data)): central_frame = cv2.imread(path.join('data', train_data[i][0])) steering = np.float32(train_data[i][3]) if train_data[i][0] == 'IMG/center_2016_12_01_13_34_43_116.jpg'\ or train_data[i][0] == 'IMG/center_2016_12_01_13_46_20_434.jpg'\ or train_data[i][0] == 'IMG/center_2016_12_01_13_39_48_531.jpg': plt.close('all') proc_frame = preprocess(central_frame) plt.imshow( cv2.cvtColor(proc_frame.astype(np.uint8), code=cv2.COLOR_BGR2RGB)) print('{}'.format(train_data[i][0])) plt.title('Steering: {:03f}'.format(steering)) plt.gca().set_axis_off() filename = path.join('.', '{}.png'.format(train_data[i][0])) plt.savefig(filename, facecolor='white', bbox_inches='tight') plt.close('all') plt.imshow(cv2.cvtColor(central_frame, code=cv2.COLOR_BGR2RGB)) print('{}'.format(train_data[i][0])) plt.title('Steering: {:03f}'.format(steering)) plt.gca().set_axis_off() filename = path.join('.', '{}'.format(train_data[i][0]))
# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import numpy as np import tensorflow as tf import load_data Imgs, labels = load_data.getData() trainX = load_data.preprocess(Imgs) learning_rate = 0.1 training_epochs = 100 batch_size = 100 display_step = 1 examples_to_show = 10 X = tf.placeholder("float", [None, 64, 64, 1]) def encoder(img): input_layer = tf.reshape(img, [-1, 64, 64, 1]) # Convolutional Layer #1 conv1 = tf.layers.conv2d(inputs=input_layer, filters=32, kernel_size=[3, 3], padding="same", activation=tf.nn.relu)
print('Frame {:06d} / {:06d}'.format(i, len(driving_data))) # ELU 1 ###################################################################### plt.close('all') # load current color frame central_frame = cv2.imread(os.path.join('data', data_row[0]), cv2.IMREAD_COLOR) gs = gridspec.GridSpec(4, 8) ax = plt.subplot(gs[0, 3:5]) ax.set_axis_off() ax.imshow(cv2.cvtColor(central_frame, cv2.COLOR_BGR2RGB)) # preprocess and add batch dimension central_frame = preprocess(central_frame) central_frame = central_frame[np.newaxis, :, :, :] # z = np.random.rand(1, 31, 98, 24) z = first_ELU.predict(central_frame) z = normalize_in_0_255(z) rows, cols = 3, 8 for r in range(rows): for c in range(cols): ax = plt.subplot(gs[r+1, c]) idx = r*cols + c cur_act = z[0, :, :, idx] cur_act = cv2.resize(cur_act, (NVIDIA_W, NVIDIA_H)) ax.set_axis_off() ax.imshow(cur_act.astype(np.uint8), cmap='gray') plt.tight_layout(pad=0.1, h_pad=-10, w_pad=-1.5)
def main(): global args args = parser.parse_args() use_gpu = torch.cuda.is_available() TEXT, _, _ = preprocess_2(args.v, args.b, False) print(len(TEXT.vocab)) # Load and process data print('Loading dataset') time_data = time.time() POS, NEG, train_iter_pos, train_iter_neg, val_iter_pos, val_iter_neg = preprocess( args.v, args.b) print('Loaded data. |POS| = {}, |NEG| = {}. Time: {:.2f}.'.format( len(POS.vocab), len(NEG.vocab), time.time() - time_data)) embedding_e1 = (torch.rand(len(POS.vocab), args.emb) - 0.5) * 2 embedding_e2 = (torch.rand(len(NEG.vocab), args.emb) - 0.5) * 2 embedding_d1 = (torch.rand(len(POS.vocab), args.emb) - 0.5) * 2 embedding_d2 = (torch.rand(len(NEG.vocab), args.emb) - 0.5) * 2 print('Initialized embedding vectors') # Create model tokens = [NEG.vocab.stoi[x] for x in ['<s>', '</s>', '<pad>', '<unk>']] model_enc = EncoderLSTM_v2(torch.rand(5147, args.emb), args.hs, args.nlayers, dropout_p=args.dp, bidirectional=args.bi) model_enc.load_state_dict( torch.load('encoder_models/encoder_1_300_epoch_60.pt')) model = Denoise_AE_v2(torch.rand(5147, args.emb), embedding_d1, embedding_d2, args.hs, args.nlayers, args.dp, args.bi, args.attn, tokens_bos_eos_pad_unk=tokens, reverse_input=args.reverse_input) model.encoder = model_enc #discrim = Discriminator(args.hs * 2 * args.nlayers if args.bi == True else args.hs * args.nlayers, 1024) # Load pretrained model if args.model is not None and os.path.isfile(args.model): model.load_state_dict(torch.load(args.model)) print('Loaded pretrained model.') model = model.cuda() if use_gpu else model #discrim = discrim.cuda() if use_gpu else discrim # Create weight to mask padding tokens for loss function weight_1 = torch.ones(len(POS.vocab)) weight_1[POS.vocab.stoi['<pad>']] = 0 weight_1 = weight_1.cuda() if use_gpu else weight_1 weight_2 = torch.ones(len(NEG.vocab)) weight_2[NEG.vocab.stoi['<pad>']] = 0 weight_2 = weight_2.cuda() if use_gpu else weight_2 # Create loss function and optimizer recons_pos = nn.CrossEntropyLoss(weight=weight_1) recons_neg = nn.CrossEntropyLoss(weight=weight_2) #d_loss = nn.BCELoss(size_average=False) enc_params = filter(lambda p: p.requires_grad, model.encoder.parameters()) for p in enc_params: p.requires_grad = False mod_optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr) #dis_optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, discrim.parameters()), lr=args.lr) scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(mod_optimizer, 'max', patience=30, factor=0.25, verbose=True, cooldown=6) # scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[10,13,16,19], gamma=0.5) # Create directory for logs, create logger, log hyperparameters path = os.path.join('saves', datetime.datetime.now().strftime("%m-%d-%H-%M-%S")) os.makedirs(path, exist_ok=True) logger = Logger(path) logger.log('COMMAND ' + ' '.join(sys.argv), stdout=False) logger.log( 'ARGS: {}\nOPTIMIZER: {}\nLEARNING RATE: {}\nSCHEDULER: {}\nMODEL: {}\n' .format(args, mod_optimizer, args.lr, vars(scheduler), model), stdout=False) # Train, validate, or predict start_time = time.time() if args.evaluate: validate_2(val_iter, model, criterion, SRC, TRG, logger) else: train_2(train_iter_pos, train_iter_neg, val_iter_pos, val_iter_neg,\ model,recons_pos, recons_neg ,mod_optimizer, scheduler, POS, NEG, TEXT, args.epochs, logger) logger.log('Finished in {}'.format(time.time() - start_time)) return