예제 #1
0
def compare_error(space_order=4, ncp=None, kernel='OT4', nbpml=40, filename='',
                  tn=1000, compression_params={}, **kwargs):
    grad, wrp, fw_timings, rev_timings = checkpointed_run(space_order, ncp,
                                                          kernel, nbpml,
                                                          filename,
                                                          compression_params,
                                                          tn, **kwargs)
    print(wrp.profiler.summary())

    compression_params['scheme'] = None

    print("*************************")
    print("Starting uncompressed run:")

    grad2, _, _, _ = checkpointed_run(space_order, ncp, kernel, nbpml,
                                      filename, compression_params,
                                      tn, **kwargs)

    # error_field = grad2.data - grad.data

    print("compression enabled norm", np.linalg.norm(grad.data))
    print("compression disabled norm", np.linalg.norm(grad2.data))
    # to_hdf5(error_field, 'zfp_grad_errors_full.h5')
    computed_errors = {}

    for k, v in error_metrics.items():
        computed_errors[k] = v(grad2.data, grad.data)

    data = computed_errors
    data['tolerance'] = compression_params['tolerance']
    data['ncp'] = ncp
    data['tn'] = tn

    write_results(data, 'gradient_error_results.csv')
def main():
    images, labels = load_labeled_training(flatten=True)
    images = standardize(images)
    unl = load_unlabeled_training(flatten=True)
    unl = standardize(unl)
    test = load_public_test(flatten=True)
    test = standardize(test)
    shuffle_in_unison(images, labels)
    #d = DictionaryLearning().fit(images)
    d = MiniBatchDictionaryLearning(n_components=500, n_iter=500, verbose=True).fit(images)
    s = SparseCoder(d.components_)
    proj_test = s.transform(images)
    pt = s.transform(test)
    #kpca = KernelPCA(kernel="rbf")
    #kpca.fit(unl)
    #test_proj = kpca.transform(images)
    #pt = kpca.transform(test)
    #spca = SparsePCA().fit(unl)
    #test_proj = spca.transform(images)
    #pt = spca.transform(test)
    svc = SVC()
    scores = cross_validation.cross_val_score(svc, proj_test, labels, cv=10)
    print scores
    print np.mean(scores)
    print np.var(scores)
    svc.fit(proj_test, labels)
    pred = svc.predict(pt)
    write_results(pred, '../svm_res.csv')
예제 #3
0
def pc_and_sample_returnfun(pc_list, times, samples, runnum):
    util.write_results(resultdir, runnum, 0, "pc", pc_list,
                       list(range(0, len(pc_list))))
    filename = resultdir + str(runnum) + "_sample"
    world.write_list_of_dnas_file(samples, filename)
    filename = resultdir + str(runnum) + "_time"
    f = open(resultdir + str(runnum) + "_time", 'wb')
    pickle.dump(times, f)
    f.close()
def run_forward_error(filename, space_order=4, kernel='OT4', tolerance=0.001,
                      nbpml=10, dtype=np.float64, **kwargs):
    # Setup solver

    solver = overthrust_setup(filename=filename, tn=1000, nbpml=nbpml,
                              space_order=space_order, kernel=kernel,
                              dtype=dtype, **kwargs)

    # Run for nt/2 timesteps as a warm up
    nt = solver.geometry.time_axis.num
    nt_2 = int(floor(nt/2))

    print("first run")
    rec, u, profiler = solver.forward(time=nt_2)
    print("second run")
    _, u2, _ = solver.forward(time=nt_2)

    assert(np.allclose(u.data, u2.data))

    # Store last timestep

    u_comp = TimeFunction(name='u', grid=solver.model.grid, time_order=2,
                          space_order=solver.space_order)
    u_comp.data  # Force memory allocation
    # Compress-decompress with given tolerance

    compressed_u = compress(get_data(u), tolerance=tolerance, parallel=True)

    mem = get_data(u_comp)
    mem[:] = decompress(compressed_u, mem.shape, mem.dtype,
                        tolerance=tolerance)

    for i in range(nt_2):
        # Run for i steps (original last time step and compressed version)
        clear_cache()
        u_copy = TimeFunction(name='u', grid=solver.model.grid, time_order=2,
                              space_order=solver.space_order)
        u_copy.data[:] = u.data
        _, u_original, _ = solver.forward(time_m=nt_2, time_M=nt_2+i, u=u_copy)

        u_l_copy = TimeFunction(name='u', grid=solver.model.grid, time_order=2,
                                space_order=solver.space_order)
        u_l_copy.data[:] = u_comp.data
        _, u_lossy, _ = solver.forward(time_m=nt_2, time_M=nt_2+i, u=u_l_copy)

        # Compare and report error metrics

        data = get_all_errors(get_data(u_original), get_data(u_lossy))
        # error_field = u_original.data[nt_2+i] - u_lossy.data[nt_2+i]
        data['ntimesteps'] = i
        data['atol'] = tolerance
        write_results(data, "forward_prop_results.csv")
def main():
    N_TREE = 1001
    k = 200
    rfc = RandomForestClassifier(n_estimators=N_TREE, criterion="entropy", max_features="auto")
    RandomForestClassifier
    proj_test, labels = load_pca_proj(K=k)
    shuffle_in_unison(proj_test, labels)
    scores = cross_validation.cross_val_score(rfc, proj_test, labels, cv=10)
    pt = load_pca_test(K=k)
    rfc.fit(proj_test, labels)
    pred = rfc.predict(pt)
    write_results(pred, "../rfc_res.csv")
    print scores
    print np.mean(scores)
    print np.var(scores)
예제 #6
0
def main():
    N_TREE = 1001
    k = 200 
    rfc = RandomForestClassifier(n_estimators=N_TREE, criterion='entropy', max_features="auto")
    RandomForestClassifier
    proj_test, labels = load_pca_proj(K=k)
    shuffle_in_unison(proj_test, labels)
    scores = cross_validation.cross_val_score(rfc, proj_test, labels, cv=10)
    pt = load_pca_test(K=k)
    rfc.fit(proj_test, labels)
    pred = rfc.predict(pt)
    write_results(pred, '../rfc_res.csv')
    print scores
    print np.mean(scores)
    print np.var(scores)
