def main():
    try:
      original = mrcfile.open(sys.argv[1],mode='r')
    except IOError:
      print("Could not open the input \nUsage tick_mrc inputfile outputfile.")
      sys.exit()

# create list of layers.
    layers = []
    for layer in original.data:
       layers.append(np.float32(layer))
#layers are the arrays containing the data.
    the_image = np.zeros_like(layers[0])
#    the_image = np.add(the_image, layers[180])
    the_image = np.add(the_image, layers[40])
# nvis,nhid set the size of the rbm
    hw = 2
    hw = 1
    hw = 2
#    nb = 3
    nb = 8
# the number of outer fuzzy sets
    nfuzz = 21
    the_fuzz = []
    delta = 2./(nfuzz-1)
    for i in range(0,nfuzz):
        cent = -1. + i*delta
        the_fuzz.append( triangle.triangle( cent-delta,cent, cent+delta))
        the_fuzz[i].associate_rbm( rbm.rbm( (hw*2+1)*(hw*2+1)*nb, 10))
        the_fuzz[i].rbm.its_symmetric()
        the_fuzz[i].rbm.add_fuzzy(-1.,1.,21)
#    the_rbm = rbm.rbm(  (hw*2+1)*(hw*2+1)*nb, 1000)
#    the_rbm.its_symmetric()
#    the_rbm.add_fuzzy(-1.,1., 21)
    the_adapted_image = adapt.adaptor(  hw, 3., the_image)
    the_adapted_image.make_nbitmap_image(nb)
#    for l in the_rbm.layers:
#        l = np.add(l, the_adapted_image.next(15,15)[2])
#    for l in range(0, len(the_rbm.layers)):
#       the_rbm.layers[l] = np.add( the_rbm.layers[l], the_adapted_image.random_bits()[2])
#    the_adapted_image.reset()
    
    
    print("training starts")
    sys.stdout.flush()
    print("crisp initialization pass")
    sys.stdout.flush() 
    for i in xrange(0,2000):
       a = the_adapted_image.random_bits()
       for f in the_fuzz:
          rate = f.belief(a[1])
          if rate > 0. :
                print(i,a[1],rate,f.rbm.train(a[2],0.1,rate,a[1]))
            
    print("fuzzy pass")
    sys.stdout.flush() 
    for i in xrange(0,10000):
       a = the_adapted_image.random_bits()
       for f in the_fuzz:
          rate = f.belief(a[1])
          if rate > 0. :
                print(i,a[1],rate,f.rbm.train_fuzzy(a[2],0.1,rate,a[1]))
            

    print("training stops")
    sys.stdout.flush()
    
    new_image = np.float32(np.zeros_like(the_image))
    for i in xrange(0, the_image.shape[0]-2*hw-1):
#    for i in xrange(0, 20):
      print("layer", i)
      sys.stdout.flush()
      for j in range(0,the_image.shape[1]-2*hw-1):
#      for j in range(0,20):
          a = the_adapted_image.at_bits(i,j)
          r = the_fuzz[0].rbm
          b = 1.
          fb = the_fuzz[0]
          for f in the_fuzz:
             bt = f.rbm.the_best_built_layer( a[2])
#             print( bt[1],b)
# catch untrained examples
# now done in best_built
             tb = bt[1]
#             if tb < -1.:
#                tb = 0.
             if tb < b:
                b = tb
                r = f.rbm
                fb = f
          x = r.estimate_EV( a[2])
          new_image[i][j] = x
#          print( x, a[1], fb.centre)
#          sys.stdout.flush()
      an_image = Image.fromarray(np.uint8( rescale(new_image,255.)))
      an_image.save('rbm_bitmap.jpg')
          
#
    the_image = Image.fromarray(np.uint8( rescale(the_adapted_image.the_image,255.)))
    the_image.save('raw.jpg')
    the_image = Image.fromarray(np.uint8( rescale(new_image,255.)))
    the_image.save('rbm_bitmap.jpg')
Exemplo n.º 2
0
def main():
    try:
        original = mrcfile.open(sys.argv[1], mode='r')
    except IOError:
        print(
            "Could not open the input \nUsage tick_mrc inputfile outputfile.")
        sys.exit()

# create list of layers.
    layers = []
    for layer in original.data:
        layers.append(np.float32(layer))
