예제 #1
0
def main():

    ###build dataset
    train_ds = DataIter(cfg.DATA.root_path, cfg.DATA.train_txt_path, True)
    test_ds = DataIter(cfg.DATA.root_path, cfg.DATA.val_txt_path, False)

    ###build trainer
    trainer = Train(train_ds=train_ds, val_ds=test_ds)

    trainer.load_weight()
    if cfg.TRAIN.vis:
        for step in range(train_ds.size):

            images, labels = train_ds()

            for i in range(images.shape[0]):
                example_image = np.array(images[i], dtype=np.uint8)
                example_image = np.transpose(example_image, [1, 2, 0])
                example_label = np.array(labels[i])

                _h, _w, _ = example_image.shape

                print(example_label.shape)

                cv2.imshow('example', example_image)
                cv2.waitKey(0)

    ### train
    trainer.custom_loop()
예제 #2
0
    def __init__(self):
        self.train_ds = DataIter(cfg.DATA.root_path, cfg.DATA.train_txt_path,
                                 True)
        self.val_ds = DataIter(cfg.DATA.root_path, cfg.DATA.val_txt_path,
                               False)

        self.inputs = []
        self.outputs = []
        self.val_outputs = []
        self.ite_num = 1

        self._graph = tf.Graph()

        self.summaries = []

        self.ema_weights = False
예제 #3
0
def main():

    data = pd.read_json('folds.json', lines=True)

    losscolector = []
    folds = [0, 1, 2, 3, 4]

    for fold in folds:

        ###build dataset
        train_data = data[data['fold'] != fold]
        val_data = data[data['fold'] == fold]

        train_ds = DataIter(train_data, shuffle=True, training_flag=True)
        val_ds = DataIter(val_data, shuffle=False, training_flag=False)
        ###build trainer
        trainer = Train(train_ds=train_ds, val_ds=val_ds, fold=fold)

        print('it is here')
        if cfg.TRAIN.vis:
            print('show it, here')
            for step in range(train_ds.size):

                images, data, labels = train_ds()
                # images, mask, labels = cutmix_numpy(images, mask, labels, 0.5)

                print(images.shape)

                for i in range(images.shape[0]):
                    example_image = np.array(images[i, 0])

                    example_label = np.array(labels[i])

                    cv2.imshow('ss', example_image)
                    cv2.waitKey(0)

        ### train
        loss, model = trainer.custom_loop()
        losscolector.append([loss, model])

    avg_loss = 0
    for k, loss_and_model in enumerate(losscolector):
        print('fold %d : loss %.5f modelname: %s' %
              (k, loss_and_model[0], loss_and_model[1]))
        avg_loss += loss_and_model[0]
    print('average loss is ', avg_loss / 5.)
예제 #4
0
def main():
    ###build dataset
    train_ds = DataIter(cfg.DATA.root_path, cfg.DATA.train_txt_path, True)
    test_ds = DataIter(cfg.DATA.root_path, cfg.DATA.val_txt_path, False)

    ##build model
    model = ShuffleNetV2(model_size='1.0x')

    ###build trainer
    trainer = Train(model, train_ds=train_ds, val_ds=test_ds)

    if cfg.TRAIN.vis:
        for step in range(train_ds.size):

            images, labels = train_ds()

            for i in range(images.shape[0]):
                example_image = np.array(images[i], dtype=np.uint8)
                example_label = np.array(labels[i])

                landmark = example_label[0:136].reshape([-1, 2])
                _h, _w, _ = example_image.shape

                print(landmark.shape)
                for _index in range(landmark.shape[0]):
                    x_y = landmark[_index]
                    cv2.circle(example_image,
                               center=(int(x_y[0] * _w), int(x_y[1] * _w)),
                               color=(255, 255, 255),
                               radius=1,
                               thickness=-1)

                cv2.imshow('example', example_image)
                cv2.waitKey(0)

    ### train
    trainer.custom_loop()
예제 #5
0
    def __init__(self):
        self.train_ds = DataIter(cfg.DATA.root_path,
                                 cfg.DATA.train_txt_path,
                                 training_flag=True)
        self.val_ds = DataIter(cfg.DATA.root_path,
                               cfg.DATA.val_txt_path,
                               training_flag=False)

        self.inputs = []
        self.outputs = []
        self.val_outputs = []
        self.ite_num = 1

        self._graph = tf.Graph()

        with self._graph.as_default():
            tf_config = tf.ConfigProto(allow_soft_placement=True,
                                       log_device_placement=False)
            tf_config.gpu_options.allow_growth = True
            self.sess = tf.Session(config=tf_config)

        self.summaries = []

        self.ema_weights = False