예제 #7
0
    def main(self):
        q = queue.Queue()
        while True:

            def frame_render(queue_from_cam):
                frame = self.cap.read(
                )  # If you capture stream using opencv (cv2.VideoCapture()) the use the following line
                # ret, frame = self.cap.read()
                frame = cv2.resize(frame, (self.width, self.height))
                queue_from_cam.put(frame)

            cam = threading.Thread(target=frame_render, args=(q, ))
            cam.start()
            cam.join()
            frame = q.get()
            q.task_done()
            fps = FPS().start()
            try:
                img, orig_im, dim = prep_image(frame, self.inp_dim)
                im_dim = torch.FloatTensor(dim).repeat(1, 2)
                if self.CUDA:  #### If you have a gpu properly installed then it will run on the gpu
                    im_dim = im_dim.cuda()
                    img = img.cuda()
                # with torch.no_grad():               #### Set the model in the evaluation mode
                output = self.model(Variable(img), self.CUDA)
                output = write_results(output,
                                       self.confidence,
                                       self.num_classes,
                                       nms=True,
                                       nms_conf=self.nms_thesh
                                       )  #### Localize the objects in a frame
                output = output.type(torch.half)

                if list(output.size()) == [1, 86]:
                    print(output.size())
                    pass
                else:
                    output[:,
                           1:5] = torch.clamp(output[:, 1:5], 0.0,
                                              float(
                                                  self.inp_dim)) / self.inp_dim

                    #            im_dim = im_dim.repeat(output.size(0), 1)
                    output[:, [1, 3]] *= frame.shape[1]
                    output[:, [2, 4]] *= frame.shape[0]
                    list(
                        map(
                            lambda boxes: write(boxes, frame, self.classes,
                                                self.colors), output))

            except:
                pass

            fps.update()
            fps.stop()
            ret, jpeg = cv2.imencode('.jpg', frame)
            print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
            print("[INFO] approx. FPS: {:.1f}".format(fps.fps()))

            return jpeg.tostring()
예제 #8
0
def save_inference_results_on_disk(loader, network, name):
    config = loader.config
    pack_volume = config['pack_volume']
    path = os.path.join(config['temp_folder'], name, '')
    print('path ', path)
    network.eval()

    all_outputs = torch.cuda.FloatTensor()
    all_ids = []
    all_empty_ids = []
    i = 1
    print('Inference is in progress')
    print('loader ', loader.batch_sampler.sampler)
    for data in tqdm(loader):
        images_tensors, target_vectors, images_ids = data
        outputs = network(Variable(images_tensors).cuda())
        # confidence here is a threshold confidence

        # outputs structure is as follows:
        # it is a tensor of shape total_number_of_predicted_boxes_for_tis_batch x 8
        # each row contains
        # index of the image in the current batch, box coordinates, objectness score, class score, class number
        # example:
        # [2.0000, 210.5068, 90.1572, 227.4409, 129.0678, 0.7430, 0.9925, 56.0000]
        outputs = write_results(outputs,
                                confidence=0.5,
                                num_classes=80,
                                nms=True,
                                nms_conf=0.4)

        if not isinstance(outputs, int):
            images_with_predictions_ids_in_the_current_batch = outputs[:,
                                                                       0].int(
                                                                       )
            images_without_predictions_ids_in_the_current_batch = np.delete(
                np.arange(len(images_ids)),
                images_with_predictions_ids_in_the_current_batch)
            for empty_id in images_without_predictions_ids_in_the_current_batch:
                all_empty_ids.append(images_ids[empty_id])
            all_outputs = torch.cat((all_outputs, outputs.data), dim=0)
            for id in images_with_predictions_ids_in_the_current_batch:
                all_ids.append(images_ids[id])
            if i % pack_volume == 0:
                torch.save(all_outputs, '%sall_outputs_%d' % (path, i))
                all_outputs = torch.cuda.FloatTensor()
                torch.cuda.empty_cache()
        i += 1
    batches_number = len(loader) // pack_volume
    print('batches_number = ', batches_number)
    with open('%sall_ids.pkl' % path, 'wb') as f:
        pickle.dump(all_ids, f)

    with open('%sall_empty_ids.pkl' % path, 'wb') as f:
        pickle.dump(all_empty_ids, f)
    all_outputs = None
    torch.cuda.empty_cache()
    return batches_number
예제 #9
0
파일: submit.py 프로젝트: zqcccc/MLDemo
def predict(model, test_loader):
    model.eval()
    candidate = []
    # val_loss = 0.
    for idx, batch in enumerate(test_loader):
        sources, source_padding_mask, sources_lengths, sources_batch_extend_vocab, \
        extra_zeros, coverage = get_input_from_batch(batch, use_cuda, use_point_gen=config.pointer_gen,
                                                            use_coverage=config.is_coverage, trian=False, test=True)
        output_ids = model.beam_sample(sources, sources_lengths, source_padding_mask, sources_batch_extend_vocab, extra_zeros, coverage, config.beam_size)
        decoder_words = outputids2words(output_ids, vocab, (batch.art_oovs[0] if config.pointer_gen else None))
        # 去除</s>
        try:
            fst_stop_idx = decoder_words.index('</s>')
            decoder_words = decoder_words[:fst_stop_idx]
        except ValueError:
            decoder_words = decoder_words
        candidate.append(decoder_words)
    util.write_results(candidate)
    model.train()
예제 #10
0
def main():
    images, labels = load_labeled_training(flatten=True)
    public_test = load_public_test(flatten=True)
    images = standardize(images)
    #images, labels = load_pca_proj(K=100)
    shuffle_in_unison(images, labels)
    ds = ClassificationDataSet(images.shape[1],1, nb_classes=7)
    testset = ClassificationDataSet(public_test.shape[1])
    public_test=standardize(public_test)
    for i in public_test:
        testset.addSample(i,[0])
    for i,l in zip(images, labels):
        ds.addSample(i,[l-1])
    #ds._convertToOneOfMany()
    test, train = ds.splitWithProportion(0.2)
    test._convertToOneOfMany()
    train._convertToOneOfMany()
    net=shortcuts.buildNetwork(train.indim, 500, 1000,train.outdim, outclass=SoftmaxLayer)

    trainer = BackpropTrainer(net, dataset=train, learningrate=0.005, weightdecay=0.01)
    #trainer = RPropMinusTrainer(net, dataset=train)
    #cv = validation.CrossValidator(trainer, ds)
    #print cv.validate()
    net.randomize()
    tr_labels_2 = net.activateOnDataset(train).argmax(axis=1)
    trnres = percentError(tr_labels_2, train['class'])
    #trnres = percentError(trainer.testOnClassData(dataset=train), train['class'])
    testres = percentError(trainer.testOnClassData(dataset=test), test['class'])
    print "Training error: %.10f, Test error: %.10f" % (trnres, testres)
    print "Iters: %d" % trainer.totalepochs
    for i in range(10):
        trainer.trainEpochs(10)
        trnres = percentError(trainer.testOnClassData(dataset=train), train['class'])
        testres = percentError(trainer.testOnClassData(dataset=test), test['class'])
        trnmse = trainer.testOnData(dataset=train)
        testmse = trainer.testOnData(dataset=test)
        print "Iteration: %d, Training error: %.5f, Test error: %.5f" % (trainer.totalepochs, trnres, testres)
        print "Training MSE: %.5f, Test MSE: %.5f" % (trnmse, testmse)
    out=trainer.testOnClassData(dataset=testset)
    for i in range(len(out)):
        out[i] += 1
    write_results(out, 'nn_predictions.csv')
