コード例 #1
0
    def next_generation_models(self):
        self.current_generation += 1
        logger.info('============================================\n' +
                    f'Generation {self.current_generation}' +
                    '============================================\n')
        # Elite Selection
        elite = self.elite_model(self.current_generation - 1)
        next_gen_models = [elite]

        # slowest Training Time changes
        n = self.number_of_models_tobe_changed_based_on_training_time
        slow_models = self.top_n_slowest_models(self.current_generation - 1, n)
        for _, slow_model in enumerate(slow_models):
            new_model = CNN.change_for_slow_training_time(slow_model)
            new_model = CNN.change_name_to(self.models[new_model], f'model_gen{self.current_generation}_{_}')
            next_gen_models.append(new_model)

        # Fix Under-fitting and over-fitting for the rest
        prev_gen_model_names = set([model['name'] for model in self.current_generation_models])
        elite_set = set(elite['name'])
        slow_models_names = set([model['name'] for model in slow_models])
        under_fitted_models = list(prev_gen_model_names - elite_set - slow_models_names)
        for _, prev_gen_model in enumerate(under_fitted_models):
            model_hp = self.models[prev_gen_model]
            if self.metrics.loc[prev_gen_model, 'over-fit'] ==1:
                new_model = CNN.change_for_over_fit(model_hp, self.input_shape)
            else:
                new_model = CNN.change_for_under_fitting(model_hp, self.input_shape, self.output_size)
            new_model = CNN.change_name_to(new_model, f'model_gen{self.current_generation}_{_}')
            next_gen_models.append(new_model)

        # Run the New Generation models
        self.current_generation_models = next_gen_models
        self.train_current_generation()
コード例 #2
0
    def train_current_generation(self):
        logger.info(f'Training generation {self.current_generation}')
        for model in self.current_generation_models:
            model_name = model['name']
            logger.info(f'Training model {model_name}.')
            try:
                model_runs = [CNN(model, verbose=1) for _ in range(self.model_reruns)]
            except Exception as error:
                logger.error(error)
                # revert Changes
                prev_model = model['prev_model']
                model = self.models[prev_model]
                model = CNN.add_change_log(model, f'Reverted to model {prev_model} due to an exception on training.')
                model_name = model['name']
                model_runs = [CNN(model, verbose=1) for _ in range(self.model_reruns)]

            logger.info(f'Training model {model_name} completed')
            self.metrics.loc[model_name, 'test_Accuracy'] = np.min([cnn.accuracy[0] for cnn in model_runs])
            self.metrics.loc[model_name, 'train_Accuracy'] = np.min([cnn.accuracy[1] for cnn in model_runs])
            self.metrics.loc[model_name, 'training_time'] = np.max([cnn.Training_time for cnn in model_runs])
            self.metrics.loc[model_name, 'over-fit'] = np.any([cnn.is_over_fitted for cnn in model_runs])
            self.metrics.loc[model_name, 'prev_model'] = model['prev_model']
            self.metrics.loc[model_name, 'generation'] = self.current_generation
            model['layers_input_output_shape'] = [ f'layer.name: {layer.input_shape} --- {layer.output_shape}'
                                                  for layer in model_runs[0].model.layers]
            self.save_model(model)
            logger.info(f'Performance results for {model_name}:-\n{self.metrics.loc[model_name, :]}')
        logger.info(f'Generation {self.current_generation} Training completed.\n------------------\n')
コード例 #3
0
ファイル: train.py プロジェクト: k0t7y0s7/TextClassification
    def build(self):
        # Dataloaderの定義
        train_helper = BertHelper(self.params['train_path'])
        self.train_loader = DataLoader(train_helper,
                                       batch_size=self.params['batch_size'],
                                       shuffle=True)
        val_helper = BertHelper(self.params['val_path'])
        self.val_loader = DataLoader(val_helper,
                                     batch_size=self.params['batch_size'],
                                     shuffle=True)
        test_helper = BertHelper(self.params['test_path'])
        self.test_loader = DataLoader(test_helper,
                                      batch_size=self.params['batch_size'],
                                      shuffle=True)

        print('train sample:' + str(len(train_helper)) + ', val sample:' +
              str(len(val_helper)) + ', test sample:' + str(len(test_helper)))
        print('finished data regularization!')
        print('create model...', end='')

        # CNNモデルの定義
        self.model = CNN(self.params)

        if self.params['cuda']:
            self.model.cuda()

        # 損失関数の定義
        self.criterion = nn.BCEWithLogitsLoss()

        # Optimizerの定義,及び重み減衰を適応させるパラメータの選択
        no_decay = ['bias', 'LayerNorm.weight']
        optimizer_parameters = [
            {
                'params': [
                    p for i, p in self.model.named_parameters()
                    if not any(j in i for j in no_decay)
                ],
                'weight_decay':
                self.params['weight_decay']
            },
            {
                'params': [
                    p for i, p in self.model.named_parameters()
                    if any(j in i for j in no_decay)
                ],
                'weight_decay':
                0.0
            },
        ]
        self.optimizer = AdamW(optimizer_parameters,
                               lr=self.params['learning_rate'])

        # Schedulerの定義
        num_training_steps = len(self.train_loader) * self.params['epoch']
        num_warmup_steps = len(self.train_loader) * self.params['warmup_steps']
        self.scheduler = get_linear_schedule_with_warmup(
            self.optimizer, num_warmup_steps, num_training_steps)

        print('done!')
        print('training start')
