Пример #1
0
def test4(verbose=False):
    """
        Test 4: ModelFactory Velocity update functionality
        Base on Test 1, 2, 3
    """
    print("-------- Start of Test 4 --------")
    x = [[1.2, 0.9], [1.4, 0.4]]
    y = [[0.1, 1.5], [0.5, 0.7]]

    my_print("input x", x, verbose)
    my_print("input y", y, verbose)
    w_number_list = []

    input_dimension = len(x[0])  # Dimension of input vector
    output_dimension = len(y[0])  # Dimension of output vector
    batch_number = len(x)  # Number of batch size

    test = ModelFactory(input_dimension, output_dimension, w_number_list, batch_number, 0.9)

    my_print("Momentum", test.update_momentum, verbose)

    for i in range(test.layer_num):
        my_print("W[%d]" % i, test.W[i].get_value(), verbose)
        my_print("B[%d]" % i, test.B[i].get_value(), verbose)

    test.train_one(x, y)

    for i in range(test.layer_num):
        my_print("W[%d]" % i, test.W[i].get_value(), verbose)
        my_print("B[%d]" % i, test.B[i].get_value(), verbose)

    print("--------- End of Test 4 ---------")
Пример #2
0
def test5(verbose=False):
    """
        Test 5: ModelFactory Convergence test
        Base on Test 1, 2, 3, 4
    """
    print("-------- Start of Test 5 --------")
    x = [[1.2, 0.9], [1.4, 0.4]]
    y = [[0.1, 0.5], [0.5, 0.2]]

    # x = [[1.2, 0.9]]
    # y = [[0.1, 0.5]]

    my_print("input x", x, verbose)
    my_print("input y", y, verbose)
    w_number_list = [3]

    input_dimension = len(x[0])  # Dimension of input vector
    output_dimension = len(y[0])  # Dimension of output vector
    batch_number = len(x)  # Number of batch size

    test = ModelFactory(input_dimension, output_dimension, w_number_list, batch_number, 0.001)
    for _ in range(5):
        test.train_one(x, y)

    my_print("(Before)")
    for i in range(test.layer_num):
        my_print("W[%d]" % i, test.W[i].get_value())
        my_print("W_Velocity[%d]" % i, test.W_velocity[i].get_value())

    for i in range(test.batch_num):
        my_print("Grad on batch[%d]" % i, test.grad_function[i](x, y))

    my_print("(After grad_function)")
    for i in range(test.layer_num):
        my_print("W[%d]" % i, test.W[i].get_value())
        my_print("W_Velocity[%d]" % i, test.W_velocity[i].get_value())

    for i in range(test.layer_num):
        test.update_param_function[i](0)

    my_print("(After update_param_function)")
    for i in range(test.layer_num):
        my_print("W[%d]" % i, test.W[i].get_value())
        my_print("W_Velocity[%d]" % i, test.W_velocity[i].get_value())

    for i in range(test.layer_num):
        test.post_update_param_function[i](0)

    my_print("(After post_update_param_function)")
    for i in range(test.layer_num):
        my_print("W[%d]" % i, test.W[i].get_value())
        my_print("W_Velocity[%d]" % i, test.W_velocity[i].get_value())

    print("--------- End of Test 5 ---------")