예제 #11
0
def run(initial_model_filename, results_dir, tn, nshots, shots_container, so, nbl, kernel, scale_gradient, max_iter,
        checkpointing, n_checkpoints, compression, tolerance, reference_solution, dtype):

    if dtype == 'float32':
        dtype = np.float32
    elif dtype == 'float64':
        dtype = np.float64
    else:
        raise ValueError("Invalid dtype")
    shot_id = 20
    water_depth = 20
    initial_model_filename, datakey = initial_model_filename

    rec, source_location, _ = load_shot(shot_id, container=shots_container)
    print("Source", source_location)
    print("rec", np.linalg.norm(rec))
    solver_params = {'h5_file': Blob("models", initial_model_filename), 'tn': tn,
                     'space_order': so, 'dtype': dtype, 'datakey': datakey, 'nbl': nbl,
                     'src_coordinates': source_location, 'opt': ('noop', {'openmp': True, 'par-dynamic-work': 1000})}

    if kernel in ['OT2', 'OT4']:
        solver_params['kernel'] = kernel
        solver = overthrust_solver_iso(**solver_params)
    elif kernel == "rho":
        solver_params['water_depth'] = water_depth
        solver_params['calculate_density'] = False
        solver = overthrust_solver_density(**solver_params)
    if not checkpointing:
        F0, gradient = process_shot(shot_id, solver, shots_container, exclude_boundaries=False)
    else:
        F0, gradient = process_shot_checkpointed(shot_id, solver, shots_container, exclude_boundaries=False,
                                                 checkpoint_params={'n_checkpoints': n_checkpoints, 'scheme': compression,
                                                                    'tolerance': tolerance})

    error1, error2, H = gradient_test_errors(solver, rec, F0, gradient)

    data = dict(zip(H, error2))
    data['compression'] = compression
    data['tolerance'] = tolerance

    write_results(data, "linearization.csv")
def run(tn=4000,
        space_order=4,
        kernel='OT4',
        nbpml=40,
        tolerance=1e-4,
        filename='',
        **kwargs):
    if kernel in ['OT2', 'OT4']:
        solver = overthrust_setup(filename=filename,
                                  tn=tn,
                                  nbpml=nbpml,
                                  space_order=space_order,
                                  kernel=kernel,
                                  **kwargs)
    else:
        raise ValueError()

    total_timesteps = solver.geometry.src.time_range.num
    u = None
    rec = None
    for t in range(1, total_timesteps - 1):
        rec, u, _ = solver.forward(u=u,
                                   rec=rec,
                                   time_m=t,
                                   time_M=t,
                                   save=False)
        uncompressed = u._data[t]
        with Timer(factor=1000) as time1:
            compressed = compress(uncompressed,
                                  tolerance=tolerance,
                                  parallel=True)
        result = {
            'timestep': t,
            'cf': len(uncompressed.tostring()) / float(len(compressed)),
            'time': time1.elapsed
        }
        write_results(result, "cf_vs_nt.csv")

    _, u2, _ = solver.forward(save=False)
    assert (u2.shape == u.shape)
    assert (np.all(np.isclose(u2.data, u.data)))
예제 #13
0
def predict(frame_img):
    result = []
    h, w, _ = frame_img.shape

    # Making it square by padding
    frame_img = cv2.copyMakeBorder(frame_img,
                                   0,
                                   max(0, w - h),
                                   0,
                                   max(0, h - w),
                                   cv2.BORDER_CONSTANT,
                                   value=0)

    frame_img = frame_img.transpose((2, 0, 1))
    tensor = torch.Tensor(frame_img)
    if CUDA:
        tensor = tensor.cuda()
    tensor = tensor.unsqueeze(0)
    with torch.no_grad():
        output = model(tensor, CUDA=CUDA, inp_dim=int(max(h, w)))

    output = write_results(output, 0.2, 80)

    if CUDA:
        output = output.cpu()
    output = output.numpy()

    for out in output:
        cls = int(out[-1])
        if cls == 2 or cls == 7 or cls == 5 or cls == 3 or cls == 1 or cls == 0:
            d_up = {}
            d = {}
            name = 'Car'
            if cls == 7:
                name = 'Truck'
            elif cls == 5:
                name = 'Bus'
            elif cls == 3:
                name = 'Motorcycle'
            elif cls == 1:
                name = 'Bicycle'
            elif cls == 0:
                name = 'Pedestrian'
            d['xmax'] = max(out[3], out[1])
            d['xmin'] = min(out[3], out[1])
            d['ymax'] = max(out[2], out[4])
            d['ymin'] = min(out[2], out[4])
            d['name'] = name
            d['confidence'] = out[-2]
            d_up['bndbox'] = d
            result.append(d_up)
    return result
예제 #14
0
def inference(test_loader, model):
    # switch to evaluate mode
    model.eval()

    results = []
    for i, (input, idx) in enumerate(test_loader):
        if args.ten_crop:
            bs, ncrop, c, h, w = input.size()
            input = input.view(-1, c, h, w)

        input_var = torch.autograd.Variable(input, volatile=True)
        output = model(input_var)

        if args.ten_crop:
            output = output.view(bs, ncrop, -1).mean(1)

        real_output = torch.mul(output,
                                torch.autograd.Variable(idx).float().cuda())

        for j in range(real_output.size()[0]):
            append_value = torch.nn.functional.softmax(
                real_output[j][real_output[j] != 0].float())
            results.append(append_value.data.cpu().numpy())

        if i % args.print_freq == 0:
            print('Inference: [{0}/{1}]\t'
                  'Time {batch_time.val:.3f} ({batch_time.avg:.3f})'.format(
                      i, len(val_loader), batch_time=batch_time))

    # write results to file
    save_path = os.path.join(args.result_path,
                             test_loader.dataset.class_name + ".csv")
    if not os.path.exists(args.result_path):
        os.makedirs(args.result_path)
    write_results(test_loader.dataset.df_load, results, save_path)

    print('Inference done')
예제 #15
0
def test(testname,
         runnum,
         iter_per_sample,
         limit=None,
         thres=None,
         index=None,
         screen_off=True):
    print("doing", runnum, "-", testname)
    filename = resultdir + str(runnum) + "_time"
    f = open(filename, 'rb')
    times = pickle.load(f)
    print("times:", times)
    f.close()
    filename = resultdir + str(runnum) + "_sample"
    samples = world.read_list_of_dnas_file(filename)
    assert len(times) == len(samples)
    n = len(times)
    for iteration in range(iter_per_sample):
        results = []
        for i, (t, dnas) in enumerate(zip(times, samples)):  #for each moment
            w = test_world(testname, dnas)
            dof = lambda w: test_dofun(w, testname)
            endf = lambda w: test_endfun(w, testname, limit=limit, thres=thres)
            returnf = lambda w: test_returnfun(
                w, testname, index=index, ininum=ininum)
            results.append(
                exputil.do(w,
                           test_k if testtype(testname) == 2 else k,
                           dof,
                           endf,
                           returnf,
                           screen_off=screen_off,
                           print_moment=False))
            print("done", runnum, t, "-", iteration)
        util.write_results(resultdir, runnum, iteration, testname, results,
                           times)
    print("done", runnum, "-", testname)
