Пример #1
0
#! /usr/bin/python2
# -*- coding: utf-8 -*-

import os
import sys
import pickle
import datetime

import numpy as np

# Import keras + tensorflow without the "Using XX Backend" message
stderr = sys.stderr
sys.stderr = open(os.devnull, 'w')
import tensorflow as tf
import plaidml.keras as keras
keras.install_backend()
from keras.models import Sequential, Model, load_model
from keras.layers import Input, Activation, Add, Concatenate, Multiply
from keras.layers import BatchNormalization, LeakyReLU, PReLU, Conv2D, Dense
from keras.layers import UpSampling2D, Lambda, Dropout
from keras.optimizers import Adam
from keras.applications.vgg19 import preprocess_input
from keras.utils.data_utils import OrderedEnqueuer
from keras import backend as K
from keras.callbacks import TensorBoard, ModelCheckpoint, LambdaCallback

sys.stderr = stderr
from vgg19_noAct import VGG19
from util import DataLoader, plot_test_images, plot_bigger_images

Пример #2
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jul  7 11:28:12 2019

@author: nigelstory
"""

import sys
import plaidml.keras as pk
pk.install_backend()
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, Activation
from keras.layers.normalization import BatchNormalization
from keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, EarlyStopping
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
from datetime import datetime

s = datetime.now()

# Initialize CNN
classifier = Sequential()

# Adding layers

# 1st convolution
classifier.add(
    Conv2D(filters=64, kernel_size=(3, 3), input_shape=(128, 128, 3)))
classifier.add(BatchNormalization(axis=-1))
classifier.add(Activation('relu'))
Пример #3
0
#!/usr/bin/env python

from time import time

from numpy import repeat
from plaidml.keras import install_backend

# Install the plaidml backend
install_backend()

from keras.applications import VGG19
from keras.datasets import cifar10

(x_train, y_train_cats), (x_test, y_test_cats) = cifar10.load_data()
batch_size = 8
x_train = x_train[:batch_size]
x_train = repeat(repeat(x_train, 7, axis=1), 7, axis=2)
model = VGG19()
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])

print("Running initial batch (compiling tile program)")
y = model.predict(x=x_train, batch_size=batch_size)

# Now start the clock and run 10 batches
print("Timing inference...")
start = time()
for i in range(10):
    _ = model.predict(x=x_train, batch_size=batch_size)
print("Ran in {} seconds".format(time() - start))
Пример #4
0
def __get_keras():
    import os
    import logging

    if 'KERAS_BACKEND' in os.environ:
        try:
            import keras
            return keras
        except ImportError as ie:
            logging.info('Keras Backend {} Not Found: {}'.format(
                os.environ['KERAS_BACKEND'], str(ie)))
            del os.environ['KERAS_BACKEND']

    try:
        import plaidml
        from plaidml.keras import install_backend
        install_backend()

        import keras
        return keras
    except ImportError as ie:
        logging.info('No PlaidML Keras Found: {}'.format(str(ie)))

    try:
        import mxnet
        os.environ['KERAS_BACKEND'] = 'mxnet'

        import keras
        return keras
    except ImportError as ie:
        if 'KERAS_BACKEND' in os.environ:
            del os.environ['KERAS_BACKEND']
        logging.info('No MXNet Keras Found: {}'.format(str(ie)))

    try:
        import cntk
        os.environ['KERAS_BACKEND'] = 'cntk'

        import keras
        return keras
    except ImportError as ie:
        if 'KERAS_BACKEND' in os.environ:
            del os.environ['KERAS_BACKEND']
        logging.info('No CNTK Keras Found: {}'.format(str(ie)))

    try:
        import theano
        os.environ['KERAS_BACKEND'] = 'theano'

        import keras
        return keras
    except ImportError as ie:
        if 'KERAS_BACKEND' in os.environ:
            del os.environ['KERAS_BACKEND']
        logging.info('No Theano Keras Found: {}'.format(str(ie)))

    try:
        from tensorflow import keras
        return keras
    except ImportError as ie:
        logging.info('No Tensorflow Keras Found: {}'.format(str(ie)))

    logging.error('No Keras Backends Found')

    return {}