Пример #1
0
def run():

    import sys, getopt, os
    load_from = None

    opts, args = getopt.getopt(sys.argv[1:], "l:")
    for opt, val in opts:
        if opt == "-l": load_from = val

    bg = QuestionsBatchGenerator(args[0], 1000)

    trainig_set_size = bg.training_samples()

    validation_data = bg.validateSet()
    print "Validation set created"
    #print validation_data

    model = createModel(bg.rowSize, 2)
    if load_from: model.load_weights(load_from)

    for i in range(100):
        pair_x, pair_truth = bg.loadPair(i)
        q1, q2, dup_ = pair_truth
        print "Question 1: ", q1
        print "Question 2: ", q2

        y = model.predict(pair_x)[0]
        dup = y[1] > 0.5

        correct = dup == dup_

        print "Same: %s   model: %.3f (%s)" % (dup_, y[1], "CORRECT"
                                               if correct else "incorrect")
        print
Пример #2
0
def Test():
    _model = model.createModel(True)

    np.set_printoptions(threshold=20)

    generator = data.ClockGenerator(model.IMG_SIZE, model.INCLUDE_SECONDS_HAND,
                                    0.5)
    generator.shakeVariance = 0

    input, output = generator.generateClocksForLocalization(
        model.MODEL_SUBDIVIDE, 20)

    results = _model.predict(input)

    correct = 0
    for i in range(0, len(output)):
        # save the image with the expect and predicted rectangles drawn on it??
        sourceImg = Image.fromarray(
            input[i].reshape(model.IMG_SIZE[1], model.IMG_SIZE[0]) *
            255.0).convert("RGB")

        draw = ImageDraw.Draw(sourceImg)
        draw.rectangle(generator.GetCoordsFromOutput(output[i],
                                                     model.IMG_SIZE),
                       outline="green")
        draw.rectangle(generator.GetCoordsFromOutput(results[i],
                                                     model.IMG_SIZE),
                       outline="red")

        filepath = '/tmp/clock_%s_%d.png' % (generator.convertOutputToRect(
            output[i]), i)
        sourceImg.save(filepath)
Пример #3
0
def Learn():

    # 1. create the model
    print("creating the model")
    _model = model.createModel(True)

    # 2. train the model
    print("initializing the generator")
    batch_size = 1
    generator = data.ClockGenerator(model.IMG_SIZE, model.INCLUDE_SECONDS_HAND,
                                    0.5)
    generator.shakeVariance = 0

    iterations = 1000000

    print("beginning training")
    handler = SignalHandler()
    i = 0
    while True:

        if handler.stop_processing:
            break

        #n = int(random.random() * 43200)
        n = 10000
        print(i)
        Train(generator, _model, n)
        i += n

        if i >= iterations:
            break

    _model.save(model.MODEL_H5_NAME)
Пример #4
0
def train():

    train, test = img_gen.loadDataset()
    train_data = []
    train_labels_one_hot = []
    test_data = []
    test_labels_one_hot = []
    for dat in train:
        train_data.append(dat[0])
        train_labels_one_hot.append(dat[1])
    for dat in test:
        test_data.append(dat[0])
        test_labels_one_hot.append(dat[1])

    train_data = np.asarray(train_data)
    train_labels_one_hot = np.asarray(train_labels_one_hot)
    test_data = np.asarray(test_data)
    test_labels_one_hot = np.asarray(test_labels_one_hot)

    model1 = model.createModel()

    batch_size = 15
    epochs = 50

    LoadPretrained = False
    if (LoadPretrained):
        model1.load_weights("model1.h5")

    history = model1.fit(train_data,
                         train_labels_one_hot,
                         batch_size=batch_size,
                         epochs=epochs,
                         verbose=1,
                         validation_data=(test_data, test_labels_one_hot))

    model1.evaluate(test_data, test_labels_one_hot)
    model1.save_weights("model1.h5")

    import matplotlib.pyplot as plt
    # Loss Curves

    plt.figure(figsize=[8, 6])
    plt.plot(history.history['loss'], 'r', linewidth=3.0)
    plt.plot(history.history['val_loss'], 'b', linewidth=3.0)
    plt.legend(['Training loss', 'Validation Loss'], fontsize=18)
    plt.xlabel('Epochs ', fontsize=16)
    plt.ylabel('Loss', fontsize=16)
    plt.title('Loss Curves', fontsize=16)

    # Accuracy Curves
    plt.figure(figsize=[8, 6])
    plt.plot(history.history['acc'], 'r', linewidth=3.0)
    plt.plot(history.history['val_acc'], 'b', linewidth=3.0)
    plt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=18)
    plt.xlabel('Epochs ', fontsize=16)
    plt.ylabel('Accuracy', fontsize=16)
    plt.title('Accuracy Curves', fontsize=16)
    plt.waitforbuttonpress()
    input("press enter to finish:")
Пример #5
0
def run():

    import sys, getopt, os
    save_to = None
    load_from = None

    batch_size = 60
    nworkers = 2
    verbose = 1

    opts, args = getopt.getopt(sys.argv[1:], "s:l:n:qw:")
    for opt, val in opts:
        if opt == "-s": save_to = val
        if opt == "-l": load_from = val
        if opt == "-w":
            load_from = val
            save_to = val
        if opt == "-n": nworkers = int(val)
        if opt == "-q": verbose = 0

    os.system("rm logs/*")

    bg = QuestionsBatchGenerator(args[0], 1000)

    print "Row size=", bg.rowSize

    trainig_set_size = bg.training_samples()

    validation_data = bg.validateSet()
    print "Validation set created"
    #print validation_data

    model = createModel(bg.rowSize, 2)

    if load_from:
        model.load_weights(load_from, by_name=True)
        print
        print "model weights loaded from %s" % (load_from, )
        print

    tb = TensorBoard(write_images=False, histogram_freq=1.0)
    callbacks = [tb]
    if save_to:
        callbacks.append(
            ModelCheckpoint(filepath=save_to,
                            verbose=1,
                            save_best_only=True,
                            save_weights_only=True,
                            monitor="val_loss"))

    model.fit_generator(bg.batches_guargded(batch_size),
                        int(trainig_set_size / batch_size / 20),
                        epochs=1000,
                        verbose=verbose,
                        workers=nworkers,
                        callbacks=callbacks,
                        validation_data=validation_data)
Пример #6
0
    def __init__(self):
        self.X0 = None
        self.predictions = []
        self.lastPredictions = None
        self.model = createModel(nbClasses=3,
                                 imageSize=datasetImageSize,
                                 maxlength=datasetMaxSerieLength)
        print "Loading model parameters..."
        self.model.load(modelsPath + 'eyeDNN_HD_SLIDE.tflearn')

        #History of last 3 predictions
        self.history = [None for i in range(averageWindowSize)]
Пример #7
0
def process(X_train, X_test, y_train, y_test):
    bestS, bestAF = mdl.getBestModel(X_train,
                                     y_train,
                                     loss=cfg.loss,
                                     metrics=cfg.metrics)
    model = mdl.createModel(len(X_train[0]),
                            bestS,
                            bestAF,
                            loss=cfg.loss,
                            metrics=cfg.metrics)

    model.compile(loss=cfg.loss, optimizer='adam', metrics=cfg.metrics)

    # mdl.estimateScore(model, X, y, epochs=cfg.epochs, batch_size=cfg.batch_size, n_folds = cfg.n_folds, repeats = cfg.repeats)

    model.fit(X_train,
              y_train,
              batch_size=cfg.batch_size,
              epochs=cfg.epochs,
              verbose=2)
    _, score = model.evaluate(X_test, y_test)

    if cfg.regr == True:
        pred_y = model.predict(X_test)
        from sklearn.metrics import mean_squared_error
        if cfg.normalization == None:
            mse = mean_squared_error(y_test, pred_y)
        elif cfg.normalization.lower() == 'standard':
            pred_y_back = pp.u_y + pred_y * pp.s_y
            y_test_back = pp.u_y + y_test * pp.s_y
            mse = mean_squared_error(y_test_back, pred_y_back)
        elif cfg.normalization.lower() == 'minmax':
            pred_y_back = pred_y / pp.s_y
            y_test_back = y_test / pp.s_y
            mse = mean_squared_error(y_test_back, pred_y_back)
        #mse = mean_squared_error(y_test/pp.s_y, pred_y/pp.s_y)
        from sklearn.metrics import r2_score
        r2 = r2_score(y_test, pred_y)
        lg.logger.info(f'Test score: {score}, R2: {r2}, mse: {mse}')
    else:
        lg.logger.info(f'Test score: {score}')

    fu.saveModel(model, bestAF, bestS)
    #fu.saveConfigNN_h() Now, substituted by savePPParams()
    fu.savePPParams()
