path_to_train = os.path.join(path_to_split_datasets, "train") path_to_validation = os.path.join(path_to_split_datasets, "validation") # get number of classes sub_dirs = [ sub_dir for sub_dir in os.listdir(path_to_train) if os.path.isdir(os.path.join(path_to_train, sub_dir)) ] num_classes = len(sub_dirs) # parameters for CNN if use_vgg: base_model = VGG(include_top=False, weights=None, input_shape=(64, 64, 3)) else: base_model = DenseNet(include_top=False, weights=None, input_shape=(64, 64, 3)) # add a global spatial average pooling layer top_model = base_model.output top_model = GlobalAveragePooling2D()(top_model) # or just flatten the layers # top_model = Flatten()(top_model) # let's add a fully-connected layer if use_vgg: # only in VGG19 a fully connected nn is added for classfication # DenseNet tends to overfitting if using additionally dense layers top_model = Dense(2048, activation='relu')(top_model) top_model = Dense(2048, activation='relu')(top_model) # and a logistic layer predictions = Dense(num_classes, activation='softmax')(top_model)
import numpy as np import tensorflow.keras as keras from tensorflow.keras.layers import Dense, Dropout, GlobalAveragePooling2D, BatchNormalization from tensorflow.keras.applications.densenet import DenseNet121 as DenseNet from model_utils import SequentialConstructor, evaluate, pretty_print_history model = SequentialConstructor([ DenseNet(weights='imagenet', include_top=False), GlobalAveragePooling2D(), BatchNormalization(), Dropout(0.5), Dense(512, activation='relu'), Dropout(0.5), Dense(256, activation='relu'), Dropout(0.5) ], output_shape=(2, )) hist = evaluate(model, model_name='densenet121-binary', train_dir='data/proc/binary/train/224/') pretty_print_history(hist)
# get number of classes sub_dirs = [ sub_dir for sub_dir in os.listdir(path_to_train) if os.path.isdir(os.path.join(path_to_train, sub_dir)) ] num_classes = len(sub_dirs) # parameters for CNN if use_vgg: base_model = VGG(include_top=False, weights='imagenet', input_shape=(64, 64, 3)) else: base_model = DenseNet(include_top=False, weights='imagenet', input_shape=(64, 64, 3)) # add a global spatial average pooling layer top_model = base_model.output top_model = GlobalAveragePooling2D()(top_model) # or just flatten the layers # top_model = Flatten()(top_model) # let's add a fully-connected layer if use_vgg: # only in VGG19 a fully connected nn is added for classfication # DenseNet tends to overfitting if using additionally dense layers top_model = Dense(2048, activation='relu')(top_model) top_model = Dense(2048, activation='relu')(top_model) # and a logistic layer predictions = Dense(num_classes, activation='softmax')(top_model)
input_tensor = Conv2D(3, (1, 1))(input_tensor) if use_vgg: base_model_imagenet = VGG(include_top=False, weights='imagenet', input_shape=(64, 64, 3)) base_model = VGG(include_top=False, weights=None, input_tensor=input_tensor) for i, layer in enumerate(base_model_imagenet.layers): # we must skip input layer, which has no weights if i == 0: continue base_model.layers[i+1].set_weights(layer.get_weights()) else: base_model_imagenet = DenseNet(include_top=False, weights='imagenet', input_shape=(64, 64, 3)) base_model = DenseNet(include_top=False, weights=None, input_tensor=input_tensor) for i, layer in enumerate(base_model_imagenet.layers): # we must skip input layer, which has no weights if i == 0: continue base_model.layers[i+1].set_weights(layer.get_weights()) # add a global spatial average pooling layer top_model = base_model.output top_model = GlobalAveragePooling2D()(top_model) # or just flatten the layers # top_model = Flatten()(top_model)