def saveParams(net, classes, epoch):

    log.i("EXPORTING MODEL PARAMS...", new_line=False)
    net_filename = cfg.MODEL_PATH + cfg.RUN_NAME + "_model_params_epoch_" + str(
        epoch) + ".pkl"
    if not os.path.exists(cfg.MODEL_PATH):
        os.makedirs(cfg.MODEL_PATH)
    with open(net_filename,
              'wb') as f:  # 'wb' instead 'w' for binary file for python3

        #We want to save the model params only and trained classes
        params = l.get_all_param_values(net)
        data = {
            'params': params,
            'classes': classes,
            'run_name': cfg.RUN_NAME,
            'epoch': epoch,
            'im_size': cfg.IM_SIZE,
            'im_dim': cfg.IM_DIM
        }
        pickle.dump(data, f,
                    -1)  # -1 specifies highest binary protocol for python3

    log.i("DONE!")

    return os.path.split(net_filename)[-1]
예제 #2
0
def parseTestSet():

    # Status
    log.i('PARSING TEST SET...', new_line=False)
    t = []

    wav_files = [
        os.path.join(cfg.TESTSET_PATH, f)
        for f in sorted(os.listdir(cfg.TESTSET_PATH))
        if os.path.splitext(f)[1] in ['.wav']
    ]

    # Parse files
    for f in wav_files:
        t.append((f, os.path.splitext(f)[0].split('_RN')[-1]))

    # Load class ids
    codes = []
    with open('metadata/labelset.txt', 'r') as lfile:
        for line in lfile.readlines():
            codes.append(line.replace('\r\n', '').replace('\n', ''))
    labels = []
    with open('metadata/labelset_latin.txt', 'r') as lfile:
        for line in lfile.readlines():
            labels.append(line.replace('\r\n', '').replace('\n', ''))

    # Status
    log.i(('Done!', len(t), 'TEST FILES'))

    return t, codes, labels
예제 #3
0
    def Process(handler, *args):
        try:
	    handler.ssid = handler.get_cookie(etc.cookie_ssid)
	    handler.ss_ua = handler.request.headers['User-Agent']
	    handler.ss_ver = handler.get_cookie(etc.cookie_ver)

	    log.i(handler.request.cookies)

	    if not handler.ssid:
	        log.w('[U-A:%s] no_ssid' % handler.ss_ua)
		res = {'idx': -1, 'ret': -1, 'msg': etc.err_op_fail, 'res': {}}
		handler.write(json.dumps(res))
		handler.finish()
		return
	    postData = handler.get_argument('postData', default=None)
	    if postData:
	        paramsJson = json.loads(postData)
	    handler.ss_idx = paramsJson['idx'] if postData else 0
	    handler.ss_params = paramsJson['params'] if postData else None
	    log.i('[RIP:%s][U-A:%s][ssid:%s][idx:%s][ver:%s][params:-]' % (
	        handler.request.headers['X-Real-Ip'],
		handler.ss_ua,
		handler.ssid,
		handler.ss_idx,
		handler.ss_ver))
	except Exception as e:
	    res = {'idx': -1, 'ret': -2, 'msg': etc.err_op_fail, 'res': {}}
            handler.write(json.dumps(res))
            handler.finish()
            return
	if handler.current_user is None:
	    log.w('[U-A:%s] signin_required' % handler.ss_ua)
	    res = {'idx': -1, 'ret': -3, 'msg': etc.err_signin_requied, 'res': {}}
	    handler.finish()
	    return
	if handler.current_user['role'] != 1:
	    log.w('[U-A:%s] store_required' % handler.ss_ua)
	    res = {'idx': -1, 'ret': -5, 'msg': '无商家身份', 'res': {}}
	    handler.write(json.dumps(res))
	    handler.finish()
	    return
        
	try:
	    request(handler, *args)
	except Exception as e:
	    log.exp(e)
	    res = {'idx': -1, 'ret': -4, 'msg': etc.err_500, 'res': {}}
	    handler.write(json.dumps(res))
	    handler.finish()
	    return

	try:
	    if handler.current_user != None:
	        handler.ss_store.replace(handler.ssid, handler.current_user)
        except Exception as e:
	    log.exp(e)
	    res = {'idx': -1, 'ret': -4, 'msg': etc.err_500, 'res': {}}
	    handler.write(json.dumps(res))
	    handler.finish()
	    return
예제 #4
0
def test_function(net, hasTargets=True, layer_index=-1):

    # We need the prediction function to calculate the validation accuracy
    # this way we can test the net during/after training
    # We need a version with targets and one without
    prediction = l.get_output(l.get_all_layers(net)[layer_index],
                              deterministic=True)

    log.i("COMPILING TEST FUNCTION...", new_line=False)
    start = time.time()
    if hasTargets:
        # Theano variable for the class targets
        targets = T.matrix('targets', dtype=theano.config.floatX)

        loss = loss_function(net, prediction, targets)
        accuracy = accuracy_function(net, prediction, targets)

        test_net = theano.function(
            [l.get_all_layers(net)[0].input_var, targets],
            [prediction, loss, accuracy],
            allow_input_downcast=True)

    else:
        test_net = theano.function([l.get_all_layers(net)[0].input_var],
                                   prediction,
                                   allow_input_downcast=True)

    log.i(("DONE! (", int(time.time() - start), "s )"))

    return test_net
예제 #5
0
def logout(handler):
    handler.clear_all_cookies()
    if handler.current_user:
        log.i('userid=%s , ssid=%s' % (handler.current_user['userid'], handler.ssid))
	handler.ss_store.delete(handler.ssid)
	one_login_store = RdsOneLoginStore()
	one_login_store.delete(handler.current_user['userid'])
    handler.current_user = None
def loadModel(filename):
    log.i(("IMPORTING MODEL...", filename.split(os.sep)[-1]), new_line=False)
    net_filename = cfg.MODEL_PATH + filename

    with open(net_filename, 'rb') as f:
        model = pickle.load(f)

    log.i("DONE!")

    return model
예제 #7
0
 def post(self):
     try:
         name = self.get_argument("name")
         password = self.get_argument("password")
         if not name or not password:
             self.render("login.html")
         if sql.user_base(name, password):
             self.redirect("/index/")
     except Exception, e:
         log.i(e)
