def _get_backbone_model(self): backbone_model = None if self.backbone_name == 'resnet18': backbone_model = ResNet18(input_shape=self.input_shape, include_top=False, weights='imagenet') if self.backbone_name == 'resnet34': backbone_model = ResNet34(input_shape=self.input_shape, include_top=False, weights='imagenet') elif self.backbone_name == 'mobilenetv2': backbone_model = MobileNetV2(input_shape=self.input_shape, include_top=False, weights='imagenet') elif self.backbone_name == 'efficientnetb0': backbone_model = EfficientNetB0(input_shape=self.input_shape, include_top=False, weights='imagenet') elif self.backbone_name == 'efficientnetb1': backbone_model = EfficientNetB1(input_shape=self.input_shape, include_top=False, weights='imagenet') elif self.backbone_name == 'efficientnetb3': backbone_model = EfficientNetB3(input_shape=self.input_shape, include_top=False, weights='imagenet') elif self.backbone_name == 'efficientnetb5': backbone_model = EfficientNetB5(input_shape=self.input_shape, include_top=False, weights='imagenet') elif self.backbone_name == 'densenet': backbone_model = DenseNet121(input_shape=self.input_shape, include_top=False, weights='imagenet') return backbone_model
def __init__(self, model_size , imag_size, num_cls): try: if model_size == "B0": model = EfficientNetB0(weights = 'imagenet', input_shape = (imag_size,imag_size,3), include_top = False) elif model_size == "B3": model = EfficientNetB3(weights = 'imagenet', input_shape = (imag_size,imag_size,3), include_top = False) elif model_size == "B5": model = EfficientNetB5(weights = 'imagenet', input_shape = (imag_size,imag_size,3), include_top = False) elif model_size == "B7": model = EfficientNetB7(weights = 'imagenet', input_shape = (imag_size,imag_size,3), include_top = False) ENet_out = model.output ENet_out = Flatten()(ENet_out) Hidden1_in = Dense(1024, activation="relu")(ENet_out) Hidden1_in = Dropout(0.5)(Hidden1_in) predictions = Dense(units = num_cls, activation="softmax")(Hidden1_in) self.model_f = Model(input = model.input, output = predictions) self.model_f.compile(optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08), loss='categorical_crossentropy', metrics=[metrics.mae, metrics.categorical_accuracy]) except: print("only B0/B3/B5/B7 allowed")
def get_model(self, class_names, model_name="EfficientNetB3", use_base_weights=True, weights_path=None, input_shape=None): if use_base_weights is True: base_weights = "imagenet" else: base_weights = None if input_shape is None: input_shape = self.models_[model_name]["input_shape"] img_input = Input(shape=input_shape) base_model = EfficientNetB3(include_top=False, input_tensor=img_input, input_shape=input_shape, weights=base_weights, pooling="avg") x = base_model.output predictions = Dense(len(class_names), activation="sigmoid", name="predictions")(x) model = Model(inputs=img_input, outputs=predictions) if weights_path == "": weights_path = None if weights_path is not None: print(f"load model weights_path: {weights_path}") model.load_weights(weights_path) return model
target_size=(img_height, img_width), batch_size=n_batch_size, shuffle=True, subset='training') valid_generator = train_datagen.flow_from_directory('./train/data', target_size=(img_height, img_width), batch_size=n_batch_size, shuffle=True, subset='validation') # 以訓練好的 EfficientNetB3 為基礎來建立模型 net = EfficientNetB3(input_shape=(img_height, img_width, 3), include_top=False, weights='imagenet', pooling='max') # 增加 Dense layer 與 softmax 產生個類別的機率值 x = net.output # x = Dense(1024, activation='relu')(x) # x = Dropout(0.5)(x) # x = Dense(128, activation='relu')(x) output_layer = Dense(3, activation='softmax', name='softmax')(x) # 設定要進行訓練的網路層 model = Model(inputs=net.input, outputs=output_layer) print('\n') print('Trainable layers:') for x in model.trainable_weights:
def trainModel(self): print(os.listdir(self.__data_directory)) class_nb = len(os.listdir(self.__data_directory)) train_generator = self.__image_generator.flow_from_directory( directory = self.__data_directory, target_size=(self.__image_size,self.__image_size), subset="training", batch_size=self.__batch_size, shuffle=True, class_mode= "categorical", seed=42 ) val_generator = self.__image_generator.flow_from_directory( directory = self.__data_directory, target_size=(self.__image_size,self.__image_size), subset="validation", batch_size=self.__batch_size, shuffle=True, class_mode= "categorical", seed=42 ) if self.__model_type== "EfficientNetB3": pretrainedModel = EfficientNetB3( weights='imagenet', input_shape=(self.__image_size, self.__image_size, 3), include_top=False, pooling='max' ) else : raise Exception('{} model is not a member of our available pretrained models'.format(self.__model_type)) model = Sequential() model.add(pretrainedModel) model.add(Dense(units = 120, activation='relu')) model.add(Dense(units = 120, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(units = class_nb, activation='softmax')) model.summary() model.compile( optimizer=Adam(lr=0.0001, clipnorm=1, clipvalue=5), loss='categorical_crossentropy', metrics=['accuracy'] ) logdir = "logs/" + datetime.now().strftime("%Y%m%d-%H%M%S")+ "_" + self.__model_name callbacks = [ #EarlyStopping(monitor='val_loss', patience= 10), ModelCheckpoint(filepath= self.__model_name + '_best_model.h5', monitor='val_loss', save_best_only=True), TensorBoard(log_dir=logdir)] history = model.fit_generator( train_generator, epochs = self.__epoch_number, #steps_per_epoch = 20, validation_data = val_generator, #validation_steps = 5, callbacks = callbacks ) acc = history.history['accuracy'] val_acc = history.history['val_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(1,len(acc) + 1) fig=plt.figure(figsize=(15, 5)) fig.add_subplot(1, 2, 1) plt.plot(epochs,acc,'g',label = 'Training Accuracy') plt.plot(epochs,val_acc,'r',label = 'Validation Accuracy') plt.title('Training and Validation Accuracy') plt.legend() fig.add_subplot(1, 2, 2) plt.plot(epochs,loss,'g',label = 'Training loss') plt.plot(epochs,val_loss,'r',label = 'Validation Loss') plt.title('Training and Validation Loss') plt.legend() plt.show() # serialize weights to HDF5 model.save(self.__model_name +'.h5') print("Saved final model : " + self.__model_name +'.h5') print("Saved best model considering val_loss : " + self.__model_name + '_best_model.h5')
def test_efficientnet(self): model = EfficientNetB3(weights=None) vis = KerasArchitectureVisualizer() vis.visualize(model)
batch_size=128, shuffle=True, class_mode="binary") val_generator = datagen.flow_from_dataframe(dataframe=df, directory=data_dir, x_col="filename", y_col="category", target_size=(32, 32), subset="validation", batch_size=64, shuffle=True, class_mode="binary") efficient_net = EfficientNetB3(weights='imagenet', input_shape=(32, 32, 3), include_top=False, pooling='max') model = Sequential() model.add(efficient_net) model.add(Dense(units=120, activation='relu')) model.add(Dense(units=120, activation='relu')) model.add(Dense(units=1, activation='sigmoid')) model.summary() model.compile(optimizer=Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy']) filename = './checkpoints/efficient-weights.h5' log_dir = "./logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")