コード例 #1
0
import matplotlib.pyplot as plt
import easydict
import os

FLAGS = easydict.EasyDict({
    "img_size": 256,
    "load_size": 266,
    "batch_size": 1,
    "epochs": 200,
    "lr": 0.0002,
    "n_classes": 24,
    "in_txt_path":
    "D:/[1]DB/[2]third_paper_DB/[4]Age_and_gender/race_age_gender_generation/Morph_AFAD_16_63/first_fold/AFAD-M_Morph-F_16_39_40_63/train/male_16_39_train.txt",
    "in_img_path":
    "D:/[1]DB/[1]second_paper_DB/AFAD_16_69_DB/backup/fix_AFAD/",
    "st_txt_path":
    "D:/[1]DB/[2]third_paper_DB/[4]Age_and_gender/race_age_gender_generation/Morph_AFAD_16_63/first_fold/AFAD-M_Morph-F_16_39_40_63/train/female_40_63_train.txt",
    "st_img_path":
    "D:/[1]DB/[2]third_paper_DB/[4]Age_and_gender/Morph/All/female_40_63/",
    "train": True,
    "pre_checkpoint": False,
    "pre_checkpoint_path": "",
    "save_checkpoint": "",
    "save_images": "C:/Users/Yuhwan/Pictures/test_sample",
    "graphs": ""
})

g_optim = tf.keras.optimizers.Adam(FLAGS.lr, beta_1=0.5, beta_2=0.5)
d_optim = tf.keras.optimizers.Adam(FLAGS.lr, beta_1=0.5, beta_2=0.5)
s_optim = tf.keras.optimizers.Adam(FLAGS.lr, beta_1=0.5, beta_2=0.5)
コード例 #2
0
import past_models
import dataset
from main_dcgan import train

import easydict

opt = easydict.EasyDict({
    'n_epochs': 200,
    'batch_size': 128,
    'lr': 0.0002,
    'b1': 0.5,
    'b2': 0.999,
    'n_cpu': 8,
    'latent_dim': 128,
    'img_size': 32,
    'n_critic': 1,
    'clip_value': 0.01,
    'sample_interval': 100,
    'log_interval': 10,
    'dataset': 'mnist',
    'num_filters': 128,  #for CNN Discriminator and Generator
    'saveDir': None,
    'resume': None,
    'loadDir': None
})

_, dataloader = dataset.makeDataloader(opt)

# Initialize generator and discriminator
generator = past_models.DCGANGenerator32(opt)
discriminator = past_models.DCGANDiscriminator32(opt)
コード例 #3
0
ファイル: trials.py プロジェクト: xl359/AA-203
import torch.optim as optim
from torch.distributions import Normal
import matplotlib.pyplot as plt

from quad_env import QuadEnv

# Cart Pole
# based on:
# https://github.com/pytorch/examples/blob/master/reinforcement_learning/actor_critic.py

# args = parser.parse_args()

args = easydict.EasyDict({
    "gamma": 0.99,
    "seed": 203,
    "render": False,
    "log_interval": 10,
    "write_logger": True
})

# env = gym.make('LunarLanderContinuous-v2')
env = QuadEnv()

# env.seed(args.seed)
torch.manual_seed(args.seed)

SavedAction = namedtuple('SavedAction', ['log_prob', 'value'])
# state_dim = env.observation_space.shape[0]
state_dim = 21
# action_dim = env.action_space.shape[0]
action_dim = 4
コード例 #4
0
    def _forward_test(self, input):
        cnn_features = input
        arg = easydict.EasyDict({
            'clip_boxes': self.test_clip_boxes,
            'nms_thresh': self.test_nms_thresh,
            'max_proposals': self.test_max_proposals
        })

        # Make sure that setImageSize has been called
        assert self.image_height and self.image_width and not self._called_forward_size, \
         'Must call setImageSize before each forward pass'
        self._called_forward_size = True

        rpn_out, act_reg = self.rpn.forward(cnn_features)
        rpn_boxes, rpn_anchors, rpn_trans, rpn_scores = rpn_out
        num_boxes = rpn_boxes.size(1)

        # Maybe clip boxes to image boundary
        if arg.clip_boxes:
            bounds = {
                'x_min': 1,
                'y_min': 1,
                'x_max': self.image_width,
                'y_max': self.image_height
            }
            rpn_boxes, valid = box_utils.clip_boxes(rpn_boxes, bounds,
                                                    'xcycwh')

            #print(string.format('%d/%d boxes are predicted valid',
            #      torch.sum(valid), valid:nElement()))

            #Clamp parallel arrays only to valid boxes (not oob of the image)
            rpn_boxes = self.clamp_data(rpn_boxes, valid)
            rpn_anchors = self.clamp_data(rpn_anchors, valid)
            rpn_trans = self.clamp_data(rpn_trans, valid)
            rpn_scores = self.clamp_data(rpn_scores, valid)
            num_boxes = rpn_boxes.size(1)

        # Convert rpn boxes from (xc, yc, w, h) format to (x1, y1, x2, y2)
        rpn_boxes_x1y1x2y2 = box_utils.xcycwh_to_x1y1x2y2(rpn_boxes[0])

        # Convert objectness positive / negative scores to probabilities
        rpn_scores_exp = torch.exp(rpn_scores)
        pos_exp = rpn_scores_exp[0, :, 0]
        neg_exp = rpn_scores_exp[0, :, 1]
        scores = (pos_exp + neg_exp).pow(-1) * pos_exp

        verbose = False
        if verbose:
            print('in LocalizationLayer forward_test')
            print('Before NMS there are %d boxes' % num_boxes)
            print('Using NMS threshold %f' % arg.nms_thresh)

        #Run NMS and sort by objectness score
        boxes_scores = torch.cat((rpn_boxes_x1y1x2y2, scores.view(-1, 1)),
                                 dim=1)

        if arg.max_proposals == -1:
            idx = box_utils.nms(boxes_scores.data, arg.nms_thresh)
        else:
            idx = box_utils.nms(boxes_scores.data, arg.nms_thresh,
                                arg.max_proposals)

        rpn_boxes_nms = torch.squeeze(rpn_boxes)[idx]

        if verbose:
            print('After NMS there are %d boxes' % rpn_boxes_nms.size(0))

        output = rpn_boxes_nms
        return output
コード例 #5
0
FLAGS = easydict.EasyDict({"img_height": 128,
                           
                           "img_width": 88,
                           
                           "tr_txt_path": "D:/[1]DB/[4]etc_experiment/Body_age/OULP-Age/train.txt",
                           
                           "tr_txt_name": "D:/[1]DB/[4]etc_experiment/Body_age/OULP-Age/GEI_IDList_train.txt",
                           
                           "tr_img_path": "D:/[1]DB/[4]etc_experiment/Body_age/OULP-Age/GEI/",
                           
                           "te_txt_path": "D:/[1]DB/[4]etc_experiment/Body_age/OULP-Age/test.txt",
                           
                           "te_txt_name": "D:/[1]DB/[4]etc_experiment/Body_age/OULP-Age/GEI_IDList_test_fix.txt",
                           
                           "te_img_path": "D:/[1]DB/[4]etc_experiment/Body_age/OULP-Age/GEI/",
                           
                           "batch_size": 200,
                           
                           "epochs": 300,
                           
                           "num_classes": 86,
                           
                           "lr": 0.0001,
                           
                           "save_checkpoint": "",
                           
                           "graphs": "C:/Users/Yuhwan/Downloads/", 
                           
                           "train": True,
                           
                           "pre_checkpoint": False,
                           
                           "pre_checkpoint_path": ""})
コード例 #6
0
ファイル: appFace.py プロジェクト: ChuckTan123/insightface
import wget

import easydict
import matplotlib.pyplot as plt
import time
import numpy as np
import cv2
import face_embedding
import argparse
import os
import logging

parser = argparse.ArgumentParser(description='face model test')
args = easydict.EasyDict()
args["image_size"] = '112,112'
args["model"] = '../models/model,0'
args["gpu"] = "-1"
args["det"] = 2
args["flip"] = 0
args["threshold"] = 1.24
model = face_embedding.FaceModel(args)

people_list = ["victor", "zhang", "zhou"]
info = [
    "You have a dental appointment today at 10:30 am in Santa Clara Hospital",
    "Your daughter will visit you at 12 am at your home.",
    "Please don't forget to take pills every 8 hours from 8 am."
]
print("Here are the people we recognize: {}".format(people_list))
imgs_list = [[cv2.imread("{}/{}.jpeg".format(person, i)) for i in range(1, 5)]
             for person in people_list]
