示例#1
0
文件: train.py 项目: juhanaka/mnist
def train(alpha, n_iter, plot=False):
    size_img, rows, cols, images = utils.read_images(
        TRAINING_IMAGES_PATH
    )
    size_lbl, labels = utils.read_labels(TRAINING_LABELS_PATH)

    images = images[range(0, M_TRAINING), :]
    labels = labels[range(0, M_TRAINING), :]
    size_img = M_TRAINING

    bias_terms = np.ones([size_img, 1], dtype=np.float64)
    images = np.concatenate((bias_terms, images), axis=1).astype(np.float64)
    thetas = np.zeros([rows*cols+1, N_LABELS], dtype=np.float64)
    costs = np.zeros([n_iter, N_LABELS])
    X = images / 255
    for i in range(N_LABELS):
        # print 'Training a classifier for label {0}'.format(i)
        y = np.array([[1 if label == i else 0 for label in labels]]).T
        thetas[:, i:i+1], costs[:, i:i+1] = func.gradient_descent(
            thetas[:, i:i+1],
            y, X, alpha,
            n_iter
        )
        if plot:
            plt.plot(costs[:, i:i+1])
            plt.show()
    return thetas
示例#2
0
文件: train.py 项目: juhanaka/mnist
def test(thetas):
    size_img, rows, cols, images = utils.read_images(
        TRAINING_IMAGES_PATH
    )
    size_lbl, labels = utils.read_labels(TRAINING_LABELS_PATH)

    bias_terms = np.ones([size_img, 1])
    images = np.concatenate((bias_terms, images), axis=1)

    training_images = images[range(0, M_TRAINING), :]
    training_labels = labels[range(0, M_TRAINING), :]

    X = training_images / 255
    y = training_labels
    accuracy = _test(thetas, y, X, M_TRAINING)

    print 'Trained with {0} examples. Training accuracy: {1}'.format(
        M_TRAINING,
        accuracy
    )

    test_labels = labels[range(M_TRAINING, M_TRAINING + M_TESTING), :]
    test_images = images[range(M_TRAINING, M_TRAINING + M_TESTING), :]

    X = test_images / 255
    y = test_labels
    accuracy = _test(thetas, y, X, M_TESTING)

    print 'Tested with {0} examples. Test accuracy: {1}'.format(M_TESTING,
                                                                accuracy)
示例#3
0
def test():
    x = tf.placeholder(tf.float32, shape=[1, 32, 32, 1], name='input')
    network = get_network(args.network)(x=x, is_training=False)

    testing_images = os.path.join(args.testing_folder, 'images')
    testing_labels = read_labels(
        os.path.join(args.testing_folder, 'labels.txt'))

    loader = tf.train.Saver()

    config = tf.ConfigProto(allow_soft_placement=True)
    correct_predictions = 0
    total_number = len(testing_labels)
    with tf.Session(config=config) as session:
        loader.restore(session, args.model)
        for step in range(total_number):
            next_batch = get_test_batch(testing_images, testing_labels, step)
            predicted_value = session.run(network.prediction,
                                          feed_dict={x: next_batch['images']})
            if np.squeeze(predicted_value) == testing_labels[step]:
                correct_predictions += 1
        print(
            'Testing results: {}/{} correct predictions Error rate:{}'.format(
                correct_predictions, total_number,
                100. * (total_number - correct_predictions) / total_number))
示例#4
0
def calc_fsaverage_labels_indices(surf_name='pial', labels_from_annot=False, labels_fol='', parc='aparc250', subjects_dir=''):
    labels_fol = os.path.join(subjects_dir, 'fsaverage', 'label', parc) if labels_fol=='' else labels_fol
    if (labels_from_annot):
        labels = mne.read_labels_from_annot('fsaverage', parc=parc, hemi='both', surf_name=surf_name)
    else:
        labels = utils.read_labels(labels_fol)
    fsave_vertices = [np.arange(10242), np.arange(10242)]
    labels_vertices, labels_names = utils.get_labels_vertices(labels, fsave_vertices)
    np.savez(op.join(LOCAL_ROOT_DIR, 'fsaverage_labels_indices'), labels_vertices=labels_vertices, labels_names=labels_names)
示例#5
0
    def __getitem__(self, item):
        image = Image.open(self.image_list[item]).convert('RGB')
        image = self.transform(image)
        label = utils.read_labels(self.label_list[item])
        # convert to S*S*5 Tensor with format <x> <y> <w> <h> <cls>
        label = utils.labels2tensor(label)

        # get filename
        filename = self.image_list[item].split('/')[-1]

        return image, label, filename
示例#6
0
    def __init__(self, model_path, label_path, score_threshold):
        """
        Initiates the ObjectDetectionEngine class.

        :param model_path: Path to SSD object detection model
        :param label_path: Path to coco format labels
        :param score_threshold: Threshold for detection in the form of a float between 0 and 1
        """
        InferenceEngine.__init__(self, model_path, [(1, 10, 4), (1, 10),
                                                    (1, 10), (1)])
        self.labels = read_labels(label_path)
        self.score_threshold = score_threshold
def run_evaluation(test_labels, predicted_labels):
    """
    Gives statistical information on the performance of given algorithm.

    :param list test_labels: Real value of test labels
    :param str predicted_labels: Predicted value of test labels
    :return: Confusion Matrix, Accuracy, Precision, Recall, fMeasure
    """
    predicted_labels = utils.read_labels(predicted_labels)
    co_matrix = confusion_matrix(test_labels, predicted_labels)
    accuracy = utils.estimate_accuracy(test_labels, predicted_labels)
    precision_recall_fscore = precision_recall_fscore_support(
        test_labels, predicted_labels, average='weighted')
    print(class_predictions(test_labels, predicted_labels))
    return co_matrix, accuracy, precision_recall_fscore
