Esempio n. 1
0
def train():
    global GLOBAL_STEP, reduction_arc, cell_arc
    # Dataset
    dataset = DatasetGenerator(batchSize=args.batch_size,
                               dataPath=args.data_path,
                               numOfWorkers=args.data_nums_workers,
                               noise_rate=args.nr,
                               asym=args.asym,
                               seed=args.seed,
                               dataset_type=args.dataset_type)
    dataLoader = dataset.getDataLoader()

    if args.dataset_type == 'cifar100':
        num_classes = 100
        args.epoch = 150
        fixed_cnn = ResNet34(num_classes=num_classes)
    elif args.dataset_type == 'cifar10':
        num_classes = 10
        args.epoch = 120
        fixed_cnn = SCEModel()
    else:
        raise ('Unimplemented')

    if args.loss == 'SCE':
        criterion = SCELoss(alpha=args.alpha,
                            beta=args.beta,
                            num_classes=num_classes)
    elif args.loss == 'CE':
        criterion = torch.nn.CrossEntropyLoss()
    else:
        logger.info("Unknown loss")

    logger.info(criterion.__class__.__name__)
    logger.info("Number of Trainable Parameters %.4f" %
                count_parameters_in_MB(fixed_cnn))
    fixed_cnn = torch.nn.DataParallel(fixed_cnn)
    fixed_cnn.to(device)

    fixed_cnn_optmizer = torch.optim.SGD(params=fixed_cnn.parameters(),
                                         lr=args.lr,
                                         momentum=0.9,
                                         nesterov=True,
                                         weight_decay=args.l2_reg)

    fixed_cnn_scheduler = torch.optim.lr_scheduler.StepLR(fixed_cnn_optmizer,
                                                          1,
                                                          gamma=0.97)

    utilHelper = TrainUtil(checkpoint_path=args.checkpoint_path,
                           version=args.version)
    starting_epoch = 0
    train_fixed(starting_epoch, dataLoader, fixed_cnn, criterion,
                fixed_cnn_optmizer, fixed_cnn_scheduler, utilHelper)
def classify_sound(model, sound_file):
    model = load_model(model)
    LABELS = 'no yes'.split()
    dsGen = DatasetGenerator(label_set=LABELS)
    x = array(dsGen.process_wav_file(sound_file))
    x = asarray(x).reshape(-1, 177, 98, 1)

    #make a prediction
    y = model.predict(x)
    prediction = y[0]

    if prediction[0] > 0.99:
        return (1)
    else:
        return (0)
Esempio n. 3
0
def prepare_data(drum_instrument):
    # Set to global scope for easy access in other functions.
    global DRUM_INSTRUMENT
    global TRAIN_DATA
    global TRAIN_LABELS
    global TRAIN_LABELS_1D
    global TEST_DATA
    global TEST_LABELS

    DRUM_INSTRUMENT = drum_instrument

    dsGen = DatasetGenerator(label_set=LABELS,
                             sample_rate=44100,
                             channels=CHANNELS,
                             mel_bands=MEL_BANDS,
                             time_frames=TIME_FRAMES,
                             diff_from_onset_ms=DIFF_FROM_ONSET_MS,
                             threshold_freq=THRESHOLD_FREQ,
                             drum_instrument=drum_instrument)

    # Load DataFrame with paths/labels for training and validation data.
    dsGen.load_datafiles(DIR)

    # Split data either by songs or windows. Produces different results.
    if TRAINING_DATA_SPLIT_BY == 'SONGS':
        dsGen.apply_train_test_split(test_size=TRAIN_TEST_SPLIT,
                                     random_state=SEED_VALUE)
        TRAIN_DATA, TRAIN_LABELS = dsGen.get_data(mode='train')
        TEST_DATA, TEST_LABELS = dsGen.get_data(mode='test')
    else:
        TRAIN_DATA, TRAIN_LABELS, TEST_DATA, TEST_LABELS = dsGen.apply_train_test_split_by_windows(
            test_size=TRAIN_TEST_SPLIT, shuffle_train_data=True)

    # Needed for class weights.
    TRAIN_LABELS_1D = np.argmax(TRAIN_LABELS, axis=1)
    print('Training data size: ', len(TRAIN_DATA))
    print('Test data size: ', len(TEST_DATA))
    print('Marked onsets count in training labels: ',
          len(list(filter(lambda x: x == 1, TRAIN_LABELS_1D))), '/',
          len(TRAIN_LABELS_1D))
    print('Marked onsets count in test labels: ',
          len(list(filter(lambda x: x == 1, TEST_LABELS))), '/',
          len(TEST_LABELS))
