#1. evrişim katmanı
model.add(Conv2D(64, (5, 5), activation='relu', input_shape=(48,48,1)))
model.add(MaxPooling2D(pool_size=(5,5), strides=(2, 2)))

#2. Evrişim katmanı
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(AveragePooling2D(pool_size=(3,3), strides=(2, 2)))

#3. Evrişim katmanı
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(AveragePooling2D(pool_size=(3,3), strides=(2, 2)))

model.add(Flatten())

# Tam bağlantı katmanı
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.2))

model.add(Dense(num_classes, activation='softmax'))
#------------------------------
#Batch (Küme) işlemleri
gen = ImageDataGenerator()
train_generator = gen.flow(x_train, y_train, batch_size=batch_size)

#------------------------------
def PointNet(classes=40, load_weights=True, num_points =2048):
	num_points = num_points
	input_points = Input(shape=(num_points, 3))
	x = Convolution1D(64, 1, activation='relu',
		          input_shape=(num_points, 3))(input_points)
	x = BatchNormalization()(x)
	x = Convolution1D(128, 1, activation='relu')(x)
	x = BatchNormalization()(x)
	x = Convolution1D(1024, 1, activation='relu')(x)
	x = BatchNormalization()(x)
	x = MaxPooling1D(pool_size=num_points)(x)
	x = Dense(512, activation='relu')(x)
	x = BatchNormalization()(x)
	x = Dense(256, activation='relu')(x)
	x = BatchNormalization()(x)
	x = Dense(9, weights=[np.zeros([256, 9]), np.array([1, 0, 0, 0, 1, 0, 0, 0, 1]).astype(np.float32)])(x)
	input_T = Reshape((3, 3))(x)

	# forward net
	g = Lambda(mat_mul, arguments={'B': input_T})(input_points)
	g = Convolution1D(64, 1, input_shape=(num_points, 3), activation='relu')(g)
	g = BatchNormalization()(g)
	g = Convolution1D(64, 1, input_shape=(num_points, 3), activation='relu')(g)
	g = BatchNormalization()(g)

	# feature transform net
	f = Convolution1D(64, 1, activation='relu')(g)
	f = BatchNormalization()(f)
	f = Convolution1D(128, 1, activation='relu')(f)
	f = BatchNormalization()(f)
	f = Convolution1D(1024, 1, activation='relu')(f)
	f = BatchNormalization()(f)
	f = MaxPooling1D(pool_size=num_points)(f)
	f = Dense(512, activation='relu')(f)
	f = BatchNormalization()(f)
	f = Dense(256, activation='relu')(f)
	f = BatchNormalization()(f)
	f = Dense(64 * 64, weights=[np.zeros([256, 64 * 64]), np.eye(64).flatten().astype(np.float32)])(f)
	feature_T = Reshape((64, 64))(f)

	# forward net
	g = Lambda(mat_mul, arguments={'B': feature_T})(g)
	g = Convolution1D(64, 1, activation='relu')(g)
	g = BatchNormalization()(g)
	g = Convolution1D(128, 1, activation='relu')(g)
	g = BatchNormalization()(g)
	g = Convolution1D(1024, 1, activation='relu')(g)
	g = BatchNormalization()(g)

	# global_feature
	global_feature = MaxPooling1D(pool_size=num_points)(g)

	# point_net_cls
	c = Dense(512, activation='relu')(global_feature)
	c = BatchNormalization()(c)
	c = Dropout(rate=0.7)(c)
	c = Dense(256, activation='relu')(c)
	c = BatchNormalization()(c)
	c = Dropout(rate=0.7)(c)
	c = Dense(classes, activation='softmax')(c)
	prediction = Flatten()(c)
	
	model = Model(inputs=input_points, outputs=prediction)
	

	model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

	if(classes == 40 and load_weights):
    		model.load_weights('Models/PointNet-ModelNet40.h5')
	if(classes == 10 and load_weights):
    		model.load_weights('Models/PointNet-ModelNet10.h5')
	if(classes == 2 and load_weights):
    		model.load_weights('Models/PointNet-KITTI.h5')
	if(classes == 3 and load_weights):
    		model.load_weights('Models/PointNet-KITTI3.h5')
	print model.summary()

	return model
예제 #3
0
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 50
batch_size = 16

# build the VGG16 network
base_model = applications.VGG16(weights='imagenet',
                                include_top=False,
                                input_shape=(150, 150, 3))
print('Model loaded.')

# build a classifier model to put on top of the convolutional model
top_model = Sequential()
top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))

# note that it is necessary to start with a fully-trained
# classifier, including the top classifier,
# in order to successfully do fine-tuning
top_model.load_weights(top_model_weights_path)

# add the model on top of the convolutional base
# model.add(top_model)
model = Model(inputs=base_model.input, outputs=top_model(base_model.output))

# set the first 25 layers (up to the last conv block)
# to non-trainable (weights will not be updated)
예제 #4
0
def run_cnn(deep_data_processor, model_type):
    x_train, x_test, y_train, y_test, vocabulary, vocabulary_inv = deep_data_processor.output

    #w2v_type = "trained" # trained | pre-trained
    # Model Hyperparameters
    embedding_dim = 300  # 50 | 300
    filter_sizes = (3, 4, 5)
    num_filters = 10
    dropout_prob = (0.5, 0.8)
    hidden_dims = 100

    # Training parameters
    batch_size = 64
    num_epochs = 4

    # Prepossessing parameters
    sequence_length = 400

    # Word2Vec parameters (see train_word2vec)
    min_word_count = 1
    context = 10

    x_train = sequence.pad_sequences(x_train,
                                     maxlen=sequence_length,
                                     padding="post",
                                     truncating="post")
    x_test = sequence.pad_sequences(x_test,
                                    maxlen=sequence_length,
                                    padding="post",
                                    truncating="post")

    # Prepare embedding layer weights and convert inputs for static model
    print("Model type is", model_type)
    if model_type == "non-static":
        #if w2v_type == "trained":
        embedding_weights = train_word2vec(np.vstack((x_train, x_test)),
                                           vocabulary_inv,
                                           num_features=embedding_dim,
                                           min_word_count=min_word_count,
                                           context=context)
        # elif w2v_type == "pre-trained":
        #     embedding_weights = load_google_trained_w2v(vocabulary_inv, embedding_dim)

    elif model_type == "rand":
        embedding_weights = None
    else:
        raise ValueError("Unknown model type")

    # Build model
    input_shape = (sequence_length, )
    model_input = Input(shape=input_shape)

    # Static model do not have embedding layer
    z = Embedding(len(vocabulary_inv),
                  embedding_dim,
                  input_length=sequence_length,
                  name="embedding")(model_input)
    z = Dropout(dropout_prob[0])(z)

    # Convolutional block
    conv_blocks = []
    for sz in filter_sizes:
        conv = Convolution1D(filters=num_filters,
                             kernel_size=sz,
                             padding="valid",
                             activation="relu",
                             strides=1)(z)
        conv = MaxPooling1D(pool_size=2)(conv)
        conv = Flatten()(conv)
        conv_blocks.append(conv)
    z = Concatenate()(conv_blocks) if len(conv_blocks) > 1 else conv_blocks[0]

    z = Dropout(dropout_prob[1])(z)
    z = Dense(hidden_dims, activation="relu")(z)
    model_output = Dense(1, activation="sigmoid")(z)

    model = Model(model_input, model_output)
    model.compile(loss="binary_crossentropy",
                  optimizer="adam",
                  metrics=["accuracy"])

    # Initialize weights with word2vec
    if model_type == "non-static":
        embedding_layer = model.get_layer("embedding")
        embedding_layer.set_weights(embedding_weights)

    # Train the model
    model.fit(x_train,
              y_train,
              batch_size=batch_size,
              epochs=num_epochs,
              validation_data=(x_test, y_test))

    scores = model.evaluate(x_test, y_test, verbose=0)

    print("Accuracy: %.2f%%" % (scores[1] * 100))
def train_on_VeRi(model, out_name, fix_layer =-1, xml_file = 'train_label.xml', image_folder = 'image_train',ismodelfile = True):
    nb_epoch = 70
    num_classes = 776
    batch_size = 32
    labels = []
    train_names = []
    xmlp = ET.XMLParser(encoding="utf-8")

    # train label file - xml
    f = ET.parse(xml_file, parser=xmlp)
    root = f.getroot()
    for child in root.iter('Item'):
        labels.append(child.attrib['vehicleID'])
        train_names.append(os.path.join(image_folder, child.attrib['imageName']))

    labels = utils.to_categorical(labels, num_classes=776)
    X_train = []

    for name in train_names:
        x = load_img(name, target_size=(img_rows, img_cols))
        x = img_to_array(x)
        X_train.append(x)

    X_train = np.array(X_train, ndmin=4)

    train_datagen = ImageDataGenerator(
        rescale=1. / 255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

    train_generator = train_datagen.flow(
        x=X_train, y=labels,
        batch_size=batch_size)

    if ismodelfile:
        base_model = load_model(model)
    else:
        base_model = model

    x = base_model.get_layer(name='block5_pool').output
    x = Flatten()(x)
    x = Dense(4096, activation='relu', name='fc1')(x)
    x = Dense(4096, activation='relu', name='fc2')(x)
    x = Dropout(0.5)(x)
    predictions = Dense(num_classes, activation='softmax')(x)
    model = Model(input=base_model.input, output=predictions)

    if fix_layer > -1:
        for layer in model.layers[:fix_layer]:
            layer.trainable = False
    sgd = SGD(lr=1e-3)
    model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])

    model.fit_generator(
        train_generator,
        epochs=nb_epoch,
        steps_per_epoch=1184,
    )
    model.save(out_name)
    return model
예제 #6
0


# Step 2 - Perform Max Pooling and add layers

# Pooling reduces the size of the feature map
# by applying matrix which picks only maximum element
# in the original feature map
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Adding a second convolutional layer and pooled layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Step 3 - Flattening - converts pooled and convolved features into one bit vector
classifier.add(Flatten())



# Step 4 - Full connection - responsible for full connection through layers

# Fully connected layer is hidden layer same as in ANN
# Esentially this part is to construct ANN

classifier.add(Dense(units = 128, activation = 'relu'))

# Output layer
classifier.add(Dense(units = 1, activation = 'sigmoid'))
# Adding output layer completes the full connections

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
from keras.utils import np_utils

# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
img_width = X_train.shape[1]
img_height = X_train.shape[2]

X_train = X_train.astype('float32')
X_train /= 255.
X_test = X_test.astype('float32')
X_test /= 255.

# one hot encode outputs
y_train = np_utils.to_categorical(y_train)
num_classes = y_train.shape[1]

y_test = np_utils.to_categorical(y_test)

# create model
model = Sequential()
model.add(Flatten(input_shape=(img_width, img_height)))
model.add(Dropout(0.5))
model.add(Dense(30, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test))
예제 #9
0
Conv2d4= Conv2D(filters= 21, kernel_size=3, padding="same", activation="relu")(Conv2d3) 
drop2 = Dropout(0.3)(Conv2d4)

Conv2d5= Conv2D(filters= 21, kernel_size=3, padding="same", activation="relu")(drop2) 
drop3 = Dropout(0.3)(Conv2d5)
Conv2d6= Conv2D(filters= 12, kernel_size=3, padding="same", activation="relu")(drop3) 
drop4 = Dropout(0.3)(Conv2d6)

Conv2d7= Conv2D(filters= 18, kernel_size=3, padding="same", activation="relu")(drop4) 

drop5 = Dropout(0.1)(Conv2d7)

Conv2d8= Conv2D(filters= 12, kernel_size=3, padding="same", activation="relu")(drop5) 
drop6 = Dropout(0.1)(Conv2d8)

output1 = (Flatten())(drop6)
output2 = Dense(10, activation = 'softmax')(output1)
model = Model(inputs=input1, outputs=output2)
model.summary()
print(x_train)
print(y_train)


#3. 훈련
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
model.fit(x_train, y_train, epochs = 50)