Пример #8
0
def Test2(timeAsString):
    parsedTime = parser.parse(timeAsString)

    _model = model.createModel(True)

    generator = data.ClockGenerator(model.IMG_SIZE, model.INCLUDE_SECONDS_HAND,
                                    0.2)
    generator.shakeVariance = 16

    train, label = generator.generateClockFace(parsedTime.hour,
                                               parsedTime.minute)
    results = _model.predict(train)

    for i in range(0, len(label)):
        filepath = '/tmp/clock_%s.png' % (generator.convertOutputToTime(
            results[i]))
        generator.saveImageToFile(train[i], filepath)
        print("expected", generator.convertOutputToTime(label[i]), "predicted",
              generator.convertOutputToTime(results[i]), "file", filepath)
Пример #9
0
def Test():
    _model = model.createModel(True)

    generator = data.ClockGenerator(model.IMG_SIZE, model.INCLUDE_SECONDS_HAND,
                                    0.2)
    generator.shakeVariance = 16

    train, label = generator.generateClockFaces(12 * 60 * 60)

    results = _model.predict(train)

    correct = 0
    for i in range(0, len(label)):
        expected = generator.convertOutputToTime(label[i])
        predicted = generator.convertOutputToTime(results[i])
        if expected == predicted:
            correct += 1
        print("expected", expected, "predicted", predicted, "correct",
              expected == predicted)
    print("correct", correct, "total", len(label))
Пример #10
0
def run():

    import sys, getopt, os
    save_to = None
    load_from = None

    batch_size = 30

    opts, args = getopt.getopt(sys.argv[1:], "s:l:")
    for opt, val in opts:
        if opt == "-s": save_to = val
        if opt == "-l": load_from = val

    os.system("rm logs/*")

    bg = QuestionsBatchGenerator(args[0], 1000)

    trainig_set_size = bg.training_samples()

    validation_data = bg.validateSet()
    print "Validation set created"
    #print validation_data

    model = createModel(bg.rowSize, 2)

    tb = TensorBoard(write_images=False, histogram_freq=1.0)
    callbacks = [tb]
    if save_to:
        callbacks.append(
            ModelCheckpoint(filepath=save_to,
                            verbose=1,
                            save_best_only=True,
                            save_weights_only=True,
                            monitor="val_loss"))

    model.fit_generator(bg.batches_guargded(batch_size),
                        int(trainig_set_size / batch_size / 100),
                        epochs=1000,
                        workers=4,
                        callbacks=callbacks,
                        validation_data=validation_data)
Пример #11
0
    def _createNewAgent(self):
        self._agents = []
        models = []
        for i, x in enumerate(glob.iglob('weights/*.h5')):
            filename = os.path.abspath(x)
            model = createModel(shape=self._maze.input_size)
            model.load_weights(filename)
            name = os.path.basename(filename)
            if name.startswith('agent-'):
                models.append(model)
            agent = DQNAgent(model)

            self._agents.append(RLAgent(name[:-3], agent, None, None))

        self._agents.insert(
            0, RLAgent('ensemble', DQNEnsembleAgent(models), None, None))

        self._assignMaze2Agents()
        self._activeAgent = 0
        self._paused = True
        return
Пример #12
0
#genres ={'(14)R&B':0,'中国风':1, '古典':2, '摇滚':3,'民谣':4, '爵士':5,'说唱':6}

#List genres
genres = os.listdir(slicesPath)
genres = [
    filename for filename in genres if os.path.isdir(slicesPath + filename)
]

print("genresw:", genres)
nbClasses = len(genres)

total = 0
genre_haven = [0, 0, 0, 0, 0, 0, 0]
right_haven = [0, 0, 0, 0, 0, 0, 0]
#Create model
model = createModel(nbClasses, sliceSize)  #生成模型(未填录数据)

if "train" in args.mode:

    #Create or load new dataset
    train_X, train_y, validation_X, validation_y = getDataset(filesPerGenre,
                                                              genres,
                                                              sliceSize,
                                                              validationRatio,
                                                              testRatio,
                                                              mode="train")

    #Define run id for graphs
    run_id = "MusicGenres - " + str(batchSize) + " " + ''.join(
        random.SystemRandom().choice(string.ascii_uppercase)
        for _ in range(10))
Пример #13
0
def classify():
    #List genres
    genres = os.listdir(slicesPath)
    genres = [filename for filename in genres if os.path.isdir(slicesPath+filename)]
    nbClasses = len(genres)

    #Create model
    model = createModel(nbClasses, sliceSize)
    #s = song()
    #lib = library()

    #create spectrogram path if it doesn't exist
    if not os.path.exists(os.path.dirname(predictSpect)):
        try:
            os.makedirs(os.path.dirname(predictSpect))
        except OSError as exc: # Guard against race condition
            if exc.errno != errno.EEXIST:
                raise

    #create slice path if it doesn't exist
    if not os.path.exists(os.path.dirname(predSlicePath)):
        try:
            os.makedirs(os.path.dirname(predSlicePath))
        except OSError as exc: # Guard against race condition
            if exc.errno != errno.EEXIST:
                raise

    counter = 0

    #print ("Creating Spectrogams")
    #parse through all files in library and create spectrograms
    for filename in os.listdir(predictionPath):
        if filename.endswith(".mp3"):
            newFilename = 'new_' + filename
            newFilename = newFilename.replace(".mp3", "")

            if (Path(predictSpect+newFilename)).exists():
                break
            
            if(isMono(predictionPath+filename)):
                command = "cp '{}' '/tmp/{}.wav' remix 1,2".format(predictionPath+filename, newFilename)
            else:
                command = "sox '{}' '/tmp/{}.wav' remix 1,2".format(predictionPath+filename,newFilename)
            p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True, cwd=currentPath)
            output, errors = p.communicate()
            if errors:
                print (errors)

            #create spectrogram from given file
            filename.replace(".mp3","")
            #print "Creating spectrogram for file {}".format(filename)
            command = "sox '/tmp/{}.wav' -n spectrogram -Y 200 -X 50 -m -r -o '{}.png'".format(newFilename,predictSpect+newFilename)
            p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True, cwd=currentPath)
            output, errors = p.communicate()
            if errors:
                print (errors)

            #Remove tmp mono track
            #os.remove("/tmp/{}.wav".format(newFilename))
            counter += 1

    subdata = []
    data = []

    #print ("Spectrogams Created! ")
    #slice spectrograms

    #print ("Slicing Spectrogams")
    for newFilename in os.listdir(predictSpect):
        if newFilename.endswith(".png"):
            #slice
            img = Image.open(predictSpect+newFilename)

            width, height = img.size
            nbSamples = int(width/sliceSize)
            width - sliceSize

            #For each sample
            for i in range(nbSamples):
                #print "Creating slice: ", (i+1), "/", nbSamples, "for", newFilename
                #Extract and save 128x128 sample
                startPixel = i*sliceSize
                imgTmp = img.crop((startPixel, 1, startPixel + sliceSize, sliceSize + 1))
                imgTmp.save(predSlicePath+"{}_{}.png".format(newFilename[:-4],i))

                img_array = getImageData(predSlicePath+newFilename[:-4]+"_"+str(i)+".png", sliceSize)
                
                #append each loaded image to a sub-data array, and break to new subdata element when name changes
                subdata.append(img_array)
                #os.remove()
            
            #append sub-data array to super array
            data.append(subdata)
            subdata = []


    #print ("Slices Created! ")

    model.load('python/musicDNN.tflearn')
    #print ("Model loaded! ")

    #print data

    #parse through super array predicting each one 
    #and assign name to song object then append song object to songList in library
    #print ("Predicting")
    for vec in data:
        predictionSoftmax = model.predict(vec)[0]
        predictedIndex = max(enumerate(predictionSoftmax), key=lambda x:x[1])[0]
        s.vector = predictionSoftmax
        s.name = filename in os.listdir(predictionPath)
        s.genre = genres[predictedIndex]
        lib.songList.append(s.vector)
        lib.labels.append(s.genre)
    

    for x in lib.songList:
        print x
Пример #14
0
import numpy as np
import tensorflow as tf
import math
import random

from config import config

from model import createModel
from data import UnclassifiedGenome

# Generate a GFF file from the model output
genome = UnclassifiedGenome(input_file=config["files"]["evaluate"])
model = createModel(output_nodes=14)

clustering = [
    'transcript', 'centromere', 'trna', 'exon', 'region', 'snorna',
    'sequence_feature', 'ncrna', 'mrna', 'gene', 'snrna', 'rrna', 'cds',
    'pseudogene'
]
clustering = sorted(clustering)

