Esempio n. 1
0
    def __init__(self):
        global model

        model = Sequential()
        model.add(
            Conv2D(64, (5, 5),
                   input_shape=(64, 64, 1),
                   activation='relu',
                   padding='same'))
        model.add(Conv2D(64, (5, 5), activation='relu'))
        model.add(MaxPooling2D((2, 2), strides=(2, 2)))
        model.add(Conv2D(128, (3, 3), activation='relu'))
        model.add(BatchNormalization())
        model.add(Conv2D(128, (3, 3), activation='relu'))
        model.add(MaxPooling2D((2, 2), strides=(2, 2)))
        model.add(Conv2D(512, (3, 3), activation='relu'))
        model.add(BatchNormalization())
        model.add(Conv2D(512, (3, 3), activation='relu'))
        model.add(BatchNormalization())
        model.add(Conv2D(512, (3, 3), activation='relu'))
        model.add(MaxPooling2D((2, 2), strides=(2, 2)))
        model.add(Flatten())
        model.add(Dense(256, activation='relu'))
        model.add(Dropout(0.4))
        model.add(Dense(256, activation='relu'))
        model.add(Dropout(0.25))
        model.add(Dense(10, activation='softmax'))
        model.load_weights('model/bangla_1.h5')
Esempio n. 2
0
class CNNMobiFallNet(object):
    def __init__(self,
                 n_timestamps,
                 n_features,
                 n_outputs,
                 pretrained_path=None):
        self.model = Sequential()
        self.model.add(
            TimeDistributed(Conv1D(filters=64,
                                   kernel_size=3,
                                   activation='relu'),
                            input_shape=(None, n_timestamps, n_features)))
        self.model.add(
            TimeDistributed(
                Conv1D(filters=64, kernel_size=3, activation='relu')))
        self.model.add(TimeDistributed(Dropout(0.5)))
        self.model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
        self.model.add(TimeDistributed(Flatten()))
        self.model.add(LSTM(100))
        self.model.add(Dropout(0.5))
        self.model.add(Dense(100, activation='relu'))
        self.model.add(Dense(n_outputs, activation='softmax'))

        if pretrained_path != None:
            self.model.load_weights(pretrained_path)
            print('Loading weights from {}'.format(pretrained_path))
        # optimizer = SGD(lr=0.01, momentum=0.9, nesterov=True)
        adam = Adam(lr=0.001)
        self.model.compile(loss='categorical_crossentropy',
                           optimizer=adam,
                           metrics=['accuracy'])

    def get_model(self):
        print(self.model.summary())
        return self.model
Esempio n. 3
0
    def __init__(self, restore=None, session=None, use_log=False):
        self.num_channels = 1
        self.image_size = 28
        self.num_labels = 10

        model = Sequential()
        model.add(Flatten(input_shape=(28, 28, 1)))
        model.add(Dense(1024))
        model.add(Lambda(lambda x: x * 10))
        model.add(Activation('softplus'))
        model.add(Lambda(lambda x: x * 0.1))
        model.add(Dense(10))
        # output log probability, used for black-box attack
        if use_log:
            model.add(Activation('softmax'))
        if restore:
            model.load_weights(restore)

        layer_outputs = []
        for layer in model.layers:
            if isinstance(layer, Conv2D) or isinstance(layer, Dense):
                layer_outputs.append(
                    K.function([model.layers[0].input], [layer.output]))

        self.layer_outputs = layer_outputs
        self.model = model