Пример #3
0
def test2(verbose=False):
    """
        Test 2: ModelFactory grad function
        Base on Test 1
    """
    print("-------- Start of Test 2 --------")
    x = [[1.2, 0.9], [1.4, 0.4]]
    y = [[2.0, 1.1], [3.0, 0.7]]

    # print "* input x:\n\t", x
    # print "* input y:\n\t", y
    my_print("input x", x, verbose)
    my_print("input y", y, verbose)
    w_number_list = []

    input_dimension = len(x[0])  # Dimension of input vector
    output_dimension = len(y[0])  # Dimension of output vector
    batch_number = len(x)  # Number of batch size

    test = ModelFactory(input_dimension, output_dimension, w_number_list, batch_number, 0.01)

    my_print("W", test.W[0].get_value(), verbose)
    my_print("B", test.B[0].get_value(), verbose)

    a = np.dot(np.matrix(x), test.W[0].get_value()) + test.B[0].get_value()
    y_e = test.y_evaluated_function(x)
    cost = test.cost_function(x, y)

    my_print("Grad[0]", test.grad_function_no_update[0](x, y), verbose)
    my_print("Grad[1]", test.grad_function_no_update[1](x, y), verbose)

    grad_w_0 = np.zeros((2, 2))
    # print (y_e[0][0] * y_e[0][0]) * np.exp(-a[0].item((0, 0))) * x[0][0] / cost[0] * (y_e[0][0] - y[0][0])
    grad_w_0[0][0] = (y_e[0][0] * y_e[0][0]) * np.exp(-a.item((0, 0))) * x[0][0] / cost[0] * (y_e[0][0] - y[0][0])
    grad_w_0[0][1] = (y_e[0][1] * y_e[0][1]) * np.exp(-a.item((0, 1))) * x[0][0] / cost[0] * (y_e[0][1] - y[0][1])
    grad_w_0[1][0] = (y_e[0][0] * y_e[0][0]) * np.exp(-a.item((0, 0))) * x[0][1] / cost[0] * (y_e[0][0] - y[0][0])
    grad_w_0[1][1] = (y_e[0][1] * y_e[0][1]) * np.exp(-a.item((0, 1))) * x[0][1] / cost[0] * (y_e[0][1] - y[0][1])
    my_assert("Grad on W of batch 0", grad_w_0, test.grad_function_no_update[0](x, y)[0])

    grad_b_1 = np.zeros((2, 2))
    grad_b_1[0][0] = (y_e[1][0] * y_e[1][0]) * np.exp(-a.item((1, 0))) / cost[1] * (y_e[1][0] - y[1][0])
    grad_b_1[0][1] = (y_e[1][1] * y_e[1][1]) * np.exp(-a.item((1, 1))) / cost[1] * (y_e[1][1] - y[1][1])
    grad_b_1[1][0] = (y_e[1][0] * y_e[1][0]) * np.exp(-a.item((1, 0))) / cost[1] * (y_e[1][0] - y[1][0])
    grad_b_1[1][1] = (y_e[1][1] * y_e[1][1]) * np.exp(-a.item((1, 1))) / cost[1] * (y_e[1][1] - y[1][1])
    my_assert("Grad on B of batch 1", grad_b_1, test.grad_function_no_update[1](x, y)[1])

    print("--------- End of Test 2 ---------")
Пример #4
0
 def create_model(self):
     self.model = ModelFactory(
         self.input_dimension,
         self.output_dimension,
         self.layer,
         self.batch_number,
         self.lr
     )
Пример #5
0
def test1(verbose=False):
    """
        Test 1: ModelFactory cost function and evaluate function
    """
    print("-------- Start of Test 1 --------")
    x = [[1.2, 1.0], [1.4, 1.0]]
    y = [[2.0, 1.1], [3.0, 1.0]]

    # print "* input x:\n\t", x
    # print "* input y:\n\t", y
    my_print("input x", x, verbose)
    my_print("input y", y, verbose)
    w_number_list = []

    input_dimension = len(x[0])  # Dimension of input vector
    output_dimension = len(y[0])  # Dimension of output vector
    batch_number = len(x)  # Number of batch size

    test = ModelFactory(input_dimension, output_dimension, w_number_list, batch_number, 0.01)
    my_print("W", test.W[0].get_value(), verbose)
    my_print("B", test.B[0].get_value(), verbose)

    _1 = np.dot(np.matrix(x), test.W[0].get_value())
    my_print("Step 1", _1, verbose)

    _2 = _1 + test.B[0].get_value()
    my_print("Step 2", _2, verbose)

    _3 = (1 + np.tanh(_2 / 2)) / 2
    my_print("Step 3,", _3, verbose)

    _4 = _3 - np.matrix(y)
    my_print("Step 4", _4, verbose)

    _5 = np.linalg.norm(_4, ord=2, axis=1)
    my_print("Step 5", _5, verbose)

    my_print("Evaluate function", test.y_evaluated_function(x), verbose)

    my_print("Cost function", test.cost_function(x, y), verbose)

    my_assert("Evaluate function", _3, test.y_evaluated_function(x))
    my_assert("Cost function", _5, test.cost_function(x, y))

    print("--------- End of Test 1 ---------")
