Ejemplo n.º 1
0
def transform(image, boxes, labels, dims=(300, 300), do_flip=True, mean=None):
    if mean is None:
        mean = [0.485, 0.456, 0.406]

    new_image = image
    new_boxes = boxes
    new_labels = labels

    new_image = photometric_distort(new_image)

    new_image = to_tensor(new_image)

    if rand_random() < 0.5:
        new_image, new_boxes = expand(new_image, boxes, filler=mean)
    new_image, new_boxes, new_labels = random_crop(new_image, new_boxes,
                                                   new_labels)

    new_image = to_pil_image(new_image)

    if do_flip and rand_random() < 0.5:
        new_image, new_boxes = flip(new_image, new_boxes)

    new_image, new_boxes = resize_and_maybe_transform_coordinates(new_image,
                                                                  new_boxes,
                                                                  dims=dims)

    return new_image, new_boxes, new_labels
Ejemplo n.º 2
0
def randomFlipCoin(p):
   """ Returns True with the *p* probability. If the *p* is 1.0,
   the function will always return True, or if is 0.0, the
   function will return always False.
   
   Example:
      >>> Util.randomFlipCoin(1.0)
      True

   :param p: probability, between 0.0 and 1.0
   :rtype: True or False

   """
   if p == 1.0: return True
   if p == 0.0: return False

   return True if rand_random() <= p else False
Ejemplo n.º 3
0
def randomFlipCoin(p):
    """ Returns True with the *p* probability. If the *p* is 1.0,
   the function will always return True, or if is 0.0, the
   function will return always False.
   
   Example:
      >>> Util.randomFlipCoin(1.0)
      True

   :param p: probability, between 0.0 and 1.0
   :rtype: True or False

   """
    if p == 1.0: return True
    if p == 0.0: return False

    return rand_random() <= p
Ejemplo n.º 4
0
def photometric_distort(image):
    new_image = image

    distortions = [
        adjust_brightness, adjust_contrast, adjust_saturation, adjust_hue
    ]
    rand_shuffle(distortions)

    for distortion in distortions:
        if rand_random() < 0.5:
            if distortion.__name__ is 'adjust_hue':
                adjust_factor = rand_uniform(-18 / 255., 18 / 255.)
            else:
                adjust_factor = rand_uniform(0.5, 1.5)

            new_image = distortion(new_image, adjust_factor)

    return new_image
Ejemplo n.º 5
0
def G1DListCrossoverRealSBX(genome, **args):
   """ Experimental SBX Implementation - Follows the implementation in NSGA-II (Deb, et.al)

   Some implementation `reference <http://vision.ucsd.edu/~sagarwal/icannga.pdf>`_.

   .. warning:: This crossover method is Data Type Dependent, which means that
                must be used for 1D genome of real values.
   """
   EPS = Consts.CDefG1DListSBXEPS
   # Crossover distribution index
   eta_c = Consts.CDefG1DListSBXEtac  

   gMom = args["mom"]
   gDad = args["dad"]

   # Get the variable bounds ('gDad' could have been used; but I love Mom:-))
   lb = gMom.getParam("rangemin", Consts.CDefRangeMin)
   ub = gMom.getParam("rangemax", Consts.CDefRangeMax)

   sister = gMom.clone()
   brother = gDad.clone()

   sister.resetStats()
   brother.resetStats()

   for i in range(0,len(gMom)):

      if math.fabs(gMom[i]-gDad[i]) > EPS:
         if gMom[i] > gDad[i]:
            #swap
            temp = gMom[i]
            gMom[i] = gDad[i]
            gDad[i] = temp

         #random number betwn. 0 & 1
         u = rand_random() 
      
         beta = 1.0 + 2*(gMom[i] - lb)/(1.0*(gDad[i]-gMom[i]))
         alpha = 2.0 - beta**(-(eta_c+1.0))

         if u <= (1.0/alpha):
            beta_q = (u*alpha)**(1.0/((eta_c + 1.0)*1.0))
         else:
            beta_q = (1.0/(2.0-u*alpha))**(1.0/(1.0*(eta_c + 1.0)))

         brother[i] = 0.5*((gMom[i] + gDad[i]) - beta_q*(gDad[i]-gMom[i]))

         beta = 1.0 + 2.0*(ub - gDad[i])/(1.0*(gDad[i]-gMom[i]))
         alpha = 2.0 - beta**(-(eta_c+1.0))

         if u <= (1.0/alpha):
            beta_q = (u*alpha)**(1.0/((eta_c + 1)*1.0))
         else:
            beta_q = (1.0/(2.0-u*alpha))**(1.0/(1.0*(eta_c + 1.0)))

         sister[i] = 0.5*((gMom[i] + gDad[i]) + beta_q*(gDad[i]-gMom[i]))


         if brother[i] > ub: brother[i] = ub
         if brother[i] < lb: brother[i] = lb

         if sister[i] > ub: sister[i] = ub
         if sister[i] < lb: sister[i] = lb

         if rand_random() > 0.5:
            # Swap
            temp = sister[i]
            sister[i] = brother[i]
            brother[i] = temp
      else:
         sister[i] = gMom[i]
         brother[i] = gDad[i]

   return (sister, brother)
