Ejemplo n.º 1
0
def closest_above_thresh(SEcat, thing, center, coords, threshold=50., k=10):
    '''
    Find an object that is closest to the center of the image that also meets
    a minimum threshold value for some desired quantity (thing)
    Returns information about that object: 
       Distance to the center
       Coordinates of the object in the image
       Index of the object in the SE catalog
       Thing value that meets the set threshold

    For example: find galaxy closest to center that has a pixel area 
                 larger than fifty pixels**2

    Also returns a flag if the closest object does NOT meet the threshold
    '''
    Flag = 0

    indexes, distances = find_closest(center, coords, k)
    idxs, dists = indexes[distances != np.inf], distances[distances != np.inf]
    things = SEcat[thing][idxs]
    if np.any(np.array(things) > threshold):
        Dist = np.min(dists[things > threshold])
        Index = idxs[dists==Dist][0]
        Coord = coords[Index]
        Thing = SEcat[thing][Index]
        if Thing != things[0]:
            Flag = 1
    else:
        Dist = dists[0]
        Index = idxs[0]
        Coord = coords[Index]
        Thing = things[0]
        Flag = 1
    return Dist, Index, Coord, Thing, Flag
Ejemplo n.º 2
0
def hunting():
    keyboard.press('d')
    monster = Monster()
    hero = Hero()
    buttons = utils.Custom_Button()
    hero.update_health()
    print(f"heros mana is {hero.health}")
    if hero.health <= 50:
        print('heal')
        hero.heal()
        return
    hero.update_mana()
    print(f"heros mana is {hero.mana}")
    if hero.mana <= 25:
        print('regening mana')
        hero.regen_mana()
        return
    time.sleep(0.15)
    hero.update_alive()
    distance = utils.find_closest()

    print(distance)
    keyboard.release('d')
    buttons.TAB()
    time.sleep(0.15)
    monster.update_found()
    print(monster.found)
    # if monster found
    if monster.found:
        # check if it close for shot
        time.sleep(0.3)
        in_range = utils.monster_in_range()
        print('checking in range')
        if in_range:
            print('starting attack')
            time.sleep(0.3)

            while True:
                monster.update_alive()
                if monster.alive == False:
                    break
                attacking()

                monster.update_damaged()
                print(f"mosnter is damaged: {monster.damaged}")
                if monster.damaged == False:
                    return
            print('monster killed')
            time.sleep(0.3)
            # monster.pick_up()
            time.sleep(0.3)

            # Check health if low heal if heal on coldown sit until hp is 75
            #

        else:
            return
    else:
        return
Ejemplo n.º 3
0
 def maintain(self):
     own_cells = self.summary[self.game.uid]['all']
     own_cells.sort(key=take_time)
     for oc in own_cells:
         if oc.takeTime > SpiderBot.MAINTENANCE_TIME_THRESHOLD:
             return False
         cell, dist = find_closest(
             oc, self.game, lambda c: c and (c.owner != self.game.uid))
Ejemplo n.º 4
0
    def calculate_heuristic_for_action(self, cost, sim=HurricaneSimulator()):
        """
        This function calculates the heuristic for a single.
        :param vertex: the vertex to which you wish to calculate the heuristic
        :param sim: the environment
        :return: a heuristic value
        """
        number_of_people_in_towns = sim.get_number_of_people_in_towns()
        people_unable_to_save = number_of_people_in_towns + self.people_in_vehicle
        cost_to_people = self.find_people(
            sim)  # Find costs for all vertices with people
        cost_to_shelter = self.find_sheleter(
            sim)  # Find costs for all shelter vertices

        if len(cost_to_shelter) == 0:
            """ No shelter, we are all doomed :("""
            return cost + people_unable_to_save * PEOPLE_UNSAVED_VALUE
        """ Find closest shelter """
        closest_shelter = find_closest(cost_to_shelter)[1]
        if (1 + sim.K * self.people_in_vehicle
            ) * closest_shelter + sim.get_time() < sim.deadline:
            """ People in vehicle can be saved """
            people_unable_to_save -= self.people_in_vehicle
        else:
            """ Unable to reach shelter """
            return cost + people_unable_to_save * PEOPLE_UNSAVED_VALUE
        """ Find people in towns """
        shelter_nodes = sim.get_shelter()
        time_elapsed = sim.get_time()
        for people in cost_to_people:
            path_to_shelter_for_person = find_closest(
                self.search_grid(shelter_nodes, people[2], sim))
            time_elapsed_for_person = time_elapsed + people[1] + \
                                      (1 + sim.K * sim.graph[people[2]][people[2]].num_of_people) \
                                      * path_to_shelter_for_person[1]
            if time_elapsed_for_person < sim.deadline:
                people_unable_to_save -= sim.graph[people[2]][
                    people[2]].num_of_people
        return cost + people_unable_to_save * PEOPLE_UNSAVED_VALUE