#acc75프로로 잡아라
#4. 예측
loss,acc = model.evaluate(x_test,y_test, batch_size=30)
예제 #10
0
def gen_model():
    m_in = Input(shape=x[0][0].shape)
    m_off = Lambda(offset_slice)(m_in)
    m_noise = GaussianNoise(np.std(x[0][0] / 100))(m_off) # how much noice to have????

    m_t = Conv1D(22, 20, padding='causal')(m_noise)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.3)(m_t)

    m_t = Conv1D(15, 20, padding='causal')(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.5)(m_t)

    m_t = Conv1D(10, 12, padding='causal')(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.6)(m_t)

    m_t = Flatten()(m_t)
    m_t = Dense(35)(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = Activation('tanh')(m_t)
    m_t = Dense(15)(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = Activation('tanh')(m_t)
    m_out1 = Dense(1)(m_t)


    m_t = Conv1D(22, 20, padding='causal')(m_noise)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.3)(m_t)

    m_t = Conv1D(15, 20, padding='causal')(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.5)(m_t)

    m_t = Conv1D(10, 12, padding='causal')(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.6)(m_t)

    m_t = Flatten()(m_t)
    m_t = Dense(35)(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = Activation('tanh')(m_t)
    m_t = Dense(15)(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = Activation('tanh')(m_t)
    m_out2 = Dense(1)(m_t)


    m_t = Conv1D(22, 20, padding='causal')(m_noise)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.3)(m_t)

    m_t = Conv1D(15, 20, padding='causal')(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.5)(m_t)

    m_t = Conv1D(10, 12, padding='causal')(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.6)(m_t)

    m_t = Flatten()(m_t)
    m_t = Dense(35)(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = Activation('tanh')(m_t)
    m_t = Dense(15)(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = Activation('tanh')(m_t)
    m_out3 = Dense(1)(m_t)

    m_conc = concatenate([m_out1, m_out2, m_out3])
    m_out = Activation('softmax')(m_conc)

    model = Model(inputs=m_in, outputs=m_out)

    model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
train_path = 'C:/Users/hp/Documents/Machine Learning Projects/Pneumonia classification/chest_xray/train'
test_path = 'C:/Users/hp/Documents/Machine Learning Projects/Pneumonia classification/chest_xray/test'

vgg = VGG16(input_shape=Image_Size + [3],
            weights='imagenet',
            include_top=False)

for layer in vgg.layers:
    layer.trainable = False

folders = glob(
    'C:/Users/hp/Documents/Machine Learning Projects/Pneumonia classification/chest_xray/train/*'
)

x = Flatten()(vgg.output)

prediction = Dense(len(folders), activation='softmax')(x)

model = Model(inputs=vgg.input, outputs=prediction)
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

train_datagen = ImageDataGenerator(rescale=1. / 255,
                                   shear_range=0.2,
                                   zoom_range=0.2,
                                   horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1. / 255)
def build_model(max_length: int,
                embedding_matrix: Union[np.ndarray, Tuple[int, int]],
                filters: List[int],
                kernel_size: List[int],
                pool_size: List[int],
                conv_padding: str,
                pool_padding: str,
                cell_type: str,
                cell_size: int,
                cell_stack_size: int,
                dense_size: List[int],
                embedding_dropout: float = 0.6,
                embedding_not_trainable: bool = False,
                conv_dropout: float = 0.1,
                dense_dropout: Union[float, List[float]] = 0.3,
                classifier_dropout: float = 0.1,
                rnn_first: bool = False) -> Model:

    if not (len(filters) > 0 and len(kernel_size) > 0 and len(pool_size) > 0):
        logger.error(
            "There are no filters, kernel sizes or pool sizes specified for the CNN."
        )
        raise ValueError(
            "There are no filters, kernel sizes or pool sizes specified for the CNN."
        )

    if type(dense_dropout) != list:
        dense_dropout = [dense_dropout]

    if len(dense_size) > 0 and len(dense_size) != len(dense_dropout):
        max_list_length = max([len(dense_size), len(dense_dropout)])
        new_dense_size = []
        new_dense_dropout = []
        for i in range(max_list_length):
            new_dense_size.append(
                dense_size[i] if i < len(dense_size) else dense_size[-1])
            new_dense_dropout.append(dense_dropout[i] if i < len(dense_dropout)
                                     else dense_dropout[-1])
        dense_size = new_dense_size
        dense_dropout = new_dense_dropout
        logger.warning(
            "Lists given for dense layer sizes and dense layer dropout rates are not the same length. "
            "The shorter lists are padded using the last value to match the length of the longest."
        )

    if len(filters) != len(kernel_size) or len(filters) != len(
            pool_size) or len(kernel_size) != len(pool_size):
        max_list_length = max([len(filters), len(kernel_size), len(pool_size)])
        new_filters = []
        new_kernel_size = []
        new_pool_size = []
        for i in range(max_list_length):
            new_filters.append(filters[i] if i < len(filters) else filters[-1])
            new_kernel_size.append(
                kernel_size[i] if i < len(kernel_size) else kernel_size[-1])
            new_pool_size.append(
                pool_size[i] if i < len(pool_size) else pool_size[-1])
        filters = new_filters
        kernel_size = new_kernel_size
        pool_size = new_pool_size
        logger.warning(
            "Lists given for convolutional filters, kernel sizes and pooling sizes had different lengths. "
            "The shorter lists are padded using the last value to match the length of the longest."
        )

    cell_type_name = cell_type.lower()
    if cell_type_name == "lstm":
        cell_type = LSTM
    elif cell_type_name == "gru":
        cell_type = GRU

    # input 1: word indices, input 2: handcrafted_features
    raw_input = Input(shape=(max_length, ), name="word_input")

    # embedding layer
    embedding_layer = Embedding(
        input_dim=(embedding_matrix[0] if type(embedding_matrix) == tuple else
                   embedding_matrix.shape[0]),
        output_dim=(embedding_matrix[1] if type(embedding_matrix) == tuple else
                    embedding_matrix.shape[1]),
        input_length=max_length,
        name="word_embedding",
        weights=(None
                 if type(embedding_matrix) == tuple else [embedding_matrix]),
        trainable=(not embedding_not_trainable))
    embedding_dropout = Dropout(embedding_dropout, name="embedding_dropout")

    # convolutional layer(s)
    conv_layers = []
    for i in range(len(filters)):
        conv_layer_name = "conv_{}".format(i)
        convolution = Conv1D(filters[i],
                             kernel_size[i],
                             padding=conv_padding,
                             activation="relu",
                             name=conv_layer_name)
        pooling = MaxPooling1D(pool_size[i],
                               padding=pool_padding,
                               name="max_pool_{}".format(i))
        conv_layers.append((convolution, pooling))
    conv_dropout = Dropout(conv_dropout, name="conv_dropout")

    # bidirectional RNN
    rnn_cells = []
    for i in range(cell_stack_size - 1):
        cell_name = "{}_{}".format(cell_type_name, i)
        cell = Bidirectional(cell_type(cell_size,
                                       return_sequences=True,
                                       name=cell_name),
                             name="bidirectional_{}".format(cell_name))
        rnn_cells.append(cell)

    # last cell
    cell_name = "{}_{}".format(cell_type_name, cell_stack_size - 1)
    cell = Bidirectional(
        cell_type(cell_size, return_sequences=rnn_first, name=cell_name))
    rnn_cells.append(cell)

    # dense layer(s)
    dense_layers = []
    for i in range(len(dense_size)):
        dropout = Dropout(rate=dense_dropout[i],
                          name="dense_dropout_{}".format(i))
        dense_layer_name = "dense_{}".format(i)
        dense = Dense(dense_size[i], name=dense_layer_name)
        dense_layers.append((dropout, dense))

    # classification layer
    classifier_dropout = Dropout(classifier_dropout, name="classifier_dropout")
    classifier = Dense(1, name="classifier")
    classifier_prediction = Activation("sigmoid", name="classifier_prediction")

    # build the actual model
    output = embedding_dropout(embedding_layer(raw_input))
    if rnn_first:
        for c in rnn_cells:
            output = c(output)
        for l in conv_layers:
            output = l[1](l[0](conv_dropout(output)))
        output = Flatten(name="flatten")(output)
    else:
        for l in conv_layers:
            output = conv_dropout(l[1](l[0](output)))
        for c in rnn_cells:
            output = c(output)
    for l in dense_layers:
        output = l[1](l[0](output))
    output = classifier_prediction(classifier(classifier_dropout(output)))
    model = Model(inputs=raw_input, outputs=output)

    return model
예제 #13
0
testdf['general_cat'], _, _ = \
            zip(*testdf['category_name'].apply(lambda x: split_cat(x)))
indexcatesub = testdf['general_cat']
del(testdf)



if True:
    input1 = Input(shape=(sub_featlen.shape[1],), dtype='float32')
    input2 = Input(shape=(1,), dtype='int32')
    embedding_layer0 = Embedding(index3,
                    30,init = RandomNormal(mean=0.0, stddev=0.005),
                        input_length=1,
                        trainable=True)
    xc3 = embedding_layer0(input2)
    xc3 = Flatten()(xc3)
    input3 = Input(shape=(FEAT_LENGTH-FIX_LEN,), dtype='int32')
    embedding_layer0 = Embedding(wordnum,
                            40,
                            init = RandomNormal(mean=0.0, stddev=0.005),
                            trainable=True)
    x30 = embedding_layer0(input3)
    x31 = Cropping1D(cropping=(0,40))(x30)
    la = []
    x3 = Conv1D(15,3,activation='sigmoid',padding = 'same',dilation_rate = 1)(x31)
    x3 = MaxPooling1D(FEAT_LENGTH-FIX_LEN)(x3)
    x3 = Flatten()(x3)
    la.append(x3)

    t=Conv1D(50,2,activation='sigmoid',padding = 'same',dilation_rate = 1)
    x3 = t(x31)
def main():
    options = parse_inputs()
    c = color_codes()

    # Prepare the net architecture parameters
    sequential = options['sequential']
    dfactor = options['dfactor']
    # Prepare the net hyperparameters
    num_classes = 5
    epochs = options['epochs']
    padding = options['padding']
    patch_width = options['patch_width']
    patch_size = (patch_width, patch_width, patch_width)
    batch_size = options['batch_size']
    dense_size = options['dense_size']
    conv_blocks = options['conv_blocks']
    n_filters = options['n_filters']
    filters_list = n_filters if len(n_filters) > 1 else n_filters*conv_blocks
    conv_width = options['conv_width']
    kernel_size_list = conv_width if isinstance(conv_width, list) else [conv_width]*conv_blocks
    balanced = options['balanced']
    recurrent = options['recurrent']
    # Data loading parameters
    preload = options['preload']
    queue = options['queue']

    # Prepare the sufix that will be added to the results for the net and images
    path = options['dir_name']
    filters_s = 'n'.join(['%d' % nf for nf in filters_list])
    conv_s = 'c'.join(['%d' % cs for cs in kernel_size_list])
    s_s = '.s' if sequential else '.f'
    ub_s = '.ub' if not balanced else ''
    params_s = (ub_s, dfactor, s_s, patch_width, conv_s, filters_s, dense_size, epochs, padding)
    sufix = '%s.D%d%s.p%d.c%s.n%s.d%d.e%d.pad_%s.' % params_s
    n_channels = np.count_nonzero([
        options['use_flair'],
        options['use_t2'],
        options['use_t1'],
        options['use_t1ce']]
    )

    print(c['c'] + '[' + strftime("%H:%M:%S") + '] ' + 'Starting cross-validation' + c['nc'])
    # N-fold cross validation main loop (we'll do 2 training iterations with testing for each patient)
    data_names, label_names = get_names_from_path(options)
    folds = options['folds']
    fold_generator = izip(nfold_cross_validation(data_names, label_names, n=folds, val_data=0.25), xrange(folds))
    dsc_results = list()
    for (train_data, train_labels, val_data, val_labels, test_data, test_labels), i in fold_generator:
        print(c['c'] + '[' + strftime("%H:%M:%S") + ']  ' + c['nc'] + 'Fold %d/%d: ' % (i+1, folds) + c['g'] +
              'Number of training/validation/testing images (%d=%d/%d=%d/%d)'
              % (len(train_data), len(train_labels), len(val_data), len(val_labels), len(test_data)) + c['nc'])
        # Prepare the data relevant to the leave-one-out (subtract the patient from the dataset and set the path)
        # Also, prepare the network
        net_name = os.path.join(path, 'baseline-brats2017.fold%d' % i + sufix + 'mdl')

        # First we check that we did not train for that patient, in order to save time
        try:
            # net_name_before =  os.path.join(path,'baseline-brats2017.fold0.D500.f.p13.c3c3c3c3c3.n32n32n32n32n32.d256.e1.pad_valid.mdl')
            net = keras.models.load_model(net_name)

            print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
            print 'load net successfully'
        except IOError:
            print '==============================================================='
            # NET definition using Keras
            train_centers = get_cnn_centers(train_data[:, 0], train_labels, balanced=balanced)
            val_centers = get_cnn_centers(val_data[:, 0], val_labels, balanced=balanced)
            train_samples = len(train_centers)/dfactor
            val_samples = len(val_centers) / dfactor
            print(c['c'] + '[' + strftime("%H:%M:%S") + ']    ' + c['g'] + 'Creating and compiling the model ' +
                  c['b'] + '(%d samples)' % train_samples + c['nc'])
            train_steps_per_epoch = -(-train_samples/batch_size)
            val_steps_per_epoch = -(-val_samples / batch_size)
            input_shape = (n_channels,) + patch_size
            if sequential:
                # Sequential model that merges all 4 images. This architecture is just a set of convolutional blocks
                # that end in a dense layer. This is supposed to be an original baseline.
                net = Sequential()
                net.add(Conv3D(
                    filters_list[0],
                    kernel_size=kernel_size_list[0],
                    input_shape=input_shape,
                    activation='relu',
                    data_format='channels_first'
                ))
                for filters, kernel_size in zip(filters_list[1:], kernel_size_list[1:]):
                    net.add(Dropout(0.5))
                    net.add(Conv3D(filters, kernel_size=kernel_size, activation='relu', data_format='channels_first'))
                net.add(Dropout(0.5))
                net.add(Flatten())
                net.add(Dense(dense_size, activation='relu'))
                net.add(Dropout(0.5))
                net.add(Dense(num_classes, activation='softmax'))
            else:
                # This architecture is based on the functional Keras API to introduce 3 output paths:
                # - Whole tumor segmentation
                # - Core segmentation (including whole tumor)
                # - Whole segmentation (tumor, core and enhancing parts)
                # The idea is to let the network work on the three parts to improve the multiclass segmentation.
                merged_inputs = Input(shape=(4,) + patch_size, name='merged_inputs')
                flair = Reshape((1,) + patch_size)(
                    Lambda(
                        lambda l: l[:, 0, :, :, :],
                        output_shape=(1,) + patch_size)(merged_inputs),
                )
                t2 = Reshape((1,) + patch_size)(
                    Lambda(lambda l: l[:, 1, :, :, :], output_shape=(1,) + patch_size)(merged_inputs)
                )
                t1 = Lambda(lambda l: l[:, 2:, :, :, :], output_shape=(2,) + patch_size)(merged_inputs)
                for filters, kernel_size in zip(filters_list, kernel_size_list):
                    flair = Conv3D(filters,
                                   kernel_size=kernel_size,
                                   activation='relu',
                                   data_format='channels_first'
                                   )(flair)
                    t2 = Conv3D(filters,
                                kernel_size=kernel_size,
                                activation='relu',
                                data_format='channels_first'
                                )(t2)
                    t1 = Conv3D(filters,
                                kernel_size=kernel_size,
                                activation='relu',
                                data_format='channels_first'
                                )(t1)
                    flair = Dropout(0.5)(flair)
                    t2 = Dropout(0.5)(t2)
                    t1 = Dropout(0.5)(t1)

                # We only apply the RCNN to the multioutput approach (we keep the simple one, simple)
                if recurrent:
                    flair = Conv3D(
                        dense_size,
                        kernel_size=(1, 1, 1),
                        activation='relu',
                        data_format='channels_first',
                        name='fcn_flair'
                    )(flair)
                    flair = Dropout(0.5)(flair)
                    t2 = concatenate([flair, t2], axis=1)
                    t2 = Conv3D(
                        dense_size,
                        kernel_size=(1, 1, 1),
                        activation='relu',
                        data_format='channels_first',
                        name='fcn_t2'
                    )(t2)
                    t2 = Dropout(0.5)(t2)
                    t1 = concatenate([t2, t1], axis=1)
                    t1 = Conv3D(
                        dense_size,
                        kernel_size=(1, 1, 1),
                        activation='relu',
                        data_format='channels_first',
                        name='fcn_t1'
                    )(t1)
                    t1 = Dropout(0.5)(t1)
                    flair = Dropout(0.5)(flair)
                    t2 = Dropout(0.5)(t2)
                    t1 = Dropout(0.5)(t1)
                    lstm_instance = LSTM(dense_size, implementation=1, name='rf_layer')
                    flair = lstm_instance(Permute((2, 1))(Reshape((dense_size, -1))(flair)))
                    t2 = lstm_instance(Permute((2, 1))(Reshape((dense_size, -1))(t2)))
                    t1 = lstm_instance(Permute((2, 1))(Reshape((dense_size, -1))(t1)))

                else:
                    flair = Flatten()(flair)
                    t2 = Flatten()(t2)
                    t1 = Flatten()(t1)
                    flair = Dense(dense_size, activation='relu')(flair)
                    flair = Dropout(0.5)(flair)
                    t2 = concatenate([flair, t2])
                    t2 = Dense(dense_size, activation='relu')(t2)
                    t2 = Dropout(0.5)(t2)
                    t1 = concatenate([t2, t1])
                    t1 = Dense(dense_size, activation='relu')(t1)
                    t1 = Dropout(0.5)(t1)

                tumor = Dense(2, activation='softmax', name='tumor')(flair)
                core = Dense(3, activation='softmax', name='core')(t2)
                enhancing = Dense(num_classes, activation='softmax', name='enhancing')(t1)

                net = Model(inputs=merged_inputs, outputs=[tumor, core, enhancing])


            # net_name_before =  os.path.join(path,'baseline-brats2017.fold0.D500.f.p13.c3c3c3c3c3.n32n32n32n32n32.d256.e1.pad_valid.mdl')
            # net = keras.models.load_model(net_name_before)
            net.compile(optimizer='adadelta', loss='categorical_crossentropy', metrics=['accuracy'])

            print(c['c'] + '[' + strftime("%H:%M:%S") + ']    ' +
                  c['g'] + 'Training the model with a generator for ' +
                  c['b'] + '(%d parameters)' % net.count_params() + c['nc'])
            print(net.summary())
       
            net.fit_generator(
                generator=load_patch_batch_train(
                    image_names=train_data,
                    label_names=train_labels,
                    centers=train_centers,
                    batch_size=batch_size,
                    size=patch_size,
                    # fc_shape = patch_size,
                    nlabels=num_classes,
                    dfactor=dfactor,
                    preload=preload,
                    split=not sequential,
                    datatype=np.float32
                ),
                validation_data=load_patch_batch_train(
                    image_names=val_data,
                    label_names=val_labels,
                    centers=val_centers,
                    batch_size=batch_size,
                    size=patch_size,
                    # fc_shape = patch_size,
                    nlabels=num_classes,
                    dfactor=dfactor,
                    preload=preload,
                    split=not sequential,
                    datatype=np.float32
                ),
                steps_per_epoch=train_steps_per_epoch,
                validation_steps=val_steps_per_epoch,
                max_q_size=queue,
                epochs=epochs
            )
            net.save(net_name)

        # Then we test the net.
        for p, gt_name in zip(test_data, test_labels):
            p_name = p[0].rsplit('/')[-2]
            patient_path = '/'.join(p[0].rsplit('/')[:-1])
            outputname = os.path.join(patient_path, 'deep-brats17' + sufix + 'test.nii.gz')
            gt_nii = load_nii(gt_name)
            gt = np.copy(gt_nii.get_data()).astype(dtype=np.uint8)
            try:
                load_nii(outputname)
            except IOError:
                roi_nii = load_nii(p[0])
                roi = roi_nii.get_data().astype(dtype=np.bool)
                centers = get_mask_voxels(roi)
                test_samples = np.count_nonzero(roi)
                image = np.zeros_like(roi).astype(dtype=np.uint8)
                print(c['c'] + '[' + strftime("%H:%M:%S") + ']    ' + c['g'] +
                      '<Creating the probability map ' + c['b'] + p_name + c['nc'] + c['g'] +
                      ' (%d samples)>' % test_samples + c['nc'])
                test_steps_per_epoch = -(-test_samples / batch_size)
                y_pr_pred = net.predict_generator(
                    generator=load_patch_batch_generator_test(
                        image_names=p,
                        centers=centers,
                        batch_size=batch_size,
                        size=patch_size,
                        preload=preload,
                    ),
                    steps=test_steps_per_epoch,
                    max_q_size=queue
                )
                [x, y, z] = np.stack(centers, axis=1)

                if not sequential:
                    tumor = np.argmax(y_pr_pred[0], axis=1)
                    y_pr_pred = y_pr_pred[-1]
                    roi = np.zeros_like(roi).astype(dtype=np.uint8)
                    roi[x, y, z] = tumor
                    roi_nii.get_data()[:] = roi
                    roiname = os.path.join(patient_path, 'deep-brats17' + sufix + 'test.roi.nii.gz')
                    roi_nii.to_filename(roiname)

                y_pred = np.argmax(y_pr_pred, axis=1)

                image[x, y, z] = y_pred
                # Post-processing (Basically keep the biggest connected region)
                image = get_biggest_region(image)
                labels = np.unique(gt.flatten())
                results = (p_name,) + tuple([dsc_seg(gt == l, image == l) for l in labels[1:]])
                text = 'Subject %s DSC: ' + '/'.join(['%f' for _ in labels[1:]])
                print(text % results)
                dsc_results.append(results)

                print(c['g'] + '                   -- Saving image ' + c['b'] + outputname + c['nc'])
                roi_nii.get_data()[:] = image
                roi_nii.to_filename(outputname)
예제 #15
0
def main(job_dir, **args):

    ##Setting up the path for saving logs
    logs_path = job_dir + 'logs/tensorboard-{}'.format(int(time.time()))

    logs_dir = job_dir + 'logs/tensorboard/'

    ##Using the GPU
    with tf.device('/device:GPU:0'):

        with file_io.FileIO(job_dir + 'dataset/train_data.pickle',
                            mode='rb') as file:
            train_set = pickle.load(file)

        with file_io.FileIO(job_dir + 'dataset/train_label.pickle',
                            mode='rb') as file:
            train_label = pickle.load(file)

        dense_layers = [0, 1, 2]
        layer_sizes = [8, 16, 32, 64]
        conv_layers = [1, 2, 3]

        for dense_layer in dense_layers:
            for layer_size in layer_sizes:
                for conv_layer in conv_layers:
                    NAME = "{}-conv-{}-nodes-{}-dense-{}".format(
                        conv_layer, layer_size, dense_layer, int(time.time()))

                    model = Sequential()

                    model.add(
                        Conv2D(layer_size, (3, 3), input_shape=(64, 64, 3)))
                    model.add(Activation('relu'))
                    model.add(MaxPooling2D(pool_size=(2, 2)))

                    for l in range(conv_layer - 1):
                        model.add(Conv2D(layer_size, (3, 3)))
                        model.add(Activation('relu'))
                        model.add(MaxPooling2D(pool_size=(2, 2)))

                    model.add(Flatten())
                    for _ in range(dense_layer):
                        model.add(Dense(layer_size))
                        model.add(Activation('relu'))

                    model.add(Dense(13))
                    model.add(Activation('sigmoid'))

                    tensorboard = callbacks.TensorBoard(log_dir=logs_dir +
                                                        "{}".format(NAME))

                    # checkpoint = ModelCheckpoint('model-checkpoint-{}.h5'.format(NAME),
                    #                              monitor='val_loss', verbose=1,
                    #                              save_best_only=True, mode='min')

                    model.compile(
                        loss='categorical_crossentropy',
                        optimizer='adam',
                        metrics=['accuracy'],
                    )

                    model.fit(train_set,
                              train_label,
                              batch_size=40,
                              epochs=15,
                              validation_split=0.3,
                              callbacks=[tensorboard])

                    model.save('model-{}.h5'.format(NAME))
                    with file_io.FileIO('model-{}.h5'.format(NAME),
                                        mode='r') as input_f:
                        with file_io.FileIO(job_dir + 'model/' +
                                            'model-{}.h5'.format(NAME),
                                            mode='w+') as output_f:
                            output_f.write(input_f.read())
예제 #16
0
파일: test.py 프로젝트: zhuyuecai/AITour
def leave_one_cnn(cpgs, phenos, which_leave):
    print(which_leave)
    correct = 0
    X = []
    cpg = []
    kids = []
    for r in cpgs:
        X.append(r[1])
        cpg.append(r[0])
        kids.append(r[2])

    X = np.array(X)
    X = X.reshape(num_kids, num_cpg)

    kids = np.array(kids)
    kids = kids.reshape(num_kids, num_cpg)
    kids = np.transpose(kids).reshape(-1)
    cpg = np.array(cpg[0:num_cpg])
    kids = np.array(kids[0:num_kids])

    Y = []

    counter = 0
    for r in phenos:
        if (r[0] != kids[counter]):
            print("kids don't match!")
            exit()
        counter += 1
        Y.append(r[1])

    #Y=np.mat(Y)
    Y = np.asarray(Y)

    X_test = np.asarray(X[which_leave])
    Y_test = np.asarray([Y[which_leave]])
    #leave one here
    #print(X.shape)
    #print(Y.shape)
    X = np.vstack((X[:which_leave], X[(which_leave + 1):]))

    t = Y[:which_leave]
    t = np.expand_dims(t, axis=1)
    tt = Y[(which_leave + 1):]
    tt = np.expand_dims(tt, axis=1)
    Y = np.vstack((t, tt))

    # print(Y_test.shape)
    # print(X_test.shape)
    # print(X.shape)
    # print(Y.shape)

    sm = SMOTE(random_state=42)

    #X_train, X_test, Y_train, Y_test = train_test_split(X,Y,test_size=0.1, random_state=42)

    #X_res, Y_res = sm.fit_sample(X_train, Y_train)

    X_res, Y_res = sm.fit_sample(X, Y)
    """
    X_train, X_val, Y_train, Y_val = train_test_split(X_res,Y_res,test_size=0.05, random_state=42)
    #X=np.transpose(X)
    X_train = np.expand_dims(X_train, axis=2) 
    #X_train = np.expand_dims(X_train, axis=1) 
    X_val= np.expand_dims(X_val, axis=2) 
    """
    X_val = np.expand_dims(X, axis=2)
    print(X.shape)
    print(X_res.shape)
    X_train = np.expand_dims(X_res, axis=2)
    X_test = np.expand_dims(X_test, axis=1)
    X_test = np.expand_dims(X_test, axis=0)
    #Y_train=np.column_stack((Y_train,1-Y_train))
    Y_train = np.column_stack((Y_res, 1 - Y_res))
    #Y_val=np.column_stack((Y_val,1-Y_val))
    Y_val = np.column_stack((Y, 1 - Y))
    Y_test = np.column_stack((Y_test, 1 - Y_test))
    #Y_res = np.expand_dims(Y_res, axis=1)
    #np.column_stack((Y_res,1-Y_res))

    #Y_test = np.expand_dims(Y_test, axis=1)

    #print(X_res.shape)
    #print(Y_res.shape)
    #print(X_test.shape)
    #print(Y_test.shape)

    #deep feed forward
    """
    model = Sequential((
        Dense(1000,input_dim = num_cpg,activation='linear',use_bias=with_bias),
        Dropout(dropout),
        Dense(500,activation='linear',use_bias=with_bias),
        Dropout(dropout),
        Dense(300,activation='linear',use_bias=with_bias),
        Dropout(dropout),
        Dense(nb_class, activation='sigmoid'),
        ))

    #deep cnn
    """
    model = Sequential((
        # The first conv layer learns `nb_filter` filters (aka kernels), each of size ``(filter_length, nb_input_series)``.
        # Its output will have shape (None, window_size - filter_length + 1, nb_filter), i.e., for each position in
        # the input timeseries, the activation of each filter at that position.
        Convolution1D(nb_filter=nb_filter1,
                      filter_length=filter_length1,
                      activation='relu',
                      input_shape=(num_cpg, 1),
                      use_bias=with_bias),
        MaxPooling1D(),  # Downsample the output of convolution by 2X.
        Dropout(dropout),
        Convolution1D(nb_filter=nb_filter2,
                      filter_length=filter_length2,
                      activation='relu',
                      use_bias=with_bias),
        MaxPooling1D(),
        Dropout(dropout),
        Flatten(),
        Dense(
            nb_filter2, activation='sigmoid'
        ),  # For binary classification, change the activation to 'sigmoid'
        Dense(
            nb_class, activation='sigmoid'
        ),  # For binary classification, change the activation to 'sigmoid'
        Activation('softmax')))

    adam = optimizers.Adam()
    sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    earlyStopping = EarlyStopping(monitor='val_loss',
                                  patience=30,
                                  verbose=0,
                                  mode='min')
    mcp_save = ModelCheckpoint(save_path % (which_leave),
                               save_best_only=True,
                               monitor='val_loss',
                               mode='min')
    reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss',
                                       factor=0.1,
                                       patience=7,
                                       verbose=1,
                                       epsilon=1e-4,
                                       mode='min')
    # To perform (binary) classification instead:
    # sgd is reported for better val accuracy: https://arxiv.org/abs/1705.08292
    model.compile(loss='categorical_crossentropy',
                  optimizer=sgd,
                  metrics=['categorical_accuracy'])

    #model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['binary_accuracy'])
    #print('\n\nModel with input size {}, output size {}, {} conv filters of length {}'.format(model.input_shape,model.output_shape, nb_filter1, filter_length1))
    #model.summary()
    model.fit(X_train,
              Y_train,
              epochs=n_epoch,
              batch_size=batch_size,
              shuffle=True,
              callbacks=[mcp_save],
              verbose=0,
              validation_data=(X_val, Y_val))
    model.load_weights(filepath=save_path % (which_leave))
    pred = model.predict(X_test)
    #print(pred[0])
    #print(Y_test[0])
    if pred[0][0] < 0.5 and Y_test[0][0] == 0:
        correct = 1
    if pred[0][0] > 0.5 and Y_test[0][0] == 1:
        correct = 1

    print('\n\nactual', 'predicted', sep='\t')
    for actual, predicted in zip(Y_test, pred.squeeze()):
        print(actual.squeeze(), predicted, sep='\t')
    if correct == 1:
        print("correct!")
    else:
        print("false!")

    #model.save(save_path%(which_leave))
    print("save model to folder trained_model")
    return ((pred[0][0], Y_test[0][0], correct))
def save_bottlebeck_features():
	np.random.seed(2929)
	
	vgg_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(150, 150, 3))
	print('Model loaded.')

	#initialise top model
	top_model = Sequential()
	top_model.add(Flatten(input_shape=vgg_model.output_shape[1:]))
	top_model.add(Dense(256, activation='relu'))
	top_model.add(Dropout(0.5))
	top_model.add(Dense(1, activation='sigmoid'))

        model = Model(inputs=vgg_model.input, outputs=top_model(vgg_model.output))

        model.trainable = True

        model.summary()

	#Total of 20 layers. The classification is considered as one layer
        #Therefore, intermediate is 19 layers
        #0, 4[:4], 3[:7], 4[:11], 4[:15], 4[:19] (Group 0, 1, 2, 3, 4, 5)
        #0 -> All trainable
        #5 -> All non-trainable except classification layer
        #Always keep layer 20 trainable because it is classification layer
        #layer_count = 1
        for layer in model.layers[:19]:
            layer.trainable = False
	    #print("NO-Top: Layer is %d trainable" %layer_count)  
	    #layer_count = layer_count + 1

        model.summary()


        train_datagen = ImageDataGenerator(rescale=1. / 255)
        test_datagen = ImageDataGenerator(rescale=1. / 255)

        train_generator = train_datagen.flow_from_directory(
            train_data_dir,
            target_size=(img_height, img_width),
            batch_size=batch_size,
            class_mode='binary')

        validation_generator = test_datagen.flow_from_directory(
            validation_data_dir,
            target_size=(img_height, img_width),
            batch_size=batch_size,
            class_mode='binary')

	sgd = optimizers.Adam(lr=1e-6) #optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)

	model.compile( loss = "binary_crossentropy", 
            optimizer = sgd, 
            metrics=['accuracy']
            )

#        model.compile(optimizer='rmsprop',
#            loss='binary_crossentropy', metrics=['accuracy'])

        history = model.fit_generator(
	    train_generator,
            steps_per_epoch=nb_train_samples // batch_size,
            epochs=epochs,
            validation_data=validation_generator,
            validation_steps=nb_validation_samples // batch_size,
            verbose=1)

        history_dict =  history.history

        #Plotting the training and validation loss
        history_dict = history.history
        loss_values = history_dict['loss']
        val_loss_values = history_dict['val_loss']
        epochs_0 = range(1, len(history_dict['acc']) + 1)
        plt.plot(epochs_0, loss_values, 'bo', label='Training loss')
        plt.plot(epochs_0, val_loss_values, 'b', label='Validation loss')
        plt.title('ADvsNM_32_VGG16_Freeze_data4_group5 - Training and validation loss')
        plt.xlabel('Epochs')
        plt.ylabel('Loss')
        plt.legend()
        #plt.show()
	plt.savefig('ADvsNM_32_VGG16_Freeze_data4_group5_loss.png')
	plt.close()

        #Plotting the training and validation accuracy
        acc_values = history_dict['acc']
        val_acc_values = history_dict['val_acc']
        plt.plot(epochs_0, acc_values, 'bo', label='Training acc')
        plt.plot(epochs_0, val_acc_values, 'b', label='Validation acc')
        plt.title('ADvsNM_32_VGG16_Freeze_data4_group5 - Training and validation accuracy')
        plt.xlabel('Epochs')
        plt.ylabel('Loss')
        plt.legend()
        #plt.show()
	plt.savefig('ADvsNM_32_VGG16_Freeze_data4_group5_acc.png')
	plt.close()
예제 #18
0
X_train, X_test, y_train, y_test = train_test_split(x,
                                                    y,
                                                    test_size=0.2,
                                                    random_state=2)

###########################################################################################################################
# Custom_resnet_model_1
#Training the classifier alone
image_input = Input(shape=(224, 224, 3))

model = ResNet50(weights='imagenet',
                 input_tensor=image_input,
                 include_top=True)
model.summary()
last_layer = model.get_layer('avg_pool').output
x = Flatten(name='flatten')(last_layer)
out = Dense(num_classes, activation='softmax', name='output_layer')(x)
custom_resnet_model = Model(inputs=image_input, outputs=out)
custom_resnet_model.summary()

for layer in custom_resnet_model.layers[:-1]:
    layer.trainable = False

custom_resnet_model.layers[-1].trainable

custom_resnet_model.compile(loss='binary_crossentropy',
                            optimizer='adam',
                            metrics=['accuracy'])

t = time.time()
hist = custom_resnet_model.fit(X_train,
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
예제 #20
0
def get_model(args):
    # Dataset config
    assert args.dataset.lower() == 'avletters'
    config = data_constants['avletters']
    inputCNNshape = config['lstm_inputCNNshape']
    inputMLPshape = config['lstm_inputMLPshape']
    nb_classes = config['nb_classes']

    # Build the CNN - pre-cross-connections
    inputCNN = Input(shape=inputCNNshape)
    inputNorm = TimeDistributed(Flatten())(inputCNN)
    inputNorm = Masking(mask_value=0.)(inputNorm)
    inputNorm = TimeDistributed(Reshape((80, 60, 1)))(inputNorm)
    inputNorm = BatchNormalization(axis=1)(inputNorm)

    conv = TimeDistributed(Convolution2D(8,
                                         3,
                                         3,
                                         border_mode='same',
                                         activation='relu'),
                           name='conv11')(inputNorm)
    pool = TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2)),
                           name='maxpool1')(conv)

    # Build the MLP - pre-cross-connections
    inputMLP = Input(shape=inputMLPshape)
    inputMasked = Masking(mask_value=0., input_shape=inputMLPshape)(inputMLP)

    fcMLP = TimeDistributed(Dense(32, activation='relu'),
                            name='fc1')(inputMasked)

    # Add the 1st round of cross-connections - CNN to MLP
    x21 = TimeDistributed(Convolution2D(8, 1, 1, border_mode='same'))(pool)
    x21 = TimeDistributed(PReLU())(x21)
    x21 = TimeDistributed(Flatten())(x21)
    x21 = TimeDistributed(Dense(32))(x21)
    x21 = TimeDistributed(PReLU())(x21)

    # Add 1st shortcut (residual connection) from CNN input to MLP
    short1_2dto1d = TimeDistributed(MaxPooling2D((4, 4),
                                                 strides=(4, 4)))(inputNorm)
    short1_2dto1d = TimeDistributed(Flatten())(short1_2dto1d)
    short1_2dto1d = TimeDistributed(Dense(32))(short1_2dto1d)
    short1_2dto1d = TimeDistributed(PReLU())(short1_2dto1d)

    # Cross-connections - MLP to CNN
    x12 = TimeDistributed(Dense(25 * 15))(fcMLP)
    x12 = TimeDistributed(PReLU())(x12)
    x12 = TimeDistributed(Reshape((25, 15, 1)))(x12)
    x12 = TimeDistributed(Conv2DTranspose(8, (16, 16), padding='valid'))(x12)
    x12 = TimeDistributed(PReLU())(x12)

    # 1st shortcut (residual connection) from MLP input to CNN
    short1_1dto2d = TimeDistributed(Dense(25 * 15))(inputMasked)
    short1_1dto2d = TimeDistributed(PReLU())(short1_1dto2d)
    short1_1dto2d = TimeDistributed(Reshape((25, 15, 1)))(short1_1dto2d)
    short1_1dto2d = TimeDistributed(
        Conv2DTranspose(8, (16, 16), padding='valid'))(short1_1dto2d)
    short1_1dto2d = TimeDistributed(PReLU())(short1_1dto2d)

    # CNN - post-cross-connections 1
    pool = add([pool, short1_1dto2d])
    merged = concatenate([pool, x12])

    conv = TimeDistributed(Convolution2D(16,
                                         3,
                                         3,
                                         border_mode='same',
                                         activation='relu'),
                           name='conv21')(merged)
    pool = TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2)),
                           name='maxpool2')(conv)

    # MLP - post-cross-connections 1
    fcMLP = add([fcMLP, short1_2dto1d])
    fcMLP = concatenate([fcMLP, x21])

    fcMLP = TimeDistributed(Dense(32, activation='relu'), name='fc2')(fcMLP)

    # Add the 2nd round of cross-connections - CNN to MLP
    x21 = TimeDistributed(Convolution2D(16, 1, 1, border_mode='same'))(pool)
    x21 = TimeDistributed(PReLU())(x21)
    x21 = TimeDistributed(Flatten())(x21)
    x21 = TimeDistributed(Dense(64))(x21)
    x21 = TimeDistributed(PReLU())(x21)

    # Add 2nd shortcut (residual connection) from CNN input to MLP
    short2_2dto1d = TimeDistributed(MaxPooling2D((8, 8),
                                                 strides=(8, 4)))(inputNorm)
    short2_2dto1d = TimeDistributed(Flatten())(short2_2dto1d)
    short2_2dto1d = TimeDistributed(Dense(32))(short2_2dto1d)
    short2_2dto1d = TimeDistributed(PReLU())(short2_2dto1d)

    # Cross-connections - MLP to CNN
    x12 = TimeDistributed(Dense(13 * 8))(fcMLP)
    x12 = TimeDistributed(PReLU())(x12)
    x12 = TimeDistributed(Reshape((13, 8, 1)))(x12)
    x12 = TimeDistributed(Conv2DTranspose(16, (8, 8), padding='valid'))(x12)
    x12 = TimeDistributed(PReLU())(x12)

    # 2nd shortcut (residual connection) from MLP input to CNN
    short2_1dto2d = TimeDistributed(Dense(13 * 8))(inputMasked)
    short2_1dto2d = TimeDistributed(PReLU())(short2_1dto2d)
    short2_1dto2d = TimeDistributed(Reshape((13, 8, 1)))(short2_1dto2d)
    short2_1dto2d = TimeDistributed(
        Conv2DTranspose(16, (8, 8), padding='valid'))(short2_1dto2d)
    short2_1dto2d = TimeDistributed(PReLU())(short2_1dto2d)

    # CNN - post-cross-connections 2
    pool = add([pool, short2_1dto2d])
    merged = concatenate([pool, x12])

    reshape = TimeDistributed(Flatten(), name='flatten1')(merged)
    fcCNN = TimeDistributed(Dense(64, activation='relu'),
                            name='fcCNN')(reshape)

    # Merge the models
    fcMLP = add([fcMLP, short2_2dto1d])
    merged = concatenate([fcCNN, fcMLP, x21])
    merged = BatchNormalization(axis=1, name='mergebn')(merged)
    merged = Dropout(0.5, name='mergedrop')(merged)

    lstm = LSTM(64)(merged)
    out = Dense(nb_classes, activation='softmax')(lstm)

    # Return the model object
    model = Model(input=[inputCNN, inputMLP], output=out)
    return model