コード例 #7
0
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
    tf.config.experimental.set_memory_growth(physical_devices[0], True)

from absl.flags import FLAGS
from tensorflow.python.saved_model import tag_constants
import cv2
import numpy as np
import easydict

FLAGS = easydict.EasyDict(
    {
        'framework' : 'tf',
        'weights' : './checkpoints/yolov4-tiny-416',
        'size' : 416,
        'tiny' : True,
        'model' : 'yolov4',
        'iou' : 0.30,
        'score' : 0.30
    }
)

class PeopleDetector:
    def __init__(self):
        self.input_size = FLAGS.size
        self.saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING])
        self.infer = self.saved_model_loaded.signatures['serving_default']

    def detect(self, frame) -> (bool, list, list) :
        input_size = self.input_size
        infer = self.infer
コード例 #8
0
from random import *

model_names = sorted(name for name in models.__dict__
                     if name.islower() and not name.startswith("__")
                     and callable(models.__dict__[name]))

args = easydict.EasyDict({
    "arch": 'resnet18',
    "root": "/home/diml/ddrive/dataset/deepfake_1st",
    "train_list": "train_list_1st.txt",
    "test_list": "test_list_1st.txt",
    "epochs": 200,
    "batch_size": 2,
    "lr": 0.01,
    "momentum": 0.9,
    "weight_decay": 5e-4,
    "print_freq": 100,
    "eval_freq": 10,
    "workers": 4,
    #"resume":"10fgsm_b7_checkpoint.pth.tar",
    "resume": False,
    "pretrained": False,
    "evaluate": False,
    "start_epoch": 0,
    "gpu": 0,
})

best_acc1 = 0


# for fgsm image generation
def fgsm_attack(model, loss, images, labels, eps):
コード例 #9
0
ファイル: trainer.py プロジェクト: 282560/Text2Image
    def __init__(self, dataset, split, lr, save_path, l1_coef, l2_coef,
                 pre_trained_gen, pre_trained_disc, val_pre_trained_gen,
                 val_pre_trained_disc, batch_size, num_workers, epochs,
                 dataset_paths, arrangement, sampling):

        with open('config.yaml',
                  'r') as f:  # Wsteczna kompatybilnosc dla Text2ImageDataset
            config = yaml.safe_load(f)

        self.generator = torch.nn.DataParallel(
            gan_factory.generator_factory('gan').cuda())
        self.discriminator = torch.nn.DataParallel(
            gan_factory.discriminator_factory('gan').cuda())

        if pre_trained_disc:
            self.discriminator.load_state_dict(torch.load(pre_trained_disc))
        else:
            self.discriminator.apply(Utils.weights_init)

        if pre_trained_gen:
            self.generator.load_state_dict(torch.load(pre_trained_gen))
        else:
            self.generator.apply(Utils.weights_init)

        self.val_pre_trained_gen = val_pre_trained_gen
        self.val_pre_trained_disc = val_pre_trained_disc
        self.arrangement = arrangement
        self.sampling = sampling

        if dataset == 'birds':  # Wsteczna kompatybilnosc dla Text2ImageDataset
            self.dataset = Text2ImageDataset(
                config['birds_dataset_path'], split=split
            )  # '...\Text2Image\datasets\ee285f-public\caltech_ucsd_birds\birds.hdf5'
        elif dataset == 'flowers':  # Wsteczna kompatybilnosc dla Text2ImageDataset
            self.dataset = Text2ImageDataset(
                config['flowers_dataset_path'], split=split
            )  # '...\Text2Image\datasets\ee285f-public\oxford_flowers\flowers.hdf5'
        elif dataset == 'live':
            self.dataset_dict = easydict.EasyDict(dataset_paths)
            self.dataset = Text2ImageDataset2(
                datasetFile=self.dataset_dict.datasetFile,
                imagesDir=self.dataset_dict.imagesDir,
                textDir=self.dataset_dict.textDir,
                split=split,
                arrangement=arrangement,
                sampling=sampling)
        else:
            print('Dataset not supported.')

        print('Images =', len(self.dataset))
        self.noise_dim = 100
        self.batch_size = batch_size
        self.num_workers = num_workers
        self.lr = lr
        self.beta1 = 0.5
        self.num_epochs = epochs

        self.l1_coef = l1_coef
        self.l2_coef = l2_coef

        self.data_loader = DataLoader(
            self.dataset,
            batch_size=self.batch_size,
            shuffle=True,
            num_workers=self.num_workers
        )  # shuffle=True - przetasowuje zbior danych w kazdej epoce

        self.optimD = torch.optim.Adam(self.discriminator.parameters(),
                                       lr=self.lr,
                                       betas=(self.beta1, 0.999))
        self.optimG = torch.optim.Adam(self.generator.parameters(),
                                       lr=self.lr,
                                       betas=(self.beta1, 0.999))

        self.checkpoints_path = 'checkpoints'
        self.save_path = save_path

        self.loss_filename_t = 'gd_loss_t.csv'
        self.loss_filename_v = 'gd_loss_v.csv'
コード例 #10
0
    def testTrainableVariables(self):
        inputs = tf.placeholder(tf.float32, [1, 224, 224, 3])

        model = TruncatedBaseNetwork(
            easydict.EasyDict({
                'architecture': 'resnet_v1_50',
                'endpoint': 'block4/unit_3/bottleneck_v1/conv2',
                'freeze_tail': False,
                'use_tail': True,
            })
        )
        model(inputs)
        # Variables in ResNet-50:
        # (the order of beta and gamma depends on the TensorFlow's version)
        #   0 conv1/weights:0
        #   1 conv1/BatchNorm/(beta|gamma):0
        #   2 conv1/BatchNorm/(beta|gamma):0
        #   3 block1/unit_1/bottleneck_v1/shortcut/weights:0
        #   (...)
        #   153 block4/unit_3/bottleneck_v1/conv2/weights:0
        #   154 block4/unit_3/bottleneck_v1/conv2/BatchNorm/(beta|gamma):0
        #   155 block4/unit_3/bottleneck_v1/conv2/BatchNorm/(beta|gamma):0
        #   --- endpoint ---
        #   156 block4/unit_3/bottleneck_v1/conv3/weights:0
        #   157 block4/unit_3/bottleneck_v1/conv3/BatchNorm/(beta|gamma):0
        #   158 block4/unit_3/bottleneck_v1/conv3/BatchNorm/(beta|gamma):0
        #   159 logits/weights:0
        #   160 logits/biases:0
        trainable_vars = model.get_trainable_vars()
        self.assertEqual(len(trainable_vars), 156)
        self.assertEqual(
            trainable_vars[-3].name,
            'truncated_base_network/resnet_v1_50/' +
            'block4/unit_3/bottleneck_v1/conv2/weights:0'
        )

        model = TruncatedBaseNetwork(
            easydict.EasyDict({
                'architecture': 'resnet_v1_50',
                'endpoint': 'block4/unit_2/bottleneck_v1/conv2',
                'fine_tune_from': 'block4/unit_2/bottleneck_v1/conv1',
                'freeze_tail': False,
                'use_tail': True,
            })
        )
        model(inputs)
        trainable_vars = model.get_trainable_vars()
        # Now there should be only 6 trainable vars:
        #   141 block4/unit_2/bottleneck_v1/conv1/weights:0
        #   142 block4/unit_2/bottleneck_v1/conv1/BatchNorm/beta:0
        #   143 block4/unit_2/bottleneck_v1/conv1/BatchNorm/gamma:0
        #   144 block4/unit_2/bottleneck_v1/conv2/weights:0
        #   145 block4/unit_2/bottleneck_v1/conv2/BatchNorm/beta:0
        #   146 block4/unit_2/bottleneck_v1/conv2/BatchNorm/gamma:0
        self.assertEqual(len(trainable_vars), 6)

        #
        # Check that we return no vars if fine_tune_from is after the chosen
        # endpoint (there is nothing to fine-tune!) and tail is frozen.
        #
        model = TruncatedBaseNetwork(
            easydict.EasyDict(
                {
                    'architecture': 'resnet_v1_50',
                    'endpoint': 'block4/unit_2/bottleneck_v1/conv1',
                    'fine_tune_from': 'block4/unit_2/bottleneck_v1/conv2',
                    'freeze_tail': True,
                    'use_tail': True,
                }
            )
        )
        model(inputs)
        self.assertEqual(len(model.get_trainable_vars()), 0)
コード例 #11
0
        elif options.mode == 1:
            model.sample()