def train_stage(epochs, im_size, step, batch_size, name):
    training_set = DatasetGenerator(im_size=im_size,
                                    num_chars=num_chars,
                                    step=step,
                                    batch_size=batch_size,
                                    font_dir=font_dir,
                                    num_fonts=first_n_fonts,
                                    get_style_labels=(num_style_labels != 0))
    num_fonts = training_set.get_num_fonts()
    for cur_epoch in range(epochs):  # Iterate epochs
        training_set.randomize_fonts()
        for cur_batch, batch in enumerate(
                training_set.batch):  # Iterate batches
            pcgan.set_alpha(
                (cur_batch) / (num_fonts // batch_size + 1) / epochs +
                (cur_epoch) / epochs
            )  # Set alpha for fade in layers (fade from 0 to 1 during whole stage)
            for cur_char in range(num_chars):
                batch_images, batch_labels = map(
                    np.asarray, zip(*batch[cur_char::num_chars])
                )  # Extract images and labels for current char from batch
                loss = pcgan.train_on_batch(
                    x=batch_images, y=batch_labels,
                    return_dict=True)  # Train one batch
                print(
                    f'{im_size}x{im_size} {name} // Epoch {cur_epoch+1} // Batch {cur_batch+1}/{num_fonts//batch_size+1} // Class {cur_char+1} // {loss}'
                )  # Logging
            pcgan.increment_random_seed()
        training_set.reset_generator()
        #generate_images(name=name, postfix=f'_epoch{cur_epoch+1}')
        #generate_images(name=name, postfix=f'_epoch{cur_epoch+1}', seed=707)
    #generate_images(shape=(num_chars, 4), name=name, postfix='_final')
    #generate_images(shape=(num_chars, 4), name=name, postfix='_final', seed=707)
    if save_model:
        pcgan.generator.save(
            f'{training_dir}models/pcgan_stage_{pcgan.n_depth}_{name}')
        pcgan.save_weights(
            f'{training_dir}models/pcgan_stage_{pcgan.n_depth}_{name}')
from sklearn.metrics import accuracy_score
from keras.callbacks import EarlyStopping

from dataset import DatasetGenerator

DIR = 'C:/Users/bijaw/Desktop/New folder (3)/trial'

INPUT_SHAPE = (177, 98, 1)
BATCH = 1
EPOCHS = 15

LABELS = 'bed cat happy'.split()
NUM_CLASSES = len(LABELS)

dsGen = DatasetGenerator(label_set=LABELS)
# Load DataFrame with paths/labels
df = dsGen.load_data(DIR)

dsGen.apply_train_test_split(test_size=0.0, random_state=2018)
dsGen.apply_train_val_split(val_size=0.01, random_state=2018)

from keras.models import Model
from keras.layers import Input, Dense, Dropout, Flatten


def deep(features_shape, num_classes, act='relu'):

    # Input
    x = Input(name='inputs', shape=features_shape, dtype='float32')
    o = x
Esempio n. 6
0
def train():
    # Dataset
    if args.dataset_type == 'clothing1m':
        dataset = Clothing1MDatasetLoader(batchSize=args.batch_size,
                                          dataPath=args.data_path,
                                          numOfWorkers=args.data_nums_workers)
    elif args.dataset_type == 'imagenet':
        dataset = ImageNetDatasetLoader(batchSize=args.batch_size,
                                        dataPath=args.data_path,
                                        seed=args.seed,
                                        target_class_num=200,
                                        nosiy_rate=0.4,
                                        numOfWorkers=args.data_nums_workers)
    else:
        dataset = DatasetGenerator(batchSize=args.batch_size,
                                   dataPath=args.data_path,
                                   numOfWorkers=args.data_nums_workers,
                                   noise_rate=args.nr,
                                   asym=args.asym,
                                   seed=args.seed,
                                   dataset_type=args.dataset_type)

    dataLoader = dataset.getDataLoader()
    eta_min = 0
    ln_neg = 1

    if args.dataset_type == 'clothing1m':
        # Train Clothing1M
        args.epoch = 20
        args.l2_reg = 1e-3
        num_classes = 14
        fixed_cnn = torchvision.models.resnet50(num_classes=14)
        # fixed_cnn.fc = torch.nn.Linear(2048, 14)

    elif args.dataset_type == 'cifar100':
        # Train CIFAR100
        args.lr = 0.1
        args.epoch = 200
        num_classes = 100
        fixed_cnn = ResNet34(num_classes=num_classes)

        # NLNL
        if args.loss == 'NLNL':
            args.epoch = 2000
            ln_neg = 110

    elif args.dataset_type == 'cifar10':
        # Train CIFAR10
        args.epoch = 120
        num_classes = 10
        fixed_cnn = SCEModel(type='cifar10')

        # NLNL
        if args.loss == 'NLNL':
            args.epoch = 1000

    elif args.dataset_type == 'mnist':
        # Train mnist
        args.epoch = 50
        num_classes = 10
        fixed_cnn = SCEModel(type='mnist')
        eta_min = 0.001
        args.l2_reg = 1e-3
        # NLNL
        if args.loss == 'NLNL':
            args.epoch = 720

    elif args.dataset_type == 'imagenet':
        args.epoch = 100
        args.l2_reg = 3e-5
        num_classes = 200
        fixed_cnn = torchvision.models.resnet50(num_classes=num_classes)

    logger.info("num_classes: %s" % (num_classes))

    loss_options = {
        'SCE': SCELoss(alpha=args.alpha, beta=args.beta, num_classes=num_classes),
        'CE': torch.nn.CrossEntropyLoss(),
        'NCE': NormalizedCrossEntropy(scale=args.alpha, num_classes=num_classes),
        'MAE': MeanAbsoluteError(scale=args.alpha, num_classes=num_classes),
        'NMAE': NormalizedMeanAbsoluteError(scale=args.alpha, num_classes=num_classes),
        'GCE': GeneralizedCrossEntropy(num_classes=num_classes, q=args.q),
        'RCE': ReverseCrossEntropy(scale=args.alpha, num_classes=num_classes),
        'NRCE': NormalizedReverseCrossEntropy(scale=args.alpha, num_classes=num_classes),
        'NGCE': NormalizedGeneralizedCrossEntropy(scale=args.alpha, num_classes=num_classes, q=args.q),
        'NCEandRCE': NCEandRCE(alpha=args.alpha, beta=args.beta, num_classes=num_classes),
        'NCEandMAE': NCEandMAE(alpha=args.alpha, beta=args.beta, num_classes=num_classes),
        'GCEandMAE': GCEandMAE(alpha=args.alpha, beta=args.beta, num_classes=num_classes, q=args.q),
        'GCEandRCE': GCEandRCE(alpha=args.alpha, beta=args.beta, num_classes=num_classes, q=args.q),
        'GCEandNCE': GCEandNCE(alpha=args.alpha, beta=args.beta, num_classes=num_classes, q=args.q),
        'MAEandRCE': MAEandRCE(alpha=args.alpha, beta=args.beta, num_classes=num_classes),
        'NGCEandNCE': NGCEandNCE(alpha=args.alpha, beta=args.beta, num_classes=num_classes, q=args.q),
        'NGCEandMAE': NGCEandMAE(alpha=args.alpha, beta=args.beta, num_classes=num_classes, q=args.q),
        'NGCEandRCE': NGCEandRCE(alpha=args.alpha, beta=args.beta, num_classes=num_classes, q=args.q),
        'FocalLoss': FocalLoss(gamma=args.gamma),
        'NFL': NormalizedFocalLoss(scale=args.alpha, gamma=args.gamma, num_classes=num_classes),
        'NLNL': NLNL(num_classes=num_classes, train_loader=dataLoader['train_dataset'], ln_neg=ln_neg),
        'NFLandNCE': NFLandNCE(alpha=args.alpha, beta=args.beta, gamma=args.gamma, num_classes=num_classes),
        'NFLandMAE': NFLandMAE(alpha=args.alpha, beta=args.beta, gamma=args.gamma, num_classes=num_classes),
        'NFLandRCE': NFLandRCE(alpha=args.alpha, beta=args.beta, gamma=args.gamma, num_classes=num_classes),
        'DMI': DMILoss(num_classes=num_classes)
    }

    if args.loss in loss_options:
        criterion = loss_options[args.loss]
    else:
        raise("Unknown loss")

    logger.info(criterion.__class__.__name__)
    logger.info("Number of Trainable Parameters %.4f" % count_parameters_in_MB(fixed_cnn))

    fixed_cnn.to(device)

    if args.loss == 'DMI':
        criterion = loss_options['CE']

    fixed_cnn_optmizer = torch.optim.SGD(params=fixed_cnn.parameters(),
                                         lr=args.lr,
                                         momentum=0.9,
                                         weight_decay=args.l2_reg)

    fixed_cnn_scheduler = CosineAnnealingLR(fixed_cnn_optmizer,
                                            float(args.epoch),
                                            eta_min=eta_min)
    if args.dataset_type == 'clothing1m':
        fixed_cnn_scheduler = MultiStepLR(fixed_cnn_optmizer, milestones=[5, 10], gamma=0.1)
    elif args.dataset_type == 'imagenet':
        fixed_cnn_scheduler = MultiStepLR(fixed_cnn_optmizer, milestones=[30, 60, 80], gamma=0.1)

    utilHelper = TrainUtil(checkpoint_path=args.checkpoint_path, version=args.version)
    starting_epoch = 0

    for arg in vars(args):
        logger.info("%s: %s" % (arg, getattr(args, arg)))

    train_fixed(starting_epoch, dataLoader, fixed_cnn, criterion, fixed_cnn_optmizer, fixed_cnn_scheduler, utilHelper)

    if args.loss == 'DMI':
        criterion = loss_options['DMI']
        fixed_cnn_optmizer = torch.optim.SGD(params=fixed_cnn.parameters(),
                                             lr=1e-6,
                                             momentum=0.9,
                                             weight_decay=args.l2_reg)
        starting_epoch = 0
        fixed_cnn_scheduler = None
        train_fixed(starting_epoch, dataLoader, fixed_cnn, criterion, fixed_cnn_optmizer, fixed_cnn_scheduler, utilHelper)
Esempio n. 7
0
from dataset import DatasetGenerator
import os
import PIL
import matplotlib.pyplot as plt
import random as rd
from scipy.io import wavfile

DIR = "input"
LABELS = list(filter(lambda e: e[0] != "_", os.listdir(DIR)))
dsGen = DatasetGenerator(LABELS)

#data = os.listdir(DIR+r"/four")
#data = data[rd.randrange(0,len(data))]
#path = DIR+r"/four/"+data
path = "test.wav"
im = dsGen.process_wav_file(path)
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)
ax.imshow(im[:, :, 0], aspect='auto', origin='lower')
ax.set_title('Spectrogram')
ax.set_ylabel('Freqs in Hz')
ax.set_xlabel('Seconds')