enc = BatchNormalization()(enc)
enc = Conv2D(filters=128,
             kernel_size=5,
             strides=(2, 2),
             padding='same',
             activation='relu',
             name='encoder_second_conv')(enc)
enc = BatchNormalization()(enc)
enc = Conv2D(filters=256,
             kernel_size=5,
             strides=(2, 2),
             padding='same',
             activation='relu',
             name='encoder_third_conv')(enc)
enc = BatchNormalization()(enc)
enc = Flatten()(enc)
z_mean = Dense(units=2048, activation='relu', name='encoder_output_mean')(enc)
z_mean = BatchNormalization()(z_mean)
z_log_var = Dense(units=2048, name='encoder_output_log_var')(enc)
z_log_var = BatchNormalization()(z_log_var)
z_sampled = Lambda(sampling, name='sampler')([z_mean, z_log_var])
encoder = Model(encoder_input, [z_mean, z_log_var, z_sampled], name='encoder')
encoder.summary()

#making the decoder model
decoder_input = Input(shape=(2048, ), name='decoder_input')
dec = Dense(units=8 * 8 * 256, activation='relu',
            name='decoder_first_layer')(decoder_input)
dec = BatchNormalization()(dec)
dec = Reshape((8, 8, 256))(dec)
dec = Conv2DTranspose(filters=256,
예제 #22
0
def resnet50_model(img_rows, img_cols, color_type=1, num_classes=None):
    """
    Resnet 50 Model for Keras
    Model Schema is based on
    https://github.com/fchollet/deep-learning-models/blob/master/resnet50.py
    ImageNet Pretrained Weights
    https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_th_dim_ordering_th_kernels.h5
    Parameters:
      img_rows, img_cols - resolution of inputs
      channel - 1 for grayscale, 3 for color
      num_classes - number of class labels for our classification task
    """

    # Handle Dimension Ordering for different backends
    global bn_axis
    if K.image_dim_ordering() == 'tf':
        bn_axis = 3
        img_input = Input(shape=(img_rows, img_cols, color_type))
    else:
        bn_axis = 1
        img_input = Input(shape=(color_type, img_rows, img_cols))

    x = ZeroPadding2D((3, 3))(img_input)
    x = Convolution2D(64, 7, 7, subsample=(2, 2), name='conv1')(x)
    x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = Activation('relu')(x)
    x = 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')

    # Fully Connected Softmax Layer
    x_fc = AveragePooling2D((7, 7), name='avg_pool')(x)
    x_fc = Flatten()(x_fc)
    x_fc = Dense(1000, activation='softmax', name='fc1000')(x_fc)

    # Create model
    model = Model(img_input, x_fc)

    # Load ImageNet pre-trained data
    if K.image_dim_ordering() == 'th':
        # Use pre-trained weights for Theano backend
        weights_path = 'imagenet_models/resnet50_weights_th_dim_ordering_th_kernels.h5'
    else:
        # Use pre-trained weights for Tensorflow backend
        weights_path = 'imagenet_models/resnet50_weights_tf_dim_ordering_tf_kernels.h5'

    model.load_weights(weights_path)

    # Truncate and replace softmax layer for transfer learning
    # Cannot use model.layers.pop() since model is not of Sequential() type
    # The method below works since pre-trained weights are stored in layers but not in the model
    x_newfc = AveragePooling2D((7, 7), name='avg_pool')(x)
    x_newfc = Flatten()(x_newfc)
    x_newfc = Dense(num_classes, activation='softmax', name='fc10')(x_newfc)

    # Create another model with our customized softmax
    model = Model(img_input, x_newfc)

    # Learning rate is changed to 0.001
    sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd,
                  loss='categorical_crossentropy',
                  metrics=['accuracy', metrics.categorical_accuracy])

    return model
