def QSSD_VGG16( config, label_maps, num_predictions=10, is_training=True, ): model_config = config["model"] if is_training: input_shape = (None, None, 3) else: input_shape = (model_config["input_size"], model_config["input_size"], 3) num_classes = len(label_maps) + 1 # for background class l2_reg = model_config["l2_regularization"] kernel_initializer = model_config["kernel_initializer"] default_quads_config = model_config["default_quads"] extra_box_for_ar_1 = model_config["extra_box_for_ar_1"] input_tensor = Input(shape=input_shape) input_tensor = ZeroPadding2D(padding=(2, 2))(input_tensor) # construct the base network and extra feature layers base_network = VGG16(input_tensor=input_tensor, classes=num_classes, weights='imagenet', include_top=False) base_network = Model(inputs=base_network.input, outputs=base_network.get_layer('block5_conv3').output) base_network.get_layer("input_1")._name = "input" for layer in base_network.layers: if "pool" in layer.name: new_name = layer.name.replace("block", "") new_name = new_name.split("_") new_name = f"{new_name[1]}{new_name[0]}" else: new_name = layer.name.replace("conv", "") new_name = new_name.replace("block", "conv") base_network.get_layer(layer.name)._name = new_name base_network.get_layer(layer.name)._kernel_initializer = "he_normal" base_network.get_layer(layer.name)._kernel_regularizer = l2(l2_reg) layer.trainable = False # each layer of the base network should not be trainable def conv_block_1(x, filters, name, padding='valid', dilation_rate=(1, 1), strides=(1, 1)): return Conv2D(filters, kernel_size=(3, 3), strides=strides, activation='relu', padding=padding, dilation_rate=dilation_rate, kernel_initializer=kernel_initializer, kernel_regularizer=l2(l2_reg), name=name)(x) def conv_block_2(x, filters, name, padding='valid', dilation_rate=(1, 1), strides=(1, 1)): return Conv2D(filters, kernel_size=(1, 1), strides=strides, activation='relu', padding=padding, dilation_rate=dilation_rate, kernel_initializer=kernel_initializer, kernel_regularizer=l2(l2_reg), name=name)(x) pool5 = MaxPool2D(pool_size=(3, 3), strides=(1, 1), padding="same", name="pool5")(base_network.get_layer('conv5_3').output) fc6 = conv_block_1(x=pool5, filters=1024, padding="same", dilation_rate=(6, 6), name="fc6") fc7 = conv_block_2(x=fc6, filters=1024, padding="same", name="fc7") conv8_1 = conv_block_2(x=fc7, filters=256, padding="valid", name="conv8_1") conv8_2 = conv_block_1(x=conv8_1, filters=512, padding="same", strides=(2, 2), name="conv8_2") conv9_1 = conv_block_2(x=conv8_2, filters=128, padding="valid", name="conv9_1") conv9_2 = conv_block_1(x=conv9_1, filters=256, padding="same", strides=(2, 2), name="conv9_2") conv10_1 = conv_block_2(x=conv9_2, filters=128, padding="valid", name="conv10_1") conv10_2 = conv_block_1(x=conv10_1, filters=256, padding="valid", name="conv10_2") conv11_1 = conv_block_2(x=conv10_2, filters=128, padding="valid", name="conv11_1") conv11_2 = conv_block_1(x=conv11_1, filters=256, padding="valid", name="conv11_2") model = Model(inputs=base_network.input, outputs=conv11_2) # construct the prediction layers (conf, loc, & default_boxes) scales = np.linspace(default_quads_config["min_scale"], default_quads_config["max_scale"], len(default_quads_config["layers"])) mbox_conf_layers = [] mbox_quad_layers = [] for i, layer in enumerate(default_quads_config["layers"]): num_default_quads = get_number_default_quads( aspect_ratios=layer["aspect_ratios"], angles=layer["angles"], extra_box_for_ar_1=extra_box_for_ar_1) x = model.get_layer(layer["name"]).output layer_name = layer["name"] # conv4_3 has different scales compared to other feature map layers if layer_name == "conv4_3": layer_name = f"{layer_name}_norm" x = L2Normalization(gamma_init=20, name=layer_name)(x) layer_mbox_conf = Conv2D(filters=num_default_quads * num_classes, kernel_size=(3, 3), padding='same', kernel_initializer=kernel_initializer, kernel_regularizer=l2(l2_reg), name=f"{layer_name}_mbox_conf")(x) layer_mbox_conf_reshape = Reshape( (-1, num_classes), name=f"{layer_name}_mbox_conf_reshape")(layer_mbox_conf) layer_mbox_quad = Conv2D(filters=num_default_quads * 8, kernel_size=(3, 3), padding='same', kernel_initializer=kernel_initializer, kernel_regularizer=l2(l2_reg), name=f"{layer_name}_mbox_quad")(x) layer_mbox_quad_reshape = Reshape( (-1, 8), name=f"{layer_name}_mbox_quad_reshape")(layer_mbox_quad) mbox_conf_layers.append(layer_mbox_conf_reshape) mbox_quad_layers.append(layer_mbox_quad_reshape) # concentenate class confidence predictions from different feature map layers mbox_conf = Concatenate(axis=-2, name="mbox_conf")(mbox_conf_layers) mbox_conf_softmax = Activation('softmax', name='mbox_conf_softmax')(mbox_conf) # concentenate object quad predictions from different feature map layers mbox_quad = Concatenate(axis=-2, name="mbox_quad")(mbox_quad_layers) if is_training: # concatenate confidence score predictions, bounding box predictions, and default boxes predictions = Concatenate( axis=-1, name='predictions')([mbox_conf_softmax, mbox_quad]) return Model(inputs=base_network.input, outputs=predictions) mbox_default_quads_layers = [] for i, layer in enumerate(default_quads_config["layers"]): num_default_quads = get_number_default_quads( aspect_ratios=layer["aspect_ratios"], angles=layer["angles"], extra_box_for_ar_1=extra_box_for_ar_1) x = model.get_layer(layer["name"]).output layer_name = layer["name"] layer_default_quads = DefaultQuads( image_shape=input_shape, scale=scales[i], next_scale=scales[i + 1] if i + 1 <= len(default_quads_config["layers"]) - 1 else 1, aspect_ratios=layer["aspect_ratios"], angles=layer["angles"], variances=default_quads_config["variances"], extra_box_for_ar_1=extra_box_for_ar_1, name=f"{layer_name}_default_quads")(x) layer_default_quads_reshape = Reshape( (-1, 16), name=f"{layer_name}_default_quads_reshape")(layer_default_quads) mbox_default_quads_layers.append(layer_default_quads_reshape) # concentenate default boxes from different feature map layers mbox_default_quads = Concatenate( axis=-2, name="mbox_default_quads")(mbox_default_quads_layers) predictions = Concatenate(axis=-1, name='predictions')( [mbox_conf_softmax, mbox_quad, mbox_default_quads]) # decoded_predictions = DecodeQSSDPredictions( # input_size=model_config["input_size"], # num_predictions=num_predictions, # name="decoded_predictions" # )(predictions) return Model(inputs=base_network.input, outputs=predictions)
def __init__(self): #Initialize all necessary models self.feature_extractor = VGG16(weights="imagenet", include_top=False) self.model = pickle.load(open("Logistic.pickle", "rb")) self.labels = pd.read_csv("labels.csv")
def __init__(self): self.__model = VGG16(include_top=False) self.__model.layers.pop() self.__model = Model(inputs=self.__model.inputs, outputs=self.__model.layers[-1].output)
image_size = 224 ''' numpy v1.16.3 より、numpy.load()関数の挙動が変更されたため、allow_pickle=Trueを追記 imagefiles_224.npyを読み込み 正規化の処理を行う(255で割る) ''' X_train, X_test, y_train, y_test = np.load('./imagefiles_224.npy', allow_pickle=True) y_train = np_utils.to_categorical(y_train, num_classes) y_test = np_utils.to_categorical(y_test, num_classes) X_train = X_train.astype('float') / 255.0 X_test = X_test.astype('float') / 255.0 # モデルの定義 model = VGG16(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3)) # print('Model loaded') # model.summary() top_model = Sequential() top_model.add(Flatten(input_shape=model.output_shape[1:])) top_model.add(Dense(256, activation='relu')) top_model.add(Dropout(0.5)) top_model.add(Dense(num_classes, activation='softmax')) model = Model(inputs=model.input, outputs=top_model(model.output)) # model.summary() # model.layers:モデルに含れるレイヤーを平滑化したリスト
'''Load Tensor Data''' Tensor = np.load(TensorPath) Label = np.load(LabelPath) if NIR: Tensor = Tensor[:, 0:Tilesize, 0:Tilesize, :] else: Tensor = Tensor[:, 0:Tilesize, 0:Tilesize, 0:3] '''Load initial CNN''' model = tf.keras.models.load_model(InitialModel) model.save_weights( os.path.dirname(InitialModel) + 'W_' + os.path.basename(InitialModel)) '''Create copy of the CNN with a new learning rate''' inShape = Tensor.shape[1:] CNN_base = VGG16(include_top=False, weights=None, input_shape=inShape) model = Sequential() model.add((CNN_base)) model.add(Flatten()) #Estimator.add(Dense(32,kernel_regularizer=regularizers.l2(0.001),kernel_initializer='normal',activation='relu')) model.add( Dense(512, activation='relu', kernel_regularizer=regularizers.l2(0.001))) model.add(Dropout(0.5)) model.add( Dense(256, activation='relu', kernel_regularizer=regularizers.l2(0.001))) model.add( Dense(NClasses + 1, kernel_initializer='normal', activation='softmax', dtype='float32'))
#Import the necessary packages from keras.models import load_model from tensorflow.keras.applications import VGG16 from tensorflow.keras.applications.vgg16 import preprocess_input from tensorflow.keras.preprocessing.image import img_to_array import numpy as np import cv2 #Import the VGG16 and pre-trained classifier model modelV = VGG16(weights="imagenet", include_top=False) model = load_model("trial1.h5") #Import the face cascade classifier face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') classes = ['with-mask', 'without-mask'] def detect_mask(img): face_img = img.copy() face_rects = face_cascade.detectMultiScale(face_img, scaleFactor=1.1, minNeighbors=8) for (x, y, w, h) in face_rects: face = face_img[y:y + h, x:x + w] face = cv2.resize(face, (224, 224)) face = img_to_array(face) custom = np.expand_dims(face, axis=0) custom = preprocess_input(custom)
return captions_list captions = np.array(get_captions(sample_images_list, images_caption)) print("Total Captions :", len(captions)) # To get the captions as a list of lists(all captions for an image in one list)..... temp = images_caption.groupby('image_name')['comment'].apply(list).reset_index( name='comment') df = pd.DataFrame(temp, columns=['comment']) captions_listoflist = df.values.tolist() captions_listoflist = captions_listoflist[:5000] captions_listoflist = np.array(captions_listoflist).reshape(5000, 5) # Pre-trained VGG16 model as an encoder...... image_model = VGG16(include_top=True, weights='imagenet') image_model.summary() # Input and output for the new model..... new_input = image_model.input hidden_layer = image_model.get_layer('fc2') # transfer values size for input to the initial states of decoder model.... transfer_values_size = K.int_shape(hidden_layer.output)[1] print(transfer_values_size) # Modified encoder model for getting the tranfer values of the images..... image_features_extract_model = tf.keras.Model(inputs=new_input, outputs=hidden_layer.output) image_features_extract_model.summary() transfer_values = image_features_extract_model.predict(images,
def create_model( model_name, log_dir, args ): # optimizer, learning rate, activation, neurons, batch size, epochs... input_shape = input_size(model_name, args) if args.head == 'max' or (args.base_trainable and args.head != 't_complex'): pool = 'max' else: pool = 'none' if model_name == 'VGG16': conv_base = VGG16(weights='imagenet', include_top=False, pooling=pool, input_shape=input_shape) elif model_name == 'VGG19': conv_base = VGG19(weights='imagenet', include_top=False, pooling=pool, input_shape=input_shape) elif model_name == 'ResNet50': conv_base = ResNet50(weights='imagenet', include_top=False, pooling=pool, input_shape=input_shape) elif model_name == 'InceptionV3': conv_base = InceptionV3(weights='imagenet', include_top=False, pooling=pool, input_shape=input_shape) elif model_name == 'Xception': conv_base = Xception(weights='imagenet', include_top=False, pooling=pool, input_shape=input_shape) elif model_name == 'InceptionResNetV2': conv_base = InceptionResNetV2(weights='imagenet', include_top=False, pooling=pool, input_shape=input_shape) elif model_name == 'NASNetMobile': conv_base = NASNetMobile(weights='imagenet', include_top=False, pooling=pool, input_shape=input_shape) elif model_name == 'NASNetLarge': conv_base = NASNetLarge(weights='imagenet', include_top=False, pooling=pool, input_shape=input_shape) elif model_name == 'DenseNet201': conv_base = DenseNet201(weights='imagenet', include_top=False, pooling=pool, input_shape=input_shape) elif model_name == 'MobileNetV2': conv_base = MobileNetV2(weights='imagenet', include_top=False, pooling=pool, input_shape=input_shape) else: conv_base = None print("Model name not known!") exit() conv_base.trainable = args.base_trainable model = models.Sequential() if args.base_trainable: if args.head == 't_complex': model = models.Sequential() model.add(conv_base) model.add( layers.Conv2D(filters=1024, kernel_size=(3, 3), padding='same', strides=1)) model.add(layers.Flatten()) # ?? model.add(layers.Dense(1024, activation='sigmoid')) model.add(layers.Dense(256, activation='sigmoid')) model.add(layers.Dense(args.CLASSES_NO, activation='softmax') ) # (samples, new_rows, new_cols, filters) else: model.add(conv_base) model.add(layers.Dense(args.CLASSES_NO, activation='softmax')) elif args.head == 'dense': # outside only? model.add(conv_base) model.add(layers.Flatten()) model.add(layers.Dropout(0.5)) model.add(layers.Dense(256, activation='relu')) model.add(layers.Dropout(0.5)) model.add(layers.Dense(128, activation='relu')) model.add(layers.Dense(args.CLASSES_NO, activation='softmax')) elif args.head == 'max': model.add(conv_base) model.add(layers.Dense(512, activation='relu')) model.add(layers.Dropout(0.5)) model.add(layers.Dense(256, activation='relu')) model.add(layers.Dense(args.CLASSES_NO, activation='softmax')) elif args.head == 'mod': model = models.Sequential() model.add(conv_base) model.add( layers.Conv2D(filters=2048, kernel_size=(3, 3), padding='valid')) model.add(layers.Flatten()) # ?? model.add(layers.Dropout(0.5)) model.add(layers.Dense(1024, activation='sigmoid')) model.add(layers.Dense(256, activation='relu')) model.add(layers.Dense( args.CLASSES_NO, activation='softmax')) # (samples, new_rows, new_cols, filters) if args.lr_decay: lr_schedule = ExponentialDecay(args.INIT_LEARN_RATE, decay_steps=args.DECAY_STEPS, decay_rate=args.DECAY_RATE, staircase=True) model.compile(loss='categorical_crossentropy', optimizer=SGD(lr_schedule), metrics=['acc']) # To different optimisers? else: model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=args.LEARNING_RATE), metrics=['acc']) with open(os.path.join(log_dir, 'modelsummary.txt'), 'w') as f: with redirect_stdout(f): model.summary() print(model.summary()) return model
def main(): directory = 'img' # 画像が保存されているフォルダ df_train = pd.read_csv('train.csv') # 学習データの情報がかかれたDataFrame df_validation = pd.read_csv('val.csv') # 検証データの情報がかかれたDataFrame df_test = pd.read_csv('test.csv') # テストデータの情報がかかれたDataFrame label_list = ['AMD', 'DR_DM', 'Gla', 'MH', 'Normal', 'RD', 'RP', 'RVO'] # ラベル名 image_size = (224, 224) # 入力画像サイズ classes = len(label_list) # 分類クラス数 batch_size = 32 # バッチサイズ epochs = 300 # エポック数 loss = 'categorical_crossentropy' # 損失関数 optimizer = Adam(lr=0.00001, amsgrad=True) # 最適化関数 metrics = 'accuracy' # 評価方法 # ImageDataGenerator画像増幅のパラメータ aug_params = { 'rotation_range': 5, 'width_shift_range': 0.05, 'height_shift_range': 0.05, 'shear_range': 0.1, 'zoom_range': 0.05, 'horizontal_flip': True, 'vertical_flip': True } # val_lossが最小になったときのみmodelを保存 mc_cb = ModelCheckpoint('model_weights.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min') # 学習が停滞したとき、学習率を0.2倍に rl_cb = ReduceLROnPlateau(monitor='loss', factor=0.2, patience=3, verbose=1, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0) # 学習が進まなくなったら、強制的に学習終了 es_cb = EarlyStopping(monitor='loss', min_delta=0, patience=5, verbose=1, mode='auto') # データの数に合わせて損失の重みを調整 weight_balanced = {} for i, label in enumerate(label_list): weight_balanced[i] = (df_train['label'] == label).sum() max_count = max(weight_balanced.values()) for label in weight_balanced: weight_balanced[label] = max_count / weight_balanced[label] print(weight_balanced) # ジェネレータの生成 ## 学習データのジェネレータ datagen = ImageDataGenerator(rescale=1. / 255, **aug_params) train_generator = datagen.flow_from_dataframe(dataframe=df_train, directory=directory, x_col='filename', y_col='label', target_size=image_size, class_mode='categorical', classes=label_list, batch_size=batch_size) step_size_train = train_generator.n // train_generator.batch_size ## 検証データのジェネレータ datagen = ImageDataGenerator(rescale=1. / 255) validation_generator = datagen.flow_from_dataframe( dataframe=df_validation, directory=directory, x_col='filename', y_col='label', target_size=image_size, class_mode='categorical', classes=label_list, batch_size=batch_size) step_size_validation = validation_generator.n // validation_generator.batch_size # ネットワーク構築 base_model = VGG16(include_top=False, weights='imagenet', pooling='avg', input_shape=(image_size[0], image_size[1], 3)) x = Dense(256, kernel_initializer='he_normal')(base_model.output) x = Dense(classes, kernel_initializer='he_normal')(x) outputs = Activation('softmax')(x) model = Model(inputs=base_model.inputs, outputs=outputs) model.summary() model.compile(loss=loss, optimizer=optimizer, metrics=[metrics]) # 学習 history = model.fit_generator(train_generator, steps_per_epoch=step_size_train, epochs=epochs, verbose=1, callbacks=[mc_cb, rl_cb, es_cb], validation_data=validation_generator, validation_steps=step_size_validation, class_weight=weight_balanced, workers=3) # 学習曲線の保存 plot_history(history) # テストデータの評価 ## 学習済み重みの読み込み model.load_weights('model_weights.h5') ## 推論 X = df_test['filename'].values y_true = list(map(lambda x: label_list.index(x), df_test['label'].values)) y_pred = [] for file in tqdm(X, desc='pred'): # 学習時と同じ条件になるように画像をリサイズ&変換 img = Image.open(f'{directory}/{file}') img = img.resize(image_size) img = np.array(img, dtype=np.float32) img *= 1. / 255 img = np.expand_dims(img, axis=0) y_pred.append(np.argmax(model.predict(img)[0])) ## 評価 print(classification_report(y_true, y_pred, target_names=label_list))
def build(self, use_cpu=False, print_summary=False): vgg16 = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3)) if use_cpu: device = '/cpu:0' else: device = '/gpu:0' print('=' * 30 + 'Loading DeconvNet' + '=' * 30) with tf.device(device): inputs = Input(shape=(224, 224, 3), batch_size=self.batch_size) print('=' * 40 + 'inputs' + '=' * 40 + '\n', inputs) conv_block_1 = self.buildConv2DBlock(inputs, 64, 1, 2) pool1, pool1_argmax = Lambda(self.max_pool_with_argmax, name='pool1')(conv_block_1) print('=' * 40 + 'pool1' + '=' * 40 + '\n', pool1) conv_block_2 = self.buildConv2DBlock(pool1, 128, 2, 2) pool2, pool2_argmax = Lambda(self.max_pool_with_argmax, name='pool2')(conv_block_2) print('=' * 40 + 'pool2' + '=' * 40 + '\n', pool2) conv_block_3 = self.buildConv2DBlock(pool2, 256, 3, 3) pool3, pool3_argmax = Lambda(self.max_pool_with_argmax, name='pool3')(conv_block_3) print('=' * 40 + 'pool3' + '=' * 40 + '\n', pool3) conv_block_4 = self.buildConv2DBlock(pool3, 512, 4, 3) pool4, pool4_argmax = Lambda(self.max_pool_with_argmax, name='pool4')(conv_block_4) print('=' * 40 + 'pool4' + '=' * 40 + '\n', pool4) conv_block_5 = self.buildConv2DBlock(pool4, 512, 5, 3) pool5, pool5_argmax = Lambda(self.max_pool_with_argmax, name='pool5')(conv_block_5) print('=' * 40 + 'pool5' + '=' * 40 + '\n', pool5) fc6 = Conv2D(512, 7, use_bias=False, padding='valid', name='fc6')(pool5) #4096 fc6 = BatchNormalization(name='batchnorm_fc6')(fc6) fc6 = Activation('relu', name='relu_fc6')(fc6) print('=' * 40 + 'fc6' + '=' * 40 + '\n', fc6) fc7 = Conv2D(512, 1, use_bias=False, padding='valid', name='fc7')(fc6) #4096 fc7 = BatchNormalization(name='batchnorm_fc7')(fc7) fc7 = Activation('relu', name='relu_fc7')(fc7) print('=' * 40 + 'fc7' + '=' * 40 + '\n', fc7) x = Conv2DTranspose(512, 7, use_bias=False, padding='valid', name='deconv-fc6')(fc7) x = BatchNormalization(name='batchnorm_deconv-fc6')(x) x = Activation('relu', name='relu_deconv-fc6')(x) print('=' * 40 + 'relu_deconv-fc6' + '=' * 40 + '\n', x) x = MaxUnpoolWithArgmax(pool5_argmax, name='unpool5')(x) x.set_shape(conv_block_5.get_shape()) print('=' * 40 + 'unpool5' + '=' * 40 + '\n', x) x = Conv2DTranspose(512, 3, use_bias=False, padding='same', name='deconv5-1')(x) x = BatchNormalization(name='batchnorm_deconv5-1')(x) x = Activation('relu', name='relu_deconv5-1')(x) print('=' * 40 + 'relu_deconv5-1' + '=' * 40 + '\n', x) x = Conv2DTranspose(512, 3, use_bias=False, padding='same', name='deconv5-2')(x) x = BatchNormalization(name='batchnorm_deconv5-2')(x) x = Activation('relu', name='relu_deconv5-2')(x) print('=' * 40 + 'relu_deconv5-2' + '=' * 40 + '\n', x) x = Conv2DTranspose(512, 3, use_bias=False, padding='same', name='deconv5-3')(x) x = BatchNormalization(name='batchnorm_deconv5-3')(x) x = Activation('relu', name='relu_deconv5-3')(x) print('=' * 40 + 'relu_deconv5-3' + '=' * 40 + '\n', x) x = MaxUnpoolWithArgmax(pool4_argmax, name='unpool4')(x) x.set_shape(conv_block_4.get_shape()) print('=' * 40 + 'unpool4' + '=' * 40 + '\n', x) x = Conv2DTranspose(512, 3, use_bias=False, padding='same', name='deconv4-1')(x) x = BatchNormalization(name='batchnorm_deconv4-1')(x) x = Activation('relu', name='relu_deconv4-1')(x) print('=' * 40 + 'relu_deconv4-1' + '=' * 40 + '\n', x) x = Conv2DTranspose(512, 3, use_bias=False, padding='same', name='deconv4-2')(x) x = BatchNormalization(name='batchnorm_deconv4-2')(x) x = Activation('relu', name='relu_deconv4-2')(x) print('=' * 40 + 'relu_deconv4-2' + '=' * 40 + '\n', x) x = Conv2DTranspose(256, 3, use_bias=False, padding='same', name='deconv4-3')(x) x = BatchNormalization(name='batchnorm_deconv4-3')(x) x = Activation('relu', name='c3')(x) print('=' * 40 + 'relu_deconv4-3' + '=' * 40 + '\n', x) x = MaxUnpoolWithArgmax(pool3_argmax, name='unpool3')(x) x.set_shape(conv_block_3.get_shape()) print('=' * 40 + 'unpool3' + '=' * 40 + '\n', x) x = Conv2DTranspose(256, 3, use_bias=False, padding='same', name='deconv3-1')(x) x = BatchNormalization(name='batchnorm_deconv3-1')(x) x = Activation('relu', name='relu_deconv3-1')(x) print('=' * 40 + 'relu_deconv3-1' + '=' * 40 + '\n', x) x = Conv2DTranspose(256, 3, use_bias=False, padding='same', name='deconv3-2')(x) x = BatchNormalization(name='batchnorm_deconv3-2')(x) x = Activation('relu', name='relu_deconv3-2')(x) print('=' * 40 + 'relu_deconv3-2' + '=' * 40 + '\n', x) x = Conv2DTranspose(128, 3, use_bias=False, padding='same', name='deconv3-3')(x) x = BatchNormalization(name='batchnorm_deconv3-3')(x) x = Activation('relu', name='relu_deconv3-3')(x) print('=' * 40 + 'relu_deconv3-3' + '=' * 40 + '\n', x) x = MaxUnpoolWithArgmax(pool2_argmax, name='unpool2')(x) x.set_shape(conv_block_2.get_shape()) print('=' * 40 + 'unpool2' + '=' * 40 + '\n', x) x = Conv2DTranspose(128, 3, use_bias=False, padding='same', name='deconv2-1')(x) x = BatchNormalization(name='batchnorm_deconv2-1')(x) x = Activation('relu', name='relu_deconv2-1')(x) print('=' * 40 + 'relu_deconv2-1' + '=' * 40 + '\n', x) x = Conv2DTranspose(64, 3, use_bias=False, padding='same', name='deconv2-2')(x) x = BatchNormalization(name='batchnorm_deconv2-2')(x) x = Activation('relu', name='relu_deconv2-2')(x) print('=' * 40 + 'relu_deconv2-2' + '=' * 40 + '\n', x) x = MaxUnpoolWithArgmax(pool1_argmax, name='unpool1')(x) x.set_shape(conv_block_1.get_shape()) print('=' * 40 + 'unpool1' + '=' * 40 + '\n', x) x = Conv2DTranspose(64, 3, use_bias=False, padding='same', name='deconv1-1')(x) x = BatchNormalization(name='batchnorm_deconv1-1')(x) x = Activation('relu', name='relu_deconv1-1')(x) print('=' * 40 + 'relu_deconv1-1' + '=' * 40 + '\n', x) x = Conv2DTranspose(64, 3, use_bias=False, padding='same', name='deconv1-2')(x) x = BatchNormalization(name='batchnorm_deconv1-2')(x) x = Activation('relu', name='relu_deconv1-2')(x) print('=' * 40 + 'relu_deconv1-2' + '=' * 40 + '\n', x) output = Conv2DTranspose(self.labels, 1, activation='softmax', padding='same', name='output')(x) print('=' * 40 + 'output' + '=' * 40 + '\n', output) self.model = Model(inputs=inputs, outputs=output) vgg16 = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3)) if print_summary: print(self.model.summary()) for layer in self.model.layers: if layer.name.startswith('conv'): block = layer.name[4:].split('-')[0] depth = layer.name[4:].split('-')[1] # apply vgg16 weights without bias layer.set_weights([ vgg16.get_layer('block{}_conv{}'.format( block, depth)).get_weights()[0] ]) #sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) #rmsprop=RMSprop(lr=0.001, rho=0.9, epsilon=1e-06) #adagrad=Adagrad(lr=0.01, epsilon=1e-06) adadelta = Adadelta(lr=1.0, rho=0.95, epsilon=1e-06) self.model.compile(optimizer=adadelta, loss='categorical_crossentropy', metrics=['accuracy', 'mse'])
y, train_size=0.8, shuffle=True, random_state=42) aaa = 1 x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], aaa) x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], aaa) print(x_train.shape, y_train.shape) # (3628, 128, 862, 1) (3628,) print(x_test.shape, y_test.shape) # (908, 128, 862, 1) (908,) model = VGG16( include_top=True, input_shape=(128, 862, 1), classes=2, pooling=None, weights=None, ) model.summary() # model.trainable = False model.save('C:/nmb/nmb_data/h5/5s/vgg16/vgg16_sgd_2.h5') # 컴파일, 훈련 op = SGD(lr=1e-2) batch_size = 4 es = EarlyStopping(monitor='val_loss', patience=20,
import tensorflow as tf from tensorflow.keras.applications import VGG16 from tensorflow.keras.applications import Xception from try05_custom_model import Discriminator a = VGG16() # print(type(a)) # a.summary() # b = Xception() # print(type(a)) ### <class 'tensorflow.python.keras.engine.training.Model'> # print(type(d)) ### <class 'try05_custom_model.Discriminator'>,但其實 # print(type(tf.keras.models.Model())) ### <class 'tensorflow.python.keras.engine.training.Model'> ######################################################################################################################## ### Layer 和 Model 還是有些差別喔!像是Model 才能使用 .summary() ! ######################################################################################################################## ### 參考: ### https://stackoverflow.com/questions/55235212/model-summary-cant-print-output-shape-while-using-subclass-model ### https://github.com/tensorflow/tensorflow/issues/25036#issuecomment-542087377 # import tensorflow as tf # from tensorflow.keras.models import Model # from tensorflow.keras.layers import Input # class MyModel(tf.keras.Model): # def __init__(self): # super().__init__() # self.dense = tf.keras.layers.Dense(1) # def call(self, inputs, **kwargs):
for image_batch, labels_batch in train_ds: print(image_batch.shape) print(labels_batch.shape) break # dataset performance configuration AUTOTUNE = AUTOTUNE train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE) val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE) vgg16 = VGG16( include_top=True, weights="imagenet", input_tensor=None, input_shape=(224, 224, 3), pooling=None, ) model = Sequential() model.add(Rescaling(1./255, input_shape=(img_height, img_width, 3))) for layers in vgg16.layers[1:-1]: model.add(layers) model.add(Dense(1, activation='sigmoid')) for layers in model.layers[1:-1]: layers.trainable = False
'flowers17') files_pattern = (dataset_path / 'images' / '*' / '*.jpg') image_paths = [*glob(str(files_pattern))] CLASSES = {p.split(os.path.sep)[-2] for p in image_paths} X, y = load_images_and_labels(image_paths) X = X.astype('float') / 255.0 y = LabelBinarizer().fit_transform(y) (X_train, X_test, y_train, y_test) = train_test_split(X, y, test_size=0.2, random_state=SEED) base_model = VGG16(weights='imagenet', include_top=False, input_tensor=Input(shape=(256, 256, 3))) for layer in base_model.layers: layer.trainable = False model = build_network(base_model, len(CLASSES)) model = Model(base_model.input, model) BATCH_SIZE = 64 augmenter = ImageDataGenerator(rotation_range=30, horizontal_flip=True, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,
import numpy as np import tensorflow as tf from tensorflow import keras from time import time from tensorflow.python.keras.callbacks import TensorBoard from IPython.display import clear_output from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, concatenate, Dense, Flatten, Add, LeakyReLU, Dropout, SpatialDropout2D from tensorflow.keras.applications import VGG16 input_shape = (720, 1280, 3) with strategy.scope(): # Init VGGLayer = VGG16(weights="imagenet", include_top=False, input_tensor=Input(shape=input_shape)) for layer in VGGLayer.layers: layer.trainable = False spatialDNN_Conv = Conv2D(128, 3, kernel_regularizer='l2', activation=LeakyReLU(alpha=0.1)) spatialDNN_SpatialDropout = SpatialDropout2D(0.2) spatialDNN_MaxPool = MaxPooling2D(2) temporalDNN_Conv = Conv2D(384, 3, kernel_regularizer='l2', activation=LeakyReLU(alpha=0.1))
# > augmentation prevents overfitting # ### 4. Try VGG16 # In[21]: from tensorflow.keras import optimizers from tensorflow.keras.applications import VGG16 from tensorflow.keras.models import Model from tensorflow.keras.layers import GlobalAveragePooling2D image_shape = (224, 224, 3) pre_trained_model = VGG16( input_shape=image_shape, include_top=False, weights="imagenet" ) for i, layer in enumerate(pre_trained_model.layers): layer.trainable = i > 20 last_layer = pre_trained_model.get_layer("block5_pool") last_output = last_layer.output x = GlobalAveragePooling2D()(last_output) x = Dense(512, activation="relu")(x) x = Dropout(0.5)(x) x = Dense(1, activation="sigmoid")(x) model = Model(pre_trained_model.input, x)
from tensorflow.keras.applications import VGG16 from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Flatten vgg16 = VGG16(weights='imagenet', include_top=False, input_shape=(32, 32, 3)) # include_top : False로 해야 input_shape를 원하는 사이즈로 가능 print(vgg16.weights) vgg16.trainable = False vgg16.summary() print(len(vgg16.weights)) # 26 print(len(vgg16.trainable_weights)) # 0 model = Sequential() model.add(vgg16) model.add(Flatten()) model.add(Dense(10)) model.add(Dense(5)) model.add(Dense(1)) model.summary() print("그냥 가중치의 수 :", len(model.weights)) # 26 -> 32 print("동결하기 전 훈련되는 가중치의 수 :", len(model.trainable_weights)) # 0 -> 6 ######################################################################################## import pandas as pd pd.set_option('max_colwidth', -1)
### Pre-trained model Next thing that we could try is to use a pre-trained model that has its parameters already optimised using some other dataset. Usually, CNNs related to computer vision are pre-trained using Imagenet data (http://www.image-net.org/). It is a vast collection of labelled images. We add our own layers after the pre-trained architecture. As our pre-trained model, we use VGG16 ![VGG16](./images/vgg.png) VGG16 is included in the **keras.applications** -module from tensorflow.keras.applications import VGG16 When we load the VGG16 model, we need to set **weights=imagenet** to get pre-trained parameter weights. **include_top=False** removes the output layer with 1000 neurons. We want our output layer to have only one neuron (prediction for dog/cat). pretrained_base = VGG16(weights='imagenet',include_top=False,input_shape=(150, 150, 3)) VGG16 has 14.7 million parameters without the last layer. It also has two or three convolutional layers in a row. Our previous models were switching between a convolutional layer and a max-pooling layer. pretrained_base.summary() model = models.Sequential() When we construct the model, we add the pre-trained VGG16-base first. Then follows a 256-neuron Dense-layer and a one-neuron output layer. model.add(pretrained_base) model.add(layers.Flatten()) model.add(layers.Dense(256, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) Overall, our model has almost 17 million parameters. However, we will lock the pre-trained VGG16 base, which will decrease the number of trainable parameters significantly.
pred = model.predict(test) print(confusion_matrix(test.classes, pred > 0.5)) pd.DataFrame(classification_report(test.classes, pred > 0.5, output_dict=True)) print(confusion_matrix(test.classes, pred > 0.7)) pd.DataFrame(classification_report(test.classes, pred > 0.7, output_dict=True)) # 使用 VGG16 模板 #from keras.models import Sequential #from keras.layers import GlobalAveragePooling2D from tensorflow.keras.applications import VGG16 vgg16_base_model = VGG16(input_shape=(180, 180, 3), include_top=False, weights='imagenet') vgg16_base_model.summary() vgg16_model = tf.keras.Sequential([ vgg16_base_model, GlobalAveragePooling2D(), Dense(512, activation='relu'), BatchNormalization(), Dropout(0.6), Dense(128, activation='relu'), BatchNormalization(), Dropout(0.4), Dense(64, activation='relu'), BatchNormalization(),
def build_model(model_name, img_size, num_classes): """ Build model. Arguments model_name : (str) model name, which is used to select model. img_size : (int) image size for both width and height for modeling. num_classes : (int) number of classes for the last layer of the model. Returns model : a tensorflow model object. """ image_shape = (img_size, img_size, 3) # Initialize model model = models.Sequential() # Load model if model_name == 'dummy': model.add( layers.MaxPooling2D(pool_size=(4, 4), input_shape=image_shape)) else: print('[KF INFO] Loading pre-trained model ...') if model_name == 'VGG16': if img_size > 224: raise Exception( "[KF ERROR] For %s model, the input image size cannot be larger than 224!" % model_name) conv = VGG16(weights='imagenet', include_top=False, input_shape=image_shape) elif model_name == 'VGG19': if img_size > 224: raise Exception( "[KF ERROR] For %s model, the input image size cannot be larger than 224!" % model_name) conv = VGG19(weights='imagenet', include_top=False, input_shape=image_shape) elif model_name == 'InceptionResNetV2': if img_size > 299: raise Exception( "[KF ERROR] For %s model, the input image size cannot be larger than 299!" % model_name) conv = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=image_shape) elif model_name == 'InceptionV3': if img_size > 299: raise Exception( "[KF ERROR] For %s model, the input image size cannot be larger than 299!" % model_name) conv = InceptionV3(weights='imagenet', include_top=False, input_shape=image_shape) elif model_name == 'ResNet50': if img_size > 224: raise Exception( "[KF ERROR] For %s model, the input image size cannot be larger than 224!" % model_name) conv = ResNet50(weights='imagenet', include_top=False, input_shape=image_shape) else: raise Exception("[KF ERROR] Cannot load the pre-trained model! ") print( "[KF INFO] The pretrained model %s's convolutional part is loaded ..." % model_name) model.add(conv) # Add top layers fc_size = 256 model.add(layers.Flatten()) model.add(layers.BatchNormalization()) model.add(layers.Dense(fc_size, activation='relu')) model.add(layers.Dropout(0.5)) model.add(layers.Dense(num_classes, activation='softmax')) return model
img_resized = resize(img, (204, 204)) y_data[i] = train_data[i, 0] x_data[i, :, :, 0] = img_resized.astype(int) x_data[i, :, :, 1] = img_resized.astype(int) x_data[i, :, :, 2] = img_resized.astype(int) x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.2, random_state=42) y_train = OneHotEncoder().fit_transform(y_train.reshape(-1, 1)).toarray() y_test = OneHotEncoder().fit_transform(y_test.reshape(-1, 1)).toarray() base_model = VGG16(include_top=False, weights='vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', input_shape=(204, 204, 3)) base_model.trainable = False inputs = tf.keras.Input(shape=(204, 204, 3)) x = base_model(inputs) x = tf.keras.layers.Flatten()(x) x = tf.keras.layers.Dense(256, activation='relu')(x) outputs = tf.keras.layers.Dense(2, activation='softmax')(x) model = keras.Model(inputs, outputs) model.summary() model.compile(optimizer=tf.keras.optimizers.SGD(), loss="binary_crossentropy", metrics=["accuracy"])
history2 = history(model_two, 100, 100, 50) #saving second model model_two.save('cat_dog_mini_two2.h5') #plot accuracy and loss acc_vloss(history2) #Using a pretrained model from tensorflow.keras.applications import VGG16 #instantiating VGG16 convolutional base with 14.7 mil parameters convolutional_base = VGG16(weights = 'imagenet', include_top = False, input_shape = (150, 150, 3)) convolutional_base.summary() """Adding convolutionaal base model to neural network without data augmentation for demo purposes""" #extracting features with pretrained model #recording the convolutional base output to np.array #datagen = ImageDataGenerator(rescale=1./255) #batch_size = 20 #def extract_features(directory, sample_count): #features = np.zeros(shape=(sample_count, 4, 4, 512)) #labels = np.zeros(shape=(sample_count))
from tensorflow.keras.applications import VGG16 import argparse import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true' ap = argparse.ArgumentParser() ap.add_argument("-i", "--include-top", type=int, default=1, help="whether or not to include top of CNN") args = vars(ap.parse_args()) #load the VGG16 network print("[INFO] loading network...") model = VGG16(weights="imagenet", include_top=args["include_top"] > 0) print("[INFO] showing layers") # loop over the layers in network and display them to the console for (i, layer) in enumerate(model.layers): print("[INFO] {}\t{}".format(i, layer.__class__.__name__))
validation_generator = input_imgen.flow_from_directory( train_dir, target_size=(img_height, img_width), class_mode="categorical", batch_size=batch_size, shuffle=True, subset='validation') print('training steps: ', train_generator.get_steps_per_epoch()) print('validation steps: ', validation_generator.samples // batch_size) # Build a model for transfer learning. # Load a pre-trained conv base model. conv_base = VGG16(weights='imagenet', include_top=False, input_shape=(img_height, img_width, 3)) # Add classification layers. model = models.Sequential() model.add(conv_base) model.add(layers.Flatten()) model.add(layers.Dense(256, activation='relu')) model.add(layers.Dense(4, activation='sigmoid')) # Freeze the cov base model, # only update classification layers weights during training. conv_base.trainable = False model.compile(optimizer=optimizers.RMSprop(lr=2e-5), loss='binary_crossentropy', metrics=['acc'])
train_labels = np.load('Train_Data/labels_train.npy') print('Training Data Loaded Successfully...') train_images,train_labels = shuffle(train_images,train_labels) ''' Initializing the VGG16 with imagenet pretrained weights, with out including top 3 layers, Addition of custom layers and All layers of VGG16 is set to trainable''' class myCallback(Callback): def on_epoch_end(self, epoch, logs={}): if(logs.get('accuracy') is not None and logs.get('accuracy')>=0.95): print("\nReached 95% accuracy so cancelling training!") self.model.stop_training = True callbacks = myCallback() model = VGG16(input_shape=(SIZE, SIZE, 3),weights='imagenet', include_top=False) for layer in model.layers: layer.trainable=True #setting VGG Layers to trainable output = model.output output = Flatten()(output) output = Dense(500,activation='relu')(output) output = Dense(50,activation='relu')(output) finallayer = Dense(4, activation='softmax')(output) #Here the number of output classes are 4, initialize with yours updated_model = Model(inputs = model.input, outputs=finallayer) updated_model.compile(optimizer = Adam(lr = 1e-4), loss = 'categorical_crossentropy', metrics = ['accuracy']) history = updated_model.fit(train_images,train_labels, validation_split=0.1,epochs=50, batch_size=50,verbose=2, shuffle=True,callbacks=[callbacks])
def __init__(self): # load the VGG16 network pre-trained on the ImageNet dataset print("[INFO] loading network...") self.model = VGG16(weights="imagenet")
labels = np.array(labels) lb = LabelBinarizer() labels = lb.fit_transform(labels) labels = to_categorical(labels) (trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.20, stratify=labels, random_state=42) trainAug = ImageDataGenerator(rotation_range=15, fill_mode="nearest") baseModel = VGG16(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3))) headModel = baseModel.output headModel = AveragePooling2D(pool_size=(4, 4))(headModel) headModel = Flatten(name="flatten")(headModel) headModel = Dense(64, activation="relu")(headModel) headModel = Dropout(0.5)(headModel) headModel = Dense(2, activation="softmax")(headModel) model = Model(inputs=baseModel.input, outputs=headModel) for layer in baseModel.layers: layer.trainable = False opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
import tensorflow as tf from tensorflow.keras.applications import VGG16 vgg_conv = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) vgg_conv.summary()
model.add(MaxPooling2D()) model.add(Conv2D(128, (3, 3), activation="relu", padding="same")) model.add(MaxPooling2D()) model.add(Flatten()) model.add(Dense(1024, activation="relu")) model.add(Dropout(0.5)) model.add(Dense(256, activation="relu")) model.add(Dropout(0.2)) model.add(Dense(len(LABELS), activation="softmax")) base_model = VGG16(weights='imagenet', include_top=False) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(512, activation='relu')(x) preds = Dense(len(LABELS), activation='softmax')(x) model = Model(inputs=base_model.input, outputs=preds) for layer in model.layers[:-5]: layer.trainable = False for layer in model.layers[-5:]: layer.trainable = True
def get_model(global_version=True): """ Return the parallelized VGG16 for the multi-patch subnet Inputs: - global_version (bool): True when training only the multi-path subnet Outputs: - model: keras model """ K.clear_session() param = get_param() input_shape = (param['nb_crops'], param['img_size'], param['img_size'], param['nb_channels']) vgg16 = VGG16(include_top=False, weights='imagenet', input_shape=(param['img_size'], param['img_size'], param['nb_channels'])) flatten = Flatten()(vgg16.output) fc1_vgg = Dense(4096, activation='relu', name='fc1_vgg')(flatten) dropout_vgg = Dropout(rate=0.5, name='dropout_vgg')(fc1_vgg) fc2_vgg = Dense(4096, activation='relu', name='fc2_vgg')(dropout_vgg) backbone = Model(vgg16.input, fc2_vgg, name='backbone') backbone.trainable = True for idx, layer in enumerate(backbone.layers): if idx < 15: layer.trainable = False else: if isinstance(layer, Conv2D) or isinstance(layer, Dense): layer.add_loss(lambda: l2(param['weight_decay_coeff']) (layer.kernel)) if hasattr(layer, 'bias_regularizer') and layer.use_bias: layer.add_loss(lambda: l2(param['weight_decay_coeff']) (layer.bias)) inputs = Input(shape=input_shape, name='input') in1, in2, in3, in4, in5 = inputs[:, 0], inputs[:, 1], inputs[:, 2], inputs[:, 3], inputs[:, 4] out1, out2, out3, out4, out5 = backbone(in1), backbone(in2), backbone( in3), backbone(in4), backbone(in5) # agregation agg_avg = Average(name='average')([out1, out2, out3, out4, out5]) agg_max = Maximum(name='max')([out1, out2, out3, out4, out5]) agg = concatenate([agg_avg, agg_max], name='agregation_layer') fc1 = Dense(units=4096, activation='relu', name='fc1')(agg) dropout = Dropout(rate=0.5, name='dropout')(fc1) fc2 = Dense(units=4096, activation='relu', name='fc2')( dropout) # output of the multi-patch subnet in the A-Lamp model if global_version: out = fc2 else: out = Dense(units=1, activation='sigmoid', name='predictions')( fc2) # we first train the multi-patch subnet alone model = Model(inputs=inputs, outputs=out) for layer in model.layers: if isinstance(layer, Conv2D) or isinstance(layer, Dense): layer.add_loss(lambda: l2(param['weight_decay_coeff']) (layer.kernel)) if hasattr(layer, 'bias_regularizer') and layer.use_bias: layer.add_loss(lambda: l2(param['weight_decay_coeff'])(layer.bias)) return model