if __name__ == "__main__": # create Spark context with Spark configuration conf = SparkConf().setAppName("Spark Count") sc = SparkContext(conf=conf) loader = mnist_loader() X_train, y_train, X_test, y_test = loader.load((0, 1)) #the network model = Sequential(name="mlp") model.add(ll.InputLayer([28, 28])) model.add(ll.Flatten()) model.add(ll.Dense(50)) model.add(ll.Activation('sigmoid')) model.add(ll.Dense(10, activation='softmax')) model.compile("adam", "categorical_crossentropy", metrics=["accuracy"]) model.summary() model_json = model.to_json() weights = model.get_weights() tasks = 4 idx = [] part = 60000 / tasks for i in xrange(tasks): idx.append((part * i, part * (i + 1)))
pooling=None) #base_model.summary() #pdb.set_trace() model.add(base_model) model.add(layers.Flatten()) #model.add(layers.BatchNormalization()) #model.add(layers.Dense(128, activation='relu')) #model.add(layers.Dropout(0.5)) #model.add(layers.BatchNormalization()) #model.add(layers.Dense(64, activation='relu')) #model.add(layers.Dropout(0.5)) #model.add(layers.BatchNormalization()) model.add(layers.Dense(10, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=args_lr), metrics=['accuracy']) #model.summary() print(model_type) #pdb.set_trace() current_epoch = 0 ################### connects interrupt signal to the process #####################
#pdb.set_trace() #model.add(layers.UpSampling2D((2,2))) #model.add(layers.UpSampling2D((2,2))) #model.add(layers.UpSampling2D((2,2))) model.add(base_model) model.add(layers.Flatten()) #model.add(layers.BatchNormalization()) #model.add(layers.Dense(128, activation='relu')) #model.add(layers.Dropout(0.5)) #model.add(layers.BatchNormalization()) #model.add(layers.Dense(64, activation='relu')) #model.add(layers.Dropout(0.5)) #model.add(layers.BatchNormalization()) model.add(layers.Dense( 10, activation='softmax')) #, kernel_initializer='he_uniform')) model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=args_lr), metrics=['accuracy']) #model.summary() print(model_type) #pdb.set_trace() current_epoch = 0 ################### connects interrupt signal to the process #####################
import keras from keras import layers import numpy as np import os from keras.preprocessing import image # GAN生成网络 latent_dim = 32 height = 32 width = 32 channels = 3 generator_input = keras.Input(shape=(latent_dim, )) x = layers.Dense(128 * 16 * 16)(generator_input) x = layers.LeakyReLU()(x) x = layers.Reshape((16, 16, 128))(x) x = layers.Conv2D(256, 5, padding='same')(x) x = layers.LeakyReLU()(x) x = layers.Conv2DTranspose(256, 4, strides=2, padding='same')(x) x = layers.LeakyReLU()(x) x = layers.Conv2D(256, 5, padding='same')(x) x = layers.LeakyReLU()(x) x = layers.Conv2D(256, 5, padding='same')(x) x = layers.LeakyReLU()(x) x = layers.Conv2D(channels, 7, activation='tanh', padding='same')(x) generator = keras.models.Model(generator_input, x)
def make_model_128_embedding(): input_img = Input(shape=(128, 128, 3)) with K.name_scope('Encoder'): with K.name_scope('Convolution_layer_1'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) #x=layers.Dropout(0.3)(x) #64 with K.name_scope('Convolution_layer_2'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) #x=layers.Dropout(0.3)(x) #32 with K.name_scope('Convolution_layer_3'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) #x=layers.Dropout(0.3)(x) #16 with K.name_scope('Convolution_layer_4'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same', name='encoded_output')(x) #x=layers.Dropout(0.3)(x) # x=Round(x.shape,name='Rounder')(x) # x=layers.Embedding(1024,1024)(x) # x=Add()(x) # x=layers.Reshape((8,8,16))(x) x = layers.Dense(512, activation='relu')(x) x = layers.Dense(1024, activation='relu')(x) with K.name_scope('Decoder'): with K.name_scope('DeConvolutiopn_layer_1'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x) x1 = layers.BatchNormalization()(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) #x1=layers.Dropout(0.3)(x1) #16 with K.name_scope('DeConvolution_layer_2'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) #x1=layers.Dropout(0.3)(x1) #3 with K.name_scope('DeConvolution_layer_3'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) #x1=layers.Dropout(0.3)(x1) #32 with K.name_scope('DeConvolution_layer_4'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) #64 with K.name_scope('DeConvolution_layer_5_with_output'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) decoder_output = layers.Conv2D(3, (3, 3), activation='relu', padding='same', name='final_output')(x1) #128 final_model = Model(input_img, decoder_output) final_model.compile(loss='mean_squared_error', optimizer='adadelta', metrics=['acc']) return final_model
def Build_CNN(input_shape, n_class): x = layers.Input(shape=input_shape) conv1 = layers.Conv2D(filters=64, kernel_size=(1, 12), strides=(1, 1), padding='same', dilation_rate=5)(x) conv1 = ELU(alpha=0.5)(conv1) conv1 = BN()(conv1) conv1 = layers.Conv2D(filters=64, kernel_size=(1, 12), strides=(1, 2), padding='same', dilation_rate=1)(conv1) conv1 = ELU(alpha=0.5)(conv1) conv1 = BN()(conv1) conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1) conv1 = layers.Conv2D(filters=96, kernel_size=(1, 9), strides=1, padding='same', dilation_rate=4)(conv1) conv1 = ELU(alpha=0.5)(conv1) conv1 = BN()(conv1) conv1 = layers.Conv2D(filters=96, kernel_size=(1, 9), strides=1, padding='same', dilation_rate=4)(conv1) conv1 = ELU(alpha=0.5)(conv1) conv1 = BN()(conv1) conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1) conv1 = layers.Conv2D(filters=128, kernel_size=(1, 6), strides=1, padding='same', dilation_rate=3)(conv1) conv1 = ELU(alpha=0.5)(conv1) conv1 = BN()(conv1) conv1 = layers.Conv2D(filters=128, kernel_size=(1, 6), strides=1, padding='same', dilation_rate=3)(conv1) conv1 = ELU(alpha=0.5)(conv1) conv1 = BN()(conv1) conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1) conv1 = layers.Conv2D(filters=192, kernel_size=(1, 3), strides=1, padding='same', dilation_rate=2)(conv1) conv1 = ELU(alpha=0.5)(conv1) conv1 = BN()(conv1) conv1 = layers.Conv2D(filters=192, kernel_size=(1, 3), strides=1, padding='same', dilation_rate=2)(conv1) conv1 = ELU(alpha=0.5)(conv1) conv1 = BN()(conv1) conv1 = layers.Conv2D(filters=192, kernel_size=(1, 3), strides=1, padding='same', dilation_rate=2)(conv1) conv1 = ELU(alpha=0.5)(conv1) conv1 = BN()(conv1) conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1) conv1 = layers.Conv2D(filters=256, kernel_size=(1, 3), strides=1, padding='same', dilation_rate=2)(conv1) conv1 = ELU(alpha=0.5)(conv1) conv1 = BN()(conv1) conv1 = layers.Conv2D(filters=256, kernel_size=(1, 3), strides=1, padding='same', dilation_rate=2)(conv1) conv1 = ELU(alpha=0.5)(conv1) conv1 = BN()(conv1) conv1 = layers.Conv2D(filters=256, kernel_size=(1, 3), strides=1, padding='same', dilation_rate=2)(conv1) conv1 = ELU(alpha=0.5)(conv1) conv1 = BN()(conv1) conv1 = layers.GlobalAveragePooling2D(data_format='channels_last')(conv1) #conv1 = layers.Dense(50, activation = 'tanh')(conv1) output = layers.Dense(n_class, activation='softmax')(conv1) model = models.Model(x, output) return model
def to_one_hot(labels, dimension=46): results = np.zeros((len(labels), dimension)) for i, label in enumerate(labels): results[i, label] = 1. return results y_train = to_one_hot(train_labels) y_test = to_one_hot(test_labels) '''模型定义''' from keras import models from keras import layers model = models.Sequential() model.add(layers.Dense(64, activation='relu', input_shape=(10000, ))) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dense(46, activation='softmax')) '''编译模型''' model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) '''验证方法''' x_val = x_train[:1000] partial_x_train = x_train[1000:] y_val = y_train[:1000] partial_y_train = y_train[1000:] history = model.fit(partial_x_train, partial_y_train,
def _build_model(self): ''' The model has 3 inputs as defined in the get_states method of the Car_handler class (in Group_handler.py)''' # driving car input branch player = layers.Input(batch_shape=(None, 4)) # dense1 = layers.Dense(8, activation = 'relu')(player) dense1 = PReLU()(layers.Dense( 8, kernel_initializer='random_uniform', bias_initializer='random_uniform')(player)) # target position input branch target = layers.Input(batch_shape=(None, 4)) # dense2 = layers.Dense(8, activation = 'relu')(target) dense2 = PReLU()(layers.Dense( 8, kernel_initializer='random_uniform', bias_initializer='random_uniform')(target)) # objects-to-avoid input branch mov_sta = layers.Input(batch_shape=INPUT_SHAPE) # conv = layers.Conv2D(8, (1,4), activation="relu")(mov_sta) conv = PReLU()(layers.Conv2D( 8, (1, 4), kernel_initializer='random_uniform', bias_initializer='random_uniform')(mov_sta)) flat = layers.Flatten()(conv) # dense5 = layers.Dense(8, activation = 'relu')(flat) dense5 = PReLU()(layers.Dense(8, kernel_initializer='random_uniform', bias_initializer='random_uniform')(flat)) # merge the first 2 branches conc1 = layers.concatenate([dense1, dense2]) # dense3 = layers.Dense(4, activation='relu')(conc1) dense3 = PReLU()(layers.Dense( 4, kernel_initializer='random_uniform', bias_initializer='random_uniform')(conc1)) # then merge with the third conc2 = layers.concatenate([dense3, dense5]) # dense4 = layers.Dense(16, activation='relu')(conc2) dense4 = PReLU()(layers.Dense( 16, kernel_initializer='random_uniform', bias_initializer='random_uniform')(conc2)) # finally split into the 2 outputs (policy,value) policy = layers.Dense(NUM_ACTIONS, activation='softmax', kernel_initializer='random_uniform', bias_initializer='random_uniform')(dense4) value = layers.Dense(1, activation='linear', kernel_initializer='random_uniform', bias_initializer='random_uniform')(dense4) model = models.Model(inputs=[player, target, mov_sta], outputs=[policy, value]) model.compile( optimizer="rmsprop", loss={ 'dense_7': 'mean_squared_error', 'dense_6': 'categorical_crossentropy' }, metrics={ 'dense_7': 'mse', 'dense_6': 'accuracy' }, ) if self.load_weights: model.load_weights('weights/pretrain_weights.h5') model.summary() return model
from keras import layers from keras import models import os model = models.Sequential() model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(180,180, 3))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(32, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(32, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(128, (3, 3), activation='relu')) model.add(layers.Flatten()) model.add(layers.Dense(512, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) from keras import optimizers model.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4),metrics=['acc']) from keras.preprocessing.image import ImageDataGenerator base_dir = '/content/drive/My Drive/FaceClassification' train_dir = os.path.join(base_dir, 'train') validation_dir = os.path.join(base_dir, 'validation') test_dir = os.path.join(base_dir, 'test') train_datagen = ImageDataGenerator(rescale=1./255) test_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory(train_dir, target_size=(180,180),batch_size=100, class_mode='binary') validation_generator = test_datagen.flow_from_directory(test_dir, target_size=(180,180), batch_size=100,class_mode='binary') history = model.fit_generator(train_generator, steps_per_epoch=100, epochs=7, validation_data=validation_generator,validation_steps=20) model.save(os.path.join(base_dir, 'face_with_glsses1.h5'))
def __init__(self, shape): self.re_rate = 0.9 self.inputs = layers.Input(shape=shape) self.f_block = layers.Conv3D(4, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2( self.re_rate), padding='same')(self.inputs) self.bn = layers.BatchNormalization()(self.f_block) self.mp1 = layers.MaxPooling3D((2, 2, 2))(self.bn) self.f_block1 = layers.Conv3D(8, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2( self.re_rate), padding='same')(self.mp1) self.bn = layers.BatchNormalization()(self.f_block1) self.mp2 = layers.MaxPooling3D((2, 2, 2))(self.bn) self.f_block2 = layers.Conv3D(8, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2( self.re_rate), padding='same')(self.mp2) self.f_block2 = layers.BatchNormalization()(self.f_block2) self.b_back2 = layers.Conv3D(8, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2( self.re_rate), padding='same')(self.f_block2) self.b_back2 = layers.BatchNormalization()(self.b_back2) self.b_back2 = layers.Conv3D( 8, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(layers.UpSampling3D((2, 2, 2))(self.f_block2)) self.b_back2 = layers.BatchNormalization()(self.b_back2) self.b_back1 = layers.Conv3D( 8, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(layers.UpSampling3D((2, 2, 2))(self.b_back2)) self.b_back1 = layers.BatchNormalization()(self.b_back1) self.gb = layers.GlobalAveragePooling3D()(self.b_back1) self.drop = layers.Dropout(rate=0.9)(self.gb) # add mmse mmse_input = layers.Input(shape=(1, ), dtype='int32') embedded_layer = layers.Embedding(shape[-1], 1)(mmse_input) emCon = layers.Flatten()(embedded_layer) self.drop = layers.concatenate([self.drop, emCon]) # add sex sex_input = layers.Input(shape=(1, ), dtype='int32') embedded_layer = layers.Embedding(shape[-1], 1)(sex_input) emCon = layers.Flatten()(embedded_layer) self.drop = layers.concatenate([self.drop, emCon]) # add age age_input = layers.Input(shape=(1, ), dtype='int32') embedded_layer = layers.Embedding(shape[-1], 1)(age_input) emCon = layers.Flatten()(embedded_layer) self.drop = layers.concatenate([self.drop, emCon]) # add marriage marriage_input = layers.Input(shape=(1, ), dtype='int32') embedded_layer = layers.Embedding(shape[-1], 1)(marriage_input) emCon = layers.Flatten()(embedded_layer) self.drop = layers.concatenate([self.drop, emCon]) # add apoe4 apoe4_input = layers.Input(shape=(1, ), dtype='int32') embedded_layer = layers.Embedding(shape[-1], 1)(apoe4_input) emCon = layers.Flatten()(embedded_layer) self.drop = layers.concatenate([self.drop, emCon]) # add education edu_input = layers.Input(shape=(1, ), dtype='int32') embedded_layer = layers.Embedding(shape[-1], 1)(edu_input) emCon = layers.Flatten()(embedded_layer) self.drop = layers.concatenate([self.drop, emCon]) self.dense = layers.Dense(1, activation='sigmoid')(self.drop) self.model = keras.Model(input=[ self.inputs, mmse_input, sex_input, age_input, marriage_input, apoe4_input, edu_input ], output=self.dense)
def ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, **kwargs): """Instantiates the ResNet50 architecture. Optionally loads weights pre-trained on ImageNet. Note that the data format convention used by the model is the one specified in your Keras config at `~/.keras/keras.json`. # Arguments include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 224)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 32. E.g. `(200, 200, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional block. - `avg` means that global average pooling will be applied to the output of the last convolutional block, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ # global backend, layers, models, keras_utils # backend, layers, models, keras_utils = get_submodules_from_kwargs(kwargs) if not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as `"imagenet"` with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=32, data_format=backend.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor if backend.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input) x = layers.Conv2D(64, (7, 7), strides=(2, 2), padding='valid', kernel_initializer='he_normal', name='conv1')(x) x = layers.BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = layers.Activation('relu')(x) x = layers.ZeroPadding2D(padding=(1, 1), name='pool1_pad')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') 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 = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') 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 = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') if include_top: x = layers.GlobalAveragePooling2D(name='avg_pool')(x) x = layers.Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D()(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D()(x) else: warnings.warn('The output shape of `ResNet50(include_top=False)` ' 'has been changed since Keras 2.2.0.') # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = keras_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = models.Model(inputs, x, name='resnet50') # Load weights. if weights == 'imagenet': if include_top: weights_path = keras_utils.get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='a7b3fe01876f51b976af0dea6bc144eb') else: weights_path = keras_utils.get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='a268eb855778b3df3c7506639542a6af') model.load_weights(weights_path) if backend.backend() == 'theano': keras_utils.convert_all_kernels_in_model(model) elif weights is not None: model.load_weights(weights) return model
input_shape=(32, 32, 3), pooling=None) elif '19' in args_model: base_model = VGG19(weights=None, include_top=False, input_shape=(32, 32, 3), pooling=None) #base_model.summary() #pdb.set_trace() model.add(base_model) model.add(layers.Flatten()) model.add(layers.BatchNormalization()) model.add(layers.Dense( 128, activation='relu')) #, kernel_initializer='he_uniform')) #model.add(layers.Dropout(0.2)) model.add(layers.BatchNormalization()) model.add(layers.Dense( 64, activation='relu')) #, kernel_initializer='he_uniform')) #model.add(layers.Dropout(0.2)) model.add(layers.BatchNormalization()) model.add(layers.Dense( 2, activation='softmax')) #, kernel_initializer='he_uniform')) model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=args_lr), metrics=['accuracy']) #model.summary() print(model_type)
from keras import models from keras import layers from keras.utils import to_categorical # load mnist dataset (train_images, train_labels), (test_images, test_labels) = mnist.load_data() # print dataset's info print(train_images.shape) print(len(train_labels)) print(test_images.shape) print(len(test_labels)) # construct networks network = models.Sequential() network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28, ))) network.add(layers.Dense(10, activation='softmax')) # set networks network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # resize dataset and normalize it train_images = train_images.reshape((60000, 28 * 28)) train_images = train_images.astype('float32') / 255 test_images = test_images.reshape((10000, 28 * 28)) test_images = test_images.astype('float32') / 255 # check what's to_categorical work
x_train = vectorize_sequences(train_data) x_test = vectorize_sequences(test_data) y_train = np.asarray(train_labels).astype('float32') y_test = np.asarray(test_labels).astype('float32') # Setting aside a validation set x_val = x_train[:10000] partial_x_train = x_train[10000:] y_val = y_train[:10000] partial_y_train = y_train[10000:] # The model definition: hidden layer's size of 16 model = models.Sequential() model.add(layers.Dense(16, activation='relu', input_shape=(10000, ))) model.add(layers.Dense(16, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) # Compiling the model model.compile( optimizer='rmsprop', loss= 'binary_crossentropy', # it is the best option with models that outputs probabilities metrics=['acc']) history = model.fit(partial_x_train, partial_y_train, epochs=20, batch_size=512, validation_data=(x_val, y_val))
#train_labels = train_data_df['SzBd400'].as_matrix().astype('float32') #test_labels = test_data_df['SzBd400'].as_matrix().astype('float32') train_data = train_data_df.as_matrix( )[:, 2:] #the 2: ensures that the patient number and tumor type are excluded from the training data. test_data = test_data_df.as_matrix()[:, 2:] means = train_data.mean(axis=0) sigmas = train_data.std(axis=0) train_data = (train_data - means) / sigmas test_data = (test_data - means) / sigmas model = models.Sequential() model.add( layers.Dense(8, activation='relu', input_shape=(train_data.shape[1], ))) model.add(layers.Dropout(0.5)) model.add(layers.Dense(8, activation='relu')) #model.add(layers.Dropout(0.5)) #model.add(layers.Dense(4, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) history = model.fit(train_data, train_labels, epochs=20, batch_size=8, validation_data=(test_data, test_labels))
def VGGFace_multimodal(input_shape, n_class): """ :param input_shape: data shape, 3d, [width, height, channels] :param n_class: number of classes :return: Keras Model used for training """ # RGB MODALITY BRANCH OF CNN inputs_rgb = layers.Input(shape=input_shape) ########################VGG/RESNET or any other network vgg_model_rgb = VGG16(include_top=False, weights='vggface', input_tensor=None, input_shape=input_shape, pooling=None, type_name='rgb') conv_model_rgb = vgg_model_rgb(inputs_rgb) ######################## inputs_depth = layers.Input(shape=input_shape) vgg_model_depth = VGG16(include_top=False, weights='vggface', input_tensor=None, input_shape=input_shape, pooling=None, type_name='depth') conv_model_depth = vgg_model_depth(inputs_depth) ###################### # conv_model_depth = vgg_model_depth(inputs_depth) # fc6_rgb = layers.Dense(2048, activation='relu', name='fc6_rgb')(dropout_rgb) # fc6_depth = layers.Dense(2048, activation='relu', name='fc6_depth')(dropout_depth) # CONACTENATE the ends of RGB & DEPTH merge_rgb_depth = layers.concatenate([conv_model_rgb,conv_model_depth], axis=-1) attention_features = cbam_block(merge_rgb_depth) # # # ############ for RGB # flat_model_rgb = layers.Flatten(name='flatten_rgb')(conv_model_rgb) # fc6_rgb = layers.Dense(2048, activation='relu', name='fc6_rgb')(flat_model_rgb) # dropout_rgb = layers.Dropout(0.2)(fc6_rgb) ######## for Depth flat_model = layers.Flatten(name='flatten')(attention_features) fc6 = layers.Dense(2048, activation='relu', name='fc6')(flat_model) bn_1 = BatchNormalization(name='1_bn')(fc6) dropout_1 = layers.Dropout(0.5)(bn_1) # flatten_concat = layers.Flatten(name='flatten_concat')(merge_rgb_depth) # fc6 = layers.Dense(2048, activation='relu', name='fc6')(merge_rgb_depth) fc7 = layers.Dense(1024, activation='relu', name='fc7')(dropout_1) bn_2 = BatchNormalization(name='2_bn')(fc7) dropout_2 = layers.Dropout(0.5)(bn_2) fc8 = layers.Dense(512, activation='relu', name='fc8')(dropout_2) bn_3 = BatchNormalization(name='3_bn')(fc8) dropout_3 = layers.Dropout(0.5)(bn_3) #VECTORIZING OUTPUT output = layers.Dense(n_class, activation='softmax', name='output')(dropout_3) # MODAL [INPUTS , OUTPUTS] train_model = models.Model(inputs=[inputs_rgb, inputs_depth], outputs=[output]) # weights_path = 'CurtinFaces/vgg_multimodal_dropout-0.5_3fc-512/weights-25.h5' # train_model.load_weights(weights_path) train_model.summary() for layer in train_model.layers[:-26]: layer.trainable = False # for layer in train_model.layers[2].layers[:-4]: # layer.trainable = False # for layer in train_model.layers[3].layers[:-4]: # layer.trainable = False return train_model
def resnet(channels, init_block_channels, bottleneck, conv1_stride, in_channels=3, in_size=(224, 224), classes=1000): """ ResNet model from 'Deep Residual Learning for Image Recognition,' https://arxiv.org/abs/1512.03385. Parameters: ---------- channels : list of list of int Number of output channels for each unit. init_block_channels : int Number of output channels for the initial unit. bottleneck : bool Whether to use a bottleneck or simple block in units. conv1_stride : bool Whether to use stride in the first or the second convolution layer in units. in_channels : int, default 3 Number of input channels. in_size : tuple of two ints, default (224, 224) Spatial size of the expected input image. classes : int, default 1000 Number of classification classes. """ input_shape = (in_channels, 224, 224) if is_channels_first() else (224, 224, in_channels) input = nn.Input(shape=input_shape) x = res_init_block( x=input, in_channels=in_channels, out_channels=init_block_channels, name="features/init_block") in_channels = init_block_channels for i, channels_per_stage in enumerate(channels): for j, out_channels in enumerate(channels_per_stage): strides = 2 if (j == 0) and (i != 0) else 1 x = res_unit( x=x, in_channels=in_channels, out_channels=out_channels, strides=strides, bottleneck=bottleneck, conv1_stride=conv1_stride, name="features/stage{}/unit{}".format(i + 1, j + 1)) in_channels = out_channels x = nn.AvgPool2D( pool_size=7, strides=1, name="features/final_pool")(x) x = flatten(x) x = nn.Dense( units=classes, input_dim=in_channels, name="output")(x) model = Model(inputs=input, outputs=x) model.in_size = in_size model.classes = classes return model
x_test = vectorizeSequences(test_data) y_train = np.asarray(train_labels).astype('float32') y_test = np.asarray(test_labels).astype('float32') # split train_data, set apart 10000 samples as validation x_validation = x_train[:10000] x_training = x_train[10000:] y_validation = y_train[:10000] y_training = y_train[10000:] # model definition model = models.Sequential() # full connect layer(dense layer) model.add(layers.Dense(units=16, activation='relu', input_shape=(10000, ))) model.add(layers.Dense(units=16, activation='relu', input_shape=(16, ))) model.add(layers.Dense(units=1, activation='sigmoid', input_shape=(16, ))) # compile the model # rmspop; Root Mean Square Prop model.compile(optimizer=optimizers.RMSprop(lr=0.001), loss=losses.binary_crossentropy, metrics=[metrics.binary_accuracy]) # training model history = model.fit(x=x_training, y=y_training, epochs=20, batch_size=512,
for j in range(r): X1[-1][9 - j] = 0.0 # Padding ka c*udap X1, Y1 = np.array(X1), np.array(Y1) # X1, Y1 = sklearn.utils.shuffle(X1,Y1) X1, Y1 = X1.reshape(5000, -1, 1), Y1.reshape(-1, 1) print(X1.shape, Y1.shape) #%%[markdown] # ## Construct Model #%% m_input = Input(batch_shape=(None, 10, 1), name='line') m = layers.SimpleRNN(10, return_sequences=True)(m_input) m = layers.SimpleRNN(5, return_sequences=False)(m) m = layers.Dense(1)(m) # m = layers.LSTM((1), return_sequences=True)(m) m = models.Model(m_input, m) m.compile(loss="mse", optimizer="rmsprop", metrics=['accuracy']) m.summary() #%% [markdown] # ## Train Model #%% m.fit(X1, Y1, epochs=30, batch_size=5) #%% [markdown] # ## Test #%%
include_top=False, input_shape=(32, 32, 3), pooling=None) #base_model = VGG16(weights=None, input_shape=(32, 32, 3)) #base_model.summary() #pdb.set_trace() #model.add(layers.UpSampling2D((2,2))) #model.add(layers.UpSampling2D((2,2))) #model.add(layers.UpSampling2D((2,2))) model.add(base_model) model.add(layers.Flatten()) model.add(layers.BatchNormalization()) model.add(layers.Dense(128, activation='relu')) #model.add(layers.Dropout(0.2)) model.add(layers.BatchNormalization()) model.add(layers.Dense(64, activation='relu')) #model.add(layers.Dropout(0.2)) model.add(layers.BatchNormalization()) model.add(layers.Dense(10, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=lr_schedule(0)), metrics=['accuracy']) #model.fit(x_train, y_train, epochs=1, batch_size=20, validation_data=(x_test, y_test)) #model.summary() print(model_type)
def __init__(self, **kwargs): super(MLP, self).__init__(**kwargs) self.dense_1 = layers.Dense(64, activation='relu') self.dense_2 = layers.Dense(10)
def build_model(self): """Build a critic (value) network that maps (state, action) pairs -> Q-values.""" # Define input layers states = layers.Input(shape=(self.state_size, ), name="states") actions = layers.Input(shape=(self.action_size, ), name="actions") states_and_actions = layers.concatenate([states, actions]) net1 = layers.Dense(units=400, activation="relu")(states_and_actions) net1 = layers.Dense(units=300, activation="relu")(net1) net2 = layers.Dense(units=400, activation="relu")(states_and_actions) net2 = layers.Dense(units=300, activation="relu")(net2) # Add hidden layer(s) for state pathway # net_states = layers.Dense(units=32, kernel_regularizer=regularizers.l2(0.01))( # states # ) # net_states = layers.BatchNormalization()(net_states) # net_states = layers.Activation("relu")(net_states) # net_states = layers.Dense(units=64, kernel_regularizer=regularizers.l2(0.01))( # net_states # ) # net_states = layers.BatchNormalization()(net_states) # net_states = layers.Activation("relu")(net_states) # # Add hidden layer(s) for action pathway # net_actions = layers.Dense(units=32, kernel_regularizer=regularizers.l2(0.01))( # actions # ) # net_actions = layers.BatchNormalization()(net_actions) # net_actions = layers.Activation("relu")(net_actions) # net_actions = layers.Dense(units=64, kernel_regularizer=regularizers.l2(0.01))( # net_actions # ) # net_actions = layers.BatchNormalization()(net_actions) # net_actions = layers.Activation("relu")(net_actions) # # Try different layer sizes, activations, add batch normalization, regularizers, etc. # # Combine state and action pathways # net = layers.Add()([net_states, net_actions]) # net = layers.Activation("relu")(net) # # Add more layers to the combined network if needed # # Add final output layer to prduce action values (Q values) # Q_values = layers.Dense(units=1, name="q_values")(net) # net_states = layers.Dense(units=400)(states) # # net_states = layers.BatchNormalization()(net_states) # net_states = layers.Activation("relu")(net_states) # net_states = layers.Dense(units=300)(net_states) # # net_states = layers.BatchNormalization()(net_states) # net_states = layers.Activation("relu")(net_states) # # # Add hidden layer(s) for action pathway # net_actions = layers.Dense(units=400)(actions) # # net_actions = layers.BatchNormalization()(net_actions) # net_actions = layers.Activation("relu")(net_actions) # net_actions = layers.Dense(units=300)(net_actions) # # net_actions = layers.BatchNormalization()(net_actions) # net_actions = layers.Activation("relu")(net_actions) # # # Combine state and action pathways # net = layers.Add()([net_states, net_actions]) # net = layers.Activation("relu")(net) # # Add final output layer to prduce action values (Q values) Q1_values = layers.Dense( units=1, name="q1_values", kernel_initializer=layers.initializers.RandomUniform(minval=-0.003, maxval=0.003), )(net1) Q2_values = layers.Dense( units=1, name="q2_values", kernel_initializer=layers.initializers.RandomUniform(minval=-0.003, maxval=0.003), )(net2) # Create Keras model self.model1 = models.Model(inputs=[states, actions], outputs=Q1_values) self.model2 = models.Model(inputs=[states, actions], outputs=Q2_values) # Define optimizer and compile model for training with built-in loss function optimizer1 = optimizers.Adam(lr=1e-3) optimizer2 = optimizers.Adam(lr=1e-3) self.model1.compile(optimizer=optimizer1, loss="mse") self.model2.compile(optimizer=optimizer2, loss="mse") # Compute action gradients (derivative of Q values w.r.t. to actions) action_gradients = K.gradients(Q1_values, actions) # Define an additional function to fetch action gradients (to be used by actor model) self.get_action_gradients = K.function( inputs=[*self.model1.input, K.learning_phase()], outputs=action_gradients)
def make_model_128_with_regularizer(): input_img = Input(shape=(128, 128, 3)) with K.name_scope('Encoder'): with K.name_scope('Convolution_layer_1'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) #x=layers.Dropout(0.3)(x) #64 with K.name_scope('Convolution_layer_2'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) #x=layers.Dropout(0.3)(x) #32 with K.name_scope('Convolution_layer_3'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) #x=layers.Dropout(0.3)(x) #16 with K.name_scope('Convolution_layer_4'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same', name='encoded_output')(x) #x=layers.Dropout(0.3)(x) with K.name_scope('FullyConnectedLayer'): x = layers.Flatten()(x) x = layers.Dense(8 * 8 * 16, activity_regularizer=binary_regularizer)(x) x1 = layers.Reshape((8, 8, 16))(x) with K.name_scope('Decoder'): with K.name_scope('DeConvolution_layer_1'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) #x1=layers.Dropout(0.3)(x1) #16 with K.name_scope('DeConvolution_layer_2'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) #x1=layers.Dropout(0.3)(x1) #3 with K.name_scope('DeConvolution_layer_3'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) #x1=layers.Dropout(0.3)(x1) #32 with K.name_scope('DeConvolution_layer_4_with_output'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) decoder_output = layers.Conv2D(3, (3, 3), activation='relu', padding='same', name='final_output')(x1) #128 final_model = Model(input_img, decoder_output) final_model.compile(loss='mean_squared_error', optimizer='adadelta', metrics=['acc']) return final_model
# As the decoder RNN's input, repeatedly provide with the last output of # RNN for each time step. Repeat 'DIGITS + 1' times as that's the maximum # length of output, e.g., when DIGITS=3, max output is 999+999=1998. model.add(layers.RepeatVector(DIGITS + 1)) # The decoder RNN could be multiple layers stacked or a single layer. for _ in range(LAYERS): # By setting return_sequences to True, return not only the last output but # all the outputs so far in the form of (num_samples, timesteps, # output_dim). This is necessary as TimeDistributed in the below expects # the first dimension to be the timesteps. model.add(RNN(HIDDEN_SIZE, return_sequences=True)) # Apply a dense layer to the every temporal slice of an input. For each of step # of the output sequence, decide which character should be chosen. model.add( layers.TimeDistributed(layers.Dense(len(chars), activation='softmax'))) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() # Store val_loss and accuracy for visulization and early stopping train_loss = [] val_loss = [] train_acc = [] val_acc = [] patience = 3 min_delta = 0.001 val_loss_increase = 0 # Train the model each generation and show predictions against the validation
sentences = tokenizer.texts_to_matrix(sentences) input_dim = len(tokenizer.word_index) + 1 le = preprocessing.LabelEncoder() y = le.fit_transform(y) X_train, X_test, y_train, y_test = train_test_split(sentences, y, test_size=0.25, random_state=1000) # Number of features input_dim = len(sentences[0]) # print(input_dim) model = Sequential() model.add(layers.Dense(300, input_dim=input_dim, activation='relu')) #Changed layer to 10 and activation to softmax model.add(layers.Dense(10, activation='softmax')) model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['acc']) history = model.fit(X_train, y_train, epochs=5, verbose=True, validation_data=(X_test, y_test), batch_size=256)
# Use a custom loss function def auc(y_true, y_pred): auc = tf.metrics.auc(y_true, y_pred)[1] keras.backend.get_session().run(tf.local_variables_initializer()) return auc # Load the sample data set and split into x and y data frames url_file = "https://github.com/bgweber/Twitch/raw/master/Recommendations/games-expand.csv" df = pd.read_csv(url_file) x = df.drop(['label'], axis=1) y = df['label'] # Define the keras model model = models.Sequential() model.add(layers.Dense(64, activation='relu', input_shape=(10, ))) model.add(layers.Dropout(0.1)) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dropout(0.1)) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) # Compile and fit the model model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=[auc]) history = model.fit(x, y, epochs=100, batch_size=100, validation_split=.2, verbose=0)
resnet_conv = ResNet50(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3)) for layer in resnet_conv.layers[:-4]: layer.trainable = False for layer in resnet_conv.layers: print(layer, layer.trainable) from keras import models from keras import layers from keras import optimizers model = models.Sequential() # Add the vgg convolutional base model model.add(resnet_conv) # Add new layers model.add(layers.Flatten()) model.add(layers.Dense(1024, activation='relu')) model.add(layers.Dropout(0.5)) model.add(layers.Dense(2, activation='softmax')) model.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc']) model.summary() early_stopping = EarlyStopping(monitor='val_loss', patience=5, verbose=0, mode='auto') EPOCHS = 20 BATCH_SIZE = 256 history1 = model.fit(X_train, Y_train,
batch_maes.append(mae) print(np.mean(batch_maes)) evaluate_naive_method() #converting MAE to Celsius Error celsius_mae = 0.29 * std[1] # Model from keras.models import Sequential from keras import layers from keras.optimizers import RMSprop model = Sequential() model.add(layers.Flatten(input_shape=(lookback // step, float_data.shape[-1]))) model.add(layers.Dense(32, activation='relu')) model.add(layers.Dense(1)) model.compile(optimizer=RMSprop(), loss='mae') history = model.fit_generator(train_gen, steps_per_epoch=500, epochs=20, validation_data=val_gen, validation_steps=val_steps) #Plotting import matplotlib.pyplot as plt loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(1, len(loss) + 1) plt.figure() plt.plot(epochs, loss, 'bo', label='Training loss')
batch_size=batch_size) val_steps = (300000 - 200001 - lookback) // batch_size test_steps = (len(float_data) - 300001 - lookback) // batch_size model = Sequential() model.add( layers.Conv1D(32, 5, activation='relu', input_shape=(None, float_data.shape[-1]))) model.add(layers.MaxPooling1D(3)) model.add(layers.Conv1D(32, 5, activation='relu')) model.add(layers.MaxPooling1D(3)) model.add(layers.Conv1D(32, 5, activation='relu')) model.add(layers.GlobalMaxPooling1D()) model.add(layers.Dense(1)) model.compile(optimizer=RMSprop(), loss='mae') history = model.fit_generator(train_gen, steps_per_epoch=500, epochs=40, validation_data=val_gen, validation_steps=val_steps) loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(1, len(loss) + 1) plt.figure() plt.plot(epochs, loss, 'bo', label='Training loss') plt.plot(epochs, val_loss, 'b', label='Validation loss') plt.title('Training and validation loss') plt.legend()
from keras import models from keras import layers from keras import optimizers from keras import callbacks def get_callbacks(file_name, patience=5): es = callbacks.EarlyStopping('val_loss', patience=patience, mode="min") msave = callbacks.ModelCheckpoint(file_name, save_best_only=True) return [es, msave] model_callbacks = get_callbacks('transfer_learning_1.h5', patience=5) model = models.Sequential() model.add(layers.Dense(256, activation='relu', input_dim=4 * 4 * 512)) model.add(layers.Dropout(0.5)) model.add(layers.Dense(num_classes, activation='softmax')) model.compile(optimizer=optimizers.RMSprop(lr=2e-5), loss='categorical_crossentropy', metrics=['acc']) history = model.fit(train_features, train_labels, epochs=200, batch_size=20, validation_data=(validation_features, validation_labels), callbacks=model_callbacks) #Plot