예제 #8
0
def main(p_port):
    if p_port == 0:
        print 'port could not be set as 0'
	log.e('port could not be set as 0')
	exit(1)
    log.c('www listening on port : %s' % p_port)
    app = Application()
    app.listen(p_port)
    log.i(app.settings['template_path'])
    log.i(app.settings['static_path'])
    tornado.ioloop.IOLoop.instance().start()
def loadParams(net, params):

    log.i("IMPORTING MODEL PARAMS...", new_line=False)
    if cfg.LOAD_OUTPUT_LAYER:
        l.set_all_param_values(net, params)
    else:
        l.set_all_param_values(l.get_all_layers(net)[:-2], params[:-2])

    log.i("DONE!")

    return net
예제 #10
0
 def incubate(self, cmdline: Union[List[str], str]):
     if isinstance(cmdline, str):
         cmdline = cmdline.split(" ")
     process = subprocess.Popen(cmdline,
                                shell=self.__shell,
                                stdin=subprocess.DEVNULL,
                                stdout=subprocess.DEVNULL,
                                stderr=subprocess.DEVNULL)
     log.i(__name__, "Incubated process. PID={}".format(process.pid))
     with self.__lock:
         self.__processes_appended.append(process)
예제 #11
0
def parseDataset():

    CLASSES = [
        c for c in sorted(os.listdir(os.path.join(cfg.TRAINSET_PATH, 'train')))
    ]

    for c in CLASSES:
        log.i(c)
        index = []
        afiles = [
            f for f in sorted(
                os.listdir(os.path.join(cfg.TRAINSET_PATH, 'train', c)))
        ]
        max_specs = cfg.MAX_SPECS_PER_CLASS // len(afiles) + 1
        for i in range(len(afiles)):

            spec_cnt = 0
            try:
                print(i + 1, '/', len(afiles), c, afiles[i])
                specs, noise = getSpecs(
                    os.path.join(cfg.TRAINSET_PATH, 'train', c, afiles[i]))
                for s in range(len(specs)):
                    if np.isnan(noise[s]):
                        noise[s] = 0.0
                    if noise[s] >= cfg.SPEC_SIGNAL_THRESHOLD:
                        filepath = os.path.join(cfg.DATASET_PATH, c)
                        if not os.path.exists(filepath):
                            os.makedirs(filepath)
                    elif noise[s] <= cfg.NOISE_THRESHOLD:
                        if RANDOM.choice([True, False], p=[0.60, 0.40]):
                            filepath = os.path.join(cfg.NOISE_PATH)
                            if not os.path.exists(filepath):
                                os.makedirs(filepath)
                        else:
                            filepath = None

                    else:
                        filepath = None
                    if filepath:
                        filename = str(
                            int(noise[s] *
                                10000)).zfill(4) + '_' + afiles[i].split(
                                    '.')[0] + '_' + str(s).zfill(3)
                        # Write to HDD
                        cv2.imwrite(os.path.join(filepath, filename + '.png'),
                                    specs[s] * 255.0)
                    if spec_cnt >= max_specs:
                        break
            except:
                log.e((spec_cnt, 'specs', 'ERROR DURING SPEC EXTRACT'))
                continue
예제 #12
0
        def _generate_res(self, ret_num, msg, step_name='', res=None):
	    if res is None:
	        res = {}
	    
	    log.w(step_name)
	    ret = {
	        'idx': self.ss_idx,
		'ret': ret_num,
		'res': res,
		'msg': msg,
	    }
	    log.i(step_name + 'finish')
	    self.write(json.dumps(ret))
	    self.finish()
예제 #13
0
def login(handler, user_base, expires):
    if True:
        user_detail = data_user_detail_mysql\
	    .get_user_detail_by_userid(user_base['userid'])
	user_brief = data_user_brief_mysql\
	    .get_user_brief_by_userid(user_base['userid'])
	user_other = data_user_other_mysql\
	    .get_user_other_by_userid(user_base['userid'])
	orig_dict = {
	    'name': '',
	    'sex': '',
            'sexot': '',
	    'love': '',
            'horo': '',
	    'intro': '',
	    'imglink': '',
	    'music': '',
	    'hobby': '',
	}
	handler.current_user = {
	    'ssid': handler.ssid,
	    'userid': user_base['userid'],
	    'phone': user_base['phone'],
	}
	log.i('before update(orig_dict)=========%s=========' % handler.current_user)

	handler.current_user.update(orig_dict)
	if user_detail:
	    handler.current_user.update(user_detail)

	if user_brief:
	    handler.current_user.update(user_brief)
        
	log.i('after update(brief)=========%s=========' % handler.current_user)

    one_login_store = RdsOneLoginStore()
    old_ssid = one_login_store.get(user_base['userid'])

    log.i('refresh ssid in mc old_ssid=%s handler.ssid=%s' % (old_ssid, handler.ssid))

    one_login_store.set(user_base['userid'], handler.ssid)

    if handler.ssid != old_ssid:
        log.i('delete old ssid if handler.ssid is not old ssid')
	handler.ss_store.delete(old_ssid)

    domain = util.get_domain_from_host(handler.request.host)
    handler.ssid_hmac = generate_hmac(handler.ssid)
    handler.set_secure_cookie(etc.cookie_check, handler.ssid, domain=domain,
                                  expires=expires)

    handler.set_secure_cookie(etc.cookie_verify, handler.ssid_hmac,
                                  domain=domain, expires=expires)
    handler.ss_store.set(handler.ssid, handler.current_user)      
예제 #14
0
def saveModel(net, classes, epoch):
    log.i("EXPORTING MODEL...", new_line=False)
    net_filename = cfg.MODEL_PATH + cfg.RUN_NAME + "_model_epoch_" + str(
        epoch) + ".pkl"
    if not os.path.exists(cfg.MODEL_PATH):
        os.makedirs(cfg.MODEL_PATH)
    with open(net_filename, 'w') as f:

        #We want to save the model architecture with all params and trained classes
        data = {
            'net': net,
            'classes': classes,
            'run_name': cfg.RUN_NAME,
            'epoch': epoch,
            'im_size': cfg.IM_SIZE,
            'im_dim': cfg.IM_DIM
        }
        pickle.dump(data, f)

    log.i("DONE!")

    return os.path.split(net_filename)[-1]