annotations = dict()

model.load_weights(config["files"]["save-to"])

threshold = config["evaluation"]["threshold"]
minimum_basepairs = config["evaluation"]["minimum-basepairs"]


def computeNucleotideScores(vector):
Пример #15
0
                            validTgtFilePath=Parameter.validTgtFilePath,
                            testTgtFilePath=Parameter.testTgtFilePath,
                            trainGraph=Parameter.trainGraph,
                            validGraph=Parameter.validGraph,
                            testGraph=Parameter.testGraph,
                            min_freq=Parameter.min_freq,
                            BATCH_SIZE=Parameter.BATCH_SIZE,
                            dataPath=Parameter.dataPath,
                            dataFile=Parameter.dataFile,
                            checkpointPath=Parameter.checkpointPath,
                            checkpointFile=Parameter.checkpointFile,
                            mode=Parameter.mode)

    trainDataSet, validDataSet, testDataSet, index2word, gword2index = checkpoint.LoadData(
    )
    model = createModel(len(index2word), len(gword2index)).to(device)
    if Parameter.mode == 'train':
        criterion = LabelSmoothing(smoothing=Parameter.smoothing,
                                   lamda=Parameter.lamda).to(device)

        optimizer = WarmUpOpt(optimizer=optim.Adam(
            params=model.parameters(),
            lr=Parameter.learning_rate,
            betas=(Parameter.beta_1, Parameter.beta_2),
            eps=Parameter.eps,
            weight_decay=Parameter.weight_decay),
                              d_model=Parameter.embedding_dim,
                              warmup_steps=Parameter.warmup_steps,
                              factor=Parameter.factor)

        fit = fit(model=model,
Пример #16
0
# PandasのdataFrameからNumpy配列へ変換
LSTM_training_inputs = [
    np.array(LSTM_training_input)
    for LSTM_training_input in LSTM_training_inputs
]
LSTM_training_inputs = np.array(LSTM_training_inputs)
LSTM_test_inputs = [
    np.array(LSTM_test_input) for LSTM_test_input in LSTM_test_inputs
]
LSTM_test_inputs = np.array(LSTM_test_inputs)

# ランダムシードの設定(randomで生成される数値の固定)
np.random.seed(202)

# モデルの構築
eth_model = model.createModel(LSTM_training_inputs, output_size=1, neurons=20)

# モデルのアウトプットは次の窓の10番目の価格(正規化されている)
LSTM_training_outputs = (training_set['Close**_y'][window_len:].values /
                         training_set['Close**_y'][:-window_len].values) - 1
# データを流してフィッティング
eth_history = eth_model.fit(LSTM_training_inputs,
                            LSTM_training_outputs,
                            epochs=50,
                            batch_size=1,
                            verbose=2,
                            shuffle=True)

# lossの変化をプロットして確認
fig, ax1 = plt.subplots(1, 1)
print("eth_history.epoch\n" + str(eth_history.epoch))
def main():
    # Check to see if GPU is being used
    print(tensorflow.test.gpu_device_name())
    print("Num GPUs Available: ",
          tf.config.experimental.list_physical_devices('GPU'))
    print("Num GPUs Available: ",
          len(tf.config.experimental.list_physical_devices('GPU')))

    # # Dataloader creation and test
    # NEW MODIS DATASET

    img_paths = glob.glob(DATA_PATH)
    print("len(img_paths):", len(img_paths))
    random.shuffle(img_paths)

    train_test_split = 0.8
    X_train_paths = img_paths[:int(train_test_split * len(img_paths))]
    X_test_paths = img_paths[int(train_test_split * len(img_paths)):]

    dims = (448, 448, 3)

    # Loading Training Data
    X_train = np.empty((len(X_train_paths), *dims))
    for i, p in enumerate(X_train_paths):
        X_train[i, :, :, :] = np.load(p)

    # Loading Testing Data
    X_test = np.empty((len(X_test_paths), *dims))
    for i, p in enumerate(X_test_paths):
        X_test[i, :, :, :] = np.load(p)

    print("X_train:", X_train.shape)
    print("X_test:", X_test.shape)

    # To check what percentage of pixels are 'nan'
    print(np.sum(np.isnan(X_train)) / np.prod(X_train.shape))
    print(np.sum(np.isnan(X_test)) / np.prod(X_test.shape))

    # Checking min max to see if normalization is needed or not
    print("Before normalization")
    print(np.nanmin(X_train), np.nanmax(X_train))
    print(np.nanmin(X_test), np.nanmax(X_test))

    # Normalize Inputs
    X_train = utils.normalize(X_train)
    X_test = utils.normalize(X_test)

    # Checking min max after normalization
    print("After normalization")
    print(np.nanmin(X_train), np.nanmax(X_train))
    print(np.nanmin(X_test), np.nanmax(X_test))

    # Set nan values to 0
    # X_train[np.isnan(X_train)] = 0.0
    # X_test[np.isnan(X_test)] = 0.0

    X_train_reshaped = X_train
    del X_train
    X_test_reshaped = X_test
    del X_test

    batch_size = 64

    AUTOTUNE = tensorflow.data.experimental.AUTOTUNE

    train_dataset = tf.data.Dataset.from_tensor_slices(
        (X_train_reshaped, X_train_reshaped))
    test_dataset = tf.data.Dataset.from_tensor_slices(
        (X_test_reshaped, X_test_reshaped))

    train_dataset = train_dataset.map(utils.convert,
                                      num_parallel_calls=AUTOTUNE)
    train_dataset = train_dataset.cache()
    train_dataset = train_dataset.batch(batch_size)
    train_dataset = train_dataset.repeat()
    train_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)

    test_dataset = test_dataset.map(utils.convert, num_parallel_calls=AUTOTUNE)
    test_dataset = test_dataset.cache()
    test_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)

    # # Model creation

    complete_model = model.createModel(dims)
    print(complete_model.summary())

    complete_model.compile(optimizer='rmsprop', loss='mse')

    image = np.expand_dims(X_test_reshaped[0], 0)
    # image = X_test_reshaped[0:10]
    print(image.shape)

    # Define the Activation Visualization callback
    output_dir = TENSORBOARD_LOG_DIR
    callbacks = [
        ActivationsVisualizationCallback(
            validation_data=(image, ),
            layers_name=['conv2d_8'],
            output_dir=output_dir,
        ),
    ]

    # tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir="./tf_callback")
    tensorboard_callback = tf.keras.callbacks.TensorBoard(
        log_dir=TENSORBOARD_LOG_DIR)

    complete_model.fit(train_dataset,
                       epochs=NUM_EPOCHS,
                       steps_per_epoch=len(X_train_reshaped) / batch_size,
                       validation_data=test_dataset,
                       validation_steps=len(X_test_reshaped) / batch_size,
                       callbacks=[callbacks, tensorboard_callback],
                       use_multiprocessing=True)

    complete_model.save(OUTPUT_MODEL_PATH)

    # ## Model Testing

    # Define the Activation Visualization explainer
    # index = np.random.randint(0, len(X_test_reshaped))
    # image = input_test[index].reshape((1, 32, 32, 3))
    # image = np.expand_dims(X_test_reshaped[index],0)
    image = X_test_reshaped[:10]
    label = image
    print('val:', image.shape)

    data = ([image])
    explainer = ExtractActivations()

    layers_of_interest = ['conv2d_1']
    grid = explainer.explain(validation_data=data,
                             model=complete_model,
                             layers_name=['conv2d_1'])
    print(grid.shape)
    explainer.save(grid, ACTIVATION_IMG_PATH, 'conv2d_1.png')

    grid = explainer.explain(validation_data=data,
                             model=complete_model,
                             layers_name=['conv2d_2'])
    print(grid.shape)
    explainer.save(grid, ACTIVATION_IMG_PATH, 'conv2d_2.png')

    grid = explainer.explain(validation_data=data,
                             model=complete_model,
                             layers_name=['conv2d_3'])
    print(grid.shape)
    explainer.save(grid, ACTIVATION_IMG_PATH, 'conv2d_3.png')

    grid = explainer.explain(validation_data=data,
                             model=complete_model,
                             layers_name=['conv2d_8'])
    print(grid.shape)
    explainer.save(grid, ACTIVATION_IMG_PATH, 'conv2d_8.png')

    # In[ ]:

    for i in range(10):
        index = np.random.randint(0, len(X_test_reshaped))

        X_test_im = np.expand_dims(X_test_reshaped[index], 0)
        out_image = np.squeeze(complete_model.predict(X_test_im))

        im_min = out_image.min(axis=(0, 1), keepdims=True)
        im_max = out_image.max(axis=(0, 1), keepdims=True)
        out_image = (out_image - im_min) / (im_max - im_min)

        print("Orig ", np.min(X_test_im), np.max(X_test_im))
        print("Gen ", np.min(out_image), np.max(out_image))
        fig = plt.figure()
        plt.subplot(1, 3, 1)
        plt.imshow(X_test_reshaped[index])
        plt.subplot(1, 3, 2)
        plt.imshow(np.squeeze(X_test_im))
        plt.subplot(1, 3, 3)
        plt.imshow(out_image)
        plt.show()