예제 #16
0
def stacking_experiment(filename, path_prefix, compression_params, tn=4000, max_shots=40, to=2, so=4, 
                        results_file="stacking_experiment_results.csv"):
    solver = overthrust_setup(path_prefix+"/"+filename, tn=tn, datakey="m0", nbpml=40)
    model, geometry, b = initial_setup(path_prefix)
    perfect_grad = Function(name="grad", grid=model.grid)
    lossy_grad = Function(name="grad", grid=model.grid)
    vp = model.vp

    atol = compression_params['tolerance']

    for i in range(max_shots):
        # The following calls are by reference and cumulative
        calculate_perfect_gradient(i, solver, vp, perfect_grad, path_prefix, so=so)
        calculate_lossy_gradient(i, solver, vp, lossy_grad, path_prefix,
                                 compression_params=compression_params, so=so)

        computed_errors = {}
        for k, v in error_metrics.items():
            computed_errors[k] = v(perfect_grad.data, lossy_grad.data)

        data = computed_errors
        data['shot'] = i
        data['atol'] = atol
        write_results(data, results_file)
def main():
    k = 500
    #images, labels = load_labeled_training(flatten=True)
    #images = standardize(images)
    #shuffle_in_unison(images, labels)
    #for k in [100,500,1024]:
    #proj_test, labels = load_pca_proj(K=k)
    #shuffle_in_unison(proj_test, labels)
    #for ker in ['linear', 'sigmoid', 'rbf']:
    #svc = SVC(kernel=ker)
    #scores = cross_validation.cross_val_score(svc, proj_test, labels, cv=10)
    #print "Kernel: " + ker
    #print "K: " + str(k)
    #print scores
    #print np.mean(scores)
    #print np.var(scores)
    proj_test, labels = load_pca_proj(K=k)
    shuffle_in_unison(proj_test, labels)
    svc = SVC()
    scores = cross_validation.cross_val_score(svc, proj_test, labels, cv=10)
    pt = load_pca_hidden(K=k)
    svc.fit(proj_test, labels)
    pred = svc.predict(pt)
    write_results(pred, '../svm_res.csv')
def main():
    k=500
      #images, labels = load_labeled_training(flatten=True)
   #images = standardize(images)
   #shuffle_in_unison(images, labels)
   #for k in [100,500,1024]:
      #proj_test, labels = load_pca_proj(K=k)
      #shuffle_in_unison(proj_test, labels)
      #for ker in ['linear', 'sigmoid', 'rbf']:
           #svc = SVC(kernel=ker)
           #scores = cross_validation.cross_val_score(svc, proj_test, labels, cv=10)
           #print "Kernel: " + ker
           #print "K: " + str(k)
           #print scores
           #print np.mean(scores)
           #print np.var(scores)
    proj_test, labels = load_pca_proj(K=k)
    shuffle_in_unison(proj_test, labels)
    svc = SVC()
    scores = cross_validation.cross_val_score(svc, proj_test, labels, cv=10)
    pt = load_pca_hidden(K=k)
    svc.fit(proj_test, labels)
    pred = svc.predict(pt)
    write_results(pred, '../svm_res.csv')
예제 #19
0
    def main(self):
        q = queue.Queue()
        def frame_render(queue_from_cam):
            frame = self.cap.read()
            frame = cv2.resize(frame,(self.width, self.height))
            queue_from_cam.put(frame)
        cam = threading.Thread(target=frame_render, args=(q,))
        cam.start()
        cam.join()
        frame = q.get()
        q.task_done()
        fps = FPS().start() 
        try:
            img, orig_im, dim = prep_image(frame, self.inp_dim)
            im_dim = torch.FloatTensor(dim).repeat(1,2)
            if self.CUDA:                            #### If you have a gpu properly installed then it will run on the gpu
                im_dim = im_dim.cuda()
                img = img.cuda()
            # with torch.no_grad():               #### Set the model in the evaluation mode
            output = self.model(Variable(img), self.CUDA)
            output = write_results(output, self.confidence, self.num_classes, nms = True, nms_conf = self.nms_thesh)  #### Localize the objects in a frame
            output = output.type(torch.half)
            if list(output.size()) == [1,86]:
                pass
            else:
                output[:,1:5] = torch.clamp(output[:,1:5], 0.0, float(self.inp_dim))/self.inp_dim

    #            im_dim = im_dim.repeat(output.size(0), 1)
                output[:,[1,3]] *= frame.shape[1]
                output[:,[2,4]] *= frame.shape[0]
                list(map(lambda x: write(x, frame, self.classes, self.colors),output))
                x,y,w,h = b_boxes["bbox"][0],b_boxes["bbox"][1], b_boxes["bbox"][2], b_boxes["bbox"][3]
                distance = (2 * 3.14 * 180) / (w + h * 360) * 1000 + 3 ### Distance measuring in Inch
                feedback = ("{}".format(labels["Current Object"])+ " " +"is"+" at {} ".format(round(distance))+"Inches")
                # speak.Speak(feedback)     # If you are running this on linux based OS kindly use espeak. Using this speaking library in winodws will add unnecessary latency 
#                 print(feedback)
        except:
            pass
        fps.update()
        fps.stop()
        print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
        print("[INFO] approx. FPS: {:.1f}".format(fps.fps()))
        frame = cv2.putText(frame, str("{:.2f} Inches".format(distance)), (x,y), cv2.FONT_HERSHEY_DUPLEX, 0.6, (0,0,255), 1, cv2.LINE_AA)
        ret, jpeg = cv2.imencode('.jpg', frame)
        return jpeg.tostring()
예제 #20
0
    def detect(self,image, show = False,verbose = False,save_file = None):
        start = time.time()
#        
        try: # image is already loaded
            img, orig_im, dim = self.prep_image(image, self.resolution)
        except: # image is a file path
            image = cv2.imread(image)
            img, orig_im, dim = self.prep_image(image, self.resolution)
            
        im_dim = torch.FloatTensor(dim).repeat(1,2)                        
            
        if self.CUDA:
            im_dim = im_dim.cuda()
            img = img.cuda()
        
        
        output = self.model(Variable(img), self.CUDA)
        output = write_results(output, self.conf, self.num_classes, nms = True, nms_conf = self.nms)
        output[:,1:5] = torch.clamp(output[:,1:5], 0.0, float(self.resolution))/self.resolution
        
        im_dim = im_dim.repeat(output.size(0), 1)
        output[:,[1,3]] *= image.shape[1]
        output[:,[2,4]] *= image.shape[0]

                
        out = list(map(lambda x: self.write(x, orig_im), output))
        
        if verbose:
            print("FPS of the video is {:5.2f}".format( 1.0 / (time.time() - start)))
            
        if save_file != None:
            cv2.imwrite(save_file, orig_im)    
            
        if show:
            cv2.imshow("frame", orig_im)
            cv2.waitKey(0)


            
        
       
        return output, orig_im