Esempio n. 4
0
 def __generator_cnn(self):
     model = Sequential([
         Dense(math.ceil(self.width / 8) * math.ceil(self.height / 8) * 256,
               input_shape=[100]),
         BatchNormalization(),
         LeakyReLU(),
         Reshape(
             (math.ceil(self.width / 8), math.ceil(self.height / 8), 256)),
         Conv2DTranspose(128, (5, 5),
                         strides=(2, 2),
                         padding='same',
                         use_bias=False,
                         output_padding=(self.height % 2, self.width % 2)),
         BatchNormalization(),
         LeakyReLU(),
         Conv2DTranspose(64, (5, 5),
                         strides=(2, 2),
                         padding='same',
                         use_bias=False,
                         output_padding=(self.height % 2, self.width % 2)),
         BatchNormalization(),
         LeakyReLU(),
         Conv2DTranspose(1, (5, 5),
                         strides=(2, 2),
                         padding='same',
                         use_bias=False,
                         activation='tanh',
                         output_padding=(self.height % 2, self.width % 2))
     ])
     info(model.summary())
     if os.path.exists(self.gen_output_model_path):
         info("Loading generator weights.")
         model.load_weights(self.gen_output_model_path)
     return model
def train(X, labels):
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        labels,
                                                        test_size=0.2,
                                                        random_state=0)

    mlb = MultiLabelBinarizer(classes=[
        'Heavy Metal', 'Thrash Metal', 'Power Metal', 'Folk Metal',
        'Progressive Metal', 'Death Metal', 'Doom Metal', 'Black Metal', 'Rock'
    ])
    y_train = [each for each in y_train]
    y_train = mlb.fit_transform(y_train)
    y_test = [each for each in y_test]
    y_test = mlb.fit_transform(y_test)

    model = Sequential()
    model.add(Dense(256, activation='relu', input_shape=(1305, )))
    model.add(Dropout(0.5))
    model.add(Dense(128, activation='relu'))
    model.add(Dense(9, activation='sigmoid'))

    mcp_save = ModelCheckpoint('best_mlp.h5',
                               save_best_only=True,
                               monitor='val_f1_m',
                               mode='max')
    reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss',
                                       factor=0.1,
                                       patience=7,
                                       verbose=1,
                                       epsilon=1e-4,
                                       mode='min')
    EarlyStopping(monitor='val_loss',
                  min_delta=0.001,
                  patience=5,
                  verbose=0,
                  mode='min')

    opt = Adam(lr=0.01)
    model.compile(optimizer=opt,
                  loss='categorical_crossentropy',
                  metrics=['acc', f1_m])

    model.fit(X_train,
              y_train,
              batch_size=64,
              epochs=50,
              verbose=2,
              validation_data=(X_test, y_test),
              callbacks=[mcp_save, reduce_lr_loss])

    model.load_weights('best_mlp.h5')

    preds = model.predict(X_test)
    preds[preds >= 0.5] = 1
    preds[preds < 0.5] = 0

    print(classification_report(y_test, preds))
    print(hamming_loss(y_test, preds))
    print(accuracy_score(y_test, preds))
 def build_model(self):
     model = Sequential()
     model.add(Dense(150, input_dim=self.states, activation='relu'))
     model.add(Dense(150, activation='relu'))
     model.add(Dense(150, activation='relu'))
     model.add(Dense(self.actions, activation='softmax'))
     model.compile(loss='mse', optimizer=Adam(lr=self.learning_rate))
     if self.load_weights:
         model.load_weights(self.weights_path)
     return model
Esempio n. 7
0
def init():
    global model, graph, session
    model = Sequential()
    model.add(Dense(32, activation='relu', input_shape=(6, )))
    model.add(Dense(32, activation='relu'))
    model.add(Dense(2, activation='linear'))
    model.compile(loss='mse', optimizer='adam', metrics=['mae'])
    model.load_weights("training_1/cp.ckpt")
    model._make_predict_function()
    session = K.get_session()
    graph = tf.get_default_graph()
