Beispiel #1
0
    def get_example(self, i):
        if self.mix:  # Training phase of BC learning
            # Select two training examples
            while True:
                sound1, label1 = self.base[random.randint(
                    0,
                    len(self.base) - 1)]
                sound2, label2 = self.base[random.randint(
                    0,
                    len(self.base) - 1)]
                if label1 != label2:
                    break
            sound1 = self.preprocess(sound1)
            sound2 = self.preprocess(sound2)

            # Mix two examples
            r = np.array(random.random())
            sound = U.mix(sound1, sound2, r, self.opt.fs).astype(np.float32)
            eye = np.eye(self.opt.nClasses)
            label = (eye[label1] * r + eye[label2] * (1 - r)).astype(
                np.float32)

        else:  # Training phase of standard learning or testing phase
            sound, label = self.base[i]
            sound = self.preprocess(sound).astype(np.float32)
            label = np.array(label, dtype=np.int32)

        if self.train and self.opt.strongAugment:
            sound = U.random_gain(6)(sound).astype(np.float32)

        return sound, label
Beispiel #2
0
def audio_mix():
    if globals.StartFlag and \
       len(audio.output_queue) < MAX_AUDIO_OUTPUT_QUEUE_LENGTH:
        end = globals.OutputSequenceNumber + BLOCK_SIZE / FRAMES_PER_PACKET
        data = []
        volumes = []
        for player in globals.Players:
            if player.id == globals.myPlayer.id:
                continue

            missing_data = False
            for i in xrange(globals.OutputSequenceNumber, end):
                if i not in player.audio_packets:
                    missing_data = True
            if missing_data:
                continue

            d = ''
            for i in xrange(globals.OutputSequenceNumber, end):
                d += player.audio_packets[i]
            data.append(d)
            volumes.append(player.volume)

        print 'eating: %d through %d' % (globals.OutputSequenceNumber, end)
        globals.OutputSequenceNumber = end
        audio.output_queue.append(mix(data, volumes))
def get_train_example(iterator_next):
    '''Get a training or testing sample
    '''
    if mix:  # Training phase of BC learning
        # sound1, label1, sound2, label2 = U.get_different_sounds(iterator_next)
        sound1, label1 = iterator_next
        sound2, label2 = iterator_next
        ratio = np.array(np.random.random())
        sound = U.mix(sound1, sound2, ratio, opt.fs)
        eye = tf.eye(opt.nClasses)
        label = tf.gather(eye, label1) * ratio
        label += tf.gather(eye, label2) * (1 - ratio)

    # elif not train and opt.longAudio > 0:  # Mix two audio on long frame (for testing)
    #     sound1, label1, sound2, label2 = U.get_different_sounds(iterator)
    #     raise NotImplementedError()

    # Training phase of standard learning or testing phase
    # sound, label = iterator_next

    if opt.noiseAugment:
        sound_len = tf.shape(sound)[0]
        sound = sound + 0.01 * next_noise(is_train, sound_len)

    if train and opt.strongAugment:
        sound, label = U.random_gain(6)(sound, label)

    return sound, label
Beispiel #4
0
def run():
    global seq_num
    end = seq_num + BLOCK_SIZE / FRAMES_PER_PACKET
    data = []
    volumes = []
    if end in players[0].audio_packets:
        for player in players:
            d = ''
            for i in xrange(seq_num, end):
                d += player.audio_packets[i]
            data.append(d)
            volumes.append(player.volume)

        audio.output_queue.append((seq_num, end, mix(data, volumes)))
        seq_num = end
Beispiel #5
0
def close():
    dump_file = wave.open('test_out.wav', 'w')
    dump_file.setparams((2, 2, 44100, 0, 'NONE', 'not compressed'))

    while len(audio.input_queue):
        seq, data = audio.input_queue.pop(0)
        input_audio_packets[seq] = data

    to_write = []

    for seq in sorted(input_audio_packets.keys()):
        volumes = [p.volume for p in players]
        data = [p.audio_packets[seq] for p in players]

        volumes.append(10.0)
        data.append(input_audio_packets[seq])

        to_write.append(mix(data, volumes))
    dump_file.writeframes(''.join(to_write))
    dump_file.close()
Beispiel #6
0
from theano import tensor as T

PURCENT = 5  # Purcentage of the set you want on the test set
NUM_FRAMES = 60
DATADIR = '/baie/corpus/emoMusic/train/'
# DATADIR = './train/'

do_regularize = False

y_, song_id, nb_of_songs = load_y(DATADIR)
X_ = load_X(DATADIR, song_id)

# Now  let's mix everything so that we can take test_set and train_set independantly
# We need to separate PER SONG
X_train, y_train, X_test, y_test, song_id_tst = mix(X_, y_, PURCENT,
                                                    NUM_FRAMES, song_id,
                                                    nb_of_songs)
print X_train.shape, y_train.shape, X_test.shape, y_test.shape
# print X_train[0:3,0:3]

