Beispiel #1
0
    def run(self):
        while True:
            playing = not self.paused and not self.rect_sel.dragging
            if playing or self.frame is None:
                ret, frame = self.cap.read()
                if not ret:
                    break
                self.frame = frame.copy()

            w, h = getsize(self.frame)
            vis = np.zeros((h, w*2, 3), np.uint8)
            vis[:h,:w] = self.frame
            if len(self.tracker.targets) > 0:
                target = self.tracker.targets[0]
                vis[:,w:] = target.image
                draw_keypoints(vis[:,w:], target.keypoints)
                x0, y0, x1, y1 = target.rect
                cv2.rectangle(vis, (x0+w, y0), (x1+w, y1), (0, 255, 0), 2)

            if playing:
                tracked = self.tracker.track(self.frame)
                if len(tracked) > 0:
                    tracked = tracked[0]
                    cv2.polylines(vis, [np.int32(tracked.quad)], True, (255, 255, 255), 2)
                    for (x0, y0), (x1, y1) in zip(np.int32(tracked.p0), np.int32(tracked.p1)):
                        cv2.line(vis, (x0+w, y0), (x1, y1), (0, 255, 0))
            draw_keypoints(vis, self.tracker.frame_points)

            self.rect_sel.draw(vis)
            cv2.imshow('plane', vis)
            ch = cv2.waitKey(1)
            if ch == ord(' '):
                self.paused = not self.paused
            if ch == 27:
                break
Beispiel #2
0
    def calculateComplexDerefOpAddress(complexDerefOp, registerMap):

        match = re.match("((?:\\-?0x[0-9a-f]+)?)\\(%([a-z0-9]+),%([a-z0-9]+),([0-9]+)\\)", complexDerefOp)
        if match != None:
            offset = 0L
            if len(match.group(1)) > 0:
                offset = long(match.group(1), 16)

            regA = RegisterHelper.getRegisterValue(match.group(2), registerMap)
            regB = RegisterHelper.getRegisterValue(match.group(3), registerMap)

            mult = long(match.group(4), 16)

            # If we're missing any of the two register values, return None
            if regA == None or regB == None:
                if regA == None:
                    return (None, "Missing value for register %s" % match.group(2))
                else:
                    return (None, "Missing value for register %s" % match.group(3))

            if RegisterHelper.getBitWidth(registerMap) == 32:
                val = int32(uint32(regA)) + int32(uint32(offset)) + (int32(uint32(regB)) * int32(uint32(mult)))
            else:
                # Assume 64 bit width
                val = int64(uint64(regA)) + int64(uint64(offset)) + (int64(uint64(regB)) * int64(uint64(mult)))
            return (long(val), None)

        return (None, "Unknown failure.")
Beispiel #3
0
    def run(self):
        while True:
            playing = not self.paused and not self.rect_sel.dragging
            if playing or self.frame is None:
                ret, frame = self.cap.read()
                if not ret:
                    break
                self.frame = frame.copy()

            vis = self.frame.copy()
            if playing:
                tracked = self.tracker.track(self.frame)
                for tr in tracked:
                    cv2.polylines(vis, [np.int32(tr.quad)], True, (255, 255, 255), 2)
                    for (x, y) in np.int32(tr.p1):
                        cv2.circle(vis, (x, y), 2, (255, 255, 255))

            self.rect_sel.draw(vis)
            cv2.imshow('plane', vis)
            ch = cv2.waitKey(1)
            if ch == ord(' '):
                self.paused = not self.paused
            if ch == ord('c'):
                self.tracker.clear()
            if ch == 27:
                break
Beispiel #4
0
Datei: RF.py Projekt: r-b-g-b/Lab
def calc_lfp_rf(lfp, stimparams, onset = 0.055, offset = 0.08):
	
	time_ix = np.linspace(0, 0.333, lfp.shape[1])
	onset_ix = (time_ix < onset).nonzero()[0][-1]
	offset_ix = (time_ix > offset).nonzero()[0][0]
	onset = np.int32(onset*1000)
	offset = np.int32(offset*1000)
	
	freqs = stimparams[:, 0]
	attens = stimparams[:, 1]

	ufreqs = np.unique(freqs)
	uattens = np.unique(attens)

	nfreqs = ufreqs.size
	nattens = uattens.size
	
	rf = np.zeros((nattens, nfreqs))
	for f in range(nfreqs):
		ix1 = freqs == ufreqs[f] # trial where the frequency was this frequency
		for a in range(nattens):
			ix2 = attens == uattens[a] # trial where the attenuation was this attenuation
			ix = np.logical_and(ix1, ix2) # trial where both were true
			rf[a, f] = np.nanmax(lfp[ix, onset_ix:offset_ix]).mean()

	return rf
Beispiel #5
0
def rotate(data, interpArray, rotation_angle):
    for i in range(interpArray.shape[0]):
        for j in range(interpArray.shape[1]):

            i1 = i - (interpArray.shape[0] / 2. - 0.5)
            j1 = j - (interpArray.shape[1] / 2. - 0.5)
            x = i1 * numpy.cos(rotation_angle) - j1 * numpy.sin(rotation_angle)
            y = i1 * numpy.sin(rotation_angle) + j1 * numpy.cos(rotation_angle)

            x += data.shape[0] / 2. - 0.5
            y += data.shape[1] / 2. - 0.5

            if x >= data.shape[0] - 1:
                x = data.shape[0] - 1.1
            x1 = numpy.int32(x)

            if y >= data.shape[1] - 1:
                y = data.shape[1] - 1.1
            y1 = numpy.int32(y)

            xGrad1 = data[x1 + 1, y1] - data[x1, y1]
            a1 = data[x1, y1] + xGrad1 * (x - x1)

            xGrad2 = data[x1 + 1, y1 + 1] - data[x1, y1 + 1]
            a2 = data[x1, y1 + 1] + xGrad2 * (x - x1)

            yGrad = a2 - a1
            interpArray[i, j] = a1 + yGrad * (y - y1)
    return interpArray
Beispiel #6
0
    def getMultipleRows(self,rowbase,rowlimit): #{{{
        """Computes multiple Tanimoto rows *rowbase:rowlimit* corresponding to comparing every SMILES string
        in the query set with the reference SMILES strings having index *row*, *row+1*, ..., *rowlimit-1* in the reference set,
        and stores this block as the most recent asynchronous result.

        This method is synchronous (it will not return until the block has been completely computed).
        """
        if rowbase < 0 or rowlimit > self.nref:
            raise

        # Pad rows out to 64 byte pitch
        rowpitchInFloat = 16*((self.nquery+15)/16)

        # Using pagelocked memory and async copy seems to actually slow us down
        # on large tiled calculations
        self.resultmatrix = numpy.empty((rowlimit-rowbase,rowpitchInFloat),dtype=numpy.float32)
        self.gpu.gpumatrix = cl.Buffer(self.gpu.context,cl.mem_flags.WRITE_ONLY,size=self.resultmatrix.nbytes)

        # With precalculated magnitudes
        lmem_bytes = int(2*4*max(self.rlengths[rowbase:rowlimit]))
        threads_per_block = 192
        self.gpu.multiRowKernel(self.gpu.queue,(threads_per_block*(rowlimit-rowbase),),
                                               self.gpu.rsmiles,self.gpu.rcounts,self.gpu.rl_gpu,self.gpu.rmag_gpu,
                                               self.refPitchInInt,
                                               self.gpu.qsmiles,self.gpu.qcounts,self.gpu.ql_gpu,self.gpu.qmag_gpu,
                                               self.qPitchTInInt,
                                               self.gpu.gpumatrix, numpy.int32(rowpitchInFloat),
                                               numpy.int32(self.qshape[0]),numpy.int32(self.qshape[1]),numpy.int32(rowbase),
                                               cl.LocalMemory(lmem_bytes),cl.LocalMemory(lmem_bytes),
                                               local_size=(threads_per_block,))

        cl.enqueue_read_buffer(self.gpu.queue,self.gpu.gpumatrix,self.resultmatrix).wait()
        return self.resultmatrix[:,0:self.nquery]
def interp(pic,flow):
    ys=np.arange(pic.shape[0]*pic.shape[1])/pic.shape[1]
    ud=(flow[:,:,0].reshape(-1)+ys)%pic.shape[0]
    xs=np.arange(pic.shape[0]*pic.shape[1])%pic.shape[1]
    lr=(flow[:,:,1].reshape(-1)+xs)%pic.shape[1]

    u=np.int32(np.floor(ud))
    d=np.int32(np.ceil(ud))%pic.shape[0]
    udiffs=ud-u
    udiffs=np.dstack((udiffs,udiffs,udiffs))
    l=np.int32(np.floor(lr))
    r=np.int32(np.ceil(lr))%pic.shape[1]
    ldiffs=lr-l
    ldiffs=np.dstack((ldiffs,ldiffs,ldiffs))

    ul=pic[u,l,:]
    ur=pic[u,r,:]
    dl=pic[d,l,:]
    dr=pic[d,r,:]


    udl=ul*(1-udiffs)+dl*udiffs
    udr=ur*(1-udiffs)+dr*udiffs
    ans=np.zeros(pic.shape)
    ans[ys,xs,:]=udl*(1-ldiffs)+udr*ldiffs
    return ans
Beispiel #8
0
    def __getattr__(self, key):
        """Return a grib key, or one of our extra keys."""

        # is it in the grib message?
        try:
            # we just get <type 'float'> as the type of the "values" array...special case here...
            if key in ["values", "pv", "latitudes", "longitudes"]:
                res = gribapi.grib_get_double_array(self.grib_message, key)
            elif key in ('typeOfFirstFixedSurface', 'typeOfSecondFixedSurface'):
                res = np.int32(gribapi.grib_get_long(self.grib_message, key))
            else:
                key_type = gribapi.grib_get_native_type(self.grib_message, key)
                if key_type == int:
                    res = np.int32(gribapi.grib_get_long(self.grib_message, key))
                elif key_type == float:
                    # Because some computer keys are floats, like
                    # longitudeOfFirstGridPointInDegrees, a float32 is not always enough...
                    res = np.float64(gribapi.grib_get_double(self.grib_message, key))
                elif key_type == str:
                    res = gribapi.grib_get_string(self.grib_message, key)
                else:
                    raise ValueError("Unknown type for %s : %s" % (key, str(key_type)))
        except gribapi.GribInternalError:
            res = None

        #...or is it in our list of extras?
        if res is None:
            if key in self.extra_keys:
                res = self.extra_keys[key]
            else:
                #must raise an exception for the hasattr() mechanism to work
                raise AttributeError("Cannot find GRIB key %s" % key)

        return res
Beispiel #9
0
    def test_asset_comparisons(self):

        s_23 = Asset(23)
        s_24 = Asset(24)

        self.assertEqual(s_23, s_23)
        self.assertEqual(s_23, 23)
        self.assertEqual(23, s_23)
        self.assertEqual(int32(23), s_23)
        self.assertEqual(int64(23), s_23)
        self.assertEqual(s_23, int32(23))
        self.assertEqual(s_23, int64(23))
        # Check all int types (includes long on py2):
        for int_type in integer_types:
            self.assertEqual(int_type(23), s_23)
            self.assertEqual(s_23, int_type(23))

        self.assertNotEqual(s_23, s_24)
        self.assertNotEqual(s_23, 24)
        self.assertNotEqual(s_23, "23")
        self.assertNotEqual(s_23, 23.5)
        self.assertNotEqual(s_23, [])
        self.assertNotEqual(s_23, None)
        # Compare to a value that doesn't fit into a platform int:
        self.assertNotEqual(s_23, sys.maxsize + 1)

        self.assertLess(s_23, s_24)
        self.assertLess(s_23, 24)
        self.assertGreater(24, s_23)
        self.assertGreater(s_24, s_23)
Beispiel #10
0
def drawLastFrame(im, im_old):
    dif = np.int32(im) - np.int32(im_old)
    dif[dif<0] = 0
    im = im + dif/1.5
    im[im>255] = 255
    im = np.uint8(im)
    return im
