def sequence_train(cfg, tub_names, model_name, transfer_model, model_type, continuous, aug): ''' use the specified data in tub_names to train an artifical neural network saves the output trained model as model_name trains models which take sequence of images ''' assert(not continuous) print("sequence of images training") if model_type == "rnn": kl = KerasRNN_LSTM(image_w=cfg.IMAGE_W, image_h=cfg.IMAGE_H, image_d=cfg.IMAGE_DEPTH, seq_length=cfg.SEQUENCE_LENGTH, num_outputs=2) elif model_type == "3d": kl = Keras3D_CNN(image_w=cfg.IMAGE_W, image_h=cfg.IMAGE_H, image_d=cfg.IMAGE_DEPTH, seq_length=cfg.SEQUENCE_LENGTH, num_outputs=2) else: raise Exception("unknown model type: %s" % model_type) tubs = gather_tubs(cfg, tub_names) records = [] for tub in tubs: record_paths = glob.glob(os.path.join(tub.path, 'record_*.json')) print("Tub:", tub.path, "has", len(record_paths), 'records') record_paths.sort(key=get_record_index) records += record_paths print('collating records') gen_records = {} for record_path in records: with open(record_path, 'r') as fp: json_data = json.load(fp) basepath = os.path.dirname(record_path) image_filename = json_data["cam/image_array"] image_path = os.path.join(basepath, image_filename) sample = { 'record_path' : record_path, "image_path" : image_path, "json_data" : json_data } sample["tub_path"] = basepath sample["index"] = get_image_index(image_filename) angle = float(json_data['user/angle']) throttle = float(json_data["user/throttle"]) sample['target_output'] = np.array([angle, throttle]) sample['img_data'] = None key = make_key(sample) gen_records[key] = sample print('collating sequences') sequences = [] for k, sample in gen_records.items(): seq = [] for i in range(cfg.SEQUENCE_LENGTH): key = make_next_key(sample, i) if key in gen_records: seq.append(gen_records[key]) else: continue if len(seq) != cfg.SEQUENCE_LENGTH: continue sequences.append(seq) #shuffle and split the data train_data, val_data = train_test_split(sequences, shuffle=True, test_size=(1 - cfg.TRAIN_TEST_SPLIT)) def generator(data, batch_size=cfg.BATCH_SIZE): num_records = len(data) while True: #shuffle again for good measure data = shuffle(data) for offset in range(0, num_records, batch_size): batch_data = data[offset:offset+batch_size] if len(batch_data) != batch_size: break b_inputs_img = [] b_labels = [] for seq in batch_data: inputs_img = [] labels = [] for record in seq: #get image data if we don't already have it if record['img_data'] is None: img_arr = load_scaled_image_arr(record['image_path'], cfg) if img_arr is None: break if aug: img_arr = augment_image(img_arr) if cfg.CACHE_IMAGES: record['img_data'] = img_arr else: img_arr = record['img_data'] inputs_img.append(img_arr) if img_arr is None: continue labels.append(seq[-1]['target_output']) b_inputs_img.append(inputs_img) b_labels.append(labels) X = [np.array(b_inputs_img).reshape(batch_size,\ cfg.SEQUENCE_LENGTH, cfg.IMAGE_H, cfg.IMAGE_W, cfg.IMAGE_DEPTH)] y = np.array(b_labels).reshape(batch_size, 2) yield X, y train_gen = generator(train_data) val_gen = generator(val_data) model_path = os.path.expanduser(model_name) total_records = len(sequences) total_train = len(train_data) total_val = len(val_data) print('train: %d, validation: %d' %(total_train, total_val)) steps_per_epoch = total_train // cfg.BATCH_SIZE print('steps_per_epoch', steps_per_epoch) if steps_per_epoch < 2: raise Exception("Too little data to train. Please record more records.") kl.train(train_gen, val_gen, saved_model_path=model_path, steps=steps_per_epoch, train_split=cfg.TRAIN_TEST_SPLIT, use_early_stop = cfg.USE_EARLY_STOP)
def sequence_train(cfg, tub_names, model_name, transfer_model, model_type, continuous): ''' use the specified data in tub_names to train an artifical neural network saves the output trained model as model_name trains models which take sequence of images ''' import sklearn import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.utils import shuffle from PIL import Image import json assert (not continuous) print(' ============================== ' + model_type) print(" ==========================---- " + str(model_type == 'rnn')) print("sequence of images training") if model_type == "rnn": print(" ========================== here") kl = KerasRNN_LSTM(image_w=cfg.IMAGE_W, image_h=cfg.IMAGE_H, image_d=cfg.IMAGE_DEPTH, seq_length=cfg.SEQUENCE_LENGTH, num_outputs=2) if model_type == "rnn_bin": kl = KerasRNN_Categorical(image_w=cfg.IMAGE_W, image_h=cfg.IMAGE_H, image_d=cfg.IMAGE_DEPTH, seq_length=cfg.SEQUENCE_LENGTH, num_outputs=15) elif model_type == "3d": kl = Keras3D_CNN(image_w=cfg.IMAGE_W, image_h=cfg.IMAGE_H, image_d=cfg.IMAGE_DEPTH, seq_length=cfg.SEQUENCE_LENGTH, num_outputs=2) tubs = gather_tubs(cfg, tub_names) records = [] for tub in tubs: record_paths = glob.glob(os.path.join(tub.path, 'record_*.json')) print("Tub:", tub.path, "has", len(record_paths), 'records') record_paths.sort(key=get_record_index) records += record_paths print('collating records') gen_records = {} for record_path in records: with open(record_path, 'r') as fp: json_data = json.load(fp) basepath = os.path.dirname(record_path) image_filename = json_data["cam/image_array"] image_path = os.path.join(basepath, image_filename) sample = { 'record_path': record_path, "image_path": image_path, "json_data": json_data } sample["tub_path"] = basepath sample["index"] = get_image_index(image_filename) angle = float(json_data['user/angle']) throttle = float(json_data["user/throttle"]) if (model_type == "rnn_bin"): sample['target_output'] = dk.utils.linear_bin(angle) else: sample['target_output'] = np.array([angle, throttle]) sample['img_data'] = None key = make_key(sample) gen_records[key] = sample print('collating sequences') sequences = [] for k, sample in gen_records.items(): seq = [] for i in range(cfg.SEQUENCE_LENGTH): key = make_next_key(sample, i) if key in gen_records: seq.append(gen_records[key]) else: continue if len(seq) != cfg.SEQUENCE_LENGTH: continue sequences.append(seq) #shuffle and split the data train_data, val_data = train_test_split(sequences, shuffle=True, test_size=(1 - cfg.TRAIN_TEST_SPLIT)) def generator(data, batch_size=cfg.BATCH_SIZE): num_records = len(data) while True: #shuffle again for good measure shuffle(data) for offset in range(0, num_records, batch_size): batch_data = data[offset:offset + batch_size] if len(batch_data) != batch_size: break b_inputs_img = [] b_labels = [] for seq in batch_data: inputs_img = [] labels = [] for record in seq: #get image data if we don't already have it if record['img_data'] is None: img_arr = load_scaled_image_arr( record['image_path'], cfg) record['img_data'] = img_arr inputs_img.append(record['img_data']) labels.append(seq[-1]['target_output']) b_inputs_img.append(inputs_img) b_labels.append(labels) X = [np.array(b_inputs_img).reshape(batch_size,\ cfg.SEQUENCE_LENGTH, cfg.IMAGE_H, cfg.IMAGE_W, cfg.IMAGE_DEPTH)] if (model_type == 'rnn_bin'): y = np.array(b_labels).reshape(batch_size, 15) else: y = np.array(b_labels).reshape(batch_size, 2) yield X, y train_gen = generator(train_data) val_gen = generator(val_data) model_path = os.path.expanduser(model_name) total_records = len(sequences) total_train = len(train_data) total_val = len(val_data) print('train: %d, validation: %d' % (total_train, total_val)) steps_per_epoch = total_train // cfg.BATCH_SIZE print('steps_per_epoch', steps_per_epoch) history, save_best = kl.train(train_gen, val_gen, saved_model_path=model_path, steps=steps_per_epoch, train_split=cfg.TRAIN_TEST_SPLIT, use_early_stop=cfg.USE_EARLY_STOP) plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('model loss : %f' % save_best.best) plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig(model_path + '_' + model_type + '_loss_%f.png' % save_best.best)