예제 #23
0
    def buildModel(self):
        """Model layers"""

        # character input
        character_input = Input(shape=(
            None,
            52,
        ), name="Character_input")
        embed_char_out = TimeDistributed(
            Embedding(len(self.char2Idx),
                      30,
                      embeddings_initializer=RandomUniform(minval=-0.5,
                                                           maxval=0.5)),
            name="Character_embedding")(character_input)

        dropout = Dropout(self.dropout)(embed_char_out)

        # CNN, time distributed allows us to map each "time stamp", i.e. character in a sentence, to an embedding.
        # Without time distributed the mapping would be between
        conv1d_out = TimeDistributed(Conv1D(kernel_size=self.conv_size,
                                            filters=30,
                                            padding='same',
                                            activation='tanh',
                                            strides=1),
                                     name="Convolution")(dropout)
        maxpool_out = TimeDistributed(MaxPooling1D(52),
                                      name="Maxpool")(conv1d_out)
        char = TimeDistributed(Flatten(), name="Flatten")(maxpool_out)
        char = Dropout(self.dropout)(char)

        # word-level input
        words = Input(shape=(
            None,
            600,
        ), dtype='float32', name='words_input')
        # words_input = Input(shape=(None,), dtype='int32', name='words_input')
        # words = Embedding(input_dim=self.wordEmbeddings.shape[0], output_dim=self.wordEmbeddings.shape[1],
        #                    weights=[self.wordEmbeddings],
        #                    trainable=False)(words_input)

        # case-info input
        casing_input = Input(shape=(None, ),
                             dtype='int32',
                             name='casing_input')
        casing = Embedding(output_dim=self.caseEmbeddings.shape[1],
                           input_dim=self.caseEmbeddings.shape[0],
                           weights=[self.caseEmbeddings],
                           trainable=False)(casing_input)

        # concat & BLSTM
        output = concatenate([words, casing, char])
        output = Bidirectional(
            LSTM(
                self.lstm_state_size,
                return_sequences=True,
                dropout=self.dropout,  # on input to each LSTM block
                recurrent_dropout=self.
                dropout_recurrent  # on recurrent input signal
            ),
            name="BLSTM")(output)
        output = TimeDistributed(Dense(len(self.label2Idx),
                                       activation='softmax'),
                                 name="Softmax_layer")(output)

        # set up model
        self.model = Model(inputs=[words, casing_input, character_input],
                           outputs=[output])
        #self.model = Model(inputs=[words_input, casing_input, character_input], outputs=[output])

        self.model.compile(loss='sparse_categorical_crossentropy',
                           optimizer=self.optimizer)

        self.init_weights = self.model.get_weights()

        plot_model(self.model, to_file='model.png')

        print("Model built. Saved model.png\n")