예제 #15
0
    def Process(handler, *args):
        try:
	    handler.ssid = handler.get_cookie(etc.cookie_ssid)
	    handler.ss_ua = handler.request.headers['User-Agent']
	    handler.ss_ver = handler.get_cookie(etc.cookie_ver)
	    if not handler.ssid:
	        log.w('[U-A:%s] no_ssid' % handler.ss_ua)
		res = {'idx': -1, 'ret': -1, 'msg': etc.err_op_fail, 'res': {}}
		handler.write(json.dumps(res))
		handler.finish()
		return
            postData = handler.get_argument('postData', default=None)
	    if postData:
	        paramsJson = json.loads(postData)
	    handler.ss_idx = paramsJson['idx'] if postData else 0
	    handler.ss_params = paramsJson['params'] if postData else None
	    log.i('[RIP:%s][U-A:%s][idx:%s][ver:%s][params:-]' % (
	        handler.request.handers['X-Real-Ip'],
		handler.ss_ua,
		handler.ss_ver))
	    log.i('[ssid:%s] [check:%s] [verify:%s]' % (
	        handler.ssid,
		handler.get_secure_cookie(etc.cookie_check),
		handler.get_secure_cookie(etc.cookie_verify)))
	except Exception as e:
	    log.exp(e)
            res = {'idx': -1, 'ret': -2, 'msg': etc.err_op_fail, 'res' {}}
            handler.write(json.dumps(res))
	    handler.finish()
	    return
	try:
	    request(handler, *args)
	except Exception as e:
	    log.exp(e)
	    res = {'idx': -1, 'ret': -4, 'msg': etc.err_500, 'res': {}}
	    handler.write(json.dumps(res))
            handler.finish()
	    return
예제 #16
0
def parseDataset():

    # Random Seed
    random = cfg.getRandomState()

    # We use subfolders as class labels
    classes = [folder for folder in sorted(os.listdir(cfg.DATASET_PATH)) if folder in cfg.CLASS_WHITELIST or len(cfg.CLASS_WHITELIST) == 0]
    if not cfg.SORT_CLASSES_ALPHABETICALLY:
        classes = shuffle(classes, random_state=random)
    classes = classes[:cfg.MAX_CLASSES]

    # Now we enlist all image paths for each class
    images = []
    tclasses = []
    sample_count = {}
    for c in classes:
        c_images = [os.path.join(cfg.DATASET_PATH, c, path) for path in shuffle(os.listdir(os.path.join(cfg.DATASET_PATH, c)), random_state=random) if isValidClass(c, path)][:cfg.MAX_SAMPLES_PER_CLASS]
        
        sample_count[c] = len(c_images)
        images += c_images
        
        # Do we want to correct class imbalance?
        # This will affect validation scores as we use some samples in TRAIN and VAL
        while sample_count[c] < cfg.MIN_SAMPLES_PER_CLASS:
            images += [c_images[random.randint(0, len(c_images))]]
            sample_count[c] += 1

    # Add labels to image paths
    for i in range(len(images)):
        path = images[i]
        label = images[i].split(os.sep)[-2]
        images[i] = (path, label)

    # Shuffle image paths
    images = shuffle(images, random_state=random)

    # Validation split
    vsplit = int(len(images) * cfg.VAL_SPLIT)
    train = images[:-vsplit]
    val = images[-vsplit:]

    # Show some stats
    log.i(("CLASSES:", len(classes)))
    log.i(( "CLASS LABELS:", sorted(sample_count.items(), key=operator.itemgetter(1))))
    log.i(("TRAINING IMAGES:", len(train)))
    log.i(("VALIDATION IMAGES:", len(val)))

    return classes, train, val
예제 #17
0
def train_function(net):

    # We use dynamic learning rates which change after some epochs
    lr_dynamic = T.scalar(name='learning_rate')

    # Theano variable for the class targets
    targets = T.matrix('targets', dtype=theano.config.floatX)

    # Get the network output
    prediction = l.get_output(net)

    # The theano train functions takes images and class targets as input
    log.i("COMPILING TRAIN FUNCTION...", new_line=False)
    start = time.time()
    loss = loss_function(net, prediction, targets)
    updates = net_updates(net, loss, lr_dynamic)
    train_net = theano.function(
        [l.get_all_layers(net)[0].input_var, targets, lr_dynamic],
        loss,
        updates=updates,
        allow_input_downcast=True)
    log.i(("DONE! (", int(time.time() - start), "s )"))

    return train_net
예제 #18
0
    def get_current_user(self):
        try:
	    log.i('---------start get_current_user-------------------')
	    cookie_check = self.get_secure_cookie(etc.cookie_check)
	    cookie_verify = self.get_secure_cookie(etc.cookie_verify)
	    if not cookie_check or not cookie_verify:
	        log.i('no cookie check or verify')
		self.clear_cookie(etc.cookie_check)
		self.clear_cookie(etc.cookie_verify)
		return None
	    check_verify = generate_hmac(cookie_check)
	    if cookie_verify != check_verify:
	        log.w("evil session : %s %s" % (cookie_check, cookie_verify))
		self.clear_cookie(etc.cookie_check)
		self.clear_cookie(etc.cookie_verify)
		return None
	    old_current_user = self.ss_store.get(cookie_check)
	    if old_current_user is None:
	        log.i("session expired")
		self.clear_cookie(etc.cookie_check)
		self.clear_cookie(etc.cookie_verify)
		return None
	    
	    log.i("---------self.current_user=%s -------------" % old_current_user)
	    return old_current_user
	except Exception as e:
	    log.exp(e)
	    self.clear_cookie(etc.cookie_check)
	    self.clear_cookie(etc.cookie_verify)
	    return None

        def _generate_res(self, ret_num, msg, step_name='', res=None):
	    if res is None:
	        res = {}
	    
	    log.w(step_name)
	    ret = {
	        'idx': self.ss_idx,
		'ret': ret_num,
		'res': res,
		'msg': msg,
	    }
	    log.i(step_name + 'finish')
	    self.write(json.dumps(ret))
	    self.finish()