Ejemplo n.º 5
0
def forecast_similar_hours(df, forecast, day):
    counts = {}
    # for each period ...
    for index, row in forecast.loc[day.strftime('%Y-%m-%d')].iterrows():
        print('Forecasting: {}'.format(index))
        # set to zero if its at night.
        if row['zenith'] > 87:
            print('Zero at night')
            forecast.loc[index, 'prediction'] = 0.0
            forecast.loc[index, 'probability'] = 1.0
        else:
            #    find all periods with sun2, temp2, zenith in given thresholds
            thresholds = {'sun2': 0.05, 'temp2': 0.1, 'zenith': 0.1}
            prediction = utils.find_closest(row, df, thresholds, 'pv_power')
            #    set power based on mean, and probability based on the others
            forecast.loc[index, 'prediction'] = prediction['mean']
            forecast.loc[index, 'probability'] = prediction['sd']
            counts[index] = prediction['n']
    print(forecast)
    print(counts)
Ejemplo n.º 6
0
def clean_frame(image, outdir, sep=17., survey='SDSS'):
    '''
    This is a multi-stage cleaning process for each galaxy cutout.

    ---------------------------------------------------------------
    INPUTS: 
    ---------------------------------------------------------------
    
    image -- Name of galaxy cutout on which to perform cleaning. 
          Galaxy must be in the center of the image! 
          Image does not have to be square.

    outdir -- Name of the directory to which SExtractor output should
          be directed. 

    sep -- Minimum separation distance (in pixels) between:
          1. obj in BRIGHT and center of image
          2. obj in FAINT and center of image
          3. obj in FAINT and obj in BRIGHT
    ---------------------------------------------------------------
    OUTPUTS:
    ---------------------------------------------------------------
    All kinds of SExtractor nonsense

    ---------------------------------------------------------------
    RETURNS:
    ---------------------------------------------------------------
    Multiple flags to assess the quality of the cleaning. After cleaning, 
    SExtractor is run again in SMOOTH mode and various flags are set 
    depending upon:
    
    oFlag -- Flag set if no obj is detected in the center of the image
    
    uFlag -- Flag set if large obj are detected far from the center

    bFlag -- Set if a bright source is detected 

    category -- This number designates which particular course image
          took through the cleaning process
    ---------------------------------------------------------------
    PROCESS:
    ---------------------------------------------------------------
    Initially, SExtractor runs with both BRIGHT and FAINT parameters.
    Based on the resulting segmentation maps, the distance of the object 
    closest to the center in each map is determined (Fdist and Bdist). 
    The distance between these two objects is also calculated (DIST). 
    Based on the possible combinations of these values, various cleanings
    are performed. 
    #'''

    # initialize some shit for laterz
    category = 0
    Barea, B2dist = 0., 0.

    # commonly used SE keywords
    num = 'NUMBER'
    area = 'ISOAREA_IMAGE'
    x, y = 'X_IMAGE', 'Y_IMAGE'
    flux = 'FLUX_AUTO'

    if survey == "SDSS":
        configfile = 'se_params_SDSS.cfg'
    elif survey == 'COSMOS':
        configfile = 'se_params_COSMOS.cfg'


    basename = os.path.basename(os.path.splitext(image)[0])
    outname = outdir+basename
    catnames = [outname+'_bright_cat.fits', outname+'_faint_cat.fits', 
                outname+'_smooth_cat.fits']
    segnames = [outname+'_bright_seg.fits', outname+'_faint_seg.fits',
                outname+'_smooth_seg.fits']

    # run SE in FAINT and BRIGHT modes
    if not run_sextractor.run_SE(image, 'BRIGHT', cfg_filename=configfile, 
                                 outdir=outdir):
        return [9, 9, 9, 9]

    run_sextractor.run_SE(image, 'FAINT', cfg_filename=configfile, 
                          outdir=outdir)

    # READ IN ORIG FITS-FILE and both Bright/Faint SEGMAPs
    img, ihdr = fits.getdata(image, header=True)
    cln = img.copy()
    bseg, fseg = fits.getdata(segnames[0]), fits.getdata(segnames[1]) 
    bcat, fcat = fits.getdata(catnames[0]), fits.getdata(catnames[1])

    center = [img.shape[0]/2., img.shape[1]/2.]

    # check to see if ANYTHING is found ANYWHERE
    if len(bcat) == 0 and len(fcat) == 0:
        return [9,9,9,9]

    brightdist, bFlag = 0., 0
    # Test for at least ONE object in BRIGHT catalog
    if len(bcat) > 0:
        # find object in BRIGHT closest to center
        BIndex, Bdist = find_closest(center,zip(bcat[x],bcat[y]))
        BCoord = zip(bcat[x], bcat[y])[BIndex]

        # If more than one obj, determine if bright source nearby
        if (len(bcat) > 1) and (BIndex != 0):
            if np.any(bcat[flux] > 1000.):
                bx, by = bcat[x][0], bcat[y][0]
                brightdist = np.sqrt((bx-center[0])**2+(by-center[0])**2)
                if brightdist < 150.:
                    bFlag = 1        


    # If nothing found in BRIGHT, assign Bdist to edge of image (251.)
    else: 
        Bdist = center[0]

    # Find closest object in FAINT
    FIndex, Fdist = find_closest(center, zip(fcat[x], fcat[y]))

    DIST = np.abs(Fdist - Bdist)

    if (DIST <= sep):
        # FLAG 1: MOST COMMON CATEGORY --> CLEAN IN FAINT MODE
        if (Bdist <= sep) & (Fdist <= sep):
            #cln = clean_image(cln, bseg, bcat, BIndex, fseg)
            cln = clean_image(cln, fseg, fcat, FIndex, fseg)
            category, mode = 1, 'FAINT'

        # FLAG 2: CLEAN IN BRIGHT MODE & FLAG THESE!!
        elif (Bdist <= sep) & (Fdist > sep):
            ''' There is only one obj in here:
            Two super bright galaxies super close together
            Seen as distinct objs in BRIGHT (but with crazy square edges)
            Seen as one blended obj in FAINT
            JUST FLAG THIS BITCH
            9/6/15 -- These statements ^^^ appear only to be true for COSMOS 
            data. Going to clean in Faint mode instead for SDSS
            '''
            #cln = clean_image(cln, bseg, bcat, BIndex, fseg)
            cln = clean_image(cln, fseg, fcat, FIndex, fseg)
            category, mode = 2, 'FAINT'

        # FLAG 3: CLEAN IN FAINT MODE
        elif (Bdist > sep) & (Fdist <= sep):
            ''' There aren't many of these
            They're oddballs but most are well cleaned in FAINT
            '''
            cln = clean_image(cln, fseg, fcat, FIndex, fseg)
            category, mode = 3, 'FAINT'

        # FLAG 4: TWO STAGE CLEANING -- BRIGHT --> RUN SE AGAIN IN FAINT
        elif (Bdist > sep) & (Fdist > sep): 

            cln = clean_image(cln, bseg, bcat, BIndex, fseg)

            cln_sv = cln.copy()
            cln_sv = fits.ImageHDU(data=cln_sv, name='MID_CLN')
            cln_sv.writeto(outname+'_mid_cln.fits', output_verify='silentfix', 
                           clobber=True)

            # run SE again in FAINT
            run_sextractor.run_SE(outname+'_mid_cln.fits', 'FAINT', 
                                  cfg_filename=configfile, 
                                  outdir=outdir, outstr2='run2')
            f2seg = fits.getdata(outname+'_mid_cln_faint_run2_seg.fits')
            f2cat = fits.getdata(outname+'_mid_cln_faint_run2_cat.fits')
            coords = zip(f2cat[x], f2cat[y])

            # find closest obj to center with area above threshold
            Fdist, FIndex, FCoord, Farea, aFlag = \
                        closest_above_thresh(f2cat, area, center, coords, k=5)
            cln = clean_image(cln, f2seg, f2cat, FIndex, f2seg)
            category, mode = 4, 'FAINT2'
            
    else:
        # FLAG 5: TWO STAGE CLEANING - BRIGHT --> RUN SE AGAIN IN FAINT
        if (Bdist <= sep) & (Fdist > sep):

            cln = clean_image(cln, bseg, bcat, BIndex, fseg)

            #save this image so that I can run SE on it
            cln_sv = cln.copy()
            cln_sv = fits.ImageHDU(data=cln_sv, name='MID_CLN')
            cln_sv.writeto(outname+'_mid_cln.fits', output_verify='silentfix',
                           clobber=True)
            
            # run SE again in FAINT
            run_sextractor.run_SE(outname+'_mid_cln.fits', 'FAINT', 
                                  cfg_filename=configfile, 
                                  outdir=outdir, outstr2='run2')
            f2seg = fits.getdata(outname+'_mid_cln_faint_run2_seg.fits')
            f2cat = fits.getdata(outname+'_mid_cln_faint_run2_cat.fits')
            coords = zip(f2cat[x], f2cat[y])

            # find closest obj to center with area above threshold
            Fdist, FIndex, FCoord, Farea, aFlag = \
                        closest_above_thresh(f2cat, area, center, coords, k=5)
            cln = clean_image(cln, f2seg, f2cat, FIndex, f2seg)
            category, mode = 5, 'FAINT2'
 
        # FLAG 6: CLEAN IN SMOOTH MODE
        elif (Bdist > sep) & (Fdist <= sep):
            ''' These are mostly faint objects not detected in BRIGHT
            run SE in SMOOTH mode and then clean
            '''
            run_sextractor.run_SE(image, 'SMOOTH', cfg_filename=configfile, 
                                  outdir=outdir)
            sseg, scat = fits.getdata(segnames[2]), fits.getdata(catnames[2])
            SIndex, Sdist = find_closest(center, zip(scat[x], scat[y]))

            cln = clean_image(cln, sseg, scat, SIndex, sseg)
            category, mode = 6, 'SMOOTH'

        # FLAG 7: CLEAN IN FAINT MODE -- ALL GARBAGE ANYWAY
        elif (Bdist > sep) & (Fdist > sep):
            ''' this is mostly a garbage bin of crap 
            any object in here needs to be flagged and is likely not a true
            galaxy at all!
            '''
            cln = clean_image(cln, fseg, fcat, FIndex, fseg)
            category, mode = 7, 'FAINT'

        # FLAG 8: 
        elif  (Bdist <= sep) & (Fdist <= sep):
            pdb.set_trace()
            cln = clean_image(cln, bseg, bcat, BIndex, fseg)
            category, mode = 8, 'BRIGHT'

    # Save all major data products
    if mode == 'BRIGHT':
        datacube = savedata(mode, ihdr, BIndex, data=[bseg, fseg, bcat], 
                            names=['BSEG', 'FSEG', 'CAT'])

    elif mode == 'FAINT':
        datacube = savedata(mode, ihdr, FIndex, data=[bseg, fseg, fcat], 
                            names=['BSEG', 'FSEG', 'CAT'])

    elif mode == 'FAINT2':
        datacube = savedata('TWOSTAGE', ihdr, FIndex, 
                            data=[bseg, fseg, f2seg, f2cat], 
                            names=['BSEG', 'FSEG', 'F2SEG', 'CAT'])
        datacube.insert(0, cln_sv)

    elif mode == 'SMOOTH':
        datacube = savedata(mode, ihdr, SIndex, data=[bseg, fseg, sseg, scat], 
                            names=['BSEG', 'FSEG', 'SSEG', 'CAT'])

        
    # SAVE ALL PRODUCTS TO DATA CUBE
    datacube.insert(0, fits.ImageHDU(data=cln, header=ihdr, name='CLN'))
    datacube.insert(1, fits.ImageHDU(data=img, header=ihdr, name='ORG'))

    newthing = fits.HDUList()
    for thing in datacube: 
        newthing.append(thing)
    newthing.update_extend()
    newthing.writeto(outdir+'f_'+basename+'.fits', output_verify='silentfix', 
                     clobber=True)


    # Now that we've done the cleaning -- Let's test it!    
    run_sextractor.run_SE(outdir+'f_'+basename+'.fits', 'SMOOTH', 
                          cfg_filename=configfile, outdir=outdir, 
                          outstr2='test')
    tseg = fits.getdata(outdir+'f_'+basename+'_smooth_test_seg.fits')
    tcat = fits.getdata(outdir+'f_'+basename+'_smooth_test_cat.fits')

    # If we find obj near the center is too small then we overcleaned it
    uFlag, oFlag = 0, 0
    if tcat.size:
        coords = zip(tcat[x], tcat[y])
        index, dist = find_closest(center, coords, k=10)
        objarea = tcat[area][index[0]]
        areas = tcat[area][np.where(tcat[area] != objarea)]

        if (dist[0] < sep): 
            if (objarea < 50.): 
                print 'OVERCLEANED!!!'
                oFlag = 1

            # If we find large objs far from the center- didn't clean enough
            if np.any(areas > 200.):       
                print 'UNDER CLEANED!!'
                uFlag = 1
                
                cln = clean_image(cln, tseg, tcat, index[0], tseg)
                data = fits.open(outdir+'f_'+basename+'.fits')
                data.insert(0,fits.ImageHDU(data=cln, name='UCLN'))
                data['UCLN'].header.set('SECATIDX', index[0], 'Index in SECAT')
                data['CAT'].data = tcat
                data['FSEG'].data = tseg
                data.writeto(outdir+'f_'+basename+'.fits', clobber=True,
                             output_verify='silentfix')
        else:
            oFlag = 1
    else:
        # if we don't find anything in the center anymore, something is wrong
        # either overcleaned or blended into nearby object
        oFlag = 1

    # I want to save the cleaned image separate -- 9/5/15 
    # get rid of this later, probably?
    cleanedup = fits.ImageHDU(data=cln, header=ihdr, name='CLN%i'%category)
    cleanedup.writeto(outdir+'f_'+basename+'_clnonly.fits', 
                      output_verify='silentfix', clobber=True)

    # clean up directory
    clean_directory(outdir) # doesn't work?! Harrumph.


    #FIndex, Fdist, Bdist, DIST, Farea, Barea,
    return  [category, oFlag, uFlag, bFlag]