예제 #24
0
def train_model(batch_size=1000,
                epochs=1005,
                num_samples=10000,
                var1=0.99,
                var2=0.01):
    num_classes = 10
    print('var1:', var1)
    print('var2:', var2)
    # input image dimensions
    img_rows, img_cols = 28, 28

    # the data, split between train and test sets
    (x_train, y_train), (x_test, y_test) = mnist.load_data()

    if K.image_data_format() == 'channels_first':
        x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
        x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
        input_shape = (1, img_rows, img_cols)
    else:
        x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
        x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
        input_shape = (img_rows, img_cols, 1)

    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255
    x_train = x_train[0:num_samples]
    print('x_train shape:', x_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

    # convert class vectors to binary class matrices
    y_train = keras.utils.to_categorical(y_train, num_classes)[0:num_samples]
    y_test = keras.utils.to_categorical(y_test, num_classes)

    model = Sequential()
    model.add(Flatten(input_shape=(28, 28, 1)))
    model.add(Dense(64, activation='tanh'))
    model.add(Dense(64, activation='tanh'))
    model.add(Dense(64, activation='tanh'))
    model.add(Dense(64, activation='tanh'))
    model.add(Dense(64, activation='tanh'))
    model.add(Dense(64, activation='tanh'))
    model.add(Dense(64, activation='tanh'))
    model.add(Dense(64, activation='tanh'))
    model.add(Dense(64, activation='tanh'))
    model.add(Dense(num_classes, activation='softmax'))

    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=keras.optimizers.sgd(),
                  metrics=['accuracy'])
    model.summary()
    n_pop = 10
    n_para = 84170
    fit = np.zeros((n_pop, 1))
    pop = np.random.normal(0, 0.01, (n_pop, n_para))
    count_update = 0

    flag_add = 0
    flag_stop = 0
    minibatches = random_mini_batches(x_train, y_train, batch_size)
    len_batch = len(minibatches)
    record = np.zeros((epochs, 4))

    count_non_update = 0
    for i in range(epochs):
        print(i,
              'th iteration------------------------------------------------')
        count_batches = 0
        for minibatch in minibatches:
            (minibatch_X, minibatch_Y) = minibatch
            model.train_on_batch(minibatch_X, minibatch_Y)

            for n in range(10):
                a = np.array(model.get_weights())
                score_old = model.evaluate(minibatch_X, minibatch_Y,
                                           verbose=0)[0]
                b = np.array(model.get_weights())
                if flag_add == 0:
                    j = np.random.randint(0, len(b))
                    adding_random = np.random.normal(0, 0.01, b[j].shape)
                b[j] = b[j] + adding_random
                model.set_weights(b)
                score_new = model.evaluate(minibatch_X, minibatch_Y,
                                           verbose=0)[0]
                if score_new < score_old * (var1 + i * var2 / epochs):
                    a = copy.deepcopy(b)
                    score_old = score_new
                    count_update = count_update + 1
                    flag_add = 1
                    break
                else:
                    model.set_weights(a)
                    flag_add = 0
            count_batches = count_batches + 1
            score = model.evaluate(x_train, y_train, verbose=0)
            # with K.Session() as sess:
            #
            #     outputTensor = model.output  # Or model.layers[index].output
            #     listOfVariableTensors = model.trainable_weights
            #     gradients =K.gradients(outputTensor, listOfVariableTensors)

        record[i, 0], record[i, 1] = model.evaluate(x_train,
                                                    y_train,
                                                    verbose=0)
        record[i, 2], record[i, 3] = model.evaluate(x_test, y_test, verbose=0)
        print('Train loss:', record[i, 0])
        print('Train accuracy:', record[i, 1])
        print('Test loss:', record[i, 2])
        print('Test accuracy', record[i, 3])
        print('count_update:', count_update)
        if i > 999 and i % 500 == 0:
            df = pd.DataFrame(
                record,
                columns=['train_loss', 'train_acc', 'test_loss', 'test_acc'])
            df.to_csv('minist_ga' + str(i) + str(num_samples) + str(var1) +
                      str(var2) + '.csv',
                      index=None,
                      header=True)
예제 #25
0
    resnet_input = Input(shape=(224,224,3))
    resnet_model = ResNet50(weights='imagenet', include_top=False, input_tensor=resnet_input)

    """net = resnet_model.get_layer('activation_46').output
    net = MaxPooling2D((7, 7), name='max_pool')(net)
    net = Flatten(name='flatten')(net)
    
    p_drop = 0.5
    net = Dense(512, name='fc1')(net)
    #net = BatchNormalization()(net)
    net = PReLU()(net)
    net = Dropout(rate=p_drop)(net)"""

    net = resnet_model.output
    net = Flatten(name='flatten')(net)
    net = Dense(512, activation='relu', name='fc1')(net)
    net = Dense(512, name='embded')(net)
    net = Lambda(l2Norm, output_shape=[512])(net)


    base_model = Model(resnet_model.input, net, name='resnet_model')
    base_model.summary()

    """ Train just the new layers, let the pretrained ones be as they are (they'll be trained later) """
    for layer in resnet_model.layers:
        layer.trainable = False


    """ Building triple siamese architecture """