示例#8
0
    def load_workspace(self, path):
        """
        Sets the workspace directory. Checks for missing directories and files,
        then loads all images, annotations & label list.
        :param path: The path of the directory
        """
        if not path:
            path = "/tmp"

        self.workspace = path
        self.output_path_edit.setText(path)

        # clear all lists and references
        self.labels = []
        self.cur_annotation_index = -1
        self.cur_annotated_image = None
        self.images_with_annotations = []
        self.changes_done = False
        self.class_id = -1
        self.label = ""

        # clear combo box and listWidgets
        self.option_selector.clear()
        self.image_list_widget.clear()
        self.annotation_list_widget.clear()

        if utils.check_workspace(self.workspace):
            image_dir = self.workspace + "/images"
            label_file = self.workspace + "/labels.txt"

            self.labels = utils.read_labels(label_file)
            for label in self.labels:
                self.option_selector.addItem(label[0])

            for dirname, dirnames, filenames in os.walk(image_dir):
                for filename in sorted(filenames):
                    image_file = dirname + '/' + filename
                    label_file = image_file.replace(
                        "images",
                        "labels").replace(".jpg",
                                          ".txt").replace(".png", ".txt")
                    annotated_image = utils.read_annotated_image(
                        image_file, label_file)
                    self.images_with_annotations.append(annotated_image)
                    self.add_image_to_list_widget("/images/" + filename)
            print("workspace loaded successfully")
示例#9
0
import xml.etree.ElementTree as et
import numpy as np
import tensorflow as tf
import time
import sys
import pickle
import config
import utils

utils.read_labels("yelp")
import class_DatasetAgN as ds
import cnn as cn
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import TruncatedSVD
from sklearn.pipeline import Pipeline
from stop_words import get_stop_words
from sklearn.metrics import pairwise_distances

env = sys.argv[1]
config.dictionary_size = 1014
config.vocabulary_size = 1
# print(labels)
print("Total labels: ", len(config.labels))
print(config.vocabulary_size)

path = ""
if env == "local":
    path = "data/reuters/"
elif env == "server":
    path = "data/reuters/"
TEST_DIR = "test/"

# use HOG as a list of features
# reading in the data. This takes a while
train_imgs = utils.read_folder(TRAIN_DIR, 0, ntrain, flatten = False)
print ("\nDone!")
sys.stdout.flush()
print ("Getting HOG3 of the data...")
sys.stdout.flush()
# also takes a while
X = utils.getHOG3(train_imgs)
print ("\nDone!")
sys.stdout.flush()
X = np.insert(X, 0, 1.0, axis = 1)
theta = np.random.randn(X.shape[1], 10) * 0.0001
y = utils.read_labels('trainLabels.csv', 0, ntrain)
best_val = -1
best_softmax = None
X_train, X_val, y_train, y_val = cross_validation.train_test_split(X, y, test_size = 0.1)

print "y_train.shape=", y_train.shape
print "y_val.shape=", y_val.shape
print "X_train.shape=", X_train.shape
print "X_val.shape=", X_val.shape
sys.stdout.flush()

# this part was used to the best hyperparameters

#best LR = [15]
learning_rates = [15] # [11,12,13,14,15,16,17,18,19,20]
#Best RS = [1e-3]
示例#11
0
from model import LeNet

parser = argparse.ArgumentParser(
    description='Training LeNet on MNIST using NumPy.')
parser.add_argument('data_dir', type=str, help='directory to mnist data')

args = parser.parse_args()

epoch = 10
lr = 0.1
momentum = 0.8
batch = 256

train_data = read_images(os.path.join(args.data_dir,
                                      'train-images.idx3-ubyte'))
train_labels = read_labels(
    os.path.join(args.data_dir, 'train-labels.idx1-ubyte'))
test_data = read_images(os.path.join(args.data_dir, 't10k-images.idx3-ubyte'))
test_labels = read_labels(os.path.join(args.data_dir,
                                       't10k-labels.idx1-ubyte'))

# normalize
train_data = (train_data - train_data.mean(
    (1, 2), keepdims=True)) / train_data.std((1, 2), keepdims=True)
test_data = (test_data - test_data.mean(
    (1, 2), keepdims=True)) / test_data.std((1, 2), keepdims=True)

my_net = LeNet()
optimizer = SGD(my_net.parameters(), lr, momentum)