예제 #21
0
    def detect2(self, img, dim):
        """
        Description
        -----------
        Takes preprocessed image and outputs result without any additional image
        processing, to perform detection more quickly than detect()
        
        Parameters
        ----------
        img : tensor [3,w,h]
            image, normalized and resized to input dimension
        dim : tensor [3]
            input image dimensions

        Returns
        -------
        output - tensor [n,8] 
            batch_num, x_min,y_min,x_max,y_max,objectness, max_class_conf, 
            max_class_idx for each of n detections

        """

        im_dim = dim  #im_dim = torch.FloatTensor(dim).repeat(1,2)

        if self.CUDA:
            im_dim = im_dim.cuda()

        output = self.model(Variable(img), self.CUDA)
        output = write_results(output,
                               self.conf,
                               self.num_classes,
                               nms=True,
                               nms_conf=self.nms)
        output[:, 1:5] = torch.clamp(output[:, 1:5], 0.0, float(
            self.resolution)) / self.resolution

        im_dim = im_dim.repeat(output.size(0), 1)
        output[:, 1:5] *= im_dim

        return output
 def format_output(self, output: Any, threshold: float,
                   frame_dimensions: Tuple[int, int]) -> Optional[Any]:
     output = write_results(output,
                            threshold,
                            self.num_classes,
                            nms=True,
                            nms_conf=threshold)
     if isinstance(output, int):
         # means no output
         return None
     frame_dimensions = frame_dimensions.repeat(output.size(0), 1)
     scaling_factor = torch.min(self.input_dim / frame_dimensions,
                                1)[0].view(-1, 1)
     output[:, [1, 3]] -= (self.input_dim - scaling_factor *
                           frame_dimensions[:, 0].view(-1, 1)) / 2
     output[:, [2, 4]] -= (self.input_dim - scaling_factor *
                           frame_dimensions[:, 1].view(-1, 1)) / 2
     output[:, 1:5] /= scaling_factor
     for i in range(output.shape[0]):
         output[i, [1, 3]] = torch.clamp(output[i, [1, 3]], 0.0,
                                         frame_dimensions[i, 0])
         output[i, [2, 4]] = torch.clamp(output[i, [2, 4]], 0.0,
                                         frame_dimensions[i, 1])
     return output
예제 #23
0
    receivers = rec.inject(field=v.backward, expr=rec*s**2/model.m)
    rev_op = Operator([rev_stencil] + receivers + [gradient_update],
                      subs=model.spacing_map)

    fwd_op(time=nt - 2, dt=model.critical_dt)

    rev_op(dt=model.critical_dt, time=nt-16)

    return grad.data


error_metrics = {'L0': error_L0, 'L1': error_L1, 'L2': error_L2,
                 'Linf': error_Linf, 'angle': error_angle}

print("Starting...")

reference_solution = subsampled_gradient(factor=1)
print(np.linalg.norm(reference_solution))
print("Reference solution acquired")

for f in [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]:
    print("Solving for f=%d"%f)
    solution = subsampled_gradient(factor=f)
    computed_errors = {}
    for k, v in error_metrics.items():
        computed_errors[k] = v(solution, reference_solution)
    computed_errors['f'] = f

    write_results(computed_errors, "subsampling_results.csv")
예제 #24
0
파일: run.py 프로젝트: keshava/daks
def run(initial_model_filename, results_dir, tn, nshots, shots_container, so, nbl, kernel, scale_gradient, max_iter,
        checkpointing, n_checkpoints, compression, tolerance, reference_solution, dtype):

    if dtype == 'float32':
        dtype = np.float32
    elif dtype == 'float64':
        dtype = np.float64
    else:
        raise ValueError("Invalid dtype")

    water_depth = 20  # Number of points at the top of the domain that correspond to water
    exclude_boundaries = True  # Exclude the boundary regions from the optimisation problem
    mute_water = True  # Mute the gradient in the water region

    initial_model_filename, datakey = initial_model_filename

    model, geometry, bounds = initial_setup(initial_model_filename, tn, dtype, so, nbl,
                                            datakey=datakey, exclude_boundaries=exclude_boundaries, water_depth=water_depth)

    client = setup_dask()

    if not os.path.exists(results_dir):
        os.mkdir(results_dir)

    intermediates_dir = os.path.join(results_dir, "intermediates")

    if not os.path.exists(intermediates_dir):
        os.mkdir(intermediates_dir)

    progress_dir = os.path.join(results_dir, "progress")

    if not os.path.exists(progress_dir):
        os.mkdir(progress_dir)

    auth = default_auth()

    solver_params = {'h5_file': Blob("models", initial_model_filename, auth=auth), 'tn': tn,
                     'space_order': so, 'dtype': dtype, 'datakey': datakey, 'nbl': nbl,
                     'opt': ('noop', {'openmp': True, 'par-dynamic-work': 1000})}

    if kernel in ['OT2', 'OT4']:
        solver_params['kernel'] = kernel
        solver = overthrust_solver_iso(**solver_params)
    elif kernel == "rho":
        solver_params['water_depth'] = water_depth
        solver_params['calculate_density'] = False
        solver = overthrust_solver_density(**solver_params)
    solver._dt = 1.75
    solver.geometry.resample(1.75)

    f_args = [nshots, client, solver, shots_container, auth, scale_gradient, mute_water, exclude_boundaries, water_depth]

    if checkpointing:
        f_args += [checkpointing, {'n_checkpoints': n_checkpoints, 'scheme': compression,
                                   'tolerance': tolerance}]
    if exclude_boundaries:
        v0 = mat2vec(trim_boundary(model.vp, model.nbl)).astype(np.float64)
    else:
        v0 = mat2vec(model.vp).astype(np.float64)

    def callback(progress_dir, intermediates_dir, model, exclude_boundaries, vec):
        global plot_model_to_file
        callback.call_count += 1

        if not hasattr(callback, "obj_fn_history"):
            callback.obj_fn_history = []

        callback.obj_fn_history.append(fwi_gradient.obj_fn_cache[vec.tobytes()])

        fwi_iteration = callback.call_count
        filename = os.path.join(intermediates_dir, "solution%d.h5" % fwi_iteration)
        if exclude_boundaries:
            to_hdf5(vec2mat(vec, model.shape), filename)
        else:
            to_hdf5(vec2mat(vec, model.vp.shape), filename)

        progress_filename = os.path.join(progress_dir, "fwi-iter%d.pdf" % (fwi_iteration))
        plot_model_to_file(solver.model, progress_filename)

    callback.call_count = 0

    partial_callback = partial(callback, progress_dir, intermediates_dir, model, exclude_boundaries)

    fwi_gradient.call_count = 0
    fwd_op = solver.op_fwd(save=False)
    rev_op = solver.op_grad(save=False)
    fwd_op.ccode
    rev_op.ccode

    solution_object = minimize(fwi_gradient,
                               v0,
                               args=tuple(f_args),
                               jac=True, method='L-BFGS-B',
                               callback=partial_callback, bounds=bounds,
                               options={'disp': True, 'maxiter': max_iter})

    if exclude_boundaries:
        final_model = vec2mat(solution_object.x, model.shape)
    else:
        final_model = vec2mat(solution_object.x, model.vp.shape)

    solver.model.update("vp", final_model)

    # Save plot of final model
    final_plot_filename = os.path.join(results_dir, "final_model.pdf")
    plot_model_to_file(solver.model, final_plot_filename)

    # Save objective function values to CSV
    obj_fn_history = callback.obj_fn_history
    obj_fn_vals_filename = os.path.join(results_dir, "objective_function_values.csv")
    with open(obj_fn_vals_filename, "w") as vals_file:
        vals_writer = csv.writer(vals_file)
        for r in obj_fn_history:
            vals_writer.writerow([r])

    # Plot objective function values
    plt.title("FWI convergence")
    plt.xlabel("Iteration number")
    plt.ylabel("Objective function value")
    obj_fun_plt_filename = os.path.join(results_dir, "convergence.tex")
    obj_fun_plt_pdf_filename = os.path.join(results_dir, "convergence.pdf")

    plt.clf()
    if reference_solution is None:
        plt.plot(obj_fn_history)
    else:
        # Load reference solution convergence history
        with open(reference_solution, 'r') as reference_file:
            vals_reader = csv.reader(reference_file)
            reference_solution_values = []
            for r in vals_reader:
                reference_solution_values.append(float(r[0]))
        # Pad with 0s to ensure same size

        # Plot with legends
        plt.plot(obj_fn_history, label="Lossy FWI")
        plt.plot(reference_solution_values, label="Reference FWI")
        # Display legend
        plt.legend()
    plt.savefig(obj_fun_plt_pdf_filename)
    tikzplotlib.save(obj_fun_plt_filename)

    true_model = overthrust_model_iso("overthrust_3D_true_model_2D.h5", datakey="m", dtype=dtype, space_order=so, nbl=nbl)
    true_model_vp = trim_boundary(true_model.vp, true_model.nbl)

    error_norm = np.linalg.norm(true_model_vp - final_model)
    print("L2 norm of final solution vs true solution: %f" % error_norm)

    data = {'error_norm': error_norm,
            'checkpointing': checkpointing,
            'compression': compression,
            'tolerance': tolerance,
            'ncp': n_checkpoints}

    write_results(data, "fwi_experiment.csv")

    # Final solution
    final_solution_filename = os.path.join(results_dir, "final_solution.h5")
    to_hdf5(final_model, final_solution_filename)