コード例 #4
0
def initialize_network(mode, load_weights, train_pre, fusion):
    torch.cuda.empty_cache()

    print("\nLoading Networks for", mode, "training: ")

    ##### IR Pre Fusion #####
    ir_net = Generic_CNN()
    ir_path = 'C:\\Users\\ancarey\\Documents\\FusionPaper\\models\\GrayandRGB\\Gray4.pth'
    if load_weights == True:
        ir_pre_net = CNN()
        ir_pre_net_dict = ir_pre_net.state_dict()
        ir_pre_net.load_state_dict(torch.load(ir_path))
        ir_dict = ir_net.state_dict()
        pretrained_dict = {
            k: v
            for k, v in ir_pre_net_dict.items() if k in ir_dict
        }
        ir_dict.update(pretrained_dict)
        ir_net.load_state_dict(pretrained_dict)
    #print(ir_dict)
    print("*** IR Loaded")

    ##### RGB Pre Fusion #####
    rgb_net = Generic_CNN()
    rgb_path = 'C:\\Users\\ancarey\\Documents\\FusionPaper\\models\\GrayandRGB\\RGB.pth'
    if load_weights == True:
        rgb_pre_net = CNN()
        rgb_pre_net_dict = rgb_pre_net.state_dict()
        rgb_pre_net.load_state_dict(torch.load(rgb_path))
        rgb_dict = rgb_net.state_dict()
        pretrained_dict_2 = {
            k: v
            for k, v in rgb_pre_net_dict.items() if k in rgb_dict
        }
        rgb_dict.update(pretrained_dict_2)
        rgb_net.load_state_dict(pretrained_dict_2)
    print("*** RGB Loaded")

    ##### Post Fusion #####
    post_fusion_net = EXP2_Post_Fusion_Layer()
    if load_weights == True:
        post_fusion_dict = post_fusion_net.state_dict()
        pretrained_dict_3 = {
            k: v
            for k, v in ir_pre_net_dict.items() if k in post_fusion_dict
        }
        post_fusion_dict.update(pretrained_dict_3)
        post_fusion_net.load_state_dict(pretrained_dict_3)
    print("*** Post Fusion Loaded")

    ##### Full Network #####
    full_net = EXP2_Full_Network(ir_net,
                                 rgb_net,
                                 post_fusion_net,
                                 fusion_type=fusion,
                                 train_pre_net=train_pre)
    full_net.to('cuda')
    print("*** Full Network Loaded")

    return full_net
コード例 #5
0
    def __init__(self, parent=None):
        super(Online_learning, self).__init__(parent)
        # QtGui.QMainWindow.__init__(self,parent)
        self.setupUi(self)

        self.connect(self.btn_view_imgs, SIGNAL("clicked()"), self.nextImage)
        self.connect(self.btn_prev_img, SIGNAL("clicked()"), self.PrevImage)
        self.connect(self.btn_predict, SIGNAL("clicked()"), self.prediction)
        self.connect(self.btn_class_1, SIGNAL("clicked()"), self.learn)
        self.connect(self.btn_clear_points, SIGNAL("clicked()"),
                     self.clear_points)

        self.file_path = "/hdd1T/frames_concert/Tomorrowland Belgium 2016 - Steve Aoki (10-19-2016 11-44-05 AM)"
        self.imgs_list = findImages(self.file_path)
        self.img_idx = 0

        self.current_img = []
        self.current_img_RGB = []
        self.head_locations = []

        self.showImage()

        self.cnn = CNN('weights/weights.pkl')

        self.create_lbl_layers(self.cnn.nkerns)
        self.fill_image_weights()
コード例 #6
0
ファイル: train_cnn.py プロジェクト: fsong666/nn
    def __init__(self,
                 training_data=None,
                 test_data=None,
                 validation_data=None,
                 learning_rate=1.0,
                 mini_batch_size=4,
                 epochs=1,
                 num_class=1):
        self.num_class = num_class
        self.training_data = training_data
        self.test_data = test_data
        self.validation_data = validation_data
        self.mini_batch_size = mini_batch_size
        self.learning_rate = learning_rate
        self.epochs = epochs

        self.model = CNN()
        self.loss_fn = nn.MSELoss()
        self.optimizer = torch.optim.SGD(self.model.parameters(),
                                         lr=self.learning_rate,
                                         momentum=0.1)
        self.optimizer2 = torch.optim.Adam(self.model.parameters(),
                                           lr=self.learning_rate)

        # early-stopping
        self.patience = 5000
        self.validation_frequency = 500  # [0, 499] = 500