예제 #26
0
def ResNetPreAct(input_shape=(500,500,3), nb_classes=13, layer1_params=(5,64,2), res_layer_params=(3,16,3),
        final_layer_params=None, init='glorot_normal', reg=0.0, use_shortcuts=False):
    
    """
    Return a new Residual Network using full pre-activation based on the work in
    "Identity Mappings in Deep Residual Networks"  by He et al
    http://arxiv.org/abs/1603.05027

    The following network definition achieves 92.0% accuracy on CIFAR-10 test using
    `adam` optimizer, 100 epochs, learning rate schedule of 1e.-3 / 1.e-4 / 1.e-5 with
    transitions at 50 and 75 epochs:
    ResNetPreAct(layer1_params=(3,128,2),res_layer_params=(3,32,25),reg=reg)
    
    Removed max pooling and using just stride in first convolutional layer. Motivated by
    "Striving for Simplicity: The All Convolutional Net"  by Springenberg et al
    (https://arxiv.org/abs/1412.6806) and my own experiments where I observed about 0.5%
    improvement by replacing the max pool operations in the VGG-like cifar10_cnn.py example
    in the Keras distribution.
    
    Parameters
    ----------
    input_dim : tuple of (C, H, W)
    nb_classes: number of scores to produce from final affine layer (input to softmax)
    layer1_params: tuple of (filter size, num filters, stride for conv)
    res_layer_params: tuple of (filter size, num res layer filters, num res stages)
    final_layer_params: None or tuple of (filter size, num filters, stride for conv)
    init: type of weight initialization to use
    reg: L2 weight regularization (or weight decay)
    use_shortcuts: to evaluate difference between residual and non-residual network
    """

    sz_L1_filters, nb_L1_filters, stride_L1 = layer1_params
    sz_res_filters, nb_res_filters, nb_res_stages = res_layer_params
    
    use_final_conv = (final_layer_params is not None)
    if use_final_conv:
        sz_fin_filters, nb_fin_filters, stride_fin = final_layer_params
        sz_pool_fin = input_shape[1] / (stride_L1 * stride_fin)
    else:
        sz_pool_fin = input_shape[1] / (stride_L1)


    '''
    from keras import backend as K
    # Permute dimension order if necessary
    if K.image_dim_ordering() == 'tf':
        input_shape = (input_shape[1], input_shape[2], input_shape[0])
    '''

    img_input = Input(shape=input_shape, name='cifar')

    x = Conv2D(
            filters=nb_L1_filters, 
            kernel_size=(sz_L1_filters,sz_L1_filters),
            padding='same',
            strides=(stride_L1, stride_L1),
            kernel_initializer=init,
            kernel_regularizer=l2(reg),
            use_bias=False,
            name='conv0'
        )(img_input)
    
    x = BatchNormalization(axis=3, name='bn0')(x)
    x = Activation('relu', name='relu0')(x)

    for stage in range(1,nb_res_stages+1):
        x = rnpa_bottleneck_layer(
                x,
                (nb_L1_filters, nb_res_filters),
                sz_res_filters, 
                stage,
                init=init, 
                reg=reg, 
                use_shortcuts=use_shortcuts
            )


    x = BatchNormalization(axis=3, name='bnF')(x)
    x = Activation('relu', name='reluF')(x)

    if use_final_conv:
        x = Conv2D(
                filters=nb_L1_filters, 
                kernel_size=(sz_L1_filters,sz_L1_filters),
                padding='same',
                strides=(stride_fin, stride_fin),
                kernel_initializer=init,
                kernel_regularizer=l2(reg),
                name='convF'
            )(x)

    x = AveragePooling2D((sz_pool_fin,sz_pool_fin), name='avg_pool')(x)

    # x = Flatten(name='flat')(x)
    x = Flatten()(x)
    x = Dense(nb_classes, activation='softmax', name='fc10')(x)

    return Model(img_input, x, name='rnpa')
예제 #27
0
l_in   = Input(shape=(DIM_COM,), dtype='float32', name='l');            # inputed command;
o_in   = Input(shape=(DIM_IMG,DIM_IMG,1), dtype='float32', name='o');   # inputed image;
a_in   = Input(shape=(DIM_a,), dtype='float32', name='a_last');         # last step action;
Gs_in  = Input(shape=(DIM_Gs,), dtype='float32', name='Gs');            # G_s[t];
Gs_in_ = Input(shape=(DIM_Gs,), dtype='float32', name='Gs_');           # G_s[t-1];
Gi_in  = Input(shape=(DIM_Gi,), dtype='float32', name='Gi');            # G_i[t];
#------- Embedding -------
T      = Dense(32, activation='relu')(l_in);
#------- CNN -------------
C      = Conv2D(16, (5,5), strides=(1, 1), activation='relu',padding='same')(o_in); # out:60*60 *6;
C      = MaxPooling2D(pool_size=(2, 2), strides=(2, 2),padding='same')(C); # out:30*30 *6;
C      = Conv2D(8, (3,3), strides=(1, 1), activation='relu',padding='same')(C); # out:60*60 *6;
C      = MaxPooling2D(pool_size=(2, 2), strides=(2, 2),padding='same')(C);
C      = Conv2D(8, (3,3), strides=(1, 1), activation='relu',padding='same')(C); 
C      = MaxPooling2D(pool_size=(2, 2), strides=(2, 2),padding='same')(C);
C      = Flatten()(C); # the output cnn features; 
#------- R(V,l) -------------
#C_     = concatenate([C,T]); # the output cat;
C_1    = Dense(32, activation='relu')(C);
C_2    = Dense(32, activation='relu')(T);
C_     = Add()([C_1,C_2]);
R_S    = Dense(DIM_Gs, activation='relu')(C_); # the output GFT;
#------ I(V,l,G_s[t],G_s[t-1]) ----
C_i    = Dense(DIM_hm, activation='relu')(C_);
S_i    = Dense(DIM_hm, activation='relu')(Gs_in);
S_i_   = Dense(DIM_hm, activation='relu')(Gs_in_);
G_i    = Add()([C_i,S_i,S_i_]);
G_i    = Dense(32, activation='relu')(G_i);
I_G    = Dense(DIM_Gi, activation='relu')(G_i);
#------ GRU for Pi(G_s,G_i,C*) -------
G_S    = Embedding(output_dim=ACT_OUT_DIM, input_dim=DIM_Gs, input_length=ACT_STEPS,name = 'emb1')(Gs_in);
예제 #28
0
def main():

        ####### Some Parameters ###########

        nepochs =int(argv[1])
        # Energy cuts
        cutlow=40
        cuthigh=260

        # 2 capas ocultas con numero de neuronas, definidas en la variable siguiente
        #neurons = [128, 32, 8] 
        #nneurons = len(argv)-3
        neurons = [int(argv[2]),int(argv[3]),int(argv[4])] 
        print("neurons: ",argv[2],argv[3],argv[4])
        nepochstot = nepochs
        modelloaded = False


        ####### Estructura de la FFNN ###########
        ####### Comprobamos si la cargamos o la creamos. Si la cargamos lo hacemos ahora, si la creamos lo haremos luego. 
        if( len(argv)==7 and argv[5]=='load'):
          ifolder = "/tmp/ml_outputs/testsize0.25sizeofbatch0.75"
          filename = "%s/model_ANN_epochs%d_2HL_%d_%d_%d__%d_%d.h5" % (ifolder,nepochs,neurons[0],neurons[1],neurons[2],cutlow,cuthigh)
          #filename = "%s" % (ifile)
          print('Loading model : ',filename)
          model = load_model(filename)
          modelloaded = True
          # epochs that want to be run in this iteration.
          nepochs = int(argv[6])
          # total number of epochs that the ouput model has gone through
          nepochstot += nepochs
        ####### Estructura de la FFNN ###########


        ####### Reading the data ###########

        #data = pd.read_csv('../rawdata/sample.txt', sep="\t", header=None)
        #data = pd.read_csv('../rawdata/lightpatternXYZar40_nominal_30000.txt', sep="\t", header=None)
        data01 = pd.read_csv('/tmp/data/sk_ar40_0_100.txt', sep="\t", header=None)
        data02 = pd.read_csv('/tmp/data/sk_ar40_100_200.txt', sep="\t", header=None)
        data03 = pd.read_csv('/tmp/data/sk_ar40_200_300.txt', sep="\t", header=None)
        data04 = pd.read_csv('/tmp/data/sk_ar40_300_400.txt', sep="\t", header=None)
        data05 = pd.read_csv('/tmp/data/sk_ar40_400_500.txt', sep="\t", header=None)
        data06 = pd.read_csv('/tmp/data/sk_ar40_500_600.txt', sep="\t", header=None)
        data07 = pd.read_csv('/tmp/data/sk_ar40_600_700.txt', sep="\t", header=None)
        data08 = pd.read_csv('/tmp/data/sk_ar40_700_800.txt', sep="\t", header=None)
        data09 = pd.read_csv('/tmp/data/sk_ar40_800_900.txt', sep="\t", header=None)
        data10 = pd.read_csv('/tmp/data/sk_ar40_900_1000.txt', sep="\t", header=None)
        data1 = pd.concat([data01,data02,data03,data04,data05,data06,data07,data08,data09,data10], ignore_index=True)
        data1 = shuffle(data1)

        data2 = pd.read_csv('/tmp/data/sk_neck_0_500.txt', sep="\t", header=None)
        data3 = pd.read_csv('/tmp/data/sk_neck_500_1000.txt', sep="\t", header=None)
        data4 = pd.read_csv('/tmp/data/sk_neck_1000_1500.txt', sep="\t", header=None)
        data5 = pd.read_csv('/tmp/data/sk_neck_1500_2000.txt', sep="\t", header=None)
        datan = pd.concat([data2,data3,data4,data5], ignore_index=True)

        print(shape(data1))
        print(shape(datan))

        # Adding summed Charge column
        datan['sumPMTs']=datan.iloc[:,0:255].sum(axis=1)

        # energy cuts: Selecting data of neck events
        datan = datan[(datan['sumPMTs'] >= cutlow) & (datan['sumPMTs'] <= cuthigh)]
        print('neck aftercuts ',shape(datan))

        data1['sumPMTs']=data1.iloc[:,0:255].sum(axis=1)
        # energy cuts: Performing same cuts on neck and Ar  events
        data1 = data1[(data1['sumPMTs'] >= cutlow) & (data1['sumPMTs'] <= cuthigh)]
        print('LAr aftercuts ',shape(data1))
        # Cutting Ar-40 data to avoid biasing
        datanum = datan.shape[0]*3
        print('LAr accepted ',datanum)
        datag= data1[:datanum]

        data = pd.concat([datag,datan], ignore_index=True)

        data = shuffle(data)

        # data = data[(data['sumPMTs'] >= cutlow) & (data['sumPMTs'] <= cuthigh)]
        
        print(shape(data))
        print(data.columns)

        # Selecting the last column: 1 if neck, 0 if Ar-40 recoil
        Y= data.iloc[:,513:514]
        print("Y.shape", Y.shape)
        
        # Enconde binary option A:
        #Y = to_categorical(Y,num_classes=2)
        #print("Y", Y.shape)

        # Encode binary option B: class values as integers
        encoder = LabelEncoder()
        encoder.fit(Y)
        encoded_Y = encoder.transform(Y)
        print("Y",Y)

        X= data.iloc[:,0:255]
        print(X.shape)
        
        pos= data.iloc[:,510:513]
        mbpos= data.iloc[:,514:517]
        print(pos.shape)

        X = np.asarray(X)
        Y = np.asarray(Y)
        pos = np.asarray(pos)
        mbpos = np.asarray(mbpos)

        # Creamos los conjuntos de datos de entrenamiento y de evaluacion. 
        #test_size = 100 
        test_size = int(np.floor(0.25*X.shape[0]) )
        #sizeofbatch =int(np.floor(0.25*test_size))
        sizeofbatch =int(np.floor(0.75*test_size))
         
        trainX, testX = X[:-test_size], X[-test_size:]
        #trainX = np.reshape(trainX, (1, trainX.shape[0], trainX.shape[1]))
        #testX = np.reshape(testX, (1, testX.shape[0], testX.shape[1]))
        trainY, testY = Y[:-test_size], Y[-test_size:]
        #trainY = np.reshape(trainY, (1, trainY.shape[0], trainY.shape[1]))
        #testYX = np.reshape(testY, (1, testY.shape[0], testY.shape[1]))
        testpos = pos[-test_size:]
        mbtestpos = mbpos[-test_size:]

        chargeT=data['sumPMTs']
        chargeT=chargeT[-test_size:]
        print("lengths:", len(chargeT), len(testY))

        #trainX=trainX[1:]
        #trainY=trainY[1:]

        trainX = np.reshape(trainX, trainX.shape + (1,))
        testX = np.reshape(testX, testX.shape + (1,))


        print(trainX.shape, testX.shape,trainY.shape, testY.shape)
        print("trainX.shape", trainX.shape[0], trainX.shape[1])
        print("trainY.shape", trainY.shape[0], trainY.shape[1])

        
        ####### Estructura de la FFNN ###########

        ####### Si no está cargada la creamos ###########

        if (not modelloaded):
          print('Loading model')
          # 2 capas ocultas con numero de neuronas, definidas en la variable siguiente
          #neurons = [128, 32, 8] 
          #nneurons = len(argv)-3
          #neurons = [int(argv[2]),int(argv[3]),int(argv[4])] 
          #  print("neurons: ",argv[2],argv[3],argv[4])
  
          # Creamos la base del modelo
          model = Sequential() 

          # Ponemos una primera capa oculta 
          model.add(Dense(neurons[0], activation='relu', input_shape=(trainX.shape[1], 1)))
          #print(model.layers[-1].output_shape)
        
          # Incorporamos una segunda capa oculta 
          model.add(Dense(neurons[1], activation='relu'))
          #print(model.layers[-1].output_shape)

          # Incorporamos una terceraa capa oculta 
          model.add(Dense(neurons[2], activation='relu'))
        
          # Aplanamos los datos para reducir la dimensionalidad en la salida
          model.add(Flatten())

          # A\~nadimos la capa de salida de la red con activacion lineal
          #model.add(Dense( trainY.shape[1], activation='tanh'))
          model.add(Dense( trainY.shape[1], activation='sigmoid'))


          # Compilamos el modelo usando el optimizador Adam
          model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 
          #model.compile(loss="binary_crossentropy", optimizer="adam", metrics=['binary_crossentropy']) 

        print(model.layers[-1].output_shape)

        # Entrenamos la red
        #model.fit(trainX, trainY, epochs=nepochs, batch_size=1, verbose=2) 
        history=model.fit(trainX, trainY, epochs=nepochs, batch_size=sizeofbatch, validation_data=(testX, testY), verbose=2) 

        # Pronosticos 
        pred = model.predict(testX)

        print("pred.shape: ",pred.shape)
        print("testY.shape: ",testY.shape)

        errors= np.empty([test_size])

        #print('\n\nReal', '        ', 'Pronosticado')
        #for actual, predicted in zip(testY, pred.squeeze()):
                #print(actual.squeeze(), '\t', predicted, '\t',actual.squeeze()-predicted)
        R= np.empty([test_size])
        mbR= np.empty([test_size])

        print(R.shape)
        print('sumPMT format ',chargeT.shape)
        filename = "./r_error_ANN_epochs%d_2HL_%d_%d_%d__%d_%d_binaryclass.txt" % (nepochstot, neurons[0],neurons[1],neurons[2],cutlow,cuthigh)
        fh = open(filename, "w")

                #errors.append(actual.squeeze()-predicted)
        for i in range(test_size):
                #print("X:", testY[i,0],pred[i,0], testY[i,0]-pred[i,0], "\t Y:", testY[i,1],pred[i,1],testY[i,1]-pred[i,1], "\t Z:")
                errors[i]= testY[i]-pred[i]
                #errorsY[i]= testY[i,1]-pred[i,1]
                #errorsZ[i]= testY[i,2]-pred[i,2]
                R[i]= np.sqrt(testpos[i][0]*testpos[i][0]+testpos[i][1]*testpos[i][1]+testpos[i][2]*testpos[i][2])        
                mbR[i]= np.sqrt(mbtestpos[i][0]*mbtestpos[i][0]+mbtestpos[i][1]*mbtestpos[i][1]+mbtestpos[i][2]*mbtestpos[i][2])        
                #print(chargeT.iloc[i])
                #print(float(chargeT.iloc[i]))
                #print(Y[i])
                #print(pred[i])
                myline = "%f %d %f %f %f\n" % (R[i], testY[i], pred[i], float(chargeT.iloc[i]), mbR[i])
                #myline = "%f %d %f %f\n" % (R[i], testY[i], pred[i], float(chargeT.iloc[i]))
                fh.write(myline)
        fh.close()


        # Guardar el modelo, si se quiere
        filename = "./model_ANN_epochs%d_2HL_%d_%d_%d__%d_%d_binaryclass.h5" % (nepochstot, neurons[0],neurons[1],neurons[2],cutlow,cuthigh)
        model.save(filename)

        # Calcular ECM y EAM
        testScoreECM = mean_squared_error(testY, pred)
        print('ECM: %.4f' % (testScoreECM))

        testScoreEAM = mean_absolute_error(testY, pred)
        print('EAM: %.4f' % (testScoreEAM))
        
        # histogram errors 
        fig,ax = plt.subplots(nrows=1,ncols=1,figsize=(8,8))# 6,6
        plt.figure(1)
        #plt.ylim(0,500)
        ax.set_yscale("log")
        plt.hist(errors, 40)
        plt.title('Error X')
        filename = "./histerrors_ANN_epochs%d_2HL_%d_%d_%d__%d_%d_binaryclass.eps" % (nepochstot, neurons[0],neurons[1],neurons[2],cutlow,cuthigh)
        plt.savefig(filename)

        # summarize history for loss
        fig,ax = plt.subplots(nrows=1,ncols=1,figsize=(8,8))# 6,6
        plt.figure(2)
        plt.plot(history.history['loss'])
        plt.plot(history.history['val_loss'])
        ax.set_yscale("log")
        #plt.title('model loss')
        plt.ylabel('loss')
        plt.xlabel('epoch')
        plt.legend(['train', 'test'], loc='upper left')
        filename = "./loss_ANN_epochs%d_2HL_%d_%d_%d__%d_%d_binaryclass.eps" % (nepochstot, neurons[0],neurons[1],neurons[2],cutlow,cuthigh)
        plt.savefig(filename)

        fig,ax = plt.subplots(nrows=1,ncols=1,figsize=(8,8))# 6,6
        plt.figure(3)
        plt.scatter(R, pred, s=0.1, color='black')
        plt.scatter(R, testY, s=0.1, color='red')
        #plt.title('model loss')
        plt.ylabel('IsNeck')
        plt.xlabel('radius')
        plt.ylim(-1.1,1.1)
        plt.legend(['train', 'test'], loc='upper left')
        filename = "./scatterplotr_ANN_epochs%d_2HL_%d_%d_%d__%d_%d_binaryclass.eps" % (nepochstot, neurons[0],neurons[1],neurons[2],cutlow,cuthigh)
        plt.savefig(filename)

        fig,ax = plt.subplots(nrows=1,ncols=1,figsize=(8,8))# 6,6
        plt.figure(4)
        plt.scatter( testY, pred, s=0.1, color='black')
        plt.ylabel('Pred')
        plt.xlabel('True')
        plt.xlim(-0.1,1.1)
        plt.legend(['pred', 'test'], loc='upper left')
        filename = "./scatterplot_TruevsPred_ANN_epochs%d_2HL_%d_%d_%d__%d_%d_binaryclass.eps" % (nepochstot, neurons[0],neurons[1],neurons[2],cutlow,cuthigh)
        plt.savefig(filename)


        fig,ax = plt.subplots(nrows=1,ncols=1,figsize=(8,8))# 6,6
        plt.figure(5)
        plt.scatter(chargeT, errors, s=0.1, color='black')
        #plt.scatter(sumPMTs, testY, s=0.1, color='red')
        #plt.title('model loss')
        plt.ylabel('error')
        plt.xlabel('charge')
        plt.ylim(-1.1,1.1)
        plt.legend(['train', 'test'], loc='upper left')
        filename = "./scatterplotch_ANN_epochs%d_2HL_%d_%d_%d__%d_%d_binaryclass.eps" % (nepochstot, neurons[0],neurons[1],neurons[2],cutlow,cuthigh)
        plt.savefig(filename)
