Exemple #1
0
class MyNet():
    def __init__(self,
                 learning_rate=1e-3,
                 batch_size=64,
                 num_class=10,
                 epochs=5):
        self.learning_rate = learning_rate
        self.batch_size = batch_size
        self.num_class = num_class
        self.epochs = epochs
        self.build_model()

    def build_model(self):
        self.model = MyModel()
        self.model.compile(
            optimizer=tf.keras.optimizers.Adam(self.learning_rate),
            loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
            metrics=['accuracy'])

    def train(self, x_train, y_train, x_test, y_test):
        db_train = gen_batches(x_train, y_train, self.batch_size)
        db_test = gen_batches(x_test, y_test, self.batch_size)
        self.model.fit(db_train,
                       epochs=self.epochs,
                       validation_data=db_test,
                       validation_freq=2)
        self.model.summary()
        self.model.evaluate(db_test)
Exemple #2
0
 def __init__(self, camera):
     self.mainwin = QMainWindow()
     self.ui = Ui_MainWindow()
     self.ui.setupUi(self.mainwin)
     self.camera_scene = QGraphicsScene()
     self.ui.cameraView.setScene(self.camera_scene)
     self.camera = camera
     self.liveinput_timer = None
     self.counter = 0
     self.words = []
     self.lock = RLock()
     self.cam_image_fit_needed = True
     self.datamodel = MyModel()
     self.controller = MyController(self.datamodel)
     self.bindir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                "EMNIST", "bin")
     self.model = load_model(self.bindir)
     self.mapping = pickle.load(open('%s/mapping.p' % self.bindir, 'rb'))
Exemple #3
0
    def run_train(self):
        print "[Trainer-Train] feedback file: \"{}\"".format(self.listfile_)
        print "[Trainer-Train] input: \"{}\"".format(self.inputfile_)
        print "[Trainer-Train] setting: \"{}\"".format(self.py_settingfile_)
        print "[Trainer-Train] dim: {}".format(self.unit_)
        print "[Trainer-Train] epoch: {}".format(self.epoch_)
        print "[Trainer-Train] mini-batch size: {}".format(self.batch_size_)
        print "[Trainer-Train] GPU id: {}".format(self.gpu_id_)

        # Remove old files
        self.remove(os.path.join(home_dir, "result"))

        # Initialize model to train
        model = MyModel(self.unit_)
        if self.gpu_id_ >= 0:
            cuda.get_device_from_id(self.gpu_id_).use()
            model.to_gpu(self.gpu_id_)

        # Load datasets and set up iterator
        self.train_ = ImportDataset(self.listfile_, self.inputfile_)
        train_iter = chainer.iterators.SerialIterator(
            self.train_, batch_size=self.batch_size_, shuffle=False)

        # Set optimizer
        optimizer = chainer.optimizers.AdaDelta()
        optimizer.setup(model)

        # Set up updateer and trainer
        updater = training.StandardUpdater(train_iter,
                                           optimizer,
                                           device=self.gpu_id_)
        trainer = training.Trainer(updater, (self.epoch_, "epoch"),
                                   os.path.join(home_dir, "result"))

        # Run trainer
        print "[Trainer-Train] Start main training."
        trainer.run()
        self.model_ = model.copy()
        print "[Trainer-Train] --> Finished."
Exemple #4
0
def load_model(input_dir):
    """
   This hook can be implemented to adjust logic in the scoring mode.

   load_model hook provides a way to implement model loading your self.
   This function should return an object that represents your model. This object will
   be passed to the predict hook for performing predictions.
   This hook can be used to load supported models if your model has multiple artifacts, or
   for loading models that drum does not natively support

   :param input_dir: the directory to load serialized models from
   :returns: Object containing the model - the predict hook will get this object as a parameter
   """

    # Returning a string with value "dummy" as the model.
    return MyModel(input_dir)