Esempio n. 8
0
def index(request):
    prior = ResNet50(include_top=False,
                     weights='imagenet',
                     input_shape=(224, 224, 3))
    model = Sequential()
    model.add(prior)
    model.add(tf.keras.layers.GlobalAveragePooling2D(name='GA'))
    model.add(Flatten(name='Flatten'))
    model.add(Dense(1024, activation='linear', name='Dense1'))
    model.add(Dense(512, activation='linear', name='Dense2'))
    model.add(Dense(256, activation='linear', name='Dense3'))
    model.add(Dense(128, activation='linear', name='Dense4'))
    model.add(Dense(64, activation='linear', name='Dense5'))
    model.add(Dense(42, activation='softmax', name='Output'))
    opt = tf.keras.optimizers.RMSprop()
    model.compile(optimizer=opt,
                  loss='categorical_crossentropy',
                  metrics=[tf.keras.metrics.TopKCategoricalAccuracy(1)])
    model.load_weights(
        os.path.join(settings.BASE_DIR, 'weights', 'weights.hdf5'))

    if request.method == "POST":
        try:
            shutil.rmtree(os.path.join(settings.BASE_DIR, 'media'))
        except:
            pass
        f = request.FILES['sentFile']  # here you get the files needed
        response = {}
        file_name = "pic.jpg"
        name = default_storage.save(file_name, f)
        response['file_url'] = default_storage.url(name)

        datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1. /
                                                                  255)
        generator = datagen.flow_from_directory(os.path.join(
            settings.BASE_DIR, 'media'),
                                                target_size=(224, 224),
                                                batch_size=128,
                                                shuffle=False,
                                                class_mode=None)
        generator.reset()

        pred = model.predict(generator)
        label = np.argmax(pred[0])
        response['score'] = pred[0][label]

        detail = pd.read_csv(
            os.path.join(settings.BASE_DIR, 'category_detail', 'detail.csv'))
        response['name'] = detail['detail'][detail['code'] == label].values[0]

        return render(request, 'index.html', response)
    else:
        return render(request, 'index.html')
Esempio n. 9
0
 def discriminator(self):
     D = Sequential()
     D.add(Masking(mask_value=0, input_shape=(200, 20)))
     D.add(LSTM(20, return_sequences=True))
     D.add(LSTM(20))
     D.add(Dense(1, activation='sigmoid'))
     if self.load_weights:
         D.load_weights('current_best_classifier.hdf5')
     D.compile(optimizer=Adam(learning_rate=0.0001),
               loss='binary_crossentropy',
               metrics=['accuracy'])
     return D
def model(output, checkpoint):
    
    '''
    This function contains deep learning model consisting of CNN and LSTM layers. CNN is used for extracting useful
    features from the image pixels and LSTM is used to remember sequence of frames.
    input shape : (number of frames, image width, image height, channel) - eg. (12,200,200,3)
    output : number of classes - (no of exercise that we wish to classify)
    checkpoint : relative path of checkpoint file (.pkl)
    '''

    model = Sequential()

    model.add(TimeDistributed(Conv2D(16, (5, 5)), input_shape=shape))
    model.add(TimeDistributed(LeakyReLU(alpha=0.05)))
    model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(4, 4), padding='same')))

    model.add(TimeDistributed(Conv2D(32, (3,3), padding='same')))
    model.add(TimeDistributed(LeakyReLU(alpha=0.05)))
    model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))

    model.add(TimeDistributed(Conv2D(64, (3,3), padding='same')))
    model.add(TimeDistributed(LeakyReLU(alpha=0.05)))
    model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))

    model.add(TimeDistributed(Conv2D(128, (3,3), padding='same')))
    model.add(TimeDistributed(LeakyReLU(alpha=0.05)))
    model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))

    model.add(TimeDistributed(Conv2D(64, (3,3), padding='same')))
    model.add(TimeDistributed(LeakyReLU(alpha=0.05)))
    model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))

    model.add(TimeDistributed(Conv2D(32, (3,3), padding='same')))
    model.add(TimeDistributed(LeakyReLU(alpha=0.05)))
    model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))


    # extract features and dropout 
    model.add(TimeDistributed(Flatten()))
    model.add(Dropout(0.3))

    # input to LSTM
    model.add(LSTM(256, return_sequences=False, dropout=0.3))

    # classifier with sigmoid activation for multilabel
    model.add(Dense(output, activation='softmax'))
    
    model.load_weights(checkpoint)
    
    optimizer = optimizers.Adam(0.0005)
    model.compile(optimizer,'categorical_crossentropy', metrics=['acc'] )
    
    return model