ax = fig.add_subplot(2, 1, 2)
wav = wavfile.read(path)[1]
ax.plot(wav)
plt.show()
print(wav.shape)
Esempio n. 8
0
    def __init__(self, args):
        print '#' * 60
        print ' ' * 20 + '    Task Created    ' + ' ' * 20
        print '#' * 60

        ######################################################################################################
        # Parameters
        self.batchSize = args.batchSize
        self.lr = args.lr
        self.weightDecay = 1e-4

        self.objNumMax = 30
        self.wordEmbeddingDim = 128
        self.instructionLength = 10
        self.pinMemory = True
        self.dropout = False

        self.epoch = args.epoch
        self.epoch_i = 0

        self.batchPrint = 100
        self.batchModelSave = args.batchModelSave
        self.checkPoint = args.checkPoint

        # Path
        self.vocabularyPath = './data/vocabulary.json'
        self.trainDatasetPath = './data/generated_data_train.json'
        self.testDatasetPath = './data/generated_data_test.json'
        self.logPath = args.logPath

        # Dataset
        self.trainDataset = DatasetGenerator(
            datasetPath=self.trainDatasetPath,
            vocabularyPath=self.vocabularyPath)
        self.testDataset = DatasetGenerator(datasetPath=self.testDatasetPath,
                                            vocabularyPath=self.vocabularyPath)

        # Tokenizer
        self.tokenizer = Tokenizer(vocabPath=self.vocabularyPath)
        self.num_embedding = self.tokenizer.get_num_embedding()

        # DataLoader
        self.trainDataLoader = DataLoader(dataset=self.trainDataset,
                                          shuffle=True,
                                          batch_size=self.batchSize,
                                          num_workers=12,
                                          pin_memory=self.pinMemory)
        self.testDataLoader = DataLoader(dataset=self.testDataset,
                                         shuffle=False,
                                         batch_size=self.batchSize,
                                         num_workers=12,
                                         pin_memory=self.pinMemory)
        # calculate batch numbers
        self.trainBatchNum = int(
            np.ceil(len(self.trainDataset) / float(self.batchSize)))
        self.testBatchNum = int(
            np.ceil(len(self.testDataset) / float(self.batchSize)))

        # Create model
        self.RN = RN(num_embedding=self.num_embedding,
                     embedding_dim=self.wordEmbeddingDim,
                     obj_num_max=self.objNumMax)

        # Run task on all available GPUs
        if torch.cuda.is_available():
            if torch.cuda.device_count() > 1:
                print("Use ", torch.cuda.device_count(), " GPUs")
                self.RN = nn.DataParallel(self.RN)
            self.RN = self.RN.cuda()
            print 'Model Created on GPUs.'

        # Optermizer
        self.optimizer = optim.Adam(self.RN.parameters(),
                                    lr=self.lr,
                                    betas=(0.9, 0.999),
                                    eps=1e-08,
                                    weight_decay=self.weightDecay)
        # Scheduler
        self.scheduler = ReduceLROnPlateau(self.optimizer,
                                           factor=0.1,
                                           patience=10,
                                           mode='min')

        # Loss Function
        self.loss = torch.nn.CrossEntropyLoss()

        # Load model if a checkPoint is given
        if self.checkPoint != "":
            self.load(self.checkPoint)

        # TensorboardX record
        self.writer = SummaryWriter()
        self.stepCnt_train = 1
        self.stepCnt_test = 1
