def cnn_model(logits=False, input_ph=None, img_rows=28, img_cols=28, channels=1, nb_filters=64, nb_classes=10): """ Defines a CNN model using Keras sequential model :param logits: If set to False, returns a Keras model, otherwise will also return logits tensor :param input_ph: The TensorFlow tensor for the input (needed if returning logits) ("ph" stands for placeholder but it need not actually be a placeholder) :param img_rows: number of row in the image :param img_cols: number of columns in the image :param channels: number of color channels (e.g., 1 for MNIST) :param nb_filters: number of convolutional filters per layer :param nb_classes: the number of output classes :return: """ model = Sequential() # Define the layers successively (convolution layers are version dependent) if keras.backend.image_dim_ordering() == 'th': input_shape = (channels, img_rows, img_cols) else: input_shape = (img_rows, img_cols, channels) layers = [conv_2d(nb_filters, (5, 5), (1, 1), "same", input_shape=input_shape), Activation('relu'), conv_2d(nb_filters, (5, 5), (1, 1), "valid"), Activation('relu'), Flatten(), Dropout(0.25), Dense(128), Activation('relu'), Dropout(0.5), Dense(nb_classes)] for layer in layers: model.add(layer) if logits: logits_tensor = model(input_ph) model.add(Activation('softmax')) if logits: return model, logits_tensor else: return model
def cleverhans_model(input_shape, nb_filters, nb_classes, logits, input_range_type, pre_filter): """ Defines a CNN model using Keras sequential model :params logits: return logits(input of softmax layer) if True; return softmax output otherwise. :params input_range_type: the expected input range, {1: [0,1], 2:[-0.5, 0.5], 3:[-1, 1]...} :return: """ model = Sequential() if input_range_type == 1: # The input data range is [0, 1]. # Don't need to do scaling for cleverhans models, as it is the input range by default. scaler = lambda x: x elif input_range_type == 2: # The input data range is [-0.5, 0.5]. Convert to [0,1] by adding 0.5 element-wise. scaler = lambda x: x + 0.5 elif input_range_type == 3: # The input data range is [-1, 1]. Convert to [0,1] by x/2+0.5. scaler = lambda x: x / 2 + 0.5 layers = [Lambda(scaler, input_shape=input_shape)] layers += [Lambda(pre_filter, output_shape=input_shape)] layers += [ Dropout(0.2), conv_2d(nb_filters, (8, 8), (2, 2), "same"), Activation('relu'), conv_2d((nb_filters * 2), (6, 6), (2, 2), "valid"), Activation('relu'), conv_2d((nb_filters * 2), (5, 5), (1, 1), "valid"), Activation('relu'), Dropout(0.5), Flatten(), Dense(nb_classes) ] for layer in layers: model.add(layer) if not logits: model.add(Activation('softmax')) return model
def conv_2d(*args, **kwargs): from cleverhans.utils_keras import conv_2d warnings.warn("utils.conv_2d is deprecated and may be removed on or after" " 2018-01-05. Switch to utils_keras.conv_2d.") return conv_2d(*args, **kwargs)