コード例 #7
0
def run_all():
    trainloader, testloader, image_datasets = init_single_folder()

    print("Num of cows: ", len(image_datasets.classes))
    size = len(image_datasets.classes)

    for learning_rate_name, learning_rate in [("001", 0.01), ("005", 0.05)]:
        activation_functions = [('relu', F.relu), ('sigmoid', torch.sigmoid),
                                ('tanh', F.tanh)]
        for act_func_name, activation_func in activation_functions:
            optimizers = ['SGD', 'Adam', 'RMSprop']
            for optimizer_name in optimizers:
                # criterions = [ ('MSELoss', torch.nn.MSELoss()),
                # ('NLLLoss', torch.nn.NLLLoss()), ('MarginRankingLoss', torch.nn.MarginRankingLoss()), ,  ('HingeEmbeddingLoss', torch.nn.HingeEmbeddingLoss()
                criterions = [('cross entropy', nn.CrossEntropyLoss())]
                for criterion_name, criterion in criterions:
                    net = CNN(size, activation_func)
                    optimizer = optimizer_builder(optimizer_name,
                                                  net.parameters(),
                                                  learning_rate)

                    name = learning_rate_name + "_" + act_func_name + "_" + optimizer_name + "_" + criterion_name
                    print("-------------------------")
                    print(name)
                    print("-------------------------")

                    train_loss, test_results = train_net(
                        net, optimizer, 50, trainloader, testloader, criterion)
                    print("***************")
                    print(train_loss)
                    print("***************")
                    print(test_results)

                    torch.save(net.state_dict(), name + '.pt')
                    print_graph(name, train_loss, test_results)
コード例 #8
0
ファイル: CNN_trainer.py プロジェクト: zhangj5/enhancer_CNN
def main():
    t = time.time()
    project, num_runs, num_epochs = get_project_and_check_arguments(sys.argv, "CNN_trainer.py")
    number_of_species = len(project.species)
    if project.k:
        create_directories(project)
    elif project.project_name == "simulated_data":
        create_directories_simulated_data(project)

    for i in range(number_of_species):
        if trained_on_one_species_only:
            if train_on_human:
                if "Homo_sapiens" in project.species:
                    trained_species_index = project.species.index("Homo_sapiens")
                elif "Human" in project.species:
                    trained_species_index = project.species.index("Human")
            elif train_on_all_samples:
                trained_species_index = number_of_species - 2  # all species 238000
            elif train_on_dog:
                if "Canis_familiaris" in project.species:
                    trained_species_index = project.species.index("Canis_familiaris")
                elif "Dog" in project.species:
                    trained_species_index = project.species.index("Dog")
            if i != trained_species_index:
                continue
        if project.project_name == "simulated_data":
            species_to_train_on = None
            str_species_to_train_on = "Simulated"
        else:
            species_to_train_on = i
            str_species_to_train_on = project.species[species_to_train_on]
        # create and evaluate the model
        net = CNN(project, num_epochs, num_runs, species_to_train_on=species_to_train_on,
                  k=project.k, n=n, init_according_to_given_filters=init_according_to_given_filters,
                  init_model_ids=init_model_ids, start_time=t)
        # evaluate without test:
        max_validation_accuracy, best_model_validation_id, best_run_validation_index = \
            net.evaluate()
        output_dir = project.CNN_output_dir
        if not os.path.exists(output_dir) and not os.path.isdir(output_dir):
            print("make directory: ", output_dir)
            os.makedirs(output_dir)
        # output the accuracy and params
        if not os.path.exists(project.output_results_file):
            open(project.output_results_file, 'w')
        with open(project.output_results_file, 'a') as f:  # appends the line at the end of the file
            f.write('train: {0}\t'
                    'max_validation_accuracy: {1:.3f}\t'
                    'best_model_validation_id: {2}\t'
                    'best_run_validation_index: {3}\t'
                    'k: {4}\t'
                    'num_epochs: {5}\t'
                    'num_runs: {6}\t'.format(str_species_to_train_on, max_validation_accuracy,
                                             best_model_validation_id, str(best_run_validation_index),
                                             str(project.k), str(num_epochs), str(num_runs)))
            f.flush()
        project.print_project_details(project.output_results_file)
    elapsed = time.time() - t
    print('Total training time (all runs): {0:0.2f} seconds'.format(elapsed))
    print("end of script! :)")