Ejemplo n.º 7
0
    def fix_broken_connection(self):
        nb_added = 0
        random_generator = np.random.default_rng()
        while not (self._is_connected()):
            connected_components = [
                list(self.sensors.subgraph(component).nodes)
                for component in nx.connected_components(self.sensors)
            ]
            logging.debug("Neighborhood 2 : {} connected components".format(
                len(list(connected_components))))
            neighbors00 = self._find_neighbors(0, distance=self._rcom)
            found_component = False
            for neighbor in neighbors00:
                if neighbor in self.sensors.nodes:
                    component_with_00 = list(
                        nx.node_connected_component(self.sensors, neighbor))
                    found_component = True
                    break
            if not found_component:
                self.add_sensor(neighbors00[0])
                component_with_00 = list(
                    nx.node_connected_component(self.sensors, neighbors00[0]))

            # Choose the closest component X to the component Y containing (0,0)
            random_element_connected_to_00 = random_generator.choice(
                component_with_00)
            smallest_distance_to_random_element = np.inf
            closest_component = None
            for component in connected_components:
                if random_element_connected_to_00 in component:
                    continue
                for node in component:
                    distance_to_random_element = self._distance_ind(
                        node, random_element_connected_to_00)
                    if distance_to_random_element < smallest_distance_to_random_element:
                        smallest_distance_to_random_element = distance_to_random_element
                        closest_component = component
            closest_component = list(closest_component)

            # Choose the closest node y0 to a random x of X, in the component (0,0)
            random_element_component = random_generator.choice(
                closest_component)
            smallest_distance_to_component = np.inf
            closest_node = 0
            for node in component_with_00:
                distance_to_component = self._distance_ind(
                    node, random_element_component)
                if distance_to_component < smallest_distance_to_component:
                    smallest_distance_to_component = distance_to_component
                    closest_node = node

            # Choose the closest node to y0, in the component X
            smallest_distance_to_closest_node = smallest_distance_to_component
            closest_node_in_component = random_element_component
            for node in component:
                distance_to_closest_node = self._distance_ind(
                    node, closest_node)
                if distance_to_closest_node < smallest_distance_to_closest_node:
                    smallest_distance_to_closest_node = distance_to_closest_node
                    closest_node_in_component = node

            barycenter = utils.compute_barycenter(
                [[closest_node, self._data[closest_node]],
                 [
                     closest_node_in_component,
                     self._data[closest_node_in_component]
                 ]])
            closest_target = utils.find_closest(barycenter,
                                                list(self._data.items()))
            self.add_sensor_close_to_target(closest_target[0])
            nb_added += 1

        return nb_added