Beispiel #11
0
    def test_simple_intersect(self):
        cube = iris.cube.Cube(np.array([[1,2,3,4,5],
                                           [2,3,4,5,6],
                                           [3,4,5,6,7],
                                           [4,5,6,7,8],
                                           [5,6,7,8,9]], dtype=np.int32))

        lonlat_cs = iris.coord_systems.RotatedGeogCS(10, 20)
        cube.add_dim_coord(iris.coords.DimCoord(np.arange(5, dtype=np.float32) * 90 - 180, 'longitude', units='degrees', coord_system=lonlat_cs), 1)
        cube.add_dim_coord(iris.coords.DimCoord(np.arange(5, dtype=np.float32) * 45 - 90, 'latitude', units='degrees', coord_system=lonlat_cs), 0)
        cube.add_aux_coord(iris.coords.DimCoord(points=np.int32(11), long_name='pressure', units='Pa'))
        cube.rename("temperature")
        cube.units = "K"

        cube2 = iris.cube.Cube(np.array([[1,2,3,4,5],
                                            [2,3,4,5,6],
                                            [3,4,5,6,7],
                                            [4,5,6,7,8],
                                            [5,6,7,8,50]], dtype=np.int32))

        lonlat_cs = iris.coord_systems.RotatedGeogCS(10, 20)
        cube2.add_dim_coord(iris.coords.DimCoord(np.arange(5, dtype=np.float32) * 90, 'longitude', units='degrees', coord_system=lonlat_cs), 1)
        cube2.add_dim_coord(iris.coords.DimCoord(np.arange(5, dtype=np.float32) * 45 - 90, 'latitude', units='degrees', coord_system=lonlat_cs), 0)
        cube2.add_aux_coord(iris.coords.DimCoord(points=np.int32(11), long_name='pressure', units='Pa'))
        cube2.rename("")

        r = iris.analysis.maths.intersection_of_cubes(cube, cube2)
        self.assertCML(r, ('cdm', 'test_simple_cube_intersection.cml'))
Beispiel #12
0
    def setup(self):
        one_count = 200000
        two_count = 1000000

        df1 = DataFrame(
            {'time': np.random.randint(0, one_count / 20, one_count),
             'key': np.random.choice(list(string.ascii_uppercase), one_count),
             'key2': np.random.randint(0, 25, one_count),
             'value1': np.random.randn(one_count)})
        df2 = DataFrame(
            {'time': np.random.randint(0, two_count / 20, two_count),
             'key': np.random.choice(list(string.ascii_uppercase), two_count),
             'key2': np.random.randint(0, 25, two_count),
             'value2': np.random.randn(two_count)})

        df1 = df1.sort_values('time')
        df2 = df2.sort_values('time')

        df1['time32'] = np.int32(df1.time)
        df2['time32'] = np.int32(df2.time)

        self.df1a = df1[['time', 'value1']]
        self.df2a = df2[['time', 'value2']]
        self.df1b = df1[['time', 'key', 'value1']]
        self.df2b = df2[['time', 'key', 'value2']]
        self.df1c = df1[['time', 'key2', 'value1']]
        self.df2c = df2[['time', 'key2', 'value2']]
        self.df1d = df1[['time32', 'value1']]
        self.df2d = df2[['time32', 'value2']]
        self.df1e = df1[['time', 'key', 'key2', 'value1']]
        self.df2e = df2[['time', 'key', 'key2', 'value2']]
Beispiel #13
0
    def test_interpolation(self):
        """
        tests the keypoints interpolation kernel
        Requires the following: "self.keypoints1", "self.actual_nb_keypoints",     "self.gpu_dog_prev", self.gpu_dog",             "self.gpu_dog_next", "self.s", "self.width", "self.height", "self.peakthresh"
        """

        # interpolation_setup :
        border_dist, peakthresh, EdgeThresh, EdgeThresh0, octsize, nb_keypoints, actual_nb_keypoints, width, height, DOGS, s, keypoints_prev, blur = interpolation_setup()

        # actual_nb_keypoints is the number of keypoints returned by "local_maxmin".
        # After the interpolation, it will be reduced, but we can still use it as a boundary.
        maxwg = kernel_workgroup_size(self.program, "interp_keypoint")
        shape = calc_size((keypoints_prev.shape[0],), maxwg)
        gpu_dogs = pyopencl.array.to_device(self.queue, DOGS)
        gpu_keypoints1 = pyopencl.array.to_device(self.queue, keypoints_prev)
        # actual_nb_keypoints = numpy.int32(len((keypoints_prev[:,0])[keypoints_prev[:,1] != -1]))
        start_keypoints = numpy.int32(0)
        actual_nb_keypoints = numpy.int32(actual_nb_keypoints)
        InitSigma = numpy.float32(1.6)  #   warning: it must be the same in my_keypoints_interpolation
        t0 = time.time()
        k1 = self.program.interp_keypoint(self.queue, shape, (maxwg,),
                                          gpu_dogs.data, gpu_keypoints1.data, start_keypoints, actual_nb_keypoints,
                                          peakthresh, InitSigma, width, height)
        res = gpu_keypoints1.get()

        t1 = time.time()
        ref = numpy.copy(keypoints_prev)  # important here
        for i, k in enumerate(ref[:nb_keypoints, :]):
            ref[i] = my_interp_keypoint(DOGS, s, k[1], k[2], 5, peakthresh, width, height)

        t2 = time.time()

        # we have to compare keypoints different from (-1,-1,-1,-1)
        res2 = res[res[:, 1] != -1]
        ref2 = ref[ref[:, 1] != -1]

        if (PRINT_KEYPOINTS):
            logger.info("[s=%s]Keypoints before interpolation: %s", s, actual_nb_keypoints)
            # logger.info(keypoints_prev[0:10,:]
            logger.info("[s=%s]Keypoints after interpolation : %s", s, res2.shape[0])
            logger.info(res[0:actual_nb_keypoints])  # [0:10,:]
            # logger.info("Ref:")
            # logger.info(ref[0:32,:]


#         print(maxwg, self.maxwg, self.wg[0], self.wg[1])
        if self.maxwg < self.wg[0] * self.wg[1]:
            logger.info("Not testing result as the WG is too little %s", self.maxwg)
            return
        self.assertLess(abs(len(ref2) - len(res2)) / (len(ref2) + len(res2)), 0.33, "the number of keypoint is almost the same")
#         print(ref2)
#         print(res2)

        delta = norm_L1(ref2, res2)
        self.assert_(delta < 0.43, "delta=%s" % (delta))
        logger.info("delta=%s" % delta)

        if self.PROFILE:
            logger.info("Global execution time: CPU %.3fms, GPU: %.3fms." % (1000.0 * (t2 - t1), 1000.0 * (t1 - t0)))
            logger.info("Keypoints interpolation took %.3fms" % (1e-6 * (k1.profile.end - k1.profile.start)))
    def execute(self, sub_intervals):
        cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects)

        global_size = (self.num,)
        local_size = None

        # set up the Kernel argument list
        w = numpy.int32(640)
        h = numpy.int32(480)
        kernelargs = (self.pos_cl, 
                      self.col_cl, 
                      self.depth_cl,
                      self.rgb_cl, 
                      self.pt_cl, 
                      self.ipt_cl, 
                      w,
                      h)

    
        for i in xrange(0, sub_intervals):
            self.program.project(self.queue, global_size, local_size, *(kernelargs))

        #pos = numpy.ndarray((self.imsize*4, 1), dtype=numpy.float32)
        #cl.enqueue_read_buffer(self.queue, self.pos_cl, pos).wait()
        #for i in xrange(0, 100, 4):
        #    print pos[i], pos[i+1], pos[i+2], pos[i+3]

        cl.enqueue_release_gl_objects(self.queue, self.gl_objects)
        self.queue.finish()
Beispiel #15
0
 def rotate(self):
     #self.x += self.r
     #self.y += self.r
     #d = 2*np.pi*random.random()
     ang = self.angle+random.random()/6
     self.x = self.xparent + np.int32(fdist(self.r)*np.cos(ang))+randint(-int(self.r),int(self.r))
     self.y = self.yparent + np.int32(fdist(self.r)*np.sin(ang))+randint(-int(self.r),int(self.r))
Beispiel #16
0
def make_image():
    img = np.zeros((500, 500), np.uint8)
    black, white = 0, 255
    for i in xrange(6):
        dx = (i%2)*250 - 30
        dy = (i/2)*150

        if i == 0:
            for j in xrange(11):
                angle = (j+5)*np.pi/21
                c, s = np.cos(angle), np.sin(angle)
                x1, y1 = np.int32([dx+100+j*10-80*c, dy+100-90*s])
                x2, y2 = np.int32([dx+100+j*10-30*c, dy+100-30*s])
                cv2.line(img, (x1, y1), (x2, y2), white)

        cv2.ellipse( img, (dx+150, dy+100), (100,70), 0, 0, 360, white, -1 )
        cv2.ellipse( img, (dx+115, dy+70), (30,20), 0, 0, 360, black, -1 )
        cv2.ellipse( img, (dx+185, dy+70), (30,20), 0, 0, 360, black, -1 )
        cv2.ellipse( img, (dx+115, dy+70), (15,15), 0, 0, 360, white, -1 )
        cv2.ellipse( img, (dx+185, dy+70), (15,15), 0, 0, 360, white, -1 )
        cv2.ellipse( img, (dx+115, dy+70), (5,5), 0, 0, 360, black, -1 )
        cv2.ellipse( img, (dx+185, dy+70), (5,5), 0, 0, 360, black, -1 )
        cv2.ellipse( img, (dx+150, dy+100), (10,5), 0, 0, 360, black, -1 )
        cv2.ellipse( img, (dx+150, dy+150), (40,10), 0, 0, 360, black, -1 )
        cv2.ellipse( img, (dx+27, dy+100), (20,35), 0, 0, 360, white, -1 )
        cv2.ellipse( img, (dx+273, dy+100), (20,35), 0, 0, 360, white, -1 )
    return img
Beispiel #17
0
  def testIgnoredArguments(self):
    """Tests that JIT computations can ignore formal parameters."""

    with self.session(config=NoRewriteSessionConfig()) as sess:
      x = array_ops.placeholder(dtypes.int32)
      y = array_ops.placeholder(dtypes.int32)
      with jit_scope():
        z = math_ops.add(x, x)
        w = math_ops.add(y, y)
        # Pulls 'w' into the same compilation via control dependencies.
        with ops.control_dependencies([w]):
          n = control_flow_ops.no_op()
        with ops.control_dependencies([n]):
          t = math_ops.add(z, z)

      run_metadata = config_pb2.RunMetadata()
      out = test_utils.RunWithWarmup(
          sess,
          t, {
              x: np.int32(7),
              y: np.int32(404)
          },
          run_metadata=run_metadata,
          options=config_pb2.RunOptions(
              trace_level=config_pb2.RunOptions.FULL_TRACE))
      self.assert_(MetadataHasXlaRunOp(run_metadata))
      self.assertAllClose(28, out)