Esempio n. 11
0
def main_thread():
    global power

    # FCN
    labels = [i for i in range(19)]
    model = Sequential()
    model.add(layers.Dense(40, activation='relu'))
    model.add(layers.Dense(50, activation='relu'))
    model.add(layers.Dense(40, activation='relu'))
    model.add(layers.Dense(30, activation='relu'))
    model.add(layers.Dense(len(labels), activation='softmax'))
    model.build(input_shape=(None, 40))

    model.load_weights('./weights/my_model.h5')

    while True:
        # print('show')
        # Load signals file
        AMOUNT = 200
        time, data = getSignalLogs(amount=AMOUNT)
        # plot(time, data)

        # Do the bias and absolution
        BIAS = 173.5
        data = np.abs(data - BIAS)

        # Grouping
        THRESHOLD = 4
        STEP_NUM = 5
        PACK_NUM = 30
        low_samples, high_samples = np.copy(data), np.copy(data)
        high_samples[data <= THRESHOLD] = 0
        low_samples[data > THRESHOLD] = 0

        FIX_INPUT_NUMBER = 40
        groups = list()
        for ginx in range(0, len(data) - PACK_NUM, STEP_NUM):
            groups.append(
                np.mean(
                    np.convolve(low_samples[ginx:ginx + PACK_NUM],
                                high_samples[ginx:ginx + PACK_NUM])))
        if len(groups) < (FIX_INPUT_NUMBER * 0.7):
            continue
        else:
            groups = np.array(
                groups + [0 for i in range(FIX_INPUT_NUMBER - len(groups))])

        p = model.predict(np.array([
            groups,
        ])).tolist()[0]
        power = 9 - abs(p.index(max(p)) - 9)
        print(power)
Esempio n. 12
0
class Predictor:
    def __init__(self, model_name):
        with open(f'models/{model_name}/class_names.txt',
                  'r',
                  encoding='utf-8') as f:
            self.class_names = f.readline().rstrip().split(',')

        self.cleaner = Cleaner()

        self.model = None
        self.load_model(model_name)

    def predict(self, text_list):
        """return list of class predictions based on list of strings"""
        if not text_list or text_list == ['']:
            return None
        text_list = [self.cleaner.clean_text(text) for text in text_list]
        prediction_result = self.model.predict(text_list)
        probabilities = [(key, sum(map(itemgetter(i), prediction_result)))
                         for i, key in enumerate(self.class_names)]
        return sorted(probabilities, key=itemgetter(1), reverse=True)

    def load_model(self, model_name):
        try:
            with open(f'models/{model_name}/params.txt', 'r') as f:
                max_features = int(f.readline())
                sequence_length = int(f.readline())
                embedding_dim = int(f.readline())
        except FileNotFoundError:
            max_features = 20000
            sequence_length = 40
            embedding_dim = 160

        vectorize_layer = TextVectorization(
            max_tokens=max_features,
            output_mode='int',
            output_sequence_length=sequence_length)

        # ATTENTION: this model MUST be absolutely
        # identical to the original one
        self.model = Sequential([
            vectorize_layer,
            Sequential([
                layers.Embedding(max_features + 1, embedding_dim),
                layers.Dropout(0.3),
                layers.GlobalAveragePooling1D(),
                layers.Dropout(0.3),
                layers.Dense(len(self.class_names))
            ])
        ])
        self.model.load_weights(f'models/{model_name}/checkpoint')
        self.model.predict(["define", "input", "shape"])