loss_history = []
示例#12
0
def main():
    """ Main function
    """
    parser = ArgumentParser(
        description='Affective Computing with AMIGOS Dataset -- XGBoost')
    parser.add_argument('--data', type=str, default='./data')
    parser.add_argument('--feat',
                        type=str,
                        choices=['eeg', 'ecg', 'gsr', 'all'],
                        default='all',
                        help='choose type of modality')
    parser.add_argument('--old', action='store_true')
    args = parser.parse_args()

    # read extracted features
    if args.old:
        data = np.loadtxt(os.path.join(args.data, 'features.csv'),
                          delimiter=',')[:, :213]
        if args.feat == 'eeg':
            data = data[:, 108:213]
        elif args.feat == 'ecg':
            data = data[:, 32:108]
        elif args.feat == 'gsr':
            data = data[:, 0:32]
        a_data, v_data = (data, data)
    else:
        a_data = np.loadtxt(os.path.join(args.data, 'a_features.csv'),
                            delimiter=',')
        v_data = np.loadtxt(os.path.join(args.data, 'v_features.csv'),
                            delimiter=',')
        if args.feat == 'eeg':
            a_data = np.hstack((a_data[:, 108:213], a_data[:, 229:232]))
            v_data = np.hstack((v_data[:, 108:213], v_data[:, 217:316]))
        elif args.feat == 'ecg':
            a_data = np.hstack(
                (a_data[:, 32:108], a_data[:, 213:215], a_data[:, 232:234]))
            v_data = np.hstack(
                (v_data[:, 32:108], v_data[:, 213:217], v_data[:, 316:324]))
        elif args.feat == 'gsr':
            a_data = np.hstack(
                (a_data[:, 0:32], a_data[:, 215:229], a_data[:, 234:254]))
            v_data = v_data[:, 0:32]

    # read labels and split to 0 and 1 by
    a_labels, v_labels = read_labels(os.path.join(args.data, 'label.csv'))

    # setup kfold cross validator
    sub_num = SUBJECT_NUM - len(MISSING_DATA_SUBJECT)
    kf = KFold(n_splits=sub_num)

    # classifier parameters
    with open(
            os.path.join(
                args.data, 'model',
                "{}_a_{}_model.pkl".format('old' if args.old else 'new',
                                           args.feat)), 'rb') as f:
        a_params = pickle.load(f)
    with open(
            os.path.join(
                args.data, 'model',
                "{}_v_{}_model.pkl".format('old' if args.old else 'new',
                                           args.feat)), 'rb') as f:
        v_params = pickle.load(f)

    a_clf = xgb.XGBClassifier(objective="binary:logistic")
    v_clf = xgb.XGBClassifier(objective="binary:logistic")

    a_clf.set_params(**a_params)
    v_clf.set_params(**v_params)

    train_a_accuracy_history = []
    train_v_accuracy_history = []
    val_a_accuracy_history = []
    val_v_accuracy_history = []

    for fold, (train_idx, val_idx) in enumerate(kf.split(a_data)):
        print('Fold', fold + 1)
        # normalize using mean and std
        a_data = (a_data - np.mean(a_data, axis=0)) / np.std(a_data, axis=0)
        v_data = (v_data - np.mean(v_data, axis=0)) / np.std(v_data, axis=0)

        # collect data for cross validation
        a_train_data, a_val_data = a_data[train_idx], a_data[val_idx]
        v_train_data, v_val_data = v_data[train_idx], v_data[val_idx]
        train_a_labels, val_a_labels = a_labels[train_idx], a_labels[val_idx]
        train_v_labels, val_v_labels = v_labels[train_idx], v_labels[val_idx]

        # fit classifier
        a_clf.fit(a_train_data, train_a_labels)
        v_clf.fit(v_train_data, train_v_labels)

        # predict arousal and valence
        train_a_predict_labels = a_clf.predict(a_train_data)
        train_v_predict_labels = v_clf.predict(v_train_data)
        val_a_predict_labels = a_clf.predict(a_val_data)
        val_v_predict_labels = v_clf.predict(v_val_data)

        # metrics calculation
        train_a_accuracy = f1_score(train_a_labels,
                                    train_a_predict_labels,
                                    average='macro')
        train_v_accuracy = f1_score(train_v_labels,
                                    train_v_predict_labels,
                                    average='macro')
        val_a_accuracy = f1_score(val_a_labels,
                                  val_a_predict_labels,
                                  average='macro')
        val_v_accuracy = f1_score(val_v_labels,
                                  val_v_predict_labels,
                                  average='macro')

        train_a_accuracy_history.append(train_a_accuracy)
        train_v_accuracy_history.append(train_v_accuracy)
        val_a_accuracy_history.append(val_a_accuracy)
        val_v_accuracy_history.append(val_v_accuracy)

    print('Training Result:')
    print("Arousal: acc = {:.4f}".format(np.mean(train_a_accuracy_history)))
    print("Valence: acc = {:.4f}\n".format(np.mean(train_v_accuracy_history)))
    print('Validating Result:')
    print("Arousal: acc = {:.4f}".format(np.mean(val_a_accuracy_history)))
    print("Valence: acc = {:.4f}\n".format(np.mean(val_v_accuracy_history)))

    a_imp = a_clf.feature_importances_
    v_imp = v_clf.feature_importances_

    a_sort_idx = np.argsort(a_imp)[::-1]
    v_sort_idx = np.argsort(v_imp)[::-1]

    with open('a_imp_name', 'w') as f:
        for idx in a_sort_idx:
            if a_imp[idx] != 0:
                f.write("{}\n".format(A_FEATURE_NAMES[idx]))

    with open('v_imp_name', 'w') as f:
        for idx in v_sort_idx:
            if v_imp[idx] != 0:
                f.write("{}\n".format(V_FEATURE_NAMES[idx]))
示例#13
0
import random

import network
import utils

labels = utils.read_labels()

utils.prepare_images()
all_images = utils.get_images()
image = utils.load_image(all_images[0])
input_shape = image.shape
BATCH_SIZE = 32
EPOCHS = 50


def load_pair(fname):
    f = utils.load_image(fname + '_firefox.png')
    print(f.shape)
    c = utils.load_image(fname + '_chrome.png')
    print(c.shape)
    return [f, c]


images_train = random.sample(all_images, int(len(all_images) * 0.9))
images_test = [i for i in all_images if i not in set(images_train)]


def couples_generator(images):
    for i in images:
        yield load_pair(i), labels[i]
示例#14
0
import argparse
from PIL import ImageTk, Image
from tkinter import Tk, Label

import utils

labels_directory = "label_persons/"

parser = argparse.ArgumentParser()
parser.add_argument("file_name", action="store")
args = parser.parse_args()

labels = utils.read_labels(labels_directory + args.file_name)

images_to_show = [i for i in utils.get_images() if i not in labels]
current_image = None

root = Tk()
panel1 = Label(root)
panel1.pack(side="left", padx=10)
panel2 = Label(root)
panel2.pack(side="left", padx=10)


def get_new_image():
    global current_image
    if len(images_to_show) == 0:
        root.quit()
        return
    current_image = images_to_show.pop()
    print("data/%s_firefox.png" % current_image)
# mean from dataset058:
mean = [0.3823625683879477, 0.3790166856065496, 0.3554138533338805]
std=[0.21754145353302254, 0.21271749678359336, 0.21233947166469555]