コード例 #9
0
ファイル: DQN.py プロジェクト: reinforcementdriving/rlsimple
    def __init__(
        self, session, hasShadowNet
    ):  # create: G, sess, saver, savedDir. load: existing model if any
        CNN.__init__(self, session, hasShadowNet)
        self._screenInputs = self.buildInputLayer(
            "_screenInputs", shape=[None, ORIGIN_LENG, ORIGIN_WIDTH, 4])
        tmp0 = self.buildConvReluWire(
            self._screenInputs, [4, 4, 4, 32],
            [1, 4, 4, 1])  # stride=[1,4,4,1], where 4,4 are important
        out1 = self.buildMaxpoolWire(tmp0)
        tmp2 = self.buildConvReluWire(
            out1, [2, 2, 32, 64],
            [1, 2, 2, 1])  # stride=[1,4,4,1], where 4,4 are important
        out2 = self.buildMaxpoolWire(tmp2)
        linearOut = self.buildFlattenWire(out2, [-1, 1600])
        tmp = self.buildLinearReluWire(linearOut, [1600, 512])
        self._qValuesForActions = self.buildLinearWire(tmp, [512, ACTIONS])
        self.setOutLayer(self._qValuesForActions)

        # train:
        #self._actionChoiceInputs = tf.placeholder(tf.float32, [None, ACTIONS])
        self._actionChoiceInputs = self.buildInputLayer("_actionChoiceInputs",
                                                        shape=[None, ACTIONS])
        #self._targetValueInputs = tf.placeholder(tf.float32, [None])
        self._targetValueInputs = self.buildInputLayer("_targetValueInputs",
                                                       shape=[None])

        #tf.sub does not exist, simply use "-"
        vvv = tf.multiply(self._actionChoiceInputs, self._qValuesForActions)
        self._observedValues = tf.reduce_sum(vvv, reduction_indices=1)
        self._cost = tf.reduce_mean(
            tf.square(self._targetValueInputs - self._observedValues), 0)
        self._optimizer = tf.train.AdamOptimizer(learning_rate=1e-6).minimize(
            self._cost)
        self.addMinimizeOperation(self._optimizer)
コード例 #10
0
class Kernel:
    def __init__(self, log):
        log.info('loading methods ...')
        self.kernels = dict()
        self.naive_bayesian = NaiveBayes(log,
                                         pretrained=True,
                                         path='.\\Model\\naivebayes.pkl')
        self.vote_fisher = VoteFisher(log,
                                      pretrained=True,
                                      path='.\\Model\\ensemble_fisher.pkl')
        self.multi_fisher = MultiFisher(
            log, pretrained=True, path='.\\Model\\multi_class_fisher.pkl')
        sk_loader = torch.load('.\\Model\\sklearn_fisher.pkl')
        self.sklearn_fisher = sk_loader['fisher']
        self.skfisher_trans = sk_loader['transform']
        log.info('scikit-learn fisher model loaded')
        self.cnn = CNN()
        cnn_loader = torch.load('.\\Model\\cnn_best_.pkl')
        self.cnn.load_state_dict(cnn_loader['model_state'])
        self.cnn_trans = cnn_loader['transform']
        log.info('cnn model loaded')

    def set_kernel(self, im_path, method='NaiveBayesian'):
        image = Image.open(im_path)
        if method == 'NaiveBayesian':
            result = self.naive_bayesian.predict(image)
            feature_map = self.naive_bayesian.transform(image)
            feature_map = feature_map.reshape(
                (1, feature_map.shape[0], feature_map.shape[1]))
            feature_map = torch.cat((feature_map, feature_map, feature_map))
        elif method == 'VoteFisher':
            result = self.vote_fisher.predict(image)
            feature_map = self.vote_fisher.transform(image)
            feature_map = feature_map.reshape(
                (1, feature_map.shape[0], feature_map.shape[1]))
            feature_map = torch.cat((feature_map, feature_map, feature_map))
        elif method == 'MultiFisher':
            result = self.multi_fisher.predict(image)
            feature_map = self.multi_fisher.transform(image)
            feature_map = feature_map.reshape(
                (1, feature_map.shape[0], feature_map.shape[1]))
            feature_map = torch.cat((feature_map, feature_map, feature_map))
        elif method == 'SklearnFisher':
            result = self.sklearn_fisher.predict(
                self.skfisher_trans(image).flatten().reshape(1, 100))[0]
            feature_map = self.skfisher_trans(image)
            feature_map = feature_map.reshape(
                (1, feature_map.shape[0], feature_map.shape[1]))
            feature_map = torch.cat((feature_map, feature_map, feature_map))
        elif method == 'CNN':
            logits, _ = self.cnn(1 -
                                 self.cnn_trans(image)[0].reshape((1, 1, 28,
                                                                   28)))
            result = torch.argmax(logits)
            feature_map = 1 - self.cnn_trans(image)
        else:
            raise ValueError('Invalid method')
        Image.Image.save(T.ToPILImage()(feature_map), './feature_map.jpg')
        # plt.imsave(fname='./feature_map.jpg', arr=T.ToPILImage()(feature_map))
        return result
コード例 #11
0
def run(neural_arch, is_trained=False, model_path=None):
    """
    Responsible for starting the program.

    :param neural_arch: defines the neural architecture that we want to use, it's a NeuralArchitecture enumerator.
    :param is_trained: True if it's trained (mandatory to specify the model in 'model_path'), False otherwise (it starts the training). By default it is set to False.
    :param model_path: the path in which the model is located (to specify only if 'is_trained' is True).
    """

    if is_trained and not model_path:
        print("Please specify the model in function call 'run(...)'.")
        return

    elif not is_trained and model_path:
        print(
            "No need to specify the model in function call 'run(...)'. Model ignored."
        )

    # Start a CNN to train if asked,
    # in any other case handle the Client <> Server communication with the Unity 3D Wold
    if neural_arch == NeuralArchitecture.CNN and not is_trained:
        cnn = CNN()
        train_dataset, test_dataset = load_data(DATA_PATH)
        cnn.train_model(train_dataset, test_dataset)
    else:
        Server(com_type=neural_arch,
               is_trained_com=is_trained,
               model_path=model_path).run()