Esempio n. 13
0
class CartPoleModel:

    def __init__(self):
        self.model = Sequential()
        self.model.add(Dense(24, activation="relu", input_shape=(4,)))
        self.model.add(Dense(24, activation="relu"))
        self.model.add(Dense(2))
        self.model.compile(optimizer='nadam', loss='mse')
        self.history = deque(maxlen=10000)

    def get_action(self, state, epsilon=0, verbose=0):
        if random.random() < epsilon:
            chosen_action = random.choice((0, 1))
            if verbose: print(state, '!!', chosen_action)
            return chosen_action

        q_values = self.model.predict(np.reshape(state, [1, 4]))[0]
        chosen_action = np.argmax(q_values)
        if verbose: print(state, '=>', chosen_action, q_values)
        return chosen_action

    def remember(self, entry):
        self.history.append(entry)

    def load(self):
        try:
            self.model.load_weights('cartpole_model.hd5')
            print('Loaded!')
        except Exception as e:
            print(e)

    def save(self):
        self.model.save_weights('cartpole_model.hd5')

    def learn(self, batch_size=64, discount=.9, verbose=0):
        if len(self.history) < batch_size: return
        batch = random.sample(self.history, batch_size)

        foo = zip(*batch)
        states, actions, rewards, state_nexts, dones = foo
        states = np.reshape(states, [batch_size, 4])
        state_nexts = np.reshape(state_nexts, [batch_size, 4])
        state_preds = self.model.predict(states)
        state_next_preds = self.model.predict(state_nexts)

        for idx, action in enumerate(actions):
            q_learned = rewards[idx]
            if not dones[idx]:
                q_learned += discount * np.amax(state_next_preds[idx])
            state_preds[idx][action] = q_learned
            
        self.model.fit(states, state_preds, verbose=verbose)
Esempio n. 14
0
 def __generator(self):
     model = Sequential([
         LSTM(4, input_shape=[32, 32]),
         BatchNormalization(),
         LeakyReLU(),
         Dense(self.width * self.height * self.channels, activation='tanh'),
         Reshape((self.width, self.height, self.channels))
     ])
     if os.path.exists(self.gen_output_model_path):
         model.build()
         info("Loading generator weights.")
         model.load_weights(self.gen_output_model_path)
     return model
Esempio n. 15
0
 def __generator_nn(self):
     model = Sequential([
         Dense(1024, input_shape=[100]),
         BatchNormalization(),
         LeakyReLU(),
         Dense(256, activation='relu'),
         Dense(self.width * self.height * self.channels, activation='tanh'),
         Reshape((self.width, self.height, self.channels))
     ])
     info(model.summary())
     if os.path.exists(self.gen_output_model_path):
         info("Loading generator weights.")
         model.load_weights(self.gen_output_model_path)
     return model
Esempio n. 16
0
    def load(self, batch_size=16, filename=None):
        if filename is None:
            filename = f"model.MNIST.h5"

        f = hw4_utils.find_file(filename)

        self.build()

        model = Sequential(self.layers)
        model.build(input_shape=(batch_size, 28, 28, 1))
        model.load_weights(f)
        self.layers = model.layers

        self._define_ops()
Esempio n. 17
0
    def __init__(self,
                 restore=None,
                 session=None,
                 use_log=False,
                 use_brelu=False):
        def bounded_relu(x):
            return K.relu(x, max_value=1)

        if use_brelu:
            activation = bounded_relu
        else:
            activation = 'relu'
        self.num_channels = 1
        self.image_size = 28
        self.num_labels = 10

        model = Sequential()

        model.add(Conv2D(32, (3, 3), input_shape=(28, 28, 1)))
        model.add(Activation(activation))
        model.add(Conv2D(32, (3, 3)))
        model.add(Activation(activation))
        model.add(MaxPooling2D(pool_size=(2, 2)))

        model.add(Conv2D(64, (3, 3)))
        model.add(Activation(activation))
        model.add(Conv2D(64, (3, 3)))
        model.add(Activation(activation))
        model.add(MaxPooling2D(pool_size=(2, 2)))

        model.add(Flatten())
        model.add(Dense(200))
        model.add(Activation(activation))
        model.add(Dense(200))
        model.add(Activation(activation))
        model.add(Dense(10))
        # output log probability, used for black-box attack
        if use_log:
            model.add(Activation('softmax'))
        if restore:
            model.load_weights(restore)

        layer_outputs = []
        for layer in model.layers:
            if isinstance(layer, Conv2D) or isinstance(layer, Dense):
                layer_outputs.append(
                    K.function([model.layers[0].input], [layer.output]))

        self.model = model
        self.layer_outputs = layer_outputs
