def __init__(self): # weights: 'imagenet' # pooling: 'max' or 'avg' # input_shape: (width, height, 3), width and height should >= 48 self.input_shape = (160, 120, 3) self.weight = 'imagenet' self.pooling = 'max' # include_top:是否保留顶层的3个全连接网络 # weights:None代表随机初始化,即不加载预训练权重。'imagenet'代表加载预训练权重 # input_tensor:可填入Keras tensor作为模型的图像输出tensor # input_shape:可选,仅当include_top=False有效,应为长为3的tuple,指明输入图片的shape,图片的宽高必须大于48,如(200,200,3) #pooling:当include_top = False时,该参数指定了池化方式。None代表不池化,最后一个卷积层的输出为4D张量。‘avg’代表全局平均池化,‘max’代表全局最大值池化。 #classes:可选,图片分类的类别数,仅当include_top = True并且不加载预训练权重时可用。 self.model_vgg = VGG16(weights=self.weight, input_shape=(self.input_shape[0], self.input_shape[1], self.input_shape[2]), pooling=self.pooling, include_top=False) # self.model_resnet = ResNet50(weights = self.weight, input_shape = (self.input_shape[0], self.input_shape[1], self.input_shape[2]), pooling = self.pooling, include_top = False) # self.model_densenet = DenseNet121(weights = self.weight, input_shape = (self.input_shape[0], self.input_shape[1], self.input_shape[2]), pooling = self.pooling, include_top = False) self.graph = tf.get_default_graph() self.sess = tf.keras.backend.get_session() with self.sess.as_default(): with self.graph.as_default(): self.model_vgg.predict(np.zeros((1, 160, 120, 3)))
def bbox_3D_net(input_shape=(224, 224, 3), vgg_weights=None, freeze_vgg=False, bin_num=6): vgg16_model = VGG16(include_top=False, weights=vgg_weights, input_shape=input_shape) if freeze_vgg: for layer in vgg16_model.layers: layer.trainable = False x = Flatten()(vgg16_model.output) dimension = Dense(512)(x) dimension = LeakyReLU(alpha=0.1)(dimension) dimension = Dropout(0.5)(dimension) dimension = Dense(3)(dimension) dimension = LeakyReLU(alpha=0.1, name='dimension')(dimension) orientation = Dense(256)(x) orientation = LeakyReLU(alpha=0.1)(orientation) orientation = Dropout(0.5)(orientation) orientation = Dense(bin_num * 2)(orientation) orientation = LeakyReLU(alpha=0.1)(orientation) orientation = Reshape((bin_num, -1))(orientation) orientation = Lambda(l2_normalize, name='orientation')(orientation) confidence = Dense(256)(x) confidence = LeakyReLU(alpha=0.1)(confidence) confidence = Dropout(0.5)(confidence) confidence = Dense(bin_num, activation='softmax', name='confidence')(confidence) model = Model(vgg16_model.input, outputs=[dimension, orientation, confidence]) return model
def main(): #VGGもでるの読みこみ input_tensor = Input(shape=(224, 224, 3)) vgg16 = VGG16(include_top=False, weights='imagenet', input_tensor=input_tensor) model = build_VGG16_model(vgg16) model.summary() #データセットの作成 create_txt(txts[0], 'train_img\\') create_txt(txts[1], 'test_img\\') X_train, Y_train = make_dataset(txts[0]) X_test, Y_test = make_dataset(txts[1]) #前処理(正規化) X_train = X_train.astype(np.float) X_test = X_test.astype(np.float) X_train = X_train / 255 X_test = X_test / 255 Y_train = to_categorical(Y_train, 9) Y_test = to_categorical(Y_test, 9) #CNNによる学習 model, history = model_train(model, X_train, Y_train) #学習結果のグラフ描画 graph_plot(history) #テストデータの検証結果の表示 model.evaluate(X_test, Y_test) #学習モデル・重みの保存 model.save('./model/model.h5')
def build_model(self, identifier): # Load VGG16 base_model = VGG16(weights='imagenet', include_top=False) fyipg = base_model.output # Add a global spatial average pooling layer fyipg = GlobalAveragePooling2D()(fyipg) # Let's add a fully-connected layer fyipg = Flatten(name ='Flatten1')(fyipg) fyipg = Dense(4096, activation='relu', name='AdditianlLayer1')(fyipg) fyipg = Dense(4096, activation='relu', name='AdditianlLayer2')(fyipg) if identifier == 1: fyipg = Dense(1, activation='sigmoid', name='Predictions')(fyipg) else: fyipg = Dense(101, activation='softmax', name='Predictions')(fyipg) # This is the model we will train model = Model(inputs = base_model.input , outputs = fyipg) # Setting optimizer for model optimizer =tf.train.GradientDescentOptimizer(learning_rate = 0.0001) # Optimize model for gender- and agemodel if identifier == 1: model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['mae','acc']) else: model.compile(optimizer= optimizer, loss='categorical_crossentropy', metrics=['mae', 'acc']) return model
def log_reg_predict_one_image(filename, log_reg_model, scaler, encode_tags, popular_tags): vgg_model = VGG16() vgg_model = Model(inputs=vgg_model.inputs, outputs=vgg_model.layers[-1].output) image = load_img(filename, target_size=(224, 224)) image = img_to_array(image) # (224, 224, 3) image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) image = preprocess_input(image) feature = vgg_model.predict(image, verbose=0) label = decode_predictions(feature)[0][0][1].replace("_", " ") encode_prediction = embed([label])[0] encode_cross_popular_tag = [] for i in range(len(encode_tags)): encode_cross_popular_tag.append(encode_tags[i] * encode_prediction) encode_cross_popular_tag = scaler.transform( encode_cross_popular_tag) # standardization probability_table = log_reg_model.predict_proba( encode_cross_popular_tag)[:, 1] # probability that the label is 1 index_list = sorted(range(len(probability_table)), key=lambda k: probability_table[k])[ -12:] # 12 index with the largest probability final_tag_prediction = [ "#" + popular_tags[i].replace(" ", "") for i in index_list ] return final_tag_prediction
def Mildnet_vgg16(): vgg_model = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3)) for layer in vgg_model.layers[:10]: layer.trainable = False intermediate_layer_outputs = get_layers_output_by_name( vgg_model, ["block1_pool", "block2_pool", "block3_pool", "block4_pool"]) convnet_output = GlobalAveragePooling2D()(vgg_model.output) for layer_name, output in intermediate_layer_outputs.items(): output = GlobalAveragePooling2D()(output) convnet_output = concatenate([convnet_output, output]) convnet_output = Dense(2048, activation='relu')(convnet_output) convnet_output = Dropout(0.6)(convnet_output) convnet_output = Dense(2048, activation='relu')(convnet_output) convnet_output = Lambda(lambda x: K.l2_normalize(x, axis=1))( convnet_output) final_model = tf.keras.models.Model(inputs=vgg_model.input, outputs=convnet_output) return final_model
def VGG16Trunk(image_shape, input_name, optimizer, loss, metrics, fine_tuning=False): x = Input(shape=image_shape, name=input_name) base_model = VGG16(weights='imagenet', include_top=False, input_tensor=x) for layer in base_model.layers: layer.trainable = False conv_base = base_model.output a = Flatten()(conv_base) a = Dense(1024, activation='relu')(a) a = Dropout(0.5)(a) y = Dense(NUM_CLASSES, activation='softmax')(a) model = Model(inputs=x, outputs=y) model.compile(loss=loss, optimizer=optimizer, metrics=metrics) return model
def new_Unet(model_flag='vUnet'): MARGIN = 30 model = VGG16(include_top=False, input_shape=(L_SIZE, L_SIZE, 3), weights='imagenet') conv5 = model.get_layer('block5_conv3').output conv4 = model.get_layer('block4_conv3').output conv3 = model.get_layer('block3_conv3').output conv2 = model.get_layer('block2_conv2').output conv1 = model.get_layer('block1_conv2').output conv6 = decoder_block(512, conv5, conv4) conv7 = decoder_block(256, conv6, conv3) conv8 = decoder_block(128, conv7, conv2) conv91 = decoder_block(64, conv8, conv1) conv92 = decoder_block(64, conv8, conv1) out1 = layers.Cropping2D(cropping=((MARGIN, MARGIN), (MARGIN, MARGIN)))(conv91) # for mask out1 = layers.Conv2D(1, (1, 1), activation='sigmoid', name='out1')(out1) if model_flag == 'vUnet': out2 = layers.Cropping2D(cropping=((MARGIN, MARGIN), (MARGIN, MARGIN)))(conv92) # for edge out2 = layers.Conv2D(1, (1, 1), activation='sigmoid', name='out2')(out2) model = tf.keras.Model(inputs=model.inputs, outputs=[out1, out2]) else: model = tf.keras.Model(inputs=model.inputs, outputs=out1) return model
def visnet_model(): vgg_model = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3)) convnet_output = GlobalAveragePooling2D()(vgg_model.output) convnet_output = Dense(4096, activation='relu')(convnet_output) convnet_output = Dropout(0.6)(convnet_output) convnet_output = Dense(4096, activation='relu')(convnet_output) convnet_output = Dropout(0.6)(convnet_output) convnet_output = Lambda(lambda x: K.l2_normalize(x, axis=1))(convnet_output) first_conv = Conv2D(96, kernel_size=(8, 8), strides=(16, 16), padding='same')(vgg_model.input) first_max = MaxPool2D(pool_size=(3, 3), strides=(4, 4), padding='same')(first_conv) first_max = Flatten()(first_max) first_max = Lambda(lambda x: K.l2_normalize(x, axis=1))(first_max) second_conv = Conv2D(96, kernel_size=(8, 8), strides=(32, 32), padding='same')(vgg_model.input) second_max = MaxPool2D(pool_size=(7, 7), strides=(2, 2), padding='same')(second_conv) second_max = Flatten()(second_max) second_max = Lambda(lambda x: K.l2_normalize(x, axis=1))(second_max) merge_one = concatenate([first_max, second_max]) merge_two = concatenate([merge_one, convnet_output], axis=1) emb = Dense(4096)(merge_two) l2_norm_final = Lambda(lambda x: K.l2_normalize(x, axis=1))(emb) final_model = tf.keras.models.Model(inputs=vgg_model.input, outputs=l2_norm_final) return final_model
def Mildnet_vgg16_big(): vgg_model = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3)) for layer in vgg_model.layers[:10]: layer.trainable = False intermediate_layer_outputs = get_layers_output_by_name(vgg_model, ["block1_pool", "block2_pool", "block3_pool", "block4_pool"]) convnet_output = GlobalAveragePooling2D()(vgg_model.output) for layer_name, output in intermediate_layer_outputs.items(): output = GlobalAveragePooling2D()(output) convnet_output = concatenate([convnet_output, output]) convnet_output = Dense(2048, activation='relu')(convnet_output) convnet_output = Dropout(0.6)(convnet_output) convnet_output = Dense(2048, activation='relu')(convnet_output) convnet_output = Lambda(lambda x: K.l2_normalize(x, axis=1))(convnet_output) first_conv = Conv2D(96, kernel_size=(8, 8), strides=(16, 16), padding='same')(vgg_model.input) first_max = MaxPool2D(pool_size=(3, 3), strides=(4, 4), padding='same')(first_conv) first_max = Flatten()(first_max) first_max = Lambda(lambda x: K.l2_normalize(x, axis=1))(first_max) second_conv = Conv2D(96, kernel_size=(8, 8), strides=(32, 32), padding='same')(vgg_model.input) second_max = MaxPool2D(pool_size=(7, 7), strides=(2, 2), padding='same')(second_conv) second_max = Flatten()(second_max) second_max = Lambda(lambda x: K.l2_normalize(x, axis=1))(second_max) merge_one = concatenate([first_max, second_max]) merge_two = concatenate([merge_one, convnet_output], axis=1) emb = Dense(4096)(merge_two) l2_norm_final = Lambda(lambda x: K.l2_normalize(x, axis=1))(emb) final_model = tf.keras.models.Model(inputs=vgg_model.input, outputs=l2_norm_final) return final_model
def visnet_lrn2d_model(): vgg_model = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3)) convnet_output = GlobalAveragePooling2D()(vgg_model.output) convnet_output = Dense(4096, activation='relu')(convnet_output) convnet_output = Dropout(0.6)(convnet_output) convnet_output = Dense(4096, activation='relu')(convnet_output) convnet_output = Dropout(0.6)(convnet_output) convnet_output = Lambda(lambda x: K.l2_normalize(x, axis=1))(convnet_output) first_maxpool = MaxPooling2D(pool_size=4, strides=4)(vgg_model.input) first_conv = Conv2D(96, kernel_size=8, strides=4, activation='relu')(first_maxpool) first_lrn2d = LRN2D(n=5)(first_conv) first_zero_padding = ZeroPadding2D(padding=(3, 3))(first_lrn2d) first_maxpool2 = MaxPooling2D(pool_size=7, strides=4, padding='same')(first_zero_padding) first_maxpool2 = Flatten()(first_maxpool2) first_maxpool2 = Lambda(lambda x: K.l2_normalize(x, axis=1))(first_maxpool2) second_maxpool = MaxPooling2D(pool_size=8, strides=8)(vgg_model.input) second_conv = Conv2D(96, kernel_size=8, strides=4, activation='relu')(second_maxpool) second_lrn2d = LRN2D(n=5)(second_conv) second_zero_padding = ZeroPadding2D(padding=(1, 1))(second_lrn2d) second_maxpool2 = MaxPooling2D(pool_size=3, strides=2, padding='same')(second_zero_padding) second_maxpool2 = Flatten()(second_maxpool2) second_maxpool2 = Lambda(lambda x: K.l2_normalize(x, axis=1))(second_maxpool2) merge_one = concatenate([first_maxpool2, second_maxpool2]) merge_two = concatenate([merge_one, convnet_output]) emb = Dense(4096)(merge_two) l2_norm_final = Lambda(lambda x: K.l2_normalize(x, axis=1))(emb) final_model = Model(inputs=vgg_model.input, outputs=l2_norm_final) return final_model
def transfer_model(model_name, input_shape, classes_nr): new_input = Input(shape=(input_shape[0], input_shape[1], 3)) if model_name == "vgg16": model = VGG16(include_top=False, input_tensor=new_input) if model_name == "densenet121": model = DenseNet121(include_top=False, input_tensor=new_input) if model_name == "inceptionv3": model = InceptionV3(include_top=False, input_tensor=new_input) if model_name == "mobilenet": model = MobileNet(include_top=False, input_tensor=new_input) if model_name == "resnet101": model = ResNet101(include_top=False, input_tensor=new_input) if model_name == "xception": model = Xception(include_top=False, input_tensor=new_input) for layer in model.layers: layer.trainable = False flat1 = layers.Flatten()(model.layers[-1].output) class1 = layers.Dense(1024, activation='relu')(flat1) drop1 = layers.Dropout(0.2)(class1) class2 = layers.Dense(256, activation='relu')(drop1) output = layers.Dense(classes_nr, activation='softmax')(class2) model = Model(inputs=model.inputs, outputs=output) return model
def create_model(): #conv_base = VGGFace(include_top=False, model='vgg16', weights='vggface', input_shape=(150, 150, 3)) conv_base = VGG16(weights='imagenet', include_top=False, input_shape=(150, 150, 3)) #print(conv_base.summary()) # 在conv_base的基础上添加全连接分类网络 model = models.Sequential() model.add(conv_base) model.add(layers.Flatten()) model.add(layers.Dense(256, activation='relu')) model.add(layers.Dense(5, activation='softmax')) conv_base.trainable = False model.compile(loss='categorical_crossentropy', optimizer=tf.keras.optimizers.Adam(lr=1e-4), metrics=['acc']) model_dir = os.path.join(os.getcwd(), "models/vggface_classifier") os.makedirs(model_dir, exist_ok=True) print("model_dir: ", model_dir) vggface_classifier = tf.keras.estimator.model_to_estimator( keras_model=model, model_dir=model_dir) print(model.summary()) return vggface_classifier
def TrainModel(path, train_dir, val_dir, batch_size, epochs, out_nums, nb_train_samples, nb_val_samples, img_width=256, img_height=256, freeze=13): #生成训练和验证数据 train_datagen = ImageDataGenerator( preprocessing_function=preprocess_input, 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(preprocessing_function=preprocess_input) # 测试数据预处理器 train_generator = train_datagen.flow_from_directory(train_dir, target_size=(img_width, img_height), batch_size=batch_size, # class_mode='binary' ) # 训练数据生成器 validation_generator = test_datagen.flow_from_directory(val_dir, target_size=(img_width, img_height), batch_size=batch_size, # class_mode='binary', shuffle=True) # 验证数据生成器 base_model = VGG16(weights=path, include_top=False, #加载迁移学习模型 input_shape=(img_width, img_height, 3)) for ix, layers in enumerate(base_model.layers): if ix < freeze: #冻结指定层 layers.trainable = False # layers.trainable = False #冻结指定层数层 #添加新的层用于训练 model = Flatten()(base_model.output) model = Dense(256, activation='relu', name='fc1')(model) model = Dropout(0.5, name='dropout1')(model) #=========================新加一层全连接======================= # model = Dense(64, activation='relu', name='fc2')(model) # model = Dropout(0.5, name='dropout2')(model) #============================================================== model = Dense(out_nums, activation='softmax')(model) # model = Dense(out_nums, activation='sigmoid')(model) model_final = Model(inputs=base_model.input, outputs=model) model_final.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.0001, momentum=0.9), metrics=['accuracy']) # model_final.compile(loss='binary_crossentropy', # optimizer=SGD(lr=0.0001, momentum=0.9), # metrics=['accuracy']) print(model_final.summary()) callbacks = [ EarlyStopping(patience=2, verbose=1), ModelCheckpoint('savemodel_1fc256.h5', monitor='val_acc', verbose=1, save_best_only=True, mode='max') # ModelCheckpoint('savemodel_1fc256_3conv_binary.h5', verbose=1, save_best_only=False, mode='max') ] # 训练&评估 model_final.fit_generator(train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=epochs, validation_data=validation_generator, validation_steps=nb_val_samples // batch_size, callbacks=callbacks, initial_epoch=0) # 每轮一行输出结果
def perceptual_loss(y_true, y_pred, image_shape): vgg = VGG16(include_top=False, weights="vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5", input_shape=(image_shape[0], image_shape[1], 3)) loss_model = KM.Model(inputs=vgg.input, outputs=vgg.get_layer('block3_conv3').output) loss_model.trainable = False return tf.reduce_mean(tf.square(loss_model(y_true) - loss_model(y_pred)))
def Mildnet_without_skip_big(): vgg_model = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3)) convnet_output = Dense(2048, activation='relu')(vgg_model.output) convnet_output = Dropout(0.6)(convnet_output) convnet_output = Dense(2048, activation='relu')(convnet_output) convnet_output = Lambda(lambda x: K.l2_normalize(x, axis=1))(convnet_output) final_model = tf.keras.models.Model(inputs=vgg_model.input, outputs=convnet_output) return final_model
def VGG16convbase(image_shape, layer='last'): if layer == 'last': conv_base = VGG16(weights='imagenet', include_top=False, input_shape=image_shape) model = conv_base return model
def get_base_model(self): """ Returns the corresponding Keras VGG model. :return: model """ if self.version == VGGVersion.VGG_16: return VGG16(input_shape=self.input_shape, include_top=True, weights='imagenet') if self.version == VGGVersion.VGG_19: return VGG19(input_shape=self.input_shape, include_top=True, weights='imagenet') else: raise ValueError("VGG Version not found!")
def channel_pruning_manual_mode(): sess = tf.compat.v1.Session() # Construct graph with sess.graph.as_default(): _ = VGG16(weights=None, input_shape=(224, 224, 3)) init = tf.compat.v1.global_variables_initializer() sess.run(init) # Create random dataset batch_size = 1 input_data = np.random.rand(100, 224, 224, 3) dataset = tf.data.Dataset.from_tensor_slices(input_data) dataset = dataset.batch(batch_size=batch_size) # Pick two convs to compress as examples block1_conv2_op = sess.graph.get_operation_by_name('block1_conv2/Conv2D') block2_conv2_op = sess.graph.get_operation_by_name('block2_conv2/Conv2D') list_of_module_comp_ratio_pairs = [ ModuleCompRatioPair(block1_conv2_op, 0.5), ModuleCompRatioPair(block2_conv2_op, 0.5) ] manual_params = ChannelPruningParameters.ManualModeParams( list_of_module_comp_ratio_pairs=list_of_module_comp_ratio_pairs) params = ChannelPruningParameters( input_op_names=['input_1'], output_op_names=['predictions/Softmax'], data_set=dataset, batch_size=32, num_reconstruction_samples=50, allow_custom_downsample_ops=False, mode=ChannelPruningParameters.Mode.manual, params=manual_params, multiplicity=8) # Single call to compress the model results = ModelCompressor.compress_model( sess, working_dir=None, eval_callback=evaluate_model, eval_iterations=10, input_shape=(32, 224, 224, 3), compress_scheme=CompressionScheme.channel_pruning, cost_metric=CostMetric.mac, parameters=params) compressed_model, stats = results print(compressed_model) print(stats) # Stats object can be pretty-printed easily
def create_model(img_dim=(128, 128, 3)): input_tensor = Input(shape=img_dim) base_model = VGG16(include_top=False, weights='imagenet', input_shape=img_dim) bn = BatchNormalization()(input_tensor) x = base_model(bn) x = Flatten()(x) output = Dense(17, activation='sigmoid')(x) model = Model(input_tensor, output) return model
def channel_pruning_auto_mode(): sess = tf.compat.v1.Session() # Construct graph with sess.graph.as_default(): _ = VGG16(weights=None, input_shape=(224, 224, 3)) init = tf.compat.v1.global_variables_initializer() sess.run(init) # ignore first Conv2D op conv2d = sess.graph.get_operation_by_name('block1_conv1/Conv2D') modules_to_ignore = [conv2d] greedy_params = GreedySelectionParameters(target_comp_ratio=Decimal(0.8), num_comp_ratio_candidates=2, use_monotonic_fit=True, saved_eval_scores_dict=None) auto_params = ChannelPruningParameters.AutoModeParams( greedy_select_params=greedy_params, modules_to_ignore=modules_to_ignore) # Create random dataset batch_size = 1 input_data = np.random.rand(100, 224, 224, 3) dataset = tf.data.Dataset.from_tensor_slices(input_data) dataset = dataset.batch(batch_size=batch_size) params = ChannelPruningParameters(input_op_names=['input_1'], output_op_names=['predictions/Softmax'], data_set=dataset, batch_size=32, num_reconstruction_samples=50, allow_custom_downsample_ops=False, mode=ChannelPruningParameters.Mode.auto, params=auto_params, multiplicity=8) # Single call to compress the model results = ModelCompressor.compress_model( sess, working_dir=None, eval_callback=evaluate_model, eval_iterations=10, input_shape=(32, 224, 224, 3), compress_scheme=CompressionScheme.channel_pruning, cost_metric=CostMetric.mac, parameters=params) compressed_model, stats = results print(compressed_model) print(stats) # Stats object can be pretty-printed easily
def MTLCNN(include_depth = False): # main conv module - color # --- input: preprocessed rodent color crop (224x224x3) # --- output: shared color features (None,512) base_model = VGG16(include_top=False, weights='imagenet', input_shape=(224,224,3), pooling='avg') # side conv moudle - depth # --- input: preprocessed rodent depth crop (64x64) # --- output: shared depth features (None,64) # [Block] = [CONV + CONV + POOL] # [Block] * 2 if include_depth == True: depth_input = Input(shape=(64,64,1),name = 'depth_input') xd = Conv2D(16, (3, 3), activation='relu', padding='same', name='depthb1_conv1')(depth_input) xd = Conv2D(16, (3, 3), activation='relu', padding='same', name='depthb1_conv2')(xd) xd = MaxPooling2D((2, 2), strides=(2, 2), name='depthb1_pool')(xd) # 16x32x32 xd = Conv2D(32, (3, 3), activation='relu', padding='same', name='depthb2_conv1')(xd) xd = Conv2D(32, (3, 3), activation='relu', padding='same', name='depthb2_conv2')(xd) xd = MaxPooling2D((2, 2), strides=(2, 2), name='depthb2_pool')(xd) # 32*16*16 xd = Conv2D(64, (3, 3), activation='relu', padding='same', name='depthb3_conv1')(xd) xd = Conv2D(64, (3, 3), activation='relu', padding='same', name='depthb3_conv2')(xd) xd = MaxPooling2D((2, 2), strides=(2, 2), name='depthb3_pool')(xd) # 64*8*8 xd = GlobalAveragePooling2D()(xd) # (none,64) shared_feature = concatenate([base_model.output, xd]) # use lower case concatenate else: shared_feature = base_model.output # pose estimation branch x = Dense(512, activation='relu', name='pose_fc1')(shared_feature) x = Dense(256, activation='relu', name='pose_fc2')(x) pose = Dense(14, name='pose_predict')(x) # behavior recognition branch - body y = Dense(256, activation='relu', name='body_fc1')(shared_feature) body = Dense(4, activation='softmax', name='body_predict')(y) # behavior recognition branch - head z = Dense(256, activation='relu', name='head_fc1')(shared_feature) head = Dense(4, activation='softmax', name='head_predict')(z) # combine all branches if include_depth == True: model = Model(inputs=[base_model.input, depth_input], outputs=[pose, body, head]) else: model = Model(inputs=base_model.input, outputs=[pose, body, head]) return model
def vanila_vgg16(): vgg_model = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3)) first_input = Input(shape=(224, 224, 3)) second_input = Input(shape=(224, 224, 3)) final_model = tf.keras.models.Model( inputs=[first_input, second_input, vgg_model.input], outputs=vgg_model.output) return final_model
def FCN(input_shape): vgg16_model = VGG16(weights = 'imagenet', include_top = False, input_shape = input_shape); #Sq_net = squeezenet(float(input_shape)); fire8 = extract_layer_from_model(vgg16_model, layer_name = 'block4_pool'); pool8 = MaxPooling2D((3,3), strides = (2,2), name = 'pool8')(fire8.output); fc1 = Conv2D(64, (6,6), strides= (1, 1), padding = 'same', name = 'fc1')(pool8); fc1 = Dropout(rate = 0.5)(fc1); if SEPERATE_CONFIDENCE: fc2 = Conv2D(4 , (1, 1), strides = (1, 1), padding = 'same', activation = 'relu', name = 'fc2')(fc1); rgb = K.l2_normalize(fc2[:, :, :, 0:3], axis = 3); w, h = map(int, fc2.get_shape()[1:3]); confidence = fc2[:, :, :, 3:4]; confidence = np.reshape(confidence, [-1, w*h]); confidence = K.softmax(confidence); confidence = np.reshape(confidence, shape=[-1, w, h, 1]); fc2 = rgb * confidence; else: fc2 = Conv2D(3, (1, 1), strides = (1, 1), padding = 'same', name = 'fc2')(fc1); fc2 = Activation('relu')(fc2); fc2 = Conv2D(3, (15, 15), padding = 'valid', name = 'fc_pooling')(fc2); def norm(fc2): fc2_norm = K.l2_normalize(fc2, axis = 3); illum_est = K.tf.reduce_sum(fc2_norm, axis = (1, 2)); illum_est = K.l2_normalize(illum_est); return illum_est; #illum_est = Dense(3)(fc2); illum_est = Lambda(norm)(fc2); FCN_model = Model(inputs = vgg16_model.input, outputs = illum_est, name = 'FC4'); return FCN_model;
def predict(): model = VGG16() print(model.summary()) # 预测一张图片类别 # 加载图片并输入到模型当中 (224, 24)是VGG的输入要求 image = load_img("./flower.jpg", target_size=(224, 224)) image = img_to_array(image) # print(image) print(image.shape) # 输入卷积中 需要四维结构 image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) print(image.shape)
def __init__(self): # 定义训练和测试图片的变化方式,标准化以及数据增强 self.train_generator = ImageDataGenerator(rescale=1. / 255) self.test_generator = ImageDataGenerator(rescale=1. / 255) # 定义图片训练相关的网络参数 self.train_dir = './data/train' self.test_dir = './data/test' # 定义图片训练相关网络参数 self.batch_size = 32 self.image_size = (224, 224) # 获取VGG16基类模型 self.base_model = VGG16(weights='imagenet', include_top=False)
def network_model(hidden_units, inputs, num_classes): conv_base = VGG16(weights='imagenet', include_top=False, input_tensor=inputs) for layer in conv_base.layers: layer.trainable = False a = Flatten()(conv_base.output) a = Dense(hidden_units, activation='relu')(a) a = Dropout(0.5)(a) y = Dense(num_classes, activation='softmax')(a) return y
def build_transfer_model(): base_model = VGG16(include_top=False, weights="imagenet", input_shape=INPUT_SHAPE, pooling='avg') base_model.trainable = False model = Sequential() model.add(base_model) model.add(Dense(256, activation='relu')) model.add(Dense(3, activation='softmax')) model.compile(optimizer="adam", loss='categorical_crossentropy', metrics=['accuracy']) model.summary() return model
def model_vgg16(self): # https://github.com/Tony607/Keras_catVSdog_tf_estimator/blob/master/keras_estimator_vgg16-cat_vs_dog.ipynb # https://github.com/Tony607/Keras_catVSdog_tf_estimator/blob/master/keras_estimator_vgg16-cat_vs_dog-TFRecord.ipynb # https://github.com/fchollet/deep-learning-with-python-notebooks/blob/master/5.3-using-a-pretrained-convnet.ipynb img_size = (150, 150, 3) conv_base = VGG16(weights='imagenet', include_top=False, input_shape=img_size) model = models.Sequential() model.add(conv_base) model.add(layers.Flatten()) model.add(layers.Dense(256, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) conv_base.trainable = False
def __init__(self): # 定义训练和测试图片的变换方法 标准化以及数据增强 self.train_generator = ImageDataGenerator(rescale=1.0/255.0) self.test_generator = ImageDataGenerator(rescale=1.0/255.0) # 指定训练数据和测试数据的目录 self.train_dir = "./data/train" self.test_dir = "./data/test" # 定义图片相关的网络参数 self.image_size = (224, 224) self.batch_size = 32 # 定义迁移学习的基类模型 # 不包含VGG当中3个全连接层的模型加载,并且加载了参数 self.base_model = VGG16(weights='imagenet', include_top=False)