#mean = [0.383661700858527, 0.3819784115384924, 0.3588786631614881]
#std=[0.2167717755518767, 0.21201058526724945, 0.21143164036556178]

# Load the dataset
from utils import read_images_stl10 as read_images
from utils import read_labels_stl10 as read_labels

#unlab_set_x = read_images('../data/stl10_binary/unlabeled_X.bin')
test_set_x = read_images('../../data/stl10_binary/test_X.bin')
train_set_x = read_images('../../data/stl10_binary/train_X.bin')

test_set_y = read_labels('../../data/stl10_binary/test_y.bin')
train_set_y = read_labels('../../data/stl10_binary/train_y.bin')

print 'Train set information: '
print (len(train_set_x), type(train_set_x[3]))
print ''
print 'Test set information: '
print (len(test_set_x), type(test_set_x[3]))
print ''

# loading the folds:
with open('../../data/stl10_binary/fold_indices.txt', 'r') as f_folds:
    folds = f_folds.readlines()

k_folds = []
for fold_i in folds:
import xml.etree.ElementTree as et
import numpy as np
import tensorflow as tf
import time
import sys
import pickle
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from stop_words import get_stop_words
import config
import utils
config.max_characters = 1932
config.vocabulary_size = 4
utils.read_labels("rcv")
import class_DatasetRcv as ds
import cnn as cn
if len(sys.argv) > 1:
    env = sys.argv[1]
else:
    env = "local"

# print(labels)
print("Total labels: ", len(config.labels))
print(config.vocabulary_size)

path = ""
if env == "local":
    path = "data/reuters/"
elif env == "server":
    path = "data/reuters/"
示例#17
0
def train():
    global_step = tf.Variable(0, trainable=False)
    x = tf.placeholder(tf.float32, shape=[args.batch, 32, 32, 1], name='input')
    labels = tf.placeholder(tf.float32, shape=[args.batch, 10], name='labels')
    network = get_network(args.network)(x=x, labels=labels)
    optimizer = tf.train.AdamOptimizer(learning_rate=args.lr)
    loss = network.loss
    optimization_op = optimizer.minimize(loss)
    saver = tf.train.Saver()

    number_of_params = 0
    for variable in tf.trainable_variables():
        number_of_params += np.array(variable.get_shape().as_list()).prod()
    print("number of trainable parameters: {}".format(number_of_params))

    training_images = os.path.join(args.training_folder, 'images')
    training_labels = read_labels(
        os.path.join(args.training_folder, 'labels.txt'))

    validation_images = os.path.join(args.validation_folder, 'images')
    validation_labels = read_labels(
        os.path.join(args.validation_folder, 'labels.txt'))

    config = tf.ConfigProto(allow_soft_placement=True)
    start = time.time()
    best_accuracy = 0
    with tf.Session(config=config) as session:
        session.run(tf.global_variables_initializer())
        session.run(tf.local_variables_initializer())

        valid_images = []
        for step in range(args.step):
            next_batch = get_training_batch(training_images,
                                            training_labels,
                                            batch_size=args.batch)
            loss_value, _ = session.run([loss, optimization_op],
                                        feed_dict={
                                            x: next_batch['images'],
                                            labels: next_batch['labels']
                                        })
            elapsed_time = time.time() - start
            if step and step % 1000 == 0:
                print(
                    'step:{}/{} | loss:{:7.6f} | elapsed time: {:2.0f}m:{:2.0f}s'
                    .format(step, args.step, loss_value, elapsed_time // (60),
                            int(elapsed_time) % 60))

            if step and args.validation_step > 0 and step % args.validation_step == 0:
                good_predictions = 0
                for validation_step in range(0, len(validation_labels),
                                             args.batch):
                    next_validation_batch = get_validation_batch(
                        validation_images,
                        validation_labels,
                        batch_size=args.batch,
                        starting_index=validation_step)
                    predicted_value = session.run(
                        network.prediction,
                        feed_dict={
                            x: next_validation_batch['images'],
                            labels: next_validation_batch['labels']
                        })
                    gts = np.argmax(next_validation_batch['labels'], axis=-1)
                    good_predictions += (predicted_value == gts).sum()
                print('Validation: {}/{} good predictions'.format(
                    good_predictions, len(validation_labels)))
                if good_predictions >= best_accuracy:
                    saver.save(session, './trained_models/best')
                    best_accuracy = good_predictions
        saver.save(session, './trained_models/model', global_step=args.step)
    print('Training ended!')
示例#18
0
@app.route('/download_report')
def download_report():
    try:
        return send_file("/home/pi/tflite1/bir_isim/no_tracking/report/" +
                         report_file_name)
    except FileNotFoundError:
        abort(404)


@app.route('/')
def index():
    return render_template('index.html')


labels = read_labels(PATH_TO_LABELS)
interpreter = initialize_detector(MODEL_NAME)


def m():
    global output, heatmap_image, total_people, field1_count, field2_count

    frame_rate_calc = 1
    freq = cv2.getTickFrequency()

    videostream = VideoStream(VIDEO_PATH).start()

    color_f1 = (0, 0, 255)
    color_f2 = (255, 0, 0)
    heatmap = np.zeros((720, 1270, 3), dtype=np.uint8)
    ht_color = (191, 255, 1)
示例#19
0
#     cols = original_score[:,1].astype(int)
#     computed_scores = A[rows, cols]
#     D = np.eye(len(A))
#     D[rows, cols] = computed_scores
#     D[cols, rows] = computed_scores
#     return D

#############################################################################################
#================================= Data extraction =========================================#
# READ THE FILE
dataset = sys.argv[1]
num_samples = int(sys.argv[3])

reshaped_preds = read_file(file_="../GYPSUM/" + dataset + "_predicts_0.npy")
# READ LABELS FROM FILE
original_scores = read_labels("../GYPSUM/" + dataset + "_label_ids.txt")

