Example #1
0
def mat2png():
    path = '/data/detect/VOC/VOCdevkit/VOC2010/trainval'
    files = os.listdir(path)
    labels_path = '/data/detect/VOC/VOCdevkit/VOC2010/test22'  #Seglabels' #os.path.join(path,'Seglabels')
    total_num = len(files)
    name2label = c459259l('../datasets/22label.txt',
                          '/data/detect/VOC/VOCdevkit/VOC2010/labels.txt')
    keys = name2label.keys()
    for i in tqdm.tqdm(range(total_num)):
        afile = files[i]
        file_path = os.path.join(path, afile)
        if os.path.isfile(file_path):
            if os.path.getsize(file_path) == 0:
                continue
            mat_idx = afile[:-4]
            mat_file = sio(file_path)
            mat_file = np.array(mat_file['LabelMap']).astype(np.int)
            h, w = mat_file.shape[:2]
            tmp_label = np.zeros([h, w])
            for tmp in keys:
                tmp_label[mat_file == int(tmp)] = int(name2label[tmp])
            tmp_label = tmp_label.astype(np.uint8)  #here is a bug 459 -> 256
            # label_img=Image.fromarray(mat_file.reshape(mat_file.shape[0],mat_file.shape[1]))
            dst_path = os.path.join(labels_path, mat_idx + '.png')
            cv2.imwrite(dst_path, tmp_label)
import os
from scipy.io import loadmat as sio
import numpy as np
import PIL.Image as Image
import matplotlib.pyplot as plt

#path='/home/dl/DL_dataset/VOCdevkit/trainval'
path = os.path.join(os.getcwd(), 'GroundTruth_trainval_mat')

files = os.listdir(path)

labels_path = os.path.join(os.getcwd(), 'GroundTruth_trainval_png')

for afile in files:
    file_path = os.path.join(path, afile)
    if os.path.isfile(file_path):
        if os.path.getsize(file_path) == 0:
            continue
        mat_idx = afile[:afile.find('.mat')]
        mat_file = sio(file_path)
        mat_file = np.array(mat_file['LabelMap'])
        #print mat_file.keys()
        mat_file = mat_file.astype(np.uint8)
        label_img = Image.fromarray(
            mat_file.reshape(mat_file.shape[0], mat_file.shape[1]))
        dst_path = os.path.join(labels_path, mat_idx + '.png')
        print(dst_path)
        label_img.save(dst_path)
                            [192, 0, 0],
                            [64, 128, 0],
                            [192, 128, 0],
                            [64, 0, 128],
                            [192, 0, 128],
                            [64, 128, 128],
                            [192, 128, 128],
                            [0, 64, 0],
                            [128, 64, 0],
                            [0, 192, 0],
                            [128, 192, 0],
                            [0, 64, 128]], dtype='uint8').flatten()



for afile in files:
    file_path=os.path.join(path,afile)
    if os.path.isfile(file_path):
        if os.path.getsize(file_path)==0:
            continue
        mat_idx=afile[:afile.find('.mat')]
        mat_file=sio(file_path)
        mat_file=mat_file['data']
        labels=np.argmax(mat_file,axis=2).astype(np.uint8)
        label_img=Image.fromarray(labels.reshape(labels.shape[0],labels.shape[1]))
        label_img.putpalette(palette)
        label_img=label_img.transpose(Image.FLIP_LEFT_RIGHT)
        label_img = label_img.rotate(90)
        dst_path=os.path.join(labels_path,mat_idx+'.png')
        label_img.save(dst_path)
Example #4
0
"""
Created on Mon Feb 20 17:20:58 2017

@author: aliTakin
"""
import numpy as np
#from sklearn.svm import svc
import matplotlib.pyplot as plt
from scipy.io import loadmat as sio
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.feature_selection import RFECV
from sklearn.metrics import accuracy_score

data = sio('arcene.mat')
y_train = np.ravel(data['y_train'])
y_test = np.ravel(data['y_test'])
X_train = data['X_train']
X_test = data['X_test']

rfecv = RFECV(estimator=LogisticRegression(),
              step=50,
              cv=10,
              scoring='accuracy')
rfecv.fit(X_train, y_train)
print(rfecv.n_features_)
lr1 = LogisticRegression()
lr1.fit(X_train, y_train)
score_lr1 = accuracy_score(y_test, lr1.predict(X_test))
print(score_lr1)