Ejemplo n.º 6
0
def G1DListCrossoverRealSBX(genome, **args):
    """ Experimental SBX Implementation - Follows the implementation in NSGA-II (Deb, et.al)

    Some implementation `reference <http://vision.ucsd.edu/~sagarwal/icannga.pdf>`_.
    And another reference to the `Simulated Binary Crossover
    <http://www.mitpressjournals.org/doi/abs/10.1162/106365601750190406>`_.

    .. warning:: This crossover method is Data Type Dependent, which means that
                 must be used for 1D genome of real values.
    """
    from . import Consts

    EPS = Consts.CDefG1DListSBXEPS
    # Crossover distribution index
    eta_c = Consts.CDefG1DListSBXEtac

    gMom = args["mom"]
    gDad = args["dad"]

    # Get the variable bounds ('gDad' could have been used; but I love Mom:-))
    lb = gMom.getParam("rangemin", Consts.CDefRangeMin)
    ub = gMom.getParam("rangemax", Consts.CDefRangeMax)

    sister = gMom.clone()
    brother = gDad.clone()

    sister.resetStats()
    brother.resetStats()

    for i in range(0, len(gMom)):
        if math.fabs(gMom[i] - gDad[i]) > EPS:
            if gMom[i] > gDad[i]:
                # swap
                temp = gMom[i]
                gMom[i] = gDad[i]
                gDad[i] = temp

            # random number betwn. 0 & 1
            u = rand_random()

            beta = 1.0 + 2 * (gMom[i] - lb) / (1.0 * (gDad[i] - gMom[i]))
            alpha = 2.0 - beta**(-(eta_c + 1.0))

            if u <= (1.0 / alpha):
                beta_q = (u * alpha)**(1.0 / ((eta_c + 1.0) * 1.0))
            else:
                beta_q = (1.0 / (2.0 - u * alpha))**(1.0 / (1.0 *
                                                            (eta_c + 1.0)))

            brother[i] = 0.5 * ((gMom[i] + gDad[i]) - beta_q *
                                (gDad[i] - gMom[i]))

            beta = 1.0 + 2.0 * (ub - gDad[i]) / (1.0 * (gDad[i] - gMom[i]))
            alpha = 2.0 - beta**(-(eta_c + 1.0))

            if u <= (1.0 / alpha):
                beta_q = (u * alpha)**(1.0 / ((eta_c + 1) * 1.0))
            else:
                beta_q = (1.0 / (2.0 - u * alpha))**(1.0 / (1.0 *
                                                            (eta_c + 1.0)))

            sister[i] = 0.5 * ((gMom[i] + gDad[i]) + beta_q *
                               (gDad[i] - gMom[i]))

            if brother[i] > ub:
                brother[i] = ub
            if brother[i] < lb:
                brother[i] = lb

            if sister[i] > ub:
                sister[i] = ub
            if sister[i] < lb:
                sister[i] = lb

            if rand_random() > 0.5:
                # Swap
                temp = sister[i]
                sister[i] = brother[i]
                brother[i] = temp
        else:
            sister[i] = gMom[i]
            brother[i] = gDad[i]

    return (sister, brother)