예제 #19
0
def parseTestSet():

    # Random Seed
    random = cfg.getRandomState()

    # Status
    log.i('PARSING TEST SET...', new_line=False)
    TEST = []

    # List of test files
    fnames = []
    for path, dirs, files in os.walk(cfg.TESTSET_PATH):
        if path.split(os.sep)[-1] in cfg.CLASSES:
            scnt = 0
            for f in files:
                fnames.append(os.path.join(path, f))
                scnt += 1
                if scnt >= cfg.MAX_TEST_SAMPLES_PER_CLASS and cfg.MAX_TEST_SAMPLES_PER_CLASS > 0:
                    break
    fnames = sorted(shuffle(fnames, random_state=random)[:cfg.MAX_TEST_FILES])

    # Get ground truth from metadata
    for f in fnames:

        # Metadata path
        m_path = os.path.join(cfg.METADATA_PATH,
                              f.split(os.sep)[-1].split('.')[0] + '.json')

        # Load JSON
        with open(m_path) as jfile:
            data = json.load(jfile)

        # Get Species (+ background species)
        # Only species present in the trained classes are relevant for the metric
        # Still, we are adding anything we have right now and sort it out later
        if cfg.TEST_WITH_BG_SPECIES:
            bg = data['background']
        else:
            bg = []
        species = [data['sci-name']] + bg

        # Add data to test set
        TEST.append((f, species))

    # Status
    log.i('DONE!')
    log.i(('TEST FILES:', len(TEST)))

    return TEST
예제 #20
0
def runTest(SNAPSHOTS, TEST):

    # Do we have more than one snapshot?
    if not isinstance(SNAPSHOTS, (list, tuple)):
        SNAPSHOTS = [SNAPSHOTS]

    # Load snapshots
    test_functions = []
    for s in SNAPSHOTS:

        # Settings
        NET = s['net']
        cfg.CLASSES = s['classes']
        cfg.IM_DIM = s['im_dim']
        cfg.IM_SIZE = s['im_size']

        # Compile test function
        test_net = birdnet.test_function(NET, hasTargets=False, layer_index=-1)
        test_functions.append(test_net)

    # Status
    log.i('START TESTING...')

    # Make predictions
    submission = ''
    for spec_batch, mediaid, timestamp, filename in bg.threadedGenerator(
            getSpecBatches(TEST)):

        try:

            # Prediction
            prediction_batch = []
            for test_func in test_functions:
                if len(prediction_batch) == 0:
                    prediction_batch = test_func(spec_batch)
                else:
                    prediction_batch += test_func(spec_batch)
            prediction_batch /= len(test_functions)

            # Eliminate the scores for 'Noise'
            if 'Noise' in cfg.CLASSES:
                prediction_batch[:, cfg.CLASSES.index('Noise')] = np.min(
                    prediction_batch)

            # Prediction pooling
            p_pool = test.predictionPooling(prediction_batch)

            # Get class labels
            p_labels = {}
            for i in range(p_pool.shape[0]):
                p_labels[cfg.CLASSES[i]] = p_pool[i]

            # Sort by score
            p_sorted = sorted(p_labels.items(),
                              key=operator.itemgetter(1),
                              reverse=True)

            # Add scores to submission
            for i in range(min(100, len(p_sorted))):
                if getClassId(p_sorted[i][0]):
                    submission += mediaid + ';' + timestamp + ';' + getClassId(
                        p_sorted[i][0]) + ';' + str(p_sorted[i][1]) + '\n'

            # Show sample stats
            log.i((filename, timestamp), new_line=False)
            log.i(('TOP PREDICTION:', p_sorted[0][0],
                   int(p_sorted[0][1] * 1000) / 10.0, '%'),
                  new_line=True)

        except KeyboardInterrupt:
            cfg.DO_BREAK = True
            break

    # Status
    log.i('DONE TESTING!')

    return submission
예제 #21
0
def build_baseline_model():

    log.i('BUILDING BASELINE MODEL...')

    # Random Seed
    lasagne_random.set_rng(cfg.getRandomState())

    # Input layer for images
    net = l.InputLayer((None, cfg.IM_DIM, cfg.IM_SIZE[1], cfg.IM_SIZE[0]))

    # Stride size (as an alternative to max pooling)
    if cfg.MAX_POOLING:
        s = 1
    else:
        s = 2

    # Convolutinal layer groups
    for i in range(len(cfg.FILTERS)):

        # 3x3 Convolution + Stride
        net = batch_norm(
            l.Conv2DLayer(net,
                          num_filters=cfg.FILTERS[i],
                          filter_size=cfg.KERNEL_SIZES[i],
                          num_groups=cfg.NUM_OF_GROUPS[i],
                          pad='same',
                          stride=s,
                          W=initialization(cfg.NONLINEARITY),
                          nonlinearity=nonlinearity(cfg.NONLINEARITY)))

        # Pooling layer
        if cfg.MAX_POOLING:
            net = l.MaxPool2DLayer(net, pool_size=2)

        # Dropout Layer (we support different types of dropout)
        if cfg.DROPOUT_TYPE == 'channels' and cfg.DROPOUT > 0.0:
            net = l.dropout_channels(net, p=cfg.DROPOUT)
        elif cfg.DROPOUT_TYPE == 'location' and cfg.DROPOUT > 0.0:
            net = l.dropout_location(net, p=cfg.DROPOUT)
        elif cfg.DROPOUT > 0.0:
            net = l.DropoutLayer(net, p=cfg.DROPOUT)

        log.i(('\tGROUP', i + 1, 'OUT SHAPE:', l.get_output_shape(net)))

    # Final 1x1 Convolution
    net = batch_norm(
        l.Conv2DLayer(net,
                      num_filters=cfg.FILTERS[i] * 2,
                      filter_size=1,
                      W=initialization('identity'),
                      nonlinearity=nonlinearity('identity')))

    log.i(('\tFINAL CONV OUT SHAPE:', l.get_output_shape(net)))

    # Global Pooling layer (default mode = average)
    net = l.GlobalPoolLayer(net)
    log.i(("\tFINAL POOLING SHAPE:", l.get_output_shape(net)))

    # Classification Layer (Softmax)
    net = l.DenseLayer(net,
                       len(cfg.CLASSES),
                       nonlinearity=nonlinearity('softmax'),
                       W=initialization('softmax'))

    log.i(("\tFINAL NET OUT SHAPE:", l.get_output_shape(net)))
    log.i("...DONE!")

    # Model stats
    log.i(("MODEL HAS",
           (sum(hasattr(layer, 'W')
                for layer in l.get_all_layers(net))), "WEIGHTED LAYERS"))
    log.i(("MODEL HAS", l.count_params(net), "PARAMS"))

    return net