Esempio n. 18
0
File: views.py Progetto: Mryao1/plh
    def load(self):
        print('bigan load model')
        #module = hub.Module(r"E:\Deep learning\pest\model\inception_model")
        module = hub.Module('/root/inception_model')

        resnet = hub.KerasLayer(module, input_shape=(299, 299, 3), output_shape=[2048])
        model = Sequential([
            resnet,
            Dense(5, activation=softmax)
        ])
        model.compile(optimizer='adam', loss=sparse_categorical_crossentropy, metrics=['accuracy'])
        model.summary()
        model.load_weights('/root/inceptionv16_wights.keras')

        return model
Esempio n. 19
0
def LSTM(lstm_pretrained=True,
         lstm_pretrained_fname='lstm5_epch240_tts10_bs1000_129_17759.h5'):
    """
    If using a pretrained model, 
    set lstm_pretrained = True, 
    and set lstm_pretrained_fname to the pretrained weights.
    """
    RNN = Sequential([
        LSTM(90, return_sequences=True, input_shape=(7, 1)),
        Attention(return_sequences=True),
        LSTM(90),
        Dense(1, activation=None)
    ])
    if lstm_pretrained == True:
        RNN.load_weights(pretrained_fname)
    return RNN
Esempio n. 20
0
    def transfer_model(self,nclasses1,nclasses2,path,name="EfficientNetB6",pool="gem",suffix = ""):
        inp = layers.Input(shape=(self.height,self.width, 3),name="input")
        pretrained_model = models[name](weights = None, include_top = False,
                         input_shape = [self.height,self.width, 3], input_tensor = inp)
        model = Sequential([
            inp, pretrained_model,self.poolings[pool],
            layers.Dense(self.embed_size, activation=None, kernel_initializer="glorot_normal",
                        dtype=tf.float32,name = "feature"+suffix),
            ArcFace(nclasses1,dtype=tf.float32,name = "ArcFace"+suffix)
        ])
        model.load_weights(path)

        model = Sequential([
            *model.layers[:-1],
            ArcFace(nclasses2,dtype=tf.float32,name = "ArcFace"+suffix)
        ],name = "%s_%s_ArcFace_more"%(name,pool))
        return model
Esempio n. 21
0
def load_parametric_model(activation, optimizer, lr, n_hidden_layers,
                          n_weights, batch_size, n_epochs):

    model = Sequential()
    model.add(Dense(10, input_shape=(5, )))
    for i in range(n_hidden_layers):
        model.add(Dense(n_weights, activation=activation))
    model.add(Dense(5))
    model.compile(loss='mse', optimizer=optimizer(lr=lr), metrics=['mape'])

    if os.path.isfile('data/keras_model.index'):
        print("Keras model weights loaded")
        model.load_weights('data/keras_model')
    else:
        print("Keras model not found!")

    return model