# reshaped_preds = reshaped_preds
# reshaped_preds = fix_vals_first(reshaped_preds, original_scores)
# print(reshaped_preds.shape)

# FIND OUT DUPLICATE ROWS AND ELIMINATE!!
unique_rows, unique_indices = np.unique(reshaped_preds,
                                        axis=0,
                                        return_index=True)

# FIND DUPLICATE ROWS AND ID THEM WITH MATCHING ROWS
# Duplicate ids == rows in reshaped_preds which has been removed
# Matched ids = rows in reshaped_preds which has a duplicate in duplcate_ids
unique_indices = np.sort(unique_indices)
# checked, and the following line works fine
import numpy as np
import tensorflow as tf
import time
import pickle
import sys
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer

import config
import utils
utils.read_labels("bibtex")
import class_DatasetRcv as ds

import encoder as au
import mlpau as ml
from stop_words import get_stop_words
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import sparse_ops
env = sys.argv[1]

bpmll_out_module = tf.load_op_library('custom/bp_mll.so')
bpmll_grad_out_module = tf.load_op_library('custom/bp_mll_grad.so')


@ops.RegisterGradient("BpMll")
def _bp_mll_grad(op, grad):
    return bpmll_grad_out_module.bp_mll_grad(grad=grad,
                                             logits=op.inputs[0],
                                             labels=op.inputs[1])
示例#21
0
def main():
    """ Main function """
    # read extracted features
    amigos_data = np.loadtxt('data/mde_features.csv', delimiter=',')
    eeg_entropy = amigos_data[:, :200]
    ecg_entropy = amigos_data[:, 200:206]
    gsr_entropy = amigos_data[:, 206:]

    # read labels and split to 0 and 1 by local mean
    a_labels, v_labels = read_labels('data/signals/label.csv')

    pos_a_idx = np.where(a_labels == 1)[0]
    neg_a_idx = np.where(a_labels == 0)[0]
    pos_v_idx = np.where(v_labels == 1)[0]
    neg_v_idx = np.where(v_labels == 0)[0]

    # EEG
    for d in range(2, 4):
        for r in range(1, 6):
            for typ in ['a', 'v']:
                start_idx = (d - 2) * 5 + (r - 1)
                end_idx = 20 * 2 * 5
                if typ == 'a':
                    pos_values = eeg_entropy[pos_a_idx, start_idx:end_idx:10]
                    neg_values = eeg_entropy[neg_a_idx, start_idx:end_idx:10]
                elif typ == 'v':
                    pos_values = eeg_entropy[pos_v_idx, start_idx:end_idx:10]
                    neg_values = eeg_entropy[neg_v_idx, start_idx:end_idx:10]
                _, ax = plt.subplots()
                for axis in [ax.xaxis, ax.yaxis]:
                    axis.set_major_locator(ticker.MaxNLocator(integer=True))
                ax.errorbar(np.arange(1, 21),
                            np.mean(pos_values, axis=0) / NORM,
                            yerr=np.std(pos_values) / pos_values.shape[0],
                            marker='o',
                            markersize=4,
                            label='pos',
                            color='#E76F51',
                            capsize=4)
                ax.errorbar(np.arange(1, 21),
                            np.mean(neg_values, axis=0) / NORM,
                            yerr=np.std(neg_values) / neg_values.shape[0],
                            marker='o',
                            markersize=4,
                            label='neg',
                            color='#44A261',
                            capsize=4)
                ax.legend()
                ax.margins(0.05)
                ax.axis('tight')
                plt.tight_layout()
                plt.savefig("plot/mde/eeg_mmde/{}_eeg_d{}_r{}.png".format(
                    typ, d, r))
                plt.close()

    # ECG
    for d in range(2, 4):
        for typ in ['a', 'v']:
            start_idx = d - 2
            end_idx = 3 * 2
            if typ == 'a':
                pos_values = ecg_entropy[pos_a_idx, start_idx:end_idx:2]
                neg_values = ecg_entropy[neg_a_idx, start_idx:end_idx:2]
            elif typ == 'v':
                pos_values = ecg_entropy[pos_v_idx, start_idx:end_idx:2]
                neg_values = ecg_entropy[neg_v_idx, start_idx:end_idx:2]
            _, ax = plt.subplots()
            for axis in [ax.xaxis, ax.yaxis]:
                axis.set_major_locator(ticker.MaxNLocator(integer=True))
            ax.errorbar(np.arange(1, 4),
                        np.mean(pos_values, axis=0) / NORM,
                        yerr=np.std(pos_values) / pos_values.shape[0],
                        marker='o',
                        markersize=4,
                        label='pos',
                        color='#E76F51',
                        capsize=4)
            ax.errorbar(np.arange(1, 4),
                        np.mean(neg_values, axis=0) / NORM,
                        yerr=np.std(neg_values) / neg_values.shape[0],
                        marker='o',
                        markersize=4,
                        label='neg',
                        color='#44A261',
                        capsize=4)
            ax.legend()
            ax.margins(0.05)
            ax.axis('tight')
            plt.tight_layout()
            plt.savefig("plot/mde/ecg_rcmde/{}_ecg_d{}.png".format(typ, d))
            plt.close()

    # GSR
    for d in range(2, 4):
        for typ in ['a', 'v']:
            start_idx = d - 2
            end_idx = 20 * 2
            if typ == 'a':
                pos_values = gsr_entropy[pos_a_idx, start_idx:end_idx:2]
                neg_values = gsr_entropy[neg_a_idx, start_idx:end_idx:2]
            elif typ == 'v':
                pos_values = gsr_entropy[pos_v_idx, start_idx:end_idx:2]
                neg_values = gsr_entropy[neg_v_idx, start_idx:end_idx:2]
            _, ax = plt.subplots()
            for axis in [ax.xaxis, ax.yaxis]:
                axis.set_major_locator(ticker.MaxNLocator(integer=True))
            ax.errorbar(np.arange(1, 21),
                        np.mean(pos_values, axis=0) / NORM,
                        yerr=np.std(pos_values) / pos_values.shape[0],
                        marker='o',
                        markersize=4,
                        label='pos',
                        color='#E76F51',
                        capsize=4)
            ax.errorbar(np.arange(1, 21),
                        np.mean(neg_values, axis=0) / NORM,
                        yerr=np.std(neg_values) / neg_values.shape[0],
                        marker='o',
                        markersize=4,
                        label='neg',
                        color='#44A261',
                        capsize=4)
            ax.legend()
            ax.margins(0.05)
            ax.axis('tight')
            plt.tight_layout()
            plt.savefig("plot/mde/gsr_rcmde/{}_gsr_d{}.png".format(typ, d))
            plt.close()