Exemple #5
0
class MyCanvas(object):
    def __init__(self, camera):
        self.mainwin = QMainWindow()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self.mainwin)
        self.camera_scene = QGraphicsScene()
        self.ui.cameraView.setScene(self.camera_scene)
        self.camera = camera
        self.liveinput_timer = None
        self.counter = 0
        self.words = []
        self.lock = RLock()
        self.cam_image_fit_needed = True
        self.datamodel = MyModel()
        self.controller = MyController(self.datamodel)
        self.bindir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                   "EMNIST", "bin")
        self.model = load_model(self.bindir)
        self.mapping = pickle.load(open('%s/mapping.p' % self.bindir, 'rb'))

    def display_model(self):
        self.datamodel.set_camera_image(self.camera_scene, self.camera.image)
        self.controller.send_updates()

    def show(self):
        self.mainwin.showMaximized()
        self.mainwin.show()
        self.ui.cameraView.setSceneRect(0, 0, CAMWIDTH, CAMHEIGHT)
        self.ui.cameraView.setBackgroundBrush(QColor(Qt.black))
        self.update_liveinput()
        camBounds = self.camera_scene.itemsBoundingRect()
        camBounds.setWidth(camBounds.width() * 1.01)
        camBounds.setHeight(camBounds.height() * 1.01)
        if self.cam_image_fit_needed:
            self.ui.cameraView.fitInView(camBounds, Qt.KeepAspectRatio)
            self.cam_image_fit_needed = False

    def update(self):
        with self.lock:
            camBounds = self.camera_scene.itemsBoundingRect()
            camBounds.setWidth(camBounds.width() * 1.01)
            camBounds.setHeight(camBounds.height() * 1.01)

    def update_liveinput(self):
        with self.lock:
            ok, image = self.camera.take_input()
            if ok:
                self.datamodel.set_camera_image(self.camera_scene, image)
            self.counter += 1
            if ok and (self.counter % READ_INTERVAL == 0):
                unwarped_image = self.image_to_words(image)
                if unwarped_image is not None:
                    mapped_words = []
                    for i, w in enumerate(self.words):
                        imgw = unwarped_image.shape[1]
                        mapped_x = Mapping.linlin(w[1][0], 0, imgw, 0, 1023)
                        imgh = unwarped_image.shape[0]
                        mapped_y = Mapping.linlin(w[1][1], 0, imgh, 0, 599)
                        if w is not None:
                            mapped_words.append((w, mapped_x, mapped_y))
                    self.datamodel.words = mapped_words
                    self.display_model()

            camBounds = self.camera_scene.itemsBoundingRect()
            camBounds.setWidth(camBounds.width() * 1.01)
            camBounds.setHeight(camBounds.height() * 1.01)
            self.ui.cameraView.fitInView(camBounds, Qt.KeepAspectRatio)

    def image_to_words(self, image):
        self.counter = 0
        unwarped_image = unwarp(image)
        if unwarped_image is None:
            print("SKIP DETECTION")
        else:
            # cv2.imshow("original", image)
            image = unwarp(image)
            # cv2.imshow("unwarped", image)
            image = remove_shadow(image)
            # cv2.imshow("shadow removal", image);
            image = threshold_image(image)
            # cv2.imshow("threshold", image)
            # image = denoise_image(image)
            # cv2.imshow("denoise", image)
            # cv2.waitKey(0)
            all_letters = segment_letters(image)
            cut_letters = order_letters(all_letters)

            self.words = []
            current_word = ""
            avgX = 0
            avgY = 0
            wordLength = 0
            for l in cut_letters:
                if l is None:
                    avgX = avgX / wordLength if wordLength != 0 else 0
                    avgY = avgY / wordLength if wordLength != 0 else 0
                    self.words.append(
                        [cleanup_word(current_word), (avgX, avgY)])
                    current_word = ""
                    avgX = 0
                    avgY = 0
                    wordLength = 0
                else:
                    letter, pos = l
                    current_word += predict(self.model, self.mapping, letter)
                    avgX += pos[0]
                    avgY += pos[1]
                    wordLength += 1
            if current_word:
                avgX = avgX / wordLength if wordLength != 0 else 0
                avgY = avgY / wordLength if wordLength != 0 else 0
                self.words.append([cleanup_word(current_word), (avgX, avgY)])
        return unwarped_image

    def start_liveinput_timer(self):
        self.liveinput_timer = QTimer()
        self.liveinput_timer.timeout.connect(self.update_liveinput)
        self.liveinput_timer.start(INPUT_INTERVAL)