Esempio n. 22
0
def get_model():

    model = Sequential([
        Embedding(input_dim=vocab_size,
                  output_dim=emb_dim,
                  input_length=max_sent_len,
                  trainable=False),
        Bidirectional(
            LSTM(64, return_sequences=False, recurrent_activation='sigmoid')),
        Dropout(0.5),
        Dense(2, activation='softmax')
    ])
    model.load_weights(WEIGHTS_PATH)
    model.compile(loss='sparse_categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Esempio n. 23
0
 def __discriminator(self, path):
     model = Sequential([
         GaussianNoise(1.5,
                       input_shape=(self.width, self.height,
                                    self.channels)),
         Conv2D(filters=128, kernel_size=(3, 3), padding='same'),
         LeakyReLU(alpha=0.2),
         MaxPool2D(2, 2),
         Dropout(0.25),
         Flatten(),
         Dense(255, activation='relu'),
         Dense(1, activation='sigmoid')
     ])
     if os.path.exists(path):
         info("Loading discriminator weights.")
         model.load_weights(path)
     return model
Esempio n. 24
0
 def __generator(self, path):
     model = Sequential([
         Conv2D(filters=128,
                kernel_size=(5, 5),
                strides=(3, 3),
                padding='same',
                input_shape=(self.width, self.height, self.channels)),
         LeakyReLU(alpha=0.2),
         MaxPool2D(2, 2),
         BatchNormalization(momentum=0.8),
         Flatten(),
         Dense(5012, activation='relu'),
         Dense(self.width * self.height * self.channels, activation='tanh'),
         Reshape((self.width, self.height, self.channels))
     ])
     if os.path.exists(path):
         info("Loading generator weights.")
         model.load_weights(path)
     return model
Esempio n. 25
0
def load_parametric_model_avg(activation,
        optimizer, lr, n_hidden_layers, n_weights, batch_size, n_epochs, input_shape):

    model = Sequential()
    for i in range(n_hidden_layers):
        if i==0:
            model.add(Dense(n_weights, activation=activation, input_shape=(input_shape,)))
        else:
            model.add(Dense(n_weights, activation=activation))
    model.add(Dense(9))
    model.compile(loss='mse', optimizer=optimizer(lr=lr), metrics=['mape'])

    if os.path.isfile('../data/keras_model_avg_best.index'):
        print ("Best Keras model weights loaded")
        model.load_weights('../data/keras_model_avg_best')
    elif os.path.isfile('../data/keras_model.index'):
        print ("Keras model weight loaded")
        modal.load_weights('../data/keras_model')
    else: 
        print ("Keras model not found!")

    return model
Esempio n. 26
0
def train(X, labels):
    X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.2, random_state=0)

    encoder = LabelEncoder()
    y_train = encoder.fit_transform(y_train)
    y_test = encoder.transform(y_test)

    onehot = OneHotEncoder(sparse=False)
    y_train = onehot.fit_transform(y_train.reshape(len(y_train), 1))
    y_test_oh = onehot.transform(y_test.reshape(len(y_test), 1))

    class_weights = [class_freq[label] for label in encoder.classes_]
    model = Sequential()
    model.add(Dense(256, activation='relu', input_shape=(1305,)))
    model.add(Dropout(0.5))
    model.add(Dense(128, activation='relu'))
    model.add(Dense(9, activation='softmax'))

    mcp_save = ModelCheckpoint('best_mlp.h5', save_best_only=True, monitor='val_f1_m', mode='max')
    reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=7, verbose=1, epsilon=1e-4, mode='min')
    EarlyStopping(monitor='val_loss', min_delta=0.001, patience=5, verbose=0, mode='min')

    opt = Adam(lr=0.01)
    model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['acc', f1_m])

    model.fit(X_train, y_train, batch_size=64, epochs=50, verbose=2,
              validation_data=(X_test, y_test_oh), callbacks=[mcp_save, reduce_lr_loss],
              class_weight=class_weights)

    model.load_weights('best_mlp.h5')

    preds = model.predict(X_test)
    preds = np.argmax(preds, axis=1)

    score_macro = f1_score(y_test, preds, average="macro")
    score_micro = f1_score(y_test, preds, average="micro")
    print("F1_macro:{0}, F1_micro:{1}".format(score_macro, score_micro))
    print(classification_report(y_test, preds))
Esempio n. 27
0
class MobiFallNet(object):
    def __init__(self, input_shape, n_outputs, pretrained_path=None):
        self.model = Sequential()
        self.model.add(
            LSTM(100, input_shape=input_shape, return_sequences=True))
        self.model.add(Dropout(0.2))
        self.model.add(LSTM(100))
        self.model.add(Dropout(0.2))
        self.model.add(Dense(100, activation='relu'))
        self.model.add(Dense(n_outputs, activation='softmax'))

        if pretrained_path != None:
            self.model.load_weights(pretrained_path)
            print('Loading weights from {}'.format(pretrained_path))
        optimizer = SGD(lr=0.001, momentum=0.9, nesterov=True)
        # adam = Adam(lr=0.001)
        self.model.compile(loss='categorical_crossentropy',
                           optimizer=optimizer,
                           metrics=['accuracy'])

    def get_model(self):
        print(self.model.summary())
        return self.model