from index import run_script, run_knn, run_logistic, run_naive_bayes, run_tree, run_evaluation
from utils import read_labels, show_estimation_utility
import constants

data1 = input('Enter data-set path for training. (Ex: file.txt): ')
data2 = input('Enter data-set path for training: ')
chosen_model = input(
    'Enter model name for testing. '
    'Options: KNN, naiveBayes, logisticRegression, treeClassifier, finalModel: '
)
test_file = input('Enter test file: ')
test_labels_file = input('Enter test labels file: ')

tweets, data_vectors, test_vector = run_script(data1, data2, test_file)
test_labels = read_labels(test_labels_file)

if chosen_model == 'KNN':
    run_knn(data_vectors, test_vector)
    confusion_matrix, accuracy, precision_recall_fscore = run_evaluation(
        test_labels, constants.KNN_PREDICTION)
    show_estimation_utility(confusion_matrix, accuracy,
                            precision_recall_fscore)
elif chosen_model == 'naiveBayes':
    run_naive_bayes(test_vector, constants.BAYES_MODEL)
    confusion_matrix, accuracy, precision_recall_fscore = run_evaluation(
        test_labels, constants.BAYES_PREDICTION)
    show_estimation_utility(confusion_matrix, accuracy,
                            precision_recall_fscore)