예제 #6
0
from lib.core.model.ShuffleNet_Series.ShuffleNetV2.network import ShuffleNetV2
from lib.core.model.semodel.SeResnet import se_resnet50
import torch
import time
import argparse
import sklearn.metrics
from tqdm import tqdm
import numpy as np
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

import cv2
from train_config import config as cfg
cfg.TRAIN.batch_size = 1

ds = DataIter(cfg.DATA.root_path, cfg.DATA.val_txt_path, False)


def vis(model_path):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    ###build model

    model = se_resnet50()

    model.load_state_dict(torch.load(model_path, map_location=device),
                          strict=False)
    model.to(device)
    model.eval()

    cls1_pre_list = []
예제 #7
0
def main():


    train_feature_file = '../lish-moa/train_features.csv'
    target_file = '../lish-moa/train_targets_scored.csv'
    noscore_target = '../lish-moa/train_targets_nonscored.csv'

    train_features = pd.read_csv(train_feature_file)
    labels = pd.read_csv(target_file)
    extra_labels = pd.read_csv(noscore_target)

    test_features = pd.read_csv('../lish-moa/test_features.csv')

    pub_test_features=test_features.copy()

    # ####
    # ####
    # GENES = [col for col in train_features.columns if col.startswith('g-')]
    # CELLS = [col for col in train_features.columns if col.startswith('c-')]
    # ####
    # # RankGauss - transform to Gauss
    # for col in (GENES + CELLS):
    #     transformer = QuantileTransformer(n_quantiles=100, random_state=0, output_distribution="normal")
    #     vec_len = len(train_features[col].values)
    #     vec_len_pub_test = len(pub_test_features[col].values)
    #     vec_len_test=len(test_features[col].values)
    #
    #     data = np.concatenate([train_features[col].values.reshape(vec_len, 1),
    #                            pub_test_features[col].values.reshape(vec_len_pub_test, 1)], axis=0)
    #
    #     transformer.fit(data)
    #
    #     train_features[col] = \
    #         transformer.transform(train_features[col].values.reshape(vec_len, 1)).reshape(1, vec_len)[0]


    print(train_features.shape)
    losscolector=[]
    folds=[0,1,2,3,4,5,6]
    seeds=[40,41,42,43,44]

    n_fold=len(folds)

    model_dicts=[{'name':'resnetlike','func':Complexer},
                 {'name':'densenetlike','func':Denseplexer},
                 {'name':'tablenet','func':Tablenet},
                 ]




    for model_dict in model_dicts:
        # for cur_seed in seeds:

        for cur_seed in seeds:
            seed_everything(cur_seed)

            #### 5 fols split
            features = train_features.copy()
            target_cols = [c for c in labels.columns if c not in ['sig_id']]
            features['fold'] = -1
            Fold = MultilabelStratifiedKFold(n_splits=n_fold, shuffle=True, random_state=cur_seed)
            for fold, (train_index, test_index) in enumerate(Fold.split(features, labels[target_cols])):
                features['fold'][test_index] = fold

            for fold in folds:


                logger.info('train with seed %d' % (cur_seed))

                ###build dataset
                train_ind = features[features['fold'] != fold].index.to_list()
                train_features_=features.iloc[train_ind].copy()
                train_target_ = labels.iloc[train_ind].copy()
                train_extra_Target_ = extra_labels.iloc[train_ind].copy()

                val_ind=features.loc[features['fold'] == fold].index.to_list()
                val_features_ = features.iloc[val_ind].copy()
                val_target_ = labels.iloc[val_ind].copy()
                val_extra_Target_ = extra_labels.iloc[val_ind].copy()


                train_ds=DataIter(train_features_,train_target_,train_extra_Target_,shuffle=True,training_flag=True)
                val_ds=DataIter(val_features_,val_target_,val_extra_Target_,shuffle=False,training_flag=False)

                ### build model

                model=model_dict['func']()

                model_name=str(model_dict['name']+str(cur_seed))



                if cfg.TRAIN.pretrain_on_no_score:
                    ###build trainer
                    trainer = Train(model_name='PRETRAIN_'+model_name, model=model, train_ds=train_ds, val_ds=val_ds, fold=fold)

                    trainer.pretrain=True
                    ### pretrian first with no score
                    loss,best_model=trainer.custom_loop()

                    ### train
                    trainer_fine = Train(model_name=model_name, model=model, train_ds=train_ds, val_ds=val_ds, fold=fold)
                    trainer_fine.load_from(best_model)
                    trainer_fine.pretrain=False
                    loss, best_model = trainer_fine.custom_loop()
                else:
                    ###build trainer
                    trainer = Train(model_name=model_name, model=model, train_ds=train_ds, val_ds=val_ds, fold=fold)

                    trainer.pretrain = False
                    loss, best_model = trainer.custom_loop()



                if cfg.TRAIN.finetune_alldata:
                    ### finetune with all data
                    train_features_ = features.copy()
                    train_target_ = labels.copy()
                    train_extra_Target_ = extra_labels.copy()

                    val_ind = features.loc[features['fold'] == fold].index.to_list()
                    val_features_ = features.iloc[val_ind].copy()
                    val_target_ = labels.iloc[val_ind].copy()
                    val_extra_Target_ = extra_labels.iloc[val_ind].copy()

                    train_ds = DataIter(train_features_, train_target_, train_extra_Target_, shuffle=True,
                                        training_flag=True)
                    val_ds = DataIter(val_features_, val_target_, val_extra_Target_, shuffle=False, training_flag=False)

                    ### build model
                    model = model_dict['func']()
                    model_name = str(model_dict['name'] + str(cur_seed))

                    ###build trainer
                    trainer = Train(model_name=model_name, model=model, train_ds=train_ds, val_ds=val_ds, fold=fold)

                    trainer.reset(best_model)

                    loss, best_model = trainer.custom_loop()


                losscolector.append([loss,best_model])

        avg_loss=0
        for k,loss_and_model in enumerate(losscolector):
            print('fold %d : loss %.5f modelname: %s'%(k,loss_and_model[0],loss_and_model[1]))
            avg_loss+=loss_and_model[0]
        print('simple,average loss is ',avg_loss/(len(losscolector)))