#layers are the arrays containing the data.
    the_image = np.zeros_like(layers[0])
    the_image = np.add(the_image, layers[40])
    # nvis,nhid
    hw = 2
    nb = 3
    the_rbm = rbm.rbm((hw * 2 + 1) * (hw * 2 + 1) * nb, 1000)
    the_rbm.its_symmetric()
    the_rbm.add_fuzzy(-1., 1., 21)
    the_adapted_image = adapt.adaptor(hw, 3., the_image)
    the_adapted_image.make_nbit_image(nb)
    #    for l in the_rbm.layers:
    #        l = np.add(l, the_adapted_image.next(15,15)[2])
    for l in range(0, len(the_rbm.layers)):
        the_rbm.layers[l] = np.add(the_rbm.layers[l],
                                   the_adapted_image.random_bits()[2])
    the_adapted_image.reset()

    print("training starts")
    sys.stdout.flush()
    #  a = the_adapted_image.next(1)
    for i in xrange(0, 2000):
        a = the_adapted_image.random_bits()
        print(i, a[1], the_rbm.train_fuzzy(a[2], 0.1, 1.0, a[1]))


#       if i > 1000:
#        print a[2]
#        print the_rbm.reconstruct(a[2])
#       print(i, a[1])

    print("training stops")
    sys.stdout.flush()

    new_image = np.float32(np.zeros_like(the_image))
    #    for i in xrange(0, the_image.shape[0]-2*hw-1):
    for i in xrange(0, 20):
        print("layer", i)
        sys.stdout.flush()
        for j in range(0, the_image.shape[1] - 2 * hw - 1):
            #      for j in range(0,20):
            a = the_adapted_image.at_bits(i, j)
            b = the_rbm.reconstruct(a[2])
            new_image[i + hw + 1][j + hw + 1] = the_rbm.estimate_EV(a[2])

    the_image = Image.fromarray(
        np.uint8(rescale(the_adapted_image.the_image, 255.)))
    the_image.save('raw.jpg')
    the_image = Image.fromarray(np.uint8(rescale(new_image, 255.)))
    the_image.save('rbm.jpg')
Exemplo n.º 3
0
def main():
    try:
        original = mrcfile.open(sys.argv[1], mode='r')
    except IOError:
        print(
            "Could not open the input \nUsage tick_mrc inputfile outputfile.")
        sys.exit()

# create list of layers.
    layers = []
    for layer in original.data:
        layers.append(np.float32(layer))


#layers are the arrays containing the data.
    the_image = np.zeros_like(layers[0])
    the_image = np.add(the_image, layers[40])
    # nvis,nhid
    hw = 1
    the_rbm = rbm.rbm((hw * 2 + 1) * (hw * 2 + 1), 10)
    the_rbm.add_fuzzy(-1., 1., 21)
    the_rbm.reinitialize_fuzzy()
    the_adapted_image = adapt.adaptor(hw, 3., the_image)
    #    the_adapted_image.make_nbit_image(3)
    #    for l in the_rbm.layers:
    #        l = np.add(l, the_adapted_image.next(15,15)[2])
    #    for l in range(0, len(the_rbm.layers)):
    #       the_rbm.layers[l] = np.add( the_rbm.layers[l], the_adapted_image.next(1,1)[2])
    the_adapted_image.reset()

    print("training starts")
    sys.stdout.flush()
    #  a = the_adapted_image.next(1)
    for i in range(0, 1000):
        a = the_adapted_image.random()
        the_rbm.train(a[2], 0.1, 0.5, a[1])
        print(i, a[1])

    a = the_adapted_image.random()
    i = 0
    j = 0
    errs = np.float32(np.zeros(100))
    while (a[0]):
        the_rbm.train_fuzzy(a[2], 0.1, 0.5, a[1])
        if i == 1000:
            the_rbm.reinitialize_fuzzy()
        x = the_rbm.estimate_EV(a[2])
        errs[j] = a[1] - x
        j = (j + 1) % 100
        print(i, a[1], x, errs.std())
        #        print(i, a[1], the_rbm.estimate_EV(a[2]))
        #        a = the_adapted_image.next(1,1)
        a = the_adapted_image.random()
        sys.stdout.flush()
        i += 1
    print("training stops")
    sys.stdout.flush()

    #    for layer in layers:
    #        the_image = np.add(the_image,layer)
    the_image = Image.fromarray(
        np.uint8(rescale(the_adapted_image.the_image, 255.)))
    the_image.save('sum.jpg')