Beispiel #18
0
    def test_maybe_convert_scalar(self):

        # pass thru
        result = maybe_convert_scalar('x')
        assert result == 'x'
        result = maybe_convert_scalar(np.array([1]))
        assert result == np.array([1])

        # leave scalar dtype
        result = maybe_convert_scalar(np.int64(1))
        assert result == np.int64(1)
        result = maybe_convert_scalar(np.int32(1))
        assert result == np.int32(1)
        result = maybe_convert_scalar(np.float32(1))
        assert result == np.float32(1)
        result = maybe_convert_scalar(np.int64(1))
        assert result == np.float64(1)

        # coerce
        result = maybe_convert_scalar(1)
        assert result == np.int64(1)
        result = maybe_convert_scalar(1.0)
        assert result == np.float64(1)
        result = maybe_convert_scalar(Timestamp('20130101'))
        assert result == Timestamp('20130101').value
        result = maybe_convert_scalar(datetime(2013, 1, 1))
        assert result == Timestamp('20130101').value
        result = maybe_convert_scalar(Timedelta('1 day 1 min'))
        assert result == Timedelta('1 day 1 min').value
 def process(self):
     img1 = cv2.cvtColor(self.imgcv1, cv2.COLOR_BGR2GRAY)  #queryimage # left image
     img2 = cv2.cvtColor(self.imgcv2, cv2.COLOR_BGR2GRAY)  #trainimage # right image
     # find the keypoints and descriptors with SIFT
     kp1, des1 = self.detector.detectAndCompute(img1,None)
     kp2, des2 = self.detector.detectAndCompute(img2,None)      
     matches = self.flann.knnMatch(des1,des2,k=2)       
     pts1 = []
     pts2 = []        
     # ratio test as per Lowe's paper
     for i,(m,n) in enumerate(matches):
         if m.distance < 0.8*n.distance:
             pts2.append(kp2[m.trainIdx].pt)
             pts1.append(kp1[m.queryIdx].pt)
     pts1 = np.float32(pts1)
     pts2 = np.float32(pts2)
     M, mask = cv2.findHomography(pts1, pts2, cv2.RANSAC,5.0)
     h,w = img1.shape
     h/=2
     w/=2
     pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
     dst = cv2.perspectiveTransform(pts,M)
     cv2.polylines(img2,[np.int32(dst)],True,255,3)
     #We select only inlier points
     pts1 = pts1[mask.ravel()==1]
     pts2 = pts2[mask.ravel()==1]
     self.data=(M,pts1,pts2)
     pts1i=np.int32(pts1)
     pts2i=np.int32(pts2)
     return drawpoints(img1,img2,pts1i,pts2i)
Beispiel #20
0
 def skip_record(self, header):
     '''
     Skip over this record, not counting header and header2.
     '''
     imeth = header['imeth']
     if imeth == 0:
         nbytes = (self.nrow * self.ncol * self.nlay 
                   * self.realtype(1).nbytes)
     elif imeth == 1:
         nbytes = (self.nrow * self.ncol * self.nlay 
                   * self.realtype(1).nbytes)
     elif imeth == 2:
         nlist = binaryread(self.file, np.int32)
         nbytes = nlist * (np.int32(1).nbytes + self.realtype(1).nbytes)
     elif imeth == 3:
         nbytes = (self.nrow * self.ncol * self.nlay 
                   * self.realtype(1).nbytes)
         nbytes += (self.nrow * self.ncol * self.nlay 
                   * np.int32(1).nbytes)
     elif imeth == 4:
         nbytes = (self.nrow * self.ncol * self.realtype(1).nbytes)
     elif imeth == 5:
         nval = binaryread(self.file, np.int32)
         for i in xrange(nval - 1):
             temp = binaryread(self.file, str, charlen=16)
         nlist = binaryread(self.file, np.int32)
         nbytes = nlist * (np.int32(1).nbytes + nval * self.realtype(1).nbytes)
     else:
         raise Exception('invalid method code ' + imeth)
     self.file.seek(nbytes, 1)
     return
Beispiel #21
0
 def __init__(self,nx,ny,bsize=16):
     self.nx = np.int32(nx)
     self.ny = np.int32(ny)
     self.bsize = bsize
     self.batch = nx/bsize*ny
     from pycudafft import FFTPlan
     self.fftplan = FFTPlan(self.bsize,1)
Beispiel #22
0
def inline_linear_interp(amps, phases, freqs, output, df, flow, imin, start_index):
    # Note that imin and start_index are ignored in the GPU code; they are only
    # needed for CPU.
    if output.precision == 'double':
        raise NotImplementedError("Double precision linear interpolation not currently supported on CUDA scheme")
    flow = numpy.float32(flow)
    texlen = numpy.int32(len(freqs))
    fmax = numpy.float32(freqs[texlen-1])
    hlen = numpy.int32(len(output))
    (fn1, fn2, ftex, atex, ptex, nt, nb) = get_dckernel(hlen)
    freqs_gpu = gpuarray.to_gpu(freqs)
    freqs_gpu.bind_to_texref_ext(ftex, allow_offset=False)
    amps_gpu = gpuarray.to_gpu(amps)
    amps_gpu.bind_to_texref_ext(atex, allow_offset=False)
    phases_gpu = gpuarray.to_gpu(phases)
    phases_gpu.bind_to_texref_ext(ptex, allow_offset=False)
    fn1 = fn1.prepared_call
    fn2 = fn2.prepared_call
    df = numpy.float32(df)
    g_out = output.data.gpudata
    lower = zeros(nb, dtype=numpy.int32).data.gpudata
    upper = zeros(nb, dtype=numpy.int32).data.gpudata
    fn1((1, 1), (nb, 1, 1), lower, upper, texlen, df, flow, fmax)
    fn2((nb, 1), (nt, 1, 1), g_out, df, hlen, flow, fmax, texlen, lower, upper)
    pycbc.scheme.mgr.state.context.synchronize()
    return output
Beispiel #23
0
def testNumpyTypeCoercion():
    t = emzed.utils.toTable("a", [np.int32(1)])
    t.info()
    assert t.getColTypes() == [int], t.getColTypes()
    t = emzed.utils.toTable("a", [None, np.int32(1)])
    t.info()
    assert t.getColTypes() == [int], t.getColTypes()

    t.addColumn("b", np.int32(1))
    assert t.getColTypes() == [int, int], t.getColTypes()
    t.replaceColumn("b", [None, np.int32(1)])
    assert t.getColTypes() == [int, int], t.getColTypes()

    t.replaceColumn("b", np.int64(1))
    assert t.getColTypes() == [int, int], t.getColTypes()
    t.replaceColumn("b", [None, np.int64(1)])
    assert t.getColTypes() == [int, int], t.getColTypes()

    t.replaceColumn("b", np.float32(1.0))
    assert t.getColTypes() == [int, float], t.getColTypes()
    t.replaceColumn("b", [None, np.float32(1.0)])
    assert t.getColTypes() == [int, float], t.getColTypes()

    t.replaceColumn("b", np.float64(2.0))
    assert t.getColTypes() == [int, float], t.getColTypes()
    t.replaceColumn("b", [None, np.float64(2.0)])
    assert t.getColTypes() == [int, float], t.getColTypes()
  def test_broadcasting_explicitly_unsupported(self):
    old_batch_shape = [4]
    new_batch_shape = [1, 4, 1]
    rate_ = self.dtype([1, 10, 2, 20])

    rate = array_ops.placeholder_with_default(
        rate_,
        shape=old_batch_shape if self.is_static_shape else None)
    poisson_4 = poisson_lib.Poisson(rate)
    new_batch_shape_ph = (
        constant_op.constant(np.int32(new_batch_shape)) if self.is_static_shape
        else array_ops.placeholder_with_default(
            np.int32(new_batch_shape), shape=None))
    poisson_141_reshaped = batch_reshape_lib.BatchReshape(
        poisson_4, new_batch_shape_ph, validate_args=True)

    x_4 = self.dtype([2, 12, 3, 23])
    x_114 = self.dtype([2, 12, 3, 23]).reshape(1, 1, 4)

    if self.is_static_shape:
      with self.assertRaisesRegexp(NotImplementedError,
                                   "too few batch and event dims"):
        poisson_141_reshaped.log_prob(x_4)
      with self.assertRaisesRegexp(NotImplementedError,
                                   "unexpected batch and event shape"):
        poisson_141_reshaped.log_prob(x_114)
      return

    with self.assertRaisesOpError("too few batch and event dims"):
      with self.test_session():
        poisson_141_reshaped.log_prob(x_4).eval()

    with self.assertRaisesOpError("unexpected batch and event shape"):
      with self.test_session():
        poisson_141_reshaped.log_prob(x_114).eval()
Beispiel #25
0
  def testSplit(self):
    for dtype in self.numeric_types:
      for axis in [0, -3]:
        self._testBinary(
            lambda x, y: array_ops.split(value=y, num_or_size_splits=3, axis=x),
            np.int32(axis),
            np.array([[[1], [2]], [[3], [4]], [[5], [6]]],
                     dtype=dtype),
            expected=[
                np.array([[[1], [2]]], dtype=dtype),
                np.array([[[3], [4]]], dtype=dtype),
                np.array([[[5], [6]]], dtype=dtype),
            ],
            equality_test=self.ListsAreClose)

      for axis in [1, -2]:
        self._testBinary(
            lambda x, y: array_ops.split(value=y, num_or_size_splits=2, axis=x),
            np.int32(axis),
            np.array([[[1], [2]], [[3], [4]], [[5], [6]]],
                     dtype=dtype),
            expected=[
                np.array([[[1]], [[3]], [[5]]], dtype=dtype),
                np.array([[[2]], [[4]], [[6]]], dtype=dtype),
            ],
            equality_test=self.ListsAreClose)
  def test_non_positive_shape(self):
    dims = 2
    old_batch_shape = [4]
    if self.is_static_shape:
      # Unknown first dimension does not trigger size check. Note that
      # any dimension < 0 is treated statically as unknown.
      new_batch_shape = [-1, 0]
    else:
      new_batch_shape = [-2, -2]  # -2 * -2 = 4, same size as the old shape.

    new_batch_shape_ph = (
        constant_op.constant(np.int32(new_batch_shape)) if self.is_static_shape
        else array_ops.placeholder_with_default(
            np.int32(new_batch_shape), shape=None))

    scale = np.ones(old_batch_shape + [dims], self.dtype)
    scale_ph = array_ops.placeholder_with_default(
        scale, shape=scale.shape if self.is_static_shape else None)
    mvn = mvn_lib.MultivariateNormalDiag(scale_diag=scale_ph)

    if self.is_static_shape:
      with self.assertRaisesRegexp(ValueError, r".*must be >=-1.*"):
        batch_reshape_lib.BatchReshape(
            distribution=mvn,
            batch_shape=new_batch_shape_ph,
            validate_args=True)

    else:
      with self.test_session():
        with self.assertRaisesOpError(r".*must be >=-1.*"):
          batch_reshape_lib.BatchReshape(
              distribution=mvn,
              batch_shape=new_batch_shape_ph,
              validate_args=True).sample().eval()
  def test_non_vector_shape(self):
    dims = 2
    new_batch_shape = 2
    old_batch_shape = [2]

    new_batch_shape_ph = (
        constant_op.constant(np.int32(new_batch_shape)) if self.is_static_shape
        else array_ops.placeholder_with_default(
            np.int32(new_batch_shape), shape=None))

    scale = np.ones(old_batch_shape + [dims], self.dtype)
    scale_ph = array_ops.placeholder_with_default(
        scale, shape=scale.shape if self.is_static_shape else None)
    mvn = mvn_lib.MultivariateNormalDiag(scale_diag=scale_ph)

    if self.is_static_shape:
      with self.assertRaisesRegexp(ValueError, r".*must be a vector.*"):
        batch_reshape_lib.BatchReshape(
            distribution=mvn,
            batch_shape=new_batch_shape_ph,
            validate_args=True)

    else:
      with self.test_session():
        with self.assertRaisesOpError(r".*must be a vector.*"):
          batch_reshape_lib.BatchReshape(
              distribution=mvn,
              batch_shape=new_batch_shape_ph,
              validate_args=True).sample().eval()