コード例 #12
0
def createLayer(index, numOfOutput):

    training_data = np.array(Datasplit[index], dtype=np.float32)
    training_label = np.array(DatasplitLabel[index], dtype=np.float32)
    test_data = np.array(Datatest[index], dtype=np.float32)
    test_label = np.array(DatatestLabel[index], dtype=np.float32)

    cnn = CNN(numOfOutput=numOfOutput,
              learning_rate=0.01,
              epochs=40,
              batch_size=500)
    # output layer
    y_ = cnn.initial_mlp_network()
    with tf.device('/cpu:0'):
        training_label_onehot = sess.run(
            tf.one_hot(indices=training_label,
                       depth=numOfOutput,
                       dtype=np.float64))
        test_label_onehot = sess.run(
            tf.one_hot(indices=test_label, depth=numOfOutput,
                       dtype=np.float64))

    cnn_accuricy, cnn_predict_label = cnn.calculate_session(
        y_, training_data, training_label_onehot, test_data, test_label_onehot)
    print(cnn_accuricy)
    return cnn_accuricy
コード例 #13
0
def train_model():
    train_files = find_files(train_data_path)
    test_files = find_files(test_data_path)
    train_samples = []
    test_samples = []
    for f in train_files:
        train_samples.extend(random.sample(list(read_file(f)), 93000))
    for f in test_files:
        test_samples.extend(list(read_file(f)))
    print("Total number of train samples: %d" % len(train_samples))
    print("Total number of test samples: %d" % len(test_samples))
    train_loader = DataLoader(dataset=TrainDataset(train_samples),
                              batch_size=8,
                              shuffle=True)
    test_loader = DataLoader(dataset=TestDataset(test_samples),
                             batch_size=1,
                             shuffle=True)
    model = CNN()
    model.load_state_dict(torch.load(os.path.join(save_path,
                                                  "model10-21.pkl")))
    trainer = Trainer(model=model,
                      iter_num=iter_num,
                      save_dir=save_path,
                      save_log=False)
    trainer.push_data(train_loader)
    trainer.push_data(test_loader, is_train=False)
    trainer.train()
コード例 #14
0
    def __init__(self,
                 env,
                 use_conv=True,
                 learning_rate=3e-4,
                 gamma=0.99,
                 buffer_size=10000,
                 resume=False):
        self.env = env
        self.learning_rate = learning_rate
        self.gamma = gamma
        self.replay_buffer = BasicBuffer(max_size=buffer_size)
        self.epsilon = 1
        self.epsilon_min = 0.001
        self.epsilon_decay = 0.0005
        self.losses = []

        self.device = torch.device(
            "cuda" if torch.cuda.is_available() else "cpu")

        self.path = 'saved-models/qwop_cnn.game.model'
        self.model = CNN()
        self.date = datetime.now().strftime("%b-%d-%Y-%H-%M-%S")
        self.save_path = 'saved-models/' + self.date + '-qwop_cnn.game' + '.model'

        if resume:
            self.model.load_state_dict(torch.load(self.path))
            self.model.eval()
            f = open('states/epsilon_decay.txt')
            self.epsilon = float(f.readline())
            f.close()

        self.optimizer = torch.optim.Adam(self.model.parameters())
        self.MSE_loss = nn.MSELoss()
コード例 #15
0
def train(tokenizer, model_dir):

    word_index, embeddings_matrix = generate_embeddings(tokenizer)

    x_train, x_validate, y_train, y_validate = train_test_split(
        data['content'], data['label'], test_size=0.1)

    list_tokenized_train = tokenizer.texts_to_sequences(x_train)
    input_train = sequence.pad_sequences(list_tokenized_train, maxlen=maxlen)

    list_tokenized_validation = tokenizer.texts_to_sequences(x_validate)
    input_validation = sequence.pad_sequences(list_tokenized_validation,
                                              maxlen=maxlen)

    y_train = keras.utils.to_categorical(y_train, num_classes=3)
    y_validate = keras.utils.to_categorical(y_validate, num_classes=3)

    model1 = CNN().model(embeddings_matrix, maxlen, word_index)
    file_path = model_dir + model_name % "{epoch:02d}"
    checkpoint = ModelCheckpoint(file_path, verbose=2, save_weights_only=True)
    metrics = Metrics()
    callbacks_list = [checkpoint, metrics]
    model1.fit(input_train,
               y_train,
               batch_size=batch_size,
               epochs=epochs,
               validation_data=(input_validation, y_validate),
               callbacks=callbacks_list,
               verbose=2)
    del model1
