def create_model(self): model = Sequential() model.add(LSTM(self.n_hidden, batch_input_shape=(None, self.maxlen, self.n_in), kernel_initializer=glorot_uniform(seed=20170719), recurrent_initializer=orthogonal(gain=1.0, seed=20170719), dropout=0.3, recurrent_dropout=0.3)) model.add(Dropout(0.3)) model.add(Dense(self.n_out, kernel_initializer=glorot_uniform(seed=20170719))) model.add(Activation("softmax")) model.compile(loss="categorical_crossentropy", optimizer="RMSprop", metrics=['categorical_accuracy']) return model
def create_model(self): model = Sequential() model.add(LSTM(self.n_hidden, batch_input_shape=(None, self.maxlen, self.n_in), kernel_initializer=glorot_uniform(seed=20170719), recurrent_initializer=orthogonal(gain=1.0, seed=20170719), dropout=0.3, recurrent_dropout=0.3)) model.add(Dropout(0.3)) model.add(Dense(1,kernel_initializer=glorot_uniform(seed=20170719))) model.add(Activation("linear")) model.compile(loss="mean_squared_error", optimizer="RMSprop", metrics=['accuracy']) return model
def create_lstm_layer_1(self): ker_in = glorot_uniform(seed=self.seed) rec_in = Orthogonal(seed=self.seed) bioutp = Bidirectional(LSTM(self.hidden_dim, input_shape=(self.max_sequence_length, self.embedding_dim,), kernel_regularizer=None, recurrent_regularizer=None, bias_regularizer=None, activity_regularizer=None, recurrent_dropout=self.recdrop_val, dropout=self.inpdrop_val, kernel_initializer=ker_in, recurrent_initializer=rec_in, return_sequences=True), merge_mode=None) return bioutp
def lstm_layer(self): """Create a LSTM layer of a model.""" if self.pooling: ret_seq = True else: ret_seq = False ker_in = glorot_uniform(seed=self.seed) rec_in = Orthogonal(seed=self.seed) if self.shared_weights: if self.recurrent == "bilstm" or self.recurrent is None: out_a = Bidirectional(LSTM(self.hidden_dim, input_shape=(self.max_sequence_length, self.embedding_dim,), kernel_initializer=ker_in, recurrent_initializer=rec_in, return_sequences=ret_seq), merge_mode='concat') elif self.recurrent == "lstm": out_a = LSTM(self.hidden_dim, input_shape=(self.max_sequence_length, self.embedding_dim,), kernel_initializer=ker_in, recurrent_initializer=rec_in, return_sequences=ret_seq) return out_a, out_a else: if self.recurrent == "bilstm" or self.recurrent is None: out_a = Bidirectional(LSTM(self.hidden_dim, input_shape=(self.max_sequence_length, self.embedding_dim,), kernel_initializer=ker_in, recurrent_initializer=rec_in, return_sequences=ret_seq), merge_mode='concat') out_b = Bidirectional(LSTM(self.hidden_dim, input_shape=(self.max_sequence_length, self.embedding_dim,), kernel_initializer=ker_in, recurrent_initializer=rec_in, return_sequences=ret_seq), merge_mode='concat') elif self.recurrent == "lstm": out_a = LSTM(self.hidden_dim, input_shape=(self.max_sequence_length, self.embedding_dim,), kernel_initializer=ker_in, recurrent_initializer=rec_in, return_sequences=ret_seq) out_b = LSTM(self.hidden_dim, input_shape=(self.max_sequence_length, self.embedding_dim,), kernel_initializer=ker_in, recurrent_initializer=rec_in, return_sequences=ret_seq) return out_a, out_b
def create_lstm_layer_2(self): ker_in = glorot_uniform(seed=self.seed) rec_in = Orthogonal(seed=self.seed) bioutp = Bidirectional(LSTM(self.aggregation_dim, input_shape=(self.max_sequence_length, 8*self.perspective_num,), kernel_regularizer=None, recurrent_regularizer=None, bias_regularizer=None, activity_regularizer=None, recurrent_dropout=self.recdrop_val, dropout=self.inpdrop_val, kernel_initializer=ker_in, recurrent_initializer=rec_in, return_sequences=False), merge_mode='concat', name="sentence_embedding") return bioutp
def fit(self, X, y, X_t, y_t): X = self.transfer_shape(X) X_t = self.transfer_shape(X_t) y = keras.utils.to_categorical(y, 2) y_t = keras.utils.to_categorical(y_t, 2) print(X.shape) if len(X_t) % 2==0: X_test, X_val = np.split(X_t, 2) y_test, y_val = np.split(y_t, 2) else: X_test, X_val = np.split(X_t[:-1,:], 2) y_test, y_val = np.split(y_t[:-1], 2) assert(len(X_test) == len(y_test)) """Hyperparameters""" num_filt_1 = self.num_filt_1 #Number of filters in first conv layer num_filt_2 = self.num_filt_2 #Number of filters in second conv layer num_fc_1 = self.num_fc_1 #Number of neurons in hully connected layer initializer = initializers.glorot_uniform(seed=123) self.classifier.add(Conv2D(filters=num_filt_1, kernel_size=[5,1], padding='same', kernel_initializer=initializer, bias_initializer=initializers.zeros(), input_shape=X.shape[1:])) self.classifier.add(Activation('relu')) self.classifier.add(Conv2D(filters=num_filt_2, kernel_size=[4,1], kernel_initializer=initializer, bias_initializer=initializers.zeros(), padding='same')) #self.classifier.add(BatchNormalization()) self.classifier.add(Activation('relu')) self.classifier.add(Flatten()) self.classifier.add(Dense(num_fc_1, kernel_initializer=initializer,bias_initializer=initializer)) self.classifier.add(Activation('relu')) self.classifier.add(Dropout(0.2, seed=123)) self.classifier.add(Dense(2, kernel_initializer=initializer, bias_initializer=initializers.Constant(0.1))) self.classifier.add(Activation('softmax')) #self.classifier.compile(loss='binary_crossentropy', optimizer=Adam(lr=learning_rate),metrics=['accuracy']) opt = Adam() self.classifier.compile(loss='binary_crossentropy', optimizer=opt,metrics=['accuracy']) self.classifier.fit(X, y, verbose=self.verbose,validation_data=(X_t, y_t), batch_size=self.batch_size, nb_epoch=self.nb_epoch, shuffle=False)
print(test_data) reshaped_array = test_data.values.reshape(9999, 32, 32, 1) print(reshaped_array.shape) print(model_lo) weights = np.asarray(model_lo.get()) print(type(weights)) import tensorflow as tf numClasses = 100 imgRows = 32 imgCols = 32 inputShape = (imgRows, imgCols, 1) np.random.seed(42) tf.set_random_seed(42) static_initializer = glorot_uniform(seed=42) inp = Input(shape=inputShape) conv1 = Conv2D(32, kernel_size=(3, 3), activation='relu', kernel_initializer=static_initializer)(inp) conv2 = Conv2D(64, (3, 3), activation='relu', kernel_initializer=static_initializer)(conv1) pool = MaxPooling2D(pool_size=(2, 2))(conv2) dp1 = Dropout(0.25, seed=42)(pool) fl = Flatten()(dp1) ds = Dense(512, activation='relu', kernel_initializer=static_initializer)(fl) dp2 = Dropout(0.5, seed=42)(ds) outp = Dense(numClasses,
def create_model(init='glorot_normal', activation_1='relu', activation_2='relu', optimizer='SGD', decay=0.1, n_samples=319, input_dim=None): """Define neural network architecture Parameters ---------- init : str initializer to set inital weights activation_1 : str activation function to be used in first hidden layer activation_2 : str activation function to be used in second hidden layer optimizer : str optimization algorithm to find global minimum on loss surface decay : float rate of weight decay n_samples : int number of observations input_dim : int The size of the input array. This is typically inferred from the global X_new variable defined by ANOVASelection, therefore this parameter should only be used outside of sklearn's grid search.. Returns ------- model : keras.models.Sequential compiled keras model Notes ----- X_new is is a global variable defined in the ANOVASelection class. It allows a varying size input_dim, required since percentile is a parameter in the grid search. This is a quick hack to work around the poor flexibility of keras + sklearn. During grid search, anova selection runs first, declaring X_new as a global variable that is then accessible by create_model. Sklearn's pipeline can't pass parameters between "nodes", i.e. the size of the reduced array from ANOVASelection to create_model. And keras isn't designed well enough to infer the dimensionality of input data. Hence the need for this quick hack. This was the case at the time of starting this projects in 2018. Things may have changed since then. """ clear_session() # Determine nodes in hidden layers (Huang et al., 2003) m = 1 # number of ouput neurons hn_1 = int( np.sum( np.sqrt((m + 2) * n_samples) + 2 * np.sqrt(n_samples / (m + 2)))) hn_2 = int(m * np.sqrt(n_samples / (m + 2))) # Create layers model = Sequential() if init == 'glorot_normal': init_seeded = initializers.glorot_normal(seed=0) elif init == 'glorot_uniform': init_seeded = initializers.glorot_uniform(seed=0) # Two hidden layers try: input_dim = np.shape(X_new)[1] except: pass if optimizer == 'SGD': model.add( Dense(hn_1, input_dim=input_dim, kernel_initializer=init_seeded, kernel_regularizer=regularizers.l2(decay), activation=activation_1)) model.add( Dense(hn_2, kernel_initializer=init, kernel_regularizer=regularizers.l2(decay), activation=activation_2)) elif optimizer == 'AdamW': model.add( Dense(hn_1, input_dim=input_dim, kernel_initializer=init_seeded, activation=activation_1)) model.add(Dense(hn_2, kernel_initializer=init, activation=activation_2)) # Output neuron model.add(Dense(1, kernel_initializer=init, activation='sigmoid')) # Compile if optimizer == 'SGD': model.compile(loss='binary_crossentropy', optimizer=SGD(nesterov=True), metrics=["accuracy"]) elif optimizer == 'AdamW': model.compile(loss='binary_crossentropy', optimizer=AdamW(weight_decay=decay), metrics=["accuracy"]) return model
def identity_block(X, f, filters, stage, block): """ Implementation of the identity block as defined in Figure 4 Arguments: X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev) f -- integer, specifying the shape of the middle CONV's window for the main path filters -- python list of integers, defining the number of filters in the CONV layers of the main path stage -- integer, used to name the layers, depending on their position in the network block -- string/character, used to name the layers, depending on their position in the network Returns: X -- output of the identity block, tensor of shape (n_H, n_W, n_C) """ # defining name basis #定义每个节点的名字基准 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' #从filters字典中重新获得滤波器 F1,F2,F3 = filters #转存输入X,将X存储到X_shortcut中,方便在最后和输出相加 X_shortcut = X # 实现主要路径的第一步 X = Conv2D(filters = F1, kernel_size = (1, 1), strides = (1,1), padding = 'valid', name = conv_name_base + '2a', kernel_initializer = glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = bn_name_base + '2a')(X) X = Activation('relu')(X) #实现主要路径的第二部 X = Conv2D(filters = F2, kernel_size = (f, f), strides = (1,1), padding = 'same', name = conv_name_base + '2b', kernel_initializer = glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = bn_name_base + '2b')(X) X = Activation('relu')(X) #实现主要路径的第三步 X = Conv2D(filters = F3, kernel_size = (1, 1), strides = (1,1), padding = 'valid', name = conv_name_base + '2c', kernel_initializer = glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = bn_name_base + '2c')(X) #最后一步 合并两个层 然后使用激活 X = layers.add([X, X_shortcut]) X = Activation('relu')(X) return X
def convolutional_block(X, f, filters, stage, block, s = 2): """ Implementation of the convolutional block as defined in Figure 4 Arguments: X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev) f -- integer, specifying the shape of the middle CONV's window for the main path filters -- python list of integers, defining the number of filters in the CONV layers of the main path stage -- integer, used to name the layers, depending on their position in the network block -- string/character, used to name the layers, depending on their position in the network s -- Integer, specifying the stride to be used Returns: X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C) """ #定义名字基准 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' #从滤波器字典中重新得到滤波器 F1, F2, F3 = filters #保存X的输入为X_shortcut X_shortcut = X #主路径第一层 X = Conv2D(F1, (1, 1), strides = (s,s), name = conv_name_base + '2a', padding='valid', kernel_initializer = glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = bn_name_base + '2a')(X) X = Activation('relu')(X) #主路径第二部 X = Conv2D(F2, (f, f), strides = (1,1), name = conv_name_base + '2b', padding='same', kernel_initializer = glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = bn_name_base + '2b')(X) X = Activation('relu')(X) #主路径第三步 X = Conv2D(F3, (1, 1), strides = (1,1), name = conv_name_base + '2c', padding='valid', kernel_initializer = glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = bn_name_base + '2c')(X) #跳跃层中的链接 X_shortcut = Conv2D(F3, (1, 1), strides = (s,s), name = conv_name_base + '1', padding='valid', kernel_initializer = glorot_uniform(seed=0))(X_shortcut) X_shortcut = BatchNormalization(axis = 3, name = bn_name_base + '1')(X_shortcut) #合并跳跃层和主路径 X = layers.add([X, X_shortcut]) X = Activation('relu')(X) return X
def ResNet50(input_shape=(2560, 1920, 3), classes=2): """ Implementation of the popular ResNet50 the following architecture: CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3 -> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> TOPLAYER IDBLOCK = identity_block CONVBLOCK = convolutional_block Arguments: input_shape -- shape of the images of the dataset classes -- integer, number of classes Returns: model -- a Model() instance in Keras """ # Define the input as a tensor with shape input_shape X_input = Input(input_shape) # Zero-Padding X = ZeroPadding2D((3, 3))(X_input) # Stage 1:CONV2D -> BATCHNORM -> RELU -> MAXPOOL X = Conv2D(64, (7, 7), strides=(2, 2), name='conv1', kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name='bn_conv1')(X) X = Activation('relu')(X) X = MaxPooling2D((3, 3), strides=(2, 2))(X) # Stage 2:CONVBLOCK -> IDBLOCK*2 # f*f卷积核 X = convolutional_block(X, f=3, filters=[64, 64, 256], stage=2, block='a', s=1) X = identity_block(X, f=3, filters=[64, 64, 256], stage=2, block='b') X = identity_block(X, f=3, filters=[64, 64, 256], stage=2, block='c') # Stage 3:CONVBLOCK -> IDBLOCK*3 # The convolutional block uses three set of filters of size [128,128,512], "f" is 3, "s" is 2 and the block is "a". # The 3 identity blocks use three set of filters of size [128,128,512], "f" is 3 and the blocks are "b", "c" and "d". X = convolutional_block(X, f=3, filters=[128, 128, 512], stage=3, block='a', s=2) X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block='b') X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block='c') X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block='d') # Stage 4:CONVBLOCK -> IDBLOCK*5 # The convolutional block uses three set of filters of size [256, 256, 1024], "f" is 3, "s" is 2 and the block is "a". # The 5 identity blocks use three set of filters of size [256, 256, 1024], "f" is 3 and the blocks are "b", "c", "d", "e" and "f". X = convolutional_block(X, f=3, filters=[256, 256, 1024], block='a', stage=4, s=2) X = identity_block(X, f=3, filters=[256, 256, 1024], block='b', stage=4) X = identity_block(X, f=3, filters=[256, 256, 1024], block='c', stage=4) X = identity_block(X, f=3, filters=[256, 256, 1024], block='d', stage=4) X = identity_block(X, f=3, filters=[256, 256, 1024], block='e', stage=4) X = identity_block(X, f=3, filters=[256, 256, 1024], block='f', stage=4) # Stage 5:CONVBLOCK -> IDBLOCK*2 # The convolutional block uses three set of filters of size [512, 512, 2048], "f" is 3, "s" is 2 and the block is "a". # The 2 identity blocks use three set of filters of size [256, 256, 2048], "f" is 3 and the blocks are "b" and "c". X = convolutional_block(X, f=3, filters=[512, 512, 2048], stage=5, block='a', s=2) # filters should be [256, 256, 2048], but it fail to be graded. Use [512, 512, 2048] to pass the grading X = identity_block(X, f=3, filters=[256, 256, 2048], stage=5, block='b') X = identity_block(X, f=3, filters=[256, 256, 2048], stage=5, block='c') # 平均池化层 AVGPOOL. # 2*2 的窗口 X = AveragePooling2D(pool_size=(2, 2))(X) # 扁平化,eg.(None, 64, 32, 32) --> (None, 65536) X = Flatten()(X) # 全连接层 # 这里的softmax有一点问题 # keras版本2.1.6,函数softmax()有一个参数为axis,传给tf.nn.softmax()。在tf中是dim,而在keras中用了axis=axis,改成dim=axis即可。 # 具体的方法 https://neyzoter.github.io/2018/07/13/keras-softmax-err/ X = Dense(classes, activation='softmax', name='fc' + str(classes), kernel_initializer=glorot_uniform(seed=0))(X) # Create model model = Model(inputs=X_input, outputs=X, name='ResNet50') return model
def convolutional_block(X, f, filters, stage, block, s = 2): """ Implementation of the convolutional block as defined in Figure 4 Arguments: X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev) f -- integer, specifying the shape of the middle CONV's window for the main path filters -- python list of integers, defining the number of filters in the CONV layers of the main path stage -- integer, used to name the layers, depending on their position in the network block -- string/character, used to name the layers, depending on their position in the network s -- Integer, specifying the stride to be used Returns: X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C) """ # defining name basis conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' # Retrieve Filters F1, F2, F3 = filters # Save the input value X_shortcut = X ##### MAIN PATH ##### # First component of main path X = Conv2D(F1, (1, 1), strides = (s,s), name = conv_name_base + '2a', kernel_initializer = glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = bn_name_base + '2a')(X) X = Activation('relu')(X) ### START CODE HERE ### # Second component of main path (≈3 lines) X = Conv2D(F2, (f, f), strides = (1,1), name = conv_name_base + '2b', padding = 'same', kernel_initializer = glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = bn_name_base + '2b')(X) X = Activation('relu')(X) # Third component of main path (≈2 lines) X = Conv2D(F3, (1, 1), strides = (1,1), name = conv_name_base + '2c', padding = 'valid', kernel_initializer = glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = bn_name_base + '2c')(X) ##### SHORTCUT PATH #### (≈2 lines) X_shortcut = Conv2D(F3, (1, 1), strides = (s,s), name = conv_name_base + '1', kernel_initializer = glorot_uniform(seed=0))(X_shortcut) X_shortcut = BatchNormalization(axis = 3, name = bn_name_base + '1')(X_shortcut) # Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines) X = Add()([X_shortcut, X]) X = Activation('relu')(X) ### END CODE HERE ### return X
def ResNet50(input_shape=(1, row, col), classes=8): X_input = Input(input_shape) # stage 1 X = Conv2D(filters=4, kernel_size=(3, 3), strides=(1, 1), padding='valid', name='conv1', kernel_initializer=glorot_uniform(seed=0))(X_input) X = BatchNormalization(axis=3, name='bn_conv1')(X) X = Activation('relu')(X) X = MaxPooling2D((2, 2), strides=(2, 2))(X) # stage 2 X = convolutional_block(X, f=3, filters=[4, 4, 16], stage=2, block='a', s=1) X = identity_block(X, f=3, filters=[4, 4, 16], stage=2, block='b') X = identity_block(X, f=3, filters=[4, 4, 16], stage=2, block='c') # stage 3 X = convolutional_block(X, f=3, filters=[8, 8, 32], stage=3, block='a', s=1) X = identity_block(X, f=3, filters=[8, 8, 32], stage=3, block='b') X = identity_block(X, f=3, filters=[8, 8, 32], stage=3, block='c') X = identity_block(X, f=3, filters=[8, 8, 32], stage=3, block='d') # stage 4 X = convolutional_block(X, f=3, filters=[16, 16, 64], stage=4, block='a', s=1) X = identity_block(X, f=3, filters=[16, 16, 64], stage=4, block='b') X = identity_block(X, f=3, filters=[16, 16, 64], stage=4, block='c') X = identity_block(X, f=3, filters=[16, 16, 64], stage=4, block='d') X = identity_block(X, f=3, filters=[16, 16, 64], stage=4, block='e') X = identity_block(X, f=3, filters=[16, 16, 64], stage=4, block='f') # stage 5 X = convolutional_block(X, f=3, filters=[16, 16, 64], stage=5, block='a', s=1) X = identity_block(X, f=3, filters=[16, 16, 64], stage=5, block='b') X = identity_block(X, f=3, filters=[16, 16, 64], stage=5, block='c') # fully connected output layer X = Flatten(name='flatten')(X) X = Dense(256, activation='relu', name='fc1')(X) X = Dense(classes, activation='softmax', name='fc2')(X) model = Model(inputs=X_input, outputs=X, name='ResNet50_semg') return model
def convolutional_block(X, f, filters, stage, block, s=2): """ Implementation of the identity block Arguments: X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev) f -- integer, 主路径中间的那个CONV的窗口形状 filters -- python整数列表, 定义主路径每个CONV层中的滤波器的数量 stage --整数,用于命名层,取决于他们在网络中的位置 阶段 block --字符串/字符,用于命名层,取决于他们在网络中的位置 块 s -- 整数,指定滑动的大小 Returns: X -- output of the identity block, tensor of shape (n_H, n_W, n_C) """ # defining name basis conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' F1, F2, F3 = filters # save the input value X_shortcut = X # first component of main path X = Conv2D(filters=F1, kernel_size=(1, 1), strides=(s, s), padding='valid', name=conv_name_base + '2a', kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X) X = Activation('relu')(X) # second component of main path X = Conv2D(filters=F2, kernel_size=(f, f), strides=(1, 1), padding='same', name=conv_name_base + '2b', kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X) X = Activation('relu')(X) # Third component of main path X = Conv2D(filters=F3, kernel_size=(1, 1), strides=(1, 1), padding='valid', name=conv_name_base + '2c', kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name=bn_name_base + '2c')(X) # shortcut path X_shortcut = Conv2D(filters=F3, kernel_size=(1, 1), strides=(s, s), padding='valid', name=conv_name_base + '1', kernel_initializer=glorot_uniform(seed=0))(X_shortcut) X_shortcut = BatchNormalization(axis=3, name=bn_name_base + '1')(X_shortcut) # Final step # Add shortcut value to main path, and pass it through a ReLU activation X = Add()([X, X_shortcut]) X = Activation('relu')(X) return X
def create_model(anchors, class_names, load_pretrained=True, freeze_body=True, reset_weights=True): ''' returns the body of the model and the model # Params: load_pretrained: whether or not to load the pretrained model or initialize all weights freeze_body: whether or not to freeze all weights except for the last layer's # Returns: model_body: YOLOv2 with new output layer model: YOLOv2 with custom loss Lambda layer ''' detectors_mask_shape = (13, 13, 5, 1) matching_boxes_shape = (13, 13, 5, 5) # Create model input layers. image_input = Input(shape=(416, 416, 3)) boxes_input = Input(shape=(None, 5)) detectors_mask_input = Input(shape=detectors_mask_shape) matching_boxes_input = Input(shape=matching_boxes_shape) # Create model body. yolo_model = yolo_body(image_input, len(anchors), len(class_names)) topless_yolo = Model(yolo_model.input, yolo_model.layers[-2].output) if load_pretrained: # Save topless yolo: topless_yolo_path = os.path.join('EDLyolo/model_data', 'yolo_topless.h5') if not os.path.exists(topless_yolo_path): print("CREATING TOPLESS WEIGHTS FILE") yolo_path = os.path.join('EDLyolo/model_data', 'yolo.h5') model_body = load_model(yolo_path) model_body = Model(model_body.inputs, model_body.layers[-2].output) model_body.save_weights(topless_yolo_path) topless_yolo.load_weights(topless_yolo_path) # Reset Weights if reset_weights: sess = K.get_session() initial_weights = topless_yolo.get_weights() from keras.initializers import glorot_uniform # Or your initializer of choice k = 65 new_weights = [glorot_uniform()(initial_weights[i].shape).eval(session=sess) if i>k else initial_weights[i] for i in range(len(initial_weights))] topless_yolo.set_weights(new_weights) if freeze_body: for layer in topless_yolo.layers[:-30]: layer.trainable = False output_size = len(anchors)*(5+len(class_names)) final_layer = Conv2D( output_size , (1, 1), activation='linear')( topless_yolo.output ) model_body = Model(image_input, final_layer) with tf.device('/cpu:0'): global_step = K.variable(0.) model_metrics = Lambda( custom_loss, output_shape=(12, ), name='yolo_metrics', arguments={'anchors': anchors, 'num_classes': len(class_names), 'global_step': global_step})([ model_body.output, boxes_input, detectors_mask_input, matching_boxes_input ]) # Total Loss Components total_loss = Lambda(lambda x: x[0], name='yolo_loss')( model_metrics ) loss_conf = Lambda(lambda x: x[1], name='loss_conf')( model_metrics ) loss_class = Lambda(lambda x: x[2], name='loss_class')( model_metrics ) loss_edl = Lambda(lambda x: x[3], name='loss_edl')( model_metrics ) loss_coord = Lambda(lambda x: x[4], name='loss_coord') ( model_metrics ) # EDL Metrics accrcy = Lambda(lambda x: x[5], name='accrcy')( model_metrics ) ev_succ = Lambda(lambda x: x[6], name='ev_succ')( model_metrics ) ev_fail = Lambda(lambda x: x[7], name='ev_fail')( model_metrics ) # EDL Loss Components annealing_coeff = Lambda(lambda x: x[8], name='annealing_coeff')( model_metrics ) loss_exp_ce = Lambda(lambda x: x[9], name='loss_exp_ce')( model_metrics ) loss_kl = Lambda(lambda x: x[10], name='loss_kl')( model_metrics ) loss_akl = Lambda(lambda x: x[11], name='loss_akl')( model_metrics ) # Model Inputs inputs = [model_body.input, boxes_input, detectors_mask_input, matching_boxes_input] # Model Outputs loss_breakdown = [total_loss, loss_conf, loss_class, loss_edl, loss_coord] edl_metrics = [accrcy, ev_succ, ev_fail] edl_loss_components = [annealing_coeff, loss_exp_ce, loss_kl, loss_akl] outputs = loss_breakdown + edl_metrics + edl_loss_components # Build Model model = Model( inputs, outputs ) # Custom metrics are outputs of Lamda layers so must hack into a metric # format as below... must be a better way to do this. # CUSTOM METRICS - EDL Metrics def class_acc(y_true, y_pred): return accrcy def succ_ev(y_true, y_pred): return ev_succ def fail_ev(y_true, y_pred): return ev_fail edl_metrics = [class_acc, succ_ev, fail_ev] # CUSTOM METRICS - TOTAL LOSS BREAKDOWN def conf_loss(y_true, y_pred): return loss_conf def class_loss(y_true, y_pred): return loss_class def edl_loss(y_true, y_pred): return loss_edl def coord_loss(y_true, y_pred): return loss_coord track_losses = [conf_loss, class_loss, edl_loss, coord_loss] # CUSTOM METRICS - EDL LOSS BREAKDOWN def coeff_anneal(y_true, y_pred): return annealing_coeff def exp_ce_loss(y_true, y_pred): return loss_exp_ce def kl_loss(y_true, y_pred): return loss_kl def akl_loss(y_true, y_pred): return loss_akl edl_loss_comps = [coeff_anneal, exp_ce_loss, kl_loss, akl_loss] metrics = edl_metrics + track_losses + edl_loss_comps return model_body, model, global_step, metrics
def test_glorot_uniform(tensor_shape): fan_in, fan_out = initializers._compute_fans(tensor_shape) scale = np.sqrt(6. / (fan_in + fan_out)) _runner(initializers.glorot_uniform(), tensor_shape, target_mean=0., target_max=scale, target_min=-scale)
def convolutional_block(X, f, filters, stage, block, s = 2): """ Implementation of the convolutional block as defined in Figure 4 Arguments: X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev) f -- integer, specifying the shape of the middle CONV's window for the main path filters -- python list of integers, defining the number of filters in the CONV layers of the main path stage -- integer, used to name the layers, depending on their position in the network block -- string/character, used to name the layers, depending on their position in the network s -- Integer, specifying the stride to be used Returns: X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C) """ # defining name basis conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' # Retrieve Filters F1, F2, F3 = filters # Save the input value X_shortcut = X ##### MAIN PATH ##### # First component of main path X = Conv2D(F1, kernel_size=(1, 1), strides = (s,s), name = conv_name_base + '2a', kernel_initializer = glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = bn_name_base + '2a')(X) X = Activation('relu')(X) ### START CODE HERE ### # Second component of main path (≈3 lines) X = Conv2D(filters=F2, kernel_size=(f,f), strides=(1,1), padding='same', name=conv_name_base + '2b', kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X) X = Activation('relu')(X) # Third component of main path (≈2 lines) X = Conv2D(filters=F3, kernel_size=(1,1), strides=(1,1), padding='valid',name=conv_name_base+'2c', kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name=bn_name_base+'2c')(X) ##### SHORTCUT PATH #### (≈2 lines) X_shortcut = Conv2D(filters=F3, kernel_size=(1,1), strides=(s,s), padding='valid', name=conv_name_base+'1',kernel_initializer=glorot_uniform(seed=0))(X_shortcut) X_shortcut = BatchNormalization(axis=3, name=bn_name_base+'1')(X_shortcut) # Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines) X = Add()([X,X_shortcut]) X = Activation('relu')(X) ### END CODE HERE ### return X
def VGG16(input_shape, output_shape, output_name='fc1000', weights=None, model_name='VGG16', input_tensor=None): '''Instantiate the VGG16 architecture, optionally loading weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_dim_ordering="tf"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The dimension ordering convention used by the model is the one specified in your Keras config file. # Arguments include_top: whether to include the 3 fully-connected layers at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. # Returns A Keras model instance. ''' # Determine proper input shape x_input = Input(shape=input_shape) # Extra padding layer to make sure we can reach the mini size requirement of ResNet50 min_size = 48 if input_shape[0] < min_size: min_padding = (min_size - input_shape[0]) // 2 x = ZeroPadding2D((min_padding, min_padding))(x_input) else: x = x_input # Block 1 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(x_input) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) x = Flatten()(x) x = Dense(output_shape, activation='softmax', name=output_name, kernel_initializer=glorot_uniform())(x) # Create model model = Model(x_input, x, name=model_name) # load weights if weights: print('Loading existing weight from %s', weights) model.load_weights(weights, by_name=True) return model
def ResNet(x_train, y_train, x_test, y_test, num_classes, batch_size, epochs, img_rows, img_cols, channels, middle_layers_activation, last_layer_activation, labels, checkpointslog, class_weight, model_unique_name): input_shape = (img_rows, img_cols, channels) X_input = Input(input_shape) # Zero-Padding X = ZeroPadding2D((3, 3))(X_input) # Stage 1 X = Conv2D(img_rows, (7, 7), strides=(2, 2), name='conv1', kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name='bn_conv1')(X) X = Activation(middle_layers_activation)(X) X = MaxPooling2D((3, 3), strides=(2, 2))(X) # Stage 2 X = convolutional_block(X, f=3, filters=[img_rows, img_rows, 256], stage=2, block='a', s=1, middle_layers_activation) X = identity_block(X, 3, [img_rows, img_rows, 256], stage=2, block='b', middle_layers_activation) X = identity_block(X, 3, [img_rows, img_rows, 256], stage=2, block='c', middle_layers_activation) # AVGPOOL (≈1 line). Use "X = AveragePooling2D(...)(X)" X = AveragePooling2D(pool_size=(2, 2), padding='same')(X) X = Flatten()(X) X = Dense(num_classes, activation=last_layer_activation, name='fc' + str(num_classes), kernel_initializer=glorot_uniform(seed=0))(X) model = Model(inputs=X_input, outputs=X, name='ResNet50') model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) datagen = data_denerator() history = model.fit_generator( datagen.flow(x_train, y_train, batch_size=batch_size), steps_per_epoch=len(x_train) / 32, epochs=epochs, class_weight=class_weight, shuffle=True, validation_data=[x_test, y_test], callbacks=[MetricsCheckpoint(checkpointslog)]) score = model.evaluate(x_test, y_test, verbose=0) print('\n ResNet - accuracy:', score[1], '\n') y_pred = model.predict(x_test) print('\n', sklearn.metrics.classification_report(np.where(y_test > 0)[1], np.argmax(y_pred, axis=1), target_names=list( labels.values())), sep='') Y_pred_classes = np.argmax(y_pred, axis=1) Y_true = np.argmax(y_test, axis=1) plotKerasLearningCurve(checkpointslog) plt.show() plot_learning_curve(history) plt.show() confusion_mtx = confusion_matrix(Y_true, Y_pred_classes) plot_confusion_matrix(confusion_mtx, classes=list(labels.values())) plt.show() model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json) # serialize weights to HDF5 model.save_weights(model_unique_name) print("Saved model to disk")
def identity_block(X, f, filters, stage, block): """ 实现图3的恒等块 参数: X - 输入的tensor类型的数据,维度为( m, n_H_prev, n_W_prev, n_H_prev ) f - 整数,指定主路径中间的CONV窗口的维度 filters - 整数列表,定义了主路径每层的卷积层的过滤器数量 stage - 整数,根据每层的位置来命名每一层,与block参数一起使用。 block - 字符串,据每层的位置来命名每一层,与stage参数一起使用。 返回: X - 恒等块的输出,tensor类型,维度为(n_H, n_W, n_C) """ #定义命名规则 conv_name_base = "res" + str(stage) + block + "_branch" bn_name_base = "bn" + str(stage) + block + "_branch" #获取过滤器 F1, F2, F3 = filters #保存输入数据,将会用于为主路径添加捷径 X_shortcut = X #主路径的第一部分 ##卷积层 X = Conv2D(filters=F1, kernel_size=(1, 1), strides=(1, 1), padding="valid", name=conv_name_base + "2a", kernel_initializer=glorot_uniform(seed=0))(X) ##归一化 X = BatchNormalization(axis=3, name=bn_name_base + "2a")(X) ##使用ReLU激活函数 X = Activation("relu")(X) #主路径的第二部分 ##卷积层 X = Conv2D(filters=F2, kernel_size=(f, f), strides=(1, 1), padding="same", name=conv_name_base + "2b", kernel_initializer=glorot_uniform(seed=0))(X) ##归一化 X = BatchNormalization(axis=3, name=bn_name_base + "2b")(X) ##使用ReLU激活函数 X = Activation("relu")(X) #主路径的第三部分 ##卷积层 X = Conv2D(filters=F3, kernel_size=(1, 1), strides=(1, 1), padding="valid", name=conv_name_base + "2c", kernel_initializer=glorot_uniform(seed=0))(X) ##归一化 X = BatchNormalization(axis=3, name=bn_name_base + "2c")(X) ##没有ReLU激活函数 #最后一步: ##将捷径与输入加在一起 X = Add()([X, X_shortcut]) ##使用ReLU激活函数 X = Activation("relu")(X) return X
def _getKerasModelWeightInitializer(self): """ Get initializer for a set of weights (e.g. the kernel/bias of a single Dense layer) within a Keras model. """ return glorot_uniform(seed=self.RANDOM_SEED)
def ResNet50(input_shape=(64, 64, 3), classes=6): """ 实现ResNet50 CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3 -> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> TOPLAYER 参数: input_shape - 图像数据集的维度 classes - 整数,分类数 返回: model - Keras框架的模型 """ #定义tensor类型的输入数据 X_input = Input(input_shape) #0填充 X = ZeroPadding2D((3, 3))(X_input) #stage1 X = Conv2D(filters=64, kernel_size=(7, 7), strides=(2, 2), name="conv1", kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name="bn_conv1")(X) X = Activation("relu")(X) X = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(X) #stage2 X = convolutional_block(X, f=3, filters=[64, 64, 256], stage=2, block="a", s=1) X = identity_block(X, f=3, filters=[64, 64, 256], stage=2, block="b") X = identity_block(X, f=3, filters=[64, 64, 256], stage=2, block="c") #stage3 X = convolutional_block(X, f=3, filters=[128, 128, 512], stage=3, block="a", s=2) X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block="b") X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block="c") X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block="d") #stage4 X = convolutional_block(X, f=3, filters=[256, 256, 1024], stage=4, block="a", s=2) X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="b") X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="c") X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="d") X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="e") X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="f") #stage5 X = convolutional_block(X, f=3, filters=[512, 512, 2048], stage=5, block="a", s=2) X = identity_block(X, f=3, filters=[512, 512, 2048], stage=5, block="b") X = identity_block(X, f=3, filters=[512, 512, 2048], stage=5, block="c") #均值池化层 X = AveragePooling2D(pool_size=(2, 2), padding="same")(X) #输出层 X = Flatten()(X) X = Dense(classes, activation="softmax", name="fc" + str(classes), kernel_initializer=glorot_uniform(seed=0))(X) #创建模型 model = Model(inputs=X_input, outputs=X, name="ResNet50") return model
def create_model(self) -> Model: if self.use_matrix: context = Input(shape=(self.max_sequence_length,)) response = Input(shape=(self.max_sequence_length,)) emb_layer = self.embedding_layer() emb_c = emb_layer(context) emb_r = emb_layer(response) else: context = Input(shape=(self.max_sequence_length, self.embedding_dim,)) response = Input(shape=(self.max_sequence_length, self.embedding_dim,)) emb_c = context emb_r = response lstm_layer = self.create_lstm_layer_1() lstm_a = lstm_layer(emb_c) lstm_b = lstm_layer(emb_r) f_layer_f = FullMatchingLayer(self.perspective_num) f_layer_b = FullMatchingLayer(self.perspective_num) f_a_forw = f_layer_f([lstm_a[0], lstm_b[0]])[0] f_a_back = f_layer_b([Lambda(lambda x: K.reverse(x, 1))(lstm_a[1]), Lambda(lambda x: K.reverse(x, 1))(lstm_b[1])])[0] f_a_back = Lambda(lambda x: K.reverse(x, 1))(f_a_back) f_b_forw = f_layer_f([lstm_b[0], lstm_a[0]])[0] f_b_back = f_layer_b([Lambda(lambda x: K.reverse(x, 1))(lstm_b[1]), Lambda(lambda x: K.reverse(x, 1))(lstm_a[1])])[0] f_b_back = Lambda(lambda x: K.reverse(x, 1))(f_b_back) mp_layer_f = MaxpoolingMatchingLayer(self.perspective_num) mp_layer_b = MaxpoolingMatchingLayer(self.perspective_num) mp_a_forw = mp_layer_f([lstm_a[0], lstm_b[0]])[0] mp_a_back = mp_layer_b([lstm_a[1], lstm_b[1]])[0] mp_b_forw = mp_layer_f([lstm_b[0], lstm_a[0]])[0] mp_b_back = mp_layer_b([lstm_b[1], lstm_a[1]])[0] at_layer_f = AttentiveMatchingLayer(self.perspective_num) at_layer_b = AttentiveMatchingLayer(self.perspective_num) at_a_forw = at_layer_f([lstm_a[0], lstm_b[0]])[0] at_a_back = at_layer_b([lstm_a[1], lstm_b[1]])[0] at_b_forw = at_layer_f([lstm_b[0], lstm_a[0]])[0] at_b_back = at_layer_b([lstm_b[1], lstm_a[1]])[0] ma_layer_f = MaxattentiveMatchingLayer(self.perspective_num) ma_layer_b = MaxattentiveMatchingLayer(self.perspective_num) ma_a_forw = ma_layer_f([lstm_a[0], lstm_b[0]])[0] ma_a_back = ma_layer_b([lstm_a[1], lstm_b[1]])[0] ma_b_forw = ma_layer_f([lstm_b[0], lstm_a[0]])[0] ma_b_back = ma_layer_b([lstm_b[1], lstm_a[1]])[0] concat_a = Lambda(lambda x: K.concatenate(x, axis=-1))([f_a_forw, f_a_back, mp_a_forw, mp_a_back, at_a_forw, at_a_back, ma_a_forw, ma_a_back]) concat_b = Lambda(lambda x: K.concatenate(x, axis=-1))([f_b_forw, f_b_back, mp_b_forw, mp_b_back, at_b_forw, at_b_back, ma_b_forw, ma_b_back]) concat_a = Dropout(self.ldrop_val)(concat_a) concat_b = Dropout(self.ldrop_val)(concat_b) lstm_layer_agg = self.create_lstm_layer_2() agg_a = lstm_layer_agg(concat_a) agg_b = lstm_layer_agg(concat_b) agg_a = Dropout(self.dropout_val)(agg_a) agg_b = Dropout(self.dropout_val)(agg_b) reduced = Lambda(lambda x: K.concatenate(x, axis=-1))([agg_a, agg_b]) if self.triplet_mode: dist = Lambda(self._pairwise_distances)([agg_a, agg_b]) else: ker_in = glorot_uniform(seed=self.seed) dense = Dense(self.dense_dim, kernel_initializer=ker_in)(reduced) dist = Dense(1, activation='sigmoid', name="score_model")(dense) model = Model([context, response], dist) return model
def convolutional_block(X, f, filters, stage, block, s=2): """ 实现图5的卷积块 参数: X - 输入的tensor类型的变量,维度为( m, n_H_prev, n_W_prev, n_C_prev) f - 整数,指定主路径中间的CONV窗口的维度 filters - 整数列表,定义了主路径每层的卷积层的过滤器数量 stage - 整数,根据每层的位置来命名每一层,与block参数一起使用。 block - 字符串,据每层的位置来命名每一层,与stage参数一起使用。 s - 整数,指定要使用的步幅 返回: X - 卷积块的输出,tensor类型,维度为(n_H, n_W, n_C) """ #定义命名规则 conv_name_base = "res" + str(stage) + block + "_branch" bn_name_base = "bn" + str(stage) + block + "_branch" #获取过滤器数量 F1, F2, F3 = filters #保存输入数据 X_shortcut = X #主路径 ##主路径第一部分 X = Conv2D(filters=F1, kernel_size=(1, 1), strides=(s, s), padding="valid", name=conv_name_base + "2a", kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name=bn_name_base + "2a")(X) X = Activation("relu")(X) ##主路径第二部分 X = Conv2D(filters=F2, kernel_size=(f, f), strides=(1, 1), padding="same", name=conv_name_base + "2b", kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name=bn_name_base + "2b")(X) X = Activation("relu")(X) ##主路径第三部分 X = Conv2D(filters=F3, kernel_size=(1, 1), strides=(1, 1), padding="valid", name=conv_name_base + "2c", kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name=bn_name_base + "2c")(X) #捷径 X_shortcut = Conv2D(filters=F3, kernel_size=(1, 1), strides=(s, s), padding="valid", name=conv_name_base + "1", kernel_initializer=glorot_uniform(seed=0))(X_shortcut) X_shortcut = BatchNormalization(axis=3, name=bn_name_base + "1")(X_shortcut) #最后一步 X = Add()([X, X_shortcut]) X = Activation("relu")(X) return X
def cnn(state, n_mels, inputlength, n_classes): # set random seed for replicability tf.set_random_seed(state['random_seed']) np.random.seed(state['random_seed']) init = glorot_uniform(seed=state['random_seed']) # initialize empty model model = Sequential() # add channel for convolutional layers model.add( Reshape(input_shape=(inputlength, n_mels), target_shape=(inputlength, n_mels, 1), name='reshape')) # convolutional layer 1 model.add( Convolution2D(kernel_size=(state['kernel_size1'], state['kernel_size2']), filters=state['number_filters'], strides=(state['stride1'], state['stride2']), padding="valid", kernel_initializer=init, input_shape=(inputlength, n_mels, 1))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(state['conv_dropout'], seed=state['random_seed'])) # convolutional layer 2 model.add( Convolution2D(kernel_size=(state['kernel_size1'], state['kernel_size2']), filters=state['number_filters'], strides=(state['stride1'], state['stride2']), padding="valid", kernel_initializer=init)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(state['conv_dropout'], seed=state['random_seed'])) # convolutional layer 3 model.add( Convolution2D(kernel_size=(state['kernel_size1'], state['kernel_size2']), filters=state['number_filters'], strides=(state['stride1'], state['stride2']), padding="valid", kernel_initializer=init)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(state['conv_dropout'], seed=state['random_seed'])) model.add(Flatten()) # dense layer 1 model.add(Dense(units=state['n_hidden_dense'], kernel_initializer=init)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(state['dense_dropout'], seed=state['random_seed'])) # softmax layer model.add(Dense(units=n_classes, kernel_initializer=init)) model.add(Activation('softmax')) print(model.summary()) return model
def combine_model(multi_gpu=True, num_gpus=4): img_X_input = Input(shape=(112, 112, 3)) lstm_X_input = Input(shape=(29, 2)) img_X = Conv2D(32, (7, 7), padding='same', strides=(2, 2), name='conv1', kernel_initializer=glorot_uniform())(img_X_input) #img_X = BatchNormalization(axis = 3, name = 'bn_conv1')(img_X) img_X = Activation('relu')(img_X) img_X = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(img_X) img_X = convolutional_block(img_X, f=3, filters=[32, 32, 128], stage=2, block='a', s=1) img_X = convolutional_block(img_X, f=3, filters=[64, 64, 256], stage=3, block='a', s=2) img_X = convolutional_block(img_X, f=3, filters=[128, 128, 512], stage=4, block='a', s=2) img_X = AveragePooling2D(pool_size=(7, 7), strides=None, padding='valid')(img_X) img_X = Flatten()(img_X) #img_X_more=img_X img_X_more = Dense(500, activation='relu')(img_X) img_X_more = Dropout(rate=0.5)(img_X_more) img_X_more = Dense(100, activation='relu')(img_X_more) img_X_more = Dropout(rate=0.5)(img_X_more) img_X_more = Dense(25, activation='relu')(img_X_more) img_X_more = Dropout(rate=0.5)(img_X_more) img_X_more = Dense(1, activation='sigmoid')(img_X_more) lstm_X = LSTM(units=64, return_sequences=True, stateful=False)(lstm_X_input) lstm_X = LSTM(units=64, return_sequences=False, stateful=False)(lstm_X) #lstm_X_more=lstm_X #lstm_X_more=Dropout(rate=0.2)(lstm_X) lstm_X_more = Dense(500, activation='relu')(lstm_X) lstm_X_more = Dense(100, activation='relu')(lstm_X_more) #lstm_X_more=Dropout(rate=0.2)(lstm_X_more) lstm_X_more = Dense(25, activation='relu')(lstm_X_more) #lstm_X_more=Dropout(rate=0.2)(lstm_X_more) lstm_X_more = Dense(1, activation='sigmoid')(lstm_X_more) #consider output will reduce neuron again fused = Concatenate()([img_X, lstm_X]) #fused=Dense(500,activation='relu')(fused) #fused=Dropout(rate=0.3)(fused) fused = Dense(100, activation='relu')(fused) #fused=Dropout(rate=0.3)(fused) fused = Dense(25, activation='relu')(fused) #fused=Dropout(rate=0.3)(fused) fused = Dense(1, activation='sigmoid')(fused) model = Model(inputs=[img_X_input, lstm_X_input], outputs=fused) def root_mean_squared_error(y_true, y_pred): #print ("y_ture: ", y_true) #print ("y_pred: ", y_pred) #print("img_X",img_X.shape) #img_X,lstm_X,fused=combine_model() #loss1=K.sqrt(K.mean(K.square(img_X_more - y_true), axis=-1)) #loss2=K.sqrt(K.mean(K.square(lstm_X_more - y_true), axis=-1)) #loss3=K.sqrt(K.mean(K.square(fused- y_true), axis=-1)) loss1 = mse(y_true, img_X_more) loss2 = mse(y_true, lstm_X_more) loss3 = mse(y_true, fused) #0.2*loss1+0.2*loss2+ return 0.2 * K.sqrt(loss1) + 0.2 * K.sqrt(loss2) + K.sqrt(loss3) print(model.summary()) adam = optimizers.Adam(lr=1e-3, decay=5e-4) model.compile(loss=root_mean_squared_error, optimizer=adam) #return img_X,lstm_X,fused if (multi_gpu == True): parallel_model = multi_gpu_model(model, gpus=num_gpus) parallel_model.compile(optimizer=adam, loss=root_mean_squared_error, metrics=[root_mean_squared_error]) return model, parallel_model return model
def ResNet50(input_shape = (64, 64, 3), classes = 6): """ Implementation of the popular ResNet50 the following architecture: CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3 -> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> TOPLAYER Arguments: input_shape -- shape of the images of the dataset classes -- integer, number of classes Returns: model -- a Model() instance in Keras """ #将输入通过keras API修改为 tensor张量 X_input = Input(input_shape) #处理输入数据 X = ZeroPadding2D((3,3))(X_input) #第一步 X = Conv2D(64, (7, 7), strides = (2, 2), name = 'conv1', kernel_initializer = glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = 'bn_conv1')(X) X = Activation('relu')(X) X = MaxPooling2D((3, 3), strides=(2, 2))(X) #第二步 X = convolutional_block(X, f = 3, filters = [64, 64, 256], stage = 2, block='a', s = 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 = convolutional_block(X, f = 3, filters = [128, 128, 512], stage = 3, block='a', s = 2) X = identity_block(X, 3, [128, 128, 512], stage=3, block='b') X = identity_block(X, 3, [128, 128, 512], stage=3, block='c') X = identity_block(X, 3, [128, 128, 512], stage=3, block='d') #第四步 X = convolutional_block(X, f = 3, filters = [256, 256, 1024], stage = 4, block='a', s = 2) X = identity_block(X, 3, [256, 256, 1024], stage=4, block='b') X = identity_block(X, 3, [256, 256, 1024], stage=4, block='c') X = identity_block(X, 3, [256, 256, 1024], stage=4, block='d') X = identity_block(X, 3, [256, 256, 1024], stage=4, block='e') X = identity_block(X, 3, [256, 256, 1024], stage=4, block='f') #第五步 X = convolutional_block(X, f = 3, filters = [512, 512, 2048], stage = 5, block='a', s = 2) X = identity_block(X, 3, [256, 256, 2048], stage=5, block='b') X = identity_block(X, 3, [256, 256, 2048], stage=5, block='c') #平均池化 X = AveragePooling2D(pool_size=(2,2))(X) X = Flatten()(X) X = Dense(classes, activation='softmax', name='fc' + str(classes), kernel_initializer = glorot_uniform(seed=0))(X) # Create model model = Model(inputs = X_input, outputs = X, name='ResNet50') return model
def ResNet(input_shape=IMAGE_SIZE, classes=NUM_CLASSES, filter_sizes=[64, 128, 256, 512], num_channels=[256, 512, 1024, 2048], num_layers=[3, 4, 6, 3], se_ratio=0, gp_cardinality=0, feature='global_max_pooling', deeper_fc=False): """ Build (SE)ResNe(X)t models ResNet paper: https://arxiv.org/abs/1512.03385.pdf SE-ResNet paper: https://arxiv.org/pdf/1709.01507.pdf ResNeXt paper: https://arxiv.org/pdf/1611.05431.pdf """ total_layers = 2 + 3 * sum(num_layers) model_name = 'Res' if se_ratio > 0: model_name = 'SE-' + model_name if gp_cardinality > 0: model_name = model_name + 'NeXt' else: model_name = model_name + 'Net' model_name += str(total_layers) X_input = Input(input_shape) X = ZeroPadding2D((3, 3))(X_input) # conv1_x X = Conv2D(filters=64, kernel_size=(7, 7), strides=(2, 2), name='conv1', kernel_initializer=glorot_uniform())(X) X = BatchNormalization(axis=3, name='bn_conv1')(X) X = Activation('relu')(X) X = MaxPooling2D((3, 3), strides=(2, 2))(X) # conv2_x filters = (filter_sizes[0], filter_sizes[0], num_channels[0]) X = convolutional_block(X, f=3, filters=filters, stage=2, block='1', strides=1, gp_cardinality=gp_cardinality, se_ratio=se_ratio) for block_name in map(str, range(98, 97 + num_layers[0])): X = identity_block(X, 3, filters, stage=2, block=block_name, gp_cardinality=gp_cardinality, se_ratio=se_ratio) # conv3_x filters = (filter_sizes[1], filter_sizes[1], num_channels[1]) X = convolutional_block(X, f=3, filters=filters, stage=3, block='1', strides=2, gp_cardinality=gp_cardinality, se_ratio=se_ratio) for block_name in map(str, range(98, 97 + num_layers[1])): X = identity_block(X, 3, filters, stage=3, block=block_name, gp_cardinality=gp_cardinality, se_ratio=se_ratio) # conv4_x filters = (filter_sizes[2], filter_sizes[2], num_channels[2]) X = convolutional_block(X, f=3, filters=filters, stage=4, block='1', strides=2, gp_cardinality=gp_cardinality, se_ratio=se_ratio) for block_name in map(str, range(98, 97 + num_layers[2])): X = identity_block(X, 3, filters, stage=4, block=block_name, gp_cardinality=gp_cardinality, se_ratio=se_ratio) # conv5_x filters = (filter_sizes[3], filter_sizes[3], num_channels[3]) X = convolutional_block(X, f=3, filters=filters, stage=5, block='1', strides=2, gp_cardinality=gp_cardinality, se_ratio=se_ratio) for block_name in map(str, range(98, 97 + num_layers[3])): X = identity_block(X, 3, filters, stage=5, block=block_name, gp_cardinality=gp_cardinality, se_ratio=se_ratio) if feature == 'global_max_pooling': X = GlobalMaxPooling2D()(X) elif feature == 'global_avg_pooling': X = GlobalAveragePooling2D()(X) elif feature == 'concat': gmp = GlobalMaxPooling2D()(X) gap = GlobalAveragePooling2D()(X) X = Concatenate()([gmp, gap]) else: X = Flatten()(X) if deeper_fc: X = Dense(units=classes, activation='linear', name='fc_1', kernel_initializer=glorot_uniform())(X) X = LeakyReLU(alpha=0.01)(X) X = Dense(units=classes, activation='sigmoid', name='out_lbl', kernel_initializer=glorot_uniform())(X) model = Model(inputs=X_input, outputs=X, name=model_name) return model
def ResNet50(input_shape=(64, 64, 3), classes=6): """ Implementation of the popular ResNet50 the following architecture: CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3 -> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> TOPLAYER Arguments: input_shape -- shape of the images of the dataset classes -- integer, number of classes Returns: model -- a Model() instance in Keras """ # Define the input as a tensor with shape input_shape X_input = Input(input_shape) # Zero-Padding X = ZeroPadding2D((3, 3))(X_input) X = Conv2D(64, (7, 7), strides=(2, 2), name='conv1', kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name='bn_conv1')(X) X = Activation('relu')(X) X = MaxPooling2D((3, 3), strides=(2, 2))(X) X = convolutional_block(X, f=3, filters=[64, 64, 256], stage=2, block='a', s=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') # The convolutional block uses three set of filters of size [128,128,512], "f" is 3, "s" is 2 and the block is "a". # The 3 identity blocks use three set of filters of size [128,128,512], "f" is 3 and the blocks are "b", "c" and "d". X = convolutional_block(X, f=3, filters=[128, 128, 512], stage=3, block='a', s=2) X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block='b') X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block='c') X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block='d') # The convolutional block uses three set of filters of size [256, 256, 1024], "f" is 3, "s" is 2 and the block is "a". # The 5 identity blocks use three set of filters of size [256, 256, 1024], "f" is 3 and the blocks are "b", "c", "d", "e" and "f". X = convolutional_block(X, f=3, filters=[256, 256, 1024], block='a', stage=4, s=2) X = identity_block(X, f=3, filters=[256, 256, 1024], block='b', stage=4) X = identity_block(X, f=3, filters=[256, 256, 1024], block='c', stage=4) X = identity_block(X, f=3, filters=[256, 256, 1024], block='d', stage=4) X = identity_block(X, f=3, filters=[256, 256, 1024], block='e', stage=4) X = identity_block(X, f=3, filters=[256, 256, 1024], block='f', stage=4) # The convolutional block uses three set of filters of size [512, 512, 2048], "f" is 3, "s" is 2 and the block is "a". # The 2 identity blocks use three set of filters of size [256, 256, 2048], "f" is 3 and the blocks are "b" and "c". X = convolutional_block(X, f=3, filters=[512, 512, 2048], stage=5, block='a', s=2) # filters should be [256, 256, 2048], but it fail to be graded. Use [512, 512, 2048] to pass the grading X = identity_block(X, f=3, filters=[256, 256, 2048], stage=5, block='b') X = identity_block(X, f=3, filters=[256, 256, 2048], stage=5, block='c') X = AveragePooling2D(pool_size=(2, 2))(X) # output layer X = Flatten()(X) X = Dense(classes, activation='softmax', name='fc' + str(classes), kernel_initializer=glorot_uniform(seed=0))(X) # Create model model = Model(inputs=X_input, outputs=X, name='ResNet50') return model
def convolutional_block(X, f, filters, stage, block, strides=2, gp_cardinality=32, se_ratio=16): conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' F1, F2, F3 = filters X_shortcut = X X = Conv2D(filters=F1, kernel_size=(1, 1), strides=(strides, strides), padding='valid', name=conv_name_base + '2a', kernel_initializer=glorot_uniform())(X) X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X) X = Activation('relu')(X) X = Conv2D(filters=F2, kernel_size=(f, f), strides=(1, 1), padding='same', name=conv_name_base + '2b', kernel_initializer=glorot_uniform())(X) X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X) X = Activation('relu')(X) if gp_cardinality > 0: X = grouped_convolution(X, F2 // gp_cardinality, gp_cardinality=gp_cardinality, strides=1) X = Conv2D(filters=F3, kernel_size=(1, 1), strides=(1, 1), padding='valid', name=conv_name_base + '2c', kernel_initializer=glorot_uniform())(X) X = BatchNormalization(axis=3, name=bn_name_base + '2c')(X) if se_ratio > 0: X = squeeze_excite_block(X, F3, stage, block, se_ratio=se_ratio) X_shortcut = Conv2D(filters=F3, kernel_size=(1, 1), strides=(strides, strides), padding='valid', name=conv_name_base + '1', kernel_initializer=glorot_uniform())(X_shortcut) X_shortcut = BatchNormalization(axis=3, name=bn_name_base + '1')(X_shortcut) X = Add()([X, X_shortcut]) X = Activation('relu')(X) return X
def procesoComprobacionImagenes(parametros): print("------- Leyendo los parametros... -------") print(parametros) enFormatoZIP = eval(parametros['enFormatoZIP']) #Nombre de la ruta en la que se encuentran los pesos y el modelo. rutaModeloYPesos = parametros['rutaModeloYPesos'] nombreModelo = parametros['nombreModelo'] nombrePesos = parametros['nombrePesos'] #Obtenemos el perdiodo de siembra del cultivo. formatoFecha = "%d-%m-%Y" fechaInicioSinSiembra = parametros['fechaInicioSinSiembra'] dateFechaInicioSinSiembra = datetime.strptime(fechaInicioSinSiembra, formatoFecha) #Convertimos al año 2013 por ejemplo, para que podamos comparar ya que solo nos interesan los dias y meses para las cosechas. dateFechaInicioSinSiembra = dateFechaInicioSinSiembra.replace(year=2013) fechaFinSinSiembra = parametros['fechaFinSinSiembra'] dateFechaFinSinSiembra = datetime.strptime(fechaFinSinSiembra, formatoFecha) dateFechaFinSinSiembra = dateFechaFinSinSiembra.replace(year=2013) fechaInicioSiembra = parametros['fechaInicioSiembra'] dateFechaInicioSiembra = datetime.strptime(fechaInicioSiembra, formatoFecha) dateFechaInicioSiembra = dateFechaInicioSiembra.replace(year=2013) fechaFinSiembra = parametros['fechaFinSiembra'] dateFechaFinSiembra = datetime.strptime(fechaFinSiembra, formatoFecha) dateFechaFinSiembra = dateFechaFinSiembra.replace(year=2013) fechaInicioCrecimiento = parametros['fechaInicioCrecimiento'] dateFechaInicioCrecimiento = datetime.strptime(fechaInicioCrecimiento, formatoFecha) dateFechaInicioCrecimiento = dateFechaInicioCrecimiento.replace(year=2013) fechaFinCrecimiento = parametros['fechaFinCrecimiento'] dateFechaFinCrecimiento = datetime.strptime(fechaFinCrecimiento, formatoFecha) dateFechaFinCrecimiento = dateFechaFinCrecimiento.replace(year=2013) #Direccion donde se encuentra el fichero con las imagenes para comprobar. rutaFicheroImagenesComprobar = parametros['rutaFicheroImagenesComprobar'] #Dimensiones de las imagenes que vamos a cargar. alturaImagen = int(parametros['alturaImagen']) anchuraImagen = int(parametros['anchuraImagen']) #Nombre de cultivo que queremos comprobar. (sirve para que muestre si es este de tipo o no) nombreCultivo = parametros['nombreCultivo'] try: # comprueba si exista ya el directorio, sino lo creará mediante descompresion os.stat(rutaModeloYPesos) print("Ya existe el directorio") print("------- Cargando la red y los pesos... -------") modelo = rutaModeloYPesos + '/' + nombreModelo print(modelo) pesos_modelo = rutaModeloYPesos + '/' + nombrePesos print(pesos_modelo) except: if (enFormatoZIP ): #Si la ruta indicada en el fichero es en formato .zip rutaZip = parametros['rutaZip'] if ".zip" not in rutaModeloYPesos: rutaZip = rutaModeloYPesos + '.zip' print('------- Descomprimiendo el archivo', rutaZip, '... -------') shutil.unpack_archive(rutaZip, extract_dir=rutaModeloYPesos) else: print('------- Descomprimiendo el archivo', rutaZip, '... -------') shutil.unpack_archive(rutaZip, extract_dir=rutaModeloYPesos) print("------- Cargando la red y los pesos... -------") modelo = rutaModeloYPesos + '/' + nombreModelo print(modelo) pesos_modelo = rutaModeloYPesos + '/' + nombrePesos print(pesos_modelo) diccionarioIMG = leerParametrosFichero(rutaFicheroImagenesComprobar) with CustomObjectScope({'GlorotUniform': glorot_uniform()}): redConvolucionaCargada = load_model(modelo) redConvolucionaCargada.load_weights(pesos_modelo) print("------- Obteniendo los resultados de las imágenes... -------") listaResultados, diccionarioFases = obtenerResultadosImagenes( diccionarioIMG, redConvolucionaCargada, dateFechaInicioSinSiembra, dateFechaFinSinSiembra, dateFechaInicioSiembra, dateFechaFinSiembra, dateFechaInicioCrecimiento, dateFechaFinCrecimiento, alturaImagen, anchuraImagen) print("------- Comprobando los resultados de las imágenes... -------") comprobarResultados(listaResultados, nombreCultivo, diccionarioFases) print("------- Fin del programa -------")
def make_mlp(training_data, validation_data, test_data, W, args): (X_train, y_train) = training_data (X_val, y_val) = validation_data (X_test, y_test) = test_data (W, hb) = W dense_params = {} if W is not None and hb is not None: dense_params['weights'] = (W, hb) # define and initialize MLP model mlp = Sequential([ Dense(7800, input_shape=(3 * 32 * 32, ), kernel_regularizer=regularizers.l2(args.mlp_l2), kernel_initializer=glorot_uniform(seed=3333), **dense_params), BN(), Activation('relu'), Dropout(args.mlp_dropout, seed=4444), Dense(10, kernel_initializer=glorot_uniform(seed=5555)), Activation('softmax'), ]) mlp.compile(optimizer=MultiAdam(lr=0.001, lr_multipliers={ 'dense_1': args.mlp_lrm[0], 'dense_2': args.mlp_lrm[1] }), loss='categorical_crossentropy', metrics=['accuracy']) # train and evaluate classifier with Stopwatch(verbose=True) as s: early_stopping = EarlyStopping(monitor=args.mlp_val_metric, patience=6, verbose=2) reduce_lr = ReduceLROnPlateau(monitor=args.mlp_val_metric, factor=0.2, verbose=2, patience=3, min_lr=1e-5) callbacks = [early_stopping, reduce_lr] try: mlp.fit(X_train, one_hot(y_train, n_classes=10), epochs=args.mlp_epochs, batch_size=args.mlp_batch_size, shuffle=False, validation_data=(X_val, one_hot(y_val, n_classes=10)), callbacks=callbacks) except KeyboardInterrupt: pass y_pred = mlp.predict(X_test) y_pred = unhot(one_hot_decision_function(y_pred), n_classes=10) print("Test accuracy: {:.4f}".format(accuracy_score(y_test, y_pred))) # save predictions, targets, and fine-tuned weights np.save(args.mlp_save_prefix + 'y_pred.npy', y_pred) np.save(args.mlp_save_prefix + 'y_test.npy', y_test) W_finetuned, _ = mlp.layers[0].get_weights() np.save(args.mlp_save_prefix + 'W_finetuned.npy', W_finetuned)
def ResNet50(input_shape=(64, 64, 3), classes=6): #定义tensor类型的输入数据 X_input = Input(input_shape) #0填充 X = ZeroPadding2D((3, 3))(X_input) X = Dropout(0.1)(X) #stage1 X = Conv2D(filters=64, kernel_size=(7, 7), strides=(2, 2), name="conv1", kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name="bn_conv1")(X) X = Activation("relu")(X) X = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(X) #stage2 X = convolutional_block(X, f=3, filters=[64, 64, 256], stage=2, block="a", s=1) X = identity_block(X, f=3, filters=[64, 64, 256], stage=2, block="b") X = identity_block(X, f=3, filters=[64, 64, 256], stage=2, block="c") #stage3 X = convolutional_block(X, f=3, filters=[128, 128, 512], stage=3, block="a", s=2) X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block="b") X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block="c") X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block="d") #stage4 X = convolutional_block(X, f=3, filters=[256, 256, 1024], stage=4, block="a", s=2) X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="b") X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="c") X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="d") X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="e") X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="f") #stage5 X = convolutional_block(X, f=3, filters=[512, 512, 2048], stage=5, block="a", s=2) X = identity_block(X, f=3, filters=[512, 512, 2048], stage=5, block="b") X = identity_block(X, f=3, filters=[512, 512, 2048], stage=5, block="c") #均值池化层 X = AveragePooling2D(pool_size=(2, 2), padding="same")(X) #输出层 X = Flatten()(X) X = Dropout(0.8)(X) X = Dense(classes, activation="softmax", name="fc" + str(classes), kernel_initializer=glorot_uniform(seed=0))(X) #创建模型 model = Model(inputs=X_input, outputs=X, name="ResNet50") return model
print("x_valid_video_appea:", x_valid_video_appea.shape) ############################### Input Layers ########################## input_train_Video = Input(shape=(316, ), name='input_video') input_train_Audio = Input(shape=(102, ), name='input_audio') input_train_ECG = Input(shape=(54, ), name='input_ecg') input_train_EDA = Input(shape=(62, ), name='input_eda') input_train_Video_Appea = Input(shape=(84, ), name='input_video_appearance') ############################### Separated Layers ########################## video_branch = Dense(316, input_dim=316, name='encoded_video_branch', kernel_initializer=initializers.glorot_uniform(seed=None), bias_initializer='zeros', activation='relu')(input_train_Video) #video_branch= Dropout(0.5)(video_branch) audio_branch = Dense(102, input_dim=102, name='encoded_audio_branch', kernel_initializer=initializers.glorot_uniform(seed=None), bias_initializer='zeros')(input_train_Audio) #audio_branch= Dropout(0.5)(audio_branch) ecg_branch = Dense(102, input_dim=54, name='encoded_ecg_branch', kernel_initializer=initializers.glorot_uniform(seed=None),
def ResNet50(input_shape = (64, 64, 3), classes = 6): """ Implementation of the popular ResNet50 the following architecture: CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3 -> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> TOPLAYER Arguments: input_shape -- shape of the images of the dataset classes -- integer, number of classes Returns: model -- a Model() instance in Keras """ # Define the input as a tensor with shape input_shape X_input = Input(input_shape) # Zero-Padding X = ZeroPadding2D((3, 3))(X_input) # Stage 1 X = Conv2D(64, (7, 7), strides = (2, 2), name = 'conv1', kernel_initializer = glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = 'bn_conv1')(X) X = Activation('relu')(X) X = MaxPooling2D((3, 3), strides=(2, 2))(X) # Stage 2 X = convolutional_block(X, f = 3, filters = [64, 64, 256], stage = 2, block='a', s = 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') ### START CODE HERE ### # Stage 3 (≈4 lines) X = convolutional_block(X, f=3, filters=[128,128,512],stage=3, block='a', s=2) X = identity_block(X, 3, [128,128,512], stage=3, block='b') X = identity_block(X, 3, [128,128,512], stage=3, block='c') X = identity_block(X, 3, [128,128,512], stage=3, block='d') # Stage 4 (≈6 lines) X = convolutional_block(X, f=3, filters=[256,256,1024], stage=4, block='a', s=2) X = identity_block(X, 3, [256,256,1024], stage=4, block='b') X = identity_block(X,3,[256,256,1024], stage=4, block='c') X = identity_block(X,3,[256,256,1024], stage=4, block='d') X = identity_block(X,3,[256,256,1024], stage=4, block='e') X = identity_block(X, 3,[256,256,1024], stage=4, block='f') # Stage 5 (≈3 lines) X = convolutional_block(X, f=3, filters=[512,512,2048],stage=5, s=2,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') # AVGPOOL (≈1 line). Use "X = AveragePooling2D(...)(X)" X = AveragePooling2D((2,2), name='avg_pool')(X) ### END CODE HERE ### # output layer X = Flatten()(X) X = Dense(classes, activation='softmax', name='fc' + str(classes), kernel_initializer = glorot_uniform(seed=0))(X) # Create model model = Model(inputs = X_input, outputs = X, name='ResNet50') return model
def build_network(self): # Create placeholders with tf.name_scope('inputs'): self.X = tf.placeholder(tf.float32, shape=(None, self.n_x), name="X") self.Y = tf.placeholder(tf.float32, shape=(None, self.n_y), name="Y") self.discounted_episode_rewards_norm = tf.placeholder( tf.float32, [ None, ], name="actions_value") units_input_layer = self.n_x units_output_layer = self.n_y from keras.layers import Dense, Dropout, BatchNormalization from keras import initializers kerner_initializer = initializers.glorot_uniform(seed=1) dropout = self.epochs / 20.0 n_neurons = np.int(1024) A1 = Dense(units=n_neurons, activation='elu', kernel_initializer=kerner_initializer, input_shape=(units_input_layer, ))(self.X) A1 = BatchNormalization()(A1) A1 = Dropout(dropout)(A1) n_neurons = np.int(n_neurons / 2) A2 = Dense(units=n_neurons, activation='elu', kernel_initializer=kerner_initializer)(A1) A2 = BatchNormalization()(A2) A2 = Dropout(dropout)(A2) A3 = Dense(units=n_neurons, activation='elu', kernel_initializer=kerner_initializer)(A2) A3 = BatchNormalization()(A3) A3 = Dropout(dropout)(A3) n_neurons = np.int(n_neurons / 2) A4 = Dense(units=n_neurons, activation='elu', kernel_initializer=kerner_initializer)(A3) A4 = BatchNormalization()(A4) A4 = Dropout(dropout)(A4) A5 = Dense(units=n_neurons, activation='elu', kernel_initializer=kerner_initializer)(A4) A5 = BatchNormalization()(A5) A5 = Dropout(dropout)(A5) n_neurons = np.int(n_neurons / 2) A6 = Dense(units=n_neurons, activation='elu', kernel_initializer=kerner_initializer)(A5) A6 = BatchNormalization()(A6) A6 = Dropout(dropout)(A6) A7 = Dense(units=n_neurons, activation='elu', kernel_initializer=kerner_initializer)(A6) A7 = BatchNormalization()(A7) A7 = Dropout(dropout)(A7) n_neurons = np.int(n_neurons / 2) A8 = Dense(units=n_neurons, activation='elu', kernel_initializer=kerner_initializer)(A7) A8 = BatchNormalization()(A8) A9 = Dense(units=n_neurons, activation='elu', kernel_initializer=kerner_initializer)(A8) A9 = BatchNormalization()(A9) n_neurons = np.int(n_neurons / 2) A10 = Dense(units=n_neurons, activation='elu', kernel_initializer=kerner_initializer)(A9) A10 = BatchNormalization()(A10) n_neurons = np.int(n_neurons / 2) A11 = Dense(units=n_neurons, activation='elu', kernel_initializer=kerner_initializer)(A10) A11 = BatchNormalization()(A11) Z = Dense(units=units_output_layer, kernel_initializer=kerner_initializer, activation=None)(A11) # Softmax outputs, we need to transpose as tensorflow nn functions expects them in this shape logits = Z labels = self.Y self.outputs_softmax = tf.nn.softmax(logits, name='A12') with tf.name_scope('loss'): self.cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2( logits=logits, labels=labels) self.loss = tf.reduce_mean( tf.add( tf.multiply(self.cross_entropy, 1 - self.alpha), tf.multiply(self.discounted_episode_rewards_norm, self.alpha))) # reward guided loss with tf.name_scope('train'): self.train_op = tf.train.AdamOptimizer(self.lr).minimize(self.loss)
def _convolution_block(self, X, filters, f, s, stage, block): """ Convolutional blocks are used when the input activation a[l] is different from the output activation a[l+2] --> X * |Conv2D|BatchNorm| --> --> --> --> X ----> |Conv2D|BatchNorm|Relu| ----> |Conv2D|BatchNorm|Relu| ----> |Conv2D|BatchNorm| + |Relu|--> Arguments: X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev filters -- a list of integers indicating the number of filters at each convolutional layer f -- an integer indicating the dimension of the filter s -- an integer indicating the stride value Returns: X -- output tensor of shape (m, n_H, n_W, n_C) """ conv_layer_name = 'res' + str(stage) + block + '_branch' bn_layer_name = 'bn' + str(stage) + block + '_branch' X_shortcut = X F1, F2, F3 = filters # First component of the main path X = Conv2D(filters=F1, kernel_size=(1, 1), strides=(s, s), padding='valid', name=conv_layer_name + '2a', kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name=bn_layer_name + '2a')(X) X = Activation('relu')(X) # Second component of the main path X = Conv2D(filters=F2, kernel_size=(f, f), strides=(1, 1), padding='same', name=conv_layer_name + '2b', kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name=bn_layer_name + '2b')(X) X = Activation('relu')(X) # Third component of the main path X = Conv2D(filters=F3, kernel_size=(1, 1), strides=(1, 1), padding='valid', name=conv_layer_name + '2c', kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name=bn_layer_name + '2c')(X) # Shortcut path X_shortcut = Conv2D( filters=F3, kernel_size=(1, 1), strides=(s, s), padding='valid', name=conv_layer_name + '1', kernel_initializer=glorot_uniform(seed=0))(X_shortcut) X_shortcut = BatchNormalization(axis=3, name=bn_layer_name + '1')(X_shortcut) # Final step : adding the shortcut component to the activation tensor from the third component of the main path X = Add()([X, X_shortcut]) X = Activation('relu')(X) return X
from lanmodel import embedding_util from classifier import classifier_dnn_scalable as dnn_classifier from classifier import mtl_util as mtl_classifier from classifier import dnn_util as util from exp.wop import exp_wop_cml as exp_util import pandas as pd from exp import exp_util from classifier import classifier_learn import random random.seed(classifier_learn.RANDOM_STATE) numpy.random.seed(classifier_learn.RANDOM_STATE) tf.set_random_seed(classifier_learn.RANDOM_STATE) my_init = initializers.glorot_uniform(seed=classifier_learn.RANDOM_STATE) # session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, # inter_op_parallelism_threads=1) # sess = tf.Session(graph=tf.get_default_graph(), config=session_conf) # K.set_session(sess) def load_word_weights(word_weights_file): weights = pd.read_csv( word_weights_file, delimiter=",", quoting=0, encoding="utf-8", ).as_matrix() words = []
def ResNet50(input_shape = (64, 64, 3), classes = 6): """ Implementation of the popular ResNet50 the following architecture: CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3 -> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> TOPLAYER Arguments: input_shape -- shape of the images of the dataset classes -- integer, number of classes Returns: model -- a Model() instance in Keras """ # Define the input as a tensor with shape input_shape X_input = Input(input_shape) # Zero-Padding X = ZeroPadding2D((3, 3))(X_input) # Stage 1 X = Conv2D(64, (7, 7), strides = (2, 2), name = 'conv1', kernel_initializer = glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = 'bn_conv1')(X) X = Activation('relu')(X) X = MaxPooling2D((3, 3), strides=(2, 2))(X) # Stage 2 X = convolutional_block(X, f = 3, filters = [64, 64, 256], stage = 2, block='a', s = 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') ### START CODE HERE ### # Stage 3 (≈4 lines) X = convolutional_block(X, f = 3, filters = [128, 128, 512], stage = 3, block='a', s = 2) X = identity_block(X, 3, [128, 128, 512], stage=3, block='b') X = identity_block(X, 3, [128, 128, 512], stage=3, block='c') X = identity_block(X, 3, [128, 128, 512], stage=3, block='d') # Stage 4 (≈6 lines) X = convolutional_block(X, f = 3, filters = [256, 256, 1024], stage=4, block='a', s = 2) X = identity_block(X, 3, [256, 256, 1024], stage=4, block='b') X = identity_block(X, 3, [256, 256, 1024], stage=4, block='c') X = identity_block(X, 3, [256, 256, 1024], stage=4, block='d') X = identity_block(X, 3, [256, 256, 1024], stage=4, block='e') X = identity_block(X, 3, [256, 256, 1024], stage=4, block='f') # Stage 5 (≈3 lines) X = convolutional_block(X, f = 3, filters = [512, 512, 2048], stage=5, block='a', s = 2) X = identity_block(X, 3, [512, 512, 2048], stage=5, block='b') X = identity_block(X, 3, [512, 512, 2048], stage=5, block='c') # AVGPOOL (≈1 line). Use "X = AveragePooling2D(...)(X)" X = AveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None, name='avg_pool')(X) ### END CODE HERE ### # output layer X = Flatten()(X) X = Dense(classes, activation='softmax', name='fc' + str(classes), kernel_initializer = glorot_uniform(seed=0))(X) # Create model model = Model(inputs = X_input, outputs = X, name='ResNet50') return model
def get_model(self, input_shapes): """ Main model function. """ feat_shape = input_shapes[0] # Features number emb_shapes = input_shapes[1:] # Embeddings shapes def build_mlp_model(input_shape, model_num=0, dense_layer_size=128, dropout_rate=0.9): """ The base MLP module. Dense + BatchNorm + Dropout. Idea from Matei Ionita's kernel: https://www.kaggle.com/mateiionita/taming-the-bert-a-baseline """ X_input = layers.Input([input_shape]) X = layers.Dense(dense_layer_size, name = 'mlp_dense_{}'.format(model_num), kernel_initializer=initializers.glorot_uniform(seed=self.seed))(X_input) X = layers.BatchNormalization(name = 'mlp_bn_{}'.format(model_num))(X) X = layers.Activation('relu')(X) X = layers.Dropout(dropout_rate, seed = self.seed)(X) model = models.Model(inputs = X_input, outputs = X, name = 'mlp_model_{}'.format(model_num)) return model # Two models with inputs of shape (, 3*3*1014) # from BERT Large cased and BERT Large uncased embeddings. # Output shape is (, 112). all_models = [build_mlp_model(emb_shape, model_num=i, dense_layer_size=112, dropout_rate=0.9) for i, emb_shape in enumerate(emb_shapes)] # Two Siamese models with distances between # Pronoun and A-term embeddings, # Pronoun and B-term embeddings as inputs and shared weights. # Input shape is (, 3*1024). Output shape is (, 2*112). for i, emb_shape in enumerate(emb_shapes): split_input = layers.Input([emb_shape]) split_model_shape = int(emb_shape / 3) split_model = build_mlp_model(split_model_shape, model_num=len(all_models), dense_layer_size=112) P = layers.Lambda(lambda x: x[:, :split_model_shape])(split_input) A = layers.Lambda(lambda x: x[:, split_model_shape : split_model_shape*2])(split_input) B = layers.Lambda(lambda x: x[:, split_model_shape*2 : split_model_shape*3])(split_input) A_out = split_model(layers.Subtract()([P, A])) B_out = split_model(layers.Subtract()([P, B])) split_out = layers.concatenate([A_out, B_out], axis=-1) merged_model = models.Model(inputs=split_input, outputs=split_out, name='split_model_{}'.format(i)) all_models.append(merged_model) # One model all_models.append(build_mlp_model(feat_shape, model_num=len(all_models), dense_layer_size=128, dropout_rate=0.8)) lambd = 0.02 # L2 regularization # Combine all models into one model # Concatenation of 5 models outputs merged_out = layers.concatenate([model.output for model in all_models]) merged_out = layers.Dense(3, name = 'merged_output', kernel_regularizer = regularizers.l2(lambd), kernel_initializer=initializers.glorot_uniform(seed=self.seed))(merged_out) merged_out = layers.BatchNormalization(name = 'merged_bn')(merged_out) merged_out = layers.Activation('softmax')(merged_out) # The final combined model. combined_model = models.Model([model.input for model in all_models], outputs = merged_out, name = 'merged_model') #print(combined_model.summary()) return combined_model
def test_glorot_uniform(tensor_shape): fan_in, fan_out = initializers._compute_fans(tensor_shape) std = np.sqrt(2. / (fan_in + fan_out)) _runner(initializers.glorot_uniform(), tensor_shape, target_mean=0., target_std=std)
def convolutional_block(X, f, filters, stage, block, s=2): """ Implementation of the convolutional block as defined in Figure 4 Arguments: X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev) f -- integer, specifying the shape of the middle CONV's window for the main path filters -- python list of integers, defining the number of filters in the CONV layers of the main path stage -- integer, used to name the layers, depending on their position in the network block -- string/character, used to name the layers, depending on their position in the network s -- Integer, specifying the stride to be used Returns: X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C) """ # 定义名字 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' # 过滤器的个数 F1, F2, F3 = filters # 保存输入值 X_shortcut = X ##### 主要路线 ##### # Conv2D返回一个函数 # 第一个卷积,F1:卷积核的个数即输出维度,1*1卷积,步长可配置(默认为2),采用valid pad,kernel_initializer权值初始化的方法为Glorot均匀分布初始化方法 # glorot_uniform:参数从[-limit, limit]的均匀分布产生,limit=sqrt(6 / (fan_in + fan_out)),其中fan_in权重张量的输入单元数,fan_out权重张量的输出单元数 X = Conv2D(filters=F1, kernel_size=(1, 1), strides=(s, s), name=conv_name_base + '2a', padding='valid', kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X) # 归一化 X = Activation('relu')(X) # 激活函数 # 第二个卷积,f*f卷积,same pad不改变输入特征的2d大小,采用Glorot均匀分布初始化方法 X = Conv2D(filters=F2, kernel_size=(f, f), strides=(1, 1), name=conv_name_base + '2b', padding='same', kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X) X = Activation('relu')(X) # 第三个卷积 X = Conv2D(filters=F3, kernel_size=(1, 1), strides=(1, 1), name=conv_name_base + '2c', padding='valid', kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3, name=bn_name_base + '2c')(X) ##### SHORTCUT PATH #### X_shortcut = Conv2D(filters=F3, kernel_size=(1, 1), strides=(s, s), name=conv_name_base + '1', padding='valid', kernel_initializer=glorot_uniform(seed=0))(X_shortcut) X_shortcut = BatchNormalization(axis=3, name=bn_name_base + '1')(X_shortcut) # 主路和小路的特征组合 X = layers.add([X, X_shortcut]) X = Activation('relu')(X) return X
while QCurrent[1,0] < 0 : QCurrent[1,0] = QCurrent[1,0] + 2*math.pi; while QCurrent[2,0] > 2*math.pi : QCurrent[2,0] = QCurrent[2,0] - 2*math.pi; while QCurrent[2,0] < 0 : QCurrent[2,0] = QCurrent[2,0] + 2*math.pi; return (QCurrent[0,0], QCurrent[1,0], QCurrent[2,0]) # load json and create model json_file = open('/home/why/doyle_why/language/python_work/c_python_test1/src/pythonpacktest/src/model_doyle_type3.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json, custom_objects={'GlorotUniform': glorot_uniform()}) # load weights into new model loaded_model.load_weights("/home/why/doyle_why/language/python_work/c_python_test1/src/pythonpacktest/src/model_doyle_type3.h5") print("Loaded model from disk") # evaluate loaded model on test data loaded_model.compile(optimizer=tf.train.AdamOptimizer(0.05), loss=customloss, metrics=['accuracy']) # load data from exiting csv tmp = np.loadtxt("/home/why/doyle_why/language/python_work/c_python_test1/src/pythonpacktest/src/traing_data_for_doyle_type3.csv", delimiter=",") tmp1=np.array(tmp) Q2 = tmp1[0:, 0] Q3 = tmp1[0:, 1] posX = tmp1[0:, 2] posY = tmp1[0:, 3] posZ = tmp1[0:, 4]
def build_mlp_model(input_shape, model_num=0, dense_layer_size=128, dropout_rate=0.9): """ The base MLP module. Dense + BatchNorm + Dropout. Idea from Matei Ionita's kernel: https://www.kaggle.com/mateiionita/taming-the-bert-a-baseline """ X_input = layers.Input([input_shape]) X = layers.Dense(dense_layer_size, name = 'mlp_dense_{}'.format(model_num), kernel_initializer=initializers.glorot_uniform(seed=self.seed))(X_input) X = layers.BatchNormalization(name = 'mlp_bn_{}'.format(model_num))(X) X = layers.Activation('relu')(X) X = layers.Dropout(dropout_rate, seed = self.seed)(X) model = models.Model(inputs = X_input, outputs = X, name = 'mlp_model_{}'.format(model_num)) return model