예제 #8
0
def main():

    epochs = cfg.TRAIN.epoch
    batch_size = cfg.TRAIN.batch_size

    enable_function = False

    ### set gpu memory
    gpus = tf.config.experimental.list_physical_devices('GPU')
    if gpus:
        try:
            # Currently, memory growth needs to be the same across GPUs
            for gpu in gpus:
                tf.config.experimental.set_memory_growth(gpu, True)
            logical_gpus = tf.config.experimental.list_logical_devices('GPU')
            print(len(gpus), "Physical GPUs,", len(logical_gpus),
                  "Logical GPUs")
        except RuntimeError as e:
            # Memory growth must be set before GPUs have been initialized
            print(e)

    devices = ['/device:GPU:{}'.format(i) for i in range(cfg.TRAIN.num_gpu)]
    strategy = tf.distribute.MirroredStrategy(devices)

    ##build model
    with strategy.scope():
        if 'ShuffleNet' in cfg.MODEL.net_structure:
            model = SimpleFace_shufflenet()
        else:
            model = SimpleFace_mobilenet()
        ##run a time to build
        image = np.zeros(shape=(1, 160, 160, 3), dtype=np.float32)
        model(image)

    ###recover weights
    if cfg.MODEL.pretrained_model is not None:
        model.load_weights(cfg.MODEL.pretrained_model)

    ###build trainer
    trainer = Train(epochs, enable_function, model, batch_size, strategy)

    ###build dataset
    train_ds = DataIter(cfg.DATA.root_path, cfg.DATA.train_txt_path, True)
    test_ds = DataIter(cfg.DATA.root_path, cfg.DATA.val_txt_path, False)

    train_dataset = tf.data.Dataset.from_generator(
        train_ds,
        output_types=(tf.float32, tf.float32),
        output_shapes=([None, None, None, None], [None,
                                                  cfg.MODEL.out_channel]))
    test_dataset = tf.data.Dataset.from_generator(
        test_ds,
        output_types=(tf.float32, tf.float32),
        output_shapes=([None, None, None, None], [None,
                                                  cfg.MODEL.out_channel]))

    train_dataset = train_dataset.prefetch(
        buffer_size=tf.data.experimental.AUTOTUNE)
    test_dataset = test_dataset.prefetch(
        buffer_size=tf.data.experimental.AUTOTUNE)

    train_dist_dataset = strategy.experimental_distribute_dataset(
        train_dataset)
    test_dist_dataset = strategy.experimental_distribute_dataset(test_dataset)

    if cfg.TRAIN.vis:
        for images, labels in train_dist_dataset:

            for i in range(images.shape[0]):
                example_image = np.array(images[i], dtype=np.uint8)
                example_label = np.array(labels[i])

                landmark = example_label[0:136].reshape([-1, 2])
                _h, _w, _ = example_image.shape

                print(landmark.shape)
                for _index in range(landmark.shape[0]):
                    x_y = landmark[_index]
                    cv2.circle(example_image,
                               center=(int(x_y[0] * _w), int(x_y[1] * _w)),
                               color=(255, 255, 255),
                               radius=1,
                               thickness=-1)

                cv2.imshow('example', example_image)
                cv2.waitKey(0)

    ### train
    trainer.custom_loop(train_dist_dataset, test_dist_dataset, strategy)