예제 #25
0
tolerances = [10**x for x in range(0, -17, -1)]

error_to_plot = []

for atol in tolerances:
    print("Compressing at tolerance %s" % str(atol))
    compressed = pyzfp.compress(field, tolerance=atol)
    decompressed = pyzfp.decompress(compressed,
                                    shape=field.shape,
                                    dtype=field.dtype,
                                    tolerance=atol)

    computed_errors = {}
    computed_errors['cf'] = len(field.tostring()) / float(len(compressed))
    for k, v in error_metrics.items():
        computed_errors[k] = v(field, decompressed)

    error_function = error_metrics[plot]
    error_to_plot.append(computed_errors[plot])

    computed_errors['tolerance'] = atol
    write_results(computed_errors, 'direct_compression_results.csv')

plt.xscale('log')
plt.yscale('log')
plt.plot(tolerances, error_to_plot)
plt.xlabel("atol")
plt.ylabel(plot)
plt.savefig("direct_%s.pdf" % plot, bbox_inches='tight')
tikzplotlib.save("direct_%s.tex" % plot)
예제 #26
0
    start = time.time()

    while capture.isOpened():
        ret, frame = capture.read()

        if ret:
            img = prepare_input_image(frame, input_dimension)
            im_dim = frame.shape[1], frame.shape[0]
            im_dim = torch.FloatTensor(im_dim).repeat(1, 2)

            if cuda:
                im_dim = im_dim.cuda()
                img = img.cuda()

            output = model(Variable(img))
            output = write_results(output, confidence, len(classes),
                                   overlap_threshold)

            if type(output) == int:
                frames += 1
                print("FPS of the _video is {:5.4f}".format(
                    frames / (time.time() - start)))
                cv2.imshow("frame", frame)
                key = cv2.waitKey(1)
                if key & 0xFF == ord('q'):
                    break
                continue
            output[:, 1:5] = torch.clamp(output[:, 1:5], 0.0,
                                         float(input_dimension))

            im_dim = im_dim.repeat(output.size(0), 1) / input_dimension
            output[:, 1:5] *= im_dim
예제 #27
0
write = 0

if CUDA:
    im_dim_list = im_dim_list.cuda()

start_det_loop = time.time()

for i, batch in enumerate(im_batches):
    # load the image
    start = time.time()

    if CUDA:
        batch = batch.cuda()

    prediction = model(Variable(batch, volatile=True), CUDA)
    prediction = write_results(prediction, confidence, num_classes, nms_conf=nms_thresh)

    end = time.time()

    if type(prediction) == int:

        for im_num, image in enumerate(
            imlist[i * batch_size:min((i + 1) * batch_size, len(imlist))]
        ):
            im_id = i * batch_size + im_num
            print(
                '{0:20s} predicted in {1:6.3f} seconds'.format(
                    image.split('/')[-1], (end - start) / batch_size))
            print(
                '{0:20s} {1:s}'.format('Objects detected:', ''))
            print('----------------------------------------------------------')
예제 #28
0
            torch.cat(
                (im_batches[i * batch_size:min((i + 1) *
                                               batch_size, len(im_batches))]))
            for i in range(num_batches)
        ]
    return (imlist, im_batches, im_dim_list, loaded_ims, read_dir, load_batch)


def load_test_input(img_file):
    img = cv2.imread(img_file)
    img = cv2.resize(img, (416, 416))  # Resize to the input dimension
    # BGR -> RGB | H X W C -> C X H X W
    img_ = img[:, :, ::-1].transpose((2, 0, 1))
    # Add a channel at 0 (for batch) | Normalise
    img_ = img_[np.newaxis, :, :, :] / 255.0
    img_ = torch.from_numpy(img_).float()  # Convert to float
    img_ = Variable(img_)  # Convert to Variable
    return img_


if __name__ == '__main__':
    model = Darknet("cfg/yolov3.cfg").cuda(device=None)
    model.load_weights("data/yolov3.weights")
    inp = load_test_input('data/dog-cycle-car.png').cuda()
    pred = model(inp, torch.cuda.is_available())
    result = write_results(
        pred.data,
        0.5,
        80,
    )
    print(result)
예제 #29
0
def make_majority_vote():

    model_paths = ['convnet_' + str(i + 1) + '.pkl' for i in range(10)]
    out_path = 'submission.csv'

    models = []

    for model_path in model_paths:
        print('Loading ' + model_path + '...')
        try:
            with open(model_path, 'rb') as f:
                models.append(pkl.load(f))
        except Exception as e:
            try:
                with gzip.open(model_path, 'rb') as f:
                    models.append(pkl.load(f))
            except Exception as e:
                usage()
                print(
                    model_path +
                    "doesn't seem to be a valid model path, I got this error when trying to load it: "
                )
                print(e)

    # load the test set
    with open('test_data_for_pylearn2.pkl', 'rb') as f:
        dataset = pkl.load(f)

    dataset = DenseDesignMatrix(X=dataset,
                                view_converter=DefaultViewConverter(
                                    shape=[32, 32, 1], axes=['b', 0, 1, 'c']))
    preprocessor = GlobalContrastNormalization(subtract_mean=True,
                                               sqrt_bias=0.0,
                                               use_std=True)
    preprocessor.apply(dataset)

    predictions = []
    print('Model description:')
    print('')
    print(models[1])
    print('')

    for model in models:

        model.set_batch_size(dataset.X.shape[0])

        X = model.get_input_space().make_batch_theano()
        Y = model.fprop(X)  # forward prop the test data

        y = T.argmax(Y, axis=1)

        f = function([X], y)

        x_arg = dataset.get_topological_view()
        y = f(x_arg.astype(X.dtype))

        assert y.ndim == 1
        assert y.shape[0] == dataset.X.shape[0]

        # add one to the results!
        y += 1

        predictions.append(y)

    predictions = np.array(predictions, dtype='int32')

    y = mode(predictions.T, axis=1)[0]
    y = np.array(y, dtype='int32')

    import itertools
    y = list(itertools.chain(*y))

    assert len(y) == dataset.X.shape[0]

    util.write_results(y, out_path)

    print('Wrote predictions to submission.csv.')
    return np.reshape(y, (1, -1))