Пример #6
0
    def __init__(self, args):
        assert len(args) >= 3, 'invalid syntax; try --help'

        self.exp_folder = args.pop(0)
        self.model = args.pop(0)
        self.opt_choice = args.pop(0)
        self.bounds_mul = 2.0
        self.targets = map(float, args)

        model = ModelFactory.create(self)
        assert len(args) == model.param_count, \
            'invalid syntax; try --help'
Пример #7
0
def main():

    if '--help' in sys.argv:
        print USAGE.format(name=os.path.basename(sys.argv[0]))
        exit()

    options = Options(sys.argv)

    model = ModelFactory.create(options)

    optimiser = OptimiserFactory.create(options)

    optimiser.maximise(model.likelihood)
Пример #8
0
    def __init__(self, args):
        assert len(args) >= 4, 'invalid syntax; try --help'

        self.chunk_size = None
        while '--chunk-size' in args:
            index = args.index('--chunk_size')
            assert len(args) > index, 'invalid syntax; try --help'
            args.pop(index)
            self.chunk_size = int(args.pop(index))
            assert self.chunk_size > 0, 'invalid chunk size'

        self.exp_folder = args.pop(0)
        self.model = args.pop(0)
        self.fasta = args

        model = ModelFactory.create(self)
        assert len(self.fasta) == model.fasta_count, \
            'invalid syntax; try --help'
Пример #9
0
    def execute_training_and_evaluation():
        training_data_x, training_labels_y = DatasetFactory.load_training_data_and_its_labels(
        )
        evaluation_data_x, evaluation_labels_y = DatasetFactory.load_evaluation_data_and_its_labels(
        )

        network_model = ModelFactory.create_fpool3_model()

        ModelService.compile_and_fit_the_model(network_model, training_data_x,
                                               training_labels_y)
        accuracy = ModelService.evaluate_the_model(network_model,
                                                   evaluation_data_x,
                                                   evaluation_labels_y)

        file_name = ModelService.save_model_and_configuration(
            network_model, accuracy)

        print(
            'Jarvis hot word detector training has completed. File stored @ ' +
            file_name)
Пример #10
0
#Define the data generator parameters
train_generator = ImageDataGenerator(rescale=1. / 255.,
                                     shear_range=0.2,
                                     zoom_range=0.2,
                                     width_shift_range=0.1,
                                     height_shift_range=0.1,
                                     horizontal_flip=True,
                                     rotation_range=20,
                                     vertical_flip=True)

val_generator = ImageDataGenerator(rescale=1. / 255.)
test_generator = ImageDataGenerator(rescale=1. / 255.)

#Create model
model = ModelFactory.MakeModel(MODEL_NAME)

#Print the summary
model.summary()

#Define callbacks
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0.01, patience=10)
model_checkpoint = ModelCheckpoint('{0}_model.{1}-{2}.h5'.format(
    MODEL_NAME, '{epoch:02d}', '{val_loss:.7f}'),
                                   save_best_only=False)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5)