options = easydict.EasyDict({
    'batch': 100,
    'seed': random.randint(0, 2**31 - 1),
    'name': 'CGAN',
    'mode': 1,
    'dataset': 'linnaeus5',
    'dataset_path': './gdrive/My Drive/dataset',
    'checkpoints_path': './gdrive/My Drive/dataset/checkpoints',
    'batch_size': 30,
    'epochs': 201,
    'lr': 3e-4,
    'lr_decay': True,
    'lr_decay_rate': 0.1,
    'lr_decay_steps': 5e5,
    'gpu_ids': '0',
    'save': True,
    'save_interval': 1200 / 30,
    'sample': True,
    'sample_size': 10,
    'sample_interval': 1200 / 30,
    'training': 0
})

os.environ['CUDA_VISIBLE_DEVICES'] = '0'

options.training = options.mode != 1
options.dataset_path += ('/' + options.dataset)
コード例 #12
0
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm, tqdm_notebook, trange

from configs import getConfig
from model.data_loader import kfold_data_generator
from model.dataset import MSI_train_Dataset, MSI_valid_Dataset
from model.net import fpn, unet
from utils import save_setting_info

# arg = getConfig()
arg = easydict.EasyDict({
    "model_name": 'unet',
    "k_folds": 1,
    "BACKBONE": 'resnet34',
    "PRETRAINED": None,
    "epochs": 300,
    "BATCH_SIZE": 32,
    'lr': 0.001,
    'WORKERS': 0,
    'Threshold': 0.7
})

# 구글스프레드시트 설정
scope = [
    'https://spreadsheets.google.com/feeds',
    'https://www.googleapis.com/auth/drive',
]

json_file_name = 'money-check-260910-c217b2d4ba6a.json'