예제 #30
0
def run(initial_model_filename, final_solution_basename, tn, nshots,
        shots_container, so, nbl, kernel, checkpointing, n_checkpoints,
        compression, tolerance):
    dtype = np.float32
    model, geometry, bounds = initial_setup(initial_model_filename, tn, dtype,
                                            so, nbl)

    solver_params = {
        'filename': initial_model_filename,
        'tn': tn,
        'space_order': so,
        'dtype': dtype,
        'datakey': 'm0',
        'nbl': nbl,
        'origin': model.origin,
        'spacing': model.spacing,
        'shots_container': shots_container
    }

    client = setup_dask()

    f_args = [model, geometry, nshots, client, solver_params]

    # if checkpointing:
    #    f_g = fwi_gradient_checkpointed
    #    compression_params = {'scheme': compression, 'tolerance': 10**(-tolerance)}
    #    f_args.append(n_checkpoints)
    #    f_args.append(compression_params)
    # else:

    f_g = fwi_gradient

    clipped_vp = mat2vec(clip_boundary_and_numpy(model.vp.data, model.nbl))

    def callback(final_solution_basename, vec):
        callback.call_count += 1
        fwi_iteration = callback.call_count
        filename = "%s_%d.h5" % (final_solution_basename, fwi_iteration)
        with profiler.get_timer('io', 'write_progress'):
            to_hdf5(vec2mat(vec, model.shape), filename)
        print(profiler.summary())

    callback.call_count = 0

    partial_callback = partial(callback, final_solution_basename)

    solution_object = minimize(f_g,
                               clipped_vp,
                               args=tuple(f_args),
                               jac=True,
                               method='L-BFGS-B',
                               callback=partial_callback,
                               bounds=bounds,
                               options={
                                   'disp': True,
                                   'maxiter': 60
                               })

    final_model = vec2mat(solution_object.x, model.shape)

    true_model = overthrust_model_iso("overthrust_3D_true_model_2D.h5",
                                      datakey="m",
                                      dtype=dtype,
                                      space_order=so,
                                      nbl=nbl)
    true_model_vp = clip_boundary_and_numpy(true_model.vp.data, true_model.nbl)

    error_norm = np.linalg.norm(true_model_vp - final_model)
    print(error_norm)

    data = {
        'error_norm': error_norm,
        'checkpointing': checkpointing,
        'compression': compression,
        'tolerance': tolerance,
        'ncp': n_checkpoints
    }

    write_results(data, "fwi_experiment.csv")

    to_hdf5(final_model, '%s_final.h5' % final_solution_basename)
    cv2.circle(img_disp, pos, 3, (255, 0, 255), -1)
    data[images[index]] = pos
  cv2.imshow('Image',img_disp)
  
  key = cv2.waitKey(1) & 0xFF
  changeimg = False
  if key == ord('z'): #prev image
    index = index - 1
    if index < 0:
      index = len(images) - 1
      print("Last image")
    changeimg = True
  elif key == ord('x'): #next image
    index = index + 1
    if index >= len(images):
      index = 0
      print("First image")
    changeimg = True
  elif key == 27:
    break
    
  if changeimg:
    pos = None
    if images[index] in data:
      pos = data[images[index]]
    print("Displaying image {0} at {1}".format(index, images[index]))
cv2.destroyAllWindows()    

print("Saving data...")
write_results(os.path.join(PATH,OUTFILE), data)
def get_dictionary_data(n_comp=20, zero_index=True):
    unlabeled = util.load_unlabeled_training(flatten=False)
    height, width = 32, 32
    n_images = 10000
    patch_size = (8, 8)

    unlabeled = util.standardize(unlabeled)
    np.random.shuffle(unlabeled)

    print('Extracting reference patches...')

    patches = np.empty((0, 64))
    t0 = time()

    for image in unlabeled[:n_images, :, :]:
        data = np.array(extract_patches_2d(image, patch_size, max_patches=0.10))
        data = data.reshape(data.shape[0], -1)
        data -= np.mean(data, axis=0)
        data /= np.std(data, axis=0) + 1e-20
        patches = np.concatenate([patches, data])

    print('done in %.2fs.' % (time() - t0))

    # whiten the patches
    z = zca.ZCA()
    z.fit(patches)
    z.transform(patches)

    print('Learning the dictionary...')
    t0 = time()
    dico = MiniBatchDictionaryLearning(n_components=n_comp, alpha=1)
    V = dico.fit(patches).components_
    dt = time() - t0
    print('done in %.2fs.' % dt)

    #plt.figure(figsize=(4.2, 4))
    #for i, comp in enumerate(V[:100]):
    #    plt.subplot(10, 10, i + 1)
    #    plt.imshow(comp.reshape(patch_size), cmap=plt.cm.gray_r,
    #               interpolation='nearest')
    #    plt.xticks(())
    #    plt.yticks(())
    #plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)
    #plt.show()

    labeled_data, labels = util.load_labeled_training(flatten=False, zero_index=True)
    labeled_data = util.standardize(labeled_data)

    test_data = util.load_all_test(flatten=False)
    test_data = util.standardize(test_data)

    #util.render_matrix(test_data, flattened=False)

    print('Training SVM with the training images...')
    t0 = time()
    reconstructed_images = np.empty((0, 64))
    multiplied_labels = np.empty((0))

    for i in range(len(labeled_data)):
        image = labeled_data[i, :, :]
        label = labels[i]
        data = extract_patches_2d(image, patch_size, max_patches=0.50)
        data = data.reshape(data.shape[0], -1)
        data -= np.mean(data, axis=0)
        data /= np.std(data, axis=0) + 1e-20

        code = dico.transform(data)
        patches = np.dot(code, V)
        z.transform(patches)

        reconstructed_images = np.concatenate([reconstructed_images, patches])
        extended_labels = np.asarray([label] * len(patches))
        multiplied_labels = np.concatenate([multiplied_labels, extended_labels])

    print(reconstructed_images.shape, multiplied_labels.shape)
    svc = SVC()
    #print('Getting cross-val scores...')
    #scores = cross_validation.cross_val_score(svc, reconstructed_images, multiplied_labels, cv=10)
    #print('cross-val scores:', scores)
    #print('cross-val mean:', np.mean(scores))
    #print('cross-val variance:', np.var(scores))

    print('done in %.2fs.' % (time() - t0))

    svc.fit(reconstructed_images, multiplied_labels)

    print('Reconstructing the test images...')
    t0 = time()

    predictions = []

    for i, image in enumerate(test_data):
        data = extract_patches_2d(image, patch_size, max_patches=0.25)
        data = data.reshape(data.shape[0], -1)
        data -= np.mean(data, axis=0)
        data /= np.std(data, axis=0) + 1e-20

        code = dico.transform(data)
        patches = np.dot(code, V)
        z.transform(patches)

        pred = svc.predict(patches)
        print('Variance in the predictions:', np.var(pred))
        predictions.append(mode(pred))

    print('done in %.2fs.' % (time() - t0))

    predictions += 1
    util.write_results(predictions, 'svm_patches_25_percent_20_comp.csv')