Ejemplo n.º 8
0
def evaluate_any_file():
    #os.system(scp )
    filepath = '../../original/processed_data/'
    weightpath = '../../scratch/bd_lstm/trainstats/weights_middle.pth'
    demoweights = '../../scratch/bd_lstm/trainstats/demoweights.pth'
    weightpath = demoweights
    parampath = '../../code/bdrnn/conf_model.cfg'
    filenamepath = '../../scratch/bd_lstm/filenames/testfiles.txt'
    minmaxdatapath = '../../original/minmaxdata/'

    #get best file
    filenames = read_names(filenamepath)
    print(len(filenames))
    filenamedict = make_dict(filenames)
    velocity = float(
        input(
            'Give rotational velocity between 4Hz and 18Hz and the closest one is used at evaluation.\n'
        ))
    filename, velocity = find_closest(filenamedict, velocity)
    files = [filename]

    #read parameters
    params = read_params(parampath)

    #init dataset with the file we selected and model
    dataset = DataSet(root_dir=filepath,
                      files=files,
                      normalize=False,
                      seq_len=params['slice_size'],
                      stride=1000)

    loader = DataLoader(dataset,
                        batch_size=int(params['batch_size']),
                        shuffle=True)

    model = LSTM_layers(input_size=int(params['input_size']),
                        hidden_size=int(params['hidden_size']),
                        num_layers=int(params['n_layers']),
                        dropout=float(params['dropout']),
                        output_size=int(params['output_size']),
                        batch_first=True,
                        bidirectional=True)
    #RuntimeError: Attempting to deserialize object on a
    #CUDA device but torch.cuda.is_available() is False.
    #If you are running on a CPU-only machine,
    #please use torch.load with map_location='cpu' to map your storages to the CPU.

    model.load_state_dict(torch.load(weightpath, map_location='cpu'))
    model.to(device)
    model.eval()
    losses = []

    for idx, sample in enumerate(loader):
        y = sample[:, :, :2].clone().detach().requires_grad_(True).to(device)
        x = sample[:, :, 2:].clone().detach().requires_grad_(True).to(device)
        h0 = model.init_hidden(int(params['batch_size']), None).to(device)
        c0 = model.init_cell(int(params['batch_size'])).to(device)

        #compute
        output = model.forward(x, (h0, c0))
        loss = F.mse_loss(output, y)
        losses.append(loss.item())

        output, y = scale_seqs(output, y, filename, minmaxdatapath)

        if (idx % 3) == 0:
            save_this_plot(0, 2763, output[0], y[0], loss.item(), velocity)
    print("Avg loss:", np.mean(losses))