Пример #18
0
def getModel(shape):
    model = createModel(shape=MODEL_INPUT_SHAPE)
    model.compile(optimizer=Adam(lr=1e-3), loss=Huber(delta=1))
    return model
Пример #19
0
def main():
    dims = (448, 448, 3)

    #     # Dataloader creation and test
    #     img_paths = glob.glob(DATA_PATH)
    #     print("len(img_paths):", len(img_paths))

    #     print("Discarding the unusable images")
    #     img_paths = utils.getUsableImagePaths(img_paths, "png")
    #     print("len(img_paths):", len(img_paths))
    f = open("hurricane_input.txt", 'r')
    img_paths = [fi.strip("\n") for fi in f.readlines()]
    print("len(img_paths):", len(img_paths))

    train_split = 1.0
    valid_split = 0.0
    test_split = 0.0
    X_train_paths = img_paths[:int(train_split * len(img_paths))]
    X_valid_paths = img_paths[int(train_split *
                                  len(img_paths)):int((train_split +
                                                       valid_split) *
                                                      len(img_paths))]
    X_test_paths = img_paths[len(img_paths) -
                             int(test_split * len(img_paths)):]

    # More efficient data fetch pipeline
    AUTOTUNE = tensorflow.data.experimental.AUTOTUNE
    train_dataset = tf.data.Dataset.from_generator(
        generator=customGenerator,
        output_types=(np.float32, np.float32),
        output_shapes=(dims, dims),
        args=[X_train_paths, dims, "png"])
    output = list(train_dataset.take(1).as_numpy_iterator())

    print("@#@#@#@#@#@#@ 1")
    print("train_dataset:", train_dataset)
    print("len(output):", len(output))
    print("len(output[0]):", len(output[0]))
    x, y = output[0]
    print("Printing the output:", x.shape, y.shape)
    print("x:", x.min(), x.max())
    print("y:", y.min(), y.max())
    print(x.shape, y.shape)

    valid_dataset = tf.data.Dataset.from_generator(
        generator=customGenerator,
        output_types=(np.float32, np.float32),
        output_shapes=(dims, dims),
        args=[X_valid_paths, dims, "png"])

    test_dataset = tf.data.Dataset.from_generator(
        generator=customGenerator,
        output_types=(tf.float32, tf.float32),
        args=[X_test_paths, dims, "png"])

    train_dataset = train_dataset.batch(BATCH_SIZE)
    train_dataset = train_dataset.map(utils.convert,
                                      num_parallel_calls=AUTOTUNE)
    train_dataset = train_dataset.cache().shuffle(buffer_size=3 * BATCH_SIZE)
    train_dataset = train_dataset.repeat()
    train_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)

    valid_dataset = valid_dataset.batch(BATCH_SIZE)
    valid_dataset = valid_dataset.map(utils.convert,
                                      num_parallel_calls=AUTOTUNE)
    valid_dataset = valid_dataset.cache().shuffle(buffer_size=3 * BATCH_SIZE)
    valid_dataset = valid_dataset.repeat()
    valid_dataset = valid_dataset.prefetch(buffer_size=AUTOTUNE)

    test_dataset = test_dataset.batch(BATCH_SIZE)
    test_dataset = test_dataset.map(utils.convert, num_parallel_calls=AUTOTUNE)
    test_dataset = test_dataset.cache()
    test_dataset = test_dataset.prefetch(buffer_size=AUTOTUNE)

    output = list(train_dataset.take(1).as_numpy_iterator())

    print("@#@#@#@#@#@#@ 2")
    print("train_dataset:", train_dataset)
    print("len(output):", len(output))
    print("len(output[0]):", len(output[0]))
    x, y = output[0]
    print("Printing the output:", x.shape, y.shape)
    print("x:", x.min(), x.max())
    print("y:", y.min(), y.max())
    print(x.shape, y.shape)
    print("---------------------------------")

    # ARCHITECTURE
    complete_model = model.createModel(dims)
    print(complete_model.summary())

    opt = tf.keras.optimizers.Adam(learning_rate=0.001)
    complete_model.compile(optimizer=opt, loss=ssim_loss)

    image = resize(plt.imread(X_train_paths[0])[:, :, :3], dims)
    print("Activation visualization image shape orig:", image.shape)
    image = np.expand_dims(image, 0)
    print("Activation visualization image shape extended:", image.shape)
    print("image:", image.min(), image.max())
    #     image = X_test_reshaped[0:10]
    #     print(image.shape)

    # Define the Activation Visualization callback
    output_dir = TENSORBOARD_LOG_DIR
    activation_viz_callbacks = [
        ActivationsVisualizationCallback(
            validation_data=(image, ),
            layers_name=['conv2d_8'],
            output_dir=output_dir,
        ),
    ]

    # tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir="./tf_callback")
    tensorboard_callback = tf.keras.callbacks.TensorBoard(
        log_dir=TENSORBOARD_LOG_DIR)

    #     # Schedule LR updates
    #     lr_callback = tf.keras.callbacks.LearningRateScheduler(lr_scheduler, verbose=1)

    # Reduce LR only when the loss graph plateaus
    lr_plateau_callback = tf.keras.callbacks.ReduceLROnPlateau(
        monitor='loss',
        factor=0.2,
        patience=5,
        verbose=1,
        mode='auto',
        min_delta=0.0001,
        cooldown=5,
        min_lr=0.00000005)

    # save after every 10 epochs
    ckpt_path = OUTPUT_MODEL_PATH + "/checkpoints/"
    subprocess.call("mkdir -p %s" % ckpt_path, shell=True)
    model_ckpt_callback = tf.keras.callbacks.ModelCheckpoint(
        filepath=ckpt_path + "epoch{epoch:03d}-loss{loss:.6f}.hdf5",
        monitor='loss',
        verbose=1,
        save_best_only=False,
        save_weights_only=False,
        mode='auto',
        save_freq=(10 * (len(X_train_paths) // BATCH_SIZE)))

    earlystopping_callback = tf.keras.callbacks.EarlyStopping(
        monitor='loss', min_delta=0.000001, patience=5, verbose=1, mode='auto')

    callback_functions = [
        activation_viz_callbacks, tensorboard_callback, lr_plateau_callback,
        model_ckpt_callback, earlystopping_callback,
        WandbCallback()
    ]

    model_history = complete_model.fit(
        train_dataset,
        epochs=NUM_EPOCHS,
        steps_per_epoch=len(X_train_paths) // BATCH_SIZE,
        initial_epoch=0,
        #                                        validation_split=0.2,
        #                                        validation_data=valid_dataset,
        #                                        validation_steps=len(X_valid_paths) // BATCH_SIZE,
        callbacks=callback_functions,
        #                                        use_multiprocessing=True
    )

    #     print("model_history.epoch:", model_history.epoch)
    #     print("model_history:")
    #     pprint(model_history.history)

    complete_model.save(OUTPUT_MODEL_PATH)

    #     sys.exit(0)

    # ## Model Testing

    # Define the Activation Visualization explainer
    # index = np.random.randint(0, len(X_test_reshaped))
    # image = input_test[index].reshape((1, 32, 32, 3))
    # image = np.expand_dims(X_test_reshaped[index],0)
    #     image = X_test[0][0][:10]
    image = np.array([
        resize(plt.imread(train_img_path)[:, :, :3], dims)
        for train_img_path in X_train_paths[:10]
    ])
    #     image = np.array([resize(plt.imread(test_img_path)[:,:,:3], dims) for test_img_path in X_test_paths[:10]])
    #     image = X_test_reshaped[:10]
    label = image
    print('val:', image.shape)

    data = ([image])
    explainer = ExtractActivations()

    layers_of_interest = [
        'conv2d_1', 'conv2d_2', 'conv2d_3', 'conv2d_4', 'conv2d_5', 'conv2d_6',
        'conv2d_7', 'conv2d_8', 'conv2d_transpose', 'conv2d_transpose_1',
        'conv2d_transpose_2', 'conv2d_transpose_3', 'conv2d_9',
        'conv2d_transpose_4'
    ]
    for layer in layers_of_interest:
        grid = explainer.explain(validation_data=data,
                                 model=complete_model,
                                 layers_name=[layer])
        print(grid.shape)
        explainer.save(grid, ACTIVATION_IMG_PATH, '%s.png' % (layer))

    # complete_model = model.load_model(OUTPUT_MODEL_PATH)
    for i in range(10):
        index = np.random.randint(0, len(X_train_paths))

        image_train = resize(plt.imread(X_train_paths[index])[:, :, :3], dims)
        X_image_train = np.expand_dims(image_train, 0)
        out_image = np.squeeze(complete_model.predict(X_image_train))

        im_min = out_image.min(axis=(0, 1), keepdims=True)
        im_max = out_image.max(axis=(0, 1), keepdims=True)
        out_image = (out_image - im_min) / (im_max - im_min)

        print("Orig ", np.min(X_image_train), np.max(X_image_train))
        print("Gen ", np.min(out_image), np.max(out_image))
Пример #20
0
def main(clean=True,
         diff=False,
         reference=False,
         threshold=False,
         only_matches=False,
         match_file=None,
         reference_file=None):
    if clean:
        print('Cleaning output.')
        subprocess.call(['rm', '-rf', 'output'])

    if not match_file and not reference_file:
        filenames = sorted(glob.glob('2018-03-26_*/*.jpeg'))

    else:
        filenames = [reference_file, match_file]

    i1 = cv2.imread(filenames[0])
    i1_gray = cv2.cvtColor(i1, cv2.COLOR_BGR2GRAY)
    reference_filename = filenames[0]
    os.makedirs('output', exist_ok=True)
    os.makedirs('matches', exist_ok=True)
    font = cv2.FONT_HERSHEY_SIMPLEX

    model1 = createModel()
    model1.load_weights('weights.hdf5')

    last_images = deque(maxlen=2)
    counter = 0
    for filename in filenames[1:]:
        orig_i2 = cv2.imread(filename)
        filename = os.path.basename(filename)
        i2_gray = cv2.cvtColor(orig_i2, cv2.COLOR_BGR2GRAY)

        i2_with_rects = orig_i2.copy()
        cv2.putText(i2_with_rects, f'{filename}', (10, 400), font, .7,
                    (0, 255, 255), 2, cv2.LINE_AA)
        d, tres, rects = get_diff_between_images(i1_gray, i2_gray)

        filtered_rects = []

        for rect in rects:
            matched_img = orig_i2[rect[1]:rect[3], rect[0]:rect[2]]
            img = cv2.resize(matched_img, (32, 32))
            predicted = model1.predict(np.array([img]))[0][0]
            print(predicted)
            if round(predicted):
                filtered_rects.append(rect)
                cv2.rectangle(i2_with_rects, rect[:2], rect[2:], (0, 255, 0),
                              1)

            else:
                cv2.rectangle(i2_with_rects, rect[:2], rect[2:], (255, 0, 0),
                              1)
                cv2.putText(i2_with_rects, str(round(predicted, 6)),
                            (rect[0], rect[1] - 10), font, .7, (0, 255, 255),
                            2, cv2.LINE_AA)

        rects = filtered_rects

        output_filename = f'output/{filename}'
        if diff or threshold or reference:
            output_filename = f'output/{filename}-00.jpeg'

        print(
            '[Ref:', reference_filename, ']', filename,
            sorted([((x2 - x1) * (y2 - y1), (x2 - x1) / (y2 - y1))
                    for x1, y1, x2, y2 in rects]))
        cv2.imwrite(output_filename, i2_with_rects)

        if len(rects) or not only_matches:
            if threshold:
                cv2.imwrite(f'output/{filename}-0-threshold.jpeg', tres)

            if diff:
                cv2.imwrite(f'output/{filename}-0-diff.jpeg', d)

            if reference:
                cv2.imwrite(f'output/{filename}-0-reference.jpeg', i1)

        if not len(rects):
            reference_filename = filename
            i1 = orig_i2
            i1_gray = i2_gray

        last_images.append(i2_gray)
Пример #21
0
def trainModel(options):
	inputReader = input_reader.InputReader(options)

	# Data placeholders
	# Image height is fixed while the system automatically adjusts the image width
	with tf.variable_scope('Model'):
		inputBatchImages = tf.placeholder(dtype=tf.float32, shape=[None, options.imageHeight, None, 3], name="inputBatchImages")
		inputBatchLabels = tf.placeholder(dtype=tf.float32, shape=[None, options.maxSequenceLength, inputReader.vocabSize], name="inputBatchLabels")
		inputKeepProbability = tf.placeholder(dtype=tf.float32, name="inputKeepProbability")

		scaledInputBatchImages = tf.scalar_mul((1.0/255), inputBatchImages)
		scaledInputBatchImages = tf.subtract(scaledInputBatchImages, 0.5)
		scaledInputBatchImages = tf.multiply(scaledInputBatchImages, 2.0)

	predictions = model.createModel(scaledInputBatchImages, maximumSeqLength=options.maxSequenceLength, isTrain=options.trainModel, 
									imageHeight=options.imageHeight, networkOutputStride=options.networkStride, 
									numLayersRNN=options.numLayersRNN, attentionSize=options.attentionSize):

	# Create list of vars to restore before train op
	variables_to_restore = slim.get_variables_to_restore(include=["InceptionResnetV2"])

	with tf.name_scope('Loss'):
		# Define loss
		weights = tf.to_float(tf.not_equal(train_output[:, :-1], 1))
		sequence_loss = tf.contrib.seq2seq.sequence_loss(predictions, inputBatchLabels, weights=weights)

		tf.losses.add_loss(sequence_loss)
		# tf.losses.add_loss(cross_entropy_loss_aux_logits)
		loss = tf.reduce_mean(tf.losses.get_losses())

	with tf.name_scope('Accuracy'):
		correct_predictions = tf.equal(tf.argmax(end_points['Predictions'], 1), tf.argmax(inputBatchLabels, 1))
		accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32), name='accuracy')

	with tf.name_scope('Optimizer'):
		# Define Optimizer
		optimizer = tf.train.AdamOptimizer(learning_rate=options.learningRate)

		# Op to calculate every variable gradient
		gradients = tf.gradients(loss, tf.trainable_variables())
		gradients = list(zip(gradients, tf.trainable_variables()))

		# Op to update all variables according to their gradient
		applyGradients = optimizer.apply_gradients(grads_and_vars=gradients)

	# Initializing the variables
	init = tf.global_variables_initializer()
	init_local = tf.local_variables_initializer()

	if options.tensorboardVisualization:
		# Create a summary to monitor cost tensor
		tf.summary.scalar("loss", loss)

		# Create summaries to visualize weights
		for var in tf.trainable_variables():
		    tf.summary.histogram(var.name, var)
		# Summarize all gradients
		for grad, var in gradients:
		    tf.summary.histogram(var.name + '/gradient', grad)

		# Merge all summaries into a single op
		mergedSummaryOp = tf.summary.merge_all()

	# 'Saver' op to save and restore all the variables
	saver = tf.train.Saver()

	bestLoss = 1e9
	step = 1

	# Train model
	if options.trainModel:
		with tf.Session() as sess:
			# Initialize all variables
			sess.run(init)
			sess.run(init_local)

			if options.startTrainingFromScratch:
				print ("Removing previous checkpoints and logs")
				os.system("rm -rf " + options.logsDir)
				os.system("rm -rf " + options.modelDir)
				os.system("mkdir " + options.modelDir)

				# Load the pre-trained Inception ResNet v2 model
				restorer = tf.train.Saver(variables_to_restore)
				restorer.restore(sess, inc_res_v2_checkpoint_file)

			# Restore checkpoint
			else:
				print ("Restoring from checkpoint")
				saver = tf.train.import_meta_graph(options.modelDir + options.modelName + ".meta")
				saver.restore(sess, options.modelDir + options.modelName)

			if options.tensorboardVisualization:
				# Op for writing logs to Tensorboard
				summaryWriter = tf.summary.FileWriter(options.logsDir, graph=tf.get_default_graph())

			print ("Starting network training")
			try:
				# Keep training until reach max iterations
				while True:
					batchImagesTrain, batchLabelsTrain = inputReader.getTrainBatch()
					# print ("Batch images shape: %s, Batch labels shape: %s" % (batchImagesTrain.shape, batchLabelsTrain.shape))

					# If training iterations completed
					if batchImagesTrain is None:
						print ("Training completed")
						break

					# Run optimization op (backprop)
					if options.tensorboardVisualization:
						[trainLoss, currentAcc, _, summary] = sess.run([loss, accuracy, applyGradients, mergedSummaryOp], feed_dict={inputBatchImages: batchImagesTrain, inputBatchLabels: batchLabelsTrain, inputKeepProbability: options.neuronAliveProbability})
						# Write logs at every iteration
						summaryWriter.add_summary(summary, step)
					else:
						[trainLoss, currentAcc, _] = sess.run([loss, accuracy, applyGradients], feed_dict={inputBatchImages: batchImagesTrain, inputBatchLabels: batchLabelsTrain, inputKeepProbability: options.neuronAliveProbability})

					print ("Iteration: %d, Minibatch Loss: %f, Accuracy: %f" % (step, trainLoss, currentAcc * 100))
					step += 1

					if step % options.saveStep == 0:
						# Save model weights to disk
						saver.save(sess, options.modelDir + options.modelName)
						print ("Model saved: %s" % (options.modelDir + options.modelName))

					#Check the accuracy on test data
					if step % options.evaluateStep == 0:
						# Report loss on test data
						batchImagesTest, batchLabelsTest = inputReader.getTestBatch()

						[testLoss, testAcc] = sess.run([loss, accuracy], feed_dict={inputBatchImages: batchImagesTest, inputBatchLabels: batchLabelsTest, inputKeepProbability: 1.0})
						print ("Test loss: %f, Test Accuracy: %f" % (testLoss, testAcc))

						# #Check the accuracy on test data
						# if step % options.saveStepBest == 0:
						# 	# Report loss on test data
						# 	batchImagesTest, batchLabelsTest = inputReader.getTestBatch()
						# 	[testLoss] = sess.run([loss], feed_dict={inputBatchImages: batchImagesTest, inputBatchLabels: batchLabelsTest, inputKeepProbability: 1.0})
						# 	print ("Test loss: %f" % testLoss)

						# 	# If its the best loss achieved so far, save the model
						# 	if testLoss < bestLoss:
						# 		bestLoss = testLoss
						# 		# bestModelSaver.save(sess, best_checkpoint_dir + 'checkpoint.data')
						# 		bestModelSaver.save(sess, checkpointPrefix, global_step=0, latest_filename=checkpointStateName)
						# 		print ("Best model saved in file: %s" % checkpointPrefix)
						# 	else:
						# 		print ("Previous best accuracy: %f" % bestLoss)

			except KeyboardInterrupt:
				print("Process interrupted by user") # Save the model and exit

			# Save final model weights to disk
			saver.save(sess, options.modelDir + options.modelName)
			print ("Model saved: %s" % (options.modelDir + options.modelName))

			# Report loss on test data
			batchImagesTest, batchLabelsTest = inputReader.getTestBatch()
			testLoss = sess.run(loss, feed_dict={inputBatchImages: batchImagesTest, inputBatchLabels: batchLabelsTest, inputKeepProbability: 1.0})
			print ("Test loss (current): %f" % testLoss)

			print ("Optimization Finished!")

	# Test model
	if options.testModel:
		print ("Testing saved model")

		os.system("rm -rf " + options.imagesOutputDirectory)
		os.system("mkdir " + options.imagesOutputDirectory)

		# Now we make sure the variable is now a constant, and that the graph still produces the expected result.
		with tf.Session() as session:
			saver = tf.train.import_meta_graph(options.modelDir + options.modelName + ".meta")
			saver.restore(session, options.modelDir + options.modelName)

			# Get reference to placeholders
			predictionsNode = session.graph.get_tensor_by_name("Predictions:0")
			accuracyNode = session.graph.get_tensor_by_name("Accuracy/accuracy:0")
			inputBatchImages = session.graph.get_tensor_by_name("Model/inputBatchImages:0")
			inputBatchLabels = session.graph.get_tensor_by_name("Model/inputBatchLabels:0")
			inputKeepProbability = session.graph.get_tensor_by_name("Model/inputKeepProbability:0")

			inputReader.resetTestBatchIndex()
			accumulatedAccuracy = 0.0
			numBatches = 0
			fileNames = []
			predictedLabels = []
			while True:
				batchImagesTest, batchLabelsTest = inputReader.getTestBatch()
				files = inputReader.getFileNames(isTrain=False)
				if batchLabelsTest is None:
					break
			
				[predictions, accuracy] = session.run([predictionsNode, accuracyNode], feed_dict={inputBatchImages: batchImagesTest, inputBatchLabels: batchLabelsTest, inputKeepProbability: 1.0})
				# print ('Current test batch accuracy: %f' % (accuracy))
				accumulatedAccuracy += accuracy

				fileNames = fileNames + files
				predictedLabels = predictedLabels + np.argmax(predictions, axis=1).tolist()

				numBatches += 1

				# Save image results
				inputReader.saveLastBatchResults(batchImagesTest, predictedLabels, isTrain=False)

				if numBatches == 20:
					break

			with open('results.npy', 'wb') as fp:
				pkl.dump(fileNames, fp)
				pkl.dump(predictedLabels, fp)

			with open("output.txt", "w") as file:
				for i in range(len(fileNames)):
					file.write(fileNames[i] + " " + str(predictedLabels[i]) + "\n")

		accumulatedAccuracy = accumulatedAccuracy / numBatches
		print ('Cummulative test set accuracy: %f' % (accumulatedAccuracy * 100))

		print ("Model tested")
Пример #22
0
print("| Slice size: {}".format(sliceSize))
print("--------------------------")

if "slice" in args.mode:
    createSlicesFromAudio()
    sys.exit()

#List genres
genres = os.listdir(slicesPath)
genres = [
    filename for filename in genres if os.path.isdir(slicesPath + filename)
]
nbClasses = len(genres)

#Create model
model = createModel(nbClasses, sliceSize)

if "train" in args.mode:

    #Create or load new dataset
    train_X, train_y, validation_X, validation_y = getDataset(filesPerGenre,
                                                              genres,
                                                              sliceSize,
                                                              validationRatio,
                                                              testRatio,
                                                              mode="train")

    #Define run id for graphs
    run_id = "MusicGenres - " + str(batchSize) + " " + ''.join(
        random.SystemRandom().choice(string.ascii_uppercase)
        for _ in range(10))
Пример #23
0
import cv2
import glob
import numpy as np
import random

from model import createModel
random.seed(128)

model1 = createModel()
model1.load_weights('weights.hdf5')

for fname in sorted(glob.glob('matches/non-pidgeon-32/*')):
    i = cv2.imread(fname)
    pred = model1.predict(np.array([i]))[0][0]
    if round(pred):
        print(fname, pred)
Пример #24
0
import tensorflow as tf
import math
import random

from config import config

from model import createModel
from data import Genome

genome = Genome()
model = createModel(output_nodes=len(genome.annotation_fields))

max_frames = config["genome"]["max-scaffolds-per-batch"]

# Run through genome
epoch = int()
while True:

    if epoch >= config["training"]["epoch-limit"]:
        break

    print("Running epoch #{}".format(str(epoch)))

    for contig in genome.contigs:

        print("################ Running framed contig: {} ################".
              format(contig))

        size = random.randint(config["genome"]["min-scaffold-length"],
                              config["genome"]["max-scaffold-length"])
        frame_limit = math.floor(config["genome"]["frame-adjustment-ratio"] *
Пример #25
0
slot_labels = tf.placeholder(tf.int32, [None, None],
                             name='slots')  # [batch, input_sequence_length]
slot_weights = tf.placeholder(
    tf.float32, [None, None],
    name='slot_weights')  # [batch, input_sequence_length]
intent_label = tf.placeholder(tf.int32, [None], name='intent')  # [batch]

use_batch_crossent = True
with tf.variable_scope('model'):
    print("create train model")
    training_outputs = createModel(
        input_data,
        len(in_vocab['vocab']),
        sequence_length,
        len(slot_vocab['vocab']),
        len(intent_vocab['vocab']),
        remove_slot_attn,
        add_final_state_to_intent,
        use_crf=arg.use_crf,
        layer_size=arg.layer_size,
        use_batch_crossent=use_batch_crossent)  # layer_size:64

# slot_output_logits:[batch * input_sequence_length, slot_size]
# slot_output_logits_crf or use_batch_crossent:[batch, input_sequence_length, slot_size]
slot_output_logits = training_outputs[0]
# intent:[batch, intent_size]
intent_output_logit = training_outputs[1]

with tf.variable_scope('slot_loss'):
    if arg.use_crf:
        """
Пример #26
0
    train_loader = torch.utils.data.ConcatDataset([train_loader, val_loader])
    train_loader = torch.utils.data.DataLoader(train_loader,
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               num_workers=1)
    val_loader = torch.utils.data.DataLoader(val_loader,
                                             batch_size=args.batch_size,
                                             shuffle=False,
                                             num_workers=1)
else:
    assert False

# Neural network and optimizer
# We define neural net in model.py so that it can be reused by the evaluate.py script
from model import createModel
model = createModel()
if use_cuda:
    print('Using GPU')
    model.cuda()
else:
    print('Using CPU')

optimizer = optim.Adam(model.parameters(), weight_decay=0)


def train(epoch):
    model.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        if use_cuda:
            data, target = data.cuda(), target.cuda()
        optimizer.zero_grad()
def main():
    # Create Parser
    parser = argparse.ArgumentParser(description='Pytorch MNIST LAB1')
    parser.add_argument("--batch-Size",
                        type=int,
                        default=64,
                        metavar='N',
                        help='input batch size for training (default: 64)')
    parser.add_argument('--test-batch-size',
                        type=int,
                        default=1000,
                        metavar='N',
                        help='input batch size for testing (default: 1000)')
    parser.add_argument('--epochs',
                        type=int,
                        default=5,
                        metavar='N',
                        help='number of epochs to train (default: 5)')
    parser.add_argument('--neural-network',
                        type=str,
                        default='MLP',
                        metavar='M',
                        help='NN type (MLP | Linear)')
    parser.add_argument('--loss',
                        type=str,
                        default='NLL',
                        metavar='M',
                        help='NN type (Margin | NLL)')
    parser.add_argument('--optim-Method',
                        type=str,
                        default='ASGD',
                        metavar='M',
                        help='NN type (SGD | ASGD | LBFGS |)')
    parser.add_argument('--lr',
                        type=float,
                        default=0.01,
                        metavar='LR',
                        help='learning rate (default: 0.01)')
    parser.add_argument('--weight-decay',
                        type=float,
                        default=0.001,
                        metavar='LR',
                        help='weight decay (default: 0.001)')
    parser.add_argument('--momentum',
                        type=float,
                        default=0.5,
                        metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--max-iter',
                        type=float,
                        default=2,
                        metavar='M',
                        help='CG and LBFGS max iterations (default: 2)')
    parser.add_argument('--t0',
                        type=float,
                        default=1,
                        metavar='LR',
                        help='t0 for ASGD (default: 1)')

    arguments = parser.parse_args()
    print(arguments)
    #   Get Batch Size

    # USE CUDA
    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda:0")
    cudnn.benchmark = True
    # device = "cpu"

    # Parameters
    params = {
        'shuffle': True,
        'pin_memory': True,
        'num_workers': 4
    }  # Exception because of bad multiworker handling !! FIXED IN NEW PYTORCH VERSION

    # Download dataset
    # torchvision.transforms.Normalize(0.5,0.5)
    datasetPath = r"..\Datasets"
    Ttransform = torchvision.transforms.Compose(
        [
            torchvision.transforms.ToTensor(),
            torchvision.transforms.Normalize((0.1307, ), (0.3081, ))
        ]
    )  # Needs the 0.5, not 0,5 to make the object iterable !! Try with 0.5, Normalization to see the results
    testData = torch.utils.data.DataLoader(
        torchvision.datasets.MNIST(datasetPath,
                                   download=True,
                                   transform=Ttransform,
                                   train=False),
        **params,
        batch_size=arguments.test_batch_size)
    trainData = torch.utils.data.DataLoader(torchvision.datasets.MNIST(
        datasetPath, download=True, transform=Ttransform, train=True),
                                            **params,
                                            batch_size=arguments.batch_Size)

    #     Start creating the model
    from loss import createLoss
    from model import createModel
    from train import train
    from test import test

    model = createModel(arguments)
    criterion = createLoss(arguments)
    train(arguments, trainData, device, criterion, model)
    test(model, criterion, testData)
def main():
    #     # Dataloader creation and test
    #     img_paths = glob.glob(DATA_PATH)
    #     print("len(img_paths):", len(img_paths))
    #     random.shuffle(img_paths)

    train_test_split = 0.8
    #     X_train_paths = img_paths[:int(train_test_split * len(img_paths))]
    #     X_test_paths = img_paths[int(train_test_split * len(img_paths)):]

    X_train_path = DATA_PATH + "/train"
    X_test_path = DATA_PATH + "/test"
    dims = (448, 448, 3)

    # Loading Data
    #     X_train = CustomDataGenerator(X_train_paths, batch_size=32, dim=dims)
    #     X_test = CustomDataGenerator(X_train_paths, batch_size=32, dim=dims)

    train_generator = ImageDataGenerator(horizontal_flip=True,
                                         vertical_flip=True,
                                         validation_split=(1 -
                                                           train_test_split))

    X_train = train_generator.flow_from_directory(X_train_path,
                                                  target_size=(448, 448),
                                                  class_mode="input",
                                                  batch_size=32,
                                                  seed=324,
                                                  subset='training')
    X_valid = train_generator.flow_from_directory(X_train_path,
                                                  target_size=(448, 448),
                                                  class_mode="input",
                                                  batch_size=32,
                                                  seed=324,
                                                  subset='validation')
    X_test = ImageDataGenerator().flow_from_directory(X_test_path,
                                                      target_size=(448, 448),
                                                      class_mode="input",
                                                      batch_size=32,
                                                      seed=324)

    sample_train = next(X_train)
    print("sample train:", sample_train[0].shape, sample_train[1].shape)
    for i, img in enumerate(sample_train[0][0][:10]):
        print(np.sum(np.isnan(img)), img.min(), img.max())
    sample_test = next(X_test)
    print("sample test:", sample_test[0].shape, sample_test[1].shape)

    #     # RESHAPE IMAGES TO THE DESIRED SIZE
    #     X_train_reshaped = X_train[0]
    #     print(X_train_reshaped[0].shape, X_train_reshaped[1].shape)
    #     X_test_reshaped = X_test[0]
    #     print(X_test_reshaped[0].shape, X_test_reshaped[1].shape)

    #     # More efficient data fetch pipeline
    #     AUTOTUNE = tensorflow.data.experimental.AUTOTUNE

    #     train_dataset = tf.data.Dataset.from_generator(generator=X_train, output_types=(tf.float32, tf.float32))
    #     test_dataset = tf.data.Dataset.from_generator(generator=X_test, output_types=(tf.float32, tf.float32))

    # #     train_dataset = tf.data.Dataset.from_tensor_slices((X_train_reshaped, X_train_reshaped))
    # #     test_dataset = tf.data.Dataset.from_tensor_slices((X_test_reshaped, X_test_reshaped))

    #     train_dataset = train_dataset.map(utils.convert, num_parallel_calls=AUTOTUNE)
    #     train_dataset = train_dataset.cache().shuffle(buffer_size=3*BATCH_SIZE)
    #     train_dataset = train_dataset.batch(BATCH_SIZE)
    #     train_dataset = train_dataset.repeat()
    #     train_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)

    #     test_dataset = test_dataset.map(utils.convert, num_parallel_calls=AUTOTUNE)
    #     test_dataset = test_dataset.cache()
    #     test_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)

    # ARCHITECTURE
    complete_model = model.createModel(dims)
    print(complete_model.summary())

    complete_model.compile(optimizer='rmsprop', loss='mse')

    image = np.expand_dims(X_test[0][0][0], 0)
    # image = X_test_reshaped[0:10]
    print(image.shape)

    # Define the Activation Visualization callback
    output_dir = TENSORBOARD_LOG_DIR
    callbacks = [
        ActivationsVisualizationCallback(
            validation_data=(image, ),
            layers_name=['conv2d_8'],
            output_dir=output_dir,
        ),
    ]

    # tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir="./tf_callback")
    tensorboard_callback = tf.keras.callbacks.TensorBoard(
        log_dir=TENSORBOARD_LOG_DIR)

    complete_model.fit(
        X_train,
        epochs=NUM_EPOCHS,
        steps_per_epoch=X_train.samples // BATCH_SIZE,
        #                        validation_split=0.2,
        validation_data=X_valid,
        validation_steps=X_valid.samples / BATCH_SIZE,
        callbacks=[callbacks, tensorboard_callback,
                   WandbCallback()],
        use_multiprocessing=True)

    complete_model.save(OUTPUT_MODEL_PATH)

    # ## Model Testing

    # Define the Activation Visualization explainer
    # index = np.random.randint(0, len(X_test_reshaped))
    # image = input_test[index].reshape((1, 32, 32, 3))
    # image = np.expand_dims(X_test_reshaped[index],0)
    image = X_test[0][0][:10]
    #     image = X_test_reshaped[:10]
    label = image
    print('val:', image.shape)

    data = ([image])
    explainer = ExtractActivations()

    layers_of_interest = [
        'conv2d_1', 'conv2d_2', 'conv2d_3', 'conv2d_4', 'conv2d_5', 'conv2d_6',
        'conv2d_7', 'conv2d_8', 'conv2d_transpose', 'conv2d_transpose_1',
        'conv2d_transpose_2', 'conv2d_transpose_3', 'conv2d_9',
        'conv2d_transpose_4'
    ]
    for layer in layers_of_interest:
        grid = explainer.explain(validation_data=data,
                                 model=complete_model,
                                 layers_name=[layer])
        print(grid.shape)
        explainer.save(grid, ACTIVATION_IMG_PATH, '%s.png' % (layer))

def batch_lab(
        batch_size, data_generator,
        data):  # Does basically nothing, but just to help with later tasks
    for batch in data_generator.flow(data, batch_size=batch_size):
        batch = resize(batch, (batch_size, *dims))
        #     print(np.max(batch),np.min(batch))
        yield batch, batch


# # Model creation

# In[7]:

complete_model = model.createModel(dims)
print(complete_model.summary())

complete_model.compile(optimizer='rmsprop', loss='mse')

image = np.expand_dims(X_test_reshaped[0], 0)
# image = X_test_reshaped[0:10]
print(image.shape)

# Define the Activation Visualization callback
output_dir = TENSORBOARD_LOG_DIR
callbacks = [
    ActivationsVisualizationCallback(
        validation_data=(image, ),
        layers_name=['conv2d_8'],
        output_dir=output_dir,
Пример #30
0
def main():
    dims = (448, 448, 3)
    
    # Dataloader creation and test
    img_paths = glob.glob(DATA_PATH)
    print("len(img_paths):", len(img_paths))

    train_split = 0.6
    valid_split = 0.2
    test_split = 0.2
    X_train_paths = img_paths[:int(train_split * len(img_paths))]
    X_valid_paths = img_paths[int(train_split * len(img_paths)):int((train_split + valid_split) * len(img_paths))]
    X_test_paths = img_paths[len(img_paths) - int(test_split * len(img_paths)):]
    
    # More efficient data fetch pipeline
    AUTOTUNE = tensorflow.data.experimental.AUTOTUNE
    
#     X_train = customGenerator(X_train_paths, dims)
#     X_test = customGenerator(X_test_paths, dims)
    train_dataset = tf.data.Dataset.from_generator(generator=customGenerator, output_types=(np.float32, np.float32), output_shapes=(dims, dims), args=[X_train_paths, dims, "tif"])
    output = list(train_dataset.take(1).as_numpy_iterator())

    print("@#@#@#@#@#@#@ 1")
    print("train_dataset:", train_dataset)
    print("len(output):", len(output))
    print("len(output[0]):", len(output[0]))
    x,y = output[0]
    print("Printing the output:", x.shape, y.shape)
    print("x:", x.min(), x.max())
    print("y:", y.min(), y.max())
    print(x.shape, y.shape)

    valid_dataset = tf.data.Dataset.from_generator(generator=customGenerator, output_types=(np.float32, np.float32), output_shapes=(dims, dims), args=[X_valid_paths, dims, "tif"])
    
    test_dataset = tf.data.Dataset.from_generator(generator=customGenerator, output_types=(tf.float32, tf.float32), args=[X_test_paths, dims, "tif"])
    
#     train_dataset = tf.data.Dataset.from_tensor_slices((X_train_reshaped, X_train_reshaped))
#     test_dataset = tf.data.Dataset.from_tensor_slices((X_test_reshaped, X_test_reshaped))

    train_dataset = train_dataset.map(utils.convert, num_parallel_calls=AUTOTUNE)
    train_dataset = train_dataset.cache().shuffle(buffer_size=3*BATCH_SIZE)
    train_dataset = train_dataset.batch(BATCH_SIZE)
    train_dataset = train_dataset.repeat()
    train_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)
    
    valid_dataset = valid_dataset.map(utils.convert, num_parallel_calls=AUTOTUNE)
    valid_dataset = valid_dataset.cache().shuffle(buffer_size=3*BATCH_SIZE)
    valid_dataset = valid_dataset.batch(BATCH_SIZE)
    valid_dataset = valid_dataset.repeat()
    valid_dataset = valid_dataset.prefetch(buffer_size=AUTOTUNE)

    test_dataset = test_dataset.map(utils.convert, num_parallel_calls=AUTOTUNE)
    test_dataset = test_dataset.cache()
    test_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)

    
#     print("train_dataset:", train_dataset)
#     print(train_dataset[0])
    output = list(train_dataset.take(1).as_numpy_iterator())

    print("@#@#@#@#@#@#@ 2")
    print("train_dataset:", train_dataset)
    print("len(output):", len(output))
    print("len(output[0]):", len(output[0]))
    x,y = output[0]
    print("Printing the output:", x.shape, y.shape)
    print("x:", x.min(), x.max())
    print("y:", y.min(), y.max())
    print(x.shape, y.shape)
    print("---------------------------------")
    
    
    # ARCHITECTURE
    complete_model = model.createModel(dims)
    print(complete_model.summary())

    complete_model.compile(optimizer='rmsprop', loss='mse')

    image = resize(plt.imread(X_test_paths[0])[:,:,:3], dims)
    print("Activation visualization image shape orig:", image.shape)
    image = np.expand_dims(image, 0)
    print("Activation visualization image shape extended:", image.shape)
    print("image:", image.min(), image.max())
#     image = X_test_reshaped[0:10]
#     print(image.shape)

    # Define the Activation Visualization callback
    output_dir = TENSORBOARD_LOG_DIR
    callbacks = [
        ActivationsVisualizationCallback(
            validation_data=(image,),
            layers_name=['conv2d_8'],
            output_dir=output_dir,
        ),
    ]

    # tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir="./tf_callback")
    tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=TENSORBOARD_LOG_DIR)

    complete_model.fit(train_dataset,
                       epochs=NUM_EPOCHS,
                       steps_per_epoch=len(X_train_paths) // BATCH_SIZE,
#                        validation_split=0.2,
                       validation_data=valid_dataset,
                       validation_steps=len(X_valid_paths) // BATCH_SIZE,
                       callbacks=[callbacks, tensorboard_callback, WandbCallback()],
#                        use_multiprocessing=True
                       )

    complete_model.save(OUTPUT_MODEL_PATH)

#     sys.exit(0)


    # ## Model Testing

    # Define the Activation Visualization explainer
    # index = np.random.randint(0, len(X_test_reshaped))
    # image = input_test[index].reshape((1, 32, 32, 3))
    # image = np.expand_dims(X_test_reshaped[index],0)
#     image = X_test[0][0][:10]
    image = np.array([resize(plt.imread(test_img_path)[:,:,:3], dims) for test_img_path in X_test_paths[:10]])
#     image = X_test_reshaped[:10]
    label = image
    print('val:', image.shape)

    data = ([image])
    explainer = ExtractActivations()

    layers_of_interest = ['conv2d_1', 'conv2d_2', 'conv2d_3', 'conv2d_4', 'conv2d_5', 'conv2d_6', 'conv2d_7', 'conv2d_8', 'conv2d_transpose', 'conv2d_transpose_1', 'conv2d_transpose_2', 'conv2d_transpose_3', 'conv2d_9', 'conv2d_transpose_4']
    for layer in layers_of_interest:
        grid = explainer.explain(validation_data=data, model=complete_model, layers_name=[layer])
        print(grid.shape)
        explainer.save(grid, ACTIVATION_IMG_PATH, '%s.png' % (layer))
Пример #31
0
print("| Test ratio: {}".format(testRatio))
print("| Slices per genre: {}".format(filesPerGenre))
print("| Slice size: {}".format(sliceSize))
print("--------------------------")

if "slice" in args.mode:
	createSlicesFromAudio()
	sys.exit()

#List genres
genres = os.listdir(slicesPath)
genres = [filename for filename in genres if os.path.isdir(slicesPath+filename)]
nbClasses = len(genres)

#Create model 
model = createModel(nbClasses, sliceSize)

if "train" in args.mode:

	#Create or load new dataset
	train_X, train_y, validation_X, validation_y = getDataset(filesPerGenre, genres, sliceSize, validationRatio, testRatio, mode="train")

	#Define run id for graphs
	run_id = "MusicGenres - "+str(batchSize)+" "+''.join(random.SystemRandom().choice(string.ascii_uppercase) for _ in range(10))

	#Train the model
	print("[+] Training the model...")
	model.fit(train_X, train_y, n_epoch=nbEpoch, batch_size=batchSize, shuffle=True, validation_set=(validation_X, validation_y), snapshot_step=100, show_metric=True, run_id=run_id)
	print("    Model trained! ✅")

	#Save trained model