예제 #22
0
def build_pi_model():

    log.i('BUILDING RASBPERRY PI MODEL...')

    # Random Seed
    lasagne_random.set_rng(cfg.getRandomState())

    # Input layer for images
    net = l.InputLayer((None, cfg.IM_DIM, cfg.IM_SIZE[1], cfg.IM_SIZE[0]))

    # Convolutinal layer groups
    for i in range(len(cfg.FILTERS)):

        # 3x3 Convolution + Stride
        net = batch_norm(
            l.Conv2DLayer(net,
                          num_filters=cfg.FILTERS[i],
                          filter_size=cfg.KERNEL_SIZES[i],
                          num_groups=cfg.NUM_OF_GROUPS[i],
                          pad='same',
                          stride=2,
                          W=initialization(cfg.NONLINEARITY),
                          nonlinearity=nonlinearity(cfg.NONLINEARITY)))

        log.i(('\tGROUP', i + 1, 'OUT SHAPE:', l.get_output_shape(net)))

    # Fully connected layers + dropout layers
    net = l.DenseLayer(net,
                       cfg.DENSE_UNITS,
                       nonlinearity=nonlinearity(cfg.NONLINEARITY),
                       W=initialization(cfg.NONLINEARITY))
    net = l.DropoutLayer(net, p=cfg.DROPOUT)

    net = l.DenseLayer(net,
                       cfg.DENSE_UNITS,
                       nonlinearity=nonlinearity(cfg.NONLINEARITY),
                       W=initialization(cfg.NONLINEARITY))
    net = l.DropoutLayer(net, p=cfg.DROPOUT)

    # Classification Layer (Softmax)
    net = l.DenseLayer(net,
                       len(cfg.CLASSES),
                       nonlinearity=nonlinearity('softmax'),
                       W=initialization('softmax'))

    log.i(("\tFINAL NET OUT SHAPE:", l.get_output_shape(net)))
    log.i("...DONE!")

    # Model stats
    log.i(("MODEL HAS",
           (sum(hasattr(layer, 'W')
                for layer in l.get_all_layers(net))), "WEIGHTED LAYERS"))
    log.i(("MODEL HAS", l.count_params(net), "PARAMS"))

    return net
예제 #23
0
def build_resnet_model():

    log.i('BUILDING RESNET MODEL...')

    # Random Seed
    lasagne_random.set_rng(cfg.getRandomState())

    # Input layer for images
    net = l.InputLayer((None, cfg.IM_DIM, cfg.IM_SIZE[1], cfg.IM_SIZE[0]))

    # First Convolution
    net = l.Conv2DLayer(net,
                        num_filters=cfg.FILTERS[0],
                        filter_size=cfg.KERNEL_SIZES[0],
                        pad='same',
                        W=initialization(cfg.NONLINEARITY),
                        nonlinearity=None)

    log.i(("\tFIRST CONV OUT SHAPE:", l.get_output_shape(net), "LAYER:",
           len(l.get_all_layers(net)) - 1))

    # Residual Stacks
    for i in range(0, len(cfg.FILTERS)):
        net = resblock(net,
                       filters=cfg.FILTERS[i] * cfg.RESNET_K,
                       kernel_size=cfg.KERNEL_SIZES[i],
                       stride=2,
                       num_groups=cfg.NUM_OF_GROUPS[i])
        for _ in range(1, cfg.RESNET_N):
            net = resblock(net,
                           filters=cfg.FILTERS[i] * cfg.RESNET_K,
                           kernel_size=cfg.KERNEL_SIZES[i],
                           num_groups=cfg.NUM_OF_GROUPS[i],
                           preactivated=False)
        log.i(("\tRES STACK", i + 1, "OUT SHAPE:", l.get_output_shape(net),
               "LAYER:", len(l.get_all_layers(net)) - 1))

    # Post Activation
    net = batch_norm(net)
    net = l.NonlinearityLayer(net, nonlinearity=nonlinearity(cfg.NONLINEARITY))

    # Pooling
    net = l.GlobalPoolLayer(net)
    log.i(("\tFINAL POOLING SHAPE:", l.get_output_shape(net), "LAYER:",
           len(l.get_all_layers(net)) - 1))

    # Classification Layer
    net = l.DenseLayer(net,
                       len(cfg.CLASSES),
                       nonlinearity=nonlinearity('identity'),
                       W=initialization('identity'))
    net = l.NonlinearityLayer(net, nonlinearity=nonlinearity('softmax'))

    log.i(("\tFINAL NET OUT SHAPE:", l.get_output_shape(net), "LAYER:",
           len(l.get_all_layers(net))))
    log.i("...DONE!")

    # Model stats
    log.i(("MODEL HAS",
           (sum(hasattr(layer, 'W')
                for layer in l.get_all_layers(net))), "WEIGHTED LAYERS"))
    log.i(("MODEL HAS", l.count_params(net), "PARAMS"))

    return net
