本文整理汇总了Python中Helper.get_textural_features方法的典型用法代码示例。如果您正苦于以下问题:Python Helper.get_textural_features方法的具体用法?Python Helper.get_textural_features怎么用?Python Helper.get_textural_features使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Helper
的用法示例。
在下文中一共展示了Helper.get_textural_features方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: classify
# 需要导入模块: import Helper [as 别名]
# 或者: from Helper import get_textural_features [as 别名]
def classify(img, featureRepresentation='image', model_file=CLASSIFIER_FILE, shouldSaveResult=False):
'''
Classifies a sub-image or list of sub-images as grain (1) or not grain (0).
Args:
img: Input sub-image or list of input sub-images.
featureRepresentation: Type of features to be used in classification.
Can ake of one of the values 'image', 'pca' or 'glcm'. Note that the
classifier must have also been built using the same
feature representation.
model_file: filepath of serialized classifier to be used.
shouldSaveResult: If this boolean flag is set to true, this function
will save the sub-images and their classifictions to the "Results"
folder after classification.
Return:
scalar or list of 1 if grain and 0 otherwise.
'''
if(isinstance(img, np.ndarray)):
img_features = None
if(featureRepresentation == 'image'):
img_features = img.flatten()
elif(featureRepresentation == 'pca'):
img_features = decomposition.PCA(n_components=8).fit_transform(img.flatten())
elif(featureRepresentation == 'glcm'):
img_features = Helper.get_textural_features(img, 1, True)
clf = get_model(model_file)
return clf.predict(img_features.reshape(1,-1))
elif(isinstance(img, list)):
if(featureRepresentation == 'glcm'):
sample_size = 16
else:
sample_size = 20*20
test_data = np.zeros((len(img), sample_size))
i = 0
for image in img:
if(featureRepresentation == 'image'):
test_data[i] = image.flatten()
elif(featureRepresentation == 'pca'):
test_data[i] = decomposition.PCA(n_components=8).fit_transform(image.flatten())
elif(featureRepresentation == 'glcm'):
test_data[i] = Helper.get_textural_features(image, 1, True)
i = i+1
clf = get_model(model_file)
result = clf.predict(test_data)
if(shouldSaveResult == True):
# Save image with result in filename
if os.path.exists("Results"):
shutil.rmtree("Results")
os.makedirs("Results")
for i in xrange(0,len(img)):
io.imsave("Results/{}_{}.png".format(Helper.generate_random_id(8), result[i]), img[i])
else:
return None
示例2: build_model
# 需要导入模块: import Helper [as 别名]
# 或者: from Helper import get_textural_features [as 别名]
def build_model(featureRepresentation='image', dataset_file=None, iters=10, glcm_distance=1, glcm_isMultidirectional=False):
'''
Creates, trains and serialises an MLP classifier.
Args:
featureRepresentation: Type of features to be used in classification.
Can ake of one of the values 'image', 'pca' or 'glcm'.
dataset_file: filename of serialized data set upon which to build the
MLP. If none, default dataset is used.
iters: Number of training iterations.
glcm_distance: Distance between pixels for co-occurence. Only used if
featureRepresentation=glcm.
isMultidirectional: Controls whether co-occurence should be calculated
in other directions (ie 45 degrees, 90 degrees and 135 degrees).
Only used if featureRepresentation=glcm.
'''
if(dataset_file == None):
# Load train data
train_filenames = []
for filename in os.listdir("../train/positive"):
if(filename != ".DS_Store"): train_filenames.append("../train/positive/" + filename)
train_targets = [1]*(len(os.listdir("../train/positive"))-1)
for filename in os.listdir("../train/negative"):
if(filename != ".DS_Store"): train_filenames.append("../train/negative/" + filename)
train_targets = train_targets + [0]*(len(os.listdir("../train/negative"))-1)
n_train_samples = len(train_filenames)
if(featureRepresentation == 'glcm'):
if(glcm_isMultidirectional):
sample_size = 16
else:
sample_size = 4
else:
sample_size = 20*20
train_data = np.zeros((n_train_samples, sample_size))
i = 0
for filename in train_filenames:
img = io.imread(filename)
if(featureRepresentation == 'image'):
train_data[i] = img.flatten()
elif(featureRepresentation == 'pca'):
train_data[i] = decomposition.PCA(n_components=8).fit_transform(img.flatten())
elif(featureRepresentation == 'glcm'):
train_data[i] = Helper.get_textural_features(img, glcm_distance, glcm_isMultidirectional)
i = i + 1;
# Load test data
test_filenames = []
expected = []
for filename in os.listdir("test"):
if(filename != ".DS_Store"):
test_filenames.append("../test/" + filename)
expected.append(int(filename.split('_')[1].split('.')[0]))
n_test_samples = len(test_filenames)
test_data = np.zeros((n_test_samples, sample_size))
i = 0
for filename in test_filenames:
img = io.imread(filename)
if(featureRepresentation == 'image'):
test_data[i] = img.flatten()
elif(featureRepresentation == 'pca'):
test_data[i] = decomposition.PCA(n_components=8).fit_transform(img.flatten())
elif(featureRepresentation == 'glcm'):
test_data[i] = Helper.get_textural_features(img, glcm_distance, glcm_isMultidirectional)
i = i + 1;
else:
train_data, train_targets, test_data, expected = Helper.unserialize(dataset_file)
# Perform build iterations
for i in tqdm.tqdm(range(0, iters)):
# Build Classifier
param_grid = {"algorithm":["l-bfgs", "sgd", "adam"], "activation":["logistic", "relu", "tanh"], "hidden_layer_sizes":[(5,2), (5), (100), (150), (200)] }
classifier = grid_search.GridSearchCV(MLPClassifier(), param_grid)
classifier.fit(train_data, train_targets)
# Get previous classifier and assess
serialized_classifier = Helper.unserialize(MLP_FILE)
if(serialized_classifier):
predictions = serialized_classifier.predict(test_data)
confusion_matrix = metrics.confusion_matrix(expected, predictions)
serialized_n_correct = confusion_matrix[0][0] + confusion_matrix[1][1]
predictions = classifier.predict(test_data)
confusion_matrix = metrics.confusion_matrix(expected, predictions)
n_correct = confusion_matrix[0][0] + confusion_matrix[1][1]
if(n_correct > serialized_n_correct):
Helper.serialize(MLP_FILE, classifier)
else:
Helper.serialize(MLP_FILE, classifier)
# Display final model performance
serialized_classifier = Helper.unserialize(MLP_FILE)
predictions = serialized_classifier.predict(test_data)
#.........这里部分代码省略.........