예제 #29
0
def ssd300(input_shape=(300, 300, 3),
           num_classes=80,
           include_top=True,
           weight_decay=0.):
    net = {}

    # Block 1
    input_tensor = Input(shape=input_shape)

    img_size = (input_shape[1], input_shape[0])
    net['input'] = input_tensor
    net['conv1_1'] = Convolution2D(64,
                                   3,
                                   3,
                                   activation='relu',
                                   W_regularizer=l2(weight_decay),
                                   border_mode='same',
                                   name='conv1_1')(net['input'])
    net['conv1_2'] = Convolution2D(64,
                                   3,
                                   3,
                                   activation='relu',
                                   W_regularizer=l2(weight_decay),
                                   border_mode='same',
                                   name='conv1_2')(net['conv1_1'])
    net['pool1'] = MaxPooling2D((2, 2),
                                strides=(2, 2),
                                border_mode='same',
                                name='pool1')(net['conv1_2'])
    # Block 2
    net['conv2_1'] = Convolution2D(128,
                                   3,
                                   3,
                                   activation='relu',
                                   W_regularizer=l2(weight_decay),
                                   border_mode='same',
                                   name='conv2_1')(net['pool1'])
    net['conv2_2'] = Convolution2D(128,
                                   3,
                                   3,
                                   activation='relu',
                                   W_regularizer=l2(weight_decay),
                                   border_mode='same',
                                   name='conv2_2')(net['conv2_1'])
    net['pool2'] = MaxPooling2D((2, 2),
                                strides=(2, 2),
                                border_mode='same',
                                name='pool2')(net['conv2_2'])
    # Block 3
    net['conv3_1'] = Convolution2D(256,
                                   3,
                                   3,
                                   activation='relu',
                                   W_regularizer=l2(weight_decay),
                                   border_mode='same',
                                   name='conv3_1')(net['pool2'])
    net['conv3_2'] = Convolution2D(256,
                                   3,
                                   3,
                                   activation='relu',
                                   W_regularizer=l2(weight_decay),
                                   border_mode='same',
                                   name='conv3_2')(net['conv3_1'])
    net['conv3_3'] = Convolution2D(256,
                                   3,
                                   3,
                                   activation='relu',
                                   W_regularizer=l2(weight_decay),
                                   border_mode='same',
                                   name='conv3_3')(net['conv3_2'])
    net['pool3'] = MaxPooling2D((2, 2),
                                strides=(2, 2),
                                border_mode='same',
                                name='pool3')(net['conv3_3'])
    # Block 4
    net['conv4_1'] = Convolution2D(512,
                                   3,
                                   3,
                                   activation='relu',
                                   W_regularizer=l2(weight_decay),
                                   border_mode='same',
                                   name='conv4_1')(net['pool3'])
    net['conv4_2'] = Convolution2D(512,
                                   3,
                                   3,
                                   activation='relu',
                                   W_regularizer=l2(weight_decay),
                                   border_mode='same',
                                   name='conv4_2')(net['conv4_1'])
    net['conv4_3'] = Convolution2D(512,
                                   3,
                                   3,
                                   activation='relu',
                                   W_regularizer=l2(weight_decay),
                                   border_mode='same',
                                   name='conv4_3')(net['conv4_2'])
    net['pool4'] = MaxPooling2D((2, 2),
                                strides=(2, 2),
                                border_mode='same',
                                name='pool4')(net['conv4_3'])
    # Block 5
    net['conv5_1'] = Convolution2D(512,
                                   3,
                                   3,
                                   activation='relu',
                                   W_regularizer=l2(weight_decay),
                                   border_mode='same',
                                   name='conv5_1')(net['pool4'])
    net['conv5_2'] = Convolution2D(512,
                                   3,
                                   3,
                                   activation='relu',
                                   W_regularizer=l2(weight_decay),
                                   border_mode='same',
                                   name='conv5_2')(net['conv5_1'])
    net['conv5_3'] = Convolution2D(512,
                                   3,
                                   3,
                                   activation='relu',
                                   W_regularizer=l2(weight_decay),
                                   border_mode='same',
                                   name='conv5_3')(net['conv5_2'])
    net['pool5'] = MaxPooling2D((3, 3),
                                strides=(1, 1),
                                border_mode='same',
                                name='pool5')(net['conv5_3'])

    if include_top:
        # FC6
        net['fc6'] = AtrousConvolution2D(1024,
                                         3,
                                         3,
                                         atrous_rate=(6, 6),
                                         activation='relu',
                                         border_mode='same',
                                         name='fc6')(net['pool5'])
        # x = Dropout(0.5, name='drop6')(x)
        # FC7
        net['fc7'] = Convolution2D(1024,
                                   1,
                                   1,
                                   activation='relu',
                                   W_regularizer=l2(weight_decay),
                                   border_mode='same',
                                   name='fc7')(net['fc6'])
        # x = Dropout(0.5, name='drop7')(x)
        # Block 6
        net['conv6_1'] = Convolution2D(256,
                                       1,
                                       1,
                                       activation='relu',
                                       W_regularizer=l2(weight_decay),
                                       border_mode='same',
                                       name='conv6_1')(net['fc7'])
        net['conv6_2'] = Convolution2D(512,
                                       3,
                                       3,
                                       subsample=(2, 2),
                                       activation='relu',
                                       border_mode='same',
                                       name='conv6_2')(net['conv6_1'])
        # Block 7
        net['conv7_1'] = Convolution2D(128,
                                       1,
                                       1,
                                       activation='relu',
                                       W_regularizer=l2(weight_decay),
                                       border_mode='same',
                                       name='conv7_1')(net['conv6_2'])
        net['conv7_2'] = ZeroPadding2D()(net['conv7_1'])
        net['conv7_2'] = Convolution2D(256,
                                       3,
                                       3,
                                       subsample=(2, 2),
                                       activation='relu',
                                       W_regularizer=l2(weight_decay),
                                       border_mode='valid',
                                       name='conv7_2')(net['conv7_2'])
        # Block 8
        net['conv8_1'] = Convolution2D(128,
                                       1,
                                       1,
                                       activation='relu',
                                       W_regularizer=l2(weight_decay),
                                       border_mode='same',
                                       name='conv8_1')(net['conv7_2'])
        net['conv8_2'] = Convolution2D(256,
                                       3,
                                       3,
                                       subsample=(2, 2),
                                       activation='relu',
                                       W_regularizer=l2(weight_decay),
                                       border_mode='same',
                                       name='conv8_2')(net['conv8_1'])
        # Last Pool
        net['pool6'] = GlobalAveragePooling2D(name='pool6')(net['conv8_2'])
        # Prediction from conv4_3
        net['conv4_3_norm'] = Normalize(20,
                                        name='conv4_3_norm')(net['conv4_3'])
        num_priors = 3
        x = Convolution2D(num_priors * 4,
                          3,
                          3,
                          border_mode='same',
                          name='conv4_3_norm_mbox_loc')(net['conv4_3_norm'])
        net['conv4_3_norm_mbox_loc'] = x
        flatten = Flatten(name='conv4_3_norm_mbox_loc_flat')
        net['conv4_3_norm_mbox_loc_flat'] = flatten(
            net['conv4_3_norm_mbox_loc'])
        name = 'conv4_3_norm_mbox_conf'
        if num_classes != 21:
            name += '_{}'.format(num_classes)
        x = Convolution2D(num_priors * num_classes,
                          3,
                          3,
                          border_mode='same',
                          name=name)(net['conv4_3_norm'])
        net['conv4_3_norm_mbox_conf'] = x
        flatten = Flatten(name='conv4_3_norm_mbox_conf_flat')
        net['conv4_3_norm_mbox_conf_flat'] = flatten(
            net['conv4_3_norm_mbox_conf'])
        priorbox = PriorBox(img_size,
                            30.0,
                            aspect_ratios=[2],
                            variances=[0.1, 0.1, 0.2, 0.2],
                            name='conv4_3_norm_mbox_priorbox')
        net['conv4_3_norm_mbox_priorbox'] = priorbox(net['conv4_3_norm'])
        # Prediction from fc7
        num_priors = 6
        net['fc7_mbox_loc'] = Convolution2D(num_priors * 4,
                                            3,
                                            3,
                                            border_mode='same',
                                            name='fc7_mbox_loc')(net['fc7'])
        flatten = Flatten(name='fc7_mbox_loc_flat')
        net['fc7_mbox_loc_flat'] = flatten(net['fc7_mbox_loc'])
        name = 'fc7_mbox_conf'
        if num_classes != 21:
            name += '_{}'.format(num_classes)
        net['fc7_mbox_conf'] = Convolution2D(num_priors * num_classes,
                                             3,
                                             3,
                                             border_mode='same',
                                             name=name)(net['fc7'])
        flatten = Flatten(name='fc7_mbox_conf_flat')
        net['fc7_mbox_conf_flat'] = flatten(net['fc7_mbox_conf'])
        priorbox = PriorBox(img_size,
                            60.0,
                            max_size=114.0,
                            aspect_ratios=[2, 3],
                            variances=[0.1, 0.1, 0.2, 0.2],
                            name='fc7_mbox_priorbox')
        net['fc7_mbox_priorbox'] = priorbox(net['fc7'])
        # Prediction from conv6_2
        num_priors = 6
        x = Convolution2D(num_priors * 4,
                          3,
                          3,
                          border_mode='same',
                          name='conv6_2_mbox_loc')(net['conv6_2'])
        net['conv6_2_mbox_loc'] = x
        flatten = Flatten(name='conv6_2_mbox_loc_flat')
        net['conv6_2_mbox_loc_flat'] = flatten(net['conv6_2_mbox_loc'])
        name = 'conv6_2_mbox_conf'
        if num_classes != 21:
            name += '_{}'.format(num_classes)
        x = Convolution2D(num_priors * num_classes,
                          3,
                          3,
                          border_mode='same',
                          name=name)(net['conv6_2'])
        net['conv6_2_mbox_conf'] = x
        flatten = Flatten(name='conv6_2_mbox_conf_flat')
        net['conv6_2_mbox_conf_flat'] = flatten(net['conv6_2_mbox_conf'])
        priorbox = PriorBox(img_size,
                            114.0,
                            max_size=168.0,
                            aspect_ratios=[2, 3],
                            variances=[0.1, 0.1, 0.2, 0.2],
                            name='conv6_2_mbox_priorbox')
        net['conv6_2_mbox_priorbox'] = priorbox(net['conv6_2'])
        # Prediction from conv7_2
        num_priors = 6
        x = Convolution2D(num_priors * 4,
                          3,
                          3,
                          border_mode='same',
                          name='conv7_2_mbox_loc')(net['conv7_2'])
        net['conv7_2_mbox_loc'] = x
        flatten = Flatten(name='conv7_2_mbox_loc_flat')
        net['conv7_2_mbox_loc_flat'] = flatten(net['conv7_2_mbox_loc'])
        name = 'conv7_2_mbox_conf'
        if num_classes != 21:
            name += '_{}'.format(num_classes)
        x = Convolution2D(num_priors * num_classes,
                          3,
                          3,
                          border_mode='same',
                          name=name)(net['conv7_2'])
        net['conv7_2_mbox_conf'] = x
        flatten = Flatten(name='conv7_2_mbox_conf_flat')
        net['conv7_2_mbox_conf_flat'] = flatten(net['conv7_2_mbox_conf'])
        priorbox = PriorBox(img_size,
                            168.0,
                            max_size=222.0,
                            aspect_ratios=[2, 3],
                            variances=[0.1, 0.1, 0.2, 0.2],
                            name='conv7_2_mbox_priorbox')
        net['conv7_2_mbox_priorbox'] = priorbox(net['conv7_2'])
        # Prediction from conv8_2
        num_priors = 6
        x = Convolution2D(num_priors * 4,
                          3,
                          3,
                          border_mode='same',
                          name='conv8_2_mbox_loc')(net['conv8_2'])
        net['conv8_2_mbox_loc'] = x
        flatten = Flatten(name='conv8_2_mbox_loc_flat')
        net['conv8_2_mbox_loc_flat'] = flatten(net['conv8_2_mbox_loc'])
        name = 'conv8_2_mbox_conf'
        if num_classes != 21:
            name += '_{}'.format(num_classes)
        x = Convolution2D(num_priors * num_classes,
                          3,
                          3,
                          border_mode='same',
                          name=name)(net['conv8_2'])
        net['conv8_2_mbox_conf'] = x
        flatten = Flatten(name='conv8_2_mbox_conf_flat')
        net['conv8_2_mbox_conf_flat'] = flatten(net['conv8_2_mbox_conf'])
        priorbox = PriorBox(img_size,
                            222.0,
                            max_size=276.0,
                            aspect_ratios=[2, 3],
                            variances=[0.1, 0.1, 0.2, 0.2],
                            name='conv8_2_mbox_priorbox')
        net['conv8_2_mbox_priorbox'] = priorbox(net['conv8_2'])
        # Prediction from pool6
        num_priors = 6
        x = Dense(num_priors * 4, name='pool6_mbox_loc_flat')(net['pool6'])
        net['pool6_mbox_loc_flat'] = x
        name = 'pool6_mbox_conf_flat'
        if num_classes != 21:
            name += '_{}'.format(num_classes)
        x = Dense(num_priors * num_classes, name=name)(net['pool6'])
        net['pool6_mbox_conf_flat'] = x
        priorbox = PriorBox(img_size,
                            276.0,
                            max_size=330.0,
                            aspect_ratios=[2, 3],
                            variances=[0.1, 0.1, 0.2, 0.2],
                            name='pool6_mbox_priorbox')
        if K.image_dim_ordering() == 'tf':
            target_shape = (1, 1, 256)
        else:
            target_shape = (256, 1, 1)
        net['pool6_reshaped'] = Reshape(target_shape,
                                        name='pool6_reshaped')(net['pool6'])
        net['pool6_mbox_priorbox'] = priorbox(net['pool6_reshaped'])
        # Gather all predictions
        net['mbox_loc'] = merge([
            net['conv4_3_norm_mbox_loc_flat'], net['fc7_mbox_loc_flat'],
            net['conv6_2_mbox_loc_flat'], net['conv7_2_mbox_loc_flat'],
            net['conv8_2_mbox_loc_flat'], net['pool6_mbox_loc_flat']
        ],
                                mode='concat',
                                concat_axis=1,
                                name='mbox_loc')
        net['mbox_conf'] = merge([
            net['conv4_3_norm_mbox_conf_flat'], net['fc7_mbox_conf_flat'],
            net['conv6_2_mbox_conf_flat'], net['conv7_2_mbox_conf_flat'],
            net['conv8_2_mbox_conf_flat'], net['pool6_mbox_conf_flat']
        ],
                                 mode='concat',
                                 concat_axis=1,
                                 name='mbox_conf')
        net['mbox_priorbox'] = merge([
            net['conv4_3_norm_mbox_priorbox'], net['fc7_mbox_priorbox'],
            net['conv6_2_mbox_priorbox'], net['conv7_2_mbox_priorbox'],
            net['conv8_2_mbox_priorbox'], net['pool6_mbox_priorbox']
        ],
                                     mode='concat',
                                     concat_axis=1,
                                     name='mbox_priorbox')
        if hasattr(net['mbox_loc'], '_keras_shape'):
            num_boxes = net['mbox_loc']._keras_shape[-1] // 4
        elif hasattr(net['mbox_loc'], 'int_shape'):
            num_boxes = K.int_shape(net['mbox_loc'])[-1] // 4
        net['mbox_loc'] = Reshape((num_boxes, 4),
                                  name='mbox_loc_final')(net['mbox_loc'])
        net['mbox_conf'] = Reshape((num_boxes, num_classes),
                                   name='mbox_conf_logits')(net['mbox_conf'])
        net['mbox_conf'] = Activation('softmax',
                                      name='mbox_conf_final')(net['mbox_conf'])
        net['predictions'] = merge(
            [net['mbox_loc'], net['mbox_conf'], net['mbox_priorbox']],
            mode='concat',
            concat_axis=2,
            name='predictions')
        ssd = Model(net['input'], net['predictions'])
    else:
        ssd = Model(net['input'], net['pool5'])
    return ssd, net