예제 #24
0
def test(SNAPSHOTS):

    # Do we have more than one snapshot?
    if not isinstance(SNAPSHOTS, (list, tuple)):
        SNAPSHOTS = [SNAPSHOTS]

    # Load snapshots
    test_functions = []
    for s in SNAPSHOTS:

        # Settings
        NET = s['net']
        cfg.CLASSES = s['classes']
        cfg.IM_DIM = s['im_dim']
        cfg.IM_SIZE = s['im_size']

        # Compile test function
        test_net = birdnet.test_function(NET, hasTargets=False, layer_index=-1)
        test_functions.append(test_net)

    # Parse Testset
    TEST = parseTestSet()

    # Status
    log.i('START TESTING...')
    stats.clearStats()
    stats.tic('test_time')

    # Make predictions
    for spec_batch, labels, filename in bg.threadedGenerator(
            getSpecBatches(TEST)):

        try:

            # Status
            stats.tic('pred_time')

            # Prediction
            prediction_batch = []
            for test_func in test_functions:
                if len(prediction_batch) == 0:
                    prediction_batch = test_func(spec_batch)
                else:
                    prediction_batch += test_func(spec_batch)
            prediction_batch /= len(test_functions)

            # Eliminate the scores for 'Noise'
            if 'Noise' in cfg.CLASSES:
                prediction_batch[:, cfg.CLASSES.index('Noise')] = np.min(
                    prediction_batch)

            # Prediction pooling
            p_pool = predictionPooling(prediction_batch)

            # Get class labels
            p_labels = {}
            for i in range(p_pool.shape[0]):
                p_labels[cfg.CLASSES[i]] = p_pool[i]

            # Sort by score
            p_sorted = sorted(p_labels.items(),
                              key=operator.itemgetter(1),
                              reverse=True)

            # Calculate MLRAP (MRR for single labels)
            targets = np.zeros(p_pool.shape[0], dtype='float32')
            for label in labels:
                if label in cfg.CLASSES:
                    targets[cfg.CLASSES.index(label)] = 1.0
            lrap = metrics.lrap(np.expand_dims(p_pool, 0),
                                np.expand_dims(targets, 0))
            stats.setValue('lrap', lrap, mode='append')

            # Show sample stats
            log.i((filename), new_line=True)
            log.i(('\tLABELS:', labels), new_line=True)
            log.i(('\tTOP PREDICTION:', p_sorted[0][0],
                   int(p_sorted[0][1] * 1000) / 10.0, '%'),
                  new_line=True)
            log.i(('\tLRAP:', int(lrap * 1000) / 1000.0), new_line=False)
            log.i(('\tMLRAP:',
                   int(np.mean(stats.getValue('lrap')) * 1000) / 1000.0),
                  new_line=True)

            # Save some stats
            if p_sorted[0][0] == labels[0]:
                stats.setValue('top1_correct', 1, 'add')
                stats.setValue('top1_confidence', p_sorted[0][1], 'append')
            else:
                stats.setValue('top1_incorrect', 1, 'add')
            stats.toc('pred_time')
            stats.setValue('time_per_batch', stats.getValue('pred_time'),
                           'append')

        except KeyboardInterrupt:
            cfg.DO_BREAK = True
            break
        except:
            log.e('ERROR WHILE TRAINING!')
            continue

    # Stats
    stats.toc('test_time')
    log.i(('TESTING DONE!', 'TIME:', stats.getValue('test_time'), 's'))
    log.r(('FINAL MLRAP:', np.mean(stats.getValue('lrap'))))
    log.r(('TOP 1 ACCURACY:',
           max(
               0,
               float(stats.getValue('top1_correct')) /
               (stats.getValue('top1_correct') +
                stats.getValue('top1_incorrect')))))
    log.r(('TOP 1 MEAN CONFIDENCE:',
           max(0, np.mean(stats.getValue('top1_confidence')))))
    log.r(('TIME PER BATCH:',
           int(np.mean(stats.getValue('time_per_batch')) * 1000), 'ms'))

    return np.mean(stats.getValue('lrap')), int(
        np.mean(stats.getValue('time_per_file')) * 1000)
예제 #25
0
def parseDataset():

    filedata = {}
    with open(r"flockfiles.json", 'r') as f:
        filedata = json.load(f)

    # List of classes, subfolders as class names
    CLASSES = [c for c in sorted(os.listdir(r""))]
    CLASSES = CLASSES[28:]

    # Parse every class
    for c in CLASSES:
        log.i(c)
        index = []
        # List all audio files
        print(c)
        afiles = [f for f in sorted(os.listdir(os.path.join(r"", c)))]
        print(len(afiles))
        # Calculate maximum specs per file
        for i in range(len(afiles)):
            if (afiles[i][:-4] + '.WAV' in filedata[c]):
                index.append(i)
                print("del one")
        index.reverse()
        for i in index:
            del afiles[i]
        print(len(afiles))

        max_specs = cfg.MAX_SPECS_PER_CLASS // len(afiles) + 1

        # Get specs for every audio file
        for i in range(len(afiles)):

            spec_cnt = 0

            try:

                # Stats
                print(i + 1, '/', len(afiles), c, afiles[i])

                # Get specs and signal to noise ratios
                specs, noise = getSpecs(os.path.join(r"", c, afiles[i]))
                # Save specs if it contains signal
                for s in range(len(specs)):
                    # NaN?
                    if np.isnan(noise[s]):
                        noise[s] = 0.0

                    # Above SIGNAL_THRESHOLD?
                    if noise[s] >= cfg.SPEC_SIGNAL_THRESHOLD:
                        # Create target path for accepted specs
                        filepath = os.path.join(cfg.DATASET_PATH, c)
                        if not os.path.exists(filepath):
                            os.makedirs(filepath)
                    elif noise[s] <= cfg.NOISE_THRESHOLD:
                        if RANDOM.choice([True, False], p=[0.60, 0.40]):
                            filepath = os.path.join(cfg.NOISE_PATH)
                            if not os.path.exists(filepath):
                                os.makedirs(filepath)
                        else:
                            filepath = None

                    else:
                        filepath = None
                    if filepath:
                        # Filename contains s2n-ratio
                        filename = str(
                            int(noise[s] *
                                10000)).zfill(4) + '_' + afiles[i].split(
                                    '.')[0] + '_' + str(s).zfill(3)
                        # Write to HDD
                        cv2.imwrite(os.path.join(filepath, filename + '.png'),
                                    specs[s] * 255.0)
                    # Do we have enough specs already?
                    if spec_cnt >= max_specs:
                        break

            except:
                log.e((spec_cnt, 'specs', 'ERROR DURING SPEC EXTRACT'))
                continue
예제 #26
0
 def default_signal_handler(signal_, _):
     log.i(__name__, "Receive signal {0}".format(signal_))
     incubator.stop()
