Exemplo n.º 1
0
    def __init__(self):
        self.img_rows = 128
        self.img_cols = 128
        self.channels = 3
        self.n_features = 128
        self.n_classes = 31
        self.img_shape = (self.img_rows, self.img_cols, self.channels)

        self.data_loader = DataLoader(img_res=(self.img_rows, self.img_cols),
                                      n_classes=self.n_classes)

        optimizer = Adam(0.0002, 0.5)

        self.D_R = build_discriminator(self.img_shape)
        self.D_F = build_feature_discriminator(self.n_features)
        self.D_R.compile(loss='binary_crossentropy',
                         optimizer=optimizer,
                         metrics=['accuracy'])
        self.D_F.compile(loss='binary_crossentropy',
                         optimizer=optimizer,
                         metrics=['accuracy'])

        self.Refiner = build_refiner(self.img_shape, self.channels)
        self.Feature = build_encoder(self.img_shape, self.n_features)
        self.Classifier = build_classifier(self.n_features, self.n_classes)

        self.D_R.trainable = False
        self.D_F.trainable = False

        self.Classifier.compile(loss='categorical_crossentropy',
                                optimizer=optimizer,
                                metrics=['accuracy'])
        self.Classifier.trainable = False

        self.GAN_1 = Sequential()
        self.GAN_1.add(self.Refiner)
        self.GAN_1.add(self.D_R)
        self.GAN_1.compile(loss='binary_crossentropy',
                           optimizer=optimizer,
                           metrics=['accuracy'])

        self.GAN_2 = Sequential()
        self.GAN_2.add(self.Refiner)
        self.GAN_2.add(self.Feature)
        self.GAN_2.add(self.D_F)
        self.GAN_2.compile(loss='binary_crossentropy',
                           optimizer=optimizer,
                           metrics=['accuracy'])

        self.GAN_3 = Sequential()
        self.GAN_3.add(self.Refiner)
        self.GAN_3.add(self.Feature)
        self.GAN_3.add(self.Classifier)
        self.GAN_3.compile(loss='categorical_crossentropy',
                           optimizer=optimizer,
                           metrics=['accuracy'])
    def __init__(self, n_vocab, max_seq_len, z_dim, c_dim, emb_dim,
                 pretrained_emb, freeze_embeddings, flow, flow_type, E_args,
                 G_args, C_args):
        super(RNN_VAE, self).__init__()
        self.MAX_SEQ_LEN = max_seq_len
        self.n_vocab = n_vocab
        self.z_dim = z_dim
        self.c_dim = c_dim
        self.device = torch.device('cuda')
        """
        Word embeddings layer
        """
        self.emb_dim = emb_dim
        self.word_emb = nn.Embedding(n_vocab, self.emb_dim, PAD_IDX)
        if pretrained_emb is not None:
            assert self.emb_dim == pretrained_emb.size(
                1), 'emb dim dont match with pretrained'
            self.word_emb = nn.Embedding(n_vocab, self.emb_dim, PAD_IDX)
            # Set pretrained embeddings
            self.word_emb.weight.data.copy_(pretrained_emb)
        if freeze_embeddings:
            self.word_emb.weight.requires_grad = False
        '''
        Initialize all the modules
        '''
        self.encoder = build_encoder('gru',
                                     emb_dim=self.emb_dim,
                                     z_dim=z_dim,
                                     **E_args)
        self.decoder = build_decoder(embedding=self.word_emb,
                                     emb_dim=self.emb_dim + z_dim + c_dim,
                                     output_dim=n_vocab,
                                     h_dim=z_dim + c_dim,
                                     **G_args)
        self.classifier = build_classifier('cnn', self.emb_dim, **C_args)

        # Intiialize flow
        self.use_flow = flow > 0
        if self.use_flow:
            self.flow_model = build_flow(flow_type, flow, z_dim)
Exemplo n.º 3
0
from models.classifier import build_classifier, sample_audio

import os

dirname = r"../audio/samples"

FRAMES = 44100
samples = []

classifier = build_classifier(dirname, 10, "svc")

for f in os.listdir(dirname):
    if f.endswith(".wav"):
        fn = os.path.join(dirname, f)

        sample = sample_audio(fn, 10000, 0)

        p = classifier.predict_sample(sample)

        print("{0}: {1}".format(f, p[0]))
Exemplo n.º 4
0
val_gen = CompositeSequence(
    thresholds_csv=classifier_config.THRESHOLDS_CSV,
    batch_size=classifier_config.BATCH_SIZE,
    shuffle_pre_training=False,
    shuffle_on_epoch=False,
    image_shape=classifier_config.INPUT_SHAPE,
    train_val='val'
    )

print(f'Training generator with {len(train_gen)} batches')
print(f'Validation generator with {len(val_gen)} batches')
print((len(train_gen)+len(val_gen))*classifier_config.BATCH_SIZE)

# Build model
model = build_classifier(classifier_config.AET_PATH, freeze_to_layer='concatenate_1')
optimizer = Adam(0.0001)
model.compile(
    optimizer,
    loss=CategoricalFocalLoss(alpha=0.25, gamma=4),
    metrics=[mean_iou]
)

# Prepare callbacks
tb_cback = TensorBoard(log_dir=log_dir, batch_size=classifier_config.BATCH_SIZE)
model_ckpt = ModelCheckpoint(
    filepath=f'./{weights_dir}/best_classifier_model.hdf5', 
    monitor='val_mean_iou', 
    save_best_only=True
    )
sgdr_cback = SGDRScheduler(
Exemplo n.º 5
0
from models.classifier import build_classifier, sample_audio

import os
import time

train_dir = r"../audio/train_samples"
test_dir = r"../audio/test_samples"

FRAMES = 44100
samples = []

classifier = build_classifier(train_dir, 10, )

tests = 0

for f in os.listdir(test_dir):
    if f.endswith(".wav"):
        fn = os.path.join(test_dir, f)

        sample = sample_audio(fn, 10000, 0)

        p = classifier.predict_sample(sample)

        print("##################################")
        tests += 1
        print("Untrained sample {0}".format(str(tests)))
        print("Processing '{0}'".format(f))
        time.sleep(1)
        print("  - Result: {0}".format(p[0]))