コード例 #16
0
class Predict():
    def __init__(self, path_to_predict_folder):
        self.path = path_to_predict_folder
        self.cnn = CNN()
        self.cnn.load_state_dict(T.load(r'./models/conv_epochs_100'))
        self.cnn.eval()
        self.transforms = transforms.Compose([
            transforms.Resize((64, 64)),
            transforms.ToTensor(),
            transforms.Normalize([.5, .5, .5], [.2, .2, .2])
        ])

    def load_image(self, image_name):
        full_path = os.path.join(self.path, image_name)
        image = Image.open(full_path)
        tensor = self.transforms(image).float().to(self.cnn.device)
        tensor = tensor.view(1, *tensor.shape)
        return tensor, image

    def predict(self, image_name):
        tensor, img = self.load_image(image_name)
        with T.no_grad():
            prediction = self.cnn(tensor)
            print(prediction)
        _ = plt.figure()
        _ = plt.imshow(img)
        title = 'Dog' if T.round(prediction).item() == 1 else 'Cat'
        plt.title(title)
        #prediction_percent = round(abs(.5-prediction.item())*100/.5)
        #plt.xlabel(f'{prediction_percent}% probability of {title}')
        plt.show()
コード例 #17
0
ファイル: HEC.py プロジェクト: vitrioil/TF_Tut
 def __init__(self,Xs,Ys,batch_size,epoch,lr,
              folder_name = "",
              search_timestamp = "1530634669",
              model_path = "C:/Users/HiteshOza/Documents/TF_Tut/CNN/MNIST/1530634669/model.ckpt-2750000"
              ,training_session = True):
              CNN.__init__(self,Xs,Ys,batch_size,epoch,lr,folder_name, search_timestamp,model_path ,training_session)
              
              self.assign_model()
コード例 #18
0
ファイル: cifar_cnn.py プロジェクト: dxmtb/nn
def main(argv):
    argv = FLAGS(argv)
    inputs, outputs = load_CIFAR_train(FLAGS.datapath)
    X_test, y_test = load_CIFAR_test(FLAGS.datapath)
    nn = CNN(10, FLAGS.activation, FLAGS.loss_type, FLAGS.batch)
    nn.fit(inputs, outputs, FLAGS.epoch, FLAGS.batch, [0.00001, 0.00002],
            X_test, y_test)
    print nn.test(X_test, y_test)
コード例 #19
0
def main():
    n_epochs = 1
    cnn = CNN()
    if torch.cuda.is_available():
        cnn = cnn.cuda()

    #cnn.load("models/crossvalidation_with_transformations/_40.pth")
    #cnn.eval()
    training_loss_per_epoch, validation_loss_per_epoch, training_error_per_epoch, valid_error_per_epoch, validation_accuracy = cnn.train_model(
        train_dataloaders, validation_dataloaders, num_epochs=n_epochs)
    print("Final validation accuracy: ", validation_accuracy)

    epoch_range = list(range(n_epochs))
    plt.figure(1)
    plt.plot(epoch_range, training_loss_per_epoch, '-', color='b')
    plt.plot(epoch_range, validation_loss_per_epoch, '-', color='g')
    plt.ylabel("loss")
    plt.xlabel("epoch iteration")
    plt.show()

    plt.figure(2)
    plt.plot(epoch_range, training_error_per_epoch, '-', color='b')
    plt.plot(epoch_range, valid_error_per_epoch, '-', color='g')
    plt.ylabel("error")
    plt.xlabel("epoch iteration")
    plt.show()

    final_predictions = np.empty([0, 0])
    final_predictions2 = np.empty([0, 0])
    for i, data in enumerate(test_dataloaders):
        inputs, target = data
        inputs = inputs.cuda()
        target = target.cuda()
        input_var = torch.autograd.Variable(inputs)
        target_var = torch.autograd.Variable(target)
        output = cnn(input_var)
        _, predicted = torch.max(output.data, 1)
        final_predictions2 = np.append(final_predictions2,
                                       predicted.cpu().numpy())

    dictionary = {}
    i = 0
    for f in os.listdir('../data/testset/test/'):
        id = int(os.path.basename(os.path.splitext(f)[0]))
        dictionary[id] = final_predictions2[i]
        i += 1
    for key in sorted(dictionary.keys()):
        final_predictions = np.append(final_predictions, dictionary[key])

    #for key in sorted(mydict.iterkeys()):
    for p in final_predictions:
        print(p)

    df = pd.read_csv('../sample_submission.csv', delimiter=',')
    map_classes = np.vectorize(lambda t: class_names[t])
    df['label'] = map_classes(final_predictions.astype(int).flatten())

    df.to_csv('./prediction.csv', index=False, sep=',')
コード例 #20
0
ファイル: LeNet.py プロジェクト: andrewv587/python
 def __init__(self, x):
     self.cnn1 = CNN(x, x.shape, (20, 1, 5, 5))
     self.cnn2 = CNN(self.cnn1.output, self.cnn1.output.shape,
                     (50, 20, 5, 5))
     middle_input = self.cnn2.output.flatten(2)
     self.mdl = MiddleLayer(middle_input, 50 * 4 * 4, 500)
     self.mlg = MultiLogisticRegression(self.mdl.y, 500, 10)
     self.pred = self.mlg.pred
     self.params = self.cnn1.params + self.cnn2.params + self.mdl.params + self.mlg.params