credentials = ServiceAccountCredentials.from_json_keyfile_name(
コード例 #13
0
import torch.optim as optim
import torch.utils.data as data
import torchvision
import torch.nn.functional as F
from torchvision import models, transforms
# -

torch.manual_seed(1234)
np.random.seed(1234)
random.seed(1234)

args = easydict.EasyDict({
    "train_root_dir": "../data/main/train",
    "val_root_dir": "../data/main/val",
    "batch_size": 4,
    "size": (480, 640),
    "num_worker": 1,
    "lr": 1e-4,
    "start_epoch": 1,
    "result_path": './results_AODNet',
})

device_str = "cuda:3"
device = torch.device(device_str if torch.cuda.is_available() else "cpu")


class HazeDataset(data.Dataset):
    def __init__(self, root_dir, size=(480, 640)):
        self.root_dir, _, self.files = next(os.walk(root_dir))
        self.size = size

    def __len__(self):
コード例 #14
0
def main():
    args = easydict.EasyDict({
        "dataroot": "/mnt/gold/users/s18150/mywork/pytorch/data/gan",
        "save_dir": "./",
        "prefix": "minibach_feature",
        "workers": 8,
        "batch_size": 128,
        "image_size": 64,
        "nc": 3,
        "nz": 100,
        "ngf": 64,
        "ndf": 64,
        "epochs": 50,
        "lr": 0.0002,
        "beta1": 0.5,
        "gpu": 9,
        "use_cuda": True,
        "feature_matching": True,
        "mini_batch": True
    })

    manualSeed = 999
    random.seed(manualSeed)
    torch.manual_seed(manualSeed)
    device = torch.device(
        'cuda:{}'.format(args.gpu) if args.use_cuda else 'cpu')

    transform = transforms.Compose([
        transforms.Resize(args.image_size),
        transforms.CenterCrop(args.image_size),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])

    dataset = dset.ImageFolder(root=args.dataroot, transform=transform)
    dataloader = torch.utils.data.DataLoader(dataset,
                                             batch_size=args.batch_size,
                                             shuffle=True,
                                             num_workers=args.workers)

    # Generator用のモデルのインスタンス作成
    netG = net.Generator(args.nz, args.ngf, args.nc).to(device)
    # Generator用のモデルの初期値を設定
    netG.apply(net.weights_init)

    # Discriminator用のモデルのインスタンス作成
    netD = net.Discriminator(args.nc, args.ndf, device, args.batch_size,
                             args.mini_batch).to(device)
    # Discriminator用のモデルの初期値を設定
    netD.apply(net.weights_init)

    # BCE Loss classのインスタンスを作成
    criterionD = nn.BCELoss()

    if args.feature_matching is True:
        criterionG = nn.MSELoss(reduction='elementwise_mean')
    else:
        criterionG = nn.BCELoss()

    # Generatorに入力するノイズをバッチごとに作成 (バッチ数は64)
    # これはGeneratorの結果を描画するために使用する
    fixed_noise = torch.randn(64, args.nz, 1, 1, device=device)

    # 最適化関数のインスタンスを作成
    optimizerD = optim.Adam(netD.parameters(),
                            lr=args.lr,
                            betas=(args.beta1, 0.999))
    optimizerG = optim.Adam(netG.parameters(),
                            lr=args.lr,
                            betas=(args.beta1, 0.999))

    r = run.NNRun(netD, netG, optimizerD, optimizerG, criterionD, criterionG,
                  device, fixed_noise, args)

    # 学習
    r.train(dataloader)
コード例 #15
0
def GlowCS(args):
    if args.init_norms == None:
        args.init_norms = [None] * len(args.m)
    else:
        assert args.init_strategy == "random_fixed_norm", "init_strategy should be random_fixed_norm if init_norms is used"
    assert len(args.m) == len(args.gamma) == len(
        args.init_norms
    ), "length of either m, gamma or init_norms are not same"
    loopOver = zip(args.m, args.gamma, args.init_norms)

    for m, gamma, init_norm in loopOver:
        skip_to_next = False  # flag to skip to next loop if recovery is fails due to instability
        n = args.size * args.size * 3
        modeldir = "./trained_models/%s/glow" % args.model
        test_folder = "./test_images/%s" % args.dataset
        save_path = "./results/%s/%s" % (args.dataset, args.experiment)

        # loading dataset
        trans = transforms.Compose(
            [transforms.Resize((args.size, args.size)),
             transforms.ToTensor()])
        test_dataset = datasets.ImageFolder(test_folder, transform=trans)
        test_dataloader = torch.utils.data.DataLoader(
            test_dataset,
            batch_size=args.batchsize,
            drop_last=False,
            shuffle=False)

        # loading glow configurations
        config_path = modeldir + "/configs.json"
        with open(config_path, 'r') as f:
            configs = json.load(f)

        # sensing matrix
        A = np.random.normal(0, 1 / np.sqrt(m), size=(n, m))
        A = torch.tensor(A,
                         dtype=torch.float,
                         requires_grad=False,
                         device=args.device)

        # regularizor
        gamma = torch.tensor(gamma,
                             requires_grad=True,
                             dtype=torch.float,
                             device=args.device)

        # adding noise
        if args.noise == "random_bora":
            noise = np.random.normal(0, 1, size=(args.batchsize, m))
            noise = noise * 0.1 / np.sqrt(m)
            noise = torch.tensor(noise,
                                 dtype=torch.float,
                                 requires_grad=False,
                                 device=args.device)
        else:
            noise = np.random.normal(0, 1, size=(args.batchsize, m))
            noise = noise / (np.linalg.norm(noise, 2, axis=-1,
                                            keepdims=True)) * float(args.noise)
            noise = torch.tensor(noise,
                                 dtype=torch.float,
                                 requires_grad=False,
                                 device=args.device)

        # start solving over batches
        Original = []
        Recovered = []
        Z_Recovered = []
        Residual_Curve = []
        Recorded_Z = []
        for i, data in enumerate(test_dataloader):
            x_test = data[0]
            x_test = x_test.clone().to(device=args.device)
            n_test = x_test.size()[0]
            assert n_test == args.batchsize, "please make sure that no. of images are evenly divided by batchsize"

            # loading glow model
            glow = Glow((3, args.size, args.size),
                        K=configs["K"],
                        L=configs["L"],
                        coupling=configs["coupling"],
                        n_bits_x=configs["n_bits_x"],
                        nn_init_last_zeros=configs["last_zeros"],
                        device=args.device)
            glow.load_state_dict(torch.load(modeldir + "/glowmodel.pt"))
            glow.eval()

            # making a forward to record shapes of z's for reverse pass
            _ = glow(glow.preprocess(torch.zeros_like(x_test)))

            # initializing z from Gaussian with std equal to init_std
            if args.init_strategy == "random":
                z_sampled = np.random.normal(0, args.init_std, [n_test, n])
                z_sampled = torch.tensor(z_sampled,
                                         requires_grad=True,
                                         dtype=torch.float,
                                         device=args.device)
            # intializing z from Gaussian and scaling its norm to init_norm
            elif args.init_strategy == "random_fixed_norm":
                z_sampled = np.random.normal(0, 1, [n_test, n])
                z_sampled = z_sampled / np.linalg.norm(
                    z_sampled, axis=-1, keepdims=True)
                z_sampled = z_sampled * init_norm
                z_sampled = torch.tensor(z_sampled,
                                         requires_grad=True,
                                         dtype=torch.float,
                                         device=args.device)
                print("z intialized with a norm equal to = %0.1f" % init_norm)
            # initializing z from pseudo inverse
            elif args.init_strategy == "pseudo_inverse":
                x_test_flat = x_test.view([-1, n])
                y_true = torch.matmul(x_test_flat, A) + noise
                A_pinv = torch.pinverse(A)
                x_pinv = torch.matmul(y_true, A_pinv)
                x_pinv = x_pinv.view([-1, 3, args.size, args.size])
                x_pinv = torch.clamp(x_pinv, 0, 1)
                z, _, _ = glow(glow.preprocess(x_pinv * 255, clone=True))
                z = glow.flatten_z(z).clone().detach()
                z_sampled = torch.tensor(z,
                                         requires_grad=True,
                                         dtype=torch.float,
                                         device=args.device)
            # initializing z from a solution of lasso-wavelet
            elif args.init_strategy == "lasso_wavelet":
                new_args = {
                    "batch_size": n_test,
                    "lmbd": 0.01,
                    "lasso_solver": "sklearn"
                }
                new_args = easydict.EasyDict(new_args)
                estimator = celebA_estimators.lasso_wavelet_estimator(new_args)
                x_ch_last = x_test.permute(0, 2, 3, 1)
                x_ch_last = x_ch_last.contiguous().view([-1, n])
                y_true = torch.matmul(x_ch_last, A) + noise
                x_lasso = estimator(
                    np.sqrt(2 * m) * A.data.cpu().numpy(),
                    np.sqrt(2 * m) * y_true.data.cpu().numpy(), new_args)
                x_lasso = np.array(x_lasso)
                x_lasso = x_lasso.reshape(-1, 64, 64, 3)
                x_lasso = x_lasso.transpose(0, 3, 1, 2)
                x_lasso = torch.tensor(x_lasso,
                                       dtype=torch.float,
                                       device=args.device)
                z, _, _ = glow(x_lasso - 0.5)
                z = glow.flatten_z(z).clone().detach()
                z_sampled = torch.tensor(z,
                                         requires_grad=True,
                                         dtype=torch.float,
                                         device=args.device)
                print("z intialized from a solution of lasso-wavelet")
            # intializing z from null(A)
            elif args.init_strategy == "null_space":
                x_test_flat = x_test.view([-1, n])
                x_test_flat_np = x_test_flat.data.cpu().numpy()
                A_np = A.data.cpu().numpy()
                nullA = null_space(A_np.T)
                coeff = np.random.normal(0, 1,
                                         (args.batchsize, nullA.shape[1]))
                x_null = np.array([(nullA * c).sum(axis=-1) for c in coeff])
                pert_norm = 5  # <-- 5 gives optimal results --  bad initialization and not too unstable
                x_null = x_null / np.linalg.norm(x_null, axis=1,
                                                 keepdims=True) * pert_norm
                x_perturbed = x_test_flat_np + x_null
                # no clipping x_perturbed to make sure forward model is ||y-Ax|| is the same
                err = np.matmul(x_test_flat_np, A_np) - np.matmul(
                    x_perturbed, A_np)
                assert (err**2).sum(
                ) < 1e-6, "null space does not satisfy ||y-A(x+x0)|| <= 1e-6"
                x_perturbed = x_perturbed.reshape(-1, 3, args.size, args.size)
                x_perturbed = torch.tensor(x_perturbed,
                                           dtype=torch.float,
                                           device=args.device)
                z, _, _ = glow(x_perturbed - 0.5)
                z = glow.flatten_z(z).clone().detach()
                z_sampled = torch.tensor(z,
                                         requires_grad=True,
                                         dtype=torch.float,
                                         device=args.device)
                print("z initialized from a point in null space of A")
            else:
                raise "Initialization strategy not defined"

            # selecting optimizer
            if args.optim == "adam":
                optimizer = torch.optim.Adam(
                    [z_sampled],
                    lr=args.lr,
                )
            elif args.optim == "lbfgs":
                optimizer = torch.optim.LBFGS(
                    [z_sampled],
                    lr=args.lr,
                )
            else:
                raise "optimizer not defined"

            # to be recorded over iteration
            psnr_t = torch.nn.MSELoss().to(device=args.device)
            residual = []
            recorded_z = []
            # running optimizer steps
            for t in range(args.steps):

                def closure():
                    optimizer.zero_grad()
                    z_unflat = glow.unflatten_z(z_sampled, clone=False)
                    x_gen = glow(z_unflat, reverse=True, reverse_clone=False)
                    x_gen = glow.postprocess(x_gen, floor_clamp=False)
                    x_test_flat = x_test.view([-1, n])
                    x_gen_flat = x_gen.view([-1, n])
                    y_true = torch.matmul(x_test_flat, A) + noise
                    y_gen = torch.matmul(x_gen_flat, A)
                    global residual_t
                    residual_t = ((y_gen - y_true)**2).sum(dim=1).mean()
                    z_reg_loss_t = gamma * z_sampled.norm(dim=1).mean()
                    loss_t = residual_t + z_reg_loss_t
                    psnr = psnr_t(x_test, x_gen)
                    psnr = 10 * np.log10(1 / psnr.item())
                    print(
                        "\rAt step=%0.3d|loss=%0.4f|residual=%0.4f|z_reg=%0.5f|psnr=%0.3f"
                        % (t, loss_t.item(), residual_t.item(),
                           z_reg_loss_t.item(), psnr),
                        end="\r")
                    loss_t.backward()
                    return loss_t

                try:
                    optimizer.step(closure)
                    recorded_z.append(z_sampled.data.cpu().numpy())
                    residual.append(residual_t.item())
                except:
                    # try may not work due to instability in the reverse direction.
                    skip_to_next = True
                    break

            if skip_to_next:
                break

            # getting recovered and true images
            with torch.no_grad():
                x_test_np = x_test.data.cpu().numpy().transpose(0, 2, 3, 1)
                z_unflat = glow.unflatten_z(z_sampled, clone=False)
                x_gen = glow(z_unflat, reverse=True, reverse_clone=False)
                x_gen = glow.postprocess(x_gen, floor_clamp=False)
                x_gen_np = x_gen.data.cpu().numpy().transpose(0, 2, 3, 1)
                x_gen_np = np.clip(x_gen_np, 0, 1)
                z_recov = z_sampled.data.cpu().numpy()
            Original.append(x_test_np)
            Recovered.append(x_gen_np)
            Z_Recovered.append(z_recov)
            Residual_Curve.append(residual)
            Recorded_Z.append(recorded_z)

            # freeing up memory for second loop
            glow.zero_grad()
            optimizer.zero_grad()
            del x_test, x_gen, optimizer, psnr_t, z_sampled, glow
            torch.cuda.empty_cache()
            print("\nbatch completed")

        if skip_to_next:
            print(
                "\nskipping current loop due to instability or user triggered quit"
            )
            continue

        # collecting everything together
        Original = np.vstack(Original)
        Recovered = np.vstack(Recovered)
        Z_Recovered = np.vstack(Z_Recovered)
        Recorded_Z = np.vstack(Recorded_Z)
        psnr = [compare_psnr(x, y) for x, y in zip(Original, Recovered)]
        z_recov_norm = np.linalg.norm(Z_Recovered, axis=-1)

        # print performance analysis
        printout = "+-" * 10 + "%s" % args.dataset + "-+" * 10 + "\n"
        printout = printout + "\t n_test        = %d\n" % len(Recovered)
        printout = printout + "\t n             = %d\n" % n
        printout = printout + "\t m             = %d\n" % m
        printout = printout + "\t gamma         = %0.6f\n" % gamma
        printout = printout + "\t optimizer     = %s\n" % args.optim
        printout = printout + "\t lr            = %0.3f\n" % args.lr
        printout = printout + "\t steps         = %0.3f\n" % args.steps
        printout = printout + "\t init_strategy = %s\n" % args.init_strategy
        printout = printout + "\t init_std      = %0.3f\n" % args.init_std
        if init_norm is not None:
            printout = printout + "\t init_norm     = %0.3f\n" % init_norm
        printout = printout + "\t z_recov_norm  = %0.3f\n" % np.mean(
            z_recov_norm)
        printout = printout + "\t PSNR          = %0.3f\n" % (np.mean(psnr))
        print(printout)

        # saving printout
        if args.save_metrics_text:
            with open("%s_cs_glow_results.txt" % args.dataset, "a") as f:
                f.write('\n' + printout)

        # setting folder to save results in
        if args.save_results:
            gamma = gamma.item()
            file_names = [
                name[0].split("/")[-1] for name in test_dataset.samples
            ]
            if args.init_strategy == "random":
                save_path_template = save_path + "/cs_m_%d_gamma_%0.6f_steps_%d_lr_%0.3f_init_std_%0.2f_optim_%s"
                save_path = save_path_template % (
                    m, gamma, args.steps, args.lr, args.init_std, args.optim)
            elif args.init_strategy == "random_fixed_norm":
                save_path_template = save_path + "/cs_m_%d_gamma_%0.6f_steps_%d_lr_%0.3f_init_%s_%0.3f_optim_%s"
                save_path = save_path_template % (m, gamma, args.steps,
                                                  args.lr, args.init_strategy,
                                                  init_norm, args.optim)
            else:
                save_path_template = save_path + "/cs_m_%d_gamma_%0.6f_steps_%d_lr_%0.3f_init_%s_optim_%s"
                save_path = save_path_template % (m, gamma, args.steps,
                                                  args.lr, args.init_strategy,
                                                  args.optim)
            if not os.path.exists(save_path):
                os.makedirs(save_path)
            else:
                save_path_1 = save_path + "_1"
                if not os.path.exists(save_path_1):
                    os.makedirs(save_path_1)
                    save_path = save_path_1
                else:
                    save_path_2 = save_path + "_2"
                    if not os.path.exists(save_path_2):
                        os.makedirs(save_path_2)
                        save_path = save_path_2
            # saving results now
            _ = [
                sio.imsave(save_path + "/" + name, x)
                for x, name in zip(Recovered, file_names)
            ]
            Residual_Curve = np.array(Residual_Curve).mean(axis=0)
            np.save(save_path + "/original.npy", Original)
            np.save(save_path + "/recovered.npy", Recovered)
            np.save(save_path + "/z_recovered.npy", Z_Recovered)
            np.save(save_path + "/residual_curve.npy", Residual_Curve)
            if init_norm is not None:
                np.save(save_path + "/Recorded_Z_init_norm_%d.npy" % init_norm,
                        Recorded_Z)
        torch.cuda.empty_cache()
コード例 #16
0
ファイル: module.py プロジェクト: khu-nlplab/WiseKB-KHU-2019
import easydict

import torchtext
import torchtext.data as data
import torchtext.datasets as datasets

import datetime
import pickle
import dill
import re
import timeit
import operator

args = easydict.EasyDict({
    "embed_num": 3257,
    "embed_dim": 128,
    "kernel_num": 100,
    "class_num": 3
})

FOOD_QM = "data/food/MEM data/QM.txt"
SHOPPING_QM = "data/shopping/MEM data/QM.txt"
WEATHER_QM = "data/weather/MEM data/QM.txt"

FOOD_POS = "data/food/MEM data/positive_org.txt"
FOOD_NEG = "data/food/MEM data/negative_org.txt"
SHOPPING_POS = "data/shopping/MEM data/positive_org.txt"
SHOPPING_NEG = "data/shopping/MEM data/negative_org.txt"
WEATHER_POS = "data/weather/MEM data/positive_org.txt"
WEATHER_NEG = "data/weather/MEM data/negative_org.txt"

if torch.cuda.is_available():
コード例 #17
0
def DCTCS(args):
    loopOver = zip(args.m, args.gamma)
    for m, gamma in loopOver:
        n = args.size * args.size * 3
        test_folder = "./test_images/%s/imgs" % args.dataset
        save_path = "./results/%s/%s" % (args.dataset, args.experiment)
        divide_by = 255  # "max" or 255 or None
        # loading test images
        x_test = [PIL.Image.open(p) for p in glob(test_folder + "/*")]
        file_names = [name.split("/")[-1] for name in glob(test_folder + "/*")]
        x_test = [
            img.resize((args.size, args.size), PIL.Image.BILINEAR) if
            (img.size[0] != 64) else img for img in x_test
        ]
        x_test = [np.array(img) for img in x_test]
        x_test = np.array(x_test)
        # normalizing images
        if divide_by == "max":
            x_test = x_test / x_test.max(axis=(1, 2, 3), keepdims=True)
        elif divide_by == 255:
            x_test = x_test / 255
        elif divide_by == None:
            pass
        n_test = x_test.shape[0]
        # arg parser to pass to solver methods
        new_args = {
            "batch_size": n_test,
            "lmbd": gamma,
            "lasso_solver": "sklearn"
        }
        new_args = easydict.EasyDict(new_args)
        estimator = celebA_estimators.lasso_dct_estimator(new_args)
        A = np.random.normal(0, 1 / np.sqrt(m), size=(n, m))
        # adding noise
        if args.noise == "random_bora":
            noise = np.random.normal(0, 1, size=(n_test, m))
            noise = noise * 0.1 / np.sqrt(m)
        else:
            noise = np.random.normal(0, 1, size=(n_test, m))
            noise = noise / (np.linalg.norm(noise, 2, axis=-1,
                                            keepdims=True)) * float(args.noise)
        y_true = np.matmul(x_test.reshape(n_test, -1), A) + noise
        x_hat = estimator(
            np.sqrt(2 * m) * A,
            np.sqrt(2 * m) * y_true, new_args)
        x_hat = np.array(x_hat)
        x_hat = x_hat.reshape(-1, 64, 64, 3)

        x_hat = np.clip(x_hat, 0, 1)
        psnr = [compare_psnr(x, xhat) for x, xhat in zip(x_test, x_hat)]
        # print performance analysis
        printout = "+-" * 10 + "%s" % args.dataset + "-+" * 10 + "\n"
        printout = printout + "\t n_test    = %d\n" % len(x_hat)
        printout = printout + "\t n         = %d\n" % n
        printout = printout + "\t m         = %d\n" % m
        printout = printout + "\t solver    = lasso_dct\n"
        printout = printout + "\t gamma     = %0.8f\n" % gamma
        printout = printout + "\t PSNR      = %0.3f \n" % np.mean(psnr)
        print(printout)

        if args.save_metrics_text:
            with open("%s_cs_dct_results.txt" % args.dataset, "a") as f:
                f.write('\n' + printout)

        # saving images
        if args.save_results:
            save_path_template = save_path + "/cs_m_%d_lasso_dct_gamma_%0.8f"
            save_path = save_path_template % (m, gamma)
            if not os.path.exists(save_path):
                os.makedirs(save_path)
            else:
                save_path_1 = save_path + "_1"
                if not os.path.exists(save_path_1):
                    os.makedirs(save_path_1)
                    save_path = save_path_1
                else:
                    save_path_2 = save_path + "_2"
                    if not os.path.exists(save_path_2):
                        os.makedirs(save_path_2)
                        save_path = save_path_2

            _ = [
                sio.imsave(save_path + "/" + name, x)
                for x, name in zip(x_hat, file_names)
            ]
            #            _ = [sio.imsave(save_path+"/"+name.split(".")[0]+".jpg", x, quality=100) for x,name in zip(x_hat,file_names)]
            np.save(save_path + "/original.npy", x_test)
            np.save(save_path + "/recovered.npy", x_hat)
コード例 #18
0
ファイル: font2img.py プロジェクト: kse0202/neural-fonts-test
args = easydict.EasyDict({
    "src_font":
    'C:/Users/1-16/python/neural-fonts-master/fonts/NanumGothic.ttf',
    "dst_font":
    'C:/Users/1-16/python/neural-fonts-master/fonts/NanumGothic.ttf',
    "filter":
    0,
    "shuffle":
    0,
    "char_size":
    80,
    "canvas_size":
    128,
    "x_offset":
    27,
    "y_offset":
    16,
    "sample_count":
    1000,
    "sample_dir":
    'C:/Users/1-16/python/neural-fonts-master/sample_dir',
    "label":
    0,
    "fixed_sample":
    0,
    "all_sample":
    0,
    "handwriting_dir":
    'C:/Users/1-16/python/neural-fonts-master/dst_dir'
})
if __name__ == "__main__":
コード例 #19
0
                                 hypothesis,
                                 weights=(0.33, 0.33, 0.33, 0))
            bleu_4 = corpus_bleu(references, hypothesis)

            print('BLEU-1: {}, BLEU-2: {}, BLEU-3: {}, BLEU-4: {}'.format(
                bleu_1, bleu_2, bleu_3, bleu_4))
            print("val_loss is: {}".format(teller_val_losses.avg))

            # Plot the BLEU Score and Validation Loss
            if visualizer is not None:
                visualizer.plot("BLEU_4", "val", iteration, bleu_4 * 100)
                visualizer.plot("BLEU_3", "val", iteration, bleu_3 * 100)
                visualizer.plot("BLEU_2", "val", iteration, bleu_2 * 100)
                visualizer.plot("BLEU_1", "val", iteration, bleu_1 * 100)
                print(type(teller_val_losses.avg.item()))
                visualizer.plot("Teller Decoder Val Loss",
                                'val',
                                iteration,
                                teller_val_losses.avg.item(),
                                total=self.iterations)
            self.model.predict(batch)