elif chosen_model == 'logisticRegression':
    run_logistic(test_vector)
    confusion_matrix, accuracy, precision_recall_fscore = run_evaluation(
示例#23
0
print(imgList[:5])
for img in imgList:
    im = cv2.imread(img, cv2.IMREAD_COLOR).astype(np.float32) / 255.
    im = np.reshape(
        im, [FLAGS.image_height, FLAGS.image_width, FLAGS.image_channel])
    imgs_input.append(im)
imgs_input = np.asarray(imgs_input)
model = cnn_lstm_otc_ocr.LSTMOCR('infer')
model.build_graph()

# 定义节点
logit = model.get_logist()

with tf.Session(config=config) as sess:
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver(tf.global_variables(), max_to_keep=100)
    ckpt = tf.train.latest_checkpoint(FLAGS.checkpoint_dir)

    saver.restore(sess, ckpt)
    print('restore from ckpt  {}'.format(ckpt))

    feed = {model.inputs: imgs_input}
    dense_decoded_code = sess.run(model.dense_decoded, feed)

    decoded_expression = utils.read_labels(dense_decoded_code)

    print(sess.run(logit, feed))

for fn, code in zip(imgList, decoded_expression):
    print(fn + ' : ' + code)
示例#24
0
def main():
    """ Main function
    """
    parser = ArgumentParser(
        description='Affective Computing with AMIGOS Dataset -- Tuning')
    parser.add_argument('--data', type=str, default='./data')
    parser.add_argument('--feat',
                        type=str,
                        choices=['eeg', 'ecg', 'gsr', 'all'],
                        default='all',
                        help='choose type of modality')
    parser.add_argument('--clf',
                        type=str,
                        choices=['gnb', 'svm', 'xgb'],
                        default='xgb',
                        help='choose type of classifier')
    parser.add_argument('--old', action='store_true')
    args = parser.parse_args()

    # read extracted features
    if args.old:
        data = np.loadtxt(os.path.join(args.data, 'features.csv'),
                          delimiter=',')[:, :213]
        if args.feat == 'eeg':
            data = data[:, 108:213]
        elif args.feat == 'ecg':
            data = data[:, 32:108]
        elif args.feat == 'gsr':
            data = data[:, 0:32]
        amigos_data = (data, data)
    else:
        a_data = np.loadtxt(os.path.join(args.data, 'a_features.csv'),
                            delimiter=',')
        v_data = np.loadtxt(os.path.join(args.data, 'v_features.csv'),
                            delimiter=',')
        if args.feat == 'eeg':
            a_data = np.hstack((a_data[:, 108:213], a_data[:, 229:232]))
            v_data = np.hstack((v_data[:, 108:213], v_data[:, 217:316]))
        elif args.feat == 'ecg':
            a_data = np.hstack(
                (a_data[:, 32:108], a_data[:, 213:215], a_data[:, 232:234]))
            v_data = np.hstack(
                (v_data[:, 32:108], v_data[:, 213:217], v_data[:, 316:324]))
        elif args.feat == 'gsr':
            a_data = np.hstack(
                (a_data[:, 0:32], a_data[:, 215:229], a_data[:, 234:254]))
            v_data = v_data[:, 0:32]
        amigos_data = (a_data, v_data)

    # read labels and split to 0 and 1 by
    a_labels, v_labels = read_labels(os.path.join(args.data, 'label.csv'))
    labels = (a_labels, v_labels)

    # setup kfold cross validator
    sub_num = SUBJECT_NUM - len(MISSING_DATA_SUBJECT)
    kf = KFold(n_splits=sub_num)

    # tune classifier parameters
    grid_search_params = {'max_depth': [], 'n_estimators': []}
    other_tuning_params = {
        'learning_rate': np.arange(0.01, 0.41, 0.01),
        'gamma': np.arange(0, 10.1, 0.5),
        'min_child_weight': np.arange(0.80, 1.21, 0.01),
        'max_delta_step': np.arange(0, 2.05, 0.05),
        'subsample': np.arange(1.00, 0.59, -0.01),
        'colsample_bytree': np.arange(1.00, 0.09, -0.01),
        'colsample_bylevel': np.arange(1.00, 0.09, -0.01),
        'reg_alpha': np.arange(0, 2.05, 0.05),
        'reg_lambda': np.arange(0.50, 2.55, 0.05),
        'scale_pos_weight': np.arange(0.80, 1.21, 0.01),
        'base_score': np.arange(0.40, 0.61, 0.01),
        'seed': np.arange(0, 41)
    }

    # with open(os.path.join(args.data, 'model', "old_a_{}_model.pkl".format(args.feat)), 'rb') as f:
    #     a_param = pickle.load(f)
    # with open(os.path.join(args.data, 'model', "old_v_{}_model.pkl".format(args.feat)), 'rb') as f:
    #     v_param = pickle.load(f)

    # grid search tuning
    a_best_params = {'max_depth': 3, 'n_estimators': 20}
    v_best_params = {'max_depth': 3, 'n_estimators': 20}
    a_acc, v_acc = 0, 0

    print('Tuning max_depth and n_estimators')
    for param in grid_search_params['max_depth']:
        print('max_depth', param)
        clf = {
            'a': xgb.XGBClassifier(max_depth=param,
                                   objective="binary:logistic"),
            'v': xgb.XGBClassifier(max_depth=param,
                                   objective="binary:logistic")
        }
        tuning_params = grid_search_params['n_estimators']
        a_param, v_param, tmp_a_acc, tmp_v_acc = tuning(
            clf, 'n_estimators', tuning_params, amigos_data, labels, kf)
        if tmp_a_acc >= a_acc:
            a_best_params['max_depth'] = param
            a_best_params['n_estimators'] = a_param
            a_acc = tmp_a_acc
        if tmp_v_acc >= v_acc:
            v_best_params['max_depth'] = param
            v_best_params['n_estimators'] = v_param
            v_acc = tmp_v_acc

    # tune other parameters
    for param_name, tuning_params in other_tuning_params.items():
        print('Tuning', param_name)
        clf = {
            'a': xgb.XGBClassifier(objective="binary:logistic"),
            'v': xgb.XGBClassifier(objective="binary:logistic")
        }
        clf['a'].set_params(**a_best_params)
        clf['v'].set_params(**v_best_params)
        a_param, v_param, _, _ = tuning(clf, param_name, tuning_params,
                                        amigos_data, labels, kf)
        a_best_params[param_name] = a_param
        v_best_params[param_name] = v_param

    ver = 'old' if args.old else 'new'
    with open(
            os.path.join(args.data, 'model',
                         "{}_a_{}_model.pkl".format(ver, args.feat)),
            'wb') as f:
        pickle.dump(a_best_params, f)
    with open(
            os.path.join(args.data, 'model',
                         "{}_v_{}_model.pkl".format(ver, args.feat)),
            'wb') as f:
        pickle.dump(v_best_params, f)
示例#25
0
    def __getitem__(
        self, idx: int
    ) -> Tuple[torch.tensor, Dict[str, torch.tensor], str]:
        """
        Get dataset item.

        Parameters
        ----------
        idx : int
            Dataset item index

        Returns
        -------
        Tuple[Tensor, Dict[str, Tensor], str]
            (image, target, image_id)
        """
        image_id = self.image_ids[idx]
        image = cv2.imread(f"{self.image_dir}/{image_id}", cv2.IMREAD_COLOR)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)

        # test dataset must have some values so that transforms work.
        target = {
            "labels": torch.as_tensor([[0]], dtype=torch.float32),
            "boxes": torch.as_tensor([[0, 0, 0, 0]], dtype=torch.float32),
        }

        # for train and valid test create target dict.
        if self.mode != "test":
            image_data = self.df.loc[self.df["image_id"] == image_id]
            boxes = image_data[["x", "y", "x1", "y1"]].values
            boxes = torch.as_tensor(boxes, dtype=torch.float32)

            areas = image_data["area"].values
            areas = torch.as_tensor(areas, dtype=torch.float32)

            label_dict = read_labels(
                to_absolute_path(self.cfg.data.labels_path)
            )
            labels = [
                label_dict[label] for label in image_data["label"].values
            ]
            labels = torch.as_tensor(labels, dtype=torch.int64)

            iscrowd = torch.zeros((image_data.shape[0],), dtype=torch.int64)

            target["boxes"] = boxes
            target["labels"] = labels
            target["image_id"] = torch.tensor([idx])
            target["area"] = areas
            target["iscrowd"] = iscrowd

            if self.transforms:
                image_dict = {
                    "image": image,
                    "bboxes": target["boxes"],
                    "labels": labels,
                }
                image_dict = self.transforms(**image_dict)
                image = image_dict["image"]
                target["boxes"] = torch.as_tensor(
                    image_dict["bboxes"], dtype=torch.float32
                )

        else:
            image_dict = {
                "image": image,
                "bboxes": target["boxes"],
                "labels": target["labels"],
            }
            image = self.transforms(**image_dict)["image"]

        return image, target, image_id