# standardize data
X_train, scaler = standardize(X_train)
X_test, _ = standardize(X_test, scaler)

X_train = X_train[:, [
    10, 12, 13, 17, 19, 82, 83, 84, 85, 89, 90, 91, 103, 140, 142, 146, 148,
    212, 214, 218, 220
]]
X_test = X_test[:, [
    10, 12, 13, 17, 19, 82, 83, 84, 85, 89, 90, 91, 103, 140, 142, 146, 148,
    212, 214, 218, 220
import theano
from theano import tensor as T

PURCENT = 5  # Purcentage of the set you want on the test set
NUM_FRAMES = 60
DATADIR = "/baie/corpus/emoMusic/train/"
# DATADIR = './train/'

do_regularize = False

y_, song_id, nb_of_songs = load_y(DATADIR)
X_ = load_X(DATADIR, song_id)

# Now  let's mix everything so that we can take test_set and train_set independantly
# We need to separate PER SONG
X_train, y_train, X_test, y_test, song_id_tst = mix(X_, y_, PURCENT, NUM_FRAMES, song_id, nb_of_songs)
print X_train.shape, y_train.shape, X_test.shape, y_test.shape
# print X_train[0:3,0:3]
# print np.mean(X_train[:,0:3], axis=0), np.std(X_train[:,0:3], axis=0)
# print np.mean(X_test[:,0:3], axis=0), np.std(X_test[:,0:3], axis=0)

# with(open('train_dummy.txt', mode='w')) as infile:
#     for i in range(X_train.shape[0]):
#         s=''
#         for feat in range(3):
#             s = s + '%g '%X_train[i,feat]
#         infile.write('%s\n'%s)

# standardize data
X_train, scaler = standardize(X_train)
X_test, _ = standardize(X_test, scaler)
Beispiel #8
0
def show_results(topology, results, size, verbose):
    for result in results:
        items = list()

        border = np.concatenate([
            np.zeros((2 * size, 2, 3)), 0.5 * np.ones((2 * size, 2 * 2, 3)),
            np.zeros((2 * size, 2, 3))
        ],
                                axis=1)

        name = 'input'
        if 'name' in result:
            name += ': ' + str(result['name'], 'utf-8')
        if 'variant' in result:
            name += '(' + str(result['variant'], 'utf-8') + ')'

        # s = set([
        #     'input: image_08046(0.75_none_0.0_0)',
        #     'input: image_07810(0.75_none_0.0_1)',
        #     'input: image_07119(0.75_none_0.0_3)',
        # ])
        # if name not in s:
        #     continue

        inputs = normalize(cv2.cvtColor(result['inputs'], cv2.COLOR_RGB2BGR))
        items.append(annotate(inputs, size, 'input', name))
        if 'labels' in result:
            labels = cv2.cvtColor(result['labels'], cv2.COLOR_GRAY2BGR)
            items.append(border)
            items.append(annotate(labels, size, 'label'))
        predictions = cv2.cvtColor(result['predictions'].astype(np.float32),
                                   cv2.COLOR_GRAY2BGR)
        items.append(border)
        items.append(annotate(predictions, size, 'prediction', topology))
        if 'mosaic' in result:
            r = result['mosaic'][:, :, 0]
            g = result['mosaic'][:, :, 1]
            b = result['mosaic'][:, :, 2]
            tp = np.sum(
                np.bitwise_and(np.bitwise_and(r == 0, g == 255), b == 0))
            fp = np.sum(
                np.bitwise_and(np.bitwise_and(r == 255, g == 0), b == 0))
            tn = np.sum(np.bitwise_and(np.bitwise_and(r == 0, g == 0), b == 0))
            fn = np.sum(
                np.bitwise_and(np.bitwise_and(r == 255, g == 0), b == 255))
            if tp + fp > 0:
                sp = tp / (tp + fp)
            else:
                sp = 1
            if tp + fn > 0:
                sr = tp / (tp + fn)
            else:
                sr = 1
            if sp + sr > 0:
                f1 = 2 * (sp * sr) / (sp + sr)
            else:
                f1 = 0
            items.append(border)
            items.append(
                annotate(
                    cv2.cvtColor(tofloats(result['mosaic']),
                                 cv2.COLOR_RGB2BGR), size,
                    'iou (P:%.00f%%, R:%.00f%%, F1:%.02f)' %
                    (100 * sp, 100 * sr, f1), topology))
        if 'labels' in result:
            items.append(border)
            items.append(annotate(mix(inputs, labels), size, 'target'))
        items.append(border)
        items.append(
            annotate(mix(inputs, predictions), size, 'result', topology))

        cv2.imshow('cv2: result', tobytes(np.concatenate(items, axis=1)))

        for key in result.keys():
            if key.startswith('mosaic_'):
                cv2.imshow('cv2: ' + key, tobytes(normalize(result[key])))

        key = cv2.waitKey()
        while key != ord('q') and key != ord('c'):
            key = cv2.waitKey()
        if key == ord('q'):
            return False
    return True