if __name__ == "__main__":
    config_file = "example_args/gandraw_teller_args.json"
    with open(config_file, 'r') as f:
        cfg = json.load(f)

    cfg = easydict.EasyDict(cfg)
    tester = TellerTester(cfg, test_eval=True)
    tester.test()
コード例 #20
0
ファイル: naf.py プロジェクト: scturtle/rl
def main():
    num_episodes = 300
    replay_memory_init_size = 500
    replay_memory_size = 50000
    replay_step_limit = 100
    batch_size = 128
    discount_factor = 0.99
    max_step = 200
    replay_repeat = 5
    update_target_per_steps = 100
    tau = 0.0003
    display = True

    Transition = namedtuple("Transition",
                            "state action reward next_state done")
    replay_memory = ReplayQueue(replay_memory_size)

    env = gym.make('Pendulum-v0')
    status_dim = env.observation_space.shape
    action_dim = env.action_space.shape
    if display:
        env.reset()
        env.render()

    naf_cfg = easydict.EasyDict(
        dict(status_dim=status_dim,
             action_dim=action_dim,
             hidden_dim=200,
             learning_rate=0.0003,
             l2_reg=0.0003,
             is_training=True))

    tf.reset_default_graph()
    sess = tf.InteractiveSession()

    date_str = datetime.now().strftime("%m%d_%H%M%S")
    summaries_dir = os.path.abspath("./summary/naf/" + date_str)

    q_estimator = NAF(naf_cfg, scope="q", summaries_dir=summaries_dir)
    target_estimator = NAF(naf_cfg, sess=sess, scope="target")

    sess.run(tf.global_variables_initializer())
    copy_params(sess, q_estimator, target_estimator, 1)

    saver = tf.train.Saver()
    checkpoint_dir = os.path.abspath("./checkpoints/naf/" + date_str)
    if not os.path.exists(checkpoint_dir):
        os.makedirs(checkpoint_dir)
    checkpoint_path = os.path.join(checkpoint_dir, "model")
    # saver.restore(sess, "checkpoints/naf/XXXX_XXXXXX/model")

    state = env.reset()
    k = 0
    for _ in range(replay_memory_init_size):
        action = env.action_space.sample()
        next_state, reward, done, _ = env.step(action)
        replay_memory.push(Transition(state, action, reward, next_state, done))
        if done or k >= replay_step_limit:
            k = 0
            state = env.reset()
        else:
            k += 1
            state = next_state

    for i_episode in range(1, num_episodes + 1):

        state = env.reset()
        episode_reward = 0
        if display:
            env.render()

        for i_step in itertools.count(1):
            action = q_estimator.predict(np.expand_dims(state, 0))[0]
            action += np.random.randn(action_dim[0]) / i_episode  # TODO
            action = np.minimum(env.action_space.high,
                                np.maximum(env.action_space.low, action))
            next_state, reward, done, _ = env.step(action)
            if display:
                env.render()
            episode_reward += reward
            replay_memory.push(
                Transition(state, action, reward, next_state, done))

            for _ in range(replay_repeat):
                samples = replay_memory.sample(batch_size)
                states_batch, action_batch, reward_batch, next_states_batch, done_batch =\
                    map(np.array, zip(*samples))
                target_values = target_estimator.values(next_states_batch)
                target_batch = reward_batch + (
                    1 - done_batch) * discount_factor * target_values
                loss = q_estimator.update(states_batch, action_batch,
                                          target_batch)

            print(
                "\rEpisode {}/{} step {} action {:.3f} reward {:.3f} loss {:.6f}"
                .format(i_episode, num_episodes, i_step, action[0],
                        episode_reward, loss),
                end='',
                flush=True)

            if i_step % update_target_per_steps == 0:
                copy_params(sess, q_estimator, target_estimator,
                            tau * update_target_per_steps)

            if done or i_step >= max_step:
                print()
                break
            state = next_state

        if i_episode % 50 == 0:
            print('Saved!')
            saver.save(sess, checkpoint_path)

        if q_estimator.summary_writer:
            episode_summary = tf.Summary()
            episode_summary.value.add(simple_value=episode_reward,
                                      node_name="episode_reward",
                                      tag="episode_reward")
            q_estimator.summary_writer.add_summary(episode_summary, i_episode)
            q_estimator.summary_writer.flush()

    saver.save(sess, checkpoint_path)
    sess.close()