#Initialize data generators
train_datagenerator = train_generator.flow_from_directory(
    '../data/images/train',
    target_size=IMG_SIZE,
Пример #11
0
test_array = []
for i in range( len(test_ques) ):
	test_array.append( np.concatenate( (test_ques[i], test_imag[i]), axis=1) )

test_c = MAP_1001TO5(1)
Y=None
test_ans.write('q_id,ans\n')

W_number_list = [1024,1024]
input_dimension = 2200  # Dimension of input vector
output_dimension = 1001 # Dimension of output vector
batch_number = 4 # Number of batch size
LR = 0.001

test = ModelFactory(input_dimension, output_dimension, W_number_list, batch_number, LR)
test.load_parm(W_parm,B_parm,vW_parm,vB_parm)

count_others = 0
for i in range(len(test_ques)):
	#Ya= test.y_evaluated_function([test_array[i][1:70]], Y)[0]
	Ya= test.y_evaluated_function([test_array[i].astype(dtype = theano.config.floatX)] , Y)[0]
	test_ans.write(name[i])
	test_ans.write(',')
	word = test_c.Map_Know_Ans(Ya,i+1)
	#if word =='Z':
	#	test_ans.write(feng_ans[i])
	#	count_others = count_others+1
	#else:
	test_ans.write(word)
	if i!=len(test_ques)-1:
Пример #12
0
class TestBench:
    def __init__(self):
        # Define training file
        self.train_file = "train_sub.ark"
        self.train_input_data = []
        self.answer_map_file = "answer_map_sub.txt"
        self.train_answer_data = []
        self.train_segment = 400

        # Define test file
        self.test_data = []
        self.test_input_file = "test_sub.ark"
        self.test_output_file = "test_ans.csv"
        self.W_parm = None
        self.B_parm = None

        # Define model parameter
        self.adagrad_enable = False  # should be defined in the model
        self.layer = [512]

        self.input_dimension = 69  # Dimension of input vector
        self.output_dimension = 48  # Dimension of output vector
        self.batch_number = 3  # Number of batch size
        self.lr = 0.001

        self.modified = False
        self.correct = 0
        self.total = 0
        self.current = 0
        self.cost = {}
        self.acc = []

        self.model = None

        self.ans_type = [
            "aa", "ae", "ah", "ao", "aw", "ax", "ay", "b", "ch", "cl", "d",
            "dh", "dx", "eh", "el", "en", "epi", "er", "ey", "f", "g", "hh",
            "ih", "ix", "iy", "jh", "k", "l", "m", "ng", "n", "ow", "oy", "p",
            "r", "sh", "sil", "s", "th", "t", "uh", "uw", "vcl", "v", "w",
            "y", "zh", "z"
        ]

        self.prompt = "Enter a command:"
        while True:
            self.prompt = "Enter a command:" if self.modified is False else "Enter a command[Modified]:"

            command = raw_input(BColors.HEADER + self.prompt + BColors.ENDC)
            self.__exec_command__(command)

    def create_model(self):
        self.model = ModelFactory(
            self.input_dimension,
            self.output_dimension,
            self.layer,
            self.batch_number,
            self.lr
        )

    def __exec_command__(self, cmd):
        dispatcher = {
            "status": self.status,
            "train": self.train,
            "load": self.load,
            "run": self.run,
            "output": self.output,
            "set": self.set,
            "quit": self.quit
        }
        commands = cmd.strip().split(';')

        for x in commands:
            m = x.strip().split()
            if len(m) == 0:
                return
            func = dispatcher.get(m[0], lambda (xx): self.__do_nothing__)
            func(m)

    def __do_nothing__(self, x):
        print BColors.WARNING, "Command not found.", BColors.ENDC
        pass

    '''
    #################### Status Functions #####################################
    '''

    def status(self, param):
        if self.model is not None:
            my_print("Model have been created")
            if self.modified:
                my_print("Correct ratio", self)
                pass
            else:
                pass
            # my_print("Adagrad enable", "True" if self.adagrad_enable else "False")
            # my_print("Dropout enable", "True" if self.dropout_enable else "False")
            pass
        else:
            print BColors.OKGREEN, "Model haven't been created", BColors.ENDC
            my_print("Layer", self.layer)
            my_print("Batch number", self.batch_number)
            my_print("Input dimension", self.input_dimension)
            my_print("Output dimension", self.output_dimension)
            my_print("Learning rate", self.lr)

    '''
    #################### Training Functions ###################################
    '''
    @staticmethod
    def update_display(cur, total, cur_epoch, cost, acc):
        stdout.write(
            "\r" + BColors.OKBLUE + "Progress: %d/%d (%.2f %%)" % (cur, total, float(cur) / total * 100) +
            "\t" + "Current epoch: %d" % cur_epoch +
            "\t" + "Cost: %.2f" % cost +
            "\t" + "ACC: %.2f" % float(acc)
        )
        pass

    def train(self, param):
        epoch = 1
        if len(param) > 1 and param[1].isdigit():
            epoch = int(param[1])
        self.train_data(epoch)

    def train_data(self, epoch):
        train_times = self.train_segment * epoch
        if len(self.train_input_data) == 0:
            self.load_train_input_data()
        if self.model is None:
            self.create_model()

        train_batch_x = range(self.batch_number)
        train_batch_y = range(self.batch_number)
        i = 0
        m = 0
        cost = 0
        acc = 0
        while i < train_times:
            for j in range(self.batch_number):
                num = random.randrange(0, self.train_segment)
                train_batch_x[j], train_batch_y[j] = self.get_one_data(num), self.get_one_answer(num)

            self.model.train_one(train_batch_x, train_batch_y)

            i += self.batch_number
            m += self.batch_number
            if m > self.train_segment:
                cost = self.model.cost_function(train_batch_x, train_batch_y)
                acc = self._test(self.train_segment)
                m -= self.train_segment

            TestBench.update_display(cur=i,
                                     total=train_times,
                                     cur_epoch=i / self.train_segment,
                                     cost=cost,
                                     acc=acc
                                     )
        stdout.write("\n")

    '''
    #################### Load Functions #######################################
    '''

    def load(self, param):
        id = 0
        if len(param) > 1 and param[1].isdigit():
            id = int(param[1])
        self.load_parameter(id)

    def load_parameter(self, id):
        filename_w = "parameter_W_%s.txt" % id
        filename_b = "parameter_B_%s.txt" % id
        filename_i = "parameter_I_%s.txt" % id
        my_print("Load parameters from parameter_X_%s.txt" % id)

        # TODO: Keep parameters of two test consistent

        try:
            i_parm_data = open(filename_i, 'r')
            # self.update_parameter(i_parm_data)
            if self.model is None:
                self.create_model()

            w_parm_data = file(filename_w, 'rb')
            b_parm_data = file(filename_b, 'rb')
            w_parm = cPickle.load(w_parm_data)
            b_parm = cPickle.load(b_parm_data)
            self.model.load_parm(w_parm, b_parm)
        except IOError:
            my_print(BColors.FAIL + "File not found. Do nothing." + BColors.ENDC)
            return

    def get_one_data(self, num):
        return self.train_input_data[num][1:self.input_dimension + 1]

    def get_one_answer(self, num):
        type_index = self.ans_type.index(str(self.train_answer_data[num][1].strip()))
        g = [0] * self.output_dimension
        g[type_index] = 1
        return g

    def run(self, param):
        pass

    '''
    #################### Output Functions #####################################
    '''

    def output(self, param):
        output_dispatcher = {
            "progress": self._output_progress,
            "csv": self._output_csv
        }
        if len(param) > 1:
            output_command = output_dispatcher.get(param[1], lambda (xx): self.__do_nothing__)
            output_command(param)
            return
        else:
            print BColors.WARNING + "Output parameter without any argument. Do nothing." + BColors.ENDC
            return
        pass

    def _output_progress(self, param):
        pass

    def _output_csv(self, param):
        _id = 0
        if len(param) > 2 and param[2].isdigit():
            _id = int(param[2])

        if len(self.test_data) == 0:
            self.load_test_data()

        filename_test = "test_ans_%d.txt" % _id
        test_stream = open(filename_test, 'w')
        my_print("Writing test answer data to %s" % filename_test)

        test_c = MAP()

        test_stream.write('Id,Prediction\n')

        for i in range(len(self.test_data)):
            x, y = [self.get_one_data(i)], [[0] * self.output_dimension]
            ya = self.model.y_evaluated_function(x, y)
            value = "%s,%s" % (self.test_data[i][0], test_c.map(ya))
            test_stream.write(value)
            test_stream.write('\n')

    def set(self, param):
        set_dispatcher = {
            "layer": self._set_layer,
            "batch": self._set_batch
        }
        if len(param) > 2:
            set_command = set_dispatcher.get(param[1], lambda: "Set parameter undefined. Do nothing")
            set_command(param)
            return
        else:
            print BColors.WARNING + "Set parameter without any argument. Do nothing." + BColors.ENDC
            return

    def _set_layer(self, param):
        start_with = 2
        layer = []
        if self.model is not None:
            if len(param) > 3 and param[2] == "force":
                start_with = 3
            else:
                x = raw_input(BColors.WARNING + "Set layer number without save parameters? [y/N]" + BColors.ENDC)
                if x == "y" or x == 'Y':
                    pass
                    self.model = None
                else:
                    return

        for i in range(start_with, len(param)):
            if param[i].isdigit():
                d = int(param[i])
                layer.append(d)

        self.layer = layer
        my_print("Layer are set to " + self.layer.__str__())

    def _set_batch(self, param):
        start_with = 2
        if self.model is not None:
            if len(param) > 3 and param[2] == "force":
                start_with = 3
                pass
            else:
                x = raw_input(BColors.WARNING + "Set layer number without save parameters? [y/N]" + BColors.ENDC)
                if x == "y" or x == 'Y':
                    self.model = None
                    pass
                else:
                    return

        if param[start_with].isdigit():
            d = int(param[start_with])
            self.batch_number = d
            my_print("Batch number is set to %d" % self.batch_number)

    def save_parameter(self, id):
        f = file('parameter_W_%s.txt' % id, 'wb')
        cPickle.dump(self.model.W, f, protocol=cPickle.HIGHEST_PROTOCOL)
        f.close()
        f = file('parameter_B_%s.txt' % id, 'wb')
        cPickle.dump(self.model.B, f, protocol=cPickle.HIGHEST_PROTOCOL)
        f.close()

    def quit(self, param):
        if self.modified is True:
            if len(param) > 1 and param[1] == "force":
                exit()
            else:
                x = raw_input(BColors.WARNING + "Exit without save parameters? [y/N]" + BColors.ENDC)
                if x == "y" or x == 'Y':
                    exit()
                else:
                    return
        exit()

    def load_train_input_data(self):
        my_print("Load training input data from %s" % self.train_file)
        for line in open(self.train_file, 'r'):
            input_x = line.split()
            input_x = [TestBench.float_convert(i) for i in input_x]
            self.train_input_data.append(input_x)

        my_print("Load training answer data from %s" % self.answer_map_file)
        for line in open(self.answer_map_file, 'r'):
            ans_x = line.split(',')
            self.train_answer_data.append(ans_x)

    def load_test_data(self):
        for line in open(self.test_input_file, 'r'):
            test_x = line.split()
            test_x = [TestBench.float_convert(x) for x in test_x]
            self.test_data.append(test_x)

    def save_test_data(self):
        if len(self.test_input_file) == 0:
            self.load_test_data()
        my_print("Writing test answer data to %s", self.answer_map_file)
        test_stream = open(self.test_output_file, 'w')
        test_c = MAP()
        y = [0] * self.output_dimension
        test_stream.write('Id,Prediction\n')
        self.model.load_parm(self.W_parm, self.B_parm)

        for i in range(len(self.test_data)):
            ya = self.model.y_evaluated_function([self.test_data[i][1:self.input_dimension]], [y])
            value = str(
                (self.test_data[i][0], test_c.map(ya, ))
            )
            test_stream.write(value)
            test_stream.write('\n')
        # print test.W_array[0].get_value()
        return

    def save_training_progress(self):
        pass

    def _test(self, training_segment):
        c = MAP()
        err = 0
        y = [0] * self.output_dimension
        _1 = len(self.train_input_data) - training_segment
        for m in range(_1):
            # print self.train_input_data[training_segment + m][1:70]
            xa = self.get_one_data(m + training_segment)
            t = self.model.y_evaluated_function([xa], [self.get_one_answer(m + training_segment)])

            if c.map(t) != self.train_answer_data[m + training_segment][1].strip():
                err += 1
            else:
                print 1

                # print [c.map(Ya)]
                # print [str(ans[m][1].split('\n')[0])]
        return 1.0 - float(err / float(_1))

    # def __run(self, batch):
    #     training_segment = 1000000
    #
    #     batch_number = batch * 1000
    #     X = None
    #     Y = None
    #     i = 0
    #     acc = 0.0
    #     W_new = []
    #     B_new = []
    #     c = MAP()
    #     while True:
    #         X = []
    #         yy = []
    #         for k in range(batch_number):
    #             num = randrange(0, training_segment)
    #             if i >= 1000000:  # i >= 1124823:
    #                 i = 0
    #                 err = 0.0
    #                 for m in range(124823):
    #                     Ya = self.model.y_evaluated_function([self.train[1000000 + m][1:70]], Y)[0]
    #                     if [c.map(Ya)] != [str(self.ans[1000000 + m][1].split('\n')[0])]:
    #                         err += 1
    #                         # print [c.map(Ya)]
    #                         # print [str(ans[m][1].split('\n')[0])]
    #                 acc = 1.0 - err / 124823.0
    #                 # print err
    #                 print acc
    #             typeidx = self.anstype.index(str(self.ans[num][1].split('\n')[0]))
    #             y = [0] * 48
    #             y[typeidx] = 1
    #             yy.append(y)
    #             X.append(self.train[num][1:70])
    #             i += 1
    #         Y = yy
    #         if i % 10000 == 0:
    #             print i
    #             # print test.y_evaluated_function(X,Y)
    #             # print [test.W_array[0].get_value(),test.W_array[1].get_value()]
    #         self.model.train_one(X, Y)



    @staticmethod
    def get_correctness_ratio(correct, total):
        return correct / total

    @staticmethod
    def float_convert(num):
        try:
            return float(num)
        except ValueError:
            return num
Пример #13
0
def _run(args):
    options = RunOptions(args)
    model = ModelFactory.create(options)
    model.load_alignments()
    optimiser = OptimiserFactory.create(options)
    optimiser.maximise(model.likelihood)
Пример #14
0
def _init(args):
    options = InitOptions(args)
    model = ModelFactory.create(options)
    model.init_alignments()
Пример #15
0
    for i in range(len(coords)):
        normalized_y = 1.0 - coords[i][0] / float(max_y)
        normalized_x = coords[i][1] / float(max_x)
        ax.text(normalized_x, normalized_y, str(i))
        #if i >= len(coords) - 2:
        #    print '(' + str(coords[i][0]) + ', ' + str(coords[i][1]) + ')'
    #print '(' + str(min_y) + ', ' + str(min_x) + ')'
    #print '(' + str(max_y) + ', ' + str(max_x) + ')'
    plt.show()


if __name__ == '__main__':
    ibug_version = False #not train
    train_on_all = False
    notify_training_complete = True
    factory = ModelFactory.ModelFactory()
    
    samples = ['100466187_1', '13602254_1', '2908549_1', '100032540_1', '1691766_1', '11564757_2', '110886318_1']
    #samples = ['100466187_1', '11564757_2', '1240746154_1', '1165647416_1', '1691766_1']

    #visualizePointOrder(np.load('downloads/3214115970_1.npy'))
    #batch_generator = BatchGenerator.PointMaskBatchGenerator(samples, 'data/train_ibug', factory.mask_side_len)
    
    """
    for sample in samples:
        test_im = cv2.imread('downloads/helen_ibug/trainset/' + sample + '.jpg')
        hsv_im = cv2.cvtColor(test_im, cv2.COLOR_BGR2HSV)
        hsv_im = hsv_im.astype(np.float32)
        hsv_im[:,:,1] *= 0.2
        hsv_im = hsv_im.astype(np.uint8)
        test_im = cv2.cvtColor(hsv_im, cv2.COLOR_HSV2RGB)