示例#26
0
            text += utils.read_text(i)
        utils.logger.info("text reading finished")
        tokens = utils.tokenize_paragraph_w2v(text)
        utils.logger.info("text tokenizing finished")
        utils.compute_paragraph_word2vec(tokens,
                                         vector_size=args.vector_size,
                                         workers=multiprocessing.cpu_count(),
                                         model_path=args.wm)
        utils.logger.info("word2vec training finished")

    elif args.train_cnn or args.predict_cnn or args.validate_cnn:
        train_data = None
        test_data = None
        if args.train_cnn or args.validate_cnn:
            loaded = False
            train_labels = utils.read_labels(args.l)
            utils.logger.info("labels reading finished")

            if args.use_d2v and utils.utils.is_path_accessible(args.dd):
                utils.logger.info(
                    "try to load from doc2vec train data from specified path")
                train_data = utils.read_array(args.dd)
                utils.logger.info("load doc2vec train data successfully")
                loaded = True
            elif args.use_w2v and utils.is_path_accessible(args.wd):
                utils.logger.info(
                    "try to load from word2vec train data from specified path")
                train_data = utils.read_array(args.wd)
                utils.logger.info("load word2vec train data successfully")
                loaded = True
示例#27
0
from neuralnetwork import NeuralNetworkModel
import autograd.numpy as np
import pickle
import matplotlib.pyplot as plt
from utils import plt_image

TRAINING_IMAGES_PATH = 'mnistset/train-images-idx3-ubyte.gz'
TRAINING_LABELS_PATH = 'mnistset/train-labels-idx1-ubyte.gz'
TESTING_IMAGES_PATH = 'mnistset/t10k-images-idx3-ubyte.gz'
TESTING_LABELS_PATH = 'mnistset/t10k-labels-idx1-ubyte.gz'
MODELS_PATH = 'mnistmodels/'

print 'Reading images from %s' % TRAINING_IMAGES_PATH
images = utils.read_images(TRAINING_IMAGES_PATH)
print 'Reading labels from %s' % TRAINING_LABELS_PATH
labels = utils.read_labels(TRAINING_LABELS_PATH)
print 'Reading images from %s' % TESTING_IMAGES_PATH
testing_images = utils.read_images(TESTING_IMAGES_PATH)
print 'Reading labels from %s' % TESTING_LABELS_PATH
testing_labels = utils.read_labels(TESTING_LABELS_PATH)
training_images = images[5000:-5000]
training_labels = labels[5000:-5000]
validating_images = np.concatenate([images[:5000], images[-5000:]])
validating_labels = np.concatenate([labels[:5000], labels[-5000:]])

np.set_printoptions(suppress=True)
nnm = None


def create(layers=[28 * 28, 100, 10], batch_size=32, dropout=0.1):
    print 'Creating neural network'
示例#28
0
import predictor
import FGSM_utils
import utils
import sys

if __name__ == '__main__':
    PGM_NUM = 154
    pgms = [
        utils.read_PGM(f"../data/pgm/{i}.pgm") for i in range(1, PGM_NUM + 1)
    ]
    labels = utils.read_labels("../data/labels.txt", PGM_NUM)
    params = predictor.read_params("../data/param.txt")

    pred = predictor.Predictor(params)
    EPS = float(sys.argv[1])

    for i in range(PGM_NUM):
        x, raw_x = pgms[i]
        t = labels[i] - 1

        pred.forward(x)
        dx = pred.backward(t)  # get the gradient vector
        sign = FGSM_utils.sign(dx)
        prx = FGSM_utils.perturb(raw_x, sign, EPS)

        utils.write_PGM(f"../data/FGSM_{EPS:.3f}/{i+1}.pgm", prx)
示例#29
0
from neuralnetwork import NeuralNetworkModel
import autograd.numpy as np
import pickle
import matplotlib.pyplot as plt
from utils import plt_image

TRAINING_IMAGES_PATH = 'mnistset/train-images-idx3-ubyte.gz'
TRAINING_LABELS_PATH = 'mnistset/train-labels-idx1-ubyte.gz'
TESTING_IMAGES_PATH = 'mnistset/t10k-images-idx3-ubyte.gz'
TESTING_LABELS_PATH = 'mnistset/t10k-labels-idx1-ubyte.gz'
MODELS_PATH = 'mnistmodels/'

print 'Reading images from %s' % TRAINING_IMAGES_PATH
images = utils.read_images(TRAINING_IMAGES_PATH)
print 'Reading labels from %s' % TRAINING_LABELS_PATH
labels = utils.read_labels(TRAINING_LABELS_PATH)
print 'Reading images from %s' % TESTING_IMAGES_PATH
testing_images = utils.read_images(TESTING_IMAGES_PATH)
print 'Reading labels from %s' % TESTING_LABELS_PATH
testing_labels = utils.read_labels(TESTING_LABELS_PATH)
training_images = images[5000:-5000]
training_labels = labels[5000:-5000]
validating_images = np.concatenate([images[:5000], images[-5000:]])
validating_labels = np.concatenate([labels[:5000], labels[-5000:]])

np.set_printoptions(suppress=True)
nnm = None


def create(layers=[28*28, 100, 10], batch_size=32, dropout=0.1):
    print 'Creating neural network'
示例#30
0
        X = np.concatenate((self.descs[self.pos_candidates, :],
                            self.descs[self.neg_candidates]))

        return Y, X

    def get_n_negs(self, N_neg=None):
        if (self._has_pos()) and (N_neg is None):
            N_neg = self.xy_pos.shape[0]
        elif (not self._has_pos()):
            N_neg = self.default_N_negs

        return N_neg


if __name__ == "__main__":

    xy_pos = utls.read_labels('../dataset/labels/0.txt')
    xy, descs = utls.read_feats('../feats/0.npz')
    im = io.imread('../dataset/images/0.png')

    sampler = Sampler(xy_pos, xy, descs)
    Y, f, xy = sampler.sample()

    import matplotlib.pyplot as plt
    _, ax = plt.subplots()
    ax.imshow(im, cmap='gray')
    utls.plot_bboxes(xy[Y == 1, :], im.shape, ax, color='g')
    utls.plot_bboxes(xy[Y == 0, :], im.shape, ax, color='r')

    plt.show()