Esempio n. 9
0
from keras.models import load_model
from dataset import DatasetGenerator
import os
import random as rd
import numpy as np
import vizualizer as vz

LABELS_DIR = "input"
DIR = 'people'
LABELS = list(filter(lambda e: e[0] != "_", os.listdir(LABELS_DIR)))
PROBABILITY = 0.86
dsGen = DatasetGenerator(label_set=LABELS)
#data = os.listdir(DIR+r"/bed")
#data = data[rd.randrange(0,len(data))]
#path = DIR+r"/bed/"+data
word = "tree"
#[vz.show_wavfile(os.path.join(DIR,word,file)) for file in os.listdir(os.path.join(DIR,word))]
paths = [
    os.path.join(DIR, word, file)
    for file in os.listdir(os.path.join(DIR, word))
]
data = np.array([dsGen.process_wav_file(path) for path in paths])
model = load_model("model.hdf5")
answ = [LABELS.index(word) for i in range(data.shape[0])]


def test(model, data, answ, verbouse=1):
    pred = model.predict(data, batch_size=data.shape[0])
    err_indexes = []
    for i, pred in enumerate(pred):
        max_ind = np.argmax(pred)
Esempio n. 10
0
'''
Database Generator
'''

from dataset import DatasetGenerator

import sys, os