예제 #9
0
파일: train.py 프로젝트: tupm2208/faceboxes
def main():

    epochs = cfg.TRAIN.epoch
    batch_size = cfg.TRAIN.batch_size

    enable_function = False

    gpus = tf.config.experimental.list_physical_devices('GPU')
    if gpus:
        try:
            # Currently, memory growth needs to be the same across GPUs
            for gpu in gpus:
                tf.config.experimental.set_memory_growth(gpu, True)
            logical_gpus = tf.config.experimental.list_logical_devices('GPU')
            print(len(gpus), "Physical GPUs,", len(logical_gpus),
                  "Logical GPUs")
        except RuntimeError as e:
            # Memory growth must be set before GPUs have been initialized
            print(e)

    strategy = tf.distribute.OneDeviceStrategy(device="/gpu:0")
    # strategy = tf.distribute.MirroredStrategy()
    with strategy.scope():
        model = FaceBoxes()

        ###run a time to build the model
        image = np.zeros(shape=(1, 512, 512, 3), dtype=np.float32)
        model.inference(image)

    ## load pretrained weights
    if cfg.MODEL.pretrained_model is not None:
        logger.info('load pretrained params from %s' %
                    cfg.MODEL.pretrained_model)
        model.load_weights(cfg.MODEL.pretrained_model)

    ### build trainer
    trainer = Train(epochs, enable_function, model, batch_size, strategy)

    ### build dataiter
    train_ds = DataIter(cfg.DATA.root_path, cfg.DATA.train_txt_path, True)
    test_ds = DataIter(cfg.DATA.root_path, cfg.DATA.val_txt_path, False)

    ### it's a tensorpack data iter, produce a batch every iter
    train_dataset = tf.data.Dataset.from_generator(
        train_ds,
        output_types=(tf.float32, tf.float32, tf.float32),
        output_shapes=([None, None, None, None], [None, None,
                                                  None], [None, None]))
    test_dataset = tf.data.Dataset.from_generator(
        test_ds,
        output_types=(tf.float32, tf.float32, tf.float32),
        output_shapes=([None, None, None, None], [None, None,
                                                  None], [None, None]))

    ####
    train_dataset = train_dataset.prefetch(
        buffer_size=tf.data.experimental.AUTOTUNE)
    test_dataset = test_dataset.prefetch(
        buffer_size=tf.data.experimental.AUTOTUNE)

    train_dist_dataset = strategy.experimental_distribute_dataset(
        train_dataset)
    test_dist_dataset = strategy.experimental_distribute_dataset(test_dataset)

    ## check the data
    if cfg.TRAIN.vis:
        for images, labels, matches in train_dist_dataset:
            for i in range(images.shape[0]):
                example_image = np.array(images[i], dtype=np.uint8)
                example_image = cv2.cvtColor(example_image, cv2.COLOR_BGR2RGB)
                example_label = np.array(labels[i])
                cv2.imshow('example', example_image)
                cv2.waitKey(0)
            break

    ##train
    trainer.custom_loop(train_dist_dataset, test_dist_dataset, strategy)
예제 #10
0
import matplotlib.pyplot as plt

from train_config import config as cfg
from lib.core.base_trainer.model import Complexer
from lib.dataset.dataietr import DataIter

data = pd.read_json('folds.json', lines=True)

cfg.TRAIN.batch_size = 1
pre_length = 91
fold = 0
###build dataset
train_data = data[data['fold'] != fold]
val_data = data[data['fold'] == fold]

train_ds = DataIter(train_data, shuffle=True, training_flag=True)
val_ds = DataIter(val_data, shuffle=True, training_flag=True)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

short_model = Complexer(pre_length=pre_length)

weight = './fold0_epoch_138_val_loss0.250854.pth'

short_model.load_state_dict(torch.load(weight, map_location=device))
short_model.to(device)

for i in range(val_data.size):
    images_np, data, target = val_ds()
    images = torch.from_numpy(images_np).to(device).float()
    data = torch.from_numpy(data).to(device).float()