def make_majority_vote():

    model_paths = ['convnet_' + str(i+1) + '.pkl' for i in range(10)]
    out_path = 'submission.csv'

    models = []

    for model_path in model_paths:
        print('Loading ' + model_path + '...')
        try:
            with open(model_path, 'rb') as f:
                models.append(pkl.load(f))
        except Exception as e:
            try:
                with gzip.open(model_path, 'rb') as f:
                    models.append(pkl.load(f))
            except Exception as e:
                usage()
                print(model_path + "doesn't seem to be a valid model path, I got this error when trying to load it: ")
                print(e)

    # load the test set
    with open('test_data_for_pylearn2.pkl', 'rb') as f:
        dataset = pkl.load(f)

    dataset = DenseDesignMatrix(X=dataset, view_converter=DefaultViewConverter(shape=[32, 32, 1], axes=['b', 0, 1, 'c']))
    preprocessor = GlobalContrastNormalization(subtract_mean=True, sqrt_bias=0.0, use_std=True)
    preprocessor.apply(dataset)

    predictions = []
    print('Model description:')
    print('')
    print(models[1])
    print('')

    for model in models:

        model.set_batch_size(dataset.X.shape[0])

        X = model.get_input_space().make_batch_theano()
        Y = model.fprop(X) # forward prop the test data

        y = T.argmax(Y, axis=1)

        f = function([X], y)

        x_arg = dataset.get_topological_view()
        y = f(x_arg.astype(X.dtype))

        assert y.ndim == 1
        assert y.shape[0] == dataset.X.shape[0]

        # add one to the results!
        y += 1

        predictions.append(y)

    predictions = np.array(predictions, dtype='int32')

    y = mode(predictions.T, axis=1)[0]
    y = np.array(y, dtype='int32')

    import itertools
    y = list(itertools.chain(*y))

    assert len(y) == dataset.X.shape[0]

    util.write_results(y, out_path)

    print('Wrote predictions to submission.csv.')
    return np.reshape(y, (1, -1))
예제 #34
0
    def detect(self, image, show=True, verbose=True, save_file=None):
        """
        Description
        -----------
        Performs detection on image
        
        Parameters
        ----------
        image - str or opencv image
            The image to be detected or the file path of the image
        show - bool
            if True, output image is displayed
        verbose - bool
            if True, time metrics are reported
        save_file - str
            if not None, output image is written to this file path
        
        Returns
        -------
        output - tensor [n,8] 
            batch_num, x_min,y_min,x_max,y_max,objectness, max_class_conf,
            max_class_idx for each of n detections
        orig_im - opencv image
            the original image
        
        """

        start = time.time()
        #
        try:  # image is already loaded
            img, orig_im, dim = self.prep_image(image, self.resolution)
        except:  # image is a file path
            image = cv2.imread(image)
            img, orig_im, dim = self.prep_image(image, self.resolution)

        im_dim = torch.FloatTensor(dim).repeat(1, 2)

        if self.CUDA:
            im_dim = im_dim.cuda()
            img = img.cuda()

        output = self.model(Variable(img), self.CUDA)
        output = write_results(output,
                               self.conf,
                               self.num_classes,
                               nms=True,
                               nms_conf=self.nms)
        output[:, 1:5] = torch.clamp(output[:, 1:5], 0.0, float(
            self.resolution)) / self.resolution

        im_dim = im_dim.repeat(output.size(0), 1)
        output[:, [1, 3]] *= image.shape[1]
        output[:, [2, 4]] *= image.shape[0]

        out = list(map(lambda x: self.write(x, orig_im), output))

        if verbose:
            print("FPS of the video is {:5.2f}".format(1.0 /
                                                       (time.time() - start)))

        if save_file != None:
            cv2.imwrite(save_file, orig_im)

        if show:
            cv2.imshow("frame", orig_im)
            cv2.waitKey(0)

        return output, orig_im
예제 #35
0
        with torch.no_grad():
            prediction = model(Variable(batch), CUDA)

        prediction = prediction[:, scales_indices]

        #get the boxes with object confidence > threshold
        #Convert the cordinates to absolute coordinates
        #perform NMS on these boxes, and save the results
        #I could have done NMS and saving seperately to have a better abstraction
        #But both these operations require looping, hence
        #clubbing these ops in one loop instead of two.
        #loops are slower than vectorised operations.

        prediction = util.write_results(prediction,
                                        confidence,
                                        num_classes,
                                        nms=True,
                                        nms_conf=nms_thesh)

        if type(prediction) == int:
            i += 1
            continue

        end = time.time()

        prediction[:, 0] += i * batch_size

        if not write:
            output = prediction
            write = 1
        else:
예제 #36
0
    if deton == "video":
        while cap.isOpened():

            ret, frame = cap.read()
            if ret:

                img, orig_im, dim = prep_image(frame, inp_dim)
                im_dim = torch.torch.cuda.FloatTensor(dim).repeat(1, 2)

                model = model.to(device)
                img = img.to(device)
                output = model(img)
                output = output.to(torch.device("cuda"))
                output = write_results(output,
                                       confidence,
                                       num_classes,
                                       nms=True,
                                       nms_conf=nms_thesh)

                if type(output) == int:
                    frames += 1
                    print("FPS of the video is {:5.2f}".format(
                        frames / (time.time() - start)))
                    cv2.imshow("frame", orig_im)
                    key = cv2.waitKey(1)
                    if key & 0xFF == ord('q'):
                        break
                    continue

                im_dim = im_dim.repeat(output.size(0), 1)
                scaling_factor = torch.min(inp_dim / im_dim, 1)[0].view(-1, 1)
    y = T.argmax(Y, axis=1)

    f = function([X], y)

    x_arg = dataset.get_topological_view()
    y = f(x_arg.astype(X.dtype))

    assert y.ndim == 1
    assert y.shape[0] == dataset.X.shape[0]

    # add one to the results!
    y += 1

    predictions.append(y)
    print(y)

predictions = np.array(predictions, dtype='int32')

y = mode(predictions.T, axis=1)[0]
y = np.array(y, dtype='int32')

import itertools
y = list(itertools.chain(*y))

assert len(y) == dataset.X.shape[0]
print(type(y[0]))
print(type(y))

util.write_results(y, out_path)