DIR = 'numbers'  # unzipped train and test data

LABELS = 'one two three four five six seven eight nine'.split()
NUM_CLASSES = len(LABELS)

#==============================================================================
# Prepare data
#==============================================================================
dsGen = DatasetGenerator(label_set=LABELS)
# Load DataFrame with paths/labels for training and validation data
# and paths for testing data
df = dsGen.load_data(DIR)

dsGen.apply_train_test_split(test_size=0.0, random_state=2018)
dsGen.apply_train_val_split(val_size=0.2, random_state=2018)

dsGen.build_dataset(mode='train')
dsGen.build_dataset(mode='val')

sys.exit()
Esempio n. 11
0

config = tf.ConfigProto(intra_op_parallelism_threads=6,
    inter_op_parallelism_threads=2,
    allow_soft_placement=True,
    device_count = {'CPU': 6 })

session = tf.Session(config=config)
backend.set_session(session)

os.environ["OMP_NUM_THREADS"] = "6"
os.environ["KMP_BLOCKTIME"] = "30"
os.environ["KMP_SETTINGS"] = "1"
os.environ["KMP_AFFINITY"]= "granularity=fine,verbose,compact,1,0"

data_generator = DatasetGenerator(dir_path="../compiler/out-without-text-nodes", batch_size=1)
validation_generator = DatasetGenerator(dir_path="../compiler/out-validation", batch_size=1)