def draw_match(img1, img2, p1, p2, status = None, H = None):
    h1, w1 = img1.shape[:2]
    h2, w2 = img2.shape[:2]
    vis = np.zeros((max(h1, h2), w1+w2), np.uint8)
    vis[:h1, :w1] = img1
    vis[:h2, w1:w1+w2] = img2
    vis = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR)

    if H is not None:
        corners = np.float32([[0, 0], [w1, 0], [w1, h1], [0, h1]])
        corners = np.int32( cv2.perspectiveTransform(corners.reshape(1, -1, 2), H).reshape(-1, 2) + (w1, 0) )
        cv2.polylines(vis, [corners], True, (255, 255, 255))
    
    if status is None:
        status = np.ones(len(p1), np.bool_)
    green = (0, 255, 0)
    red = (0, 0, 255)
    for (x1, y1), (x2, y2), inlier in zip(np.int32(p1), np.int32(p2), status):
        col = [red, green][inlier]
        if inlier:
            cv2.line(vis, (x1, y1), (x2+w1, y2), col)
            cv2.circle(vis, (x1, y1), 2, col, -1)
            cv2.circle(vis, (x2+w1, y2), 2, col, -1)
        else:
            r = 2
            thickness = 3
            cv2.line(vis, (x1-r, y1-r), (x1+r, y1+r), col, thickness)
            cv2.line(vis, (x1-r, y1+r), (x1+r, y1-r), col, thickness)
            cv2.line(vis, (x2+w1-r, y2-r), (x2+w1+r, y2+r), col, thickness)
            cv2.line(vis, (x2+w1-r, y2+r), (x2+w1+r, y2-r), col, thickness)
    return vis
  def test_bad_reshape_size(self):
    dims = 2
    new_batch_shape = [2, 3]
    old_batch_shape = [2]   # 2 != 2*3

    new_batch_shape_ph = (
        constant_op.constant(np.int32(new_batch_shape)) if self.is_static_shape
        else array_ops.placeholder_with_default(
            np.int32(new_batch_shape), shape=None))

    scale = np.ones(old_batch_shape + [dims], self.dtype)
    scale_ph = array_ops.placeholder_with_default(
        scale, shape=scale.shape if self.is_static_shape else None)
    mvn = mvn_lib.MultivariateNormalDiag(scale_diag=scale_ph)

    if self.is_static_shape:
      with self.assertRaisesRegexp(
          ValueError, (r"`batch_shape` size \(6\) must match "
                       r"`distribution\.batch_shape` size \(2\)")):
        batch_reshape_lib.BatchReshape(
            distribution=mvn,
            batch_shape=new_batch_shape_ph,
            validate_args=True)

    else:
      with self.test_session():
        with self.assertRaisesOpError(r"Shape sizes do not match."):
          batch_reshape_lib.BatchReshape(
              distribution=mvn,
              batch_shape=new_batch_shape_ph,
              validate_args=True).sample().eval()
Beispiel #30
0
def to_sigproc_keyword(keyword, value=None):
    """ Generate a serialized string for a sigproc keyword:value pair
    
    If value=None, just the keyword will be written with no payload.
    Data type is inferred by keyword name (via a lookup table)
    
    Args: 
        keyword (str): Keyword to write
        value (None, float, str, double or angle): value to write to file
    
    Returns:
        value_str (str): serialized string to write to file.
    """
    if not value:
        return np.int32(len(keyword)).tostring() + keyword
    else:
        dtype = header_keyword_types[keyword]

        dtype_to_type = {'<l': np.int32,
                         'str': str,
                         '<d': np.float64,
                         'angle': to_sigproc_angle}

        value_dtype = dtype_to_type[dtype]

        if value_dtype is str:
            return np.int32(len(keyword)).tostring() + keyword + np.int32(len(value)).tostring() + value
        else:
            return np.int32(len(keyword)).tostring() + keyword + value_dtype(value).tostring()
def Louvain_no_isolation(dataset,
                         edge_measure,
                         datatype=np.int32,
                         cluster_batch_size=10000,
                         iso_thres=5):

    print('initializing the graph...')
    g = nx.Graph()
    g.add_nodes_from(np.arange(len(dataset)).tolist())
    print('preparing idx_list...')
    idx_list = []
    for i in range(len(dataset)):
        for j in range(len(dataset)):
            if j == i:
                break
            idx_list.append((i, j))

    print('calculating edges...')
    batch_count = 0
    batch_size = cluster_batch_size
    left_data = np.zeros(list((batch_size, ) + dataset[0].shape),
                         dtype=datatype)
    right_data = np.zeros(list((batch_size, ) + dataset[0].shape),
                          dtype=datatype)
    edge_list = []
    for count, idx_pair in enumerate(idx_list):
        left_data[batch_count] = dataset[idx_pair[0]]
        right_data[batch_count] = dataset[idx_pair[1]]
        batch_count += 1
        if batch_count == batch_size:
            print('predicting...',
                  str(round(count / len(idx_list) * 100, 2)) + '%')
            temp_edge_list = edge_measure(left_data,
                                          right_data)  # error occurs here
            edge_list = edge_list + temp_edge_list.reshape(batch_size).tolist()
            batch_count = 0
    if batch_count != 0:
        print('predicting...')
        temp_edge_list = edge_measure(left_data[:batch_count],
                                      right_data[:batch_count])
        edge_list = edge_list + temp_edge_list.reshape(batch_count).tolist()
    simi_list = edge_list
    edge_list = np.int32(np.round(edge_list))

    #------------------
    print('forming simi_matrix...')
    simi_matrix = np.zeros([len(dataset), len(dataset)])
    for count, idx_pair in enumerate(idx_list):
        simi_matrix[idx_pair[0], idx_pair[1]] = simi_list[count]
        simi_matrix[idx_pair[1], idx_pair[0]] = simi_list[count]
    #------------------

    print('adding edges...')
    true_edge_list = []
    for i in range(len(idx_list)):
        if edge_list[i] == 0:
            true_edge_list.append(idx_list[i])
    g.add_edges_from(true_edge_list)

    print('Clustering...')
    partition = community.best_partition(g)

    # decode to get label_list
    print('decoding to get label_list...')
    label_list = [0] * len(dataset)
    for key in partition:
        label_list[key] = partition[key]

    #------------------
    print('solving isolation...')
    cluster_datanum_dict = {}
    for reltype in label_list:
        if reltype in cluster_datanum_dict.keys():
            cluster_datanum_dict[reltype] += 1
        else:
            cluster_datanum_dict[reltype] = 1

    iso_reltype_list = []
    for reltype in cluster_datanum_dict:
        if cluster_datanum_dict[reltype] <= iso_thres:
            iso_reltype_list.append(reltype)

    for point_idx, reltype in enumerate(label_list):
        if reltype in iso_reltype_list:
            search_idx_list = np.argsort(
                simi_matrix[point_idx])  # from small to big
            for idx in search_idx_list:
                if label_list[idx] not in iso_reltype_list:
                    label_list[point_idx] = label_list[idx]
                    break
    #------------------

    return label_list, create_msg(label_list)
    def __getitem__(self, idx):
        """
        Args: idx (int): Index in list to load image
        """
        # Load img, gt, x_coordinates, y_coordinates, line_type
        assert self.rgb_lst[idx].split('.')[0] == self.gt_lst[idx].split(
            '.')[0]
        img_name = os.path.join(self.image_dir, self.rgb_lst[idx])
        gt_name = os.path.join(self.gt_dir, self.gt_lst[idx])
        with open(img_name, 'rb') as f:
            image = (Image.open(f).convert('RGB'))
        with open(gt_name, 'rb') as f:
            gt = (Image.open(f).convert('P'))
        idx = int(self.rgb_lst[idx].split('.')[0]) - 1
        lanes_lst = self.ordered_lanes[idx]["lanes"]
        h_samples = self.ordered_lanes[idx]["h_samples"]
        line_lst = self.line_file[idx]["lines"]

        # Crop and resize images
        w, h = image.size
        image, gt = F.crop(image, h - 640, 0, 640,
                           w), F.crop(gt, h - 640, 0, 640, w)
        image = F.resize(image,
                         size=(self.resize, 2 * self.resize),
                         interpolation=Image.BILINEAR)
        gt = F.resize(gt,
                      size=(self.resize, 2 * self.resize),
                      interpolation=Image.NEAREST)

        # Adjust size of lanes matrix
        # lanes = np.array(lanes_lst)[:, -self.num_points:]
        lanes = np.array(lanes_lst)
        to_add = np.full((4, 56 - lanes.shape[1]), -2)
        lanes = np.hstack((to_add, lanes))

        # Get valid coordinates from lanes matrix
        valid_points = np.int32(lanes > 0)
        valid_points[:, :8] = 0  # start from h-samples = 210

        # Resize coordinates
        lanes = lanes / 2.5
        track = lanes < 0
        h_samples = np.array(h_samples) / 2.5 - 32
        lanes[track] = -2

        # Compute horizon for resized img
        horizon_lanes = []
        for lane in lanes:
            horizon_lanes.append(
                min([
                    y_cord for (x_cord, y_cord) in zip(lane, h_samples)
                    if x_cord != -2
                ] or [self.resize]))
        y_val = min(horizon_lanes)
        horizon = torch.zeros(gt.size[1])
        horizon[0:int(np.floor(y_val))] = 1

        # Compute line type in image
        # line_lst = np.prod(lanes == -2, axis=1) # 1 when line is not present

        gt = np.array(gt)
        idx3 = np.isin(gt, 3)
        idx4 = np.isin(gt, 4)
        if self.nclasses < 3:
            gt[idx3] = 0
            gt[idx4] = 0
        # Flip ground truth ramdomly
        hflip_input = np.random.uniform(0.0, 1.0) > 0.5 and self.flip_on
        if idx not in self.valid_idx and hflip_input:
            image, gt = F.hflip(image), np.flip(gt, axis=1)
            idx1 = np.isin(gt, 1)
            idx2 = np.isin(gt, 2)
            gt[idx1] = 2
            gt[idx2] = 1
            gt[idx3] = 4
            gt[idx4] = 3
            lanes = (2 * self.resize - 1) - lanes
            lanes[track] = -2
            lanes = lanes[[1, 0, 3, 2]]
            # line_lst = np.prod(lanes == -2, axis=1)
            line_lst = mirror_list(line_lst)

        # Get Tensors
        gt = Image.fromarray(gt)
        image, gt = self.totensor(image).float(), (self.totensor(gt) *
                                                   255).long()

        # Cast to correct types
        line_lst = np.array(line_lst[3:7])
        line_lst = torch.from_numpy(np.array(line_lst + 1)).clamp(0, 1).float()
        valid_points = torch.from_numpy(valid_points).double()

        lanes = torch.from_numpy(lanes).double()
        horizon = horizon.float()

        if idx in self.valid_idx:
            index = self.valid_idx.index(idx)
            return image, gt, lanes, idx, line_lst, horizon, index, valid_points
        return image, gt, lanes, idx, line_lst, horizon, valid_points
Beispiel #33
0
    
if __name__ == '__main__':
    M = 3000;
    K = 4
    
    dataset = 'data-0.pkl.gz'
    path = '/home/bo/Data/RCV1/Processed/'
    f = gzip.open(path + dataset, 'rb')
    data = cPickle.load(f)
    f.close()
    
    train_x = data[0].toarray()
    train_x = train_x.astype(np.float32)
    train_y = np.asarray(data[1], dtype = np.int32)
    train_y = np.reshape(train_y, (train_y.shape[0], 1))
    
    dim = train_x.shape[1]
    data = np.concatenate((train_x, train_y), axis = 1)
    np.random.shuffle(data) 
    
    train_x = data[:][:, 0:dim]
    train_y = np.int32(np.squeeze(data[:][:, -1]))  
    
    V = nystrom(train_x, M)
    V = V[:][:, 0:K]
    
    km = KMeans(n_clusters = 4)
    ypred = km.fit_predict(V)
    
    nmi = metrics.normalized_mutual_info_score(train_y, ypred)
    print >> sys.stderr, ('NMI for deep clustering: %.2f' % (nmi))