예제 #27
0
def parseDataset():
    spec_all = []

    # List of classes, subfolders as class names
    CLASSES = [
        c for c in sorted(os.listdir(os.path.join(cfg.TRAINSET_PATH, 'audio')))
    ]
    stop = 0
    #switch = 0
    # Parse every class
    for c in CLASSES:
        #if stop==1:
        #break
        #if c == 'norfli':
        #   switch = 1
        #if switch == 1:
        # List all audio files
        afiles = [
            f for f in sorted(
                os.listdir(os.path.join(cfg.TRAINSET_PATH, 'audio', c)))
        ]

        # Calculate maximum specs per file
        max_specs = cfg.MAX_SPECS_PER_CLASS // len(afiles) + 1

        # Get specs for every audio file
        for i in range(len(afiles)):

            spec_cnt = 0

            try:

                # Stats
                print(i + 1, '/', len(afiles), c, afiles[i])

                # Get specs and signal to noise ratios
                specs, noise = getSpecs(
                    os.path.join(cfg.TRAINSET_PATH, 'audio', c, afiles[i]))
                # print('number of specs = ' + str(specs))
                threshold = 0
                max_index = 0
                # Save specs if it contains signal
                for s in range(len(specs)):
                    # NaN?
                    if np.isnan(noise[s]):
                        noise[s] = 0.0

                    # Above SIGNAL_THRESHOLD?
                    if noise[s] >= threshold:
                        threshold = noise[s]
                        max_index = s
                        #print(threshold)
                        # Create target path for accepted specs
                        spec_filepath = os.path.join(cfg.DATASET_PATH, c)
                        if not os.path.exists(spec_filepath):
                            os.makedirs(spec_filepath)

                    else:
                        # Create target path for rejected specs -
                        # but we don't want to save every sample (only 10%)
                        if RANDOM.choice([True, False], p=[0.1, 0.90]):
                            filepath = os.path.join(cfg.NOISE_PATH)
                            if not os.path.exists(filepath):
                                os.makedirs(filepath)
                            if filepath:
                                # Filename contains s2n-ratio
                                filename = str(int(
                                    noise[s] *
                                    10000)).zfill(4) + '_' + afiles[i].split(
                                        '.')[0] + '_' + str(s).zfill(3)

                                # Write to HDD
                                cv2.imwrite(
                                    os.path.join(filepath, filename + '.png'),
                                    specs[s] * 255.0)

                        else:
                            filepath = None

                    if s == (len(specs) - 1):
                        # Filename contains s2n-ratio
                        filename = str(
                            int(noise[max_index] *
                                10000)).zfill(4) + '_' + afiles[i].split(
                                    '.')[0] + '_' + str(s).zfill(3)

                        # Write to HDD
                        cv2.imwrite(
                            os.path.join(spec_filepath, filename + '.png'),
                            specs[max_index] * 255.0)

                        # Save all spec into a list
                        spec_all.append(specs[max_index])

                        # Count specs
                        spec_cnt += 1

                    # Do we have enough specs already?
                    if spec_cnt >= max_specs:
                        break

                # Stats
                log.i((spec_cnt, 'specs'))

            except:
                log.e((spec_cnt, 'specs', 'ERROR DURING SPEC EXTRACT'))
                continue
        stop += 1
    print(spec_all)
    spec_all = np.array(spec_all)
    spec_mean = np.mean(spec_all)
    spec_std = np.std(spec_all)
    print(spec_mean)
    print(spec_std)
예제 #28
0
        except KeyboardInterrupt:
            cfg.DO_BREAK = True
            break

    # Status
    log.i('DONE TESTING!')

    return submission


if __name__ == '__main__':

    # Parse Testset
    TEST, CODES, LABELS = parseTestSet()

    # Load trained models
    if not isinstance(cfg.TEST_MODELS, (list, tuple)):
        cfg.TEST_MODELS = [cfg.TEST_MODELS]
    SNAPSHOTS = []
    for test_model in cfg.TEST_MODELS:
        SNAPSHOTS.append(io.loadModel(test_model))

    # Generate submission
    submission = runTest(SNAPSHOTS, TEST)

    # Write submission to file
    log.i('WRITING SUBMISSION...', new_line=False)
    with open(cfg.RUN_NAME + '_SOUNDSCAPE_SUBMISSION.txt', 'w') as sfile:
        sfile.write(submission)
    log.i('DONE!')