number_of_words = utils.get_number_of_words()
latent_dim = 128

# Training

# Define an input sequence and process it.
encoder_inputs = layers.Input(shape=(None, 4))
encoder = layers.LSTM(latent_dim, input_shape=(None, 4), return_state=True)

encoders_outputs, state_h, state_c = encoder(encoder_inputs)
# We discard `encoder_outputs` and only keep the states.
encoder_states = [state_h, state_c]
Esempio n. 12
0
from keras.layers import Conv2D, MaxPooling2D, BatchNormalization
from keras.models import Model
from keras.layers import Input, Dense, Dropout, Flatten
from keras.callbacks import TensorBoard
import pickle

DIR = '/home/amrgalal7/Documents/speech_commands_v0.01'  # unzipped train and test data

INPUT_SHAPE = (177, 98, 1)
BATCH = 32
EPOCHS = 15

LABELS = 'yes no up'.split()
NUM_CLASSES = len(LABELS)

dsGen = DatasetGenerator(label_set=LABELS)
# Load DataFrame with paths/labels
df = dsGen.load_data(DIR)
# dsGen.apply_train_val_split(val_size=0.2, random_state=2018)
dsGen.apply_train_test_val_split(0.7, 0.7)


def deep_cnn(features_shape, num_classes, act='relu'):

    x = Input(name='inputs', shape=features_shape, dtype='float32')
    o = x

    # Block 1
    o = Conv2D(8, (11, 11),
               activation=act,
               padding='same',
Esempio n. 13
0
import matplotlib.pyplot as plt
from scipy.io import wavfile
from dataset import DatasetGenerator
import os

DIR = "input"
LABELS = list(filter(lambda e: e[0] != "_", os.listdir(DIR)))
dsGen = DatasetGenerator(LABELS)


def show_wavfile(path):
    im = dsGen.process_wav_file(path)
    fig = plt.figure()
    ax = fig.add_subplot(2, 1, 1)
    ax.imshow(im[:, :, 0], aspect='auto', origin='lower')
    ax.set_title('Spectrogram')
    ax.set_ylabel('Freqs in Hz')
    ax.set_xlabel('Seconds')

    ax = fig.add_subplot(2, 1, 2)
    wav = wavfile.read(path)[1]
    ax.plot(wav)
    plt.show()
    print(wav.shape)


def show_history(history):
    plt.plot(history.history['acc'])
    plt.plot(history.history['val_acc'])
    plt.title('Model accuracy')
    plt.ylabel('Accuracy')
Esempio n. 14
0
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.legend(['train', 'val'], loc='upper left')
    plt.show()

    # model history for loss
    plt.plot(history['loss'])
    plt.plot(history['val_loss'])
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['train', 'val'], loc='upper left')
    plt.show()