Esempio n. 28
0
def getModel():
    #forming model
    model = Sequential()
    #adding layers and forming the model
    model.add(
        Conv2D(64,
               kernel_size=5,
               strides=1,
               padding="Same",
               activation="relu",
               input_shape=(40, 5, 1)))
    model.add(MaxPooling2D(padding="same"))
    model.add(
        Conv2D(128,
               kernel_size=5,
               strides=1,
               padding="same",
               activation="relu"))
    model.add(MaxPooling2D(padding="same"))
    model.add(Dropout(0.3))
    model.add(Flatten())
    model.add(Dense(256, activation="relu"))
    model.add(Dropout(0.3))
    model.add(Dense(512, activation="relu"))
    model.add(Dropout(0.3))
    model.add(Dense(5, activation="softmax"))
    # model.add(Dense(10,activation="softmax"))
    # model.add(Dense(11,activation="softmax"))
    #compiling
    model.compile(optimizer="adam",
                  loss="categorical_crossentropy",
                  metrics=["accuracy"])
    #loading
    # model.load_weights('../Classifier/modelv1.hdf5')
    # model.load_weights('../Classifier/modelv0.hdf5')
    model.load_weights('../Classifier/modelv2.hdf5')
    return model
Esempio n. 29
0
class FacialExpressionModel(object):
    emotion_dict = [
        "Angry", "Disgusted", "Fearful", "Happy", "Neutral", "Sad", "Surprised"
    ]

    def __init__(self, model_weights_file):
        self.loaded_model = Sequential()

        self.loaded_model.add(
            Conv2D(32,
                   kernel_size=(3, 3),
                   activation='relu',
                   input_shape=(48, 48, 1)))
        self.loaded_model.add(Conv2D(64, kernel_size=(3, 3),
                                     activation='relu'))
        self.loaded_model.add(MaxPooling2D(pool_size=(2, 2)))
        self.loaded_model.add(Dropout(0.25))

        self.loaded_model.add(
            Conv2D(128, kernel_size=(3, 3), activation='relu'))
        self.loaded_model.add(MaxPooling2D(pool_size=(2, 2)))
        self.loaded_model.add(
            Conv2D(128, kernel_size=(3, 3), activation='relu'))
        self.loaded_model.add(MaxPooling2D(pool_size=(2, 2)))
        self.loaded_model.add(Dropout(0.25))

        self.loaded_model.add(Flatten())
        self.loaded_model.add(Dense(1024, activation='relu'))
        self.loaded_model.add(Dropout(0.5))
        self.loaded_model.add(Dense(7, activation='softmax'))
        self.loaded_model.load_weights(model_weights_file)
        self.loaded_model._make_predict_function()

    def predict_emotion(self, img):
        self.preds = self.loaded_model.predict(img)
        return FacialExpressionModel.emotion_dict[int(np.argmax(self.preds))]
def build_advanced_net(model_weights=None,
                       image_size: int = 224,
                       classes: int = 7) -> Sequential:

    conv_base = VGG16(include_top=False,
                      weights='imagenet',
                      input_shape=(image_size, image_size, 3))
    for layer in conv_base.layers[:-2]:
        layer.trainable = False
    model = Sequential()
    model.add(conv_base)
    model.add(GlobalAveragePooling2D())
    model.add(Dense(512, activation='relu'))
    model.add(Dense(256, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(64, activation='relu'))
    model.add(BatchNormalization())
    model.add(Dense(32, activation='relu'))
    model.add(Dense(16, activation='relu'))
    model.add(Dense(classes, activation='softmax'))
    if model_weights is not None:
        model.load_weights(model_weights)

    return model