Ejemplo n.º 7
0
n_in=5
n_out=1
n_hid=1
n_hneu=[3]
n_neu=[n_in]+n_hneu+[n_out]
temp=0
sum=0
to_list=[]
momgenome=G1DConnections.G1DConnections()
dadgenome=G1DConnections.G1DConnections()
for i in xrange(1,len(n_neu)):
  sum=sum+n_neu[i-1]+1
  for toNode in xrange(sum,sum+n_neu[i]):
    to_list=to_list+[toNode]
    for fromNode in xrange(temp,sum-1):
      momgenome.append((fromNode,toNode,rand_random()))
      dadgenome.append((fromNode,toNode,rand_random()))
  temp=sum
momgenome.genomeList=np.array(momgenome.genomeList, dtype=[('from','i'),('to','i'),('weight','f')])
dadgenome.genomeList=np.array(dadgenome.genomeList, dtype=[('from','i'),('to','i'),('weight','f')])
f = open('CrossoverResults.txt','w')
f.write( "######  Network setting  ######"+'\n')
f.write( "Number of input: "+str(n_in)+'\n')
f.write( "Number of output: "+str(n_out)+'\n')
f.write( "Number of hidden layer: "+str(n_hid)+'\n')
f.write( "Number of neurons for each hidden layer: "+str(n_hneu)+'\n')
f.write( "###############################"+'\n')
f.write( "toNode list: "+str(to_list)+'\n')
f.write("###############################"+'\n')
f.write( "         Parents:"+'\n')
f.write("###############################"+'\n')
Ejemplo n.º 8
0
def G1DListMutatorInteger_my(genome, **args):
    #print genome[:-1]
    if args["pmut"] <= 0.0: return 0
    a=genome.My_get_listdic()
    list_dic=copy.deepcopy(a[0])
    list_margin=copy.deepcopy(a[1])
    totalmoney=a[2]
    list_zhonglei=a[3]
    all_money=totalmoney
    num = len(genome)
    g_list=[0]*num
    #print genome[0:],1
    mutations = args["pmut"] * num
    if rand_random()<mutations:
            #''''IF':90000,'CU':30000,'RU':10000,'I':3500,'M':1500,'RB':1500,'SR':3000,'TA':2000,'Y':3500'''
        first_lic=['CU','IF','RU']
        second_lic=['I','M','RB','SR','TA','Y']
        while(1):
                if all_money<totalmoney*0.4 or all_money<10000:
                    break
                len_cu=len(list_dic['CU'])
                len_if=len(list_dic['IF'])
                len_ru=len(list_dic['RU'])
                if len_cu<=0 and len_if<=0 and len_ru<=0:
                    break
                for i in first_lic:
                    len_dic=len(list_dic[i])
                    if len_dic<=0:
                        continue
                    if all_money>list_margin[i]:
                        if len_dic>1:
                            j=rand_randint(0,len_dic-1)
                        else:
                            j=0
                        money=list_margin[i]
                        all_lot=int(all_money/money)
                        lot=rand_randint(0,all_lot)
                        all_money=all_money-money*lot
                        n=list_dic[i][j]
                        g_list[n]=lot
                        del list_dic[i][j]
        while(1):
                if  all_money<2000:
                    break
                sum=0
                for i in second_lic:
                    sum=sum+len(list_dic[i])
                if sum<=0:
                    break
                for i in second_lic:
                    len_dic=len(list_dic[i])
                    if len_dic<=0:
                        continue
                    if all_money>list_margin[i]:
                        if len_dic>1:
                            j=rand_randint(0,len_dic-1)
                        else:
                            j=0
                        money=list_margin[i]
                        all_lot=int(all_money/money)
                        lot=rand_randint(0,all_lot)
                        all_money=all_money-money*lot
                        n=list_dic[i][j]
                        g_list[n]=lot
                        del list_dic[i][j]
                sum_l=0
                for i in second_lic:
                    sum_l=sum_l+len(list_dic[i])
                if sum==sum_l:
                    break
        genome.genomeList=g_list
    return int(mutations)