dsGen = DatasetGenerator(label_set=LABELS)
# load DataFrame with paths/labels
df = dsGen.load_data(DIR)
dsGen.apply_train_test_val_split(0.7, 0.7)

# load the model
model = load_model('cnn_4.h5')

# load and visualise model history
plot_model_history('history_4')

y_pred_proba = model.predict_generator(dsGen.generator(BATCH, mode='test'),
                                       int(np.ceil(len(dsGen.df_test) /
                                                   BATCH)),
                                       verbose=1)
y_pred = np.argmax(y_pred_proba, axis=1)
Esempio n. 15
0
            pickle.dump(self.logs, file)


###########

CHECPOINT_DIR = "models"
LOGS_DIR = "models/logs"
TRAIN_DIR = "input"
VAL_DIR = "people"
INPUT_SHAPE = (177, 98, 1)
BATCH = 32
EPOCHS = 20
LABELS = list(filter(lambda e: e[0] != "_", os.listdir('input')))
NUM_CLASSES = len(LABELS)
NETWORK_NAME = "model.hdf5"
dsGen = DatasetGenerator(label_set=LABELS)
# Load DataFrame with paths/labels
df = dsGen.load_data(TRAIN_DIR)
dsGen.apply_train_test_split(test_size=0.3, random_state=2018)
#dsGen.apply_train_val_split(val_size=0.2, random_state=2018)


def deep_cnn(features_shape, num_classes, act='relu'):

    x = Input(name='inputs', shape=features_shape, dtype='float32')
    o = x

    # Block 1
    o = Conv2D(32, (3, 3),
               activation=act,
               padding='same',
Esempio n. 16
0
from dataset import DatasetGenerator

DIR = 'input'  # unzipped train and test data

INPUT_SHAPE = (177, 98, 1)
BATCH = 32
EPOCHS = 15

LABELS = 'yes no up'.split()
NUM_CLASSES = len(LABELS)

#==============================================================================
# Prepare data
#==============================================================================
dsGen = DatasetGenerator(label_set=LABELS)
# Load DataFrame with paths/labels for training and validation data
# and paths for testing data
df = dsGen.load_data(DIR)

dsGen.apply_train_test_split(test_size=0.3, random_state=2018)
dsGen.apply_train_val_split(val_size=0.2, random_state=2018)

#==============================================================================
# Train
#==============================================================================
model = deep_cnn(INPUT_SHAPE, NUM_CLASSES)
model.compile(optimizer='Adam',
              loss='categorical_crossentropy',
              metrics=['acc'])
Esempio n. 17
0
import numpy as np
import utils

from keras import models, layers
from dataset import DatasetGenerator

data_generator = DatasetGenerator(dir_path="../compiler/out-new", batch_size=1)
latent_dim = 128
max_decoder_seq_length = 100000

number_of_words = utils.get_number_of_words()
model = models.load_model("s2s.flex")

encoder_inputs = model.input[0]  # input_1
encoder_outputs, state_h_enc, state_c_enc = model.layers[2].output  # lstm_1
encoder_states = [state_h_enc, state_c_enc]
encoder_model = models.Model(encoder_inputs, encoder_states)

decoder_inputs = model.input[1]  # input_2
decoder_state_input_h = layers.Input(shape=(latent_dim, ), name='input_3')
decoder_state_input_c = layers.Input(shape=(latent_dim, ), name='input_4')
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]