예제 #30
0
#importing keras model and layers to construct LSTM model
from keras.models import Sequential
from keras.layers import Dense, Flatten, LSTM, Dropout

#initializing regression model
regressor = Sequential()

#adding layer(s) to model
regressor.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1], x_train.shape[2])))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units=50, return_sequences=True ))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units=33, return_sequences=True))


regressor.add(Flatten())
regressor.add(Dense(units=1))

#compiling the model with  mean_absolute_percentage_error and adam optimizer
regressor.compile(optimizer='adam', loss='mean_absolute_percentage_error')

#fitting model with training sets and validation set
history = regressor.fit(x_train, y_train, epochs = EPOCH, batch_size=BATCH_SIZE, validation_data=(x_test, y_test))
bm.save_val_loss_plot(history, PLACE+"_epoch_history.csv")

results = regressor.predict(x_test)

#constructing estimation dataframe
real_values = pd.DataFrame(index = test.index, 
                           data = bm.inverse_scale(sc, y_test.reshape(-1, 1)),
                           columns = ['Real'])
예제 #31
0
# layer1_model3.add(Convolution2D(layer1, 6,1293,
#                         border_mode='valid',
#                         input_shape=(1, 7, 1293)))
# layer1_model3.add(Activation('tanh'))
# layer1_model3.add(MaxPooling2D(pool_size=(nb_pool[0], nb_pool[1])))

model = merge([m1,m2,m3],mode='concat',concat_axis=2)

# model = Sequential()

# model.add(Merge([layer1_model2,layer1_model1,layer1_model3], mode='concat',concat_axis=2))
# model = model([l1_input,l1_input,l1_input])

model=MaxPooling2D(pool_size=(nb_pool[0], nb_pool[1]))(model)
# model.add(Dropout(0.25))
model=Flatten()(model) #平铺

# model.add(Dense(hidden1)) #Full connection 1:  1000
# model.add(Activation('tanh'))
# model.add(Dropout(0.5))


# model.add(Dense(hidden2)) #Full connection 2:   200
# model.add(Activation('tanh'))
# model.add(Dropout(0.5))

model=Dense(nb_classes)(model)
model=Activation('softmax')(model)
# model.summary()
model = Model(input=[l1_input], output=[model])
model.summary()
예제 #32
0
x = Conv2D(30, (5, 5), padding='same', activation='relu')(x)
print(x.shape)
x = MaxPooling2D((2,2))(x)
print(x.shape)
x = Conv2D(35, (5, 5), padding='same', activation='relu')(x)
print(x.shape)
x = MaxPooling2D((2,2))(x)
print(x.shape)
x = Conv2D(40, (5, 5), padding='same', activation='relu')(x)
print(x.shape)
x = MaxPooling2D((2,2))(x)
print(x.shape)
print('pre flatten shape')
shape = K.int_shape(x)
print(shape)
x = Flatten()(x)
print(x.get_shape())
# time.sleep(20)
# z_mean = Dense(latentDim, name='z_mean')(x)
# z_log_var = Dense(latentDim, name='z_log_var')(x)

# print(z_mean.shape)
# print(z_log_var.shape)

# z = Lambda(sampling, output_shape=(latentDim,), name='z')([z_mean, z_log_var])

encoder_model = Model(encoder_in, x)

#This ensures the model will be shared, including weights
encoded1 = encoder_model(image1)
encoded2 = encoder_model(image2)