コード例 #21
0
ファイル: train_cnn.py プロジェクト: undertherain/vsmlib
def main(options):
    
    #load the config params
    gpu = options['gpu']
    data_path = options['path_dataset']
    embeddings_path = options['path_vectors']
    n_epoch = options['epochs']
    batchsize = options['batchsize']
    test = options['test']
    embed_dim = options['embed_dim']
    freeze = options['freeze_embeddings']

    #load the data
    data_processor = DataProcessor(data_path, test)
    data_processor.prepare_dataset()
    train_data = data_processor.train_data
    dev_data = data_processor.dev_data
    test_data = data_processor.test_data

    vocab = data_processor.vocab
    cnn = CNN(n_vocab=len(vocab), input_channel=1,
                  output_channel=10, n_label=2, embed_dim=embed_dim, freeze=freeze)
    cnn.load_embeddings(embeddings_path, data_processor.vocab)
    model = L.Classifier(cnn)
    if gpu >= 0:
        model.to_gpu()
    
    #setup the optimizer
    optimizer = O.Adam()
    optimizer.setup(model)


    train_iter = chainer.iterators.SerialIterator(train_data, batchsize)
    dev_iter = chainer.iterators.SerialIterator(dev_data, batchsize,repeat=False, shuffle=False)
    test_iter = chainer.iterators.SerialIterator(test_data, batchsize,repeat=False, shuffle=False) 
    batch1 = train_iter.next()
    batch2 = dev_iter.next()
    updater = training.StandardUpdater(train_iter, optimizer, converter=util.concat_examples, device=gpu)
    trainer = training.Trainer(updater, (n_epoch, 'epoch'))

    # Evaluation
    eval_model = model.copy()
    eval_model.predictor.train = False
    trainer.extend(extensions.Evaluator(dev_iter, eval_model, device=gpu, converter=util.concat_examples))

    test_model = model.copy()
    test_model.predictor.train = False

    trainer.extend(extensions.LogReport())
    trainer.extend(extensions.PrintReport(
        ['epoch', 'main/loss', 'validation/main/loss',
         'main/accuracy', 'validation/main/accuracy']))
    trainer.extend(extensions.ProgressBar(update_interval=10))
    
    
    trainer.run()
コード例 #22
0
 def __init__(self, path_to_predict_folder):
     self.path = path_to_predict_folder
     self.cnn = CNN()
     self.cnn.load_state_dict(T.load(r'./models/conv_epochs_100'))
     self.cnn.eval()
     self.transforms = transforms.Compose([
         transforms.Resize((64, 64)),
         transforms.ToTensor(),
         transforms.Normalize([.5, .5, .5], [.2, .2, .2])
     ])
コード例 #23
0
def main():
    #(2560, 256, 123)
    X, Y = get_data()

    # model = DNN(X, Y)
    # model = DCNN(X, Y)
    # model = ANN(X, Y)
    model = CNN(X, Y)
    model.cnn()
    print('Done')
コード例 #24
0
 def fitness(self):
     uow=UnitOfWork()
     genoWithSegSiz=[geno for geno in self._genomes if geno._genName== 'segment_size']
     if genoWithSegSiz==[]:
         self._shelveDataFile=uow._dataSet().PreparingData()
     else:
         segment_size=genoWithSegSiz[0]._value
         self._shelveDataFile=uow._dataSet.PreparingData(segment_size)
     cnn=CNN(self._shelveDataFile,self._genomes)
     self._accuracy=cnn.RunAndAccuracy()
     return self._accuracy
コード例 #25
0
def run_cnn(x_train,
            y_train,
            x_valid,
            y_valid,
            save_path='../models/vi-5sentiment-analysis_cnn.models'):
    # Create model
    cnn = CNN(sequence_length=max_seq)
    # print(len(x_train), len(y_train))
    cnn.train(x_train, y_train, x_valid, y_valid, save_path)
    cm = Confusion(x_valid, y_valid)
    cm.confusion_matrix()
コード例 #26
0
    def __init__(self, model_path, messages_size):
        """
        Represents the communication between the trained CNN and the Unity 3D World, for next action prediction.

        :param model_path: the path of the trained model
        :param messages_size: the size in bytes of the message to read
        """

        # load the model and assign it to the CNN
        model = load_model(model_path)
        self.messages_size = messages_size
        self.cnn = CNN(model=model)
コード例 #27
0
 def model(self, model_params, return_dict, train_data, validation_data):
     """
     This functions builds the CNN based on the provided parameters
     :param model_params: The parameters that have to be used for the model
     :param return_dict: A dictionary in which the results should be saved
     :return: results in a dictionary
     """
     model = CNN(**model_params)
     output = model.train_neural_network(train_data,
                                         validation_data,
                                         output_folder='data/temp/',
                                         return_output=True)
     return_dict['data'] = output