コード例 #21
0
    def __init__(self, opt):
        super(LocalizationLayer, self).__init__()
        self.opt = easydict.EasyDict()
        self.opt.input_dim = utils.getopt(opt, 'input_dim')
        self.opt.output_size = utils.getopt(opt, 'output_size')

        # list x0, y0, sx, sy
        self.opt.field_centers = utils.getopt(opt, 'field_centers')

        self.opt.mid_box_reg_weight = utils.getopt(opt, 'mid_box_reg_weight')
        self.opt.mid_objectness_weight = utils.getopt(opt,
                                                      'mid_objectness_weight')

        self.opt.rpn_filter_size = utils.getopt(opt, 'rpn_filter_size', 3)
        self.opt.rpn_num_filters = utils.getopt(opt, 'rpn_num_filters', 256)
        self.opt.zero_box_conv = utils.getopt(opt, 'zero_box_conv', True)
        self.opt.std = utils.getopt(opt, 'std', 0.01)
        self.opt.anchor_scale = utils.getopt(opt, 'anchor_scale', 1.0)
        self.opt.anchors = utils.getopt(opt, 'anchors', 'original')

        self.opt.sampler_batch_size = utils.getopt(opt, 'sampler_batch_size',
                                                   256)
        self.opt.sampler_high_thresh = utils.getopt(opt, 'sampler_high_thresh',
                                                    0.75)
        self.opt.sampler_low_thresh = utils.getopt(opt, 'sampler_low_thresh',
                                                   0.4)
        self.opt.train_remove_outbounds_boxes = utils.getopt(
            opt, 'train_remove_outbounds_boxes', 1)
        self.opt.box_reg_decay = utils.getopt(opt, 'box_reg_decay', 5e-5)
        self.opt.tunable_anchors = utils.getopt(opt, 'tunable_anchors', False)
        self.opt.backprop_rpn_anchors = utils.getopt(opt,
                                                     'backprop_rpn_anchors',
                                                     False)
        self.box_loss = utils.getopt(opt, 'wordness_loss')
        self.opt.contrastive_loss = utils.getopt(opt, 'contrastive_loss')

        self.stats = easydict.EasyDict()
        self.stats.losses = easydict.EasyDict()
        self.stats.vars = easydict.EasyDict()
        self.dtp_train = utils.getopt(opt, 'dtp_train', False)

        if self.dtp_train:
            self.opt.sampler_batch_size /= 2

        sampler_opt = {
            'batch_size': self.opt.sampler_batch_size,
            'low_thresh': self.opt.sampler_low_thresh,
            'high_thresh': self.opt.sampler_high_thresh,
            'contrastive_loss': self.opt.contrastive_loss
        }

        debug_sampler = utils.getopt(opt, 'box_sampler', False)
        if debug_sampler != False:
            sampler_opt['box_sampler'] = debug_sampler

        self.rpn = RPN(self.opt)
        self.box_sampler_helper = BoxSamplerHelper(sampler_opt)
        self.roi_pooling = BilinearRoiPooling(self.opt.output_size[0],
                                              self.opt.output_size[1])
        self.invert_box_transform = InvertBoxTransform()

        # Construct criterions
        if self.opt.backprop_rpn_anchors:
            self.box_reg_loss = BoxRegressionCriterion(
                self.opt.mid_box_reg_weight)
        else:
            self.box_reg_loss = nn.SmoothL1Loss()  # for RPN box regression

        self.box_scoring_loss = nn.CrossEntropyLoss()

        self.image_height = None
        self.image_width = None
        self._called_forward_size = False
        self._called_backward_size = False
