for i in range(9): img = load_img(images[i]) plt.subplot(gs[i]) plt.imshow(img, aspect='auto') plt.axis("off") plt.savefig(result_images) shutil.rmtree(temp_dir) # In[11]: #重みvをimagenetとすると、学習済みパラメータを初期値としてXceptionを読み込む。 #model_load() base_model = Xception(weights='imagenet', include_top=False, input_tensor=Input(shape=(img_size, img_size, 3))) #base_model.summary() x = base_model.output #入力を平滑化 x = Flatten()(x) #過学習防止 x = Dropout(.8)(x) # In[12]: #model_build() # 最後の全結合層の出力次元はクラスの数(= mizumashi_generator.num_class) predictions = Dense(mizumashi_generator.num_classes, kernel_initializer='glorot_uniform', activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions)
def create_architecture(map_table, output_dim=1, output_activation=None): """ (conv -> pool) * 2 -> FC -> concat -> FC -> FC -> output. Parameters ---------- map_table: pd.DataFrame PLINK's map file loaded as DataFrame. output_dim: int Output dimension (that would depend on the choice of your loss function) output_activation: str or keras.activations activation type to apply on the output layer Returns ------- Model: keras model. """ inputs = { chr_num: Input(shape=(1, LATENT_DIM, df.shape[0]), name="input_chr{:02}".format(chr_num)) for chr_num, df in map_table.groupby("chr") } # First convolution swipes the 4-dimensional embedding of the data in order to output a vector rather than a matrix: conv_1 = { chr_num: Conv2D(filters=32, kernel_size=(LATENT_DIM, 2048), use_bias=True, strides=1, data_format="channels_first", activation="relu", name="conv_1_chr{:02}".format(chr_num))(input_layer) for chr_num, input_layer in inputs.items() } # Discard redundant dimensions (since kernel_size[0] equal to data dimension the output dim is explicit 1, # On the way of doing that (technical reasons) transpose the result so that the channels will be the last dimension: reshape = { chr_num: Reshape( (conv_layer.shape[3].value, conv_layer.shape[1].value))(conv_layer) for chr_num, conv_layer in conv_1.items() } pool_1 = { chr_num: MaxPooling1D(pool_size=2, name="maxpool_1_chr{:02}".format(chr_num))(conv_layer) for chr_num, conv_layer in reshape.items() } conv_2 = { chr_num: Conv1D(filters=64, kernel_size=1064, use_bias=True, strides=1, activation="relu", name="conv_2_chr{:02}".format(chr_num))(pool_layer) for chr_num, pool_layer in pool_1.items() } pool_2 = { chr_num: MaxPooling1D(pool_size=2, name="maxpool_2_chr{:02}".format(chr_num))(conv_layer) for chr_num, conv_layer in conv_2.items() } flatten = { chr_num: Flatten(name="flatten_chr{:02}".format(chr_num))(pool_layer) for chr_num, pool_layer in pool_2.items() } dense_1 = { chr_num: Dense(units=256, activation="relu", name="dense_1_chr{:02}".format(chr_num))(flat_layer) for chr_num, flat_layer in flatten.items() } merge = concatenate(list(dense_1.values()), name="concat") dense_2 = Dense(units=4196, activation="relu", name="dense_merged_1")(merge) dense_3 = Dense(units=1024, activation="relu", name="dense_merged_2")(dense_2) dropout_1 = Dropout(0.4, name="droupout_merge_1")(dense_3) output = Dense(units=output_dim, activation=output_activation, name="output")(dropout_1) # Verify that the chromosome input is defined in an ordered fashion: model = Model( inputs=[inputs[chr_num] for chr_num in sorted(list(inputs.keys()))], outputs=output) return model
plt.subplot(224) plt.imshow(x_train[42000][:,:,0]) plt.show() # BUILD THE MODEL # # ================= ############# # # Encoder #Let us define 4 conv2D, flatten and then dense # # ================= ############ latent_dim = 2 # Number of latent dim parameters input_img = Input(shape=input_shape, name='encoder_input') x = Conv2D(32, 3, padding='same', activation='relu')(input_img) x = Conv2D(64, 3, padding='same', activation='relu',strides=(2, 2))(x) x = Conv2D(64, 3, padding='same', activation='relu')(x) x = Conv2D(64, 3, padding='same', activation='relu')(x) conv_shape = K.int_shape(x) #Shape of conv to be provided to decoder #Flatten x = Flatten()(x) x = Dense(32, activation='relu')(x) # Two outputs, for latent mean and log variance (std. dev.) #Use these to sample random variables in latent space to which inputs are mapped. z_mu = Dense(latent_dim, name='latent_mu')(x) #Mean values of encoded input z_sigma = Dense(latent_dim, name='latent_sigma')(x) #Std dev. (variance) of encoded input
def create_model(nb_classes, input_shape, config=None): """Create a VGG-16 like model.""" if len(input_shape) != 3: raise Exception("Input shape should be a tuple (nb_channels, nb_rows, " "nb_cols) or (nb_rows, nb_cols, nb_channels), " "depending on your backend.") if config is None: config = {'model': {}} min_feature_map_dimension = min(input_shape[:2]) if min_feature_map_dimension < 32: print("ERROR: Please upsample the feature maps to have at least " "a size of 32 x 32. Currently, it has {}".format(input_shape)) nb_filter = 32 # Network definition # input_shape = (None, None, 3) # for fcn input_ = Input(shape=input_shape) x = input_ # Scale feature maps down to [63, 32] x [63, 32] tmp = min_feature_map_dimension / 32. if tmp >= 2: while tmp >= 2.: for _ in range(2): x = Convolution2D(nb_filter, (3, 3), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = BatchNormalization()(x) x = Activation('elu')(x) x = MaxPooling2D(pool_size=(2, 2))(x) nb_filter *= 2 tmp /= 2 # 32x32 x = Convolution2D(nb_filter, (3, 3), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = BatchNormalization()(x) x = Activation('elu')(x) x = Convolution2D(nb_filter, (3, 3), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = BatchNormalization()(x) x = Activation('elu')(x) # 16x16 x = MaxPooling2D(pool_size=(2, 2))(x) x = Convolution2D(2 * nb_filter, (3, 3), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = BatchNormalization()(x) x = Activation('elu')(x) x = Convolution2D(2 * nb_filter, (3, 3), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = BatchNormalization()(x) x = Activation('elu')(x) # 8x8 x = MaxPooling2D(pool_size=(2, 2))(x) x = Convolution2D(2 * nb_filter, (3, 3), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = BatchNormalization()(x) x = Activation('elu')(x) x = Convolution2D(2 * nb_filter, (3, 3), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = BatchNormalization()(x) x = Activation('elu')(x) # 4x4 x = MaxPooling2D(pool_size=(2, 2))(x) x = Convolution2D(495, (4, 4), padding='valid', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = BatchNormalization()(x) x = Activation('elu')(x) x = Dropout(0.5)(x) # 1x1 x = Convolution2D(494, (1, 1), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = BatchNormalization()(x) x = Activation('elu')(x) x = Dropout(0.5)(x) x = Convolution2D(nb_classes, (1, 1), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))(x) x = GlobalAveragePooling2D()(x) # Adjust for FCN x = BatchNormalization()(x) x = Activation('softmax')(x) model = Model(inputs=input_, outputs=x) return model
(x_train, y_train), (x_test, y_test) = cifar10.load_data() x_train = x_train.astype('float32') x_test = x_test.astype('float32') mean = np.mean(x_train) std = np.std(x_train) x_train = (x_train - mean) / (std) x_test = (x_test - mean) / (std) # one hot encode target values y_train = to_categorical(y_train) y_test = to_categorical(y_test) #FINAL TABLE IMPLEMENTATION!!!!!!!!!!! input_img = Input(shape=(32, 32, 3)) print(input_img.shape) #layer0 = Conv2D(8, (3,3),strides=(2,2) ,padding='valid' ,activation='relu')(input_img) #layer1 = Conv2D(8, (3,3), padding='valid', activation='relu')(input_img) #layer2 = Conv2D(8, (3,3), padding='same', activation='relu')(layer1) #layer3 = MaxPooling2D((3,3), strides=(2,2), padding='same')(input_img) #layer4 = Conv2D(8, (3,3), padding='valid', activation='relu')(layer1) #layer5 = Conv2D(8, (3,3), strides=(2,2), padding='valid', activation='relu')(input_img) #layer6 = Conv2D(8, (3,3), padding='valid', activation='relu')(layer2) #layer7 =Input(shape=(32, 32, 3)) #3 x fig 5 #layer70 = Conv2D(8, (1,1), padding='same', activation='relu')(layer7) #layer71 = Conv2D(8, (3,3), padding='same', activation='relu')(layer70) #layer72 = Conv2D(8, (3,3), padding='same', activation='relu')(layer71) #layer73 = Conv2D(8, (1,1), padding='same', activation='relu')(layer7) #layer74 = Conv2D(8, (3,3), padding='same', activation='relu')(layer73)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=RNG_SEED) Q1_train = X_train[:,0] Q2_train = X_train[:,1] Q1_test = X_test[:,0] Q2_test = X_test[:,1] #Defining the model embedding_layer = Embedding(len(word_index) + 1, 300, weights=[embedding_matrix], input_length=30, trainable=False) lstm_layer = LSTM(200, dropout=0.20, recurrent_dropout=0.20) sequence_1_input = Input(shape=(30,), dtype='int32') embedded_sequences_1 = embedding_layer(sequence_1_input) x1_layer = lstm_layer(embedded_sequences_1) sequence_2_input = Input(shape=(30,), dtype='int32') embedded_sequences_2 = embedding_layer(sequence_2_input) y1_layer = lstm_layer(embedded_sequences_2) merged = concatenate([x1_layer, y1_layer]) merged = Dropout(0.25)(merged) merged = BatchNormalization()(merged) merged = Dense(300, activation='relu')(merged) merged = Dropout(0.25)(merged) merged = BatchNormalization()(merged)
if len(embedding_matrix[i]) == len(embedding_vector): embedding_matrix[i] = embedding_vector # load pre-trained word embeddings into an Embedding layer # note that we set trainable = False so as to keep the embeddings fixed embedding_layer = Embedding(num_words, EMBEDDING_DIM, weights=[embedding_matrix], input_length=MAX_SEQUENCE_LENGTH, # trainable=False ) print('Training model.') # import pdb; pdb.set_trace() # train a 1D convnet with global maxpooling sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32') embedded_sequences = embedding_layer(sequence_input) x = Conv1D(128, 5, activation='relu')(embedded_sequences) x = MaxPooling1D(5)(x) x = Conv1D(128, 5, activation='relu')(x) x = MaxPooling1D(5)(x) x = Conv1D(128, 5, activation='relu')(x) x = MaxPooling1D(35)(x) x = Flatten()(x) x = Dense(128, activation='relu')(x) preds = Dense(len(unique_emojis), activation='softmax')(x) model = Model(sequence_input, preds) model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['acc'])
def __init__(self, train_data_source, test_data_source): self.train_df = parseCSV(train_data_source) self.test_df = parseCSV_testing(test_data_source) self.x_train = np.array(self.train_df["Word"]) self.x_test = np.array(self.test_df["Word"]) CHAR_STRING = 'abcdefghijklmnopqrstuvwxyzáéíóúüñàèìòùçâêîôûëïäöß()-āēīōū’ā̆ē̆ī̆ō̆ăĭḗū́u̯ṇ̃þʒ¹²/\ :;"!?¿¡".' char_dict = {} for i, char, in enumerate(CHAR_STRING): char_dict[char] = i + 1 #print(char_dict) self.tk = Tokenizer(num_words=None, char_level=True, oov_token='UNK') self.tk.fit_on_texts(self.x_train) self.tk.word_index = char_dict.copy() self.tk.word_index[self.tk.oov_token] = max(char_dict.values()) + 1 train_sequences = self.tk.texts_to_sequences(self.x_train) test_texts = self.tk.texts_to_sequences(self.x_test) #print(tk.word_index) # Padding train_data = pad_sequences(train_sequences, maxlen=1014, padding='post') test_data = pad_sequences(test_texts, maxlen=1014, padding='post') # Convert to numpy array train_data = np.array(train_data, dtype='float32') test_data = np.array(test_data, dtype='float32') self.class2indexes = dict( (l, i) for i, l in enumerate(set(self.train_df["Language"]))) self.index2class = dict( (i, l) for i, l in enumerate(set(self.train_df["Language"]))) print(self.class2indexes) train_classes = self.train_df["Language"] train_class_list = [self.class2indexes[x] for x in train_classes] test_classes = self.test_df["Language"] test_class_list = [self.class2indexes[x] for x in test_classes] train_classes = to_categorical(train_class_list) test_classes = to_categorical( test_class_list) #converts class to binary class martix vocab_size = 93 print(vocab_size) embedding_weights = [] embedding_weights.append(np.zeros(vocab_size)) for i in range(vocab_size): onehot = np.zeros(vocab_size) onehot[i] = 1 embedding_weights.append(onehot) embedding_weights = np.array(embedding_weights) # print(embedding_weights.shape) # print(embedding_weights) print('Load') # Model Construction #parameters input_size = 1014 embedding_size = 93 conv_layers = [[256, 7, 3], [256, 7, 3], [256, 3, -1], [256, 3, -1], [256, 3, -1], [256, 3, 3]] fully_connected_layers = [1024, 1024] nums_of_classes = 7 dropout_p = 0.5 optimizer = "adam" loss = "categorical_crossentropy" # Embedding layer Initialization embedding_layer = Embedding(vocab_size + 1, embedding_size, input_length=input_size, weights=[embedding_weights]) # Input inputs = Input(shape=(input_size, ), name='input', dtype='int64') # shape=(?, 1014) x = embedding_layer(inputs) #conv for filter_num, filter_size, pooling_size in conv_layers: x = Conv1D(filter_num, filter_size)(x) #data_format = 'channels_first' x = Activation('relu')(x) if pooling_size != -1: x = MaxPooling1D(pool_size=pooling_size)( x) #prevents overfitting x = Flatten()(x) #turns in a martix into a 1D array #Fully connected layers for dense_size in fully_connected_layers: x = Dense(dense_size, activation='relu')(x) x = Dropout(dropout_p)(x) #help reduce overfitting #Output Layer predictions = Dense(nums_of_classes, activation='softmax')(x) #Build Model self.model = Model(input=inputs, outputs=predictions) self.model.compile(optimizer=optimizer, loss=loss) self.model.summary() self.model.fit(train_data, train_classes, validation_data=(test_data, test_classes), batch_size=128, epochs=5, verbose=2)
def generate_model(self): """ Model for RNN with Encoder Decoder for S2S separating the dependent variable from the auxiliary variables ------------- json config: "arch": { "neuronsE":128, "neuronsD":64, "k_reg": "None", "k_regw": 0.1, "rec_reg": "None", "rec_regw": 0.1, "drop": 0.3, "nlayersE": 1, "nlayersD": 1, "activation": "relu", "activation_r": "hard_sigmoid", #"CuDNN": false, #"bidirectional": false, #"bimerge":"ave", "rnn": "GRU", "mode": "RNN_ED_s2s_dep" } ------------ :return: """ neuronsE = self.config['arch']['neuronsE'] neuronsD = self.config['arch']['neuronsD'] drop = self.config['arch']['drop'] nlayersE = self.config['arch']['nlayersE'] # >= 1 nlayersD = self.config['arch']['nlayersD'] # >= 1 activation = self.config['arch']['activation'] activation_r = self.config['arch']['activation_r'] rec_reg = self.config['arch']['rec_reg'] rec_regw = self.config['arch']['rec_regw'] k_reg = self.config['arch']['k_reg'] k_regw = self.config['arch']['k_regw'] rnntype = self.config['arch']['rnn'] CuDNN = self.config['arch']['CuDNN'] # Extra added from training function idimensions = self.config['idimensions'] odimensions = self.config['odimensions'] impl = self.runconfig.impl if rec_reg == 'l1': rec_regularizer = l1(rec_regw) elif rec_reg == 'l2': rec_regularizer = l2(rec_regw) else: rec_regularizer = None if k_reg == 'l1': k_regularizer = l1(k_regw) elif rec_reg == 'l2': k_regularizer = l2(k_regw) else: k_regularizer = None RNN = LSTM if rnntype == 'LSTM' else GRU # Dependent variable input enc_Dep_input = Input(shape=(idimensions[0])) rec_Dep_input = recurrent_encoder_functional(RNN, nlayersE, neuronsE, impl, drop, activation, activation_r, rec_regularizer, k_regularizer, enc_Dep_input) # Auxiliary variables input enc_Aux_input = Input(shape=(idimensions[1])) rec_Aux_input = recurrent_encoder_functional(RNN, nlayersE, neuronsE, impl, drop, activation, activation_r, rec_regularizer, k_regularizer, enc_Aux_input) enc_input = concatenate([rec_Dep_input, rec_Aux_input]) output = RepeatVector(odimensions)(enc_input) output = recurrent_decoder_functional(RNN, nlayersD, neuronsD, impl, drop, activation, activation_r, rec_regularizer, k_regularizer, output) output = TimeDistributed(Dense(1))(output) self.model = Model(inputs=[enc_Dep_input, enc_Aux_input], outputs=output)
def autoencode(epochs, verbose, performance, hidden): print("Running the autoencoder") #Set the dimensionality of the encoded input encoding_dim = hidden #Placeholders. Dunno why these are necessary, the Keras guide recommends it input_img = Input(shape=(784,)) encoded_input = Input(shape=(encoding_dim,)) #Layer that encodes the input (from 784 dims to 32) encoded = Dense(encoding_dim, activation='relu')(input_img) #Layer that decodes the input (from 32 dims to 784) decoded = Dense(784, activation='sigmoid')(encoded) # Model that maps an input to itself autoencoder = Model(input_img, decoded) # Model that maps an input to its encoded version encoder = Model(input_img, encoded) # Model that maps an input to its decoded version decoder_layer = autoencoder.layers[-1] decoder = Model(encoded_input, decoder_layer(encoded_input)) if(performance == "high"): autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') else: autoencoder.compile(optimizer='sgd', loss='mse') # Setup early stopping earlystop = EarlyStopping(monitor='loss', min_delta=0.0001, patience=5, verbose=0, mode='auto') callback_list = [earlystop] history = autoencoder.fit(train_in, train_in, epochs=epochs, batch_size=256, shuffle=True, verbose = verbose, validation_data=(test_in, test_in), callbacks = callback_list) plt.figure("Autoencoder Loss") plt.plot(history.history['loss']) # encode and decode some digits # note that we take them from the *test* set encoded_imgs = encoder.predict(test_in) decoded_imgs = decoder.predict(encoded_imgs) samples = [18, 3, 7, 0, 2, 1,15 , 8, 6 ,5 ] n = 10 # how many digits we will display plt.figure("Autoencoder", figsize=(20, 4)) for i in range(n): index = samples[i] # display original ax = plt.subplot(2, n, i + 1) plt.imshow(test_in[index].reshape(28, 28)) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) # display reconstruction ax = plt.subplot(2, n, i + 1 + n) plt.imshow(decoded_imgs[index].reshape(28, 28)) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.show() return autoencoder
def unet_model(): inputs = Input((2, img_size, img_size)) conv1 = Conv2D(64, (3, 3), activation='relu', padding='same') (inputs) batch1 = BatchNormalization(axis=1)(conv1) conv1 = Conv2D(64, (3, 3), activation='relu', padding='same') (batch1) batch1 = BatchNormalization(axis=1)(conv1) pool1 = MaxPooling2D((2, 2)) (batch1) conv2 = Conv2D(128, (3, 3), activation='relu', padding='same') (pool1) batch2 = BatchNormalization(axis=1)(conv2) conv2 = Conv2D(128, (3, 3), activation='relu', padding='same') (batch2) batch2 = BatchNormalization(axis=1)(conv2) pool2 = MaxPooling2D((2, 2)) (batch2) conv3 = Conv2D(256, (3, 3), activation='relu', padding='same') (pool2) batch3 = BatchNormalization(axis=1)(conv3) conv3 = Conv2D(256, (3, 3), activation='relu', padding='same') (batch3) batch3 = BatchNormalization(axis=1)(conv3) pool3 = MaxPooling2D((2, 2)) (batch3) conv4 = Conv2D(512, (3, 3), activation='relu', padding='same') (pool3) batch4 = BatchNormalization(axis=1)(conv4) conv4 = Conv2D(512, (3, 3), activation='relu', padding='same') (batch4) batch4 = BatchNormalization(axis=1)(conv4) pool4 = MaxPooling2D(pool_size=(2, 2)) (batch4) conv5 = Conv2D(1024, (3, 3), activation='relu', padding='same') (pool4) batch5 = BatchNormalization(axis=1)(conv5) conv5 = Conv2D(1024, (3, 3), activation='relu', padding='same') (batch5) batch5 = BatchNormalization(axis=1)(conv5) up6 = Conv2DTranspose(512, (2, 2), strides=(2, 2), padding='same') (batch5) up6 = concatenate([up6, conv4], axis=1) conv6 = Conv2D(512, (3, 3), activation='relu', padding='same') (up6) batch6 = BatchNormalization(axis=1)(conv6) conv6 = Conv2D(512, (3, 3), activation='relu', padding='same') (batch6) batch6 = BatchNormalization(axis=1)(conv6) up7 = Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same') (batch6) up7 = concatenate([up7, conv3], axis=1) conv7 = Conv2D(256, (3, 3), activation='relu', padding='same') (up7) batch7 = BatchNormalization(axis=1)(conv7) conv7 = Conv2D(256, (3, 3), activation='relu', padding='same') (batch7) batch7 = BatchNormalization(axis=1)(conv7) up8 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same') (batch7) up8 = concatenate([up8, conv2], axis=1) conv8 = Conv2D(128, (3, 3), activation='relu', padding='same') (up8) batch8 = BatchNormalization(axis=1)(conv8) conv8 = Conv2D(128, (3, 3), activation='relu', padding='same') (batch8) batch8 = BatchNormalization(axis=1)(conv8) up9 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same') (batch8) up9 = concatenate([up9, conv1], axis=1) conv9 = Conv2D(64, (3, 3), activation='relu', padding='same') (up9) batch9 = BatchNormalization(axis=1)(conv9) conv9 = Conv2D(64, (3, 3), activation='relu', padding='same') (batch9) batch9 = BatchNormalization(axis=1)(conv9) conv10 = Conv2D(1, (1, 1), activation='sigmoid')(batch9) model = Model(inputs=[inputs], outputs=[conv10]) model.compile(optimizer=Adam(lr=LR), loss=dice_coef_loss, metrics=[dice_coef]) return model
def train_and_predict(x_train, y_train, x_valid, y_valid, fold): # data augmentation x_train = np.append(x_train, [np.fliplr(x) for x in x_train], axis=0) y_train = np.append(y_train, [np.fliplr(x) for x in y_train], axis=0) print("x_train after hflip", x_train.shape) print("y_train after hflip", y_valid.shape) # model input_layer = Input((img_size_target, img_size_target, 3)) output_layer = build_model(input_layer, 16, 0.5) model1 = Model(input_layer, output_layer) c = optimizers.adam(lr=0.005) model1.compile(loss="binary_crossentropy", optimizer=c, metrics=[my_iou_metric]) save_model_name = f"{basic_name}_stage1_fold{fold}.hdf5" early_stopping = EarlyStopping(monitor='my_iou_metric', mode='max', patience=15, verbose=1) model_checkpoint = ModelCheckpoint(save_model_name, monitor='my_iou_metric', mode='max', save_best_only=True, verbose=1) reduce_lr = ReduceLROnPlateau(monitor='my_iou_metric', mode='max', factor=0.5, patience=5, min_lr=0.0001, verbose=1) epochs = 80 batch_size = 128 if not PREDICT_ONLY: history = model1.fit( x_train, y_train, validation_data=[x_valid, y_valid], epochs=epochs, batch_size=batch_size, callbacks=[early_stopping, model_checkpoint, reduce_lr], verbose=2) model1 = load_model(save_model_name, custom_objects={'my_iou_metric': my_iou_metric}) # remove activation layer and use lovasz loss input_x = model1.layers[0].input output_layer = model1.layers[-1].input model = Model(input_x, output_layer) c = optimizers.adam(lr=0.01) model.compile(loss=lovasz_loss, optimizer=c, metrics=[my_iou_metric_2]) save_model_name = f"{basic_name}_stage2_fold{fold}.hdf5" early_stopping = EarlyStopping(monitor='val_my_iou_metric_2', mode='max', patience=30, verbose=1) model_checkpoint = ModelCheckpoint(save_model_name, monitor='val_my_iou_metric_2', mode='max', save_best_only=True, verbose=1) reduce_lr = ReduceLROnPlateau(monitor='val_my_iou_metric_2', mode='max', factor=0.5, patience=5, min_lr=0.00005, verbose=1) epochs = 120 batch_size = 128 if not PREDICT_ONLY: history = model.fit( x_train, y_train, validation_data=[x_valid, y_valid], epochs=epochs, batch_size=batch_size, callbacks=[model_checkpoint, reduce_lr, early_stopping], verbose=2) model = load_model(save_model_name, custom_objects={ 'my_iou_metric_2': my_iou_metric_2, 'lovasz_loss': lovasz_loss }) def predict_result(model, x_test, img_size_target): # predict both orginal and reflect x x_test_reflect = np.array([np.fliplr(x) for x in x_test]) preds_test = model.predict(x_test).reshape(-1, img_size_target, img_size_target) preds_test2_refect = model.predict(x_test_reflect).reshape( -1, img_size_target, img_size_target) preds_test += np.array([np.fliplr(x) for x in preds_test2_refect]) return preds_test / 2 preds_valid = predict_result(model, x_valid, img_size_target) preds_test = predict_result(model, x_test, img_size_target) return preds_valid, preds_test
p = model_discrim.predict([ Q, A, Y]) p = p[-sa:-1] P = np.sum(np.log(p))/sa return P print('Starting the model...') # ******************************************************************* # Keras model of the chatbot: # ******************************************************************* ad = Adam(lr=learning_rate) input_context = Input(shape=(maxlen_input,), dtype='int32', name='the context text') input_answer = Input(shape=(maxlen_input,), dtype='int32', name='the answer text up to the current token') LSTM_encoder = LSTM(sentence_embedding_size, kernel_initializer= 'lecun_uniform', name='Encode context') LSTM_decoder = LSTM(sentence_embedding_size, kernel_initializer= 'lecun_uniform', name='Encode answer up to the current token') Shared_Embedding = Embedding(output_dim=word_embedding_size, input_dim=dictionary_size, input_length=maxlen_input, name='Shared') word_embedding_context = Shared_Embedding(input_context) context_embedding = LSTM_encoder(word_embedding_context) word_embedding_answer = Shared_Embedding(input_answer) answer_embedding = LSTM_decoder(word_embedding_answer) merge_layer = concatenate([context_embedding, answer_embedding], axis=1, name='concatenate the embeddings of the context and the answer up to current token') out = Dense(dictionary_size/2, activation="relu", name='relu activation')(merge_layer) out = Dense(dictionary_size, activation="softmax", name='likelihood of the current token using softmax activation')(out)
nb_actions = env.action_space.shape[0] obs_dim = env.observation_space.shape[0] print(f'Number of Actions: {nb_actions}') # Next, we build a very simple model. actor = Sequential() actor.add(Flatten(input_shape=(1,) + env.observation_space.shape)) actor.add(Dense(16, activation='relu')) actor.add(Dense(16, activation='relu')) actor.add(Dense(16, activation='relu')) actor.add(Dropout(0.2, input_shape=(1,) + env.observation_space.shape)) actor.add(Dense(nb_actions, activation='tanh')) print(actor.summary()) action_input = Input(shape=(nb_actions,), name='action_input') observation_input = Input(shape=(1,) + env.observation_space.shape, name='observation_input') flattened_observation = Flatten()(observation_input) x = Concatenate()([action_input, flattened_observation]) x = Dense(32, activation='relu')(x) x = Dense(32, activation='relu')(x) x = Dense(32, activation='relu')(x) x = Dense(1, activation='linear')(x) critic = Model(inputs=[action_input, observation_input], outputs=x) print(critic.summary()) # memory = EpisodeParameterMemory(limit=1000000, window_length=1) memory = SequentialMemory(limit=1000000, window_length=1) random_process = OrnsteinUhlenbeckProcess(size=nb_actions, theta=.15, mu=0., sigma=.3) agent = DDPGAgent(nb_actions=nb_actions, actor=actor, critic=critic, critic_action_input=action_input, memory=memory, nb_steps_warmup_critic=100, nb_steps_warmup_actor=100,
X_Train, Y_Train = preprocess_dataset( '/Users/m.salmanghazi/Downloads/MachineLearningCVE/Wednesday-workingHours.pcap_ISCX.csv') X_Test, Y_Test = preprocess_dataset( '/Users/m.salmanghazi/Downloads/MachineLearningCVE/Thursday-WorkingHours-Morning-WebAttacks.pcap_ISCX.csv') print(X_Train.shape) print(Y_Train.shape) inputdim = X_Train.shape[0] print(inputdim) encoding_dim=10 i=Input(shape=(78,)) encoded=Dense(40,activation='sigmoid')(i) encoded1=Dense(20,activation='sigmoid')(encoded) encoded2=Dense(10,activation='relu')(encoded1) #encoded=Dense(encoding_dim,activation='sigmoid')(encoded2) decoded=Dense(20,activation='sigmoid')(encoded2)
def Tiramisu( input_shape=(None, None, 3), n_classes=1, n_filters_first_conv=48, n_pool=5, growth_rate=16, n_layers_per_block=[4, 5, 7, 10, 12, 15, 12, 10, 7, 5, 4], dropout_p=0.2 ): if type(n_layers_per_block) == list: print(len(n_layers_per_block)) elif type(n_layers_per_block) == int: n_layers_per_block = [n_layers_per_block] * (2 * n_pool + 1) else: raise ValueError ##################### # First Convolution # ##################### inputs = Input(shape=input_shape) stack = Conv2D(filters=n_filters_first_conv, kernel_size=3, padding='same', kernel_initializer='he_uniform')(inputs) n_filters = n_filters_first_conv ##################### # Downsampling path # ##################### skip_connection_list = [] for i in range(n_pool): for j in range(n_layers_per_block[i]): l = BN_ReLU_Conv(stack, growth_rate, dropout_p=dropout_p) stack = concatenate([stack, l]) n_filters += growth_rate skip_connection_list.append(stack) stack = TransitionDown(stack, n_filters, dropout_p) skip_connection_list = skip_connection_list[::-1] # https://blog.csdn.net/HARDBIRD123/article/details/82261651 ##################### # Bottleneck # ##################### block_to_upsample = [] for j in range(n_layers_per_block[n_pool]): l = BN_ReLU_Conv(stack, growth_rate, dropout_p=dropout_p) block_to_upsample.append(l) stack = concatenate([stack, l]) block_to_upsample = concatenate(block_to_upsample) ##################### # Upsampling path # ##################### for i in range(n_pool): n_filters_keep = growth_rate * n_layers_per_block[n_pool + i] stack = TransitionUp(skip_connection_list[i], block_to_upsample, n_filters_keep) block_to_upsample = [] for j in range(n_layers_per_block[n_pool + i + 1]): l = BN_ReLU_Conv(stack, growth_rate, dropout_p=dropout_p) block_to_upsample.append(l) stack = concatenate([stack, l]) block_to_upsample = concatenate(block_to_upsample) ##################### # Softmax # ##################### output = SoftmaxLayer(stack, n_classes) model = Model(inputs=inputs, outputs=output) model.summary() return model
return Q # Open files to save the conversation for further training: qf = open(file_saved_context, 'w') af = open(file_saved_answer, 'w') # print('Starting the model...') # ******************************************************************* # Keras model of the chatbot: # ******************************************************************* ad = Adam(lr=0.00005) input_context = Input(shape=(maxlen_input,), dtype='int32', name='the_context_text') input_answer = Input(shape=(maxlen_input,), dtype='int32', name='the_answer_text_up_to_the_current_token') LSTM_encoder = LSTM(sentence_embedding_size, kernel_initializer= 'lecun_uniform', name='Encode_context') LSTM_decoder = LSTM(sentence_embedding_size, kernel_initializer= 'lecun_uniform', name='Encode_answer_up_to_the_current_token') if os.path.isfile(weights_file): Shared_Embedding = Embedding(output_dim=word_embedding_size, input_dim=dictionary_size, input_length=maxlen_input, name='Shared') else: Shared_Embedding = Embedding(output_dim=word_embedding_size, input_dim=dictionary_size, weights=[embedding_matrix], input_length=maxlen_input, name='Shared') word_embedding_context = Shared_Embedding(input_context) context_embedding = LSTM_encoder(word_embedding_context) word_embedding_answer = Shared_Embedding(input_answer) answer_embedding = LSTM_decoder(word_embedding_answer) merge_layer = concatenate([context_embedding, answer_embedding], axis=1, name='concatenate_the_embeddings_of_the_context_and_the_answer_up_to_current_token') # print(dictionary_size)
x_sys_test = [ t[:50] if len(t) >= 50 else t + (['end'] * (50 - len(t))) for t in x_sys_raw_test ] x_user_test = np.array([[user_model[word] for word in arr] for arr in x_user_test]) x_sys_test = np.array([[system_model[word] for word in arr] for arr in x_sys_test]) print('done!', flush=True) del x_sys_raw_train, x_sys_raw_test, x_user_raw_train, x_user_raw_test gc.collect() user_activity_input = Input(shape=x_user_train.shape[1:]) user_activity_stream_hidden_layer = Bidirectional( LSTM(128, return_sequences=True, input_shape=(x_user_train.shape[1], x_user_train.shape[2])))(user_activity_input) user_activity_stream = Bidirectional( LSTM(256, return_sequences=True, input_shape=(x_user_train.shape[1], 256)))(user_activity_stream_hidden_layer) # In[36]: sys_activity_input = Input(shape=x_sys_train.shape[1:])
def resnet101_model(img_rows, img_cols, color_type=3, num_classes=1): """ Resnet 101 Model for Keras Model Schema and layer naming follow that of the original Caffe implementation https://github.com/KaimingHe/deep-residual-networks ImageNet Pretrained Weights Theano: https://drive.google.com/file/d/0Byy2AcGyEVxfdUV1MHJhelpnSG8/view?usp=sharing TensorFlow: https://drive.google.com/file/d/0Byy2AcGyEVxfTmRRVmpGWDczaXM/view?usp=sharing Parameters: img_rows, img_cols - resolution of inputs channel - 1 for grayscale, 3 for color num_classes - number of class labels for our classification task """ eps = 1.1e-5 # Handle Dimension Ordering for different backends global bn_axis if K.image_dim_ordering() == 'tf': bn_axis = 3 img_input = Input(shape=(img_rows, img_cols, color_type), name='data') else: bn_axis = 1 img_input = Input(shape=(color_type, img_rows, img_cols), name='data') x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), use_bias=False, name='conv1')(x) x = BatchNormalization(epsilon=eps, axis=bn_axis, name='bn_conv1')(x) x = Scale(axis=bn_axis, name='scale_conv1')(x) x = Activation('relu', name='conv1_relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') for i in range(1,4): x = identity_block(x, 3, [128, 128, 512], stage=3, block='b'+str(i)) x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') for i in range(1,23): x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b'+str(i)) x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') #x_fc = AveragePooling2D((7, 7), name='avg_pool')(x) #x_fc = Flatten()(x_fc) #x_fc = Dense(1000, activation='softmax', name='fc1000')(x_fc) #model = Model(img_input, x_fc) if K.image_dim_ordering() == 'th': # Use pre-trained weights for Theano backend weights_path = 'imagenet_models/resnet101_weights_th.h5' else: # Use pre-trained weights for Tensorflow backend weights_path = 'checkpoints/resnet101_weights_tf.h5' #model.load_weights(weights_path, by_name=True) # Truncate and replace softmax layer for transfer learning # Cannot use model.layers.pop() since model is not of Sequential() type # The method below works since pre-trained weights are stored in layers but not in the model x_newfc = AveragePooling2D((5, 5), name='avg_pool')(x) x_newfc = Flatten()(x_newfc) x_newfc = Dense(num_classes, activation=None, name='fc8')(x_newfc) model = Model(img_input, x_newfc) # Learning rate is changed to 0.001 #sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True) #model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy']) return model
import cv2 def mean_iou(y_true, y_pred): prec = [] for t in np.arange(0.5, 1.0, 0.05): y_pred_ = tf.to_int32(y_pred > t) score, up_opt = tf.metrics.mean_iou(y_true, y_pred_, 2) K.get_session().run(tf.local_variables_initializer()) with tf.control_dependencies([up_opt]): score = tf.identity(score) prec.append(score) return K.mean(K.stack(prec), axis=0) inputs = Input((128, 128, 3)) s = Lambda(lambda x: x / 255)(inputs) c1 = Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(s) c1 = Dropout(0.1)(c1) c1 = Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same')(c1) p1 = MaxPooling2D((2, 2))(c1) c2 = Conv2D(32, (3, 3), activation='elu',
def neural_net(X, y, y_class, prev_model=False): """ PARAMETER SETTINGS slight preprocessing """ y_labels = [y] layer_param = dict(kernel_initializer='truncated_normal', activation='tanh', bias_initializer='ones') for i in range(y_class.shape[1]): y_labels.append(y_class[:, i, :]) nn_name = Path('model.hd5') if prev_model & nn_name.exists(): print('Using prev model') nn_architecture = load_model('model.hd5') else: inputs = Input(shape=(X.shape[1], )) x = Dense(100, **layer_param)(inputs) # x = Dense(20,**layer_param)(x) output_0 = Dense(y_class.shape[1], activation='linear', name='output_0')(x) output_1 = Dense(y_class.shape[2], activation='softmax', name='output_1')(x) output_2 = Dense(y_class.shape[2], activation='softmax', name='output_2')(x) output_3 = Dense(y_class.shape[2], activation='softmax', name='output_3')(x) output_4 = Dense(y_class.shape[2], activation='softmax', name='output_4')(x) nn_architecture = Model( inputs=inputs, outputs=[output_0, output_1, output_2, output_3, output_4]) # sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) # nn_architecture.compile(loss='mean_squared_error', optimizer=sgd) nn_architecture.compile(optimizer='adam', metrics={ 'output_0': 'mean_squared_error', 'output_1': 'accuracy', 'output_2': 'accuracy', 'output_3': 'accuracy', 'output_4': 'accuracy' }, loss={ 'output_0': 'mean_squared_error', 'output_1': 'categorical_crossentropy', 'output_2': 'categorical_crossentropy', 'output_3': 'categorical_crossentropy', 'output_4': 'categorical_crossentropy' }, loss_weights={ 'output_0': 1.0, 'output_1': 0.5, 'output_2': 0.2, 'output_3': 0.5, 'output_4': 0.3 }) history = nn_architecture.fit(X, y_labels, verbose=2, epochs=200, shuffle=True, batch_size=64, validation_split=0.1) loss = history.history['loss'] fig = plt.figure() ax = fig.add_subplot(111) ax.plot(range(len(loss)), loss) plt.show() return nn_architecture
import numpy as np from keras.layers import Input, Dense, Conv2D, UpSampling2D from keras.layers import MaxPooling2D from keras.utils import print_summary from keras.models import Model from keras.callbacks import TensorBoard from keras.datasets import mnist input_img = Input(shape=(784, )) input_img_conv = Input(shape=(28, 28, 1)) encoding_dim = 64 class SimpleCoder: def encoder(self): encoded = Dense(encoding_dim, activation='relu')(input_img) return encoded def decoder(self, encoded): decoded = Dense(784, activation='sigmoid')(encoded) return decoded class DeepCoder: def encoder(self): encoded = Dense(512, activation='relu')(input_img) encoded = Dense(256, activation='relu')(encoded) encoded = Dense(128, activation='relu')(encoded) encoded = Dense(encoding_dim, activation='relu')(encoded) return encoded
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Sat Jun 23 09:01:18 2018 @author: ubuntu """ import keras from keras.models import Model from keras.layers import Input,Conv2D,MaxPooling2D from keras.layers import Flatten,Dense digit_input = Input(shape=(27,27,1)) x = Conv2D(64,(3,3))(digit_input) x = Conv2D(64,(3,3))(x) x = MaxPooling2D((2,2))(x) out = Flatten()(x) vision_model = Model(digit_input,out) digit_a = Input(shape=(27,27,1)) digit_b = Input(shape=(27,27,1)) out_a = vision_model(digit_a) out_b = vision_model(digit_b) concatenated = keras.layers.concatenate([out_a,out_b]) out = Dense(1,activation='sigmoid')(concatenated) classification_model = Model([digit_a,digit_b],out)
def Model3(input_tensor=None, train=False): nb_classes = 10 # convolution kernel size kernel_size = (5, 5) if train: batch_size = 256 nb_epoch = 10 # input image dimensions img_rows, img_cols = 28, 28 # the data, shuffled and split between train and test sets (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) input_shape = (img_rows, img_cols, 1) x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 # convert class vectors to binary class matrices y_train = to_categorical(y_train, nb_classes) y_test = to_categorical(y_test, nb_classes) input_tensor = Input(shape=input_shape) elif input_tensor is None: exit() # block1 x = Convolution2D(6, kernel_size, activation='relu', padding='same', name='block1_conv1')(input_tensor) x = MaxPooling2D(pool_size=(2, 2), name='block1_pool1')(x) # block2 x = Convolution2D(16, kernel_size, activation='relu', padding='same', name='block2_conv1')(x) x = MaxPooling2D(pool_size=(2, 2), name='block2_pool1')(x) x = Flatten(name='flatten')(x) x = Dense(120, activation='relu', name='fc1')(x) x = Dense(84, activation='relu', name='fc2')(x) x = Dense(nb_classes, name='before_softmax')(x) x = Activation('softmax', name='predictions')(x) model = Model(input_tensor, x) if train: # compiling model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) # trainig model.fit(x_train, y_train, validation_data=(x_test, y_test), batch_size=batch_size, epochs=nb_epoch, verbose=1) # save model model.save_weights('./Models/Model3.h5') score = model.evaluate(x_test, y_test, verbose=0) print('\n') print('Overall Test score:', score[0]) print('Overall Test accuracy:', score[1]) else: model.load_weights('./Models/Model3.h5') return model
def input_layer (word_limit, feature_size) : input_layer = Input(shape=((word_limit*feature_size) + (word_limit*word_limit), 1), name='main_input') return input_layer
def cnnModelCofiguration(self): print("start model") filterSize = [3,4,5] modelInput = Input(shape=(self.TWEET_SIZE, self.EMDEDDING_DIM)) transientCNNTraining = np.array([]) print("input CNN shape>>",self.x_Train.shape) ''' convBlocks = [] for eachFilter in filterSize: #hybridFeedDimension = int(np.rint(self.TWEET_SIZE/2)) hybridFeedDimension = 256 singleConv = Conv1D(filters=hybridFeedDimension, kernel_size=eachFilter, padding='valid',activation='relu',strides=2)(modelInput) singleConv = MaxPool1D(pool_size = 1)(singleConv) convBlocks.append(singleConv) concatenatedCNNLayer = Concatenate(axis=1)(convBlocks) if len(convBlocks) > 1 else convBlocks[0] concatenatedCNNLayer = Dense(1, activation='sigmoid')(concatenatedCNNLayer) model = Model(inputs = modelInput, outputs=concatenatedCNNLayer) transientFeedScores = model.predict(self.x_Train) print("CNN output shape>>>>", transientFeedScores.shape) transientCNNTraining = self.populateArray(self, transientCNNTraining, transientFeedScores.transpose(0,2,1)) ''' cnn_Threshold = int(self.EMDEDDING_DIM*0.50) convBlocks = [] for eachFilter in filterSize: print("\n******************filter>>",eachFilter) tweetSpan = self.TWEET_SIZE modelInput = Input(shape=(self.TWEET_SIZE, self.EMDEDDING_DIM)) hybridFeedDimension = self.EMDEDDING_DIM layers = 0 filterSequential = Sequential() while((hybridFeedDimension > cnn_Threshold) and (eachFilter < tweetSpan)): #print("1>>>",hybridFeedDimension,"\t2>>>",self.EMDEDDING_DIM,"\t3>>>",tweetSpan) if hybridFeedDimension == self.EMDEDDING_DIM: singleConv = Conv1D(filters=hybridFeedDimension, kernel_size=eachFilter, padding='valid',activation='relu',strides=1)(modelInput) singleConv = MaxPool1D(pool_size = 1)(singleConv) model = Model(inputs = modelInput, outputs = singleConv) filterSequential.add(model) else: filterSequential.add(Conv1D(filters=hybridFeedDimension, kernel_size=eachFilter, padding='valid',activation='relu',strides=1)) filterSequential.add(MaxPool1D(pool_size = 1)) print("\t>>",filterSequential.predict(self.x_Train).shape) tweetSpan = filterSequential.predict(self.x_Train).shape[1] stepReduce = int(np.ceil(0.25*hybridFeedDimension)) hybridFeedDimension = (hybridFeedDimension - stepReduce) print(">>>",stepReduce,"CUrr>>",hybridFeedDimension) layers +=1 print("layers>>>",layers) filterSequential.add(Dense(1, activation='sigmoid')) print("\t dense>>",filterSequential.predict(self.x_Train).shape) convBlocks.append(filterSequential.predict(self.x_Train)) for tweetIndex in range(self.x_Train.shape[0]): tier1Array = np.array([]) for filterIndex in range(len(convBlocks)): tier2Array = np.array(convBlocks[filterIndex]) tier1Array = self.populateArray(self, tier1Array, tier2Array[tweetIndex]) tier1Array = tier1Array.reshape(1,tier1Array.shape[0],tier1Array.shape[1]) tier1Array = tier1Array.transpose(0,2,1) #print("subArray>>",tier1Array.shape) transientCNNTraining = self.populateArray(self, transientCNNTraining, tier1Array) ''' print("total Dense shape>>>",transientCNNTraining.shape) modelInput = Input(shape = (transientCNNTraining.shape[1],transientCNNTraining.shape[2])) hiddenDenseLayer = Dense(1, activation='sigmoid')(modelInput) model = Model(inputs = modelInput, outputs = hiddenDenseLayer, name = 'denseLayer') transientFeedScores = model.predict(transientCNNTraining) print("final Dense shape",transientFeedScores.shape) transientCNNTraining = np.array([]) transientCNNTraining = self.populateArray(self, transientCNNTraining, transientFeedScores) ''' ''' for tier1Index in range(4): subTrainArray = np.array(self.x_Train[tier1Index]) subTrainArray = subTrainArray.reshape(1, subTrainArray.shape[0], subTrainArray.shape[1]) convBlocks = np.array([]) for eachFilter in filterSize: hybridFeedDimension = int(np.rint(self.TWEET_SIZE/2)) singleConv = Conv1D(filters=hybridFeedDimension, kernel_size=eachFilter, padding='valid',activation='relu',strides=300)(modelInput) singleConv = MaxPool1D(pool_size = 1)(singleConv) model = Model(inputs = modelInput, outputs=singleConv) transientFeedScores = model.predict(subTrainArray) transientFeedScores = transientFeedScores[0] #print("window %d" % eachFilter,"\t>>>",transientFeedScores.shape) convBlocks = self.populateArray(self, convBlocks, transientFeedScores) convBlocks = convBlocks.reshape(1, convBlocks.shape[0], convBlocks.shape[1]) #print("index %d" % tier1Index,"\t>>>",convBlocks.shape) transientCNNTraining = self.populateArray(self, transientCNNTraining, convBlocks) ''' print("CNN output shape>>>>", transientCNNTraining.shape) ''' lrAdam = Adam(lr=0.01,decay=0.001) model.compile(loss='binary_crossentropy', optimizer=lrAdam, metrics=['accuracy']) print(model.summary()) model.fit(self.x_Train, self.y_Train, validation_data=(self.x_Train, self.y_Train), epochs=5, batch_size=int(self.x_Train.shape[0]/2)) scores = model.evaluate(self.x_Train, self.y_Train, verbose=0) print("Accuracy: %.2f%%" % (scores[1])) for tier1Itr in range(self.x_Validation.shape[0]): testExample = self.x_Validation[tier1Itr].reshape(1,self.TWEET_SIZE,self.EMDEDDING_DIM) probability = model.predict(testExample, verbose=2) print(">>>",probability,"\t actual>>>",self.y_Validation[tier1Itr]) ''' return(transientCNNTraining)
def vgg19(weights_path='./../../../essentials/standardModels/pretrainedWeights/vgg19_weights_tf_dim_ordering_tf_kernels.h5', retainTop = False): # -*- coding: utf-8 -*- """VGG19 model for Keras. # Reference - [Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556) """ #WEIGHTS_PATH = #WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5' classes = 1000 img_input = Input(shape=(227, 227, 3)) # Block 1 conv_1 = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) conv_2 = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(conv_1) max_1 = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(conv_2) # Block 2 conv_3 = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(max_1) conv_4 = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(conv_3) max_2 = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(conv_4) # Block 3 conv_5 = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(max_2) conv_6 = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(conv_5) conv_7 = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(conv_6) conv_8 = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv4')(conv_7) max_3 = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(conv_8) # Block 4 conv_9 = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(max_3) conv_10 = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(conv_9) conv_11 = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(conv_10) conv_12 = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv4')(conv_11) max_4 = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(conv_12) # Block 5 conv_13 = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(max_4) conv_14 = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(conv_13) conv_15 = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(conv_14) conv_16 = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv4')(conv_15) max_5 = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(conv_16) flat = Flatten(name='flatten')(max_5) dense_1 = Dense(4096, activation='relu', name='fc1')(flat) dense_2 = Dropout(0.5)(dense_1) dense_2 = Dense(4096, activation='relu', name='fc2')(dense_2) dense_3 = Dropout(0.5)(dense_2) prediction = Dense(classes, activation='softmax', name='predictions')(dense_3) tempModel = Model(img_input, prediction, name='vgg19') tempModel.load_weights(weights_path) if not retainTop: model = Model(inputs=[img_input], outputs=[dense_1]) lastLayer = dense_2 else: model = tempModel lastLayer = prediction firstLayer = img_input return model, firstLayer, lastLayer
# Verify we have a GPU available # The output of the following should not be an empty array # If you get an empty array back, it means no GPU was detected, which might mean you need to # uninstall keras/tensorflow/tensorflow-gpu and then reinstall tensorflow-gpu and keras K.tensorflow_backend._get_available_gpus() # We use Fashion mnist dataset from keras.datasets import fashion_mnist # We download and load the data (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() # Build the encoder input_img = Input(shape=(28, 28, 1)) x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img) x = MaxPooling2D((2, 2), padding='same')(x) x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) x = MaxPooling2D((2, 2), padding='same')(x) x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) encoded_feature_vector = MaxPooling2D((2, 2), padding='same', name='feature_vector')(x) # at this point the representation is (4, 4, 8) i.e. 128-dimensional compressed feature vector # Build the decoder x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded_feature_vector) x = UpSampling2D((2, 2))(x) x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) x = UpSampling2D((2, 2))(x)
from keras.layers.core import Dense, Lambda, Reshape from keras.layers.convolutional import Convolution1D from keras.models import Model LETTER_GRAM_SIZE = 3 # See section 3.2. WINDOW_SIZE = 3 # See section 3.2. TOTAL_LETTER_GRAMS = int(3 * 1e4) # Determined from data. See section 3.2. WORD_DEPTH = WINDOW_SIZE * TOTAL_LETTER_GRAMS # See equation (1). K = 300 # Dimensionality of the max-pooling layer. See section 3.4. L = 128 # Dimensionality of latent semantic space. See section 3.5. J = 4 # Number of random unclicked documents serving as negative examples for a query. See section 4. FILTER_LENGTH = 1 # We only consider one time step for convolutions. # Input tensors holding the query, positive (clicked) document, and negative (unclicked) documents. # The first dimension is None because the queries and documents can vary in length. query = Input(shape=(None, WORD_DEPTH)) pos_doc = Input(shape=(None, WORD_DEPTH)) neg_docs = [Input(shape=(None, WORD_DEPTH)) for j in range(J)] # Query model. The paper uses separate neural nets for queries and documents (see section 5.2). # In this step, we transform each word vector with WORD_DEPTH dimensions into its # convolved representation with K dimensions. K is the number of kernels/filters # being used in the operation. Essentially, the operation is taking the dot product # of a single weight matrix (W_c) with each of the word vectors (l_t) from the # query matrix (l_Q), adding a bias vector (b_c), and then applying the tanh function. # That is, h_Q = tanh(W_c • l_Q + b_c). With that being said, that's not actually # how the operation is being calculated here. To tie the weights of the weight # matrix (W_c) together, we have to use a one-dimensional convolutional layer. # Further, we have to transpose our query matrix (l_Q) so that time is the first # dimension rather than the second (as described in the paper). That is, l_Q[0, :]
def CreateModel(self): ''' 定义CNN/LSTM/CTC模型,使用函数式模型 输入层:200维的特征值序列,一条语音数据的最大长度设为1600(大约16s) 隐藏层:3*3卷积层 隐藏层:池化层,池化窗口大小为2 隐藏层:Dropout层,需要断开的神经元的比例为0.2,防止过拟合 隐藏层:全连接层 目标输出层:全连接层,神经元数量为self.MS_OUTPUT_SIZE,使用softmax作为激活函数 输出层:自定义层,即CTC层,使用CTC的loss作为损失函数,实现连接性时序多输出 ''' # 每一帧使用13维mfcc特征及其13维一阶差分和13维二阶差分表示,最大信号序列长度为1500 input_data = Input(name='the_input', shape=(self.AUDIO_LENGTH, self.AUDIO_FEATURE_LENGTH, 1)) layer_h1 = Conv2D(32, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(input_data) # 卷积层 layer_h1 = Dropout(0.1)(layer_h1) layer_h2 = Conv2D(32, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h1) # 卷积层 layer_h3 = MaxPooling2D(pool_size=2, strides=None, padding="valid")(layer_h2) # 池化层 #layer_h3 = Dropout(0.2)(layer_h2) # 随机中断部分神经网络连接,防止过拟合 layer_h3 = Dropout(0.1)(layer_h3) layer_h4 = Conv2D(64, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h3) # 卷积层 layer_h4 = Dropout(0.2)(layer_h4) layer_h5 = Conv2D(64, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h4) # 卷积层 layer_h6 = MaxPooling2D(pool_size=2, strides=None, padding="valid")(layer_h5) # 池化层 layer_h6 = Dropout(0.2)(layer_h6) layer_h7 = Conv2D(128, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h6) # 卷积层 layer_h7 = Dropout(0.3)(layer_h7) layer_h8 = Conv2D(128, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h7) # 卷积层 layer_h9 = MaxPooling2D(pool_size=2, strides=None, padding="valid")(layer_h8) # 池化层 layer_h9 = Dropout(0.3)(layer_h9) layer_h10 = Conv2D(128, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h9) # 卷积层 layer_h10 = Dropout(0.4)(layer_h10) layer_h11 = Conv2D(128, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h10) # 卷积层 layer_h12 = MaxPooling2D(pool_size=1, strides=None, padding="valid")(layer_h11) # 池化层 #test=Model(inputs = input_data, outputs = layer_h12) #test.summary() layer_h10 = Reshape((200, 3200))(layer_h12) #Reshape层 #layer_h5 = LSTM(256, activation='relu', use_bias=True, return_sequences=True)(layer_h4) # LSTM层 #layer_h6 = Dropout(0.2)(layer_h5) # 随机中断部分神经网络连接,防止过拟合 layer_h10 = Dropout(0.4)(layer_h10) layer_h11 = Dense(128, activation="relu", use_bias=True, kernel_initializer='he_normal')(layer_h10) # 全连接层 layer_h11 = Dropout(0.5)(layer_h11) layer_h12 = Dense(self.MS_OUTPUT_SIZE, use_bias=True, kernel_initializer='he_normal')(layer_h11) # 全连接层 y_pred = Activation('softmax', name='Activation0')(layer_h12) model_data = Model(inputs = input_data, outputs = y_pred) #model_data.summary() labels = Input(name='the_labels', shape=[self.label_max_string_length], dtype='float32') input_length = Input(name='input_length', shape=[1], dtype='int64') label_length = Input(name='label_length', shape=[1], dtype='int64') # Keras doesn't currently support loss funcs with extra parameters # so CTC loss is implemented in a lambda layer #layer_out = Lambda(ctc_lambda_func,output_shape=(self.MS_OUTPUT_SIZE, ), name='ctc')([y_pred, labels, input_length, label_length])#(layer_h6) # CTC loss_out = Lambda(self.ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred, labels, input_length, label_length]) model = Model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out) #model.summary() # clipnorm seems to speeds up convergence #sgd = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5) ada_d = Adadelta(lr = 0.01, rho = 0.95, epsilon = 1e-06) #model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd) model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer = ada_d) # captures output of softmax so we can decode the output during visualization test_func = K.function([input_data], [y_pred]) print('[*提示] 创建模型成功,模型编译成功') return model, model_data