__global__ void matrix_mult_ker(float * matrix_a, float * matrix_b, float * output_matrix, int N)
{

    int row = blockIdx.x*blockDim.x + threadIdx.x;
    int col = blockIdx.y*blockDim.y + threadIdx.y;

	output_matrix[col + row*N] = rowcol_dot(matrix_a, matrix_b, row, col, N);

}
''')

matrix_ker = ker.get_function('matrix_mult_ker')

test_a = np.float32([xrange(1, 5)] * 4)
test_b = np.float32([xrange(14, 10, -1)] * 4)

output_mat = np.matmul(test_a, test_b)

test_a_gpu = gpuarray.to_gpu(test_a)
test_b_gpu = gpuarray.to_gpu(test_b)
output_mat_gpu = gpuarray.empty_like(test_a_gpu)

matrix_ker(test_a_gpu,
           test_b_gpu,
           output_mat_gpu,
           np.int32(4),
           block=(2, 2, 1),
           grid=(2, 2, 1))

assert (np.allclose(output_mat_gpu.get(), output_mat))
Beispiel #35
0
    def __init__(self,
                 csv_file,
                 images_dir,
                 boat_ids_csv=None,
                 videos_dir=None,
                 transform=None):
        """
        Args:
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """
        self.num_inputs = 1
        self.images_dir = images_dir
        self.fish_frame = None

        if csv_file:
            self.num_targets = 2
            self.fish_frame = pd.read_csv(csv_file)
            if boat_ids_csv:
                boad_ids_frame = pd.read_csv(boat_ids_csv).set_index(
                    'video_id')
                n_boats = len(boad_ids_frame['boat_id'].unique())
            else:
                boad_ids_frame = None

            classes = []
            species = np.empty((8, ), dtype='int')
            for idx in range(len(self.fish_frame)):
                species[:7] = self.fish_frame.ix[
                    idx, 'species_fourspot':].as_matrix().astype('int')
                species[7] = 1 if np.all(species[:7] == 0) else 0
                _class = np.argmax(species)

                # stratify by boat_id too if CSV provided
                if boad_ids_frame is not None:
                    video_id = self.fish_frame.ix[idx, 'video_id']
                    boat_id = int(boad_ids_frame.get_value(
                        video_id, 'boat_id'))
                    _class = boat_id * 8 + _class
                classes.append(_class)

            print(np.bincount(classes))

            self.classes = np.int32(classes)
        else:
            self.num_targets = 0
            if images_dir:
                s = lambda x: [x[0], int(x[1])]
                self.fish_frame = pd.DataFrame(sorted(
                    map(s, [
                        os.path.basename(x)[:-4].split('_')[0:2]
                        for x in glob.glob(
                            os.path.join(self.images_dir, '*.png'))
                    ])),
                                               columns=['video_id', 'frame'])

        if self.fish_frame is not None:
            self.total_images = len(self.fish_frame)

        if videos_dir:
            self.videos_dir = videos_dir
            self.total_images = 0
            self.video_frames = []
            self.video_index = []
            for video_filename in sorted(
                    glob.glob(os.path.join(self.videos_dir, '*.avi'))):
                vin = cv2.VideoCapture(video_filename)
                frames = int(vin.get(cv2.CAP_PROP_FRAME_COUNT))
                vin.release()
                video_id = re.sub(
                    r'.*([0-9a-zA-Z]{16})(\-True|\-False)?(_\-?\d+)?\.(mp4|avi)',
                    r'\1', video_filename)
                self.video_frames.append((self.total_images, video_filename))
                for it in range(frames):
                    self.video_index.append(((self.total_images + it),
                                             video_filename, it, video_id))
                self.total_images += frames
            self.video_frames_frame = pd.DataFrame(
                self.video_frames,
                columns=['frames', 'video_filename']).set_index('frames')
            self.video_index_frame = pd.DataFrame(
                self.video_index,
                columns=['frame', 'video_filename', 'base_frame',
                         'video_id']).set_index('frame')

            self.video_filename = None
            self.vin = None

        self.transform = transform
Beispiel #36
0
def NoFlowPixels(row, col, min_drainage):

    flow_raster = config.tileset().tilename('flow', row=row, col=col)
    acc_raster = config.tileset().tilename('acc', row=row, col=col)
    stream_features = config.tileset().tilename('streams-from-sources',
                                                row=row,
                                                col=col)
    output = config.tileset().tilename('noflow-from-sources', row=row, col=col)

    if not os.path.exists(stream_features):
        click.secho('\nMissing streams-from-sources for tile (%d, %d)' %
                    (row, col),
                    fg='yellow')
        return

    driver = 'ESRI Shapefile'
    schema = {
        'geometry': 'Point',
        'properties': [('GID', 'int'), ('ROW', 'int:4'), ('COL', 'int:4')]
    }
    crs = fiona.crs.from_epsg(2154)
    options = dict(driver=driver, crs=crs, schema=schema)

    with rio.open(flow_raster) as ds:

        flow = ds.read(1)
        height, width = flow.shape

        # streams = np.zeros_like(flow, dtype='int16')
        with rio.open(acc_raster) as ds2:
            streams = np.int16(ds2.read(1) > min_drainage)

        with fiona.open(stream_features) as fs:
            for feature in fs:

                coordinates = np.array(feature['geometry']['coordinates'],
                                       dtype='float32')
                pixels = fct.worldtopixel(coordinates, ds.transform)

                for i, j in pixels:
                    if 0 <= i < height and 0 <= j < width:
                        streams[i, j] = 1

                # pixels = np.array([
                #     (i, j)
                #     for i, j in fct.worldtopixel(coordinates, ds.transform)
                #     if 0 <= i < height and 0 <= j < width
                # ])

                # streams[pixels[:, 0], pixels[:, 1]] = 1

        with fiona.open(output, 'w', **options) as dst:

            pixels = speedup.noflow(streams, flow)

            if pixels:

                coordinates = ta.pixeltoworld(np.int32(pixels),
                                              ds.transform,
                                              gdal=False)

                for current, point in enumerate(coordinates):
                    dst.write({
                        'type': 'Feature',
                        'geometry': {
                            'type': 'Point',
                            'coordinates': point
                        },
                        'properties': {
                            'GID': current,
                            'ROW': row,
                            'COL': col
                        }
                    })
Beispiel #37
0
def StreamToFeatureFromSources(row, col, min_drainage):
    """
    DOCME
    """

    ci = [-1, -1, 0, 1, 1, 1, 0, -1]
    cj = [0, 1, 1, 1, 0, -1, -1, -1]

    flow_raster = config.tileset().tilename('flow', row=row, col=col)
    acc_raster = config.tileset().tilename('acc', row=row, col=col)
    sources = config.tileset().tilename('inlet-sources', row=row, col=col)
    output = config.tileset().tilename('streams-from-sources',
                                       row=row,
                                       col=col)

    if not os.path.exists(sources):
        click.secho('\nMissing inlet-sources for tile (%d, %d)' % (row, col),
                    fg='yellow')
        return

    driver = 'ESRI Shapefile'
    schema = {
        'geometry':
        'LineString',
        'properties': [('GID', 'int'), ('HEAD', 'int:1'), ('ROW', 'int:4'),
                       ('COL', 'int:4')]
    }
    crs = fiona.crs.from_epsg(2154)
    options = dict(driver=driver, crs=crs, schema=schema)

    with rio.open(flow_raster) as ds:

        flow = ds.read(1)

        height, width = flow.shape

        def intile(i, j):
            return all([i >= 0, i < height, j >= 0, j < width])

        with rio.open(acc_raster) as ds2:
            streams = np.int16(ds2.read(1) > min_drainage)

        with fiona.open(sources) as fs:
            for feature in fs:

                x, y = feature['geometry']['coordinates']
                i, j = ds.index(x, y)

                while intile(i, j) and streams[i, j] == 0:

                    streams[i, j] = 1
                    direction = flow[i, j]

                    if direction == -1 or direction == 0:
                        break

                    n = int(np.log2(direction))
                    i = i + ci[n]
                    j = j + cj[n]

        with fiona.open(output, 'w', **options) as dst:

            for current, (segment, head) in enumerate(
                    speedup.stream_to_feature(streams, flow)):

                coords = ta.pixeltoworld(np.fliplr(np.int32(segment)),
                                         ds.transform,
                                         gdal=False)
                dst.write({
                    'type': 'Feature',
                    'geometry': {
                        'type': 'LineString',
                        'coordinates': coords
                    },
                    'properties': {
                        'GID': current,
                        'HEAD': 1 if head else 0,
                        'ROW': row,
                        'COL': col
                    }
                })
Beispiel #38
0
@testing.parameterize(*testing.product(
    {
        'value': [None, 1, ()],
    }))
@attr.gpu
class TestGpuDeviceFromArrayInvalidValue(unittest.TestCase):

    def test_from_array(self):
        device = backend.GpuDevice.from_array(self.value)
        assert device is None


@testing.parameterize(*testing.product(
    {
        'device_id': [0, 1, 99999, numpy.int32(1)],
    }))
@attr.gpu
class TestGpuDeviceFromDeviceId(unittest.TestCase):

    def test_from_device_id(self):
        device = backend.GpuDevice.from_device_id(self.device_id)
        assert isinstance(device, backend.GpuDevice)
        assert device == chainer.get_device((cuda.cupy, self.device_id))
        assert device.device.id == int(self.device_id)


@testing.parameterize(*testing.product(
    {
        'device_id': [None, -1, (), 0.0, numpy.float32(0)],
    }))
img_path = args.image
mask_path = args.mask

img = io.imread(img_path)
mask = io.imread(mask_path)

dst_dir = args.output
os.makedirs(dst_dir)

input_height = 200
input_width = 200
ori_height, ori_width = img.shape[:2]

if ori_height / ori_width > 1:
    scale = ori_width / 200
    input_height = np.int32(scale * 200)
else:
    scale = ori_height / 200
    input_width = np.int32(scale * 200)


# compute pseudo inverse for input matrix
def pinv(A, reltol=1e-6):
    # compute SVD of input A
    s, u, v = tf.svd(A)

    # invert s and clear entries lower than reltol*s_max
    atol = tf.reduce_max(s) * reltol
    s = tf.boolean_mask(s, s > atol)
    s_inv = tf.diag(1. / s)
Beispiel #40
0
# Python Bitshift 32 Bit Constraint
import numpy as np
np.int32(1171855803) &lt;&lt; 7
Beispiel #41
0
    def get_area_def(self, dsid):
        """Get the area definition of the band."""
        if dsid.name != 'HRV':
            return super(HRITMSGFileHandler, self).get_area_def(dsid)

        cfac = np.int32(self.mda['cfac'])
        lfac = np.int32(self.mda['lfac'])
        loff = np.float32(self.mda['loff'])

        a = self.mda['projection_parameters']['a']
        b = self.mda['projection_parameters']['b']
        h = self.mda['projection_parameters']['h']
        lon_0 = self.mda['projection_parameters']['SSP_longitude']

        nlines = int(self.mda['number_of_lines'])
        ncols = int(self.mda['number_of_columns'])

        segment_number = self.mda['segment_sequence_number']

        current_first_line = (
            segment_number - self.mda['planned_start_segment_number']) * nlines
        bounds = self.epilogue['ImageProductionStats'][
            'ActualL15CoverageHRV'].copy()
        if self.fill_hrv:
            bounds['UpperEastColumnActual'] = 1
            bounds['UpperWestColumnActual'] = 11136
            bounds['LowerEastColumnActual'] = 1
            bounds['LowerWestColumnActual'] = 11136
            ncols = 11136

        upper_south_line = bounds[
            'LowerNorthLineActual'] - current_first_line - 1
        upper_south_line = min(max(upper_south_line, 0), nlines)

        lower_coff = (5566 - bounds['LowerEastColumnActual'] + 1)
        upper_coff = (5566 - bounds['UpperEastColumnActual'] + 1)

        lower_area_extent = self.get_area_extent(
            (upper_south_line, ncols), (loff, lower_coff), (lfac, cfac), h)

        upper_area_extent = self.get_area_extent(
            (nlines - upper_south_line, ncols),
            (loff - upper_south_line, upper_coff), (lfac, cfac), h)

        proj_dict = {
            'a': float(a),
            'b': float(b),
            'lon_0': float(lon_0),
            'h': float(h),
            'proj': 'geos',
            'units': 'm'
        }

        lower_area = geometry.AreaDefinition('some_area_name',
                                             "On-the-fly area", 'geosmsg',
                                             proj_dict, ncols,
                                             upper_south_line,
                                             lower_area_extent)

        upper_area = geometry.AreaDefinition('some_area_name',
                                             "On-the-fly area", 'geosmsg',
                                             proj_dict, ncols,
                                             nlines - upper_south_line,
                                             upper_area_extent)
        area = geometry.StackedAreaDefinition(lower_area, upper_area)

        self.area = area.squeeze()
        return self.area
def read(params):

    """
        Read data from 'datasets/...'
    
    """
    if params.dataset == 'mnist':
        
       filename = 'datasets/mnist.pkl.gz' 
       if not os.path.exists(filename):
           raise Exception("Dataset not found!")
    
       data = cPickle.load(gzip.open(filename))
       t1Data, t1Label = data[0][0], np.int32(data[0][1])
       vData, vLabel = data[1][0], np.int32(data[1][1])
       testD, testL = data[2][0], np.int32(data[2][1])
    
    elif params.dataset == 'not_mnist':
        
       filename = 'datasets/not_mnist.pkl.gz' 
       if not os.path.exists(filename):
           raise Exception("Dataset not found!")
    
       data = cPickle.load(gzip.open(filename))
       t1Data, t1Label = data[0][0], np.int32(data[0][1])
       testD, testL = data[1][0], np.int32(data[1][1])
       del data
                      
       split = 400000
       t1Data, t1Label = permute(t1Data, t1Label, params)                
       vData, vLabel = t1Data[split:], t1Label[split:]
       t1Data, t1Label = t1Data[:split], t1Label[:split]

    elif params.dataset == 'svhn':
        
       f1 = 'datasets/svhn_train.pkl.gz' 
       f2 = 'datasets/svhn_test.pkl.gz' 
       if not os.path.exists(f1) or not os.path.exists(f2):
           raise Exception("Dataset not found!")
    
       [t1Data, t1Label] = cPickle.load(gzip.open(f1))
       [testD, testL] = cPickle.load(gzip.open(f2))
       t1Label = t1Label[:,0]; testL = testL[:,0]
                      
       split = 65000
       t1Data, t1Label = permute(t1Data, t1Label, params)                
       vData, vLabel = t1Data[split:], t1Label[split:]
       t1Data, t1Label = t1Data[:split], t1Label[:split]

    elif params.dataset == 'cifar10':
    
       folderName = 'datasets/cifar-10-batches-py/' # assumes unzipped
       if not os.path.exists(folderName):
           raise Exception("Dataset not found!")
    
       batchNames = ['data_batch_1', 'data_batch_2', 'data_batch_3', 'data_batch_4'] 
       t1Data, t1Label = np.empty((0,3072), dtype = float), np.empty((0), dtype = int)
    
       for item in batchNames: 
           fo = open(folderName + item, 'rb'); dict = cPickle.load(fo); fo.close()
           t1Data = np.append(t1Data, np.float32(dict['data']), axis = 0)
           t1Label = np.append(t1Label, np.int32(dict['labels']))
           
       fo = open(folderName + 'data_batch_5', 'rb'); dict = cPickle.load(fo); fo.close()
       vData = np.float32(dict['data']); vLabel = np.int32(dict['labels'])  
       fo = open(folderName + 'test_batch', 'rb'); dict = cPickle.load(fo); fo.close()
       testD = np.float32(dict['data']); testL = np.int32(dict['labels'])   

    else: 
        print 'Dataset '+params.dataset+' is not implemented.'
#    TODO
#    elif params.daaset == 'svhn':        
    return  t1Data, t1Label, vData, vLabel, testD, testL
Beispiel #43
0
    def train(self, views, verbose=True):
        # Show Message or not
        self.verbose = verbose
        # Number of Subjects
        self.V = np.shape(views)[0]

        try:
            if len(self.regularization) == self.V:
                self.eps = [np.float32(e) for e in self.regularization]
            else:
                self.eps = [
                    np.float32(self.regularization) for i in range(self.V)
                ]  # Assume eps is same for each view
        except:
            self.eps = [
                np.float32(self.regularization) for i in range(self.V)
            ]  # Assume eps is same for each view

        self.F = [np.int(np.shape(views)[2])
                  for i in range(self.V)]  # Assume eps is same for each view

        if self.Dim is None:
            self.k = np.shape(views)[
                2]  # Dimensionality of embedding we want to learn
        else:
            try:
                self.k = np.int32(self.Dim)
            except:
                self.k = np.shape(views)[
                    2]  # Dimensionality of embedding we want to learn

        N = views[0].shape[0]

        _Stilde = np.float32(np.zeros(self.k))
        _Gprime = np.float32(np.zeros((N, self.k)))

        ProjectMats = list()

        # Take SVD of each view, to calculate A_i and T_i
        for i, (eps, view) in enumerate(zip(self.eps, views)):
            if self.verbose:
                print('TRAIN DATA -> View %d -> Run SVD ...' % (i + 1))
            A, S_thin, B = scipy.linalg.svd(view, full_matrices=False)
            if self.verbose:
                print('TRAIN DATA -> View %d -> Calculate Sigma inverse ...' %
                      (i + 1))
            S2_inv = 1. / (np.multiply(S_thin, S_thin) + eps)
            T = np.diag(
                np.sqrt(np.multiply(np.multiply(S_thin, S2_inv), S_thin)))
            if self.verbose:
                print('TRAIN: Calculate dot product AT for View %d' % (i + 1))
            ajtj = A.dot(T)
            ProjectMats.append(ajtj)
            if self.verbose:
                print(
                    'TRAIN DATA -> View %d -> Calculate Incremental PCA ...' %
                    (i + 1))
            _Gprime, _Stilde = self._batch_incremental_pca(
                ajtj, _Gprime, _Stilde, i, self.verbose)
            if self.verbose:
                print('TRAIN DATA -> View %d -> Decomposing data matrix ...' %
                      (i + 1))
        self.G = _Gprime
        self.EigVal = _Stilde
        self.Xtrain = list()

        print('TRAIN DATA -> Mapping to shared space ...')
        # Get mapping to shared space
        for pid, project in enumerate(ProjectMats):
            self.Xtrain.append(
                np.dot(np.dot(project, np.transpose(project)), self.G))
            print('TRAIN DATA -> View %d is projected ...' % (pid + 1))

        return self.Xtrain, self.G
Beispiel #44
0
    def read_uvfits(self,
                    filename,
                    run_check=True,
                    check_extra=True,
                    run_check_acceptability=True):
        """
        Read in data from a uvfits file.

        Args:
            filename: The uvfits file to read from.
            run_check: Option to check for the existence and proper shapes of
                parameters after reading in the file. Default is True.
            check_extra: Option to check optional parameters as well as required
                ones. Default is True.
            run_check_acceptability: Option to check acceptable range of the values of
                parameters after reading in the file. Default is True.
        """
        F = fits.open(filename)
        D = F[0]  # assumes the visibilities are in the primary hdu
        hdr = D.header.copy()
        hdunames = uvutils.fits_indexhdus(F)  # find the rest of the tables

        # astropy.io fits reader scales date according to relevant PZER0 (?)
        time0_array = D.data['DATE']
        try:
            # uvfits standard is to have 2 DATE parameters, both floats:
            # DATE (full day) and _DATE (fractional day)
            time1_array = D.data['_DATE']
            self.time_array = (time0_array.astype(np.double) +
                               time1_array.astype(np.double))
        except (KeyError):
            # cotter uvfits files have one DATE that is a double
            self.time_array = time0_array
            if np.finfo(time0_array[0]).precision < 5:
                raise ValueError('JDs in this file are not precise to '
                                 'better than a second.')
            if (np.finfo(time0_array[0]).precision > 5
                    and np.finfo(time0_array[0]).precision < 8):
                warnings.warn('The JDs in this file have sub-second '
                              'precision, but not sub-millisecond. '
                              'Use with caution.')

        self.Ntimes = len(np.unique(self.time_array))

        # if antenna arrays are present, use them. otherwise use baseline array
        try:
            # Note: uvfits antennas are 1 indexed,
            # need to subtract one to get to 0-indexed
            self.ant_1_array = np.int32(D.data.field('ANTENNA1')) - 1
            self.ant_2_array = np.int32(D.data.field('ANTENNA2')) - 1
            subarray = np.int32(D.data.field('SUBARRAY')) - 1
            # error on files with multiple subarrays
            if len(set(subarray)) > 1:
                raise ValueError('This file appears to have multiple subarray '
                                 'values; only files with one subarray are '
                                 'supported.')
        except (KeyError):
            # cannot set this to be the baseline array because it uses the
            # 256 convention, not our 2048 convention
            bl_input_array = np.int64(D.data.field('BASELINE'))

            # get antenna arrays based on uvfits baseline array
            self.ant_1_array, self.ant_2_array = \
                self.baseline_to_antnums(bl_input_array)
        # check for multi source files
        try:
            source = D.data.field('SOURCE')
            if len(set(source)) > 1:
                raise ValueError('This file has multiple sources. Only single '
                                 'source observations are supported.')
        except (KeyError):
            pass

        # get self.baseline_array using our convention
        self.baseline_array = \
            self.antnums_to_baseline(self.ant_1_array,
                                     self.ant_2_array)
        self.Nbls = len(np.unique(self.baseline_array))

        # initialize internal variables based on the antenna lists
        self.Nants_data = int(
            len(
                np.unique(self.ant_1_array.tolist() +
                          self.ant_2_array.tolist())))

        self.set_phased()
        # check if we have an spw dimension
        if hdr.pop('NAXIS') == 7:
            if hdr['NAXIS5'] > 1:
                raise ValueError('Sorry.  Files with more than one spectral' +
                                 'window (spw) are not yet supported. A ' +
                                 'great project for the interested student!')
            self.data_array = (D.data.field('DATA')[:, 0, 0, :, :, :, 0] +
                               1j * D.data.field('DATA')[:, 0, 0, :, :, :, 1])
            self.flag_array = (D.data.field('DATA')[:, 0, 0, :, :, :, 2] <= 0)
            self.nsample_array = np.abs(
                D.data.field('DATA')[:, 0, 0, :, :, :, 2])
            self.Nspws = hdr.pop('NAXIS5')
            assert (self.Nspws == self.data_array.shape[1])

            # the axis number for phase center depends on if the spw exists
            # subtract 1 to be zero-indexed
            self.spw_array = np.int32(uvutils.fits_gethduaxis(D, 5)) - 1

            self.phase_center_ra_degrees = np.float(hdr.pop('CRVAL6'))
            self.phase_center_dec_degrees = np.float(hdr.pop('CRVAL7'))
        else:
            # in many uvfits files the spw axis is left out,
            # here we put it back in so the dimensionality stays the same
            self.data_array = (D.data.field('DATA')[:, 0, 0, :, :, 0] +
                               1j * D.data.field('DATA')[:, 0, 0, :, :, 1])
            self.data_array = self.data_array[:, np.newaxis, :, :]
            self.flag_array = (D.data.field('DATA')[:, 0, 0, :, :, 2] <= 0)
            self.flag_array = self.flag_array[:, np.newaxis, :, :]
            self.nsample_array = np.abs(D.data.field('DATA')[:, 0, 0, :, :, 2])
            self.nsample_array = (self.nsample_array[:, np.newaxis, :, :])

            # the axis number for phase center depends on if the spw exists
            self.Nspws = 1
            self.spw_array = np.array([0])

            self.phase_center_ra_degrees = np.float(hdr.pop('CRVAL5'))
            self.phase_center_dec_degrees = np.float(hdr.pop('CRVAL6'))

        # get shapes
        self.Nfreqs = hdr.pop('NAXIS4')
        self.Npols = hdr.pop('NAXIS3')
        self.Nblts = hdr.pop('GCOUNT')

        # read baseline vectors in units of seconds, return in meters
        self.uvw_array = (np.array(
            np.stack(
                (D.data.field('UU'), D.data.field('VV'), D.data.field('WW'))))
                          * const.c.to('m/s').value).T

        self.freq_array = uvutils.fits_gethduaxis(D, 4)
        self.channel_width = hdr.pop('CDELT4')

        try:
            self.integration_time = float(D.data.field('INTTIM')[0])
        except (KeyError):
            if self.Ntimes > 1:
                self.integration_time = \
                    float(np.diff(np.sort(list(set(self.time_array))))
                          [0]) * 86400
            else:
                raise ValueError('integration time not specified and only '
                                 'one time present')

        self.freq_array.shape = (self.Nspws, ) + self.freq_array.shape

        self.polarization_array = np.int32(uvutils.fits_gethduaxis(D, 3))

        # other info -- not required but frequently used
        self.object_name = hdr.pop('OBJECT', None)
        self.telescope_name = hdr.pop('TELESCOP', None)
        self.instrument = hdr.pop('INSTRUME', None)
        latitude_degrees = hdr.pop('LAT', None)
        longitude_degrees = hdr.pop('LON', None)
        altitude = hdr.pop('ALT', None)
        self.x_orientation = hdr.pop('XORIENT', None)
        self.history = str(hdr.get('HISTORY', ''))
        if not uvutils.check_history_version(self.history,
                                             self.pyuvdata_version_str):
            self.history += self.pyuvdata_version_str

        while 'HISTORY' in hdr.keys():
            hdr.remove('HISTORY')

        # if 'CASAHIST' in hdr.keys():
        #    self.casa_history=hdr.pop('CASAHIST',None)
        self.vis_units = hdr.pop('BUNIT', 'UNCALIB')
        self.phase_center_epoch = hdr.pop('EPOCH', None)

        # remove standard FITS header items that are still around
        std_fits_substrings = [
            'SIMPLE', 'BITPIX', 'EXTEND', 'BLOCKED', 'GROUPS', 'PCOUNT',
            'BSCALE', 'BZERO', 'NAXIS', 'PTYPE', 'PSCAL', 'PZERO', 'CTYPE',
            'CRVAL', 'CRPIX', 'CDELT', 'CROTA', 'CUNIT', 'DATE-OBS'
        ]
        for key in hdr.keys():
            for sub in std_fits_substrings:
                if key.find(sub) > -1:
                    hdr.remove(key)

        # find all the remaining header items and keep them as extra_keywords
        for key in hdr:
            if key == 'COMMENT':
                self.extra_keywords[key] = str(hdr.get(key))
            elif key != '':
                self.extra_keywords[key] = hdr.get(key)

        # READ the antenna table
        ant_hdu = F[hdunames['AIPS AN']]

        # stuff in the header
        if self.telescope_name is None:
            self.telescope_name = ant_hdu.header['ARRNAM']

        self.gst0 = ant_hdu.header['GSTIA0']
        self.rdate = ant_hdu.header['RDATE']
        self.earth_omega = ant_hdu.header['DEGPDY']
        self.dut1 = ant_hdu.header['UT1UTC']
        try:
            self.timesys = ant_hdu.header['TIMESYS']
        except (KeyError):
            # CASA misspells this one
            self.timesys = ant_hdu.header['TIMSYS']

        try:
            xyz_telescope_frame = ant_hdu.header['FRAME']
        except (KeyError):
            warnings.warn('Required Antenna frame keyword not set, '
                          'setting to ????')
            xyz_telescope_frame = '????'

        # get telescope location and antenna positions.
        # VLA incorrectly sets ARRAYX/ARRAYY/ARRAYZ to 0, and puts array center
        # in the antenna positions themselves
        if (np.isclose(ant_hdu.header['ARRAYX'], 0)
                and np.isclose(ant_hdu.header['ARRAYY'], 0)
                and np.isclose(ant_hdu.header['ARRAYZ'], 0)):
            x_telescope = np.mean(ant_hdu.data['STABXYZ'][:, 0])
            y_telescope = np.mean(ant_hdu.data['STABXYZ'][:, 1])
            z_telescope = np.mean(ant_hdu.data['STABXYZ'][:, 2])
            self.antenna_positions = (
                ant_hdu.data.field('STABXYZ') -
                np.array([x_telescope, y_telescope, z_telescope]))

        else:
            x_telescope = ant_hdu.header['ARRAYX']
            y_telescope = ant_hdu.header['ARRAYY']
            z_telescope = ant_hdu.header['ARRAYZ']
            # AIPS memo #117 says that antenna_positions should be relative to
            # the array center, but in a rotated ECEF frame so that the x-axis
            # goes through the local meridian.
            rot_ecef_positions = ant_hdu.data.field('STABXYZ')
            latitude, longitude, altitude = \
                uvutils.LatLonAlt_from_XYZ(np.array([x_telescope, y_telescope, z_telescope]))
            self.antenna_positions = uvutils.ECEF_from_rotECEF(
                rot_ecef_positions, longitude)

        if xyz_telescope_frame == 'ITRF':
            self.telescope_location = np.array(
                [x_telescope, y_telescope, z_telescope])
        else:
            if latitude_degrees is not None and longitude_degrees is not None and altitude is not None:
                self.telescope_location_lat_lon_alt_degrees = (
                    latitude_degrees, longitude_degrees, altitude)

        # stuff in columns
        ant_names = ant_hdu.data.field('ANNAME').tolist()
        self.antenna_names = []
        for name in ant_names:
            self.antenna_names.append(name.replace('\x00!', ''))

        # subtract one to get to 0-indexed values rather than 1-indexed values
        self.antenna_numbers = ant_hdu.data.field('NOSTA') - 1

        self.Nants_telescope = len(self.antenna_numbers)

        try:
            self.antenna_diameters = ant_hdu.data.field('DIAMETER')
        except (KeyError):
            pass

        del (D)

        try:
            self.set_telescope_params()
        except ValueError, ve:
            warnings.warn(str(ve))
Beispiel #45
0
	def initHostArrays(self, res_expo):
		#Use ar_ySize below to increase the worldspace, should be a power of 2
		self.ar_ySize = np.int32(2**res_expo)
		#-------------------------------------------
		self.a = np.ones((self.ar_ySize,self.ar_ySize), dtype=np.int32)
Beispiel #46
0
    if p1.distance < 0.7 * p2.distance:
        validmatches.append(p1)

if len(validmatches) > min_count:
    src = np.float32([kp1[m.queryIdx].pt
                      for m in validmatches]).reshape(-1, 1, 2)
    dst = np.float32([kp2[m.trainIdx].pt
                      for m in validmatches]).reshape(-1, 1, 2)
    M, mask = cv2.findHomography(src, dst, cv2.RANSAC, 5.0)
    matchesMask = mask.ravel().tolist()

    h, w = img1.shape
    pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1],
                      [w - 1, 0]]).reshape(-1, 1, 2)
    destination = cv2.perspectiveTransform(pts, M)

    img2 = cv2.polylines(img2, [np.int32(dst)], True, 255, 3, cv2.LINE_AA)
else:
    print("Not enough matches are found.")
    matchesMask = None

draw = dict(matchColor=(255, 0, 0),
            singlePointColor=None,
            matchesMask=matchesMask,
            flags=2)
img3 = cv2.drawMatches(img1, kp1, img2, kp2, validmatches, None, **draw)

cv2.imshow('Cute_SYW', img3)
if cv2.waitKey(0):
    cv2.destroyAllWindows()
Beispiel #47
0
 def numpy_things(self):
     return (numpy.int32(10), numpy.int64(20), numpy.array([
         42,
     ]))
Beispiel #48
0
	def loadImg(self, seedImage):
		img = np.array(Image.open(seedImage))
		img2 = np.where(img != 0, 1, 0)
		self.a = np.int32(img2)
import numpy as np

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True)

args = vars(ap.parse_args())

image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

thresh = cv2.threshold(gray, 15, 255, cv2.THRESH_BINARY)[1]

(_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)

cv2.imshow("Thres", thresh)
print(len(cnts))
cnt = sorted(cnts, key=cv2.contourArea)[0]
(x, y, w, h) = cv2.boundingRect(cnt)
roi = gray[y:y + h, x:x + w]
zernike_mom = mahotas.features.zernike_moments(roi, 200, degree=3)
print(zernike_mom)

cv2.imshow("Thres", roi)
# cv2.drawContours(image, [cnt], -1, (255, 0, 0), -2)
box = cv2.minAreaRect(cnt)
box = np.int32(cv2.boxPoints(box))
cv2.drawContours(image, [box], -1, (0, 0, 255), 2)

cv2.imshow("Image", image)
cv2.waitKey(0)
Beispiel #50
0
 def test_int(self):
     self.assertRoundtrip(numpy.int32(42))
     self.assertRoundtrip(numpy.int64(42))
Beispiel #51
0
 def __len__(self):  #Return the number of batches in a sequence
     num_image = len(self.list_of_file_names)
     num_batches = np.int32(np.ceil(num_image / self.batch_size))
     return num_batches
Beispiel #52
0
                    x2 = dst[1][0][0]
                    y2 = dst[1][0][1]
                    x4 = dst[2][0][0]
                    y4 = dst[2][0][1]
                    x3 = dst[3][0][0]
                    y3 = dst[3][0][1]

                    #print(dst)
                    #input('wait')

                    # pts2 = [[x1+offset, y1], [x3+offset, y3], [x2+offset, y2], [x4+offset, y4]]
                    # positions = pts2
                    # positions2 = [[x1+offset, y1], [x3+offset, y3], [x4+offset, y4], [x2+offset, y2]]


                    gray2 = cv2.polylines(gray2,[np.int32(dst)],True,(0,255,0),3, cv2.LINE_AA) #poly lines
        # Draw match points with mask ---⑨
        ####res = cv2.drawMatches(gray1, kp1, gray2, kp2, good_matches, None, matchesMask=matchesMask, flags=cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS)
        res = gray2
        #--------------------------------------------------------------------------
        #height, width = res.shape[:2]
        # pts2 = np.float32(pts2)
        # h,mask = cv2.findHomography(srcPoints=pts1,dstPoints=pts2,method=cv2.RANSAC, ransacReprojThreshold=5.0)
        # height, width, channels = res.shape
        # im1Reg = cv2.warpPerspective(note, h, (width, height))
        # mask2 = np.zeros(res.shape, dtype=np.uint8)
        # roi_corners2 = np.int32(positions2)
        # channel_count2 = res.shape[2]
        # ignore_mask_color2 = (255,) * channel_count2
        # cv2.fillConvexPoly(mask2, roi_corners2, ignore_mask_color2)
        # mask2 = cv2.bitwise_not(mask2)
Beispiel #53
0
def copyElement(e, dt, buffer, offset):
    #print(f"copyElement - dt: {dt}  offset: {offset}")
    if len(dt) > 1:
        for name in dt.names:
            field_dt = dt[name]
            field_val = e[name]
            offset = copyElement(field_val, field_dt, buffer, offset)
    elif not dt.metadata or "vlen" not in dt.metadata:
        #print("e vlen: {} type: {} itemsize: {}".format(e, type(e), dt.itemsize))
        e_buf = e.tobytes()
        #print("tobytes:", e_buf)
        if len(e_buf) < dt.itemsize:
            # extend the buffer for fixed size strings
            #print("extending buffer")
            e_buf_ex = bytearray(dt.itemsize)
            for i in range(len(e_buf)):
                e_buf_ex[i] = e_buf[i]
            e_buf = bytes(e_buf_ex)

        #print("length:", len(e_buf))
        offset = copyBuffer(e_buf, buffer, offset)
    else:
        # variable length element
        vlen = dt.metadata["vlen"]
        #print("copyBuffer vlen:", vlen)
        if isinstance(e, int):
            #print("copyBuffer int")
            if e == 0:
                # write 4-byte integer 0 to buffer
                offset = copyBuffer(b'\x00\x00\x00\x00', buffer, offset)
            else:
                raise ValueError("Unexpected value: {}".format(e))
        elif isinstance(e, bytes):
            #print("copyBuffer bytes")
            count = np.int32(len(e))
            if count > MAX_VLEN_ELEMENT:
                raise ValueError("vlen element too large")
            offset = copyBuffer(count.tobytes(), buffer, offset)
            offset = copyBuffer(e, buffer, offset)
        elif isinstance(e, str):
            #print("copyBuffer, str")
            text = e.encode('utf-8')
            count = np.int32(len(text))
            if count > MAX_VLEN_ELEMENT:
                raise ValueError("vlen element too large")
            offset = copyBuffer(count.tobytes(), buffer, offset)
            offset = copyBuffer(text, buffer, offset)

        elif isinstance(e, np.ndarray):
            nElements = np.prod(e.shape)
            #print("copyBuffer ndarray, nElements:", nElements, "kind:", e.dtype.kind)

            if e.dtype.kind != 'O':
                count = np.int32(e.dtype.itemsize * nElements)
                #print("copyBuffeer got vlen count:", count)
                #print("copyBuffer e:", e)
                if count > MAX_VLEN_ELEMENT:
                    raise ValueError("vlen element too large")
                offset = copyBuffer(count.tobytes(), buffer, offset)
                #print("copyBuffer write new count, offset:", offset)
                offset = copyBuffer(e.tobytes(), buffer, offset)
                #print("copyBuffer write data, offset:", offset)
            else:
                arr1d = e.reshape((nElements,))
                for item in arr1d:
                    offset = copyElement(item, dt, buffer, offset)

        elif isinstance(e, list) or isinstance(e, tuple):
            #print("cooyBuffer list/tuple  vlen:", vlen, "e:", e)
            count = np.int32(len(e) * vlen.itemsize)
            offset = copyBuffer(count.tobytes(), buffer, offset)
            if isinstance(e, np.ndarray):
                arr = e
            else:
                arr = np.asarray(e, dtype=vlen)
            offset = copyBuffer(arr.tobytes(), buffer, offset)

        else:
            raise TypeError("unexpected type: {}".format(type(e)))
        #print("buffer: {}".format(buffer))
    return offset
    print('error opening video')

# if you don't like ugly mega while-loops, this is where i advice
# you to look at something else. This is not my proudest moment.
# viewer discretion is adviced
while(cap.isOpened()):
    ret,frame = cap.read()
    # make a copy to draw on
    final = np.array(frame)

    if ret == True:
        # set region of interest
        roi       = setROI(frame)

        # grayscale the image
        gray     = ut.grayscale(np.int32(roi)).get()
        
        # equalize the histogram. As calculating a histogram in a functional
        # setting with immutable data is fruitless, this is done in openCV.
        histEq   = cv.equalizeHist(np.array(gray, dtype=np.uint8))

        # binarize the image, keeping only those pixels that have a value
        # greater than 240. You might need to tweak this depending on what
        # setting your image is captured in.
        binarized = ut.binarization(np.int32(histEq), 240).get()

        # Now that hopefully only the lanes are left, perform canny edge
        # detection to keep only the edges of the lanes. Change the first
        # argument (std_dev) to change how well the image is blurred before
        # edges are extracted. The following two arguments are the lower and
        # upper threshold for the double thresholding. Should typically be 1:2
    print()

    if len(sys.argv) > 1:
        fn = sys.argv[1]
        print('loading %s ...' % fn)
        img = cv2.imread(fn)
        if img is None:
            print('Failed to load fn:', fn)
            sys.exit(1)

    else:
        sz = 4096
        print('generating %dx%d procedural image ...' % (sz, sz))
        img = np.zeros((sz, sz), np.uint8)
        track = np.cumsum(np.random.rand(500000, 2) - 0.5, axis=0)
        track = np.int32(track * 10 + (sz / 2, sz / 2))
        cv2.polylines(img, [track], 0, 255, 1, cv2.LINE_AA)

    small = img
    for i in xrange(3):
        small = cv2.pyrDown(small)

    def onmouse(event, x, y, flags, param):
        h, w = img.shape[:2]
        h1, w1 = small.shape[:2]
        x, y = 1.0 * x * h / h1, 1.0 * y * h / h1
        zoom = cv2.getRectSubPix(img, (800, 600), (x + 0.5, y + 0.5))
        cv2.imshow('zoom', zoom)

    cv2.imshow('preview', small)
    cv2.setMouseCallback('preview', onmouse)
random.shuffle(temp)
img_arr, img_label = zip(*temp)
img_arr = np.asarray(img_arr)
img_label = np.asarray(img_label)

train_data, test_data, train_label, test_label = train_test_split(
    img_arr, img_label, test_size=0.2, random_state=42)
train_data, val_data, train_label, val_label = train_test_split(
    train_data, train_label, test_size=0.2, random_state=42)

model = load_model(WEIGHTS_FINAL)

label_name = {0: 'Cat', 1: 'Dog'}
index = np.random.randint(1, 200, size=1, dtype=np.int32)
image = test_data[index]
image_show = array_to_img(image)
img = image.reshape(-1, 64, 64, 3)
image = img.reshape(64, 64, 3)
image = np.int32(image * 256)

print(img)
pred = model.predict(img)
print('This photo is a {}, predicted as a {}'.format(
    label_name[np.argmax(test_label[index])], label_name[np.argmax(pred)]))
plt.imshow(image)
plt.title('This photo is a {}, predicted as a {}'.format(
    label_name[np.argmax(test_label[index])], label_name[np.argmax(pred)]))
plt.xticks([])
plt.yticks([])
plt.show()
ones_like = utils.copy_docstring(
    'tf.ones_like',
    _ones_like)

pad = utils.copy_docstring(
    'tf.pad',
    _pad)

range = utils.copy_docstring(  # pylint: disable=redefined-builtin
    'tf.range',
    _range)

rank = utils.copy_docstring(
    'tf.rank',
    lambda input, name=None: np.int32(np.array(input).ndim))  # pylint: disable=redefined-builtin,g-long-lambda

repeat = utils.copy_docstring(
    'tf.repeat',
    lambda input, repeats, axis=None, name=None: np.repeat(  # pylint: disable=g-long-lambda
        input, repeats, axis=axis))

reshape = utils.copy_docstring(
    'tf.reshape',
    lambda tensor, shape, name=None: np.reshape(  # pylint: disable=g-long-lambda
        ops.convert_to_tensor(tensor), shape))

roll = utils.copy_docstring(
    'tf.roll',
    lambda input, shift, axis: np.roll(input, shift, axis))  # pylint: disable=unnecessary-lambda
                         name),
            X_test_img.squeeze()[i, :, :] * 255)
        cv2.imwrite(
            os.path.join(args.results_dir, args.exper_name, "test", "masks",
                         name),
            y_pred_test.squeeze()[i, :, :] * 255)

    # 損失関数
    if (args.train_mode in ["train"]):
        model.plot_loss(
            os.path.join(args.results_dir, args.exper_name, "losees.png"))

    # IoU
    thresholds = np.linspace(0, 1, 50)  # IoU スコアの低い結果を除外するためのスレッショルド
    ious = np.array([
        iou_metric_batch(y_valid_mask, np.int32(y_pred_train > threshold))
        for threshold in thresholds
    ])

    threshold_best_index = np.argmax(ious[9:-10]) + 9
    iou_best = ious[threshold_best_index]
    threshold_best = thresholds[threshold_best_index]  # ?
    print("iou_best = {:0.4f} ".format(iou_best))
    print("threshold_best = {:0.4f} ".format(threshold_best))

    fig, axs = plt.subplots()
    axs.plot(thresholds, ious)
    axs.plot(threshold_best, iou_best, "xr", label="Best threshold")
    plt.xlabel("Threshold")
    plt.ylabel("IoU")
    plt.title("Threshold vs IoU ({}, {})".format(threshold_best, iou_best))
Beispiel #59
0
except:
    print('Build log:')
    print(prog.get_build_info(dev, cl.program_build_info.LOG))
    raise

# Determine maximum work-group size
wg_max_size = dev.max_work_group_size
local_size = (2**np.trunc(np.log2(wg_max_size))).astype(np.int32)

# Data and device buffers
# Random integer values as float32 (same as the C-implementation)
data = np.random.randint(low=0, high=np.iinfo(np.int32).max,
                         size=NUM_FLOATS).astype(np.float32)
print('Input: ' + str(data))

direction = np.int32(0 if SORT_ASCENDING else -1)

mf = cl.mem_flags
data_buffer = cl.Buffer(context,
                        mf.READ_WRITE | mf.COPY_HOST_PTR,
                        hostbuf=data)
local_buffer = cl.LocalMemory(8 * local_size * np.dtype(np.float32).itemsize)

# Enqueue initial sorting kernel
global_size = NUM_FLOATS // 8
if global_size < local_size:
    local_size = global_size

global_size = (global_size, )
local_size = (local_size, )
Beispiel #60
0
def main():
    # Read in an image, and convert it to grayscale.
    marker_image = cv2.imread("stones.jpg", cv2.IMREAD_GRAYSCALE)
    if marker_image is None:
        print("Can't read marker image from file")
        sys.exit()
    # Optionally reduce the size of the marker image.
    h = int(0.5 * marker_image.shape[0])
    w = int(0.5 * marker_image.shape[1])
    marker_image = cv2.resize(src=marker_image, dsize=(w, h))

    # Initialize feature detector.
    detector = cv2.ORB_create(nfeatures=2500,  # default = 500
                              edgeThreshold=16)  # default = 31

    # Detect keypoints in marker image and compute descriptors.
    kp1, desc1 = detector.detectAndCompute(image=marker_image, mask=None)

    # Initialize image capture from camera.
    video_capture = cv2.VideoCapture(CAMERA_NUMBER)  # Open video capture object
    is_ok, bgr_image_input = video_capture.read()  # Make sure we can read video
    if not is_ok:
        print("Cannot read video source")
        sys.exit()

    while True:
        is_ok, bgr_image_input = video_capture.read()
        if not is_ok:
            break  # no camera, or reached end of video file

        # Set this to true if at any point in the processing we can't find the marker.
        unable_to_find_marker = False

        # Detect keypoints and compute descriptors for input image.
        image_input_gray = cv2.cvtColor(src=bgr_image_input, code=cv2.COLOR_BGR2GRAY)
        kp2, desc2 = detector.detectAndCompute(image=image_input_gray, mask=None)
        if len(kp2) < MIN_MATCHES_FOR_DETECTION:  # Higher threshold - fewer false detections
            unable_to_find_marker = True

        # Match descriptors to marker image.
        if not unable_to_find_marker:
            matcher = cv2.BFMatcher_create(normType=cv2.NORM_L2, crossCheck=False)
            matches = matcher.knnMatch(desc1, desc2, k=2)  # Find closest 2
            good_matches = []
            for m in matches:
                if m[0].distance < 0.8 * m[1].distance:  # Ratio test
                    good_matches.append(m[0])
            if len(good_matches) < MIN_MATCHES_FOR_DETECTION:  # Higher threshold - fewer false detections
                unable_to_find_marker = True

        # Fit homography.
        if not unable_to_find_marker:
            src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1, 2)
            dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1, 2)

            Hmat, mask = cv2.findHomography(
                srcPoints=src_pts, dstPoints=dst_pts, method=cv2.RANSAC,
                ransacReprojThreshold=5.0,  # default is 3.0
                maxIters=2000  # default is 2000
            )
            num_inliers = sum(mask)  # mask[i] is 1 if point i is an inlier, else 0
            if num_inliers < MIN_MATCHES_FOR_DETECTION:
                unable_to_find_marker = True

        # Draw marker border on the image.
        if not unable_to_find_marker:
            # Project the marker border lines to the image using the computed homography.
            h, w = marker_image.shape
            marker_corners = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]])
            warped_corners = cv2.perspectiveTransform(marker_corners.reshape(-1, 1, 2), Hmat)

            cv2.polylines(img=bgr_image_input, pts=[np.int32(warped_corners)], isClosed=True,
                          color=[0, 255, 0], thickness=4, lineType=cv2.LINE_AA)

        # Show image and wait for xx msec (0 = wait till keypress).
        cv2.imshow("Input image", bgr_image_input)
        key_pressed = cv2.waitKey(0) & 0xFF
        if key_pressed == 27 or key_pressed == ord('q'):
            break  # Quit on ESC or q

    video_capture.release()
    cv2.destroyAllWindows()