decoder_lstm = model.layers[3]
decoder_outputs, state_h_dec, state_c_dec = decoder_lstm(
    decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h_dec, state_c_dec]
decoder_dense = model.layers[4]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = models.Model([decoder_inputs] + decoder_states_inputs,
                             [decoder_outputs] + decoder_states)
Esempio n. 18
0
    def __init__(self, args):
        print '#' * 60
        print ' ' * 20 + '    Task Created    ' + ' ' * 20
        print '#' * 60

        ######################################################################################################
        # Parameters
        self.batchSize = args.batchSize
        self.lr = args.lr
        self.weightDecay = 1e-4

        self.objNumMax = 60
        self.wordEmbeddingDim = 64
        self.lstmHiddenDim = 128
        self.instructionLength = 10
        self.pinMemory = True
        self.dropout = False

        self.epoch = args.epoch
        self.epoch_i = 0

        self.batchPrint = 100
        self.batchModelSave = args.batchModelSave
        self.checkPoint = args.checkPoint

        # Path
        self.scanListTrain = '../data/scan_list_train.txt'
        self.scanListTest = '../data/scan_list_test.txt'
        self.datasetPath = '../generated_data'
        self.logPath = args.logPath

        # Dataset
        self.tokenizer = Tokenizer(encoding_length=self.instructionLength)
        self.trainDataset = DatasetGenerator(scanListPath=self.scanListTrain,
                                             datasetPath=self.datasetPath)
        self.testDataset = DatasetGenerator(scanListPath=self.scanListTest,
                                            datasetPath=self.datasetPath)

        # build vocabulary from all instructions in the training dataset
        self.tokenizer.build_vocab_from_dataset(self.trainDataset)

        # DataLoader
        self.trainDataLoader = DataLoader(dataset=self.trainDataset,
                                          shuffle=True,
                                          batch_size=self.batchSize,
                                          num_workers=12,
                                          pin_memory=self.pinMemory)
        self.testDataLoader = DataLoader(dataset=self.testDataset,
                                         shuffle=False,
                                         batch_size=self.batchSize,
                                         num_workers=12,
                                         pin_memory=self.pinMemory)
        # calculate batch numbers
        self.trainBatchNum = int(
            np.ceil(len(self.trainDataset) / float(self.batchSize)))
        self.testBatchNum = int(
            np.ceil(len(self.testDataset) / float(self.batchSize)))

        # Create model
        self.RN = RN(batch_size=self.batchSize,
                     num_objects=self.objNumMax,
                     vocab_size=self.tokenizer.get_vocal_length(),
                     embedding_size=self.wordEmbeddingDim,
                     hidden_size=self.lstmHiddenDim,
                     padding_idx=1,
                     dropout=self.dropout)

        # Run task on all available GPUs
        if torch.cuda.is_available():
            if torch.cuda.device_count() > 1:
                print("Use ", torch.cuda.device_count(), " GPUs!")
                self.RN = nn.DataParallel(self.RN)
            self.RN = self.RN.cuda()
            print 'Model Created on GPUs.'

        # Optermizer
        self.optimizer = optim.Adam(self.RN.parameters(),
                                    lr=self.lr,
                                    betas=(0.9, 0.999),
                                    eps=1e-08,
                                    weight_decay=self.weightDecay)

        # Scheduler
        self.scheduler = ReduceLROnPlateau(self.optimizer,
                                           factor=0.1,
                                           patience=10,
                                           mode='min')

        # Loss Function
        self.loss = torch.nn.MSELoss()

        # Load model given a checkPoint
        if self.checkPoint != "":
            self.load(self.checkPoint)

        # create TensorboardX record
        self.writer = SummaryWriter(
            comment='word_embedding_64_lstm_hidden_state_128')
        self.stepCnt_train = 1
        self.stepCnt_test = 1