Exemple #6
0
def do_train():
    # Reads label_map.
    label_map_path = os.path.join(args.data_path, "predicate2id.json")
    if not (os.path.exists(label_map_path) and os.path.isfile(label_map_path)):
        sys.exit("{} dose not exists or is not a file.".format(label_map_path))
    with open(label_map_path, 'r', encoding='utf8') as fp:
        label_map = json.load(fp)
    num_classes = (len(label_map.keys()) - 2) * 2 + 2

    train_num = len(open(os.path.join(args.data_path, 'train_data.json')).readlines())

    # Loads pretrained model ERNIE
    model = MyModel(num_classes)
    tokenizer = BertTokenizer.from_pretrained('bert-base-chinese')
    criterion = BCELossForDuIE()

    # Loads dataset.
    train_generator = DuIEDataset.from_file(
        os.path.join(args.data_path, 'train_data.json'), tokenizer,
        args.max_seq_length, True)
    train_dataset = tf.data.Dataset.from_generator(train_generator,
                                                   (tf.int32, tf.int32, tf.int32, tf.int32, tf.int32),
                                                   (tf.TensorShape([None]), tf.TensorShape([]), tf.TensorShape([None]),
                                                    tf.TensorShape([None]), tf.TensorShape([None, 1]))).shuffle(
        10000).batch(args.batch_size)

    eval_file_path = os.path.join(args.data_path, 'dev_data.json')
    test_generator = DuIEDataset.from_file(eval_file_path, tokenizer,
                                           args.max_seq_length, True)
    test_dataset = tf.data.Dataset.from_generator(test_generator,
                                                  (tf.int32, tf.int32, tf.int32, tf.int32, tf.int32),
                                                  (tf.TensorShape([None]), tf.TensorShape([]), tf.TensorShape([None]),
                                                   tf.TensorShape([None]), tf.TensorShape([None, 1]))).batch(
        args.batch_size)

    # Defines learning rate strategy.
    steps_by_epoch = math.ceil(train_num / args.batch_size)
    num_training_steps = steps_by_epoch * args.num_train_epochs
    lr_scheduler = LinearDecayWithWarmup(args.learning_rate, num_training_steps,
                                         args.warmup_ratio)
    # Generate parameter names needed to perform weight decay.
    # All bias and LayerNorm parameters are excluded.
    decay_params = [
        tvar for tvar in tf.compat.v1.trainable_variables() if not any(nd in tvar.name for nd in ["bias", "Norm"])
    ]
    optimizer = tfa.optimizers.AdamW(weight_decay=args.weight_decay, learning_rate=lr_scheduler)

    train_loss = tf.keras.metrics.Mean(name='train_loss')
    test_loss = tf.keras.metrics.Mean(name='test_loss')

    @tf.function
    def train_step(batch):
        input_ids, seq_lens, tok_to_orig_start_index, tok_to_orig_end_index, labels = batch
        with tf.GradientTape() as tape:
            logits = model(input_ids=input_ids)
            mask = (input_ids != 0).logical_and((input_ids != 1)).logical_and((input_ids != 2))
            loss = criterion((logits, labels, mask))
        gradients = tape.gradient(loss, decay_params)
        optimizer.apply_gradients(zip(gradients, decay_params))

        train_loss(loss)

    # Starts training.
    global_step = 0
    logging_steps = 50
    save_steps = 10000
    tic_train = time.time()
    for epoch in range(args.num_train_epochs):
        print("\n=====start training of %d epochs=====" % epoch)
        tic_epoch = time.time()
        train_loss.reset_states()
        for step, batch in enumerate(train_dataset):
            train_step(batch)
            loss_item = train_loss.result()
            global_step += 1

            if global_step % logging_steps == 0:
                print("epoch: %d / %d, steps: %d / %d, loss: %f, speed: %.2f step/s"
                      % (epoch, args.num_train_epochs, step, steps_by_epoch, loss_item,
                         logging_steps / (time.time() - tic_train)))
                tic_train = time.time()

            if global_step % save_steps == 0:
                print("\n=====start evaluating ckpt of %d steps=====" % global_step)
                test_loss.reset_states()
                precision, recall, f1 = evaluate(model, criterion, test_dataset, test_loss, eval_file_path, "eval")
                print("precision: %.2f\t recall: %.2f\t f1: %.2f\t" % (100 * precision, 100 * recall, 100 * f1))
                print("saving checkpoing model_%d.pdparams to %s " % (global_step, args.output_dir))
                model.save(os.path.join(args.output_dir, "model_%d.pdparams.h5" % global_step))

        tic_epoch = time.time() - tic_epoch
        print("epoch time footprint: %d hour %d min %d sec" % (
            tic_epoch // 3600, (tic_epoch % 3600) // 60, tic_epoch % 60))

    # Does final evaluation.
    print("\n=====start evaluating last ckpt of %d steps=====" % global_step)
    test_loss.reset_states()
    precision, recall, f1 = evaluate(model, criterion, test_dataset, test_loss, eval_file_path, "eval")
    print("precision: %.2f\t recall: %.2f\t f1: %.2f\t" % (100 * precision, 100 * recall, 100 * f1))
    model.save(os.path.join(args.output_dir, "model_%d.pdparams.h5" % global_step))
    print("\n=====training complete=====")
Exemple #7
0
labels = data_array[:, 0]
features_tensor = Variable(torch.Tensor(features).float())
labels_tensor = Variable(torch.Tensor(labels).long())

# Hyper parameters
input_neurons = features.shape[1]
hidden_neurons = 13
output_neurons = np.unique(labels).size
learning_rate = 0.01
num_epoch = 1000000

assert input_neurons == ATTRIBUTE_NUMBER
assert output_neurons == CLASS_NUMBER

# NN
net = MyModel(input_neurons, hidden_neurons, output_neurons)
loss_func = torch.nn.CrossEntropyLoss()
optimiser = torch.optim.SGD(net.parameters(), lr=learning_rate)
# Cuda
if torch.cuda.is_available():
    features_tensor = features_tensor.cuda()
    labels_tensor = labels_tensor.cuda()
    net.cuda()

all_losses=[]

for epoch in range(num_epoch):
    # Perform forward pass: compute predicted y by passing x to the model.
    pred = net(features_tensor)

    # Compute loss
Exemple #8
0
def populate():
    session = DBSession()
    model = MyModel(name=u'root', value=55)
    session.add(model)
    session.flush()
    transaction.commit()
Exemple #9
0
                    type=str,
                    help='flower image filepath')
parser.add_argument('checkpoint',
                    action="store",
                    type=str,
                    help='model checkpoint file, eg: checkpoint.pth')
parser.add_argument('--top_k',
                    action="store",
                    type=int,
                    help='topk return',
                    default=5)
parser.add_argument('--category_names',
                    action="store",
                    type=str,
                    help='category name json file',
                    default="cat_to_name.json")
parser.add_argument('--gpu',
                    action='store_true',
                    help='use gpu, if not specified, will use cpu',
                    default=False)

args = parser.parse_args()

mm = MyModel(category_name_filepath=args.category_names, use_gpu=args.gpu)
model, optimizer = mm.load_checkpoint(args.checkpoint)

answer = mm.predict(args.image_path, model, topk=args.top_k)

print("Predictions: ")
print(answer)
from pypdevs.simulator import Simulator

from mymodel import MyModel

model = MyModel()
simulator = Simulator(model)

simulator.setVerbose()
simulator.setClassicDEVS()

simulator.simulate()
 def get_model(self):
     # return TransferLearnModel.get_model(verbose=1)
     return MyModel.get_model(verbose=1)
Exemple #12
0
from modeller.automodel import *
from modeller.parallel import *
from myloop import MyLoop
from mymodel import MyModel

log.verbose()
env = environ()

env.io.atom_files_directory = ['.', '../atom_files']

# Create a new class based on 'loopmodel' so that we can redefine
# select_loop_atoms

a = MyModel(env,
                alnfile  = 'alignment.ali',      # alignment filename
                knowns   = ('bare_protein'),               # codes of the templates
                sequence = 'R_extended',               # code of the target
                assess_methods=assess.DOPE) # assess each loop with DOPE

#a = MyLoop(env,
#                alnfile  = 'alignment.ali',      # alignment filename
#                knowns   = ('pdbfile'),               # codes of the templates
#                sequence = 'rdrp',               # code of the target
#                loop_assess_methods=assess.DOPE) # assess each loop with DOPE
a.starting_model= 1 # index of the first model
a.ending_model  = 500 # index of the last model

#a.loop.starting_model = 1           # First loop model
#a.loop.ending_model   = 10           # Last loop model
#a.loop.md_level = refine.very_slow # Loop model refinement level
Exemple #13
0
 def build_model(self):
     self.model = MyModel()
     self.model.compile(
         optimizer=tf.keras.optimizers.Adam(self.learning_rate),
         loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
         metrics=['accuracy'])
Exemple #14
0
parser.add_argument('--epochs',
                    action='store',
                    type=int,
                    help='how many epochs to train',
                    default=1)
parser.add_argument('--learning_rate',
                    action='store',
                    type=float,
                    help='learning rate',
                    default=0.001)
parser.add_argument('--hidden_units',
                    action='store',
                    type=int,
                    help='hidden unit',
                    default=4096)
parser.add_argument('--save_dir',
                    action='store',
                    type=str,
                    help='save model into file',
                    default='.')

args = parser.parse_args()

mm = MyModel(use_gpu=args.gpu, epochs=args.epochs)
model = mm.create_model(base_model=args.arch,
                        hidden_unit_num=args.hidden_units)
optimizer = mm.create_optimizer(model, learning_rate=args.learning_rate)
mm.train_model(model, optimizer)
mm.save_checkpoint(model, optimizer)
if args.save_dir != None:
    mm.save_checkpoint(model, optimizer, dir=args.save_dir)
Exemple #15
0
    def run_LOOCV(self):
        # Cross-validation testing.
        print "[Trainer-LOOCV] Start leave-one-out testing."

        loo = LeaveOneOut()
        split_samples = SplitImportDataset(self.train_.base_)
        true_labels = []
        predicted_labels = []

        for train_index, [test_index] in loo.split(split_samples.input_base_):
            print "[Trainer-LOOCV] Test: [{}], Train: {}.".format(
                test_index, train_index)

            # Initialize model to train
            model_loccv = MyModel(self.unit_)

            if self.gpu_id_ >= 0:
                cuda.get_device_from_id(self.gpu_id_).use()
                model_loccv.to_gpu(self.gpu_id_)

            split_samples.split_LOOCV(train_index)
            train_iter = chainer.iterators.SerialIterator(
                split_samples, batch_size=self.batch_size_, shuffle=False)

            # Set optimizer
            optimizer = chainer.optimizers.AdaDelta()
            optimizer.setup(model_loccv)

            # Set up updateer and trainer
            updater = training.StandardUpdater(train_iter,
                                               optimizer,
                                               device=self.gpu_id_)
            trainer = training.Trainer(updater, (self.epoch_, "epoch"),
                                       os.path.join(home_dir, "result"))

            # Run trainer
            trainer.run()

            # Testing
            clf = Chainer2Sklearn(model_loccv.to_cpu())
            X_test = [list(split_samples.input_base_[test_index][0])]

            true_label = int(split_samples.input_base_[test_index][1])
            predicted_label = int(clf.predict(X_test))

            true_labels.append(true_label)
            predicted_labels.append(predicted_label)

        # Calculate accuracy and valiance
        acc = accuracy_score(true_labels, predicted_labels)
        val = self.variance_score(acc, len(split_samples.input_base_))
        print "[Trainer-LOOCV] Accuracy (average): {}, Variance: {}.".format(
            acc, val)

        # Save as figure.
        if (self.train_.iter_num_ - 1) == 1:
            if os.path.exists(os.path.join(home_dir, "acc_val.npy")):
                os.remove(os.path.join(home_dir, "acc_val.npy"))
            acc_val = np.array([[0.0], [0.0]])
        else:
            acc_val = np.load(os.path.join(home_dir, "acc_val.npy"))

        self.acc_val_ = np.append(acc_val, [[acc], [val]], axis=1)
        np.save(os.path.join(home_dir, "acc_val.npy"), self.acc_val_)

        # Draw reriability graph.
        self.drawGraph()
Exemple #16
0
    raise BaseException('outdir already exsit\n{}'.format(zarr_fp))

outdir, outbase = os.path.split(os.path.abspath(args.outfile))
if not os.path.exists(outdir):
    os.makedirs(outdir)

# gpu
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu

# Run inference
import torch
torch.set_default_tensor_type('torch.cuda.FloatTensor')
from torch.autograd import Variable
from mymodel import MyModel

mymodel = MyModel()
preprocess = mymodel.preprocess()
labels = mymodel.labels()

store = zarr.DirectoryStore(zarr_fp)
root = zarr.group(store=store, overwrite=True)

for ibeg in np.arange(n_skip, n_image, args.nframe):
    iend = np.min((ibeg + args.nframe, n_image))
    _, f_beg = os.path.split(img_files[ibeg])
    _, f_end = os.path.split(img_files[iend-1])
    print("Images {}-{}: ".format(f_beg, f_end), end='')

    print("preproc...", end='')
    sys.stdout.flush()
    imgs = []