Exemple #1
0
def get_stats(im):
    def standardize(x):
        x = np.array(x)
        x[np.isnan(x)] = 0.0
        x = x - np.min(x)
        x = x / np.max(x)
        return x

    patch = standardize(im)
    h, _ = hurst2d(patch, max_tau=7)
    # estimate Gaussianity via kurtosis
    kurt = kurtosis(patch.flatten())
    coh = coherence(patch)
    return dict({'H': h, 'Kurtosis': kurt, 'MeanCoh': np.mean(coh['logcoh'])})
Exemple #2
0
def get_kurtsim_imgs(N=120,n=32,reCalc=False,resize=None):
    #reCalc = True
    #reCalc = False
    fname = 'data_kurtsim.bin'
    if os.path.exists(fname) and not reCalc:
        print 'loading data from file'
        Xtrain, Xtest, Ytrain, Ytest = pickle.load(open(fname,'r'))
    else:
        print 'generating data and saving'
        np.random.seed(0)
        X = []
        stds = np.linspace(0.0,0.4,N)# [0.5]*N
        feats=[]
        im = cv2.imread('/home/ido/brodatz/1.1.01.tiff').astype(np.float32)
        im = im[:224,:224,0]
        im = im-np.min(im)
        im0 = im/np.max(im)

        #plt.imshow(im)
        for k,s in enumerate(stds):
            if not k % 20:
                print 'done %f'%(1.0*k/N)
            im = im0 + np.random.randn(224,224)*s

            X.append(im)
            h = hurst2d(im,max_tau=7)
            kurt = kurtosis(im.flatten())
            coh = coherence(im)
            #print(kurt)
            feats.append([h, kurt, np.mean(coh['logcoh']), np.std(coh['logcoh'])])

        X0=np.array(X)
        Y = np.array(feats)
        #X=np.expand_dims(X0,2) # to make input of size (n,1) instead of just (n)
        if resize is not None:
            sz=int(np.sqrt(resize))
            X0r = np.zeros([X0.shape[0],sz,sz])
            for i in range(X0.shape[0]):
                X0r[i,:,:] = imresize(X0[i,:,:],[sz,sz])
                #print X0r[i,:,:]
                #plt.imshow(X0r[i,:,:],interpolation='none',cmap='gray')
                #plt.show()
            X0=X0r

        Xtrain, Xtest, Ytrain, Ytest = train_test_split(X0,Y,test_size=0.2,random_state=0)

        pickle.dump([Xtrain,Xtest,Ytrain,Ytest],open(fname,'w'))

    return Xtrain, Xtest, Ytrain, Ytest
Exemple #3
0
def get_kth_imgs(N=10000,n=32,reCalc=False,resize=None):
    #reCalc = True
    #reCalc = False

    if os.path.exists('data_kth.bin') and not reCalc:
        print 'loading data from file'
        Xtrain, Xtest, Ytrain, Ytest = pickle.load(open('data_kth.bin','r'))
    else:
        print 'loading data and saving'
        X = []
        H = []
        names = []
        path0 = '/home/ido/combined_model_classification/KTH-TIPS2-b/'
        samples = ['wool', 'cotton', 'cracker']
        path = ['/sample_', '/']

        dirs = ['a','b','c','d']
        files = []
        for sample in samples:
            for d in dirs:
                path1 = path0+sample+path[0]+d+path[1]
                files += [path1+f for f in listdir(path1)]
                names += [sample+d for i in range(len(listdir(path1)))]
        print 'tot files:',len(files)
        p = n
        samp_names=[]
        try:
            for i,(f,name) in enumerate(zip(files,names)):
                print i,'name',name,'file',f
                img = Image.open(f).convert('L')
                (width, height) = img.size
                img = np.array(list(img.getdata()))
                img = img.reshape((height, width))
                sz = [width, height]

                for k in range(0,sz[0],p):
                    for l in range(0,sz[1],p):
                        patch = img[k:k+p,l:l+p]*1.0
                        if np.min(patch.shape) == 0:
                            continue
                        patch = patch-np.min(patch)
                        patch = patch/np.max(patch)
                        #print patch
                        if patch.shape[0]<p or patch.shape[1]<p:
                            continue
                        h,_ = hurst2d(patch,max_tau=7)
                        # estimate Gaussianity via kurtosis
                        kurt = kurtosis(patch.flatten())
                        coh = coherence(patch)
                        #print(kurt)
                        H.append([h, kurt, name, np.mean(coh['logcoh']), np.std(coh['logcoh'])])
                        samp_names.append(name)
                        X.append(patch)
                        if len(X)>=N:
                            print 'too many images'
                            raise GetOutOfLoop
        except GetOutOfLoop:
            pass
        print 'max N',N,'number of patches',len(X)
        Y = np.vstack(H)
        X = np.array(X)
        if resize is not None:
            sz=int(np.sqrt(resize))
            X0r = np.zeros([X.shape[0],sz,sz])
            for i in range(X.shape[0]):
                X0r[i,:,:] = imresize(X[i,:,:],[sz,sz])
                #print X0r[i,:,:]
                #plt.imshow(X0r[i,:,:],interpolation='none',cmap='gray')
                #plt.show()
            X=X0r
        #X = np.expand_dims(np.array(X),3) # to make input of size (n,1) instead of just (n)
        Xtrain, Xtest, Ytrain, Ytest = train_test_split(X,Y,test_size=0.2,random_state=0)

        pickle.dump([Xtrain,Xtest,Ytrain,Ytest],open('data_kth.bin','w'))

    return Xtrain, Xtest, Ytrain, Ytest
    #rr[0]=0
    #z_sample = np.array([[xi, yi, zi, rr[0], rr[1]]])
    z_sample = np.array([rr])
    x_decoded = generator.predict(z_sample)
    x_decoded = np.reshape(x_decoded[0],[n,n])
    """
    plt.imshow(x_decoded,interpolation='none')
    plt.show()
    plt.pause(0.1)
    continue
    #"""

    #digit = x_decoded[0].reshape(digit_size, digit_size)
    x_decoded = x_decoded-np.min(x_decoded)
    x_decoded = x_decoded/np.max(x_decoded)
    H_est, rsquare = hurst2d(x_decoded,max_tau=7)
    Hs.append(H_est)
    rsquares+=rsquare
    tot+=1
    if not i % 30:
        spl[0].hold(False)
        diffres = np.squeeze(np.reshape(x_decoded[1:]-x_decoded[:-1],[1,-1]))
        _, bins, patches = spl[0].hist(diffres, 50, normed=1, facecolor='green', alpha=0.75)
        y = mlab.normpdf( bins, np.mean(diffres), np.std(diffres))
        spl[0].hold(True)
        spl[0].plot(bins, y, 'r--', linewidth=1)
        spl[0].set_title('diff, exp %d/%d'%(i,num_exps))
        spl[1].hold(False)
        spl[1].imshow(x_decoded,interpolation='none')
        #plt.title('y %f, x %f, z %f'%(yi,xi,zi))