예제 #29
0
def train(NET, TRAIN, VAL):

    # Random Seed
    random = cfg.getRandomState()
    image.resetRandomState()

    # Load pretrained model
    if cfg.PRETRAINED_MODEL_NAME:
        snapshot = io.loadModel(cfg.PRETRAINED_MODEL_NAME)
        NET = io.loadParams(NET, snapshot['params'])

    # Load teacher models
    teach_funcs = []
    if len(cfg.TEACHER) > 0:
        for t in cfg.TEACHER:
            snapshot = io.loadModel(t)
            TEACHER = snapshot['net']
            teach_funcs.append(birdnet.test_function(TEACHER, hasTargets=False))

    # Compile Theano functions
    train_net = birdnet.train_function(NET)
    test_net = birdnet.test_function(NET)

    # Status
    log.i("START TRAINING...")

    # Train for some epochs...
    for epoch in range(cfg.EPOCH_START, cfg.EPOCHS + 1):

        try:

            # Stop?
            if cfg.DO_BREAK:
                break

            # Clear stats for every epoch
            stats.clearStats()
            stats.setValue('sample_count', len(TRAIN) + len(VAL))

            # Start timer
            stats.tic('epoch_time')
            
            # Shuffle dataset (this way we get "new" batches every epoch)
            TRAIN = shuffle(TRAIN, random_state=random)

            # Iterate over TRAIN batches of images
            for image_batch, target_batch in bg.nextBatch(TRAIN):

                # Show progress
                stats.showProgress(epoch)

                # If we have a teacher, we use that model to get new targets
                if len(teach_funcs) > 0:
                    target_batch = np.zeros((len(teach_funcs), target_batch.shape[0], target_batch.shape[1]), dtype='float32')
                    for i in range(len(teach_funcs)):
                        target_batch[i] = teach_funcs[i](image_batch)
                    target_batch = np.mean(target_batch, axis=0)
                
                # Calling the training functions returns the current loss
                loss = train_net(image_batch, target_batch, lr.dynamicLearningRate(cfg.LR_SCHEDULE, epoch))
                stats.setValue('train loss', loss, 'append')
                stats.setValue('batch_count', 1, 'add')

                # Stop?
                if cfg.DO_BREAK:
                    break

            # Iterate over VAL batches of images
            for image_batch, target_batch in bg.nextBatch(VAL, False, True):

                # Calling the test function returns the net output, loss and accuracy
                prediction_batch, loss, acc = test_net(image_batch, target_batch)
                stats.setValue('val loss', loss, 'append')
                stats.setValue('val acc', acc, 'append')
                stats.setValue('batch_count', 1, 'add')
                stats.setValue('lrap', [metrics.lrap(prediction_batch, target_batch)], 'add')

                # Show progress
                stats.showProgress(epoch)

                # Stop?
                if cfg.DO_BREAK:
                    break

            # Show stats for epoch
            stats.showProgress(epoch, done=True)
            stats.toc('epoch_time')
            log.r(('TRAIN LOSS:', np.mean(stats.getValue('train loss'))), new_line=False)
            log.r(('VAL LOSS:', np.mean(stats.getValue('val loss'))), new_line=False)
            log.r(('VAL ACC:', int(np.mean(stats.getValue('val acc')) * 10000) / 100.0, '%'), new_line=False)          
            log.r(('MLRAP:', int(np.mean(stats.getValue('lrap')) * 1000) / 1000.0), new_line=False)
            log.r(('TIME:', stats.getValue('epoch_time'), 's'))

            # Save snapshot?
            if not epoch % cfg.SNAPSHOT_EPOCHS:
                io.saveModel(NET, cfg.CLASSES, epoch)
                print('vish')
                io.saveParams(NET, cfg.CLASSES, epoch)

            # New best net?
            if np.mean(stats.getValue('lrap')) > stats.getValue('best_mlrap'):
                stats.setValue('best_net', NET, static=True)
                stats.setValue('best_epoch', epoch, static=True)
                stats.setValue('best_mlrap', np.mean(stats.getValue('lrap')), static=True)

            # Early stopping?
            if epoch - stats.getValue('best_epoch') >= cfg.EARLY_STOPPING_WAIT:
                log.i('EARLY STOPPING!')
                break

            # Stop?
            if cfg.DO_BREAK:
                break

        except KeyboardInterrupt:
            log.i('KeyboardInterrupt')
            cfg.DO_BREAK = True
            break

    # Status
    log.i('TRAINING DONE!')
    log.r(('BEST MLRAP:', stats.getValue('best_mlrap'), 'EPOCH:', stats.getValue('best_epoch')))

    # Save best model and return
    io.saveParams(stats.getValue('best_net'), cfg.CLASSES, stats.getValue('best_epoch'))
    print('in training vish')
    return io.saveModel(stats.getValue('best_net'), cfg.CLASSES, stats.getValue('best_epoch'))
예제 #30
0
def parseDataset():

    # List of classes, subfolders as class names
    CLASSES = [
        c for c in sorted(os.listdir(os.path.join(cfg.TRAINSET_PATH, 'train')))
    ]

    # Parse every class
    for c in CLASSES:

        # List all audio files
        afiles = [
            f for f in sorted(
                os.listdir(os.path.join(cfg.TRAINSET_PATH, 'train', c)))
        ]

        # Calculate maximum specs per file
        max_specs = cfg.MAX_SPECS_PER_CLASS // len(afiles) + 1

        # Get specs for every audio file
        for i in range(len(afiles)):

            spec_cnt = 0

            try:

                # Stats
                print i + 1, '/', len(afiles), c, afiles[i],

                # Get specs and signal to noise ratios
                specs, noise = getSpecs(
                    os.path.join(cfg.TRAINSET_PATH, 'train', c, afiles[i]))

                # Save specs if it contains signal
                for s in range(len(specs)):

                    # NaN?
                    if np.isnan(noise[s]):
                        noise[s] = 0.0

                    # Above SIGNAL_THRESHOLD?
                    if noise[s] >= cfg.SPEC_SIGNAL_THRESHOLD:

                        # Create target path for accepted specs
                        filepath = os.path.join(cfg.DATASET_PATH, c)
                        if not os.path.exists(filepath):
                            os.makedirs(filepath)

                        # Count specs
                        spec_cnt += 1

                    else:

                        # Create target path for rejected specs -
                        # but we don't want to save every sample (only 10%)
                        if RANDOM.choice([True, False], p=[0.1, 0.90]):
                            filepath = os.path.join(cfg.NOISE_PATH)
                            if not os.path.exists(filepath):
                                os.makedirs(filepath)
                        else:
                            filepath = None

                    if filepath:
                        # Filename contains s2n-ratio
                        filename = str(
                            int(noise[s] *
                                10000)).zfill(4) + '_' + afiles[i].split(
                                    '.')[0] + '_' + str(s).zfill(3)

                        # Write to HDD
                        cv2.imwrite(os.path.join(filepath, filename + '.png'),
                                    specs[s] * 255.0)

                    # Do we have enough specs already?
                    if spec_cnt >= max_specs:
                        break

                # Stats
                log.i((spec_cnt, 'specs'))

            except:
                log.e((spec_cnt, 'specs', 'ERROR DURING SPEC EXTRACT'))
                continue
예제 #31
0
                        help="Target script name",
                        action="store",
                        default=None)
    parser.add_argument("-c",
                        "--target-count",
                        help="Target instance count",
                        action="store",
                        type=int,
                        default=1)
    parser.add_argument("--shell",
                        help="Run command as shell mode",
                        action="store_true",
                        default=False)
    parser.add_argument("--socket",
                        help="Unix domain socket path",
                        action="store",
                        default=None)

    args = parser.parse_args()
    log.init(args.name)

    log.i(__name__, "Start QB Incubator")
    if args.script is None and args.target is None:
        log.e(__name__, "--script and --target are not set.")
        exit(1)

    if args.script is not None:
        script_main(args.shell, args.script, args.socket)
    else:
        target_main(args.shell, args.target, args.target_count, args.socket)