shutil.copyfile(src, dst) # Sanity Check we divided and copied the imags properly print('total training cat images:', len(os.listdir(train_cats_dir))) print('total training dog images:', len(os.listdir(train_dogs_dir))) print('total validation cat images:', len(os.listdir(validation_cats_dir))) print('total validation dog images:', len(os.listdir(validation_dogs_dir))) print('total test cat images:', len(os.listdir(test_cats_dir))) print('total test dog images:', len(os.listdir(test_dogs_dir))) # Build Model model = models.Sequential() models.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3))) models.add(layers.MaxPooling2D(2, 2)) models.add(layers.Conv2D(64, (3, 3), activation='relu')) models.add(layers.MaxPooling2D(2, 2)) models.add(layers.Conv2D(128, (3, 3), activation='relu')) models.add(layers.MaxPooling2D(2, 2)) models.add(layers.Conv2D(128, (3, 3), activation='relu')) models.add(layers.MaxPooling2D(2, 2)) models.add(layers.Flatten()) models.add(layers.Dense(512, activation='relu')) models.add(layers.Dense(1, activation='sigmoid')) # Compile Network # RMSprop optimizer, as usual. Because you ended the network with a single sigmoid unit, # you’ll use binary crossentropy as the loss models.compile(loss='binary_crossentropy',
plt.grid(False) plt.imshow(train_image[i], cmap=plt.cm.binary) plt.xlabel(categories[train_label[i]]) plt.show() # 모델 생성 ResNet18 import keras from keras import layers from keras import Model from keras import optimizers input = layers.Input(shape = (256, 256, 3), dtype='float32') model = layers.Conv2D(64, (7,7), strides=2, padding='same', name='block1_conv1')(input) model = layers.MaxPooling2D(pool_size=(3, 3), strides=2, padding='same')(model) model_shortcut = model model = layers.Conv2D(64, (3,3), activation = 'relu', padding='same', name='block2_conv1')(model) model = layers.Conv2D(64, (3,3), activation = 'relu', padding='same', name='block2_conv2')(model) model = layers.Add()([model, model_shortcut]) model = layers.Conv2D(64, (3,3), activation = 'relu', padding='same', name='block2_conv3')(model) model = layers.Conv2D(64, (3,3), activation = 'relu', padding='same', name='block2_conv4')(model) model = layers.Add()([model, model_shortcut]) model = layers.Conv2D(128, (3,3), activation = 'relu', strides = 2, padding='same', name='block3_conv1')(model) model = layers.Conv2D(128, (3,3), activation = 'relu', padding='same', name='block3_conv2')(model) model_shortcut = model
target_size=(IMAGE_DIM_X, IMAGE_DIM_Y), batch_size=20, class_mode='categorical') for data_batch, labels_batch in train_generator: print("Data batch shape:", data_batch.shape) print("Labels batch shape:", labels_batch.shape) break #Build covnet model = models.Sequential() model.add( layers.Conv2D(32, (3, 3), activation='relu', input_shape=(IMAGE_DIM_X, IMAGE_DIM_Y, 3))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(128, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(128, (3, 3), activation='relu')) model.add(layers.GlobalAveragePooling2D()) #model.add(layers.Flatten()) model.add(layers.Dense(512, activation='relu')) model.add(layers.Dense(len(POSSIBLE_SHAPES), activation='softmax')) print(model.summary()) model.compile( loss='mean_squared_error', #mean_squared_error optimizer=optimizers.RMSprop(lr=2e-5),
def ResNet(stack_fn, preact, use_bias, model_name='resnet', include_top=True, weights=None, input_tensor=None, input_shape=None, pooling=None, nclass=1000, **kwargs): """Instantiates the ResNet, ResNetV2, and ResNeXt architecture. Optionally loads weights pre-trained on ImageNet. Note that the data format convention used by the model is the one specified in your Keras config at `~/.keras/keras.json`. # Arguments stack_fn: a function that returns output tensor for the stacked residual blocks. preact: whether to use pre-activation or not (True for ResNetV2, False for ResNet and ResNeXt). use_bias: whether to use biases for convolutional layers or not (True for ResNet and ResNetV2, False for ResNeXt). model_name: string, model name. include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 224)` (with `channels_first` data format). It should have exactly 3 inputs channels. pooling: optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ # Determine proper input shape # input_shape = input_shape if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1 x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)), name='conv1_pad')(img_input) x = layers.Conv2D(64, 7, strides=2, use_bias=use_bias, name='conv1_conv')(x) if preact is False: x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1_bn')(x) x = layers.Activation('relu', name='conv1_relu')(x) x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name='pool1_pad')(x) x = layers.MaxPooling2D(3, strides=2, name='pool1_pool')(x) x = stack_fn(x) if preact is True: x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='post_bn')(x) x = layers.Activation('relu', name='post_relu')(x) if include_top: x = layers.GlobalAveragePooling2D(name='avg_pool')(x) x = layers.Dense(nclass, activation='softmax', name='probs')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D(name='avg_pool')(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D(name='max_pool')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = keras_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = models.Model(inputs, x, name=model_name) # Load weights. if weights is not None: model.load_weights(weights) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model
def VGG16(img_input): # Block 1 # 512,512,3 -> 512,512,64 x = layers.Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = layers.Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) feat1 = x # 512,512,64 -> 256,256,64 x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 # 256,256,64 -> 256,256,128 x = layers.Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = layers.Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) feat2 = x # 256,256,128 -> 128,128,128 x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 # 128,128,128 -> 128,128,256 x = layers.Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = layers.Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = layers.Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) feat3 = x # 128,128,256 -> 64,64,256 x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 # 64,64,256 -> 64,64,512 x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) feat4 = x # 64,64,512 -> 32,32,512 x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 # 32,32,512 -> 32,32,512 x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) feat5 = x return feat1, feat2, feat3, feat4, feat5
def fcn_Vgg16_32s(feature_map, weight_decay=0., batch_momentum=0.9, batch_shape=None, classes=0): '''Builds the computation graph of Region Proposal Network. feature_map: Contextual Tensor [batch, num_classes, width, depth] Returns: rpn_logits: [batch, H, W, 2] Anchor classifier logits (before softmax) rpn_probs: [batch, W, W, 2] Anchor classifier probabilities. rpn_bbox: [batch, H, W, (dy, dx, log(dh), log(dw))] Deltas to be applied to anchors. ''' print(' feature map shape is ', type(feature_map), feature_map.shape, 'classes :', feature_map.shape[-1]) # TODO: Assert proper shape of input [batch_size, width, height, num_classes] # TODO: check if stride of 2 causes alignment issues if the featuremap is not even. # if batch_shape: # img_input = Input(batch_shape=batch_shape) # image_size = batch_shape[1:3] # else: # img_input = Input(shape=input_shape) # image_size = input_shape[0:2] # Block 1 x0 = KL.Conv2D(64, (3, 3), activation='relu', padding='same', name='fcn_block1_conv1', kernel_regularizer=l2(weight_decay))(feature_map) print(' FCN Block 10 shape is : ', x0.get_shape()) x1 = KL.Conv2D(64, (3, 3), activation='relu', padding='same', name='fcn_block1_conv2', kernel_regularizer=l2(weight_decay))(x0) print(' FCN Block 11 shape is : ', x1.get_shape()) x2 = KL.MaxPooling2D((2, 2), strides=(2, 2), name='fcn_block1_pool')(x1) print(' FCN Block 12 shape is : ', x2.get_shape()) # Block 2 # x = KL.Conv2D(128, (3, 3), activation='relu', padding='same', name='fcn_block2_conv1', kernel_regularizer=l2(weight_decay))(x) # x = KL.Conv2D(128, (3, 3), activation='relu', padding='same', name='fcn_block2_conv2', kernel_regularizer=l2(weight_decay))(x) # x = KL.MaxPooling2D((2, 2), strides=(2, 2), name='fcn_block2_pool')(x) # Block 3 # x = KL.Conv2D(256, (3, 3), activation='relu', padding='same', name='fcn_block3_conv1', kernel_regularizer=l2(weight_decay))(x) # x = KL.Conv2D(256, (3, 3), activation='relu', padding='same', name='fcn_block3_conv2', kernel_regularizer=l2(weight_decay))(x) # x = KL.Conv2D(256, (3, 3), activation='relu', padding='same', name='fcn_block3_conv3', kernel_regularizer=l2(weight_decay))(x) # x = KL.MaxPooling2D((2, 2), strides=(2, 2), name='fcn_block3_pool')(x) # Block 4 # x = KL.Conv2D(512, (3, 3), activation='relu', padding='same', name='fcn_block4_conv1', kernel_regularizer=l2(weight_decay))(x) # x = KL.Conv2D(512, (3, 3), activation='relu', padding='same', name='fcn_block4_conv2', kernel_regularizer=l2(weight_decay))(x) # x = KL.Conv2D(512, (3, 3), activation='relu', padding='same', name='fcn_block4_conv3', kernel_regularizer=l2(weight_decay))(x) # x = KL.MaxPooling2D((2, 2), strides=(2, 2), name='fcn_block4_pool')(x) # Block 5 # x = KL.Conv2D(512, (3, 3), activation='relu', padding='same', name='fcn_block5_conv1', kernel_regularizer=l2(weight_decay))(x) # x = KL.Conv2D(512, (3, 3), activation='relu', padding='same', name='fcn_block5_conv2', kernel_regularizer=l2(weight_decay))(x) # x = KL.Conv2D(512, (3, 3), activation='relu', padding='same', name='fcn_block5_conv3', kernel_regularizer=l2(weight_decay))(x) # x = KL.MaxPooling2D((2, 2), strides=(2, 2), name='fcn_block5_pool')(x) # Convolutional layers transfered from fully-connected layers # changed from 4096 to 2048 - reduction of weights from 42,752,644 to x = KL.Conv2D(2048, (7, 7), activation='relu', padding='same', name='fcn_fc1', kernel_regularizer=l2(weight_decay))(x2) x = KL.Dropout(0.5)(x) x = KL.Conv2D(2048, (1, 1), activation='relu', padding='same', name='fcn_fc2', kernel_regularizer=l2(weight_decay))(x) x = KL.Dropout(0.5)(x) #classifying layer x = KL.Conv2D(classes, (1, 1), kernel_initializer='he_normal', activation='linear', padding='valid', strides=(1, 1), kernel_regularizer=l2(weight_decay), name='fcn_classify')(x) output = BilinearUpSampling2D(size=(8, 8), name='fcn_bilinear')(x) return [x0, x1, x2, output]
copy_file(True, original_train_dir, train_cats_dir, range_train) copy_file(True, original_train_dir, validation_cats_dir, range_validation) copy_file(True, original_test_dir, test_cats_dir, range_test) copy_file(False, original_train_dir, train_dogs_dir, range_train) copy_file(False, original_train_dir, validation_dogs_dir, range_validation) copy_file(False, original_test_dir, test_dogs_dir, range_test) """ # endregion # region model model = models.Sequential() model.add( layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3))) model.add(layers.MaxPooling2D((4, 4))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((4, 4))) model.add(layers.Flatten()) model.add(layers.Dropout(0.5)) model.add(layers.Dense(512, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc']) # endregion # region generators train_datagen = idg(rescale=1. / 255,
def dense_graph_simple_long(input_image, stage5=True, train_bn=True, net_name="dense_res_l"): # Stage 1 x = KL.Conv2D(8, (7, 7), strides=(2, 2), name=net_name + 'conv1', use_bias=True, padding="same")(input_image) x = BatchNorm(name=net_name + 'bn_conv1')(x, training=train_bn) x = KL.Activation('relu')(x) C1 = x = KL.MaxPooling2D((7, 7), strides=(2, 2), padding="same")(x) # Stage 2 x = connectedConv(x, 7, [8, 8, 32], stage=2, block='a', strides=(1, 1), train_bn=train_bn, net_name=net_name) x = KL.Dropout(rate=0.2, name=net_name + "dense_dropout2")(x) C2 = x = connectedIdentity(x, 7, [8, 8, 32], stage=2, block='b', strides=(1, 1), train_bn=train_bn, net_name=net_name) # Stage 3 x = connectedConv(x, 7, [16, 16, 64], stage=3, block='a', train_bn=train_bn, net_name=net_name) x = KL.Dropout(rate=0.2, name=net_name + "dense_dropout3")(x) C3 = x = connectedIdentity(x, 7, [16, 16, 64], stage=3, block='b', strides=(1, 1), train_bn=train_bn, net_name=net_name) # Stage 4 x = connectedConv(x, 7, [32, 32, 128], stage=4, block='a', train_bn=train_bn, net_name=net_name) x = KL.Dropout(rate=0.2, name=net_name + "dense_dropout4")(x) C4 = x = connectedIdentity(x, 7, [32, 32, 128], stage=4, block='b', train_bn=train_bn, net_name=net_name) # Stage 5 if stage5: x = connectedIdentity(x, 7, [64, 64, 256], stage=5, block='a', train_bn=train_bn, net_name=net_name) x = KL.Dropout(rate=0.2, name=net_name + "dense_dropout5")(x) C5 = x = connectedIdentity(x, 7, [64, 64, 256], stage=5, block='b', train_bn=train_bn, net_name=net_name) else: C5 = None return [C1, C2, C3, C4, C5]
shuffle=False, class_mode='categorical') # In[3]: from keras.layers import BatchNormalization network = models.Sequential() network.add( layers.Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3), kernel_regularizer=regularizers.l2(0.01))) network.add(layers.BatchNormalization()) network.add(layers.MaxPooling2D((2, 2))) network.add( layers.Conv2D(64, (3, 3), activation='relu', kernel_regularizer=regularizers.l2(0.01))) network.add(layers.BatchNormalization()) network.add(layers.MaxPooling2D((2, 2))) network.add( layers.Conv2D(128, (3, 3), activation='relu', kernel_regularizer=regularizers.l2(0.01))) network.add(layers.BatchNormalization()) network.add(layers.MaxPooling2D((2, 2)))
def AutoEncoder(input_shape=( 256, 256, )): encoder = Sequential() encoder.add(L.Reshape(( 256, 256, 1, ), input_shape=( 256, 256, ))) encoder.add( L.Conv2D(64, (3, 3), strides=2, padding='same', input_shape=input_shape)) encoder.add(L.BatchNormalization()) encoder.add(L.Activation('relu')) encoder.add(L.MaxPooling2D((2, 2))) encoder.add(L.Conv2D(128, (3, 3), strides=2, padding='same')) encoder.add(L.BatchNormalization()) encoder.add(L.Activation('relu')) encoder.add(L.MaxPooling2D((2, 2))) encoder.add(L.Conv2D(256, (3, 3), strides=2, padding='same')) encoder.add(L.BatchNormalization()) encoder.add(L.Activation('relu')) encoder.add(L.MaxPooling2D((2, 2))) encoder.add(L.Conv2D(512, (3, 3), strides=2, padding='same')) encoder.add(L.BatchNormalization()) encoder.add(L.Activation('relu')) encoder.add(L.MaxPooling2D((2, 2))) encoder.add(L.Conv2D(1024, (3, 3), strides=2, padding='same')) encoder.add(L.BatchNormalization()) encoder.add(L.Activation('relu', name='unflattened')) encoder.add(L.Flatten(name='flattened')) unflattened_shape = encoder.get_layer('unflattened').output_shape[1:] flattened_shape = encoder.get_layer('flattened').output_shape[1:] decoder = Sequential() decoder.add( L.Reshape(target_shape=unflattened_shape, input_shape=flattened_shape)) decoder.add(L.Conv2DTranspose(512, (2, 2), strides=2, padding='same')) decoder.add(L.Activation('relu')) decoder.add(L.Conv2DTranspose(256, (2, 2), strides=2, padding='same')) decoder.add(L.Activation('relu')) decoder.add(L.Conv2DTranspose(128, (2, 2), strides=2, padding='same')) decoder.add(L.Activation('relu')) decoder.add(L.Conv2DTranspose(64, (2, 2), strides=2, padding='same')) decoder.add(L.Activation('relu')) decoder.add(L.Conv2DTranspose(32, (2, 2), strides=2, padding='same')) decoder.add(L.Activation('relu')) decoder.add(L.Conv2DTranspose(16, (2, 2), strides=2, padding='same')) decoder.add(L.Activation('relu')) decoder.add(L.Conv2DTranspose(8, (2, 2), strides=2, padding='same')) decoder.add(L.Activation('relu')) decoder.add(L.Conv2DTranspose(1, (2, 2), strides=2, padding='same')) decoder.add(L.Activation('sigmoid')) autoencoder = Sequential() autoencoder.add(encoder) autoencoder.add(decoder) return autoencoder
def nn_base(input_tensor=None, trainable=False): input_shape = (None, None, 3) if input_tensor is None: img_input = Layers.Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor bn_axis = 3 # Block 1 x = Layers.Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = Layers.Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = Layers.MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = Layers.Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Layers.Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = Layers.MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = Layers.Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Layers.Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Layers.Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = Layers.MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = Layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = Layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = Layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = Layers.MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = Layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = Layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = Layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) # x = Layers.MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) return x
def VAE(optimizer, latent_dim=512): encoder_input = L.Input(shape=(256, 256, 3), name='encoder_input') en = L.Conv2D(64, (3, 3), padding='same')(encoder_input) en = L.BatchNormalization()(en) en = L.Activation('relu')(en) en = L.MaxPooling2D((2, 2))(en) en = L.Conv2D(64, (3, 3), padding='same')(en) en = L.BatchNormalization()(en) en = L.Activation('relu')(en) en = L.MaxPooling2D((2, 2))(en) en = L.Conv2D(128, (3, 3), padding='same')(en) en = L.BatchNormalization()(en) en = L.Activation('relu')(en) en = L.MaxPooling2D((2, 2))(en) en = L.Conv2D(128, (3, 3), padding='same')(en) en = L.BatchNormalization()(en) en = L.Activation('relu')(en) en = L.MaxPooling2D((2, 2))(en) en = L.Conv2D(256, (3, 3), padding='same')(en) en = L.BatchNormalization()(en) en = L.Activation('relu')(en) en = L.MaxPooling2D((2, 2))(en) en = L.Conv2D(512, (3, 3), padding='same')(en) en = L.BatchNormalization()(en) en = L.Activation('relu')(en) en = L.MaxPooling2D((2, 2))(en) en = L.Conv2D(512, (3, 3), padding='same')(en) en = L.BatchNormalization()(en) en = L.Activation('relu')(en) en = L.MaxPooling2D((2, 2))(en) unflatted_shape = K.int_shape(en) flatted = L.Flatten(name='encoder_output')(en) z_mean = L.Dense(latent_dim, name='z_mean')(flatted) z_log_var = L.Dense(latent_dim, name='z_log_var')(flatted) z = L.Lambda(_sampling, output_shape=(latent_dim, ), name='z')([z_mean, z_log_var]) decoder_input = L.Input(shape=(latent_dim, ), name='decoder_input') de = L.Dense(np.prod(unflatted_shape[1:]))(decoder_input) de = L.Reshape((unflatted_shape[1:]))(de) de = L.Conv2DTranspose(512, (2, 2), strides=2, padding='same')(de) de = L.Conv2D(512, (3, 3), padding='same')(de) de = L.BatchNormalization()(de) de = L.Activation('relu')(de) de = L.Conv2DTranspose(512, (2, 2), strides=2, padding='same')(de) de = L.Conv2D(512, (3, 3), padding='same')(de) de = L.BatchNormalization()(de) de = L.Activation('relu')(de) de = L.Conv2DTranspose(256, (2, 2), strides=2, padding='same')(de) de = L.Conv2D(256, (3, 3), padding='same')(de) de = L.BatchNormalization()(de) de = L.Activation('relu')(de) de = L.Conv2DTranspose(128, (2, 2), strides=2, padding='same')(de) de = L.Conv2D(128, (3, 3), padding='same')(de) de = L.BatchNormalization()(de) de = L.Activation('relu')(de) de = L.Conv2DTranspose(128, (2, 2), strides=2, padding='same')(de) de = L.Conv2D(128, (3, 3), padding='same')(de) de = L.BatchNormalization()(de) de = L.Activation('relu')(de) de = L.Conv2DTranspose(64, (2, 2), strides=2, padding='same')(de) de = L.Conv2D(64, (3, 3), padding='same')(de) de = L.BatchNormalization()(de) de = L.Activation('relu')(de) de = L.Conv2DTranspose(3, (2, 2), strides=2, padding='same')(de) de = L.Conv2D((3, 3), padding='same')(de) decoded = L.Activation('sigmoid')(de) encoder = Model(encoder_input, [z_mean, z_log_var, z], name='encoder') decoder = Model(decoder_input, decoded, name='decoder') output = decoder(encoder(encoder_input)[2]) def vae_loss(y_true, y_pred, z_mean=z_mean, z_log_var=z_log_var): reconstruction_loss = (256 * 256) * K.mean( keras.losses.mse(y_true, y_pred), axis=[1, 2]) kl_loss = -0.5 * K.sum( 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=1) vae_loss = K.mean(reconstruction_loss + kl_loss) return vae_loss vae = Model(encoder_input, output) vae.compile(optimizer=optimizer, loss=vae_loss) return vae
rows = xs.shape[0] cursor = 0 while True: start = cursor cursor += batch_size if(cursor > rows): cursor = 0 bxs = xs[start:cursor,:,:,:] bys = ys[start:cursor,0] yield( (xs[start:cursor,:,:,:], ys[start:cursor,0]) ) input_tensor = Input(shape=(w,h,1)) # start with max-pool to envelop the UT-data ib = layers.MaxPooling2D(pool_size=(window,1), padding='valid' )(input_tensor) # MaxPooling1D would work, but we may want to pool adjacent A-scans in the future #build the network cb = layers.Conv2D(96,3,padding='same', activation='relu')(ib) cb = layers.Conv2D(64,3,padding='same', activation='relu')(cb) cb = layers.MaxPooling2D( (2,8), padding='same')(cb) cb = layers.Conv2D(48,3,padding='same', activation='relu')(cb) cb = layers.Conv2D(32,3,padding='same', activation='relu')(cb) cb = layers.MaxPooling2D( (3,4), padding='same' )(cb) cb = layers.Flatten()(cb) cb = layers.Dense(14, activation='relu', name='RNN')(cb) iscrack = layers.Dense(1, activation='sigmoid', name='output')(cb) model = Model(input_tensor, iscrack)
img_height=img_height, batch_size=batch_size, enhance=data_enhance) # # 创建模型 # In[5]: #创建模型 model = models.Sequential() model.add( layers.Conv2D(32, (3, 3), activation='relu', input_shape=(img_height, img_width, 3), name='conv2d_1')) model.add(layers.MaxPooling2D((2, 2), name='max_pooling2d_1')) model.add(layers.Conv2D(64, (3, 3), activation='relu', name='conv2d_2')) model.add(layers.MaxPooling2D((2, 2), name='max_pooling2d_2')) model.add(layers.Conv2D(128, (3, 3), activation='relu', name='conv2d_3')) model.add(layers.MaxPooling2D((2, 2), name='max_pooling2d_3')) model.add(layers.Conv2D(128, (3, 3), activation='relu', name='conv2d_4')) model.add(layers.MaxPooling2D((2, 2), name='max_pooling2d_4')) model.add(layers.Flatten(name='flatten_1')) model.add(layers.Dense(512, activation='relu', name='dense_1')) model.add(layers.Dense(1, activation='sigmoid', name='dense_2')) #打印模型 model.summary() #模型编译 model.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc'])
def _build_custom(self): trainable = not self._fine_tune pool_eye_in = layers.MaxPooling2D(pool_size=2, padding="same") # Encoder layers. #conv_e1 = layers.Conv2D(48, (11, 11), strides=(2, 2), activation="relu", # padding="same", # kernel_regularizer=self._l2, trainable=trainable) #norm_e1 = layers.BatchNormalization(trainable=trainable) conv_e2 = layers.Conv2D(16, (3, 3), activation="relu", padding="same", kernel_regularizer=self._l2, trainable=trainable) pool_e2 = layers.MaxPooling2D(pool_size=2, padding="same") norm_e2 = layers.BatchNormalization(trainable=trainable) conv_e3 = layers.Conv2D(8, (3, 3), activation="relu", padding="same", kernel_regularizer=self._l2, trainable=trainable) pool_e3 = layers.MaxPooling2D(pool_size=2, padding="same") norm_e3 = layers.BatchNormalization(trainable=trainable) conv_e4 = layers.Conv2D(8, (3, 3), activation="relu", padding="same", kernel_regularizer=self._l2, trainable=trainable) pool_e4 = layers.MaxPooling2D(pool_size=2, padding="same") norm_e4 = layers.BatchNormalization(trainable=trainable) # Decoder layers. conv_d1 = layers.Conv2D(8, (3, 3), activation="relu", padding="same", kernel_regularizer=self._l2, trainable=trainable) upsample_d1 = layers.UpSampling2D(size=2) norm_d1 = layers.BatchNormalization(trainable=trainable) conv_d2 = layers.Conv2D(8, (3, 3), activation="relu", padding="same", kernel_regularizer=self._l2, trainable=trainable) upsample_d2 = layers.UpSampling2D(size=2) norm_d2 = layers.BatchNormalization(trainable=trainable) conv_d3 = layers.Conv2D(16, (3, 3), activation="relu", padding="same", kernel_regularizer=self._l2, trainable=trainable) upsample_d3 = layers.UpSampling2D(size=2) norm_d3 = layers.BatchNormalization(trainable=trainable) conv_d4 = layers.Conv2D(1, (3, 3), padding="same", kernel_regularizer=self._l2, trainable=trainable, name="decode") #norm_d4 = layers.BatchNormalization(trainable=trainable) #upsample_d4 = layers.UpSampling2D(size=2) #conv_d4 = layers.Conv2D(48, (11, 11), activation="relu", # padding="same", # kernel_regularizer=self._l2, trainable=trainable, # name="decode") self._small_eye = pool_eye_in(self._left_eye_node) # Build the autoencoder network. #enc1 = conv_e1(self._small_eye) #enc2 = norm_e1(enc1) enc3 = conv_e2(self._small_eye) enc4 = pool_e2(enc3) enc5 = norm_e2(enc4) enc6 = conv_e3(enc5) enc7 = pool_e3(enc6) enc8 = norm_e3(enc7) enc9 = conv_e4(enc8) enc10 = pool_e4(enc9) enc11 = norm_e4(enc10) dec1 = conv_d1(enc11) dec2 = upsample_d1(dec1) dec3 = norm_d1(dec2) dec4 = conv_d2(dec3) dec5 = upsample_d2(dec4) dec6 = norm_d2(dec5) dec7 = conv_d3(dec6) dec8 = upsample_d3(dec7) dec9 = norm_d3(dec8) dec10 = conv_d4(dec9) # Build the gaze prediction pathway. self.__encoded = layers.Flatten(name="encode")(enc11) gaze_dense1 = layers.Dense(128, activation="relu", kernel_regularizer=self._l2, trainable=trainable)(self.__encoded) gaze_dense2 = layers.Dense(128, activation="relu", kernel_regularizer=self._l2, trainable=trainable)(gaze_dense1) gaze_pred = layers.Dense(2, kernel_regularizer=self._l2, trainable=trainable, name="dots")(gaze_dense2) # The outputs are the decoded input and the gaze prediction. return dec10, gaze_pred, self.__encoded
def CNN_MNIST(x_train, y_train, x_test, y_test, conv2d = False, dense = False, epoch = 100, batch_size = 1024 ): #Data Reshaping x_train = x_train.copy().reshape(*(x_train.shape),1) y_train = to_categorical(y_train.copy()) x_test = x_test.copy().reshape(*(x_test.shape),1) y_test = to_categorical(y_test.copy()) #Model building network = models.Sequential() #convolutional layer if conv2d == False: #to simulate a convolutional layer network.add(layers.Conv2D(10, kernel_size=(4, 4), strides=1, bias = 0, input_shape=(a,b,1), kernel_constraint = non_neg(), activation='relu', )) elif conv2d != False: #to use weights that are encoded in the array network.add(layers.Conv2D(10, trainable = False, kernel_initializer = conv2d_weightmap, kernel_size=(4, 4), strides=1, bias = 0, input_shape=(a,b,1), kernel_constraint = CustomizedConstraint(axis = [0,1]), activation='relu', )) #max-pooling network.add(layers.MaxPooling2D(pool_size=(5,5))) #flattening network.add(layers.Flatten()) #fully connected layer if dense == False: #to simulate a convolutional layer network.add(layers.Dense(10, kernel_constraint = CustomizedConstraint(axis = [0,1]), bias = 0, activation='softmax')) elif dense != False: #to use weights that are encoded in the array network.add(layers.Dense(10, trainable = False, kernel_initializer = dense_weightmap, bias = 0, activation='softmax', )) network.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) model_log = network.fit(x_train, y_train, batch_size=batch_size, epochs=epoch, verbose=1, validation_data=(x_test, y_test)) return network, model_log
def construct_graph(self, input_tensor, stage5=True): assert self.input_tensor is not None, "input_tensor can not be none!" # Stage 1 x = KL.ZeroPadding2D((3, 3))(input_tensor) x = KL.Conv2D(64, (7, 7), strides=(2, 2), name='conv1', use_bias=True)(x) x = BatchNorm(axis=3, name='bn_conv1')(x) x = KL.Activation('relu')(x) C1 = x = KL.MaxPooling2D((3, 3), strides=(2, 2), padding="same")(x) # Stage 2 x = self.conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = self.identity_block(x, 3, [64, 64, 256], stage=2, block='b') C2 = x = self.identity_block(x, 3, [64, 64, 256], stage=2, block='c') # Stage 3 x = self.conv_block(x, 3, [128, 128, 512], stage=3, block='a') x = self.identity_block(x, 3, [128, 128, 512], stage=3, block='b') x = self.identity_block(x, 3, [128, 128, 512], stage=3, block='c') C3 = x = self.identity_block(x, 3, [128, 128, 512], stage=3, block='d') # Stage 4 x = self.conv_block(x, 3, [256, 256, 1024], stage=4, block='a') block_count = {"resnet50": 5, "resnet101": 22}[self.architecture] for i in range(block_count): x = self.identity_block(x, 3, [256, 256, 1024], stage=4, block=chr(98 + i)) C4 = x # Stage 5 x = self.conv_block(x, 3, [256, 256, 256], stage=5, block='a', dilated=2, strides=(1, 1)) x = self.identity_block(x, 3, [256, 256, 256], stage=5, block='b', dilated=2) C5 = x = self.identity_block(x, 3, [256, 256, 256], stage=5, block='c', dilated=2) # Stage 6 x = self.conv_block(x, 3, [256, 256, 256], stage=6, block='a', dilated=2, strides=(1, 1)) x = self.identity_block(x, 3, [256, 256, 256], stage=6, block='b', dilated=2) C6 = x = self.identity_block(x, 3, [256, 256, 256], stage=6, block='c', dilated=2) P6 = KL.Conv2D(256, (1, 1), name='fpn_c6p6')(C6) P5 = KL.Add(name="fpn_p5add")( [P6, KL.Conv2D(256, (1, 1), name='fpn_c5p5')(C5)]) P4 = KL.Add(name="fpn_p4add")( [P5, KL.Conv2D(256, (1, 1), name='fpn_c4p4')(C4)]) P3 = KL.Add(name="fpn_p3add")([ KL.UpSampling2D(size=(2, 2), name="fpn_p4upsampled")(P4), KL.Conv2D(256, (1, 1), name='fpn_c3p3')(C3) ]) P2 = KL.Add(name="fpn_p2add")([ KL.UpSampling2D(size=(2, 2), name="fpn_p3upsampled")(P3), KL.Conv2D(256, (1, 1), name='fpn_c2p2')(C2) ]) # Attach 3x3 conv to all P layers to get the final feature maps. P2 = KL.Conv2D(256, (3, 3), padding="SAME", name="fpn_p2")(P2) P3 = KL.Conv2D(256, (3, 3), padding="SAME", name="fpn_p3")(P3) P4 = KL.Conv2D(256, (3, 3), padding="SAME", name="fpn_p4")(P4) P5 = KL.Conv2D(256, (3, 3), padding="SAME", name="fpn_p5")(P5) # P6 is used for the 5th anchor scale in RPN. Generated by # subsampling from P5 with stride of 2. P6 = KL.Conv2D(256, (3, 3), padding="SAME", name="fpn_p6")(P6) self.output_layers = [P2, P3, P4, P5, P6]
import keras from keras import layers #残差连接可以稍微解决梯度消失和标识瓶颈问题 #假设有一个4维的张量x x = ... #以下是恒等残差连接 Identity Residual Connection y = layers.Conv2D(128, 3, activation='relu', padding='same')(x) y = layers.Conv2D(128, 3, activation='relu', padding='same')(y) y = layers.Conv2D(128, 3, activation='relu', padding='same')(y) y = layers.add([y, x]) #以下是线性残差连接 Linear Residual Connection y = layers.Conv2D(128, 3, activation='relu', padding='same')(x) y = layers.Conv2D(128, 3, activation='relu', padding='same')(y) y = layers.MaxPooling2D(2, strides=2)(y) residual = layers.Conv2D(128, 1, strides=2, padding='same')(x) y = layers.add([y, residual])
def cnn2x3maxpool(img_shape, conv_filters, kernel_size, pool_size, time_dense_size, dropout, rnn_size, act_conv, act_dense, n_classes, max_string_len, training=True): input_data = layers.Input(name='the_input', shape=img_shape) inner = layers.Conv2D(conv_filters, kernel_size, padding='same', activation=act_conv, name='conv1_1')(input_data) inner = layers.Conv2D(conv_filters, kernel_size, padding='same', activation=act_conv, name='conv1_2')(inner) inner = layers.MaxPooling2D(pool_size=(pool_size, pool_size), name='max1')(inner) inner = layers.Conv2D(conv_filters * 2, kernel_size, padding='same', activation=act_conv, name='conv2_1')(inner) inner = layers.Conv2D(conv_filters * 2, kernel_size, padding='same', activation=act_conv, name='conv2_2')(inner) inner = layers.MaxPooling2D(pool_size=(pool_size, pool_size), name='max2')(inner) inner = layers.Conv2D(conv_filters * 4, kernel_size, padding='same', activation=act_conv, name='conv3_1')(inner) inner = layers.Conv2D(conv_filters * 4, kernel_size, padding='same', activation=act_conv, name='conv3_2')(inner) conv_to_rnn_dims = (int(inner.shape[1]), int(inner.shape[2] * inner.shape[3])) inner = layers.Reshape(target_shape=conv_to_rnn_dims, name='reshape')(inner) # dropout1 = layers.Dropout(dropout, name='dropout1')(inner) # cuts down input size going into RNN: inner_dense = layers.Dense(time_dense_size, activation=act_dense, name='time_dense')(inner) inner_dense = layers.BatchNormalization()(inner_dense) # Two layers of bidirecitonal GRUs # GRU seems to work as well, if not better than GRU: gru_1 = layers.GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru1_a')(inner_dense) gru_1b = layers.GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru1_b')(inner_dense) gru1_merged = layers.merge([gru_1, gru_1b], mode='sum') gru1_merged = layers.BatchNormalization()(gru1_merged) gru_2 = layers.GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru2_a')(gru1_merged) gru_2b = layers.GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru2_b')(gru1_merged) gru_merged = layers.merge([gru_2, gru_2b], mode='concat') gru_merged = layers.BatchNormalization()(gru_merged) # dropout2 = layers.Dropout(dropout, name='dropout2')(gru_merged) # transforms RNN output to character activations: 0-9 + '/' + ' ' + null dense = layers.Dense(n_classes, kernel_initializer='he_normal', name='class_dense')(gru_merged) y_pred = layers.Activation('softmax', name='softmax')(dense) # show model # clipnorm seems to speeds up convergence sgd = SGD(lr=0.02, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5) # loss with CTC if training: # make layers for output labels = layers.Input(name='the_labels', shape=[max_string_len], dtype='float64') input_length = layers.Input(name='input_length', shape=[1], dtype='int64') label_length = layers.Input(name='label_length', shape=[1], dtype='int64') # Keras doesn't currently support loss funcs with extra parameters # so CTC loss is implemented in a lambda layer loss_out = layers.Lambda(ctc_lambda_func, output_shape=(1, ), name='ctc')([ y_pred, labels, input_length, label_length ]) model = Model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out) model.compile(loss={ 'ctc': lambda y_true, y_pred: y_pred }, optimizer=sgd) else: model = Model(input_data, y_pred) model.summary() return model
'resnet': ResNet50} __all__ = ['efficientnetb0', 'efficientnetb1', 'efficientnetb2', 'efficientnetb3', 'efficientnetb4', 'efficientnetb5', 'efficientnetb6', 'efficientnetb7', 'resnet'] _change_effi_dc = {'block2a_dwconv': 6, 'block3a_dwconv': 1, 'block4a_dwconv': 1, 'block6a_dwconv': 1} _change_effi_co = {'stem_conv': 48} _change_resnet = {'ZeroPadding2D': kl.ZeroPadding2D(padding = (0, 0), name = 'conv1_pad'), 'pool1_pool': kl.MaxPooling2D(1, name = 'pool1_pool'), 'conv3_block1_1_conv': kl.Conv2D(filters = 128, kernel_size = (3, 3), strides = (1, 1), padding = 'same'), 'conv4_block1_1_conv': kl.Conv2D(filters = 256, kernel_size = (3, 3), strides = (1, 1), padding = 'same'), 'conv5_block1_1_conv': kl.Conv2D(filters = 512, kernel_size = (3, 3), strides = (1, 1), padding = 'same')} class BaseModel(): #used to defined the base model
class_mode='binary') # Flow validation images in batches of 32 using test_datagen generator validation_generator = test_datagen.flow_from_directory(validation_dir, target_size=(150, 150), batch_size=20, class_mode='binary') # Our input feature map is 150x150x3: 150x150 for the image pixels, and 3 for # the three color channels: R, G, and B img_input = layers.Input(shape=(150, 150, 3)) # First convolution extracts 16 filters that are 3x3 # Convolution is followed by max-pooling layer with a 2x2 window x = layers.Conv2D(16, 3, activation='relu')(img_input) x = layers.MaxPooling2D(2)(x) # Second convolution extracts 32 filters that are 3x3 # Convolution is followed by max-pooling layer with a 2x2 window x = layers.Conv2D(32, 3, activation='relu')(x) x = layers.MaxPooling2D(2)(x) # Third convolution extracts 64 filters that are 3x3 # Convolution is followed by max-pooling layer with a 2x2 window x = layers.Convolution2D(64, 3, activation='relu')(x) x = layers.MaxPooling2D(2)(x) # Flatten feature map to a 1-dim tensor x = layers.Flatten()(x) # Create a fully connected layer with ReLU activation and 512 hidden units
pool_size = 2 #Training parameters epochs = 20 nr_classes = 10 #Stacking layers of the Sequetial CNN features_layers = [ keras_Input(shape=inputsize), layers.experimental.preprocessing.Rescaling(1.0 / 255), layers.Conv2D(nr_Conv_filters, kernel_size, padding='valid', activation='relu'), #, input_shape=inputsize), layers.Conv2D(2 * nr_Conv_filters, kernel_size, activation='relu'), layers.MaxPooling2D(pool_size=(pool_size, pool_size)), layers.Dropout(0.25), layers.Flatten() ] classification_layers = [ layers.Dense(128, activation='relu'), layers.Dropout(0.5), layers.Dense(nr_classes, activation='softmax'), ] def train_model(model, nr_classes_in, epochs_in, X_train, y_train, X_test, y_test): #Preprocessing Output y_train1_cat = np_utils.to_categorical(y_train, nr_classes_in) y_test1_cat = np_utils.to_categorical(y_test, nr_classes_in)
import time # mnist = keras.datasets.mnist # model_name = "mnist{}".format(str(time.time())) model_name = "mnist_log_1" tensorboard=TensorBoard(log_dir='logs/{}'.format(model_name)) (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train.reshape((60000, 28, 28, 1)) x_test = x_test.reshape((10000, 28, 28, 1)) x_train, x_test = x_train.astype('float32') / 255.0, x_test.astype('float32') / 255.0 model = models.Sequential([ layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), layers.MaxPooling2D((2, 2)), layers.Conv2D(64, (3, 3), activation='relu'), layers.MaxPooling2D((2, 2)), layers.Conv2D(64, (3, 3), activation='relu'), layers.Flatten(), layers.Dense(64, activation='relu'), layers.Dense(10, activation="softmax") ]) model.summary() model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train, epochs=5, batch_size=64,callbacks=[tensorboard]) test_loss, test_acc = model.evaluate(x_test, y_test) print(test_loss)
def CNN_Model_with_DataAug(): pickle_in = open("X_train.pickle", "rb") X_train = pickle.load(pickle_in) pickle_in = open("X_test.pickle", "rb") X_test = pickle.load(pickle_in) pickle_in = open("y_train.pickle", "rb") y_train = pickle.load(pickle_in) pickle_in = open("y_test.pickle", "rb") y_test = pickle.load(pickle_in) # setup the data augmentation parameter from keras.preprocessing.image import ImageDataGenerator train_datagen = ImageDataGenerator( rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, ) test_datagen = ImageDataGenerator() train_generator = train_datagen.fit(X_train) # since we see binary_crossentropy loss, we need binary labels validation_generator = test_datagen.fit(X_test) img_size = 50 for epochs in [15, 30]: model = models.Sequential() model.add( layers.Conv2D(32, (3, 3), activation='relu', input_shape=(img_size, img_size, 3))) model.add(layers.Conv2D(32, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(128, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(128, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Flatten()) model.add(layers.Dense(512, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) print(model.summary()) from keras import optimizers #model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy']) model.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc']) model.fit_generator(train_datagen.flow(X_train, y_train, batch_size=32), steps_per_epoch=len(X_train) / 32, epochs=epochs, validation_data=test_datagen.flow(X_test, y_test, batch_size=20), validation_steps=len(X_test) / 20) if epochs == 15: # save the model model.save('CNN_DataAugmentation_Chile_Disease_vs_Normal_15') else: model.save('CNN_DataAugmentation_Chile_Disease_vs_Normal_30')
def _reduction_a_cell(ip, p, filters, block_id=None): '''Adds a Reduction cell for NASNet-A (Fig. 4 in the paper). # Arguments ip: Input tensor `x` p: Input tensor `p` filters: Number of output filters block_id: String block_id # Returns A Keras tensor ''' channel_dim = 1 if backend.image_data_format() == 'channels_first' else -1 with backend.name_scope('reduction_A_block_%s' % block_id): p = _adjust_block(p, ip, filters, block_id) h = layers.Activation('relu')(ip) h = layers.Conv2D(filters, (1, 1), strides=(1, 1), padding='same', kernel_regularizer=l2(weight_decay), name='reduction_conv_1_%s' % block_id, use_bias=False, kernel_initializer='he_normal')(h) if use_bn: h = layers.BatchNormalization(axis=channel_dim, momentum=bn_momentum, epsilon=1e-3, name='reduction_bn_1_%s' % block_id)(h) h = layers.SpatialDropout2D(drop_p)(h) h3 = layers.ZeroPadding2D(padding=correct_pad(backend, h, 3), name='reduction_pad_1_%s' % block_id)(h) with backend.name_scope('block_1'): x1_1 = _separable_conv_block(h, filters, (5, 5), strides=(2, 2), block_id='reduction_left1_%s' % block_id) x1_2 = _separable_conv_block(p, filters, (7, 7), strides=(2, 2), block_id='reduction_right1_%s' % block_id) x1 = layers.add([x1_1, x1_2], name='reduction_add_1_%s' % block_id) with backend.name_scope('block_2'): x2_1 = layers.MaxPooling2D( (3, 3), strides=(2, 2), padding='valid', name='reduction_left2_%s' % block_id)(h3) x2_2 = _separable_conv_block(p, filters, (7, 7), strides=(2, 2), block_id='reduction_right2_%s' % block_id) x2 = layers.add([x2_1, x2_2], name='reduction_add_2_%s' % block_id) with backend.name_scope('block_3'): x3_1 = layers.AveragePooling2D( (3, 3), strides=(2, 2), padding='valid', name='reduction_left3_%s' % block_id)(h3) x3_2 = _separable_conv_block(p, filters, (5, 5), strides=(2, 2), block_id='reduction_right3_%s' % block_id) x3 = layers.add([x3_1, x3_2], name='reduction_add3_%s' % block_id) with backend.name_scope('block_4'): x4 = layers.AveragePooling2D( (3, 3), strides=(1, 1), padding='same', name='reduction_left4_%s' % block_id)(x1) x4 = layers.add([x2, x4]) with backend.name_scope('block_5'): x5_1 = _separable_conv_block(x1, filters, (3, 3), block_id='reduction_left4_%s' % block_id) x5_2 = layers.MaxPooling2D( (3, 3), strides=(2, 2), padding='valid', name='reduction_right5_%s' % block_id)(h3) x5 = layers.add([x5_1, x5_2], name='reduction_add4_%s' % block_id) x = layers.concatenate([x2, x3, x4, x5], axis=channel_dim, name='reduction_concat_%s' % block_id) return x, ip
def Conv2DClassifierIn1(x_train, y_train, x_test, y_test): summary = True verbose = 1 # setHyperParams------------------------------------------------------------------------------------------------ batch_size = {{choice([512, 256, 128, 64, 32])}} epoch = {{ choice([300, 275, 250, 225, 200, 175, 150, 125, 100, 75, 50, 25]) }} conv_block = {{choice(['four', 'three', 'two'])}} conv1_num = {{choice([64, 32, 16, 8])}} conv2_num = {{choice([128, 64, 32, 16])}} conv3_num = {{choice([128, 64, 32])}} conv4_num = {{choice([256, 128, 64, 32])}} dense1_num = {{choice([512, 256, 128])}} dense2_num = {{choice([256, 128, 64])}} l1_regular_rate = {{uniform(0.00001, 1)}} l2_regular_rate = {{uniform(0.000001, 1)}} drop1_num = {{uniform(0.1, 1)}} drop2_num = {{uniform(0.0001, 1)}} activator = {{choice(['tanh', 'relu', 'elu'])}} optimizer = {{choice(['SGD', 'rmsprop', 'adam'])}} #--------------------------------------------------------------------------------------------------------------- kernel_size = (3, 3) pool_size = (2, 2) initializer = 'random_uniform' padding_style = 'same' loss_type = 'binary_crossentropy' metrics = ['accuracy'] my_callback = None # early_stopping = EarlyStopping(monitor='val_loss', patience=4) # checkpointer = ModelCheckpoint(filepath='keras_weights.hdf5', # verbose=1, # save_best_only=True) # my_callback = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2, # patience=5, min_lr=0.0001) # build -------------------------------------------------------------------------------------------------------- input_layer = Input(shape=x_train.shape[1:]) conv = layers.Conv2D(conv1_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(input_layer) conv = layers.Conv2D(conv1_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(conv) pool = layers.MaxPooling2D(pool_size, padding=padding_style)(conv) if conv_block == 'two': conv = layers.Conv2D(conv2_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(pool) conv = layers.Conv2D(conv2_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(conv) BatchNorm = layers.BatchNormalization(axis=-1)(conv) pool = layers.MaxPooling2D(pool_size, padding=padding_style)(BatchNorm) elif conv_block == 'three': conv = layers.Conv2D(conv2_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(pool) conv = layers.Conv2D(conv2_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(conv) BatchNorm = layers.BatchNormalization(axis=-1)(conv) pool = layers.MaxPooling2D(pool_size, padding=padding_style)(BatchNorm) conv = layers.Conv2D(conv3_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(pool) conv = layers.Conv2D(conv3_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(conv) BatchNorm = layers.BatchNormalization(axis=-1)(conv) pool = layers.MaxPooling2D(pool_size, padding=padding_style)(BatchNorm) elif conv_block == 'four': conv = layers.Conv2D(conv2_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(pool) conv = layers.Conv2D(conv2_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(conv) BatchNorm = layers.BatchNormalization(axis=-1)(conv) pool = layers.MaxPooling2D(pool_size, padding=padding_style)(BatchNorm) conv = layers.Conv2D(conv3_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(pool) conv = layers.Conv2D(conv3_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(conv) BatchNorm = layers.BatchNormalization(axis=-1)(conv) pool = layers.MaxPooling2D(pool_size, padding=padding_style)(BatchNorm) conv = layers.Conv2D(conv4_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(pool) conv = layers.Conv2D(conv4_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(conv) BatchNorm = layers.BatchNormalization(axis=-1)(conv) pool = layers.MaxPooling2D(pool_size, padding=padding_style)(BatchNorm) flat = layers.Flatten()(pool) drop = layers.Dropout(drop1_num)(flat) dense = layers.Dense(dense1_num, activation=activator, kernel_regularizer=regularizers.l1_l2( l1=l1_regular_rate, l2=l2_regular_rate))(drop) BatchNorm = layers.BatchNormalization(axis=-1)(dense) drop = layers.Dropout(drop2_num)(BatchNorm) dense = layers.Dense(dense2_num, activation=activator, kernel_regularizer=regularizers.l1_l2( l1=l1_regular_rate, l2=l2_regular_rate))(drop) output_layer = layers.Dense(len(np.unique(y_train)), activation='softmax')(dense) model = models.Model(inputs=input_layer, outputs=output_layer) if summary: model.summary() # train(self): class_weights = class_weight.compute_class_weight('balanced', np.unique(y_train), y_train.reshape(-1)) class_weights_dict = dict(enumerate(class_weights)) model.compile( optimizer=optimizer, loss=loss_type, metrics=metrics # accuracy ) result = model.fit(x=x_train, y=y_train, batch_size=batch_size, epochs=epoch, verbose=verbose, callbacks=my_callback, validation_data=(x_test, y_test), shuffle=True, class_weight=class_weights_dict) validation_acc = np.amax(result.history['val_acc']) print('Best validation acc of epoch:', validation_acc) return {'loss': -validation_acc, 'status': STATUS_OK, 'model': model}
# optimizer=optimizers.Adam(lr=1e-5, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False), # metrics=['accuracy']) image_size = 64 from keras import models from keras import layers from keras import optimizers model = models.Sequential() model.add( layers.Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=(image_size, image_size, 3))) model.add(layers.Conv2D(32, (3, 3), activation='relu')) model.add(layers.MaxPooling2D(pool_size=(2, 2))) model.add(layers.Dropout(0.5)) model.add(layers.Conv2D(64, (3, 3), padding='same', activation='relu')) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D(pool_size=(2, 2))) model.add(layers.Dropout(0.5)) model.add(layers.Flatten()) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dropout(0.5)) model.add(layers.Dense(2, activation='softmax')) model.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(learning_rate=0.0001, decay=1e-6), metrics=['acc']) model.summary()
loss_per_fold = [] # Define per-fold score containers fold_no = 1 for train, val in kfold.split(xtrain, ytrain): X_train = xtrain[train] X_val = xtrain[val] X_train = X_train.astype('float32') X_val = X_val.astype('float32') y_train = ytrain[train] y_val = ytrain[val] # Model architecture # model = models.Sequential() model.add(base_model) model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same')) model.add(layers.MaxPooling2D((2, 2), strides=(2, 2))) model.add(layers.Flatten()) model.add(layers.Dense(256, activation='relu')) #model.add(layers.Dropout(0.5)) model.add(layers.Dense(256, activation='relu')) #model.add(layers.Dropout(0.5)) model.add(layers.Dense(2, activation='softmax')) for layer in base_model.layers: layer.trainable = False model.summary() # Compile the model opt = adam(lr=0.0001) model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['acc']) print( '------------------------------------------------------------------------'
def build_model(self): # VGG style convolutional model inputs = kl.Input(shape=(1, self.resolution, self.resolution)) x = kl.Conv2D(32, (3, 3), activation='relu', padding='same', name='one_a')(inputs) x = kl.BatchNormalization(axis=1)(x) x = kl.MaxPooling2D()(x) x = kl.Conv2D(64, (3, 3), activation='relu', padding='same', name='one_b')(x) x = kl.BatchNormalization(axis=1)(x) x = kl.MaxPooling2D()(x) x = kl.Conv2D(128, (3, 3), activation='relu', padding='same', name='two_a')(x) x = kl.BatchNormalization(axis=1)(x) x = kl.Conv2D(64, (1, 1), activation='relu', padding='same', name='two_b')(x) x = kl.BatchNormalization(axis=1)(x) x = kl.Conv2D(128, (3, 3), activation='relu', padding='same', name='two_c')(x) x = kl.BatchNormalization(axis=1)(x) x = kl.MaxPooling2D()(x) x = kl.Conv2D(256, (3, 3), activation='relu', padding='same', name='three_a')(x) x = kl.BatchNormalization(axis=1)(x) x = kl.Conv2D(128, (1, 1), activation='relu', padding='same', name='three_b')(x) x = kl.BatchNormalization(axis=1)(x) x = kl.Conv2D(256, (3, 3), activation='relu', padding='same', name='three_c')(x) x = kl.BatchNormalization(axis=1)(x) x = kl.MaxPooling2D()(x) # Get down to three channels e,b,w. Softmax across channels such that c0+c1+c2 = 1. x_class_conv = kl.Conv2D(3, (1, 1), activation=ut.softMaxAxis1, padding='same', name='lastconv')(x) # flatten into chan0,chan0,..,chan0,chan1,chan1,...,chan1,chan2,chan2,...chan2 x_out = kl.Flatten(name='out')(x_class_conv) # Various templates #-------------------- #channels_0_0 = kl.Lambda(lambda x: x[:,:,0,0], (x_class_conv._keras_shape[1],)) (x_class_conv) #channels_0_1 = kl.Lambda(lambda x: x[:,:,0,1], (x_class_conv._keras_shape[1],)) (x_class_conv) #out_0_0 = kl.Activation('softmax')(channels_0_0) #out_0_1 = kl.Activation('softmax')(channels_0_1) #out = channels_at_xy(0,0,x_class_conv.shape)(x_class_conv) #x_class_conv = kl.MaxPooling2D()(x) # Split into GRIDSIZE x GRIDSIZE groups of three # Conv layer used for classification #x_class_conv = kl.Conv2D(3,(3,3),name='classconv')(x) #x_class_pool = kl.GlobalAveragePooling2D()(x_class_conv) #x_class = kl.Activation('softmax', name='class')(x_class_pool) #x_class = kl.Dense(2,activation='softmax', name='class')(x_flat0) self.model = km.Model(inputs=inputs, outputs=x_out) self.model.summary() if self.rate > 0: opt = kopt.Adam(self.rate) else: opt = kopt.Adam() self.model.compile(loss=ut.plogq, optimizer=opt, metrics=[ut.bool_match, ut.bitwise_match])
from keras import models, layers from keras.datasets import mnist from keras.utils import to_categorical import matplotlib.pyplot as plt import numpy as np #load data (train_images, train_labels), (test_images, test_labels) = mnist.load_data() #The CNN model model = models.Sequential() model.add(layers.Conv2D(80, (12, 12), activation='relu', input_shape=(28, 28, 1))) model.add(layers.MaxPooling2D((3, 3))) model.add(layers.Flatten()) model.add(layers.Dense(10, activation='softmax')) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) train_images = train_images.reshape((60000, 28, 28, 1)) train_images= train_images.astype('float32') / 255 # rescale pixel values from range [0, 255] to [0, 1] test_images = test_images.reshape((10000, 28, 28, 1)) test_images= test_images.astype('float32') / 255 train_labels = to_categorical(train_labels) test_labels = to_categorical(test_labels) history = model.fit(train_images, train_labels, epochs=10, batch_size=64, validation_data=(test_images, test_labels))