コード例 #22
0
 def graph_forward(self, opt, batch, training=False):
     var = easydict.EasyDict()
     [var.image, var.points_GT] = [v.to(opt.device) for v in batch]
     var.points_pred = self.network.forward(opt, var.image)
     return var
コード例 #23
0
def get_config(config):
    with open(config, 'r') as stream:
        return easydict.EasyDict(yaml.load(stream))
コード例 #24
0
def main():
    os.environ["MXNET_CUDNN_AUTOTUNE_DEFAULT"] = "0"
    os.environ["MXNET_GPU_MEM_POOL_TYPE"] = "Round"
    args = parse_args()
    setattr(mobula.config, "NVCC", args.nvcc)

    config = easydict.EasyDict()
    config.gpus = [int(x) for x in str(args.gpus).split(',')]
    config.dataset = easydict.EasyDict()
    config.dataset.NUM_CLASSES = args.num_classes
    config.dataset.dataset_type = args.dataset_type
    config.dataset.dataset_path = args.dataset_root
    config.retinanet = easydict.EasyDict()
    config.retinanet.network = easydict.EasyDict()
    config.retinanet.network.FPN_STRIDES = [8, 16, 32, 64, 128]
    config.retinanet.network.BASE_SIZES = [(32, 32), (64, 64), (128, 128),
                                           (256, 256), (512, 512)]
    config.retinanet.network.SCALES = [2**0, 2**(1 / 2), 2**(2 / 3)]
    config.retinanet.network.RATIOS = [1 / 2, 1, 2]
    config.retinanet.network.bbox_norm_coef = [0.1, 0.1, 0.2, 0.2]

    config.TRAIN = easydict.EasyDict()
    config.TRAIN.batch_size = 2 * len(config.gpus)
    config.TRAIN.lr = 0.01 * config.TRAIN.batch_size / 16
    config.TRAIN.warmup_lr = config.TRAIN.lr
    config.TRAIN.warmup_step = 1000
    config.TRAIN.wd = 1e-4
    config.TRAIN.momentum = .9
    config.TRAIN.log_path = "output/{}/RetinaNet-hflip".format(
        config.dataset.dataset_type, config.TRAIN.lr)
    config.TRAIN.log_interval = 100
    config.TRAIN.cls_focal_loss_alpha = .25
    config.TRAIN.cls_focal_loss_gamma = 2
    config.TRAIN.image_short_size = 600
    config.TRAIN.image_max_long_size = 1333
    config.TRAIN.aspect_grouping = True
    # if aspect_grouping is set to False, all images will be pad to (PAD_H, PAD_W)
    config.TRAIN.PAD_H = 768
    config.TRAIN.PAD_W = 768
    config.TRAIN.begin_epoch = 0
    config.TRAIN.end_epoch = 28
    config.TRAIN.lr_step = [6, 8]
    config.TRAIN.FLIP = True
    config.TRAIN.resume = None
    config.TRAIN.trainer_resume = None

    config.network = easydict.EasyDict()
    config.network.FIXED_PARAMS = []
    config.network.sync_bn = True
    config.network.use_global_stats = False if config.network.sync_bn else True
    config.network.merge_backbone_bn = False

    config.val = easydict.EasyDict()
    if args.demo:
        config.val.params_file = args.demo_params
        config.val.viz = args.viz
        demo_net(config)
    else:
        os.makedirs(config.TRAIN.log_path, exist_ok=True)
        log_init(filename=os.path.join(config.TRAIN.log_path,
                                       "train_{}.log".format(time.time())))
        msg = pprint.pformat(config)
        logging.info(msg)
        train_net(config)
コード例 #25
0
"""

"""

import easydict

config = easydict.EasyDict()

config.tv_weight = 10
config.generator_g_lr = 1e-4
config.generator_f_lr = 1e-4
config.discriminator_c_lr = 1e-4
config.discriminator_t_lr = 1e-4
config.model_path = ''

コード例 #26
0
ファイル: resultControl.py プロジェクト: yobee0304/Hanbaljjak
def resultControl():
    if(request.method == 'POST'):

        # 클라이언트에서 sentenceId & wav file 받아옴
        wav = request.files['receiveFile']
        filename = request.form['fileName']
        sentenceId = request.form['sentenceId']

        # upload 디렉터리에 저장
        wav.save(FILE_DIRECTORY + secure_filename(wav.filename))

        ##### upload 디렉터리에 있는 파일을 STT로 변한

        # 임시 path
        args = easydict.EasyDict({"local_file_path": "./uploadFile/"+filename})

        # TODO Credential Error
        # print(sample_recognize(args.local_file_path))


        # sentenceId를 통해 DB에서 표준 발음 텍스트 가져옴
        Pick_sentence = db_session.query(Sentence).filter(Sentence.sentenceId == sentenceId).first()

        receiveSTTData = sample_recognize(args.local_file_path)
        receiveData = similaritySentence(receiveSTTData, Pick_sentence.standard)
        #receiveData = "날시가 참 말따"
        print("STT result : ", receiveData)

        # print(Pick_sentence)
        ##### 분석 알고리즘

        hannanum = Hannanum()

        StandardSentence = Pick_sentence.standard
        sentenceData = Pick_sentence.sentenceData

        # 공백 인덱스 리스트 생성
        # 공백 개수는 일치
        userBlankList = [i for i, value in enumerate(receiveData) if value == " "]
        standardBlankList = [i for i, value in enumerate(StandardSentence) if value == " "]
        # print(BlankList)

        # 문자열 길이가 다르거나 공백 개수가 다르면
        # 재시도 요청
        if (len(receiveData) != len(StandardSentence) or len(userBlankList) != len(standardBlankList)):
            os.remove("./uploadFile/"+filename)

            return jsonify(
                status="failure",
                resultData=receiveData,
                errorMessage="repeat",
            )

        # 공백 제거
        UserSentence = receiveData.replace(" ", "")
        StandardSentence = StandardSentence.replace(" ", "")
        sentenceData = sentenceData.replace(" ", "")

        # print(UserSentence)
        # print(StandardSentence)

        Total_pho = 0           # 총 음소 개수
        Wrong_total_pho = 0     # 틀린 음소 개수
        Wrong_word_index_list = []   # 틀린 글자 데이터가 들어있는 리스트
        Wrong_word_list = []         # 틀린 단어 데이터가 들어있는 리스트
        Wrong_pho_dict = {'u' : {},
                          'm' : {},     # 틀린 음소가 저장되는 딕셔너리
                          'b' : {}}     # u : 자음, m : 모음, b : 받침


        for index, standard in enumerate(StandardSentence):
            StandPho = phonemeConvert(standard)

            # 글자가 일치하는 경우
            if(UserSentence[index] == standard):
                if(StandPho[2] == ' '):
                    Total_pho += 2
                else:
                    Total_pho += 3
            # 글자가 일치하지 않는 경우
            # 음소 분해
            else:
                Wrong_word_index_list.append(index)
                UserPho = phonemeConvert(UserSentence[index])
                SentencePho = phonemeConvert(sentenceData[index])

                if(UserPho[2] == ' ' and StandPho[2] == ' '):
                    Total_pho += 2
                else:
                    Total_pho += 3

                    if(UserPho[2] != StandPho[2]):
                        Wrong_total_pho += 1

                        if StandPho[2] != ' ':
                            if StandPho[2] in Wrong_pho_dict['b']:
                                Wrong_pho_dict['b'][SentencePho[2]] += 1
                            else:
                                Wrong_pho_dict['b'][SentencePho[2]] = 1

                if (UserPho[0] != StandPho[0]):
                    Wrong_total_pho += 1
                    if StandPho[0] in Wrong_pho_dict['u']:
                        Wrong_pho_dict['u'][SentencePho[0]] += 1
                    else:
                        Wrong_pho_dict['u'][SentencePho[0]] = 1

                if (UserPho[1] != StandPho[1]):
                    Wrong_total_pho += 1
                    if StandPho[1] in Wrong_pho_dict['m']:
                        Wrong_pho_dict['m'][SentencePho[1]] += 1
                    else:
                        Wrong_pho_dict['m'][SentencePho[1]] = 1

        # print(Wrong_pho_dict)


        ######### 틀린 음소 record 테이블에 count 올림 -> TEST SUCCESS
        for type in Wrong_pho_dict:
            for pho in Wrong_pho_dict[type]:
                # print(pho)
                updateData = db_session.query(Record).filter(Record.recordType == type)\
                                                    .filter(Record.recordData == pho).first()
                # print(updateData.type, updateData.recordData)
                updateData.count += Wrong_pho_dict[type][pho]
                db_session.commit()



        # 일치율
        Correct_rate = round(1 - (Wrong_total_pho / Total_pho), 4)
        """
        # 일치율 100%인 경우
        if Correct_rate == 1:
            os.remove("./uploadFile/" + filename)

            return jsonify(
                status="perfect",
                resultData=receiveData,
                score=Correct_rate,
            )
        """
        # print(Wrong_word_list)

        # 변경 후
        # print(Wrong_word_index_list)
        sentenceData_split = Pick_sentence.sentenceData.split()
        # print(sentenceData_split)
        # 틀린 인덱스가 포함된 단어 선택
        word_start_point = 0
        for sentence_word in sentenceData_split:
            word_end_point = word_start_point + len(sentence_word)-1

            # print(word_start_point, word_end_point)

            for wrong_index in Wrong_word_index_list:
                if word_start_point <= wrong_index and word_end_point >= wrong_index:
                    word_to_pos = hannanum.pos(sentence_word)

                    # print(word_to_pos)
                    wrong_word_pho_list = phonemeConvert(sentenceData[wrong_index])
                    # print(wrong_word_pho_list)
                    for pos in word_to_pos:
                        #TODO 틀린 단어에 N이나 P가 여러개 들어있으면??
                        if pos[1] == 'N' or pos[1] == 'P':
                            for pos_word in pos[0]:
                                pos_word_pho_list = phonemeConvert(pos_word)
                                # print(pos_word_pho_list)
                                if wrong_word_pho_list[0] == pos_word_pho_list[0]:
                                    Wrong_word_list.append(pos)

                    break

            word_start_point += len(sentence_word)

        print(Wrong_word_list)

        # 틀린 글자 인덱스를 원래 문장을 기준으로 변경
        for i in userBlankList:
            for index, j in enumerate(Wrong_word_index_list):
                if(j >= i):
                    Wrong_word_index_list[index] += 1

        # print(Wrong_word_index)

        ######## result 테이블에 결과값 저장 -> TEST SUCCESS
        resultData = Result(stid=sentenceId, rsdata=receiveData, score=Correct_rate)
        db_session.add(resultData)
        db_session.commit()


        # 일치율 100%인 경우
        if Correct_rate == 1:
            os.remove("./uploadFile/" + filename)

            return jsonify(
                status="perfect",
                resultData=receiveData,
                score=Correct_rate,
            )

        ######## 가장 많이 틀린 단어에 대한 추천 문장 1개

        recommend_OtoD = dict(sentenceId=-1, sentenceData="", standard="")
        recommend_word = ""

        # 틀린 단어 리스트에 단어가 존재할 경우
        if Wrong_word_list:
            random.shuffle(Wrong_word_list)

            for random_select_word in Wrong_word_list:
                Word_query = db_session.query(Word).filter(Word.wordData == random_select_word[0])\
                    .filter(Word.wordType == random_select_word[1]).filter(Word.sentenceId != sentenceId)
                Word_entry = [pq.sentenceId for pq in Word_query]

                if Word_entry:
                    recommend_word = random_select_word[0]
                    if random_select_word[1] == 'P':
                        recommend_word += '다'
                    random_select_setencdId = random.choice(Word_entry)
                    Recommend_sentence = db_session.query(Sentence).filter(Sentence.sentenceId == random_select_setencdId).first()
                    recommend_OtoD['sentenceId'] = Recommend_sentence.sentenceId
                    recommend_OtoD['sentenceData'] = Recommend_sentence.sentenceData
                    recommend_OtoD['standard'] = Recommend_sentence.standard
                    break

    os.remove("./uploadFile/" + filename)

    # response해줄 틀린 단어 리스트
    wordList = []

    for w in Wrong_word_list:
        if w[1] == "P":
            wordList.append(w[0]+"다")
        else:
            wordList.append(w[0])

    # 결과 데이터를 모두 json으로 묶음
    return jsonify(
        status = "success",
        score = Correct_rate,
        wordList = wordList,
        userBlank = userBlankList,
        standardBlank = standardBlankList,
        wrongIndex = Wrong_word_index_list,
        resultData = receiveData
    )
