def gettest_model(): input = Input(shape=[16, 66, 3]) # change this shape to [None,None,3] to enable arbitraty shape input A = Conv2D(10, (3, 3), strides=1, padding='valid', name='conv1')(input) B = Activation("relu", name='relu1')(A) C = MaxPool2D(pool_size=2)(B) x = Conv2D(16, (3, 3), strides=1, padding='valid', name='conv2')(C) x = Activation("relu", name='relu2')(x) x = Conv2D(32, (3, 3), strides=1, padding='valid', name='conv3')(x) K = Activation("relu", name='relu3')(x) x = Flatten()(K) dense = Dense(2,name = "dense")(x) output = Activation("relu", name='relu4')(dense) x = Model([input], [output]) x.load_weights("./model/model12.h5") ok = Model([input], [dense]) for layer in ok.layers: print layer return ok
def ResNet(inp, layers): names = [ "conv1_1_3x3_s2", "conv1_1_3x3_s2_bn", "conv1_2_3x3", "conv1_2_3x3_bn", "conv1_3_3x3", "conv1_3_3x3_bn" ] cnv1 = Conv2D(64, (3, 3), strides=(2, 2), padding='same', name=names[0], use_bias=False)(inp) # "conv1_1_3x3_s2" bn1 = BN(name=names[1])(cnv1) # "conv1_1_3x3_s2/bn" relu1 = Activation('relu')(bn1) # "conv1_1_3x3_s2/relu" cnv1 = Conv2D(64, (3, 3), strides=(1, 1), padding='same', name=names[2], use_bias=False)(relu1) # "conv1_2_3x3" bn1 = BN(name=names[3])(cnv1) # "conv1_2_3x3/bn" relu1 = Activation('relu')(bn1) # "conv1_2_3x3/relu" cnv1 = Conv2D(128, (3, 3), strides=(1, 1), padding='same', name=names[4], use_bias=False)(relu1) # "conv1_3_3x3" bn1 = BN(name=names[5])(cnv1) # "conv1_3_3x3/bn" relu1 = Activation('relu')(bn1) # "conv1_3_3x3/relu" res = MaxPooling2D(pool_size=(3, 3), padding='same', strides=(2, 2))(relu1) # "pool1_3x3_s2" # 2_1- 2_3 res = residual_short(res, 1, pad=1, lvl=2, sub_lvl=1) for i in range(2): res = residual_empty(res, 1, pad=1, lvl=2, sub_lvl=i + 2) # 3_1 - 3_3 res = residual_short(res, 2, pad=1, lvl=3, sub_lvl=1, modify_stride=True) for i in range(3): res = residual_empty(res, 2, pad=1, lvl=3, sub_lvl=i + 2) if layers is 50: # 4_1 - 4_6 res = residual_short(res, 4, pad=2, lvl=4, sub_lvl=1) for i in range(5): res = residual_empty(res, 4, pad=2, lvl=4, sub_lvl=i + 2) elif layers is 101: # 4_1 - 4_23 res = residual_short(res, 4, pad=2, lvl=4, sub_lvl=1) for i in range(22): res = residual_empty(res, 4, pad=2, lvl=4, sub_lvl=i + 2) else: print("This ResNet is not implemented") # 5_1 - 5_3 res = residual_short(res, 8, pad=4, lvl=5, sub_lvl=1) for i in range(2): res = residual_empty(res, 8, pad=4, lvl=5, sub_lvl=i + 2) res = Activation('relu')(res) return res
import keras import numpy as np import tensorflow as tf import keras.backend as K from keras.layers import Input, Merge, Dense, Lambda, Activation from keras.layers.embeddings import Embedding from keras.layers.convolutional import Convolution1D def attentive(q, a): return tf.batch_matmul(q, a, adj_x=False, adj_y=True, name="attention") q = Input(shape=[None,10], dtype='float32') a = Input(shape=[None,10], dtype='float32') qua = Activation('tanh')( Merge( mode=lambda x: attentive(*x), output_shape=lambda x: x[0][0:2] + x[1][2:] )([q, a]) ) print qua.get_shape() q_softmax = Activation('softmax')(Lambda(lambda x: K.max(x, axis=2, keepdims=True))(qua)) print q_softmax.get_shape() a_softmax = Activation('softmax')(Lambda(lambda x: K.max(x, axis=1, keepdims=True))(qua)) print a_softmax.get_shape() sess = tf.Session() print sess.run([q_softmax], {q:np.random.random((3,5,10)), a:np.random.random((3,7,10))})[0].shape print sess.run([a_softmax], {q:np.random.random((3,5,10)), a:np.random.random((3,7,10))})[0].shape
def YogaCNNModel(train_data,train_labels_one_hot,test_data,test_labels_one_hot,save_dir,model_name): print('train samples',train_data.shape) print('test samples',test_data.shape) print('train labels',train_labels_one_hot.shape) print('test labels',test_labels_one_hot.shape) train_labels_one_hot = np_utils.to_categorical(train_labels_one_hot) test_labels_one_hot = np_utils.to_categorical(test_labels_one_hot) model = Sequential() model.add(Conv2D(32, (3, 3), padding='same',input_shape=(28,28,3))) model.add(Activation('relu')) model.add(Conv2D(32, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(64, (3, 3), padding='same')) model.add(Activation('relu')) model.add(Conv2D(64, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes)) model.add(Activation('softmax')) # initiate RMSprop optimizer opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6) # Let's train the model using RMSprop model.compile(loss='categorical_crossentropy',optimizer=opt,metrics=['accuracy']) #train_data = train_data.astype('float32') #test_data = test_data.astype('float32') #train_data /= 255 #test_data /= 255 if not data_augmentation: print('Not using data augmentation.') model.fit(train_data, train_labels_one_hot, batch_size=batch_size, epochs=epochs, validation_data=(test_data, test_labels_one_hot), shuffle=True) else: print('Using real-time data augmentation.') # This will do preprocessing and realtime data augmentation: datagen = ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization=False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening zca_epsilon=1e-06, # epsilon for ZCA whitening rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180) # randomly shift images horizontally (fraction of total width) width_shift_range=0.1, #steps_per_epoch = 1, # randomly shift images vertically (fraction of total height) height_shift_range=0.1, shear_range=0., # set range for random shear zoom_range=0., # set range for random zoom channel_shift_range=0., # set range for random channel shifts # set mode for filling points outside the input boundaries fill_mode='nearest', cval=0., # value used for fill_mode = "constant" horizontal_flip=True, # randomly flip images vertical_flip=False, # randomly flip images # set rescaling factor (applied before any other transformation) rescale=None, # set function that will be applied on each input preprocessing_function=None, # image data format, either "channels_first" or "channels_last" data_format=None, # fraction of images reserved for validation (strictly between 0 and 1) validation_split=0.0) # Compute quantities required for feature-wise normalization # (std, mean, and principal components if ZCA whitening is applied). datagen.fit(train_data) # Fit the model on the batches generated by datagen.flow(). #steps_per_epoch should be equivalent to the total number of samples divided by the batch size. model.fit_generator(datagen.flow(train_data, train_labels_one_hot, batch_size=batch_size), epochs=epochs,steps_per_epoch=2000, validation_data=(test_data, test_labels_one_hot)) # Save model and weights if not os.path.isdir(save_dir): os.makedirs(save_dir) model_path = os.path.join(save_dir, model_name) model.save(model_path) print('Saved trained model at %s ' % model_path) # Score trained model. scores = model.evaluate(test_data, test_labels_one_hot, verbose=1) print('Test loss:', scores[0]) print('Test accuracy:', scores[1]) #model.save("CBA-model.h5") return scores[0],scores[1]
from keras.models import load_model from keras.models import Sequential, Model from keras.layers import Dense, Activation from keras.layers import Conv2D, MaxPooling2D, Input, Merge, ZeroPadding2D, merge from keras.preprocessing import image from scipy.misc import imsave, imread, imresize, toimage import numpy as np import matplotlib.pyplot as plt img_shape = (41, 41, 1) input_img = Input(shape=(img_shape)) model = Conv2D(64, (3, 3), padding='same', name='conv1')(input_img) model = Activation('relu', name='act1')(model) model = Conv2D(64, (3, 3), padding='same', name='conv2')(model) model = Activation('relu', name='act2')(model) model = Conv2D(64, (3, 3), padding='same', name='conv3')(model) model = Activation('relu', name='act3')(model) model = Conv2D(64, (3, 3), padding='same', name='conv4')(model) model = Activation('relu', name='act4')(model) model = Conv2D(64, (3, 3), padding='same', name='conv5')(model) model = Activation('relu', name='act5')(model) model = Conv2D(64, (3, 3), padding='same', name='conv6')(model) model = Activation('relu', name='act6')(model) model = Conv2D(64, (3, 3), padding='same', name='conv7')(model) model = Activation('relu', name='act7')(model) model = Conv2D(64, (3, 3), padding='same', name='conv8')(model) model = Activation('relu', name='act8')(model) model = Conv2D(64, (3, 3), padding='same', name='conv9')(model)
batches = np.float32(batches) cost = np.array(cost) x, z = x_valid, y_valid y = x.dot(w) + b s = softmax(y) val_cost = np.sum(-z * np.log(s), axis=-1).mean() val_acc = np.argmax(y, axis=-1) == np.argmax(z, axis=-1) val_acc = val_acc.astype(np.float32).mean() print epoch, np.sum(cost) / np.sum(batches), np.mean( accuracy), val_cost, val_acc x, z = x_train, y_train y = x.dot(w) + b s = softmax(y) cost = np.sum(-z * np.log(s), axis=-1).mean() acc = np.argmax(y, axis=-1) == np.argmax(z, axis=-1) acc = acc.astype(np.float32).mean() print cost, acc weights = [w, b.reshape(-1)] model.layers[1].set_weights(weights) if __name__ == '__main__': x_in = Input(shape=(dim_in, )) x = Dense(dim_out)(x_in) x = Activation('softmax')(x) model = Model(inputs=x_in, outputs=x) train_numpy_model(model) train_keras_model(model)
Y_Train = train_y model = Sequential() model.add(Embedding( input_dim=EMBEDDING_LENGTH, output_dim=EMBEDDING_OUTPUT_DIM, input_length=TWEET_LENGTH, mask_zero=True )) model.add(LSTM( output_dim=50, return_sequences=False )) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) print('Train...') model.fit(X_Train, Y_Train, batch_size=32, nb_epoch=15, verbose=2) # serialize model to JSON model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json) # load json and create model
def resnet_v2(input_shape, depth, num_classes=10): """ResNet Version 2 Model builder [b] Stacks of (1 x 1)-(3 x 3)-(1 x 1) BN-ReLU-Conv2D or also known as bottleneck layer First shortcut connection per layer is 1 x 1 Conv2D. Second and onwards shortcut connection is identity. At the beginning of each stage, the feature map size is halved (downsampled) by a convolutional layer with strides=2, while the number of filter maps is doubled. Within each stage, the layers have the same number filters and the same filter map sizes. Features maps sizes: conv1 : 32x32, 16 stage 0: 32x32, 64 stage 1: 16x16, 128 stage 2: 8x8, 256 # Arguments input_shape (tensor): shape of input image tensor depth (int): number of core convolutional layers num_classes (int): number of classes (CIFAR10 has 10) # Returns model (Model): Keras model instance """ if (depth - 2) % 9 != 0: raise ValueError('depth should be 9n+2 (eg 56 or 110 in [b])') # Start model definition. num_filters_in = 16 num_res_blocks = int((depth - 2) / 9) inputs = Input(shape=input_shape) # v2 performs Conv2D with BN-ReLU on input before splitting into 2 paths x = resnet_layer(inputs=inputs, num_filters=num_filters_in, conv_first=True) # Instantiate the stack of residual units for stage in range(3): for res_block in range(num_res_blocks): activation = 'relu' batch_normalization = True strides = 1 if stage == 0: num_filters_out = num_filters_in * 4 if res_block == 0: # first layer and first stage activation = None batch_normalization = False else: num_filters_out = num_filters_in * 2 if res_block == 0: # first layer but not first stage strides = 2 # downsample # bottleneck residual unit y = resnet_layer(inputs=x, num_filters=num_filters_in, kernel_size=1, strides=strides, activation=activation, batch_normalization=batch_normalization, conv_first=False) y = resnet_layer(inputs=y, num_filters=num_filters_in, conv_first=False) y = resnet_layer(inputs=y, num_filters=num_filters_out, kernel_size=1, conv_first=False) if res_block == 0: # linear projection residual shortcut connection to match # changed dims x = resnet_layer(inputs=x, num_filters=num_filters_out, kernel_size=1, strides=strides, activation=None, batch_normalization=False) x = keras.layers.add([x, y]) num_filters_in = num_filters_out # Add classifier on top. # v2 has BN-ReLU before Pooling x = BatchNormalization()(x) x = Activation('relu')(x) x = AveragePooling2D(pool_size=8)(x) y = Flatten()(x) outputs = Dense(num_classes, activation='softmax', kernel_initializer='he_normal')(y) # Instantiate model. model = Model(inputs=inputs, outputs=outputs) return model
def resnet_v1(input_shape, depth, num_classes=10): """ResNet Version 1 Model builder [a] Stacks of 2 x (3 x 3) Conv2D-BN-ReLU Last ReLU is after the shortcut connection. At the beginning of each stage, the feature map size is halved (downsampled) by a convolutional layer with strides=2, while the number of filters is doubled. Within each stage, the layers have the same number filters and the same number of filters. Features maps sizes: stage 0: 32x32, 16 stage 1: 16x16, 32 stage 2: 8x8, 64 The Number of parameters is approx the same as Table 6 of [a]: ResNet20 0.27M ResNet32 0.46M ResNet44 0.66M ResNet56 0.85M ResNet110 1.7M # Arguments input_shape (tensor): shape of input image tensor depth (int): number of core convolutional layers num_classes (int): number of classes (CIFAR10 has 10) # Returns model (Model): Keras model instance """ if (depth - 2) % 6 != 0: raise ValueError('depth should be 6n+2 (eg 20, 32, 44 in [a])') # Start model definition. num_filters = 16 num_res_blocks = int((depth - 2) / 6) inputs = Input(shape=input_shape) x = resnet_layer(inputs=inputs) # Instantiate the stack of residual units for stack in range(3): for res_block in range(num_res_blocks): strides = 1 if stack > 0 and res_block == 0: # first layer but not first stack strides = 2 # downsample y = resnet_layer(inputs=x, num_filters=num_filters, strides=strides) y = resnet_layer(inputs=y, num_filters=num_filters, activation=None) if stack > 0 and res_block == 0: # first layer but not first stack # linear projection residual shortcut connection to match # changed dims x = resnet_layer(inputs=x, num_filters=num_filters, kernel_size=1, strides=strides, activation=None, batch_normalization=False) x = keras.layers.add([x, y]) x = Activation('relu')(x) num_filters *= 2 # Add classifier on top. # v1 does not use BN after last shortcut connection-ReLU x = AveragePooling2D(pool_size=8)(x) y = Flatten()(x) outputs = Dense(num_classes, activation='softmax', kernel_initializer='he_normal')(y) # Instantiate model. model = Model(inputs=inputs, outputs=outputs) return model
def network_2Dcnn(input_shape, activation=relu, num_classes=2): """2D CNN""" nfeat = 32 dropout_rate = 0.3 dil_rate = 2 batch_normalization = False activ = activation activ_end = 'softmax' init = 'normal' input1 = Input(shape=input_shape, name='input') if not batch_normalization: conv1 = Conv2D(nfeat, (3, 3), kernel_initializer=init, padding='same', dilation_rate=dil_rate, name="conv1")(input1) activ1 = Activation(activ, name="activ1")(conv1) conv2 = Conv2D(nfeat, (3, 3), kernel_initializer=init, padding='same', dilation_rate=dil_rate, name="conv2")(activ1) activ2 = Activation(activ, name="activ2")(conv2) skip0 = concatenate([activ1, activ2], axis=-1, name="skip0") pool1 = MaxPooling2D(pool_size=(2, 2), name="pool1")(skip0) drop1 = Dropout(dropout_rate, name="drop1")(pool1) conv5 = Conv2D(nfeat * 2, (3, 3), kernel_initializer=init, padding='same', dilation_rate=dil_rate, name="conv5")(drop1) activ5 = Activation(activ, name="activ5")(conv5) conv6 = Conv2D(nfeat * 2, (3, 3), kernel_initializer=init, padding='same', dilation_rate=dil_rate, name="conv6")(activ5) activ6 = Activation(activ, name="activ6")(conv6) skip1 = concatenate([activ6, pool1], axis=-1, name="skip1") gmp = GlobalMaxPooling2D(name="gmp")(skip1) drop2 = Dropout(dropout_rate, name="drop2")(gmp) dense1 = Dense(50, activation=activ, name="dense1")(drop2) activ7 = Activation(activ, name="activ7")(dense1) drop3 = Dropout(dropout_rate, name="drop3")(activ7) else: conv1 = Conv2D(nfeat, (3, 3), kernel_initializer=init, padding='same', dilation_rate=dil_rate, name="conv1")(input1) bn1 = BatchNormalization(name="bn1")(conv1) activ1 = Activation(activ, name="activ1")(bn1) conv2 = Conv2D(nfeat, (3, 3), kernel_initializer=init, padding='same', dilation_rate=dil_rate, name="conv2")(activ1) bn2 = BatchNormalization(name="bn2")(conv2) activ2 = Activation(activ, name="activ2")(bn2) skip0 = concatenate([activ1, activ2], axis=-1, name="skip0") pool1 = MaxPooling2D(pool_size=(2, 2), name="pool1")(skip0) drop1 = Dropout(dropout_rate, name="drop1")(pool1) conv5 = Conv2D(nfeat * 2, (3, 3), kernel_initializer=init, padding='same', dilation_rate=dil_rate, name="conv5")(drop1) bn5 = BatchNormalization(name="bn5")(conv5) activ5 = Activation(activ, name="activ5")(bn5) conv6 = Conv2D(nfeat * 2, (3, 3), kernel_initializer=init, padding='same', dilation_rate=dil_rate, name="conv6")(activ5) bn6 = BatchNormalization(name="bn6")(conv6) activ6 = Activation(activ, name="activ6")(bn6) skip1 = concatenate([pool1, activ6], axis=-1, name="skip1") gmp = GlobalMaxPooling2D(name="gmp")(skip1) drop2 = Dropout(dropout_rate, name="drop2")(gmp) dense1 = Dense(50, activation=activ, name="dense")(drop2) activ7 = Activation(activ, name="activ7")(dense1) drop3 = Dropout(dropout_rate, name="drop3")(activ7) out1 = Dense(num_classes, kernel_initializer=init, activation=activ_end, name="out1")(drop3) model = Model(inputs=input1, outputs=out1) return model
def get_activation(self, activation): if activation == 'leaky_relu': layer = LeakyReLU(alpha = 0.2) else: layer = Activation(activation) return layer
def builder(x): y = Conv2D(f, kernel, padding=padding, strides=strides)(x) y = Activation('relu')(y) y = BatchNormalization()(y) return y
def model(cls): model = Sequential() model.add(cls._init_conv2D()) model.add(cls._batchNorm()) model.add(PReLU()) model.add(cls._conv2D(16)) model.add(cls._batchNorm()) model.add(PReLU()) model.add(cls._maxPooling2D()) model.add(cls._conv2D(32)) model.add(cls._batchNorm()) model.add(PReLU()) model.add(cls._conv2D(32)) model.add(cls._batchNorm()) model.add(PReLU()) model.add(cls._conv2D(32)) model.add(cls._batchNorm()) model.add(PReLU()) model.add(cls._maxPooling2D()) model.add(cls._conv2D(64)) model.add(cls._batchNorm()) model.add(PReLU()) model.add(cls._conv2D(64)) model.add(cls._batchNorm()) model.add(PReLU()) model.add(cls._conv2D(64)) model.add(cls._batchNorm()) model.add(PReLU()) model.add(cls._maxPooling2D()) model.add(cls._conv2D(128)) model.add(cls._batchNorm()) model.add(PReLU()) model.add(cls._conv2D(128)) model.add(cls._batchNorm()) model.add(PReLU()) model.add(cls._conv2D(128)) model.add(cls._batchNorm()) model.add(PReLU()) model.add(cls._maxPooling2D()) model.add(Dropout(0.5)) model.add(Flatten()) model.add(cls._dense(config.PENULTIMATE_SIZE)) model.add(cls._batchNorm()) model.add(PReLU()) model.add(Dropout(0.5)) model.add(cls._dense(config.SOFTMAX_SIZE)) model.add(cls._batchNorm()) model.add(cls._dense(config.NUM_PAINTERS)) model.add(Activation("softmax")) model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.000074), metrics=['accuracy']) return model
x_val = x_val.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_val /= 255 x_test /= 255 #--------------------- Checks--------------------------- if K.image_data_format() != "channels_last": K.set_image_data_format("channels_last") # ----------------- MODEL ---------------------- input_shape = (img_size, img_size, 3) model = Sequential() model.add(Conv2D(32, (3, 3), padding='same', input_shape=input_shape)) model.add(Activation('relu')) model.add(Conv2D(32, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(64, (3, 3), padding='same')) model.add(Activation('relu')) model.add(Conv2D(64, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(512)) model.add(Activation('relu'))
img_types = ["FA", "anatomica"] # prepare input for the input_shape_2d = (inp_dim_2d, inp_dim_2d, len(img_types)) input_shape_3d = (inp_dim_3d, inp_dim_3d, inp_dim_3d, len(img_types)) ## paralel NN, x model_x = Sequential() print("Input shape to the 2d networks:", input_shape_2d) model_x.add( Convolution2D(nb_filters, kernel_size_2d[0], kernel_size_2d[1], border_mode='valid', input_shape=input_shape_2d)) model_x.add(Activation('relu')) print("Output shape of 1st convolution (2d):", model_x.output_shape) model_x.add( Convolution2D(nb_filters, kernel_size_2d[0], kernel_size_2d[1], border_mode='valid', input_shape=input_shape_2d)) model_x.add(Activation('relu')) print("Output shape of 2nd convolution (2d):", model_x.output_shape) model_x.add(Convolution2D(nb_filters, kernel_size_2d[0], kernel_size_2d[1])) model_x.add(Activation('relu')) print("Output shape of 3rd convolution (2d):", model_x.output_shape) model_x.add(MaxPooling2D(pool_size=pool_size_2d)) #model_x.add(Dropout(0.25)) print("Output shape after max pooling (2d):", model_x.output_shape)
im = np.transpose(x_idx[img_num, ::], (0, 1, 2)) ax.set_title(class_names[i]) plt.imshow(im) plt.show() x_train = x_train.astype('float32') / 255 x_val = x_val.astype('float32') / 255 x_test = x_test.astype('float32') / 255 y_train = keras.utils.to_categorical(y_train, num_classes) y_val = keras.utils.to_categorical(y_val, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) model = Sequential() model.add(Conv2D(32, (3, 3), padding='same', input_shape=x_train.shape[1:])) model.add(Activation('relu')) model.add(Conv2D(32, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(64, (3, 3), padding='same')) model.add(Activation('relu')) model.add(Conv2D(64, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(512)) model.add(Activation('relu'))
model = Sequential() model.add(Reshape((input_shape), input_shape=(WINDOW_LENGTH,) + input_shape)) if K.image_dim_ordering() == 'tf': print('tensorflow ordering') # (width, height, channels) model.add(Permute((2, 3, 1), input_shape=input_shape)) permute_shape = (MAP_X, MAP_Y, num_zones) elif K.image_dim_ordering() == 'th': # (channels, width, height) model.add(Permute((1, 2, 3), input_shape=input_shape)) permute_shape = (num_zones, MAP_X, MAP_Y) else: raise RuntimeError('Unknown image_dim_ordering.') model.add(Convolution2D(32, (8, 8), strides=(2, 2), padding='same')) model.add(Activation('relu')) model.add(Convolution2D(64, (4, 4), strides=(2, 2), padding='same')) model.add(Activation('relu')) model.add(Convolution2D(64, (3, 3), strides=(1, 1), padding='valid')) model.add(Activation('relu')) model.add(Flatten()) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dense(nb_actions)) model.add(Activation('linear')) print(model.summary()) # Finally, we configure and compile our agent. You can use every built-in Keras optimizer and # even the metrics! memory = SequentialMemory(limit=1000000, window_length=1) processor = MicropolisProcessor()
def f(input_tensor): nb_filter1, nb_filter2, nb_filter3 = filters if K.image_dim_ordering() == 'tf': bn_axis = 3 if dim3: bn_axis += 1 else: bn_axis = 1 if dim3: kernel_size0 = (1, 1, 1) kernel_sizes = (kernel_size, kernel_size, kernel_size) strides = (stride, stride, stride) conv = Conv3D else: kernel_size0 = (1, 1) kernel_sizes = (kernel_size, kernel_size) strides = (stride, stride) conv = Conv2D x = conv(nb_filter1, kernel_size0, strides=strides, kernel_regularizer=l2(weight_decay))(input_tensor) if batchnormalization: x = BatchNormalization(axis=bn_axis, momentum=batch_momentum)(x) if activation == 1: x = CReLU()(x) else: x = Activation('relu')(x) x = conv(nb_filter2, kernel_sizes, padding='same', kernel_regularizer=l2(weight_decay))(x) if batchnormalization: x = BatchNormalization(axis=bn_axis, momentum=batch_momentum)(x) if activation == 'CReLU': x = CReLU()(x) else: x = Activation('relu')(x) x = conv(nb_filter3, kernel_size0, kernel_regularizer=l2(weight_decay))(x) if batchnormalization: x = BatchNormalization(axis=bn_axis, momentum=batch_momentum)(x) shortcut = conv(nb_filter3, kernel_size0, strides=strides, kernel_regularizer=l2(weight_decay))(input_tensor) if batchnormalization: shortcut = BatchNormalization(axis=bn_axis, momentum=batch_momentum)(shortcut) x = add([x, shortcut]) if activation == 'CReLU': x = CReLU()(x) else: x = Activation(activation)(x) return x
def separable_inception_resnet_block(x, scale, block_type, block_idx, activation='relu'): """Adds a Inception-ResNet block. This function builds 3 types of Inception-ResNet blocks mentioned in the paper, controlled by the `block_type` argument (which is the block name used in the official TF-slim implementation): - Inception-ResNet-A: `block_type='block35'` - Inception-ResNet-B: `block_type='block17'` - Inception-ResNet-C: `block_type='block8'` # Arguments x: input tensor. scale: scaling factor to scale the residuals (i.e., the output of passing `x` through an inception module) before adding them to the shortcut branch. Let `r` be the output from the residual branch, the output of this block will be `x + scale * r`. block_type: `'block35'`, `'block17'` or `'block8'`, determines the network structure in the residual branch. block_idx: an `int` used for generating layer names. The Inception-ResNet blocks are repeated many times in this network. We use `block_idx` to identify each of the repetitions. For example, the first Inception-ResNet-A block will have `block_type='block35', block_idx=0`, ane the layer names will have a common prefix `'block35_0'`. activation: activation function to use at the end of the block (see [activations](../activations.md)). When `activation=None`, no activation is applied (i.e., "linear" activation: `a(x) = x`). # Returns Output tensor for the block. # Raises ValueError: if `block_type` is not one of `'block35'`, `'block17'` or `'block8'`. """ if block_type == 'block35': branch_0 = conv2d_bn(x, 32, 1) branch_1 = conv2d_bn(x, 32, 1) branch_1 = SeparableConv2D(filters=32, kernel_size=3, padding='same')(branch_1) branch_2 = conv2d_bn(x, 32, 1) branch_2 = SeparableConv2D(filters=48, kernel_size=3, padding='same')(branch_2) branch_2 = SeparableConv2D(filters=64, kernel_size=3, padding='same')(branch_2) branches = [branch_0, branch_1, branch_2] elif block_type == 'block17': branch_0 = conv2d_bn(x, 192, 1) branch_1 = conv2d_bn(x, 128, 1) branch_1 = SeparableConv2D(filters=160, kernel_size=[1, 7], padding='same')(branch_1) branch_1 = SeparableConv2D(filters=192, kernel_size=[7, 1], padding='same')(branch_1) branches = [branch_0, branch_1] elif block_type == 'block8': branch_0 = conv2d_bn(x, 192, 1) branch_1 = conv2d_bn(x, 192, 1) branch_1 = SeparableConv2D(filters=224, kernel_size=[1, 3], padding='same')(branch_1) branch_1 = SeparableConv2D(filters=256, kernel_size=[3, 1], padding='same')(branch_1) branches = [branch_0, branch_1] else: raise ValueError('Unknown Inception-ResNet block type. ' 'Expects "block35", "block17" or "block8", ' 'but got: ' + str(block_type)) block_name = block_type + '_' + str(block_idx) channel_axis = 1 if K.image_data_format() == 'channels_first' else 3 mixed = Concatenate(axis=channel_axis, name=block_name + '_mixed')(branches) up = conv2d_bn(mixed, K.int_shape(x)[channel_axis], 1, activation=None, use_bias=True, name=block_name + '_conv') x = Lambda(lambda inputs, scale: inputs[0] + inputs[1] * scale, output_shape=K.int_shape(x)[1:], arguments={'scale': scale}, name=block_name)([x, up]) if activation is not None: x = Activation(activation, name=block_name + '_ac')(x) return x
nb_filters1 = 32 nb_filters2 = 64 conv1_size = 3 conv2_size = 2 pool_size = 2 classes_num = 3 lr = 0.0001 model = Sequential() model.add( Convolution2D(nb_filters1, conv1_size, conv1_size, border_mode="same", input_shape=(img_width, img_height, 3))) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(pool_size, pool_size))) model.add( Convolution2D(nb_filters2, conv2_size, conv2_size, border_mode="same")) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(pool_size, pool_size), dim_ordering='th')) model.add(Flatten()) model.add(Dense(256, kernel_regularizer=regularizers.l2(.01))) ################################ #model.add(Dense(128,kernel_regularizer = regularizers.l2(.01))) ############################################################### model.add(Activation("relu"))
def NIH(max_token_length, vocabulary_size, rnn='gru' ,num_image_features=2048, hidden_size=512, embedding_size=512, regularizer=1e-8): # word embedding text_input = Input(shape=(max_token_length, vocabulary_size), name='text') text_mask = Masking(mask_value=0.0, name='text_mask')(text_input) text_to_embedding = TimeDistributed(Dense(units=embedding_size, kernel_regularizer=l2(regularizer), name='text_embedding'))(text_mask) text_dropout = Dropout(.5, name='text_dropout')(text_to_embedding) # object image embedding object_image_input = Input(shape=(max_token_length, num_image_features), name='image_object') object_image_embedding = TimeDistributed(Dense(units=embedding_size, kernel_regularizer=l2(regularizer), name='object_image_embedding'))(object_image_input) object_image_dropout = Dropout(.5,name='object_image_dropout')(object_image_embedding) # scene image embedding scene_image_input = Input(shape=(max_token_length, num_image_features), name='image_scene') scene_image_embedding = TimeDistributed(Dense(units=embedding_size, kernel_regularizer=l2(regularizer), name='scene_image_embedding'))(scene_image_input) scene_image_dropout = Dropout(.5,name='scene_image_dropout')(scene_image_embedding) #object attention object_attention_network = LSTM(units=hidden_size, return_sequences=True, name='object_attention_recurrent_network')(object_image_input) object_attention_network = Activation('tanh')(object_attention_network) object_attention_network = TimeDistributed(Dense(1))(object_attention_network) object_attention_probs = Activation('softmax')(object_attention_network) object_attention_mul =merge([object_image_embedding, object_attention_probs], mode='dot', dot_axes=(1, 1)) object_attention_permute=Permute((2, 1))(object_attention_mul) #scene attention scene_attention_network = LSTM(units=hidden_size, return_sequences=True, name='scene_attention_recurrent_network')(scene_image_input) scene_attention_network = Activation('tanh')(scene_attention_network) scene_attention_network = TimeDistributed(Dense(1))(scene_attention_network) scene_attention_probs = Activation('softmax')(scene_attention_network) scene_attention_mul =merge([object_image_embedding, scene_attention_probs], mode='dot', dot_axes=(1, 1)) scene_attention_permute=Permute((2, 1))(scene_attention_mul) # language model # recurrent_inputs = [text_dropout, image_dropout] left_recurrent_inputs = [text_dropout, object_attention_permute] left_merged_input = Add()(left_recurrent_inputs) right_recurrent_inputs = [text_dropout, scene_attention_permute] right_merged_input = Add()(right_recurrent_inputs) if rnn == 'gru': left_recurrent_network = GRU(units=hidden_size, recurrent_regularizer=l2(regularizer), kernel_regularizer=l2(regularizer), bias_regularizer=l2(regularizer), return_sequences=True, name='forward_recurrent_network')(left_merged_input) right_recurrent_network = GRU(units=hidden_size, recurrent_regularizer=l2(regularizer), kernel_regularizer=l2(regularizer), bias_regularizer=l2(regularizer), return_sequences=True, go_backwards=True, name='backword_recurrent_network')(right_merged_input) # if rnn == 'lstm': # recurrent_network = LSTM(units=hidden_size, # recurrent_regularizer=l2(regularizer), # kernel_regularizer=l2(regularizer), # bias_regularizer=l2(regularizer), # return_sequences=True, # name='recurrent_network')(merged_input) # elif rnn == 'gru': # recurrent_network = GRU(units=hidden_size, # recurrent_regularizer=l2(regularizer), # kernel_regularizer=l2(regularizer), # bias_regularizer=l2(regularizer), # return_sequences=True, # name='recurrent_network')(merged_input) else: raise Exception('Invalid rnn name') merged_recur=merge([left_recurrent_network, right_recurrent_network], mode='sum') output = TimeDistributed(Dense(units=vocabulary_size, kernel_regularizer=l2(regularizer), activation='softmax'), name='output')(merged_recur) inputs = [text_input, object_image_input,scene_image_input] model = Model(inputs=inputs, outputs=output) return model
# 导入需要的包, Sequential()是最简单的模型——序贯模型 from keras.models import Sequential from keras.layers import Dense, Dropout, Activation from keras.optimizers import SGD from keras.datasets import mnist import numpy as np # 搭建网络 model = Sequential() model.add(Dense(500, input_shape=(784, ))) #输入层, 28*28=784 model.add(Activation('tanh')) model.add(Dropout(0.5)) #50% dropout model.add(Dense(500)) #隐藏层, 500 model.add(Activation('tanh')) model.add(Dropout(0.5)) #50% dropout model.add(Dense(8)) #输出结果, 10 model.add(Activation('softmax')) # 详解 # 通过model.add()增加模型的层数。其中Dense()设定该层的结构,第一个参数表示输出的个数, # 第二个参数是接受的输入数据的格式。第一层中需要指定输入的格式,在之后的增加的层中输入层 # 节点数默认是上一层的输出个数。Actication()指定激活函数,Dropout()指定每层要丢掉的 # 节点信息百分比。输出层激活函数一般为softmax,不需要丢弃节点。 # 编译模型 sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) #设定学习效率等参数 model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) #使用交叉熵作为loss # 详解
lc = deepcopy(X['target']) del X['target'] y = [x>0 for x in lc] a = len(X) s = int(a*.7) trainX = X[:s] trainy = y[:s] test0X = X[s:] test0y = y[s:] trainX2 = np.asarray(trainX).reshape((trainX.shape[0], 1, trainX.shape[1])) test0X = np.asarray(test0X).reshape((test0X.shape[0], 1, test0X.shape[1])) keras.backend.clear_session() monkey = Sequential() monkey.add(LSTM(LSTMwidth,input_shape = (trainX2.shape[1], trainX2.shape[2]))) monkey.add(Dense(2)) monkey.add(Activation('softmax')) monkey.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['acc']) monkey.fit(trainX2,np.asarray(trainy),nb_epoch=30, validation_data=(test0X,np.asarray(test0y))) balances.append(1 - sum(test0y)/len(test0y)) preds = monkey.predict(test0X) predBalances.append(float(sum(x[0]>x[1] for x in preds))/float(len(preds))) rets = [] del monkey for i in thresh: hm = [int(x[1]-x[0]>i) for x in preds] cum = np.zeros(foreward) tc = list(lc[s:]) for j in range(len(tc)): if hm[j]==1: cum[j%foreward]+=tc[j] rr = np.exp(cum)
lstm1 = conc else: lstm1 = LSTM_use(hidden_size, return_sequences=True)(conc) if args.attention: lstm1b = Lambda(attentions_layer)(lstm1) else: lstm1b = lstm1 lstm4 = LSTM_use(hidden_size, return_sequences=True)(lstm1b) # x1 = Dense(hidden_size, activation='relu')(lstm4) # x2 = Dense(hidden_size, activation='relu')(x1) # x3 = Dense(hidden_size, activation='relu')(x2) conc2 = Concatenate()([x2,lstm4]) x3 = Dense(args.dense_size,activation='relu')(conc2) x4 = Dense(args.dense_size,activation='relu')(x3) x = Dense(max_output)(x4) predictions = Activation('softmax')(x) model = Model(inputs=inputs, outputs=predictions) import inspect with open(__file__) as f: a = f.readlines() startline = inspect.currentframe().f_lineno print(a[startline+1:startline+2]) optimizer = RMSprop(lr=args.lr, rho=0.9, epsilon=None, decay=0) print("learning rate",keras.backend.eval(optimizer.lr)) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['categorical_accuracy'])
def PSNR(y_true, y_pred): max_pixel = 1.0 return 10.0 * tf_log10((max_pixel ** 2) / (K.mean(K.square(y_pred - y_true)))) # Get the training and testing data train_list = get_image_list("./data/train/", scales=TRAIN_SCALES) test_list = get_image_list("./data/test/Set5/", scales=VALID_SCALES) input_img = Input(shape=IMG_SIZE) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(input_img) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model)
def AFNN(filters=8, image_channels=1, use_bnorm=True): layer_count = 0 inpt = Input(shape=(128, 128, image_channels), name='input'+str(layer_count)) # 1st layer, Conv+relu layer_count += 1 x_0 = Conv2D(filters=filters, kernel_size=(3, 3), strides=(2, 2), kernel_initializer='Orthogonal', padding='same', name='conv'+str(layer_count))(inpt) layer_count += 1 x_0 = Activation('relu', name='relu'+str(layer_count))(x_0) # Path 1 layer_count += 1 x1 = MaxPooling2D(pool_size=(2, 2), strides=None, padding='same', data_format=None, name='maxpool_p1'+str(layer_count))(x_0) # 2 layers, Conv+BN+relu+MaxPooling for i in range(2): layer_count += 1 x1 = Conv2D(filters=filters, kernel_size=(3, 3), strides=(2, 2), kernel_initializer='Orthogonal', padding='same', use_bias=False, name='conv_p1'+str(layer_count))(x1) if use_bnorm: layer_count += 1 x1 = BatchNormalization( axis=3, momentum=0.0, epsilon=0.0001, name='bn_p1'+str(layer_count))(x1) layer_count += 1 x1 = Activation('relu', name='relu_p1'+str(layer_count))(x1) x1 = MaxPooling2D(pool_size=(2, 2), strides=None, padding='same', data_format=None, name='maxpool_p1'+str(layer_count))(x1) i += 1 # Path 2 # 2 layers, Conv+BN+ReLU+MaxPooling layer_count += 1 x_1 = Conv2D(filters=filters, kernel_size=(7, 7), strides=(4, 4), kernel_initializer='Orthogonal', padding='same', name='conv_p2'+str(layer_count))(x_0) layer_count += 1 x_1 = Activation('relu', name='relu_p2'+str(layer_count))(x_1) # Path 2_1 # 1 layer, Conv+BN+ReLU+MaxPooling layer_count += 1 x2 = MaxPooling2D(pool_size=(2, 2), strides=None, padding='same', data_format=None, name='maxpool_p2'+str(layer_count))(x_1) layer_count += 1 x2 = Conv2D(filters=filters, kernel_size=(5, 5), strides=(2, 2), kernel_initializer='Orthogonal', padding='same', use_bias=False, name='conv_p2'+str(layer_count))(x2) if use_bnorm: layer_count += 1 x2 = BatchNormalization( axis=3, momentum=0.0, epsilon=0.0001, name='bn_p2'+str(layer_count))(x2) layer_count += 1 x2 = Activation('relu', name='relu_p2'+str(layer_count))(x2) x2 = MaxPooling2D(pool_size=(2, 2), strides=None, padding='same', data_format=None, name='maxpool_p2'+str(layer_count))(x2) # Path 2_2 # 1 layer, Conv+BN+ReLU+MaxPooling layer_count += 1 x_2 = Conv2D(filters=filters, kernel_size=(7, 7), strides=(4, 4), kernel_initializer='Orthogonal', padding='same', name='conv_p3'+str(layer_count))(x_1) if use_bnorm: layer_count += 1 x3 = BatchNormalization( axis=3, momentum=0.0, epsilon=0.0001, name='bn_p3'+str(layer_count))(x_2) layer_count += 1 x3 = Activation('relu', name='relu_p3'+str(layer_count))(x3) layer_count += 1 x3 = MaxPooling2D(pool_size=(2, 2), strides=None, padding='same', data_format=None, name='maxpool_p3'+str(layer_count))(x3) # Merge layer layer_count += 1 x = Concatenate(axis=-1, name='concat'+str(layer_count))([x1, x2, x3]) layer_count += 1 x = Flatten(data_format=None, name='Flat'+str(layer_count))(x) # Dense output layer layer_count += 1 x = Dense(128, activation='relu', name='dense'+str(layer_count))(x) layer_count += 1 x = Dropout(0.5, name='dropout'+str(layer_count))(x) layer_count += 1 y = Dense(1, activation='relu', name='dense'+str(layer_count))(x) model = Model(inputs=inpt, outputs=y) return model
# LOADING WEIGHTS TO FINE-TUNNE THEM model.load_weights(weights_path) pop_layer(model) pop_layer(model) # for layer in model.layers: # layer.trainable= False nb_classes=13 layer_last=Dense(nb_classes) layer_last.trainable=True layer_last2=Activation('softmax') layer_last2.trainable=True model.add(layer_last) model.add(layer_last2) print(model.summary()) # let's train the model using SGD + momentum (how original). #sgd = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer="sgd", metrics=['accuracy'])
def create_model(nb_classes, input_shape): """Create a VGG-16 like model.""" model = Sequential() print("input_shape: %s" % str(input_shape)) # input_shape = (None, None, 3) # for fcn model.add( Convolution2D(32, (3, 3), padding='same', input_shape=input_shape, kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( Convolution2D(32, (3, 3), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( Convolution2D(32, (2, 2), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001), strides=2)) model.add( Convolution2D(64, (3, 3), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( Convolution2D(64, (3, 3), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( Convolution2D(64, (2, 2), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001), strides=2)) model.add( Convolution2D(64, (3, 3), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( Convolution2D(64, (2, 2), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001), strides=2)) model.add(Convolution2D(512, (4, 4), padding='valid')) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add( Convolution2D(512, (1, 1), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add( Convolution2D(nb_classes, (1, 1), padding='same', kernel_initializer='he_uniform', kernel_regularizer=l2(0.0001))) model.add(BatchNormalization()) model.add(Activation('softmax')) model.add(Flatten()) # Remove for FCN return model
def modify_model_backprop(model, backprop_modifier): """Creates a copy of model by modifying all activations to use a custom op to modify the backprop behavior. Args: model: The `keras.models.Model` instance. backprop_modifier: One of `{'guided', 'rectified'}` Returns: A copy of model with modified activations for backwards pass. """ # The general strategy is as follows: # - Clone original model via save/load so that upstream callers don't see unexpected results with their models. # - Modify all activations in the model as ReLU. # - Save modified model so that it can be loaded with custom context modifying backprop behavior. # - Call backend specific function that registers the custom op and loads the model under modified context manager. # - Maintain cache to save this expensive process on subsequent calls. # # The reason for this round about way is because the graph needs to be rebuild when any of its layer builder # functions are changed. This is very complicated to do in Keras and makes the implementation very tightly bound # with keras internals. By saving and loading models, we dont have to worry about future compatibility. # # The only exception to this is the way advanced activations are handled which makes use of some keras internal # knowledge and might break in the future. # 0. Retrieve from cache if previously computed. modified_model = _MODIFIED_MODEL_CACHE.get((model, backprop_modifier)) if modified_model is not None: return modified_model model_path = '/tmp/' + next(tempfile._get_candidate_names()) + '.h5' try: # 1. Clone original model via save and load. model.save(model_path) modified_model = load_model(model_path) # 2. Replace all possible activations with ReLU. for i, layer in utils.reverse_enumerate(modified_model.layers): if hasattr(layer, 'activation'): layer.activation = tf.nn.relu if isinstance(layer, _ADVANCED_ACTIVATIONS): # NOTE: This code is brittle as it makes use of Keras internal serialization knowledge and might # break in the future. modified_layer = Activation('relu') modified_layer.inbound_nodes = layer.inbound_nodes modified_layer.name = layer.name modified_model.layers[i] = modified_layer # 3. Save model with modifications. modified_model.save(model_path) # 4. Register modifier and load modified model under custom context. modifier_fn = _BACKPROP_MODIFIERS.get(backprop_modifier) if modifier_fn is None: raise ValueError("'{}' modifier is not supported".format(backprop_modifier)) modifier_fn(backprop_modifier) # 5. Create graph under custom context manager. with tf.get_default_graph().gradient_override_map({'Relu': backprop_modifier}): # This should rebuild graph with modifications. modified_model = load_model(model_path) # Cache to improve subsequent call performance. _MODIFIED_MODEL_CACHE[(model, backprop_modifier)] = modified_model return modified_model finally: os.remove(model_path)
Y_test = numpy.array([True] * (10 ** 2) + [False] * (10 ** 2)) X_train = X_train.astype("float32") X_test = X_test.astype("float32") Y_train = Y_train.astype("bool") Y_test = Y_test.astype("bool") # build deep learning model from keras.optimizers import RMSprop from keras.models import Sequential from keras.layers import Activation, Dense, Dropout model = Sequential() # takes a 128 vector as input and outputs a 50 node layer, densely connected model.add(Dense(50, input_dim=128)) model.add(Activation('relu')) model.add(Dropout(0.2)) model.add(Dense(50)) model.add(Activation('relu')) model.add(Dropout(0.2)) # model.add(Dense(1, init='normal')) # for regression - just end here, no sigmoid layer model.add(Dense(1)) # for classification model.add(Activation('sigmoid')) # for classification, must add this rms = RMSprop() model.compile(loss='binary_crossentropy', optimizer=rms, metrics=['accuracy']) batch_size = 32 nb_epoch = 3 model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=2, validation_data=(X_test, Y_test))