コード例 #28
0
def run_cnn(learning_rate, batch_size):
    """
        Args:
        - learning_rate: learning rate used to train the network.
        - batch_size: batch_size to train the network.
        """
    X_train, y_train, X_test, y_test = get_data()
    model = CNN(n_classes=10,
                learning_rate=learning_rate,
                batch_size=batch_size)
    print("Epoch 1...")
    model.train(X_train[:50], y_train[:50])
    test_network(model, X_test[:5], y_test[:5])
コード例 #29
0
ファイル: Agent.py プロジェクト: lolPirate/Deep-Learning-A-Z
class Agent():
    def __init__(self, train_data, test_data, lr=1e-3, epochs=1):
        self.train_data = train_data
        self.test_data = test_data
        self.lr = lr
        self.epochs = epochs
        self.cnn = CNN(lr)

    def learn(self):
        all_loss = []
        all_accuracy = []
        for ep in range(1, self.epochs + 1):
            print(f'Epoch {ep}')
            epoch_loss = 0
            epoch_accuracy = 0
            for x_batch, y_batch in self.train_data:
                x_batch, y_batch = x_batch.to(self.cnn.device), y_batch.to(
                    self.cnn.device)
                self.cnn.optimizer.zero_grad()
                y_pred = self.cnn.forward(x_batch.type(T.float))
                loss = self.cnn.loss(y_pred,
                                     y_batch.unsqueeze(1).type(T.float))
                accuracy = self.accuracy(y_pred,
                                         y_batch.unsqueeze(1).type(T.float))
                loss.backward()
                self.cnn.optimizer.step()
                epoch_loss += loss.item()
                epoch_accuracy += accuracy.item()
            all_loss.append(epoch_loss / len(self.train_data))
            all_accuracy.append(epoch_accuracy / len(self.train_data))
            test_accuracy, test_loss = self.evaluate()
            print(
                f'Train Loss:{epoch_loss/len(self.train_data)} | Train Accuracy:{epoch_accuracy/len(self.train_data)} | Test Loss:{test_loss} | Test Accuracy:{test_accuracy}'
            )
        return all_loss, all_accuracy

    def accuracy(self, y_pred, y_correct):
        y_pred = T.round(y_pred)
        correct_sum = (y_pred == y_correct).sum().float()
        return correct_sum / y_correct.shape[0]

    def evaluate(self):
        tot_accuracy = 0
        tot_loss = 0
        for x_test, y_test in self.test_data:
            x, y = x_test.to(self.cnn.device), y_test.to(self.cnn.device)
            y_pred = self.cnn.forward(x)
            tot_accuracy += self.accuracy(y_pred, y.unsqueeze(1))
            tot_loss += self.cnn.loss(y_pred, y.unsqueeze(1).type(T.float))
        return tot_accuracy / len(self.test_data), tot_loss / len(
            self.test_data)
コード例 #30
0
    def __init__(self,
                 input_shape,
                 actions,
                 replay_buffer,
                 minibatch_size,
                 logger,
                 name="cnn"):
        self.input_shape = input_shape
        self.action_space = actions
        self.minibatch_size = minibatch_size

        self.network = CNN(self.input_shape, self.action_space,
                           self.minibatch_size)

        super(CNNAgent, self).__init__(logger, replay_buffer, name=name)
コード例 #31
0
 def __init__(self,
              index2word,
              emb_size,
              class_inv_freq,
              num_kernels,
              kernel_sizes,
              learning_rate,
              model_save_path,
              pretrained_path=""):
     self.softmax = nn.Softmax()
     self.model = CNN(index2word, emb_size, len(class_inv_freq),
                      num_kernels, kernel_sizes, pretrained_path)
     self.optimizer = optim.Adam(self.model.parameters(), lr=learning_rate)
     #self.criterion = nn.CrossEntropyLoss(weight=torch.FloatTensor(class_inv_freq))
     self.criterion = nn.CrossEntropyLoss()
     self.model_save_path = model_save_path
コード例 #32
0
def train_intra(window_size, learning_rate=1e-03):
    dataset_type = "Intra"
    x_train, y_train = get_all_dataset(dataset_type, "train", window_size)
    x_test, y_test = get_all_dataset(dataset_type, "test", window_size)

    model = CNN(type=dataset_type,
                epochs=args.epochs,
                window_size=window_size,
                input_shape=(x_train.shape[1], x_train.shape[2]),
                learning_rate=learning_rate)
    model.fit(x=x_train, y=y_train, callbacks=[tensorboard_callback])

    print("Evaluation:")
    return model.evaluate(x_test,
                          y_test,
                          callbacks=[tensorboard_callback_eval])
コード例 #33
0
#! -*- coding: utf-8 -*-

from CNN import CNN
from animeface import AnimeFaceDataset
from chainer import cuda

#GPUつかうよ
cuda.init(0)

print 'load AnimeFace dataset'
dataset = AnimeFaceDataset()
dataset.load_data_target()
data = dataset.data
target = dataset.target
n_outputs = dataset.get_n_types_target()

cnn = CNN(data = data,
          target = target,
          gpu = 0,
          n_outputs = n_outputs)

cnn.train_and_test(n_epoch = 100)