コード例 #27
0
ファイル: configs.py プロジェクト: maxpark/yolov4-tiny-keras
# -*- coding:utf-8 -*-
# author:平手友梨奈ii
# e-mail:[email protected]
# datetime:1993/12/01
# filename:configs.py
# software: PyCharm

import easydict

CONFIG = easydict.EasyDict()

# mAP evaluation
CONFIG.DETECT = easydict.EasyDict()

CONFIG.DETECT.SCORE = 0.3
CONFIG.DETECT.IOU = 0.5
CONFIG.DETECT.RESOLUTION = (416, 416)
CONFIG.DETECT.mAP_THRES = 0.5

# prediction
CONFIG.PREDICT = easydict.EasyDict()
CONFIG.PREDICT.WEIGHTS = 'logs/yolo4_tiny_weight.h5'
CONFIG.PREDICT.ANCHOR_PATH = 'model_data/yolo4_tiny_anchors.txt'
CONFIG.PREDICT.CLASS_PATH = 'model_data/coco_classes.txt'
CONFIG.PREDICT.SCORE = 0.2
CONFIG.PREDICT.IOU = 0.3
CONFIG.PREDICT.RESOLUTION = (416, 416)
CONFIG.PREDICT.MAX_BOXES = 40

# train
CONFIG.TRAIN = easydict.EasyDict()
コード例 #28
0
    import multiResData
    import PoseTools as pt
    import easydict
    res = pt.pickle_load(res_file)[0]
    ims,locs,info = multiResData.read_and_decode_without_session(db,5,())
    ims = np.array(ims)
    locs = np.array(locs)
    pred = res[-1][0]
    dd = np.sqrt(np.sum((res[-1][0]-res[-1][1])**2,axis=-1))

    gg = np.percentile(dd,[50,75,90,96],axis=0)
    pt.show_result_hist(ims[0,...],locs[0,...],gg)

    ndx = np.random.choice(ims.shape[0],6)
    pt.show_result(ims,ndx,locs,pred)
    conf = easydict.EasyDict()
    conf.adjust_contrast = True
    conf.clahe_grid_size = 20
    cims = pt.adjust_contrast(ims,conf)
    pt.show_result(cims,ndx,locs,pred)


## Original DLC and Leap Training
import run_apt_expts as rae
dtypes = ['alice','stephen']
for dd in dtypes:
    reload(rae)
    rae.setup(dd)
    rae.train_deepcut_orig()
    rae.train_leap_orig()
# In[ ]:

import argparse
from PIL import Image  #This may not be used
import os  ##This may not be used
import easydict

args = easydict.EasyDict({
    'type': 'gan',
    'lr': 0.0002,
    'l1_coef': 50,
    'l2_coef': 100,
    'cls': True,
    'save_path': './flowers_cls_test',
    'inference': True,
    'pre_trained_disc': 'checkpoints/flowers_cls/disc_190.pth',
    'pre_trained_gen': 'checkpoints/flowers_cls/gen_190.pth',
    'dataset': 'flowers',
    'split': 2,
    'batch_size': 64,
    'num_workers': 8,
    'epochs': 200
})

trainer = Trainer(type=args.type,
                  dataset=args.dataset,
                  split=args.split,
                  lr=args.lr,
                  save_path=args.save_path,
                  l1_coef=args.l1_coef,
                  l2_coef=args.l2_coef,
コード例 #30
0
    "wifi_funny_special": dummy_handler,
    "network_percent_data": dummy_handler,
    "battery_mean_usage": dummy_handler
}

user_achievement_handlers = {
    "count_based": count_based_badge,
    "procent_based": proc_based_badge,
    "time_based": time_based_badge,
    "wifi_security_special": wifi_security_special_badge,
    "wifi_funny_special": wifi_funny_special_badge,
    "network_percent_data": network_percent_data_badge,
    "battery_mean_usage": battery_mean_usage_badge
}

user_ranking_handlers = {
    "count_based": count_based_place,
    "procent_based": proc_based_place,
    "time_based": time_based_place,
    "wifi_security_special": dummy_handler,
    "wifi_funny_special": dummy_handler,
    "network_percent_data": dummy_handler,
    "battery_mean_usage": dummy_handler
}

handlers = easydict.EasyDict({})
handlers.ranking = ranking_handlers
handlers.user = {}
handlers.user.achievements = user_achievement_handlers
handlers.user.ranking = user_ranking_handlers