kernel_size=(3, 3), activation='relu', name='block4_conv2')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = Conv2D(filters=512, kernel_size=(3, 3), activation='relu', name='block4_conv3')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = MaxPool2D(pool_size=(3, 3), strides=(1, 1))(h) # block5 (start to use Atrous convolution) h = ZeroPadding2D(padding=(2, 2))(h) # padding <-> atrous_rate h = AtrousConv2D(filters=512, atrous_rate=(2, 2), kernel_size=(3, 3), activation='relu', name='block5_conv1')(h) h = ZeroPadding2D(padding=(2, 2))(h) h = AtrousConv2D(filters=512, atrous_rate=(2, 2), kernel_size=(3, 3), activation='relu', name='block5_conv2')(h) h = ZeroPadding2D(padding=(2, 2))(h) h = AtrousConv2D(filters=512, atrous_rate=(2, 2), kernel_size=(3, 3), activation='relu', name='block5_conv3')(h) h = ZeroPadding2D(padding=(1, 1))(h)
if args.model == "conv": model = Sequential() model.add(Conv2D(8, kernel_size=(3, 3), activation="relu", input_shape=input_shape)) model.add(Conv2D(16, (3, 3), activation="relu")) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(32, activation="relu")) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation="softmax")) elif args.model == "dilated_conv": model = Sequential() model.add(AtrousConv2D(8, kernel_size=(3, 3), atrous_rate=(2, 2), activation="relu", input_shape=input_shape)) # shape is 5x5 model.add(AtrousConv2D(16, kernel_size=(3, 3), atrous_rate=(3, 3), activation="relu")) # shape is 7x7 model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(32, activation="relu")) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation="softmax")) elif args.model == "fc": model = Sequential() model.add(Dense(512, activation="hard_sigmoid", input_shape=(784,))) model.add(Dropout(0.2)) model.add(Dense(512, activation="relu")) model.add(Dropout(0.2))
def _setup_model(model_type): from keras import backend as K from keras.layers import Dense, Dropout, Flatten, Conv2D, AtrousConv2D, MaxPooling2D, Input, add, GlobalAveragePooling2D, Activation from keras.models import Sequential, Model input_shape = get_input_shape(model_type) if model_type == "conv": model = Sequential() model.add( Conv2D(8, kernel_size=(3, 3), activation="relu", input_shape=input_shape)) model.add(Conv2D(16, (3, 3), activation="relu")) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(32, activation="relu")) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation="softmax")) elif model_type == "dilated_conv": model = Sequential() model.add( AtrousConv2D(8, kernel_size=(3, 3), atrous_rate=(2, 2), activation="relu", input_shape=input_shape)) # shape is 5x5 model.add( AtrousConv2D(16, kernel_size=(3, 3), atrous_rate=(3, 3), activation="relu")) # shape is 7x7 model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(32, activation="relu")) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation="softmax")) elif model_type == "fc": model = Sequential() model.add(Dense(512, activation="hard_sigmoid", input_shape=(784, ))) model.add(Dropout(0.2)) model.add(Dense(512, activation="relu")) model.add(Dropout(0.2)) model.add(Dense(10, activation="softmax")) elif model_type == "residual": nn_input = Input(shape=(28, 28, 1)) hidden = Conv2D(8, kernel_size=(3, 3), activation="relu")(nn_input) hidden = MaxPooling2D(pool_size=(2, 2))(hidden) hidden_1 = Conv2D(16, kernel_size=(1, 1), activation="relu", padding="same")(hidden) hidden_2 = Conv2D(16, kernel_size=(3, 3), activation="relu", padding="same")(hidden) hidden = add([hidden_1, hidden_2]) hidden_1 = hidden hidden_2 = Conv2D(16, kernel_size=(3, 3), activation="relu", padding="same")(hidden) hidden = add([hidden_1, hidden_2]) hidden = GlobalAveragePooling2D()(hidden) nn_output = Dense(num_classes, activation="softmax")(hidden) model = Model(inputs=[nn_input], outputs=[nn_output]) elif model_type == "complex": # graph which has graph and sequential # this is for testing converting complex model nn_input = Input(shape=(28, 28, 1)) hidden_1 = Conv2D(8, kernel_size=(3, 3), activation="relu")(nn_input) submodel_input = Input(shape=(26, 26, 8)) submodel_conv = Conv2D(8, kernel_size=(3, 3), activation="relu") submodel_1 = submodel_conv(submodel_input) submodel_2 = submodel_conv(submodel_1) # use same layer multiple times submodel_3 = Conv2D(16, kernel_size=(3, 3), activation="relu")(submodel_1) submodel = Model(inputs=[submodel_input], outputs=[submodel_3, submodel_2]) subseq = Sequential() subseq.add( Conv2D(16, kernel_size=(3, 3), activation="relu", input_shape=(22, 22, 16))) subseq.add(Flatten()) subseq.add(Dense(10)) hidden_2, hidden_3 = submodel(hidden_1) hidden_4 = subseq(hidden_2) hidden_5 = Flatten()(hidden_3) hidden_6 = Dense(10)(hidden_5) hidden_sum = add([hidden_4, hidden_6]) nn_output = Activation(activation="softmax")(hidden_sum) model = Model(inputs=[nn_input], outputs=[nn_output]) else: raise NotImplementedError("Unknown model type") print(f"input shape: {input_shape}, data_format: {K.image_data_format()}") return model
def VGG19Dense(weights='imagenet', input_tensor=None): '''Instantiate the VGG19 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 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. ''' if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') # Determine proper input shape if K.image_dim_ordering() == 'th': input_shape = (3, None, None) else: input_shape = (None, None, 3) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor) else: img_input = input_tensor # Block 1 x = AtrousConv2D(64, 3, 3, atrous_rate=(1,1), activation='relu', border_mode='same', name='block1_conv1')(img_input) x = AtrousConv2D(64, 3, 3, atrous_rate=(1,1), activation='relu', border_mode='same', name='block1_conv2')(x) #x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = AtrousConv2D(128, 3, 3, atrous_rate=(2,2), activation='relu', border_mode='same', name='block2_conv1')(x) x = AtrousConv2D(128, 3, 3, atrous_rate=(2,2), activation='relu', border_mode='same', name='block2_conv2')(x) #x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = AtrousConv2D(256, 3, 3, atrous_rate=(4,4), activation='relu', border_mode='same', name='block3_conv1')(x) x = AtrousConv2D(256, 3, 3, atrous_rate=(4,4), activation='relu', border_mode='same', name='block3_conv2')(x) x = AtrousConv2D(256, 3, 3, atrous_rate=(4,4), activation='relu', border_mode='same', name='block3_conv3')(x) x = AtrousConv2D(256, 3, 3, atrous_rate=(4,4), activation='relu', border_mode='same', name='block3_conv4')(x) #x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = AtrousConv2D(512, 3, 3, atrous_rate=(8,8), activation='relu', border_mode='same', name='block4_conv1')(x) x = AtrousConv2D(512, 3, 3, atrous_rate=(8,8), activation='relu', border_mode='same', name='block4_conv2')(x) x = AtrousConv2D(512, 3, 3, atrous_rate=(8,8), activation='relu', border_mode='same', name='block4_conv3')(x) x = AtrousConv2D(512, 3, 3, atrous_rate=(8,8), activation='relu', border_mode='same', name='block4_conv4')(x) #x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = AtrousConv2D(512, 3, 3, atrous_rate=(16,16), activation='relu', border_mode='same', name='block5_conv1')(x) x = AtrousConv2D(512, 3, 3, atrous_rate=(16,16), activation='relu', border_mode='same', name='block5_conv2')(x) x = AtrousConv2D(512, 3, 3, atrous_rate=(16,16), activation='relu', border_mode='same', name='block5_conv3')(x) x = AtrousConv2D(512, 3, 3, atrous_rate=(16,16), activation='relu', border_mode='same', name='block5_conv4')(x) #x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) # Create model model = Model(img_input, x) # load weights if weights == 'imagenet': print('K.image_dim_ordering:', K.image_dim_ordering()) if K.image_dim_ordering() == 'th': weights_path = get_file('vgg19_weights_th_dim_ordering_th_kernels_notop.h5', TH_WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights_path) if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image dimension ordering convention ' '(`image_dim_ordering="th"`). ' 'For best performance, set ' '`image_dim_ordering="tf"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') convert_all_kernels_in_model(model) else: weights_path = get_file('vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5', TF_WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights_path) if K.backend() == 'theano': convert_all_kernels_in_model(model) return model