Ejemplo n.º 9
0
def find_closest_image_to_gradcam(emotion_id, gradcams, id_to_img_dict):
    mean_gradcam = get_mean_cam(gradcams[emotion_id - 1])
    res, res_i = find_closest(gradcams[emotion_id - 1], mean_gradcam)
    res_image = np.reshape(id_to_img_dict[emotion_id, res_i], (224, 224))
    return res_image, res
Ejemplo n.º 10
0
import cv2
import keras
import numpy as np
from utils import load_image, find_closest

IMG_SIZE = (32, 32)

model = keras.models.load_model('./checkpoints/dae-model-01.hdf5')

image1 = load_image("../dataset/image0.jpg",IMG_SIZE)
image2 = load_image("../dataset/image1.jpg",IMG_SIZE)
image3 = load_image("../dataset/image2.png",IMG_SIZE)
image4 = load_image("../dataset/image3.jpg",IMG_SIZE)

bottleneck = keras.models.Model(
    model.input, model.get_layer('bottleneck').output)

bottleneck_value1 = bottleneck.predict(image1)
bottleneck_value2 = bottleneck.predict(image2)
bottleneck_value3 = bottleneck.predict(image3)
bottleneck_value4 = bottleneck.predict(image4)

find_closest([bottleneck_value1,bottleneck_value3,bottleneck_value2,bottleneck_value4])
Ejemplo n.º 11
0
def waterfall():
    filepath = '../../original/processed_data/'
    minmaxdatapath = '../../original/minmaxdata/'
    filenamepath = '../../scratch/bd_lstm/filenames/testfiles.txt'
    weightpath = '../../scratch/bd_lstm/trainstats/weights_middle.pth'
    parampath = './conf_model.cfg'

    filenames = read_names(filenamepath)

    filenamedict = make_dict(filenames)

    vels = ascendingorder_wf(filenames)
    num_files = len(vels)

    params = read_params(parampath)
    model = LSTM_layers(input_size=int(params['input_size']),
                        hidden_size=int(params['hidden_size']),
                        num_layers=int(params['n_layers']),
                        dropout=float(params['dropout']),
                        output_size=int(params['output_size']),
                        batch_first=True,
                        bidirectional=True)

    model.load_state_dict(torch.load(weightpath, map_location='cpu'))
    model.to(device)
    model.eval()
    arr = None
    hack_idx = 0
    for velocity in vels:
        filename, velocity = find_closest(filenamedict, velocity)

        files = [filename]
        dataset = DataSet(root_dir=filepath,
                          files=files,
                          normalize=False,
                          seq_len=seq_len,
                          stride=max_stride)
        loader = DataLoader(dataset,
                            batch_size=int(params['batch_size']),
                            shuffle=True)
        for idx, sample in enumerate(loader):
            y = sample[:, :, :2].clone().detach().requires_grad_(True).to(
                device)
            x = sample[:, :,
                       2:].clone().detach().requires_grad_(True).to(device)
            h0 = model.init_hidden(int(params['batch_size']), None).to(device)
            c0 = model.init_cell(int(params['batch_size'])).to(device)

            #compute
            output = model.forward(x, (h0, c0))
            frq_pred, Y_pred, frq_true, Y_true = fft(output, y, velocity,
                                                     seq_len, filename,
                                                     minmaxdatapath)
            vel_pred = np.ones(len(frq_pred)) * velocity
            break
        if hack_idx == 0:
            arr_pred = np.vstack((vel_pred, frq_pred, Y_pred))
            arr_true = np.vstack((vel_pred, frq_true, Y_true))
        else:
            arr2_pred = np.vstack((vel_pred, frq_pred, Y_pred))
            arr2_true = np.vstack((vel_pred, frq_true, Y_true))
            arr_pred = np.hstack((arr_pred, arr2_pred))
            arr_true = np.hstack((arr_true, arr2_true))
        if hack_idx > limit:
            break
        else:
            hack_idx += 1
        print(velocity, hack_idx, '/', num_files)
    return arr_pred, arr_true