Python preprocess.preprocess() Examples

The following are 7 code examples of preprocess.preprocess(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module preprocess , or try the search function .
Example #1
Source File: train.py    From DeepRNN with MIT License 6 votes vote down vote up
def main():
    parser = argparse.ArgumentParser(description='Deep BiLSTM with Residual')
    add_arguments(parser)
    args = parser.parse_args()
    print(args)
    hparams = tf.contrib.training.HParams(**vars(args))
    # check GPU device
    utils.print_out("# Devices visible to TensorFlow: %s" % repr(tf.Session().list_devices()))
    #  create dirs
    expr_dir, config_dir, log_dir, data_dir, model_dir, figure_dir, result_dir = create_dirs(hparams)
    # save hyperameter
    check_and_save_hparams(config_dir, hparams)

    stage = 'test'  # preprocess','train_eval', or 'test'
    assert stage in [, 'train_eval', 'test'], 'stage not recognized'
    utils.print_out('stage: %s' % stage)
    # if stage == 'preprocess':
    #     preprocess.preprocess(hparams, data_dir)
        # the data are stored in the data_dir for the training step
    if stage == 'train_eval':
        process.train_eval(hparams, data_dir, model_dir, log_dir)
    if stage == 'test':
        process.infer(hparams, data_dir, model_dir, result_dir) 
Example #2
Source File: utils.py    From MalConv-keras with MIT License 5 votes vote down vote up
def data_generator(data, labels, max_len=200000, batch_size=64, shuffle=True):
    idx = np.arange(len(data))
    if shuffle:
        np.random.shuffle(idx)
    batches = [idx[range(batch_size*i, min(len(data), batch_size*(i+1)))] for i in range(len(data)//batch_size+1)]
    while True:
        for i in batches:
            xx = preprocess(data[i], max_len)[0]
            yy = labels[i]
            yield (xx, yy) 
Example #3
Source File: data_shuffled.py    From DeepPicar-v2 with GNU General Public License v2.0 5 votes vote down vote up
def load_imgs():
    global imgs
    global wheels

    for p in purposes:
        for epoch_id in epochs[p]:
            print ('processing and loading "{}" epoch {} into memory, current num of imgs is {}...'.format(p, epoch_id, len(imgs[p])))

            # vid_path = cm.jn(data_dir, 'epoch{:0>2}_front.mkv'.format(epoch_id))
            vid_path = cm.jn(data_dir, 'out-video-{}.avi'.format(epoch_id))

            assert os.path.isfile(vid_path)

            frame_count = cm.frame_count(vid_path)

            cap = cv2.VideoCapture(vid_path)

            # csv_path = cm.jn(data_dir, 'epoch{:0>2}_steering.csv'.format(epoch_id))
            csv_path = cm.jn(data_dir, 'out-key-{}.csv'.format(epoch_id))
            assert os.path.isfile(csv_path)

            rows = cm.fetch_csv_data(csv_path)
            print ("{}, {}".format(len(rows), frame_count))
            assert frame_count == len(rows)
            yy = [[float(row['wheel'])] for row in rows]

            while True:
                ret, img = cap.read()
                if not ret:
                    break

                img = preprocess.preprocess(img)
                imgs[p].append(img)

            wheels[p].extend(yy)
            assert len(imgs[p]) == len(wheels[p])

            cap.release() 
Example #4
Source File: gen_adversarial.py    From MalConv-keras with MIT License 4 votes vote down vote up
def gen_adv_samples(model, fn_list, pad_percent=0.1, step_size=0.001, thres=0.5):
    
    ###   search for nearest neighbor in embedding space ###
    def emb_search(org, adv, pad_idx, pad_len, neigh):
        out = org.copy()
        for idx in range(pad_idx, pad_idx+pad_len):
            target = adv[idx].reshape(1, -1)
            best_idx = neigh.kneighbors(target, 1, False)[0][0]
            out[0][idx] = best_idx
        return out
    
    
    max_len = int(model.input.shape[1])
    emb_layer = model.layers[1]
    emb_weight = emb_layer.get_weights()[0]
    inp2emb = K.function([model.input]+ [K.learning_phase()], [emb_layer.output]) # [function] Map sequence to embedding 
    
    # Build neighbor searches
    neigh = NearestNeighbors(1)
    neigh.fit(emb_weight)
    
    log = utils.logger()
    adv_samples = []

    for e, fn in enumerate(fn_list):

        ###   run one file at a time due to different padding length, [slow]
        inp, len_list = preprocess([fn], max_len)
        inp_emb = np.squeeze(np.array(inp2emb([inp, False])), 0)

        pad_idx = len_list[0]
        pad_len = max(min(int(len_list[0]*pad_percent), max_len-pad_idx), 0)
        org_score = model.predict(inp)[0][0]    ### origianl score, 0 -> malicious, 1 -> benign
        loss, pred = float('nan'), float('nan')
        
        if pad_len > 0:
            
            if org_score < thres:
                adv_emb, gradient, loss = fgsm(model, inp_emb, pad_idx, pad_len, e, step_size)
                adv = emb_search(inp, adv_emb[0], pad_idx, pad_len, neigh)
                pred = model.predict(adv)[0][0]
                final_adv = adv[0][:pad_idx+pad_len]
                
            else: # use origin file
                final_adv = inp[0][:pad_idx]
        
        
        log.write(fn, org_score, pad_idx, pad_len, loss, pred)
        
        # sequence to bytes
        bin_adv = bytes(list(final_adv))
        adv_samples.append(bin_adv)
        
    return adv_samples, log 
Example #5
Source File: run.py    From DeepPicar-v2 with GNU General Public License v2.0 4 votes vote down vote up
def process_epoch(epoch_id):
    print '---------- processing video for epoch {} ----------'.format(epoch_id)
    vid_path = cm.jn(params.data_dir, 'out-video-{}.avi'.format(epoch_id))
    frame_count = cm.frame_count(vid_path)        
    
    vid_scaled_path = cm.jn(params.data_dir, 'out-video-{}-scaled.avi'.format(epoch_id))
    if not os.path.exists(vid_scaled_path):
        assert os.path.isfile(vid_path)
        os.system("ffmpeg -i " + vid_path + " -vf scale=1280:720 " + vid_scaled_path)
        print("ffmpeg -i " + vid_path + " -vf scale=1280:720 " + vid_scaled_path)
    vid_path = vid_scaled_path
    
    cap = cv2.VideoCapture(vid_path)

    machine_steering = []

    print 'performing inference...'
    time_start = time.time()
    for frame_id in xrange(frame_count):
        ret, img = cap.read()
        assert ret

        prep_start = time.time()
        img = preprocess.preprocess(img)

        pred_start = time.time()
        rad = model.y.eval(feed_dict={model.x: [img], model.keep_prob: 1.0})[0][0]
        deg = rad2deg(rad)
        pred_end   = time.time()

        prep_time = pred_start - prep_start
        pred_time = pred_end - pred_start

        # print 'pred: {} deg. took {} ms'.format(deg, pred_time * 1000)
        # print 'pred: {} deg (rad={})'.format(deg, rad)

        machine_steering.append(deg)

    cap.release()

    fps = frame_count / (time.time() - time_start)
    print ('completed inference, total frames: {}, average fps: {} Hz'.format(frame_count, round(fps, 1)))
    # print "Machine Steering:", machine_steering
    return machine_steering 
Example #6
Source File: data_shuffled.py    From DeepPicar-v2 with GNU General Public License v2.0 4 votes vote down vote up
def load_imgs_v2():
    global imgs
    global wheels

    for epoch_id in epochs['all']:
        print ('processing and loading epoch {} into memorys. train:{}, val:{}'.format(
            epoch_id, len(imgs['train']), len(imgs['val'])))

        # vid_path = cm.jn(data_dir, 'epoch{:0>2}_front.mkv'.format(epoch_id))
        vid_path = cm.jn(data_dir, 'out-video-{}.avi'.format(epoch_id))

        if not os.path.isfile(vid_path):
            continue

        frame_count = cm.frame_count(vid_path)
        cap = cv2.VideoCapture(vid_path)

        # csv_path = cm.jn(data_dir, 'epoch{:0>2}_steering.csv'.format(epoch_id))
        csv_path = cm.jn(data_dir, 'out-key-{}.csv'.format(epoch_id))
        assert os.path.isfile(csv_path)

        rows = cm.fetch_csv_data(csv_path)
        print ("{}, {}".format(len(rows), frame_count))
        assert frame_count == len(rows)

        for row in rows:
            ret, img = cap.read()
            if not ret:
                break

            img = preprocess.preprocess(img)
            angle = float(row['wheel'])

            if random.random() < params.train_pct:
                imgs['train'].append(img)
                wheels['train'].append([angle])
            else:
                imgs['val'].append(img)
                wheels['val'].append([angle])

        cap.release()

    print ('Total data: train:{}, val:{}'.format(len(imgs['train']), len(imgs['val'])))
    
# load all preprocessed training images into memory 
Example #7
Source File: data_ordered.py    From DeepPicar-v2 with GNU General Public License v2.0 4 votes vote down vote up
def load_batch(purpose):
    global current_batch_id
    xx = []
    yy = []

    # fetch the batch definition
    batch_id = current_batch_id[purpose]
    assert batch_id < len(batches[purpose])
    batch = batches[purpose][batch_id]
    epoch_id, frame_start, frame_end = batch['epoch_id'], batch['frame_start'], batch['frame_end']
    assert epoch_id is not None and frame_start is not None and frame_end is not None

    # update the current batch
    current_batch_id[purpose] = (current_batch_id[purpose] + 1) % len(batches[purpose])

    # fetch image and steering data
    vid_path = cm.jn(data_dir, 'epoch{:0>2}_front.mkv'.format(epoch_id))
    assert os.path.isfile(vid_path)
    frame_count = cm.frame_count(vid_path)
    cap = cv2.VideoCapture(vid_path)
    cm.cv2_goto_frame(cap, frame_start)

    csv_path = cm.jn(data_dir, 'epoch{:0>2}_steering.csv'.format(epoch_id))
    assert os.path.isfile(csv_path)
    rows = cm.fetch_csv_data(csv_path)
    assert frame_count == len(rows)
    yy = [[float(row['wheel'])] for row in rows[frame_start:frame_end+1]]

    for frame_id in xrange(frame_start, frame_end+1):
        ret, img = cap.read()
        assert ret

        img = preprocess.preprocess(img)
        
        #cv2.imwrite(os.path.abspath('output/sample_frame.jpg'), img)            

        xx.append(img)

    assert len(xx) == len(yy)

    cap.release()

    return xx, yy